Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Make sure that tests can run with `pyposeidon-base`
Remove `cartopy` dependency from tests
Replace `jigsaw` with `gmsh` and bypass  ec-jrc#84
  • Loading branch information
brey committed Jan 29, 2022
1 parent a5091f2 commit bd831e0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 86 deletions.
38 changes: 16 additions & 22 deletions tests/test_boundary.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import pyposeidon.boundary as pb
import pytest
import geopandas as gp
import cartopy.feature as cf

from . import DATA_DIR

noaa = DATA_DIR / "bl.zip"

land = cf.NaturalEarthFeature(category="physical", name="land", scale="50m")
nland = gp.GeoDataFrame(geometry=[x for x in land.geometries()])
COAST_FILE = (DATA_DIR / "ocean.zip").as_posix()

coast = cf.NaturalEarthFeature(category="physical", name="coastline", scale="50m")
ncoast = gp.GeoDataFrame(geometry=[x for x in coast.geometries()])
land = gp.read_file(COAST_FILE).drop("FID", axis=1)
coast = gp.GeoDataFrame(geometry=land.boundary)

# gh = cf.GSHHSFeature(scale="intermediate", levels=[1])
# iGSHHS = gp.GeoDataFrame(geometry=[x for x in gh.geometries()])


INPUTS = pytest.mark.parametrize("input", [nland, ncoast]) # , iGSHHS])
INPUTS = pytest.mark.parametrize("input", [land, coast])
CUSTOM = pytest.mark.parametrize("boundary", [noaa])
WINDOWS = pytest.mark.parametrize(
"window",
[
{"lon_min": -30, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 70.0},
{"lon_min": 175.0, "lon_max": 184.0, "lat_min": 14.5, "lat_max": 16.5},
{"lon_min": 175.0, "lon_max": 184.0, "lat_min": -50, "lat_max": -30},
],
)
DEM_SOURCES = pytest.mark.parametrize(
Expand Down Expand Up @@ -61,21 +55,21 @@ def test_buffer(tmpdir, window, input):
assert isinstance(df.contours, gp.GeoDataFrame)


#@INPUTS
#@WINDOWS
#@DEM_SOURCES
#def test_isodem(tmpdir, window, input, dem_source):
# @INPUTS
# @WINDOWS
# @DEM_SOURCES
# def test_isodem(tmpdir, window, input, dem_source):
# df = pb.Boundary(geometry=window, dem_source=dem_source, blevels=[-100], rpath=str(tmpdir) + "/")
# assert isinstance(df.contours, gp.GeoDataFrame)


@pytest.mark.slow
def test_isodem_with_url(tmpdir):
input = nland
window = {"lon_min": 176.5, "lon_max": 177.0, "lat_min": 16.0, "lat_max": 16.5}
dem_source = "https://coastwatch.pfeg.noaa.gov/erddap/griddap/srtm30plus"
df = pb.Boundary(geometry=window, dem_source=dem_source, blevels=[-100], rpath=str(tmpdir) + "/")
assert isinstance(df.contours, gp.GeoDataFrame)
# @pytest.mark.slow
# def test_isodem_with_url(tmpdir):
# input = nland
# window = {"lon_min": 176.5, "lon_max": 177.0, "lat_min": 16.0, "lat_max": 16.5}
# dem_source = "https://coastwatch.pfeg.noaa.gov/erddap/griddap/srtm30plus"
# df = pb.Boundary(geometry=window, dem_source=dem_source, blevels=[-100], rpath=str(tmpdir) + "/")
# assert isinstance(df.contours, gp.GeoDataFrame)


@CUSTOM
Expand Down
28 changes: 14 additions & 14 deletions tests/test_dem_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import pytest
import numpy as np
import geopandas as gp
import cartopy.feature as cf

from . import DATA_DIR

COAST_FILE = (DATA_DIR / "ocean.zip").as_posix()

DEM_SOURCES = pytest.mark.parametrize(
"dem_source",
[
Expand All @@ -19,58 +20,57 @@
"window",
[
{"lon_min": -30, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 70.0},
{"lon_min": 175.0, "lon_max": 185.0, "lat_min": -21.5, "lat_max": -14.5},
{"lon_min": -185.0, "lon_max": -175.0, "lat_min": -21.5, "lat_max": -14.5},
{"lon_min": 165.0, "lon_max": 185.0, "lat_min": -50, "lat_max": -30},
{"lon_min": -185.0, "lon_max": -165.0, "lat_min": -50, "lat_max": -30},
{"lon_min": -3, "lon_max": 1.0, "lat_min": 42.0, "lat_max": 45.0},
],
)


@pytest.fixture(scope="session")
def natural_earth():
coast = cf.NaturalEarthFeature(category="physical", name="land", scale="110m")
gdf = gp.GeoDataFrame(geometry=[x.buffer(0.001) for x in coast.geometries()])
return gdf
def coasts():
coast = gp.read_file(COAST_FILE).drop("FID", axis=1)
return coast


@DEM_SOURCES
@WINDOWS
def test_dem_adjust(natural_earth, dem_source, window):
def test_dem_adjust(coasts, dem_source, window):
# Just elevation
df = pdem.Dem(**window, dem_source=dem_source) # get dem
df.adjust(natural_earth)
df.adjust(coasts)
assert np.isnan(df.Dataset.adjusted.values).sum() == 0


# Schism mesh
@pytest.mark.schism
@DEM_SOURCES
@WINDOWS
def test_schism_mesh(tmpdir, natural_earth, dem_source, window):
def test_schism_mesh(tmpdir, coasts, dem_source, window):
mesh = pmesh.set(
type="tri2d",
geometry=window,
coastlines=natural_earth,
coastlines=coasts,
mesh_generator="jigsaw",
rpath=str(tmpdir) + "/",
)
xg = mesh.Dataset.SCHISM_hgrid_node_x.values
yg = mesh.Dataset.SCHISM_hgrid_node_y.values
dem = pdem.Dem(**window, dem_source=dem_source, adjust_dem=False) # get dem
dem.Dataset = pdem.dem_on_mesh(dem.Dataset, grid_x=xg, grid_y=yg) # get dem on mesh
dem.adjust(natural_earth)
dem.adjust(coasts)
assert np.isnan(dem.Dataset.fval.values).sum() == 0


# D3D mesh
@pytest.mark.delft
@DEM_SOURCES
@WINDOWS
def test_d3d_mesh(tmpdir, natural_earth, dem_source, window):
def test_d3d_mesh(tmpdir, coasts, dem_source, window):
mesh = pmesh.set(type="r2d", geometry=window, resolution=0.1, rpath=str(tmpdir) + "/")
gr = mesh.Dataset
xp, yp = gr.lons.values, gr.lats.values
dem = pdem.Dem(**window, dem_source=dem_source, adjust_dem=False) # get dem
dem.Dataset = pdem.dem_on_mesh(dem.Dataset, grid_x=xp, grid_y=yp) # get dem on mesh
dem.adjust(natural_earth)
dem.adjust(coasts)
assert np.isnan(dem.Dataset.fval.values).sum() == 0
54 changes: 14 additions & 40 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,30 @@
import numpy as np
import pytest
import os
import geopandas as gp
import cartopy.feature as cf

from . import DATA_DIR

cr = "i"
coast = cf.NaturalEarthFeature(
category="physical",
name="land",
scale="{}m".format({"l": 110, "i": 50, "h": 10}[cr]),
)


natural_earth = gp.GeoDataFrame(geometry=[x for x in coast.geometries()])

coast = cf.GSHHSFeature(scale="auto", levels=[1])

GSHHS = gp.GeoDataFrame(geometry=[x for x in coast.geometries()])

COAST_FILE = (DATA_DIR / "ocean.zip").as_posix()

# define the lat/lon window and time frame of interest
window0 = {"lon_min": -30, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 70.0}

window1 = {
"lon_min": 175.0,
"lon_max": 184.0,
"lat_min": -21.5,
"lat_max": -14.5,
} # lat/lon window

window2 = {"lon_min": -20.0, "lon_max": -10.0, "lat_min": 63.0, "lat_max": 67.0}

window3 = {
"lon_min": 175.0 - 360.0,
"lon_max": 184.0 - 360.0,
"lat_min": -21.5,
"lat_max": -14.5,
} # lat/lon window

window4 = {"lon_min": -25.0, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 68.0}
WINDOWS = pytest.mark.parametrize(
"window",
[
{"lon_min": -30, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 70.0},
{"lon_min": 165.0, "lon_max": 185.0, "lat_min": -50, "lat_max": -30},
{"lon_min": -185.0, "lon_max": -165.0, "lat_min": -50, "lat_max": -30},
{"lon_min": -25.0, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 68.0},
],
)


@pytest.mark.parametrize("window", [window0, window1, window2, window3, window4])
@pytest.mark.parametrize("coast", [natural_earth, GSHHS])
@WINDOWS
@pytest.mark.parametrize("ggor", ["jigsaw", "gmsh"])
def test_answer(tmpdir, window, coast, ggor):
def test_answer(tmpdir, window, ggor):

mesh = pmesh.set(
type="tri2d",
geometry=window,
coastlines=coast,
coastlines=COAST_FILE,
rpath=str(tmpdir) + "/",
mesh_generator=ggor,
)
Expand Down
13 changes: 12 additions & 1 deletion tests/test_grid_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest
import os
import geopandas as gp
import cartopy.feature as cf

from . import DATA_DIR

Expand All @@ -21,6 +20,11 @@ def test_io(pytestconfig, tmpdir, ggor, bgmesh, bindings):
if not pytestconfig.getoption("--runslow"):
pytest.skip("slow test")

if ggor == "jigsaw":
cb = 0.001
else:
cb = None

mesh = pmesh.set(
type="tri2d",
geometry="global",
Expand All @@ -29,6 +33,7 @@ def test_io(pytestconfig, tmpdir, ggor, bgmesh, bindings):
mesh_generator=ggor,
dem_source=bgmesh,
use_bindings=bindings,
cbuffer=cb,
)

# save to file
Expand Down Expand Up @@ -63,6 +68,11 @@ def test_validate(pytestconfig, tmpdir, ggor, bgmesh, bindings):
if not pytestconfig.getoption("--runslow"):
pytest.skip("slow test")

if ggor == "jigsaw":
cb = 0.001
else:
cb = None

mesh = pmesh.set(
type="tri2d",
geometry="global",
Expand All @@ -71,6 +81,7 @@ def test_validate(pytestconfig, tmpdir, ggor, bgmesh, bindings):
mesh_generator=ggor,
dem_source=bgmesh,
use_bindings=bindings,
cbuffer=cb,
)

rpath = str(tmpdir) + "/"
Expand Down
14 changes: 5 additions & 9 deletions tests/test_schism_total.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
import os
import multiprocessing
import geopandas as gp
import cartopy.feature as cf

from . import DATA_DIR

DEM_FILE = DATA_DIR / "dem.nc"

coast = cf.GSHHSFeature(scale="auto", levels=[1])

GSHHS = gp.GeoDataFrame(geometry=[x for x in coast.geometries()])

COAST_FILE = (DATA_DIR / "ocean.zip").as_posix()

WINDOWS = pytest.mark.parametrize(
"window",
[
{"lon_min": -30, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 70.0},
{"lon_min": 175.0, "lon_max": 185.0, "lat_min": -21.5, "lat_max": -14.5},
{"lon_min": -185.0, "lon_max": -175.0, "lat_min": -21.5, "lat_max": -14.5},
{"lon_min": 165.0, "lon_max": 185.0, "lat_min": -50, "lat_max": -30},
{"lon_min": -185.0, "lon_max": -165.0, "lat_min": -50, "lat_max": -30},
{"lon_min": -25.0, "lon_max": -10.0, "lat_min": 60.0, "lat_max": 68.0},
],
)
Expand All @@ -32,13 +28,13 @@ def test_schism(tmpdir, window):
dic = {
"solver_name": "schism",
"geometry": window,
"coastlines": GSHHS,
"coastlines": COAST_FILE,
"manning": 0.12,
"windrot": 0.00001,
"tag": "test",
"start_date": "2017-10-1 0:0:0",
"time_frame": "12H",
"mesh_generator": "jigsaw",
"mesh_generator": "gmsh",
"meteo_source": [DATA_DIR / "erai.grib"], # meteo file
"dem_source": DEM_FILE,
"update": ["all"], # update only meteo, keep dem
Expand Down

0 comments on commit bd831e0

Please sign in to comment.