Skip to content
Snippets Groups Projects
Commit bc11f16b authored by Fraser William Goldsworth's avatar Fraser William Goldsworth
Browse files

updated tests, nabla failing

parent 08d7a8b0
No related branches found
No related tags found
No related merge requests found
import pytest
from pathlib import Path
import pytest
import xarray as xr
import pyicon as pyic
@pytest.fixture()
def raw_grid():
cur_dir = Path(__file__).parent.resolve()
grid_path = cur_dir / "test_data/icon_grid_0014_R02B04_O.nc"
def lazy_raw_grid():
path_data = Path(pyic.params['path_example_data'])
path_data.mkdir(parents=True, exist_ok=True)
grid_path = path_data / "icon_grid_0014_R02B04_O.nc"
if not grid_path.exists():
import requests
......@@ -19,37 +21,57 @@ def raw_grid():
raise FileNotFoundError("{grid_path} does not exist and unable to \
download it")
ds_grid = xr.open_dataset(grid_path)
ds_grid = xr.open_dataset(grid_path, chunks="auto")
return ds_grid
@pytest.fixture()
def processed_tgrid(raw_grid):
return pyic.convert_tgrid_data(raw_grid)
def eager_raw_grid(lazy_raw_grid):
ds_raw_grid = lazy_raw_grid.compute()
return ds_raw_grid
@pytest.fixture()
def lazy_processed_tgrid(lazy_raw_grid):
return pyic.convert_tgrid_data(lazy_raw_grid)
@pytest.fixture()
def eager_processed_tgrid(eager_raw_grid):
return pyic.convert_tgrid_data(eager_raw_grid)
@pytest.fixture()
def examp_icon_dataset():
#cur_dir = Path(__file__).parent.resolve()
#fpath_data = cur_dir / "test_data/icon_example_data_r2b6.nc"
def lazy_examp_icon_dataset():
path_data = Path(pyic.params['path_example_data'])
path_data.mkdir(parents=True, exist_ok=True)
fpath_data = path_data / "icon_example_data_r2b4.nc"
if not fpath_data.exists():
import requests
download_link = "https://swift.dkrz.de/v1/dkrz_07387162e5cd4c81b1376bd7c648bb60/pyicon_example_data/icon_example_data_r2b4.nc"
download_link = "https://swift.dkrz.de/v1/dkrz_83018ad4-3c8d-4c7d-b684-7ba0742caa1a/pyicon_test_data/icon_example_data_r2b4.nc?temp_url_sig=03fb5d20a44832c7cf736ab83c1be3936364dbf6&temp_url_expires=2026-11-24T11:53:16Z"
try:
r = requests.get(download_link, allow_redirects=True)
r = requests.get(download_link, allow_redirects=True, stream=True)
with open(fpath_data, "wb") as fobj:
fobj.write(r.content)
except:
raise FileNotFoundError("{fpath_data} does not exist and unable to \
download it")
ds = xr.open_dataset(fpath_data)
ds = xr.open_dataset(fpath_data, chunks="auto")
return ds
@pytest.fixture()
def examp_icon_dataarray(examp_icon_dataset):
return examp_icon_dataset["to"]
def eager_examp_icon_dataset(lazy_examp_icon_dataset):
return lazy_examp_icon_dataset.compute()
@pytest.fixture()
def lazy_examp_icon_dataarray(lazy_examp_icon_dataset):
return lazy_examp_icon_dataset["to"]
@pytest.fixture()
def eager_examp_icon_dataarray(eager_examp_icon_dataset):
return eager_examp_icon_dataset["to"]
......@@ -2,10 +2,12 @@ from itertools import product
import pytest
import numpy as np
import pyicon as pyic
from .conftest import raw_grid, processed_tgrid
from .conftest import eager_raw_grid, eager_processed_tgrid, lazy_raw_grid, lazy_processed_tgrid
def test_convert_tgrid_data(raw_grid):
@pytest.mark.parametrize("raw_grid", ["lazy_raw_grid", "eager_raw_grid"])
def test_convert_tgrid_data(raw_grid, request):
raw_grid = request.getfixturevalue(raw_grid)
converted_tgrid = pyic.convert_tgrid_data(raw_grid)
# Check conversion to pythonic indexing of neighbour info has worked
......@@ -50,9 +52,8 @@ def test_convert_tgrid_data(raw_grid):
assert "cell" in converted_tgrid.dims
@pytest.mark.parametrize("tgrid", ["raw_grid", "processed_tgrid"])
@pytest.mark.parametrize("tgrid", ["lazy_raw_grid", "lazy_processed_tgrid", "eager_raw_grid", "eager_processed_tgrid"])
def test_xr_crop_tgrid(tgrid, request):
# Set ireg_c and crop the grid
tgrid = request.getfixturevalue(tgrid)
for point, dim in product("cev", ["lon", "lat"]):
......@@ -60,6 +61,9 @@ def test_xr_crop_tgrid(tgrid, request):
if tgrid[coord].units == "radian":
tgrid[coord] = np.degrees(tgrid[coord])
tgrid["clon"] = tgrid["clon"].compute()
tgrid["clat"] = tgrid["clat"].compute()
ireg_c = tgrid["cell"].where(
(tgrid["clon"] > -5) & (tgrid["clon"] < 5)
& (tgrid["clat"] > -5) & (tgrid["clat"] < 5),
......@@ -90,7 +94,10 @@ def test_xr_crop_tgrid(tgrid, request):
assert cropped_tgrid["ireg_v"].prod() == -1427286351937536000
def test_nabla_funcs(processed_tgrid):
@pytest.mark.parametrize("processed_tgrid", ["lazy_processed_tgrid", "eager_processed_tgrid"])
def test_nabla_funcs(processed_tgrid, request):
processed_tgrid = request.getfixturevalue(processed_tgrid)
# Want to check curl of a gradient
gradient = pyic.xr_calc_grad(processed_tgrid, processed_tgrid["clon"])
curl_of_grad = pyic.xr_calc_curl(processed_tgrid, gradient)
......
......@@ -2,19 +2,19 @@ import pytest
import pyicon as pyic
import xarray as xr
import matplotlib.pyplot as plt
from .conftest import examp_icon_dataarray
from .conftest import lazy_examp_icon_dataarray, eager_examp_icon_dataarray
def test_plot(examp_icon_dataarray):
pyic.plot(examp_icon_dataarray.isel(time=0, depth=0))
return
def test_plot_sec(examp_icon_dataarray):
pyic.plot_sec(examp_icon_dataarray.isel(time=0), section='170W')
@pytest.mark.parametrize("examp_icon_dataarray", ["lazy_examp_icon_dataarray", "eager_examp_icon_dataarray"])
@pytest.mark.parametrize("grid_type,", ["auto", "native"])
def test_plot(examp_icon_dataarray, grid_type, request):
examp_icon_dataarray = request.getfixturevalue(examp_icon_dataarray)
pyic.plot(examp_icon_dataarray.isel(time=0, depth=0), grid_type=grid_type)
return
#def test_shade():
# dai = pyic.interp_to_rectgrid_xr(da)
# dai = dai.compute()
# pyic.shade(dai.lon, dai.lat, dai)
# return
@pytest.mark.parametrize("examp_icon_dataarray", ["lazy_examp_icon_dataarray", "eager_examp_icon_dataarray"])
def test_plot_sec(examp_icon_dataarray, request):
examp_icon_dataarray = request.getfixturevalue(examp_icon_dataarray)
pyic.plot_sec(examp_icon_dataarray.isel(time=0), section='170W')
return
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment