From 31f42e5ae472263b167764351405f454e7ed1f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dav=C3=ADd=20Brakenhoff?= Date: Thu, 29 May 2025 23:31:38 +0200 Subject: [PATCH] deal with floating point issues --- nlmod/dims/resample.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nlmod/dims/resample.py b/nlmod/dims/resample.py index 62a47df3..4a8cc0b8 100644 --- a/nlmod/dims/resample.py +++ b/nlmod/dims/resample.py @@ -13,7 +13,7 @@ logger = logging.getLogger(__name__) -def get_xy_mid_structured(extent, delr, delc, descending_y=True): +def get_xy_mid_structured(extent, delr, delc, descending_y=True, tiny=1e-10): """Calculates the x and y coordinates of the cell centers of a structured grid. Parameters @@ -27,6 +27,9 @@ def get_xy_mid_structured(extent, delr, delc, descending_y=True): descending_y : bool, optional if True the resulting ymid array is in descending order. This is the default for MODFLOW models. default is True. + tiny : float, optional + A small value (for floating point reasons) to check if the extent is valid. + The default is 1e-10. Returns ------- @@ -40,12 +43,12 @@ def get_xy_mid_structured(extent, delr, delc, descending_y=True): raise TypeError("if delr is a number delc should be a number as well") # check if extent is valid - if (extent[1] - extent[0]) % delr != 0.0: + if (extent[1] - extent[0]) % delr > tiny: raise ValueError( "invalid extent, the extent should contain an integer" " number of cells in the x-direction" ) - if (extent[3] - extent[2]) % delc != 0.0: + if (extent[3] - extent[2]) % delc > tiny: raise ValueError( "invalid extent, the extent should contain an integer" " number of cells in the y-direction"