Skip to content

Commit

Permalink
feat(mbase): suppress duplicate package warning if verbose is False (#…
Browse files Browse the repository at this point in the history
…908)

Add similar suppression for other informative messages in mbase, mf,
and mt.

Closes #345
  • Loading branch information
jdhughes-usgs committed Jun 9, 2020
1 parent f539d07 commit 4fc61d5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 46 deletions.
8 changes: 4 additions & 4 deletions autotest/t015_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

def test_str_free():
m = flopy.modflow.Modflow.load(str_items[0]['mfnam'], exe_name=mfexe,
model_ws=path, verbose=True, check=False)
model_ws=path, verbose=False, check=False)
ws = tpth
m.change_model_ws(ws)

Expand Down Expand Up @@ -91,7 +91,7 @@ def test_str_free():
# load the fixed format model with aux variables
try:
m2 = flopy.modflow.Modflow.load(str_items[0]['mfnam'], exe_name=mfexe,
model_ws=ws, verbose=True, check=False)
model_ws=ws, verbose=False, check=False)
except:
m2 = None

Expand All @@ -112,7 +112,7 @@ def test_str_free():
# load the free format model
try:
m2 = flopy.modflow.Modflow.load(str_items[0]['mfnam'], exe_name=mfexe,
model_ws=ws, verbose=True, check=False)
model_ws=ws, verbose=False, check=False)
except:
m2 = None

Expand All @@ -139,4 +139,4 @@ def test_str_plot():

if __name__ == '__main__':
test_str_free()
# test_str_plot()
test_str_plot()
64 changes: 37 additions & 27 deletions flopy/mbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from .discretization.modeltime import ModelTime
from .discretization.grid import Grid


# Global variables
iconst = 1 # Multiplier for individual array elements in integer and real arrays read by MODFLOW's U2DREL, U1DREL and U2DINT.
iprn = -1 # Printout flag. If >= 0 then array values read are printed in listing file.
Expand Down Expand Up @@ -535,17 +534,20 @@ def add_package(self, p):
pn = p.name[idx]
except:
pn = p.name
msg = "WARNING: unit {} ".format(u) + \
"of package {} already in use".format(pn)
print(msg)
if self.verbose:
msg = "\nWARNING:\n unit {} ".format(u) + \
"of package {} ".format(pn) + \
"already in use."
print(msg)
self.package_units.append(u)
for i, pp in enumerate(self.packagelist):
if pp.allowDuplicates:
continue
elif isinstance(p, type(pp)):
print('****Warning -- two packages of the same type: ',
type(p), type(pp))
print('replacing existing Package...')
if self.verbose:
print("\nWARNING:\n Two packages of the same type, " +
"Replacing existing " +
"'{}' package.".format(p.name[0]))
self.packagelist[i] = p
return
if self.verbose:
Expand Down Expand Up @@ -620,7 +622,8 @@ def __getattr__(self, item):
return self.dis.start_datetime
else:
return None
#return self.get_package(item)

# return self.get_package(item)
# to avoid infinite recursion
if item == "_packagelist" or item == "packagelist":
raise AttributeError(item)
Expand Down Expand Up @@ -745,8 +748,10 @@ def add_output(self, fname, unit, binflag=False, package=None):
"""
if fname in self.output_fnames:
print("BaseModel.add_output() warning: " +
"replacing existing filename {0}".format(fname))
if self.verbose:
msg = "BaseModel.add_output() warning: " + \
"replacing existing filename {}".format(fname)
print(msg)
idx = self.output_fnames.index(fname)
if self.verbose:
self._output_msg(idx, add=False)
Expand Down Expand Up @@ -800,8 +805,8 @@ def remove_output(self, fname=None, unit=None):
self.output_binflag.pop(i)
self.output_packages.pop(i)
else:
raise Exception(
' either fname or unit must be passed to remove_output()')
msg = ' either fname or unit must be passed to remove_output()'
raise Exception(msg)
return

def get_output(self, fname=None, unit=None):
Expand All @@ -828,8 +833,8 @@ def get_output(self, fname=None, unit=None):
return self.output_fnames[i]
return None
else:
raise Exception(
' either fname or unit must be passed to get_output()')
msg = ' either fname or unit must be passed to get_output()'
raise Exception(msg)
return

def set_output_attribute(self, fname=None, unit=None, attr=None):
Expand Down Expand Up @@ -859,9 +864,9 @@ def set_output_attribute(self, fname=None, unit=None, attr=None):
idx = i
break
else:
raise Exception(
' either fname or unit must be passed ' +
' to set_output_attribute()')
msg = ' either fname or unit must be passed ' + \
' to set_output_attribute()'
raise Exception(msg)
if attr is not None:
if idx is not None:
for key, value in attr.items:
Expand Down Expand Up @@ -930,16 +935,20 @@ def add_external(self, fname, unit, binflag=False, output=False):
"""
if fname in self.external_fnames:
print("BaseModel.add_external() warning: " +
"replacing existing filename {}".format(fname))
if self.verbose:
msg = "BaseModel.add_external() warning: " + \
"replacing existing filename {}".format(fname)
print(msg)
idx = self.external_fnames.index(fname)
self.external_fnames.pop(idx)
self.external_units.pop(idx)
self.external_binflag.pop(idx)
self.external_output.pop(idx)
if unit in self.external_units:
print("BaseModel.add_external() warning: " +
"replacing existing unit {}".format(unit))
if self.verbose:
msg = "BaseModel.add_external() warning: " + \
"replacing existing unit {}".format(unit)
print(msg)
idx = self.external_units.index(unit)
self.external_fnames.pop(idx)
self.external_units.pop(idx)
Expand Down Expand Up @@ -975,8 +984,8 @@ def remove_external(self, fname=None, unit=None):
if u == unit:
plist.append(i)
else:
raise Exception(
' either fname or unit must be passed to remove_external()')
msg = ' either fname or unit must be passed to remove_external()'
raise Exception(msg)
# remove external file
j = 0
for i in plist:
Expand Down Expand Up @@ -1048,8 +1057,9 @@ def get_name_file_entries(self):
for i in range(len(p.name)):
if p.unit_number[i] == 0:
continue
s = '{:14s} {:5d} {}'.format(
p.name[i], p.unit_number[i], p.file_name[i])
s = '{:14s} '.format(p.name[i]) + \
'{:5d} '.format(p.unit_number[i]) + \
'{}'.format(p.file_name[i])
if p.extra[i]:
s += ' ' + p.extra[i]
lines.append(s)
Expand Down Expand Up @@ -1107,8 +1117,8 @@ def set_version(self, version):

