flopy.mbase module
- mbase module
This module contains the base model class from which all of the other models inherit from.
- class BaseModel(modelname='modflowtest', namefile_ext='nam', exe_name: str | PathLike = 'mf2005', model_ws: str | PathLike = '.', structured=True, verbose=False, **kwargs)[source]
Bases:
ModelInterface
MODFLOW-based models base class.
- Parameters:
modelname (str, default "modflowtest") – Name of the model, which is also used for model file names.
namefile_ext (str, default "nam") – Name file extension, without “.”
exe_name (str or PathLike, default "mf2005") – Name or path of the modflow executable. If a name is provided, the executable must be on the system path.
model_ws (str or PathLike, optional, default ".") – Path to the model workspace. Model files will be created in this directory. Default is the current working directory.
structured (bool, default True) – Specify if model grid is structured (default) or unstructured.
verbose (bool, default False) – Print additional information to the screen.
**kwargs (dict, optional) – Used to define:
xll
/yll
for the x- and y-coordinates of the lower-left corner of the grid,xul
/yul
for the x- and y-coordinates of the upper-left corner of the grid (deprecated),rotation
for the grid rotation (default 0.0),crs
for the coordinate reference system, andstart_datetime
for model start date (default “1-1-1970”).
- add_existing_package(filename: str | PathLike, ptype=None, copy_to_model_ws=True)[source]
Add an existing package to a model instance.
- add_external(fname: str | PathLike, unit, binflag=False, output=False)[source]
Assign an external array so that it will be listed as a DATA or DATA(BINARY) entry in the name file. This will allow an outside file package to refer to it.
- add_output(fname: str | PathLike, unit, binflag=False, package=None)[source]
Assign an external array so that it will be listed as a DATA or DATA(BINARY) entry in the name file. This will allow an outside file package to refer to it.
- add_output_file(unit, fname: str | PathLike | None = None, extension='cbc', binflag=True, package=None)[source]
Add an ascii or binary output file for a package
- Parameters:
unit (int) – Unit number of external array
fname (str or PathLike, optional) – Path of external array, default is None
extension (str) – Extension to use for the cell-by-cell file. Only used if fname is None, default is cbc
binflag (bool) – Whether the output file is a binary file, efault is True
package (str) – The package the output file is attached to, default is None
- add_pop_key_list(key)[source]
Add a external file unit number to a list that will be used to remove model output (typically binary) files from ext_unit_dict.
- Parameters:
key (int) – file unit number
Examples
- change_model_ws(new_pth: str | PathLike | None = '.', reset_external=False)[source]
Change the model work space.
- Parameters:
new_pth (str or PathLike) – Path of the new model workspace. If this path does not exist, it will be created. If no value (None) is given, the default is the present working directory.
- Returns:
val – Can be used to see what packages are in the model, and can then be used with get_package to pull out individual packages.
- Return type:
list of strings
- check(f: str | PathLike | None = None, verbose=True, level=1)[source]
Check model data for common errors.
- Parameters:
f (str or PathLike, optional, default None) – String defining file name or file handle for summary file of check method output. If a string is passed a file handle is created. If f is None, check method does not write results to a summary file. (default is None)
verbose (bool) – Boolean flag used to determine if check method results are written to the screen
level (int) – Check method analysis level. If level=0, summary checks are performed. If level=1, full checks are performed.
- Return type:
None
Examples
>>> import flopy >>> m = flopy.modflow.Modflow.load('model.nam') >>> m.check()
- export(f: str | PathLike, **kwargs)[source]
Method to export a model to netcdf or shapefile based on the extension of the file name (.shp for shapefile, .nc for netcdf)
- Parameters:
f (str or PathLike) – The file path
kwargs (keyword arguments) –
- modelgridflopy.discretization.Grid instance
user supplied modelgrid which can be used for exporting in lieu of the modelgrid associated with the model object
- Return type:
None or Netcdf object
- get_output(fname: str | PathLike | None = None, unit=None)[source]
Get an output file from the model by specifying either the file name or the unit number.
- get_output_attribute(fname: str | PathLike | None = None, unit=None, attr=None)[source]
Get an attribute of a model output file by specifying either the file name or the unit number.
- get_package(name)[source]
Get a package.
- Parameters:
name (str) – Name of the package, ‘RIV’, ‘LPF’, etc. (case-insensitive).
- Returns:
pp – Package object of type
flopy.pakbase.Package
- Return type:
Package object
- property hdry
- property hnoflo
- property laycbd
- property laytyp
- property modelgrid
- property modeltime
- property packagelist
- plot(SelPackList=None, **kwargs)[source]
Plot 2-D, 3-D, transient 2-D, and stress period list (MfList) model input data
- Parameters:
SelPackList (bool or list) – List of of packages to plot. If SelPackList=None all packages are plotted. (default is None)
**kwargs (dict) –
- filename_basestr
Base file name that will be used to automatically generate file names for output image files. Plots will be exported as image files if file_name_base is not None. (default is None)
- file_extensionstr
Valid matplotlib.pyplot file extension for savefig(). Only used if filename_base is not None. (default is ‘png’)
- mflayint
MODFLOW zero-based layer number to return. If None, then all all layers will be included. (default is None)
- kperint
MODFLOW zero-based stress period number to return. (default is zero)
- keystr
MfList dictionary key. (default is None)
- Returns:
axes – Empty list is returned if filename_base is not None. Otherwise a list of matplotlib.pyplot.axis are returned.
- Return type:
Notes
Examples
>>> import flopy >>> ml = flopy.modflow.Modflow.load('test.nam') >>> ml.plot()
- remove_external(fname: str | PathLike | None = None, unit=None)[source]
Remove an external file from the model by specifying either the file name or the unit number.
- remove_output(fname: str | PathLike | None = None, unit=None)[source]
Remove an output file from the model by specifying either the file name or the unit number.
- remove_package(pname)[source]
Remove a package from this model
- Parameters:
pname (string) – Name of the package, such as ‘RIV’, ‘BAS6’, etc.
- run_model(silent=False, pause=False, report=False, normal_msg='normal termination') Tuple[bool, List[str]] [source]
This method will run the model using subprocess.Popen.
- Parameters:
silent (boolean) – Echo run information to screen (default is True).
pause (boolean, optional) – Pause upon completion (default is False).
report (boolean, optional) – Save stdout lines to a list (buff) which is returned by the method . (default is False).
normal_msg (str) – Normal termination message used to determine if the run terminated normally. (default is ‘normal termination’)
- Returns:
success (boolean)
buff (list of lines of stdout)
- set_output_attribute(fname: str | PathLike | None = None, unit=None, attr=None)[source]
Set a variable in an output file from the model by specifying either the file name or the unit number and a dictionary with attributes to change.
- to_shapefile(filename: str | PathLike, package_names=None, **kwargs)[source]
Wrapper function for writing a shapefile for the model grid. If package_names is not None, then search through the requested packages looking for arrays that can be added to the shapefile as attributes
- Parameters:
- Return type:
None
Examples
>>> import flopy >>> m = flopy.modflow.Modflow() >>> m.to_shapefile('model.shp', SelPackList)
- property verbose
- property version
- class ModelInterface[source]
Bases:
object
- abstract property exename
- abstract property hdry
- abstract property hnoflo
- abstract property laycbd
- abstract property laytyp
- abstract property model_ws
- abstract property modelgrid
- abstract property namefile
- abstract property packagelist
- abstract property solver_tols
- abstract property verbose
- abstract property version
- exception PackageLoadException(error, location='')[source]
Bases:
Exception
FloPy package load exception.
- resolve_exe(exe_name: str | PathLike) str [source]
Resolves the absolute path of the executable.
- Parameters:
exe_name (str or PathLike) – The executable’s name or path. If only the name is provided, the executable must be on the system path.
- Returns:
str
- Return type:
absolute path to the executable
- run_model(exe_name: str | PathLike, namefile: str | None, model_ws: str | PathLike = '.', silent=False, pause=False, report=False, normal_msg='normal termination', use_async=False, cargs=None) Tuple[bool, List[str]] [source]
Run the model using subprocess.Popen, optionally collecting stdout and printing timestamped progress. Model workspace, namefile, executable to use, and several other options may be configured, and additional command line arguments may also be provided.
- Parameters:
exe_name (str or PathLike) – Executable name or path. If the executable name is provided, the executable must be on the system path. Alternatively, a full path to the executable may be provided.
namefile (str, optional) – Name of the name file of model to run. The name may be None to run models that don’t require a control file (name file)
model_ws (str or PathLike, optional, default '.') – Path to the parent directory of the namefile. (default is the current working directory ‘.’)
silent (boolean, default True) – Whether to suppress model output. (Default is True)
pause (boolean, optional, default False) – Pause and wait for keystroke upon completion. (Default is False)
report (boolean, optional, default False) – Save stdout lines to a list (buff) returned by the method. (Default is False)
normal_msg (str or list) – Termination message used to determine if the model terminated normally. More than one message can be provided using a list. (Default is ‘normal termination’)
use_async (boolean) – Asynchronously read model stdout and report with timestamps. Good for models taking a long time to run, not good for models that run quickly.
cargs (str or list, optional, default None) – Additional command line arguments to pass to the executable. (Default is None)
- Returns:
success (boolean)
buff (list of lines of stdout (empty if report is False))