#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 = [
('ModelDataPaddedIntTime242.0.060529.fits', 'ModelData64x64IntTime242.0.060529.fits'), 
('ModelDataPaddedIntTime806.0.060529.fits', 'ModelData64x64IntTime806.0.060529.fits'),
('ModelFluxPadRS8BremICPion1-1000GeV.060529.fits', 'ModelFlux64x64RS8BremICPion1-1000GeV.060529.fits'),
('counts_g1-9_g004gt1GeVpadded.fits','counts_g1-9_g004gt1GeV64x64.fits')
]
insumfilestuples = [
    ('/AstroData/counts_g1-9_g004.fits','counts_g1-9_g004gt1GeV64x64b.fits')
    ]

WantFlxRebin = [12,8]
#WantExpRebin = [2,2]
#WhichExp3rdBin = 1
WhichFlx3rdBin = 1

for this_tuple in insumfilestuples:
#   1.1/ Get input file to be summed (assumed 128x128):
    this_infitsfile, thisoutrebinfile = this_tuple
    model_flux_HDUList = pyfits.open(this_infitsfile)
#   "touch" them to make sure -32 format read in:
    print 'model_flux_HDUList[0].header: ', model_flux_HDUList[0].header
    print 'model_flux_HDUList[0].data: ', model_flux_HDUList[0].data
#   1.2/ Rebin to desired pixel sizes (sum model flux per bin):

    model_flux_rebinned_hdu = simpler_sum_rebin(model_flux_HDUList[0],WantFlxRebin,WhichFlx3rdBin)
#   "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()


inavfiletuples = [
    ('ExposrTimPaddedIntTime22.0.060529.fits', 'ExposrTim64x64RS8IntTime22.0.060529.fits'),
('ExposrTimPaddedIntTime242.0.060529.fits', 'ExposrTim64x64RS8IntTime242.0.060529.fits'),
('ExposrTimPaddedIntTime7.0.060529.fits', 'ExposrTim64x64RS8IntTime7.0.060529.fits'),
('ExposrTimPaddedIntTime73.0.060529.fits', 'ExposrTim64x64RS8IntTime73.0.060529.fits'),
('ExposrTimPaddedIntTime806.0.060529.fits', 'ExposrTim64x64RS8IntTime806.0.060529.fits'),
]

inavfiletuples = []

IntTime = 1.0e-8

for that_tuple in inavfiletuples:
    that_infitsfile, that_outrebinfile = that_tuple
    exposr_HDUList = pyfits.open(that_infitsfile)
#   "touch" them to make sure -32 format read in:
    print 'model_flux_HDUList[0].header: ', exposr_HDUList[0].header
    print 'model_flux_HDUList[0].data: ', exposr_HDUList[0].data
#    2.1/ The exposure is averaged: 
    exposr_rebinned_hdu = simpler_average_rebin(exposr_HDUList[0],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_HDUList.close()

#   2.2/ Into proper Fits Format:
    self = exposr_rebinned_hdu
    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(that_outrebinfile)
#        print self.data
#        print self.header
#        print 'Good at end of module.'

