SPIRE-NEP master catalogue

This notebook presents the merge of the various pristine catalogues to produce HELP mater catalogue on SPIRE-NEP.

In [1]:
from herschelhelp_internal import git_version
print("This notebook was run with herschelhelp_internal version: \n{}".format(git_version()))
This notebook was run with herschelhelp_internal version: 
04829ed (Thu Nov 2 16:57:19 2017 +0000)
In [2]:
%matplotlib inline
#%config InlineBackend.figure_format = 'svg'

import matplotlib.pyplot as plt
plt.rc('figure', figsize=(10, 6))

import os
import time

from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.table import Column, Table
import numpy as np
from pymoc import MOC

from herschelhelp_internal.masterlist import merge_catalogues, nb_merge_dist_plot, specz_merge
from herschelhelp_internal.utils import coords_to_hpidx, ebv, gen_help_id, inMoc
In [3]:
TMP_DIR = os.environ.get('TMP_DIR', "./data_tmp")
OUT_DIR = os.environ.get('OUT_DIR', "./data")
SUFFIX = os.environ.get('SUFFIX', time.strftime("_%Y%m%d"))

try:
    os.makedirs(OUT_DIR)
except FileExistsError:
    pass

I - Reading the prepared pristine catalogues

In [4]:
ps1 = Table.read("{}/PS1.fits".format(TMP_DIR))

II - Merging tables

We first merge the optical catalogues and then add the infrared ones: DECaLS, HSC, KIDS, PanSTARRS, UKIDSS-LAS, and VISTA-VIKING.

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.

DECaLS

In [5]:
master_catalogue = ps1
master_catalogue['ps1_ra'].name = 'ra'
master_catalogue['ps1_dec'].name = 'dec'

Cleaning

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.

In [6]:
for col in master_catalogue.colnames:
    if "m_" in col or "merr_" in col or "f_" in col or "ferr_" in col or "stellarity" in col:
        master_catalogue[col].fill_value = np.nan
    elif "flag" in col:
        master_catalogue[col].fill_value = 0
    elif "id" in col:
        master_catalogue[col].fill_value = -1
        
master_catalogue = master_catalogue.filled()
In [7]:
master_catalogue[:10].show_in_notebook()
Out[7]:
<Table length=10>
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

III - Merging flags and stellarity

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.

In [8]:
flag_cleaned_columns = [column for column in master_catalogue.colnames
                        if 'flag_cleaned' in column]

flag_column = np.zeros(len(master_catalogue), dtype=bool)
for column in flag_cleaned_columns:
    flag_column |= master_catalogue[column]
    
master_catalogue.add_column(Column(data=flag_column, name="flag_cleaned"))
master_catalogue.remove_columns(flag_cleaned_columns)

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.

In [9]:
flag_gaia_columns = [column for column in master_catalogue.colnames
                     if 'flag_gaia' in column]

master_catalogue.add_column(Column(
    data=np.max([master_catalogue[column] for column in flag_gaia_columns], axis=0),
    name="flag_gaia"
))
master_catalogue.remove_columns(flag_gaia_columns)

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.

In [10]:
# No ps1 stellarity
#stellarity_columns = [column for column in master_catalogue.colnames
#                      if 'stellarity' in column]
#
#master_catalogue.add_column(Column(
#    data=np.nanmax([master_catalogue[column] for column in stellarity_columns], axis=0),
#    name="stellarity"
#))
#master_catalogue.remove_columns(stellarity_columns)

IV - Adding E(B-V) column

In [11]:
master_catalogue.add_column(
    ebv(master_catalogue['ra'], master_catalogue['dec'])
)

V - Adding HELP unique identifiers and field columns

In [12]:
master_catalogue.add_column(Column(gen_help_id(master_catalogue['ra'], master_catalogue['dec']),
                                   name="help_id"))
master_catalogue.add_column(Column(np.full(len(master_catalogue), "SPIRE-NEP", dtype='<U18'),
                                   name="field"))
