<jupyter_start><jupyter_text>Finding Antares SNe in DESI BGS    This notebook selects BGS in coadd tiles and find SNe from Antares in those BGS positions<jupyter_code>import numpy as np

from astropy.io import fits
from astropy.table import Table, vstack
import matplotlib.pyplot as plt

import desitarget
from desitarget.targetmask import desi_mask, bgs_mask
from desitarget.cmx.cmx_targetmask import cmx_mask
from desispec.io import read_spectra

from glob import glob

from astropy.coordinates import SkyCoord, match_coordinates_sky
from astropy.io import ascii
from astropy import units as u<jupyter_output><empty_output><jupyter_text>example single coadd file<jupyter_code>coadd_file = '/global/cfs/cdirs/desi/spectro/redux/daily/tiles/70502/20200225/coadd-0-70502-20200225.fits' # spectra in petal 0 in 70502 tile observed on Feb. 25 2020 
coaddsfits=fits.open(coadd_file)
coaddsfits.info()
coaddsfits['fibermap'].header<jupyter_output><empty_output><jupyter_text>loop through all coadds in one tile<jupyter_code>prefix = '/global/cfs/cdirs/desi/spectro/redux/daily/tiles/70502/20200225'
coadds = sorted(glob('{}/*coadd*.fits'.format(prefix)))
coadds
fibermap = None
for coadd in coadds:
    hdus = fits.open(coadd)
    spectra = read_spectra(coadd)
    if fibermap is None:
        fibermap = spectra.fibermap  # read in the first coadd fibermap
    else:
        fibermap = vstack([fibermap, spectra.fibermap])<jupyter_output><empty_output><jupyter_text>select BGS<jupyter_code>cmx_mask.mask('SV0_BGS') # this is commissioning SV0 BGS mask

sv0_bgs= fibermap['CMX_TARGET'] & cmx_mask.mask('SV0_BGS') != 0

np.sum(sv0_bgs) # total 1484 BGS in 5 petals
columns = ['TARGETID', 'TARGET_RA', 'TARGET_DEC']
fibermap[sv0_bgs][columns]
BGS_targets=fibermap[sv0_bgs]
print(np.max(fibermap[sv0_bgs]['TARGET_DEC']))
print(np.median(fibermap[sv0_bgs]['TARGET_DEC']))
coadds_ra  = fibermap[sv0_bgs]['TARGET_RA']
coadds_dec = fibermap[sv0_bgs]['TARGET_DEC']<jupyter_output><empty_output><jupyter_text>Matching BGS coords with with Antares SNe coords<jupyter_code>coadds_coords = SkyCoord(ra=coadds_ra, dec=coadds_dec, unit='deg')

antares_data = ascii.read('/global/u1/x/xychen/timedomain/antares/results.csv') # to generate this file, run python query_alerts.py --ra XX --dec XX --tobs XXXXX (ra, dec in degree that are close to tile ra and dec, tobs in MJD) in a directory where the antares client python package is installed
columns_antares=['ra','dec']
print(antares_data[columns_antares])
antares_coords = SkyCoord(ra=antares_data['ra'], dec=antares_data['dec'], unit='deg')

idx, sep2d, dist3d = match_coordinates_sky(coadds_coords, antares_coords)

selected_targets = None

select = sep2d < 3600*10*u.arcsec # this is 10 degree, made it large to find more matches for demonstration. 1 arcsec is more reasonable

selected_targets = BGS_targets[select] 


print(np.sum(select), len(selected_targets)) # 150 matched in this case
print(selected_targets)
print(select)<jupyter_output>[False False False ... False False False]
{'fraction_non_alphanumeric': 0.10393909301555776, 'fraction_numerical': 0.028136378682555446, 'mean_word_length': 6.3707317073170735, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '520366', 'license': 'MIT', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1112, 'n_words': 258, 'path': 'antares/td_bgs_tran_select.ipynb', 'repo_name': 'divijsharma18/timedomain', 'size': '3021'}