Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
netcdf
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Feb 18, 2024
1 parent cdf6977 commit 4df3e14
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 119 deletions.
24 changes: 21 additions & 3 deletions ecml_tools/create/functions/actions/grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,35 @@
from climetlab.utils.patterns import Pattern


def check(ds, paths, **kwargs):
count = 1
for k, v in kwargs.items():
if isinstance(v, (tuple, list)):
count *= len(v)

if len(ds) != count:
raise ValueError(
f"Expected {count} fields, got {len(ds)} (kwargs={kwargs}, paths={paths})"
)


def execute(context, dates, path, *args, **kwargs):
paths = Pattern(path, ignore_missing_keys=True).substitute(
*args, date=dates, **kwargs
)

for name in ("grid", "area", "rotation", "frame", "resol", "bitmap"):
if name in kwargs:
raise ValueError(f"MARS interpolation parameter '{name}' not supported")

ds = load_source("empty")
dates = [d.isoformat() for d in dates]

for path in paths:
print("PATH", path)
context.trace("📁", "PATH", path)
s = load_source("file", path)
s = s.sel(valid_datetime=[d.isoformat() for d in dates], **kwargs)

s = s.sel(valid_datetime=dates, **kwargs)
ds = ds + s

check(ds, paths, valid_datetime=dates, **kwargs)
return ds
42 changes: 40 additions & 2 deletions ecml_tools/create/functions/actions/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@
# nor does it submit to any jurisdiction.
#

from .opendap import execute as opendap_execute
from climetlab import load_source
from climetlab.utils.patterns import Pattern

execute = opendap_execute # netcdf is an alias for opendap

def check(what, ds, paths, **kwargs):
count = 1
for k, v in kwargs.items():
if isinstance(v, (tuple, list)):
count *= len(v)

if len(ds) != count:
raise ValueError(
f"Expected {count} fields, got {len(ds)} (kwargs={kwargs}, {what}s={paths})"
)


def load_netcdfs(emoji, what, context, dates, path, *args, **kwargs):
paths = Pattern(path, ignore_missing_keys=True).substitute(
*args, date=dates, **kwargs
)

ds = load_source("empty")
levels = kwargs.get("level", kwargs.get("levelist"))

for path in paths:
context.trace(emoji, what.upper(), path)
s = load_source("opendap", path)
s = s.sel(
valid_datetime=[d.isoformat() for d in dates],
param=kwargs["param"],
step=kwargs.get("step", 0),
)
if levels:
s = s.sel(levelist=levels)
ds = ds + s
check(what, ds, paths, valid_datetime=dates, **kwargs)
return ds


def execute(context, dates, path, *args, **kwargs):
return load_netcdfs("📁", "path", context, dates, path, *args, **kwargs)
26 changes: 3 additions & 23 deletions ecml_tools/create/functions/actions/opendap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,8 @@
# nor does it submit to any jurisdiction.
#

from .netcdf import load_netcdfs

from climetlab import load_source
from climetlab.utils.patterns import Pattern


def execute(context, dates, url_pattern, *args, **kwargs):
urls = Pattern(url_pattern, ignore_missing_keys=True).substitute(
*args, date=dates, **kwargs
)

ds = load_source("empty")
levels = kwargs.get("level", kwargs.get("levelist"))

for url in urls:
context.trace("🌐", url)
s = load_source("opendap", url)
s = s.sel(
valid_datetime=[d.isoformat() for d in dates],
param=kwargs["param"],
step=kwargs.get("step", 0),
)
if levels:
s = s.sel(levelist=levels)
ds = ds + s
return ds
def execute(context, dates, url, *args, **kwargs):
return load_netcdfs("🌐", "url", context, dates, url, *args, **kwargs)
Loading

0 comments on commit 4df3e14

Please sign in to comment.