Skip to content

Commit 3131e97

Browse files
fix(quotes+exe_path+nam_file): fixes for quoted strings, exe path, and nam file (#1645)
* fix(quotes+exe_path+nam_file): Quotes properly removed when loading files with quoted strings. If all else fails trying to find exe_path, abs_path is used. Proper error message given when user attempts to create namefile. * test(quotes): added test for quoting of names in spaces Co-authored-by: jdhughes-usgs <[email protected]>
1 parent 57d0862 commit 3131e97

File tree

7 files changed

+94
-7
lines changed

7 files changed

+94
-7
lines changed

autotest/regression/test_mf6.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ def test005_create_tests_advgw_tidal(tmpdir, example_data_path):
13791379

13801380
obs_dict = {
13811381
("ghb_obs.csv", "binary"): [
1382-
("ghb-2-6-10", "GHB", (1, 5, 9)),
1382+
("ghb- 2-6-10", "GHB", (1, 5, 9)),
13831383
("ghb-3-6-10", "GHB", (2, 5, 9)),
13841384
],
13851385
"ghb_flows.csv": [
@@ -1743,6 +1743,7 @@ def test005_create_tests_advgw_tidal(tmpdir, example_data_path):
17431743
# there should be only one
17441744
assert not found_obs
17451745
found_obs = True
1746+
assert value[0][0] == "ghb- 2-6-10"
17461747
assert found_flows and found_obs
17471748

17481749
# clean up

autotest/test_mf6.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ModflowGwflak,
2626
ModflowGwfmaw,
2727
ModflowGwfmvr,
28+
ModflowGwfnam,
2829
ModflowGwfnpf,
2930
ModflowGwfoc,
3031
ModflowGwfrch,
@@ -41,6 +42,7 @@
4142
ModflowGwtoc,
4243
ModflowGwtssm,
4344
ModflowIms,
45+
ModflowNam,
4446
ModflowTdis,
4547
ModflowUtllaktab,
4648
)
@@ -1570,3 +1572,55 @@ def test_multi_model(tmpdir):
15701572
# save and run updated model
15711573
sim.write_simulation()
15721574
sim.run_simulation()
1575+
1576+
1577+
@requires_exe("mf6")
1578+
def test_namefile_creation(tmpdir):
1579+
test_ex_name = "test_namefile"
1580+
# build MODFLOW 6 files
1581+
sim = MFSimulation(
1582+
sim_name=test_ex_name,
1583+
version="mf6",
1584+
exe_name="mf6",
1585+
sim_ws=str(tmpdir),
1586+
)
1587+
1588+
tdis_rc = [(6.0, 2, 1.0), (6.0, 3, 1.0), (6.0, 3, 1.0), (6.0, 3, 1.0)]
1589+
tdis = ModflowTdis(sim, time_units="DAYS", nper=4, perioddata=tdis_rc)
1590+
ims_package = ModflowIms(
1591+
sim,
1592+
pname="my_ims_file",
1593+
filename=f"{test_ex_name}.ims",
1594+
print_option="ALL",
1595+
complexity="SIMPLE",
1596+
outer_dvclose=0.0001,
1597+
outer_maximum=50,
1598+
under_relaxation="NONE",
1599+
inner_maximum=30,
1600+
inner_dvclose=0.0001,
1601+
linear_acceleration="CG",
1602+
preconditioner_levels=7,
1603+
preconditioner_drop_tolerance=0.01,
1604+
number_orthogonalizations=2,
1605+
)
1606+
model = ModflowGwf(
1607+
sim,
1608+
modelname=test_ex_name,
1609+
model_nam_file="{}.nam".format(test_ex_name),
1610+
)
1611+
1612+
# try to create simulation name file
1613+
ex_happened = False
1614+
try:
1615+
nam = ModflowNam(sim)
1616+
except flopy.mf6.mfbase.FlopyException:
1617+
ex_happened = True
1618+
assert ex_happened
1619+
1620+
# try to create model name file
1621+
ex_happened = False
1622+
try:
1623+
nam = ModflowGwfnam(model)
1624+
except flopy.mf6.mfbase.FlopyException:
1625+
ex_happened = True
1626+
assert ex_happened

flopy/mbase.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,9 @@ def run_model(
17011701
if exe_name.lower().endswith(".exe"):
17021702
# try removing .exe suffix
17031703
exe = which(exe_name[:-4])
1704+
if exe is None:
1705+
# try abspath
1706+
exe = which(os.path.abspath(exe_name))
17041707
if exe is None:
17051708
raise Exception(
17061709
f"The program {exe_name} does not exist or is not executable."

flopy/mf6/mfmodel.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ def __init__(
146146
raise FlopyException(excpt_str)
147147

148148
self.name_file = package_obj(
149-
self, filename=self.model_nam_file, pname=self.name
149+
self,
150+
filename=self.model_nam_file,
151+
pname=self.name,
152+
_internal_package=True,
150153
)
151154

152155
def __init_subclass__(cls):
@@ -1644,7 +1647,10 @@ def register_package(
16441647
package.package_type
16451648
)
16461649
if add_to_package_list and path in self._package_paths:
1647-
if not package_struct.multi_package_support:
1650+
if (
1651+
package_struct is not None
1652+
and not package_struct.multi_package_support
1653+
):
16481654
# package of this type already exists, replace it
16491655
self.remove_package(package.package_type)
16501656
if (
@@ -1685,6 +1691,15 @@ def register_package(
16851691
self._package_paths[path] = 1
16861692

16871693
if package.package_type.lower() == "nam":
1694+
if not package.internal_package:
1695+
excpt_str = (
1696+
"Unable to register nam file. Do not create your own nam "
1697+
"files. Nam files are automatically created and managed "
1698+
"for you by FloPy."
1699+
)
1700+
print(excpt_str)
1701+
raise FlopyException(excpt_str)
1702+
16881703
return path, self.structure.name_file_struct_obj
16891704

16901705
package_extension = package.package_type
@@ -1853,6 +1868,7 @@ def load_package(
18531868
pname=dict_package_name,
18541869
loading_package=True,
18551870
parent_file=parent_package,
1871+
_internal_package=True,
18561872
)
18571873
try:
18581874
package.load(strict)
@@ -1865,6 +1881,7 @@ def load_package(
18651881
pname=dict_package_name,
18661882
loading_package=True,
18671883
parent_file=parent_package,
1884+
_internal_package=True,
18681885
)
18691886
package.load(strict)
18701887

flopy/mf6/mfpackage.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,10 @@ def __init__(
15941594
else:
15951595
self.model_or_sim = parent
15961596
self.parent_file = None
1597+
if "_internal_package" in kwargs and kwargs["_internal_package"]:
1598+
self.internal_package = True
1599+
else:
1600+
self.internal_package = False
15971601
self._data_list = []
15981602
self._package_type = package_type
15991603
if self.model_or_sim.type == "Model" and package_type.lower() != "nam":

flopy/mf6/modflow/mfsimulation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ class MFSimulation(PackageContainer):
336336
version : str
337337
Version of MODFLOW 6 executable
338338
exe_name : str
339-
Relative path to MODFLOW 6 executable from the simulation
340-
working folder.
339+
Path to MODFLOW 6 executable
341340
sim_ws : str
342341
Path to MODFLOW 6 simulation working folder. This is the folder
343342
containing the simulation name file.
@@ -432,6 +431,7 @@ def __init__(
432431
continue_=continue_,
433432
nocheck=nocheck,
434433
memory_print_option=memory_print_option,
434+
_internal_package=True,
435435
)
436436

437437
# try to build directory structure
@@ -1902,6 +1902,14 @@ def register_package(
19021902
# added during ims package registration
19031903
self._add_package(package, path)
19041904
if package.package_type.lower() == "nam":
1905+
if not package.internal_package:
1906+
excpt_str = (
1907+
"Unable to register nam file. Do not create your own nam "
1908+
"files. Nam files are automatically created and managed "
1909+
"for you by FloPy."
1910+
)
1911+
print(excpt_str)
1912+
raise FlopyException(excpt_str)
19051913
return path, self.structure.name_file_struct_obj
19061914
elif package.package_type.lower() == "tdis":
19071915
self._tdis_file = package

flopy/utils/datautil.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ def split_data_line(line, external_file=False, delimiter_conf_length=15):
358358
if item and item[0] in PyListUtil.quote_list:
359359
# starts with a quote, handle quoted text
360360
if item[-1] in PyListUtil.quote_list:
361-
# if quoted on both ends, keep quotes
362-
arr_fixed_line.append(item)
361+
# if quoted on both ends, remove quotes
362+
arr_fixed_line.append(item[1:-1])
363363
else:
364364
arr_fixed_line.append(item[1:])
365365
# loop until trailing quote found

0 commit comments

Comments
 (0)