Skip to content

Commit

Permalink
916 make test read polyfile as gdf points not local (#917)
Browse files Browse the repository at this point in the history
* made test_read_polyfile_as_gdf_points a non-local test

* also add test for linestrings
  • Loading branch information
veenstrajelmer authored Aug 2, 2024
1 parent e898206 commit a83d940
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
54 changes: 54 additions & 0 deletions tests/test_hydrolib_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
@author: veenstra
"""

import os
import pytest
import numpy as np
import geopandas as gpd
import dfm_tools as dfmt
import hydrolib.core.dflowfm as hcdfm
from dfm_tools.hydrolib_helpers import get_ncbnd_construct


@pytest.mark.unittest
Expand Down Expand Up @@ -135,3 +139,53 @@ def test_geodataframe_to_PolyFile_namecolumn_numeric_start(bnd_gdf):
assert 'names in polyfile do not all start with a letter' in str(e.value)


@pytest.mark.unittest
def test_read_polyfile_as_gdf_points_linestrings(tmp_path):
ncbnd_construct = get_ncbnd_construct()
varn_pointname = ncbnd_construct['varn_pointname']

# write a dummy polyfile with first three points of reference file
# p:\archivedprojects\11208054-004-dcsm-fm\models\model_input\bnd_cond\pli\DCSM-FM_OB_all_20181108.pli
poly_str = """DCSM-FM_OB_all_20181108
3 2
-9.25 43.0
-9.50 43.0
-9.75 43.0
"""
file_pli = os.path.join(tmp_path, "dummy.pli")
with open(file_pli, "w") as f:
f.write(poly_str)

# read polyfile with hydrolib-core
polyfile_object = hcdfm.PolyFile(file_pli)

# convert to geodataframe with points
gdf_points = dfmt.PolyFile_to_geodataframe_points(polyfile_object)

reference_names = ['DCSM-FM_OB_all_20181108_0001', 'DCSM-FM_OB_all_20181108_0002', 'DCSM-FM_OB_all_20181108_0003']
reference_x = np.array([-9.25, -9.5 , -9.75])
reference_y = np.array([43., 43., 43.])

assert isinstance(gdf_points, gpd.GeoDataFrame)
assert len(gdf_points) == 3
assert (gdf_points.geometry.type == "Point").all()
assert np.allclose(gdf_points.geometry.x.to_numpy(), reference_x)
assert np.allclose(gdf_points.geometry.y.to_numpy(), reference_y)
assert varn_pointname in gdf_points.columns
assert gdf_points[varn_pointname].tolist() == reference_names

# convert to geodataframe with linestrings
gdf_lines = dfmt.PolyFile_to_geodataframe_linestrings(polyfile_object)
line0_geom = gdf_lines.geometry.iloc[0]

reference_names = ['DCSM-FM_OB_all_20181108']
reference_x = np.array([-9.25, -9.5 , -9.75])
reference_y = np.array([43., 43., 43.])

assert isinstance(gdf_lines, gpd.GeoDataFrame)
assert len(gdf_lines) == 1
assert (gdf_lines.geometry.type == "LineString").all()
assert np.allclose(line0_geom.xy[0], reference_x)
assert np.allclose(line0_geom.xy[1], reference_y)
assert 'name' in gdf_lines.columns
assert gdf_lines['name'].tolist() == reference_names
30 changes: 4 additions & 26 deletions tests/test_interpolate_grid2bnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import geopandas as gpd
from dfm_tools.interpolate_grid2bnd import (tidemodel_componentlist,
components_translate_upper,
get_ncbnd_construct,
interp_regularnc_to_plipointsDataset,
check_time_extent,
ext_add_boundary_object_per_polyline,
Expand All @@ -25,7 +24,7 @@
ds_apply_conventions,
ds_apply_conversion_dict,
)
from dfm_tools.hydrolib_helpers import PolyFile_to_geodataframe_points
from dfm_tools.hydrolib_helpers import get_ncbnd_construct
import hydrolib.core.dflowfm as hcdfm
from dfm_tools.errors import OutOfRangeError

Expand Down Expand Up @@ -177,7 +176,7 @@ def test_interpolate_nc_to_bc():

# read polyfile as geodataframe
polyfile_object = hcdfm.PolyFile(file_pli)
gdf_points_all = PolyFile_to_geodataframe_points(polyfile_object)
gdf_points_all = dfmt.PolyFile_to_geodataframe_points(polyfile_object)
gdf_points = gdf_points_all.iloc[:npoints]

tstart = '2012-12-16 12:00'
Expand Down Expand Up @@ -211,7 +210,7 @@ def test_plipointsDataset_to_ForcingModel_drop_allnan_points():
poly_object = dfmt.DataFrame_to_PolyObject(polyobject_pd, name="abc_bnd")
polyfile_object = hcdfm.PolyFile()
polyfile_object.objects.append(poly_object)
gdf_points = PolyFile_to_geodataframe_points(polyfile_object)
gdf_points = dfmt.PolyFile_to_geodataframe_points(polyfile_object)

# actual cmems data
no3_values = [[[ np.nan, np.nan, np.nan, np.nan,
Expand Down Expand Up @@ -445,7 +444,7 @@ def test_interpolate_tide_to_plipoints():

# read polyfile as geodataframe
polyfile_object = hcdfm.PolyFile(file_pli)
gdf_points_all = PolyFile_to_geodataframe_points(polyfile_object)
gdf_points_all = dfmt.PolyFile_to_geodataframe_points(polyfile_object)
gdf_points = gdf_points_all.iloc[:npoints]

tidemodel_list = ['tpxo80_opendap', 'FES2014', 'FES2012', 'EOT20', 'GTSMv4.1']#, 'GTSMv4.1_opendap']
Expand Down Expand Up @@ -519,27 +518,6 @@ def test_interpolate_tide_to_forcingmodel():
assert forcing0.quantityunitpair[2].quantity == 'waterlevelbnd phase'


@pytest.mark.systemtest
@pytest.mark.requireslocaldata
def test_read_polyfile_as_gdf_points():
ncbnd_construct = get_ncbnd_construct()
varn_pointname = ncbnd_construct['varn_pointname']

npoints = 3
file_pli = r'p:\archivedprojects\11208054-004-dcsm-fm\models\model_input\bnd_cond\pli\DCSM-FM_OB_all_20181108.pli'

# read polyfile as geodataframe
polyfile_object = hcdfm.PolyFile(file_pli)
gdf_points_all = PolyFile_to_geodataframe_points(polyfile_object)
gdf_points = gdf_points_all.iloc[:npoints]

reference = data_dcsm_gdf()

assert isinstance(gdf_points, gpd.GeoDataFrame)
assert (gdf_points.geometry == reference.geometry).all()
assert gdf_points[varn_pointname].tolist() == reference[varn_pointname].tolist()


@pytest.mark.unittest
def test_interp_uds_to_plipoints():
"""
Expand Down

0 comments on commit a83d940

Please sign in to comment.