Example #1
0
def main():
    # Change URL to point to a valid portal node.
    # If you are not interested in any subarray specific information
    # (e.g. schedule blocks), then the number can be omitted, as below.
    # Note: if on_update_callback is set to None, then we cannot use the
    #       KATPortalClient.connect() and subscribe() methods here.
    portal_client = KATPortalClient(
        'http://{host}/api/client'.format(**vars(args)),
        on_update_callback=None,
        logger=logger)

    # Get the names of sensors matching the patterns
    sensor_names = yield portal_client.sensor_names(args.sensors)
    print "\nMatching sensor names: {}".format(sensor_names)
    # Example output (if sensors is 'm01[12]_pos_request_base'):
    #   Matching sensor names: [u'm011_pos_request_base_azim',
    #   u'm012_pos_request_base_ra', u'm012_pos_request_base_dec',
    #   u'm011_pos_request_base_ra', u'm012_pos_request_base_elev',
    #   u'm011_pos_request_base_dec', u'm012_pos_request_base_azim',
    #   u'm011_pos_request_base_elev']

    # Fetch the details for the sensors found.
    for sensor_name in sensor_names:
        sensor_detail = yield portal_client.sensor_detail(sensor_name)
        print "\nDetail for sensor {}:".format(sensor_name)
        for key in sensor_detail:
            print "    {}: {}".format(key, sensor_detail[key])
        # Example output:
        #   Detail for sensor m011_pos_request_base_azim:
        #       name: m011_pos_request_base_azim
        #       systype: mkat
        #       component: m011
        #       site: deva
        #       katcp_name: m011.pos.request-base-azim
        #       params: [-195.0, 370.0]
        #       units: deg
        #       type: float
        #       description: Requested target azimuth

    num_sensors = len(sensor_names)
    if num_sensors == 0:
        print "\nNo matching sensors found - no history to request!"
    else:
        print("\nRequesting history for {} sensors, from {} to {}".format(
            num_sensors,
            datetime.utcfromtimestamp(
                args.start).strftime('%Y-%m-%dT%H:%M:%SZ'),
            datetime.utcfromtimestamp(
                args.end).strftime('%Y-%m-%dT%H:%M:%SZ')))
        if len(sensor_names) == 1:
            # Request history for just a single sensor - result is timestamp, value, status
            #    If value timestamp is also required, then add the additional argument: include_value_ts=True
            #    result is then timestmap, value_timestmap, value, status
            history = yield portal_client.sensor_history(
                sensor_names[0],
                args.start,
                args.end,
                timeout_sec=args.timeout)
            histories = {sensor_names[0]: history}
        else:
            # Request history for all the sensors - result is timestamp, value, status
            #    If value timestamp is also required, then add the additional argument: include_value_ts=True
            #    result is then timestmap, value_timestmap, value, status
            histories = yield portal_client.sensors_histories(
                sensor_names, args.start, args.end, timeout_sec=args.timeout)

        print "Found {} sensors.".format(len(histories))
        for sensor_name, history in histories.items():
            num_samples = len(history)
            print "History for: {} ({} samples)".format(
                sensor_name, num_samples)
            if num_samples > 0:
                print "\tindex,timestamp,value,status"
                for count in range(0, num_samples, args.decimate):
                    print "\t{},{}".format(count, history[count].csv())
Example #2
0
def _helper_create_from_sensors(starttime, stoptime, variables=None):
    """
    Create a list of weather objects from sensor data using tornado server.

    Parameters
    ----------
    starttime : astropy Time object
        Time to start getting history.
    stoptime : astropy Time object
        Time to stop getting history.
    variable : str
        Variable to get history for. Must be a key in weather_sensor_dict,
        defaults to all keys in weather_sensor_dict.

    Returns
    -------
    A list of WeatherData objects (only accessible via a yield call)

    """
    from katportalclient import KATPortalClient

    if not isinstance(starttime, Time):
        raise ValueError('starttime must be an astropy Time object')

    if not isinstance(stoptime, Time):
        raise ValueError('stoptime must be an astropy Time object')

    if variables is None:
        variables = weather_sensor_dict.keys()
    elif not isinstance(variables, (list, tuple)):
        variables = [variables]

    sensor_names = []
    sensor_var_dict = {}
    for var in variables:
        if var not in weather_sensor_dict.keys():
            raise ValueError('variable must be a key in weather_sensor_dict')
        sensor_names.append(weather_sensor_dict[var]['sensor_name'])
        sensor_var_dict[weather_sensor_dict[var]['sensor_name']] = var

    portal_client = KATPortalClient(katportal_url, on_update_callback=None)

    histories = yield portal_client.sensors_histories(sensor_names,
                                                      starttime.unix,
                                                      stoptime.unix,
                                                      timeout_sec=120)

    weather_obj_list = []
    for sensor_name, history in histories.items():
        variable = sensor_var_dict[sensor_name]
        sensor_times = [0.0]
        sensor_data = []
        for item in history:
            # status is usually nominal, but can indicate sensor errors.
            # Since we can't do anything about those and the data might be bad, ignore them
            if item.status != 'nominal':
                continue
            # skip it if nan is supplied
            if isnan(float(item.value)):
                continue

            # the value_time is the sensor timestamp, while the other is
            # when the recording system got it. The value_time isn't always
            # present, so test for it
            if 'value_time' in item._fields:
                timestamp = item.value_time
            else:
                timestamp = item.sample_time
            if timestamp > sensor_times[-1]:
                sensor_times.append(timestamp)
                sensor_data.append(float(item.value))
        del (sensor_times[0])
        if len(sensor_data):
            reduction = weather_sensor_dict[variable]['reduction']
            period = weather_sensor_dict[variable]['period']

            times_use, values_use = _reduce_time_vals(np.array(sensor_times),
                                                      np.array(sensor_data),
                                                      period,
                                                      strategy=reduction)
            if times_use is not None:
                for count, timestamp in enumerate(times_use.tolist()):
                    time_obj = Time(timestamp, format='unix')
                    weather_obj_list.append(
                        WeatherData.create(time_obj, variable,
                                           values_use[count]))

    raise tornado.gen.Return(weather_obj_list)