""" Code to compute cosmological data.

Context : SRP
Module	: SRPCosmology.py
Version : 1.2.1
Author  : Stefano Covino
Date    : 20/08/2011
E-mail  : stefano.covino@brera.inaf.it
URL     : http://www.merate.mi.astro.it/utenti/covino
Purpose : Compute cosmological data for any expanding universe.

Usage   : SRPCosmology [-h] [-hubbleconstant arg1] [--omegalambda arg2] [--omegamatter arg3] -z arg4 [-v]
                --hubbleconstant Hubble Constant
                --omegamatter Omega Matter
                --omegalambda Omega Lambda
                -z Redshift

History : (23/07/2007) First version.
        : (07/11/2007) Moved to astLib.
        : (12/11/2007) Right units for output.
        : (06/01/2011) Angular scale added.
        : (20/08/2011) Better cosmetics.
"""


import astLib.astCalc as cm
import SRP.SRPConstants as SRPConstants
from optparse import OptionParser
import sys
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')


parser = OptionParser(usage="usage: %prog [-h] [-hubbleconstant arg1] [--omegalambda arg2] [--omegamatter arg3] -z arg4 [-v]", version="%prog 1.2.1")
parser.add_option("--hubbleconstant", action="store", nargs=1, type="float", dest="hubble", default=71.0, help="Hubble Constant")
parser.add_option("--omegamatter", action="store", nargs=1, type="float", dest="omegam", default=0.27, help="Omega Matter")
parser.add_option("--omegalambda", action="store", nargs=1, type="float", dest="omegal", default=0.73, help="Omega Lambda")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="fully describe operations")
parser.add_option("-z", "--redshift", action="store", nargs=1, type="float", dest="z", help="Redshift")
(options, args) = parser.parse_args()


if options.hubble <= 0 or options.omegam < 0 or options.omegal < 0:
 if options.hubble <= 0:
  parser.error("Hubble constant must be positive.")
 elif options.omegam < 0:
  parser.error("Omega matter must be non-negative.")
 elif options.omegal < 0:
  parser.error("Omega lambda must be non-negative.")
 elif options.omegal + options.omegam == 1:
  parser.error("Omega matter and lambda together must be equal to 1.")
elif options.z == None:
 parser.print_help()
else:
 if options.z < 0.0:
  parser.error("Minimum redshift must be positive.")
 
 cm.OMEGA_M0 = options.omegam
 cm.OMEGA_L = options.omegal
 cm.H0 = options.hubble
 
# dh = cm.DH(options.hubble)
 da = cm.da(options.z)
 dc = cm.dc(options.z)
 dl = cm.dl(options.z)
 dmod = 10-cm.absMag(10,dl)
 tl = cm.tl(options.z)
 tz = cm.tz(options.z)
 scl = da*1e3/206265.
 
 
 if options.verbose:
  print "Redshift                         : %g" % options.z
  print "Hubble constant (km/s Mpc)       : %g" % options.hubble
  print "Omega matter                     : %g" % options.omegam
  print "Omega lambda                     : %g" % options.omegal
  print "Angular diameter distance (Mpc)  : %g" % da
  print "Angular scale (Kpc/arcsec)       : %g" % scl
  print "Comoving distance (Mpc)          : %g" % dc
  print "Distance modulus (mag)           : %g" % dmod
  print "Luminosity distance (Mpc)        : %g" % dl
  print "Loop-back time (Gyr)             : %g" % tl
  print "Age of the universe (Gyr)        : %g" % tz
 else:
  print "%g %g %g %g %g %g %g %g %g %g %g" % (options.z, options.hubble, options.omegam, options.omegal, da, scl, dc, dmod, dl, tl, tz)

sys.exit(SRPConstants.SRPExitSuccess)







