xFLS master catalogue

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

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: 
255270d (Fri Nov 24 10:35:51 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]:
spitzer = Table.read("{}/Spitzer.fits".format(TMP_DIR))
wfc = Table.read("{}/INT-WFC.fits".format(TMP_DIR))
kpno = Table.read("{}/KPNO.fits".format(TMP_DIR))
ps1 = Table.read("{}/PS1.fits".format(TMP_DIR))
legacy = Table.read("{}/LegacySurvey.fits".format(TMP_DIR))
uhs = Table.read("{}/UHS.fits".format(TMP_DIR))                

II - Merging tables

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.

WFC

In [5]:
master_catalogue = wfc
master_catalogue['wfc_ra'].name = 'ra'
master_catalogue['wfc_dec'].name = 'dec'

Add PanSTARRS

In [6]:
nb_merge_dist_plot(
    SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
    SkyCoord(ps1['ps1_ra'], ps1['ps1_dec'])
)
In [7]:
# Given the graph above, we use 0.8 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, ps1, "ps1_ra", "ps1_dec", radius=0.8*u.arcsec)

Add Legacy Survey

In [8]:
nb_merge_dist_plot(
    SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
    SkyCoord(legacy['legacy_ra'], legacy['legacy_dec'])
)
In [9]:
# Given the graph above, we use 0.8 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, legacy, "legacy_ra", "legacy_dec", radius=0.8*u.arcsec)

Add KPNO

In [10]:
nb_merge_dist_plot(
    SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
    SkyCoord(kpno['kpno_ra'], kpno['kpno_dec'])
)
In [11]:
# Given the graph above, we use 0.8 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, kpno, "kpno_ra", "kpno_dec", radius=0.8*u.arcsec)

Add UHS

In [12]:
nb_merge_dist_plot(
    SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
    SkyCoord(uhs['uhs_ra'], uhs['uhs_dec'])
)
In [13]:
# Given the graph above, we use 0.8 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, uhs, "uhs_ra", "uhs_dec", radius=0.8*u.arcsec)

Add Spitzer

