Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
blaylockbk authored Jun 22, 2024
2 parents f6c7a5e + b06ab31 commit 6f5e9ed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Changelog for cfgrib
====================

x.x.x.x (xxxx-xx-xx)
--------------------
0.9.12.0 (2024-05-26)
---------------------

- fixed issue where GRIB messages with non-hourly steps could not be read
See `#370 <https://github.com/ecmwf/cfgrib/pull/370>`_.
Expand Down
2 changes: 1 addition & 1 deletion cfgrib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.9.11.0"
__version__ = "0.9.12.0"

# cfgrib core API depends on the ECMWF ecCodes C-library only
from .abc import Field, Fieldset, Index, MappingFieldset
Expand Down
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 6f5e9ed

Please sign in to comment.