COSMOS master catalogue¶

Preparation of Canada France Hawaii Telescope WIRDS Survey (CFHT-WIRDS) data¶

The catalogue is in dmu0_CFHT-WIRDS.

In the catalogue, we keep:

  • The position;
  • The stellarity;
  • The aperture magnitude (3 arcsec).
  • The total magnitude (Kron like aperture magnitude).
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: 
33f5ec7 (Wed Dec 6 16:56:17 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, join
import numpy as np

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

RA_COL = "wirds_ra"
DEC_COL = "wirds_dec"

I - Column selection¶

In [4]:
#We have to import and combine the H, J and Ks catalogues separately. 
#Fluxes are given in counts sowe compute them fresh from the magnitudes

epoch = 2007

imported_columns_h = OrderedDict({
        'NUMBER': "wirds_h_id",
        'ALPHA_J2000': "wirds_h_ra",
        'DELTA_J2000': "wirds_h_dec",
        'CLASS_STAR':  "wirds_h_stellarity",
        'MAG_APER': "m_ap_wirds_h",
        'MAGERR_APER': "merr_ap_wirds_h",
        'MAG_AUTO': "m_wirds_h",
        'MAGERR_AUTO': "merr_wirds_h"
        #'FLUX_APER': "f_ap_wirds_h",
        #'FLUXERR_APER': "ferr_ap_wirds_h",
        #'FLUX_AUTO': "f_wirds_h",
        #'FLUXERR_AUTO': "ferr_wirds_h"
        
    })


catalogue_h = Table.read("../../dmu0/dmu0_CFHT-WIRDS/data/COSMOS_H.fits")[list(imported_columns_h)]
for column in imported_columns_h:
    catalogue_h[column].name = imported_columns_h[column]

for col in catalogue_h.colnames:
    if col.startswith('m_'):
        
        errcol = "merr{}".format(col[1:])
        #catalogue_h[col].name = imported_columns_h[col]
    
        flux, error = mag_to_flux(np.array(catalogue_h[col]), np.array(catalogue_h[errcol]))
        
        # Fluxes are added in µJy
        catalogue_h.add_column(Column(flux * 1.e6, name="f{}".format(col[1:])))
        catalogue_h.add_column(Column(error * 1.e6, name="f{}".format(errcol[1:])))

        # Band-flag column
        if "ap" not in col:
            catalogue_h.add_column(Column(np.zeros(len(catalogue_h), dtype=bool), name="flag{}".format(col[1:])))
        
# Clean table metadata
catalogue_h.meta = None



imported_columns_j = OrderedDict({
        'NUMBER': "wirds_j_id",
        'ALPHA_J2000': "wirds_j_ra",
        'DELTA_J2000': "wirds_j_dec",
        'CLASS_STAR':  "wirds_j_stellarity",
        'MAG_APER': "m_ap_wirds_j",
        'MAGERR_APER': "merr_ap_wirds_j",
        'MAG_AUTO': "m_wirds_j",
        'MAGERR_AUTO': "merr_wirds_j"
        #'FLUX_APER': "f_ap_wirds_j",
        #'FLUXERR_APER': "ferr_ap_wirds_j",
        #'FLUX_AUTO': "f_wirds_j",
        #'FLUXERR_AUTO': "ferr_wirds_j"
        
    })


catalogue_j = Table.read("../../dmu0/dmu0_CFHT-WIRDS/data/COSMOS_J.fits")[list(imported_columns_j)]
for column in imported_columns_j:
    catalogue_j[column].name = imported_columns_j[column]
    
for col in catalogue_j.colnames:
    if col.startswith('m_'):
        
        errcol = "merr{}".format(col[1:])
        #catalogue_h[col].name = imported_columns_h[col]
    
        flux, error = mag_to_flux(np.array(catalogue_j[col]), np.array(catalogue_j[errcol]))
        
        # Fluxes are added in µJy
        catalogue_j.add_column(Column(flux * 1.e6, name="f{}".format(col[1:])))
        catalogue_j.add_column(Column(error * 1.e6, name="f{}".format(errcol[1:])))

        # Band-flag column
        if "ap" not in col:
            catalogue_j.add_column(Column(np.zeros(len(catalogue_j), dtype=bool), name="flag{}".format(col[1:])))
# Clean table metadata
catalogue_j.meta = None



imported_columns_ks = OrderedDict({
        'NUMBER': "wirds_ks_id",
        'ALPHA_J2000': "wirds_ks_ra",
        'DELTA_J2000': "wirds_ks_dec",
        'CLASS_STAR':  "wirds_ks_stellarity",
        'MAG_APER': "m_ap_wirds_ks",
        'MAGERR_APER': "merr_ap_wirds_ks",
        'MAG_AUTO': "m_wirds_ks",
        'MAGERR_AUTO': "merr_wirds_ks"
        #'FLUX_APER': "f_ap_wirds_ks",
        #'FLUXERR_APER': "ferr_ap_wirds_ks",
        #'FLUX_AUTO': "f_wirds_ks",
        #'FLUXERR_AUTO': "ferr_wirds_ks"
        
    })


catalogue_ks = Table.read("../../dmu0/dmu0_CFHT-WIRDS/data/COSMOS_Ks.fits")[list(imported_columns_ks)]
for column in imported_columns_ks:
    catalogue_ks[column].name = imported_columns_ks[column]

for col in catalogue_ks.colnames:
    if col.startswith('m_'):
        
        errcol = "merr{}".format(col[1:])
        #catalogue_h[col].name = imported_columns_h[col]
    
        flux, error = mag_to_flux(np.array(catalogue_ks[col]), np.array(catalogue_ks[errcol]))
        
        # Fluxes are added in µJy
        catalogue_ks.add_column(Column(flux * 1.e6, name="f{}".format(col[1:])))
        catalogue_ks.add_column(Column(error * 1.e6, name="f{}".format(errcol[1:])))

        # Band-flag column
        if "ap" not in col:
            catalogue_ks.add_column(Column(np.zeros(len(catalogue_ks), dtype=bool), name="flag{}".format(col[1:])))
# Clean table metadata
catalogue_ks.meta = None

Merging different bands¶

CFHT-WIRDS has indivdual extractions from each band. We must therefore merge them as if they were individual catalogues (they have different

In [5]:
catalogue_ks[:10].show_in_notebook()
Out[5]:
<Table length=10>
idxwirds_ks_idwirds_ks_rawirds_ks_decwirds_ks_stellaritym_ap_wirds_ksmerr_ap_wirds_ksm_wirds_ksmerr_wirds_ksf_ap_wirds_ksferr_ap_wirds_ksf_wirds_ksferr_wirds_ksflag_wirds_ks
degdegmagmagmagmag
01149.64064191.73017530.9613.21090.000112.11150.000118864.2698371.7374634606751927.80915414.7827279708False
12149.8286271.72473360.0316.66720.000315.67480.0005781.8438061720.2160314271771950.203807490.898102043085False
23149.82624721.71721611.013.2890.012.9320.017554.96629620.024389.33730280.0False
34149.62614891.71884060.9913.40170.000113.00110.000115824.13575231.4574567637122885.47864312.10783047878False
45149.91326911.71444590.9812.99890.012.53390.022931.89792680.035191.68000630.0False
56149.91668381.71086270.9817.05920.000416.87710.0009544.9040050690.20075005427644.4066299640.534168395994False
67149.97905521.71624391.014.96530.000114.8260.00013748.694076890.3452674839864261.868054240.392532554False
78149.8077081.71563410.0318.64490.001417.64670.0028126.4852839360.163096152393317.1903747270.818000767906False
89150.27316971.71467670.0317.73930.000716.71390.0011291.2594331820.187781896131748.9278831520.758766878969False
910150.58902781.714650.8818.48720.001218.07160.0021146.2581244760.161650453031214.466764520.414815498939False
In [6]:
nb_merge_dist_plot(
    SkyCoord(catalogue_ks['wirds_ks_ra'], catalogue_ks['wirds_ks_dec']),
    SkyCoord(catalogue_j['wirds_j_ra'], catalogue_j['wirds_j_dec'])
)
In [7]:
catalogue = catalogue_ks
catalogue_ks['wirds_ks_ra'].name = 'ra'
catalogue_ks['wirds_ks_dec'].name = 'dec'
# Given the graph above, we use 0.8 arc-second radius
catalogue = merge_catalogues(catalogue, catalogue_j, "wirds_j_ra", "wirds_j_dec", radius=0.8*u.arcsec)
In [8]:
nb_merge_dist_plot(
    SkyCoord(catalogue['ra'], catalogue['dec']),
    SkyCoord(catalogue_h['wirds_h_ra'], catalogue_h['wirds_h_dec'])
)
In [9]:
# Given the graph above, we use 0.8 arc-second radius
catalogue = merge_catalogues(catalogue, catalogue_h, "wirds_h_ra", "wirds_h_dec", radius=0.8*u.arcsec)
In [10]:
#rename radec colums
catalogue['ra'].name = 'wirds_ra'
catalogue['dec'].name = 'wirds_dec'
In [11]:
    
for col in catalogue.colnames:
    if "m_" in col or "merr_" in col or "f_" in col or "ferr_" in col or "stellarity" in col:
        catalogue[col].fill_value = np.nan
    elif "flag" in col:
        catalogue[col].fill_value = 0
    elif "id" in col:
        catalogue[col].fill_value = 999999
        
catalogue =  catalogue.filled()

Generate internal id¶

Since every source has an independent id we combine them in 6 digit groups so that each iondividual id can be retrieved from the final integer

In [12]:
wirds_intid = catalogue['wirds_ks_id'] + catalogue['wirds_j_id']*10**6 + catalogue['wirds_h_id']*10**12
In [13]:
catalogue.add_column(Column(data=wirds_intid, name="wirds_intid"))
catalogue.remove_columns(['wirds_ks_id','wirds_j_id','wirds_h_id'])
In [14]:
stellarity_columns = [column for column in catalogue.colnames
                      if 'stellarity' in column]

print(", ".join(stellarity_columns))
wirds_ks_stellarity, wirds_j_stellarity, wirds_h_stellarity
In [15]:
# 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([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

catalogue.add_column(Column(data=max_stellarity.filled(), name="wirds_stellarity"))

catalogue.remove_columns(stellarity_columns)
catalogue['flag_merged'].name = 'wirds_flag_merged'
In [16]:
catalogue[:10].show_in_notebook()
Out[16]:
<Table length=10>
idxwirds_rawirds_decm_ap_wirds_ksmerr_ap_wirds_ksm_wirds_ksmerr_wirds_ksf_ap_wirds_ksferr_ap_wirds_ksf_wirds_ksferr_wirds_ksflag_wirds_kswirds_flag_mergedm_ap_wirds_jmerr_ap_wirds_jm_wirds_jmerr_wirds_jf_ap_wirds_jferr_ap_wirds_jf_wirds_jferr_wirds_jflag_wirds_jm_ap_wirds_hmerr_ap_wirds_hm_wirds_hmerr_wirds_hf_ap_wirds_hferr_ap_wirds_hf_wirds_hferr_wirds_hflag_wirds_hwirds_intidwirds_stellarity
degdegmagmagmagmagmagmagmagmagmagmagmagmag
0149.64070121.713177322.11750.048519.79320.03565.164163692720.23068397093143.92583238181.4402758478FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990000140.0
1150.36659091.708844522.7810.065220.63620.03882.802850927650.16831517608220.20784490410.722149983435FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990000270.0
2150.58129431.71009123.10380.079821.65880.06062.081996523530.1530236551477.879161457360.439772738741FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990000290.0
3149.64044031.709611921.95890.042720.34340.04685.976404719020.23504096150126.46305625381.14067397518FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990000480.0
4149.64099361.70940522.48030.075321.75360.0813.697260065940.2564192680927.22037909270.538667407374FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990000490.49
5150.58836571.708493222.98450.064921.43840.0482.323806702520.1389057989719.65250421630.426733676516FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990000960.0
6150.58890121.709127122.92190.065521.77170.06262.461727629290.1485104383667.101007968520.409420904342FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990000970.0
7149.96389451.709240323.70.147723.62930.16991.202264434620.1635521190271.283157598310.200793226749FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990001030.51
8149.75655231.709092223.47190.133423.4710.16341.483337581330.1822516671011.484567675050.223422924501FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990001100.67
9149.64027691.708567122.51680.073321.63520.06963.575032547330.2413568641918.052300986710.516184452742FalseFalsenannannannannannannannanFalsenannannannannannannannanFalse9999999999990001160.16

II - Removal of duplicated sources¶

We remove duplicated objects from the input catalogues.

In [17]:
SORT_COLS = ['merr_ap_wirds_ks',
            'merr_ap_wirds_j',
            'merr_ap_wirds_h']
FLAG_NAME = 'wirds_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])))
The initial catalogue had 447687 sources.
The cleaned catalogue has 447668 sources (19 removed).
The cleaned catalogue has 19 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 [18]:
gaia = Table.read("../../dmu0/dmu0_GAIA/data/GAIA_COSMOS.fits")
gaia_coords = SkyCoord(gaia['ra'], gaia['dec'])
In [19]:
nb_astcor_diag_plot(catalogue[RA_COL], catalogue[DEC_COL], 
                    gaia_coords.ra, gaia_coords.dec)
In [20]:
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.13158172980638483 arcsec
Dec correction: -0.05163567519641532 arcsec
In [21]:
catalogue[RA_COL] = catalogue[RA_COL] + delta_ra.to(u.deg)
catalogue[DEC_COL] = catalogue[DEC_COL] + delta_dec.to(u.deg)
In [22]:
nb_astcor_diag_plot(catalogue[RA_COL], catalogue[DEC_COL], 
                    gaia_coords.ra, gaia_coords.dec)

IV - Flagging Gaia objects¶

In [23]:
catalogue.add_column(
    gaia_flag_column(SkyCoord(catalogue[RA_COL], catalogue[DEC_COL]), epoch, gaia)
)
In [24]:
GAIA_FLAG_NAME = "cfht-wirds_flag_gaia"

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

V - Flagging objects near bright stars¶

VI - Saving to disk¶

In [25]:
catalogue.write("{}/CFHT-WIRDS.fits".format(OUT_DIR), overwrite=True)