```python
import astropy
from astropy import units as u
from astropy import cosmology as cosmo
```


```python
from astropy.cosmology import WMAP5
WMAP5.H(0)
```




$70.2 \; \mathrm{\frac{km}{Mpc\,s}}$




```python
WMAP5.critical_density(0)# critical density at z = 0 in g/cm^3
```




$9.2565426 \times 10^{-30} \; \mathrm{\frac{g}{cm^{3}}}$




```python
WMAP5.H(7)  # Hubble parameter at z = 7 in km/s/Mpc
```




$839.15905 \; \mathrm{\frac{km}{Mpc\,s}}$




```python
from astropy.cosmology import FlatLambdaCDM
import astropy.units as u
cosmo = FlatLambdaCDM(H0=70.2 * u.km / u.s / u.Mpc, Om0=0.3)
```


```python
cosmo
```




    FlatLambdaCDM(H0=70.2 km / (Mpc s), Om0=0.3, Tcmb0=0 K, Neff=3.04, m_nu=None, Ob0=None)




```python
FlatLambdaCDM(H0=70.2, Om0=0.3, Tcmb0=2.725 ,
              Neff=3.04, m_nu=[0], Ob0=None)
```




    FlatLambdaCDM(H0=70.2 km / (Mpc s), Om0=0.3, Tcmb0=2.725 K, Neff=3.04, m_nu=[0. 0. 0.] eV, Ob0=None)




```python
from astropy.cosmology import WMAP5   # WMAP 5-year cosmology
WMAP5.Om([0, 1.0, 2.0]), WMAP5.Ode([0., 1.0, 2.0])  
```




    (array([0.277     , 0.7536719 , 0.91109749]),
     array([0.72291523, 0.24586683, 0.08806609]))




```python
from astropy.cosmology import WMAP5
print(WMAP5.__doc__)
```

    WMAP5 instance of FlatLambdaCDM cosmology
    (from Komatsu et al. 2009, ApJS, 180, 330, doi: 10.1088/0067-0049/180/2/330. Table 1 (WMAP + BAO + SN ML).)



```python

```


```python
import pysm

import numpy as np
import healpy as hp
import matplotlib.pyplot as plt
%matplotlib inline
```


```python
from pysm.nominal import models
```


