Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/user_guide/kernels_write_in_kernel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@
"# The file that will write when the `write_kernel` is executed.\n",
"output_file_kernel = parcels.ParticleFile(\n",
" \"output_kernel.parquet\",\n",
" outputdt=np.timedelta64(\n",
" 2, \"h\"\n",
" ), # TODO: this argument is not relevant for this file, since we will write to it manually in the kernel, but it still needs to be specified when creating the ParticleFile object\n",
" outputdt=np.timedelta64(2, \"h\"),\n",
")"
]
},
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ ignore = [
'N802', # We use function names that are camelcased to denote functions
'N806', # We use capitalized variable names within functions in Parcels to denote fields, as well as multidimensional arrays
'N811', # Well, this just seems like a bad rule O_o
'N816', # TODO: Enable for `src` and consider disabling for notebooks
'N818', # Not all Parcels exceptions should have an "Error" suffix

# # TODO: ignore for now (requires more work). Remove ignore once fixed
Expand All @@ -133,8 +132,10 @@ ignore = [
]

[tool.ruff.lint.per-file-ignores]
# No docstring linting outside the package source
"{tests,docs,tools}/**" = ["D"]
# No docstring or global variable naming linting outside the package source
"{tests,docs,tools}/**" = ["D", "N816"]
"ci/**" = ["N816"]
"*.ipynb" = ["N816"]

[tool.ruff.lint.pydocstyle]
convention = "numpy"
Expand Down
2 changes: 0 additions & 2 deletions src/parcels/_compat_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ def particlefile_to_v3_zarr(from_parquet: str | Path | io.BytesIO, to_zarr: str
df = pl.read_parquet(from_parquet)
table = pq.read_table(from_parquet)

# TODO: Check for available memory here and fail as a safeguard?

# Rename columns to v3 conventions
rename_map = {"particle_id": "trajectory", "t": "time", "x": "lon", "y": "lat"}
try:
Expand Down
13 changes: 6 additions & 7 deletions src/parcels/_core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ def __init__(
name: str,
model: ModelData,
):
# TODO PR: Enable isinstance check once ModelData is moved to abc.ModelData
# if not isinstance(model, "ModelData"):
# raise ValueError(
# f"Expected `model` to be a Parcels ModelData object. Got {type(model)}."
# )
from parcels._core.model import ModelData

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this import belong here or at the top of the file?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is not pretty, but otherwise I got a circular import. Do you know a better solution?


if not isinstance(model, ModelData):
raise ValueError(f"Expected `model` to be a Parcels ModelData object. Got {type(model)}.")

_assert_str_and_python_varname(name)

Expand All @@ -103,7 +102,7 @@ def data(self):
return self.model.field_data(self.name)

@property
def grid(self): # TODO PR: Remove in favour of referencing model grid directly
def grid(self):
return self.model.grid

@property
Expand All @@ -115,7 +114,7 @@ def igrid(self, value):
self._igrid = value

@property
def time_interval(self): # TODO PR: Remove in favour of referencing model time_interval directly
def time_interval(self):
if "time" not in self.model.data[self.name].dims:
# This field does not have a time-dimension
return None
Expand Down
41 changes: 1 addition & 40 deletions src/parcels/_core/fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
UnstructuredModelData,
create_empty_constant_field_model,
)
from parcels._core.statuscodes import CalendarError
from parcels._core.utils.string import _assert_str_and_python_varname
from parcels._core.utils.time import get_datetime_type_calendar
from parcels._core.utils.time import is_compatible as datetime_is_compatible
Expand Down Expand Up @@ -415,10 +416,6 @@ def assert_models_have_same_mesh(models: list[ModelData]):
)


class CalendarError(Exception): # TODO: Move to a Parcels errors module
"""Exception raised when the calendar of a field is not compatible with the rest of the Fields. The user should ensure that they only add fields to a FieldSet that have compatible CFtime calendars."""


def assert_compatible_calendars(fields: Iterable[Field | VectorField]):
time_intervals = [f.time_interval for f in fields if f.time_interval is not None]

Expand Down Expand Up @@ -446,39 +443,3 @@ def _datetime_to_msg(example_datetime: TimeLike) -> str:

def _format_calendar_error_message(field: Field | VectorField, reference_datetime: TimeLike) -> str:
return f"Expected field {field.name!r} to have calendar compatible with datetime object {_datetime_to_msg(reference_datetime)}. Got field with calendar {_datetime_to_msg(field.time_interval.left)}. Have you considered using xarray to update the time dimension of the dataset to have a compatible calendar?"


