Skip to content

Commit 47de86f

Browse files
committed
Support simple SHOC datasets with no depth coordinate
This can happen after selecting a single layer, for example. Fixes #123
1 parent 87b8712 commit 47de86f

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

docs/releases/development.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ Next release (in development)
1111
* Drop dependency on importlib_metadata.
1212
This was only required to support Python 3.8, which was dropped in a previous release
1313
(:issue:`122`, :pr:`125`).
14+
* Fix an error with `ShocSimple.get_all_depth_names()`
15+
when the dataset had no depth coordinates
16+
(:issue:`123`, :pr:`126`).

src/emsarray/conventions/shoc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def get_depth_name(self) -> Hashable:
5757
return name
5858

5959
def get_all_depth_names(self) -> List[Hashable]:
60-
return ['z_centre', 'z_grid']
60+
return [
61+
name for name in ['z_centre', 'z_grid']
62+
if name in self.dataset.variables]
6163

6264
def get_time_name(self) -> Hashable:
6365
name = 't'
@@ -123,4 +125,8 @@ def get_depth_name(self) -> Hashable:
123125
return name
124126

125127
def get_all_depth_names(self) -> List[Hashable]:
126-
return [self.get_depth_name()]
128+
name = 'zc'
129+
if name in self.dataset.variables:
130+
return [name]
131+
else:
132+
return []

tests/conventions/test_cfgrid2d.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from emsarray.conventions import get_dataset_convention
2525
from emsarray.conventions.grid import CFGrid2DTopology, CFGridKind
2626
from emsarray.conventions.shoc import ShocSimple
27+
from emsarray.exceptions import NoSuchCoordinateError
2728
from emsarray.operations import geometry
2829
from tests.utils import (
2930
DiagonalShocGrid, ShocGridGenerator, ShocLayerGenerator,
@@ -175,6 +176,16 @@ def test_varnames():
175176
assert dataset.ems.get_time_name() == 'time'
176177

177178

179+
def test_no_depth_coordinate():
180+
dataset = make_dataset(j_size=10, i_size=10)
181+
dataset = dataset.isel({'k': -1}, drop=True)
182+
print(dataset)
183+
184+
assert dataset.ems.get_all_depth_names() == []
185+
with pytest.raises(NoSuchCoordinateError):
186+
dataset.ems.get_depth_name()
187+
188+
178189
@pytest.mark.parametrize(
179190
['name', 'attrs'],
180191
[

tests/conventions/test_shoc_standard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def make_dataset(
3232
corner_size: int = 0,
3333
) -> xarray.Dataset:
3434
"""
35-
Make a dummy SHOC simple dataset of a particular size.
35+
Make a dummy SHOC standard dataset of a particular size.
3636
It will have a sheared grid of points located near (0, 0),
3737
with increasing j moving north east, and increasing i moving south east.
3838
@@ -187,7 +187,7 @@ def make_dataset(
187187
def test_make_dataset():
188188
dataset = make_dataset(j_size=5, i_size=9, corner_size=2)
189189

190-
# Check that this is recognised as a ShocSimple dataset
190+
# Check that this is recognised as a ShocStandard dataset
191191
assert get_dataset_convention(dataset) is ShocStandard
192192

193193
# Check that the correct convention is used

0 commit comments

Comments
 (0)