API

class pymom6.pymom6.Dataset(filename, **initializer)

This class provides more convinient access to variables of a netcdf file.

>>> ds = Dataset(filename)
>>> var = ds.var
>>> ds.close()

It can be used as a contextmanager using with statement.

>>> with Dataset(filename) as ds:
        var = ds.var
close()

Closes an open Dataset object.

Returns:None
Return type:None
class pymom6.pymom6.GridGeometry(filename)

This class holds the variables from ocean_geometry.nc file

Parameters:filename – Name of ocean_geometry file
Returns:A class with references to variables of ocean_geometry.nc file
Return type:pymom6.GridGeometry
class pymom6.pymom6.MOM6Variable(var, fh, **initializer)

A class to hold a variable from netcdf file generated by MOM6. A MOM6 variable that is located at one of the h,u,v,q,l,i points.

Parameters:
  • var – name of the variable
  • fh – a handle to an open netCDF file
Returns:

a variable object holding all the data

Return type:

MOM6Variable

BoundaryCondition

alias of _BoundaryCondition

LazyNumpyOperation

alias of _LazyNumpyOperation

bc_type(bc_type)

Specifies the boundary conditions for the top, bottom, south, north, west, and east boundaries.

Example:

bc_type = dict(v=[‘neumann’, ‘dirichleth’, ‘zeros’, ‘dirichletq’, ‘dirichleth’, ‘dirichleth’])

TODO: Simplify specifying BCs

Parameters:bc_type – Dict indicating six boundary conditions
Returns:MOM6Variable
Return type:MOM6Variable
compute(check_loc=True)

Sequentially executes all the lazy operations.

Parameters:check_loc – Checks if the final location is same as the

current location if true else disables the check. :returns: MOM6Variable :rtype: MOM6Variable

conditional_toz(toz, z, e, dimstr='z (m)')

Implements toz conditionally. See toz method documentation for definitions of other arguments.

Parameters:toz – Boolean that determines whether to run toz method

or not :returns: MOM6Variable :rtype: MOM6Variable

dbyd(axis, weights=None)

This method implements the differentiation operator.

Parameters:
  • axis – 0, 1, 2, or 3 for t, z, y, or x
  • weights – If None (deafult) divisor is grid spacing, if

‘area’ divisor is cell area :returns: MOM6Variable :rtype: MOM6Variable

fillvalue(fillvalue)

Sets the fillvalue for masked arrays (these are generally values at topography, either 0 or np.nan)

Parameters:fillvalue – 0 (default) or np.nan
Returns:MOM6Variable
Return type:MOM6Variable
final_loc(final_loc=None)

Sets the final location of MOM6Variable

Possible usage:

>>> MOM6Variable('u',fh).final_loc('ul').read()

Same as:

>>> MOM6Variable('u',fh,final_loc='ul').read()
Parameters:final_loc – Two alphabet string (One of ‘h’,’u’,’v’, or ‘q’

and one of ‘l’ or ‘i’). Default is the location of the variable e.g. u will be at ‘ul’, e at ‘hi’, etc. :returns: MOM6Variable :rtype: MOM6Variable

geometry(geometry)

Specifies the GridGeometry object. This is necessary when differentiating or moving location (e.g. from u to h points) of the variable.

Parameters:geometry – pymom6.GridGeometry instance
Returns:MOM6Variable
Return type:MOM6Variable
static get_dimensions_by_location(loc)

Returns the names of the dimensions corresponding to a location, loc.

Parameters:loc – Location (same format as final_loc, 2 letter string)
Returns:4-element tuple containing dimension names
Return type:tuple
get_slice()

Populates the _slice attribute of MOM6Variable based on current dimensions. This slice can be used to slice other arrays.

Returns:MOM6Variable
Return type:MOM6Variable
get_slice_2D()

Populates the _slice_2D attribute of MOM6Variable based on current dimensions. Only x and y slices are populated. This slice can be used to slice other arrays.

Returns:MOM6Variable
Return type:MOM6Variable
implement_BC_if_necessary()

This post-read method checks if any of the indices extend beyond the buondary implements boundary conditions as specified by bc_type (or default spedified by _default_bc_type attribute)

Returns:MOM6Variable
Return type:MOM6Variable
implement_BC_if_necessary_for_multiplier(multiplier)

Same as implement BC if necessary but for multiplier

Parameters:multiplier – Attribute of GridGeometry (like area, dxT, etc.)
Returns:multiplier with boundary conditions imposed if necessary
Return type:np.ndarray
isel(**kwargs)

Creates a subset of a MOM6Variable object by index. This only works if used before read().

Possible usage:

>>> MOM6Variable('u',fh).isel(x=48)
>>> MOM6Variable('u',fh).isel(x=48,y=25)
>>> MOM6Variable('u',fh).isel(x=slice(48,50),y=25)

Any of ‘t’, ‘time’, ‘T’, or ‘Time’ can be used to slice time dimension. Any of ‘z’, ‘zl’, or ‘zi’ can be used to slice vertical dimension. Any of ‘y’, ‘yh’, or ‘yq’ can be used to slice meridional dimension. Any of ‘x’, ‘xh’, or ‘xq’ can be used to slice zonal dimension.

Returns:Subset MOM6Variable object
Return type:MOM6Variable object
modify_index(axis, startend, value)

This method modifies the indices associated with the axis