_COPERNICUS_MARINE_AXIS_VARNAMES = {
"T": "time",
"Z": "depth",
"Y": "lat",
"X": "lon",
}


_COPERNICUS_MARINE_CF_STANDARD_NAME_FALLBACKS = {
"UV": [
(
"eastward_sea_water_velocity",
"northward_sea_water_velocity",
), # GLOBAL_ANALYSISFORECAST_PHY_001_024, MEDSEA_ANALYSISFORECAST_PHY_006_013, BALTICSEA_ANALYSISFORECAST_PHY_003_006, BLKSEA_ANALYSISFORECAST_PHY_007_001, IBI_ANALYSISFORECAST_PHY_005_001, NWSHELF_ANALYSISFORECAST_PHY_004_013, MULTIOBS_GLO_PHY_MYNRT_015_003, MULTIOBS_GLO_PHY_W_3D_REP_015_007
(
"surface_geostrophic_eastward_sea_water_velocity",
"surface_geostrophic_northward_sea_water_velocity",
), # SEALEVEL_GLO_PHY_L4_MY_008_047, SEALEVEL_EUR_PHY_L4_NRT_008_060
(
"geostrophic_eastward_sea_water_velocity",
"geostrophic_northward_sea_water_velocity",
), # MULTIOBS_GLO_PHY_TSUV_3D_MYNRT_015_012
(
"sea_surface_wave_stokes_drift_x_velocity",
"sea_surface_wave_stokes_drift_y_velocity",
), # GLOBAL_ANALYSISFORECAST_WAV_001_027, MEDSEA_MULTIYEAR_WAV_006_012, ARCTIC_ANALYSIS_FORECAST_WAV_002_014, BLKSEA_ANALYSISFORECAST_WAV_007_003, IBI_ANALYSISFORECAST_WAV_005_005, NWSHELF_ANALYSISFORECAST_WAV_004_014
("sea_water_x_velocity", "sea_water_y_velocity"), # ARCTIC_ANALYSISFORECAST_PHY_002_001
(
"eastward_sea_water_velocity_vertical_mean_over_pelagic_layer",
"northward_sea_water_velocity_vertical_mean_over_pelagic_layer",
), # GLOBAL_MULTIYEAR_BGC_001_033
],
"W": ["upward_sea_water_velocity", "vertical_sea_water_velocity"],
}
5 changes: 2 additions & 3 deletions src/parcels/_core/index_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def _search_1d_array(
array of float
Barycentric coordinate.
"""
# TODO v4: We probably rework this to deal with 0D arrays before this point (as we already know field dimensionality)
if len(arr) < 2:
return np.zeros(shape=x.shape, dtype=np.int32), np.zeros_like(x)
index = np.clip(np.searchsorted(arr, x, side="left") - 1, 0, len(arr) - 2)
Expand Down Expand Up @@ -158,7 +157,7 @@ def curvilinear_point_in_cell(grid, y: np.ndarray, x: np.ndarray, yi: np.ndarray
return is_in_cell, np.column_stack((xsi, eta))


_invA = np.array(
_inv_a = np.array(
[
[1, 0, 0, 0],
[-1, 1, 0, 0],
Expand All @@ -171,7 +170,7 @@ def curvilinear_point_in_cell(grid, y: np.ndarray, x: np.ndarray, yi: np.ndarray
def _bilinear_inverse(px, py, xq, yq):
"""Solve for (xsi, eta) s.t. bilinear blend of (px, py) corners equals (xq, yq)."""
eta_init = -1.0 * np.ones(len(xq), dtype=float)
a, b = np.dot(_invA, px), np.dot(_invA, py)
a, b = np.dot(_inv_a, px), np.dot(_inv_a, py)
aa = a[3] * b[2] - a[2] * b[3]
bb = a[3] * b[0] - a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + xq * b[3] - yq * a[3]
cc = a[1] * b[0] - a[0] * b[1] + xq * b[1] - yq * a[1]
Expand Down
11 changes: 2 additions & 9 deletions src/parcels/_core/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,10 @@ def __init__(
self._pclass = pset._pclass

for f in kernels:
self.check_fieldsets_in_kernels(f)
self.assert_is_compatible(f)

self._kernels: list[Callable] = kernels

@property #! Ported from v3. To be removed in v4? (/find another way to name kernels in output file)
def funcname(self):
ret = ""
for f in self._kernels:
ret += f.__name__
return ret

@property
def pclass(self):
return self._pclass
Expand All @@ -131,7 +124,7 @@ def _position_update(self, particles, fieldset):
# Update dt in case it's increased in RK45 kernel
particles.dt = particles.next_dt

def check_fieldsets_in_kernels(self, kernel): # TODO v4: this can go into another method? assert_is_compatible()?
def assert_is_compatible(self, kernel):
"""
Checks the integrity of the fieldset with the kernels.

Expand Down
11 changes: 2 additions & 9 deletions src/parcels/_core/particleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,10 @@ def __init__(
y=y,
x=x,
particle_id=particle_ids,
**kwargs,
),
)
self._pclass = pclass

