{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Make Background File"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `make_background_file()` function allows one to create an event file of a background for a particular instrument configuration. This event file can then be added as background to any source event file one creates using `instrument_simulator()`. `make_background_file()` also allows one to specify a particular set of point sources to make the point-source contribution to the background, or to create the point-source properties on the fly. In this example, we will show how to do the former using a point source table created by `make_point_source_list()`."
   ]
  },
  {
   "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 = (100.0, \"ks\")  # in seconds\n",
    "fov = 50.0  # in arcmin, used only for the point source generation\n",
    "sky_center = [22.0, -27.0]  # in degrees"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, use `make_point_source_list()` to create an ASCII table of point source properties that can be used when creating the background. We'll set a random seed using the `prng` parameter to use a unique set of random numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.make_point_source_list(\"point_source_table.dat\", fov, sky_center, prng=24)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use this table of point source properties as an input to `make_point_sources_file()` or to `make_background_file()` to control precisely the properties of the generated sources. Because in this example we want to create a background, we'll use `make_background_file()`. We have to pass the name of the table we created to the ``input_sources`` parameter if we want to use it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.make_background_file(\n",
    "    \"bkgnd_evt.fits\",\n",
    "    exp_time,\n",
    "    \"chandra_aciss_cy0\",\n",
    "    sky_center,\n",
    "    input_pt_sources=\"point_source_table.dat\",\n",
    "    overwrite=True,\n",
    "    prng=24,\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:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "soxs.write_image(\"bkgnd_evt.fits\", \"bkgnd_img.fits\", overwrite=True)"
   ]
  },
  {
   "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",
    "    \"bkgnd_img.fits\",\n",
    "    stretch=\"sqrt\",\n",
    "    cmap=\"plasma\",\n",
    "    width=0.5,\n",
    "    vmin=0.0,\n",
    "    vmax=1.0,\n",
    "    center=[22.0, -27.0],\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
}