22
33import sys
44
5+ import numpy as np
56import pytest
67from conftest import EXAMPLES_PATH
78from modflow_devtools .misc import run_cmd
@@ -19,8 +20,73 @@ def pytest_generate_tests(metafunc):
1920 metafunc .parametrize ("example_script" , scripts .values (), ids = scripts .keys ())
2021
2122
23+ def compare_hds (compare_fpth , check_path ):
24+ from flopy .utils import HeadFile
25+
26+ if compare_fpth .suffix == ".hds" :
27+ hds_compare = HeadFile (compare_fpth , precision = "double" ).get_data ()
28+ elif compare_fpth .suffix == ".npy" :
29+ hds_compare = np .load (compare_fpth )
30+
31+ # check *.hds files
32+ for f in check_path .rglob ("*.hds" ):
33+ hds = HeadFile (f , precision = "double" )
34+ assert np .allclose (hds_compare , hds .get_data ())
35+
36+
37+ def compare_grb (compare_fpth , check_path ):
38+ from flopy .mf6 .utils import MfGrdFile
39+
40+ grb_compare = MfGrdFile (compare_fpth )
41+
42+ # check *.grb files
43+ for f in check_path .rglob ("*.grb" ):
44+ grb = MfGrdFile (f )
45+ np .testing .assert_equal (grb_compare ._datadict , grb ._datadict )
46+
47+
48+ def compare_bud (compare_fpth , check_path ):
49+ from flopy .utils .binaryfile import CellBudgetFile
50+
51+ bud_compare = CellBudgetFile (compare_fpth )
52+
53+ # check *.bud and *.cbc files
54+ pattern = ["*.bud" , "*.cbc" ]
55+ files = [f for p in pattern for f in check_path .rglob (p )]
56+ for f in files :
57+ bud = CellBudgetFile (f )
58+ names = bud_compare .get_unique_record_names ()
59+ for n in names :
60+ bud_cmp = bud_compare .get_data (text = n )
61+ bud_chk = bud .get_data (text = n )
62+ if isinstance (bud_cmp [0 ], np .rec .recarray ):
63+ assert len (bud_cmp ) == len (bud_chk )
64+ for ra_cmp , ra in zip (bud_cmp , bud_chk ):
65+ assert len (ra_cmp ) == len (ra )
66+ for i , rec in enumerate (ra_cmp ):
67+ assert all (np .allclose (x , y ) for x , y in zip (rec , ra [i ]))
68+ else :
69+ assert np .allclose (bud_compare .get_data (text = n ), bud .get_data (text = n ))
70+
71+
72+ def compare (example_script ):
73+ from pathlib import Path
74+
75+ check_path = Path (f"{ example_script .parent } /{ example_script .stem } " )
76+ compare_path = Path (f"{ example_script .parent .parent .parent } /test/__compare__/test_examples" )
77+
78+ for f in compare_path .glob (f"{ example_script .stem } .*" ):
79+ if f .suffix == ".bud" or f .suffix == ".cbc" :
80+ compare_bud (f , check_path )
81+ if f .suffix == ".hds" or f .match ("*hds.npy" ):
82+ compare_hds (f , check_path )
83+ if f .suffix == ".grb" :
84+ compare_grb (f , check_path )
85+
86+
2287@pytest .mark .slow
2388def test_scripts (example_script ):
2489 args = [sys .executable , example_script ]
2590 stdout , stderr , retcode = run_cmd (* args , verbose = True )
2691 assert not retcode , stdout + stderr
92+ compare (example_script )
0 commit comments