Skip to content

Commit 07c1c30

Browse files
authored
Set points outside grid to undefined (#56)
* Set points outside grid to undefined * Linting * Correct unit test * Linting again * Linting again * Linting again * Linting again * Lint
1 parent 4ba4127 commit 07c1c30

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

pysurfex/interpolation.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,14 @@ def gridpos2points(
326326
)
327327

328328

329-
def grid2points(grid, points, grid_values, operator="bilinear", elev_gradient=None):
329+
def grid2points(
330+
grid,
331+
points,
332+
grid_values,
333+
operator="bilinear",
334+
elev_gradient=None,
335+
max_distance=25000.0,
336+
):
330337
"""Convert a grid to points.
331338
332339
Args:
@@ -335,6 +342,8 @@ def grid2points(grid, points, grid_values, operator="bilinear", elev_gradient=No
335342
grid_values (np.ndarray): Grid values
336343
operator (str, optional): Interpolation operator. Defaults to "bilinear".
337344
elev_gradient (float, optional): Elevation gradient for downscaler
345+
max_distance (float, optional): Maximum distance from grid points.
346+
Defaults to 25000.0.
338347
339348
Raises:
340349
NotImplementedError: Operator not implemented
@@ -355,20 +364,24 @@ def grid2points(grid, points, grid_values, operator="bilinear", elev_gradient=No
355364
values = gridpp.simple_gradient(
356365
grid.grid, points.points, grid_values, elev_gradient, gridpp.Bilinear
357366
)
367+
in_grid = np.array(points.inside_grid(grid, distance=max_distance))
368+
values[in_grid == False] = np.nan # noqa: E712
358369
elif operator == "nearest":
359370
if elev_gradient is None:
360371
values = gridpp.nearest(grid.grid, points.points, grid_values)
361372
else:
362373
values = gridpp.simple_gradient(
363374
grid.grid, points.points, grid_values, elev_gradient, gridpp.Nearest
364375
)
376+
in_grid = np.array(points.inside_grid(grid, distance=max_distance))
377+
values[in_grid == False] = np.nan # noqa: E712
365378
else:
366379
raise NotImplementedError(f"Operator {operator} not implemented!")
367380
return values
368381

369382

370383
def inside_grid(grid_lons, grid_lats, p_lons, p_lats, distance=2500.0):
371-
"""Get number of neighbours.
384+
"""Check if inside grid.
372385
373386
Args:
374387
grid_lons (np.ndarray): Grid longitudes

tests/unit/test_verification.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,28 +430,28 @@ def fixture_data_surfex_nc_file(tmp_path_factory):
430430
431431
BETA = 0 ;
432432
433-
LATORI = 59.1044427299632 ;
433+
LATORI = 59.0 ;
434434
435-
LONORI = 9.09806611390404 ;
435+
LONORI = 9.1 ;
436436
437437
IMAX = 2 ;
438438
439439
JMAX = 3 ;
440440
441441
XX =
442-
10000, 20000, 30000, 40000, 50000, 60000;
442+
40000, 80000, 40000, 80000, 40000, 80000;
443443
444444
YY =
445-
10000, 20000, 30000, 40000, 50000, 60000;
445+
0, 40000, 80000, 0, 40000, 80000;
446446
447447
DX =
448-
10000, 10000, 10000, 10000, 10000, 10000;
448+
40000, 40000, 40000, 40000, 40000, 40000;
449449
450450
DY =
451-
10000, 10000, 10000, 10000, 10000, 10000;
451+
40000, 40000, 40000, 40000, 40000, 40000;
452452
453453
T2M =
454-
285, 285, 285, 285, 285, 285;
454+
281, 282, 283, 284, 285, 286;
455455
456456
HU2M =
457457
0.1, 0.2, 0.3, 0.4, 0.5, 0.6;
@@ -488,7 +488,7 @@ def test_verif_nc(tmp_path_factory, stationlist_file, data_surfex_nc_file):
488488
with working_directory(tmp_path_factory.getbasetemp()):
489489
converter2ds(argv=argv)
490490
ds = xr.open_dataset(vfilename, engine="netcdf4")
491-
assert ds.fcst.data[0][0][1] == 285
491+
assert ds.fcst.data[0][0][1] == 283.0
492492

493493

494494
@pytest.mark.usefixtures("_mockers")

0 commit comments

Comments
 (0)