Skip to content

Commit

Permalink
Fix to_exchange and to_coards failing on variables with second units
Browse files Browse the repository at this point in the history
  • Loading branch information
DocOtak committed Mar 21, 2024
1 parent 7e355c6 commit eea6001
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v1.0.2.7 (2024-??-??)
=====================
* (Bug) fix to_exchange accessor failing for variables with seconds and the unit
* (Bug) fix to_coards accessor failing for variables with seconds and the unit
* Add status-cf-derived command that tests all all public CF files at CCHDO going from netCDF to every other supported format

v1.0.2.6 (2024-03-18)
=====================
* Support for duplicate parameters
Expand Down
6 changes: 5 additions & 1 deletion cchdo/hydro/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def _make_ctd_headers(self, params) -> list[str]:
*[f"{key} = {value}" for key, value in headers.items()],
]

def _make_data_block(self, params) -> list[str]:
def _make_data_block(self, params: dict[WHPName, xr.DataArray]) -> list[str]:
# TODO N_PROF is guaranteed
valid_levels = params[WHPNames["SAMPNO"]] != ""
data_block = []
Expand All @@ -584,6 +584,10 @@ def _make_data_block(self, params) -> list[str]:
.tolist()
)
else:
if da.dtype.char == "m":
nat_mask = np.isnat(da)
da = da.astype("timedelta64[s]").astype("float64")
da[nat_mask] = np.nan
data = np.nditer(da[valid_levels], flags=["refs_ok"])
numeric_precision_override = self.cchdo_c_format_precision(
da.attrs.get("C_format", "")
Expand Down
4 changes: 4 additions & 0 deletions cchdo/hydro/legacy/coards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def get_dataarrays(ds: xr.Dataset):
data = variable.dt.strftime("%Y%m%d").astype(float).to_numpy()
elif parameter_name == "BTL_TIME":
data = variable.dt.strftime("%H%M").astype(float).to_numpy()
elif parameter_name == "CTDETIME":
nat_mask = np.isnat(variable)
data = variable.astype("timedelta64[s]").astype("float64").to_numpy()
data[nat_mask] = np.nan
else:
data = variable.to_numpy()

Expand Down
57 changes: 57 additions & 0 deletions cchdo/hydro/tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from importlib.resources import read_text

import pytest
import xarray as xr
from xarray.testing import assert_identical

from ..accessors import CCHDOAccessor
Expand Down Expand Up @@ -90,3 +91,59 @@ def test_exchange_ctd_round_trip():
# the magic slice removes the stamp and the newline with #
rt = read_exchange(ds.cchdo.to_exchange()[22:])
assert_identical(ds, rt)


def test_nc_serialize_all_ctd(tmp_path):
"""A crash was discovered when the ctd elapsed time param was present, and was seralized to disk then read back in"""
test_data = b"""CTD,test
# some comment
NUMBER_HEADERS = 8
EXPOCODE = TEST
STNNBR = 1
CASTNO = 1
DATE = 20200101
TIME = 0000
LATITUDE = 0
LONGITUDE = 0
CTDPRS
DBAR
0
END_DATA
"""
ds = read_exchange(test_data)
nc = tmp_path / "test.nc"
ds.to_netcdf(nc)
ds = xr.load_dataset(nc)
# the magic slice removes the stamp and the newline with #
ds.cchdo.to_exchange()
ds.cchdo.to_coards()
ds.cchdo.to_woce()
ds.cchdo.to_sum()


def test_nc_serialize_all_ctdetime(tmp_path):
"""A crash was discovered when the ctd elapsed time param was present, and was seralized to disk then read back in"""
test_data = b"""CTD,test
# some comment
NUMBER_HEADERS = 8
EXPOCODE = TEST
STNNBR = 1
CASTNO = 1
DATE = 20200101
TIME = 0000
LATITUDE = 0
LONGITUDE = 0
CTDPRS,CTDETIME
DBAR,SECONDS
0,-999
END_DATA
"""
ds = read_exchange(test_data)
nc = tmp_path / "test.nc"
ds.to_netcdf(nc)
ds = xr.load_dataset(nc)
# the magic slice removes the stamp and the newline with #
ds.cchdo.to_exchange()
ds.cchdo.to_coards()
ds.cchdo.to_woce()
ds.cchdo.to_sum()

0 comments on commit eea6001

Please sign in to comment.