""" Code to compute the amount of absorption due to IGM.

Context : SRP
Module  : SRPIGM.py
Version : 1.1.0
Author  : Stefano Covino
Date    : 03/09/2011
E-mail  : stefano.covino@brera.inaf.it
URL:    : http://www.merate.mi.astro.it/~covino
Purpose : Compute the amount of DLA absorption.

Usage   : SRPIGM -b arg1 [-l arg2] [-h] -t arg3 [-v] -x arg4
            -b Lower IGM redshift (>= 0) [default 6.0]
            -l Observed wavelength (micron)
            -t Upper IGM redshift (>= 0)
            -x Neutral hydrogen fraction (0 <= xHI <= 1)
            IGM extinction computed as in Totani et al. 2006, PASJ, 58, 485


History : (12/06/2009) First version.
        : (03/09/2011) Several computation and logic errors corrected.
"""

import SRP.SRPSpectroscopy as SS
from SRP.SRPSpectroscopy.Ralpha import Ralpha
from SRP.SRPSpectroscopy.TauIGM import TauIGM
from optparse import OptionParser
import math, sys



parser = OptionParser(usage="usage: %prog -b arg1 [-l arg2] [-h] -t arg3 [-v] -x arg4", version="%prog 1.1.0")
parser.add_option("-b", "--zigml", action="store", nargs=1, type="float", dest="zIGMl", default=6, help="Lower IGM redshift (>= 0) [default 6.0]")
parser.add_option("-l", "--lambd", action="store", nargs=1, type="float", dest="lambd", help="Observed wavelength (micron)")
parser.add_option("-t", "--zigmu", action="store", nargs=1, type="float", dest="zIGMu", help="Upper IGM redshift (>= 0)")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="fully describe operations")
parser.add_option("-x", "--xhi", action="store", nargs=1, type="float", dest="xHI", help="Neutral hydrogen fraction (0 <= xHI <= 1)")
(options, args) = parser.parse_args()

if options.lambd > 0.0 and 0.0 <= options.xHI <= 1.0 and options.zIGMu >= options.zIGMl and options.zIGMl >= 0.0:
    if (options.lambd/(SS.LambdaLymanAlpha*1e4) - 1.0 - options.zIGMu) > Ralpha()*(options.lambd/(SS.LambdaLymanAlpha*1e4)):
        tau = TauIGM(options.lambd*1e-4,options.xHI,options.zIGMl,options.zIGMu)
        if options.verbose:
            print "Wavelength       : %.3f micron" % options.lambd
            print "xHI              : %.2g" % options.xHI
            print "IGM lower z      : %.2f" % options.zIGMl        
            print "IGM upper z      : %.2f" % options.zIGMu
            print "Absorption factor: %g" % math.e**-tau
        else:
            print options.lambd, options.xHI, options.zIGMl, options.zIGMu, math.e**-tau
    else:
        print "Parameter range not allowed for computation."
        sys.exit(1)
else:
    parser.print_help()

