Exemplo n.º 1
0
def get_rise_set_intervals(request_dict, site=''):
    intervals = []
    site = site if site else request_dict['location'].get('site', '')
    telescope_details = configdb.get_telescopes_with_instrument_type_and_location(
        request_dict['molecules'][0]['instrument_name'], site,
        request_dict['location'].get('observatory', ''),
        request_dict['location'].get('telescope', ''))
    if not telescope_details:
        return intervals

    intervals_by_site = get_rise_set_intervals_by_site(request_dict)
    intervalsets_by_telescope = intervals_by_site_to_intervalsets_by_telescope(
        intervals_by_site, telescope_details.keys())
    filtered_intervalsets_by_telescope = filter_out_downtime_from_intervalsets(
        intervalsets_by_telescope)
    filtered_intervalset = Intervals().union(
        filtered_intervalsets_by_telescope.values())
    filtered_intervals = filtered_intervalset.toTupleList()

    return filtered_intervals
def get_rise_set_intervals_by_site(request: dict,
                                   only_schedulable: bool = False) -> dict:
    """Get rise_set intervals by site for a request

    Computes the intervals only if they do not already exist in cache.

    Parameters:
        request: The request for which to get the intervals
    Returns:
        rise_set intervals by site
    """
    site_details = configdb.get_sites_with_instrument_type_and_location(
        instrument_type=request['configurations'][0]['instrument_type'],
        only_schedulable=only_schedulable)
    intervals_by_site = {}
    for site in site_details:
        intervals_by_site[site] = None
        if request.get('id'):
            cache_key = '{}.{}.rsi'.format(request['id'], site)
            intervals_by_site[site] = cache.get(cache_key, None)
        else:
            cache_key = 'rise_set_intervals_' + site + '_' + str(
                hashlib.sha1(
                    json.dumps(request, sort_keys=True,
                               cls=DjangoJSONEncoder).encode()).hexdigest())
            intervals_by_site[site] = caches['locmem'].get(cache_key, None)

        if intervals_by_site[site] is None:
            # There is no cached rise_set intervals for this request and site, so recalculate it now
            intervals_by_site[site] = []
            rise_set_site = get_rise_set_site(site_details[site])
            unique_targets_constraints = set([
                json.dumps(
                    (configuration['target'], configuration['constraints']))
                for configuration in request['configurations']
            ])

            for window in request['windows']:
                visibility = get_rise_set_visibility(rise_set_site,
                                                     window['start'],
                                                     window['end'],
                                                     site_details[site])
                target_intervals = Intervals()
                first_target = True
                for target_constraints in unique_targets_constraints:
                    (target, constraints) = json.loads(target_constraints)
                    rise_set_target = get_rise_set_target(target)
                    try:
                        rs_interval = visibility.get_observable_intervals(
                            rise_set_target,
                            airmass=constraints['max_airmass'],
                            moon_distance=Angle(
                                degrees=constraints['min_lunar_distance']))
                        # We only want times when all targets are visible to keep things simple
                        if first_target:
                            first_target = False
                            target_intervals = Intervals(rs_interval)
                        else:
                            target_intervals = Intervals(
                                rs_interval).intersect([target_intervals])
                    except MovingViolation:
                        pass
                intervals_by_site[site].extend(target_intervals.toTupleList())

            if request.get('id'):
                cache.set(cache_key, intervals_by_site[site],
                          86400 * 30)  # cache for 30 days
            else:
                caches['locmem'].set(cache_key, intervals_by_site[site],
                                     300)  # cache for 5 minutes
    return intervals_by_site