-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix interpolating fieldlists with regular latlon grid (#32)
* Fix regular_ll fieldlist interpolation
- Loading branch information
1 parent
f2a2e6e
commit 0d3d0b7
Showing
6 changed files
with
161 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# (C) Copyright 2023 ECMWF. | ||
# | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
import os | ||
import sys | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from earthkit.regrid import interpolate | ||
|
||
here = os.path.dirname(__file__) | ||
sys.path.insert(0, here) | ||
from testing import get_test_data # noqa: E402 | ||
from testing import get_test_data_path # noqa: E402 | ||
|
||
try: | ||
from earthkit.data import from_source # noqa | ||
|
||
NO_EKD = False | ||
except Exception: | ||
NO_EKD = True | ||
|
||
|
||
@pytest.mark.download | ||
@pytest.mark.tmp_cache | ||
@pytest.mark.skipif(NO_EKD, reason="No access to earthkit-data") | ||
@pytest.mark.parametrize( | ||
"_kwarg,method", | ||
[ | ||
({}, "linear"), | ||
({"method": "linear"}, "linear"), | ||
({"method": "nearest-neighbour"}, "nearest-neighbour"), | ||
({"method": "nn"}, "nearest-neighbour"), | ||
({"method": "nearest-neighbor"}, "nearest-neighbour"), | ||
], | ||
) | ||
def test_fieldlist_reg_ll(_kwarg, method): | ||
ds = from_source("url", get_test_data_path("5x5.grib")) | ||
|
||
f_ref = get_test_data(f"out_5x5_10x10_{method}.npz") | ||
v_ref = np.load(f_ref)["arr_0"] | ||
|
||
r = interpolate(ds, out_grid={"grid": [10, 10]}, **_kwarg) | ||
|
||
assert len(r) == 1 | ||
assert r[0].shape == (19, 36) | ||
assert np.allclose(r[0].values, v_ref) | ||
|
||
|
||
@pytest.mark.download | ||
@pytest.mark.tmp_cache | ||
@pytest.mark.skipif(NO_EKD, reason="No access to earthkit-data") | ||
@pytest.mark.parametrize( | ||
"_kwarg,method", | ||
[ | ||
({}, "linear"), | ||
({"method": "linear"}, "linear"), | ||
({"method": "nearest-neighbour"}, "nearest-neighbour"), | ||
({"method": "nn"}, "nearest-neighbour"), | ||
({"method": "nearest-neighbor"}, "nearest-neighbour"), | ||
], | ||
) | ||
def test_fieldlist_gg(_kwarg, method): | ||
ds = from_source("url", get_test_data_path("O32.grib")) | ||
|
||
f_ref = get_test_data(f"out_O32_10x10_{method}.npz") | ||
v_ref = np.load(f_ref)["arr_0"] | ||
|
||
r = interpolate(ds, out_grid={"grid": [10, 10]}, **_kwarg) | ||
|
||
assert len(r) == 1 | ||
assert r[0].shape == (19, 36) | ||
assert np.allclose(r[0].values, v_ref) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# (C) Copyright 2023 ECMWF. | ||
# | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
import os | ||
|
||
PATH = os.path.dirname(__file__) | ||
|
||
URL_ROOT = "https://get.ecmwf.int/repository/test-data/earthkit-regrid/test-data" | ||
|
||
|
||
def simple_download(url, target): | ||
import requests | ||
|
||
r = requests.get(url, allow_redirects=True) | ||
r.raise_for_status() | ||
open(target, "wb").write(r.content) | ||
|
||
|
||
def get_test_data_path(filename, subfolder="global_0_360"): | ||
return os.path.join(URL_ROOT, subfolder, filename) | ||
|
||
|
||
def get_test_data(filename, subfolder="global_0_360"): | ||
if not isinstance(filename, list): | ||
filename = [filename] | ||
|
||
res = [] | ||
for fn in filename: | ||
d_path = os.path.join(PATH, "data", subfolder) | ||
os.makedirs(d_path, exist_ok=True) | ||
f_path = os.path.join(d_path, fn) | ||
if not os.path.exists(f_path): | ||
simple_download(url=f"{URL_ROOT}/{subfolder}/{fn}", target=f_path) | ||
res.append(f_path) | ||
|
||
if len(res) == 1: | ||
return res[0] | ||
else: | ||
return res |