oceanstream 0.0.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oceanstream/__init__.py +0 -0
- oceanstream/cli/__init__.py +3 -0
- oceanstream/cli/main.py +267 -0
- oceanstream/denoise/__init__.py +2 -0
- oceanstream/denoise/apply_masks.py +49 -0
- oceanstream/denoise/applying_masks_handler.py +173 -0
- oceanstream/denoise/background_noise_remover.py +97 -0
- oceanstream/denoise/create_masks.py +87 -0
- oceanstream/denoise/noise_masks.py +398 -0
- oceanstream/denoise/types.py +73 -0
- oceanstream/echodata/__init__.py +5 -0
- oceanstream/echodata/ensure_time_continuity.py +75 -0
- oceanstream/echodata/mvbs_computation.py +77 -0
- oceanstream/echodata/raw_handler.py +577 -0
- oceanstream/echodata/sv_computation.py +118 -0
- oceanstream/echodata/sv_dataset_extension.py +126 -0
- oceanstream/echodata/sv_interpolation.py +260 -0
- oceanstream/echodata/target_strength_computation.py +109 -0
- oceanstream/exports/__init__.py +2 -0
- oceanstream/exports/csv/csv_export_from_Sv.py +89 -0
- oceanstream/exports/csv/csv_export_from_raw.py +92 -0
- oceanstream/exports/csv/csv_export_nasc.py +135 -0
- oceanstream/exports/frequency_differencing_handler.py +210 -0
- oceanstream/exports/nasc.py +23 -0
- oceanstream/exports/nasc_computation.py +62 -0
- oceanstream/exports/shoals/shoal_detection_handler.py +109 -0
- oceanstream/exports/shoals/shoal_process.py +224 -0
- oceanstream/exports/shoals/shoals_handler.py +35 -0
- oceanstream/plot/__init__.py +2 -0
- oceanstream/plot/echogram.py +542 -0
- oceanstream/process/__init__.py +21 -0
- oceanstream/process/combine_zarr.py +90 -0
- oceanstream/process/file_processor.py +151 -0
- oceanstream/process/folder_processor.py +246 -0
- oceanstream/process/process.py +77 -0
- oceanstream/process/processed_data_io.py +140 -0
- oceanstream/settings/__init__.py +1 -0
- oceanstream/settings/config.py +17 -0
- oceanstream/utils.py +101 -0
- oceanstream-0.0.0.dev0.dist-info/LICENSE +21 -0
- oceanstream-0.0.0.dev0.dist-info/METADATA +214 -0
- oceanstream-0.0.0.dev0.dist-info/RECORD +46 -0
- oceanstream-0.0.0.dev0.dist-info/WHEEL +5 -0
- oceanstream-0.0.0.dev0.dist-info/dependency_links.txt +1 -0
- oceanstream-0.0.0.dev0.dist-info/entry_points.txt +2 -0
- oceanstream-0.0.0.dev0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""
|
|
2
|
+
sv_dataset_extension.py
|
|
3
|
+
-----------------------
|
|
4
|
+
|
|
5
|
+
Module for enriching the volume backscattering strength (Sv) dataset by adding depth, location,
|
|
6
|
+
and split-beam angle information.
|
|
7
|
+
|
|
8
|
+
Functions:
|
|
9
|
+
- `enrich_sv_dataset`: Main function to enhance the Sv dataset by adding depth, location,
|
|
10
|
+
and split-beam angle information.
|
|
11
|
+
|
|
12
|
+
Usage:
|
|
13
|
+
|
|
14
|
+
To enrich an Sv dataset `sv` for a given EchoData object, `ed`, simply call:
|
|
15
|
+
`enriched_sv = enrich_sv_dataset(sv, ed, **kwargs)`
|
|
16
|
+
|
|
17
|
+
Note:
|
|
18
|
+
The specific keyword arguments (`**kwargs`) that can be passed to the function are dependent on
|
|
19
|
+
the `add_depth()`, `add_location()`, and `add_splitbeam_angle()` functions from the echopype module.
|
|
20
|
+
Refer to their respective documentation for details.
|
|
21
|
+
|
|
22
|
+
"""
|
|
23
|
+
import warnings
|
|
24
|
+
import numpy as np
|
|
25
|
+
|
|
26
|
+
from echopype.consolidate import add_location, add_splitbeam_angle
|
|
27
|
+
from echopype.echodata.echodata import EchoData
|
|
28
|
+
from xarray import Dataset
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def enrich_sv_dataset(sv: Dataset, echodata: EchoData, **kwargs) -> Dataset:
|
|
32
|
+
"""
|
|
33
|
+
Enhances the input `sv` dataset by adding depth, location, and split-beam angle information.
|
|
34
|
+
|
|
35
|
+
Parameters:
|
|
36
|
+
- sv (xr.Dataset): Volume backscattering strength (Sv) from the given echodata.
|
|
37
|
+
- echodata (EchoData): An EchoData object holding the raw data.
|
|
38
|
+
- **kwargs: Keyword arguments specific to `add_depth()`, `add_location()`, and `add_splitbeam_angle()`.
|
|
39
|
+
Note: These functions are implemented in the echopype module.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
xr.Dataset:
|
|
43
|
+
An enhanced dataset with depth, location, and split-beam angle.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
depth_keys = ["depth_offset", "tilt", "downward"]
|
|
47
|
+
depth_args = {k: kwargs[k] for k in depth_keys if k in kwargs}
|
|
48
|
+
|
|
49
|
+
location_keys = ["nmea_sentence"]
|
|
50
|
+
location_args = {k: kwargs[k] for k in location_keys if k in kwargs}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
splitbeam_keys = [
|
|
54
|
+
"waveform_mode",
|
|
55
|
+
"encode_mode",
|
|
56
|
+
"pulse_compression",
|
|
57
|
+
"storage_options",
|
|
58
|
+
"return_dataset",
|
|
59
|
+
]
|
|
60
|
+
splitbeam_args = {k: kwargs[k] for k in splitbeam_keys if k in kwargs}
|
|
61
|
+
enriched_sv = sv
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
add_depth(enriched_sv, **depth_args)
|
|
65
|
+
except Exception as e:
|
|
66
|
+
warnings.warn(f"Failed to add depth due to error: {str(e)}")
|
|
67
|
+
|
|
68
|
+
# try:
|
|
69
|
+
# enriched_sv = add_location(enriched_sv, echodata, **location_args)
|
|
70
|
+
# except Exception as e:
|
|
71
|
+
# warnings.warn(f"Failed to add location due to error: {str(e)}")
|
|
72
|
+
|
|
73
|
+
# try:
|
|
74
|
+
# add_splitbeam_angle(enriched_sv, echodata, **splitbeam_args)
|
|
75
|
+
# except Exception as e:
|
|
76
|
+
# warnings.warn(f"Failed to add split-beam angle due to error: {str(e)}")
|
|
77
|
+
# traceback.print_exc()
|
|
78
|
+
|
|
79
|
+
return enriched_sv
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def add_depth(Sv: Dataset, depth_offset: float = 0, tilt: float = 0, downward: bool = True):
|
|
83
|
+
"""
|
|
84
|
+
Given an existing Sv dataset, it adds a data variable called depth containing the depth of
|
|
85
|
+
each ping.
|
|
86
|
+
|
|
87
|
+
Parameters:
|
|
88
|
+
- Sv (xr.Dataset): Volume backscattering strength (Sv) from the given echodata.
|
|
89
|
+
- depth_offset (float): Depth offset to be added to the depth values.
|
|
90
|
+
- tilt (float): Tilt angle of the transducer in degrees.
|
|
91
|
+
- downward (bool): Flag indicating whether the depth is measured downward (True) or upward (False).
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
mult = 1 if downward else -1
|
|
95
|
+
|
|
96
|
+
first_channel = Sv["channel"].values[0]
|
|
97
|
+
first_ping_time = Sv["ping_time"].values[0]
|
|
98
|
+
|
|
99
|
+
original_echo_range = Sv["echo_range"].sel(channel=first_channel, ping_time=first_ping_time).values
|
|
100
|
+
|
|
101
|
+
# Slice the echo_range to get the desired range of values
|
|
102
|
+
selected_echo_range = Sv["echo_range"].sel(channel=first_channel, ping_time=first_ping_time)
|
|
103
|
+
selected_echo_range = selected_echo_range.values.tolist()
|
|
104
|
+
selected_echo_range = [mult * value * np.cos(tilt / 180 * np.pi) + depth_offset for value in selected_echo_range]
|
|
105
|
+
Sv = Sv.assign_coords(range_sample=selected_echo_range)
|
|
106
|
+
|
|
107
|
+
return Sv
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def add_seabed_depth(Sv: Dataset):
|
|
111
|
+
"""
|
|
112
|
+
Given an existing Sv dataset with a seabed mask attached, it adds a
|
|
113
|
+
data variable called seabed depth containing the location of the seabed on
|
|
114
|
+
each ping
|
|
115
|
+
|
|
116
|
+
Parameters:
|
|
117
|
+
- sv (xr.Dataset): Volume backscattering strength (Sv) from the given echodata.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
xr.Dataset:
|
|
121
|
+
An enhanced dataset with seabed depth
|
|
122
|
+
"""
|
|
123
|
+
seabed_mask = ~Sv["mask_seabed"]
|
|
124
|
+
seabed_level = seabed_mask.argmax(dim="range_sample")
|
|
125
|
+
res = Sv.assign(seabed_level=seabed_level)
|
|
126
|
+
return res
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""
|
|
2
|
+
sv_interpolation.py
|
|
3
|
+
-------------------
|
|
4
|
+
Description: Module dedicated to the interpolation of acoustic backscatter data in echosounder datasets.
|
|
5
|
+
It provides utilities for converting between decibel and linear scales, applying various masks to the data,
|
|
6
|
+
and performing interpolation to fill missing or NaN values. The module supports both direct `xr.Dataset`
|
|
7
|
+
inputs and file path inputs, ensuring flexibility in data processing workflows. Additionally,
|
|
8
|
+
it offers edge filling capabilities to enhance the quality of interpolated echograms.
|
|
9
|
+
"""
|
|
10
|
+
import numpy as np
|
|
11
|
+
import xarray as xr
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Hashable, Tuple, Union
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def db_to_linear(db: xr.DataArray) -> xr.DataArray:
|
|
17
|
+
"""Convert decibel to linear scale, handling NaN values."""
|
|
18
|
+
linear = xr.where(db.isnull(), np.nan, 10 ** (db / 10))
|
|
19
|
+
return linear
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def linear_to_db(linear: xr.DataArray) -> xr.DataArray:
|
|
23
|
+
"""Convert linear to decibel scale, handling NaN values."""
|
|
24
|
+
db = xr.where(linear.isnull(), np.nan, 10 * np.log10(linear))
|
|
25
|
+
return db
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def interpolate_sv(
|
|
29
|
+
sv: Union[xr.Dataset, str, Path], method: str = "linear", with_edge_fill: bool = False
|
|
30
|
+
) -> xr.Dataset:
|
|
31
|
+
"""
|
|
32
|
+
Apply masks to the Sv DataArray in the dataset and interpolate over the resulting NaN values.
|
|
33
|
+
|
|
34
|
+
Parameters:
|
|
35
|
+
- Sv (Union[xr.Dataset, str, Path]): Dataset or path to the netCDF or zarr file.
|
|
36
|
+
- method (str): Interpolation method.
|
|
37
|
+
- with_edge_fill (bool): Flag to allow filling the edges of echograms
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
- xr.Dataset: Dataset with the masked and interpolated Sv DataArray.
|
|
41
|
+
|
|
42
|
+
Example:
|
|
43
|
+
>> interpolate_sv(Sv, method)
|
|
44
|
+
Expected Output
|
|
45
|
+
"""
|
|
46
|
+
# Load the dataset
|
|
47
|
+
if isinstance(sv, xr.Dataset):
|
|
48
|
+
dataset = sv
|
|
49
|
+
|
|
50
|
+
# Initialize an empty list to store the processed channels
|
|
51
|
+
processed_channels = []
|
|
52
|
+
|
|
53
|
+
# Loop over each channel
|
|
54
|
+
for channel in sv_dataarray["channel"]:
|
|
55
|
+
channel_data = sv_dataarray.sel(channel=channel)
|
|
56
|
+
|
|
57
|
+
# Convert from dB to linear scale
|
|
58
|
+
channel_data_linear = db_to_linear(channel_data)
|
|
59
|
+
|
|
60
|
+
# Perform interpolation to fill NaN values in linear scale using Xarray's interpolate_na
|
|
61
|
+
interpolated_channel_data_linear = channel_data_linear.interpolate_na(
|
|
62
|
+
dim="ping_time", method=method, use_coordinate=True
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
if with_edge_fill:
|
|
66
|
+
interpolated_channel_data_linear = interpolated_channel_data_linear.ffill(dim="ping_time")
|
|
67
|
+
interpolated_channel_data_linear = interpolated_channel_data_linear.bfill(dim="ping_time")
|
|
68
|
+
|
|
69
|
+
# Convert back to dB scale
|
|
70
|
+
interpolated_channel_data = linear_to_db(interpolated_channel_data_linear)
|
|
71
|
+
|
|
72
|
+
# Append the processed channel data to the list
|
|
73
|
+
processed_channels.append(interpolated_channel_data)
|
|
74
|
+
|
|
75
|
+
# Combine the processed channels back into a single DataArray
|
|
76
|
+
interpolated_sv = xr.concat(processed_channels, dim="channel")
|
|
77
|
+
|
|
78
|
+
# Update the Sv DataArray in the dataset with the interpolated values
|
|
79
|
+
dataset["Sv"] = interpolated_sv
|
|
80
|
+
|
|
81
|
+
return dataset
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def find_impacted_variables(
|
|
85
|
+
dataset: xr.Dataset, target_dim: str = "range_sample"
|
|
86
|
+
) -> list[Hashable]:
|
|
87
|
+
"""
|
|
88
|
+
Finds and lists the variable names in a given xarray Dataset that are associated with a specified dimension.
|
|
89
|
+
|
|
90
|
+
Parameters:
|
|
91
|
+
- dataset (xr.Dataset): The xarray Dataset to search for variables.
|
|
92
|
+
- target_dim (str, optional): The name of the target dimension to check in each variable. Defaults to "range_sample".
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
- list[Hashable]: A list of variable names (hashable items) that have the target dimension.
|
|
96
|
+
|
|
97
|
+
Example:
|
|
98
|
+
>> impacted_vars = find_impacted_variables(dataset)
|
|
99
|
+
Expected Output:
|
|
100
|
+
A list of variable names from the dataset which have 'range_sample' as one of their dimensions.
|
|
101
|
+
|
|
102
|
+
>> impacted_vars = find_impacted_variables(dataset, target_dim='time')
|
|
103
|
+
Expected Output:
|
|
104
|
+
A list of variable names from the dataset which have 'time' as one of their dimensions.
|
|
105
|
+
"""
|
|
106
|
+
variable_list = []
|
|
107
|
+
for var_name, data_array in dataset.variables.items():
|
|
108
|
+
if target_dim in data_array.dims and var_name != target_dim:
|
|
109
|
+
variable_list.append(var_name)
|
|
110
|
+
return variable_list
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def find_lowest_resolution_channel(dataset: xr.Dataset) -> Tuple[int, int]:
|
|
114
|
+
"""
|
|
115
|
+
Finds the channel with the lowest resolution in a given xarray Dataset based on echo range data.
|
|
116
|
+
|
|
117
|
+
This function iterates over all channels in the dataset, identified by 'frequency_nominal',
|
|
118
|
+
and determines the channel that reaches the smallest maximum depth across all pings.
|
|
119
|
+
The depth is determined based on 'echo_range' values in the dataset.
|
|
120
|
+
|
|
121
|
+
Parameters:
|
|
122
|
+
- dataset (xr.Dataset): The xarray Dataset containing echo data and channel information.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
- Tuple[int, int]: A tuple containing the index of the channel with the lowest resolution
|
|
126
|
+
and the maximum depth index of that channel. The first element is the channel index,
|
|
127
|
+
and the second element is the maximum depth index within that channel.
|
|
128
|
+
|
|
129
|
+
Example:
|
|
130
|
+
>> lowest_res_channel, max_depth_index = find_lowest_resolution_channel(dataset)
|
|
131
|
+
Expected Output:
|
|
132
|
+
A tuple where the first element is the index of the channel with the lowest resolution,
|
|
133
|
+
and the second element is the maximum depth index within that channel.
|
|
134
|
+
"""
|
|
135
|
+
arg_max_dataset = len(dataset["range_sample"].values)
|
|
136
|
+
return_channel = 0
|
|
137
|
+
for ch in range(len(dataset["frequency_nominal"])):
|
|
138
|
+
arg_max = np.max(np.nanargmax(dataset["echo_range"].values[ch, :, :], axis=1))
|
|
139
|
+
if arg_max < arg_max_dataset:
|
|
140
|
+
arg_max_dataset = arg_max
|
|
141
|
+
return_channel = ch
|
|
142
|
+
return return_channel, arg_max_dataset
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def resample_xarray(da: xr.DataArray, old_depth_da, new_depth_da, new_range_sample) -> xr.DataArray:
|
|
146
|
+
"""
|
|
147
|
+
Resamples an xarray DataArray to a new depth profile using linear interpolation.
|
|
148
|
+
|
|
149
|
+
This function processes an xarray DataArray representing acoustic data, resampling it to
|
|
150
|
+
match a new depth profile. The function handles both 'Sv' (volume backscattering strength)
|
|
151
|
+
and other data types. For 'Sv' data, it first converts from decibel to linear scale before
|
|
152
|
+
resampling, and then back to decibel scale after resampling.
|
|
153
|
+
|
|
154
|
+
Parameters:
|
|
155
|
+
- da (xr.DataArray): The xarray DataArray to resample. It should contain 'channel' and 'ping_time' dimensions.
|
|
156
|
+
- old_depth_da (array-like): The original depth values corresponding to each point in 'da'.
|
|
157
|
+
- new_depth_da (array-like): The new depth values to which 'da' will be resampled.
|
|
158
|
+
- new_range_sample (array-like): The new range sample values that will form the new 'range_sample' coordinate.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
- xr.DataArray: The resampled xarray DataArray with the new depth profile.
|
|
162
|
+
|
|
163
|
+
Example:
|
|
164
|
+
>> resampled_da = resample_xarray(da, old_depth_da, new_depth_da, new_range_sample)
|
|
165
|
+
Expected Output:
|
|
166
|
+
An xarray DataArray resampled to the new depth profile specified by 'new_depth_da' and 'new_range_sample'.
|
|
167
|
+
|
|
168
|
+
Note:
|
|
169
|
+
- The function assumes 'da' has dimensions 'channel' and 'ping_time'.
|
|
170
|
+
- For 'Sv' data, conversions between decibel and linear scales are performed.
|
|
171
|
+
"""
|
|
172
|
+
from scipy.interpolate import interp1d
|
|
173
|
+
|
|
174
|
+
interpolated_data_arrays = []
|
|
175
|
+
channel_order_list = []
|
|
176
|
+
time_coord = da["ping_time"]
|
|
177
|
+
depth_values = old_depth_da
|
|
178
|
+
for ch in range(da.sizes["channel"]):
|
|
179
|
+
depth_idx = len(depth_values[ch])
|
|
180
|
+
if da.name == "Sv":
|
|
181
|
+
data_array = db_to_linear(da[ch, :, :depth_idx]).values
|
|
182
|
+
else:
|
|
183
|
+
data_array = da[ch, :, :depth_idx].values
|
|
184
|
+
new_data_array = np.zeros((data_array.shape[0], len(new_range_sample)))
|
|
185
|
+
for i in range(len(data_array)): # Loop over the time dimension
|
|
186
|
+
# Assign new depth values as coordinates
|
|
187
|
+
# Create an interpolation function for the current slice
|
|
188
|
+
interp_func = interp1d(
|
|
189
|
+
depth_values[ch],
|
|
190
|
+
data_array[i],
|
|
191
|
+
kind="nearest",
|
|
192
|
+
bounds_error=False,
|
|
193
|
+
fill_value=np.nan,
|
|
194
|
+
)
|
|
195
|
+
# Interpolate the data to the new depth values
|
|
196
|
+
new_data_array[i, :] = interp_func(new_depth_da[:])
|
|
197
|
+
# Create a DataArray for the interpolated data of this channel
|
|
198
|
+
channel_data = xr.DataArray(
|
|
199
|
+
new_data_array,
|
|
200
|
+
coords={"ping_time": time_coord, "range_sample": new_range_sample},
|
|
201
|
+
dims=["ping_time", "range_sample"],
|
|
202
|
+
)
|
|
203
|
+
channel_order_list.append(da["channel"][ch].values)
|
|
204
|
+
interpolated_data_arrays.append(channel_data)
|
|
205
|
+
# Combine interpolated data from all channels
|
|
206
|
+
combined_data = xr.concat(interpolated_data_arrays, dim="channel")
|
|
207
|
+
combined_data = combined_data.assign_coords(channel=("channel", channel_order_list))
|
|
208
|
+
if da.name == "Sv":
|
|
209
|
+
combined_data = linear_to_db(combined_data)
|
|
210
|
+
return combined_data
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def regrid_dataset(dataset: xr.Dataset) -> xr.Dataset:
|
|
214
|
+
"""
|
|
215
|
+
Regrids an xarray Dataset to a new depth profile based on the channel with the lowest resolution.
|
|
216
|
+
|
|
217
|
+
This function identifies the channel with the lowest resolution in the provided dataset and
|
|
218
|
+
uses its depth profile to regrid all relevant variables in the dataset to a new common depth
|
|
219
|
+
profile. It resamples variables that include the 'range_sample' dimension and retains other
|
|
220
|
+
coordinates and attributes unchanged.
|
|
221
|
+
|
|
222
|
+
Parameters:
|
|
223
|
+
- dataset (xr.Dataset): The xarray Dataset to be regridded. It should contain 'echo_range'
|
|
224
|
+
and 'range_sample' among other coordinates and dimensions.
|
|
225
|
+
|
|
226
|
+
Returns:
|
|
227
|
+
- xr.Dataset: A new xarray Dataset with variables resampled to the new common depth profile.
|
|
228
|
+
|
|
229
|
+
Example:
|
|
230
|
+
>> regridded_dataset = regrid_dataset(dataset)
|
|
231
|
+
Expected Output:
|
|
232
|
+
A new xarray Dataset with variables that have been resampled to match the depth profile of
|
|
233
|
+
the channel with the lowest resolution in the original dataset.
|
|
234
|
+
|
|
235
|
+
Note:
|
|
236
|
+
- The function determines the new common depth profile based on the channel with the lowest resolution.
|
|
237
|
+
- Variables with the 'range_sample' dimension are resampled; other variables and coordinates
|
|
238
|
+
are copied as is.
|
|
239
|
+
- The attributes of the original dataset are preserved in the new dataset.
|
|
240
|
+
"""
|
|
241
|
+
new_dataset = xr.Dataset()
|
|
242
|
+
channel, max_depth_idx = find_lowest_resolution_channel(dataset)
|
|
243
|
+
|
|
244
|
+
per_ping_depth = np.nanargmax(dataset["echo_range"].values[channel, :, :], axis=1)
|
|
245
|
+
new_range_sample = np.arange(np.max(per_ping_depth) + 1)
|
|
246
|
+
ping_with_max_depth = np.nanargmax(per_ping_depth)
|
|
247
|
+
bin_depths = dataset["echo_range"].values[:, ping_with_max_depth, :]
|
|
248
|
+
new_bin_depths = dataset["echo_range"].values[channel, ping_with_max_depth, : max_depth_idx + 1]
|
|
249
|
+
for coord_name in dataset.coords:
|
|
250
|
+
if coord_name == "range_sample":
|
|
251
|
+
new_dataset.coords[coord_name] = ("range_sample", new_range_sample)
|
|
252
|
+
else:
|
|
253
|
+
new_dataset.coords[coord_name] = dataset.coords[coord_name]
|
|
254
|
+
impacted_vars = find_impacted_variables(dataset, target_dim="range_sample")
|
|
255
|
+
for var_name, data_array in dataset.data_vars.items():
|
|
256
|
+
if var_name in impacted_vars:
|
|
257
|
+
data_array = resample_xarray(data_array, bin_depths, new_bin_depths, new_range_sample)
|
|
258
|
+
new_dataset[var_name] = data_array
|
|
259
|
+
new_dataset.attrs = dataset.attrs.copy()
|
|
260
|
+
return new_dataset
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""
|
|
2
|
+
target_strength_computation.py
|
|
3
|
+
-------------------------------
|
|
4
|
+
|
|
5
|
+
Module for computing the target strength (TS) from raw data.
|
|
6
|
+
|
|
7
|
+
Supported Sonar Models:
|
|
8
|
+
- EK60
|
|
9
|
+
- AZFP
|
|
10
|
+
- EK80
|
|
11
|
+
|
|
12
|
+
Functions and Classes:
|
|
13
|
+
- `SupportedSonarModelsForTS`: An Enum containing the sonar models supported
|
|
14
|
+
for TS computation.
|
|
15
|
+
- `WaveformMode`: Enum specifying the waveform mode ("CW" or "BB").
|
|
16
|
+
- `EncodeMode`: Enum indicating the encoding mode ("complex" or "power").
|
|
17
|
+
- `ComputeTSParams`: Class to validate and structure the parameters passed
|
|
18
|
+
to the TS computation function.
|
|
19
|
+
- `compute_target_strength`: Main function to calculate TS given an EchoData object
|
|
20
|
+
and other optional parameters. This function is based on the `echopype.calibrate.compute_TS()` function.
|
|
21
|
+
|
|
22
|
+
Usage:
|
|
23
|
+
|
|
24
|
+
To compute TS for a given EchoData object, `ed`, simply call:
|
|
25
|
+
`compute_target_strength(ed)`
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from enum import Enum
|
|
29
|
+
from typing import Any, Optional
|
|
30
|
+
|
|
31
|
+
import echopype as ep
|
|
32
|
+
import xarray as xr
|
|
33
|
+
from echopype.echodata.echodata import EchoData
|
|
34
|
+
from pydantic import BaseModel, ValidationError, field_validator
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class SupportedSonarModelsForTS(str, Enum):
|
|
38
|
+
EK60 = "EK60"
|
|
39
|
+
AZFP = "AZFP"
|
|
40
|
+
EK80 = "EK80"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class WaveformMode(str, Enum):
|
|
44
|
+
CW = "CW"
|
|
45
|
+
BB = "BB"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class EncodeMode(str, Enum):
|
|
49
|
+
COMPLEX = "complex"
|
|
50
|
+
POWER = "power"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class ComputeTSParams(BaseModel):
|
|
54
|
+
echodata: Any
|
|
55
|
+
env_params: Optional[dict] = None
|
|
56
|
+
cal_params: Optional[dict] = None
|
|
57
|
+
waveform_mode: Optional[WaveformMode] = None
|
|
58
|
+
encode_mode: Optional[EncodeMode] = None
|
|
59
|
+
|
|
60
|
+
@field_validator("echodata")
|
|
61
|
+
def check_echodata_type(cls, value):
|
|
62
|
+
if not isinstance(value, EchoData):
|
|
63
|
+
raise ValueError("Invalid type for echodata. Expected an instance of EchoData.")
|
|
64
|
+
return value
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def compute_target_strength(echodata: EchoData, **kwargs) -> xr.Dataset:
|
|
68
|
+
"""
|
|
69
|
+
Compute target strength (TS) from raw data.
|
|
70
|
+
|
|
71
|
+
Parameters:
|
|
72
|
+
- echodata (EchoData): The EchoData object containing
|
|
73
|
+
sonar data for computation.
|
|
74
|
+
- **kwargs: Additional keyword arguments passed to the TS computation.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
- xr.Dataset: A Dataset containing the computed TS values.
|
|
78
|
+
|
|
79
|
+
Notes:
|
|
80
|
+
This function:
|
|
81
|
+
- Validates the `echodata`'s sonar model against supported models.
|
|
82
|
+
- Uses the `ComputeTSParams` pydantic model to validate parameters.
|
|
83
|
+
- Checks if the computed TS is empty.
|
|
84
|
+
- Returns TS only if it is not empty.
|
|
85
|
+
- Is based on the `echopype.calibrate.compute_TS()` function.
|
|
86
|
+
|
|
87
|
+
"""
|
|
88
|
+
# Validate parameters using the pydantic model
|
|
89
|
+
try:
|
|
90
|
+
ComputeTSParams(echodata=echodata, **kwargs)
|
|
91
|
+
except ValidationError as e:
|
|
92
|
+
raise ValueError(str(e))
|
|
93
|
+
# Check if the sonar model is supported
|
|
94
|
+
sonar_model = echodata.sonar_model
|
|
95
|
+
try:
|
|
96
|
+
SupportedSonarModelsForTS(sonar_model)
|
|
97
|
+
except ValueError:
|
|
98
|
+
raise ValueError(
|
|
99
|
+
f"Sonar model '{sonar_model}'\
|
|
100
|
+
is not supported for TS computation.\
|
|
101
|
+
Supported models are \
|
|
102
|
+
{list(SupportedSonarModelsForTS)}."
|
|
103
|
+
)
|
|
104
|
+
# Compute TS
|
|
105
|
+
TS = ep.calibrate.compute_TS(echodata, **kwargs)
|
|
106
|
+
# Check if the computed TS is empty
|
|
107
|
+
if TS["TS"].values.size == 0:
|
|
108
|
+
raise ValueError("Computed TS is empty!")
|
|
109
|
+
return TS
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import xarray as xr
|
|
5
|
+
from haversine import haversine
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def create_location(data: xr.Dataset) -> pd.DataFrame:
|
|
9
|
+
"""
|
|
10
|
+
Given a processed Sv file, enriched with lat/lon, it returns location data:
|
|
11
|
+
lat,lon,time, speed
|
|
12
|
+
|
|
13
|
+
Parameters:
|
|
14
|
+
- data: xr.Dataset
|
|
15
|
+
The raw data to extract information from.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
- pd.DataFrame
|
|
19
|
+
The required metadata.
|
|
20
|
+
"""
|
|
21
|
+
df = data.drop_vars(
|
|
22
|
+
[v for v in data.variables if v not in ["latitude", "longitude"]]
|
|
23
|
+
).to_dataframe()
|
|
24
|
+
df["dt"] = data.coords["ping_time"]
|
|
25
|
+
df.columns = ["lat", "lon", "dt"]
|
|
26
|
+
df["distance"] = [
|
|
27
|
+
haversine(
|
|
28
|
+
(df["lat"].iloc[i], df["lon"].iloc[i]),
|
|
29
|
+
(df["lat"].iloc[i - 1], df["lon"].iloc[i - 1]),
|
|
30
|
+
unit="nmi",
|
|
31
|
+
)
|
|
32
|
+
if i > 0
|
|
33
|
+
else 0
|
|
34
|
+
for i in range(len(df))
|
|
35
|
+
]
|
|
36
|
+
df["time_interval"] = df["dt"] - df["dt"].shift()
|
|
37
|
+
df["knt"] = (df["distance"] / df["time_interval"].dt.total_seconds()) * 3600
|
|
38
|
+
df = df[["lat", "lon", "dt", "knt"]]
|
|
39
|
+
|
|
40
|
+
return df
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def create_Sv(data: xr.Dataset, channel: str) -> pd.DataFrame:
|
|
44
|
+
"""
|
|
45
|
+
Given a processed Sv file, enriched with lat/lon, it returns Sv data
|
|
46
|
+
|
|
47
|
+
Parameters:
|
|
48
|
+
- data: xr.Dataset
|
|
49
|
+
The raw data to extract information from.
|
|
50
|
+
- channel: str
|
|
51
|
+
The channel to use
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
- pd.DataFrame
|
|
55
|
+
The required data.
|
|
56
|
+
"""
|
|
57
|
+
data = data.copy(deep=True)
|
|
58
|
+
data["ping_time"] = range(0, len(data.ping_time))
|
|
59
|
+
data["range_sample"] = data["range_sample"] / 2
|
|
60
|
+
df = data.sel(channel=channel)["Sv"].to_dataframe()
|
|
61
|
+
df = df["Sv"].unstack(level="ping_time")
|
|
62
|
+
return df
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def export_Sv_csv(data: xr.Dataset, folder: str, root_name: str):
|
|
66
|
+
"""
|
|
67
|
+
Given a Sv file, a folder to write the outputs into, and a name pattern for the files,
|
|
68
|
+
it extracts and exports to CSV the location data and the Sv data
|
|
69
|
+
|
|
70
|
+
Parameters:
|
|
71
|
+
- data: xr.Dataset
|
|
72
|
+
The Sv to extract this information from.
|
|
73
|
+
- folder: str
|
|
74
|
+
The folder name to use.
|
|
75
|
+
- root_name: str
|
|
76
|
+
The root name to be used in the file patterns.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
- None
|
|
80
|
+
"""
|
|
81
|
+
location = create_location(data)
|
|
82
|
+
Sv = create_Sv(data, data["channel"][1]) # for R Shiny compat reasons
|
|
83
|
+
location_filename = os.path.join(folder, root_name + "_GPS.csv")
|
|
84
|
+
Sv_filename = os.path.join(folder, root_name + "_Sv_38000.0.csv")
|
|
85
|
+
try:
|
|
86
|
+
location.to_csv(location_filename, index=False)
|
|
87
|
+
Sv.to_csv(Sv_filename)
|
|
88
|
+
except Exception as e:
|
|
89
|
+
raise ValueError(str(e))
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import xarray as xr
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def create_metadata(data: xr.Dataset) -> pd.DataFrame:
|
|
8
|
+
"""
|
|
9
|
+
Given a raw echodata file, it extracts the metadata we require.
|
|
10
|
+
|
|
11
|
+
Parameters:
|
|
12
|
+
- data: xr.Dataset
|
|
13
|
+
The raw data to extract information from.
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
- pd.DataFrame
|
|
17
|
+
The required metadata.
|
|
18
|
+
"""
|
|
19
|
+
bg_1 = data["Sonar/Beam_group1"].isel(ping_time=0).isel(range_sample=0)
|
|
20
|
+
bg_1.set_coords("frequency_nominal")
|
|
21
|
+
variable_list = [
|
|
22
|
+
"frequency_nominal",
|
|
23
|
+
"beamwidth_twoway_alongship",
|
|
24
|
+
"beamwidth_twoway_athwartship",
|
|
25
|
+
"angle_offset_alongship",
|
|
26
|
+
"angle_offset_athwartship",
|
|
27
|
+
"angle_sensitivity_alongship",
|
|
28
|
+
"angle_sensitivity_athwartship",
|
|
29
|
+
"equivalent_beam_angle",
|
|
30
|
+
"gpt_software_version",
|
|
31
|
+
"transmit_bandwidth",
|
|
32
|
+
"transmit_duration_nominal",
|
|
33
|
+
"transmit_power",
|
|
34
|
+
"sample_interval",
|
|
35
|
+
]
|
|
36
|
+
df = (
|
|
37
|
+
bg_1.drop_vars([v for v in bg_1.variables if v not in variable_list])
|
|
38
|
+
.to_dataframe()
|
|
39
|
+
.transpose()
|
|
40
|
+
)
|
|
41
|
+
df.columns = df.loc["frequency_nominal"]
|
|
42
|
+
df = df.loc[[r for r in df.index if r != "frequency_nominal"]]
|
|
43
|
+
df["vars"] = df.index
|
|
44
|
+
df = df.melt(id_vars="vars")
|
|
45
|
+
df.columns = ["Name", "Frequency", "Value"]
|
|
46
|
+
return df
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def create_calibration(data: xr.Dataset) -> pd.DataFrame:
|
|
50
|
+
"""
|
|
51
|
+
Given a raw echodata file, it extracts the calibration data we require.
|
|
52
|
+
|
|
53
|
+
Parameters:
|
|
54
|
+
- data: xr.Dataset
|
|
55
|
+
The raw data to extract this information from.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
- pd.DataFrame
|
|
59
|
+
The required calibration data.
|
|
60
|
+
"""
|
|
61
|
+
df = data["Vendor_specific"].to_dataframe()
|
|
62
|
+
columns = ["frequency_nominal", "pulse_length", "gain_correction", "sa_correction"]
|
|
63
|
+
df = df[columns]
|
|
64
|
+
df.columns = ["Frequency", "Pulse_length", "Gain", "Sa"]
|
|
65
|
+
return df
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def export_raw_csv(data: xr.Dataset, folder: str, root_name: str):
|
|
69
|
+
"""
|
|
70
|
+
Given a raw echodata file, a folder to write the outputs into, and a name pattern for the files,
|
|
71
|
+
it extracts and exports to CSV the calibration data and the metadata.
|
|
72
|
+
|
|
73
|
+
Parameters:
|
|
74
|
+
- data: xr.Dataset
|
|
75
|
+
The raw data to extract this information from.
|
|
76
|
+
- folder: str
|
|
77
|
+
The folder name to use.
|
|
78
|
+
- root_name: str
|
|
79
|
+
The root name to be used in the file patterns.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
- None
|
|
83
|
+
"""
|
|
84
|
+
metadata = create_metadata(data)
|
|
85
|
+
calibration = create_calibration(data)
|
|
86
|
+
metadata_filename = os.path.join(folder, root_name + "_metadata.csv")
|
|
87
|
+
calibration_filename = os.path.join(folder, root_name + "_calibration.csv")
|
|
88
|
+
try:
|
|
89
|
+
metadata.to_csv(metadata_filename, index=False)
|
|
90
|
+
calibration.to_csv(calibration_filename, index=False)
|
|
91
|
+
except Exception as e:
|
|
92
|
+
raise ValueError(str(e))
|