{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# PSF Model Construction\n\nDemonstration of building an ePSF model from Gaia stars in an LCO image.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport diffimageml\nfrom matplotlib import pyplot as plt\n\n# Get a dict with test data filenames\nexample_data_dict = diffimageml.get_example_data()\nsearchim1 = example_data_dict['searchim1']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## PSF Model Construction Overview\n\nTo construct a PSF model we go through the following steps\n\n* Load an example fits image\n\n* Fetch a catalog of stars in the image from the Gaia db\n\n* Do photometry of the Gaia stars\n\n* Measure the zero point for the image.\n\n* Build an ePSF model from the Gaia stars\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "assert(os.path.isfile(searchim1))\nsearchim = diffimageml.FitsImage(searchim1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Fetch a catalog of stars in the image from the Gaia db\n(or read in a saved local copy)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "searchim.fetch_gaia_sources(overwrite=False)\nsearchim.plot_gaia_sources(magmin=12, magmax=18)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Do photometry of the Gaia stars, within a user-specified\nmagnitude range\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "searchim.do_stellar_photometry(searchim.gaia_source_table)\n\nsearchim.plot_stellar_photometry()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Measure the zero point for this image from the Gaia stars\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "searchim.measure_zeropoint(showplot=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Build an ePSF Model from the Gaia stars that are not saturated\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "searchim.build_epsf_model(verbose=False, save_suffix='TestEPSFModel')\nsearchim.plot_epsf_model()"
      ]
    }
  ],
  "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
}