#from numarray import * ## Changed to numpy Aug 2007
from numpy import *
import pyfits
from simpler_skymap_datons import *

insumfilestuples = [
('NewCompDataPaddedIntTime22.0.060604.fits', 'NewCompData8by8IntTime22.0.060604.fits'),
('NewCompDataPaddedIntTime242.0.060604.fits', 'NewCompData8by8IntTime242.0.060604.fits'),
('NewCompDataPaddedIntTime7.0.060604.fits', 'NewCompData8by8IntTime7.0.060604.fits'),
('NewCompDataPaddedIntTime73.0.060604.fits', 'NewCompData8by8IntTime73.0.060604.fits'),
('NewCompDataPaddedIntTime806.0.060604.fits', 'NewCompData8by8IntTime806.0.060604.fits'),
('NewCompPoisDatonPaddedIntTime22.0.060604.fits', 'NewCompPoisDaton8by8IntTime22.0.060604.fits'),
('NewCompPoisDatonPaddedIntTime242.0.060604.fits', 'NewCompPoisDaton8by8IntTime242.0.060604.fits'),
('NewCompPoisDatonPaddedIntTime7.0.060604.fits', 'NewCompPoisDaton8by8IntTime7.0.060604.fits'),
('NewCompPoisDatonPaddedIntTime73.0.060604.fits', 'NewCompPoisDaton8by8IntTime73.0.060604.fits'),
('NewCompPoisDatonPaddedIntTime806.0.060604.fits', 'NewCompPoisDaton8by8IntTime806.0.060604.fits'),
('NewSumPoisDatonPaddedIntTime22.0.060604.fits', 'NewSumPoisDaton8by8IntTime22.0.060604.fits'),
('NewSumPoisDatonPaddedIntTime242.0.060604.fits', 'NewSumPoisDaton8by8IntTime242.0.060604.fits'),
('NewSumPoisDatonPaddedIntTime7.0.060604.fits', 'NewSumPoisDaton8by8IntTime7.0.060604.fits'),
('NewSumPoisDatonPaddedIntTime73.0.060604.fits', 'NewSumPoisDaton8by8IntTime73.0.060604.fits'),
('NewSumPoisDatonPaddedIntTime806.0.060604.fits', 'NewSumPoisDaton8by8IntTime806.0.060604.fits'),
('PoisDatonPaddedIntTime22.0.060604.fits', 'PoisDaton8by8IntTime22.0.060604.fits'),
('PoisDatonPaddedIntTime242.0.060604.fits', 'PoisDaton8by8IntTime242.0.060604.fits'),
('PoisDatonPaddedIntTime7.0.060604.fits', 'PoisDaton8by8IntTime7.0.060604.fits'),
('PoisDatonPaddedIntTime73.0.060604.fits', 'PoisDaton8by8IntTime73.0.060604.fits'),
('PoisDatonPaddedIntTime806.0.060604.fits', 'PoisDaton8by8IntTime806.0.060604.fits')
]
insumfilestuples = [
('ModelDataPaddedIntTime7.0.060529.fits', 'ModelData8by8IntTime7.0.060529.fits'), 
('ModelDataPaddedIntTime22.0.060529.fits', 'ModelData8by8IntTime22.0.060529.fits'), 
('ModelDataPaddedIntTime73.0.060529.fits', 'ModelData8by8IntTime73.0.060529.fits'), 
('ModelDataPaddedIntTime242.0.060529.fits', 'ModelData8by8IntTime242.0.060529.fits'), 
('ModelDataPaddedIntTime806.0.060529.fits', 'ModelData8by8IntTime806.0.060529.fits')
]

insumfilestuples = [
('/AstroData/counts_g1-9_g004.fits', 'counts_g1-9_g004gt1GeVrebinnd.fits', 'counts_g1-9_g004gt1GeVpadded.fits')
]

WantFlxRebin = [6,4]
#WantExpRebin = [16,16]
IEBIN = 1
for this_tuple in insumfilestuples:
#   1.1/ Get input file to be summed (assumed want 120x90):
    this_infitsfile, thisoutrebinfile, thisoutpaddedfile = this_tuple
    model_flux_HDUList = pyfits.open(this_infitsfile)
#   "touch" them to make sure -32 format read in:
    print 'model_flux_HDUList.shape: ', model_flux_HDUList[0].data.shape
    print 'model_flux_HDUList[0].header: ', model_flux_HDUList[0].header
    print 'model_flux_HDUList[0].data: ', model_flux_HDUList[0].data
#   But for this WANT the 2nd layer of the image:
    datain = zeros((1,360,720))
    datain[0] = model_flux_HDUList[0].data[1]
    model_flux_HDU = pyfits.PrimaryHDU(data=datain,header=model_flux_HDUList[0].header)
#   "touch" them to make sure -32 format read in:
    print 'model_flux_hdu.shape: ', model_flux_HDU.data.shape
    print 'model_flux_hdu.header: ', model_flux_HDU.header
    print 'model_flux_hdu.data: ', model_flux_HDU.data

#   1.2/ Rebin to desired pixel sizes (sum model flux per bin):

    which3rdbin = 0 	# Needed only for calling sequence
    model_flux_rebinned_hdu = simpler_sum_rebin(model_flux_HDU,WantFlxRebin,which3rdbin)
#   "Touching" the data to make sure it is read in properly:
    print 'Sample of rebinned data at [[0][1][2]:' , model_flux_rebinned_hdu.data[0][1][2]
    print 'Sample of rebinned header:' , model_flux_rebinned_hdu.header
    model_flux_rebinned_HDU = pyfits.PrimaryHDU(data=model_flux_rebinned_hdu.data,header=model_flux_rebinned_hdu.header)

    model_flux_rebinned_HDU.writeto(thisoutrebinfile)

    model_flux_HDUList.close()

#   1.3/ Pad them to square power of two:

    model_flux_padded_hdu = simpler_zerozero_pad_to_square_power2(model_flux_rebinned_HDU)
    print 'Sample of padded data at [[0][1][2]:' , model_flux_padded_hdu.data[0][1][2]
    print 'Sample of padded header:' , model_flux_padded_hdu.header
    model_flux_padded_HDU = pyfits.PrimaryHDU(data=model_flux_padded_hdu.data,header=model_flux_padded_hdu.header)

    model_flux_padded_HDU.writeto(thisoutpaddedfile)

###
###    2.1/ The exposure is averaged: 
##
##        exposr_rebinned_hdu = simpler_average_rebin(exposr_hdu,WantExpRebin,WhichExp3rdBin)
##        print 'Sample of rebinned exposr at [[0][1][2]:' , exposr_rebinned_hdu.data[0][1][2]
##        history_string = 'Exposure:'+ExposrFile+EBandString
##        exposr_rebinned_hdu.header.add_history('='+history_string)
##        exposr_hdulst.close()
##
###   2.2/ Into proper Fits Format:
##        self.data = exposr_rebinned_hdu.data*IntTime
##        self.header = exposr_rebinned_hdu.header
###        print self.data
###        print self.header
##        temp_HDU = pyfits.PrimaryHDU(data=self.data,header=self.header)
##        temp_HDU.writeto(OutExpAreaTimeFitsFile)
###        print self.data
##        print self.header
###        print 'Good at end of module.'

