{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SPIRE-NEP master catalogue\n", "\n", "This notebook presents the merge of the various pristine catalogues to produce HELP mater catalogue on SPIRE-NEP." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This notebook was run with herschelhelp_internal version: \n", "0246c5d (Thu Jan 25 17:01:47 2018 +0000) [with local modifications]\n", "This notebook was executed on: \n", "2018-02-20 16:17:05.784701\n" ] } ], "source": [ "from herschelhelp_internal import git_version\n", "print(\"This notebook was run with herschelhelp_internal version: \\n{}\".format(git_version()))\n", "import datetime\n", "print(\"This notebook was executed on: \\n{}\".format(datetime.datetime.now()))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "#%config InlineBackend.figure_format = 'svg'\n", "\n", "import matplotlib.pyplot as plt\n", "plt.rc('figure', figsize=(10, 6))\n", "\n", "import os\n", "import time\n", "\n", "from astropy import units as u\n", "from astropy.coordinates import SkyCoord\n", "from astropy.table import Column, Table\n", "import numpy as np\n", "from pymoc import MOC\n", "\n", "from herschelhelp_internal.masterlist import merge_catalogues, nb_merge_dist_plot, specz_merge\n", "from herschelhelp_internal.utils import coords_to_hpidx, ebv, gen_help_id, inMoc" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TMP_DIR = os.environ.get('TMP_DIR', \"./data_tmp\")\n", "OUT_DIR = os.environ.get('OUT_DIR', \"./data\")\n", "SUFFIX = os.environ.get('SUFFIX', time.strftime(\"_%Y%m%d\"))\n", "\n", "try:\n", " os.makedirs(OUT_DIR)\n", "except FileExistsError:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## I - Reading the prepared pristine catalogues" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ps1 = Table.read(\"{}/PS1.fits\".format(TMP_DIR))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## II - Merging tables\n", "\n", "We first merge the optical catalogues and then add the infrared ones: DECaLS, HSC, KIDS, PanSTARRS, UKIDSS-LAS, and VISTA-VIKING.\n", "\n", "At every step, we look at the distribution of the distances separating the sources from one catalogue to the other (within a maximum radius) to determine the best cross-matching radius." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DECaLS" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "master_catalogue = ps1\n", "master_catalogue['ps1_ra'].name = 'ra'\n", "master_catalogue['ps1_dec'].name = 'dec'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cleaning\n", "\n", "When we merge the catalogues, astropy masks the non-existent values (e.g. when a row comes only from a catalogue and has no counterparts in the other, the columns from the latest are masked for that row). We indicate to use NaN for masked values for floats columns, False for flag columns and -1 for ID columns." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for col in master_catalogue.colnames:\n", " if \"m_\" in col or \"merr_\" in col or \"f_\" in col or \"ferr_\" in col or \"stellarity\" in col:\n", " master_catalogue[col].fill_value = np.nan\n", " elif \"flag\" in col:\n", " master_catalogue[col].fill_value = 0\n", " elif \"id\" in col:\n", " master_catalogue[col].fill_value = -1\n", " \n", "master_catalogue = master_catalogue.filled()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<Table length=10>\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
idxps1_idradecm_ap_gpc1_gmerr_ap_gpc1_gm_gpc1_gmerr_gpc1_gm_ap_gpc1_rmerr_ap_gpc1_rm_gpc1_rmerr_gpc1_rm_ap_gpc1_imerr_ap_gpc1_im_gpc1_imerr_gpc1_im_ap_gpc1_zmerr_ap_gpc1_zm_gpc1_zmerr_gpc1_zm_ap_gpc1_ymerr_ap_gpc1_ym_gpc1_ymerr_gpc1_yf_ap_gpc1_gferr_ap_gpc1_gf_gpc1_gferr_gpc1_gflag_gpc1_gf_ap_gpc1_rferr_ap_gpc1_rf_gpc1_rferr_gpc1_rflag_gpc1_rf_ap_gpc1_iferr_ap_gpc1_if_gpc1_iferr_gpc1_iflag_gpc1_if_ap_gpc1_zferr_ap_gpc1_zf_gpc1_zferr_gpc1_zflag_gpc1_zf_ap_gpc1_yferr_ap_gpc1_yf_gpc1_yferr_gpc1_yflag_gpc1_yps1_flag_cleanedps1_flag_gaia
degdeg
0190582648149477540264.81491050768.822329736214.22799968720.0063359998166614.3051004410.00061500002630113.7480001453.70000016119e-0513.82439994810.0037940000183913.59510040280.002245000097913.66300010680.0016659999964813.54310035710.001668000011713.60270023350.0013239999534613.51679992680.0031240000389513.57989978790.003249000059447392.6496564243.14107484166885.88592373.90041338367False11502.70394220.39199214554710721.116635637.4639037866False13242.190731327.38116253112439.415011819.0875695911False13891.842703521.341826578713149.822983816.0355387821False14232.461880640.951210132513428.889023940.1851398577FalseFalse3
1190632649450999463264.94503776768.865634656214.24919986720.00095999997574814.30620002750.0004269999917613.92899990080.0004529999860114.00300025940.0047969999723113.84739971160.0033549999352513.8996000290.0027709999121713.7959995270.0025440000463313.84420013430.003142999950813.78940010070.0031419999431813.83520030980.002049000002447249.700295596.410131740526878.915727562.70535047658False9736.436587414.062317917159094.9412126340.1832715479False10496.393783432.434582460510003.684547825.5312560102False11005.257008925.786534719710527.371434130.4747394022False11072.353931932.042162057710614.997008220.0326090265FalseFalse3
2190692655307894027265.53070944768.911089986215.52550029750.0040099998004715.56340026860.0024309998843814.58189964290.00072700000600914.64039993290.0020129999611514.14260005950.0015210000565314.18579959870.0011530000483613.93140029910.00099800003226813.97350025180.001623000018313.78670024870.0020260000601413.83500003810.00152699998582237.68979358.264563331432160.925988424.83838597716False5336.298841853.573141683125056.383749019.37474521502False7997.6054710711.20378810277685.642168448.16178529652False9714.934578848.929890134159345.4431360513.9699259492False11099.921323620.712619852510616.95519914.9318871076FalseFalse3
3190832651577179648265.15765750769.032427326215.22389984130.0040670000016715.26939964290.0079009998589814.66100025180.00078300002496714.70790004730.0015709999715914.44950008390.001156999962414.50920009610.002215000102314.36769962310.0019620000384714.42520046230.0053710001520814.3171997070.0018589999526714.36079978940.003620000090452954.2011541611.0659808952832.9580449120.6156900868False4961.350385373.577975440624751.601175276.87530293145False6028.370923256.424051165475705.8448913611.6404398809False6500.1016869611.74613106216164.8116880830.4965463702False6809.5767195411.65937245526541.541248421.8104359117FalseFalse3
4190982647386142438264.73855532769.151391426214.86209964750.0030829999595914.91709995270.0070119998417814.44149971010.0008249999955314.49890041350.0055909999646214.29730033870.0025470000691714.36610031130.0025569999124914.26360034940.002038999926314.32689952850.0017099999822714.23719978330.0026670000515914.30760002140.003421999979764122.4950323911.70602210973918.8624381125.3091521454False6072.955739894.614554102835760.2301312829.662312373False6935.533351516.26988567636509.6835262415.3308512086False7154.1799568313.4354664986749.0118176310.6294789083False7330.2719430418.00606405556870.0514806921.6528802529FalseFalse3
5191022651184291037265.11839793769.183577886214.28999996190.0024570000823614.35389995570.0054879998788213.95979976650.00090099999215514.01790046690.0025609999429413.84490013120.0020190000068413.91390037540.0027769999578613.83590030670.0008999999845413.88679981230.0027369998861113.81879997250.003238999983313.88669967650.003401000052696982.3242860915.80086513726583.2449960433.2758984938False9464.116842497.853813074918970.9784461521.1604579345False10520.58640419.56374295029872.7888733125.2517454654False10608.15550228.7934249094610122.32028925.5170541028False10776.556519432.148938419210123.253898331.7104531382FalseFalse3
6190822654421335368265.44183701769.020465626215.12189960480.0030960000585815.1894998550.0039249998517314.7174997330.0010539999930214.76910018920.0023519999813314.55809974670.0013109999708814.61180019380.0017160000279514.53670024870.0034179999493114.59119987490.0027469999622614.50800037380.0027419999241814.57219982150.004133000038563245.190217039.25372945843049.299327511.0233953293False4709.77442234.572107097914491.174446829.72910562323False5454.564747226.586253818795191.345368658.20489245794False5563.1388616617.51328569855290.7842257913.3861098164False5712.1532494214.42590171375384.1862103520.495624736FalseFalse3
7190952647902459389264.79021141769.132094836212.34350013730.0010860000038512.82180023190.0010860000038511.14140033720.0010860000038513.33129978180.082465000450611.50279998780.0016189999878411.88609981540.0016909999540110.42790031430.00108600000385nannan10.05819988250.010.07960033420.041937.249662241.947439069526994.787134827.0013460027False126893.642811126.92447389916884.18442491282.40568023False90966.1899263135.64460661763908.713306399.5358087793False244816.043102244.875525553nannanFalse344128.0289510.0337411.488940.0FalseFalse3
8190762653395766088265.33952690768.971131576212.83460044860.0010860000038512.88049983980.0010860000038512.23299980160.0010860000038512.52989959720.0010860000038512.9261999130.019184999167913.66349983220.042027000337811.87360000610.0090979998931311.91699981690.010944000445311.72220039370.0029309999663411.75660037990.0038469999562926678.402502626.684884499225574.082632125.5802963144False46430.14921146.441430248935321.583171335.3301651837False24519.9756628433.26888301912433.6909137481.287010547False64648.7299718541.72840124262115.5125588626.111751754False74322.4200552200.63714346672004.5346572255.127756139FalseFalse3
9190722653912264533265.39117483768.936484876212.7140998840.0011909999884712.7580995560.0010860000038512.30280017850.0010860000038512.35519981380.0010860000038512.69429969790.2155240029112.64470005040.16099299490512.19279956820.0010860000038512.25209999080.0010860000038512.18680000310.00085999997099912.23229980470.0006500000017729809.867549332.699979748128625.967746428.6329229389False43539.14823443.549726851341487.768344841.4978485426False30358.48681586026.3096801631777.5184734711.97133078False48181.484055348.193190611445620.496095945.6315804141False48448.462433338.375506043746460.093355727.8143628545FalseFalse3
\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "master_catalogue[:10].show_in_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## III - Merging flags and stellarity\n", "\n", "Each pristine catalogue contains a flag indicating if the source was associated to a another nearby source that was removed during the cleaning process. We merge these flags in a single one." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "flag_cleaned_columns = [column for column in master_catalogue.colnames\n", " if 'flag_cleaned' in column]\n", "\n", "flag_column = np.zeros(len(master_catalogue), dtype=bool)\n", "for column in flag_cleaned_columns:\n", " flag_column |= master_catalogue[column]\n", " \n", "master_catalogue.add_column(Column(data=flag_column, name=\"flag_cleaned\"))\n", "master_catalogue.remove_columns(flag_cleaned_columns)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each pristine catalogue contains a flag indicating the probability of a source being a Gaia object (0: not a Gaia object, 1: possibly, 2: probably, 3: definitely). We merge these flags taking the highest value." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "flag_gaia_columns = [column for column in master_catalogue.colnames\n", " if 'flag_gaia' in column]\n", "\n", "master_catalogue.add_column(Column(\n", " data=np.max([master_catalogue[column] for column in flag_gaia_columns], axis=0),\n", " name=\"flag_gaia\"\n", "))\n", "master_catalogue.remove_columns(flag_gaia_columns)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each prisitine catalogue may contain one or several stellarity columns indicating the probability (0 to 1) of each source being a star. We merge these columns taking the highest value." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# No ps1 stellarity\n", "#stellarity_columns = [column for column in master_catalogue.colnames\n", "# if 'stellarity' in column]\n", "#\n", "#master_catalogue.add_column(Column(\n", "# data=np.nanmax([master_catalogue[column] for column in stellarity_columns], axis=0),\n", "# name=\"stellarity\"\n", "#))\n", "#master_catalogue.remove_columns(stellarity_columns)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IV - Adding E(B-V) column" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "master_catalogue.add_column(\n", " ebv(master_catalogue['ra'], master_catalogue['dec'])\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## V - Adding HELP unique identifiers and field columns" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "master_catalogue.add_column(Column(gen_help_id(master_catalogue['ra'], master_catalogue['dec']),\n", " name=\"help_id\"))\n", "master_catalogue.add_column(Column(np.full(len(master_catalogue), \"SPIRE-NEP\", dtype='= 2\n", "has_nir_flux = nb_nir_flux >= 2\n", "has_mir_flux = nb_mir_flux >= 2\n", "\n", "master_catalogue.add_column(\n", " Column(\n", " 1 * has_optical_flux + 2 * has_nir_flux + 4 * has_mir_flux,\n", " name=\"flag_optnir_det\")\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IX - Cross-identification table\n", "\n", "We are producing a table associating to each HELP identifier, the identifiers of the sources in the pristine catalogue. This can be used to easily get additional information from them.\n", "\n", "For convenience, we also cross-match the master list with the SDSS catalogue and add the objID associated with each source, if any. **TODO: should we correct the astrometry with respect to Gaia positions?**" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 master list rows had multiple associations.\n" ] } ], "source": [ "#\n", "# Addind SDSS ids\n", "#\n", "sdss = Table.read(\"../../dmu0/dmu0_SDSS-DR13/data/SDSS-DR13_SPIRE-NEP.fits\")['objID', 'ra', 'dec']\n", "sdss_coords = SkyCoord(sdss['ra'] * u.deg, sdss['dec'] * u.deg)\n", "idx_ml, d2d, _ = sdss_coords.match_to_catalog_sky(SkyCoord(master_catalogue['ra'], master_catalogue['dec']))\n", "idx_sdss = np.arange(len(sdss))\n", "\n", "# Limit the cross-match to 1 arcsec\n", "mask = d2d <= 1. * u.arcsec\n", "idx_ml = idx_ml[mask]\n", "idx_sdss = idx_sdss[mask]\n", "d2d = d2d[mask]\n", "nb_orig_matches = len(idx_ml)\n", "\n", "# In case of multiple associations of one master list object to an SDSS object, we keep only the\n", "# association to the nearest one.\n", "sort_idx = np.argsort(d2d)\n", "idx_ml = idx_ml[sort_idx]\n", "idx_sdss = idx_sdss[sort_idx]\n", "_, unique_idx = np.unique(idx_ml, return_index=True)\n", "idx_ml = idx_ml[unique_idx]\n", "idx_sdss = idx_sdss[unique_idx]\n", "print(\"{} master list rows had multiple associations.\".format(nb_orig_matches - len(idx_ml)))\n", "\n", "# Adding the ObjID to the master list\n", "master_catalogue.add_column(Column(data=np.full(len(master_catalogue), -1, dtype='>i8'), name=\"sdss_id\"))\n", "master_catalogue['sdss_id'][idx_ml] = sdss['objID'][idx_sdss]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ps1_id', 'help_id', 'specz_id', 'sdss_id']\n" ] } ], "source": [ "id_names = []\n", "for col in master_catalogue.colnames:\n", " if '_id' in col:\n", " id_names += [col]\n", " if '_intid' in col:\n", " id_names += [col]\n", " \n", "print(id_names)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "master_catalogue[id_names].write(\n", " \"{}/master_list_cross_ident_spire-nep{}.fits\".format(OUT_DIR, SUFFIX), overwrite=True)\n", "id_names.remove('help_id')\n", "master_catalogue.remove_columns(id_names)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## X - Adding HEALPix index\n", "\n", "We are adding a column with a HEALPix index at order 13 associated with each source." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "master_catalogue.add_column(Column(\n", " data=coords_to_hpidx(master_catalogue['ra'], master_catalogue['dec'], order=13),\n", " name=\"hp_idx\"\n", "))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## XI - Saving the catalogue" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "columns = [\"help_id\", \"field\", \"ra\", \"dec\", \"hp_idx\"]\n", "\n", "bands = [column[5:] for column in master_catalogue.colnames if 'f_ap' in column]\n", "for band in bands:\n", " columns += [\"f_ap_{}\".format(band), \"ferr_ap_{}\".format(band),\n", " \"m_ap_{}\".format(band), \"merr_ap_{}\".format(band),\n", " \"f_{}\".format(band), \"ferr_{}\".format(band),\n", " \"m_{}\".format(band), \"merr_{}\".format(band),\n", " \"flag_{}\".format(band)] \n", " \n", "columns += [\"flag_cleaned\", \"flag_gaia\", \n", " \"flag_optnir_obs\", \"flag_optnir_det\", \"ebv\", 'zspec_association_flag', 'zspec_qual', 'zspec'] #\"stellarity\", \"flag_merged\"" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Missing columns: set()\n" ] } ], "source": [ "# We check for columns in the master catalogue that we will not save to disk.\n", "print(\"Missing columns: {}\".format(set(master_catalogue.colnames) - set(columns)))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "master_catalogue[columns].write(\"{}/master_catalogue_spire-nep{}.fits\".format(OUT_DIR, SUFFIX))" ] } ], "metadata": { "kernelspec": { "display_name": "Python (herschelhelp_internal)", "language": "python", "name": "helpint" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }