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 ~~~~~~~ diff --git a/imod/formats/common.py b/imod/formats/common.py new file mode 100644 index 000000000..d2fbbdcf2 --- /dev/null +++ b/imod/formats/common.py @@ -0,0 +1,30 @@ +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 if delimited by commas. + + Parameters + ---------- + line : str + The line to analyze. + ncol : int + 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 if delimited by commas. + """ + 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]: diff --git a/imod/tests/test_formats/test_format_common.py b/imod/tests/test_formats/test_format_common.py new file mode 100644 index 000000000..cc9f56824 --- /dev/null +++ b/imod/tests/test_formats/test_format_common.py @@ -0,0 +1,10 @@ +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) + 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)