Skip to content

Commit

Permalink
fix(rename and comments): package rename code and comment propagation (
Browse files Browse the repository at this point in the history
…#1226)

* fix(rename and comments): fixed package rename code (#1133). fixed problem with comments not propagating properly when data switched between internal and external (#1183).

* fix(reformat)

* fix(f strings)

* fix(export test case fix): call write after export so that netcdf file closes properly before object is destroyed
  • Loading branch information
spaulins-usgs authored Sep 3, 2021
1 parent 5d4324d commit c2a01df
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 223 deletions.
8 changes: 5 additions & 3 deletions autotest/t027_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,10 @@ def test_export():

# netDF4 tests
if netCDF4 is not None:
m.wel.export(os.path.join(cpth, "MNW2-Fig28_well.nc"))
m.mnw2.export(os.path.join(cpth, "MNW2-Fig28.nc"))
fcw = m.wel.export(os.path.join(cpth, "MNW2-Fig28_well.nc"))
fcw.write()
fcm = m.mnw2.export(os.path.join(cpth, "MNW2-Fig28.nc"))
fcm.write()
fpth = os.path.join(cpth, "MNW2-Fig28.nc")
nc = netCDF4.Dataset(fpth)
assert np.array_equal(
Expand Down Expand Up @@ -509,7 +511,7 @@ def test_checks():
test_make_well()
test_blank_lines()
test_make_package()
# test_export()
test_export()
# test_checks()
test_mnw1_load_write()
test_mnw2_create_file()
Expand Down
106 changes: 54 additions & 52 deletions autotest/t505_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
os.makedirs(cpth)


def np001():
def test_np001():
# init paths
test_ex_name = "np001"
model_name = "np001_mod"
Expand Down Expand Up @@ -405,7 +405,7 @@ def np001():
budget_file = os.path.join(os.getcwd(), expected_cbc_file)
budget_obj = bf.CellBudgetFile(budget_file, precision="double")
budget_frf_valid = np.array(
budget_obj.get_data(text="FLOW-JA-FACE", full3D=True)
budget_obj.get_data(text="RIV", full3D=False)
)

# compare output to expected results
Expand All @@ -415,11 +415,8 @@ def np001():
assert pymake.compare_heads(
None, None, files1=head_file, files2=head_new, outfile=outfile
)

budget_frf = sim.simulation_data.mfdata[
(model_name, "CBC", "FLOW-JA-FACE")
]
assert array_util.array_comp(budget_frf_valid, budget_frf)
budget_frf = sim.simulation_data.mfdata[(model_name, "CBC", "RIV")]
assert array_util.riv_array_comp(budget_frf_valid, budget_frf)

# clean up
sim.delete_output_files()
Expand All @@ -443,7 +440,7 @@ def np001():
budget_file = os.path.join(os.getcwd(), expected_cbc_file)
budget_obj = bf.CellBudgetFile(budget_file, precision="double")
budget_frf_valid = np.array(
budget_obj.get_data(text="FLOW-JA-FACE", full3D=True)
budget_obj.get_data(text="RIV", full3D=False)
)

# compare output to expected results
Expand All @@ -454,14 +451,21 @@ def np001():
None, None, files1=head_file, files2=head_new, outfile=outfile
)

budget_frf = sim.simulation_data.mfdata[
(model_name, "CBC", "FLOW-JA-FACE")
]
assert array_util.array_comp(budget_frf_valid, budget_frf)
budget_frf = sim.simulation_data.mfdata[(model_name, "CBC", "RIV")]
assert array_util.riv_array_comp(budget_frf_valid, budget_frf)

# clean up
sim.delete_output_files()

# test rename all packages
rename_folder = os.path.join(run_folder, "rename")
sim.rename_all_packages("file_rename")
sim.set_sim_path(rename_folder)
sim.write_simulation()
if run:
sim.run_simulation()
sim.delete_output_files()

try:
error_occurred = False
well_spd = {
Expand Down Expand Up @@ -543,6 +547,7 @@ def np001():
)
wel_package.write()
mpath = sim.simulation_data.mfpath.get_model_path(model.name)
spath = sim.simulation_data.mfpath.get_sim_path()
found_cellid = False
with open(os.path.join(mpath, "np001_mod.wel"), "r") as fd:
for line in fd:
Expand All @@ -560,6 +565,7 @@ def np001():
well_spd = {0: [(-1, -1, -1, -2000.0), (0, 0, 7, -2.0)], 1: []}
wel_package = ModflowGwfwel(
model,
filename="file_rename.wel",
print_input=True,
print_flows=True,
save_flows=True,
Expand All @@ -570,7 +576,7 @@ def np001():
found_begin = False
found_end = False
text_between_begin_and_end = False
with open(os.path.join(mpath, "np001_mod.wel"), "r") as fd:
with open(os.path.join(mpath, "file_rename.wel"), "r") as fd:
for line in fd:
if line.strip().lower() == "begin period 2":
found_begin = True
Expand All @@ -587,7 +593,7 @@ def np001():
test_ex_name,
"mf6",
exe_name,
run_folder,
spath,
write_headers=False,
)
wel = test_sim.get_model().wel
Expand All @@ -611,7 +617,7 @@ def np001():
return


def np002():
def test_np002():
# init paths
test_ex_name = "np002"
model_name = "np002_mod"
Expand Down Expand Up @@ -780,13 +786,6 @@ def np002():
npf_package = model_.get_package("npf")
k = npf_package.k.array

# get expected results
budget_file = os.path.join(os.getcwd(), expected_cbc_file)
budget_obj = bf.CellBudgetFile(budget_file, precision="double")
budget_frf_valid = np.array(
budget_obj.get_data(text="FLOW JA FACE ", full3D=True)
)

# compare output to expected results
head_file = os.path.join(os.getcwd(), expected_head_file)
head_new = os.path.join(run_folder, "np002_mod.hds")
Expand All @@ -795,12 +794,6 @@ def np002():
None, None, files1=head_file, files2=head_new, outfile=outfile
)

array_util = PyListUtil()
budget_frf = sim.simulation_data.mfdata[
(model_name, "CBC", "FLOW-JA-FACE")
]
assert array_util.array_comp(budget_frf_valid, budget_frf)

# verify external text file was written correctly
ext_file_path = os.path.join(run_folder, "initial_heads.txt")
fd = open(ext_file_path, "r")
Expand Down Expand Up @@ -2505,28 +2498,6 @@ def test006_2models_gnc():
stress_period_data=stress_period_data,
)

