Skip to content

Commit 33cae5b

Browse files
committed
better grib2 support
1 parent cf4b994 commit 33cae5b

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/ai_models/inputs/opendata.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def __init__(self, owner, **kwargs):
6161
self.owner = owner
6262

6363
def _adjust(self, kwargs):
64+
65+
kwargs.setdefault("step", 0)
66+
6467
if "level" in kwargs:
6568
# OpenData uses levelist instead of level
6669
kwargs["levelist"] = kwargs.pop("level")
@@ -105,7 +108,7 @@ def pl_load_source(self, **kwargs):
105108
kwargs["param"] = param
106109
gh_to_z = make_z_from_gh
107110

108-
logging.debug("load source ecmwf-open-data %s", kwargs)
111+
logging.info("ecmwf-open-data %s", kwargs)
109112

110113
opendata = recenter(ekd.from_source("ecmwf-open-data", **kwargs))
111114
opendata = gh_to_z(opendata)

src/ai_models/model.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# granted to it by virtue of its status as an intergovernmental organisation
66
# nor does it submit to any jurisdiction.
77

8+
import base64
89
import datetime
910
import json
1011
import logging
@@ -541,6 +542,8 @@ def write_input_fields(
541542
if ignore is None:
542543
ignore = []
543544

545+
fields.save("input.grib")
546+
544547
with self.timer("Writing step 0"):
545548
for field in fields:
546549
if field.metadata("shortName") in ignore:
@@ -560,18 +563,49 @@ def write_input_fields(
560563
if accumulations_shape is None:
561564
accumulations_shape = accumulations_template.shape
562565

563-
for param in accumulations:
564-
self.write(
565-
np.zeros(accumulations_shape, dtype=np.float32),
566-
stepType="accum",
567-
template=accumulations_template,
568-
param=param,
569-
startStep=0,
570-
endStep=0,
571-
date=int(self.start_datetime.strftime("%Y%m%d")),
572-
time=int(self.start_datetime.strftime("%H%M")),
573-
check=True,
574-
)
566+
if accumulations_template.metadata("edition") == 1:
567+
for param in accumulations:
568+
569+
self.write(
570+
np.zeros(accumulations_shape, dtype=np.float32),
571+
stepType="accum",
572+
template=accumulations_template,
573+
param=param,
574+
startStep=0,
575+
endStep=0,
576+
date=int(self.start_datetime.strftime("%Y%m%d")),
577+
time=int(self.start_datetime.strftime("%H%M")),
578+
check=True,
579+
)
580+
else:
581+
# # TODO: Remove this when accumulations are supported for GRIB edition 2
582+
583+
template = """
584+
R1JJQv//AAIAAAAAAAAA3AAAABUBAGIAABsBAQfoCRYGAAAAAQAAABECAAEAAQAJBAIwMDAxAAAA
585+
SAMAAA/XoAAAAAAG////////////////////AAAFoAAAAtEAAAAA/////wVdSoAAAAAAMIVdSoAV
586+
cVlwAAPQkAAD0JAAAAAAOgQAAAAIAcEC//8AAAABAAAAAAH//////////////wfoCRYGAAABAAAA
587+
AAECAQAAAAD/AAAAAAAAABUFAA/XoAAAAAAAAIAKAAAAAAAAAAYG/wAAAAUHNzc3N0dSSUL//wAC
588+
AAAAAAAAANwAAAAVAQBiAAAbAQEH6AkWDAAAAAEAAAARAgABAAEACQQBMDAwMQAAAEgDAAAP16AA
589+
AAAABv///////////////////wAABaAAAALRAAAAAP////8FXUqAAAAAADCFXUqAFXFZcAAD0JAA
590+
A9CQAAAAADoEAAAACAHBAv//AAAAAQAAAAAB//////////////8H6AkWDAAAAQAAAAABAgEAAAAA
591+
/wAAAAAAAAAVBQAP16AAAAAAAACACgAAAAAAAAAGBv8AAAAFBzc3Nzc=
592+
"""
593+
594+
template = base64.b64decode(template)
595+
accumulations_template = ekd.from_source("memory", template)[0]
596+
597+
for param in accumulations:
598+
self.write(
599+
np.zeros(accumulations_shape, dtype=np.float32),
600+
stepType="accum",
601+
template=accumulations_template,
602+
param=param,
603+
startStep=0,
604+
endStep=0,
605+
date=int(self.start_datetime.strftime("%Y%m%d")),
606+
time=int(self.start_datetime.strftime("%H%M")),
607+
check=True,
608+
)
575609

576610

577611
def load_model(name, **kwargs):

0 commit comments

Comments
 (0)