def test_get_dataset_indexes_for_single_cell(self): dataset = new_cube(width=360, height=180, drop_bounds=True) cell = dataset.isel(time=2, lat=20, lon=30) with self.assertRaises(ValueError) as cm: get_dataset_indexes(cell, "lon", np.array([-149.5])) self.assertEqual( "cannot determine cell boundaries for coordinate variable 'lon' of size 1", f"{cm.exception}")
def test_write_dataset(self): dataset = new_cube() try: write_dataset(dataset, TEST_NC_FILE_2) self.assertTrue(os.path.isfile(TEST_NC_FILE_2)) finally: if os.path.isfile(TEST_NC_FILE_2): os.remove(TEST_NC_FILE_2)
def _new_test_cube(self): return new_cube(width=2000, height=1000, lon_start=0.0, lat_start=50.0, spatial_res=4.0 / 2000, time_start="2010-01-01", time_periods=20, variables=dict(precipitation=0.6, temperature=276.2))
def test_verify_cube(self): cube = new_cube() self.assertEqual([], verify_cube(cube)) ds = cube.drop("time") self.assertEqual(["missing coordinate variable 'time'"], verify_cube(ds)) ds = ds.drop("lat") self.assertEqual([ "missing coordinate variable 'time'", "missing coordinate variable 'lat'" ], verify_cube(ds))
def test_assert_cube_illegal_coord_var(self): cube = new_cube(variables=dict(precipitation=0.5)) cube = cube.assign_coords( lat=xr.DataArray(np.outer(cube.lat, np.ones(cube.lon.size)), dims=("y", "x")), lon=xr.DataArray(np.outer(np.ones(cube.lat.size), cube.lon), dims=("y", "x"))) with self.assertRaises(ValueError) as cm: assert_cube(cube) self.assertEqual( "Dataset is not a valid data cube, because:\n" "- coordinate variable 'lat' must have a single dimension 'lat';\n" "- coordinate variable 'lon' must have a single dimension 'lon'.", f"{cm.exception}")
def test_assert_cube_illegal_data_var(self): cube = new_cube(variables=dict(precipitation=0.5)) shape = cube.dims["lat"], cube.dims["lon"] cube["chl"] = xr.DataArray(np.random.rand(*shape), dims=("lat", "lon"), coords=dict(lat=cube.lat, lon=cube.lon)) with self.assertRaises(ValueError) as cm: assert_cube(cube) self.assertEqual( "Dataset is not a valid data cube, because:\n" "- dimensions of data variable 'chl' must be" " ('time', ..., 'lat', 'lon'), but were ('lat', 'lon') for 'chl';\n" "- dimensions of all data variables must be same," " but found ('time', 'lat', 'lon') for 'precipitation'" " and ('lat', 'lon') for 'chl'.", f"{cm.exception}")
def test_assert_cube_illegal_coord_bounds_var(self): cube = new_cube(variables=dict(precipitation=0.5)) lat_bnds = np.zeros((cube.time.size, cube.lat.size, 2)) lon_bnds = np.zeros((cube.time.size, cube.lon.size, 2), dtype=np.float16) lat_bnds[:, :, :] = cube.lat_bnds lon_bnds[:, :, :] = cube.lon_bnds cube = cube.assign_coords(lat_bnds=xr.DataArray(lat_bnds, dims=("time", "lat", "bnds")), lon_bnds=xr.DataArray(lon_bnds, dims=("time", "lon", "bnds"))) with self.assertRaises(ValueError) as cm: assert_cube(cube) self.assertEqual( "Dataset is not a valid data cube, because:\n" "- bounds coordinate variable 'lat_bnds' must have dimensions ('lat', <bounds_dim>);\n" "- shape of bounds coordinate variable 'lat_bnds' must be (180, 2) but was (5, 180, 2);\n" "- bounds coordinate variable 'lon_bnds' must have dimensions ('lon', <bounds_dim>);\n" "- shape of bounds coordinate variable 'lon_bnds' must be (360, 2) but was (5, 360, 2);\n" "- type of bounds coordinate variable 'lon_bnds' must be dtype('float64')" " but was dtype('float16').", f"{cm.exception}")
def test_assert_cube_without_bounds(self): cube = new_cube(variables=dict(precipitation=0.5), drop_bounds=True) self.assertIs(cube, assert_cube(cube))
def test_assert_cube_ok(self): cube = new_cube(variables=dict(precipitation=0.5)) self.assertIs(cube, assert_cube(cube))
def setUp(self): super().setUp() self.dataset = new_cube( variables=dict(precipitation=0.2, temperature=279.1)) self.dataset.to_netcdf(TEST_NC_FILE, mode="w") self.dataset.close()
def test_get_dataset_indexes_without_bounds_inverse_lat(self): dataset = new_cube(width=360, height=180, inverse_lat=True, drop_bounds=True) self._assert_get_dataset_indexes_works(dataset, inverse_lat=True)
def test_get_dataset_indexes_with_bounds(self): dataset = new_cube(width=360, height=180, drop_bounds=False) self._assert_get_dataset_indexes_works(dataset)
def setUp(self): self._rm_outputs() dataset = new_cube(variables=dict(precipitation=0.4, temperature=275.2, soil_moisture=0.5)) dataset.to_netcdf(TEST_NC_FILE, mode="w") dataset.to_zarr(TEST_ZARR_DIR, mode="w")