Skip to content
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
2 changes: 1 addition & 1 deletion src/earthkit/data/utils/xarray/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _metadata():
fixed_attrs[a.name] = a.value()
elif callable(a):
res.update(a(_metadata()))
elif isinstance(a, str):
else:
res.update(a.get(_metadata()))

res = {k: v for k, v in res.items() if v is not None}
Expand Down
40 changes: 40 additions & 0 deletions tests/xr_engine/test_xr_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,43 @@ def test_xr_attrs_types(kwargs, coords, dims, attrs):
print(ds["t"].attrs)
for k, v in attrs.items():
assert ds["t"].attrs[k] == v, f"{k}"


@pytest.mark.cache
def test_xr_global_attrs():
ds_fl = from_source(
"url", earthkit_remote_test_data_file("test-data", "xr_engine", "level", "pl_small.grib")
)
ds = ds_fl.to_xarray(
attrs_mode="fixed",
global_attrs=[
{"centre_fixed": "_ecmf_"},
lambda md: {"centre_callable": md.get("centre")},
"centre",
"key=centreDescription",
{"centre_key": "key=centre"},
{"geography_namespace": "namespace=geography"},
],
)
ref_global_attrs = {
"centre_callable": "ecmf",
"centre": "ecmf",
"centreDescription": "European Centre for Medium-Range Weather Forecasts",
"centre_key": "ecmf",
"geography_namespace": {
"Ni": 36,
"Nj": 19,
"latitudeOfFirstGridPointInDegrees": 90.0,
"longitudeOfFirstGridPointInDegrees": 0.0,
"latitudeOfLastGridPointInDegrees": -90.0,
"longitudeOfLastGridPointInDegrees": 350.0,
"iScansNegatively": 0,
"jScansPositively": 0,
"jPointsAreConsecutive": 0,
"jDirectionIncrementInDegrees": 10.0,
"iDirectionIncrementInDegrees": 10.0,
"gridType": "regular_ll",
},
"centre_fixed": "_ecmf_",
}
assert ds.attrs == ref_global_attrs
Loading