import matplotlib.pylab as plt
import matplotlib.ticker as mtick
import sys
from matplotlib import rc

# Save the plot?
save=False
# Files To Open
File='SoftAP_N50'
Dat=File+'_Data.dat'
Image = Dat +'_TimestepPlot'	# Name of the Image to Save

# Customisation variables
s=3			# Marker size
lw=1		# Line width
txt=16 		# Plot label text size
lblpad=10		# Plot label padding
tick=12		# Tick text size
tickh=12		# Tick size
pad=1			# Tick padding

# Open the output file 
f = open(Dat,'r')
# read all lines
line1 = f.readline()		# Parameters
lines = f.readlines()	# Data
#Close the file
f.close()

paras = line1.split()
# Number of bodies to plot
N=int(paras[0])
# Dynamical timescale
tdyn=float(paras[2])
#Number of data points
dp = len(lines)

# Read data and make plots
t=[0 for i in xrange(dp)]
dt=[0 for i in xrange(dp)]

print '=====Reading Data====='
for i,line in zip(xrange(dp),lines):
	data = line.split()
	t[i]=float(data[-3])/tdyn
	dt[i]=float(data[-2])
print '======Data Read======='
print '===Generating Plots==='

dtmin = min(dt)-max(dt)/50.
dtmax = max(dt)+max(dt)/50.
tmin  = min(t)-max(t)/50.
tmax  = max(t)+max(t)/50.

# Customisation
plt.rc('path',simplify=True)
plt.rc('savefig',dpi=300,format='png')
plt.rc('axes',linewidth=lw,labelpad=lblpad)
plt.rc('xtick',labelsize=tick,direction='inout')
plt.rc('xtick.major',size=tickh,width=lw,pad=pad)
plt.rc('ytick',labelsize=tick,direction='inout')
plt.rc('ytick.major',size=tickh,width=lw,pad=pad)

# Set up the plot
plt.figure(figsize=(24,12))

ax=plt.axes(xlim=(tmin,tmax),ylim=(dtmin,dtmax))
plot=ax.plot(t,dt,marker='.',linestyle='none',markersize=s)
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))
ax.axhline(y=min(dt),linestyle='dashed',color='k',lw=lw)
#ax.set_yscale('log')
#ax.set_xscale('log')

# Set axes labels
plt.xlabel('t/tdyn',size=txt)
plt.ylabel('dt [Gyr]',size=txt)

# Set plot titles	
ax.set_title('Timestep',size=1.5*txt)

print '====Plots Generated==='
# Save the figure
if (save):
	plt.savefig('/home/echo/cbm7/Summer_Project/Graphs/'+Image)
	print '======Image Saved====='
else:
	# Show the Figure
	plt.show()

