ELAIS-N1 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.

Note that there are y band columns because we combined all the SpARCS data in HeDaM, but there is no y data for the ELAIS-N1 sources.

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: 
284b2ef (Mon Aug 14 20:02:12 2017 +0100)
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-N1.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)
/home/yroehlly/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/home/yroehlly/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)
/home/yroehlly/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/home/yroehlly/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)
/home/yroehlly/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/home/yroehlly/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)
/home/yroehlly/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/home/yroehlly/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-N1.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_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = magerr_aper.data,
        name = "merr_ap_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_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = fluxerr_aper * 1.e6,
        name = "ferr_ap_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_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = magerr_auto,
        name = "merr_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_megacam_{}".format(band)
    ))
    catalogue.add_column(Column(
        data = fluxerr * 1.e6,
        name = "ferr_megacam_{}".format(band)
    ))
    
    # Band-flag column
    catalogue.add_column(Column(np.zeros(len(catalogue), dtype=bool), 
                             name="flag_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)
/home/yroehlly/herschelhelp_internal/herschelhelp_internal/utils.py:131: RuntimeWarning: invalid value encountered in greater_equal
  mask &= (mag >= mag_min)
/home/yroehlly/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.14031028747558594
Number of source used: 1854
RMS: 0.04614434468089496

Aperture correction for SpARCS band g:
Correction: -0.15618896484375
Number of source used: 1655
RMS: 0.05094869324020216

Aperture correction for SpARCS band r:
Correction: -0.13660430908203125
Number of source used: 4415
RMS: 0.038120613108765716

Aperture correction for SpARCS band z:
Correction: -0.15040016174316406
Number of source used: 2359
RMS: 0.03453256417107372

In [15]:
catalogue[:10].show_in_notebook()
Out[15]:
<Table masked=True length=10>
idxsparcs_intidsparcs_rasparcs_decsparcs_stellaritym_ap_megacam_umerr_ap_megacam_uf_ap_megacam_uferr_ap_megacam_um_megacam_umerr_megacam_uf_megacam_uferr_megacam_uflag_megacam_um_ap_megacam_gmerr_ap_megacam_gf_ap_megacam_gferr_ap_megacam_gm_megacam_gmerr_megacam_gf_megacam_gferr_megacam_gflag_megacam_gm_ap_megacam_rmerr_ap_megacam_rf_ap_megacam_rferr_ap_megacam_rm_megacam_rmerr_megacam_rf_megacam_rferr_megacam_rflag_megacam_rm_ap_megacam_zmerr_ap_megacam_zf_ap_megacam_zferr_ap_megacam_zm_megacam_zmerr_megacam_zf_megacam_zferr_megacam_zflag_megacam_z
degdegmagmagmagmagmagmagmagmag
02966095238.57828136754.56886109190.39845926.78260.1398870.07030220.009057827.09030.1200260.05295030.00585355False25.77160.0378580.1783830.0062199426.19070.035320.1212580.00394464False25.78960.06531210.1754540.010554426.18150.06031810.1222930.00679398FalsenannannannannannannannanFalse
12966096238.57859742454.57085328960.59707625.63150.05145910.2029570.0096192626.40150.0591170.09986040.00543728False25.31250.02540520.2722630.006370726.06720.02808490.1358720.00351463False25.80320.06659810.1732680.010628126.27890.05779990.1118040.00595197False24.60770.07530860.5210760.036142825.54740.09836850.2192970.0198685False
22966097238.57914480554.56843343930.48299627.0960.1867740.05267650.0090617126.89420.1390680.06343090.00812461False26.61430.08156140.08208780.006166526.90170.09403120.06299550.00545579False25.32710.04310880.2686240.010665625.49740.04548070.2296320.00961912False24.2830.05533770.7027410.035817324.47350.05874080.5896540.0319016False
32966098238.57975296454.57110769870.011392825.80980.05977630.1722220.0094818725.8850.07501360.1606970.0111026False25.02970.01974020.353280.006423125.10410.02470580.3298930.00750667False24.6970.02459260.4799720.010871724.74820.03083930.4578430.0130046False24.32850.05861240.6739160.036380724.51350.08221460.5683110.0430339False
42966099238.57978010154.56810649990.45954925.88840.06443640.1601980.0095074327.11840.09141310.05159790.00434427False25.97610.04554980.1477560.0061987826.90720.048360.06267950.00279182False25.80390.0680520.1731640.010853626.65950.07028430.07873720.005097False25.67130.1970460.1956470.035507326.85930.2626980.0655070.0158497False
52966100238.580426354.56512248280.89139423.38770.009769791.602930.014423723.43050.01025191.540960.0145503False22.68550.002903363.060630.0081844222.75210.003211292.878390.00851344False22.15930.003308374.968960.01514122.23090.003651484.651930.0156451False21.46820.005037479.39140.043573121.55350.00584328.681330.0467211False
62966101238.5806284954.56340291270.0092800923.92350.01395530.9785780.01257823.80810.01401461.088370.0140486False23.73440.006535521.164750.0070111223.66220.007264321.244850.00832887False23.53030.009059761.405610.011728923.47520.01047031.478830.0142611False22.58840.01241353.346980.03826722.57560.01521853.386520.0474679False
72966102238.58068333254.5692272980.0019152525.49330.04607040.2305150.0097813125.18390.04282090.3065120.0120887False24.89650.01742810.3993960.0064110724.77260.01927350.4476830.00794706False23.78430.01137021.112460.0116523.71190.01300611.189210.0142457False23.36420.02449811.638060.036960523.24520.02739141.827840.0461134False
82966103238.58151846154.5708163540.0856524.58520.02238070.5320330.01096724.47360.02733230.5896130.0148429False24.46920.01221520.5920230.0066606224.4440.0171580.6058960.00957504False24.4010.01915980.6303820.011124324.27020.02524160.7110980.0165319False24.01330.04328520.9008910.03591624.40340.09319610.6289630.0539881False
92966104238.58252674654.58177640590.17372625.12580.03384170.3233470.010078525.08270.0407490.3364480.0126273False24.8470.01672930.418030.006441124.89990.02252770.3981470.00826105False24.65320.02360310.4997260.010863724.59720.02948290.5261820.0142883False23.46330.02633631.495190.036268223.46180.03452471.497210.047609False

II - Removal of duplicated sources

We remove duplicated objects from the input catalogues.

In [16]:
SORT_COLS = ['merr_ap_megacam_r', 'merr_ap_megacam_u',
              'merr_ap_megacam_g', 'merr_ap_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 1522722 sources.
The cleaned catalogue has 1522722 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-N1.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.04957245422474443 arcsec
Dec correction: -0.01778054084127234 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)))
43241 sources flagged.

V - Saving to disk

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