flopy.export.netcdf module

class Logger(filename, echo=False)[source]

Bases: object

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
items

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

Type:dict
log(phrase)[source]

log something that happened

Parameters:phrase (str) – the thing that happened
warn(message)[source]

Write a warning to the log file

Parameters:message (str) – the warning text
class NetCdf(output_filename, model, time_values=None, z_positive='up', verbose=None, prj=None, logger=None, forgive=False, **kwargs)[source]

Bases: object

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
  • 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.
  • **kwargs (keyword arguments) –
    modelgrid : flopy.discretization.Grid instance
    user supplied model grid which will be used in lieu of the model object modelgrid for netcdf production

Notes

This class relies heavily on the grid and modeltime objects, including these attributes: lenuni, itmuni, start_datetime, and proj4. Make sure these attributes have meaningful values.

add_global_attributes(attr_dict)[source]

add global attribute to an initialized file

Parameters:

attr_dict (dict(attribute name, attribute value)) –

Returns:

Return type:

None

Raises:
  • Exception of self.nc is None (initialize_file()
  • has not been called)
add_sciencebase_metadata(id, check=True)[source]

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

Returns:metadata
Return type:flopy.export.metadata.acdd object
append(other, suffix='_1')[source]
copy(output_filename)[source]
create_group_variable(group, name, attributes, precision_str, dimensions=('time', ))[source]

Create a new group 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”)
  • group (str) – which netcdf group the variable goes in default : None which creates the variable in root
Returns:

Return type:

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
create_variable(name, attributes, precision_str='f4', dimensions=('time', 'layer'), group=None)[source]

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”)
  • group (str) – which netcdf group the variable goes in default : None which creates the variable in root
Returns:

Return type:

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)[source]

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:

Return type:

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

classmethod empty_like(other, output_filename=None, verbose=None, logger=None)[source]
get_longnames_from_docstrings(outfile='longnames.json')[source]

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_file(time_values=None)[source]

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()[source]

initialize the geometric information needed for the netcdf file

initialize_group(group='timeseries', dimensions=('time', ), attributes=None, dimension_data=None)[source]

Method to initialize a new group within a netcdf file. This group can have independent dimensions from the global dimensions

name : str
name of the netcdf group
dimensions : tuple
data dimension names for group
dimension_shape : tuple
tuple of data dimension lengths
attributes : dict
nested dictionary of {dimension : {attributes}} for each netcdf group dimension
dimension_data : dict
dictionary of {dimension : [data]} for each netcdf group dimension
static normalize_name(name)[source]
write()[source]

write the nc object to disk

classmethod zeros_like(other, output_filename=None, verbose=None, logger=None)[source]