ELAIS-N2 master catalogue

Preparation of Spitzer Adaptation of the Red-sequence Cluster Survey (SpARCS) data

This catalogue comes from dmu0_SpARCS. Alexandru Tudorica confirmed that the magnitudes are AB ones and are not aperture corrected.

In the catalogue, we keep:

  • The internal identifier (this one is only in HeDaM data);
  • The position;
  • The ugrz magnitudes in the 8th aperture (11×0.186=2.046 arcsec).
  • The "auto" magnitudes.

Is there y band data?

The maps on the web page indicate they were observed in 2012 (or late 2011). Let's use 2012 as epoch.

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: 
44f1ae0 (Thu Nov 30 18:27:54 2017 +0000)
In [2]:
%matplotlib inline
#%config InlineBackend.figure_format = 'svg'

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

from collections import OrderedDict
import os

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

from herschelhelp_internal.flagging import  gaia_flag_column
from herschelhelp_internal.masterlist import nb_astcor_diag_plot, remove_duplicates, nb_plot_mag_ap_evol, nb_plot_mag_vs_apcor
from herschelhelp_internal.utils import aperture_correction, astrometric_correction, mag_to_flux
In [3]:
OUT_DIR =  os.environ.get('TMP_DIR', "./data_tmp")
try:
    os.makedirs(OUT_DIR)
except FileExistsError:
    pass

RA_COL = "sparcs_ra"
DEC_COL = "sparcs_dec"

I - Parametres for aperture correction

To compute aperture correction we need to dertermine two parametres: the target aperture and the range of magnitudes for the stars that will be used to compute the correction.

Target aperture: To determine the target aperture, we simulate a curve of growth using the provided apertures and draw two figures:

  • The evolution of the magnitudes of the objects by plotting on the same plot aperture number vs the mean magnitude.
  • The mean gain (loss when negative) of magnitude is each aperture compared to the previous (except for the first of course).

As target aperture, we should use the smallest (i.e. less noisy) aperture for which most of the flux is captures.

Magnitude range: To know what limits in aperture to use when doing the aperture correction, we plot for each magnitude bin the correction that is computed and its RMS. We should then use the wide limits (to use more stars) where the correction is stable and with few dispersion.

In [4]:
# We are using the aperture index 7 (0 base) that correspond to 11 pix * 0.186 arcsec/pix = 2.046 arcsec
AP_INDEX = 7
In [5]:
t = Table.read("../../dmu0/dmu0_SpARCS/data/SpARCS_HELP_ELAIS-N2.fits")

stellarity = t['CLASS_STAR']

mags_r = np.array(t['MAG_APER_r']).T
mags_r[mags_r == 99] = np.nan

mags_u = np.array(t['MAG_APER_u']).T
mags_u[mags_u == 99] = np.nan

mags_g = np.array(t['MAG_APER_g']).T
mags_g[mags_g == 99] = np.nan

mags_z = np.array(t['MAG_APER_z']).T
mags_z[mags_z == 99] = np.nan

del t
WARNING: UnitsWarning: '""' did not parse as fits unit: Invalid character at col 0 [astropy.units.core]

I.a r-band

In [6]:
nb_plot_mag_ap_evol(mags_r, stellarity)

We will use the 16th (aperture number above begin to 0) aperture as target.

In [7]:
nb_plot_mag_vs_apcor(mags_r[AP_INDEX], mags_r[15], stellarity)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:133: RuntimeWarning: invalid value encountered in less_equal
  mask &= (mag <= mag_max)

We use magnitudes between 17 and 17.9.

I.b u-band

In [8]:
nb_plot_mag_ap_evol(mags_u, stellarity)

We will use the 16th (aperture number above begin to 0) aperture as target. Should we use the 12nd because of the increasing magnitude?

In [9]:
nb_plot_mag_vs_apcor(mags_u[AP_INDEX], mags_u[15], stellarity)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:133: RuntimeWarning: invalid value encountered in less_equal
  mask &= (mag <= mag_max)

We use magnitudes between 17 and 17.9.

I.c g-band

In [10]:
nb_plot_mag_ap_evol(mags_g, stellarity)

We will use the 16th (aperture number above begin to 0) aperture as target.

In [11]:
nb_plot_mag_vs_apcor(mags_g[AP_INDEX], mags_g[15], stellarity)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:133: RuntimeWarning: invalid value encountered in less_equal
  mask &= (mag <= mag_max)

We use magnitudes between 17.2 and 18.

I.d z-band

In [12]:
nb_plot_mag_ap_evol(mags_z, stellarity)

