Updates to gage package and added more through test of files and filenames options.

develop
jdhughes-usgs 2017-03-04 12:07:54 -05:00
parent 90fd1df6c1
commit de70398cde
3 changed files with 263 additions and 17 deletions

View File

@ -23,7 +23,7 @@ if v is None:
run = False
def test_obs_load_and_write():
def test_gage_load_and_write():
"""
test043 load and write of MODFLOW-2005 GAGE example problem
"""
@ -79,4 +79,4 @@ def test_obs_load_and_write():
if __name__ == '__main__':
test_obs_load_and_write()
test_gage_load_and_write()

227
autotest/t047_test.py Normal file
View File

@ -0,0 +1,227 @@
"""
Test the gmg load and write with an external summary file
"""
import os
import shutil
import numpy as np
import flopy
cpth = os.path.join('temp', 't047')
# delete the directory if it exists
if os.path.isdir(cpth):
shutil.rmtree(cpth)
# make the directory
os.makedirs(cpth)
def get_namefile_entries(fpth):
try:
f = open(fpth, 'r')
except:
print('could not open...{}'.format(fpth))
return None
dtype = [('ftype', '|S12'), ('unit', np.int),
('filename', '|S128'), ('status', '|S10')]
lines = f.readlines()
data = []
for line in lines:
if line[0] == '#':
continue
t = line.rstrip().split()
ftype = t[0]
if isinstance(ftype, bytes):
ftype = ftype.decode()
iu = int(t[1])
filename = t[2]
if isinstance(filename, bytes):
filename = filename.decode()
if len(t) < 4:
status = ''
else:
status = t[3]
if isinstance(status, bytes):
status = status.decode()
data.append([ftype, iu, filename, status])
data = np.rec.array(data, dtype=dtype)
return data
def test_gage():
mnam = 'gage_test'
m = flopy.modflow.Modflow(modelname=mnam, model_ws=cpth)
dis = flopy.modflow.ModflowDis(m)
spd = {(0, 0): ['print head'],
(0, 1): [],
(0, 249): ['print head'],
(0, 250): [],
(0, 499): ['print head', 'save ibound'],
(0, 500): [],
(0, 749): ['print head', 'ddreference'],
(0, 750): [],
(0, 999): ['print head', 'save budget', 'save drawdown']}
oc = flopy.modflow.ModflowOc(m, stress_period_data=spd, cboufm='(20i5)')
gages = [[-1, -26, 1], [-2, -27, 1]]
gage = flopy.modflow.ModflowGage(m, numgage=2,
gage_data=gages)
m.write_input()
# check that the gage output units entries are in the name file
fpth = os.path.join(cpth, '{}.nam'.format(mnam))
entries = get_namefile_entries(fpth)
for idx, g in enumerate(gages):
if g[0] < 0:
iu = abs(g[1])
else:
iu = abs(g[2])
found = False
iun = None
for jdx, iut in enumerate(entries['unit']):
if iut == iu:
found = True
iun = iut
break
assert found, '{} not in name file entries'.format(iu)
return
def test_gage_files():
mnam = 'gage_test_files'
m = flopy.modflow.Modflow(modelname=mnam, model_ws=cpth)
dis = flopy.modflow.ModflowDis(m)
spd = {(0, 0): ['print head'],
(0, 1): [],
(0, 249): ['print head'],
(0, 250): [],
(0, 499): ['print head', 'save ibound'],
(0, 500): [],
(0, 749): ['print head', 'ddreference'],
(0, 750): [],
(0, 999): ['print head', 'save budget', 'save drawdown']}
oc = flopy.modflow.ModflowOc(m, stress_period_data=spd, cboufm='(20i5)')
gages = [[-1, -26, 1], [-2, -27, 1]]
files = ['gage1.go', 'gage2.go']
gage = flopy.modflow.ModflowGage(m, numgage=2,
gage_data=gages, files=files)
m.write_input()
# check that the gage output file entries are in the name file
fpth = os.path.join(cpth, '{}.nam'.format(mnam))
entries = get_namefile_entries(fpth)
for idx, f in enumerate(files):
found = False
iun = None
for jdx, fnn in enumerate(entries['filename']):
if isinstance(fnn, bytes):
fnn = fnn.decode()
if fnn == f:
found = True
iun = entries[jdx]['unit']
break
assert found, '{} not in name file entries'.format(f)
iu = abs(gages[idx][1])
assert iu == iun, '{} unit not equal to {} '.format(f, iu) + \
'- name file unit = {}'.format(iun)
return
def test_gage_filenames0():
mnam = 'gage_test_filenames0'
m = flopy.modflow.Modflow(modelname=mnam, model_ws=cpth)
dis = flopy.modflow.ModflowDis(m)
spd = {(0, 0): ['print head'],
(0, 1): [],
(0, 249): ['print head'],
(0, 250): [],
(0, 499): ['print head', 'save ibound'],
(0, 500): [],
(0, 749): ['print head', 'ddreference'],
(0, 750): [],
(0, 999): ['print head', 'save budget', 'save drawdown']}
oc = flopy.modflow.ModflowOc(m, stress_period_data=spd, cboufm='(20i5)')
gages = [[-1, -126, 1], [-2, -127, 1]]
filenames = 'mygages0.gage'
gage = flopy.modflow.ModflowGage(m, numgage=2,
gage_data=gages, filenames=filenames)
m.write_input()
# check that the gage output units entries are in the name file
fpth = os.path.join(cpth, '{}.nam'.format(mnam))
entries = get_namefile_entries(fpth)
for idx, g in enumerate(gages):
if g[0] < 0:
iu = abs(g[1])
else:
iu = abs(g[2])
found = False
iun = None
for jdx, iut in enumerate(entries['unit']):
if iut == iu:
found = True
iun = iut
break
assert found, '{} not in name file entries'.format(iu)
return
def test_gage_filenames():
mnam = 'gage_test_filenames'
m = flopy.modflow.Modflow(modelname=mnam, model_ws=cpth)
dis = flopy.modflow.ModflowDis(m)
spd = {(0, 0): ['print head'],
(0, 1): [],
(0, 249): ['print head'],
(0, 250): [],
(0, 499): ['print head', 'save ibound'],
(0, 500): [],
(0, 749): ['print head', 'ddreference'],
(0, 750): [],
(0, 999): ['print head', 'save budget', 'save drawdown']}
oc = flopy.modflow.ModflowOc(m, stress_period_data=spd, cboufm='(20i5)')
gages = [[-1, -126, 1], [-2, -127, 1]]
filenames = ['mygages.gage', 'mygage1.go', 'mygage2.go']
gage = flopy.modflow.ModflowGage(m, numgage=2,
gage_data=gages, filenames=filenames)
m.write_input()
# check that the gage output file entries are in the name file
fpth = os.path.join(cpth, '{}.nam'.format(mnam))
entries = get_namefile_entries(fpth)
for idx, f in enumerate(filenames[1:]):
found = False
iun = None
for jdx, fnn in enumerate(entries['filename']):
if isinstance(fnn, bytes):
fnn = fnn.decode()
if fnn == f:
found = True
iun = entries[jdx]['unit']
break
assert found, '{} not in name file entries'.format(f)
iu = abs(gages[idx][1])
assert iu == iun, '{} unit not equal to {} '.format(f, iu) + \
'- name file unit = {}'.format(iun)
return
if __name__ == '__main__':
test_gage()
test_gage_files()
test_gage_filenames()

