Skip to content

Commit

Permalink
work on templates
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Nov 24, 2024
1 parent 24f4b40 commit 0d60d1d
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions src/anemoi/inference/inputs/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import base64
import logging
import zlib

import earthkit.data as ekd

Expand All @@ -17,18 +18,40 @@

LOG = logging.getLogger(__name__)

# 1 - Get a GRIB with mars
# 2 - grib_set -d 0 data.grib out.grib
# 4 - python -c 'import base64, sys;print(base64.b64encode(open(sys.argv[1], "rb").read()))' out.grib
# 1 - Get a GRIB with mars: retrieve,param=tp,levtype=sfc,type=fc,step=6,target=data.grib,grid=0.25/0.25
# 2 - grib_set -s edition=2,packingType=grid_ccsds data.grib data.grib2
# 3 - grib_set -d 0 data.grib2 out.grib
# 4 - python -c 'import base64, sys, zlib;print(base64.b64encode(zlib.compress(open(sys.argv[1], "rb").read())))' out.grib

# tp in grib2, 0.25/0.25 grid
TEMPLATE = """
R1JJQv//AAIAAAAAAAAA4AAAABUBAGIAACEBAQfoCxcGAAAAAQAAABECAAEAAQAJBAIwMDAxAAAASAMAAA/Xo
AAAAAAG////////////////////AAAFoAAAAtEAAAAA/////wVdSoAAAAAAMBVxWXAKtsRwAAPQkAAD0JAAAA
AAOgQAAAAIAcEC/54AAAABAAAAAAH//////////////wfoCxcMAAABAAAAAAECAQAAAAYNAAABwgAAABkFAA/
XoAAqAAAAAIATAAAAAA4gAIAAAAAGBv8AAAAFBzc3Nzc=
TEMPLATE_0p25 = """
eJxzD/J0+v+fgYkBAh4AsSgjQxIDgyIjI/sLbnEeoAAjEAsyASlGBk4WRgMDA0OggAczAwP/9QVgXf9RAQMDK1Cc6SJMijXWqw
HENmgFMkQLIwsYmC9MAGMgsGIBEhyMBxn+zwMqBVnFxohkFsgNQhAnMDAygan/ULdKsoIdoAViNwiDSD4FBpBFbGwgJazs5kAA
AKlBO70=
"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hIp/d0fEfntyi4QckVis5RdorCY5Qeongf5bsQ6EEigk
gHMm0ItSDVhFgdcjUIViFZgWgZsiUIFyFdgHgO8lkuYT7FNOxLxd+4leSqB/3PPSZvYPTlCchBf4lf3orepZMHoOnE7o4l85s8
jZBgMAlJD80y/gBx3pOi
"""

TEMPLATES = {(0.25, 0.25): TEMPLATE_0p25, "N320": TEMPLATE_N320, "O96": TEMPLATE_O96}


@input_registry.register("templates")
class TemplatesInput(Input):
Expand All @@ -37,6 +60,9 @@ class TemplatesInput(Input):
def __init__(self, context, *args, **kwargs):
super().__init__(context)

def __repr__(self):
return f"TemplatesInput({self.context.checkpoint.grid})"

def create_input_state(self, *, date):
raise NotImplementedError("TemplatesInput.create_input_state() not implemented")

Expand All @@ -48,7 +74,11 @@ def template(self, variable, date, **kwargs):
if not typed.is_accumulation:
return None

template = base64.b64decode(TEMPLATE)
grid = self.context.checkpoint.grid
if isinstance(grid, str):
grid = grid.upper()

template = zlib.decompress(base64.b64decode(TEMPLATES[grid]))
# eccodes.codes_new_from_message(template)

return ekd.from_source("memory", template)[0]
Expand Down

0 comments on commit 0d60d1d

Please sign in to comment.