Ejemplo n.º 1
0
def do_snls_photo(catalog):
    task_str = catalog.get_current_task_str()
    snls_path = os.path.join(catalog.get_current_task_repo(), 'SNLS-ugriz.dat')
    data = list(
        csv.reader(
            open(snls_path, 'r'),
            delimiter=' ',
            quotechar='"',
            skipinitialspace=True))
    for row in pbar(data, task_str):
        counts = row[3]
        err = row[4]
        name = 'SNLS-' + row[0]
        name = catalog.add_entry(name)
        source = catalog.entries[name].add_source(
            bibcode='2010A&A...523A...7G')
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
        # Conversion comes from SNLS-Readme
        # NOTE: Datafiles avail for download suggest diff zeropoints than 30,
        # but README states mags should be calculated assuming 30. Need to
        # inquire.
        zp = 30.0
        photodict = {
            PHOTOMETRY.TIME: row[2],
            PHOTOMETRY.U_TIME: 'MJD',
            PHOTOMETRY.BAND: row[1],
            PHOTOMETRY.COUNT_RATE: counts,
            PHOTOMETRY.E_COUNT_RATE: err,
            PHOTOMETRY.ZERO_POINT: str(zp),
            PHOTOMETRY.SOURCE: source,
            PHOTOMETRY.TELESCOPE: 'CFHT',
            PHOTOMETRY.INSTRUMENT: 'MegaCam',
            PHOTOMETRY.BAND_SET: 'MegaCam',
            PHOTOMETRY.SYSTEM: 'BD17'
        }
        set_pd_mag_from_counts(photodict, counts, ec=err, zp=zp, sig=5.0)
        catalog.entries[name].add_photometry(**photodict)

    catalog.journal_entries()
    return
Ejemplo n.º 2
0
def do_snls_photo(catalog):
    task_str = catalog.get_current_task_str()
    snls_path = os.path.join(catalog.get_current_task_repo(), 'SNLS-ugriz.dat')
    data = list(
        csv.reader(open(snls_path, 'r'),
                   delimiter=' ',
                   quotechar='"',
                   skipinitialspace=True))
    for row in pbar(data, task_str):
        counts = row[3]
        err = row[4]
        name = 'SNLS-' + row[0]
        name = catalog.add_entry(name)
        source = catalog.entries[name].add_source(
            bibcode='2010A&A...523A...7G')
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
        # Conversion comes from SNLS-Readme
        # NOTE: Datafiles avail for download suggest diff zeropoints than 30,
        # but README states mags should be calculated assuming 30. Need to
        # inquire.
        zp = 30.0
        photodict = {
            PHOTOMETRY.TIME: row[2],
            PHOTOMETRY.U_TIME: 'MJD',
            PHOTOMETRY.BAND: row[1],
            PHOTOMETRY.COUNT_RATE: counts,
            PHOTOMETRY.E_COUNT_RATE: err,
            PHOTOMETRY.ZERO_POINT: str(zp),
            PHOTOMETRY.SOURCE: source,
            PHOTOMETRY.TELESCOPE: 'CFHT',
            PHOTOMETRY.INSTRUMENT: 'MegaCam',
            PHOTOMETRY.BAND_SET: 'MegaCam',
            PHOTOMETRY.SYSTEM: 'BD17'
        }
        set_pd_mag_from_counts(photodict, counts, ec=err, zp=zp, sig=5.0)
        catalog.entries[name].add_photometry(**photodict)

    catalog.journal_entries()
    return
Ejemplo n.º 3
0
def _first_event_first_source(catalog):
    """Try adding a single source, with some data.
    """
    log = catalog.log
    # Add Entry to Catalog
    log.info("Calling: ``add_entry('{}')``".format(FAKE_ALIAS_1))
    name = catalog.add_entry(FAKE_ALIAS_1)
    log.debug("\t `name`: '{}'".format(name))
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure entry exists
    if FAKE_ALIAS_1 not in catalog.entries:
        raise RuntimeError("`FAKE_ALIAS_1`: '{}' is not in entries".format(
            FAKE_ALIAS_1))
    # Make sure entry has the correct name
    stored_name = catalog.entries[FAKE_ALIAS_1][ENTRY.NAME]
    if stored_name != FAKE_ALIAS_1:
        raise RuntimeError("`FAKE_ALIAS_1`[{}]: '{}' does not match".format(
            ENTRY.NAME, stored_name, FAKE_ALIAS_1))

    # Add source to entry
    log.info("Calling: ``add_source('{}')``".format(FAKE_BIBCODE_1))
    source = catalog.entries[name].add_source(
        name=FAKE_NAME_1, bibcode=FAKE_BIBCODE_1)
    log.debug("\t `source`: '{}'".format(source))
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure source alias is correct
    if source != '1':
        raise RuntimeError("Returned `source`: '{}' is wrong.".format(source))
    # Make sure source has the right properties
    check_source_1(catalog, name)

    # Add alias
    log.info("Calling: ``add_quantity('alias', '{}', '{}')``".format(
        FAKE_ALIAS_2, source))
    catalog.entries[name].add_quantity(ENTRY.ALIAS, FAKE_ALIAS_2, source)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure source alias is correct
    stored_aliases = catalog.entries[name][ENTRY.ALIAS]
    if ((len(stored_aliases) != 1 or
         stored_aliases[0]['value'] != FAKE_ALIAS_2 or
         stored_aliases[0]['source'] != source)):
        raise RuntimeError("Stored alias: '{}' looks wrong.".format(
            stored_aliases[0]))

    log.info("Calling: ``add_quantity('redshift', '{}', '{}')``".format(
        FAKE_REDZ_1, source))
    catalog.entries[name].add_quantity(
        ENTRY.REDSHIFT, FAKE_REDZ_1, source, kind='spectroscopic')
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation
    log.info("Calling: ``add_photometry(...)``")
    catalog.entries[name].add_photometry(
        time='12345', magnitude='20.0', band='g', e_magnitude='0.01',
        telescope='OWELTMT', instrument='UltraCam', observer='I. M. Fake',
        observatory='Mt. Olympus', survey='Zeus Analog Sky Survey',
        source=source)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation with mangled data
    log.info("Calling: ``add_photometry(...)``")
    catalog.entries[name].add_photometry(
        time='oiasjdqw', magnitude='oihqwr', source=source)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation without required fields
    log.info("Calling: ``add_photometry(...)``")
    catalog.entries[name].add_photometry(e_magnitude='0.01')
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation by counts
    log.info("Calling: ``add_photometry(...)``")
    photodict = {
        PHOTOMETRY.TIME: '12345',
        PHOTOMETRY.U_TIME: 'MJD',
        PHOTOMETRY.BAND: 'r',
        PHOTOMETRY.SOURCE: source
    }
    set_pd_mag_from_counts(photodict, str(100.0), ec=10.0, zp=30.0)
    catalog.entries[name].add_photometry(**photodict)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    log.info("Calling: ``journal_entries()``")
    catalog.journal_entries()
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure the remaining stub looks right
    check_stub(catalog, name)
    return
Ejemplo n.º 4
0
def _first_event_first_source(catalog):
    """Try adding a single source, with some data.
    """
    log = catalog.log
    # Add Entry to Catalog
    log.info("Calling: ``add_entry('{}')``".format(FAKE_ALIAS_1))
    name = catalog.add_entry(FAKE_ALIAS_1)
    log.debug("\t `name`: '{}'".format(name))
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure entry exists
    if FAKE_ALIAS_1 not in catalog.entries:
        raise RuntimeError(
            "`FAKE_ALIAS_1`: '{}' is not in entries".format(FAKE_ALIAS_1))
    # Make sure entry has the correct name
    stored_name = catalog.entries[FAKE_ALIAS_1][ENTRY.NAME]
    if stored_name != FAKE_ALIAS_1:
        raise RuntimeError("`FAKE_ALIAS_1`[{}]: '{}' does not match".format(
            ENTRY.NAME, stored_name, FAKE_ALIAS_1))

    # Add source to entry
    log.info("Calling: ``add_source('{}')``".format(FAKE_BIBCODE_1))
    source = catalog.entries[name].add_source(name=FAKE_NAME_1,
                                              bibcode=FAKE_BIBCODE_1)
    log.debug("\t `source`: '{}'".format(source))
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure source alias is correct
    if source != '1':
        raise RuntimeError("Returned `source`: '{}' is wrong.".format(source))
    # Make sure source has the right properties
    check_source_1(catalog, name)

    # Add alias
    log.info("Calling: ``add_quantity('alias', '{}', '{}')``".format(
        FAKE_ALIAS_2, source))
    catalog.entries[name].add_quantity(ENTRY.ALIAS, FAKE_ALIAS_2, source)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure source alias is correct
    stored_aliases = catalog.entries[name][ENTRY.ALIAS]
    if ((len(stored_aliases) != 1 or stored_aliases[0]['value'] != FAKE_ALIAS_2
         or stored_aliases[0]['source'] != source)):
        raise RuntimeError("Stored alias: '{}' looks wrong.".format(
            stored_aliases[0]))

    log.info("Calling: ``add_quantity('redshift', '{}', '{}')``".format(
        FAKE_REDZ_1, source))
    catalog.entries[name].add_quantity(ENTRY.REDSHIFT,
                                       FAKE_REDZ_1,
                                       source,
                                       kind='spectroscopic')
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation
    log.info("Calling: ``add_photometry(...)``")
    catalog.entries[name].add_photometry(time='12345',
                                         magnitude='20.0',
                                         band='g',
                                         e_magnitude='0.01',
                                         telescope='OWELTMT',
                                         instrument='UltraCam',
                                         observer='I. M. Fake',
                                         observatory='Mt. Olympus',
                                         survey='Zeus Analog Sky Survey',
                                         source=source)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation with mangled data
    log.info("Calling: ``add_photometry(...)``")
    catalog.entries[name].add_photometry(time='oiasjdqw',
                                         magnitude='oihqwr',
                                         source=source)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation without required fields
    log.info("Calling: ``add_photometry(...)``")
    catalog.entries[name].add_photometry(e_magnitude='0.01')
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    # Add a fake photometric observation by counts
    log.info("Calling: ``add_photometry(...)``")
    photodict = {
        PHOTOMETRY.TIME: '12345',
        PHOTOMETRY.U_TIME: 'MJD',
        PHOTOMETRY.BAND: 'r',
        PHOTOMETRY.SOURCE: source
    }
    set_pd_mag_from_counts(photodict, str(100.0), ec=10.0, zp=30.0)
    catalog.entries[name].add_photometry(**photodict)
    log.debug("\n{}\n".format(repr(catalog.entries[name])))

    log.info("Calling: ``journal_entries()``")
    catalog.journal_entries()
    log.debug("\n{}\n".format(repr(catalog.entries[name])))
    # Make sure the remaining stub looks right
    check_stub(catalog, name)
    return