In [14]:
nb_merge_dist_plot(
    SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
    SkyCoord(spitzer['spitzer_ra'], spitzer['spitzer_dec'])
)
In [15]:
# Given the graph above, we use 1 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, spitzer, "spitzer_ra", "spitzer_dec", radius=1.*u.arcsec)

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 [16]:
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 [17]:
master_catalogue[:10].show_in_notebook()
Out[17]:
<Table length=10>
idxwfc_idradecwfc_stellaritym_ap_wfc_umerr_ap_wfc_um_wfc_umerr_wfc_um_ap_wfc_gmerr_ap_wfc_gm_wfc_gmerr_wfc_gm_ap_wfc_rmerr_ap_wfc_rm_wfc_rmerr_wfc_rm_ap_wfc_imerr_ap_wfc_im_wfc_imerr_wfc_im_ap_wfc_zmerr_ap_wfc_zm_wfc_zmerr_wfc_zf_ap_wfc_uferr_ap_wfc_uf_wfc_uferr_wfc_uflag_wfc_uf_ap_wfc_gferr_ap_wfc_gf_wfc_gferr_wfc_gflag_wfc_gf_ap_wfc_rferr_ap_wfc_rf_wfc_rferr_wfc_rflag_wfc_rf_ap_wfc_iferr_ap_wfc_if_wfc_iferr_wfc_iflag_wfc_if_ap_wfc_zferr_ap_wfc_zf_wfc_zferr_wfc_zflag_wfc_zwfc_flag_cleanedwfc_flag_gaiaflag_mergedps1_idm_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_gaialegacy_idf_bass_gferr_bass_gf_ap_bass_gferr_ap_bass_gf_bass_rferr_bass_rf_ap_bass_rferr_ap_bass_rf_bass_zferr_bass_zf_ap_bass_zferr_ap_bass_zlegacy_stellaritym_bass_gmerr_bass_gflag_bass_gm_ap_bass_gmerr_ap_bass_gm_bass_rmerr_bass_rflag_bass_rm_ap_bass_rmerr_ap_bass_rm_bass_zmerr_bass_zflag_bass_zm_ap_bass_zmerr_ap_bass_zlegacy_flag_cleanedlegacy_flag_gaiakpno_intidkpno_stellaritym_ap_kpno_rmerr_ap_kpno_rm_kpno_rmerr_kpno_rf_ap_kpno_rferr_ap_kpno_rf_kpno_rferr_kpno_rflag_kpno_rkpno_flag_cleanedkpno_flag_gaiauhs_iduhs_stellaritym_wfcam_jmerr_wfcam_jm_ap_wfcam_jmerr_ap_wfcam_jf_wfcam_jferr_wfcam_jflag_wfcam_jf_ap_wfcam_jferr_ap_wfcam_juhs_flag_cleaneduhs_flag_gaiaspitzer_intidspitzer_stellarityf_irac_i1ferr_irac_i1f_ap_irac_i1ferr_ap_irac_i1f_irac_i2ferr_irac_i2f_ap_irac_i2ferr_ap_irac_i2f_irac_i3ferr_irac_i3f_ap_irac_i3ferr_ap_irac_i3f_irac_i4ferr_irac_i4f_ap_irac_i4ferr_ap_irac_i4m_irac_i1merr_irac_i1flag_irac_i1m_ap_irac_i1merr_ap_irac_i1m_irac_i2merr_irac_i2flag_irac_i2m_ap_irac_i2merr_ap_irac_i2m_irac_i3merr_irac_i3flag_irac_i3m_ap_irac_i3merr_ap_irac_i3m_irac_i4merr_irac_i4flag_irac_i4m_ap_irac_i4merr_ap_irac_i4spitzer_flag_cleanedspitzer_flag_gaia
degdeguJyuJyuJyuJyuJyuJymagmagmagmaguJyuJyuJyuJyuJyuJyuJyuJyuJyuJyuJyuJyuJyuJyuJyuJy
0465698102781258.81538617659.58032674570.013.3970.03513.4470.03515.0120.01812.0370.01814.7310.01611.8570.01614.4430.02211.6210.02212.4240.05511.4810.05515892.8512.32315177.5489.265False3590.8759.531755616.0922.036False4651.5768.548165644.7967.376112938False6064.57122.88581583.01653.09621394False38940.41972.692811.14701.52FalseFalse3True-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
1465698400822259.20539629759.80060917470.016.3140.03516.3150.03516.8020.01816.6960.01815.2980.01614.6340.01615.2270.02214.5990.022nannannannan1082.4334.89351081.4334.8613False690.55711.4485761.37812.6226False2759.3140.66265086.2874.9541297555False2945.7859.68955252.91106.438377872FalsenannannannanFalseFalse0False-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
2465698403144258.71022409559.67323626570.014.720.03514.6510.03514.9970.01814.0970.01814.8090.01613.7720.01614.6050.02213.7950.022nannannannan4698.94151.4765007.26161.415False3640.8360.35988340.65138.276False4329.1263.796311251.2165.804132819False5223.96105.85211015.4223.202072084FalsenannannannanFalseFalse2True-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
3465698103597258.65480092259.59281758670.0nannannannan15.0020.01813.8980.01814.7420.01613.3350.01614.480.02213.2830.022nannannannannannannannanFalse3624.160.082510018.4166.092False4604.6867.857116826.7247.967913747False5861.38118.76817652.2357.682913542FalsenannannannanFalseFalse1False-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
4465698102599258.86989590659.59541323770.0nannannannan15.0030.01812.7860.01814.7220.01612.480.01614.4750.02212.4360.022nannannannannannannannanFalse3620.7660.027227899.7462.539False4690.2969.118736982.8544.999063015False5888.43119.31638512.3780.365847051FalsenannannannanFalseFalse0False-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
5465698402740258.78187797359.61599536170.0524781nannannannan16.7750.01816.8230.01817.5140.01617.4730.01617.40.02217.3660.022nannannannannannannannanFalse707.94611.7368677.32911.2292False358.4265.28197372.225.4852408357False398.1078.06674410.7718.3233587211FalsenannannannanFalseFalse2False-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
6465698302968258.76848370959.93841388770.0nannannannan15.0310.01812.1520.01814.9630.01612.3670.01614.6790.02212.2880.022nannannannannannannannanFalse3528.5858.498950026.4829.369False3756.6455.359941039.3604.77745533False4879.7798.877744136.7894.330792129FalsenannannannanFalseFalse0False-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
7465698301564259.05341508659.82522388970.0nannannannan15.1250.01813.5040.01814.950.01613.1380.01614.6660.02212.8870.022nannannannannannannannanFalse3235.9353.647314401.2238.753False3801.8956.026820174.4297.300547361False4938.55100.06925421.4515.107814223FalsenannannannanFalseFalse1False-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
8465698300805259.2109437359.82084360170.0nannannannan17.1140.0316.7810.0314.9760.01613.5360.01614.6280.02412.7190.024nannannannannannannannanFalse518.08414.3152704.04419.4535False3711.9354.70113983.0206.060796976False5114.46113.05429675.6655.974403024FalsenannannannanFalseFalse1False-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0
9465698303358258.6944995259.89706631170.0nannannannan17.3330.0317.0910.0315.0450.01613.8870.01614.8440.02213.7340.022nannannannannannannannanFalse423.44811.7003529.17614.6217False3483.3751.332910120.4149.140462279False4191.7984.937211652.0236.101187766FalsenannannannanFalseFalse2True-1nannannannannannannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalseFalse0-1nannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0-1nannannannannannannannannanFalseFalse0-1nannannannannannannanFalsenannanFalse0-1nannannannannannannannannannannannannannannannannannannanFalsenannannannanFalsenannannannanFalsenannannannanFalsenannanFalse0

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 [18]:
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 [19]:
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. We keep trace of the origin of the stellarity.

