def test_custom_region(self): """ Test execution with a generic WKT poygon """ dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': ([datetime(2001, x, 1) for x in range(1, 13)] + [datetime(2002, x, 1) for x in range(1, 13)]) }) lta = 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': [x for x in range(1, 13)] }) lta = 2 * lta expected_time = ([datetime(2001, x, 1) for x in range(3, 13)] + [datetime(2002, x, 1) for x in range(1, 11)]) expected = pd.DataFrame( data=(np.ones([20]) * -1), columns=[ 'ENSO Index over POLYGON ' '((-141.15234375 3.513421045640057, -129.0234375 6.839169626342807,' ' -102.65625 6.489983332670652, -90.703125 -3.688855143147035, -110' '.21484375 -13.06877673435769, -141.6796875 -6.31529853833002, -141' '.15234375 3.513421045640057))' ], index=expected_time) region = str( 'POLYGON((-141.15234375 3.513421045640057,-129.0234375' ' 6.839169626342807,-102.65625 6.4899833326706515,-90.703125 ' '-3.6888551431470353,-110.21484375 -13.068776734357693,' '-141.6796875 -6.31529853833002,-141.15234375 ' '3.513421045640057))') with create_tmp_file() as tmp_file: lta.to_netcdf(tmp_file) actual = index.enso(dataset, 'first', tmp_file, region='custom', custom_region=region) self.assertTrue(expected.equals(actual)) # Test a situation where the user forgets to provide the custom region with self.assertRaises(ValueError) as err: index.enso(dataset, 'first', 'dummy/file.nc', region='custom') self.assertIn('No region', str(err.exception))
def test_antimeridian(self): """ Test execution with N4 region that crosses the antimeridian """ dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': ([datetime(2001, x, 1) for x in range(1, 13)] + [datetime(2002, x, 1) for x in range(1, 13)]) }) lta = 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': [x for x in range(1, 13)] }) lta = 2 * lta expected_time = ([datetime(2001, x, 1) for x in range(3, 13)] + [datetime(2002, x, 1) for x in range(1, 11)]) expected = pd.DataFrame(data=(np.ones([20]) * -1), columns=['ENSO N4 Index'], index=expected_time) with create_tmp_file() as tmp_file: lta.to_netcdf(tmp_file) actual = index.enso(dataset, 'first', tmp_file, region='N4') self.assertTrue(expected.equals(actual))
def test_antimeridian(self): """ Test execution with N4 region that crosses the antimeridian """ dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': ([datetime(2001, x, 1) for x in range(1, 13)] + [datetime(2002, x, 1) for x in range(1, 13)])}) lta = 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': [x for x in range(1, 13)]}) lta = 2 * lta expected_time = ([datetime(2001, x, 1) for x in range(3, 13)] + [datetime(2002, x, 1) for x in range(1, 11)]) expected = pd.DataFrame(data=(np.ones([20]) * -1), columns=['ENSO N4 Index'], index=expected_time) with create_tmp_file() as tmp_file: lta.to_netcdf(tmp_file) actual = index.enso(dataset, 'first', tmp_file, region='N4') self.assertTrue(expected.equals(actual))
def test_custom_region(self): """ Test execution with a generic WKT poygon """ dataset = xr.Dataset({ 'first': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'second': (['lat', 'lon', 'time'], np.ones([45, 90, 24])), 'lat': np.linspace(-88, 88, 45), 'lon': np.linspace(-178, 178, 90), 'time': ([datetime(2001, x, 1) for x in range(1, 13)] + [datetime(2002, x, 1) for x in range(1, 13)])}) lta = 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': [x for x in range(1, 13)]}) lta = 2 * lta expected_time = ([datetime(2001, x, 1) for x in range(3, 13)] + [datetime(2002, x, 1) for x in range(1, 11)]) expected = pd.DataFrame(data=(np.ones([20]) * -1), columns=['ENSO Index over POLYGON ' '((-141.15234375 3.513421045640057, -129.0234375 6.839169626342807,' ' -102.65625 6.489983332670652, -90.703125 -3.688855143147035, -110' '.21484375 -13.06877673435769, -141.6796875 -6.31529853833002, -141' '.15234375 3.513421045640057))'], index=expected_time) region = str('POLYGON((-141.15234375 3.513421045640057,-129.0234375' ' 6.839169626342807,-102.65625 6.4899833326706515,-90.703125 ' '-3.6888551431470353,-110.21484375 -13.068776734357693,' '-141.6796875 -6.31529853833002,-141.15234375 ' '3.513421045640057))') with create_tmp_file() as tmp_file: lta.to_netcdf(tmp_file) actual = index.enso(dataset, 'first', tmp_file, region='custom', custom_region=region) self.assertTrue(expected.equals(actual)) # Test a situation where the user forgets to provide the custom region with self.assertRaises(ValueError) as err: index.enso(dataset, 'first', 'dummy/file.nc', region='custom') self.assertIn('No region', str(err.exception))