|
3 | 3 | import xarray as xr |
4 | 4 |
|
5 | 5 | from tests import requires_dask |
6 | | -from tests.fixtures import generate_dataset |
| 6 | +from tests.fixtures import generate_dataset, generate_lev_dataset |
7 | 7 | from xcdat.spatial import SpatialAccessor |
8 | 8 |
|
9 | 9 |
|
@@ -45,6 +45,35 @@ def test_raises_error_if_data_var_not_in_dataset(self): |
45 | 45 | with pytest.raises(KeyError): |
46 | 46 | self.ds.spatial.average("not_a_data_var", axis=["Y", "incorrect_axis"]) |
47 | 47 |
|
| 48 | + def test_vertical_average_with_weights(self): |
| 49 | + # check that vertical averaging returns the correct answer |
| 50 | + # get dataset with vertical levels |
| 51 | + ds = generate_lev_dataset() |
| 52 | + # subset to one column for testing (and shake up data) |
| 53 | + ds = ds.isel(time=[0], lat=[0], lon=[0]).squeeze() |
| 54 | + so = ds["so"] |
| 55 | + so[:] = np.array([1, 2, 3, 4]) |
| 56 | + ds["so"] = so |
| 57 | + result = ds.spatial.average( |
| 58 | + "so", lev_bounds=(4000, 10000), axis=["Z"], keep_weights=True |
| 59 | + ) |
| 60 | + # specify expected result |
| 61 | + expected = xr.DataArray( |
| 62 | + data=np.array(1.8), coords={"time": ds.time, "lat": ds.lat, "lon": ds.lon} |
| 63 | + ) |
| 64 | + # compare |
| 65 | + xr.testing.assert_allclose(result["so"], expected) |
| 66 | + |
| 67 | + # check that vertical averaging returns the correct weights |
| 68 | + expected = xr.DataArray( |
| 69 | + data=np.array([2000, 2000, 1000, 0.0]), |
| 70 | + coords={"time": ds.time, "lev": ds.lev, "lat": ds.lat, "lon": ds.lon}, |
| 71 | + dims=["lev"], |
| 72 | + attrs={"xcdat_bounds": True}, |
| 73 | + ) |
| 74 | + |
| 75 | + xr.testing.assert_allclose(result["lev_wts"], expected) |
| 76 | + |
48 | 77 | def test_raises_error_if_axis_list_contains_unsupported_axis(self): |
49 | 78 | with pytest.raises(ValueError): |
50 | 79 | self.ds.spatial.average("ts", axis=["Y", "incorrect_axis"]) |
@@ -313,6 +342,23 @@ def test_raises_error_if_dataset_has_multiple_bounds_variables_for_an_axis(self) |
313 | 342 | with pytest.raises(TypeError): |
314 | 343 | ds.spatial.get_weights(axis=["Y", "X"]) |
315 | 344 |
|
| 345 | + def test_vertical_weighting(self): |
| 346 | + # get dataset with vertical coordinate |
| 347 | + ds = generate_lev_dataset() |
| 348 | + # call _get_vertical_weights |
| 349 | + result = ds.spatial._get_vertical_weights( |
| 350 | + domain_bounds=ds.lev_bnds, region_bounds=np.array([4000, 10000]) |
| 351 | + ) |
| 352 | + # specify expected result |
| 353 | + expected = xr.DataArray( |
| 354 | + data=np.array([2000, 2000, 1000, 0.0]), |
| 355 | + coords={"lev": ds.lev}, |
| 356 | + dims=["lev"], |
| 357 | + attrs={"units": "m", "positive": "down", "axis": "Z", "bounds": "lev_bnds"}, |
| 358 | + ) |
| 359 | + # compare |
| 360 | + xr.testing.assert_allclose(result, expected) |
| 361 | + |
316 | 362 | def test_data_var_weights_for_region_in_lat_and_lon_domains(self): |
317 | 363 | ds = self.ds.copy() |
318 | 364 |
|
|
0 commit comments