Skip to content

Commit 5a9044c

Browse files
committed
coords_as_attributes functionality
1 parent 8b8e896 commit 5a9044c

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

cfgrib/dataset.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ def build_variable_components(
494494
read_keys: T.Iterable[str] = (),
495495
time_dims: T.Sequence[str] = ("time", "step"),
496496
extra_coords: T.Dict[str, str] = {},
497+
coords_as_attributes: T.Dict[str, str] = {},
497498
cache_geo_coords: bool = True,
498499
) -> T.Tuple[T.Dict[str, int], Variable, T.Dict[str, Variable]]:
499500
data_var_attrs = enforce_unique_attributes(index, DATA_ATTRIBUTES_KEYS, filter_by_keys)
@@ -520,6 +521,9 @@ def build_variable_components(
520521
and "GRIB_typeOfLevel" in data_var_attrs
521522
):
522523
coord_name = data_var_attrs["GRIB_typeOfLevel"]
524+
if coord_name in coords_as_attributes and len(values) == 1:
525+
data_var_attrs[f"GRIB_{coord_name}"] = values
526+
continue
523527
coord_name_key_map[coord_name] = coord_key
524528
attributes = {
525529
"long_name": "original GRIB coordinate for key: %s(%s)" % (orig_name, coord_name),
@@ -666,6 +670,7 @@ def build_dataset_components(
666670
read_keys: T.Iterable[str] = (),
667671
time_dims: T.Sequence[str] = ("time", "step"),
668672
extra_coords: T.Dict[str, str] = {},
673+
coords_as_attributes: T.Dict[str, str] = {},
669674
cache_geo_coords: bool = True,
670675
) -> T.Tuple[T.Dict[str, int], T.Dict[str, Variable], T.Dict[str, T.Any], T.Dict[str, T.Any]]:
671676
dimensions = {} # type: T.Dict[str, int]
@@ -692,6 +697,7 @@ def build_dataset_components(
692697
read_keys=read_keys,
693698
time_dims=time_dims,
694699
extra_coords=extra_coords,
700+
coords_as_attributes=coords_as_attributes,
695701
cache_geo_coords=cache_geo_coords,
696702
)
697703
except DatasetBuildError as ex:
@@ -814,5 +820,4 @@ def open_file(
814820
stream = messages.FileStream(path, errors=errors)
815821
index_keys = compute_index_keys(time_dims, extra_coords)
816822
index = open_fileindex(stream, indexpath, index_keys, ignore_keys=ignore_keys, filter_by_keys=filter_by_keys)
817-
818823
return open_from_index(index, read_keys, time_dims, extra_coords, errors=errors, **kwargs)

cfgrib/xarray_plugin.py

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def open_dataset(
105105
time_dims: T.Iterable[str] = ("time", "step"),
106106
errors: str = "warn",
107107
extra_coords: T.Dict[str, str] = {},
108+
coords_as_attributes: T.Dict[str, str] = {},
108109
cache_geo_coords: bool = True,
109110
) -> xr.Dataset:
110111
store = CfGribDataStore(
@@ -119,6 +120,7 @@ def open_dataset(
119120
lock=lock,
120121
errors=errors,
121122
extra_coords=extra_coords,
123+
coords_as_attributes=coords_as_attributes,
122124
cache_geo_coords=cache_geo_coords,
123125
)
124126
with xr.core.utils.close_on_error(store):
1.79 KB
Binary file not shown.

tests/test_50_xarray_plugin.py

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
TEST_DATA = os.path.join(SAMPLE_DATA_FOLDER, "regular_ll_sfc.grib")
1212
TEST_DATA_MISSING_VALS = os.path.join(SAMPLE_DATA_FOLDER, "fields_with_missing_values.grib")
1313
TEST_DATA_MULTI_PARAMS = os.path.join(SAMPLE_DATA_FOLDER, "multi_param_on_multi_dims.grib")
14+
TEST_DATA_MULTI_LEVTYPES = os.path.join(SAMPLE_DATA_FOLDER, "soil-surface-level-mix.grib")
1415

1516

1617
def test_plugin() -> None:
@@ -164,3 +165,14 @@ def test_xr_open_dataset_file_missing_vals() -> None:
164165
t2 = ds["t2m"]
165166
assert np.isclose(np.nanmean(t2.values[0, :, :]), 268.375)
166167
assert np.isclose(np.nanmean(t2.values[1, :, :]), 270.716)
168+
169+
170+
def test_xr_open_dataset_coords_to_attributes() -> None:
171+
ds = xr.open_dataset(
172+
TEST_DATA_MULTI_LEVTYPES, engine="cfgrib", coords_as_attributes=["surface", "depthBelowLandLayer"]
173+
)
174+
assert "surface" not in ds.coords
175+
assert "depthBelowLandLayer" not in ds.coords
176+
177+
assert "GRIB_surface" in ds["t2m"].attrs
178+
assert "GRIB_depthBelowLandLayer" in ds["stl1"].attrs

0 commit comments

Comments
 (0)