{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cosmological Source Catalog"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SOXS provides the `make_cosmological_sources_file()` function to generate a set of photons from cosmological halos and store them in a SIMPUT catalog. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, import our modules:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "\n",
    "matplotlib.rc(\"font\", size=18)\n",
    "import soxs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Second, define our parameters, including the location within the catalog where we are pointed via the argument ``cat_center``. To aid in picking a location, see [the halo map](../users_guide/source_catalogs.html). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp_time = (200.0, \"ks\")\n",
    "fov = 20.0  # in arcmin\n",
    "sky_center = [30.0, 45.0]  # in degrees\n",
    "cat_center = [3.1, -1.9]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, use `make_cosmological_sources_file()` to create a SIMPUT catalog made up of photons from the halos. We'll set a random seed using the `prng` parameter to make sure we get the same result every time. We will also write the halo properties to an ASCII table for later analysis, using the `output_sources` parameter:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.make_cosmological_sources_file(\n",
    "    \"my_cat.simput\",\n",
    "    \"cosmo\",\n",
    "    exp_time,\n",
    "    fov,\n",
    "    sky_center,\n",
    "    cat_center=cat_center,\n",
    "    prng=33,\n",
    "    overwrite=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, use the `instrument_simulator()` to simulate the observation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.instrument_simulator(\n",
    "    \"my_cat.simput\",\n",
    "    \"cosmo_cat_evt.fits\",\n",
    "    exp_time,\n",
    "    \"lynx_hdxi\",\n",
    "    sky_center,\n",
    "    overwrite=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use the `write_image()` function in SOXS to bin the events into an image and write them to a file, restricting the energies between 0.7 and 7.0 keV:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.write_image(\n",
    "    \"cosmo_cat_evt.fits\", \"cosmo_img.fits\", emin=0.7, emax=7.0, overwrite=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now show the resulting image:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = soxs.plot_image(\n",
    "    \"cosmo_img.fits\",\n",
    "    stretch=\"sqrt\",\n",
    "    cmap=\"cubehelix\",\n",
    "    vmin=0.0,\n",
    "    vmax=2.0,\n",
    "    width=0.33333,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}