Skip to content

Commit c22566e

Browse files
committed
apply log filter to adcp and onboard measurements
1 parent e70d881 commit c22566e

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/virtualship/instruments/adcp.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""ADCP instrument."""
22

3+
import logging
34
from pathlib import Path
45

56
import numpy as np
67
from parcels import FieldSet, ParticleSet, ScipyParticle, Variable
78

9+
from ..log_filter import DuplicateFilter
810
from ..spacetime import Spacetime
911

1012
# we specifically use ScipyParticle because we have many small calls to execute
@@ -30,6 +32,7 @@ def simulate_adcp(
3032
min_depth: float,
3133
num_bins: int,
3234
sample_points: list[Spacetime],
35+
log_filter: bool = True,
3336
) -> None:
3437
"""
3538
Use Parcels to simulate an ADCP in a fieldset.
@@ -40,6 +43,7 @@ def simulate_adcp(
4043
:param min_depth: Minimum depth the ADCP can measure.
4144
:param num_bins: How many samples to take in the complete range between max_depth and min_depth.
4245
:param sample_points: The places and times to sample at.
46+
:param log_filter: Whether to filter duplicate log messages (defaults to True). This is a bit of a hack, but it works and could be removed if changed in Parcels.
4347
"""
4448
sample_points.sort(key=lambda p: p.time)
4549

@@ -60,6 +64,12 @@ def simulate_adcp(
6064
# outputdt set to infinite as we just want to write at the end of every call to 'execute'
6165
out_file = particleset.ParticleFile(name=out_path, outputdt=np.inf)
6266

67+
# whether to filter parcels duplicate log messages
68+
if log_filter:
69+
external_logger = logging.getLogger("parcels.tools.loggers")
70+
for handler in external_logger.handlers:
71+
handler.addFilter(DuplicateFilter())
72+
6373
for point in sample_points:
6474
particleset.lon_nextloop[:] = point.location.lon
6575
particleset.lat_nextloop[:] = point.location.lat
@@ -76,3 +86,9 @@ def simulate_adcp(
7686
verbose_progress=False,
7787
output_file=out_file,
7888
)
89+
90+
# turn off log filter after .execute(), to prevent being applied universally to all loggers
91+
# separate if statement from above to prevent error if log_filter is False
92+
if log_filter:
93+
for handler in external_logger.handlers:
94+
handler.removeFilter(handler.filters[0])

src/virtualship/instruments/ship_underwater_st.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""Ship salinity and temperature."""
22

3+
import logging
34
from pathlib import Path
45

56
import numpy as np
67
from parcels import FieldSet, ParticleSet, ScipyParticle, Variable
78

9+
from ..log_filter import DuplicateFilter
810
from ..spacetime import Spacetime
911

1012
# we specifically use ScipyParticle because we have many small calls to execute
@@ -32,6 +34,7 @@ def simulate_ship_underwater_st(
3234
out_path: str | Path,
3335
depth: float,
3436
sample_points: list[Spacetime],
37+
log_filter: bool = True,
3538
) -> None:
3639
"""
3740
Use Parcels to simulate underway data, measuring salinity and temperature at the given depth along the ship track in a fieldset.
@@ -40,6 +43,7 @@ def simulate_ship_underwater_st(
4043
:param out_path: The path to write the results to.
4144
:param depth: The depth at which to measure. 0 is water surface, negative is into the water.
4245
:param sample_points: The places and times to sample at.
46+
:param log_filter: Whether to filter duplicate log messages (defaults to True). This is a bit of a hack, but it works and could be removed if changed in Parcels.
4347
"""
4448
sample_points.sort(key=lambda p: p.time)
4549

@@ -56,6 +60,12 @@ def simulate_ship_underwater_st(
5660
# outputdt set to infinie as we want to just want to write at the end of every call to 'execute'
5761
out_file = particleset.ParticleFile(name=out_path, outputdt=np.inf)
5862

63+
# whether to filter parcels duplicate log messages
64+
if log_filter:
65+
external_logger = logging.getLogger("parcels.tools.loggers")
66+
for handler in external_logger.handlers:
67+
handler.addFilter(DuplicateFilter())
68+
5969
# iterate over each point, manually set lat lon time, then
6070
# execute the particle set for one step, performing one set of measurement
6171
for point in sample_points:
@@ -74,3 +84,9 @@ def simulate_ship_underwater_st(
7484
verbose_progress=False,
7585
output_file=out_file,
7686
)
87+
88+
# turn off log filter after .execute(), to prevent being applied universally to all loggers
89+
# separate if statement from above to prevent error if log_filter is False
90+
if log_filter:
91+
for handler in external_logger.handlers:
92+
handler.removeFilter(handler.filters[0])

0 commit comments

Comments
 (0)