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

insumfilestuples = [
('PadModelData.Av3C279_vp0110_g001_002-004GeV.070218b.fits', 'Pad128ModelData.Av3C279_vp0110_g001_002-004GeV.070218b.fits',0)
]
insumfilestuples = [
('PadModelData.Av3C279_vp0030_g001_002-004GeV.070218b.fits', 'Pad128ModelData.Av3C279_vp0030_g001_002-004GeV.070218b.fits',0),
('Pad_002-004GeVcounts_vp0030_g001.fits','Pad128_002-004GeVcounts_vp0030_g001.fits', 0),
('PadExpAreaTime_vp0030_g001_002-004GeV.070218b.fits', 'Pad128ExpAreaTime_vp0030_g001_002-004GeV.070218b.fits',1)
]

insumfilestuples = [
    ('PadModelData.PSFonVP113C279b_vp0110_g001_001-002GeV.070220b.fits','Pad128ModelData.PSFonVP113C279b_vp0110_g001_001-002GeV.070220b.fits',0),
    ('PadModelFlux.PSFonVP113C279b_vp0110_g001_001-002GeV.070220b.fits','Pad128ModelFlux.PSFonVP113C279b_vp0110_g001_001-002GeV.070220b.fits',0)
]
WantDatRebin = [2,2]
isexp = 0
which3rdbin = 0

for this_tuple in insumfilestuples:
#   1.1/ Get input file to be summed (assumed square power of 2):
    this_infitsfile, thisoutrebinfile,isexp = this_tuple
    model_dat_HDUList = pyfits.open(this_infitsfile)
#   "touch" them to make sure -32 format read in:
    print 'model_dat_HDUList[0].header: ', model_dat_HDUList[0].header
    print 'model_dat_HDUList[0].data: ', model_dat_HDUList[0].data
#   1.2/ Rebin to desired pixel sizes (sum model flux per bin):
    if(isexp==0):
        model_dat_rebinned_hdu = simpler_sum_rebin(model_dat_HDUList[0],WantDatRebin,which3rdbin)
    elif(isexp==1):
        model_dat_rebinned_hdu = simpler_average_rebin(model_dat_HDUList[0],WantDatRebin,which3rdbin)
    else:
        print 'Fatal Error: only recognizes isexp of 0 or 1 but you typed:', isexp
        
#   "Touching" the data to make sure it is read in properly:
    print 'Sample of rebinned data at [[0][1][2]:' , model_dat_rebinned_hdu.data[0][1][2]
    print 'Sample of rebinned header:' , model_dat_rebinned_hdu.header
    model_dat_rebinned_HDU = pyfits.PrimaryHDU(data=model_dat_rebinned_hdu.data)

    model_dat_rebinned_HDU.writeto(thisoutrebinfile)

    model_dat_HDUList.close()