In [20]:
stellarity_columns = [column for column in master_catalogue.colnames
                      if 'stellarity' in column]

print(", ".join(stellarity_columns))
wfc_stellarity, legacy_stellarity, kpno_stellarity, uhs_stellarity, spitzer_stellarity
In [21]:
# We create an masked array with all the stellarities and get the maximum value, as well as its
# origin.  Some sources may not have an associated stellarity.
stellarity_array = np.array([master_catalogue[column] for column in stellarity_columns])
stellarity_array = np.ma.masked_array(stellarity_array, np.isnan(stellarity_array))

max_stellarity = np.max(stellarity_array, axis=0)
max_stellarity.fill_value = np.nan

no_stellarity_mask = max_stellarity.mask

master_catalogue.add_column(Column(data=max_stellarity.filled(), name="stellarity"))

stellarity_origin = np.full(len(master_catalogue), "NO_INFORMATION", dtype="S20")
stellarity_origin[~no_stellarity_mask] = np.array(stellarity_columns)[np.argmax(stellarity_array, axis=0)[~no_stellarity_mask]]

master_catalogue.add_column(Column(data=stellarity_origin, name="stellarity_origin"))

master_catalogue.remove_columns(stellarity_columns)

IV - Adding E(B-V) column

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

V - Adding HELP unique identifiers and field columns

In [23]:
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), "xFLS", dtype='<U18'),
                                   name="field"))
In [24]:
# 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 [25]:
specz =  Table.read("../../dmu23/dmu23_xFLS/data/xFLS-specz-v2.fits")
In [26]:
nb_merge_dist_plot(
    SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
    SkyCoord(specz['ra'] * u.deg, specz['dec'] * u.deg)
)
In [27]:
master_catalogue = specz_merge(master_catalogue, specz, radius=1. * u.arcsec)

