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

Context : SRP
Module  : SRPDustAbs.py
Version : 1.2.0
Author  : Stefano Covino
Date    : 15/11/2013
E-mail  : stefano.covino@brera.inaf.it
URL:    : http://www.merate.mi.astro.it/~covino
Purpose : Compute the amount of dust absorption in different galactic environments.

Usage   : SRPDustAbs [-c arg1] -g arg2 [-h] [-r arg3] [-v] -w arg4
            -c E(B-V) color excess (mag).
            -g Kind of extinction curve.
            -r Extinction ratio.
            -w Wavelength (micron)
            Extinction curves for the MW, LMC and SMC galaxies following Pei (1992, ApJ, 395, 130)
            and starburst galaxies following Calzetti et al. (2000, ApJ, 533, 682)

History : (24/09/2003) First version.
        : (07/10/2003) Better management of bad inputs.
        : (03/02/2005) Optparse.
        : (03/07/2005) Calzetti's extinction law.
        : (15/11/2007) Minor improvements in the output.
        : (21/03/2011) Library modified for numpy input.
        : (23/08/2011) Better cosmetics.
        : (15/11/2013) Extinction ratio added.
"""


import SRP.SRPConstants as SRPConstants
import SRP.SRPSpectroscopy.ExtConstants as ExtConstants
from SRP.SRPSpectroscopy.getAbs import getAbs
from optparse import OptionParser
import sys


parser = OptionParser(usage="usage: %prog [-c arg1] -g arg2 [-h] [-r arg3] [-v] -w arg4", version="%prog 1.2.0")
parser.add_option("-c", "--colorexcess", action="store", nargs=1, type="float", dest="colexc", default=1.0, help="E(B-V) color excess (mag).")
parser.add_option("-g", "--galaxy", action="store", nargs=1, type="string", dest="galaxy", help="Kind of extinction curve.")
parser.add_option("-r", "--ExtRatio", action="store", nargs=1, type="float", dest="extratio", default=None, help="Extinction ratio.")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="fully describe operations")
parser.add_option("-w", "--wavelength", action="store", nargs=1, type="float", dest="wavel", help="Wavelength (micron)")
(options, args) = parser.parse_args()


if options.galaxy and options.wavel > 0.0 and options.colexc >= 0.0:
    if options.galaxy in ExtConstants.ExtCurDict.keys():
        if options.extratio == None:
            adl = getAbs(options.colexc,options.galaxy,options.wavel)
        elif options.extratio > 0:
            adl = getAbs(options.colexc,options.galaxy,options.wavel,options.extratio)
        else:
            parser.error("Extinction ratio must be positive.")
        if options.verbose:
            print "\tAbsorption in the %s:" % (ExtConstants.ExtCurDict[options.galaxy])
            print "\t\tat %s micron and with E(B-V) = %s is: %.3f mag" % (options.wavel, options.colexc, adl)
        else:
            print "%.3f %s" % (adl, options.wavel)
    else:
        if options.verbose:
            print "Exctinction curves available for:"
            for i in ExtConstants.ExtCurDict.keys():
                print "\t%s for %s" % (i, ExtConstants.ExtCurDict[i])
        sys.exit(SRPConstants.SRPExitFailure)
else:
    parser.print_help()
