def test_null_json_payload_to_observation_df(): observation_values_text = b""" { "_links": { "metadata": "" }, "observation_id": "OBSID", "values": [ { "quality_flag": 1, "timestamp": "2019-01-01T12:00:00-0700", "value": null }, { "quality_flag": 1, "timestamp": "2019-01-01T12:05:00-0700", "value": null } ] }""" ind = pd.DatetimeIndex([ pd.Timestamp("2019-01-01T19:00:00Z"), pd.Timestamp("2019-01-01T19:05:00Z") ], name='timestamp') observation_values = pd.DataFrame({ 'value': pd.Series([None, None], index=ind, dtype=float), 'quality_flag': pd.Series([1, 1], index=ind) }) out = utils.json_payload_to_observation_df( json.loads(observation_values_text)) pdt.assert_frame_equal(out, observation_values)
def get_observation_values(self, observation_id, start, end): """ Get observation values from start to end for observation_id from the API Parameters ---------- observation_id : string UUID of the observation object. start : timelike object Start time in interval to retrieve values for end : timelike object End time of the interval Returns ------- pandas.DataFrame With a datetime index and (value, quality_flag) columns """ req = self.get(f'/observations/{observation_id}/values', params={ 'start': start, 'end': end }) return json_payload_to_observation_df(req.json())
def timeseries_adapter(type_, metadata, json_value_response): metadata = deepcopy(metadata) # ignores any modeling parameters as they aren't need for this if 'site' in metadata: site = datamodel.Site.from_dict(metadata['site'], raise_on_extra=False) metadata['site'] = site elif 'aggregate' in metadata: # Patch aggregates so data model doesn't throw an error. We don't # need to load all of the observations when plotting forecasts. metadata['aggregate']['observations'] = [] if type_ == 'forecast': obj = datamodel.Forecast.from_dict(metadata) data = io_utils.json_payload_to_forecast_series(json_value_response) return timeseries.generate_forecast_figure(obj, data, return_components=True, limit=None) elif type_ == 'observation': obj = datamodel.Observation.from_dict(metadata) data = io_utils.json_payload_to_observation_df(json_value_response) return timeseries.generate_observation_figure(obj, data, return_components=True, limit=None) elif type_ == 'probabilistic_forecast': cvs = [] for constant_value in metadata['constant_values']: cv_dict = metadata.copy() cv_dict.update(constant_value) cvs.append( datamodel.ProbabilisticForecastConstantValue.from_dict( cv_dict)) metadata['constant_values'] = cvs obj = datamodel.ProbabilisticForecast.from_dict(metadata, raise_on_extra=False) return timeseries.generate_probabilistic_forecast_figure( obj, json_value_response) else: # remove observations, we aren't using them for plotting aggregates metadata['observations'] = [] obj = datamodel.Aggregate.from_dict(metadata, raise_on_extra=False) data = io_utils.json_payload_to_observation_df(json_value_response) return timeseries.generate_observation_figure(obj, data, return_components=True, limit=None)
def timeseries_adapter(type_, metadata, json_value_response): metadata = deepcopy(metadata) # ignores any modeling parameters as they aren't need for this site = datamodel.Site.from_dict(metadata['site'], raise_on_extra=False) metadata['site'] = site if type_ == 'forecast': obj = datamodel.Forecast.from_dict(metadata) data = io_utils.json_payload_to_forecast_series(json_value_response) return timeseries.generate_forecast_figure(obj, data, return_components=True) else: obj = datamodel.Observation.from_dict(metadata) data = io_utils.json_payload_to_observation_df(json_value_response) return timeseries.generate_observation_figure(obj, data, return_components=True)
def get_observation_values(self, observation_id, start, end, interval_label=None): """ Get observation values from start to end for observation_id from the API Parameters ---------- observation_id : string UUID of the observation object. start : timelike object Start time in interval to retrieve values for end : timelike object End time of the interval interval_label : str or None If beginning, ending, adjust the data to return only data that is valid between start and end. If None or instant, return any data between start and end inclusive of the endpoints. Returns ------- pandas.DataFrame With a datetime index and (value, quality_flag) columns Raises ------ ValueError If start or end cannot be converted into a Pandas Timestamp """ req = self.get(f'/observations/{observation_id}/values', params={ 'start': start, 'end': end }) out = json_payload_to_observation_df(req.json()) return adjust_timeseries_for_interval_label(out, interval_label, start, end)
def test_empty_payload_to_obsevation_df(): out = utils.json_payload_to_observation_df({'values': []}) assert set(out.columns) == {'value', 'quality_flag'} assert isinstance(out.index, pd.DatetimeIndex)
def test_json_payload_to_observation_df(observation_values, observation_values_text): out = utils.json_payload_to_observation_df( json.loads(observation_values_text)) pdt.assert_frame_equal(out, observation_values)