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

Parameters:

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

Attributes

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(phrase)

log something that happened

Parameters:

phrase : str

the thing that happened

warn(message)

Write a warning to the log file

Parameters:

message : str

the warning text

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

Support for writing a netCDF4 compliant file from a flopy model

Parameters:

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

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.

Notes

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_attributes(attr_dict)

add global attribute to an initialized file

Parameters:

attr_dict : dict(attribute name, attribute value)

Returns:

None

Raises:

Exception of self.nc is None (initialize_file()

has not been called)

create_variable(name, attributes, precision_str='f4', dimensions=('time', 'layer', 'y', 'x'))

Create a new variable in the netcdf object

Parameters:

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”)

Returns:

nc variable

Raises:

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

Parameters:

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

Returns:

net NetCDF instance

Notes

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

initialize_file(time_values=None)

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

Parameters:

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_geometry()

initialize the geometric information needed for the netcdf file

write()

write the nc object to disk