VII - Choosing between multiple values for the same filter

No overlapping filters on xFLS.

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 [28]:
spitzer_moc = MOC(filename="../../dmu0/dmu0_DataFusion-Spitzer/data/Datafusion-xFLS_MOC.fits")
wfc_moc = MOC(filename="../../dmu0/dmu0_INTWFC/data/fls_intwfc_v2.1_HELP_coverage_MOC.fits")
kpno_moc = MOC(filename="../../dmu0/dmu0_KPNO-FLS/data/KPNO-FLS_xFLS_MOC.fits")
ps1_moc = MOC(filename="../../dmu0/dmu0_PanSTARRS1-3SS/data/PanSTARRS1-3SS_xFLS_v2_MOC.fits")
legacy_moc = MOC(filename="../../dmu0/dmu0_LegacySurvey/data/LegacySurvey-dr4_xFLS_MOC.fits")
uhs_moc = MOC(filename="../../dmu0/dmu0_UHS/data/UHS-DR1_xFLS_MOC.fits")
In [29]:
was_observed_optical = inMoc(
    master_catalogue['ra'], master_catalogue['dec'],
    wfc_moc + legacy_moc + kpno_moc + ps1_moc) 

was_observed_nir = inMoc(
    master_catalogue['ra'], master_catalogue['dec'],
    uhs_moc
)

was_observed_mir = inMoc(
    master_catalogue['ra'], master_catalogue['dec'],
    spitzer_moc
)
In [30]:
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 [31]:
# 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_wfc_u']) +
    1 * ~np.isnan(master_catalogue['f_wfc_g']) +
    1 * ~np.isnan(master_catalogue['f_wfc_r']) +
    1 * ~np.isnan(master_catalogue['f_wfc_i']) +
    1 * ~np.isnan(master_catalogue['f_wfc_z']) +
    1 * ~np.isnan(master_catalogue['f_bass_g']) +
    1 * ~np.isnan(master_catalogue['f_bass_r']) +
    1 * ~np.isnan(master_catalogue['f_bass_z']) +
    1 * ~np.isnan(master_catalogue['f_kpno_r']) +
    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 = (
    1 * ~np.isnan(master_catalogue['f_wfcam_j']) 
)

nb_mir_flux = (
    1 * ~np.isnan(master_catalogue['f_irac_i1']) +
    1 * ~np.isnan(master_catalogue['f_irac_i2']) +
    1 * ~np.isnan(master_catalogue['f_irac_i3']) +
    1 * ~np.isnan(master_catalogue['f_irac_i4'])
)
In [32]:
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 catalogues. 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 [33]:
#
# Addind SDSS ids
#
sdss = Table.read("../../dmu0/dmu0_SDSS-DR13/data/SDSS-DR13_xFLS.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]
91 master list rows had multiple associations.
In [34]:
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)
master_catalogue[id_names].write(
    "{}/master_list_cross_ident_xfls{}.fits".format(OUT_DIR, SUFFIX))
id_names.remove('help_id')
master_catalogue.remove_columns(id_names)
['wfc_id', 'ps1_id', 'legacy_id', 'kpno_intid', 'uhs_id', 'spitzer_intid', 'help_id', 'specz_id', 'sdss_id']

X - Adding HEALPix index

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

In [35]:
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 [36]:
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 += ["stellarity", "stellarity_origin", "flag_cleaned", "flag_merged", "flag_gaia", "flag_optnir_obs", 
            "flag_optnir_det", "zspec", "zspec_qual", "zspec_association_flag", "ebv"]
In [37]:
# 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 [38]:
master_catalogue[columns].write("{}/master_catalogue_xfls{}.fits".format(OUT_DIR, SUFFIX))