We will use the 16th (aperture number above begin to 0) aperture as target.

In [13]:
nb_plot_mag_vs_apcor(mags_z[AP_INDEX], mags_z[15], stellarity)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:133: RuntimeWarning: invalid value encountered in less_equal
  mask &= (mag <= mag_max)

We use magnitudes between 16 and 17.

II - Column selection

In [14]:
# Index of the target aperture when doing aperture correction
# (see 'sparcs_aperture_correction' notebook).
AP_TARG_INDEX = {
    'u': 15,
    'g': 15,
    'r': 15,
    'z': 15
}

# Magnitude range for aperture correction (see 'sparcs_aperture_correction' notebook).
APCOR_MAG_LIMITS = {
    'u': (17., 17.9),
    'g': (17., 17.9),
    'r': (17.2, 18.),
    'z': (16., 17.)
}

epoch = 2012

sparcs_tmp = Table.read("../../dmu0/dmu0_SpARCS/data/SpARCS_HELP_ELAIS-N2.fits")[
    'internal_id', 'ALPHA_J2000', 'DELTA_J2000', 'CLASS_STAR',      
    'MAG_APER_r', 'MAGERR_APER_r', 'MAG_AUTO_r', 'MAGERR_AUTO_r',
    'MAG_APER_u', 'MAGERR_APER_u', 'MAG_AUTO_u', 'MAGERR_AUTO_u',
    'MAG_APER_g', 'MAGERR_APER_g', 'MAG_AUTO_g', 'MAGERR_AUTO_g',
    'MAG_APER_z', 'MAGERR_APER_z', 'MAG_AUTO_z', 'MAGERR_AUTO_z']


# Clean table metadata
sparcs_tmp.meta = None

catalogue = Table(
    data = sparcs_tmp['internal_id', 'ALPHA_J2000', 'DELTA_J2000', 'CLASS_STAR'],
    names = ['sparcs_intid', 'sparcs_ra', 'sparcs_dec', 'sparcs_stellarity'])

for band in ['u', 'g', 'r', 'z']:
    
    # Aperture magnitudes
    mag_aper = sparcs_tmp["MAG_APER_{}".format(band)][:, AP_INDEX]
    mag_aper_target = sparcs_tmp["MAG_APER_{}".format(band)][:, AP_TARG_INDEX[band]]
    magerr_aper = sparcs_tmp["MAGERR_APER_{}".format(band)][:, AP_INDEX]
    
    # Set bad values (99.0) to NaN
    mask = (mag_aper > 90) | (mag_aper_target > 90) | (magerr_aper > 90)
    mag_aper[mask] = np.nan
    mag_aper_target[mask] = np.nan
    magerr_aper[mask] = np.nan
    
    # Aperture correction
    mag_diff, num, std = aperture_correction(
        mag_aper, mag_aper_target, catalogue['sparcs_stellarity'],
        mag_min=APCOR_MAG_LIMITS[band][0], mag_max=APCOR_MAG_LIMITS[band][1]
        )
    print("Aperture correction for SpARCS band {}:".format(band))
    print("Correction: {}".format(mag_diff))
    print("Number of source used: {}".format(num))
    print("RMS: {}".format(std))
    print("")
    mag_aper += mag_diff
    
    catalogue.add_column(Column(
        data = mag_aper.data,
        name = "m_ap_cfht_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = magerr_aper.data,
        name = "merr_ap_cfht_megacam_{}".format(band)
    ))
    
    # Computing the aperture flux columns
    flux_aper, fluxerr_aper = mag_to_flux(mag_aper.data, magerr_aper.data)
    
    catalogue.add_column(Column(
        data = flux_aper * 1.e6,
        name = "f_ap_cfht_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = fluxerr_aper * 1.e6,
        name = "ferr_ap_cfht_megacam_{}".format(band)
    ))
    
    # Auto magnitudes
    mag_auto = sparcs_tmp["MAG_AUTO_{}".format(band)]
    magerr_auto = sparcs_tmp["MAGERR_AUTO_{}".format(band)]
    
    # Set bad values (99.0) to NaN
    mask = (mag_auto > 90) | (magerr_auto > 90)
    mag_auto[mask] = np.nan
    magerr_auto[mask] = np.nan    
    
    catalogue.add_column(Column(
        data = mag_auto,
        name = "m_cfht_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = magerr_auto,
        name = "merr_cfht_megacam_{}".format(band)
    ))
    
    # Computing the flux columns
    flux, fluxerr = mag_to_flux(mag_auto, magerr_auto)
    
    catalogue.add_column(Column(
        data = flux * 1.e6,
        name = "f_cfht_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = fluxerr * 1.e6,
        name = "ferr_cfht_megacam_{}".format(band)
    ))
    
    # Band-flag column
    catalogue.add_column(Column(np.zeros(len(catalogue), dtype=bool), 
                             name="flag_cfht_megacam_{}".format(band)
    ))
        
