Skip to content
Open
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
10 changes: 5 additions & 5 deletions docs/user_guide/examples/explanation_kernelloop.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ ds_fields = parcels.tutorial.open_dataset("CopernicusMarine_data_for_Argo_tutori
ds_fields.load() # load the dataset into memory

# Create an idealised wind field and add it to the dataset
tdim, ydim, xdim = (len(ds_fields.time),len(ds_fields.latitude), len(ds_fields.longitude))
ydim, xdim = len(ds_fields.latitude), len(ds_fields.longitude)
ds_fields["UWind"] = xr.DataArray(
data=0.5 * np.ones((tdim, ydim, xdim)) * np.sin(ds_fields.latitude.values - ds_fields.latitude.values.mean())[None, :, None],
coords=[ds_fields.time, ds_fields.latitude, ds_fields.longitude])
data=0.5 * np.ones((ydim, xdim)) * np.sin(ds_fields.latitude.values - ds_fields.latitude.values.mean())[:, None],
coords=[ds_fields.latitude, ds_fields.longitude])

ds_fields["VWind"] = xr.DataArray(
data=np.zeros((tdim, ydim, xdim)),
coords=[ds_fields.time, ds_fields.latitude, ds_fields.longitude])
data=np.zeros((ydim, xdim)),
coords=[ds_fields.latitude, ds_fields.longitude])

fields = {
"U": ds_fields["uo"],
Expand Down
3 changes: 3 additions & 0 deletions src/parcels/_core/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def grid(self): # TODO PR: Remove in favour of referencing model grid directly

@property
def time_interval(self): # TODO PR: Remove in favour of referencing model time_interval directly
if "time" not in self.model.data[self.name].dims:
# This field does not have a time-dimension
return None
Comment on lines +112 to +114

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.

This is the key change that fixes #2735

return self.model.time_interval

def __repr__(self):
Expand Down
4 changes: 2 additions & 2 deletions src/parcels/_core/index_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def _search_time_index(field: Field, time: np.ndarray):
if field.time_interval is None:
return {
"T": {
"index": np.zeros(shape=time.shape, dtype=np.int32),
"bcoord": np.zeros(shape=time.shape, dtype=np.float32),
"index": np.zeros(shape=np.atleast_1d(time).shape, dtype=np.int32),
"bcoord": np.zeros(shape=np.atleast_1d(time).shape, dtype=np.float32),
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/test_fieldset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import pandas as pd
import pytest
import xarray as xr

from parcels import Field, ParticleFile, ParticleSet, XGrid, convert
from parcels._core.fieldset import FieldSet, _datetime_to_msg
Expand Down Expand Up @@ -359,6 +360,17 @@ def test_fieldset_add():
assert set(fields_before) == set(fset.fields.keys())


def test_vectorfields_without_time():
"""Test that vector fields without a time dimension can be evaluated."""
ds1 = datasets_structured["ds_2d_left"][["U_A_grid", "V_A_grid", "grid"]].rename({"U_A_grid": "U", "V_A_grid": "V"})
ds2 = ds1.isel(time=0).drop_vars("time").rename({"U": "U_const", "V": "V_const"})
ds = xr.merge([ds1, ds2])

fset = FieldSet.from_sgrid_conventions(ds, mesh="flat", vector_fields={"UV_const": ("U_const", "V_const")})
fset.UV_const.eval(t=0, z=0, y=0, x=0)
fset.U_const.eval(t=0, z=0, y=0, x=0)


def test_fieldset_add_error_on_duplicate_context_values():
"""Test that adding FieldSets with overlapping context value names raises a ValueError."""
ds1 = datasets_structured["ds_2d_left"][["U_A_grid", "grid"]].rename({"U_A_grid": "U1"})
Expand Down