<jupyter_start><jupyter_text>TNS Ledger MakerAccess CSV downloads from TNS and produce a DESI ToO ledger.<jupyter_code>from astropy.io import ascii
from astropy.time import Time
from astropy.coordinates import SkyCoord
from astropy.table import Table, vstack
from astropy import units as u

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('font', size=16)<jupyter_output><empty_output><jupyter_text>Access CTIO-4m-DECAM DataGrab CSV data downloaded from TNS sent from DESIRT.The search is specifically from the GAMA15 fields observed by DECam from late March to mid-April 2021.<jupyter_code>data = ascii.read('desirt_ztf_tns_20210418.csv')
# data_decam = ascii.read('desirt_tns_20210415.csv')
# data_ztf = ascii.read('ztf_tns_20210415.csv')
# print(data_decam.keys())
# print(data_ztf.keys())
# vstack([data_ztf, data_decam], metadata_conflicts='silent')
# data_decam #= data_decam + data_ztf
data.sort('ID')
data<jupyter_output><empty_output><jupyter_text>Diagnostic Plots<jupyter_code>ra, dec = data['RA'], data['DEC']
mag = data['Discovery Mag/Flux']
mag_cut = None

filters = data['Discovery Filter']
print(np.unique(filters, return_counts=True))
coords = SkyCoord(ra=ra, dec=dec, unit=(u.hourangle, u.degree), frame='icrs')
for filt in np.unique(filters):
    plt.hist(mag[filters==filt], bins=np.linspace(20,28,17), alpha=0.8, label=filt)

if mag_cut is not None:
    plt.axvline(mag_cut, color='k', ls='--')
plt.legend(fontsize=12)
plt.xlabel('discovery mag')
plt.ylabel('count')
fig, ax = plt.subplots(1,1, figsize=(8,5))

if mag_cut is not None:
    select = mag<mag_cut
else:
    select = np.ones(len(coords), dtype=bool)

sc = ax.scatter(coords.ra[select], coords.dec[select], c=mag[select], cmap='magma', alpha=0.8)
cb = fig.colorbar(sc, fraction=0.046, pad=0.04)
cb.set_label('discovery magnitude')
ax.set(aspect='equal',
       xlim=(225, 210), xlabel='RA [deg]',
       ylim=(-5,5), ylabel='Dec [deg]')
ax.grid(ls=':')<jupyter_output><empty_output><jupyter_text>Write ToO Ledger<jupyter_code>def write_too_ledger(filename, too_table, checker, overwrite=False, verbose=False):
    """Write ToO ledger in the ECSV format specified by <NAME>.
    These can be passed to fiberassign for secondary targeting.
    
    Parameters
    ----------
    filename : str
        Output filename of the ledger (can be an absolute path).
    too_table : pandas.DataFrame
        Table of ToO data, using DECam format.
    checker : str
        Initials of individual(s) who have verified the ToO list.
    overwrite : bool
        If True, overwrite the output file.
    verbose : bool
        If True, 
    """
    mode = 'w' if overwrite else 'a'
    if verbose:
        mode = mode + '+'
    
    with open(filename, mode) as outf:
        if overwrite:
            outf.write("""# %ECSV 0.9
# ---
# datatype:
# - {name: RA, unit: deg, datatype: float64}
# - {name: DEC, unit: deg, datatype: float64}
# - {name: PMRA, unit: mas / yr, datatype: float32}
# - {name: PMDEC, unit: mas / yr, datatype: float32}
# - {name: REF_EPOCH, unit: yr, datatype: float32}
# - {name: CHECKER, datatype: string}
# - {name: TOO_TYPE, datatype: string}
# - {name: TOO_PRIO, datatype: string}
# - {name: OCLAYER, datatype: string}
# - {name: MJD_BEGIN, unit: d, datatype: float64}
# - {name: MJD_END, unit: d, datatype: float64}
# - {name: TOOID, datatype: int32}
# meta: {DEPNAM00: desitarget, DEPNAM01: desitarget-git, DEPVER00: 0.53.0.dev4635, DEPVER01: 0.53.0-24-g58c9a719, EXTNAME: TOO, RELEASE: 9999}
# schema: astropy-2.0
RA DEC PMRA PMDEC REF_EPOCH CHECKER TOO_TYPE TOO_PRIO OCLAYER MJD_BEGIN MJD_END TOOID\n""")
            
        today = Time.now()
        datedict = {}
        reporting = []
            
        for j, row in enumerate(too_table):
            # Ledger format:
            # datatype:
            # - {name: RA, unit: deg, datatype: float64}
            # - {name: DEC, unit: deg, datatype: float64}
            # - {name: PMRA, unit: mas / yr, datatype: float32}
            # - {name: PMDEC, unit: mas / yr, datatype: float32}
            # - {name: REF_EPOCH, unit: yr, datatype: float32}
            # - {name: CHECKER, datatype: string}
            # - {name: TOO_TYPE, datatype: string}
            # - {name: OCLAYER, datatype: string}
            # - {name: MJD_BEGIN, unit: d, datatype: float64}
            # - {name: MJD_END, unit: d, datatype: float64}
            coord = SkyCoord(ra=row['RA'], dec=row['DEC'], unit=(u.hourangle, u.degree), frame='icrs')
            ra, dec = coord.ra.to('deg').value, coord.dec.to('deg').value
            t_disc = Time(row['Discovery Date (UT)'], scale='utc')

            mag  = row['Discovery Mag/Flux']
            too_type = 'FIBER'
            too_prog = 'BRIGHT' #if mag < 21 else 'DARK'
            too_prio = 'HI'
            
            # Encode the ToO ID as: YY + DOY + NNNN.
            # E.g., ToO 15 of 17 Apr 2021 will get ID 211070015.
