diff --git a/input/tasks.json b/input/tasks.json index bd490d2..7bb3cb4 100644 --- a/input/tasks.json +++ b/input/tasks.json @@ -543,6 +543,17 @@ "always_journal": true, "priority": 209 }, + "essence_spectra": { + "nice_name": "%pre ESSENCE spectra", + "active": true, + "update": false, + "module": "supernovae.tasks.essence", + "function": "do_essence_spectra", + "groups": ["spectra"], + "repo": "input/sne-external-spectra", + "always_journal": true, + "priority": 210 + }, "hst": { "nice_name": "Coordinates from HST pointings", "active": true, diff --git a/tasks/essence.py b/tasks/essence.py index d4f0111..2db89ad 100644 --- a/tasks/essence.py +++ b/tasks/essence.py @@ -1,11 +1,16 @@ '''Import tasks for ESSENCE. ''' import csv +import datetime import os from glob import glob from astrocats.catalog.photometry import PHOTOMETRY, set_pd_mag_from_counts -from astrocats.catalog.utils import pbar +from astrocats.catalog.spectrum import SPECTRUM +from astrocats.catalog.utils import is_number, pbar, pbar_strings +from astropy.time import Time as astrotime + +from cdecimal import Decimal from ..supernova import SUPERNOVA @@ -85,3 +90,97 @@ def do_essence_photo(catalog): catalog.journal_entries() return + + +def do_essence_spectra(catalog): + task_str = catalog.get_current_task_str() + + insdict = { + "lris": "LRIS", + "esi": "ESI", + "deimos": "DEIMOS", + "gmos": "GMOS", + "fors1": "FORS1", + "bluechannel": "Blue Channel", + "ldss2": "LDSS-2", + "ldss3": "LDSS-3", + "imacs": "IMACS", + "fast": "FAST" + } + + teldict = { + "lris": "Keck", + "esi": "Keck", + "deimos": "Keck", + "gmos": "Gemini", + "fors1": "VLT", + "bluechannel": "MMT", + "ldss2": "Magellan Clay & Baade", + "ldss3": "Magellan Clay & Baade", + "imacs": "Magellan Clay & Baade", + "fast": "FLWO 1.5m" + } + + file_names = glob( + os.path.join(catalog.get_current_task_repo(), 'ESSENCE', '*')) + oldname = '' + for fi, fname in enumerate(pbar_strings(file_names, task_str)): + filename = os.path.basename(fname) + fileparts = filename.split('_') + name = 'ESSENCE ' + fileparts[0] + name = catalog.get_preferred_name(name) + if oldname and name != oldname: + catalog.journal_entries() + oldname = name + + if is_number(fileparts[1]): + doffset = 1 + else: + if fileparts[1] != 'comb': + continue + doffset = 2 + + dstr = fileparts[doffset] + mjd = str( + astrotime( + datetime.datetime( + year=int(dstr[:4]), + month=int(dstr[4:6]), + day=int(dstr[6:8])) + datetime.timedelta(days=float(dstr[ + 8:]))).mjd) + + instrument = fileparts[-1].split('.')[0] + telescope = teldict.get(instrument, '') + instrument = insdict.get(instrument, '') + + with open(fname, 'r') as f: + data = csv.reader(f, delimiter=' ', skipinitialspace=True) + data = [list(i) for i in zip(*data)] + wavelengths = data[0] + fluxes = [str(Decimal('1.0e-15') + Decimal(x)) for x in data[1]] + + name, source = catalog.new_entry(name, bibcode='2016ApJS..224....3N') + + specdict = { + SPECTRUM.TIME: mjd, + SPECTRUM.U_TIME: 'MJD', + SPECTRUM.U_WAVELENGTHS: 'Angstrom', + SPECTRUM.WAVELENGTHS: wavelengths, + SPECTRUM.FLUXES: fluxes, + SPECTRUM.U_FLUXES: 'erg/s/cm^2/Angstrom', + SPECTRUM.FILENAME: filename, + SPECTRUM.SOURCE: source + } + + if instrument: + specdict[SPECTRUM.INSTRUMENT] = instrument + if telescope: + specdict[SPECTRUM.TELESCOPE] = telescope + + catalog.entries[name].add_spectrum(**specdict) + + if catalog.args.travis and fi >= catalog.TRAVIS_QUERY_LIMIT: + break + + catalog.journal_entries() + return