Skip to content
Merged
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
23 changes: 22 additions & 1 deletion src/glide/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import functools
import inspect
import logging
from datetime import datetime, timezone
from importlib.metadata import version
from pathlib import Path

Expand Down Expand Up @@ -41,6 +42,13 @@ def wrapper(*args, **kwargs):
return wrapper


def generate_ioos_filename(ds, glider_name: str) -> str:
first_time = float(ds["time"].values[0])
dt = datetime.fromtimestamp(first_time, tz=timezone.utc)
timestamp = dt.strftime("%Y%m%dT%H%M%SZ")
return f"{glider_name}_{timestamp}.nc"


# Commonly used argument annotations
_config_annotation = Annotated[
str | None,
Expand Down Expand Up @@ -129,6 +137,14 @@ def l2(
"output.",
),
] = False,
glider_name: Annotated[
str | None,
typer.Option(
"--glider",
"-g",
help="Glider name for IOOS-style filename when output is a directory.",
),
] = None,
) -> None:
"""
Generate L2 data from L1 data.
Expand Down Expand Up @@ -159,7 +175,12 @@ def l2(

out.encoding["unlimited_dims"] = {}

out.to_netcdf(out_file)
out_path = Path(out_file)
if out_path.is_dir():
name = glider_name or conf["globals"]["trajectory"]["name"].split("_")[-1]
out_path = out_path / generate_ioos_filename(out, name)

out.to_netcdf(out_path)

if riot_csv:
from .riot_csv_writer import write_riot_csv
Expand Down
38 changes: 38 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,44 @@ def test_l2() -> None:
assert result.exit_code == 0


def test_l2_directory_output() -> None:
import re
import tempfile
from datetime import datetime, timezone

data_dir = Path(str(resources.files("tests").joinpath("data")))
sbd_files = sorted(data_dir.glob("*.sbd.csv"))

with tempfile.TemporaryDirectory() as tmpdir:
for sbd_file in sbd_files:
tbd_file = sbd_file.with_suffix("").with_suffix(".tbd.csv")
if not tbd_file.exists():
continue

glider = sbd_file.name.split("-")[0].split(".")[0]
result = runner.invoke(
app, ["l2", str(sbd_file), str(tbd_file), "-o", tmpdir, "-g", glider]
)
assert result.exit_code == 0, f"Failed for {sbd_file.name}: {result.output}"

nc_files = list(Path(tmpdir).glob("*.nc"))
assert len(nc_files) == len(sbd_files)

for nc_file in nc_files:
assert re.match(r"\w+_\d{8}T\d{6}Z\.nc", nc_file.name)

ds = xr.open_dataset(nc_file)
first_time = ds["time"].values[0]
ds.close()

dt = np.datetime64(first_time, "s").astype("datetime64[s]").astype(datetime)
dt = dt.replace(tzinfo=timezone.utc)
expected_ts = dt.strftime("%Y%m%dT%H%M%SZ")
assert expected_ts in nc_file.name, (
f"Timestamp mismatch: expected {expected_ts} in {nc_file.name}"
)


def test_l3() -> None:
l2_file = str(resources.files("tests").joinpath("data/slocum.l2.nc"))
out_file = str(resources.files("tests").joinpath("data/slocum.l3.nc"))
Expand Down
Loading