Parameters:
  • axis – One of 0,1,2,3 corresponding to Time, z, y, or x axis
  • startend – 0 or 1 indicates whether the starting or the

ending index is modified, respectively :param value: Modify the index by this value :returns: None :rtype: Nonetype

modify_index_return_self(axis, startend, value)

Same as modify_index. This method returns self at the end. This method is used to create convinience methods xsm, xep, ysm, yep, zsm, and zep. The first, second, and third alphabets stand for the axis, starting or ending index, and plus or minus. These methods add or subtract one from the index of the given axis.

For example if you want to lengthen the x-axis domain towards the east, you should use xep as this increases the ending index of x by 1. Conversely, if you want to extend the x-axis domain towrds the west, you should use xsm as this subtracts 1 from the starting index of x.

Parameters:
  • axis – One of 0,1,2,3 corresponding to Time, z, y, or x axis
  • startend – 0 or 1 indicates whether the starting or the

ending index is modified, respectively :param value: Modify the index by this value :returns: MOM6Variable :rtype: MOM6Variable

move_to(new_loc)

Moves the MOM6Variable to a new location

Parameters:new_loc – One letter string (u, v, h, q, l, or i). If l

or i is given, vertical move is assumed else horizontal move is assumed. :returns: MOM6Variable at a new location :rtype: MOM6Variable

multiply_by(multiplier, power=1)

This is a post-read method that multiplies the MOM6Variable by an attribute of GridGeometry.

Parameters:multiplier – A string indicating any attribute from

GridGeometry like area, dxT, etc. :param power: 1 (default) or -1 (used for divide_by method) :returns: MOM6Variable :rtype: MOM6Variable

nanmean(axis=[0, 1, 2, 3])

Implements nanmean for MOM6Variable (post-read method)

Parameters:axis – A single axis or sequence of axes among 0, 1, 2,

3. The mean is taken along axis/axes supplied here :returns: MOM6Variable :rtype: MOM6Variable

np_ops(npfunc, *args, **kwargs)

Implements functionality to apply a numpy operation to MOM6Variable. If the numpy operation changes dimension sizes, they must be manually adjusted using axis, ns, and ne kwargs. If numpy operation changes vertical or horizontal location, it should be manually set by using sets_vloc or sets_hloc kwargs.

Parameters:npfunc – string indicating numpy method (e.g. ‘nanmean’)
Returns:MOM6Variable
Return type:MOM6Variable
read()

This method reads the data from the disk into memory. Once this method is called all the pre-read methods should not be used. Post-read methods should be used only after this method has been called.

Returns:MOM6Variable
Return type:MOM6Variable
reduce_(reduce_func, *args, keepdims=True, **kwargs)

Implements reduction operations on MOM6Variable. Axes can be specified as a single axis or a sequence of 0, 1, 2, 3.

Parameters:reduce_func – numpy reduction operations like np.mean,

np.sum, etc. :returns: MOM6Variable :rtype: MOM6Variable

return_dimensions()

Gives the current dimensions of a MOM6Variable. This method is also invoked by MOM6Variable.dimension property.

Returns:A dictionary of current dimensions
Return type:OrderedDict
sel(**kwargs)

Creates a subset of a MOM6Variable object. This only works if used before read().

Possible usage:

>>> MOM6Variable('u',fh).sel(x=48)
>>> MOM6Variable('u',fh).sel(x=48,y=25.2)
>>> MOM6Variable('u',fh).sel(x=slice(48.5,50),y=25)

Any of ‘t’, ‘time’, ‘T’, or ‘Time’ can be used to slice time dimension. Any of ‘z’, ‘zl’, or ‘zi’ can be used to slice vertical dimension. Any of ‘y’, ‘yh’, or ‘yq’ can be used to slice meridional dimension. Any of ‘x’, ‘xh’, or ‘xq’ can be used to slice zonal dimension.

Returns:Subset MOM6Variable object
Return type:MOM6Variable object
to_DataArray(check_loc=True)

Converts MOM6Variable to xarray.DataArray instance. This is useful for plotting.

Parameters:check_loc – Checks if the final location is same as the

current location if true else disables the check. :returns: xarray.DataArray of the MOM6Variable :rtype: xarray.DataArray

tob(axis, dim_str=None)

Converts the vertical dimension to buoyancy from density

Parameters:
  • axis – 1
  • dim_str – String representing the newly converted dimension
Returns:

MOM6Variable

Return type:

MOM6Variable

tokm(axis, dim_str=None)

Converts x or y axis dimension from degrees to km

Parameters:
  • axis – one of 2 or 3
  • dim_str – String representing the new dimension
Returns:

MOM6Variable with axis converted to km

Return type:

MOM6Variable

toz(z, e=None, dimstr='z (m)')

Move MOM6Variable to z coordinate from buoyancy coordinates

Parameters:
  • z – np.ndarray of z locations or a single location
  • e – MOM6Variable containing e (isopycnal heights)
  • dimstr – The string to represent new vertical dimension
Returns:

MOM6Variable in z coordinates

Return type:

MOM6Variable

where(logic_function, other_array, y=None)

Implements the numpy.where method for MOM6Variable

Parameters:
  • logic_function – numpy logical method (e.g. np.greater_equal)
  • other_array – array of values to be checked against (see

numpy.where docs) :param y: Values from this array are chosen where logic_func returns false (see numpy.where docs). If y is None (default) MOM6Variable.array.nonzero is returned. :returns: MOM6Variable :rtype: MOM6Variable