{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model data\n",
    "\n",
    "In this tutorial we will inspect how the different models are defined in PySM and how you can inspect the templates.\n",
    "\n",
    "The notebook cell with all `import` statements is hidden, see the \"Getting started\" tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "nbsphinx": "hidden"
   },
   "outputs": [],
   "source": [
    "import pysm\n",
    "import pysm.units as u\n",
    "import healpy as hp\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sky = pysm.Sky(nside=128, preset_strings=[\"d1\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sky.components[0].__class__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(pysm.models.dust.ModifiedBlackBody.__init__.__doc__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Details of a model are defined in `presets.cfg` at https://github.com/healpy/pysm/blob/master/pysm/data/presets.cfg, for example:\n",
    "\n",
    "```\n",
    "[d1]\n",
    "class = \"ModifiedBlackBody\"\n",
    "map_I = \"pysm_2/dust_t_new.fits\"\n",
    "map_Q = \"pysm_2/dust_q_new.fits\"\n",
    "map_U = \"pysm_2/dust_u_new.fits\"\n",
    "unit_I = \"uK_RJ\"\n",
    "unit_Q = \"uK_RJ\"\n",
    "unit_U = \"uK_RJ\"\n",
    "map_mbb_index = \"pysm_2/dust_beta.fits\"\n",
    "map_mbb_temperature = \"pysm_2/dust_temp.fits\"\n",
    "unit_mbb_temperature = \"K\"\n",
    "freq_ref_I = \"545 GHz\"\n",
    "freq_ref_P = \"353 GHz\"\n",
    "```\n",
    "\n",
    "The filenames refer to another Github repository: https://github.com/healpy/pysm-data/tree/master/pysm_2\n",
    "\n",
    "PySM 3 automatically downloads and caches those data using the `astropy.utils.data` facilities. Therefore the first time you use `d1` it downloads all the templates and caches them in your home folder, next will load from there.\n",
    "\n",
    "## Inspect model inputs\n",
    "\n",
    "Model templates are stored as attributes of the model class and can be inspected and even modified in place."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dust = sky.components[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hp.mollview(dust.mbb_index, title=\"Dust spectral index\", unit=dust.mbb_index.unit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hp.mollview(dust.mbb_temperature, title=\"Dust temperature\", unit=dust.mbb_temperature.unit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySM 3",
   "language": "python",
   "name": "pysm3"
  },
  "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.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
