def omeganuh2a( a , 
	Neff, 
	omeganuh2  = 0. ,
	sigmamass = None , 
	omegagammah2 = 2.471e-5 , 
	method  = "HACC") :

	"""
	returns a tuple of (omeganuh2 (a) , \dot(omeganuh2) (a)) in different 
	approximations specified by the string method. If sigmamass is 
	provided (ie. not None), the mass is assumed  to be equal for each 
	neutrino and overrides the specified values of omeganuh2 using the 
	relation omeganuh2 = sigmamass /94 eV 

	args:
		a	:
		Neff 	:
		omeganuh2 :
		sigmamass: optional, float, defaults to None
			sum of masses of neutrinos assumed in units of eV. 
		method:

	"""
	import utils.typeutils as tu

	if tu.isiterable(a) :
		a  = np.asarray(a) 
	else :
		a = np.asarray([a])

	if sigmamass !=None:
		#mass  = sigmamass /Neff 
		omeganuh2 =  sigmamass / 94.0 

	#print "a ", a
	#print omegagammah2

	omeganumasslessh2  = Neff*omegagammah2
	omeganumasslessh2 *= (7.0/8.0) *(4.0/11.0)**(4.0/3.0)/a**4.0

	omeganumassiveh2a =  omeganuh2 /a**3.0

	if method =="HACC":
		masslessval = -4.0 
		massiveval = -3.0 
		omeganuh2, res = tu.greaterarray( omeganumasslessh2 , omeganumassiveh2a,masslessval, massiveval)
		#print 1./a - 1.0, masslessval , massiveval ,res	, omeganuh2
		dotomeganuh2  = omeganuh2 *res /a 
		
		return omeganuh2, dotomeganuh2 

	else:
		raise ValueError("Method undefined")
def sigmaM (M , 
	ps , 
	bgtype = "matter", 
	khmin = 1.0e-5 , 
	khmax = 2.0  ,
	logkhint = 0.005 ,
	z = 0.0 ,
	cosmo = None ,
	**params):

	"""Returns the standard deviation of the overdensity fields 
	smoothed at a radius corresponding to mass M.
	args:
		M: array like , mandatory
			mass of halo in units of solar masses
		ps: 
		bgtype :
		z  = : 
		khmin :
		cosmo :
		
		
	notes:
		the bgtype matters for converting Mass M to R.
		Also ps must be set accordingly

	"""	

	if tu.isiterable(M):
		M =  np.asarray(M)

	#if cosmo == None:
	#	from astropy.cosmology import Planck13 as cosmo

	h = cosmo.H0/100.0

	R =  filterradiusformass( M  , bgtype= bgtype, z = z, cosmo = cosmo)
	RinMpcoverh = R*h 

	
	#print "RinMpcoverh ***************"
	#print RinMpcoverh 
	#return RinMpcoverh 	
	return sigma( ps , R = RinMpcoverh, khmin = khmin , khmax = khmax, logkhint = logkhint , cosmo= cosmo, **params) 
def sigmasq (ps , R = 8. , usenative = True, khmin = 0.9e-5 , khmax = 5.0, logkhint = 0.005 , 
	cosmo = None, filt= filters.Wtophatkspacesq,  **params) : 
	"""
	Returns the variance of the overdensity field smoothed at 
	a radius of R Mpc/h using a filter specified by filt

	args:
		ps: tuple of koverh, power spectrum values
		R : float array like
			distance scale in units of Mpc/h over which 
			the filtering is done
		usenative: bool, optional , defaults to True
			Use values provided in ps, rather than 
			interpolation
		cosmo: Model, whose hubble constant will be used
		khmin: float, value below which the integral will not be 
			calculated
		
	returns :
		array of sigmasq values 

	notes:
		- We need h, even if CAMB power spectrum is given
		- If interpolation is used only, and the range provided
			is outside the range of the data, only those points
			in the original range will be used. extrapolation
			is dangerous, particularly at high k, unless it is 
			made to drop as a power law. 

	"""

	import numpy as np
	import scipy.integrate as si


	h = cosmo.H0/100.0
	if usenative :
		mask = ps[1] is not np.nan
		#khvals = ps[0][mask]
		khvals = ps[0]
	else: 
		logkhmin  = np.log(khmin)
		logkhmax  = np.log(khmax)
		logkh = np.arange(logkhmin, logkhmax , logkhint) 
		khvals = np.exp(logkh)

		logkhmin = max(min(ps[0]),logkhmin)
		logkhmax = min(max(ps[0]),logkhmax) 
	mask = khvals >= khmin 
	khvals = khvals[mask]

	k  = khvals * h  
	

	psinterp = np.interp (khvals , ps[0], ps[1], left = np.nan, right = np.nan)

	#plt.loglog(khvals, psinterp, label="interp")
	#plt.loglog(ps[0], ps[1], label="native")
	#plt.legend(loc= "best")
	#plt.show()

	if tu.isiterable(R):
		R = np.asarray(R)
		kr = np.outer( R, khvals ) 
	else:
		kr  = R* khvals 
	kwinsq=   filt (kr, R)
	#kwin = 3*(np.sin(kr)-kr*np.cos(kr))/(kr)**3
	#kwinsq = kwin *kwin 
	
	ksqWsqPk = k*k *kwinsq* psinterp /2. /np.pi/ np.pi/h /h/h	
	
	
	sigmasq = si.simps ( ksqWsqPk, x = k, even = 'avg') 
	return sigmasq