{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Fake Source Planting\n\nDemonstration of planting fake PSFs in diff images to mimic\nstrongly-lensed supernovae.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport numpy as np\nimport diffimageml\nfrom matplotlib import pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Fake Source Planting Overview\n\nFrom a trio of images (template, search, diff), we\n\n#. find galaxies in the template image\n\n#. build a PSF model from the search image\n\n#. plant Fake PSFs in the diff image near the galaxy locations\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Setup 1: Get the Data\nLoad in a trio of fits images from the example data dir\nPull them together into a FakePlanter triplet\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "example_data_dict = diffimageml.get_example_data()\nassert(os.path.isfile(example_data_dict['diffim1']))\nassert(os.path.isfile(example_data_dict['searchim1']))\nassert(os.path.isfile(example_data_dict['templateim1']))\n\nfakeplantertrio = diffimageml.FakePlanter(\n    example_data_dict['diffim1'],\n    searchim_fitsfilename=example_data_dict['searchim1'],\n    templateim_fitsfilename=example_data_dict['templateim1'])\n\nprint(\"FakePlanter Trio constructed.\")\nassert(fakeplantertrio.searchim.has_fakes == False)\nassert(fakeplantertrio.diffim.has_fakes == False)\nprint(\"  (No fakes yet)\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Setup 2: Make the PSF Model\n\nMeasure the zero point and build the ePSF model from Gaia stars\nSee the other example for details and plots.\nIn practice, this code will load an existing ePSF model from the\nexample data directory.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fakeplantertrio.searchim.fetch_gaia_sources(overwrite=False)\nfakeplantertrio.searchim.do_stellar_photometry(\n    fakeplantertrio.searchim.gaia_source_table)\nfakeplantertrio.searchim.measure_zeropoint(showplot=False)\nfakeplantertrio.searchim.build_epsf_model(\n    verbose=False, save_suffix='TestEPSFModel')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Plant Fakes\n\nHere we plant just 10 very bright fakes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# detect sources in the template image, identify likely galaxies\nfakeplantertrio.templateim.detect_sources()\nhostgaltable = fakeplantertrio.templateim.detect_host_galaxies()\n\n\n# Make 10 locations for random fakes (each relative to a galaxy center point)\nNfakes = 10\nphi = np.random.uniform(0, 360, Nfakes)\nd = np.random.uniform(0, 5, Nfakes)\nfluxes = np.random.uniform(10**2, 10**4, Nfakes)\n\n# fix the positions of the fakes in x,y coordinates on the diff image\n# This returns three tables: one each for the diffim, searchim, and templateim\nfake_positions_and_fluxes = fakeplantertrio.set_fake_positions_at_galaxies(\n    phi, d, fluxes)\n\n# Grab the existing ePSF model from the search image\nepsfmodel = fakeplantertrio.searchim.epsf\n\n# Plant the fakes\nfakeplantertrio.plant_fakes_triplet(\n    fake_positions_and_fluxes, psfmodel=epsfmodel,\n    writetodisk=False, save_suffix=\"planted.fits\")\n\nprint(\"Fake planting is done.\")\nassert(fakeplantertrio.diffim.has_fakes==True)\nassert(fakeplantertrio.searchim.has_fakes==True)\nprint(\" has_fakes is True, True!\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Display Fakes\n\nShow a few examples of fakes from the diff image, using a few\nrandom indices from the list of fakes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fakeIDs, fake_positions =  fakeplantertrio.get_fake_locations()\n\nrng = np.random.default_rng()\nfakeids_to_show = rng.choice(fakeIDs, 3)\nprint(fakeids_to_show)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Display the fakes in the diff image\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fakeplantertrio.plot_fakes(\n    fake_indices=fakeids_to_show)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## TODO:  Make a trio of postage stamps for each fake\n\nShow the trio of fakes for each\n\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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}