In [13]:
# Check that the HELP Ids are unique
if len(master_catalogue) != len(np.unique(master_catalogue['help_id'])):
    print("The HELP IDs are not unique!!!")
else:
    print("OK!")
OK!

VI - Cross-matching with spec-z catalogue

In [14]:
#specz =  Table.read("../../dmu23/dmu23_GAMA-12/data/GAMA12-specz-v2-PRIVATE.csv")
In [15]:
#specz.rename_column('RA', 'ra')
#specz.rename_column('DEC', 'dec')
#specz.rename_column('OBJID', 'specz_id')
#specz.rename_column('Z_SPEC', 'z_spec')
#specz.rename_column('Z_SOURCE', 'z_source')
#specz.rename_column('Z_QUAL', 'z_qual')
#specz.rename_column('REL', 'rel')
#specz.rename_column('AGN', 'agn')
In [16]:
#specz[:10].show_in_notebook()
In [17]:
#nb_merge_dist_plot(
#    SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
#    SkyCoord(specz['ra'] * u.deg, specz['dec'] * u.deg)
#)
In [18]:
#master_catalogue = specz_merge(master_catalogue, specz, radius=1. * u.arcsec)

VII - Choosing between multiple values for the same filter

We only have PanSTARRS on SPIRE-NEP

VIII.a Wavelength domain coverage

We add a binary flag_optnir_obs indicating that a source was observed in a given wavelength domain:

  • 1 for observation in optical;
  • 2 for observation in near-infrared;
  • 4 for observation in mid-infrared (IRAC).

It's an integer binary flag, so a source observed both in optical and near-infrared by not in mid-infrared would have this flag at 1 + 2 = 3.

Note 1: The observation flag is based on the creation of multi-order coverage maps from the catalogues, this may not be accurate, especially on the edges of the coverage.

Note 2: Being on the observation coverage does not mean having fluxes in that wavelength domain. For sources observed in one domain but having no flux in it, one must take into consideration de different depths in the catalogue we are using.

In [19]:
ps1_moc = MOC(filename="../../dmu0/dmu0_PanSTARRS1-3SS/data/PanSTARRS1-3SS_SPIRE-NEP_MOC.fits")
In [20]:
was_observed_optical = inMoc(
    master_catalogue['ra'], master_catalogue['dec'],
    ps1_moc) 

was_observed_nir = np.zeros(len(master_catalogue), dtype=bool)

was_observed_mir = np.zeros(len(master_catalogue), dtype=bool)
In [21]:
master_catalogue.add_column(
    Column(
        1 * was_observed_optical + 2 * was_observed_nir + 4 * was_observed_mir,
        name="flag_optnir_obs")
)

VIII.b Wavelength domain detection

We add a binary flag_optnir_det indicating that a source was detected in a given wavelength domain:

  • 1 for detection in optical;
  • 2 for detection in near-infrared;
  • 4 for detection in mid-infrared (IRAC).

It's an integer binary flag, so a source detected both in optical and near-infrared by not in mid-infrared would have this flag at 1 + 2 = 3.

Note 1: We use the total flux columns to know if the source has flux, in some catalogues, we may have aperture flux and no total flux.

To get rid of artefacts (chip edges, star flares, etc.) we consider that a source is detected in one wavelength domain when it has a flux value in at least two bands. That means that good sources will be excluded from this flag when they are on the coverage of only one band.

In [22]:
# SpARCS is a catalogue of sources detected in r (with fluxes measured at 
# this prior position in the other bands).  Thus, we are only using the r
# CFHT band.
# Check to use catalogue flags from HSC and PanSTARRS.
nb_optical_flux = (
    1 * ~np.isnan(master_catalogue['f_gpc1_g']) +
    1 * ~np.isnan(master_catalogue['f_gpc1_r']) +
    1 * ~np.isnan(master_catalogue['f_gpc1_i']) +
    1 * ~np.isnan(master_catalogue['f_gpc1_z']) +
    1 * ~np.isnan(master_catalogue['f_gpc1_y']) 
)

