Skip to content

Commit

Permalink
Support opendata to gaussian grid interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
gmertes committed Nov 20, 2024
1 parent c5847cd commit 80f65ec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies = [
"earthkit-data>=0.10.3",
"earthkit-meteo",
"earthkit-regrid",
"eccodes>=2.37",
"eccodes>=2.38.3",
"ecmwf-api-client",
"ecmwf-opendata",
"entrypoints",
Expand Down
13 changes: 12 additions & 1 deletion src/ai_models/inputs/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def __init__(self, grid, source, metadata):
self.source = list(source) if isinstance(source, tuple) else source
self.metadata = metadata

self._reduced_gg = metadata.get("gridType") == "reduced_gg"

def __call__(self, ds):
tmp = temp_file()

Expand All @@ -29,7 +31,16 @@ def __call__(self, ds):
result = []
for f in tqdm.tqdm(ds, delay=0.5, desc="Interpolating", leave=False):
data = ekr.interpolate(f.to_numpy(), dict(grid=self.source), dict(grid=self.grid))
out.write(data, template=f, **self.metadata)
template = f

if self._reduced_gg:
# template is missing the pl matrix required to output a reduced_gg grib
# this is a hack to let earthkit generate it for us
template = None
keys = ("shortName", "levelist", "date", "time", "step", "number")
self.metadata.update({key: f._metadata[key] for key in keys if key in f._metadata})

out.write(data, template=template, **self.metadata)

out.close()

Expand Down
4 changes: 2 additions & 2 deletions src/ai_models/inputs/opendata.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
Nj=1801,
),
),
# "N320": ("0p25", (0.25, 0.25), True, False, dict(gridType='reduced_gg')),
# "O96": ("0p25", (0.25, 0.25), True, False, dict(gridType='reduced_gg', )),
"N320": ("0p25", (0.25, 0.25), True, False, dict(gridType="reduced_gg", N=320)),
"O96": ("0p25", (0.25, 0.25), True, False, dict(gridType="reduced_gg", N=96)),
}


Expand Down
7 changes: 5 additions & 2 deletions src/ai_models/outputs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def output(self):

def write(self, data, *args, check=False, **kwargs):

if kwargs.get("param") in ("cp", "tp"):
kwargs["edition"] = 1

try:
handle, path = self.output.write(data, *args, **kwargs)

Expand Down Expand Up @@ -85,9 +88,9 @@ def write(self, data, *args, check=False, **kwargs):
# Check that the GRIB keys are as expected

if kwargs.get("expver") is None:
ignore = ("template", "check_nans", "expver", "class", "type", "stream")
ignore = ("edition", "template", "check_nans", "expver", "class", "type", "stream")
else:
ignore = ("template", "check_nans")
ignore = ("edition", "template", "check_nans")

for key, value in itertools.chain(self.grib_keys.items(), kwargs.items()):
if key in ignore:
Expand Down

0 comments on commit 80f65ec

Please sign in to comment.