#!/usr/bin/env python import os import matplotlib.pyplot as plt import camb_utils from camb_utils.cambio import cbpowerspectrum from camb_utils import example_data # example_data = os.path.join(os.path.split(camb_utils.__file__)[0], 'example_data') fname = os.path.join(example_data, 'oneh_transfer_out.dat') ps = cbpowerspectrum(transferfile=fname, Omegacdm=0.3, Omegab=0.05, h=0.71, Omeganu=0.0, As=None, ns=None, koverh=None) plt.loglog(ps[:, 0], ps[:, 1]) plt.show()
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