Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions flopy4/mf6/codec/reader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import mmap
import os
import re
import warnings
from typing import IO, Any

from lark.exceptions import LarkError
Expand All @@ -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,
)

Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -95,23 +109,25 @@ 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
-------
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:
Expand All @@ -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()
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@
"print_flows": false,
"print_input": false,
"readasarrays": true,
"recharge": {
"0": "float64[1] c23d0b6f19b9c381"
},
"save_flows": false,
"tas": []
},
Expand Down
57 changes: 57 additions & 0 deletions test/mf6/test_mf6_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading