Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/parcels/_core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ def interp_method(self, method: VectorInterpolator):
if not isinstance(method, VectorInterpolator):
raise ValueError(f"method must be a `VectorInterpolator` object. Got {type(method)=!r}")
self._interp_method = method
if getattr(self.U, "model", None) is not None:
self.U.model.field_to_interpolator[self.name] = method

@property
def igrid(self):
Expand Down
12 changes: 8 additions & 4 deletions src/parcels/_core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ def construct_fields(self) -> list[Field | VectorField]:
single_fields[varname] = Field(str(varname), self)

for vfield_name, components in self.vector_field_components.items():
interp_method = (
XLinear_Velocity() if _is_agrid(self.data, u=components[0], v=components[1]) else CGrid_Velocity()
)
interp_method = self.field_to_interpolator.get(vfield_name)
if interp_method is None:
interp_method = (
XLinear_Velocity() if _is_agrid(self.data, u=components[0], v=components[1]) else CGrid_Velocity()
)

component_fields = [single_fields[name] for name in components]
vector_fields[vfield_name] = VectorField(vfield_name, *component_fields, interp_method=interp_method) # type:ignore[misc,arg-type]
Expand Down Expand Up @@ -388,7 +390,9 @@ def construct_fields(self) -> list[Field | VectorField]:
single_fields[varname] = Field(str(varname), self)

for vfield_name, components in self.vector_field_components.items():
interp_method = Ux_Velocity()
interp_method = self.field_to_interpolator.get(vfield_name)
if interp_method is None:
interp_method = Ux_Velocity()

component_fields = [single_fields[name] for name in components]
vector_fields[vfield_name] = VectorField(vfield_name, *component_fields, interp_method=interp_method) # type:ignore[misc, arg-type]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ def test_fieldset_add():
assert set(fields_before) == set(fset.fields.keys())


def test_fieldset_add_preserves_vector_field_interp_method():
"""Test that custom Field and VectorField interp_method values survive FieldSet extension."""
from parcels.interpolators import XFreeslip, XNearest

ds1 = datasets_structured["ds_2d_left"][["U_A_grid", "V_A_grid", "grid"]].rename({"U_A_grid": "U", "V_A_grid": "V"})
fieldset = FieldSet.from_sgrid_conventions(ds1)
fieldset.U.interp_method = XNearest()
fieldset.UV.interp_method = XFreeslip()

ds2 = ds1.copy().rename({"U": "U2", "V": "V2"})
fieldset += FieldSet.from_sgrid_conventions(ds2, vector_fields={"UV2": ("U2", "V2")})

assert isinstance(fieldset.U.interp_method, XNearest)
assert isinstance(fieldset.UV.interp_method, XFreeslip)


def test_fieldset_add_different_meshes():
ds1 = datasets_structured["ds_2d_left"][["U_A_grid", "V_A_grid", "grid"]].rename({"U_A_grid": "U", "V_A_grid": "V"})
ds2 = datasets_structured["ds_2d_left"][["U_A_grid", "V_A_grid", "grid"]].rename(
Expand Down