Skip to content

Commit

Permalink
Merge branch 'master' into ignore_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
iainrussell authored Jun 21, 2024
2 parents e325b4f + b06ab31 commit 0fd4cd9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cfgrib/xarray_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def open_dataset(path, **kwargs):
def merge_datasets(datasets, **kwargs):
# type: (T.Sequence[xr.Dataset], T.Any) -> T.List[xr.Dataset]
merged = [] # type: T.List[xr.Dataset]
first = [] # type: T.List[xr.Dataset]
for ds in datasets:
ds.attrs.pop("history", None)
for i, o in enumerate(merged):
Expand All @@ -55,6 +56,19 @@ def merge_datasets(datasets, **kwargs):
pass
else:
merged.append(ds)
first.append(ds)

# Add the important coordinate encoding fields from the first found, to the merged:
preserve_encoding_fields = ["source", "units", "calendar", "dtype"]
for i, o in enumerate(first):
for var in o.coords:
out_encoding = {
key: o[var].encoding[key]
for key in preserve_encoding_fields
if key in o[var].encoding
}
merged[i][var].encoding.update(out_encoding)

return merged


Expand Down
13 changes: 13 additions & 0 deletions tests/test_40_xarray_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ def test_open_datasets_differet_step_types_zeros() -> None:
assert res[1].cfrzr.attrs["GRIB_stepType"] == "avg"


# ensure that the encoding of the coordinates is preserved
def test_open_datasets_differet_preserve_coordinate_encoding() -> None:
res = xarray_store.open_datasets(TEST_DATA_DIFFERENT_STEP_TYPES)
assert len(res) == 2
assert "units" in res[0].valid_time.encoding
assert "units" in res[1].valid_time.encoding

res = xarray_store.open_datasets(TEST_DATA_DIFFERENT_STEP_TYPES_ZEROS)
assert len(res) == 2
assert "units" in res[0].valid_time.encoding
assert "units" in res[1].valid_time.encoding


def test_open_dataset_steps_in_minutes() -> None:
res = xarray_store.open_dataset(TEST_DATA_STEPS_IN_MINUTES)

Expand Down

0 comments on commit 0fd4cd9

Please sign in to comment.