|
1 | 1 | import numpy as np |
2 | 2 | import pytest |
| 3 | +import xarray as xr |
3 | 4 |
|
4 | 5 | from parcels._datasets.structured.generic import datasets |
5 | 6 | from parcels._index_search import _search_indices_curvilinear_2d |
6 | 7 | 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 |
10 | 11 |
|
11 | 12 |
|
12 | 13 | @pytest.fixture |
@@ -51,3 +52,32 @@ def test_grid_indexing_fpoints(field_cone): |
51 | 52 | ] |
52 | 53 | assert x > np.min(cell_lon) and x < np.max(cell_lon) |
53 | 54 | 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