-
Notifications
You must be signed in to change notification settings - Fork 303
/
swt.py
459 lines (391 loc) · 15.3 KB
/
swt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import os
from ..mbase import BaseModel
from ..pakbase import Package
from ..modflow import Modflow
from ..mt3d import Mt3dms
from .swtvdf import SeawatVdf
from .swtvsc import SeawatVsc
from ..discretization.structuredgrid import StructuredGrid
from flopy.discretization.modeltime import ModelTime
class SeawatList(Package):
"""
List Package class
"""
def __init__(self, model, extension='list', listunit=7):
Package.__init__(self, model, extension, 'LIST', listunit)
return
def __repr__(self):
return 'List package class'
def write_file(self):
# Not implemented for list class
return
class Seawat(BaseModel):
"""
SEAWAT Model Class.
Parameters
----------
modelname : string, optional
Name of model. This string will be used to name the SEAWAT input
that are created with write_model. (the default is 'swttest')
namefile_ext : string, optional
Extension for the namefile (the default is 'nam')
version : string, optional
Version of SEAWAT to use (the default is 'seawat').
exe_name : string, optional
The name of the executable to use (the default is
'swtv4.exe').
listunit : integer, optional
Unit number for the list file (the default is 2).
model_ws : string, optional
model workspace. Directory name to create model data sets.
(default is the present working directory).
external_path : string
Location for external files (default is None).
verbose : boolean, optional
Print additional information to the screen (default is False).
load : boolean, optional
(default is True).
silent : integer
(default is 0)
Attributes
----------
Methods
-------
See Also
--------
Notes
-----
Examples
--------
>>> import flopy
>>> m = flopy.seawat.swt.Seawat()
"""
def __init__(self, modelname='swttest', namefile_ext='nam',
modflowmodel=None, mt3dmodel=None,
version='seawat', exe_name='swtv4',
structured=True, listunit=2, model_ws='.', external_path=None,
verbose=False, load=True, silent=0):
# Call constructor for parent object
BaseModel.__init__(self, modelname, namefile_ext, exe_name, model_ws,
structured=structured, verbose=verbose)
# Set attributes
self.version_types = {'seawat': 'SEAWAT'}
self.set_version(version)
self.lst = SeawatList(self, listunit=listunit)
self.glo = None
self._mf = None
self._mt = None
# If a MODFLOW model was passed in, then add its packages
self.mf = self
if modflowmodel is not None:
for p in modflowmodel.packagelist:
self.packagelist.append(p)
self._modelgrid = modflowmodel.modelgrid
else:
modflowmodel = Modflow()
# If a MT3D model was passed in, then add its packages
if mt3dmodel is not None:
for p in mt3dmodel.packagelist:
self.packagelist.append(p)
else:
mt3dmodel = Mt3dms()
# external option stuff
self.array_free_format = False
self.array_format = 'mt3d'
self.external_fnames = []
self.external_units = []
self.external_binflag = []
self.external = False
self.load = load
# the starting external data unit number
self._next_ext_unit = 3000
if external_path is not None:
assert model_ws == '.', "ERROR: external cannot be used " + \
"with model_ws"
# external_path = os.path.join(model_ws, external_path)
if os.path.exists(external_path):
print("Note: external_path " + str(external_path) +
" already exists")
# assert os.path.exists(external_path),'external_path does not exist'
else:
os.mkdir(external_path)
self.external = True
self.external_path = external_path
self.verbose = verbose
self.silent = silent
# Create a dictionary to map package with package object.
# This is used for loading models.
self.mfnam_packages = {}
for k, v in modflowmodel.mfnam_packages.items():
self.mfnam_packages[k] = v
for k, v in mt3dmodel.mfnam_packages.items():
self.mfnam_packages[k] = v
self.mfnam_packages['vdf'] = SeawatVdf
self.mfnam_packages['vsc'] = SeawatVsc
return
@property
def modeltime(self):
# build model time
data_frame = {'perlen': self.dis.perlen.array,
'nstp': self.dis.nstp.array,
'tsmult': self.dis.tsmult.array}
self._model_time = ModelTime(data_frame,
self.dis.itmuni_dict[self.dis.itmuni],
self.dis.start_datetime,
self.dis.steady.array)
return self._model_time
@property
def modelgrid(self):
if not self._mg_resync:
return self._modelgrid
if self.has_package('bas6'):
ibound = self.bas6.ibound.array
else:
ibound = None
# build grid
# self.dis should exist if modflow model passed
self._modelgrid = StructuredGrid(self.dis.delc.array,
self.dis.delr.array,
self.dis.top.array,
self.dis.botm.array,
idomain=ibound,
lenuni=self.dis.lenuni,
proj4=self._modelgrid.proj4,
epsg=self._modelgrid.epsg,
xoff=self._modelgrid.xoffset,
yoff=self._modelgrid.yoffset,
angrot=self._modelgrid.angrot,
nlay=self.dis.nlay)
# resolve offsets
xoff = self._modelgrid.xoffset
if xoff is None:
if self._xul is not None:
xoff = self._modelgrid._xul_to_xll(self._xul)
else:
xoff = 0.0
yoff = self._modelgrid.yoffset
if yoff is None:
if self._yul is not None:
yoff = self._modelgrid._yul_to_yll(self._yul)
else:
yoff = 0.0
self._modelgrid.set_coord_info(xoff, yoff, self._modelgrid.angrot,
self._modelgrid.epsg,
self._modelgrid.proj4)
self._mg_resync = not self._modelgrid.is_complete
return self._modelgrid
@property
def nlay(self):
if (self.dis):
return self.dis.nlay
else:
return 0
@property
def nrow(self):
if (self.dis):
return self.dis.nrow
else:
return 0
@property
def ncol(self):
if (self.dis):
return self.dis.ncol
else:
return 0
@property
def nper(self):
if (self.dis):
return self.dis.nper
else:
return 0
@property
def nrow_ncol_nlay_nper(self):
dis = self.get_package('DIS')
if (dis):
return dis.nrow, dis.ncol, dis.nlay, dis.nper
else:
return 0, 0, 0, 0
def get_nrow_ncol_nlay_nper(self):
return self.nrow_ncol_nlay_nper
def get_ifrefm(self):
bas = self.get_package('BAS6')
if (bas):
return bas.ifrefm
else:
return False
@property
def ncomp(self):
if (self.btn):
return self.btn.ncomp
else:
return 1
@property
def mcomp(self):
if (self.btn):
return self.btn.mcomp
else:
return 1
def _set_name(self, value):
# Overrides BaseModel's setter for name property
BaseModel._set_name(self, value)
# for i in range(len(self.lst.extension)):
# self.lst.file_name[i] = self.name + '.' + self.lst.extension[i]
# return
def change_model_ws(self, new_pth=None, reset_external=False):
# if hasattr(self,"_mf"):
if self._mf is not None:
self._mf.change_model_ws(new_pth=new_pth,
reset_external=reset_external)
# if hasattr(self,"_mt"):
if self._mt is not None:
self._mt.change_model_ws(new_pth=new_pth,
reset_external=reset_external)
super(Seawat, self).change_model_ws(new_pth=new_pth,
reset_external=reset_external)
def write_name_file(self):
"""
Write the name file
Returns
-------
None
"""
# open and write header
fn_path = os.path.join(self.model_ws, self.namefile)
f_nam = open(fn_path, 'w')
f_nam.write('{}\n'.format(self.heading))
# Write global file entry
if self.glo is not None:
if self.glo.unit_number[0] > 0:
f_nam.write('{:14s} {:5d} {}\n'.format(self.glo.name[0],
self.glo.unit_number[
0],
self.glo.file_name[0]))
# Write list file entry
f_nam.write('{:14s} {:5d} {}\n'.format(self.lst.name[0],
self.lst.unit_number[0],
self.lst.file_name[0]))
# Write SEAWAT entries and close
f_nam.write('{}'.format(self.get_name_file_entries()))
if self._mf is not None:
# write the external files
for b, u, f in zip(self._mf.external_binflag,
self._mf.external_units, \
self._mf.external_fnames):
tag = "DATA"
if b:
tag = "DATA(BINARY)"
f_nam.write('{0:14s} {1:5d} {2}\n'.format(tag, u, f))
# write the output files
for u, f, b in zip(self._mf.output_units, self._mf.output_fnames,
self._mf.output_binflag):
if u == 0:
continue
if b:
f_nam.write(
'DATA(BINARY) {0:5d} '.format(u) + f + ' REPLACE\n')
else:
f_nam.write('DATA {0:5d} '.format(u) + f + '\n')
if self._mt is not None:
# write the external files
for b, u, f in zip(self._mt.external_binflag,
self._mt.external_units, \
self._mt.external_fnames):
tag = "DATA"
if b:
tag = "DATA(BINARY)"
f_nam.write('{0:14s} {1:5d} {2}\n'.format(tag, u, f))
# write the output files
for u, f, b in zip(self._mt.output_units, self._mt.output_fnames,
self._mt.output_binflag):
if u == 0:
continue
if b:
f_nam.write(
'DATA(BINARY) {0:5d} '.format(u) + f + ' REPLACE\n')
else:
f_nam.write('DATA {0:5d} '.format(u) + f + '\n')
# write the external files
for b, u, f in zip(self.external_binflag, self.external_units, \
self.external_fnames):
tag = "DATA"
if b:
tag = "DATA(BINARY)"
f_nam.write('{0:14s} {1:5d} {2}\n'.format(tag, u, f))
# write the output files
for u, f, b in zip(self.output_units, self.output_fnames,
self.output_binflag):
if u == 0:
continue
if b:
f_nam.write(
'DATA(BINARY) {0:5d} '.format(u) + f + ' REPLACE\n')
else:
f_nam.write('DATA {0:5d} '.format(u) + f + '\n')
f_nam.close()
return
@staticmethod
def load(f, version='seawat', exe_name='swtv4', verbose=False,
model_ws='.', load_only=None):
"""
Load an existing model.
Parameters
----------
f : string
Full path and name of SEAWAT name file.
version : string
The version of SEAWAT (seawat)
(default is seawat)
exe_name : string
The name of the executable to use if this loaded model is run.
(default is swtv4.exe)
verbose : bool
Write information on the load process if True.
(default is False)
model_ws : string
The path for the model workspace.
(default is the current working directory '.')
load_only : list of strings
Filetype(s) to load (e.g. ['lpf', 'adv'])
(default is None, which means that all will be loaded)
Returns
-------
m : flopy.seawat.swt.Seawat
flopy Seawat model object
Examples
--------
>>> import flopy
>>> m = flopy.seawat.swt.Seawat.load(f)
"""
# test if name file is passed with extension (i.e., is a valid file)
if os.path.isfile(os.path.join(model_ws, f)):
modelname = f.rpartition('.')[0]
else:
modelname = f
# create instance of a seawat model and load modflow and mt3dms models
ms = Seawat(modelname=modelname, namefile_ext='nam',
modflowmodel=None, mt3dmodel=None,
version=version, exe_name=exe_name, model_ws=model_ws,
verbose=verbose)
mf = Modflow.load(f, version='mf2k', exe_name=None, verbose=verbose,
model_ws=model_ws, load_only=load_only,
forgive=False, check=False)
mt = Mt3dms.load(f, version='mt3dms', exe_name=None, verbose=verbose,
model_ws=model_ws, forgive=False)
# set listing and global files using mf objects
ms.lst = mf.lst
ms.glo = mf.glo
for p in mf.packagelist:
p.parent = ms
ms.add_package(p)
ms._mt = None
if mt is not None:
for p in mt.packagelist:
p.parent = ms
ms.add_package(p)
mt.external_units = []
mt.external_binflag = []
mt.external_fnames = []
ms._mt = mt
ms._mf = mf
# return model object
return ms