Skip to content

Commit 5bf4a16

Browse files
Merge pull request #2329 from vicenzodarezzo/v4-dev
changing TimeExtrapolationError to OutsideTimeInterval
2 parents 770d424 + a5cc9b6 commit 5bf4a16

File tree

7 files changed

+21
-24
lines changed

7 files changed

+21
-24
lines changed

docs/examples_v3/example_globcurrent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test__particles_init_time():
121121
assert pset[0].time - pset4[0].time == 0
122122

123123

124-
def test_globcurrent_time_extrapolation_error():
124+
def test_globcurrent_outside_time_interval_error():
125125
fieldset = set_globcurrent_fieldset()
126126
pset = parcels.ParticleSet(
127127
fieldset,
@@ -130,7 +130,7 @@ def test_globcurrent_time_extrapolation_error():
130130
lat=[-35],
131131
time=fieldset.U.grid.time[0] - timedelta(days=1).total_seconds(),
132132
)
133-
with pytest.raises(parcels.TimeExtrapolationError):
133+
with pytest.raises(parcels.OutsideTimeInterval):
134134
pset.execute(
135135
parcels.kernels.AdvectionRK4,
136136
runtime=timedelta(days=1),
@@ -179,7 +179,7 @@ def SampleP(particle, fieldset, time): # pragma: no cover
179179
pset = parcels.ParticleSet(fieldset, pclass=MyParticle, lon=[25], lat=[-35])
180180

181181
if with_starttime:
182-
with pytest.raises(parcels.TimeExtrapolationError):
182+
with pytest.raises(parcels.OutsideTimeInterval):
183183
pset.execute(
184184
pset.Kernel(parcels.kernels.AdvectionRK4) + SampleP,
185185
runtime=timedelta(days=1),

docs/examples_v3/tutorial_kernelloop.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
"### 3. The last time is not written to file\n",
266266
"Because the location at the start of the loop is written at the end of the Kernel loop, the last `particle.time` of the particle is not written to file. This is similar behaviour to e.g. `np.arange(start, stop)`, which also doesn't include the `stop` value itself. \n",
267267
"\n",
268-
"If you do want to write the last time to file, you can increase the `runtime` or `endtime` by `dt` (although this may cause a TimeExtrapolationError if your run was to the end of the available hydrodynamic data), or you can call `pfile.write_latest_locations(pset, time=pset[0].time_nextloop)`. Note that in the latter case, the particle locations (longitude, latitude and depth) will be updated, but other variables will _not_ be updated as the Kernels are not run again."
268+
"If you do want to write the last time to file, you can increase the `runtime` or `endtime` by `dt` (although this may cause an OutsideTimeInterval if your run was to the end of the available hydrodynamic data), or you can call `pfile.write_latest_locations(pset, time=pset[0].time_nextloop)`. Note that in the latter case, the particle locations (longitude, latitude and depth) will be updated, but other variables will _not_ be updated as the Kernels are not run again."
269269
]
270270
},
271271
{

src/parcels/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
FieldOutOfBoundError,
3030
FieldSamplingError,
3131
KernelError,
32+
OutsideTimeInterval,
3233
StatusCode,
33-
TimeExtrapolationError,
3434
)
3535
from parcels._core.uxgrid import UxGrid
3636
from parcels._core.warnings import (
@@ -69,8 +69,8 @@
6969
"FieldOutOfBoundError",
7070
"FieldSamplingError",
7171
"KernelError",
72+
"OutsideTimeInterval",
7273
"StatusCode",
73-
"TimeExtrapolationError",
7474
# Warnings
7575
"FieldSetWarning",
7676
"FileWarning",

src/parcels/_core/index_search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import numpy as np
77

8-
from parcels._core.statuscodes import _raise_time_extrapolation_error
8+
from parcels._core.statuscodes import _raise_outside_time_interval_error
99

1010
if TYPE_CHECKING:
1111
from parcels._core.field import Field
@@ -78,7 +78,7 @@ def _search_time_index(field: Field, time: datetime):
7878
return np.zeros(shape=time.shape, dtype=np.float32), np.zeros(shape=time.shape, dtype=np.int32)
7979

8080
if not field.time_interval.is_all_time_in_interval(time):
81-
_raise_time_extrapolation_error(time, field=None)
81+
_raise_outside_time_interval_error(time, field=None)
8282

8383
ti = np.searchsorted(field.data.time.data, time, side="right") - 1
8484
tau = (time - field.data.time.data[ti]) / (field.data.time.data[ti + 1] - field.data.time.data[ti])

src/parcels/_core/kernel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
_raise_field_out_of_bound_surface_error,
1515
_raise_general_error,
1616
_raise_grid_searching_error,
17-
_raise_time_extrapolation_error,
17+
_raise_outside_time_interval_error,
1818
)
1919
from parcels._core.warnings import KernelWarning
2020
from parcels.kernels import (
@@ -31,7 +31,7 @@
3131

3232

3333
ErrorsToThrow = {
34-
StatusCode.ErrorTimeExtrapolation: _raise_time_extrapolation_error,
34+
StatusCode.ErrorOutsideTimeInterval: _raise_outside_time_interval_error,
3535
StatusCode.ErrorOutOfBounds: _raise_field_out_of_bound_error,
3636
StatusCode.ErrorThroughSurface: _raise_field_out_of_bound_surface_error,
3737
StatusCode.ErrorInterpolation: _raise_field_interpolation_error,
@@ -279,7 +279,7 @@ def execute(self, pset, endtime, dt):
279279
for error_code, error_func in ErrorsToThrow.items():
280280
if np.any(pset.state == error_code):
281281
inds = pset.state == error_code
282-
if error_code == StatusCode.ErrorTimeExtrapolation:
282+
if error_code == StatusCode.ErrorOutsideTimeInterval:
283283
error_func(pset[inds].time)
284284
else:
285285
error_func(pset[inds].z, pset[inds].lat, pset[inds].lon)

src/parcels/_core/statuscodes.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"FieldOutOfBoundError",
66
"FieldSamplingError",
77
"KernelError",
8+
"OutsideTimeInterval",
89
"StatusCode",
9-
"TimeExtrapolationError",
1010
"_raise_field_interpolation_error",
1111
"_raise_field_out_of_bound_error",
1212
"_raise_field_out_of_bound_surface_error",
1313
"_raise_general_error",
1414
"_raise_grid_searching_error",
15-
"_raise_time_extrapolation_error",
15+
"_raise_outside_time_interval_error",
1616
]
1717

1818

@@ -30,7 +30,7 @@ class StatusCode:
3030
ErrorGridSearching = 52
3131
ErrorOutOfBounds = 60
3232
ErrorThroughSurface = 61
33-
ErrorTimeExtrapolation = 70
33+
ErrorOutsideTimeInterval = 70
3434

3535

3636
class FieldInterpolationError(RuntimeError):
@@ -94,19 +94,16 @@ def _raise_general_error(z, y, x):
9494
raise GeneralError(f"General error occurred at (z={z}, lat={y}, lon={x})")
9595

9696

97-
class TimeExtrapolationError(RuntimeError):
97+
class OutsideTimeInterval(RuntimeError):
9898
"""Utility error class to propagate erroneous time extrapolation sampling."""
9999

100100
def __init__(self, time, field=None):
101-
message = (
102-
f"{field.name if field else 'Field'} sampled outside time domain at time {time}."
103-
" Try setting allow_time_extrapolation to True."
104-
)
101+
message = f"{field.name if field else 'Field'} sampled outside time domain at time {time}."
105102
super().__init__(message)
106103

107104

108-
def _raise_time_extrapolation_error(time: float, field=None):
109-
raise TimeExtrapolationError(time, field)
105+
def _raise_outside_time_interval_error(time: float, field=None):
106+
raise OutsideTimeInterval(time, field)
110107

111108

112109
class KernelError(RuntimeError):
@@ -124,7 +121,7 @@ def __init__(self, particles, fieldset=None, msg=None):
124121
FieldOutOfBoundError: StatusCode.ErrorOutOfBounds,
125122
FieldOutOfBoundSurfaceError: StatusCode.ErrorThroughSurface,
126123
GridSearchingError: StatusCode.ErrorGridSearching,
127-
TimeExtrapolationError: StatusCode.ErrorTimeExtrapolation,
124+
OutsideTimeInterval: StatusCode.ErrorOutsideTimeInterval,
128125
KernelError: StatusCode.Error,
129126
GeneralError: StatusCode.Error,
130127
}

tests/test_particleset_execute.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
FieldInterpolationError,
1010
FieldOutOfBoundError,
1111
FieldSet,
12+
OutsideTimeInterval,
1213
Particle,
1314
ParticleFile,
1415
ParticleSet,
1516
StatusCode,
16-
TimeExtrapolationError,
1717
UxGrid,
1818
Variable,
1919
VectorField,
@@ -286,7 +286,7 @@ def test_some_particles_throw_outoftime(fieldset):
286286
def FieldAccessOutsideTime(particles, fieldset): # pragma: no cover
287287
fieldset.U[particles.time + np.timedelta64(400, "D"), particles.z, particles.lat, particles.lon, particles]
288288

289-
with pytest.raises(TimeExtrapolationError):
289+
with pytest.raises(OutsideTimeInterval):
290290
pset.execute(FieldAccessOutsideTime, runtime=np.timedelta64(1, "D"), dt=np.timedelta64(10, "D"))
291291

292292

0 commit comments

Comments
 (0)