Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions echopype/consolidate/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import pathlib
import sys
from numbers import Number
from pathlib import Path
from typing import Optional, Union
Expand Down Expand Up @@ -224,8 +225,12 @@ def add_depth(
used_platform_angles = use_platform_angles and not tilt
used_beam_angles = use_beam_angles and not tilt
history_attr = (
f"{datetime.datetime.utcnow()} +00:00. depth` calculated using:"
f" Sv `echo_range`"
f"{datetime.datetime.utcnow()}+00:00. `depth` calculated using:"
if sys.version_info < (3, 11, 0)
else f"{datetime.datetime.now(datetime.UTC)}. `depth` calculated using:"
)
history_attr = (
history_attr + f" Sv `echo_range`"
f"{', Echodata `Platform` Vertical Offsets' if (used_platform_vertical_offsets) else ''}"
f"{', Echodata `Platform` Angles' if (used_platform_angles) else ''}"
f"{', Echodata `%s` Angles' % (beam_group_name) if (used_beam_angles) else ''}"
Expand Down Expand Up @@ -320,8 +325,12 @@ def add_location(
# Most attributes are attached automatically via interpolation
# here we add the history
history_attr = (
f"{datetime.datetime.utcnow()} +00:00. "
f"Interpolated or propagated from Platform {lat_name}/{lon_name}." # noqa
f"{datetime.datetime.utcnow()}+00:00. `depth` calculated using:"
if sys.version_info < (3, 11, 0)
else f"{datetime.datetime.now(datetime.UTC)}. `depth` calculated using:"
)
history_attr = (
history_attr + f"Interpolated or propagated from Platform {lat_name}/{lon_name}." # noqa
)
for da_name in POSITION_VARIABLES:
interp_ds[da_name] = interp_ds[da_name].assign_attrs({"history": history_attr})
Expand Down Expand Up @@ -515,8 +524,13 @@ def add_splitbeam_angle(

# Add history attribute
history_attr = (
f"{datetime.datetime.utcnow()} +00:00. "
"Calculated using data stored in the Beam groups of the echodata object." # noqa
f"{datetime.datetime.utcnow()}+00:00. `depth` calculated using:"
if sys.version_info < (3, 11, 0)
else f"{datetime.datetime.now(datetime.UTC)}. `depth` calculated using:"
)
history_attr = (
history_attr
+ "Calculated using data stored in the Beam groups of the echodata object." # noqa
)
for da_name in ["angle_alongship", "angle_athwartship"]:
source_Sv[da_name] = source_Sv[da_name].assign_attrs({"history": history_attr})
Expand Down
15 changes: 11 additions & 4 deletions echopype/convert/parse_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import os
import sys
from collections import defaultdict
from datetime import datetime as dt
from typing import Any, Dict, Literal, Optional, Tuple

import dask
Expand Down Expand Up @@ -72,9 +73,15 @@ def __init__(self, file, bot_file, idx_file, storage_options, sonar_model):
self.CON1_datagram = None # Holds the ME70 CON1 datagram

def _print_status(self):
time = dt.utcfromtimestamp(self.config_datagram["timestamp"].tolist() / 1e9).strftime(
"%Y-%b-%d %H:%M:%S"
)
if sys.version_info < (3, 11, 0):
time = datetime.datetime.utcfromtimestamp(
self.config_datagram["timestamp"].tolist() / 1e9
).strftime("%Y-%b-%d %H:%M:%S")
else:
time = datetime.datetime.fromtimestamp(
self.config_datagram["timestamp"].tolist() / 1e9, datetime.UTC
).strftime("%Y-%b-%d %H:%M:%S")

logger.info(
f"parsing file {os.path.basename(self.source_file)}, " f"time of first ping: {time}"
)
Expand Down
8 changes: 7 additions & 1 deletion echopype/echodata/echodata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import re
import sys
import warnings
from html import escape
from pathlib import Path
Expand Down Expand Up @@ -412,7 +413,12 @@ def update_platform(
extra_platform_data = extra_platform_data.swap_dims({obs_dim: time_dim})

# History attribute to be included in each updated variable
history_attr = f"{datetime.datetime.utcnow()} +00:00. Added from external platform data"
history_attr = (
f"{datetime.datetime.utcnow()}+00:00. `depth` calculated using:"
if sys.version_info < (3, 11, 0)
else f"{datetime.datetime.now(datetime.UTC)}. `depth` calculated using:"
)
history_attr = f"{history_attr}. Added from external platform data"
if extra_platform_data_file_name:
history_attr += ", from file " + extra_platform_data_file_name

Expand Down
16 changes: 14 additions & 2 deletions echopype/mask/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import operator as op
import pathlib
import sys
from typing import List, Optional, Union

import dask
Expand Down Expand Up @@ -252,7 +253,12 @@ def _variable_prov_attrs(
],
}
# Add history attribute
history_attr = f"{datetime.datetime.utcnow()} +00:00. " "Created masked Sv dataarray." # noqa
history_attr = (
f"{datetime.datetime.utcnow()}+00:00. `depth` calculated using:"
if sys.version_info < (3, 11, 0)
else f"{datetime.datetime.now(datetime.UTC)}. `depth` calculated using:"
)
history_attr = f"{history_attr}. " "Created masked Sv dataarray." # noqa
attrs = {**attrs, **{"history": history_attr}}

# Add attributes from the mask DataArray, if present
Expand Down Expand Up @@ -620,9 +626,15 @@ def _create_mask(lhs: np.ndarray, diff: float) -> np.ndarray:
coords=template.coords,
)

history_attr = (
f"{datetime.datetime.utcnow()}+00:00. `depth` calculated using:"
if sys.version_info < (3, 11, 0)
else f"{datetime.datetime.now(datetime.UTC)}. `depth` calculated using:"
)

xr_dataarray_attrs = {
"mask_type": "frequency differencing",
"history": f"{datetime.datetime.utcnow()} +00:00. "
"history": f"{history_attr}. "
"Mask created by mask.frequency_differencing. "
f"Operation: Sv['{chanA}'] - Sv['{chanB}'] {operator} {diff}",
}
Expand Down
2 changes: 1 addition & 1 deletion echopype/tests/commongrid/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def ds_Sv_echo_range_regular(regular_data_params, random_number_generator):
@pytest.fixture
def latlon_history_attr():
return (
"2023-08-31 12:00:00.000000 +00:00. "
"2023-08-31 12:00:00.000000+00:00. "
"Interpolated or propagated from Platform latitude/longitude." # noqa
)

Expand Down
20 changes: 10 additions & 10 deletions echopype/tests/consolidate/test_add_depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ def test_add_depth_without_echodata():

# Check history attribute
history_attribute = ds_Sv_depth["depth"].attrs["history"]
history_attribute_without_time = history_attribute[33:]
assert history_attribute_without_time == ". depth` calculated using: Sv `echo_range`."
history_attribute_without_time = history_attribute[32:]
assert history_attribute_without_time == ". `depth` calculated using: Sv `echo_range`."


@pytest.mark.integration
Expand Down Expand Up @@ -384,9 +384,9 @@ def test_add_depth_EK_with_platform_vertical_offsets(file, sonar_model, compute_

# Check history attribute
history_attribute = ds_Sv_with_depth["depth"].attrs["history"]
history_attribute_without_time = history_attribute[33:]
history_attribute_without_time = history_attribute[32:]
assert history_attribute_without_time == (
". depth` calculated using: Sv `echo_range`, Echodata `Platform` Vertical Offsets."
". `depth` calculated using: Sv `echo_range`, Echodata `Platform` Vertical Offsets."
)

# Compute transducer depth
Expand Down Expand Up @@ -439,9 +439,9 @@ def test_add_depth_EK_with_platform_angles(file, sonar_model, compute_Sv_kwargs)

# Check history attribute
history_attribute = ds_Sv_with_depth["depth"].attrs["history"]
history_attribute_without_time = history_attribute[33:]
history_attribute_without_time = history_attribute[32:]
assert history_attribute_without_time == (
". depth` calculated using: Sv `echo_range`, Echodata `Platform` Angles."
". `depth` calculated using: Sv `echo_range`, Echodata `Platform` Angles."
)

# Compute transducer depth
Expand Down Expand Up @@ -494,9 +494,9 @@ def test_add_depth_EK_with_beam_angles(file, sonar_model, compute_Sv_kwargs):

# Check history attribute
history_attribute = ds_Sv_with_depth["depth"].attrs["history"]
history_attribute_without_time = history_attribute[33:]
history_attribute_without_time = history_attribute[32:]
assert history_attribute_without_time == (
". depth` calculated using: Sv `echo_range`, Echodata `Beam_group1` Angles."
". `depth` calculated using: Sv `echo_range`, Echodata `Beam_group1` Angles."
)

# Compute echo range scaling values
Expand Down Expand Up @@ -543,9 +543,9 @@ def test_add_depth_EK_with_beam_angles_with_different_beam_groups(

# Check history attribute
history_attribute = ds_Sv["depth"].attrs["history"]
history_attribute_without_time = history_attribute[33:]
history_attribute_without_time = history_attribute[32:]
assert history_attribute_without_time == (
f". depth` calculated using: Sv `echo_range`, Echodata `{expected_beam_group_name}` Angles."
f". `depth` calculated using: Sv `echo_range`, Echodata `{expected_beam_group_name}` Angles."
)


Expand Down
10 changes: 7 additions & 3 deletions echopype/utils/prov.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import functools
import re
from datetime import datetime as dt
import sys
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union

Expand Down Expand Up @@ -29,11 +30,14 @@ def echopype_prov_attrs(process_type: ProcessType) -> Dict[str, str]:
process_type : ProcessType
Echopype process function type
"""

if sys.version_info < (3, 11, 0):
utc_now = datetime.datetime.utcnow().isoformat(timespec="seconds") + "+00:00"
else:
utc_now = datetime.datetime.now(datetime.UTC).isoformat(timespec="seconds")
prov_dict = {
f"{process_type}_software_name": "echopype",
f"{process_type}_software_version": ECHOPYPE_VERSION,
f"{process_type}_time": dt.utcnow().isoformat(timespec="seconds") + "Z", # use UTC time
f"{process_type}_time": utc_now,
}

return prov_dict
Expand Down
Loading