|
3 | 3 | import xarray as xr |
4 | 4 |
|
5 | 5 | import parcels._interpolation as interpolation |
6 | | -from parcels import AdvectionRK4_3D, JITParticle, ParticleSet, ScipyParticle |
| 6 | +from parcels import AdvectionRK4_3D, FieldSet, JITParticle, ParticleSet, ScipyParticle |
7 | 7 | from tests.utils import create_fieldset_zeros_3d |
8 | 8 |
|
9 | 9 |
|
@@ -50,6 +50,29 @@ def create_interpolation_data(): |
50 | 50 | return xr.DataArray([spatial_data, spatial_data, spatial_data], dims=("time", "depth", "lat", "lon")) |
51 | 51 |
|
52 | 52 |
|
| 53 | +def create_interpolation_data_with_land(): |
| 54 | + tdim, zdim, ydim, xdim = 20, 5, 10, 10 |
| 55 | + ds = xr.Dataset( |
| 56 | + { |
| 57 | + "U": (("time", "depth", "lat", "lon"), np.random.random((tdim, zdim, ydim, xdim))), |
| 58 | + "V": (("time", "depth", "lat", "lon"), np.random.random((tdim, zdim, ydim, xdim))), |
| 59 | + "W": (("time", "depth", "lat", "lon"), np.random.random((tdim, zdim, ydim, xdim))), |
| 60 | + }, |
| 61 | + coords={ |
| 62 | + "time": np.linspace(0, tdim - 1, tdim), |
| 63 | + "depth": np.linspace(0, 1, zdim), |
| 64 | + "lat": np.linspace(0, 1, ydim), |
| 65 | + "lon": np.linspace(0, 1, xdim), |
| 66 | + }, |
| 67 | + ) |
| 68 | + # Set a land point (for testing freeslip) |
| 69 | + ds["U"][:, :, 2, 5] = 0.0 |
| 70 | + ds["V"][:, :, 2, 5] = 0.0 |
| 71 | + ds["W"][:, :, 2, 5] = 0.0 |
| 72 | + |
| 73 | + return ds |
| 74 | + |
| 75 | + |
53 | 76 | @pytest.fixture |
54 | 77 | def data_2d(): |
55 | 78 | """2D slice of the reference data at depth=0.""" |
@@ -131,12 +154,11 @@ def test_interpolator2(ctx: interpolation.InterpolationContext3D): |
131 | 154 | @pytest.mark.parametrize("interp_method", ["linear", "freeslip", "nearest", "cgrid_velocity"]) |
132 | 155 | def test_scipy_vs_jit(interp_method): |
133 | 156 | """Test that the scipy and JIT versions of the interpolation are the same.""" |
134 | | - fieldset = create_fieldset_zeros_3d() |
135 | | - # Randomize the field data and set the interpolation method |
136 | | - rng = np.random.default_rng(1636) |
137 | | - for field in [fieldset.U, fieldset.V, fieldset.W]: |
138 | | - field.data = rng.random(fieldset.U.data.shape, dtype=np.float32) |
139 | | - field.data[:, 3, 2, 5] = 0.0 # Set a land point (for testing freeslip) |
| 157 | + variables = {"U": "U", "V": "V", "W": "W"} |
| 158 | + dimensions = {"time": "time", "lon": "lon", "lat": "lat", "depth": "depth"} |
| 159 | + fieldset = FieldSet.from_xarray_dataset(create_interpolation_data_with_land(), variables, dimensions, mesh="flat") |
| 160 | + |
| 161 | + for field in [fieldset.U, fieldset.V, fieldset.W]: # Set a land point (for testing freeslip) |
140 | 162 | field.interp_method = interp_method |
141 | 163 |
|
142 | 164 | x, y, z = np.meshgrid(np.linspace(0, 1, 7), np.linspace(0, 1, 13), np.linspace(0, 1, 5)) |
|
0 commit comments