Skip to content

fix few issues with byc #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/meteodatalab/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ def cosmo_grib_defs():
if "ECCODES_DEFINITION_PATH" in os.environ or "GRIB_DEFINITION_PATH" in os.environ:
return nullcontext()

restore = eccodes.codes_definition_path()
root_dir = Path(sys.prefix) / "share"
paths = (
root_dir / "eccodes-cosmo-resources/definitions",
Path(restore),
root_dir / "eccodes/definitions",
)

for path in paths:
if not path.exists():
raise RuntimeError(f"{path} does not exist")
defs_path = ":".join(map(str, paths))
defs_path = ":".join(map(str, paths))
eccodes.codes_set_definitions_path(defs_path)

try:
yield
finally:
eccodes.codes_set_definitions_path(restore)
eccodes.codes_set_definitions_path(str(root_dir / "eccodes/definitions"))


def grib_def_ctx(grib_def: str):
Expand Down
18 changes: 8 additions & 10 deletions src/meteodatalab/grib_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def _is_ensemble(field) -> bool:

def _get_hcoords(field):
if field.metadata("gridType") == "unstructured_grid":
return {}
grid_uuid = field.metadata("uuidOfHGrid")
return icon_grid.get_icon_grid(grid_uuid)
return {
Expand Down Expand Up @@ -124,7 +125,7 @@ def __post_init__(self, is_ensemble: bool):
if not is_ensemble:
self.dims = self.dims[1:]

def load(self, field: GribField) -> None:
def load(self, field: GribField, hcoords=None) -> None:
key = _get_key(field, self.dims)
name = field.metadata(NAME_KEY)
logger.debug("Received field for param: %s, key: %s", name, key)
Expand All @@ -142,7 +143,7 @@ def load(self, field: GribField) -> None:
}

if not self.hcoords:
self.hcoords = _get_hcoords(field)
self.hcoords = hcoords if hcoords else _get_hcoords(field)

def _gather_coords(self):
coord_values = zip(*self.values)
Expand Down Expand Up @@ -170,8 +171,7 @@ def to_xarray(self) -> xr.DataArray:
ref_time = xr.DataArray(coords["ref_time"], dims="ref_time")
lead_time = xr.DataArray(coords["lead_time"], dims="lead_time")
tcoords = {"valid_time": ref_time + lead_time}
hdims = self.hcoords["lon"].dims

