From 5516c00a31b97d12ac0f68b3bb9a8dad60987485 Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Wed, 6 May 2026 18:46:30 +0200 Subject: [PATCH 1/6] Fix erronous warning thrown about headers for GEN files --- imod/formats/common.py | 29 +++++++++++++++++++++++++++++ imod/formats/gen/gen.py | 4 ++-- imod/formats/ipf.py | 21 ++++++++++++--------- 3 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 imod/formats/common.py diff --git a/imod/formats/common.py b/imod/formats/common.py new file mode 100644 index 000000000..d6312f92a --- /dev/null +++ b/imod/formats/common.py @@ -0,0 +1,29 @@ +import csv + + +def infer_delimwhitespace(line: str, ncol: int): + """ + Infer whether the line is delimited by whitespace or commas, based on the number of columns. + Also returns whether the line has the amount of expected columns. + + Parameters + ---------- + line : str + The line to analyze. + ncol : int + The expected number of columns. + + Returns + ------- + has_whitespace : bool + Whether the line is delimited by whitespace. + has_expected_cols : bool + Whether the line has the expected number of columns. + """ + n_elem = len(next(csv.reader([line]))) + if n_elem == 1: + return True, True + elif n_elem == ncol: + return False, True + else: + return False, False diff --git a/imod/formats/gen/gen.py b/imod/formats/gen/gen.py index ce2ffde30..cbb908358 100644 --- a/imod/formats/gen/gen.py +++ b/imod/formats/gen/gen.py @@ -7,7 +7,7 @@ import pandas as pd from scipy.io import FortranFile, FortranFormattingError -from imod.formats.ipf import _infer_delimwhitespace +from imod.formats.common import infer_delimwhitespace from imod.util.imports import MissingOptionalModule try: @@ -52,7 +52,7 @@ def parse_ascii_segments(lines: List[str]): indices = np.repeat(np.arange(n_feature), n_vertex) first_coord = features[0][1:][0] - has_whitespace = _infer_delimwhitespace(first_coord, 2) + has_whitespace, _ = infer_delimwhitespace(first_coord, 3) sep = r"\s+" if has_whitespace else "," vertex_coords = [] diff --git a/imod/formats/ipf.py b/imod/formats/ipf.py index e5c8d7cb9..4ffb53b15 100644 --- a/imod/formats/ipf.py +++ b/imod/formats/ipf.py @@ -17,20 +17,23 @@ import pandas as pd import imod +from imod.formats.common import infer_delimwhitespace +from imod.logging import LogLevel from imod.util.time import to_pandas_datetime_series def _infer_delimwhitespace(line, ncol): - n_elem = len(next(csv.reader([line]))) - if n_elem == 1: - return True - elif n_elem == ncol: - return False - else: - warnings.warn( - f"Inconsistent IPF: header states {ncol} columns, first line contains {n_elem}" + infer_whitespace, has_expected_cols = infer_delimwhitespace(line, ncol) + + if not has_expected_cols: + log_message = f"Inconsistent IPF: header states {ncol} columns, first line contains {len(line.split())} whitespace-delimited columns and {len(next(csv.reader([line])))} comma-delimited columns." + imod.logging.logger.log( + loglevel=LogLevel.WARNING, + message=log_message, + additional_depth=2, ) - return False + warnings.warn(log_message) + return infer_whitespace def _read_ipf(path, kwargs=None) -> Tuple[pd.DataFrame, int, str]: From 0402848c38b1c148a2b9664b92decfce48426800 Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Wed, 6 May 2026 18:47:15 +0200 Subject: [PATCH 2/6] Update changelog --- docs/api/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/api/changelog.rst b/docs/api/changelog.rst index 0dd6c8165..a1bfc7b5e 100644 --- a/docs/api/changelog.rst +++ b/docs/api/changelog.rst @@ -69,6 +69,7 @@ Fixed :meth:`imod.mf6.Recharge.from_imod5_cap_data`, :meth:`imod.mf6.LayeredWell.from_imod5_cap_data` now regrids the iMOD5 CAP data to the MODFLOW6 target discretization. +- Fixed confusing warning about inconsistent IPF columns when loading GEN files. Changed ~~~~~~~ From 731d872f251655c7bebcc75c5d3d3e3f410d70bb Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Wed, 6 May 2026 18:54:49 +0200 Subject: [PATCH 3/6] Update docstring --- imod/formats/common.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/imod/formats/common.py b/imod/formats/common.py index d6312f92a..d2fbbdcf2 100644 --- a/imod/formats/common.py +++ b/imod/formats/common.py @@ -3,22 +3,23 @@ def infer_delimwhitespace(line: str, ncol: int): """ - Infer whether the line is delimited by whitespace or commas, based on the number of columns. - Also returns whether the line has the amount of expected columns. + Infer whether the line is delimited by whitespace or commas, based on the + number of columns. Also returns whether the line has the amount of expected + columns if delimited by commas. Parameters ---------- line : str The line to analyze. ncol : int - The expected number of columns. + The expected number of columns if line delimited by commas. Returns ------- has_whitespace : bool Whether the line is delimited by whitespace. has_expected_cols : bool - Whether the line has the expected number of columns. + Whether the line has the expected number of columns if delimited by commas. """ n_elem = len(next(csv.reader([line]))) if n_elem == 1: From 48615706c4662fec5a8c91940cb0168840ef9446 Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Wed, 6 May 2026 18:55:10 +0200 Subject: [PATCH 4/6] Add test --- imod/tests/test_formats/test_common.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 imod/tests/test_formats/test_common.py diff --git a/imod/tests/test_formats/test_common.py b/imod/tests/test_formats/test_common.py new file mode 100644 index 000000000..f275c26ba --- /dev/null +++ b/imod/tests/test_formats/test_common.py @@ -0,0 +1,9 @@ +def test_infer_delimwhitespace(): + from imod.formats.gen.gen import infer_delimwhitespace + + assert infer_delimwhitespace("1 2 3", 3) == (True, True) + assert infer_delimwhitespace("1,2,3", 3) == (False, True) + assert infer_delimwhitespace("1,2,3", 4) == (False, False) + assert infer_delimwhitespace("1, 2, 3", 3) == (False, True) + assert infer_delimwhitespace("1\t2\t3", 3) == (True, True) + assert infer_delimwhitespace("1 2,3", 3) == (False, False) From 6cca737558602e8ae2d4f197523c8c79a1d4e63e Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Thu, 7 May 2026 09:10:47 +0200 Subject: [PATCH 5/6] Fix messy import --- imod/tests/test_formats/test_common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/imod/tests/test_formats/test_common.py b/imod/tests/test_formats/test_common.py index f275c26ba..cc9f56824 100644 --- a/imod/tests/test_formats/test_common.py +++ b/imod/tests/test_formats/test_common.py @@ -1,6 +1,7 @@ -def test_infer_delimwhitespace(): - from imod.formats.gen.gen import infer_delimwhitespace +from imod.formats.common import infer_delimwhitespace + +def test_infer_delimwhitespace(): assert infer_delimwhitespace("1 2 3", 3) == (True, True) assert infer_delimwhitespace("1,2,3", 3) == (False, True) assert infer_delimwhitespace("1,2,3", 4) == (False, False) From 4cddcb7ccb24aafec0f619c15ee29ab56843e312 Mon Sep 17 00:00:00 2001 From: JoerivanEngelen Date: Thu, 7 May 2026 09:13:34 +0200 Subject: [PATCH 6/6] Rename test file to fix pytest error --- imod/tests/test_formats/{test_common.py => test_format_common.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename imod/tests/test_formats/{test_common.py => test_format_common.py} (100%) diff --git a/imod/tests/test_formats/test_common.py b/imod/tests/test_formats/test_format_common.py similarity index 100% rename from imod/tests/test_formats/test_common.py rename to imod/tests/test_formats/test_format_common.py