This notebook presents the merge of the various pristine catalogues to produce HELP mater catalogue on ELAIS-N2.
from herschelhelp_internal import git_version
print("This notebook was run with herschelhelp_internal version: \n{}".format(git_version()))
%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
from herschelhelp_internal.utils import coords_to_hpidx, ebv, gen_help_id, inMoc
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
wfc = Table.read("{}/INT-WFC.fits".format(TMP_DIR))
rcs = Table.read("{}/RCSLenS.fits".format(TMP_DIR))
ps1 = Table.read("{}/PS1.fits".format(TMP_DIR))
sparcs = Table.read("{}/SpARCS.fits".format(TMP_DIR))
swire= Table.read("{}/SWIRE.fits".format(TMP_DIR))
We first merge the optical catalogues and then add the infrared ones: WFC, DXS, SpARCS, HSC, PS1, SERVS, SWIRE.
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.
master_catalogue = wfc
master_catalogue['wfc_ra'].name = 'ra'
master_catalogue['wfc_dec'].name = 'dec'
nb_merge_dist_plot(
SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
SkyCoord(rcs['rcs_ra'], rcs['rcs_dec'])
)
# Given the graph above, we use 0.8 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, rcs, "rcs_ra", "rcs_dec", radius=0.8*u.arcsec)
nb_merge_dist_plot(
SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
SkyCoord(ps1['ps1_ra'], ps1['ps1_dec'])
)
# 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)
nb_merge_dist_plot(
SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
SkyCoord(sparcs['sparcs_ra'], sparcs['sparcs_dec'])
)
# Given the graph above, we use 0.8 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, sparcs, "sparcs_ra", "sparcs_dec", radius=0.8*u.arcsec)
nb_merge_dist_plot(
SkyCoord(master_catalogue['ra'], master_catalogue['dec']),
SkyCoord(swire['swire_ra'], swire['swire_dec'])
)
# Given the graph above, we use 1 arc-second radius
master_catalogue = merge_catalogues(master_catalogue, swire, "swire_ra", "swire_dec", radius=1.*u.arcsec)
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.
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()
master_catalogue[:10].show_in_notebook()
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.
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.
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.
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)
master_catalogue.add_column(
ebv(master_catalogue['ra'], master_catalogue['dec'])
)
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), "ELAIS-N2", dtype='<U18'),
name="field"))
# 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!")
SpARCS apears to be significantly deeper and contains both total and aperture magnitudes so we take SpARCS over RCSLenS if both are available
Survey | Bands |
---|---|
SpARCS | ugrz |
RCSLenS | grizy |
megacam_origin = Table()
megacam_origin.add_column(master_catalogue['help_id'])
megacam_stats = Table()
megacam_stats.add_column(Column(data=['u','g','r','i','z','y'], name="Band"))
for col in [ "SpARCS", "RCSLenS"]:
megacam_stats.add_column(Column(data=np.full(6, 0), name="{}".format(col)))
megacam_stats.add_column(Column(data=np.full(6, 0), name="use {}".format(col)))
megacam_bands = ['u','g','r','i','z','y'] # Lowercase naming convention (k is Ks)
for band in ['g', 'r', 'z']: #the bands in both catalogues
# Megacam total flux
has_sparcs = ~np.isnan(master_catalogue['f_cfht_megacam_' + band])
has_rcs = ~np.isnan(master_catalogue['f_rcs_' + band])
use_sparcs = has_sparcs
use_rcs = has_rcs & ~has_sparcs
f_megacam = np.full(len(master_catalogue), np.nan)
f_megacam[use_sparcs] = master_catalogue['f_cfht_megacam_' + band][use_sparcs]
f_megacam[use_rcs] = master_catalogue['f_rcs_' + band][use_rcs]
ferr_megacam = np.full(len(master_catalogue), np.nan)
ferr_megacam[use_sparcs] = master_catalogue['ferr_cfht_megacam_' + band][use_sparcs]
ferr_megacam[use_rcs] = master_catalogue['ferr_rcs_' + band][use_rcs]
m_megacam = np.full(len(master_catalogue), np.nan)
m_megacam[use_sparcs] = master_catalogue['m_cfht_megacam_' + band][use_sparcs]
m_megacam[use_rcs] = master_catalogue['m_rcs_' + band][use_rcs]
merr_megacam = np.full(len(master_catalogue), np.nan)
merr_megacam[use_sparcs] = master_catalogue['merr_cfht_megacam_' + band][use_sparcs]
merr_megacam[use_rcs] = master_catalogue['merr_rcs_' + band][use_rcs]
flag_megacam = np.full(len(master_catalogue), np.nan)
flag_megacam[use_sparcs] = master_catalogue['flag_cfht_megacam_' + band][use_sparcs]
flag_megacam[use_rcs] = master_catalogue['flag_rcs_' + band][use_rcs]
master_catalogue.add_column(Column(data=f_megacam, name="f_megacam_" + band))
master_catalogue.add_column(Column(data=ferr_megacam, name="ferr_megacam_" + band))
master_catalogue.add_column(Column(data=m_megacam, name="m_megacam_" + band))
master_catalogue.add_column(Column(data=merr_megacam, name="merr_megacam_" + band))
master_catalogue.add_column(Column(data=flag_megacam, name="flag_megacam_" + band))
old_columns = ['f_cfht_megacam_' + band,
'ferr_cfht_megacam_' + band,
'm_cfht_megacam_' + band,
'merr_cfht_megacam_' + band,
'flag_cfht_megacam_' + band,
'f_rcs_' + band,
'ferr_rcs_' + band,
'm_rcs_' + band,
'merr_rcs_' + band,
'flag_rcs_' + band]
master_catalogue.remove_columns(old_columns)
origin = np.full(len(master_catalogue), ' ', dtype='<U5')
origin[use_sparcs] = "SpARCS"
origin[use_rcs] = "RCSLenS"
megacam_origin.add_column(Column(data=origin, name= 'f_megacam_' + band ))
megacam_stats['RCSLenS'][megacam_stats['Band'] == band] = np.sum(has_rcs)
megacam_stats['SpARCS'][megacam_stats['Band'] == band] = np.sum(has_sparcs)
megacam_stats['use RCSLenS'][megacam_stats['Band'] == band] = np.sum(use_rcs)
megacam_stats['use SpARCS'][megacam_stats['Band'] == band] = np.sum(use_sparcs)
#No aperture fluxes in rcs so just rename all the sparcs columns
for col in master_catalogue.colnames:
if '_cfht_megacam_' in col:
master_catalogue[col].name = col.replace('_cfht_megacam_','_megacam_')
if ('_rcs_' in col) and ('_ap_rcs_' not in col):
master_catalogue[col].name = col.replace('_rcs_','_megacam_')
if '_ap_rcs_' in col:
master_catalogue.remove_columns(col)
megacam_stats.show_in_notebook()
megacam_origin.write("{}/lockman-swire_megacam_fluxes_origins{}.fits".format(OUT_DIR, SUFFIX), overwrite=True)
We add a binary flag_optnir_obs
indicating that a source was observed in a given wavelength domain:
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.
wfc_moc = MOC(filename="../../dmu0/dmu0_INTWFC/data/en2_intwfc_v2.1_HELP_coverage_MOC.fits")
sparcs_moc = MOC(filename="../../dmu0/dmu0_SpARCS/data/SpARCS_HELP_ELAIS-N2_MOC.fits")
rcs_moc = MOC(filename="../../dmu0/dmu0_RCSLenS/data/RCSLenS_ELAIS-N2_MOC.fits")
ps1_moc = MOC(filename="../../dmu0/dmu0_PanSTARRS1-3SS/data/PanSTARRS1-3SS_ELAIS-N2_MOC.fits")
swire_moc = MOC(filename="../../dmu0/dmu0_DataFusion-Spitzer/data/DF-SWIRE_ELAIS-N2_MOC.fits")
was_observed_optical = inMoc(
master_catalogue['ra'], master_catalogue['dec'],
wfc_moc + sparcs_moc + ps1_moc)
#There are seemingly no MIR observations in ELAIS N2
#was_observed_nir = inMoc(
# master_catalogue['ra'], master_catalogue['dec'],
#
#)
was_observed_mir = inMoc(
master_catalogue['ra'], master_catalogue['dec'],
swire_moc
)
master_catalogue.add_column(
Column(
1 * was_observed_optical + 4 * was_observed_mir, #2 * was_observed_nir +
name="flag_optnir_obs")
)
We add a binary flag_optnir_det
indicating that a source was detected in a given wavelength domain:
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.
# 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_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']) +
1 * ~np.isnan(master_catalogue['f_megacam_u']) +
1 * ~np.isnan(master_catalogue['f_megacam_g']) +
1 * ~np.isnan(master_catalogue['f_megacam_r']) +
1 * ~np.isnan(master_catalogue['f_megacam_i']) +
1 * ~np.isnan(master_catalogue['f_megacam_z']) +
1 * ~np.isnan(master_catalogue['f_megacam_y'])
)
nb_nir_flux = (
1 * np.zeros(len(master_catalogue), dtype=bool)
)
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'])
)
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")
)
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.
master_catalogue['help_id',
'wfc_id',
'rcs_id',
'swire_intid',
'sparcs_intid',
'ps1_id'].write(
"{}/master_list_cross_ident_elais-n2{}.fits".format(OUT_DIR, SUFFIX), overwrite=True)
master_catalogue.remove_columns(['wfc_id',
'rcs_id',
'swire_intid',
'sparcs_intid',
'ps1_id'])
We are adding a column with a HEALPix index at order 13 associated with each source.
master_catalogue.add_column(Column(
data=coords_to_hpidx(master_catalogue['ra'], master_catalogue['dec'], order=13),
name="hp_idx"
))
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 += ['m_rcs_g', 'merr_rcs_g', 'f_rcs_g', 'ferr_rcs_g',
# 'm_rcs_r', 'merr_rcs_r', 'f_rcs_r', 'ferr_rcs_r',
# 'm_rcs_i', 'merr_rcs_i', 'f_rcs_i', 'ferr_rcs_i',
# 'm_rcs_z', 'merr_rcs_z', 'f_rcs_z', 'ferr_rcs_z',
# 'm_rcs_y', 'merr_rcs_y', 'f_rcs_y', 'ferr_rcs_y']
columns += [ 'm_megacam_i', 'ferr_megacam_i', 'merr_megacam_i', 'flag_megacam_i', 'f_megacam_i',
'm_megacam_y', 'ferr_megacam_y', 'merr_megacam_y', 'flag_megacam_y', 'f_megacam_y']
columns += ["stellarity", "flag_cleaned", "flag_merged", "flag_gaia", "flag_optnir_obs", "flag_optnir_det", "ebv"]
# 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)))
master_catalogue[columns].write("{}/master_catalogue_elais-n2{}.fits".format(OUT_DIR, SUFFIX), overwrite=True)