# update initial values provided on ParticleSet creation # TODO: Wrap this into create_particle_data
particle_variables = [v.name for v in pclass.variables]
for kwvar, kwval in kwargs.items():
if kwvar not in particle_variables:
raise RuntimeError(f"Particle class does not have Variable {kwvar}")
self._data[kwvar][:] = kwval

self._kernel = None

def __del__(self):
Expand Down Expand Up @@ -397,7 +390,7 @@ def execute(
if output_file is not None:
output_file.set_metadata(self.fieldset.gridset[0].mesh)
output_file.set_time_interval(self.fieldset.time_interval)
output_file.metadata["parcels_kernels"] = self._kernel.funcname
output_file.metadata["parcels_kernels"] = " ".join(f.__name__ for f in self._kernel._kernels)

dt, sign_dt = _convert_dt_to_float(dt)
self._data["dt"][:] = dt
Expand Down
5 changes: 5 additions & 0 deletions src/parcels/_core/statuscodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

__all__ = [
"AllParcelsErrorCodes",
"CalendarError",
"FieldOutOfBoundError",
"FieldSamplingError",
"KernelError",
Expand Down Expand Up @@ -84,6 +85,10 @@ def _raise_grid_searching_error(z, y, x):
raise GridSearchingError(f"Grid searching failed at (z={z}, y={y}, x={x})")


class CalendarError(ValueError):
"""Exception raised when the calendar of a field is not compatible with the rest of the Fields. The user should ensure that they only add fields to a FieldSet that have compatible CFtime calendars."""


class GeneralError(RuntimeError):
"""Utility error class to propagate general errors."""

Expand Down
6 changes: 0 additions & 6 deletions src/parcels/_core/uxgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ def _init_time_varying_z(self) -> None:

@property
def depth(self):
"""
Notes
-----
Included for compatibility with v3 codebase. May be removed in future.
TODO v4: Evaluate
"""
# Load time varying z lazily
if self.z.ndim == 3:
return self.z
Expand Down
50 changes: 1 addition & 49 deletions src/parcels/_core/xgrid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Hashable, Sequence
from functools import cached_property
from typing import Literal, cast
from typing import cast

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -211,12 +211,6 @@ def z_interp_method(self, value: VerticalCoordinateInterpolator):

@property
def lon(self):
"""
Notes
-----
Included for compatibility with v3 codebase. May be removed in future.
TODO v4: Evaluate
"""
if "X" not in self.axes:
return np.zeros(1)
x_coordinate_name = self.sgrid_metadata.node_coordinates[0]
Expand All @@ -227,12 +221,6 @@ def lon(self):

@property
def lat(self):
"""
Notes
-----
Included for compatibility with v3 codebase. May be removed in future.
TODO v4: Evaluate
"""
if "Y" not in self.axes:
return np.zeros(1)
y_coordinate_name = self.sgrid_metadata.node_coordinates[1]
Expand All @@ -243,12 +231,6 @@ def lat(self):

@property
def depth(self):
"""
Notes
-----
Included for compatibility with v3 codebase. May be removed in future.
TODO v4: Evaluate
"""
if "Z" not in self.axes:
return np.zeros(1)
# Load time varying z lazily
Expand Down Expand Up @@ -352,39 +334,9 @@ def localise(
if axis in axis_to_var
}

@property
def _z4d(self) -> Literal[0, 1]:
"""
Notes
-----
Included for compatibility with v3 codebase. May be removed in future.
TODO v4: Evaluate
"""
return 1 if self.depth.shape == 4 else 0

@property
def zonal_periodic(self): ... # ? hmmm, from v3, do we still need this?

@property
def _gtype(self):
"""This class is created *purely* for compatibility with v3 code and will be removed
or changed in future.

TODO: Remove
"""
from parcels._core.basegrid import GridType

if len(self.lon.shape) <= 1:
if self.depth is None or len(self.depth.shape) <= 1:
return GridType.RectilinearZGrid
else:
return GridType.RectilinearSGrid
else:
if self.depth is None or len(self.depth.shape) <= 1:
return GridType.CurvilinearZGrid
else:
return GridType.CurvilinearSGrid

def search(self, z, y, x, ei=None, ti=None, tau=None):
ds = self._ds
if self.sgrid_metadata.node_coordinates is not None:
Expand Down
1 change: 0 additions & 1 deletion src/parcels/_datasets/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ class _Purpose(enum.Enum):
("NemoNorthSeaORCA025-N006_data/V", (_V3Dataset(_ODIE,"data/NemoNorthSeaORCA025-N006_data/ORCA025-N06_200001*05V.nc"), _Purpose.TUTORIAL)),
("NemoNorthSeaORCA025-N006_data/W", (_V3Dataset(_ODIE,"data/NemoNorthSeaORCA025-N006_data/ORCA025-N06_200001*05W.nc"), _Purpose.TUTORIAL)),
("NemoNorthSeaORCA025-N006_data/mesh_mask", (_V3Dataset(_ODIE,"data/NemoNorthSeaORCA025-N006_data/coordinates.nc", _preprocess_drop_time_from_mesh1), _Purpose.TUTORIAL)),
# "POPSouthernOcean_data/t.x1_SAMOC_flux.16900*.nc", # TODO v4: In v3 but should not be in v4 https://github.com/Parcels-code/Parcels/issues/2571#issuecomment-4214476973
# ("SWASH_data/data", (_V3Dataset(_ODIE,"data/SWASH_data/field_00655*.nc"), _Purpose.TUTORIAL)),
# ("WOA_data/data", (_V3Dataset(_ODIE,"data/WOA_data/woa18_decav_t*_04.nc", _preprocess_set_cf_calendar_360_day), _Purpose.TUTORIAL)),
("CROCOoverflow_data/data", (_V3Dataset(_ODIE,"data/CROCOOverflow_data/over_his.nc"), _Purpose.TUTORIAL)),
Expand Down
9 changes: 7 additions & 2 deletions src/parcels/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,12 @@ def _normalize_fields(fields: dict[str, xr.Dataset | xr.DataArray]) -> dict[str,
f"the DataArray instead (e.g., fields={{{name!r}: ds['<variable name>']}})."
)
normalized[name] = field[name]
# TODO: logging message, warn if multiple fields are in this dataset
ignored = [var for var in field.data_vars if var != name]
if ignored:
logger.warning(
f"fields[{name!r}] selects only {name!r} from the Dataset; "
f"other data variables are ignored for this entry: {ignored}."
)
else:
normalized[name] = field.rename(name)
return normalized
Expand Down Expand Up @@ -407,7 +412,7 @@ def nemo_to_sgrid(*, fields: dict[str, xr.Dataset | xr.DataArray], coords: xr.Da
ds["gphif"].attrs["units"] = "degrees"

# Update to use lon and lat for internal naming
ds = ds.sgrid.rename({"gphif": "lat", "glamf": "lon"}) # TODO: Logging message about rename
ds = ds.sgrid.rename({"gphif": "lat", "glamf": "lon"})
return ds


Expand Down
2 changes: 1 addition & 1 deletion tests/test_advection.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def SubmergeParticle(particles, fieldset): # pragma: no cover
)
def test_length1dimensions(u_value, x_slice, v_value, y_slice, w_value, z_slice):
ds = datasets_sgrid["ds_2d_padded_high"].copy()[["U_A_grid", "grid"]]
ds = ds.isel(time=slice(2)) # TODO make this also work for length-1 time dimensions
ds = ds.isel(time=0)
ds = ds.rename({"U_A_grid": "U"})
ds["U"] = xr.full_like(ds["U"], u_value)
ds["V"] = xr.full_like(ds["U"], v_value)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,12 @@ def test_mom6_to_sgrid_withdepth():
V = V.expand_dims(depth=[0], axis=1)
with pytest.raises(ValueError, match="has more than 2 spatial dimensions"):
convert.mom6_to_sgrid(fields={"U": U, "V": V}, coords=coords)


def test_normalize_fields_warns_about_other_variables(caplog):
ds = xr.Dataset({"U": ("x", [1.0]), "V": ("x", [2.0])})

result = convert._normalize_fields({"U": ds})

xr.testing.assert_identical(result["U"], ds["U"])
assert "other data variables are ignored for this entry: ['V']" in caplog.text
Loading
Loading