<commit_before>import fitsio
from legacypipe.common import *

from astrometry.util.fits import fits_table
from astrometry.util.file import trymakedirs

def add_depth_tag(survey, brick, outdir, overwrite=False):
    outfn = survey.find_file('tractor', brick=brick, output=True)
    if os.path.exists(outfn) and not overwrite:
        print 'Exists:', outfn
        return
    fn = survey.find_file('tractor', brick=brick)
    if not os.path.exists(fn):
        print 'Does not exist:', fn
        return
    T = fits_table(fn, lower=False)
    primhdr = fitsio.read_header(fn)
    hdr = fitsio.read_header(fn, ext=1)
    print 'Read', len(T), 'from', fn
    T.decam_depth    = np.zeros((len(T), len(survey.allbands)), np.float32)
    T.decam_galdepth = np.zeros((len(T), len(survey.allbands)), np.float32)
    bands = 'grz'
    ibands = [survey.index_of_band(b) for b in bands]
    ix = np.clip(np.round(T.bx).astype(int), 0, 3599)
    iy = np.clip(np.round(T.by).astype(int), 0, 3599)
    for iband,band in zip(ibands, bands):
        fn = survey.find_file('depth', brick=brick, band=band)
        if os.path.exists(fn):
            print 'Reading', fn
            img = fitsio.read(fn)
            T.decam_depth[:,iband] = img[iy, ix]

        fn = survey.find_file('galdepth', brick=brick, band=band)
        if os.path.exists(fn):
            print 'Reading', fn
            img = fitsio.read(fn)
            T.decam_galdepth[:,iband] = img[iy, ix]

    for s in [
        'Data product of the DECam Legacy Survey (DECaLS)',
        'Full documentation at http://legacysurvey.org',
        ]:
        primhdr.add_record(dict(name='COMMENT', value=s, comment=s))

    with out as survey.write_output('tractor', brick=brick):
        # print 'Header:', hdr
        # T.writeto(outfn, header=hdr, primheader=primhdr)<commit_msg>oops, fix doco for write_output, and use in add-depth-tags<commit_after>import fitsio
from legacypipe.common import *

from astrometry.util.fits import fits_table
from astrometry.util.file import trymakedirs

def add_depth_tag(survey, brick, outdir, overwrite=False):
    outfn = survey.find_file('tractor', brick=brick, output=True)
    if os.path.exists(outfn) and not overwrite:
        print 'Exists:', outfn
        return
    fn = survey.find_file('tractor', brick=brick)
    if not os.path.exists(fn):
        print 'Does not exist:', fn
        return
    T = fits_table(fn, lower=False)
    primhdr = fitsio.read_header(fn)
    hdr = fitsio.read_header(fn, ext=1)
    print 'Read', len(T), 'from', fn
    T.decam_depth    = np.zeros((len(T), len(survey.allbands)), np.float32)
    T.decam_galdepth = np.zeros((len(T), len(survey.allbands)), np.float32)
    bands = 'grz'
    ibands = [survey.index_of_band(b) for b in bands]
    ix = np.clip(np.round(T.bx).astype(int), 0, 3599)
    iy = np.clip(np.round(T.by).astype(int), 0, 3599)
    for iband,band in zip(ibands, bands):
        fn = survey.find_file('depth', brick=brick, band=band)
        if os.path.exists(fn):
            print 'Reading', fn
            img = fitsio.read(fn)
            T.decam_depth[:,iband] = img[iy, ix]

        fn = survey.find_file('galdepth', brick=brick, band=band)
        if os.path.exists(fn):
            print 'Reading', fn
            img = fitsio.read(fn)
            T.decam_galdepth[:,iband] = img[iy, ix]

    for s in [
        'Data product of the DECam Legacy Survey (DECaLS)',
        'Full documentation at http://legacysurvey.org',
        ]:
        primhdr.add_record(dict(name='COMMENT', value=s, comment=s))

    with survey.write_output('tractor', brick=brick) as out:
        # print 'Header:', hdr
        # T.writeto(outfn, header=hdr, primheader=primhdr)
{'fraction_non_alphanumeric': 0.13942177789786545, 'fraction_numerical': 0.00810591731964334, 'mean_word_length': 2.9009483667017912, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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}}
{'dir': 'git-commits-cleaned', 'id': '340124', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1296, 'n_words': 291}