#             ttup = t_disc.to_datetime().timetuple()
#             yydoy = 1000*(ttup.tm_year-2000) + ttup.tm_yday
            
#             if yydoy in datedict:
#                 datedict[yydoy] += 1
#             else:
#                 datedict[yydoy] = 1

#             too_id = 10000*yydoy + datedict[yydoy]

            # Encode the ToO ID as: MJD + ID + NNN.
            mjd_disc = int(t_disc.mjd) 
            reporter = row['Reporting Group/s'].split(',')[0]
            if reporter not in reporting:
                reporting.append(reporter)
            mjd_exp = 100*mjd_disc + reporting.index(reporter)
            if mjd_exp in datedict:
                datedict[mjd_exp] += 1
            else:
                datedict[mjd_exp] = 1
            too_id = 100*mjd_exp + datedict[mjd_exp]

            epoch = 2000.0

            outf.write('{:<10.6f} {:>10.6f} {:>8.6f} {:>8.6f} {:>6.1f} {} {} {} {} {:>13.8f} {:>13.8f} {}\n'.format(
                    ra, dec, 0, 0, epoch, checker, too_type, too_prio, too_prog, t_disc.mjd, today.mjd+14, too_id))
    
        if verbose:
            outf.seek(0)
            for line in outf:
                print(line.strip())
write_too_ledger('ToO-input.ecsv', data[select], checker='SB/AP', overwrite=True, verbose=True)
data[select]
toos = None

for _ra, _dec in zip((220.8908, 221.0202, 221.1424), (-0.758, -0.7057, -0.8466)):
    yup = np.logical_and(np.abs(coords.ra.value - _ra) < 0.01, np.abs(coords.dec.value - _dec) < 0.01)
    j = np.argwhere(yup)[0,0]
    #     j = np.argwhere(np.abs(coords.ra.value - _ra) < 0.01 and np.abs(coords.dec.value - _dec) < 0.01)[0,0]

    if toos is None:
        toos = data[j]
    else:
        toos = vstack([toos, data[j]])
toos.write('too_tile252_20210419.csv', format='ascii.csv', overwrite=True)
toos<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14460495283018868, 'fraction_numerical': 0.03876768867924528, 'mean_word_length': 2.742415885273028, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '123979', 'license': 'MIT', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2479, 'n_words': 595, 'path': 'too_ledgers/TNS_ledgermaker.ipynb', 'repo_name': 'divijsharma18/timedomain', 'size': '6789'}