Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 224 additions & 96 deletions tests/test_axis.py

Large diffs are not rendered by default.

38 changes: 1 addition & 37 deletions tests/test_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
lat_bnds,
lon_bnds,
)
from xcdat.bounds import BoundsAccessor, _get_bounds_dim
from xcdat.bounds import BoundsAccessor

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1059,39 +1059,3 @@ def test_add_monthly_bounds_for_end_of_month_set_to_true(self):
)

assert result.identical(expected)


class TestGetBoundsDim:
def test_returns_bounds_dim_for_standard_case(self):
coords = xr.DataArray([0, 1, 2], dims=["lat"])
bounds = xr.DataArray([[0, 1], [1, 2], [2, 3]], dims=["lat", "bnds"])

result = _get_bounds_dim(coords, bounds)

assert result == "bnds"

def test_returns_bounds_dim_when_bounds_dim_has_custom_name(self):
coords = xr.DataArray([10, 20, 30], dims=["lon"])
bounds = xr.DataArray([[5, 15], [15, 25], [25, 35]], dims=["lon", "boundaries"])

result = _get_bounds_dim(coords, bounds)

assert result == "boundaries"

def test_raises_error_when_bounds_has_no_extra_dim(self):
coords = xr.DataArray([0, 1, 2], dims=["lat"])
bounds = xr.DataArray([0, 1, 2], dims=["lat"])

with pytest.raises(
ValueError, match="No extra dimension found in bounds variable"
):
_get_bounds_dim(coords, bounds)

def test_raises_error_when_bounds_has_multiple_extra_dims(self):
coords = xr.DataArray([0, 1, 2], dims=["lat"])
bounds = xr.DataArray(np.zeros((3, 2, 2)), dims=["lat", "bnds", "extra"])

with pytest.raises(
ValueError, match="Bounds variable must have exactly one more dimension"
):
_get_bounds_dim(coords, bounds)
48 changes: 30 additions & 18 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,29 +1614,35 @@ def test_adds_missing_lat_and_lon_and_time_bounds(self):
assert "lon_bnds" in result_data_vars
assert "time_bnds" in result_data_vars

def test_orients_longitude_bounds_from_180_to_360_and_sorts_with_prime_meridian_cell(
def test_swaps_single_dim_from_180_to_360_and_normalizes_prime_meridian_cell_in_lon_bnds_to_360(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test update for open_dataset for changes to swap_lon_axis.

self,
):
# Chunk the input dataset to test method also works with Dask.
ds = xr.Dataset(
ds_180 = xr.Dataset(
coords={
"lon": xr.DataArray(
name="lon",
data=np.array([-180, -1, 0, 1, 179]),
data=np.array([-179.5, -1.5, -0.5, 1.5, 179.5]),
dims=["lon"],
attrs={"units": "degrees_east", "axis": "X", "bounds": "lon_bnds"},
)
),
},
data_vars={
"ts": xr.DataArray(
name="ts",
data=np.array([0, 1, 2, 3, 4]),
dims=["lon"],
attrs={"test_attr": "test"},
),
"lon_bnds": xr.DataArray(
name="lon_bnds",
data=np.array(
[
[-180.5, -1.5],
[-1.5, -0.5],
[-0.5, 0.5],
[0.5, 1.5],
[1.5, 179.5],
[-180.0, -179],
[-2.0, -1.0],
[-1.0, 0.0], # Prime meridian cell.
[1.0, 2.0],
[179.0, 180.0],
]
),
dims=["lon", "bnds"],
Expand All @@ -1645,27 +1651,33 @@ def test_orients_longitude_bounds_from_180_to_360_and_sorts_with_prime_meridian_
},
).chunk({"lon": 2})

result = _postprocess_dataset(ds, lon_orient=(0, 360))
result = _postprocess_dataset(ds_180, lon_orient=(0, 360))
expected = xr.Dataset(
coords={
"lon": xr.DataArray(
name="lon",
data=np.array([0.0, 1.0, 179.0, 180.0, 359.0, 360.0]),
data=np.array([1.5, 179.5, 180.5, 358.5, 359.5]),
dims=["lon"],
attrs={"units": "degrees_east", "axis": "X", "bounds": "lon_bnds"},
)
),
},
data_vars={
"ts": xr.DataArray(
name="ts",
data=np.array([3, 4, 0, 1, 2]),
dims=["lon"],
attrs={"test_attr": "test"},
),
"lon_bnds": xr.DataArray(
name="lon_bnds",
data=np.array(
[
[0, 0.5],
[0.5, 1.5],
[1.5, 179.5],
[179.5, 358.5],
[358.5, 359.5],
[359.5, 360],
[1.0, 2.0],
[179.0, 180.0],
[180.0, 181.0],
[358.0, 359.0],
# Instead of [359, 0], normalize to [359, 360].
[359.0, 360.0],
]
),
dims=["lon", "bnds"],
Expand Down
18 changes: 18 additions & 0 deletions tests/test_spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,24 @@ def test_dataset_weights_for_region_in_lon_domain_with_region_spanning_p_meridia

xr.testing.assert_allclose(result, expected)

def test_dataset_weights_raises_error_when_more_than_one_grid_cell_spans_prime_meridian(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test specifically for spatial average prime meridian cell logic.

self,
):
ds = self.ds.copy(deep=True)

# Modify the dataset to have more than one grid cell spanning the prime meridian.
lon_bnds = ds.lon_bnds.copy()
lon_bnds[0, :] = [359, 1] # First grid cell spans the prime meridian.
lon_bnds[1, :] = [358, 0] # Second grid cell also spans the prime meridian.
ds["lon_bnds"] = lon_bnds

with pytest.raises(
ValueError, match="More than one grid cell spans the prime meridian"
):
ds.spatial._get_longitude_weights(
domain_bounds=ds.lon_bnds, region_bounds=np.array([359, 1])
)

def test_dataset_weights_all_longitudes_for_equal_region_bounds(self):
expected = xr.DataArray(
data=np.array(
Expand Down
Loading
Loading