""" Code to convert energy to frequency and viceversa

Context : SRP
Module  : SRPEnergyFreqFlux.py
Version : 1.2.0
Author  : Stefano Covino
Date    : 23/10/2011
E-mail  : stefano.covino@brera.inaf.it
URL:    : http://www.merate.mi.astro.it/utenti/covino
Purpose : Compute the frequency at a given energy or vice-versa

Usage   : SRPEnergyFreq [-h] [-v] -e arg1 / -f arg2 / -w arg3
            -f Frequency (Hz)
            -e Energy (eV)
            -w Wavelength (micron)


History : (02/07/2008) First version.
        : (20/03/2009) Better input parameter management.
        : (02/08/2009) Logic improvement.
        : (30/05/2010) Help if executed with no parameters.
        : (24/08/2011) Better cosmetics.
        : (23/10/2011) Flux conversion.
"""


import sys, math
from optparse import OptionParser
import SRP.SRPConstants as SRPConstants
from SRP.SRPSpectroscopy.Ecm2sA2Jy import Ecm2sA2Jy
from SRP.SRPSpectroscopy.Jy2Ecm2sA import Jy2Ecm2sA


eV2erg = 1.6021765e-12
h = 6.62606876e-27
c = 2.99792458e14



parser = OptionParser(usage="usage: %prog [-a arg1 / -j arg1] -e arg2 / -f arg2 / -w arg2 [-h] [-v]", version="%prog 1.2.0")
parser.add_option("-a", "--ergscm2A", action="store", nargs=1, type="float", dest="fluxdenA", help="Flux density in Erg / cm s A")
parser.add_option("-e", "--energy", action="store", nargs=1, type="float", default=0.0, dest="energy", help="Energy (eV)")
parser.add_option("-f", "--frequency", action="store", nargs=1, type="float", default=0.0, dest="frequency", help="Frequency (Hz)")
parser.add_option("-j", "--Jy", action="store", nargs=1, type="float", dest="fluxdenJ", help="Flux density in Jy")
parser.add_option("-w", "--wavelength", action="store", nargs=1, default=0.0, type="float", dest="wavelength", help="Wavelength (micron)")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Fully describe operations")
(options, args) = parser.parse_args()



if options.frequency <= 0.0 and options.wavelength <= 0.0 and options.energy <= 0.0:
    parser.print_help()
    sys.exit(SRPConstants.SRPExitFailure)
elif options.frequency > 0.0 and options.wavelength > 0.0:
    if options.verbose:
        print "You can not enter frequency and wavelength simultaneously."
    parser.print_help()
    sys.exit(SRPConstants.SRPExitFailure)
elif options.energy > 0.0 and options.wavelength > 0.0:
    if options.verbose:
        print "You can not enter energy and wavelength simultaneously."
    parser.print_help()
    sys.exit(SRPConstants.SRPExitFailure)
elif options.frequency > 0.0 and options.energy > 0.0:
    if options.verbose:
        print "You can not enter frequency and energy simultaneously."
    parser.print_help()
    sys.exit(SRPConstants.SRPExitFailure)
elif options.frequency > 0.0 and options.energy > 0.0 and options.wavelength > 0.0:
    if options.verbose:
        print "You can not enter frequency, wavelength and energy simultaneously."
    parser.print_help()
    sys.exit(SRPConstants.SRPExitFailure)

if options.fluxdenA or options.fluxdenJ:
    if options.fluxdenA < 0.0 and options.fluxdenJ < 0.0:
        if options.verbose:
            print "Flux density is a positive quantity."
        parser.print_help()
        sys.exit(SRPConstants.SRPExitFailure)
    elif options.fluxdenA > 0.0 and options.fluxdenJ > 0.0:
        if options.verbose:
            print "Flux density in one only unit."
        parser.print_help()
        sys.exit(SRPConstants.SRPExitFailure)



if options.energy > 0.0:
    energ = options.energy
    freq = options.energy*eV2erg/h
    wavel = c/((options.energy*eV2erg)/h)
elif options.frequency > 0.0:
    energ = h*options.frequency/eV2erg
    freq = options.frequency
    wavel = c/options.frequency
elif options.wavelength > 0.0:
    energ = h*(c/options.wavelength)/eV2erg
    freq = c/options.wavelength
    wavel = options.wavelength
#
if options.fluxdenA:
    fluxdenJ = Ecm2sA2Jy(options.fluxdenA,wavel)
    fluxdenA = options.fluxdenA
elif options.fluxdenJ:
    fluxdenA = Jy2Ecm2sA(options.fluxdenJ,wavel)
    fluxdenJ = options.fluxdenJ
#
if options.verbose:
    print "Energy    : %g eV" % energ
    print "Frequency : %g Hz" % freq
    print "Wavelength: %g micron" % wavel
    if options.fluxdenA or options.fluxdenJ:
        print "Flux density: %g erg / cm2 s A" % fluxdenA
        print "Flux density: %g Jy" % fluxdenJ
else:
    if options.fluxdenA or options.fluxdenJ:
        print "%g %g %g %g %g" % (energ, freq, wavel, fluxdenA, fluxdenJ)
    else:
        print "%g %g %g" % (energ, freq, wavel)
#
