Skip to content

Commit 8b6c417

Browse files
Random x, y offset for drifter deployments (#253)
* add small random noise to drifter release locations * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add limit to offset which can be added * update test to account for x,y offsets * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c17e24d commit 8b6c417

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/virtualship/instruments/drifter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from virtualship.instruments.base import Instrument
99
from virtualship.instruments.types import InstrumentType
1010
from virtualship.models.spacetime import Spacetime
11-
from virtualship.utils import register_instrument
11+
from virtualship.utils import _random_noise, register_instrument
1212

1313
# =====================================================
1414
# SECTION: Dataclass
@@ -102,11 +102,18 @@ def simulate(self, measurements, out_path) -> None:
102102
fieldset = self.load_input_data()
103103

104104
# define parcel particles
105+
lat_release = [
106+
drifter.spacetime.location.lat + _random_noise() for drifter in measurements
107+
] # with small random noise to get different trajectories for multiple drifters released at same waypoint
108+
lon_release = [
109+
drifter.spacetime.location.lon + _random_noise() for drifter in measurements
110+
]
111+
105112
drifter_particleset = ParticleSet(
106113
fieldset=fieldset,
107114
pclass=_DrifterParticle,
108-
lat=[drifter.spacetime.location.lat for drifter in measurements],
109-
lon=[drifter.spacetime.location.lon for drifter in measurements],
115+
lat=lat_release,
116+
lon=lon_release,
110117
depth=[drifter.depth for drifter in measurements],
111118
time=[drifter.spacetime.time for drifter in measurements],
112119
has_lifetime=[

src/virtualship/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,9 @@ def _find_files_in_timerange(
560560
)
561561

562562
return [fname for _, fname in files_with_dates]
563+
564+
565+
def _random_noise(scale: float = 0.01, limit: float = 0.03) -> float:
566+
"""Generate a small random noise value for drifter seeding locations."""
567+
value = np.random.normal(loc=0.0, scale=scale)
568+
return np.clip(value, -limit, limit) # ensure noise is within limits

tests/instruments/test_drifter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_simulate_drifters(tmpdir) -> None:
3636
drifters = [
3737
Drifter(
3838
spacetime=Spacetime(
39-
location=Location(latitude=0, longitude=0),
39+
location=Location(latitude=0.05, longitude=0.05),
4040
time=base_time + datetime.timedelta(days=0),
4141
),
4242
depth=0.0,

0 commit comments

Comments
 (0)