Skip to content

Commit

Permalink
add method to apply_function and start moving away from eval
Browse files Browse the repository at this point in the history
related to #94
  • Loading branch information
JessyBarrette committed Feb 14, 2025
1 parent 988b60c commit c23a4e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ocean_data_parser/parsers/amundsen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from gsw import z_from_p
from loguru import logger

from ocean_data_parser.parsers.utils import standardize_dataset
from ocean_data_parser.parsers.utils import standardize_dataset, apply_function
from ocean_data_parser.vocabularies.load import amundsen_vocabulary

string_attributes = ["Cruise_Number", "Cruise_Name", "Station"]

Check failure on line 21 in ocean_data_parser/parsers/amundsen.py

View workflow job for this annotation

GitHub Actions / testing

Ruff (I001)

ocean_data_parser/parsers/amundsen.py:9:1: I001 Import block is un-sorted or un-formatted
Expand Down Expand Up @@ -344,6 +344,8 @@ def int_format(
for key, value in item.items()
if key not in ["accepted_units", "rename", "file_type"]
}
if "apply_function" in ds[var].attrs:
ds = apply_function(ds, var)
break
else:
logger.warning(
Expand Down
32 changes: 32 additions & 0 deletions ocean_data_parser/parsers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,38 @@ def convert_datetime_str(time_str: str, **to_datetime_kwargs) -> pd.Timestamp:
return pd.to_datetime(time_str, **to_datetime_kwargs)


def apply_function(ds: xr.Dataset, variable: str) -> xr.Dataset:
"""Apply a function to a variable based on the apply_function attribute."""

def _append_to_history(comment, timestamp: pd.Timestamp = None):
if "history" not in ds.attrs:
ds.attrs["history"] = ""
ds.attrs["history"] += (
f"\n{timestamp or pd.Timestamp.now(tz='UTC')} {comment}\n"
)
ds.attrs["history"] = ds.attrs["history"].strip()

apply_function = ds[variable].attrs.get("apply_function")
if not apply_function:
return ds

attributes = ds[variable].attrs
if apply_function == "x*44.661":
new_variable = ds[variable] * 44.661
_append_to_history(
f"Convert variable {variable} mL/L to uM units with: {apply_function}"
)
else:
logger.warning("Unknown apply function: %s", apply_function)
return ds

# Integrate modified variable
new_variable.attrs = attributes
new_variable.attrs.pop("apply_function")
ds[variable] = new_variable
return ds


global_attributes_order = [
"organization",
"institution",
Expand Down

0 comments on commit c23a4e3

Please sign in to comment.