Skip to content

Commit 0acb855

Browse files
committed
add tests for the cf convention decoder
1 parent 60dc4e8 commit 0acb855

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

xdggs/tests/conventions/test_decoders.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,75 @@ def test_xdggs(obj_type, coord_name, coord, grid_info, name):
107107
)
108108
actual = decoders.xdggs(obj, grid_info, name, index_options={})
109109
xr.testing.assert_identical(actual.to_dataset(), expected.to_dataset())
110+
111+
112+
@pytest.mark.parametrize("obj_type", ["DataArray", "Dataset"])
113+
@pytest.mark.parametrize(
114+
["coord_name", "dim", "metadata", "grid_info", "name"],
115+
(
116+
pytest.param(
117+
"cell_ids",
118+
"cells",
119+
{
120+
"grid_mapping_name": "healpix",
121+
"refinement_level": 1,
122+
"indexing_scheme": "nested",
123+
},
124+
None,
125+
None,
126+
id="healpix-all_defaults",
127+
),
128+
pytest.param(
129+
"cell_ids",
130+
"cells",
131+
{"grid_mapping_name": "h3", "refinement_level": 2},
132+
None,
133+
None,
134+
id="h3-all_defaults",
135+
),
136+
pytest.param(
137+
"zone_ids",
138+
"zones",
139+
{
140+
"grid_mapping_name": "healpix",
141+
"refinement_level": 1,
142+
"indexing_scheme": "nested",
143+
},
144+
None,
145+
"zone_ids",
146+
id="healpix-override_name",
147+
),
148+
pytest.param(
149+
"cell_ids",
150+
"cells",
151+
{"grid_mapping_name": "h3", "refinement_level": 2},
152+
{"grid_name": "h3", "level": 2},
153+
None,
154+
id="h3-override_grid_info",
155+
),
156+
),
157+
)
158+
def test_cf(obj_type, coord_name, dim, metadata, grid_info, name):
159+
cell_ids = generate_cell_ids("healpix", level=metadata["refinement_level"])
160+
coord = xr.Variable(
161+
dim,
162+
cell_ids,
163+
{"standard_name": f"{metadata['grid_mapping_name']}_index", "units": 1},
164+
)
165+
crs = xr.Variable((), np.array(0, dtype="uint8"), metadata)
166+
167+
if obj_type == "Dataset":
168+
obj = xr.Dataset(coords={coord_name: coord, "crs": crs})
169+
else:
170+
obj = xr.DataArray(
171+
np.zeros_like(coord.data),
172+
coords={coord_name: coord, "crs": crs},
173+
dims=coord.dims,
174+
)
175+
176+
expected = xr.Coordinates(
177+
{coord_name: coord},
178+
indexes={coord_name: create_index(coord_name, coord, grid_info=metadata)},
179+
)
180+
actual = decoders.cf(obj, grid_info, name, index_options={})
181+
xr.testing.assert_identical(actual.to_dataset(), expected.to_dataset())

0 commit comments

Comments
 (0)