示例#1
0
#!/usr/bin/env python

#Show that cbtransfer works as expected with an example dataset
import sys
import os
from camb_utils import cambio
from camb_utils import example_data
import numpy as np
import matplotlib.pyplot as plt

# example_data =  os.path.join(os.path.split(camb_utils.__file__)[0], 'example_data/')
        
fname=os.path.join(example_data, 'oneh_transfer_out.dat')
t = cambio.loadtransfers(filename = fname )
transfertuples = cambio.__preparetransfersforcombination(t,[1,2]) 
#c = cambio.__combinetransfers(transfertuples, f = [0.24 ,0.04])
c = cambio.cbtransfer(fname , 0.24, 0.04)
plt.plot(t[:,0], (0.24* t[:,1] + 0.04 * t[:,2])/0.28, label ="by hand")
plt.plot(c[0], c[1], label =" combine")
print np.shape(c[0])
print np.shape(c[1])
plt.yscale('log')
plt.xscale('log')
plt.legend(loc= "best")
plt.show()
from interfaces import FCPL
import massfunctions as mf
import plotutils as pu 
import sys

# INPUT FILES 
dirn  = "interfacecosmology/"
tkfile  = dirn +"example_data/M000/m000n0_transfer_fin_out.dat"
pkfile = dirn + "example_data/M000/m000n0_matterpower_fin.dat"
#psrunfile = "/home/rbiswas/doc/CAMB_outs/m000r03/m000r03_matterpower_fin.dat"

M000 = FCPL(H0 = 71. ,Om0  = 0.2647, Ob0 = 0., ns = 0.963, As = 2.16e-9)
M000s = FCPL(H0 = 71. ,Om0  = 0.2647, Ob0 = 0., ns = 0.963, sigma8 = 0.8)
M000p = FCPL(H0 = 71. ,Om0  = 0.3647, Ob0 = 0.0, ns = 0.963, As = 2.16e-9)

Tks = cio.loadtransfers(filename = tkfile)
k = Tks[:,0]
Tk = Tks[:,-1]
sumanrhoc    = 2.77536627e11
sumannorm    = fs.Pk_norm(sigma8 = 0.8, ns = 0.963, k = k , Tk=Tk, hubble= 0.71)

	#working power spectrum example
psfrompk = psu.powerspectrum(koverh = None, asciifile =pkfile)
	#working transfer function example
psfromtk = psu.powerspectrum(koverh = None, pstype ="cb", asciifile =tkfile, cosmo = M000)
psfromtkm = psu.powerspectrum(koverh = None, pstype ="matter", asciifile =tkfile, cosmo = M000)
psfromtkms = psu.powerspectrum(koverh = None, pstype ="matter", asciifile =tkfile, cosmo = M000s)
psfromtkcbs = psu.powerspectrum(koverh = None, pstype ="cb", asciifile =tkfile, cosmo = M000s)
psfromtkcbscb = psu.powerspectrum(koverh = None, pstype ="cb",sigma8ype= "cb", asciifile =tkfile, cosmo = M000s)
#psfromtkcbscb = psu.powerspectrum(koverh = None, pstype ="cb", asciifile =tkfile, cosmo = M000s)
#print  len(psfromtk), "now"
def powerspectrumfromfile(fname, 
	koverh = None ,
	pstype = "matter" , 
	h  = 0.71 ,
	ns = 0.963 ,
	As = 2.1e-9 , 
	Omegacdm = None , 
	Omegab   = None ):

	"""
	****************************************
	DEPRECATED: USE cambio functions instead
	*****************************************
	returns a tuple of koverh values and the interpolated power
	spectra at these values of hoverh using a CAMB output which 
	may be a power spectrum output or transfer function output. 
	If the output is a transfer function output, then ns,  h, 
	and As must be supplied 

	args:

	returns:
		tuple of (koverh, ps values)

	
	"""

	#decide if file is transfer function or Power spectrum output

	psfile = False
	tffile = False
	Unknown = True

	tmpfile = np.loadtxt(fname)
	shapetuple = np.shape(tmpfile)
	if shapetuple[-1] == 7:
		tffile  = True
		Unknown = False 
	if shapetuple[-1] ==2 :
		psfile  = True 	
		Unknown  = False
	if koverh == None:
		koverh = tmpfile[:,0]
	

	if Unknown:
		#file is not CAMB transfer function or power spectrum output
		raise ValueError("Unknown filename supplied")

	if psfile :
		if pstype != "matter" :
			raise ValueError ("Cannot obtain non-matter power spectrum  from CAMB power spectrum file")
		return (koverh , powerspectrum(koverh, fname )	)


	if tffile :
		if pstype == "matter" :
			transfer  = cio.loadtransfers(rootname = None, 
				filename = fname) 
			ps = cio.matterpowerfromtransfersforsinglespecies( 				koverh ,
				transfer , 
				h ,
				As ,
				ns )


			return (ps [:,0], ps[:,1])

		elif pstype == "cb"    :

			#LOST CODE HERE
			return 0