Ejemplo n.º 5
0
    def generate_event_list(self, event_list):
        """Generate a list of events and/or convert events to JSON format."""
        prt = self._printer
        cidict = OrderedDict()
        intro_shown = False

        new_event_list = []
        previous_file = None
        for event in event_list:
            rsource = {SOURCE.NAME: self._DEFAULT_SOURCE}
            use_self_source = None
            new_events = []
            toffset = Decimal('0')
            if ('.' in event and os.path.isfile(event) and
                    not event.endswith('.json')):
                if not intro_shown:
                    prt.message('converter_info')
                    intro_shown = True

                prt.message('converting_to_json', [event])

                with open(event, 'r') as f:
                    ftxt = f.read()

                # Try a couple of table formats from astropy.
                table = None
                try:
                    table = read(ftxt, Reader=Cds, guess=False)
                except Exception:
                    pass
                else:
                    prt.message('convert_cds')
                    flines = [table.colnames] + [
                        list(x) for x in np.array(table).tolist()]
                    for i in range(len(flines)):
                        flines[i] = [str(x) for x in flines[i]]

                try:
                    table = read(ftxt, Reader=Latex, guess=False)
                except Exception:
                    pass
                else:
                    prt.message('convert_latex')
                    flines = [table.colnames] + [
                        list(x) for x in np.array(table).tolist()]

                if table is None:
                    # Count to try and determine delimiter.
                    delims = [' ', '\t', ',', ';', '|', '&']
                    delimnames = [
                        'Space: ` `', 'Tab: `\t`', 'Comma: `,`',
                        'Semi-colon: `;`', 'Bar: `|`', 'Ampersand: `&`']
                    delim = None
                    delimcounts = [ftxt.count(x) for x in delims]
                    maxdelimcount = max(delimcounts)
                    delim = delims[delimcounts.index(maxdelimcount)]
                    # If two delimiter options are close in count, ask user.
                    for i, x in enumerate(delimcounts):
                        if x > 0.5 * maxdelimcount and delims[i] != delim:
                            delim = None
                    if delim is None:
                        odelims = list(np.array(delimnames)[
                            np.array(delimcounts) > 0])
                        delim = delims[prt.prompt(
                            'delim', kind='option', options=odelims) - 1]
                    ad = list(delims)
                    ad.remove(delim)
                    ad = ''.join(ad)

                    fsplit = ftxt.splitlines()
                    fsplit = [
                        x.replace('$', '').replace('\\pm', delim)
                        .replace('±', delim).replace('(', delim + '(')
                        .strip(ad + '()# ').replace('′', "'")
                        for x in fsplit]
                    flines = []
                    for fs in fsplit:
                        flines.append(list(
                            csv.reader([fs], delimiter=delim))[0])

                    flines = [[
                        x.strip(ad + '#$()\\')
                        for x in y] for y in flines]

                    # Find band columns if they exist and insert error columns
                    # if they don't exist.
                    for fi, fl in enumerate(list(flines)):
                        flcopy = list(fl)
                        offset = 0
                        if not any([is_number(x) for x in fl]):
                            for fci, fc in enumerate(fl):
                                if (fc in self._band_names and
                                    (fci == len(fl) - 1 or
                                     fl[fci + 1] not in self._emagstrs)):
                                    flcopy.insert(fci + 1 + offset, 'e mag')
                                    offset += 1
                        flines[fi] = flcopy

                    # Find the most frequent column count. These are probably
                    # the tables we wish to read.
                    flens = [len(x) for x in flines]
                    ncols = Counter(flens).most_common(1)[0][0]

                    newlines = []
                    potential_name = None
                    for fi, fl in enumerate(flines):
                        if (len(fl) and flens[fi] == 1 and
                            fi < len(flines) - 1 and
                                flens[fi + 1] == ncols and not len(newlines)):
                            potential_name = fl[0]
                        if flens[fi] == ncols:
                            if potential_name is not None and any(
                                    [is_number(x) for x in fl]):
                                newlines.append([potential_name] + list(fl))
                            else:
                                newlines.append(list(fl))
                    flines = newlines
                    for fi, fl in enumerate(flines):
                        if len(fl) == ncols and potential_name is not None:
                            if not any([is_number(x) for x in fl]):
                                flines[fi] = ['name'] + list(fl)

                # If none of the rows contain numeric data, the file
                # is likely a list of transient names.
                if (len(flines) and
                    (not any(any([is_number(x) or x == '' for x in y])
                             for y in flines) or
                     len(flines) == 1)):
                    new_events = [
                        it for s in flines for it in s]

                # If last row is numeric, then likely this is a file with
                # transient data.
                elif (len(flines) > 1 and
                        any([is_number(x) for x in flines[-1]])):

                    # Check that each row has the same number of columns.
                    if len(set([len(x) for x in flines])) > 1:
                        print(set([len(x) for x in flines]))
                        raise ValueError(
                            'Number of columns in each row not '
                            'consistent!')

                    if len(cidict) and len(new_event_list):
                        msg = ('is_file_same' if
                               previous_file else 'is_event_same')
                        reps = [previous_file] if previous_file else [''.join(
                            new_event_list[-1].split('.')[:-1])]
                        text = prt.text(msg, reps)
                        is_same = prt.prompt(text, message=False,
                                             kind='bool')
                        if not is_same:
                            cidict = OrderedDict()

                    # If the first row has no numbers it is likely a header.
                    if not len(cidict):
                        self.assign_columns(cidict, flines)

                    perms = 1
                    for key in cidict:
                        if isinstance(cidict[key], list) and not isinstance(
                                cidict[key], string_types):
                            if cidict[key][0] != 'j':
                                perms = len(cidict[key])

                    # Get event name (if single event) or list of names from
                    # table.
                    event_names = []
                    if ENTRY.NAME in cidict:
                        for fi, fl in enumerate(flines):
                            flines[fi][cidict[ENTRY.NAME]] = name_clean(
                                fl[cidict[ENTRY.NAME]])
                        event_names = list(sorted(set([
                            x[cidict[ENTRY.NAME]] for x in flines[
                                self._first_data:]])))
                        new_events = [x + '.json' for x in event_names]
                    else:
                        new_event_name = '.'.join(event.split(
                            '.')[:-1]).split('/')[-1]
                        text = prt.message(
                            'is_event_name', [new_event_name], prt=False)
                        is_name = prt.prompt(text, message=False,
                                             kind='bool', default='y')
                        if not is_name:
                            new_event_name = ''
                            while new_event_name.strip() == '':
                                new_event_name = prt.prompt(
                                    'enter_name', kind='string')
                        event_names.append(new_event_name)
                        new_events = [new_event_name + '.json']

                    # Create a new event, populate the photometry, and dump
                    # to a JSON file in the run directory.
                    entries = OrderedDict([(x, Entry(name=x))
                                           for x in event_names])

                    # Clean up the data a bit now that we know the column
                    # identities.

                    # Strip common prefixes/suffixes from band names
                    if PHOTOMETRY.BAND in cidict:
                        bi = cidict[PHOTOMETRY.BAND]
                        for d in [True, False]:
                            if not isinstance(bi, (int, np.integer)):
                                break
                            strip_cols = []
                            lens = [len(x[bi])
                                    for x in flines[self._first_data:]]
                            llen = min(lens)
                            ra = range(llen) if d else range(-1, -llen - 1, -1)
                            for li in ra:
                                letter = None
                                for row in list(flines[self._first_data:]):
                                    if letter is None:
                                        letter = row[bi][li]
                                    elif row[bi][li] != letter:
                                        letter = None
                                        break
                                if letter is not None:
                                    strip_cols.append(li)
                                else:
                                    break
                            if len(strip_cols) == llen:
                                break
                            for ri in range(len(flines[self._first_data:])):
                                flines[self._first_data + ri][bi] = ''.join(
                                    [c for i, c in enumerate(flines[
                                        self._first_data + ri][bi])
                                     if (i if d else i - len(flines[
                                         self._first_data + ri][bi])) not in
                                     strip_cols])

                    if (PHOTOMETRY.TIME in cidict and
                            (not isinstance(cidict[PHOTOMETRY.TIME], list) or
                             len(cidict[PHOTOMETRY.TIME]) <= 2)):
                        bi = cidict[PHOTOMETRY.TIME]

                        if isinstance(bi, list) and not isinstance(
                            bi, string_types) and isinstance(
                                bi[0], string_types) and bi[0] == 'jd':
                            bi = bi[-1]

                        mmtimes = [float(x[bi])
                                   for x in flines[self._first_data:]]
                        mintime, maxtime = min(mmtimes), max(mmtimes)

                        if mintime < 10000:
                            while True:
                                try:
                                    response = prt.prompt(
                                        'small_time_offset', kind='string')
                                    if response is not None:
                                        toffset = Decimal(response)
                                    break
                                except Exception:
                                    pass
                        elif maxtime > 60000 and cidict[
                                PHOTOMETRY.TIME][0] != 'jd':
                            isjd = prt.prompt(
                                'large_time_offset',
                                kind='bool', default='y')
                            if isjd:
                                toffset = Decimal('-2400000.5')

                    for row in flines[self._first_data:]:
                        photodict = {}
                        rname = (row[cidict[ENTRY.NAME]]
                                 if ENTRY.NAME in cidict else event_names[0])
                        for pi in range(perms):
                            sources = set()
                            for key in cidict:
                                if key in self._bool_keys:
                                    rval = row[cidict[key]]

                                    if rval in self._FALSE_VALS:
                                        rval = False
                                    elif rval in self._TRUE_VALS:
                                        rval = True

                                    if type(rval) != 'bool':
                                        try:
                                            rval = bool(rval)
                                        except Exception:
                                            pass

                                    if type(rval) != 'bool':
                                        try:
                                            rval = bool(float(rval))
                                        except Exception:
                                            rval = True

                                    if not rval:
                                        continue
                                    row[cidict[key]] = rval
                                elif key == 'reference':
                                    if (isinstance(cidict[key],
                                                   string_types) and
                                            len(cidict[key]) == 19):
                                        new_src = entries[rname].add_source(
                                            bibcode=cidict[key])
                                        sources.update(new_src)
                                        row[
                                            cidict[key]] = new_src
                                elif key == ENTRY.NAME:
                                    continue
                                elif (isinstance(key, Key) and
                                        key.type == KEY_TYPES.TIME and
                                        isinstance(cidict[key], list) and not
                                        isinstance(cidict[key],
                                                   string_types)):
                                    tval = np.array(row)[np.array(cidict[key][
                                        1:], dtype=int)]
                                    if cidict[key][0] == 'j':
                                        date = '-'.join([x.zfill(2) for x in
                                                         tval])
                                        date = self._month_rep.sub(
                                            lambda x: self._MONTH_IDS[
                                                x.group()], date)
                                        photodict[key] = str(
                                            astrotime(date, format='isot').mjd)
                                    elif cidict[key][0] == 'jd':
                                        photodict[key] = str(
                                            jd_to_mjd(Decimal(tval[-1])))
                                    continue

                                val = cidict[key]
                                if (isinstance(val, list) and not
                                        isinstance(val, string_types)):
                                    val = val[pi]
                                    if isinstance(val, string_types):
                                        if val != '':
                                            photodict[key] = val
                                    else:
                                        photodict[key] = row[val]
                                else:
                                    if isinstance(val, string_types):
                                        if val != '':
                                            photodict[key] = val
                                    else:
                                        photodict[key] = row[val]
                            if self._data_type == 2:
                                if self._zp:
                                    photodict[PHOTOMETRY.ZERO_POINT] = self._zp
                                else:
                                    photodict[PHOTOMETRY.ZERO_POINT] = (
                                        row[cidict[PHOTOMETRY.ZERO_POINT][pi]]
                                        if isinstance(cidict[
                                            PHOTOMETRY.ZERO_POINT], list) else
                                        row[cidict[PHOTOMETRY.ZERO_POINT]])
                                zpp = photodict[PHOTOMETRY.ZERO_POINT]
                                cc = (
                                    row[cidict[PHOTOMETRY.COUNT_RATE][pi]] if
                                    isinstance(cidict[
                                        PHOTOMETRY.COUNT_RATE], list) else
                                    row[cidict[PHOTOMETRY.COUNT_RATE]])
                                ecc = (
                                    row[cidict[PHOTOMETRY.E_COUNT_RATE][pi]] if
                                    isinstance(cidict[
                                        PHOTOMETRY.E_COUNT_RATE], list) else
                                    row[cidict[PHOTOMETRY.E_COUNT_RATE]])
                                if '<' in cc:
                                    set_pd_mag_from_counts(
                                        photodict, ec=cc.strip('<'), zp=zpp)
                                else:
                                    set_pd_mag_from_counts(
                                        photodict, c=cc, ec=ecc, zp=zpp)
                            elif self._data_type == 3:
                                photodict[
                                    PHOTOMETRY.U_FLUX_DENSITY] = self._ufd
                                if PHOTOMETRY.U_FLUX_DENSITY in cidict:
                                    photodict[PHOTOMETRY.U_FLUX_DENSITY] = (
                                        row[cidict[
                                            PHOTOMETRY.U_FLUX_DENSITY][pi]]
                                        if isinstance(cidict[
                                            PHOTOMETRY.
                                            U_FLUX_DENSITY], list) else
                                        row[cidict[PHOTOMETRY.U_FLUX_DENSITY]])
                                if photodict[
                                        PHOTOMETRY.U_FLUX_DENSITY] == '':
                                    photodict[
                                        PHOTOMETRY.U_FLUX_DENSITY] = 'µJy'
                                fd = (
                                    row[cidict[PHOTOMETRY.FLUX_DENSITY][pi]] if
                                    isinstance(cidict[
                                        PHOTOMETRY.FLUX_DENSITY], list) else
                                    row[cidict[PHOTOMETRY.FLUX_DENSITY]])
                                efd = (
                                    row[cidict[
                                        PHOTOMETRY.E_FLUX_DENSITY][pi]] if
                                    isinstance(cidict[
                                        PHOTOMETRY.E_FLUX_DENSITY], list) else
                                    row[cidict[PHOTOMETRY.E_FLUX_DENSITY]])

                                mult = Decimal('1')
                                ufd = photodict[PHOTOMETRY.U_FLUX_DENSITY]
                                if ufd.lower() in [
                                        'mjy', 'millijy', 'millijansky']:
                                    mult = Decimal('1e3')
                                elif ufd.lower() in ['jy', 'jansky']:
                                    mult = Decimal('1e6')

                                if '<' in fd:
                                    set_pd_mag_from_flux_density(
                                        photodict, efd=str(
                                            Decimal(fd.strip('<')) * mult))
                                else:
                                    set_pd_mag_from_flux_density(
                                        photodict, fd=Decimal(fd) * mult,
                                        efd=Decimal(efd) * mult)
                            if not len(sources):
                                if use_self_source is None:
                                    sopts = [
                                        ('Bibcode', 'b'), ('Last name', 'l')]
                                    if self._require_source:
                                        sel_str = 'must_select_source'
                                    else:
                                        sel_str = 'select_source'
                                    text = prt.text(sel_str)
                                    skind = prt.prompt(
                                        text, kind='option',
                                        options=sopts, default='b',
                                        none_string=(
                                            None if self._require_source else
                                            'Neither, tag MOSFiT as source'))
                                    if skind == 'b':
                                        rsource = {}
                                        bibcode = ''

                                        while len(bibcode) != 19:
                                            bibcode = prt.prompt(
                                                'bibcode',
                                                kind='string',
                                                allow_blank=False
                                            )
                                            bibcode = bibcode.strip()
                                            if (re.search(
                                                '[0-9]{4}..........[\.0-9]{4}'
                                                '[A-Za-z]', bibcode)
                                                    is None):
                                                bibcode = ''
                                        rsource[
                                            SOURCE.BIBCODE] = bibcode
                                        use_self_source = False
                                    elif skind == 'l':
                                        rsource = {}
                                        last_name = prt.prompt(
                                            'last_name', kind='string'
                                        )
                                        rsource[
                                            SOURCE.NAME] = (
                                                last_name.strip().title() +
                                                ' et al., in preparation')
                                        use_self_source = False
                                    elif skind == 'n':
                                        use_self_source = True

                                photodict[
                                    PHOTOMETRY.SOURCE] = entries[
                                        rname].add_source(**rsource)

                            if any([x in photodict.get(
                                    PHOTOMETRY.MAGNITUDE, '')
                                    for x in ['<', '>']]):
                                photodict[PHOTOMETRY.UPPER_LIMIT] = True
                                photodict[
                                    PHOTOMETRY.MAGNITUDE] = photodict[
                                        PHOTOMETRY.MAGNITUDE].strip('<>')

                            if '<' in photodict.get(PHOTOMETRY.COUNT_RATE, ''):
                                photodict[PHOTOMETRY.UPPER_LIMIT] = True
                                photodict[
                                    PHOTOMETRY.COUNT_RATE] = photodict[
                                        PHOTOMETRY.COUNT_RATE].strip('<')
                                if PHOTOMETRY.E_COUNT_RATE in photodict:
                                    del(photodict[PHOTOMETRY.E_COUNT_RATE])

                            if '<' in photodict.get(
                                    PHOTOMETRY.FLUX_DENSITY, ''):
                                photodict[PHOTOMETRY.UPPER_LIMIT] = True
                                photodict[
                                    PHOTOMETRY.FLUX_DENSITY] = photodict[
                                        PHOTOMETRY.FLUX_DENSITY].strip('<')
                                if PHOTOMETRY.E_FLUX_DENSITY in photodict:
                                    del(photodict[PHOTOMETRY.E_FLUX_DENSITY])

                            # Apply offset time if set.
                            if (PHOTOMETRY.TIME in photodict and
                                    toffset != Decimal('0')):
                                photodict[PHOTOMETRY.TIME] = str(
                                    Decimal(photodict[PHOTOMETRY.TIME]) +
                                    toffset)

                            # Skip entries for which key values are not
                            # expected type.
                            if not all([
                                is_number(photodict.get(x, ''))
                                for x in photodict.keys() if
                                (PHOTOMETRY.get_key_by_name(x).type ==
                                 KEY_TYPES.NUMERIC)]):
                                continue

                            # Skip placeholder values.
                            if float(photodict.get(
                                    PHOTOMETRY.MAGNITUDE, 0.0)) > 50.0:
                                continue

                            # Add system if specified by user.
                            if (self._system is not None and
                                    PHOTOMETRY.SYSTEM not in photodict):
                                photodict[PHOTOMETRY.SYSTEM] = self._system

                            # Remove keys not in the `PHOTOMETRY` class.
                            for key in list(photodict.keys()):
                                if key not in PHOTOMETRY.vals():
                                    del(photodict[key])

                            # Add the photometry.
                            entries[rname].add_photometry(
                                **photodict)

                    merge_with_existing = None
                    for ei, entry in enumerate(entries):
                        entries[entry].sanitize()
                        if os.path.isfile(new_events[ei]):
                            if merge_with_existing is None:
                                merge_with_existing = prt.prompt(
                                    'merge_with_existing', default='y')
                            if merge_with_existing:
                                existing = Entry.init_from_file(
                                    catalog=None,
                                    name=event_names[ei],
                                    path=new_events[ei],
                                    merge=False,
                                    pop_schema=False,
                                    ignore_keys=[ENTRY.MODELS],
                                    compare_to_existing=False)
                                Catalog().copy_entry_to_entry(
                                    existing, entries[entry])

                        oentry = entries[entry]._ordered(entries[entry])
                        entabbed_json_dump(
                            {entry: oentry}, open(new_events[ei], 'w'),
                            separators=(',', ':'))

                    self._converted.extend([
                        [event_names[x], new_events[x]]
                        for x in range(len(event_names))])

                new_event_list.extend(new_events)
                previous_file = event
            else:
                new_event_list.append(event)

        return new_event_list
