def get_observation_counts(start_gps, end_gps, low_or_high, eor):
    low_high_clause, eor_clause = db_utils.get_lowhigh_and_eor_clauses(
        low_or_high, eor)

    response = db_utils.send_query(
        g.eor_db, '''SELECT starttime
                FROM mwa_setting
                WHERE starttime >= {} AND starttime <= {}
                AND projectid='G0009'
                {}
                {}
                ORDER BY starttime ASC'''.format(start_gps, end_gps,
                                                 low_high_clause,
                                                 eor_clause)).fetchall()

    GPS_LEAP_SECONDS_OFFSET, GPS_UTC_DELTA = db_utils.get_gps_utc_constants()

    observation_counts = []
    utc_obsid_map = []

    for observation in response:
        utc_millis = int(
            (observation[0] - GPS_LEAP_SECONDS_OFFSET + GPS_UTC_DELTA) * 1000)
        observation_counts.append([utc_millis, 1])
        utc_obsid_map.append([utc_millis, observation[0]])

    return (observation_counts, utc_obsid_map)
def get_error_counts(start_gps, end_gps):
    error_counts = []
    error_count = 0

    obscontroller_response = db_utils.send_query(
        g.eor_db, '''SELECT FLOOR(reference_time) AS reference_time
                            FROM obscontroller_log
                            WHERE reference_time >= {} AND reference_time <= {}
                            ORDER BY reference_time ASC'''.format(
            start_gps, end_gps)).fetchall()

    recvstatuspolice_response = db_utils.send_query(
        g.eor_db, '''SELECT FLOOR(reference_time) AS reference_time
                            FROM recvstatuspolice_log
                            WHERE reference_time >= {} AND reference_time <= {}
                            ORDER BY reference_time ASC'''.format(
            start_gps, end_gps)).fetchall()

    GPS_LEAP_SECONDS_OFFSET, GPS_UTC_DELTA = db_utils.get_gps_utc_constants()

    prev_time = 0

    for error in obscontroller_response:
        utc_millis = int(
            (error[0] - GPS_LEAP_SECONDS_OFFSET + GPS_UTC_DELTA) * 1000)
        if utc_millis == prev_time:
            error_counts[-1][1] += 1
        else:
            error_counts.append([utc_millis, 1])
            prev_time = utc_millis
        error_count += 1

    prev_time = 0

    for error in recvstatuspolice_response:
        utc_millis = int(
            (error[0] - GPS_LEAP_SECONDS_OFFSET + GPS_UTC_DELTA) * 1000)
        if utc_millis == prev_time:
            error_counts[-1][1] += 1
        else:
            error_counts.append([utc_millis, 1])
            prev_time = utc_millis
        error_count += 1

    error_counts.sort(key=lambda error: error[0])

    return (error_counts, error_count)
def get_plot_bands(the_set):
    flagged_subsets = models.FlaggedSubset.query.filter(
        models.FlaggedSubset.set_id == the_set.id).all()

    GPS_LEAP_SECONDS_OFFSET, GPS_UTC_DELTA = db_utils.get_gps_utc_constants()

    plot_bands = [{
        'from':
        int((flagged_subset.start - GPS_LEAP_SECONDS_OFFSET + GPS_UTC_DELTA) *
            1000),
        'to':
        int((flagged_subset.end - GPS_LEAP_SECONDS_OFFSET + GPS_UTC_DELTA) *
            1000),
        'color':
        'yellow'
    } for flagged_subset in flagged_subsets]

    return plot_bands