nb_nir_flux = np.zeros(len(master_catalogue), dtype=float)

nb_mir_flux = np.zeros(len(master_catalogue), dtype=float)
In [23]:
has_optical_flux = nb_optical_flux >= 2
has_nir_flux = nb_nir_flux >= 2
has_mir_flux = nb_mir_flux >= 2

master_catalogue.add_column(
    Column(
        1 * has_optical_flux + 2 * has_nir_flux + 4 * has_mir_flux,
        name="flag_optnir_det")
)

IX - Cross-identification table

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.

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?

In [24]:
#
# Addind SDSS ids
#
sdss = Table.read("../../dmu0/dmu0_SDSS-DR13/data/SDSS-DR13_SPIRE-NEP.fits")['objID', 'ra', 'dec']
sdss_coords = SkyCoord(sdss['ra'] * u.deg, sdss['dec'] * u.deg)
idx_ml, d2d, _ = sdss_coords.match_to_catalog_sky(SkyCoord(master_catalogue['ra'], master_catalogue['dec']))
idx_sdss = np.arange(len(sdss))

# Limit the cross-match to 1 arcsec
mask = d2d <= 1. * u.arcsec
idx_ml = idx_ml[mask]
idx_sdss = idx_sdss[mask]
d2d = d2d[mask]
nb_orig_matches = len(idx_ml)

# In case of multiple associations of one master list object to an SDSS object, we keep only the
# association to the nearest one.
sort_idx = np.argsort(d2d)
idx_ml = idx_ml[sort_idx]
idx_sdss = idx_sdss[sort_idx]
_, unique_idx = np.unique(idx_ml, return_index=True)
idx_ml = idx_ml[unique_idx]
idx_sdss = idx_sdss[unique_idx]
print("{} master list rows had multiple associations.".format(nb_orig_matches - len(idx_ml)))

# Adding the ObjID to the master list
master_catalogue.add_column(Column(data=np.full(len(master_catalogue), -1, dtype='>i8'), name="sdss_id"))
master_catalogue['sdss_id'][idx_ml] = sdss['objID'][idx_sdss]
4 master list rows had multiple associations.
In [25]:
id_names = []
for col in master_catalogue.colnames:
    if '_id' in col:
        id_names += [col]
    if '_intid' in col:
        id_names += [col]
        
print(id_names)
['ps1_id', 'help_id', 'sdss_id']
In [26]:
master_catalogue[id_names].write(
    "{}/master_list_cross_ident_spire-nep{}.fits".format(OUT_DIR, SUFFIX), overwrite=True)
id_names.remove('help_id')
master_catalogue.remove_columns(id_names)

X - Adding HEALPix index

We are adding a column with a HEALPix index at order 13 associated with each source.

In [27]:
master_catalogue.add_column(Column(
    data=coords_to_hpidx(master_catalogue['ra'], master_catalogue['dec'], order=13),
    name="hp_idx"
))

XI - Saving the catalogue

In [28]:
columns = ["help_id", "field", "ra", "dec", "hp_idx"]

bands = [column[5:] for column in master_catalogue.colnames if 'f_ap' in column]
for band in bands:
    columns += ["f_ap_{}".format(band), "ferr_ap_{}".format(band),
                "m_ap_{}".format(band), "merr_ap_{}".format(band),
                "f_{}".format(band), "ferr_{}".format(band),
                "m_{}".format(band), "merr_{}".format(band),
                "flag_{}".format(band)]    
    
columns += ["flag_cleaned", "flag_gaia", 
            "flag_optnir_obs", "flag_optnir_det", "ebv"] #"stellarity", "flag_merged"
In [29]:
# We check for columns in the master catalogue that we will not save to disk.
print("Missing columns: {}".format(set(master_catalogue.colnames) - set(columns)))
Missing columns: set()
In [30]:
master_catalogue[columns].write("{}/master_catalogue_spire-nep{}.fits".format(OUT_DIR, SUFFIX))