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 (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, default "mf2k.exe") – Name of the modflow executable.

  • model_ws (str, optional) – 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.

  • 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), proj4_str for a PROJ string, and start_datetime for model start date (default “1-1-1970”).

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.

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.

Return type

None

Examples

>>> import flopy
>>> m = flopy.modflow.Modflow.load('model.nam')
>>> m.check()
property 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) –

    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_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

property hdry
property hnoflo
property laycbd
property laytyp
load_results()[source]
property model_ws
property modelgrid
property modeltime
property name

Get model name

Returns

name – name of model

Return type

str

property namefile
next_ext_unit()[source]

Function to encapsulate next_ext_unit attribute

next_unit(i=None)[source]
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

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)

Return type

None

Examples

>>> import flopy
>>> m = flopy.modflow.Modflow()
>>> m.to_shapefile('model.shp', SelPackList)
property verbose
property 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

abstract check(f=None, verbose=True, level=1)[source]
abstract property exename
abstract 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

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
update_modelgrid()[source]
abstract property verbose
abstract property version
exception PackageLoadException(error, location='')[source]

Bases: Exception

FloPy package load exception.

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)