<commit_before>
"""Functions defining cadence windows.  Return True if the field can
be observed at the supplied time."""

import numpy as np
from constants import *


def no_cadence(*args):
    """No cadence requirement--can be observed at any time."""
    return True


def time_since_obs(request_row, current_state):
    """

    parameters for the cadence window are given in the cadence_pars
    dictionary of the row:
        'ref_obs': ['last_observed', 'first_obs_tonight']
            which previous observation to use as reference time
        'prev_filter': ['same','other', 'any']
            which filter the previous observation should be in
        'window_start', 'window_stop'
            relative time bounds for the window after the ref_obs time
            e.g, window_start = 1.0*u.hour starts at ref_obs + 1 hour
        'filter_id'
            which filter to observe in
        'program_id'
            program requesting the observation
    One pair of :
        window_start : time delta (astropy quantity)
            time since ref observation after which observations can begin
        window_stop : time delta (astropy quantity)
            time since ref observation after which observations stop.
    or:
        window_center : time (astropy quantity)
            middle of acceptable window since ref observation
        window_half_width : time delta (astropy quantity)
            half width of acceptable window.

    prev_filter : {'same','any', 'other', [specific filterkey]}
        which filter to use to determine time of last observation
    """

    now = current_state['current_time'].mjd
    pars = request_row['cadence_pars']

    # which filter are we using to determine the time of last observation?
    # TODO: for now, require last observation to be from the same program
    if pars['prev_filter'] == 'any':
        ref_obses = []
        for filter_id in FILTER_IDS:
            ref_obses.append(request_row['{}_{}_{}'.format(
                pars['ref_obs'],
                request_row['program_id'], filter_id)])
        ref_obses = np.array(ref_obses)
        if np.sum(np.isfinite(ref_obses)):
            ref_obs = np.max(ref_obses[np.isfinite(ref_obses)])
        else:
            # no previous observations
            ref_obs = np.nan
    else:
        if pars['prev_filter'] == 'same':
            ref_obs_filter = request_row['filter_id']
        elif pars['prev_filter'] == 'other':
            assert(len(FILTER_IDS) == 2)
            raise NotImplementedError
        elif pars['prev_filter'] in FILTER_IDS:
            ref_obs_filter = pars['prev_filter']

        ref_obs = request_row['{}_{}_{}'.format(
            pars['ref_obs'],
            request_row['program_id'], request_row['filter_id'])]

    if np.isnan(ref_obs):
        # for first_obs_tonight scheduling, have to wait until first obs is
        # taken
        return False

    if pars.has_key('window_start') and pars.has_key('window_stop'):
        window_start_ut = ref_obs + pars['window_start']<commit_msg>bugfix ref_obs selection from filter_id<commit_after>
"""Functions defining cadence windows.  Return True if the field can
be observed at the supplied time."""

import numpy as np
from constants import *


def no_cadence(*args):
    """No cadence requirement--can be observed at any time."""
    return True


def time_since_obs(request_row, current_state):
    """

    parameters for the cadence window are given in the cadence_pars
    dictionary of the row:
        'ref_obs': ['last_observed', 'first_obs_tonight']
            which previous observation to use as reference time
        'prev_filter': ['same','other', 'any']
            which filter the previous observation should be in
        'window_start', 'window_stop'
            relative time bounds for the window after the ref_obs time
            e.g, window_start = 1.0*u.hour starts at ref_obs + 1 hour
        'filter_id'
            which filter to observe in
        'program_id'
            program requesting the observation
    One pair of :
        window_start : time delta (astropy quantity)
            time since ref observation after which observations can begin
        window_stop : time delta (astropy quantity)
            time since ref observation after which observations stop.
    or:
        window_center : time (astropy quantity)
            middle of acceptable window since ref observation
        window_half_width : time delta (astropy quantity)
            half width of acceptable window.

    prev_filter : {'same','any', 'other', [specific filterkey]}
        which filter to use to determine time of last observation
    """

    now = current_state['current_time'].mjd
    pars = request_row['cadence_pars']

    # which filter are we using to determine the time of last observation?
    # TODO: for now, require last observation to be from the same program
    if pars['prev_filter'] == 'any':
        ref_obses = []
        for filter_id in FILTER_IDS:
            ref_obses.append(request_row['{}_{}_{}'.format(
                pars['ref_obs'],
                request_row['program_id'], filter_id)])
        ref_obses = np.array(ref_obses)
        if np.sum(np.isfinite(ref_obses)):
            ref_obs = np.max(ref_obses[np.isfinite(ref_obses)])
        else:
            # no previous observations
            ref_obs = np.nan
    else:
        if pars['prev_filter'] == 'same':
            ref_obs_filter = request_row['filter_id']
        elif pars['prev_filter'] == 'other':
            assert(len(FILTER_IDS) == 2)
            raise NotImplementedError
        elif pars['prev_filter'] in FILTER_IDS:
            ref_obs_filter = pars['prev_filter']

        ref_obs = request_row['{}_{}_{}'.format(
            pars['ref_obs'],
            request_row['program_id'], ref_obs_filter)]

    if np.isnan(ref_obs):
        # for first_obs_tonight scheduling, have to wait until first obs is
        # taken
        return False

    if pars.has_key('window_start') and pars.has_key('window_stop'):
        window_start_ut = ref_obs + pars['window_start']
{'fraction_non_alphanumeric': 0.08965968586387435, 'fraction_numerical': 0.0013089005235602095, 'mean_word_length': 2.5355696934644305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3783009', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1640, 'n_words': 545}