import scipy.stats as sp
import numpy as np
import matplotlib.pyplot as plt
import sys

filename=sys.argv[1]
outfile=sys.argv[2]
title=sys.argv[3]
raColNo=sys.argv[4]
decColNo=sys.argv[5]
colColNo=sys.argv[6]

title_font = {'size':'10'}

print filename
data = np.genfromtxt(filename)

#ra=data[:,6]
#dec=data[:,7]
#ymj=data[:,10]
#jmk=data[:,11]
#col=jmk

ra=data[:,raColNo]
dec=data[:,decColNo]
col=data[:,colColNo]



print "RA ",ra
print "DEC ",dec
print "COL ",col

st,x_edge,y_edge,binnumber=sp.binned_statistic_2d(dec,ra,col,'median',32)


print 'MEDIAN ',st

                                                        

#H,xedges,yedges=np.histogram2d(dec,ra,bins=(32,32))
#TC,xedges,yedges=np.histogram2d(dec,ra,bins=(32,32),weights=col)

#C=np.where(np.greater(H,0),np.divide(TC,H),0.)

#print "CCCC ", C

#MA=np.ma.masked_where(C == 0, C)

cmap = plt.cm.jet
cmap.set_bad('w',1.)

print "XEDGE ", x_edge[0],x_edge[-1]
print "YEDGE ", y_edge[0],y_edge[-1]
#print H.shape, xedges.shape, yedges.shape
extent = [y_edge[0], y_edge[-1], x_edge[0], x_edge[-1]]
print "EXTENT ",extent
plt.imshow(st, extent=extent,origin='upper', aspect='auto', interpolation='nearest', vmin=-0.5, vmax=0.5, cmap=cmap)
plt.colorbar()
plt.title(title,**title_font)
#plt.gca().invert_yaxis()
#plt.gca().invert_xaxis()
#plt.show()
plt.savefig(outfile)
#print H
#print TC
#print C
#print MA
