<jupyter_start><jupyter_text>Make CIGALE test input from VIDEO SXDS dataThis notebook uses the HELP python environment to make a CIGALE input catalogue:https://github.com/H-E-L-P/herschelhelp_internalto make the CIGALE inputs we must correct for galactic extinction, convert flux measurements from nano-Janksy to milli-Jansky, merge in the spectroscopic redshifts.This is an early test on the catalogues as part of our validation process.<jupyter_code>%matplotlib inline
#%config InlineBackend.figure_format = 'svg'

import matplotlib.pyplot as plt
plt.rc('figure', figsize=(10, 6))
from herschelhelp.filters import correct_galactic_extinction
from herschelhelp.external import convert_table_for_cigale
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

import numpy as np
import os
import time

from astropy.table import Table
import astropy.units as u
from astropy.coordinates import SkyCoord
SUFFIX = os.environ.get('SUFFIX', time.strftime("%Y%m%d"))
SUFFIX
hsc_bands = 'GRIZY'
vista_bands = 'ZYJHK'
bands = ['HSC_{}'.format(b) for b in hsc_bands]
bands += ['VISTA_{}'.format(b) for b in vista_bands]
cols = ['id', 'VISTA_Ks_m_coord_ra','VISTA_Ks_m_coord_dec','VISTA_Ks_m_detect_isPrimary']
cols+=['{}_m_slot_ModelFlux_flux'.format(b.replace('K','Ks')) for b in bands]
cols+=['{}_m_slot_ModelFlux_fluxErr'.format(b.replace('K','Ks'))  for b in bands]
cat = Table.read('../dmu5/dmu5_VIDEO/data/full_reduced_cat_SXDS.fits')[cols]
cat['VISTA_Ks_m_coord_ra'].name='ra'
cat['VISTA_Ks_m_coord_dec'].name='dec'
#Some HELP functions require a help_id col
cat['id'].name='help_id'
#Rename to CIGALE/HELP format
for c in cat.colnames:
    if c.endswith('m_slot_ModelFlux_flux'):
        mask = cat[c]>1e10
        cat[c][mask]=np.nan
        f_name='f_{}_{}'.format(
            c.split('_')[0].lower().replace('hsc','suprime'),
            c.split('_')[1].lower()
        )
        cat[c].name=f_name
        cat[f_name]=cat[f_name].to(u.uJy)
    if c.endswith('m_slot_ModelFlux_fluxErr'):
        mask = cat[c]>1e10
        cat[c][mask]=np.nan
        ferr_name='ferr_{}_{}'.format(
            c.split('_')[0].lower().replace('hsc','suprime'),
            c.split('_')[1].lower()
        )
        cat[c].name=ferr_name
        cat[ferr_name]=cat[ferr_name].to(u.uJy)
mask = (cat['ra']<1e10) | (cat['dec']<1e10)
mask&=cat['VISTA_Ks_m_detect_isPrimary']==1
mask&=(cat['f_suprime_i']/cat['ferr_suprime_i'] > 5)
mask&=(cat['f_vista_ks']/cat['ferr_vista_ks'] > 5)
cat = cat[mask]
    
cat['ra']=cat['ra'].to(u.deg)
cat['dec']=cat['dec'].to(u.deg)
cat[0:5]
!mkdir data
!wget http://hedam.lam.fr/HELP/dataproducts/dmu23/dmu23_XMM-LSS/data/XMM-LSS-specz-v2.91.fits.gz -P ./data/<jupyter_output>mkdir: data: File exists
--2021-09-06 13:38:25--  http://hedam.lam.fr/HELP/dataproducts/dmu23/dmu23_XMM-LSS/data/XMM-LSS-specz-v2.91.fits.gz
Resolving hedam.lam.fr (hedam.lam.fr)... 172.16.58.3
Connecting to hedam.lam.fr (hedam.lam.fr)|172.16.58.3|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3391102 (3.2M) [application/x-gzip]
Saving to: ‘./data/XMM-LSS-specz-v2.91.fits.gz.9’

XMM-LSS-specz-v2.91 100%[===================>]   3.23M  5.61MB/s    in 0.6s    

2021-09-06 13:38:26 (5.61 MB/s) - ‘./data/XMM-LSS-specz-v2.91.fits.gz.9’ saved [3391102/3391102]<jupyter_text>Merge Specz<jupyter_code>specz=Table.read('./data/XMM-LSS-specz-v2.91.fits.gz')
for c in specz.colnames:
    specz[c].name=c.lower()
specz['objid'].name='specz_id'
len(specz)
nb_merge_dist_plot(
    SkyCoord(cat['ra'], cat['dec']),
    SkyCoord(specz['ra'] * u.deg, specz['dec'] * u.deg)
)
cat = specz_merge(cat, specz, radius=1. * u.arcsec)
cat=cat[cat['specz_id']!='']
cat['redshift']=cat['zspec']
len(cat)
cat[0:5]<jupyter_output><empty_output><jupyter_text>Add EBV columnThis is used to correct for galactic extinction.<jupyter_code>cat.add_column(
    ebv(cat['ra'], cat['dec'])
)<jupyter_output><empty_output><jupyter_text>Convert to CIGALE and write catalogue<jupyter_code># Correction for galactic extinction
best_catalogue = correct_galactic_extinction(cat, inplace=True)
# Convertion to CIGALE format
best_catalogue = convert_table_for_cigale(best_catalogue, inplace=True, remove_zerofluxes=True)
best_catalogue[0:5]
best_catalogue.write('./data/lsst_ir_fusion_sxds_specz_cigale_input_{}.fits'.format(SUFFIX),overwrite=True)
try:
    cat['ebv'].to(u.mJy)
except UnitConversionError:
    print('asdf')
from astropy.units import UnitConversionError<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16478014801915541, 'fraction_numerical': 0.03156290814105355, 'mean_word_length': 5.226287262872629, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '152257', 'license': 'Apache-2.0', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1843, 'n_words': 308, 'path': 'dmu7/1_Make_sxds_CIGALE_input.ipynb', 'repo_name': 'raphaelshirley/lsst-ir-fusion', 'size': '4602'}