# TODO: Set to True the flag columns for fluxes that should not be used for SED fitting.

del sparcs_tmp
WARNING: UnitsWarning: '""' did not parse as fits unit: Invalid character at col 0 [astropy.units.core]
/opt/anaconda3/envs/herschelhelp_internal/lib/python3.6/site-packages/astropy/table/column.py:1096: MaskedArrayFutureWarning: setting an item on a masked array which has a shared mask will not copy the mask and also change the original mask array in the future.
Check the NumPy 1.11 release notes for more information.
  ma.MaskedArray.__setitem__(self, index, value)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/opt/herschelhelp_internal/herschelhelp_internal/utils.py:133: RuntimeWarning: invalid value encountered in less_equal
  mask &= (mag <= mag_max)
Aperture correction for SpARCS band u:
Correction: -0.12485599517822266
Number of source used: 1001
RMS: 0.06557130828443923

Aperture correction for SpARCS band g:
Correction: -0.13389968872070312
Number of source used: 1252
RMS: 0.052055042021584536

Aperture correction for SpARCS band r:
Correction: -0.1177825927734375
Number of source used: 2187
RMS: 0.05621025637278401

Aperture correction for SpARCS band z:
Correction: -0.11841869354248047
Number of source used: 1581
RMS: 0.06271163119030113

In [15]:
catalogue[:10].show_in_notebook()
Out[15]:
<Table masked=True length=10>
idxsparcs_intidsparcs_rasparcs_decsparcs_stellaritym_ap_cfht_megacam_umerr_ap_cfht_megacam_uf_ap_cfht_megacam_uferr_ap_cfht_megacam_um_cfht_megacam_umerr_cfht_megacam_uf_cfht_megacam_uferr_cfht_megacam_uflag_cfht_megacam_um_ap_cfht_megacam_gmerr_ap_cfht_megacam_gf_ap_cfht_megacam_gferr_ap_cfht_megacam_gm_cfht_megacam_gmerr_cfht_megacam_gf_cfht_megacam_gferr_cfht_megacam_gflag_cfht_megacam_gm_ap_cfht_megacam_rmerr_ap_cfht_megacam_rf_ap_cfht_megacam_rferr_ap_cfht_megacam_rm_cfht_megacam_rmerr_cfht_megacam_rf_cfht_megacam_rferr_cfht_megacam_rflag_cfht_megacam_rm_ap_cfht_megacam_zmerr_ap_cfht_megacam_zf_ap_cfht_megacam_zferr_ap_cfht_megacam_zm_cfht_megacam_zmerr_cfht_megacam_zf_cfht_megacam_zferr_cfht_megacam_zflag_cfht_megacam_z
degdegmagmagmagmagmagmagmagmag
04488817247.58478797941.66674821740.0192727nannannannannannannannanFalsenannannannannannannannanFalsenannannannannannannannanFalsenannannannan27.13761.87740.05069220.0876542False
14488818247.58478907241.72089119480.0192727nannannannannannannannanFalsenannannannan29.90682.196770.003956160.00800448Falsenannannannan26.40960.3614090.09911820.0329935Falsenannannannan25.38010.390160.2558280.091932False
24488819247.58480755541.61735771880.0192727nannannannannannannannanFalsenannannannannannannannanFalsenannannannan28.55551.212240.01373440.0153346FalsenannannannannannannannanFalse
34488820247.58480883141.70539244740.019272725.41480.07008850.2477790.015995125.23090.06614160.2935160.0178806False25.06480.02863210.3420390.0090199624.55850.01869050.5452530.00938629Falsenannannannan25.14990.1318960.3162440.0384175FalsenannannannannannannannanFalse
44488821247.58481751441.67741523810.019272726.18510.1375690.1218920.015444428.68841.180470.01215190.0132122False24.90250.02494120.3971830.0091239725.32820.02959510.268350.00731472Falsenannannannan27.71270.7199340.02984940.0197927FalsenannannannannannannannanFalse
54488822247.58482409641.70497711660.4821125.74980.1018770.1820120.017078523.66380.02995091.243050.0342905False24.92970.02548470.3873760.009092622.96790.008109862.359650.0176253Falsenannannannan22.9140.02973692.479740.0679168Falsenannannannan23.57060.1563651.354490.19507False
64488823247.58482478541.71061049630.019272728.03640.7438530.02215260.015177126.15250.1534180.1256010.0177479False25.8980.0594170.1587860.0086895825.33020.03750530.2678560.00925272FalsenannannannannannannannanFalsenannannannan26.13680.8032630.1274290.0942763False
74488824247.584840541.72941583430.0192727nannannannannannannannanFalsenannannannannannannannanFalsenannannannannannannannanFalsenannannannannannannannanFalse
84488825247.58488703141.62611711060.583028nannannannannannannannanFalse25.35070.03830810.2628510.0092741724.66280.029870.4953310.0136272Falsenannannannan23.63460.04869221.276850.0572633FalsenannannannannannannannanFalse
94488826247.58491410941.70703346630.65482524.25810.02737240.7190520.01812822.44350.0108593.824630.038252False22.23140.003510984.650010.015036920.28260.0011320227.98670.0291797False29.52080.7568330.005645130.0039350422.12010.01571875.151780.0745846Falsenannannannan22.01120.03880655.695120.203556False

