Skip to content

Commit 8c143a5

Browse files
committed
Add Field.time_interval attribute and update time dim in example datasets
1 parent 2254932 commit 8c143a5

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

parcels/_datasets/structured/generic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
__all__ = ["N", "T", "datasets"]
77

88
N = 30
9-
T = 10
9+
T = 13
1010

1111

1212
def _rotated_curvilinear_grid():
@@ -45,7 +45,7 @@ def _rotated_curvilinear_grid():
4545
{"axis": "Z"},
4646
),
4747
"depth": (["ZG"], np.arange(3 * N), {"axis": "Z"}),
48-
"time": (["time"], np.arange(T), {"axis": "T"}),
48+
"time": (["time"], xr.date_range("2000", "2001", T), {"axis": "T"}),
4949
"lon": (
5050
["YG", "XG"],
5151
LON,
@@ -120,7 +120,7 @@ def _unrolled_cone_curvilinear_grid():
120120
{"axis": "Z"},
121121
),
122122
"depth": (["ZG"], np.arange(3 * N), {"axis": "Z"}),
123-
"time": (["time"], np.arange(T), {"axis": "T"}),
123+
"time": (["time"], xr.date_range("2000", "2001", T), {"axis": "T"}),
124124
"lon": (
125125
["YG", "XG"],
126126
LON,
@@ -176,7 +176,7 @@ def _unrolled_cone_curvilinear_grid():
176176
"lon": (["XG"], 2 * np.pi / N * np.arange(0, N)),
177177
"lat": (["YG"], 2 * np.pi / (2 * N) * np.arange(0, 2 * N)),
178178
"depth": (["ZG"], np.arange(3 * N)),
179-
"time": (["time"], np.arange(T), {"axis": "T"}),
179+
"time": (["time"], xr.date_range("2000", "2001", T), {"axis": "T"}),
180180
},
181181
),
182182
"2d_left_unrolled_cone": _unrolled_cone_curvilinear_grid(),

parcels/field.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
from __future__ import annotations
2+
13
import inspect
24
import warnings
35
from collections.abc import Callable
46
from datetime import datetime
57
from enum import IntEnum
6-
from typing import TYPE_CHECKING
78

89
import numpy as np
910
import uxarray as ux
1011
import xarray as xr
1112
from uxarray.grid.neighbors import _barycentric_coordinates
1213

14+
from parcels._core.utils.time import TimeInterval
1315
from parcels._core.utils.unstructured import get_vertical_location_from_dims
1416
from parcels._reprs import default_repr, field_repr
1517
from parcels._typing import (
@@ -33,9 +35,6 @@
3335

3436
from ._index_search import _search_indices_rectilinear, _search_time_index
3537

36-
if TYPE_CHECKING:
37-
pass
38-
3938
__all__ = ["Field", "GridType", "VectorField"]
4039

4140

@@ -167,6 +166,7 @@ def __init__(
167166
self.name = name
168167
self.data = data
169168
self.grid = grid
169+
self.time_interval = get_time_interval(data)
170170

171171
# For compatibility with parts of the codebase that rely on v3 definition of Grid.
172172
# Should be worked to be removed in v4
@@ -663,3 +663,10 @@ def _assert_compatible_combination(data: xr.DataArray | ux.UxDataArray, grid: ux
663663
raise ValueError(
664664
f"Incompatible data-grid combination. Data is a xarray.DataArray, expected `grid` to be a parcels Grid object, got {type(grid)}."
665665
)
666+
667+
668+
def get_time_interval(data: xr.DataArray | ux.UxDataArray) -> TimeInterval | None:
669+
if "time" not in data.dims:
670+
return None
671+
672+
return TimeInterval(data.time.values[0], data.time.values[-1])

tests/v4/test_field.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import pytest
23
import uxarray as ux
34
import xarray as xr
@@ -69,6 +70,21 @@ def test_field_structured_grid_creation(data, grid):
6970
assert field.grid == grid
7071

7172

73+
@pytest.mark.parametrize(
74+
"data,grid",
75+
[
76+
pytest.param(
77+
structured_datasets["ds_2d_left"]["data_g"], Grid(structured_datasets["ds_2d_left"]), id="ds_2d_left"
78+
),
79+
],
80+
)
81+
def test_field_time_interval(data, grid):
82+
"""Test creating a field."""
83+
field = Field(name="test_field", data=data, grid=grid, mesh_type="flat")
84+
assert field.time_interval.left == np.datetime64("2000-01-01")
85+
assert field.time_interval.right == np.datetime64("2001-01-01")
86+
87+
7288
def test_field_unstructured_grid_creation(): ...
7389

7490

@@ -79,6 +95,3 @@ def test_field_interpolation_out_of_spatial_bounds(): ...
7995

8096

8197
def test_field_interpolation_out_of_time_bounds(): ...
82-
83-
84-
def test_field_allow_time_extrapolation(): ...

0 commit comments

Comments
 (0)