Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close opened files after loading #673

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions flopy/modflow/mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,14 @@ def load(f, version='mf2005', exe_name='mf2005.exe', verbose=False,
if dis_key is None:
raise KeyError('discretization entry not found in nam file')
disnamdata = ext_unit_dict[dis_key]
dis = disnamdata.package.load(disnamdata.filename, ml,
dis = disnamdata.package.load(disnamdata.filehandle, ml,
ext_unit_dict=ext_unit_dict,
check=False)
files_successfully_loaded.append(disnamdata.filename)
if ml.verbose:
print(' {:4s} package load...success'.format(dis.name[0]))
assert ml.pop_key_list.pop() == dis_key
ext_unit_dict.pop(dis_key)
ext_unit_dict.pop(dis_key).filehandle.close()

dis.start_datetime = ml._start_datetime

Expand Down Expand Up @@ -774,11 +774,11 @@ def load(f, version='mf2005', exe_name='mf2005.exe', verbose=False,
if forgive:
try:
if "check" in package_load_args:
item.package.load(item.filename, ml,
item.package.load(item.filehandle, ml,
ext_unit_dict=ext_unit_dict,
check=False)
else:
item.package.load(item.filename, ml,
item.package.load(item.filehandle, ml,
ext_unit_dict=ext_unit_dict)
files_successfully_loaded.append(item.filename)
if ml.verbose:
Expand All @@ -795,11 +795,11 @@ def load(f, version='mf2005', exe_name='mf2005.exe', verbose=False,
files_not_loaded.append(item.filename)
else:
if "check" in package_load_args:
item.package.load(item.filename, ml,
item.package.load(item.filehandle, ml,
ext_unit_dict=ext_unit_dict,
check=False)
else:
item.package.load(item.filename, ml,
item.package.load(item.filehandle, ml,
ext_unit_dict=ext_unit_dict)
files_successfully_loaded.append(item.filename)
if ml.verbose:
Expand Down Expand Up @@ -840,7 +840,9 @@ def load(f, version='mf2005', exe_name='mf2005.exe', verbose=False,
for key in ml.pop_key_list:
try:
ml.remove_external(unit=key)
ext_unit_dict.pop(key)
item = ext_unit_dict.pop(key)
if hasattr(item.filehandle, 'close'):
item.filehandle.close()
except KeyError:
if ml.verbose:
msg = 'Warning: external file unit {} '.format(key) + \
Expand Down
7 changes: 5 additions & 2 deletions flopy/modflow/mfbas.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ def load(f, model, ext_unit_dict=None, check=True, **kwargs):
ncol = None

# open the file if not already open
if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

# dataset 0 -- header
while True:
line = f.readline()
Expand Down Expand Up @@ -339,7 +341,8 @@ def load(f, model, ext_unit_dict=None, check=True, **kwargs):
# dataset 4 -- strt
strt = Util3d.load(f, model, (nlay, nrow, ncol), np.float32, 'strt',
ext_unit_dict)
f.close()
if openfile:
f.close()

# set package unit number
unitnumber = None
Expand Down
6 changes: 5 additions & 1 deletion flopy/modflow/mfbcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ def load(f, model, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading bcf package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

Expand Down Expand Up @@ -406,6 +407,9 @@ def load(f, model, ext_unit_dict=None):
ext_unit_dict)
wetdry[k] = t

if openfile:
f.close()

# set package unit number
unitnumber = None
filenames = [None, None]
Expand Down
7 changes: 6 additions & 1 deletion flopy/modflow/mfde4.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,11 @@ def load(f, model, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading de4 package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

# read dataset 0 -- header
while True:
line = f.readline()
Expand Down Expand Up @@ -277,6 +279,9 @@ def load(f, model, ext_unit_dict=None):
hclose = float(line[30:40].strip())
iprd4 = int(line[40:50].strip())

if openfile:
f.close()

# set package unit number
unitnumber = None
filenames = [None]
Expand Down
7 changes: 6 additions & 1 deletion flopy/modflow/mfdis.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,11 @@ def load(f, model, ext_unit_dict=None, check=True):
if model.verbose:
sys.stdout.write('loading dis package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

# dataset 0 -- header
header = ''
while True:
Expand Down Expand Up @@ -1000,6 +1002,9 @@ def load(f, model, ext_unit_dict=None, check=True):
tsmult.append(a3)
steady.append(a4)

if openfile:
f.close()

# set package unit number
unitnumber = None
filenames = [None]
Expand Down
6 changes: 5 additions & 1 deletion flopy/modflow/mfdisu.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ def load(f, model, ext_unit_dict=None, check=False):
print(msg)
model.version = 'mfusg'

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

Expand Down Expand Up @@ -656,6 +657,9 @@ def load(f, model, ext_unit_dict=None, check=False):
print(' TSMULT {}'.format(tsmult))
print(' STEADY {}'.format(steady))

if openfile:
f.close()

# set package unit number
unitnumber = None
filenames = [None]
Expand Down
7 changes: 6 additions & 1 deletion flopy/modflow/mfevt.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ def load(f, model, nper=None, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading evt package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

# Dataset 0 -- header
while True:
line = f.readline()
Expand Down Expand Up @@ -331,6 +333,9 @@ def load(f, model, nper=None, ext_unit_dict=None):
current_ievt = t
ievt[iper] = current_ievt

if openfile:
f.close()

# create evt object
args = {}
if ievt:
Expand Down
6 changes: 5 additions & 1 deletion flopy/modflow/mffhb.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ def load(f, model, nper=None, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading fhb package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

Expand Down Expand Up @@ -668,6 +669,9 @@ def load(f, model, nper=None, ext_unit_dict=None):
current[n] = tuple(raw[:len(dtype.names)])
ds8.append(current.copy())

if openfile:
f.close()

# determine specified unit number
unitnumber = None
filenames = [None, None]
Expand Down
7 changes: 4 additions & 3 deletions flopy/modflow/mfflwob.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ def load(f, model, ext_unit_dict=None, check=True):
if model.verbose:
sys.stdout.write('loading flwob package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

Expand Down Expand Up @@ -483,8 +484,8 @@ def load(f, model, ext_unit_dict=None, check=True):
column = np.array(column) - 1
factor = np.array(factor)

# close the file
f.close()
if openfile:
f.close()

# get ext_unit_dict if none passed
if ext_unit_dict is None:
Expand Down
6 changes: 5 additions & 1 deletion flopy/modflow/mfgage.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ def load(f, model, nper=None, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading gage package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
if sys.version_info[0] == 2:
f = open(filename, 'r')
Expand Down Expand Up @@ -351,6 +352,9 @@ def load(f, model, nper=None, ext_unit_dict=None):
files.append(relpth)
break

if openfile:
f.close()

# determine specified unit number
unitnumber = None
filenames = []
Expand Down
8 changes: 5 additions & 3 deletions flopy/modflow/mfgmg.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,11 @@ def load(f, model, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading gmg package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

# dataset 0 -- header
while True:
line = f.readline()
Expand Down Expand Up @@ -358,8 +360,8 @@ def load(f, model, ext_unit_dict=None):
t = line.strip().split()
relax = float(t[0])

# close the open file
f.close()
if openfile:
f.close()

# determine specified unit number
unitnumber = None
Expand Down
8 changes: 7 additions & 1 deletion flopy/modflow/mfhfb.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@ def load(f, model, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading hfb6 package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

# dataset 0 -- header
while True:
line = f.readline()
Expand Down Expand Up @@ -360,6 +362,10 @@ def load(f, model, ext_unit_dict=None):
else:
bnd_output = stack_arrays((bnd_output, par_current),
asrecarray=True, usemask=False)

if openfile:
f.close()

# set package unit number
unitnumber = None
filenames = [None]
Expand Down
8 changes: 5 additions & 3 deletions flopy/modflow/mfhob.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,11 @@ def load(f, model, ext_unit_dict=None, check=True):
if model.verbose:
sys.stdout.write('loading hob package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

# dataset 0 -- header
while True:
line = f.readline()
Expand Down Expand Up @@ -417,8 +419,8 @@ def load(f, model, ext_unit_dict=None, check=True):
if nobs == nh:
break

# close the file
f.close()
if openfile:
f.close()

# set package unit number
unitnumber = None
Expand Down
7 changes: 4 additions & 3 deletions flopy/modflow/mfhyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ def load(f, model, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading hydmod package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
f = open(filename, 'r')

Expand Down Expand Up @@ -327,8 +328,8 @@ def load(f, model, ext_unit_dict=None):
obs['yl'][idx] = float(t[5])
obs['hydlbl'][idx] = t[6].strip()

# close the file
f.close()
if openfile:
f.close()

# set package unit number
unitnumber = None
Expand Down
6 changes: 5 additions & 1 deletion flopy/modflow/mflak.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ def load(f, model, nper=None, ext_unit_dict=None):
if model.verbose:
sys.stdout.write('loading lak package file...\n')

if not hasattr(f, 'read'):
openfile = not hasattr(f, 'read')
if openfile:
filename = f
if sys.version_info[0] == 2:
f = open(filename, 'r')
Expand Down Expand Up @@ -789,6 +790,9 @@ def load(f, model, nper=None, ext_unit_dict=None):
ds9[n] = tds
flux_data[iper] = ds9

if openfile:
f.close()

# convert lake data to Transient3d objects
lake_loc = Transient3d(model, (nlay, nrow, ncol), np.int32,
lake_loc, name='lakarr_')
Expand Down
Loading