```python
import os
import json
import numpy as np
from scipy.spatial import cKDTree
from astropy import units as u
from astropy.coordinates import Angle, Distance, ICRSCoordinates
from astropy.cosmology import Planck13, WMAP5
from vpf_calc import vpf
from dat.cartesian_cosmo import mk_coords
from dat.h5_funcs import h5_arr, arr2h5


def mk_mock_srch(radecfile, nzdictfile, Nsph, simul_cosmo):

    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    radecarr = h5_arr(radecfile, "good_pts")
    nzdict = json.load(open(nzdictfile))

    Nrands = radecarr.shape[0]
    Narrs = Nsph / Nrands
    remain = Nsph % Nrands

    radecz = np.zeros((Nsph, 3))

    for i in range(Narrs):

        start = Nrands * i
        stop = Nrands * (i + 1)
        radecz[start:stop, :2] = radecarr[:, :]

    endchunk = Nrands * (Narrs)
    radecz[endchunk:, :2] = radecarr[:remain, :]

    rad = np.arange(1.0, 67.0, 5.0)
    zlo = nzdict["zlo"]
    zhi = nzdict["zhi"]

    radeczlist = len(rad) * [radecz]

    for r_i, r in enumerate(rad):

        dis_near = Distance(comv(zlo).value + r, u.Mpc)
        dis_far = Distance(comv(zhi).value - r, u.Mpc)

        z_a = dis_near.compute_z(cosmology=cosmo)

        z_b = dis_far.compute_z(cosmology=cosmo)

        randz = (z_a ** 3 + \
                 (z_b ** 3 - z_a ** 3) * np.random.rand(Nsph)) ** (1. / 3.)

        radeczlist[r_i][:, 2] = randz[:]

        arr2h5(radeczlist[r_i], "{0}/{1}/mocks/mock_srch_pts.hdf5".format(os.path.dirname(radecfile), simul_cosmo), "radecz_{0}".format(str(r_i * 5 + 1)))


def mk_mock_coords(radeczfile, outfile, simul_cosmo):

    if simul_cosmo == "Planck":
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5

    rad = np.arange(1.0, 67.0, 5.0)

    radecz = h5_arr(radeczfile, "radecz")

    cart = np.zeros(radecz.shape)

    for i, rdz in enumerate(radecz):

        ra = Angle(rdz[0], u.deg)
        dec = Angle(rdz[1], u.deg)

        losd = cosmo.comoving_distance(rdz[2])
        dis = Distance(losd)

        coord = ICRSCoordinates(ra, dec, distance=dis)

        cart[i, :] = np.array([coord.x.value, coord.y.value, coord.z.value])

    np.savetxt(outfile, cart)


def mock_vpf(mock_cart_coords, spheresfile, simul_cosmo, rad):

    gals = h5_arr(mock_cart_coords, "coords")

    print gals

    name = mock_cart_coords.split("/")[-1].split(".")[0]

    if simul_cosmo == "Planck":
        # First make h free
        Planck13.__init__(100.0, Planck13.Om0)
        cosmo = Planck13
    elif simul_cosmo == "WMAP":
        WMAP5.__init__(100.0, WMAP5.Om0)
        cosmo = WMAP5
    comv = cosmo.comoving_distance

    gal_baum = cKDTree(gals)

    spheres = h5_arr(spheresfile, "radecz_{0}".format(str(int(rad))))

    print spheres

    for i, sphere in enumerate(spheres):

        rang = Angle(sphere[0], u.deg)
        decang = Angle(sphere[1], u.deg)

        dis = Distance(comv(sphere[2]), u.Mpc)

        coord = ICRSCoordinates(rang, decang, distance=dis)

        sph_cen = np.array([coord.x, coord.y, coord.z])

        nn = gal_baum.query(sph_cen)

        print "rad: ", rad, ", sphere: ", i

        f = open("{0}/vpf_out/ascii/{1}_{2}.dat".format(os.path.dirname(spheresfile), name, str(int(rad))), 'a')

        if not nn[0] < rad:
            f.write("1\n")
        else:
            f.write("0\n")

        f.close()
```


```python
from nbodykit.lab import cosmology

cosmo = cosmology.Cosmology()
print("original sigma8 = %.4f" % cosmo.sigma8)

new_cosmo = cosmo.match(sigma8=0.82)
print("new sigma8 = %.4f" % new_cosmo.sigma8)
```


```python
$ sudo port install autoconf automake libtool
```


```python
install autoconf automake libtool
```


```python
from nbodykit.lab import *
from nbodykit import setup_logging
```


```python
import nbodykit; print(nbodykit, nbodykit.__version__);
```