Ejemplo n.º 6
0
def do_essence_photo(catalog):
    task_str = catalog.get_current_task_str()
    ess_path = os.path.join(catalog.get_current_task_repo(), 'ESSENCE',
                            'obj_table.dat')
    data = list(
        csv.reader(open(ess_path, 'r'),
                   delimiter=' ',
                   quotechar='"',
                   skipinitialspace=True))
    for row in pbar(data[1:], task_str):
        etype = row[2]
        if etype.upper().replace('?', '') in catalog.nonsnetypes:
            continue
        ess_name = 'ESSENCE ' + row[0]
        name, source = catalog.new_entry(ess_name,
                                         bibcode='2016ApJS..224....3N')
        if row[1] != '---':
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, 'SN' + row[1],
                                               source)
        if etype != '---':
            catalog.entries[name].add_quantity(SUPERNOVA.CLAIMED_TYPE, etype,
                                               source)
        catalog.entries[name].add_quantity(SUPERNOVA.RA, row[5], source)
        catalog.entries[name].add_quantity(SUPERNOVA.DEC, row[6], source)
        if is_number(row[11]):
            quantdict = {
                QUANTITY.VALUE: row[11],
                QUANTITY.SOURCE: source,
                QUANTITY.KIND: 'host'
            }
            if is_number(row[12]):
                quantdict[QUANTITY.E_VALUE] = row[12]
            catalog.entries[name].add_quantity(
                [SUPERNOVA.REDSHIFT, SUPERNOVA.HOST_REDSHIFT], **quantdict)

    files = glob(
        os.path.join(catalog.get_current_task_repo(), 'ESSENCE',
                     '*clean*.dat'))

    for pfile in pbar(files, task_str):
        name = 'ESSENCE ' + pfile.split('/')[-1].split('.')[0]
        name, source = catalog.new_entry(name, bibcode='2016ApJS..224....3N')
        with open(pfile, 'r') as f:
            rows = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        for ri, row in enumerate(rows):
            if ri == 1:
                catalog.entries[name].add_quantity(
                    SUPERNOVA.REDSHIFT,
                    row[5],
                    source,
                    kind=['spectroscopic', 'heliocentric'])
                catalog.entries[name].add_quantity(
                    SUPERNOVA.REDSHIFT,
                    row[6],
                    source,
                    kind=['spectroscopic', 'cmb'])
                continue
            if row[0].startswith('#'):
                continue
            counts = row[3][:6]
            lerr = row[4][:6]
            uerr = row[5][:6]
            zp = 25.0
            photodict = {
                PHOTOMETRY.TIME: row[1],
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.BAND: row[2][0],
                PHOTOMETRY.COUNT_RATE: counts,
                PHOTOMETRY.E_LOWER_COUNT_RATE: lerr,
                PHOTOMETRY.E_UPPER_COUNT_RATE: uerr,
                PHOTOMETRY.ZERO_POINT: str(zp),
                PHOTOMETRY.SOURCE: source,
                PHOTOMETRY.TELESCOPE: 'CTIO 4m',
                PHOTOMETRY.SYSTEM: 'Natural'
            }
            set_pd_mag_from_counts(photodict,
                                   counts,
                                   ec='',
                                   lec=lerr,
                                   uec=uerr,
                                   zp=zp,
                                   sig=5.0)
            catalog.entries[name].add_photometry(**photodict)

    catalog.journal_entries()
    return
