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

Decode time_dim based on monthlyVerificationDate #393

Merged
merged 2 commits into from
Jul 18, 2024
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
4 changes: 4 additions & 0 deletions cfgrib/cfmessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ def build_valid_time(time, step):
functools.partial(from_grib_date_time, date_key="indexingDate", time_key="indexingTime"),
functools.partial(to_grib_date_time, date_key="indexingDate", time_key="indexingTime"),
),
"valid_month": (
functools.partial(from_grib_date_time, date_key="monthlyVerificationDate", time_key="validityTime"),
functools.partial(to_grib_date_time, date_key="monthlyVerificationDate", time_key="validityTime"),
),
} # type: messages.ComputedKeysType


Expand Down
25 changes: 19 additions & 6 deletions cfgrib/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"verifying_time",
"forecastMonth",
"indexing_time",
"valid_month",
]
SPECTRA_KEYS = ["directionNumber", "frequencyNumber"]

Expand Down Expand Up @@ -250,6 +251,12 @@
"standard_name": "time",
"long_name": "time",
},
"valid_month": {
"units": "seconds since 1970-01-01T00:00:00",
"calendar": "proleptic_gregorian",
"standard_name": "time",
"long_name": "time",
},
"verifying_time": {
"units": "seconds since 1970-01-01T00:00:00",
"calendar": "proleptic_gregorian",
Expand Down Expand Up @@ -457,10 +464,7 @@ def encode_cf_first(data_var_attrs, encode_cf=("parameter", "time"), time_dims=(
if "GRIB_units" in data_var_attrs:
data_var_attrs["units"] = data_var_attrs["GRIB_units"]
if "time" in encode_cf:
if set(time_dims).issubset(ALL_REF_TIME_KEYS):
coords_map.extend(time_dims)
else:
raise ValueError("time_dims %r not a subset of %r" % (time_dims, ALL_REF_TIME_KEYS))
coords_map.extend(time_dims)
else:
coords_map.extend(DATA_TIME_KEYS)
coords_map.extend(VERTICAL_KEYS)
Expand Down Expand Up @@ -498,8 +502,9 @@ def build_variable_components(
first = index.first()
extra_attrs = read_data_var_attrs(first, extra_keys)
data_var_attrs.update(**extra_attrs)
coords_map = encode_cf_first(data_var_attrs, encode_cf, time_dims)

coords_map = encode_cf_first(
data_var_attrs, encode_cf, time_dims,
)
coord_name_key_map = {}
coord_vars = {}
for coord_key in coords_map:
Expand Down Expand Up @@ -667,6 +672,14 @@ def build_dataset_components(
variables = {} # type: T.Dict[str, Variable]
filter_by_keys = index.filter_by_keys

# Warn about time_dims here to prevent repeasted messages in build_variable_components
if errors != "ignore" and not set(time_dims).issubset(ALL_REF_TIME_KEYS):
log.warning(
"Not all time_dimensions are recognised, those which are not in the following list will not "
" be decoded as datetime objects:\n"
f"{ALL_REF_TIME_KEYS}"
)

for param_id in index.get("paramId", []):
var_index = index.subindex(paramId=param_id)
try:
Expand Down
Binary file added tests/sample-data/cams-egg4-monthly.grib
Binary file not shown.
Binary file modified tests/sample-data/era5-levels-members.nc
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_50_sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,12 @@ def test_dataset_missing_field_values() -> None:
t2 = res.variables["t2m"]
assert np.isclose(np.nanmean(t2[0, :, :]), 268.375)
assert np.isclose(np.nanmean(t2[1, :, :]), 270.716)


def test_valid_month_time_dim() -> None:

test_file = os.path.join(SAMPLE_DATA_FOLDER, "cams-egg4-monthly.grib")
ds = xr.open_dataset(test_file, time_dims=["valid_month"])

assert "valid_month" in ds.dims

Loading