def test_strict_cf_convention(self): a = self._new_array(standard_name='a', ancillary_variables='a_stdev b c') a_stdev = self._new_array(standard_name='a standard_error') a_uncert = self._new_array(standard_name='a standard_error') a_count = self._new_array(standard_name='a number_of_observations') b = self._new_array(standard_name='b') b_stdev = self._new_array(standard_name='b standard_error') c = self._new_array(standard_name='c') ds = xr.Dataset(dict(a=a)) self.assertEqual({}, find_ancillary_var_names(ds, 'a')) ds = xr.Dataset( dict(a=a, a_stdev=a_stdev, a_uncert=a_uncert, a_count=a_count, b=b, b_stdev=b_stdev, c=c)) self.assertEqual({ '': {'c', 'b'}, 'standard_error': {'a_stdev'} }, find_ancillary_var_names(ds, 'a'))
def test_xcube_prefix_convention(self): a = self._new_array() a_std = self._new_array() a_count = self._new_array() b = self._new_array() b_std = self._new_array() ds = xr.Dataset(dict(a=a)) self.assertEqual({}, find_ancillary_var_names(ds, 'a')) ds = xr.Dataset(dict(a=a, a_std=a_std, a_count=a_count, b=b, b_std=b_std)) self.assertEqual({'number_of_observations': {'a_count'}, 'standard_error': {'a_std'}}, find_ancillary_var_names(ds, 'a'))
def _get_time_series_for_point(dataset: xr.Dataset, var_name: str, point: shapely.geometry.Point, start_date: np.datetime64 = None, end_date: np.datetime64 = None, max_valids: int = None) -> Dict: var_names = [var_name] ancillary_var_names = find_ancillary_var_names(dataset, var_name, same_shape=True, same_dims=True) uncert_var_name = None if 'standard_error' in ancillary_var_names: uncert_var_name = next(iter(ancillary_var_names['standard_error'])) var_names.append(uncert_var_name) ts_ds = timeseries.get_time_series(dataset, point, var_names, start_date=start_date, end_date=end_date) if ts_ds is None: return {'results': []} return _collect_ts_result(ts_ds, var_name, uncert_var_name=uncert_var_name, count_var_name=None, max_valids=max_valids)
def _get_time_series_for_point(dataset: xr.Dataset, var_name: str, point: shapely.geometry.Point, agg_methods: Set[str], start_date: np.datetime64 = None, end_date: np.datetime64 = None, max_valids: int = None, incl_ancillary_vars=False) -> TimeSeries: var_key = None if timeseries.AGG_MEAN in agg_methods: var_key = timeseries.AGG_MEAN elif timeseries.AGG_MEDIAN in agg_methods: var_key = timeseries.AGG_MEDIAN elif timeseries.AGG_MIN in agg_methods or timeseries.AGG_MAX in agg_methods: var_key = timeseries.AGG_MIN if not var_key: raise RuntimeError( 'Aggregation methods must include one of "mean", "median", "min", "max"' ) roles_to_anc_var_names = dict() if incl_ancillary_vars: roles_to_anc_var_name_sets = find_ancillary_var_names(dataset, var_name, same_shape=True, same_dims=True) for role, roles_to_anc_var_name_sets in roles_to_anc_var_name_sets.items( ): if role: roles_to_anc_var_names[role] = roles_to_anc_var_name_sets.pop() var_names = [var_name] + list(set(roles_to_anc_var_names.values())) time_series_ds = timeseries.get_time_series(dataset, point, var_names, start_date=start_date, end_date=end_date, cube_asserted=True) if time_series_ds is None: return [] key_to_var_names = {var_key: var_name} for role, anc_var_name in roles_to_anc_var_names.items(): key_to_var_names[role] = anc_var_name return _collect_timeseries_result(time_series_ds, key_to_var_names, max_valids=max_valids)
def _get_time_series_for_geometry(dataset: xr.Dataset, var_name: str, geometry: shapely.geometry.base.BaseGeometry, start_date: np.datetime64 = None, end_date: np.datetime64 = None, include_count=True, include_stdev=False, max_valids: int = None) -> Dict: if isinstance(geometry, shapely.geometry.Point): return _get_time_series_for_point(dataset, var_name, geometry, start_date=start_date, end_date=end_date) agg_methods = set() agg_methods.add('mean') if include_stdev: agg_methods.add('std') if include_count: agg_methods.add('count') ts_ds = timeseries.get_time_series(dataset, geometry, [var_name], agg_methods=agg_methods, start_date=start_date, end_date=end_date, cube_asserted=True) if ts_ds is None: return {'results': []} ancillary_var_names = find_ancillary_var_names(ts_ds, var_name) uncert_var_name = None if 'standard_error' in ancillary_var_names: uncert_var_name = next(iter(ancillary_var_names['standard_error'])) count_var_name = None if 'number_of_observations' in ancillary_var_names: count_var_name = next( iter(ancillary_var_names['number_of_observations'])) return _collect_ts_result(ts_ds, var_name, uncert_var_name=uncert_var_name, count_var_name=count_var_name, max_valids=max_valids)