{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate a mock peculiar velocity catalogue from CSiBORG haloes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from h5py import File\n",
    "from scipy.interpolate import interp1d\n",
    "from astropy.cosmology import FlatLambdaCDM\n",
    "\n",
    "import csiborgtools\n",
    "\n",
    "%matplotlib inline\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "SPEED_OF_LIGHT = 299_792.458 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dist2redshift(dist, cosmo):\n",
    "    x = np.linspace(0., 1., int(1e5))\n",
    "    y = cosmo.comoving_distance(x).value\n",
    "    return interp1d(y, x, kind=\"cubic\")(dist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "nsim = 17417\n",
    "nsnap = 99\n",
    "kind = \"main\"\n",
    "cat = csiborgtools.read.CSiBORG2SUBFINDCatalogue(nsim, nsnap, kind)\n",
    "\n",
    "cosmo = FlatLambdaCDM(H0=100, Om0=0.3111)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "dist, RA, dec = [cat[\"spherical_pos\"][:, i] for i in range(3)]\n",
    "Vx, Vy, Vz = [cat[\"cartesian_vel\"][:, i] for i in range(3)]\n",
    "vrad = csiborgtools.flow.project_Vext(Vx, Vy, Vz, np.deg2rad(RA), np.deg2rad(dec))\n",
    "zcosmo = dist2redshift(dist, cosmo)\n",
    "zobs = (1 + zcosmo) * (1 + vrad / SPEED_OF_LIGHT) - 1\n",
    "\n",
    "data = {\"r_hMpc\": dist,\n",
    "        \"RA\": RA,\n",
    "        \"DEC\": dec,\n",
    "        \"vrad\": vrad,\n",
    "        \"zcosmo\": zcosmo,\n",
    "        \"zobs\": zobs}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 75644 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "plt.scatter(zcosmo, zobs, s=0.01)\n",
    "\n",
    "plt.axline((0, 0), slope=1, color=\"red\", linestyle=\"--\")\n",
    "plt.xlabel(r\"$z_{\\rm cosmo}$\")\n",
    "plt.ylabel(r\"$z_{\\rm obs}$\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 122260 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "plt.scatter(zcosmo, zobs - zcosmo, s=0.01)\n",
    "\n",
    "plt.axhline(0, color=\"red\", linestyle=\"--\")\n",
    "plt.xlabel(r\"$z_{\\rm cosmo}$\")\n",
    "plt.ylabel(r\"$z_{\\rm obs} - z_{\\rm cosmo}$\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = (cat[\"totmass\"] > 5e12) & (cat[\"totmass\"] < 1e13) & cat[\"Central\"] & (cat[\"dist\"] < 135.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "choice = np.random.choice(np.arange(len(cat))[mask], size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "fname = f\"/mnt/extraspace/rstiskalek/catalogs/PV_mock_CB2_{nsim}_small.hdf5\"\n",
    "with File(fname, \"w\") as f:\n",
    "    for key, value in data.items():\n",
    "        f.create_dataset(key, data=value[choice])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv_csiborg",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
