|
3 | 3 | import xarray as xr |
4 | 4 |
|
5 | 5 | import parcels |
6 | | -from parcels import Field, FieldSet, Particle, ParticleFile, ParticleSet, StatusCode, Variable, VectorField, XGrid |
| 6 | +from parcels import ( |
| 7 | + Field, |
| 8 | + FieldSet, |
| 9 | + Particle, |
| 10 | + ParticleFile, |
| 11 | + ParticleSet, |
| 12 | + StatusCode, |
| 13 | + Variable, |
| 14 | + VectorField, |
| 15 | + XGrid, |
| 16 | + convert, |
| 17 | +) |
7 | 18 | from parcels._core.utils.time import timedelta_to_float |
8 | 19 | from parcels._datasets.structured.generated import ( |
9 | 20 | decaying_moving_eddy_dataset, |
@@ -491,3 +502,39 @@ def test_nemo_3D_curvilinear_fieldset(kernel): |
491 | 502 | [p.z for p in pset], |
492 | 503 | [0.666162, 0.8667131, 0.92150104, 0.9605109, 0.9577529, 1.0041442, 1.0284728, 1.0033542, 1.2949713, 1.3928112], |
493 | 504 | ) # fmt:skip |
| 505 | + |
| 506 | + |
| 507 | +def test_mitgcm(): |
| 508 | + data_folder = parcels.download_example_dataset("MITgcm_example_data") |
| 509 | + ds_fields = xr.open_dataset(data_folder / "mitgcm_UV_surface_zonally_reentrant.nc") |
| 510 | + |
| 511 | + # TODO fix cftime conversion in Parcels itself |
| 512 | + t = ds_fields["time"].values |
| 513 | + secs = np.array([(ti - t[0]).total_seconds() for ti in t]) |
| 514 | + td_ns = np.rint(secs * 1e9).astype("int64").astype("timedelta64[ns]") |
| 515 | + ds_fields = ds_fields.assign_coords(time=td_ns) |
| 516 | + |
| 517 | + coords = ds_fields[["XG", "YG", "Zl", "time"]] |
| 518 | + ds_fset = convert.mitgcm_to_sgrid(fields={"U": ds_fields.UVEL, "V": ds_fields.VVEL}, coords=coords) |
| 519 | + fieldset = FieldSet.from_sgrid_conventions(ds_fset) |
| 520 | + |
| 521 | + npart = 10 |
| 522 | + lon = [24e3] * npart |
| 523 | + lat = np.linspace(22e3, 1950e3, npart) |
| 524 | + |
| 525 | + pset = parcels.ParticleSet(fieldset, lon=lon, lat=lat) |
| 526 | + pset.execute(AdvectionRK4, runtime=np.timedelta64(5, "D"), dt=np.timedelta64(30, "m")) |
| 527 | + |
| 528 | + lon_v3 = [ |
| 529 | + 25334.3084714, |
| 530 | + 82824.04760837, |
| 531 | + 136410.63322281, |
| 532 | + 98325.83708985, |
| 533 | + 83152.54325753, |
| 534 | + 89321.35275493, |
| 535 | + 237376.5840757, |
| 536 | + 56860.97672692, |
| 537 | + 153947.52685014, |
| 538 | + 28349.16658616, |
| 539 | + ] |
| 540 | + np.testing.assert_allclose(pset.lon, lon_v3, atol=10) |
0 commit comments