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='mf2k.exe', model_ws=None, structured=True, verbose=False, **kwargs)[source]

Bases: flopy.mbase.ModelInterface

MODFLOW based models base class

Parameters:
  • modelname (string) – Name of the model. Model files will be given this name. (default is ‘modflowtest’
  • namefile_ext (string) – name file extension (default is ‘nam’)
  • exe_name (string) – name of the modflow executable
  • model_ws (string) – Path to the model workspace. Model files will be created in this directory. Default is None, in which case model_ws is assigned to the current working directory.
add_existing_package(filename, ptype=None, copy_to_model_ws=True)[source]

Add an existing package to a model instance.

Parameters:
  • filename (str) – the name of the file to add as a package
  • ptype (optional) – the model package type (e.g. “lpf”, “wel”, etc). If None, then the file extension of the filename arg is used
  • copy_to_model_ws (bool) – flag to copy the package file into the model_ws directory.
Returns:

Return type:

None

add_external(fname, 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.

Parameters:
  • fname (str) – filename of external array
  • unit (int) – unit number of external array
  • binflag (boolean) – binary or not. (default is False)
add_output(fname, 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.

Parameters:
  • fname (str) – filename of external array
  • unit (int) – unit number of external array
  • binflag (boolean) – binary or not. (default is False)
add_output_file(unit, fname=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) – filename 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) – boolean flag indicating if the output file is a binary file. Default is True
  • package (str) – string that defines the package the output file is attached to. Default is None
add_package(p)[source]

Add a package.

Parameters:p (Package object) –
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=None, reset_external=False)[source]

Change the model work space.

Parameters:new_pth (str) – Location of new model workspace. If this path does not exist, it will be created. (default is None, which will be assigned to 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=None, verbose=True, level=1)[source]

Check model data for common errors.

Parameters:
  • f (str or file handle) – 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.
Returns:

Return type:

None

Examples

>>> import flopy
>>> m = flopy.modflow.Modflow.load('model.nam')
>>> m.check()
exename
export(f, **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) – filename
  • kwargs (keyword arguments) –
    modelgrid : flopy.discretization.Grid instance
    user supplied modelgrid which can be used for exporting in lieu of the modelgrid associated with the model object
Returns:

Return type:

None or Netcdf object

get_ext_dict_attr(ext_unit_dict=None, unit=None, filetype=None, pop_key=True)[source]
get_name_file_entries()[source]

Get a string representation of the name file.

get_output(fname=None, unit=None)[source]

Get an output file from the model by specifying either the file name or the unit number.

Parameters:
  • fname (str) – filename of output array
  • unit (int) – unit number of output array
get_output_attribute(fname=None, unit=None, attr=None)[source]

Get a attribute for an output file from the model by specifying either the file name or the unit number.

Parameters:
  • fname (str) – filename of output array
  • unit (int) – unit number of output array
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
has_package(name)[source]

Check if package name is in package list.

Parameters:name (str) – Name of the package, ‘DIS’, ‘BAS6’, etc. (case-insensitive).
Returns:True if package name exists, otherwise False if not found.
Return type:bool
hdry
hnoflo
laycbd
laytyp
load_results()[source]
model_ws
modelgrid
modeltime
name

Get model name

Returns:name – name of model
Return type:str
namefile
next_ext_unit()[source]

Function to encapsulate next_ext_unit attribute

next_unit(i=None)[source]
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_base : str
    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_extension : str
    Valid matplotlib.pyplot file extension for savefig(). Only used if filename_base is not None. (default is ‘png’)
    mflay : int
    MODFLOW zero-based layer number to return. If None, then all all layers will be included. (default is None)
    kper : int
    MODFLOW zero-based stress period number to return. (default is zero)
    key : str
    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:

list

Notes

Examples

>>> import flopy
>>> ml = flopy.modflow.Modflow.load('test.nam')
>>> ml.plot()
remove_external(fname=None, unit=None)[source]

Remove an external file from the model by specifying either the file name or the unit number.

Parameters:
  • fname (str) – filename of external array
  • unit (int) – unit number of external array
remove_output(fname=None, unit=None)[source]

Remove an output file from the model by specifying either the file name or the unit number.

Parameters:
  • fname (str) – filename of output array
  • unit (int) – unit number of output array
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')[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, buff)
  • success (boolean)
  • buff (list of lines of stdout)

set_model_units()[source]

Every model needs its own set_model_units method

set_output_attribute(fname=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.

Parameters:
  • fname (str) – filename of output array
  • unit (int) – unit number of output array
set_version(version)[source]
to_shapefile(filename, 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:
  • filename (string) – name of the shapefile to write
  • package_names (list of package names (e.g. ["dis","lpf"])) – Packages to export data arrays to shapefile. (default is None)
Returns:

Return type:

None

Examples

>>> import flopy
>>> m = flopy.modflow.Modflow()
>>> m.to_shapefile('model.shp', SelPackList)
verbose
version
write_input(SelPackList=False, check=False)[source]

Write the input.

Parameters:SelPackList (False or list of packages) –
write_name_file()[source]

Every Package needs its own writenamefile function

class FileData[source]

Bases: object

add_file(fname, unit, binflag=False, output=False, package=None)[source]
class FileDataEntry(fname, unit, binflag=False, output=False, package=None)[source]

Bases: object

class ModelInterface[source]

Bases: object

check(f=None, verbose=True, level=1)[source]
exename
export(f, **kwargs)[source]
get_package_list(ftype=None)[source]

Get a list of all the package names.

Parameters:ftype (str) – Type of package, ‘RIV’, ‘LPF’, etc.
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
hdry
hnoflo
laycbd
laytyp
model_ws
modelgrid
namefile
packagelist
solver_tols
update_modelgrid()[source]
verbose
version
run_model(exe_name, namefile, model_ws='./', silent=False, pause=False, report=False, normal_msg='normal termination', use_async=False, cargs=None)[source]

This function will run the model using subprocess.Popen. It communicates with the model’s stdout asynchronously and reports progress to the screen with timestamps

Parameters:
  • exe_name (str) – Executable name (with path, if necessary) to run.
  • namefile (str) – Namefile of model to run. The namefile must be the filename of the namefile without the path. Namefile can be None to allow programs that do not require a control file (name file) to be passed as a command line argument.
  • model_ws (str) – Path to the location of the namefile. (default is the current working directory - ‘./’)
  • 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 or list) – Normal termination message used to determine if the run 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 that take long time to run. not good for models that run really fast
  • cargs (str or list of strings) – additional command line arguments to pass to the executable. Default is None
Returns:

  • (success, buff)
  • success (boolean)
  • buff (list of lines of stdout)