|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import xarray as xr |
| 4 | + |
| 5 | +from uxarray.io.utils import _parse_grid_type |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize( |
| 9 | + ("path_args", "expected_spec"), |
| 10 | + [ |
| 11 | + (("exodus", "outCSne8", "outCSne8.g"), "Exodus"), |
| 12 | + (("scrip", "outCSne8", "outCSne8.nc"), "Scrip"), |
| 13 | + (("ugrid", "outCSne30", "outCSne30.ug"), "UGRID"), |
| 14 | + (("mpas", "QU", "mesh.QU.1920km.151026.nc"), "MPAS"), |
| 15 | + (("esmf", "ne30", "ne30pg3.grid.nc"), "ESMF"), |
| 16 | + (("geos-cs", "c12", "test-c12.native.nc4"), "GEOS-CS"), |
| 17 | + (("icon", "R02B04", "icon_grid_0010_R02B04_G.nc"), "ICON"), |
| 18 | + (("fesom", "soufflet-netcdf", "grid.nc"), "FESOM2"), |
| 19 | + ], |
| 20 | +) |
| 21 | +def test_parse_grid_type_detects_supported_formats(gridpath, path_args, expected_spec): |
| 22 | + with xr.open_dataset(gridpath(*path_args)) as ds: |
| 23 | + source_grid_spec, lon_name, lat_name = _parse_grid_type(ds) |
| 24 | + |
| 25 | + assert source_grid_spec == expected_spec |
| 26 | + assert lon_name is None |
| 27 | + assert lat_name is None |
| 28 | + |
| 29 | + |
| 30 | +def test_parse_grid_type_detects_structured_grid(): |
| 31 | + lon = xr.DataArray( |
| 32 | + np.array([0.0, 1.0, 2.0]), |
| 33 | + dims=["lon"], |
| 34 | + attrs={"standard_name": "longitude"}, |
| 35 | + ) |
| 36 | + lat = xr.DataArray( |
| 37 | + np.array([-1.0, 0.0, 1.0]), |
| 38 | + dims=["lat"], |
| 39 | + attrs={"standard_name": "latitude"}, |
| 40 | + ) |
| 41 | + ds = xr.Dataset(coords={"lon": lon, "lat": lat}) |
| 42 | + |
| 43 | + source_grid_spec, lon_name, lat_name = _parse_grid_type(ds) |
| 44 | + |
| 45 | + assert source_grid_spec == "Structured" |
| 46 | + assert lon_name == "lon" |
| 47 | + assert lat_name == "lat" |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "dataset", |
| 52 | + [ |
| 53 | + xr.Dataset({"grid_center_lon": xr.DataArray([0.0], dims=["grid_size"])}), |
| 54 | + xr.Dataset( |
| 55 | + { |
| 56 | + "coordx": xr.DataArray([0.0, 1.0], dims=["num_nodes"]), |
| 57 | + "coordy": xr.DataArray([0.0, 1.0], dims=["num_nodes"]), |
| 58 | + } |
| 59 | + ), |
| 60 | + xr.Dataset({"verticesOnCell": xr.DataArray([[1, 2, 3]], dims=["nCells", "nVert"])}), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_parse_grid_type_rejects_incomplete_format_signals(dataset): |
| 64 | + with pytest.raises(RuntimeError, match="Could not recognize dataset format"): |
| 65 | + _parse_grid_type(dataset) |
0 commit comments