import astropy
import math
import numpy as np
from astropy.io import fits
hdu_list = fits.open('grid.cam.meas.fits')
image_data = hdu_list[0].data

# these are all FITS images with very simple coordinates: RA = X - 180, DEC = Y - 90, with pixels of 
# first [ ] is the dec
# second [ ] is the ra

total = np.nansum(image_data)

decslice = np.nansum(image_data,1, dtype='double')
#print decslice.shape
#for i in range(0,180):
#    print i, decslice[i]

datamachines = ['ipp100.0','ipp100.1' , 'ipp101.0', 'ipp101.1','ipp102.0','ipp102.1','ipp103.0','ipp103.1','ipp104.0','ipp104.1']
numdata = len(datamachines)
dmlen = len(datamachines)
dmcnt =0 
#decboundaries from email (see end)

decboundaries=[-28,-26,-24,-21,-19,-17,-14,-12,-9,-7,-4,-1,1,4,6,9,12,15,18,21,25,28,31,35,38,42,46,50,55,60,67,90]

image_data_decfix = np.empty_like (image_data)
image_data_decfix[:] = image_data

for d in range (0,180):
    conv = math.cos((d-90)*math.pi/180.)
    image_data_decfix[d,:]=image_data[d,:]*conv
    
    
decslice2 = np.nansum(image_data_decfix,1,dtype='double')
for i in range (0,180):
    print decslice[i], decslice2[i]


image_data [:] = image_data_decfix
    
decmin = -35
xmin = decmin + 90
#print "full total", np.nansum(image_data,dtype='double')
actualtotal = np.nansum(image_data[xmin:180,0:360], dtype='double')
#ok! I figured it out: np.nansum[x:x] gives 0, so to get a 1 pixel slice, need [x:x+1]
#print "total between -55 and 90 dec " , actualtotal
#print "other total between -55 and 90dec ", np.nansum(decslice[decmin:180],dtype='double')
j = 0
sumtotal = 0
slicecnt =0
for dec in decboundaries:
    i = dec+90
    if dec == 90:
        j = 1 
    if j == 0:
        imin = decmin+90
        j  = 1
    else:
        j = 0
        sumcheck = np.nansum(image_data[imin:i],dtype='double')
        #print imin, i, decmin, dec, sumcheck
        decmin = dec
        sumtotal = sumtotal + sumcheck
        segsize = sumcheck/4.
        jj =0
        sumtotalra = 0
        sumcheckra = 0
        armin = 0
        armax = 0
        newsumcheckra = 0 
        for ar in range (1,361):
            #note, this is not how gene defined it, however, he's completely wrong I think...
            oldsumcheckra = newsumcheckra
            newsumcheckra = np.nansum(image_data[imin:i,armin:ar],dtype='double')
      #     print "check",imin, i, armin, ar, armax, sumcheckra, newsumcheckra, segsize
#            if ar == 360:
#                print newsumcheckra
            if (newsumcheckra > segsize or ar == 360):
                # print the previous one
                
                armax = ar
#                print imin, i, armin, armax, newsumcheckra, "here"
#                print 360-armax, 360-armin, imin-90, i-90
                ramin= 360-armax
                ramax = 360-armin
                decccmin=imin-90
                decccmax = i-90
                PSPS = "PSPS_PV3_OB_ra"+str(360-armax)+"to"+str(360-armin)+"dec"+str(imin-90)+"to"+str(i-90)
                PSPS2 = "PSPS_PV3_P2_SLICE_"+str(slicecnt)
                
                print str(ramin).ljust(5), str(ramax).ljust(5),str(decccmin).ljust(5), str(decccmax).ljust(5),PSPS2,newsumcheckra, datamachines[dmcnt]
                dmcnt = ( dmcnt + 1 ) % dmlen
                sumtotalra = newsumcheckra + sumtotalra
#                sumtotalra = newsumcheckra
                armin = ar
                sumcheckra = 0
                slicecnt=slicecnt+1
#                sumcheckra = np.nansum(image_data[imin:i][armin:ar],dtype='double')
                #calnew
            else:
                
                sumcheckra = newsumcheckra
       # print "total ra sizes for decmin: " + str(decccmin)+", decmax: "+str(decccmax)+" expected: " + str(sumcheck)+" sum of slices: "+ str(sumtotalra)
        #print "--------"
#print "these should match" ,sumtotal, actualtotal        

#decd
for i in range(0,180):
    for j in range(0,360):
	ra = j - 180
       	dec = i - 90	
	#print ra, dec, image_data[i][j]
	
#Dec Boundaries from email:
#-54.82   3.02e+12
#-28.68   3.44e+12
#-26.41   3.45e+12
#-24.12   3.47e+12
#-21.88   3.47e+12
#-19.55   3.45e+12
#-17.20   3.47e+12
#-14.78   3.45e+12
#-12.24   3.44e+12
# -9.64   3.49e+12
# -7.00   3.45e+12
# -4.29   3.44e+12
# -1.40   3.47e+12
# +1.38   3.47e+12
# +4.13   3.45e+12
# +6.91   3.48e+12
# +9.78   3.46e+12
#+12.70   3.48e+12
#+15.72   3.43e+12
#+18.79   3.44e+12
#+21.93   3.44e+12
#+25.24   3.45e+12
#+28.59   3.42e+12
#+31.95   3.46e+12
#+35.44   3.44e+12
#+38.98   3.46e+12
#+42.73   3.45e+12
#+46.73   3.46e+12
#+50.90   3.45e+12
#+55.41   3.48e+12
#+60.62   3.46e+12
#+67.83   3.42e+12
#+90.00        nan