# check that this is a valid model version
if self.version not in list(self.version_types.keys()):
err = 'Error: Unsupported model version ({}).'.format(
self.version) + \
err = 'Error: Unsupported model ' + \
'version ({}).'.format(self.version) + \
' Valid model versions are:'
for v in list(self.version_types.keys()):
err += ' {}'.format(v)
Expand Down
17 changes: 9 additions & 8 deletions flopy/modflow/mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ def write_name_file(self):
if o:
replace_text = 'REPLACE'
if b:
f_nam.write(
'DATA(BINARY) {0:5d} '.format(u) + f +
replace_text + '\n')
line = 'DATA(BINARY) {0:5d} '.format(u) + f + \
replace_text + '\n'
f_nam.write(line)
else:
f_nam.write('DATA {0:5d} '.format(u) + f + '\n')

Expand Down Expand Up @@ -820,7 +820,7 @@ def load(f, version='mf2005', exe_name='mf2005.exe', verbose=False,
files_not_loaded.append(item.filename)
if ml.verbose:
msg = 3 * ' ' + '{:4s} '.format(item.filetype) + \
'package load...skipped'
'package load...skipped'
print(msg)
elif "data" in item.filetype.lower():
if ml.verbose:
Expand Down Expand Up @@ -849,15 +849,16 @@ def load(f, version='mf2005', exe_name='mf2005.exe', verbose=False,
item.filehandle.close()
except KeyError:
if ml.verbose:
msg = 'Warning: external file unit {} '.format(key) + \
msg = '\nWARNING:\n External file ' + \
'unit {} '.format(key) + \
'does not exist in ext_unit_dict.'
print(msg)

# write message indicating packages that were successfully loaded
if ml.verbose:
msg = 3 * ' ' + 'The following ' + \
'{} '.format(len(files_successfully_loaded)) + \
'packages were successfully loaded.'
msg = 3 * ' ' + 'The following ' + \
'{} '.format(len(files_successfully_loaded)) + \
'packages were successfully loaded.'
print('')
print(msg)
for fname in files_successfully_loaded:
Expand Down
14 changes: 7 additions & 7 deletions flopy/mt3d/mt.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,10 @@ def load(f, version='mt3dms', exe_name='mt3dms.exe', verbose=False,
ext_unit_dict.pop(btn_key).filehandle.close()
ncomp = mt.btn.ncomp
# reserved unit numbers for .ucn, s.ucn, .obs, .mas, .cnf
poss_output_units = set(list(range(201, 201+ncomp)) +
list(range(301, 301+ncomp)) +
list(range(401, 401+ncomp)) +
list(range(601, 601+ncomp)) + [17])
poss_output_units = set(list(range(201, 201 + ncomp)) +
list(range(301, 301 + ncomp)) +
list(range(401, 401 + ncomp)) +
list(range(601, 601 + ncomp)) + [17])
if load_only is None:
load_only = []
for key, item in ext_unit_dict.items():
Expand Down Expand Up @@ -760,9 +760,9 @@ def load(f, version='mt3dms', exe_name='mt3dms.exe', verbose=False,
item.filehandle.close()
except KeyError:
if mt.verbose:
sys.stdout.write(
"Warning: external file unit "
"{} does not exist in ext_unit_dict.\n".format(key))
msg = "\nWARNING:\n External file unit " + \
"{} does not exist in ext_unit_dict.\n".format(key)
sys.stdout.write(msg)

# write message indicating packages that were successfully loaded
if mt.verbose:
Expand Down

0 comments on commit 4fc61d5

Please sign in to comment.