def test_invalid_dtype(self): # Test passing in a MJD dataset dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'second': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'lat': np.linspace(-89.5, 89.5, 180), 'lon': np.linspace(-179.5, 179.5, 360), 'time': [2451544.5, 2451575.5, 2451604.5, 2451635.5, 2451665.5, 2451696.5] }) with self.assertRaises(ValueError) as err: subset.subset_temporal(dataset, '2000-01-10, 2000-04-01') self.assertIn('type datetime', str(err.exception))
def anomaly_internal(ds: xr.Dataset, time_range: TimeRangeLike.TYPE = None, region: PolygonLike.TYPE = None, monitor: Monitor = Monitor.NONE) -> xr.Dataset: """ Calculate anomaly using as reference data the mean of an optional region and time slice from the given dataset. If no time slice/spatial region is given, the operation will calculate anomaly using the mean of the whole dataset as the reference. This is done for each data array in the dataset. :param ds: The dataset to calculate anomalies from :param time_range: Time range to use for reference data :param region: Spatial region to use for reference data :param monitor: a progress monitor. :return: The anomaly dataset """ ref = ds.copy() if time_range: time_range = TimeRangeLike.convert(time_range) ref = subset_temporal(ref, time_range) if region: region = PolygonLike.convert(region) ref = subset_spatial(ref, region) with monitor.observing("Calculating anomaly"): ref = ref.mean(keep_attrs=True, skipna=True) diff = ds - ref return diff
def test_invalid_dtype(self): # Test passing in a MJD dataset dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'second': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'lat': np.linspace(-89.5, 89.5, 180), 'lon': np.linspace(-179.5, 179.5, 360), 'time': [2451544.5, 2451575.5, 2451604.5, 2451635.5, 2451665.5, 2451696.5]}) with self.assertRaises(ValueError) as err: subset.subset_temporal(dataset, '2000-01-10, 2000-04-01') self.assertIn('type datetime', str(err.exception))
def anomaly_internal(ds: xr.Dataset, time_range: TimeRangeLike.TYPE = None, region: PolygonLike.TYPE = None, monitor: Monitor = Monitor.NONE) -> xr.Dataset: """ Calculate anomaly using as reference data the mean of an optional region and time slice from the given dataset. If no time slice/spatial region is given, the operation will calculate anomaly using the mean of the whole dataset as the reference. This is done for each data array in the dataset. :param ds: The dataset to calculate anomalies from :param time_range: Time range to use for reference data :param region: Spatial region to use for reference data :param monitor: a progress monitor. :return: The anomaly dataset """ ref = ds.copy() if time_range: time_range = TimeRangeLike.convert(time_range) ref = subset_temporal(ref, time_range) if region: region = PolygonLike.convert(region) ref = subset_spatial(ref, region) with monitor.observing("Calculating anomaly"): ref = ref.mean(keep_attrs=True, skipna=True) diff = ds - ref return diff
def test_subset_temporal(self): # Test general functionality dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'second': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'lat': np.linspace(-89.5, 89.5, 180), 'lon': np.linspace(-179.5, 179.5, 360), 'time': [datetime(2000, x, 1) for x in range(1, 7)]}) actual = subset.subset_temporal(dataset, '2000-01-10, 2000-04-01') expected = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([180, 360, 3])), 'second': (['lat', 'lon', 'time'], np.ones([180, 360, 3])), 'lat': np.linspace(-89.5, 89.5, 180), 'lon': np.linspace(-179.5, 179.5, 360), 'time': [datetime(2000, x, 1) for x in range(2, 5)]}) assert_dataset_equal(expected, actual)
def test_subset_temporal(self): # Test general functionality dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'second': (['lat', 'lon', 'time'], np.ones([180, 360, 6])), 'lat': np.linspace(-89.5, 89.5, 180), 'lon': np.linspace(-179.5, 179.5, 360), 'time': [datetime(2000, x, 1) for x in range(1, 7)] }) actual = subset.subset_temporal(dataset, '2000-01-10, 2000-04-01') expected = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([180, 360, 3])), 'second': (['lat', 'lon', 'time'], np.ones([180, 360, 3])), 'lat': np.linspace(-89.5, 89.5, 180), 'lon': np.linspace(-179.5, 179.5, 360), 'time': [datetime(2000, x, 1) for x in range(2, 5)] }) assert_dataset_equal(expected, actual)