Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with alternativeRowScanning and mercator grid (#358) #359

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Changelog for cfgrib
- fix issue where to_grib() could crash if given a dataset with a single-valued dimension
See `#347 <https://github.com/ecmwf/cfgrib/issues/347>`_.

- fix issue where values could not be extracted when alternativeRowScanning=1 and
grid is not represented as 2D
See `#358 <https://github.com/ecmwf/cfgrib/issues/358>`_.

0.9.10.4 (2023-05-19)
---------------------
Expand Down
3 changes: 2 additions & 1 deletion cfgrib/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ def get_values_in_order(message, shape):
# type: (abc.Field, T.Tuple[int]) -> np.ndarray
# inform the data provider to return missing values as missing_value
values = message["values"]
if message.get("alternativeRowScanning", False):
# for 2D array (lat/lon) re-arrange if alternative row scanning
if len(shape) == 2 and message.get("alternativeRowScanning", False):
values = values.copy().reshape(shape)
values[1::2, :] = values[1::2, ::-1]
return values.flatten()
Expand Down
Binary file added tests/sample-data/ds.waveh.5.grib
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_40_xarray_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TEST_DATA_MULTIPLE_FIELDS = os.path.join(SAMPLE_DATA_FOLDER, "regular_gg_ml_g2.grib")
TEST_DATA_DIFFERENT_STEP_TYPES = os.path.join(SAMPLE_DATA_FOLDER, "cfrzr_and_cprat.grib")
TEST_DATA_DIFFERENT_STEP_TYPES_ZEROS = os.path.join(SAMPLE_DATA_FOLDER, "cfrzr_and_cprat_0s.grib")
TEST_DATA_ALTERNATE_ROWS_MERCATOR = os.path.join(SAMPLE_DATA_FOLDER, "ds.waveh.5.grib")


def test_open_dataset() -> None:
Expand Down Expand Up @@ -154,3 +155,11 @@ def test_open_datasets_differet_step_types_zeros() -> None:
assert res[0].cfrzr.attrs["GRIB_stepType"] == "instant"
assert res[1].cprat.attrs["GRIB_stepType"] == "avg"
assert res[1].cfrzr.attrs["GRIB_stepType"] == "avg"


def test_alternating_scanning_mercator() -> None:
ds = xarray_store.open_dataset(TEST_DATA_ALTERNATE_ROWS_MERCATOR)
values = ds.variables["shww"].data
assert np.isnan(values[5])
assert values[760500] == 1.5
values_all = ds.variables["shww"].data[:]
Loading