View File

@ -25,12 +25,35 @@ class ModflowGage(Package):
model : model object
The model object (of type :class:`flopy.modflow.mf.Modflow`) to which
this package will be added.
options : list of strings
Package options. (default is None).
numgage : int
The total number of gages included in the gage file (default is 0).
gage_data : list or numpy array
data for dataset 2a and 2b in the gage package. If a list is provided
then the list includes 2 to 3 entries (LAKE UNIT [OUTTYPE]) for each
LAK Package entry and 4 entries (GAGESEG GAGERCH UNIT OUTTYPE) for
each SFR Package entry. If a numpy array it passed each gage location
must have 4 entries, where LAK Package gages can have any value for the
second column. The numpy array can be created using the get_empty()
method available in ModflowGage. Default is None
files : list of strings
Names of gage output files. A file name must be provided for each gage.
If files are not provided and filenames=None then a gage name will be
created using the model name and the gage number (for example,
modflowtest.gage1.go). Default is None.
extension : string
Filename extension (default is 'str')
Filename extension (default is 'gage')
unitnumber : int
File unit number (default is 118).
File unit number (default is None).
filenames : str or list of str
Filenames to use for the package and the output files. If
filenames=None the package name will be created using the model name
and gage package extensions and gage output names will be
created using the model name and the gage number (for example,
modflowtest.gage1.go). If a single string is passed the package will
be set to the string and gage output names will be created using the
model name and the gage number. To define the names for all gage files
(input and output) the length of the list of strings should be
numgage + 1. Default is None.
Methods
-------
@ -55,7 +78,7 @@ class ModflowGage(Package):
"""
def __init__(self, model, numgage=0, gage_data=None, files=None,
extension='gage', unitnumber=None, options=None,
extension='gage', unitnumber=None,
filenames=None, **kwargs):
"""
Package constructor.
@ -79,10 +102,11 @@ class ModflowGage(Package):
dtype = ModflowGage.get_default_dtype()
if numgage > 0:
# check the provided file entries
if filenames is None:
if filenames[1] is None:
if files is None:
err = "a list of output gage 'files' must be provided"
raise Exception(err)
files = []
for idx in range(numgage):
files.append('{}.gage{}.go'.format(model.name, idx+1))
if isinstance(files, np.ndarray):
files = files.flatten().aslist()
elif isinstance(files, str):
@ -162,9 +186,6 @@ class ModflowGage(Package):
'generated by Flopy.'
self.url = 'gage.htm'
if options is None:
options = []
self.options = options
self.numgage = numgage
self.files = files
@ -177,7 +198,7 @@ class ModflowGage(Package):
return
@staticmethod
def get_default_dtype(structured=True):
def get_default_dtype():
dtype = np.dtype([("gageloc", np.int), ("gagerch", np.int),
("unit", np.int), ("outtype", np.int)])
return dtype
@ -185,9 +206,7 @@ class ModflowGage(Package):
@staticmethod
def get_empty(ncells=0, aux_names=None, structured=True):
# get an empty recaray that correponds to dtype
dtype = ModflowGage.get_default_dtype(structured=structured)
if aux_names is not None:
dtype = Package.add_to_dtype(dtype, aux_names, np.float32)
dtype = ModflowGage.get_default_dtype()
d = np.zeros((ncells, len(dtype)), dtype=dtype)
d[:, :] = -1.0E+10
return np.core.records.fromarrays(d.transpose(), dtype=dtype)