def __powerspectrum ( koverh , 
	asciifile = None ,
	pstype = "matter", 
	method = "CAMBoutfile",
	z      = 0.0 , 
	cosmo =  None ,
	**params):

	"""
	DO NOT CALL DIRECTLY. CALL powerspectrum instead 
	returns linearly interpolated values of the powerspectrum in the 
	powerspectrumfile with k values in units of h/Mpc. Using
	this with koverh = None, returns the values in the table. 

	args:
		koverh : array-like of floats or Nonetype, mandatory
			k in units of h/Mpc
		asciifile: string, 
			Filename for power spectrum or CAMB transfer function. 
			power sepctrum or transfer function input will be 
			recognized from CAMB file structure.
		method   : string, optional , defaults to "CAMBoutfile" 
			Method of obtaining power spectrum with fixed options
			options:
			-------
			CAMBoutfile   :assume that the asciifile output of CAMB 
				is at desired redshift 
			CAMBoutgrowth :Use the asciifile from CAMB output at 
				z  = 0.0 , and use a growth function to find 
				the power spectrum at z = z
			
			
	returns:
		tuple (koverh , power spectrum)

	notes: should be able to obtain the powerspectrum in a variety of 
		methods with code being added
	"""

	#ensure we are supposed to read CAMB outfiles
	if not method in ["CAMBoutfile","CAMBoutgrowth"]:
		raise ValueError("Method not defined")

#	# Decide whether this ia a matter or transfer file
	#This has been made a function
#	psfile = False
#	tkfile = False
#	Unknown = True
#
#	shapetuple = np.shape(tmpfile)
#	if shapetuple[-1] == 7:
#		tkfile  = True
#		Unknown = False 
#	if shapetuple[-1] ==2 :
#		psfile  = True 	
#		Unknown  = False


	psfile, tkfile, Unknown = cambasciifiletype ( asciifile )
	
	tmpfile = np.loadtxt(asciifile)
	if koverh == None:
		koverh = tmpfile[:,0]

	if Unknown:
		#file is not CAMB transfer function or power spectrum output
		raise ValueError("Unknown filename supplied")

	if psfile: 
		pk = cio.loadpowerspectrum(asciifile)
		if not np.all(np.diff(pk[:,0])>0.):
			raise ValueError("The k values in the power spectrum file are not in ascending order")

		if koverh == None :
			return (pk[:,0], pk[:,1])

		return  koverh, np.interp( koverh, pk[:,0],pk[:,1],left = np.nan, right = np.nan)
	if tkfile:
		#print "AS " , params["As"]
		#print cosmo.Ob0, cosmo.Oc0
	
		if pstype == "cb":
			#print "filename ", asciifile
			pk = cio.cbpowerspectrum ( transferfile = asciifile , 
				Omegacdm  =  cosmo.Oc0,
				Omegab    =  cosmo.Ob0, 
				h         =  cosmo.h, 
				Omeganu   =  cosmo.On0,
				As        =  params["As"], 
				#As        =  cosmo.As, 
				ns        =  cosmo.ns, 
				koverh    =  None )

			return (pk [:,0], pk[:,1])

		if pstype == "cbmatter":
			Omegam = cosmo.Om0
			Omegacb = cosmo.Ob0 + cosmo.Oc0 
			ratiosq = (Omegacb/Omegam)**2.0
			#print "filename ", asciifile
			pk = cio.cbpowerspectrum ( transferfile = asciifile , 
				Omegacdm  =  cosmo.Oc0,
				Omegab    =  cosmo.Ob0, 
				h         =  cosmo.h, 
				Omeganu   =  cosmo.On0,
				As        =  params["As"], 
				#As        =  cosmo.As, 
				ns        =  cosmo.ns, 
				koverh    =  None )

			return (pk [:,0], pk[:,1]*ratiosq)
		if pstype == "matter" :
			if koverh == None :
				koverh = tmpfile[:,0]

			transfer  = cio.loadtransfers( filename = asciifile) 

			transfertuple = (transfer[:,0], transfer[:,-1])
			ps = cio.matterpowerfromtransfersforsinglespecies(
				koverh ,
				transfer  = transfertuple,
				h = cosmo.h ,
				As = params["As"],
				ns = cosmo.ns)


			return (ps [:,0], ps[:,1])

		
		return koverh, pk 
		unittype = "solarmassperMpc3",  cosmo = cosmo)
	 
	dndlnM = rhobg *f_sigma *dlsinvdlM /M 

	#dndlnM = dlsinvdlM  *f_sigma/M * rhobg
		#critdensity(h = cosmo.h, unittype = "solarmassperMpc3")*cosmo.Om0 
	return dndlnM   
if __name__=="__main__":

	import numpy as np
	import matplotlib.pyplot as plt
	import camb_utils.cambio  as cio
	import sys
	#pk = cio.loadpowerspectrum ("example_data/LCDM_def_matterpower.dat")
	pk = cio.loadpowerspectrum ("LCDM_matterpower.dat")
	ts = cio.loadtransfers(filename = "example_data/LCDM_def_transfer_out.dat")
	#print np.shape(ts)
	#print pk[:,0]
	pkt = cio.matterpowerfromtransfersforsinglespecies(koverh = pk[:,0], 
		transfer = (ts[:,0],ts[:,-1]), h = 0.71, As = 2.1e-9, ns  = 0.963)
	
	plt.loglog ( pk[:,0], pk[:,1])	
	plt.loglog ( pkt[:,0], pkt[:,1])
	
	plt.figure()
	from astropy.cosmology import Planck13 as cosmo
	#print sigma(ps = (pk[:,0],pk[:,1]) , R = 8.0, cosmo = cosmo)
	#plt.show()
	sys.exit()
	M = 10.**(np.arange(7,16,0.2))
	R = np.arange(0.0005, 50.0,0.1)