{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# pyphot - A tool for computing photometry from spectra\n", "\n", "\n", "Some examples are provided in this notebook\n", "\n", "Full documentation available at http://mfouesneau.github.io/docs/pyphot/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import pylab as plt\n", "import numpy as np\n", "\n", "import sys\n", "sys.path.append('../')\n", "from pyphot import astropy as pyphot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quick Start" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quick start example to access the library and it's content" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Library contains: 256 filters\n", "Filter object information:\n", " name: SPITZER_IRAC_36\n", " detector type: photon\n", " wavelength units: AA\n", " central wavelength: 35634.293911 Angstrom\n", " pivot wavelength: 35569.270678 Angstrom\n", " effective wavelength: 35134.324451 Angstrom\n", " photon wavelength: 35263.778583 Angstrom\n", " minimum wavelength: 31310.000000 Angstrom\n", " maximum wavelength: 39740.000000 Angstrom\n", " norm: 3181.966405\n", " effective width: 6848.829972 Angstrom\n", " fullwidth half-max: 7430.000000 Angstrom\n", " definition contains 505 points\n", "\n", " Zeropoints\n", " Vega: 27.948397 mag,\n", " 6.6166982851778816e-12 erg / (Angstrom cm2 s),\n", " 279.2354479161161 Jy\n", " 5.511736607732767 ph / (Angstrom cm2 s)\n", " AB: 25.163323 mag,\n", " 8.603413213872212e-11 erg / (Angstrom cm2 s),\n", " 3630.7805477009956 Jy\n", " ST: 21.100000 mag,\n", " 3.6307805477010028e-09 erg / (Angstrom cm2 s),\n", " 153224.8545764173 Jy\n", " \n", "Filter object information:\n", " name: SPITZER_IRAC_45\n", " detector type: photon\n", " wavelength units: AA\n", " central wavelength: 45110.141614 Angstrom\n", " pivot wavelength: 45020.219955 Angstrom\n", " effective wavelength: 44425.747085 Angstrom\n", " photon wavelength: 44603.204646 Angstrom\n", " minimum wavelength: 39250.000000 Angstrom\n", " maximum wavelength: 50640.000000 Angstrom\n", " norm: 4664.680820\n", " effective width: 8714.143135 Angstrom\n", " fullwidth half-max: 10110.000000 Angstrom\n", " definition contains 417 points\n", "\n", " Zeropoints\n", " Vega: 28.933083 mag,\n", " 2.6715713304827696e-12 erg / (Angstrom cm2 s),\n", " 180.61811118349118 Jy\n", " 3.246736588322029 ph / (Angstrom cm2 s)\n", " AB: 25.674986 mag,\n", " 5.370385702161592e-11 erg / (Angstrom cm2 s),\n", " 3630.780547701007 Jy\n", " ST: 21.100000 mag,\n", " 3.6307805477010028e-09 erg / (Angstrom cm2 s),\n", " 245467.79536259372 Jy\n", " \n", "Filter object information:\n", " name: SPITZER_IRAC_58\n", " detector type: photon\n", " wavelength units: AA\n", " central wavelength: 57593.367607 Angstrom\n", " pivot wavelength: 57450.413675 Angstrom\n", " effective wavelength: 56473.407964 Angstrom\n", " photon wavelength: 56760.560047 Angstrom\n", " minimum wavelength: 49340.000000 Angstrom\n", " maximum wavelength: 65060.000000 Angstrom\n", " norm: 2116.064825\n", " effective width: 12454.766480 Angstrom\n", " fullwidth half-max: 14060.000000 Angstrom\n", " definition contains 370 points\n", "\n", " Zeropoints\n", " Vega: 29.955073 mag,\n", " 1.0422472235401564e-12 erg / (Angstrom cm2 s),\n", " 114.74568537901237 Jy\n", " 0.5133774402352529 ph / (Angstrom cm2 s)\n", " AB: 26.204414 mag,\n", " 3.2978764583832674e-11 erg / (Angstrom cm2 s),\n", " 3630.7805477010043 Jy\n", " ST: 21.100000 mag,\n", " 3.6307805477010028e-09 erg / (Angstrom cm2 s),\n", " 399728.9635290507 Jy\n", " \n", "Filter object information:\n", " name: SPITZER_IRAC_80\n", " detector type: photon\n", " wavelength units: AA\n", " central wavelength: 79594.931057 Angstrom\n", " pivot wavelength: 79157.670512 Angstrom\n", " effective wavelength: 76171.441325 Angstrom\n", " photon wavelength: 77030.477773 Angstrom\n", " minimum wavelength: 63550.000000 Angstrom\n", " maximum wavelength: 96210.000000 Angstrom\n", " norm: 8138.005260\n", " effective width: 25631.512630 Angstrom\n", " fullwidth half-max: 28760.000000 Angstrom\n", " definition contains 421 points\n", "\n", " Zeropoints\n", " Vega: 31.291951 mag,\n", " 3.042422601167823e-13 erg / (Angstrom cm2 s),\n", " 63.58941738453659 Jy\n", " 0.38702836200357943 ph / (Angstrom cm2 s)\n", " AB: 26.900413 mag,\n", " 1.7371394883847807e-11 erg / (Angstrom cm2 s),\n", " 3630.7805477010024 Jy\n", " ST: 21.100000 mag,\n", " 3.6307805477010028e-09 erg / (Angstrom cm2 s),\n", " 758866.3704732973 Jy\n", " \n" ] } ], "source": [ "# get the internal default library of passbands filters\n", "lib = pyphot.get_library()\n", "print(\"Library contains: \", len(lib), \" filters\")\n", "# find all filter names that relates to IRAC\n", "# and print some info\n", "f = lib.find('irac')\n", "for name in f:\n", " lib[name].info(show_zeropoints=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose one has a calibrated spectrum and wants to compute the vega magnitude throug the HST WFC3 F110W passband," ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(<Quantity 4.08225856e-10 erg / (Angstrom cm2 s)>,\n", " <Quantity [9.00452026e+02, 9.01354004e+02, 9.02257996e+02, ...,\n", " 2.99353200e+06, 2.99653275e+06, 2.99953700e+06] Angstrom>,\n", " <Quantity [1.23800003e-17, 1.67599994e-17, 1.78000003e-17, ...,\n", " 1.40099994e-19, 1.38700004e-19, 1.26499994e-19] flam>)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# convert to magnitudes\n", "import numpy as np\n", "\n", "# We'll use Vega spectrum as example\n", "from pyphot.astropy import Vega\n", "vega = Vega()\n", "f = lib['HST_WFC3_F110W']\n", "# compute the integrated flux through the filter f\n", "# note that it work on many spectra at once\n", "fluxes = f.get_flux(vega.wavelength, vega.flux, axis=-1)\n", "# Note that fluxes is now with units of erg/s/cm2/AA\n", "# pyphot gives Vega in flam and can convert between flux density units. \n", "fluxes, vega.wavelength, vega.flux" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vega magnitude of Vega in HST_WFC3_F110W is : 0.000000 mag\n", "AB magnitude of Vega in HST_WFC3_F110W is : 0.751950 mag\n", "ST magnitude of Vega in HST_WFC3_F110W is : 2.372749 mag\n" ] } ], "source": [ "# convert to vega magnitudes\n", "mags = -2.5 * np.log10(fluxes.value) - f.Vega_zero_mag\n", "print(\"Vega magnitude of Vega in {0:s} is : {1:f} mag\".format(f.name, mags))\n", "mags = -2.5 * np.log10(fluxes.value) - f.AB_zero_mag\n", "print(\"AB magnitude of Vega in {0:s} is : {1:f} mag\".format(f.name, mags))\n", "mags = -2.5 * np.log10(fluxes.value) - f.ST_zero_mag\n", "print(\"ST magnitude of Vega in {0:s} is : {1:f} mag\".format(f.name, mags))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Provided Filter library\n", "This section shows the content of the provided library with respective properties of the passband filters. The code to generate the table is also provided in the documentation." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# define header and table format (as csv)\n", "hdr = (\"name\", \"detector type\", \"wavelength units\",\n", " \"central wavelength\", \"pivot wavelength\", \"effective wavelength\",\n", " \"Vega mag\", \"Vega flux\", \"Vega Jy\",\n", " \"AB mag\", \"AB flux\", \"AB Jy\",\n", " \"ST mag\", \"ST flux\", \"ST Jy\")\n", "fmt = \"{0:s},{1:s},{2:s},{3:.3f},{4:.3f},{5:.3f},{6:.5f},{7:.5g},{8:.5g},{9:.5f},{10:.5g},{11:.5g},{12:.5f},{13:.5g},{14:.5g}\\n\"\n", "\n", "l = pyphot.get_library()\n", "\n", "with open('table.csv', 'w') as output:\n", " output.write(','.join(hdr) + '\\n')\n", "\n", " for k in sorted(l.content):\n", " fk = l[k]\n", " rec = (fk.name, fk.dtype, fk.wavelength_unit,\n", " fk.cl.value, fk.lpivot.value, fk.leff.value,\n", " fk.Vega_zero_mag, fk.Vega_zero_flux.value, fk.Vega_zero_Jy.value,\n", " fk.AB_zero_mag, fk.AB_zero_flux.value, fk.AB_zero_Jy.value,\n", " fk.ST_zero_mag, fk.ST_zero_flux.value, fk.ST_zero_Jy.value)\n", " output.write(fmt.format(*rec)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Table description**\n", "\n", "* name: the identification name of the filter in the library.\n", "* detector type: energy or photon counter.\n", "* wavelength units: filter defined with these units and all wavelength properties: central wavelength, pivot wavelength, and effective wavelength.\n", "* <X> mag: magnitude in Vega, AB or ST system (w.r.t. the detector type)\n", "* <X> flux: flux in $erg/s/cm^2/AA $ in the X system\n", "* <X> Jy: flux in $Jy$ (Jansky) in the X system" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>name</th>\n", " <th>detector type</th>\n", " <th>wavelength units</th>\n", " <th>central wavelength</th>\n", " <th>pivot wavelength</th>\n", " <th>effective wavelength</th>\n", " <th>Vega mag</th>\n", " <th>Vega flux</th>\n", " <th>Vega Jy</th>\n", " <th>AB mag</th>\n", " <th>AB flux</th>\n", " <th>AB Jy</th>\n", " <th>ST mag</th>\n", " <th>ST flux</th>\n", " <th>ST Jy</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>2MASS_H</td>\n", " <td>photon</td>\n", " <td>AA</td>\n", " <td>16513.665</td>\n", " <td>16494.947</td>\n", " <td>16386.037</td>\n", " <td>24.86219</td>\n", " <td>1.135300e-10</td>\n", " <td>1030.40</td>\n", " <td>23.49470</td>\n", " <td>4.000500e-10</td>\n", " <td>3630.8</td>\n", " <td>21.1</td>\n", " <td>3.630800e-09</td>\n", " <td>32952.0</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>2MASS_J</td>\n", " <td>photon</td>\n", " <td>AA</td>\n", " <td>12407.217</td>\n", " <td>12389.684</td>\n", " <td>12282.394</td>\n", " <td>23.76727</td>\n", " <td>3.112400e-10</td>\n", " <td>1593.60</td>\n", " <td>22.87325</td>\n", " <td>7.090900e-10</td>\n", " <td>3630.8</td>\n", " <td>21.1</td>\n", " <td>3.630800e-09</td>\n", " <td>18591.0</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>2MASS_Ks</td>\n", " <td>photon</td>\n", " <td>AA</td>\n", " <td>21655.839</td>\n", " <td>21638.144</td>\n", " <td>21521.419</td>\n", " <td>25.92164</td>\n", " <td>4.279000e-11</td>\n", " <td>668.29</td>\n", " <td>24.08405</td>\n", " <td>2.324800e-10</td>\n", " <td>3630.8</td>\n", " <td>21.1</td>\n", " <td>3.630800e-09</td>\n", " <td>56705.0</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>CFHT_CFH12K_CFH7406</td>\n", " <td>photon</td>\n", " <td>AA</td>\n", " <td>4888.664</td>\n", " <td>4888.514</td>\n", " <td>4891.329</td>\n", " <td>20.91453</td>\n", " <td>4.307100e-09</td>\n", " <td>3433.40</td>\n", " <td>20.85383</td>\n", " <td>4.554800e-09</td>\n", " <td>3630.8</td>\n", " <td>21.1</td>\n", " <td>3.630800e-09</td>\n", " <td>2894.2</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>CFHT_CFH12K_CFH7504</td>\n", " <td>photon</td>\n", " <td>AA</td>\n", " <td>5037.756</td>\n", " <td>5037.593</td>\n", " <td>5036.966</td>\n", " <td>20.83347</td>\n", " <td>4.641000e-09</td>\n", " <td>3928.60</td>\n", " <td>20.91906</td>\n", " <td>4.289200e-09</td>\n", " <td>3630.8</td>\n", " <td>21.1</td>\n", " <td>3.630800e-09</td>\n", " <td>3073.4</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " name detector type wavelength units central wavelength \\\n", "0 2MASS_H photon AA 16513.665 \n", "1 2MASS_J photon AA 12407.217 \n", "2 2MASS_Ks photon AA 21655.839 \n", "3 CFHT_CFH12K_CFH7406 photon AA 4888.664 \n", "4 CFHT_CFH12K_CFH7504 photon AA 5037.756 \n", "\n", " pivot wavelength effective wavelength Vega mag Vega flux Vega Jy \\\n", "0 16494.947 16386.037 24.86219 1.135300e-10 1030.40 \n", "1 12389.684 12282.394 23.76727 3.112400e-10 1593.60 \n", "2 21638.144 21521.419 25.92164 4.279000e-11 668.29 \n", "3 4888.514 4891.329 20.91453 4.307100e-09 3433.40 \n", "4 5037.593 5036.966 20.83347 4.641000e-09 3928.60 \n", "\n", " AB mag AB flux AB Jy ST mag ST flux ST Jy \n", "0 23.49470 4.000500e-10 3630.8 21.1 3.630800e-09 32952.0 \n", "1 22.87325 7.090900e-10 3630.8 21.1 3.630800e-09 18591.0 \n", "2 24.08405 2.324800e-10 3630.8 21.1 3.630800e-09 56705.0 \n", "3 20.85383 4.554800e-09 3630.8 21.1 3.630800e-09 2894.2 \n", "4 20.91906 4.289200e-09 3630.8 21.1 3.630800e-09 3073.4 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "df = pd.read_csv('./table.csv')\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extention to Lick indices\n", "\n", "We also include functions to compute lick indices and provide a series of commonly use ones.\n", "\n", "The Lick system of spectral line indices is one of the most commonly used methods of determining ages and metallicities of unresolved (integrated light) stellar populations." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The index of Vega in CN_1 is -0.281614 mag\n" ] } ], "source": [ "# convert to magnitudes\n", "import numpy as np\n", "from pyphot.astropy import UnitLickLibrary as LickLibrary\n", "from pyphot.astropy import Vega\n", "\n", "vega = Vega()\n", "# using the internal collection of indices\n", "lib = LickLibrary()\n", "f = lib['CN_1']\n", "# work on many spectra at once\n", "index = f.get(vega.wavelength, vega.flux, axis=-1)\n", "print(\"The index of Vega in {0:s} is {1:f} {2:s}\".format(f.name, index, f.index_unit))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, we show the content of the provided library with respective properties of the passband filters. \n", "The table below is also part of the documentation." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# define header and table format (as csv)\n", "hdr = (\"name\", \"wavelength units\", \"index units\", \"min\", \"max\" \"min blue\", \"max blue\", \"min red\", \"max red\")\n", "fmt = \"{0:s},{1:s},{2:s},{3:.3f},{4:.3f},{5:.3f},{6:.5f},{7:.3f},{8:.3f}\\n\"\n", "\n", "l = pyphot.UnitLickLibrary()\n", "\n", "with open('licks_table.csv', 'w') as output:\n", " output.write(','.join(hdr) + '\\n')\n", "\n", " for k in sorted(l.content):\n", " fk = l[k]\n", " # wavelength have units\n", " band = fk.band.value\n", " blue = fk.blue.value\n", " red = fk.red.value\n", " rec = (fk.name, fk.wavelength_unit, fk.index_unit, band[0], band[1],\n", " blue[0], blue[1], red[0], red[1])\n", " output.write(fmt.format(*rec))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>name</th>\n", " <th>wavelength units</th>\n", " <th>index units</th>\n", " <th>min</th>\n", " <th>maxmin blue</th>\n", " <th>max blue</th>\n", " <th>min red</th>\n", " <th>max red</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>CN_1</td>\n", " <td>AA</td>\n", " <td>mag</td>\n", " <td>4142.125</td>\n", " <td>4177.125</td>\n", " <td>4080.125</td>\n", " <td>4117.625</td>\n", " <td>4244.125</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>CN_2</td>\n", " <td>AA</td>\n", " <td>mag</td>\n", " <td>4142.125</td>\n", " <td>4177.125</td>\n", " <td>4083.875</td>\n", " <td>4096.375</td>\n", " <td>4244.125</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>Ca1_LB13</td>\n", " <td>AA</td>\n", " <td>ew</td>\n", " <td>8484.000</td>\n", " <td>8513.000</td>\n", " <td>8474.000</td>\n", " <td>8484.000</td>\n", " <td>8563.000</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>Ca2_LB13</td>\n", " <td>AA</td>\n", " <td>ew</td>\n", " <td>8522.000</td>\n", " <td>8562.000</td>\n", " <td>8474.000</td>\n", " <td>8484.000</td>\n", " <td>8563.000</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>Ca3_LB13</td>\n", " <td>AA</td>\n", " <td>ew</td>\n", " <td>8642.000</td>\n", " <td>8682.000</td>\n", " <td>8619.000</td>\n", " <td>8642.000</td>\n", " <td>8700.000</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " name wavelength units index units min maxmin blue max blue \\\n", "0 CN_1 AA mag 4142.125 4177.125 4080.125 \n", "1 CN_2 AA mag 4142.125 4177.125 4083.875 \n", "2 Ca1_LB13 AA ew 8484.000 8513.000 8474.000 \n", "3 Ca2_LB13 AA ew 8522.000 8562.000 8474.000 \n", "4 Ca3_LB13 AA ew 8642.000 8682.000 8619.000 \n", "\n", " min red max red \n", "0 4117.625 4244.125 \n", "1 4096.375 4244.125 \n", "2 8484.000 8563.000 \n", "3 8484.000 8563.000 \n", "4 8642.000 8700.000 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "df = pd.read_csv('./licks_table.csv', index_col=False)\n", "df.head()" ] } ], "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.6.8" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "ctrl-shift-i" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }