Skip to content

Commit 85f025b

Browse files
Adding unit test for spatial hash on 1/4 degree NEMO curvilinear grid
The unit test currently fails, because the spatial hash index returned is the cell on the anti-meridian (between 179.875 and 179.875)
1 parent 161ad9c commit 85f025b

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

tests/v4/test_index_search.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import numpy as np
22
import pytest
3+
import xarray as xr
34

45
from parcels._datasets.structured.generic import datasets
56
from parcels._index_search import _search_indices_curvilinear_2d
67
from parcels.field import Field
7-
from parcels.xgrid import (
8-
XGrid,
9-
)
8+
from parcels.tools.exampledata_utils import download_example_dataset
9+
from parcels.xgcm import Grid
10+
from parcels.xgrid import XGrid
1011

1112

1213
@pytest.fixture
@@ -51,3 +52,32 @@ def test_grid_indexing_fpoints(field_cone):
5152
]
5253
assert x > np.min(cell_lon) and x < np.max(cell_lon)
5354
assert y > np.min(cell_lat) and y < np.max(cell_lat)
55+
56+
57+
def test_indexing_nemo_curvilinear():
58+
data_folder = download_example_dataset("NemoCurvilinear_data")
59+
ds = xr.open_mfdataset(
60+
data_folder.glob("*.nc4"), combine="nested", data_vars="minimal", coords="minimal", compat="override"
61+
)
62+
ds = ds.isel({"time_counter": 0, "time": 0, "z_a": 0}, drop=True).rename(
63+
{"glamf": "lon", "gphif": "lat", "z": "depth"}
64+
)
65+
xgcm_grid = Grid(ds, coords={"X": {"left": "x"}, "Y": {"left": "y"}}, periodic=False)
66+
grid = XGrid(xgcm_grid)
67+
68+
# Test points on the NEMO 1/4 degree curvilinear grid
69+
lats = np.array([-30, 0, 88])
70+
lons = np.array([30, 60, -150])
71+
72+
yi, eta, xi, xsi = _search_indices_curvilinear_2d(grid, lats, lons)
73+
74+
# Construct cornerpoints px
75+
px = np.array([grid.lon[yi, xi], grid.lon[yi, xi + 1], grid.lon[yi + 1, xi + 1], grid.lon[yi + 1, xi]])
76+
77+
# Maximum 5 degree difference between px values
78+
for i in range(lons.shape[0]):
79+
np.testing.assert_allclose(px[1, i], px[:, i], atol=5)
80+
81+
# Reconstruct lons values from cornerpoints
82+
xx = (1 - xsi) * (1 - eta) * px[0] + xsi * (1 - eta) * px[1] + xsi * eta * px[2] + (1 - xsi) * eta * px[3]
83+
np.testing.assert_allclose(xx, lons, atol=1e-6)

0 commit comments

Comments
 (0)