
#RunFileListTuples = ['Flat']
RunFileListTuples = ['ICS','Pion','Bremss']

basic_string1a = 'ModelFluxRebinnd'
basic_string1b = 'ModelFluxPadded'
basic_string1c = 'ModelDataRebinnd'
basic_string1d = 'ModelDataPadded'
ERangeMeme = '1-1000GeV.'
DateMeme = '060529.'

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

#
# Beginning 1st module:
# 1/ Assume correct model has alredy been made: it would be the sum-o-all-three.
#   Here, read in files in "rebinnned" but not "padded" format.
EBandString = ''
## Which file to twiddle?? and, which twiddle method (ie squished or inverse-squished; others as ready)
intwiddlefile = 0

icheck = -1
whattwiddle = 'halfsquished'
intwiddle = 0
NameMemeAll =''
for NameMeme in RunFileListTuples:
    InModelFluxFitsFile = basic_string1a+NameMeme+ERangeMeme+DateMeme+'fits'
    print '\n Input Model Flux Fits File:', InModelFluxFitsFile
    ThisModelFluxHDULst = pyfits.open(InModelFluxFitsFile)
#   Touch the header and data to read it in:
    icheck = icheck+1
    print '\n File Number: ', icheck
    print 'Header of: ', InModelFluxFitsFile
    print ThisModelFluxHDULst[0].header
    print 'Data of: ', InModelFluxFitsFile
    print ThisModelFluxHDULst[0].data, '\n'

#   Initialize size of data, etc:
    if(icheck == 0):
        n1 = ThisModelFluxHDULst[0].header['NAXIS1']
        n2 = ThisModelFluxHDULst[0].header['NAXIS2']
        OutHDUData = zeros((1,n2,n1),dtype=float)


# -------------------------------------------------------------------------------------
#
# 2/ Now twiddle with last component to make it look like an early incorrect model:

    if(icheck == intwiddle):
        if(whattwiddle == 'squished'):
            ThatModelFluxData = Squished(ThisModelFluxHDULst[0])
            OutHDUData = OutHDUData + ThatModelFluxData.data
            NameMemeAll = NameMemeAll+'Squish'
        elif(whattwiddle == 'inversesquished'):
            ThatModelFluxData = InverseSquished(ThisModelFluxHDULst[0])
            OutHDUData = OutHDUData + ThatModelFluxData.data
            NameMemeAll = NameMemeAll + 'InvSquish'
        elif(whattwiddle == 'halfsquished'):
            ThatModelFluxData = HalfSquished(ThisModelFluxHDULst[0])
            OutHDUData = OutHDUData + ThatModelFluxData.data
            NameMemeAll = NameMemeAll + 'HlfSquish'
        else:
            OutHDUData = OutHDUData + ThisModelFluxHDULst[0].data
    else:
        OutHDUData = OutHDUData + ThisModelFluxHDULst[0].data

    NameMemeAll = NameMemeAll+NameMeme

# -------------------------------------------------------------------------------------
#
# 3/ Write out the ModelFlux file:
#
OutFluxModelHDU = pyfits.PrimaryHDU(data=OutHDUData)
print 'Final OutFluxModelHDU header: \n', OutFluxModelHDU.header
print 'Final OutFluxModelHDU data: \n',   OutFluxModelHDU.data

OutFluxModelHDUFile = basic_string1a+NameMemeAll+ERangeMeme+'060912.fits'
OutFluxModelHDU.writeto(OutFluxModelHDUFile)

# -------------------------------------------------------------------------------------
#
# 3/ Now pad the beastie to square power of two:
#

OutPadFluxModelhdu = simpler_zero_pad_to_square_power2(OutFluxModelHDU)
print 'Sample of padded data at [[0][1][2]:' , OutPadFluxModelhdu.data[0][1][2]
print 'Sample of padded header:' , OutPadFluxModelhdu.header
OutPadFluxModelHDU = pyfits.PrimaryHDU(data=OutPadFluxModelhdu.data,header=OutPadFluxModelhdu.header)

OutPadFluxModelHDUFile = basic_string1b+NameMemeAll+ERangeMeme+'060912.fits'
OutPadFluxModelHDU.writeto(OutPadFluxModelHDUFile)




## Ta Da! Now run another routine to convolve it with instrument's effective area map.