gncrecarray = testutils.read_gncrecarray(os.path.join(pth, "gnc.txt"))
# test gnc delete
new_gncrecarray = gncrecarray[10:]
gnc_package = ModflowGwfgnc(
sim,
print_input=True,
print_flows=True,
numgnc=26,
numalphaj=1,
gncdata=new_gncrecarray,
)
sim.remove_package(gnc_package.package_type)

gnc_package = ModflowGwfgnc(
sim,
print_input=True,
print_flows=True,
numgnc=36,
numalphaj=1,
gncdata=gncrecarray,
)

exgrecarray = testutils.read_exchangedata(os.path.join(pth, "exg.txt"))

# build obs dictionary
Expand Down Expand Up @@ -2569,6 +2540,28 @@ def test006_2models_gnc():
observations=gwf_obs,
)

gncrecarray = testutils.read_gncrecarray(os.path.join(pth, "gnc.txt"))
# test gnc delete
new_gncrecarray = gncrecarray[10:]
gnc_package = ModflowGwfgnc(
sim,
print_input=True,
print_flows=True,
numgnc=26,
numalphaj=1,
gncdata=new_gncrecarray,
)
sim.remove_package(gnc_package.package_type)

gnc_package = ModflowGwfgnc(
sim,
print_input=True,
print_flows=True,
numgnc=36,
numalphaj=1,
gncdata=gncrecarray,
)

# change folder to save simulation
sim.simulation_data.mfpath.set_sim_path(run_folder)

Expand Down Expand Up @@ -2616,6 +2609,15 @@ def test006_2models_gnc():
# clean up
sim.delete_output_files()

# test rename all packages
rename_folder = os.path.join(run_folder, "rename")
sim.rename_all_packages("file_rename")
sim.set_sim_path(rename_folder)
sim.write_simulation()
if run:
sim.run_simulation()
sim.delete_output_files()