Ejemplo n.º 7
0
def do_donated_photo(catalog):
    """Import donated photometry."""
    task_str = catalog.get_current_task_str()

    # Private donations here #
    if not catalog.args.travis:
        pass
    # End private donations #

    # Ponder 05-12-17 donation
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Ponder-05-12-17', 'meta.json'), 'r') as f:
        metadict = json.loads(f.read())
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Ponder-05-12-17', '*.dat'))
    for path in file_names:
        with open(path, 'r') as f:
            tsvin = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        oname = path.split('/')[-1].split('.')[0]
        name, source = catalog.new_entry(
            oname, bibcode=metadict[oname]['bibcode'])
        for row in pbar(tsvin, task_str + ': Ponder ' + oname):
            if row[0][0] == '#' or not is_number(row[-1]):
                continue
            mjd = row[1]
            bandinst = row[2].split('_')
            band = bandinst[0]
            inst = ''
            if len(bandinst) > 1:
                inst = bandinst[1]
            mag = row[3]
            uerr = row[4]
            lerr = row[5]
            photodict = {
                PHOTOMETRY.TIME: mjd,
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.BAND: band,
                PHOTOMETRY.MAGNITUDE: mag,
                PHOTOMETRY.E_LOWER_MAGNITUDE: lerr,
                PHOTOMETRY.E_UPPER_MAGNITUDE: uerr,
                PHOTOMETRY.SOURCE: source
            }
            if inst:
                photodict[PHOTOMETRY.INSTRUMENT] = inst
            catalog.entries[name].add_photometry(**photodict)

    # Benetti 03-08-17 donation
    path = os.path.join(catalog.get_current_task_repo(), 'Donations',
                        'Benetti-03-08-17', '1999E.dat')
    with open(path, 'r') as f:
        tsvin = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        name, source = catalog.new_entry(
            'SN1999E', bibcode='2003MNRAS.340..191R')
        bands = None
        for row in tsvin:
            if not row or row[0][0] == '#':
                continue
            if not bands:
                bands = row[2:-2]
                continue
            mjd = row[1]
            tel = row[-1] if 'IAUC' not in row[-1] else None
            for bi, band in enumerate(bands):
                mag = row[2 + 2 * bi]
                if mag == '9999':
                    continue
                err = row[2 + 2 * bi + 1]
                limit = row[6] == 'True'
                photodict = {
                    PHOTOMETRY.TIME: mjd,
                    PHOTOMETRY.U_TIME: 'MJD',
                    PHOTOMETRY.TELESCOPE: tel,
                    PHOTOMETRY.BAND: band,
                    PHOTOMETRY.MAGNITUDE: mag,
                    PHOTOMETRY.SOURCE: source
                }
                if err != '.00':
                    photodict[PHOTOMETRY.E_MAGNITUDE] = str(Decimal(err))
                if tel:
                    photodict[PHOTOMETRY.TELESCOPE] = tel
                catalog.entries[name].add_photometry(**photodict)

    # Nicholl 01-29-17 donation
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Nicholl-01-29-17', 'meta.json'), 'r') as f:
        metadict = json.loads(f.read())
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nicholl-01-29-17', '*.txt'))
    for path in file_names:
        data = read(path, format='cds')
        oname = path.split('/')[-1].split('_')[0]
        name, source = catalog.new_entry(
            oname, bibcode=metadict[oname]['bibcode'])
        for row in pbar(data, task_str + ': Nicholl ' + oname):
            photodict = {
                PHOTOMETRY.TIME: str(row['MJD']),
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.MAGNITUDE: str(row['mag']),
                PHOTOMETRY.BAND: row['Filter'],
                PHOTOMETRY.SOURCE: source
            }
            if 'system' in metadict[oname]:
                photodict[PHOTOMETRY.SYSTEM] = metadict[oname]['system']
            if 'l_mag' in row.columns and row['l_mag'] == '>':
                photodict[PHOTOMETRY.UPPER_LIMIT] = True
            elif 'e_mag' in row.columns:
                photodict[PHOTOMETRY.E_MAGNITUDE] = str(row['e_mag'])
            if 'Telescope' in row.columns:
                photodict[PHOTOMETRY.TELESCOPE] = row['Telescope']
            catalog.entries[name].add_photometry(**photodict)

    # Arcavi 2016gkg donation
    path = os.path.join(catalog.get_current_task_repo(), 'Donations',
                        'Arcavi-01-24-17', 'SN2016gkg.txt')
    with open(path, 'r') as f:
        tsvin = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        name, source = catalog.new_entry(
            'SN2016gkg', bibcode='2016arXiv161106451A')
        for row in tsvin:
            if row[0][0] == '#':
                continue
            mjd = str(jd_to_mjd(Decimal(row[0])))
            tel = row[1]
            band = row[3]
            mag = row[4]
            err = row[5]
            limit = row[6] == 'True'
            photodict = {
                PHOTOMETRY.TIME: mjd,
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.TELESCOPE: tel,
                PHOTOMETRY.BAND: band,
                PHOTOMETRY.MAGNITUDE: mag,
                PHOTOMETRY.SOURCE: source
            }
            if limit:
                photodict[PHOTOMETRY.UPPER_LIMIT] = True
            else:
                photodict[PHOTOMETRY.E_MAGNITUDE] = err
            catalog.entries[name].add_photometry(**photodict)

    # Nicholl Gaia16apd donation
    path = os.path.join(catalog.get_current_task_repo(), 'Donations',
                        'Nicholl-01-20-17', 'gaia16apd_phot.txt')

    data = read(path, format='cds')
    name, source = catalog.new_entry(
        'Gaia16apd', bibcode='2017ApJ...835L...8N')
    for row in pbar(data, task_str + ': Nicholl Gaia16apd'):
        photodict = {
            PHOTOMETRY.TIME: str(row['MJD']),
            PHOTOMETRY.U_TIME: 'MJD',
            PHOTOMETRY.MAGNITUDE: str(row['mag']),
            PHOTOMETRY.BAND: row['Filter'],
            PHOTOMETRY.TELESCOPE: row['Telescope'],
            PHOTOMETRY.SOURCE: source
        }
        if row['l_mag'] == '>':
            photodict[PHOTOMETRY.UPPER_LIMIT] = True
        else:
            photodict[PHOTOMETRY.E_MAGNITUDE] = str(row['e_mag'])
        catalog.entries[name].add_photometry(**photodict)

    # Kuncarayakti-01-09-17
    datafile = os.path.join(catalog.get_current_task_repo(), 'Donations',
                            'Kuncarayakti-01-09-17', 'SN1978K.dat')
    inpname = os.path.basename(datafile).split('.')[0]
    with open(datafile, 'r') as f:
        tsvin = csv.reader(f, delimiter=' ', skipinitialspace=True)
        host = False
        for ri, row in enumerate(tsvin):
            if ri == 0:
                continue
            if row[0][0] == '#':
                rsplit = [x.strip('# ') for x in ' '.join(row).split(',')]
                bc = rsplit[0]
                tel, ins = '', ''
                if len(rsplit) > 1:
                    tel = rsplit[1]
                if len(rsplit) > 2:
                    ins = rsplit[2]
                continue
            (name, source) = catalog.new_entry(inpname, bibcode=bc)
            mag = row[4]
            err = row[5]
            mjd = str(astrotime('-'.join(row[:3]), format='iso').mjd)
            photodict = {
                PHOTOMETRY.BAND: row[3],
                PHOTOMETRY.TIME: mjd,
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.MAGNITUDE: mag.strip('>s'),
                PHOTOMETRY.SOURCE: source
            }
            if is_number(err):
                photodict[PHOTOMETRY.E_MAGNITUDE] = err
            if tel:
                photodict[PHOTOMETRY.TELESCOPE] = tel
            if ins:
                photodict[PHOTOMETRY.INSTRUMENT] = ins
            if '>' in mag:
                photodict[PHOTOMETRY.UPPER_LIMIT] = True
            if 's' in mag:
                photodict[PHOTOMETRY.SYNTHETIC] = True
            catalog.entries[name].add_photometry(**photodict)

    # Nugent 01-09-17 donation
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nugent-01-09-17', '*.dat'))
    for datafile in pbar_strings(file_names, task_str + ': Nugent-01-09-17'):
        inpname = os.path.basename(datafile).split('.')[0]
        (name, source) = catalog.new_entry(
            inpname, bibcode='2006ApJ...645..841N')
        with open(datafile, 'r') as f:
            tsvin = csv.reader(f, delimiter=' ', skipinitialspace=True)
            host = False
            for urow in tsvin:
                row = list(filter(None, urow))
                counts = row[2]
                e_counts = row[3]
                zp = row[4]
                photodict = {
                    PHOTOMETRY.BAND: row[1],
                    PHOTOMETRY.TIME: row[0],
                    PHOTOMETRY.U_TIME: 'MJD',
                    PHOTOMETRY.COUNT_RATE: counts,
                    PHOTOMETRY.E_COUNT_RATE: e_counts,
                    PHOTOMETRY.ZERO_POINT: zp,
                    PHOTOMETRY.TELESCOPE: 'CFHT',
                    PHOTOMETRY.SURVEY: 'SNLS',
                    PHOTOMETRY.SOURCE: source
                }
                set_pd_mag_from_counts(photodict, counts, ec=e_counts, zp=zp,
                                       sig=5.0)
                catalog.entries[name].add_photometry(**photodict)

    # Inserra 09-04-16 donation
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Inserra-09-04-16', '*.txt'))
    for datafile in pbar_strings(file_names, task_str + ': Inserra-09-04-16'):
        inpname = os.path.basename(datafile).split('.')[0]
        (name, source) = catalog.new_entry(
            inpname, bibcode='2013ApJ...770..128I')
        with open(datafile, 'r') as f:
            tsvin = csv.reader(f, delimiter=' ', skipinitialspace=True)
            host = False
            for row in tsvin:
                if row[0][0] == '#':
                    if row[0] == '#Host':
                        host = True
                        continue
                    host = False
                    bands = row[3:-1]
                    continue
                for bi, ba in enumerate(bands):
                    mag = row[5 + 2 * bi]
                    if not is_number(mag):
                        continue
                    system = 'AB'
                    if ba in ['U', 'B', 'V', 'R', 'I', 'J', 'H', 'K']:
                        system = 'Vega'
                    photodict = {
                        PHOTOMETRY.TIME: row[3],
                        PHOTOMETRY.U_TIME: 'MJD',
                        PHOTOMETRY.BAND: ba,
                        PHOTOMETRY.MAGNITUDE: mag.strip('< '),
                        PHOTOMETRY.SOURCE: source,
                        PHOTOMETRY.SYSTEM: system
                    }
                    if 'ATel' not in row[-1]:
                        photodict[PHOTOMETRY.TELESCOPE] = row[-1]
                    if host:
                        photodict[PHOTOMETRY.HOST] = True
                    if '<' in mag:
                        photodict[PHOTOMETRY.UPPER_LIMIT] = True
                    e_mag = row[5 + 2 * bi + 1].strip('() ')
                    if is_number(e_mag):
                        photodict[PHOTOMETRY.E_MAGNITUDE] = e_mag
                    catalog.entries[name].add_photometry(**photodict)

    # Nicholl 04-01-16 donation
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Nicholl-04-01-16', 'bibcodes.json'), 'r') as f:
        bcs = json.loads(f.read())

    kcorrected = ['SN2011ke', 'SN2011kf', 'SN2012il', 'PTF10hgi', 'PTF11rks']
    ignorephoto = ['PTF10hgi', 'PTF11rks', 'SN2011ke', 'SN2011kf', 'SN2012il']

    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nicholl-04-01-16/*.txt'))
    for datafile in pbar_strings(file_names, task_str + ': Nicholl-04-01-16'):
        inpname = os.path.basename(datafile).split('_')[0]
        isk = inpname in kcorrected
        name = catalog.add_entry(inpname)
        bibcode = ''
        for bc in bcs:
            if inpname in bcs[bc]:
                bibcode = bc
        if not bibcode:
            raise ValueError('Bibcode not found!')
        source = catalog.entries[name].add_source(bibcode=bibcode)
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, inpname, source)
        if inpname in ignorephoto:
            continue
        with open(datafile, 'r') as f:
            tsvin = csv.reader(f, delimiter='\t', skipinitialspace=True)
            rtelescope = ''
            for r, rrow in enumerate(tsvin):
                row = list(filter(None, rrow))
                if not row:
                    continue
                if row[0] == '#MJD':
                    bands = [x for x in row[1:] if x and 'err' not in x]
                elif row[0][0] == '#' and len(row[0]) > 1:
                    rtelescope = row[0][1:]
                if row[0][0] == '#':
                    continue
                mjd = row[0]
                if not is_number(mjd):
                    continue
                for v, val in enumerate(row[1::2]):
                    upperlimit = ''
                    mag = val.strip('>')
                    emag = row[2 * v + 2]
                    if '>' in val or (is_number(emag) and float(emag) == 0.0):
                        upperlimit = True
                    if (not is_number(mag) or isnan(float(mag)) or
                            float(mag) > 90.0):
                        continue
                    band = bands[v]
                    instrument = ''
                    survey = ''
                    system = ''
                    telescope = rtelescope
                    if telescope == 'LSQ':
                        instrument = 'QUEST'
                    elif telescope == 'PS1':
                        instrument = 'GPC'
                    elif telescope == 'NTT':
                        instrument = 'EFOSC'
                    elif telescope == 'GROND':
                        instrument = 'GROND'
                        telescope = 'MPI/ESO 2.2m'
                    else:
                        if band == 'NUV':
                            instrument = 'GALEX'
                            telescope = 'GALEX'
                        elif band in ['u', 'g', 'r', 'i', 'z']:
                            if inpname.startswith('PS1'):
                                instrument = 'GPC'
                                telescope = 'PS1'
                                survey = 'Pan-STARRS'
                            elif inpname.startswith('PTF'):
                                telescope = 'P60'
                                survey = 'PTF'
                        elif band.upper() in ['UVW2', 'UVW1', 'UVM2']:
                            instrument = 'UVOT'
                            telescope = 'Swift'
                            if inpname in ['PTF12dam']:
                                system = 'AB'
                    if inpname in ['SCP-06F6']:
                        system = 'Vega'
                    photodict = {
                        PHOTOMETRY.TIME: mjd,
                        PHOTOMETRY.U_TIME: 'MJD',
                        PHOTOMETRY.BAND: band,
                        PHOTOMETRY.MAGNITUDE: mag,
                        PHOTOMETRY.UPPER_LIMIT: upperlimit,
                        PHOTOMETRY.SOURCE: source
                    }
                    if instrument:
                        photodict[PHOTOMETRY.INSTRUMENT] = instrument
                    if telescope:
                        photodict[PHOTOMETRY.TELESCOPE] = telescope
                    if survey:
                        photodict[PHOTOMETRY.SURVEY] = survey
                    if system:
                        photodict[PHOTOMETRY.SYSTEM] = system
                    if (is_number(emag) and
                            not isnan(float(emag)) and float(emag) > 0.0):
                        photodict[PHOTOMETRY.E_MAGNITUDE] = emag
                    if isk:
                        photodict[PHOTOMETRY.KCORRECTED] = True
                    catalog.entries[name].add_photometry(**photodict)
    catalog.journal_entries()

    # Maggi 04-11-16 donation (MC SNRs)
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Maggi-04-11-16', 'LMCSNRs_OpenSNe.csv')) as f:
        tsvin = csv.reader(f, delimiter=',')
        for row in pbar(list(tsvin), task_str + ': Maggi-04-11-16/LMCSNRs'):
            name = 'MCSNR ' + row[0]
            name = catalog.add_entry(name)
            ra = row[2]
            dec = row[3]
            source = (catalog.entries[name]
                      .add_source(bibcode='2016A&A...585A.162M'))
            catalog.entries[name].add_quantity(
                SUPERNOVA.ALIAS,
                'LMCSNR J' + rep_chars(ra, ' :.') + rep_chars(dec, ' :.'),
                source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
            if row[1] != 'noname':
                catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, row[1],
                                                   source)
            catalog.entries[name].add_quantity(SUPERNOVA.RA, row[2], source)
            catalog.entries[name].add_quantity(SUPERNOVA.DEC, row[3], source)
            catalog.entries[name].add_quantity(SUPERNOVA.HOST, 'LMC', source)
            if row[4] == '1':
                catalog.entries[name].add_quantity(SUPERNOVA.CLAIMED_TYPE,
                                                   'Ia', source)
            elif row[4] == '2':
                catalog.entries[name].add_quantity(SUPERNOVA.CLAIMED_TYPE,
                                                   'CC', source)
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Maggi-04-11-16', 'SMCSNRs_OpenSNe.csv')) as f:
        tsvin = csv.reader(f, delimiter=',')
        for row in pbar(list(tsvin), task_str + ': Maggi-04-11-16/SMCSNRs'):
            name = 'MCSNR ' + row[0]
            name = catalog.add_entry(name)
            source = catalog.entries[name].add_source(name='Pierre Maggi')
            ra = row[3]
            dec = row[4]
            catalog.entries[name].add_quantity(
                SUPERNOVA.ALIAS, 'SMCSNR J' + ra.replace(
                    ':', '')[:6] + dec.replace(':', '')[:7], source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, row[1], source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, row[2], source)
            catalog.entries[name].add_quantity(SUPERNOVA.RA, row[3], source)
            catalog.entries[name].add_quantity(SUPERNOVA.DEC, row[4], source)
            catalog.entries[name].add_quantity(SUPERNOVA.HOST, 'SMC', source)
    catalog.journal_entries()

    # Galbany 04-18-16 donation
    folders = next(
        os.walk(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Galbany-04-18-16/')))[1]
    bibcode = '2016AJ....151...33G'
    for folder in folders:
        infofiles = glob(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Galbany-04-18-16/') + folder + '/*.info')
        photfiles = glob(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Galbany-04-18-16/') + folder + '/*.out*')

        zhel = ''
        zcmb = ''
        zerr = ''
        for path in infofiles:
            with open(path, 'r') as f:
                lines = f.read().splitlines()
                for line in lines:
                    splitline = line.split(':')
                    field = splitline[0].strip().lower()
                    value = splitline[1].strip()
                    if field == 'name':
                        name = value[:6].upper()
                        name += (value[6].upper()
                                 if len(value) == 7 else value[6:])
                        name = catalog.add_entry(name)
                        source = (catalog.entries[name]
                                  .add_source(bibcode=bibcode))
                        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS,
                                                           name, source)
                    elif field == 'type':
                        claimedtype = value.replace('SN', '')
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.CLAIMED_TYPE, claimedtype, source)
                    elif field == 'zhel':
                        zhel = value
                    elif field == 'redshift_error':
                        zerr = value
                    elif field == 'zcmb':
                        zcmb = value
                    elif field == 'ra':
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.RA,
                            value,
                            source,
                            u_value='floatdegrees')
                    elif field == 'dec':
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.DEC,
                            value,
                            source,
                            u_value='floatdegrees')
                    elif field == 'host':
                        value = value.replace('- ', '-').replace('G ', 'G')
                        catalog.entries[name].add_quantity(SUPERNOVA.HOST,
                                                           value, source)
                    elif field == 'e(b-v)_mw':
                        catalog.entries[name].add_quantity(SUPERNOVA.EBV,
                                                           value, source)

        catalog.entries[name].add_quantity(
            SUPERNOVA.REDSHIFT,
            zhel,
            source,
            e_value=zerr,
            kind='heliocentric')
        catalog.entries[name].add_quantity(
            SUPERNOVA.REDSHIFT, zcmb, source, e_value=zerr, kind='cmb')

        for path in photfiles:
            with open(path, 'r') as f:
                band = ''
                lines = f.read().splitlines()
                for li, line in enumerate(lines):
                    if li in [0, 2, 3]:
                        continue
                    if li == 1:
                        band = line.split(':')[-1].strip()
                    else:
                        cols = list(filter(None, line.split()))
                        if not cols:
                            continue
                        catalog.entries[name].add_photometry(
                            time=cols[0],
                            u_time='MJD',
                            magnitude=cols[1],
                            e_magnitude=cols[2],
                            band=band,
                            system=cols[3],
                            telescope=cols[4],
                            source=source)
    catalog.journal_entries()

    # Nicholl 05-03-16
    files = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nicholl-05-03-16', '*.txt'))
    name = catalog.add_entry('SN2015bn')
    for fi in pbar(files, task_str + ': Nicholl-05-03-16'):
        if 'late' in fi:
            bc = '2016ApJ...828L..18N'
        else:
            bc = '2016ApJ...826...39N'
        source = catalog.entries[name].add_source(bibcode=bc)
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, 'PS15ae', source)
        telescope = os.path.basename(fi).split('_')[1]
        with open(fi, 'r') as f:
            lines = f.read().splitlines()
            for li, line in enumerate(lines):
                if not line or (line[0] == '#' and li != 0):
                    continue
                cols = list(filter(None, line.split()))
                if not cols:
                    continue
                if li == 0:
                    bands = cols[1:]
                    continue

                mjd = cols[0]
                for ci, col in enumerate(cols[1::2]):
                    if not is_number(col) or np.isnan(float(col)):
                        continue

                    band = bands[ci]
                    band_set = ''
                    system = 'Vega'
                    if bands[ci] in ["u'", "g'", "r'", "i'", "z'"]:
                        band_set = 'SDSS'
                        system = 'SDSS'
                    elif telescope == 'ASASSN':
                        band_set = 'ASASSN'
                        system = 'Vega'
                    photodict = {
                        PHOTOMETRY.TIME: mjd,
                        PHOTOMETRY.U_TIME: 'MJD',
                        PHOTOMETRY.MAGNITUDE: col,
                        PHOTOMETRY.BAND: bands[ci],
                        PHOTOMETRY.SOURCE: source,
                        PHOTOMETRY.TELESCOPE: telescope,
                        PHOTOMETRY.SYSTEM: system
                    }
                    if band_set:
                        photodict[PHOTOMETRY.BAND_SET] = band_set
                    emag = cols[2 * ci + 2]
                    if is_number(emag):
                        photodict[PHOTOMETRY.E_MAGNITUDE] = emag
                    else:
                        photodict[PHOTOMETRY.UPPER_LIMIT] = True
                    if telescope == 'Swift':
                        photodict[PHOTOMETRY.INSTRUMENT] = 'UVOT'
                    catalog.entries[name].add_photometry(**photodict)

    catalog.journal_entries()
    return
Ejemplo n.º 8
0
def do_donated_photo(catalog):
    """Import donated photometry."""
    task_str = catalog.get_current_task_str()

    # Private donations here #
    if not catalog.args.travis:
        pass
    # End private donations #

    # Ponder 05-12-17 donation
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Ponder-05-12-17', 'meta.json'), 'r') as f:
        metadict = json.loads(f.read())
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Ponder-05-12-17', '*.dat'))
    for path in file_names:
        with open(path, 'r') as f:
            tsvin = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        oname = path.split('/')[-1].split('.')[0]
        name, source = catalog.new_entry(oname,
                                         bibcode=metadict[oname]['bibcode'])
        for row in pbar(tsvin, task_str + ': Ponder ' + oname):
            if row[0][0] == '#' or not is_number(row[-1]):
                continue
            mjd = row[1]
            bandinst = row[2].split('_')
            band = bandinst[0]
            inst = ''
            if len(bandinst) > 1:
                inst = bandinst[1]
            mag = row[3]
            uerr = row[4]
            lerr = row[5]
            photodict = {
                PHOTOMETRY.TIME: mjd,
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.BAND: band,
                PHOTOMETRY.MAGNITUDE: mag,
                PHOTOMETRY.E_LOWER_MAGNITUDE: lerr,
                PHOTOMETRY.E_UPPER_MAGNITUDE: uerr,
                PHOTOMETRY.SOURCE: source
            }
            if inst:
                photodict[PHOTOMETRY.INSTRUMENT] = inst
            catalog.entries[name].add_photometry(**photodict)

    # Benetti 03-08-17 donation
    path = os.path.join(catalog.get_current_task_repo(), 'Donations',
                        'Benetti-03-08-17', '1999E.dat')
    with open(path, 'r') as f:
        tsvin = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        name, source = catalog.new_entry('SN1999E',
                                         bibcode='2003MNRAS.340..191R')
        bands = None
        for row in tsvin:
            if not row or row[0][0] == '#':
                continue
            if not bands:
                bands = row[2:-2]
                continue
            mjd = row[1]
            tel = row[-1] if 'IAUC' not in row[-1] else None
            for bi, band in enumerate(bands):
                mag = row[2 + 2 * bi]
                if mag == '9999':
                    continue
                err = row[2 + 2 * bi + 1]
                limit = row[6] == 'True'
                photodict = {
                    PHOTOMETRY.TIME: mjd,
                    PHOTOMETRY.U_TIME: 'MJD',
                    PHOTOMETRY.TELESCOPE: tel,
                    PHOTOMETRY.BAND: band,
                    PHOTOMETRY.MAGNITUDE: mag,
                    PHOTOMETRY.SOURCE: source
                }
                if err != '.00':
                    photodict[PHOTOMETRY.E_MAGNITUDE] = str(Decimal(err))
                if tel:
                    photodict[PHOTOMETRY.TELESCOPE] = tel
                catalog.entries[name].add_photometry(**photodict)

    # Nicholl 01-29-17 donation
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Nicholl-01-29-17', 'meta.json'), 'r') as f:
        metadict = json.loads(f.read())
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nicholl-01-29-17', '*.txt'))
    for path in file_names:
        data = read(path, format='cds')
        oname = path.split('/')[-1].split('_')[0]
        name, source = catalog.new_entry(oname,
                                         bibcode=metadict[oname]['bibcode'])
        for row in pbar(data, task_str + ': Nicholl ' + oname):
            photodict = {
                PHOTOMETRY.TIME: str(row['MJD']),
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.MAGNITUDE: str(row['mag']),
                PHOTOMETRY.BAND: row['Filter'],
                PHOTOMETRY.SOURCE: source
            }
            if 'system' in metadict[oname]:
                photodict[PHOTOMETRY.SYSTEM] = metadict[oname]['system']
            if 'l_mag' in row.columns and row['l_mag'] == '>':
                photodict[PHOTOMETRY.UPPER_LIMIT] = True
            elif 'e_mag' in row.columns:
                photodict[PHOTOMETRY.E_MAGNITUDE] = str(row['e_mag'])
            if 'Telescope' in row.columns:
                photodict[PHOTOMETRY.TELESCOPE] = row['Telescope']
            catalog.entries[name].add_photometry(**photodict)

    # Arcavi 2016gkg donation
    path = os.path.join(catalog.get_current_task_repo(), 'Donations',
                        'Arcavi-01-24-17', 'SN2016gkg.txt')
    with open(path, 'r') as f:
        tsvin = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        name, source = catalog.new_entry('SN2016gkg',
                                         bibcode='2016arXiv161106451A')
        for row in tsvin:
            if row[0][0] == '#':
                continue
            mjd = str(jd_to_mjd(Decimal(row[0])))
            tel = row[1]
            band = row[3]
            mag = row[4]
            err = row[5]
            limit = row[6] == 'True'
            photodict = {
                PHOTOMETRY.TIME: mjd,
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.TELESCOPE: tel,
                PHOTOMETRY.BAND: band,
                PHOTOMETRY.MAGNITUDE: mag,
                PHOTOMETRY.SOURCE: source
            }
            if limit:
                photodict[PHOTOMETRY.UPPER_LIMIT] = True
            else:
                photodict[PHOTOMETRY.E_MAGNITUDE] = err
            catalog.entries[name].add_photometry(**photodict)

    # Nicholl Gaia16apd donation
    path = os.path.join(catalog.get_current_task_repo(), 'Donations',
                        'Nicholl-01-20-17', 'gaia16apd_phot.txt')

    data = read(path, format='cds')
    name, source = catalog.new_entry('Gaia16apd',
                                     bibcode='2017ApJ...835L...8N')
    for row in pbar(data, task_str + ': Nicholl Gaia16apd'):
        photodict = {
            PHOTOMETRY.TIME: str(row['MJD']),
            PHOTOMETRY.U_TIME: 'MJD',
            PHOTOMETRY.MAGNITUDE: str(row['mag']),
            PHOTOMETRY.BAND: row['Filter'],
            PHOTOMETRY.TELESCOPE: row['Telescope'],
            PHOTOMETRY.SOURCE: source
        }
        if row['l_mag'] == '>':
            photodict[PHOTOMETRY.UPPER_LIMIT] = True
        else:
            photodict[PHOTOMETRY.E_MAGNITUDE] = str(row['e_mag'])
        catalog.entries[name].add_photometry(**photodict)

    # Kuncarayakti-01-09-17
    datafile = os.path.join(catalog.get_current_task_repo(), 'Donations',
                            'Kuncarayakti-01-09-17', 'SN1978K.dat')
    inpname = os.path.basename(datafile).split('.')[0]
    with open(datafile, 'r') as f:
        tsvin = csv.reader(f, delimiter=' ', skipinitialspace=True)
        host = False
        for ri, row in enumerate(tsvin):
            if ri == 0:
                continue
            if row[0][0] == '#':
                rsplit = [x.strip('# ') for x in ' '.join(row).split(',')]
                bc = rsplit[0]
                tel, ins = '', ''
                if len(rsplit) > 1:
                    tel = rsplit[1]
                if len(rsplit) > 2:
                    ins = rsplit[2]
                continue
            (name, source) = catalog.new_entry(inpname, bibcode=bc)
            mag = row[4]
            err = row[5]
            mjd = str(astrotime('-'.join(row[:3]), format='iso').mjd)
            photodict = {
                PHOTOMETRY.BAND: row[3],
                PHOTOMETRY.TIME: mjd,
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.MAGNITUDE: mag.strip('>s'),
                PHOTOMETRY.SOURCE: source
            }
            if is_number(err):
                photodict[PHOTOMETRY.E_MAGNITUDE] = err
            if tel:
                photodict[PHOTOMETRY.TELESCOPE] = tel
            if ins:
                photodict[PHOTOMETRY.INSTRUMENT] = ins
            if '>' in mag:
                photodict[PHOTOMETRY.UPPER_LIMIT] = True
            if 's' in mag:
                photodict[PHOTOMETRY.SYNTHETIC] = True
            catalog.entries[name].add_photometry(**photodict)

    # Nugent 01-09-17 donation
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nugent-01-09-17', '*.dat'))
    for datafile in pbar_strings(file_names, task_str + ': Nugent-01-09-17'):
        inpname = os.path.basename(datafile).split('.')[0]
        (name, source) = catalog.new_entry(inpname,
                                           bibcode='2006ApJ...645..841N')
        with open(datafile, 'r') as f:
            tsvin = csv.reader(f, delimiter=' ', skipinitialspace=True)
            host = False
            for urow in tsvin:
                row = list(filter(None, urow))
                counts = row[2]
                e_counts = row[3]
                zp = row[4]
                photodict = {
                    PHOTOMETRY.BAND: row[1],
                    PHOTOMETRY.TIME: row[0],
                    PHOTOMETRY.U_TIME: 'MJD',
                    PHOTOMETRY.COUNT_RATE: counts,
                    PHOTOMETRY.E_COUNT_RATE: e_counts,
                    PHOTOMETRY.ZERO_POINT: zp,
                    PHOTOMETRY.TELESCOPE: 'CFHT',
                    PHOTOMETRY.SURVEY: 'SNLS',
                    PHOTOMETRY.SOURCE: source
                }
                set_pd_mag_from_counts(photodict, counts, ec=e_counts, zp=zp)
                catalog.entries[name].add_photometry(**photodict)

    # Inserra 09-04-16 donation
    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Inserra-09-04-16', '*.txt'))
    for datafile in pbar_strings(file_names, task_str + ': Inserra-09-04-16'):
        inpname = os.path.basename(datafile).split('.')[0]
        (name, source) = catalog.new_entry(inpname,
                                           bibcode='2013ApJ...770..128I')
        with open(datafile, 'r') as f:
            tsvin = csv.reader(f, delimiter=' ', skipinitialspace=True)
            host = False
            for row in tsvin:
                if row[0][0] == '#':
                    if row[0] == '#Host':
                        host = True
                        continue
                    host = False
                    bands = row[3:-1]
                    continue
                for bi, ba in enumerate(bands):
                    mag = row[5 + 2 * bi]
                    if not is_number(mag):
                        continue
                    system = 'AB'
                    if ba in ['U', 'B', 'V', 'R', 'I', 'J', 'H', 'K']:
                        system = 'Vega'
                    photodict = {
                        PHOTOMETRY.TIME: row[3],
                        PHOTOMETRY.U_TIME: 'MJD',
                        PHOTOMETRY.BAND: ba,
                        PHOTOMETRY.MAGNITUDE: mag.strip('< '),
                        PHOTOMETRY.SOURCE: source,
                        PHOTOMETRY.SYSTEM: system
                    }
                    if 'ATel' not in row[-1]:
                        photodict[PHOTOMETRY.TELESCOPE] = row[-1]
                    if host:
                        photodict[PHOTOMETRY.HOST] = True
                    if '<' in mag:
                        photodict[PHOTOMETRY.UPPER_LIMIT] = True
                    e_mag = row[5 + 2 * bi + 1].strip('() ')
                    if is_number(e_mag):
                        photodict[PHOTOMETRY.E_MAGNITUDE] = e_mag
                    catalog.entries[name].add_photometry(**photodict)

    # Nicholl 04-01-16 donation
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Nicholl-04-01-16', 'bibcodes.json'), 'r') as f:
        bcs = json.loads(f.read())

    kcorrected = ['SN2011ke', 'SN2011kf', 'SN2012il', 'PTF10hgi', 'PTF11rks']
    ignorephoto = ['PTF10hgi', 'PTF11rks', 'SN2011ke', 'SN2011kf', 'SN2012il']

    file_names = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nicholl-04-01-16/*.txt'))
    for datafile in pbar_strings(file_names, task_str + ': Nicholl-04-01-16'):
        inpname = os.path.basename(datafile).split('_')[0]
        isk = inpname in kcorrected
        name = catalog.add_entry(inpname)
        bibcode = ''
        for bc in bcs:
            if inpname in bcs[bc]:
                bibcode = bc
        if not bibcode:
            raise ValueError('Bibcode not found!')
        source = catalog.entries[name].add_source(bibcode=bibcode)
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, inpname, source)
        if inpname in ignorephoto:
            continue
        with open(datafile, 'r') as f:
            tsvin = csv.reader(f, delimiter='\t', skipinitialspace=True)
            rtelescope = ''
            for r, rrow in enumerate(tsvin):
                row = list(filter(None, rrow))
                if not row:
                    continue
                if row[0] == '#MJD':
                    bands = [x for x in row[1:] if x and 'err' not in x]
                elif row[0][0] == '#' and len(row[0]) > 1:
                    rtelescope = row[0][1:]
                if row[0][0] == '#':
                    continue
                mjd = row[0]
                if not is_number(mjd):
                    continue
                for v, val in enumerate(row[1::2]):
                    upperlimit = ''
                    mag = val.strip('>')
                    emag = row[2 * v + 2]
                    if '>' in val or (is_number(emag) and float(emag) == 0.0):
                        upperlimit = True
                    if (not is_number(mag) or isnan(float(mag))
                            or float(mag) > 90.0):
                        continue
                    band = bands[v]
                    instrument = ''
                    survey = ''
                    system = ''
                    telescope = rtelescope
                    if telescope == 'LSQ':
                        instrument = 'QUEST'
                    elif telescope == 'PS1':
                        instrument = 'GPC'
                    elif telescope == 'NTT':
                        instrument = 'EFOSC'
                    elif telescope == 'GROND':
                        instrument = 'GROND'
                        telescope = 'MPI/ESO 2.2m'
                    else:
                        if band == 'NUV':
                            instrument = 'GALEX'
                            telescope = 'GALEX'
                        elif band in ['u', 'g', 'r', 'i', 'z']:
                            if inpname.startswith('PS1'):
                                instrument = 'GPC'
                                telescope = 'PS1'
                                survey = 'Pan-STARRS'
                            elif inpname.startswith('PTF'):
                                telescope = 'P60'
                                survey = 'PTF'
                        elif band.upper() in ['UVW2', 'UVW1', 'UVM2']:
                            instrument = 'UVOT'
                            telescope = 'Swift'
                            if inpname in ['PTF12dam']:
                                system = 'AB'
                    if inpname in ['SCP-06F6']:
                        system = 'Vega'
                    photodict = {
                        PHOTOMETRY.TIME: mjd,
                        PHOTOMETRY.U_TIME: 'MJD',
                        PHOTOMETRY.BAND: band,
                        PHOTOMETRY.MAGNITUDE: mag,
                        PHOTOMETRY.UPPER_LIMIT: upperlimit,
                        PHOTOMETRY.SOURCE: source
                    }
                    if instrument:
                        photodict[PHOTOMETRY.INSTRUMENT] = instrument
                    if telescope:
                        photodict[PHOTOMETRY.TELESCOPE] = telescope
                    if survey:
                        photodict[PHOTOMETRY.SURVEY] = survey
                    if system:
                        photodict[PHOTOMETRY.SYSTEM] = system
                    if (is_number(emag) and not isnan(float(emag))
                            and float(emag) > 0.0):
                        photodict[PHOTOMETRY.E_MAGNITUDE] = emag
                    if isk:
                        photodict[PHOTOMETRY.KCORRECTED] = True
                    catalog.entries[name].add_photometry(**photodict)
    catalog.journal_entries()

    # Maggi 04-11-16 donation (MC SNRs)
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Maggi-04-11-16', 'LMCSNRs_OpenSNe.csv')) as f:
        tsvin = csv.reader(f, delimiter=',')
        for row in pbar(list(tsvin), task_str + ': Maggi-04-11-16/LMCSNRs'):
            name = 'MCSNR ' + row[0]
            name = catalog.add_entry(name)
            ra = row[2]
            dec = row[3]
            source = (catalog.entries[name].add_source(
                bibcode='2016A&A...585A.162M'))
            catalog.entries[name].add_quantity(
                SUPERNOVA.ALIAS,
                'LMCSNR J' + rep_chars(ra, ' :.') + rep_chars(dec, ' :.'),
                source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
            if row[1] != 'noname':
                catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, row[1],
                                                   source)
            catalog.entries[name].add_quantity(SUPERNOVA.RA, row[2], source)
            catalog.entries[name].add_quantity(SUPERNOVA.DEC, row[3], source)
            catalog.entries[name].add_quantity(SUPERNOVA.HOST, 'LMC', source)
            if row[4] == '1':
                catalog.entries[name].add_quantity(SUPERNOVA.CLAIMED_TYPE,
                                                   'Ia', source)
            elif row[4] == '2':
                catalog.entries[name].add_quantity(SUPERNOVA.CLAIMED_TYPE,
                                                   'CC', source)
    with open(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Maggi-04-11-16', 'SMCSNRs_OpenSNe.csv')) as f:
        tsvin = csv.reader(f, delimiter=',')
        for row in pbar(list(tsvin), task_str + ': Maggi-04-11-16/SMCSNRs'):
            name = 'MCSNR ' + row[0]
            name = catalog.add_entry(name)
            source = catalog.entries[name].add_source(name='Pierre Maggi')
            ra = row[3]
            dec = row[4]
            catalog.entries[name].add_quantity(
                SUPERNOVA.ALIAS, 'SMCSNR J' + ra.replace(':', '')[:6] +
                dec.replace(':', '')[:7], source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, row[1], source)
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, row[2], source)
            catalog.entries[name].add_quantity(SUPERNOVA.RA, row[3], source)
            catalog.entries[name].add_quantity(SUPERNOVA.DEC, row[4], source)
            catalog.entries[name].add_quantity(SUPERNOVA.HOST, 'SMC', source)
    catalog.journal_entries()

    # Galbany 04-18-16 donation
    folders = next(
        os.walk(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Galbany-04-18-16/')))[1]
    bibcode = '2016AJ....151...33G'
    for folder in folders:
        infofiles = glob(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Galbany-04-18-16/') + folder + '/*.info')
        photfiles = glob(
            os.path.join(catalog.get_current_task_repo(), 'Donations',
                         'Galbany-04-18-16/') + folder + '/*.out*')

        zhel = ''
        zcmb = ''
        zerr = ''
        for path in infofiles:
            with open(path, 'r') as f:
                lines = f.read().splitlines()
                for line in lines:
                    splitline = line.split(':')
                    field = splitline[0].strip().lower()
                    value = splitline[1].strip()
                    if field == 'name':
                        name = value[:6].upper()
                        name += (value[6].upper()
                                 if len(value) == 7 else value[6:])
                        name = catalog.add_entry(name)
                        source = (catalog.entries[name].add_source(
                            bibcode=bibcode))
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.ALIAS, name, source)
                    elif field == 'type':
                        claimedtype = value.replace('SN', '')
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.CLAIMED_TYPE, claimedtype, source)
                    elif field == 'zhel':
                        zhel = value
                    elif field == 'redshift_error':
                        zerr = value
                    elif field == 'zcmb':
                        zcmb = value
                    elif field == 'ra':
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.RA,
                            value,
                            source,
                            u_value='floatdegrees')
                    elif field == 'dec':
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.DEC,
                            value,
                            source,
                            u_value='floatdegrees')
                    elif field == 'host':
                        value = value.replace('- ', '-').replace('G ', 'G')
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.HOST, value, source)
                    elif field == 'e(b-v)_mw':
                        catalog.entries[name].add_quantity(
                            SUPERNOVA.EBV, value, source)

        catalog.entries[name].add_quantity(SUPERNOVA.REDSHIFT,
                                           zhel,
                                           source,
                                           e_value=zerr,
                                           kind='heliocentric')
        catalog.entries[name].add_quantity(SUPERNOVA.REDSHIFT,
                                           zcmb,
                                           source,
                                           e_value=zerr,
                                           kind='cmb')

        for path in photfiles:
            with open(path, 'r') as f:
                band = ''
                lines = f.read().splitlines()
                for li, line in enumerate(lines):
                    if li in [0, 2, 3]:
                        continue
                    if li == 1:
                        band = line.split(':')[-1].strip()
                    else:
                        cols = list(filter(None, line.split()))
                        if not cols:
                            continue
                        catalog.entries[name].add_photometry(
                            time=cols[0],
                            u_time='MJD',
                            magnitude=cols[1],
                            e_magnitude=cols[2],
                            band=band,
                            system=cols[3],
                            telescope=cols[4],
                            source=source)
    catalog.journal_entries()

    # Nicholl 05-03-16
    files = glob(
        os.path.join(catalog.get_current_task_repo(), 'Donations',
                     'Nicholl-05-03-16', '*.txt'))
    name = catalog.add_entry('SN2015bn')
    for fi in pbar(files, task_str + ': Nicholl-05-03-16'):
        if 'late' in fi:
            bc = '2016ApJ...828L..18N'
        else:
            bc = '2016ApJ...826...39N'
        source = catalog.entries[name].add_source(bibcode=bc)
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, name, source)
        catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, 'PS15ae', source)
        telescope = os.path.basename(fi).split('_')[1]
        with open(fi, 'r') as f:
            lines = f.read().splitlines()
            for li, line in enumerate(lines):
                if not line or (line[0] == '#' and li != 0):
                    continue
                cols = list(filter(None, line.split()))
                if not cols:
                    continue
                if li == 0:
                    bands = cols[1:]
                    continue

                mjd = cols[0]
                for ci, col in enumerate(cols[1::2]):
                    if not is_number(col) or np.isnan(float(col)):
                        continue

                    band = bands[ci]
                    band_set = ''
                    system = 'Vega'
                    if bands[ci] in ["u'", "g'", "r'", "i'", "z'"]:
                        band_set = 'SDSS'
                        system = 'SDSS'
                    elif telescope == 'ASASSN':
                        band_set = 'ASASSN'
                        system = 'Vega'
                    photodict = {
                        PHOTOMETRY.TIME: mjd,
                        PHOTOMETRY.U_TIME: 'MJD',
                        PHOTOMETRY.MAGNITUDE: col,
                        PHOTOMETRY.BAND: bands[ci],
                        PHOTOMETRY.SOURCE: source,
                        PHOTOMETRY.TELESCOPE: telescope,
                        PHOTOMETRY.SYSTEM: system
                    }
                    if band_set:
                        photodict[PHOTOMETRY.BAND_SET] = band_set
                    emag = cols[2 * ci + 2]
                    if is_number(emag):
                        photodict[PHOTOMETRY.E_MAGNITUDE] = emag
                    else:
                        photodict[PHOTOMETRY.UPPER_LIMIT] = True
                    if telescope == 'Swift':
                        photodict[PHOTOMETRY.INSTRUMENT] = 'UVOT'
                    catalog.entries[name].add_photometry(**photodict)

    catalog.journal_entries()
    return
Ejemplo n.º 9
0
def do_essence_photo(catalog):
    task_str = catalog.get_current_task_str()
    ess_path = os.path.join(catalog.get_current_task_repo(), 'ESSENCE',
                            'obj_table.dat')
    data = list(
        csv.reader(
            open(ess_path, 'r'),
            delimiter=' ',
            quotechar='"',
            skipinitialspace=True))
    for row in pbar(data[1:], task_str):
        etype = row[2]
        if etype.upper().replace('?', '') in catalog.nonsnetypes:
            continue
        ess_name = 'ESSENCE ' + row[0]
        name, source = catalog.new_entry(
            ess_name, bibcode='2016ApJS..224....3N')
        if row[1] != '---':
            catalog.entries[name].add_quantity(SUPERNOVA.ALIAS, 'SN' + row[1],
                                               source)
        if etype != '---':
            catalog.entries[name].add_quantity(SUPERNOVA.CLAIMED_TYPE, etype,
                                               source)
        catalog.entries[name].add_quantity(SUPERNOVA.RA, row[5], source)
        catalog.entries[name].add_quantity(SUPERNOVA.DEC, row[6], source)
        if is_number(row[11]):
            quantdict = {
                QUANTITY.VALUE: row[11],
                QUANTITY.SOURCE: source,
                QUANTITY.KIND: 'host'
            }
            if is_number(row[12]):
                quantdict[QUANTITY.E_VALUE] = row[12]
            catalog.entries[name].add_quantity(
                [SUPERNOVA.REDSHIFT, SUPERNOVA.HOST_REDSHIFT], **quantdict)

    files = glob(
        os.path.join(catalog.get_current_task_repo(), 'ESSENCE',
                     '*clean*.dat'))

    for pfile in pbar(files, task_str):
        name = 'ESSENCE ' + pfile.split('/')[-1].split('.')[0]
        name, source = catalog.new_entry(name, bibcode='2016ApJS..224....3N')
        with open(pfile, 'r') as f:
            rows = list(csv.reader(f, delimiter=' ', skipinitialspace=True))
        for ri, row in enumerate(rows):
            if ri == 1:
                catalog.entries[name].add_quantity(
                    SUPERNOVA.REDSHIFT,
                    row[5],
                    source,
                    kind=['spectroscopic', 'heliocentric'])
                catalog.entries[name].add_quantity(
                    SUPERNOVA.REDSHIFT,
                    row[6],
                    source,
                    kind=['spectroscopic', 'cmb'])
                continue
            if row[0].startswith('#'):
                continue
            counts = row[3][:6]
            lerr = row[4][:6]
            uerr = row[5][:6]
            zp = 25.0
            photodict = {
                PHOTOMETRY.TIME: row[1],
                PHOTOMETRY.U_TIME: 'MJD',
                PHOTOMETRY.BAND: row[2][0],
                PHOTOMETRY.COUNT_RATE: counts,
                PHOTOMETRY.E_LOWER_COUNT_RATE: lerr,
                PHOTOMETRY.E_UPPER_COUNT_RATE: uerr,
                PHOTOMETRY.ZERO_POINT: str(zp),
                PHOTOMETRY.SOURCE: source,
                PHOTOMETRY.TELESCOPE: 'CTIO 4m',
                PHOTOMETRY.SYSTEM: 'Natural'
            }
            set_pd_mag_from_counts(
                photodict, counts, ec='', lec=lerr, uec=uerr, zp=zp, sig=5.0)
            catalog.entries[name].add_photometry(**photodict)

    catalog.journal_entries()
    return