```python
pip install camb
```

    Collecting camb
      Using cached camb-1.3.5.tar.gz (1.7 MB)
    Requirement already satisfied: scipy>=1.0 in /opt/anaconda3/lib/python3.8/site-packages (from camb) (1.8.0)
    Requirement already satisfied: sympy>=1.0 in /opt/anaconda3/lib/python3.8/site-packages (from camb) (1.8)
    Requirement already satisfied: numpy<1.25.0,>=1.17.3 in /opt/anaconda3/lib/python3.8/site-packages (from scipy>=1.0->camb) (1.22.2)
    Requirement already satisfied: mpmath>=0.19 in /opt/anaconda3/lib/python3.8/site-packages (from sympy>=1.0->camb) (1.2.1)
    Building wheels for collected packages: camb
      Building wheel for camb (setup.py) ... [?25lerror
    [31m  ERROR: Command errored out with exit status 1:
       command: /opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py'"'"'; __file__='"'"'/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-wheel-_dnc0ofq
           cwd: /private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/
      Complete output (35 lines):
      running bdist_wheel
      running build
      running build_py
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
        File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py", line 268, in <module>
          setup(name=os.getenv('CAMB_PACKAGE_NAME', 'camb'),
        File "/opt/anaconda3/lib/python3.8/site-packages/setuptools/__init__.py", line 153, in setup
          return distutils.core.setup(**attrs)
        File "/opt/anaconda3/lib/python3.8/distutils/core.py", line 148, in setup
          dist.run_commands()
        File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 966, in run_commands
          self.run_command(cmd)
        File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/opt/anaconda3/lib/python3.8/site-packages/wheel/bdist_wheel.py", line 299, in run
          self.run_command('build')
        File "/opt/anaconda3/lib/python3.8/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/opt/anaconda3/lib/python3.8/distutils/command/build.py", line 135, in run
          self.run_command(cmd_name)
        File "/opt/anaconda3/lib/python3.8/distutils/cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
          cmd_obj.run()
        File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py", line 232, in run
          make_library(False)
        File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py", line 111, in make_library
          ok, gfortran_version = _compile.check_gfortran(msg=not _compile.is_windows)
        File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/camb/_compilers.py", line 71, in check_gfortran
          raise Exception(
      Exception: You need ifort or gfortran 6 or higher to compile (found: None).
      See https://camb.readthedocs.io/en/latest/fortran_compilers.html
      ----------------------------------------[0m
    [31m  ERROR: Failed building wheel for camb[0m
    [?25h  Running setup.py clean for camb
    Failed to build camb
    Installing collected packages: camb
        Running setup.py install for camb ... [?25lerror
    [31m    ERROR: Command errored out with exit status 1:
         command: /opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py'"'"'; __file__='"'"'/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-record-i3yzpu7m/install-record.txt --single-version-externally-managed --compile --install-headers /opt/anaconda3/include/python3.8/camb
             cwd: /private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/
        Complete output (37 lines):
        running install
        running build
        running build_py
        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py", line 268, in <module>
            setup(name=os.getenv('CAMB_PACKAGE_NAME', 'camb'),
          File "/opt/anaconda3/lib/python3.8/site-packages/setuptools/__init__.py", line 153, in setup
            return distutils.core.setup(**attrs)
          File "/opt/anaconda3/lib/python3.8/distutils/core.py", line 148, in setup
            dist.run_commands()
          File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 966, in run_commands
            self.run_command(cmd)
          File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
            cmd_obj.run()
          File "/opt/anaconda3/lib/python3.8/site-packages/setuptools/command/install.py", line 61, in run
            return orig.install.run(self)
          File "/opt/anaconda3/lib/python3.8/distutils/command/install.py", line 545, in run
            self.run_command('build')
          File "/opt/anaconda3/lib/python3.8/distutils/cmd.py", line 313, in run_command
            self.distribution.run_command(command)
          File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
            cmd_obj.run()
          File "/opt/anaconda3/lib/python3.8/distutils/command/build.py", line 135, in run
            self.run_command(cmd_name)
          File "/opt/anaconda3/lib/python3.8/distutils/cmd.py", line 313, in run_command
            self.distribution.run_command(command)
          File "/opt/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
            cmd_obj.run()
          File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py", line 232, in run
            make_library(False)
          File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py", line 111, in make_library
            ok, gfortran_version = _compile.check_gfortran(msg=not _compile.is_windows)
          File "/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/camb/_compilers.py", line 71, in check_gfortran
            raise Exception(
        Exception: You need ifort or gfortran 6 or higher to compile (found: None).
        See https://camb.readthedocs.io/en/latest/fortran_compilers.html
        ----------------------------------------[0m
    [31mERROR: Command errored out with exit status 1: /opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py'"'"'; __file__='"'"'/private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-install-z3bc_mrp/camb_a39c83dcdb3e448ba60a0623d907c9aa/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/pr/2pb42ldd0dzc3mwbz3chdrpc0000gn/T/pip-record-i3yzpu7m/install-record.txt --single-version-externally-managed --compile --install-headers /opt/anaconda3/include/python3.8/camb Check the logs for full command output.[0m
    [?25hNote: you may need to restart the kernel to use updated packages.



```python

```

ipynb
3fa77b491c34ce1d32c7074a2f2187b3f268c8b6
Jupyter Notebook
83bced0692c78399cea906e8ba4ebb2a17b57d31
MIT
DhruvKumarPHY/solutions
3***.ipynb
83bced0692c78399cea906e8ba4ebb2a17b57d31
MIT
DhruvKumarPHY/solutions
3***.ipynb
83bced0692c78399cea906e8ba4ebb2a17b57d31
MIT
DhruvKumarPHY/solutions
3***.ipynb