Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/ecmwf/climetlab into dev…
Browse files Browse the repository at this point in the history
…elop
  • Loading branch information
floriankrb committed Sep 11, 2023
2 parents 340ab75 + e0fb59e commit 5aee200
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
41 changes: 28 additions & 13 deletions climetlab/readers/grib/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@
ACCUMULATIONS = {("tp", 2): {"productDefinitionTemplateNumber": 8}}


_ORDER = (
"edition",
"setLocalDefinition",
"typeOfGeneratingProcess",
"productDefinitionTemplateNumber",
)

ORDER = {}
for i, k in enumerate(_ORDER):
ORDER[k] = i


def order(key):
ORDER.setdefault(key, len(ORDER))
return ORDER[key]


class Combined:
def __init__(self, handle, metadata):
self.handle = handle
self.metadata = metadata

def __contains__(self, key):
# return key in self.metadata or key in self.handle
raise NotImplementedError()

def __getitem__(self, key):
Expand Down Expand Up @@ -89,13 +107,9 @@ def write(
else:
handle = template.handle.clone()

# print("->", metadata)
self.update_metadata(handle, metadata, compulsary)

other = {}

for k, v in list(metadata.items()):
if not isinstance(v, (int, float, str)):
other[k] = metadata.pop(k)
# print("<-", metadata)

if check_nans:
import numpy as np
Expand All @@ -107,11 +121,13 @@ def write(
metadata["missingValue"] = missing_value
metadata["bitmapPresent"] = 1

LOG.debug("GribOutput.metadata %s, other %s", metadata, other)
metadata = {
k: v for k, v in sorted(metadata.items(), key=lambda x: order(x[0]))
}

handle.set_multiple(metadata)
LOG.debug("GribOutput.metadata %s", metadata)

for k, v in other.items():
for k, v in metadata.items():
handle.set(k, v)

handle.set_values(values)
Expand Down Expand Up @@ -166,10 +182,9 @@ def update_metadata(self, handle, metadata, compulsary):
if "number" in metadata:
compulsary += ("numberOfForecastsInEnsemble",)
productDefinitionTemplateNumber = {"tp": 11}
metadata.setdefault(
"productDefinitionTemplateNumber",
productDefinitionTemplateNumber.get(handle.get("shortName"), 1),
)
metadata[
"productDefinitionTemplateNumber"
] = productDefinitionTemplateNumber.get(handle.get("shortName"), 1)

if metadata.get("type") in ("pf", "cf"):
metadata.setdefault("typeOfGeneratingProcess", 4)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_tp():


if __name__ == "__main__":
test_tp()
test_mars_labeling()
# from climetlab.testing import main

# main(__file__)

0 comments on commit 5aee200

Please sign in to comment.