Skip to content

Commit

Permalink
ENH: switch to add optional how attributes (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
egouden authored Mar 9, 2023
1 parent 7bd8328 commit 02b2d92
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Development Version

* ENH: switch to add optional how attributes in ODIM format writer ({pull}`97`) by [@egouden](https://github.com/egouden)
* FIX: add keyword argument for mandatory source attribute in ODIM format writer ({pull}`96`) by [@egouden](https://github.com/egouden)
* FIX: check for dim0 if not given, only swap_dims if needed ({issue}`92`), ({pull}`94`) by [@kmuehlbauer](https://github.com/kmuehlbauer)
* FIX+ENH: need array copy before overwriting and make compression available in to_odim ({pull}`95`) by [@kmuehlbauer](https://github.com/kmuehlbauer)
Expand Down
44 changes: 43 additions & 1 deletion tests/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,52 @@ def test_odim_roundtrip(odim_file2, compression, compression_opts):
)
dtree2 = open_odim_datatree(outfile, reindex_angle=False)
for d0, d1 in zip(dtree.groups, dtree2.groups):
print(d0, d1)
xr.testing.assert_equal(dtree[d0].ds, dtree2[d1].ds)


def test_odim_optional_how(odim_file2):
dtree = open_odim_datatree(odim_file2)
outfile = tempfile.NamedTemporaryFile(mode="w+b").name
xradar.io.to_odim(
dtree,
outfile,
source="WMO:01104,NOD:norst",
optional_how=True,
)
ds = h5py.File(outfile)

for i in range(1, 6):
ds_how = ds["dataset%s" % (i)]["how"].attrs
assert "scan_index" in ds_how
assert "scan_count" in ds_how
assert "startazA" in ds_how
assert "stopazA" in ds_how
assert "startazT" in ds_how
assert "startazT" in ds_how
assert "startelA" in ds_how
assert "stopelA" in ds_how

outfile = tempfile.NamedTemporaryFile(mode="w+b").name
xradar.io.to_odim(
dtree,
outfile,
source="WMO:01104,NOD:norst",
optional_how=False,
)
ds = h5py.File(outfile)

for i in range(1, 6):
ds_how = ds["dataset%s" % (i)]["how"].attrs
assert "scan_index" not in ds_how
assert "scan_count" not in ds_how
assert "startazA" not in ds_how
assert "stopazA" not in ds_how
assert "startazT" not in ds_how
assert "startazT" not in ds_how
assert "startelA" not in ds_how
assert "stopelA" not in ds_how


def test_write_odim_source(rainbow_file2):
dtree = open_rainbow_datatree(rainbow_file2)
outfile = tempfile.NamedTemporaryFile(mode="w+b").name
Expand Down
35 changes: 24 additions & 11 deletions xradar/io/export/odim.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ def _write_odim_dataspace(source, destination, compression, compression_opts):
ds.attrs.create("IMAGE_VERSION", version, dtype=H5T_C_S1_VER)


def to_odim(dtree, filename, source=None, compression="gzip", compression_opts=6):
def to_odim(
dtree,
filename,
source=None,
optional_how=False,
compression="gzip",
compression_opts=6,
):
"""Save DataTree to ODIM_H5/V2_2 compliant file.
Parameters
Expand All @@ -147,6 +154,8 @@ def to_odim(dtree, filename, source=None, compression="gzip", compression_opts=6
-----------------
source : str
mandatory radar identifier (see ODIM documentation)
optional_how : boolean
True to include optional how attributes, defaults to False
compression : str
Compression filter name, defaults to "gzip".
compression_opts : compression strategy
Expand Down Expand Up @@ -265,16 +274,20 @@ def to_odim(dtree, filename, source=None, compression="gzip", compression_opts=6

# ODIM_H5 datasetN numbers are 1-based
sweep_number = ds.sweep_number + 1
ds_how = {
"scan_index": sweep_number,
"scan_count": len(grps),
"startazT": tout - difft,
"stopazT": tout + difft,
"startazA": azout - diffa,
"stopazA": azout + diffa,
"startelA": elout - diffe,
"stopelA": elout + diffe,
}

ds_how = {}
if optional_how:
optional = {
"scan_index": sweep_number,
"scan_count": len(grps),
"startazT": tout - difft,
"stopazT": tout + difft,
"startazA": azout - diffa,
"stopazA": azout + diffa,
"startelA": elout - diffe,
"stopelA": elout + diffe,
}
ds_how.update(optional)
_write_odim(ds_how, h5_ds_how)

# write moments
Expand Down

0 comments on commit 02b2d92

Please sign in to comment.