""" Code to compute the afterglow synchrotron spectrum frequency.

Context : SRP
Module	: SRPAftSynchrSpectrumWind.py
Version : 2.0.1
Author  : Stefano Covino
Date    : 18/02/2011
E-mail  : stefano.covino@brera.inaf.it
URL     : http://www.merate.mi.astro.it/covino
Purpose : Compute the afterglow synchrotron spectrum in a
          constant density environment

Usage   : SRPAftSynchrSpectrumWind [-a arg1] [-b arg2] [-d arg3] [-e arg4] [-f arg5] [-h] [-g arg6] [-p arg7] [-t arg8] [-v] [-z arg9]
            -a Astar.
            -b Epsilon B (0,1).
            -e Epsilon E (0,1).
            -d Luminosity distance (cm).
            -g Isotropic energy (erg).
            -f Frequency (Hz).
            -p Electron distribution index.
            -t Time from burst (days).
            -z Source redshift.

            Afterglow model for wind shaped environemnt.

History : (29/04/2007) First version.
        : (18/02/2011) More information
        : (12/08/2011) Better cosmetics.
"""


import SRP.SRPAfterglow as SRPAfterglow
import SRP.SRPConstants as SRPConstants
from optparse import OptionParser
import sys


parser = OptionParser(usage="usage: %prog [-a arg1] [-b arg2] [-d arg3] [-e arg4] [-f arg5] [-h] [-g arg6] [-p arg7] [-t arg8] [-v] [-z arg9]", version="%prog 2.0.1")
parser.add_option("-a", "--astar", action="store", nargs=1, type="float", dest="astar", default=1, help="Astar.")
parser.add_option("-b", "--epsb", action="store", nargs=1, type="float", dest="epsb", default=0.01, help="Epsilon B (0,1).")
parser.add_option("-e", "--epse", action="store", nargs=1, type="float", dest="epse", default=0.1, help="Epsilon E (0,1).")
parser.add_option("-d", "--dist", action="store", nargs=1, type="float", dest="dist", default=1e28, help="Luminosity distance (cm).")
parser.add_option("-g", "--energy", action="store", nargs=1, type="float", dest="energy", default=1e52, help="Isotropic energy (erg).")
parser.add_option("-f", "--frequency", action="store", nargs=1, type="float", dest="freq", default=1e15, help="Frequency (Hz).")
parser.add_option("-p", "--p", action="store", nargs=1, type="float", dest="p", default=2.2, help="Electron distribution index.")
parser.add_option("-t", "--time", action="store", nargs=1, type="float", dest="time", default=1, help="Time from burst (days).")
parser.add_option("-z", "--redshift", action="store", nargs=1, type="float", dest="redshift", default=1, help="Source redshift.")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="fully describe operations")
(options, args) = parser.parse_args()


if 0 < options.epsb < 1 and 0 < options.epse < 1 and options.astar > 0 and options.energy > 0 and options.freq > 0 and options.redshift > 0 and options.time > 0 and options.p >= 2.0 and options.dist > 0:
 if options.verbose:
  print "Epsilon B : %.2g" % options.epsb
  print "Epsilon E : %.2g" % options.epse
  print "Distance  : %.2g cm" % options.dist
  print "Astar     : %.2g" % options.astar
  print "Eenergy   : %.1g erg" % options.energy
  print "Frequency : %.2g Hz" % options.freq
  print "p         : %.2g" % options.p
  print "Redshift  : %.2g" % options.redshift
  print "Time	  : %.2g days" % options.time
 flux = SRPAfterglow.SynchrSpectrWind (options.freq, options.epse, options.epsb, options.energy, options.time, options.redshift, options.astar, options.dist, options.p)
 freqc = SRPAfterglow.CoolSynchrFreqWind (options.epsb, options.energy, options.time, options.redshift, options.astar)
 fluxt = SRPAfterglow.TypSynchrFluxWind (options.epsb, options.energy, options.dist, options.astar, options.redshift, options.time)
 freqa = SRPAfterglow.SelfAbsSynchrFreqWind (options.epse, options.epsb, options.energy, options.astar, options.time, options.redshift)
 freqt = SRPAfterglow.TypSynchrFreqWind (options.epse, options.epsb, options.energy, options.time, options.redshift)
 if options.verbose:
  print "Flux      : %g mJy." % flux
  print "Synchrotron absortion frequency  : %g Hz." % freqa
  print "Synchrotron cooling frequency    : %g Hz." % freqc
  print "Typical synchrotron frequency    : %g Hz." % freqt
  print "Typical synchrotron flux         : %g mJy." % fluxt
 else:
  print "%.2g %.2g %.2g %.2g %.2g %.2g %.2g %.2g %.2g %g %g %g %g %g" % (options.epsb, options.epse, options.dist, options.astar, options.energy, options.freq, options.p, options.redshift, options.time, flux, freqa, freqc, freqt, fluxt)
else:
 if not 0 < options.epsb < 1:
  if options.verbose:
   print "Magnetic field fraction of equipartition must be in (0,1) range."
 if not 0 < options.epse < 1:
  if options.verbose:
   print "Electric field fraction of equipartition must be in (0,1) range."
 if not options.dist > 0:
  if options.verbose:
   print "Distance must be positive."
 if not options.astar > 0:
  if options.verbose:
   print "Astar parameter must be positive."
 if options.energy <= 0:
  if options.verbose:
   print "Energy must be positive."
 if options.freq < 0:
  if options.verbose:
   print "Frequency must be positive."
 if not options.p >= 2:
  if options.verbose:
   print "Electron distribution index must be greater than 2.0."
 if options.time <= 0:
  if options.verbose:
   print "Time from burst must be positive."
 if options.redshift <= 0:
  if options.verbose:
   print "Source redshift must be positive."
 sys.exit(SRPConstants.SRPExitFailure)







