{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Point Source Catalog"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Though SOXS creates events from point sources as part of the background, one may want to study the point source properties in detail, and desire finer-grained control over their generation. SOXS provides the `make_point_sources_file()` function for this purpose, to create a set of photons from point sources using the point-source background model 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:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exp_time = (300.0, \"ks\")  # in seconds\n",
    "fov = 20.0  # in arcmin\n",
    "sky_center = [22.0, -27.0]  # in degrees"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, use `make_point_sources_file()` to create a SIMPUT catalog made up of photons from point sources. 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 point source properties to an ASCII table for later analysis, using the `output_sources` parameter:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.make_point_sources_file(\n",
    "    \"my_cat.simput\",\n",
    "    \"ptsrc\",\n",
    "    exp_time,\n",
    "    fov,\n",
    "    sky_center,\n",
    "    prng=24,\n",
    "    output_sources=\"point_source_table.dat\",\n",
    "    overwrite=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In a subsequent invocation of `make_point_sources_file()`, one could use the ASCII table of sources as an input to generate events from the same sources, using the `input_sources` keyword argument. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, use the `instrument_simulator()` to simulate the observation. Since we explicitly created a SIMPUT catalog of point sources, we should turn the automatic point-source background in SOXS off by setting `ptsrc_bkgnd=False`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.instrument_simulator(\n",
    "    \"my_cat.simput\",\n",
    "    \"ptsrc_cat_evt.fits\",\n",
    "    exp_time,\n",
    "    \"lynx_hdxi\",\n",
    "    sky_center,\n",
    "    overwrite=True,\n",
    "    ptsrc_bkgnd=False,\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 2.0 keV:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.write_image(\n",
    "    \"ptsrc_cat_evt.fits\", \"ptsrc_img.fits\", emin=0.7, emax=2.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",
    "    \"ptsrc_img.fits\",\n",
    "    stretch=\"sqrt\",\n",
    "    cmap=\"plasma\",\n",
    "    width=0.1,\n",
    "    vmin=0.0,\n",
    "    vmax=1.0,\n",
    "    center=[22.0, -27.0],\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}