Skip to content

Commit

Permalink
drop seabird code from process module
Browse files Browse the repository at this point in the history
  • Loading branch information
JessyBarrette committed Nov 16, 2023
1 parent 5fa4c13 commit 9a43694
Showing 1 changed file with 1 addition and 100 deletions.
101 changes: 1 addition & 100 deletions ocean_data_parser/process/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,110 +6,11 @@
import pandas as pd
import xarray as xr

from ocean_data_parser.parsers import seabird, utils
from ocean_data_parser.parsers import utils

logger = logging.getLogger(__name__)


def generate_dataset_file_name(ds: xr.Dataset, suffix: str = "") -> str:
return (
"_".join(
[
"Hakai",
f"{ds.attrs['instrument_manufacturer']}-{ds.attrs['instrument_type']}-{ds.attrs['instrument_sub_type']}",
f"SN{ds.attrs['serial_number']}",
f"{ds.attrs['region']}-{ds.attrs['site']}",
f"{pd.to_datetime(ds['time'].min().values):%y%m%d}-{pd.to_datetime(ds['time'].max().values):%y%m%d}",
]
)
+ suffix
)


def load_cnv(file: str):
"""Load seabird cnv and apply conversion
Args:
file (str): _description_
Returns:
_type_: _description_
"""
ds = seabird.cnv(file)
ds["time"] = (
ds["timeK"].dims,
pd.to_datetime(ds["timeK"], origin="2000-01-01", unit="s"),
)
return ds.swap_dims({"index": "time"}).drop("index")


def match_metadata(file, log):
file = Path(file)
ds = load_cnv(file)

# Append lat/lon/station/file_id variables
# Find any matching records in log
is_matching_sn = log["Serial Number"] == int(ds.attrs["temperature_sn"])
is_within_deploymen_time = (
log["Deployment Time"].dt.tz_localize(None) < ds["time"].mean().values
) & (
(log["Retrieval Time"].dt.tz_localize(None) > ds["time"].mean().values)
| (log["Retrieval Time"].isna())
)
selected_log_record = log.loc[is_matching_sn & is_within_deploymen_time]

if len(selected_log_record) != 1:
raise RuntimeError("Failed to match an appropriate record")
elif selected_log_record.empty:
raise RuntimeError("Failed to match record to any deployments")
return selected_log_record.iloc[0]


def get_L0_file(
file: str, selected_log_record: pd.Series = None, ds: xr.Dataset = None
):
# Load raw file
file = Path(file)
if not ds:
ds = load_cnv(file)

# Try to match metadata if not given
if selected_log_record is None:
selected_log_record = match_metadata

# Add Log metadata to record
ds["file_id"] = file.name
ds["instrument_model"] = ds.attrs["instrument_type"].strip()
ds["instrument_serialnumber"] = f"037{ds.attrs['temperature_sn']}"
ds["station"] = selected_log_record["Site"]
ds["latitude"] = selected_log_record["Target Latitude (dd°mm.mmm'N) "]
ds["latitude"].attrs = {
"long_name": "Latitude",
"standard_name": "latitude",
"units": "degrees_north",
}
ds["longitude"] = selected_log_record["Target Longitude (dd°mm.mmm'W)"]
ds["longitude"].attrs = {
"long_name": "Longitude",
"standard_name": "longitude",
"units": "degrees_east",
}

# Standardize units to match previous data
ds["sal00"].attrs["units"] = "1e-3"
ds["sbeopoxMLPerL"].attrs["units"] = "mL/L"
ds["prdM"].attrs["units"] = "dbar"

# Dump all log as global attributes
ds.attrs.update(
{
key.lower().replace(" ", "_").split("(")[0]: value
for key, value in selected_log_record.to_dict().items()
}
)
return ds


@xr.register_dataset_accessor("process")
class Processing:
def __init__(
Expand Down

0 comments on commit 9a43694

Please sign in to comment.