hdims = self.hcoords["lon"].dims if "lon" in self.hcoords else ("cell",)
array = xr.DataArray(
data=np.array(
[self.values.pop(key) for key in sorted(self.values)]
Expand All @@ -188,8 +188,7 @@ def to_xarray(self) -> xr.DataArray:


def _load_buffer_map(
source: data_source.DataSource,
request: Request,
source: data_source.DataSource, request: Request, hcoords=None
) -> dict[str, _FieldBuffer]:
logger.info("Retrieving request: %s", request)
fs = source.retrieve(request)
Expand All @@ -202,7 +201,7 @@ def _load_buffer_map(
buffer = buffer_map[name]
else:
buffer = buffer_map[name] = _FieldBuffer(_is_ensemble(field))
buffer.load(field)
buffer.load(field, hcoords)

return buffer_map

Expand Down Expand Up @@ -246,8 +245,7 @@ def load_single_param(


def load(
source: data_source.DataSource,
request: Request,
source: data_source.DataSource, request: Request, hcoords=None
) -> dict[str, xr.DataArray]:
"""Request data from a data source.

Expand All @@ -269,7 +267,7 @@ def load(
A mapping of shortName to data arrays of the requested fields.

"""
buffer_map = _load_buffer_map(source, request)
buffer_map = _load_buffer_map(source, request, hcoords)
result = {}
for name, buffer in buffer_map.items():
try:
Expand Down
9 changes: 6 additions & 3 deletions src/meteodatalab/operators/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,11 @@ def _linear_weights_cropped_domain(
"""Crop the grid to output domain."""
xmin, ymin = np.min(pts_dst, axis=0) - buffer
xmax, ymax = np.max(pts_dst, axis=0) + buffer

x, y = np.transpose(pts_src)
mask = (xmin < x) & (x < xmax) & (ymin < y) & (y < ymax)
[idx] = np.nonzero(mask)
indices, weights = _linear_weights(np.extract(mask, pts_src), pts_dst)
indices, weights = _linear_weights(pts_src[mask], pts_dst)
return idx[indices], weights


Expand Down Expand Up @@ -485,11 +486,13 @@ def iconremap(

gx, gy = np.meshgrid(dst.x, dst.y)
transformer_dst = Transformer.from_crs(dst.crs.wkt, utm_crs)
points_dst = transformer_dst.transform(gx.flat, gy.flat)
points_dst = transformer_dst.transform(gy.flat, gx.flat)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which coordinate system are you using for your test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geolatlon

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, I would rather add always_xy to the previous line because this change is breaking support for all CRSs that are in the traditional order


xy = np.array(points_src).T
uv = np.array(points_dst).T

indices, weights = _linear_weights_cropped_domain(xy, uv)

return _icon2regular(field, dst, indices, weights)
return _icon2regular(field, dst, indices, weights).assign_coords(
lon=(("y", "x"), gx), lat=(("y", "x"), gy)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be lon or longitude?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only applies if the dst grid is defined in geolatlon CRS

)
3 changes: 0 additions & 3 deletions src/meteodatalab/operators/wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ def speed(u: xr.DataArray, v: xr.DataArray) -> xr.DataArray:
the horizontal wind speed [m/s].

"""
if u.origin_x != 0.0 or v.origin_y != 0.0:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we transition towards ICON native grid support and or other grids, I think we should decommission the C staggering (i.e. origin_x, origin_y). Still variables will be staggered in z

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we drop support for COSMO data from the archive, this will need to live on. The meaning of the origin_x and origin_y attributes need to be defined in the context of the ICON native grid. Then, we can adapt the checks.

raise ValueError("The wind components should not be staggered.")

name = {"U": "SP", "U_10M": "SP_10M"}[u.parameter["shortName"]]
return xr.DataArray(
np.sqrt(u**2 + v**2),
Expand Down
27 changes: 14 additions & 13 deletions tests/test_meteodatalab/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_icon2rotlatlon(data_dir, fieldextra, model_name):
assert_allclose(observed, fx_ds["T"], rtol=1e-4, atol=1e-4)


@pytest.mark.skip(reason="the byc method in fx is not optimised (>30min on icon-ch1)")
# @pytest.mark.skip(reason="the byc method in fx is not optimised (>30min on icon-ch1)")
@pytest.mark.data("iconremap")
@pytest.mark.parametrize("model_name", ["icon-ch1-eps", "icon-ch2-eps"])
def test_icon2swiss(data_dir, fieldextra, model_name):
Expand All @@ -158,16 +158,17 @@ def test_icon2swiss(data_dir, fieldextra, model_name):
"icon-ch2-eps": f"{root}/ICON-CH2-EPS/icon_grid_0002_R19B07_mch.nc",
}

fx_ds = fieldextra(
"iconremap",
model_name=model_name,
out_regrid_target=out_regrid_target[model_name],
out_regrid_method="icontools,byc",
icon_grid_description=icon_grid_description[model_name],
conf_files={
"inputi": data_dir / f"{model_name.upper()}_lfff<DDHH>0000_000",
"output": "<HH>_outfile.nc",
},
)

assert_allclose(observed, fx_ds["T"], rtol=1e-2, atol=1)
# fx_ds = fieldextra(
# "iconremap",
# model_name=model_name,
# out_regrid_target=out_regrid_target[model_name],
# out_regrid_method="icontools,byc",
# icon_grid_description=icon_grid_description[model_name],
# conf_files={
# "inputi": data_dir / f"{model_name.upper()}_lfff<DDHH>0000_000",
# "output": "<HH>_outfile.nc",
# },
# )
#
# assert_allclose(observed, fx_ds["T"], rtol=1e-2, atol=1)
97 changes: 0 additions & 97 deletions tools/setup_env.sh

This file was deleted.

Loading