Beispiel #4
0
def save_new_set():
    if (g.user is not None and g.user.is_authenticated()):
        request_content = request.get_json()

        name = request_content['name']

        if name is None:
            return jsonify(error=True, message="Name cannot be empty.")

        name = name.strip()

        if len(name) == 0:
            return jsonify(error=True, message="Name cannot be empty.")

        if models.Set.query.filter(models.Set.name == name).count() > 0:
            return jsonify(error=True, message="Name must be unique.")

        flagged_range_dicts = []

        GPS_LEAP_SECONDS_OFFSET, GPS_UTC_DELTA = db_utils.get_gps_utc_constants()

        for flagged_range_dict in request_content['flaggedRanges']:
            flagged_gps_dict = {}
            flagged_gps_dict['flaggedRange'] = [pair[1] for pair in flagged_range_dict['flaggedRange']]
            flagged_gps_dict['start_gps'] = int(flagged_range_dict['start_millis'] / 1000) +\
                GPS_LEAP_SECONDS_OFFSET - GPS_UTC_DELTA
            flagged_gps_dict['end_gps'] = int(flagged_range_dict['end_millis'] / 1000) +\
                GPS_LEAP_SECONDS_OFFSET - GPS_UTC_DELTA
            flagged_range_dicts.append(flagged_gps_dict)

        start_gps = request_content['startObsId']
        end_gps = request_content['endObsId']
        low_or_high = request_content['lowOrHigh']
        eor = request_content['eor']

        total_data_hrs, flagged_data_hrs = get_data_hours_in_set(
            start_gps, end_gps, low_or_high, eor, flagged_range_dicts)

        insert_set_into_db(name, start_gps, end_gps, flagged_range_dicts,
            low_or_high, eor, total_data_hrs, flagged_data_hrs)

        return jsonify()
    else:
        return make_response("You need to be logged in to save a set.", 401)
def get_obs_err_histogram(start_gps, end_gps, start_time_str, end_time_str):
    response = db_utils.send_query(
        g.eor_db, '''SELECT starttime, stoptime, obsname, ra_phase_center
                    FROM mwa_setting
                    WHERE starttime >= {} AND starttime <= {}
                    AND projectid='G0009'
                    ORDER BY starttime ASC'''.format(start_gps,
                                                     end_gps)).fetchall()

    low_eor0_counts = []

    high_eor0_counts = []

    low_eor1_counts = []

    high_eor1_counts = []

    error_counts, error_count = get_error_counts(start_gps, end_gps)

    utc_obsid_map_l0 = []
    utc_obsid_map_l1 = []
    utc_obsid_map_h0 = []
    utc_obsid_map_h1 = []

    GPS_LEAP_SECONDS_OFFSET, GPS_UTC_DELTA = db_utils.get_gps_utc_constants()

    for observation in response:
        # Actual UTC time of the observation (for the graph)
        utc_millis = int(
            (observation[0] - GPS_LEAP_SECONDS_OFFSET + GPS_UTC_DELTA) * 1000)

        obs_name = observation[2]

        try:
            ra_phase_center = int(observation[3])
        except TypeError as te:
            ra_phase_center = -1

        if 'low' in obs_name:
            if ra_phase_center == 0:  # EOR0
                low_eor0_counts.append([utc_millis, 1])
                utc_obsid_map_l0.append([utc_millis, int(observation[0])])
            elif ra_phase_center == 60:  # EOR1
                low_eor1_counts.append([utc_millis, 1])
                utc_obsid_map_l1.append([utc_millis, int(observation[0])])
        elif 'high' in obs_name:
            if ra_phase_center == 0:  # EOR0
                high_eor0_counts.append([utc_millis, 1])
                utc_obsid_map_h0.append([utc_millis, int(observation[0])])
            elif ra_phase_center == 60:  # EOR1
                high_eor1_counts.append([utc_millis, 1])
                utc_obsid_map_h1.append([utc_millis, int(observation[0])])

    return render_template('histogram.html',
                           low_eor0_counts=low_eor0_counts,
                           high_eor0_counts=high_eor0_counts,
                           low_eor1_counts=low_eor1_counts,
                           high_eor1_counts=high_eor1_counts,
                           error_counts=error_counts,
                           utc_obsid_map_l0=utc_obsid_map_l0,
                           utc_obsid_map_l1=utc_obsid_map_l1,
                           utc_obsid_map_h0=utc_obsid_map_h0,
                           utc_obsid_map_h1=utc_obsid_map_h1,
                           range_start=start_time_str,
                           range_end=end_time_str,
                           start_time_str_short=start_time_str.replace(
                               'T', ' ')[0:16],
                           end_time_str_short=end_time_str.replace('T',
                                                                   ' ')[0:16])