Skip to content

Commit

Permalink
fix(quotes+exe_path+nam_file): fixes for quoted strings, exe path, an…
Browse files Browse the repository at this point in the history
…d nam file (modflowpy#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]>
  • Loading branch information
2 people authored and wpbonelli committed Dec 14, 2022
1 parent 5bfa2bc commit 37d5038
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
3 changes: 2 additions & 1 deletion autotest/regression/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ def test005_create_tests_advgw_tidal(tmpdir, example_data_path):

obs_dict = {
("ghb_obs.csv", "binary"): [
("ghb-2-6-10", "GHB", (1, 5, 9)),
("ghb- 2-6-10", "GHB", (1, 5, 9)),
("ghb-3-6-10", "GHB", (2, 5, 9)),
],
"ghb_flows.csv": [
Expand Down Expand Up @@ -1743,6 +1743,7 @@ def test005_create_tests_advgw_tidal(tmpdir, example_data_path):
# there should be only one
assert not found_obs
found_obs = True
assert value[0][0] == "ghb- 2-6-10"
assert found_flows and found_obs

# clean up
Expand Down
54 changes: 54 additions & 0 deletions autotest/test_mf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ModflowGwflak,
ModflowGwfmaw,
ModflowGwfmvr,
ModflowGwfnam,
ModflowGwfnpf,
ModflowGwfoc,
ModflowGwfrch,
Expand All @@ -41,6 +42,7 @@
ModflowGwtoc,
ModflowGwtssm,
ModflowIms,
ModflowNam,
ModflowTdis,
ModflowUtllaktab,
)
Expand Down Expand Up @@ -1570,3 +1572,55 @@ def test_multi_model(tmpdir):
# save and run updated model
sim.write_simulation()
sim.run_simulation()


@requires_exe("mf6")
def test_namefile_creation(tmpdir):
test_ex_name = "test_namefile"
# build MODFLOW 6 files
sim = MFSimulation(
sim_name=test_ex_name,
version="mf6",
exe_name="mf6",
sim_ws=str(tmpdir),
)

tdis_rc = [(6.0, 2, 1.0), (6.0, 3, 1.0), (6.0, 3, 1.0), (6.0, 3, 1.0)]
tdis = ModflowTdis(sim, time_units="DAYS", nper=4, perioddata=tdis_rc)
ims_package = ModflowIms(
sim,
pname="my_ims_file",
filename=f"{test_ex_name}.ims",
print_option="ALL",
complexity="SIMPLE",
outer_dvclose=0.0001,
outer_maximum=50,
under_relaxation="NONE",
inner_maximum=30,
inner_dvclose=0.0001,
linear_acceleration="CG",
preconditioner_levels=7,
preconditioner_drop_tolerance=0.01,
number_orthogonalizations=2,
)
model = ModflowGwf(
sim,
modelname=test_ex_name,
model_nam_file="{}.nam".format(test_ex_name),
)

# try to create simulation name file
ex_happened = False
try:
nam = ModflowNam(sim)
except flopy.mf6.mfbase.FlopyException:
ex_happened = True
assert ex_happened

# try to create model name file
ex_happened = False
try:
nam = ModflowGwfnam(model)
except flopy.mf6.mfbase.FlopyException:
ex_happened = True
assert ex_happened
3 changes: 3 additions & 0 deletions flopy/mbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,9 @@ def run_model(
if exe_name.lower().endswith(".exe"):
# try removing .exe suffix
exe = which(exe_name[:-4])
if exe is None:
# try abspath
exe = which(os.path.abspath(exe_name))
if exe is None:
raise Exception(
f"The program {exe_name} does not exist or is not executable."
Expand Down
21 changes: 19 additions & 2 deletions flopy/mf6/mfmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def __init__(
raise FlopyException(excpt_str)

self.name_file = package_obj(
self, filename=self.model_nam_file, pname=self.name
self,
filename=self.model_nam_file,
pname=self.name,
_internal_package=True,
)

def __init_subclass__(cls):
Expand Down Expand Up @@ -1644,7 +1647,10 @@ def register_package(
package.package_type
)
if add_to_package_list and path in self._package_paths:
if not package_struct.multi_package_support:
if (
package_struct is not None
and not package_struct.multi_package_support
):
# package of this type already exists, replace it
self.remove_package(package.package_type)
if (
Expand Down Expand Up @@ -1685,6 +1691,15 @@ def register_package(
self._package_paths[path] = 1

if package.package_type.lower() == "nam":
if not package.internal_package:
excpt_str = (
"Unable to register nam file. Do not create your own nam "
"files. Nam files are automatically created and managed "
"for you by FloPy."
)
print(excpt_str)
raise FlopyException(excpt_str)

return path, self.structure.name_file_struct_obj

package_extension = package.package_type
Expand Down Expand Up @@ -1853,6 +1868,7 @@ def load_package(
pname=dict_package_name,
loading_package=True,
parent_file=parent_package,
_internal_package=True,
)
try:
package.load(strict)
Expand All @@ -1865,6 +1881,7 @@ def load_package(
pname=dict_package_name,
loading_package=True,
parent_file=parent_package,
_internal_package=True,
)
package.load(strict)

Expand Down
4 changes: 4 additions & 0 deletions flopy/mf6/mfpackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,10 @@ def __init__(
else:
self.model_or_sim = parent
self.parent_file = None
if "_internal_package" in kwargs and kwargs["_internal_package"]:
self.internal_package = True
else:
self.internal_package = False
self._data_list = []
self._package_type = package_type
if self.model_or_sim.type == "Model" and package_type.lower() != "nam":
Expand Down
12 changes: 10 additions & 2 deletions flopy/mf6/modflow/mfsimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ class MFSimulation(PackageContainer):
version : str
Version of MODFLOW 6 executable
exe_name : str
Relative path to MODFLOW 6 executable from the simulation
working folder.
Path to MODFLOW 6 executable
sim_ws : str
Path to MODFLOW 6 simulation working folder. This is the folder
containing the simulation name file.
Expand Down Expand Up @@ -432,6 +431,7 @@ def __init__(
continue_=continue_,
nocheck=nocheck,
memory_print_option=memory_print_option,
_internal_package=True,
)

# try to build directory structure
Expand Down Expand Up @@ -1902,6 +1902,14 @@ def register_package(
# added during ims package registration
self._add_package(package, path)
if package.package_type.lower() == "nam":
if not package.internal_package:
excpt_str = (
"Unable to register nam file. Do not create your own nam "
"files. Nam files are automatically created and managed "
"for you by FloPy."
)
print(excpt_str)
raise FlopyException(excpt_str)
return path, self.structure.name_file_struct_obj
elif package.package_type.lower() == "tdis":
self._tdis_file = package
Expand Down
4 changes: 2 additions & 2 deletions flopy/utils/datautil.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ def split_data_line(line, external_file=False, delimiter_conf_length=15):
if item and item[0] in PyListUtil.quote_list:
# starts with a quote, handle quoted text
if item[-1] in PyListUtil.quote_list:
# if quoted on both ends, keep quotes
arr_fixed_line.append(item)
# if quoted on both ends, remove quotes
arr_fixed_line.append(item[1:-1])
else:
arr_fixed_line.append(item[1:])
# loop until trailing quote found
Expand Down

0 comments on commit 37d5038

Please sign in to comment.