II - Removal of duplicated sources

We remove duplicated objects from the input catalogues.

In [16]:
SORT_COLS = ['merr_ap_cfht_megacam_r', 'merr_ap_cfht_megacam_u',
              'merr_ap_cfht_megacam_g', 'merr_ap_cfht_megacam_z']
FLAG_NAME = 'sparcs_flag_cleaned'

nb_orig_sources = len(catalogue)

catalogue = remove_duplicates(catalogue, RA_COL, DEC_COL, sort_col=SORT_COLS,flag_name=FLAG_NAME)

nb_sources = len(catalogue)

print("The initial catalogue had {} sources.".format(nb_orig_sources))
print("The cleaned catalogue has {} sources ({} removed).".format(nb_sources, nb_orig_sources - nb_sources))
print("The cleaned catalogue has {} sources flagged as having been cleaned".format(np.sum(catalogue[FLAG_NAME])))
/opt/anaconda3/envs/herschelhelp_internal/lib/python3.6/site-packages/astropy/table/column.py:1096: MaskedArrayFutureWarning: setting an item on a masked array which has a shared mask will not copy the mask and also change the original mask array in the future.
Check the NumPy 1.11 release notes for more information.
  ma.MaskedArray.__setitem__(self, index, value)
The initial catalogue had 785579 sources.
The cleaned catalogue has 785579 sources (0 removed).
The cleaned catalogue has 0 sources flagged as having been cleaned

III - Astrometry correction

We match the astrometry to the Gaia one. We limit the Gaia catalogue to sources with a g band flux between the 30th and the 70th percentile. Some quick tests show that this give the lower dispersion in the results.

In [17]:
gaia = Table.read("../../dmu0/dmu0_GAIA/data/GAIA_ELAIS-N2.fits")
gaia_coords = SkyCoord(gaia['ra'], gaia['dec'])
In [18]:
nb_astcor_diag_plot(catalogue[RA_COL], catalogue[DEC_COL], 
                    gaia_coords.ra, gaia_coords.dec)
In [19]:
delta_ra, delta_dec =  astrometric_correction(
    SkyCoord(catalogue[RA_COL], catalogue[DEC_COL]),
    gaia_coords
)

print("RA correction: {}".format(delta_ra))
print("Dec correction: {}".format(delta_dec))
RA correction: -0.061004910895690045 arcsec
Dec correction: -0.06102552299012132 arcsec
In [20]:
catalogue[RA_COL] +=  delta_ra.to(u.deg)
catalogue[DEC_COL] += delta_dec.to(u.deg)
In [21]:
nb_astcor_diag_plot(catalogue[RA_COL], catalogue[DEC_COL], 
                    gaia_coords.ra, gaia_coords.dec)

IV - Flagging Gaia objects

In [22]:
catalogue.add_column(
    gaia_flag_column(SkyCoord(catalogue[RA_COL], catalogue[DEC_COL]), epoch, gaia)
)
In [23]:
GAIA_FLAG_NAME = "sparcs_flag_gaia"

catalogue['flag_gaia'].name = GAIA_FLAG_NAME
print("{} sources flagged.".format(np.sum(catalogue[GAIA_FLAG_NAME] > 0)))
26154 sources flagged.

V - Flagging objects near bright stars

VI - Saving to disk

In [24]:
catalogue.write("{}/SpARCS.fits".format(OUT_DIR), overwrite=True)