""" Code to convert energy to frequency and viceversa

Context : SRP
Module  : SRPEnergyFreq.py
Version : 1.1.2
Author  : Stefano Covino
Date    : 24/08/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.

"""


import sys, math
from optparse import OptionParser
import SRP.SRPConstants as SRPConstants


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



parser = OptionParser(usage="usage: %prog [-h] [-v] -e arg1 / -f arg2 / -w arg3", version="%prog 1.1.2")
parser.add_option("-f", "--frequency", action="store", nargs=1, type="float", default=0.0, dest="frequency", help="Frequency (Hz)")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Fully describe operations")
parser.add_option("-e", "--energy", action="store", nargs=1, type="float", default=0.0, dest="energy", help="Energy (eV)")
parser.add_option("-w", "--wavelength", action="store", nargs=1, default=0.0, type="float", dest="wavelength", help="Wavelength (micron)")
(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.energy > 0.0:
        if options.verbose:
                print "Energy    : %g eV" % options.energy
                print "Frequency : %g Hz" % (options.energy*eV2erg/h)
                print "Wavelength: %g micron" % (c/((options.energy*eV2erg)/h))
        else:
                print "%g %g %g" % (options.energy, options.energy*eV2erg/h, c*h/(options.energy*eV2erg))
elif options.frequency > 0.0:
        if options.verbose:
                print "Energy    : %g eV" % (h*options.frequency/eV2erg)
                print "Frequency : %g Hz" % options.frequency
                print "Wavelength: %g micron" % (c/options.frequency)
        else:
                print "%g %g %g" % (h*options.frequency/eV2erg, options.frequency, c/options.frequency)
elif options.wavelength > 0.0:
        if options.verbose:
                print "Energy    : %g eV" % (h*(c/options.wavelength)/eV2erg)
                print "Frequency : %g Hz" % (c/options.wavelength)
                print "Wavelength: %g micron" % options.wavelength
        else:
                print "%g %g %g" % (h*c*eV2erg/options.wavelength,c/options.wavelength,options.wavelength)

