def test_seasonal(self): """ Test aggregation to a seasonal dataset """ # daily -> seasonal ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 5])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 5])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('1999-12-01', freq='QS-DEC', periods=5)}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, output_resolution='season', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # monthly -> seasonal ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) actual = temporal_aggregation(ds, output_resolution='season', monitor=m) self.assertTrue(actual.broadcast_equals(ex))
def test_seasonal(self): """ Test aggregation to a seasonal dataset """ # daily -> seasonal ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 5])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 5])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('1999-12-01', freq='QS-DEC', periods=5)}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, output_resolution='season', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # monthly -> seasonal ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) actual = temporal_aggregation(ds, output_resolution='season', monitor=m) self.assertTrue(actual.broadcast_equals(ex))
def test_validation(self): """ Test input validation """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90) }) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('normalize', str(err.exception)) ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=24) }) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('daily dataset', str(err.exception))
def test_custom(self): """ Test aggergating to custom temporal resolutions """ # daily -> 8 days ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='8D')}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, custom_resolution='8D', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # daily -> weekly ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 53])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 53])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='W')}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' actual = temporal_aggregation(ds, custom_resolution='W', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # monthly -> 4 month seasons ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 4])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 4])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='4M', periods=4)}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' actual = temporal_aggregation(ds, custom_resolution='4M', monitor=m) print(actual) self.assertTrue(actual.broadcast_equals(ex))
def test_custom(self): """ Test aggergating to custom temporal resolutions """ # daily -> 8 days ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='8D')}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, custom_resolution='8D', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # daily -> weekly ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 53])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 53])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='W')}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' actual = temporal_aggregation(ds, custom_resolution='W', monitor=m) self.assertTrue(actual.broadcast_equals(ex)) # monthly -> 4 month seasons ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 4])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 4])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='4M', periods=4)}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' actual = temporal_aggregation(ds, custom_resolution='4M', monitor=m) print(actual) self.assertTrue(actual.broadcast_equals(ex))
def test_validation(self): """ Test input validation """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90)}) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('normalize', str(err.exception)) # invalid custom resolution ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds, custom_resolution='SDFG') self.assertIn('Invalid custom resolution', str(err.exception)) # unexpected input resolution string ds.attrs['time_coverage_resolution'] = 'P2M1D' with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('Could not interpret', str(err.exception)) # output resolution finer than input ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds, custom_resolution='W') self.assertIn('output resolution is smaller', str(err.exception)) # output and input resolutions are the same with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('already at the requested', str(err.exception))
def test_validation(self): """ Test input validation """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90)}) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('normalize', str(err.exception)) # invalid custom resolution ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 366])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31')}) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds, custom_resolution='SDFG') self.assertIn('Invalid custom resolution', str(err.exception)) # unexpected input resolution string ds.attrs['time_coverage_resolution'] = 'P2M1D' with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('Could not interpret', str(err.exception)) # output resolution finer than input ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ds = adjust_temporal_attrs(ds) with self.assertRaises(ValueError) as err: temporal_aggregation(ds, custom_resolution='W') self.assertIn('output resolution is smaller', str(err.exception)) # output and input resolutions are the same with self.assertRaises(ValueError) as err: temporal_aggregation(ds) self.assertIn('already at the requested', str(err.exception))
def test_8days(self): """ Test nominal execution with a 8 Days frequency input dataset """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='8D') }) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12) }) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, monitor=m) self.assertTrue(actual.broadcast_equals(ex))
def test_8days(self): """ Test nominal execution with a 8 Days frequency input dataset """ ds = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 46])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', '2000-12-31', freq='8D')}) ds = adjust_temporal_attrs(ds) ex = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 12])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': pd.date_range('2000-01-01', freq='MS', periods=12)}) ex.first.attrs['cell_methods'] = 'time: mean within years' ex.second.attrs['cell_methods'] = 'time: mean within years' m = ConsoleMonitor() actual = temporal_aggregation(ds, monitor=m) self.assertTrue(actual.broadcast_equals(ex))