from numpy import *
import ctes
import units
import cosmolib

####################################################################################################################################
#
# SOME COSMOLOGICAL RELATIONS
#
####################################################################################################################################    

UnitLength_in_cm         = 3.085678e+21
UnitMass_in_g            = 1.989e+43
UnitVelocity_in_cm_per_s = 100000.0

HUBBLE_CTE  = 3.2407789e-18	# Hubble param in h/sec
HubbleParam = 0.73		# Hubble param in 100 km/s/Mpc
OmegaLambda = 0.76
Omega0      = 0.24

# Hubble constance in cgs cm/s/cm/h
Hubble = 100* 1e5 *1./(1e6 * 3.0857e18) 
# Hubble constance in code units /h
Hubble = Hubble* 1/UnitVelocity_in_cm_per_s * UnitLength_in_cm

# deflauts cosmo parameters
defaultpars = {"Hubble":Hubble,"OmegaLambda":OmegaLambda,"Omega0":Omega0}



###################
def Z_a(a):
###################
  '''
  z(a)
  '''
  return 1./a - 1

###################
def A_z(z):
###################
  '''
  a(z)
  '''
  return 1/(z+1)
  
  
###################
def Rho_c(localsystem_of_units):
###################
  '''
  Critical density
  '''
  
  G = ctes.GRAVITY.into(localsystem_of_units)
  H = ctes.HUBBLE.into(localsystem_of_units)
  
  rhoc = 3*H**2/(8*pi*G)
  
  return  rhoc
  

###################
def Hubble_a(a,pars=defaultpars):
###################
  '''
  H(a)
  '''
  OmegaLambda	= pars['OmegaLambda']
  Omega0	= pars['Omega0']
  Hubble	= pars['Hubble']

  hubble_a = Omega0 / (a * a * a) + (1 - Omega0 - OmegaLambda) / (a * a) + OmegaLambda
  hubble_a = Hubble * sqrt(hubble_a)

  return hubble_a


###################
def Adot_a(a,pars=defaultpars):
###################
  '''
  da/dt
  '''
  OmegaLambda	= pars['OmegaLambda']
  Omega0	= pars['Omega0']
  Hubble	= pars['Hubble']

  hubble_a = Omega0 / (a * a * a) + (1 - Omega0 - OmegaLambda) / (a * a) + OmegaLambda
  hubble_a = Hubble * sqrt(hubble_a)
  adot_a   = hubble_a*a

  return adot_a
 
###################
def Age_a(a,pars=defaultpars):
###################
  '''
  cosmic age as a function of a
  '''
  OmegaLambda	= pars['OmegaLambda']
  Omega0	= pars['Omega0']
  Hubble	= pars['Hubble']
  
  a = array([a],float)
  LITTLEH = 0.73
  age_a = cosmolib.Age_a(a,Omega0,OmegaLambda,Hubble)  *0.978/LITTLEH
 
 

  return age_a
 
