flopy.export.netcdf Module

class flopy.export.netcdf.Logger(filename, echo=False)

Basic class for logging events during the linear analysis calculations if filename is passed, then an file handle is opened


filename : bool or string

if string, it is the log file to write. If a bool, then log is written to the screen. echo (bool): a flag to force screen output


items (dict) tracks when something is started. If a log entry is not in items, then it is treated as a new entry with the string being the key and the datetime as the value. If a log entry is in items, then the end time and delta time are written and the item is popped from the keys

log something that happened


phrase : str

the thing that happened


Write a warning to the log file


message : str

the warning text

class flopy.export.netcdf.NetCdf(output_filename, model, time_values=None, z_positive='up', verbose=None, prj=None, logger=None, forgive=False)

Support for writing a netCDF4 compliant file from a flopy model


output_filename : str

Name of the .nc file to write

model : flopy model instance

time_values : the entries for the time dimension

if not None, the constructor will initialize the file. If None, the perlen array of ModflowDis will be used

z_positive : str (‘up’ or ‘down’)

Positive direction of vertical coordinates written to NetCDF file. (default ‘down’)

verbose : if True, stdout is verbose. If str, then a log file

is written to the verbose file

forgive: what to do if a duplicate variable name is being created. If

True, then the newly requested var is skipped. If False, then an exception is raised.


This class relies heavily on the ModflowDis object, including these attributes: lenuni, itmuni, start_datetime, sr (SpatialReference). Make sure these attributes have meaningful values.


add global attribute to an initialized file


attr_dict : dict(attribute name, attribute value)




Exception of self.nc is None (initialize_file()

has not been called)

add_sciencebase_metadata(id, check=True)

Add metadata from ScienceBase using the flopy.export.metadata.acdd class.

Returns:metadata : flopy.export.metadata.acdd object
create_variable(name, attributes, precision_str='f4', dimensions=('time', 'layer', 'y', 'x'))

Create a new variable in the netcdf object


name : str

the name of the variable

attributes : dict

attributes to add to the new variable

precision_str : str

netcdf-compliant string. e.g. f4

dimensions : tuple

which dimensions the variable applies to default : (“time”,”layer”,”x”,”y”)


nc variable


AssertionError if precision_str not right

AssertionError if variable name already in netcdf object

AssertionError if one of more dimensions do not exist

difference(other, minuend='self', mask_zero_diff=True, onlydiff=True)

make a new NetCDF instance that is the difference with another netcdf file


other : either an str filename of a netcdf file or

a netCDF4 instance

minuend : (optional) the order of the difference operation.

Default is self (e.g. self - other). Can be “self” or “other”

mask_zero_diff : bool flag to mask differences that are zero. If

True, positions in the difference array that are zero will be set to self.fillvalue

only_diff : bool flag to only add non-zero diffs to output file


net NetCDF instance


assumes the current NetCDF instance has been populated. The variable names and dimensions between the two files must match exactly. The name of the new .nc file is <self.output_filename>.diff.nc. The masks from both self and other are carried through to the new instance


This is experimental.

Scrape Flopy module docstrings and return docstrings for parameters included in the list of variables added to NetCdf object. Create a dictionary of longnames keyed by the NetCdf variable names; make each longname from the first sentence of the docstring for that parameter.

One major limitation is that variables from mflists often aren’t described in the docstrings.


initialize the netcdf instance, including global attributes, dimensions, and grid information


time_values : list of times to use as time dimension

entries. If none, then use the times in self.model.dis.perlen and self.start_datetime


initialize the geometric information needed for the netcdf file


write the nc object to disk