Skip to content

Commit

Permalink
Fix COARDS writer crash with empty string comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DocOtak committed Apr 22, 2024
1 parent 7c27bee commit a2532d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.0.2.9 (2024-04-??)
=====================
* (Bug) Fix crash in the COARDS writer when the comments are just an empty string

v1.0.2.8 (2024-03-22)
=====================
* netCDF4 is now requried as part of the selftest option when installing
Expand Down
2 changes: 2 additions & 0 deletions cchdo/hydro/legacy/coards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def get_coards_global_attributes(ds: xr.Dataset, *, profile_type: Literal["B", "
)

_comments = ds.attrs.get("comments", "").splitlines()
if len(_comments) == 0:
_comments = [""]
og_header = "\n".join([_comments[0], *[f"#{line}" for line in _comments[1:]], ""])
attrs["ORIGINAL_HEADER"] = og_header

Expand Down
14 changes: 14 additions & 0 deletions cchdo/hydro/tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def test_gen_fname_machinery(
)


def test_coards_no_comments():
# note that the differet bottle and sampno are intentional
test_data = b"""BOTTLE,test
# some comment
EXPOCODE,STNNBR,CASTNO,SAMPNO,BTLNBR,DATE,TIME,LATITUDE,LONGITUDE,CTDPRS
,,,,,,,,,DBAR
TEST ,1 , 1,1 ,2 ,20200101,0000, 0, 0, 0
END_DATA
"""
ds = read_exchange(test_data)
ds.attrs["comments"] = ""
ds.cchdo.to_coards()


def test_exchange_bottle_round_trip():
# note that the differet bottle and sampno are intentional
test_data = b"""BOTTLE,test
Expand Down

0 comments on commit a2532d0

Please sign in to comment.