-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6167e0
commit 7406e0d
Showing
5 changed files
with
138 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
oceanstream/L2_calibrated_data/target_strength_computation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
""" | ||
target_strength_computation.py | ||
------------------------------- | ||
Module for computing the target strength (TS) from raw data. | ||
Supported Sonar Models: | ||
- EK60 | ||
- AZFP | ||
- EK80 | ||
Functions and Classes: | ||
- `SupportedSonarModelsForTS`: An Enum containing the sonar models supported | ||
for TS computation. | ||
- `WaveformMode`: Enum specifying the waveform mode ("CW" or "BB"). | ||
- `EncodeMode`: Enum indicating the encoding mode ("complex" or "power"). | ||
- `ComputeTSParams`: Class to validate and structure the parameters passed | ||
to the TS computation function. | ||
- `compute_target_strength`: Main function to calculate TS given an EchoData object | ||
and other optional parameters. This function is based on the `echopype.calibrate.compute_TS()` function. | ||
Usage: | ||
To compute TS for a given EchoData object, `ed`, simply call: | ||
`compute_target_strength(ed)` | ||
""" | ||
|
||
from enum import Enum | ||
from typing import Any, Optional | ||
|
||
import echopype as ep | ||
import xarray as xr | ||
from echopype.echodata.echodata import EchoData | ||
from pydantic import BaseModel, ValidationError, field_validator | ||
|
||
|
||
class SupportedSonarModelsForTS(str, Enum): | ||
EK60 = "EK60" | ||
AZFP = "AZFP" | ||
EK80 = "EK80" | ||
|
||
|
||
class WaveformMode(str, Enum): | ||
CW = "CW" | ||
BB = "BB" | ||
|
||
|
||
class EncodeMode(str, Enum): | ||
COMPLEX = "complex" | ||
POWER = "power" | ||
|
||
|
||
class ComputeTSParams(BaseModel): | ||
echodata: Any | ||
env_params: Optional[dict] = None | ||
cal_params: Optional[dict] = None | ||
waveform_mode: Optional[WaveformMode] = None | ||
encode_mode: Optional[EncodeMode] = None | ||
|
||
@field_validator("echodata") | ||
def check_echodata_type(cls, value): | ||
if not isinstance(value, EchoData): | ||
raise ValueError("Invalid type for echodata. Expected an instance of EchoData.") | ||
return value | ||
|
||
|
||
def compute_target_strength(echodata: EchoData, **kwargs) -> xr.Dataset: | ||
""" | ||
Compute target strength (TS) from raw data. | ||
Parameters: | ||
- echodata (EchoData): The EchoData object containing | ||
sonar data for computation. | ||
- **kwargs: Additional keyword arguments passed to the TS computation. | ||
Returns: | ||
- xr.Dataset: A Dataset containing the computed TS values. | ||
Notes: | ||
This function: | ||
- Validates the `echodata`'s sonar model against supported models. | ||
- Uses the `ComputeTSParams` pydantic model to validate parameters. | ||
- Checks if the computed TS is empty. | ||
- Returns TS only if it is not empty. | ||
- Is based on the `echopype.calibrate.compute_TS()` function. | ||
""" | ||
# Validate parameters using the pydantic model | ||
try: | ||
ComputeTSParams(echodata=echodata, **kwargs) | ||
except ValidationError as e: | ||
raise ValueError(str(e)) | ||
# Check if the sonar model is supported | ||
sonar_model = echodata.sonar_model | ||
try: | ||
SupportedSonarModelsForTS(sonar_model) | ||
except ValueError: | ||
raise ValueError( | ||
f"Sonar model '{sonar_model}'\ | ||
is not supported for TS computation.\ | ||
Supported models are \ | ||
{list(SupportedSonarModelsForTS)}." | ||
) | ||
# Compute TS | ||
TS = ep.calibrate.compute_TS(echodata, **kwargs) | ||
# Check if the computed TS is empty | ||
if TS["TS"].values.size == 0: | ||
raise ValueError("Computed TS is empty!") | ||
return TS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from oceanstream.L2_calibrated_data import target_strength_computation | ||
|
||
|
||
def test_ctarget_strength_computation(ed_ek_60_for_Sv): | ||
TS = target_strength_computation.compute_target_strength(ed_ek_60_for_Sv, encode_mode="power") | ||
val = np.nanmean(TS["TS"].values) | ||
assert val == pytest.approx(-68.06057158474684, 0.0001) |