'''
@brief Reads in a bunch of CGRO PSF in 1D vector form, as produced by "COMPASS"; and writes out extrapolated iaq ascii form.
@author A. Connors
'''
# @file compassiaq_extrap_to_ascii.py
#

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

# ----------------------------------------------------------------
# FIRST:  Get appropriate COMPASS IAQ/PSF Vector from input ones:
# ----------------------------------------------------------------
# 
# 1/ get LIST of names of IAQ/Fits format files
# (i.e. one-dim instrument PSF files).
#   ALSO input wished-for total ENERGY RANGE.
#   ALSO wished-for SPECTRUM with which to weight them (power-law).

# 
# 2/ READ in each. KEEP list of energy-ranges in each file.

def GetCompassIAQ(InCOMPASSFitsFile,Outwhatsists...HDULst)
#
#   1/ Here it is
    in_hdulst = pyfits.open(InCOMPASSFitsFile)
#   and touch the data and header:

    print 'Read in  : '+InFitsFile
    print in_hdulst[0].header
    print in_hdulst[0].data

    naxis1 = in_hdulst[0].header['NAXIS1']
    energyrange = in_hdulst[0].header['Energies']
    in_hdulst.close()

# 
#   2.1/ CALCULATE weighting-factor for each E-range;
#        NORMALIZE.










#   2.2/ (for later ----) any extrapolation necessary?
#        if so, CREATE extrapolated IAQ vector  here.



#   2.3/ MULTIPLY each by its "weight" from the spectrum.







#   2.4/ ADD the weighted vectors.



#   2.5/ CHECK that the result is a proper probability vector,
#        i.e. sum <=1.



# 
# 3/ WRITE out the resulting vector
#    Make sure its in proper HDU format
#    Make it into a FITS file similar to the COMPASS ones.
# 





# -------------------------------------------------------------------
# NEXT:  Put in binned 2D format approriate for R-based EMC2 code.
# -------------------------------------------------------------------
# 
# 1/  GET HDU IAQ vector; reqested binning; and output ascii file-name.
#     ALSO get the number of grid-points for the trapezoidal summing.
#       It may be this is from the intrinsic binning of the PSF vector.












# 
# 2/  CREATE a grid (square matrix) to cover the requested binning
#     (i.e. of size num_trap_gridpts * num_psf_out_bins)












# 
# 3/ For each grid point, store the 2D PSF from the Vector PSF
# 










# 4/ Rebin, via trapezoidal rule orsimple sum,
#    to output PSF R-matrix format.
# 










# 5/ Write it out in ASCII form.
# 
# 