From e3873a5b1a17a3cc37d567f6bfe84d7f4333b6e8 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Thu, 5 Nov 2026 15:45:33 -0800 Subject: [PATCH] keep blocks ending with a period number when trimming trailing text When a file failed to parse, the reader cut it back to the last block _BLOCK_RE recognized and parsed again. The regex allowed one word after END, so END PERIOD 1 didn't end a block: the trim removed every period block and the second parse succeeded, silently losing the data. Allow anything after END, as MF6 does. Warn when the trim drops text, with the file and the lines. Update test021_twri's snapshots, which recorded its RCHA without its recharge. Co-Authored-By: Claude Opus 5.5 --- flopy4/mf6/codec/reader/__init__.py | 41 +++++++++---- .../test_roundtrip[mf6testtest021_twri].json | 8 ++- ..._load_simulation[mf6testtest021_twri].json | 3 + test/mf6/test_mf6_reader.py | 57 +++++++++++++++++++ 4 files changed, 97 insertions(+), 12 deletions(-) diff --git a/flopy4/mf6/codec/reader/__init__.py b/flopy4/mf6/codec/reader/__init__.py index 4f3cc87eb..1f788d28e 100644 --- a/flopy4/mf6/codec/reader/__init__.py +++ b/flopy4/mf6/codec/reader/__init__.py @@ -2,6 +2,7 @@ import mmap import os import re +import warnings from typing import IO, Any from lark.exceptions import LarkError @@ -19,7 +20,7 @@ # each well-formed block in turn; used as a fallback to trim anything # after the last one when the file fails to parse as-is. _BLOCK_RE = re.compile( - r"^[ \t]*begin\s+\S+.*?^[ \t]*end\s+\S+[ \t]*$", + r"^[ \t]*begin\s+\S+.*?^[ \t]*end(?:[ \t][^\n]*)?$", re.IGNORECASE | re.MULTILINE | re.DOTALL, ) @@ -32,6 +33,18 @@ def _trim_trailing_garbage(data: str) -> str: return data[: matches[-1].end()] +def _warn_trimmed(data: str, trimmed: str, name: str | None) -> None: + """Say which lines `_trim_trailing_garbage` left out of a file that + parsed without them.""" + start = trimmed.count("\n") + 1 + rest = data[len(trimmed) :].split("\n") + lines = [number for number, line in enumerate(rest, start) if line.strip()] + if not lines: + return + where = f"line {lines[0]}" if len(lines) == 1 else f"lines {lines[0]}-{lines[-1]}" + warnings.warn(f"{name or 'MF6 input'}: ignoring text after the last block ({where})") + + def load(fp: IO[str], *, component: str | None = None) -> Any: """ Load and parse an MF6 input file. @@ -50,9 +63,10 @@ def load(fp: IO[str], *, component: str | None = None) -> Any: Any Parsed MF6 input file structure """ + name = getattr(fp, "name", None) if component is None and (mapped := _map(fp)) is not None: - return _parse_basic(*mapped) - return loads(fp.read(), component=component) + return _parse_basic(*mapped, name=name) + return loads(fp.read(), component=component, name=name) # Files at least this big are mapped (see `_map`): a mapping costs ~4 ms @@ -83,7 +97,7 @@ def _map(fp: IO[str]) -> tuple[mmap.mmap, str] | None: return buffer, encoding -def loads(data: str, *, component: str | None = None) -> Any: +def loads(data: str, *, component: str | None = None, name: str | None = None) -> Any: """ Parse MF6 input file content from string. @@ -95,6 +109,8 @@ def loads(data: str, *, component: str | None = None) -> Any: DFN name (e.g. ``"gwf-wel"``) of the file's package/component. If given, parses with that component's typed grammar/transformer instead of the generic basic grammar. + name : + What to call the content in warnings, e.g. the file it came from. Returns ------- @@ -102,16 +118,16 @@ def loads(data: str, *, component: str | None = None) -> Any: Parsed MF6 input file structure """ if component is not None: - return loads_typed(data, component) - return loads_basic(data) + return loads_typed(data, component, name=name) + return loads_basic(data, name=name) -def loads_basic(data: str) -> Any: +def loads_basic(data: str, *, name: str | None = None) -> Any: """Parse MF6 input file content with the generic, untyped grammar.""" - return _parse_basic(data.encode(), "utf-8") + return _parse_basic(data.encode(), "utf-8", name=name) -def _parse_basic(buffer: bytes | mmap.mmap, encoding: str) -> Any: +def _parse_basic(buffer: bytes | mmap.mmap, encoding: str, *, name: str | None = None) -> Any: """Parse text's bytes, or a buffer of them, in `encoding`.""" token = SOURCE.set((buffer, encoding)) try: @@ -121,12 +137,14 @@ def _parse_basic(buffer: bytes | mmap.mmap, encoding: str) -> Any: trimmed = _trim_trailing_garbage(data) if trimmed == data: raise - return _parse_basic(trimmed.encode(encoding), encoding) + parsed = _parse_basic(trimmed.encode(encoding), encoding) + _warn_trimmed(data, trimmed, name) + return parsed finally: SOURCE.reset(token) -def loads_typed(data: str, component: str) -> Any: +def loads_typed(data: str, component: str, *, name: str | None = None) -> Any: """Parse MF6 input file content with ``component``'s typed grammar.""" parser = get_typed_parser(component) transformer = get_typed_transformer() @@ -137,4 +155,5 @@ def loads_typed(data: str, component: str) -> Any: if trimmed == data: raise tree = parser.parse(trimmed) + _warn_trimmed(data, trimmed, name) return transformer.transform(tree) diff --git a/test/mf6/__snapshots__/test_mf6_io_roundtrip/test_roundtrip[mf6testtest021_twri].json b/test/mf6/__snapshots__/test_mf6_io_roundtrip/test_roundtrip[mf6testtest021_twri].json index 8d218494e..02693db09 100644 --- a/test/mf6/__snapshots__/test_mf6_io_roundtrip/test_roundtrip[mf6testtest021_twri].json +++ b/test/mf6/__snapshots__/test_mf6_io_roundtrip/test_roundtrip[mf6testtest021_twri].json @@ -151,7 +151,13 @@ "BEGIN OPTIONS", " READASARRAYS", " FIXED_CELL", - "END OPTIONS" + "END OPTIONS", + "", + "BEGIN PERIOD 1", + " RECHARGE", + " CONSTANT 3e-08", + "", + "END PERIOD 1" ], "twri.tdis": [ "BEGIN OPTIONS", diff --git a/test/mf6/__snapshots__/test_mf6_load_all_models/test_load_simulation[mf6testtest021_twri].json b/test/mf6/__snapshots__/test_mf6_load_all_models/test_load_simulation[mf6testtest021_twri].json index 31b1e5452..6cf91f804 100644 --- a/test/mf6/__snapshots__/test_mf6_load_all_models/test_load_simulation[mf6testtest021_twri].json +++ b/test/mf6/__snapshots__/test_mf6_load_all_models/test_load_simulation[mf6testtest021_twri].json @@ -264,6 +264,9 @@ "print_flows": false, "print_input": false, "readasarrays": true, + "recharge": { + "0": "float64[1] c23d0b6f19b9c381" + }, "save_flows": false, "tas": [] }, diff --git a/test/mf6/test_mf6_reader.py b/test/mf6/test_mf6_reader.py index ed1127c5c..4a379f404 100644 --- a/test/mf6/test_mf6_reader.py +++ b/test/mf6/test_mf6_reader.py @@ -1074,3 +1074,60 @@ def test_transform_record_header(): keys = ["continuous FILEOUT Heads.csv", "continuous FILEOUT flows.bsv BINARY"] assert list(loads_typed(text, "utl-obs")) == keys assert [k.lower() for k in loads(text)] == [k.lower() for k in keys] + + +TRAILING = "\n 1 0 NRCHOP,IRCHBD\n 1 INRECH\n" + + +@pytest.mark.parametrize("end", ["END PERIOD", "END PERIOD 1", "end period 1", "END PERIOD 1 "]) +def test_text_after_the_last_block_keeps_the_blocks(end): + """mf5to6 can leave MODFLOW-2005 lines after the last block, which MF6 ignores. + Trimming them mustn't take a block whose END has a period number with them.""" + from flopy4.mf6.codec.reader import loads_typed + + text = ( + "BEGIN OPTIONS\nEND OPTIONS\n" + "BEGIN DIMENSIONS\n MAXBOUND 1\nEND DIMENSIONS\n" + f"BEGIN PERIOD 1\n 1 1 1 -1.0\n{end}\n" + ) + clean = loads_typed(text, "gwf-wel") + assert "period 1" in clean + assert list(loads_typed(text + TRAILING, "gwf-wel")) == list(clean) + + +@pytest.mark.parametrize("component", [None, "gwf-wel"]) +@pytest.mark.parametrize("mapped", [False, True]) +def test_trimmed_text_warns_with_the_file_and_lines(tmp_path, monkeypatch, component, mapped): + from flopy4.mf6.codec import reader + + if mapped: + monkeypatch.setattr(reader, "_MAP_SIZE", 1) + text = ( + "BEGIN OPTIONS\nEND OPTIONS\n" + "BEGIN DIMENSIONS\n MAXBOUND 1\nEND DIMENSIONS\n" + "BEGIN PERIOD 1\n 1 1 1 -1.0\nEND PERIOD 1\n" + ) + path = tmp_path / "x.wel" + path.write_text(text + TRAILING) + with open(path) as f, pytest.warns(UserWarning) as caught: + reader.load(f, component=component) + assert [str(w.message) for w in caught] == [ + f"{path}: ignoring text after the last block (lines 10-11)" + ] + + +@pytest.mark.parametrize("component", [None, "gwf-wel"]) +def test_trim_stays_quiet_unless_it_dropped_text_from_a_file_that_parses(component, recwarn): + from lark.exceptions import LarkError + + from flopy4.mf6.codec.reader import loads + + block = "BEGIN PERIOD 1\n 1 1 1 -1.0\nEND PERIOD 1\n" + loads(block, component=component) + # the retry fails too, so this is an error rather than ignored text + with pytest.raises(LarkError): + loads( + f"{block}stray words\nBEGIN PERIOD 2\n 1 1 1 -1.0\nEND PERIOD 2\n{TRAILING}", + component=component, + ) + assert not recwarn.list