Skip to content

Commit 43b1d33

Browse files
Add more netcdf geography tests (#873)
1 parent b45261b commit 43b1d33

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

tests/netcdf/test_netcdf_geography.py

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import numpy as np
1313
import pytest
1414

15+
from earthkit.data import from_object
1516
from earthkit.data import from_source
1617
from earthkit.data.testing import earthkit_examples_file
1718
from earthkit.data.testing import earthkit_remote_examples_file
@@ -260,6 +261,221 @@ def test_netcdf_forecast_reference_time():
260261
assert ds[5].metadata("valid_datetime") == "2020-01-23T05:00:00"
261262

262263

264+
@pytest.mark.parametrize("lat_name,lon_name", [("lat", "lon"), ("latitude", "longitude")])
265+
def test_netcdf_geography_2d_1(lat_name, lon_name):
266+
# Dimensions: (level: 2, lat: 3, lon: 3)
267+
# Coordinates:
268+
# * level (level) int64 16B 700 500
269+
# * lat (lat) int64 24B 50 40 30
270+
# * lon (lon) int64 24B 0 10 20
271+
# Data variables:
272+
# a (level, lat, lon) int64 144B 11 12 13 21 22 23 ... 25 26 34 35 36
273+
274+
import xarray as xr
275+
276+
dims = {"level": 2, lat_name: 3, lon_name: 3}
277+
coords = {
278+
"level": np.array([700, 500]),
279+
lat_name: np.array([50, 40, 30]),
280+
lon_name: np.array([0, 10, 20]),
281+
}
282+
283+
lats = [[50, 50, 50], [40, 40, 40], [30, 30, 30]]
284+
lons = [[0, 10, 20], [0, 10, 20], [0, 10, 20]]
285+
286+
data = np.array(
287+
[
288+
[[11, 12, 13], [21, 22, 23], [31, 32, 33]],
289+
[[14, 15, 16], [24, 25, 26], [34, 35, 36]],
290+
]
291+
)
292+
293+
a = xr.Variable(dims, data)
294+
v = {"a": a}
295+
ds_in = xr.Dataset(v, coords=coords)
296+
297+
ds = from_object(ds_in)
298+
assert len(ds) == 2
299+
assert np.allclose(ds.metadata("level"), coords["level"])
300+
301+
for ll in [ds[0].to_latlon(), ds.to_latlon()]:
302+
assert ll["lat"].shape == (3, 3)
303+
assert ll["lon"].shape == (3, 3)
304+
assert np.allclose(ll["lat"], lats)
305+
assert np.allclose(ll["lon"], lons)
306+
307+
308+
@pytest.mark.parametrize("lat_name,lon_name", [("lat", "lon"), ("latitude", "longitude")])
309+
def test_netcdf_geography_2d_2(lat_name, lon_name):
310+
# Dimensions: (level: 2, y: 3, x: 2)
311+
# Coordinates:
312+
# * level (level) int64 16B 700 500
313+
# lat (y, x) int64 48B 50 50 40 40 30 30
314+
# lon (y, x) int64 48B 0 10 0 10 0 10
315+
# Dimensions without coordinates: y, x
316+
# Data variables:
317+
# a (level, y, x) int64 96B 11 12 21 22 31 32 14 15 24 25 34 35
318+
319+
import xarray as xr
320+
321+
dims = {"level": 2, "y": 3, "x": 2}
322+
coords = {
323+
"level": np.array([700, 500]),
324+
lat_name: (["y", "x"], np.array([[50, 50], [40, 40], [30, 30]])),
325+
lon_name: (["y", "x"], np.array([[0, 10], [0, 10], [0, 10]])),
326+
}
327+
328+
data = np.array(
329+
[
330+
[[11, 12], [21, 22], [31, 32]],
331+
[[14, 15], [24, 25], [34, 35]],
332+
]
333+
)
334+
335+
a = xr.Variable(dims, data)
336+
v = {"a": a}
337+
ds_in = xr.Dataset(v, coords=coords)
338+
339+
ds = from_object(ds_in)
340+
assert len(ds) == 2
341+
assert np.allclose(ds.metadata("level"), coords["level"])
342+
343+
for ll in [ds[0].to_latlon(), ds.to_latlon()]:
344+
assert ll["lat"].shape == (3, 2)
345+
assert ll["lon"].shape == (3, 2)
346+
assert np.allclose(ll["lat"], coords[lat_name][1])
347+
assert np.allclose(ll["lon"], coords[lon_name][1])
348+
349+
350+
@pytest.mark.skip(reason="To be seen if lat-lon as variables have to be supported")
351+
@pytest.mark.parametrize("lat_name,lon_name", [("lat", "lon"), ("latitude", "longitude")])
352+
def test_netcdf_geography_2d_3(lat_name, lon_name):
353+
# Dimensions: (level: 2, y: 3, x: 2)
354+
# Coordinates:
355+
# * level (level) int64 16B 700 500
356+
# Dimensions without coordinates: y, x
357+
# Data variables:
358+
# a (level, y, x) int64 96B 11 12 21 22 31 32 14 15 24 25 34 35
359+
# lat (y, x) int64 48B 50 50 40 40 30 30
360+
# lon (y, x) int64 48B 0 10 0 10 0 10
361+
362+
import xarray as xr
363+
364+
dims = {"level": 2, "y": 3, "x": 2}
365+
coords = {
366+
"level": np.array([700, 500]),
367+
}
368+
369+
data = np.array(
370+
[
371+
[[11, 12], [21, 22], [31, 32]],
372+
[[14, 15], [24, 25], [34, 35]],
373+
]
374+
)
375+
376+
a = xr.Variable(dims, data)
377+
lat = xr.Variable({"y": 3, "x": 3}, np.array([[50, 50], [40, 40], [30, 30]]))
378+
lon = xr.Variable({"y": 3, "x": 3}, np.array([[0, 10], [0, 10], [0, 10]]))
379+
v = {"a": a, lat_name: lat, lon_name: lon}
380+
ds_in = xr.Dataset(v, coords=coords)
381+
382+
ds = from_object(ds_in)
383+
assert len(ds) == 2
384+
assert np.allclose(ds.metadata("level"), coords["level"])
385+
386+
for ll in [ds[0].to_latlon(), ds.to_latlon()]:
387+
assert ll["lat"].shape == (3, 2)
388+
assert ll["lon"].shape == (3, 2)
389+
assert np.allclose(ll["lat"], lat.data)
390+
assert np.allclose(ll["lon"], lon.data)
391+
392+
393+
@pytest.mark.parametrize("lat_name,lon_name", [("lat", "lon"), ("latitude", "longitude")])
394+
def test_netcdf_geography_1d_1(lat_name, lon_name):
395+
# Dimensions: (level: 2, values: 9)
396+
# Coordinates:
397+
# * level (level) int64 16B 700 500
398+
# lat (values) int64 72B 50 50 50 40 40 40 30 30 30
399+
# lon (values) int64 72B 0 10 20 0 10 20 0 10 20
400+
# Dimensions without coordinates: values
401+
# Data variables:
402+
# a (level, values) int64 144B 11 12 13 21 22 23 ... 24 25 26 34 35 36
403+
404+
import xarray as xr
405+
406+
dims = {"level": 2, "values": 9}
407+
coords = {
408+
"level": np.array([700, 500]),
409+
lat_name: ("values", np.array([50, 50, 50, 40, 40, 40, 30, 30, 30])),
410+
lon_name: ("values", np.array([0, 10, 20, 0, 10, 20, 0, 10, 20])),
411+
}
412+
413+
data = np.array(
414+
[
415+
[11, 12, 13, 21, 22, 23, 31, 32, 33],
416+
[14, 15, 16, 24, 25, 26, 34, 35, 36],
417+
]
418+
)
419+
420+
a = xr.Variable(dims, data)
421+
v = {"a": a}
422+
ds_in = xr.Dataset(v, coords=coords)
423+
424+
ds = from_object(ds_in)
425+
assert len(ds) == 2
426+
assert np.allclose(ds.metadata("level"), coords["level"])
427+
428+
for ll in [ds[0].to_latlon(), ds.to_latlon()]:
429+
assert ll["lat"].shape == (9,)
430+
assert ll["lon"].shape == (9,)
431+
assert np.allclose(ll["lat"], coords[lat_name][1])
432+
assert np.allclose(ll["lon"], coords[lon_name][1])
433+
434+
435+
@pytest.mark.skip(reason="To be seen if lat-lon as variables have to be supported")
436+
@pytest.mark.parametrize("lat_name,lon_name", [("lat", "lon"), ("latitude", "longitude")])
437+
def test_netcdf_geography_1d_2(lat_name, lon_name):
438+
# Dimensions: (level: 2, values: 9)
439+
# Coordinates:
440+
# * level (level) int64 16B 700 500
441+
# Dimensions without coordinates: values
442+
# Data variables:
443+
# a (level, values) int64 144B 11 12 13 21 22 23 ... 24 25 26 34 35 36
444+
# lat (values) int64 72B 50 50 50 40 40 40 30 30 30
445+
# lon (values) int64 72B 0 10 20 0 10 20 0 10 20
446+
447+
import xarray as xr
448+
449+
dims = {"level": 2, "values": 9}
450+
coords = {
451+
"level": np.array([700, 500]),
452+
}
453+
454+
data = np.array(
455+
[
456+
[11, 12, 13, 21, 22, 23, 31, 32, 33],
457+
[14, 15, 16, 24, 25, 26, 34, 35, 36],
458+
]
459+
)
460+
461+
a = xr.Variable(dims, data)
462+
lat = xr.Variable({"values": 9}, np.array([50, 50, 50, 40, 40, 40, 30, 30, 30]))
463+
lon = xr.Variable({"values": 9}, np.array([0, 10, 20, 0, 10, 20, 0, 10, 20]))
464+
465+
v = {"a": a, lat_name: lat, lon_name: lon}
466+
ds_in = xr.Dataset(v, coords=coords)
467+
468+
ds = from_object(ds_in)
469+
assert len(ds) == 2
470+
assert np.allclose(ds.metadata("level"), coords["level"])
471+
472+
for ll in [ds[0].to_latlon(), ds.to_latlon()]:
473+
assert ll["lat"].shape == (9,)
474+
assert ll["lon"].shape == (9,)
475+
assert np.allclose(ll["lat"], lat.data)
476+
assert np.allclose(ll["lon"], lon.data)
477+
478+
263479
if __name__ == "__main__":
264480
from earthkit.data.testing import main
265481

0 commit comments

Comments
 (0)