예제 #1
0
    def test_illegal_agg_methods(self):
        with self.assertRaises(ValueError) as cm:
            get_time_series(self.cube, POLYGON_GEOMETRY, agg_methods=['mean', 'median', 'stdev'])
        self.assertEqual('invalid aggregation method: stdev', f'{cm.exception}')

        with self.assertRaises(ValueError) as cm:
            get_time_series(self.cube, POLYGON_GEOMETRY, agg_methods=['median', 'stdev', 'avg'])
        self.assertEqual('invalid aggregation methods: avg, stdev', f'{cm.exception}')
예제 #2
0
def _get_time_series_for_geometry(dataset: xr.Dataset,
                                  var_name: str,
                                  geometry: shapely.geometry.base.BaseGeometry,
                                  agg_methods: Set[str],
                                  start_date: np.datetime64 = None,
                                  end_date: np.datetime64 = None,
                                  max_valids: int = None,
                                  incl_ancillary_vars=False) -> TimeSeries:
    if isinstance(geometry, shapely.geometry.Point):
        return _get_time_series_for_point(
            dataset,
            var_name,
            geometry,
            agg_methods,
            start_date=start_date,
            end_date=end_date,
            max_valids=max_valids,
            incl_ancillary_vars=incl_ancillary_vars)

    time_series_ds = timeseries.get_time_series(dataset,
                                                geometry, [var_name],
                                                agg_methods=agg_methods,
                                                start_date=start_date,
                                                end_date=end_date)
    if time_series_ds is None:
        return []

    var_names = {
        agg_method: f'{var_name}_{agg_method}'
        for agg_method in agg_methods
    }

    return _collect_timeseries_result(time_series_ds,
                                      var_names,
                                      max_valids=max_valids)
예제 #3
0
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)
예제 #4
0
 def test_polygon(self):
     ts_ds = get_time_series(
         self.cube,
         dict(type='Polygon',
              coordinates=[[[20.0, 10.0], [20.0, 20.0], [10.0, 20.0],
                            [10.0, 10.0], [20.0, 10.0]]]))
     self.assert_dataset_ok(ts_ds, 100, True, False, False, True, False,
                            False)
예제 #5
0
 def test_no_vars(self):
     ts_ds = get_time_series(self.cube,
                             dict(type='Polygon',
                                  coordinates=[[[20.0, 10.0], [20.0, 20.0],
                                                [10.0, 20.0], [10.0, 10.0],
                                                [20.0, 10.0]]]),
                             var_names=[])
     self.assertIsNone(ts_ds)
예제 #6
0
 def test_polygon_incl_count_stdev(self):
     ts_ds = get_time_series(self.cube,
                             dict(type='Polygon',
                                  coordinates=[[[20.0, 10.0], [20.0, 20.0],
                                                [10.0, 20.0], [10.0, 10.0],
                                                [20.0, 10.0]]]),
                             include_count=True,
                             include_stdev=True)
     self.assert_dataset_ok(ts_ds, 100, True, True, True, True, True, True)
예제 #7
0
 def test_polygon_incl_stdev_var_subs(self):
     ts_ds = get_time_series(self.cube,
                             dict(type='Polygon',
                                  coordinates=[[[20.0, 10.0], [20.0, 20.0],
                                                [10.0, 20.0], [10.0, 10.0],
                                                [20.0, 10.0]]]),
                             var_names=['B'],
                             include_stdev=True)
     self.assert_dataset_ok(ts_ds, 100, False, False, False, True, False,
                            True)
예제 #8
0
파일: timeseries.py 프로젝트: dcs4cop/xcube
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)
예제 #9
0
파일: ts_legacy.py 프로젝트: dcs4cop/xcube
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)
예제 #10
0
 def test_no_vars(self):
     ts_ds = get_time_series(self.cube,
                             POLYGON_GEOMETRY,
                             var_names=[])
     self.assertIsNone(ts_ds)
예제 #11
0
 def test_polygon(self):
     ts_ds = get_time_series(self.cube, POLYGON_GEOMETRY)
     self.assert_dataset_ok(ts_ds, 100, {'A_mean', 'B_mean'})
예제 #12
0
 def test_point(self):
     ts_ds = get_time_series(self.cube, POINT_GEOMETRY)
     self.assert_dataset_ok(ts_ds, 1, {'A', 'B'})
예제 #13
0
 def test_polygon_agg_median_mean_std_groupby(self):
     ts_ds = get_time_series(self.cube,
                             POLYGON_GEOMETRY,
                             agg_methods=['mean', 'std', 'median'],
                             use_groupby=True)
     self.assert_dataset_ok(ts_ds, 100, {'A_mean', 'A_median', 'A_std', 'B_mean', 'B_median', 'B_std'})
예제 #14
0
 def test_polygon_agg_mean_count(self):
     ts_ds = get_time_series(self.cube,
                             POLYGON_GEOMETRY,
                             agg_methods=['mean', 'count'])
     self.assert_dataset_ok(ts_ds, 100, {'A_mean', 'A_count', 'B_mean', 'B_count'})
예제 #15
0
 def test_point(self):
     ts_ds = get_time_series(self.cube,
                             dict(type='Point', coordinates=[20.0, 10.0]))
     self.assert_dataset_ok(ts_ds, 1, True, False, False, True, False,
                            False)
예제 #16
0
 def test_polygon_agg_mean_std_var_subs(self):
     ts_ds = get_time_series(self.cube,
                             POLYGON_GEOMETRY,
                             var_names=['B'],
                             agg_methods=['mean', 'std'])
     self.assert_dataset_ok(ts_ds, 100, {'B_mean', 'B_std'})
예제 #17
0
 def test_polygon_deprecated_incl_count_stdev(self):
     ts_ds = get_time_series(self.cube,
                             POLYGON_GEOMETRY,
                             include_count=True,
                             include_stdev=True)
     self.assert_dataset_ok(ts_ds, 100, {'A_mean', 'A_count', 'A_std', 'B_mean', 'B_count', 'B_std'})