return


Expand Down Expand Up @@ -3228,8 +3230,8 @@ def test_transport():


if __name__ == "__main__":
np001()
np002()
test_np001()
test_np002()
test004_bcfss()
test005_advgw_tidal()
test006_2models_gnc()
Expand Down
Binary file not shown.
10 changes: 6 additions & 4 deletions flopy/mf6/data/mfdatalist.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ def _get_file_entry(
self._crnt_line_num = 1
for mflist_line in range(0, data_lines):
text_line = []

# data
index = 0
self._get_file_entry_record(
data_complete,
Expand All @@ -808,12 +810,12 @@ def _get_file_entry(
indent,
)

# include comments
# comments
if (
mflist_line in storage.comments
and storage.comments[mflist_line].text
mflist_line + 1 in storage.comments
and storage.comments[mflist_line + 1].text
):
text_line.append(storage.comments[mflist_line].text)
text_line.append(storage.comments[mflist_line + 1].text)

file_entry.append(f"{indent}{indent.join(text_line)}\n")
self._crnt_line_num += 1
Expand Down
10 changes: 6 additions & 4 deletions flopy/mf6/data/mfdatastorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,10 +907,8 @@ def _set_list(
elif "data" in data:
data = data["data"]
if isinstance(data, list):
if (
len(data) > 0
and not isinstance(data[0], tuple)
and not isinstance(data[0], list)
if len(data) > 0 and (
not PyListUtil.is_iterable(data[0]) or isinstance(data[0], str)
):
# single line of data needs to be encapsulated in a tuple
data = [tuple(data)]
Expand Down Expand Up @@ -1738,6 +1736,10 @@ def external_to_external(
)

def external_to_internal(self, layer, store_internal=False):
# reset comments
self.pre_data_comments = None
self.comments = {}

if layer is None:
layer = 0
# load data from external file
Expand Down
12 changes: 9 additions & 3 deletions flopy/mf6/data/mffileaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ def _read_pre_data_comments(
arr_line, self._path, self._simulation_data, line_num
)

storage.add_data_line_comment(arr_line, line_num)

line = file_handle.readline()
arr_line = PyListUtil.split_data_line(line)
return line
Expand Down Expand Up @@ -1175,7 +1173,15 @@ def read_list_data_from_file(
# read any pre-data commented lines
while current_line and MFComment.is_comment(arr_line, True):
arr_line.insert(0, "\n")
storage.add_data_line_comment(arr_line, line_num)
if storage.pre_data_comments is None:
storage.pre_data_comments = MFComment(
" ".join(arr_line),
self._path,
self._simulation_data,
line_num,
)
else:
storage.pre_data_comments.add_text(" ".join(arr_line))
PyListUtil.reset_delimiter_used()
current_line = file_handle.readline()
arr_line = PyListUtil.split_data_line(current_line)
Expand Down
31 changes: 31 additions & 0 deletions flopy/mf6/mfbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,37 @@ def _remove_package(self, package):
for key in items_to_remove:
del self.simulation_data.mfdata[key]

def _rename_package(self, package, new_name):
# fix package_name_dict key
if (
package.package_name is not None
and package.package_name.lower() in self.package_name_dict
):
del self.package_name_dict[package.package_name.lower()]
self.package_name_dict[new_name.lower()] = package
# fix package_key_dict key
new_package_path = package.path[:-1] + (new_name,)
del self.package_key_dict[package.path[-1].lower()]
self.package_key_dict[new_package_path.lower()] = package
# get keys to fix in main dictionary
main_dict = self.simulation_data.mfdata
items_to_fix = []
for key in main_dict:
is_subkey = True
for pitem, ditem in zip(package.path, key):
if pitem != ditem:
is_subkey = False
break
if is_subkey:
items_to_fix.append(key)

# fix keys in main dictionary
for key in items_to_fix:
new_key = (
package.path[:-1] + (new_name,) + key[len(package.path) - 1 :]
)
main_dict[new_key] = main_dict.pop(key)

def get_package(self, name=None):
"""
Finds a package by package name, package key, package type, or partial
Expand Down
Loading

0 comments on commit c2a01df

Please sign in to comment.