Core Classes

class PseudoNetCDF.PseudoNetCDFFile(*args, **kwds)[source]

PseudoNetCDFFile provides an interface and standard set of methods that a file should present to act like a netCDF file using the Scientific.IO.NetCDF.NetCDFFile interface.

apply(verbose=0, **dimfuncs)

Similar to numpy.apply_along_axis, but for damed dimensions and processes dimensions as well as variables

Parameters
  • dimfuncs (dictionary) – key value pairs where the key is a dimensions and the value is a 1D function (func1d) or a dictionary. If the value is a dictionary it must include func1d as a function and any keyword arguments as additional options

  • verbose (integer) – 0 silent, 1 show variable, 2 show dimensions and variables

Returns

outf – instance with variables and dimensions after processing

Return type

PseudoNetCDFFile

applyAlongDimensions(verbose=0, **dimfuncs)[source]

Similar to numpy.apply_along_axis, but for damed dimensions and processes dimensions as well as variables

Parameters
  • dimfuncs (dictionary) – key value pairs where the key is a dimensions and the value is a 1D function (func1d) or a dictionary. If the value is a dictionary it must include func1d as a function and any keyword arguments as additional options

  • verbose (integer) – 0 silent, 1 show variable, 2 show dimensions and variables

Returns

outf – instance with variables and dimensions after processing

Return type

PseudoNetCDFFile

close()[source]

Does nothing. Implemented for continuity with Scientific.IO.NetCDF

copy(props=True, dimensions=True, variables=True, data=True)[source]

Function for making copies of the same type

Parameters
  • props (boolean) – include properties (default: True)

  • dimensions (boolean) – include dimensions (default: True)

  • variables (boolean) – include variable structures (default: True)

  • data (boolean) – include variable data (default: True)

Returns

outf

Return type

PseudoNetCDFFile instance

copyVariable(var, key=None, dtype=None, dimensions=None, fill_value=None, withdata=True)[source]

Copy var into self as vark

Parameters
  • var (PseudoNetCDFVariable) – netCDF4.Variable-like object (must have ncattrs and setncatts)

  • key (string) – key for variable in self (can be omitted if var has name, standard_name, or long_name)

  • dtype (string or numpy.dtype) – change the data type to dtype

  • dimensions (iterable of strings) – change the dimensions to dimensions

  • fill_value (integer or flaot) – change the fill_value to this values

  • withdata (boolean) – default True, copies data

Returns

myvar – copy of var in this file

Return type

PseuodNetCDFVairable

createDimension(name, length)[source]

Create a dimension

Parameters
  • name (string) – name for dimension

  • length (integer) – maximum length of dimension

Returns

dim – new dimension

Return type

PseudoNetCDFDimensions

createVariable(name, type, dimensions, fill_value=None, **properties)[source]

Create a variable

Parameters
  • name (string) – name for new variable

  • type (string or numpy dtype) – code (e.g., ‘f’, ‘i’, ‘d’)

  • dimensions (tuple of strigns) – dimension keys that can be found in objects’ dimensions dictionary

Returns

var

Return type

new variable

date2num(time, timekey='time')[source]
Parameters
  • time (array-like) – array of datetime.datetime objects

  • timekey (str) – time variable key which requires units and should have calendar. If calendar is missing, standard is the default. default ‘time’

Returns

num – time in relative time as defined by units of time variable (i.e., timekey) which defaults to ‘time’

Return type

array-like

eval(expr, inplace=False, copyall=False)[source]

Evaluate expr and return a PseudoNetCDFFile object with resutl

Parameters
  • expr (string) – expression to evaluate

  • inplace (boolean) – create the new variable in this netcdf file (default False)

  • copyall (boolean) – if not inplace, should all variables be copied to new file

Returns

outf – instance with renamed variable (this file if inplace = True)

Return type

PseudoNetCDFFile

flush()

Does nothing. Implemented for continuity with Scientific.IO.NetCDF

classmethod from_ncf(infile)[source]
Parameters

infile (PseudoNetCDF-like file) –

Returns

outf

Return type

PseudoNetcdf-like file

classmethod from_ncvs(*invars, **invarkw)[source]
Parameters
  • invars (list) – NetCDF-like variable must have standard_name, long_name or name

  • invarkw (kwds) – NetCDF-like variables

Returns

outf

Return type

PseudoNetcdf-like file

getCoords()[source]

Return a list of coordkeys

getMap(maptype='basemap_auto', **kwds)[source]

Description

Parameters
  • maptype (string) –

    choices ‘basemap’, ‘basemap_auto’, ‘cartopy’ (not yet) basemap : attempts to open a basemap with only supplied kwds basemap_auto : automatically adds llcrnrlon,llcrnrlat,u

    rcrnrlon,urcrnrlat based on longitude_bounds

  • **kwds (keywords) – for basemap or cartopy

Returns

map

Return type

basemap or cartopy axis

getTimes(datetype='datetime', bounds=False)[source]

Get an array of datetime objects

Parameters
  • datetype (string or numpy.dtype) – ‘datetime’ or datetime64 dtype

  • bounds (boolean) – get time boundaries

Returns

out – datetime objects or array of numpy’s datetype type

Return type

array

Notes

self must have a time or TFLAG variable

get_dest()[source]
Returns

path – path where a new file is created on some action

Return type

str

Notes

If None, a file is created in memory. Else, a netcdf file is created on disk.

get_varopt()[source]

Get options

Parameters

None

Returns

options

Return type

dictionary of options

getncatts()[source]

Return all ncattrs keys and values as a dictionary

Returns

attdict – key/value pairs of properties

Return type

dictionary

getproj(withgrid=False, projformat='pyproj')[source]

Description

Parameters
  • withgrid (boolean) – use grid units instead of meters

  • projformat (string) – ‘pyproj’ (default), ‘proj4’ or ‘wkt’ allows function to return a pyproj projection object or a string in the format of proj4 or WKT

Returns

proj – (wkt, proj4) or pyprojProj (pyproj)

Return type

string pyproj.Proj

ij2ll(i, j)[source]

Converts i, j to lon, lat (no false easting/northing) using cell centers assuming 0-based i/j

Parameters
  • i (scalar/iterable) – indicies (0-based) for the west-east dimension

  • j (scalar/iterable) – indicies (0-based) for the south-north dimension

Returns

lon, lat – longitudes and latitudes in decimal degrees

Return type

scalars or iterables

insertDimension(newonly=True, multionly=False, before=None, after=None, inplace=False, **newdims)[source]

Insert dimensions with keys and lengths from newdims

Parameters
  • **newdims (dictionary) – where key is the new dimension and value is the length

  • newonly (boolean) – Only add dimension to variables that do not already have it, default True

  • multionly (boolean) – Only add dimension if there are already more than one (good for ignoring coordinate dimensions)

  • before (string) – if variable has this dimension, insert the new dimension before it. Otherwise, add to the beginning. (before takes precedence)

  • after (string) – if variable has this dimension, insert the new dimension after it. Otherwise, add to the beginning.

  • inplace (boolean) – create the new variable in this netcdf file (default False)

Returns

outf – instance will new dimension in dimensions and variables

Return type

PseudoNetCDFFile

Notes

  1. Adding a non unity dimension will cause the data to be repeated along the new axis.

  2. If order of addition matters, use multiple calls. newdimsuse will be a non-ordered dictionary

interpDimension(dimkey, newdimvals, coordkey=None, **interpkwds)[source]
Parameters
  • dimkey (string) – the new dimension for interpolation

  • newdimvals (iterable) – the new values to interpolate to

  • coordkey (string) – the variable to use as the old coordinate values

  • interptype (string) –

    ‘linear’ or ‘conserve’. linear uses a linear interpolation

    conserve uses a mass conserving interpolation

  • extrapolate (boolean) – allow extrapolation beyond bounds with linear, default False

  • fill_value (numeric value) – set fill value (e.g, nan) to prevent extrapolation or edge continuation

Returns

outf – instance with all variables interpolated

Return type

PseudoNetCDFFile

Notes

When extrapolate is false, the edge values are used for points beyond the inputs.

classmethod isMine(*args, **kwds)[source]

True if this file or object can be identified for use by this class. Useful to override for classes that can be initialized from disk.

ll2ij(lon, lat, bounds='ignore', clean='none')[source]

Converts lon/lat to 0-based indicies (0,M), (0,N)

Parameters
  • lon (scalar or iterable) – longitudes in decimal degrees

  • lat (scalar or iterable) – latitudes in decimal degrees

  • bounds (string) – ignore, error, warn if i,j are out of domain

  • clean (string) – none - return values regardless of bounds; mask - mask values out of bounds; clip - return min(max(0, v), nx - 1)

Returns

i, j

Return type

indices (0-based) for variables

ll2xy(lon, lat)[source]

Converts lon/lat to x distances (no false easting/northing)

Parameters
  • lon (scalar or iterable) – longitudes in decimal degrees

  • lat (scalar or iterable) – latitudes in decimal degrees

Returns

x, y – coordinates in map projection (meters or radians)

Return type

tuple of arrays

mask(where=None, less=None, less_equal=None, greater=None, greater_equal=None, values=None, equal=None, invalid=False, mask=None, dims=None, fill_value=- 999, coords=False, verbose=0)[source]

Apply mask to all variables of same shape or just where dimensions match.

Parameters
  • where (array-like) – boolean array to use as a mask see numpy.ma.masked_where

  • greater (scalar) – mask when values are greater than this value see numpy.ma.masked_greater

  • less (scalar) – mask when values are less than this value see numpy.ma.masked_less

  • greater_equal (scalar) – mask when values are greater than or equal to this value see numpy.ma.masked_greater

  • less_equal (scalar) – mask when values are less than or equal to this value see numpy.ma.masked_less

  • values (scalar) – mask when values are equal to this value within standard floating point see numpy.ma.masked_values

  • equal (scalar) – mask when values are exactly this value (i.e., integers) see numpy.ma.masked_equal

  • invalid (boolean) – mask when values are invalid, see numpy.ma.masked_invalid

  • mask (array-like) – alias for where

  • dims (iterable of strings) – only apply “mask” or “where” to variables with these dimensions no effet on other masks

  • fill_value (scalar) – value to use as the fill_value for new arrays

  • coords (boolean) – if True, apply masks to coordinate variables. Default, False

  • verbose (int) – level of verbosity for function, mostly for debugging

Returns

outf – instance with masked variables

Return type

PseudoNetCDFFile

See also

numpy.ma()

all masks are passing throught to numpy.ma.masked_

Notes

mask options are not mutually exclusive. the order is where, greater, greater_equal, less, less_equal, values, equal, invalid

plot(varkey, plottype='longitude-latitude', ax_kw=None, plot_kw=None, cbar_kw=None, map_kw=None, dimreduction='mean')[source]
Parameters
  • varkey (string) – the variable to plot

  • plottype (string) – longitude-latitude, latitude-pressure, longitude-pressure, vertical-profile, time-longitude, time-latitude, time-pressure default, longitude-latitude

  • ax_kw (dictionary) – keywords for the axes to be created

  • plot_kw (dictionary) – keywords for the plot (plot, scatter, or pcolormesh) to be created

  • cbar_kw (dictionary) – keywords for the colorbar

  • map_kw (dictionary) – keywords for the getMap routine, which is only used with plottype=’longitude-latitude’

  • dimreduction (string or function) – dimensions not being used in the plot are removed using applyAlongDimensions(dimkey=dimreduction) where each dimenions

removeSingleton(dimkey=None)[source]

Return a netcdflike object with dimensions sliced

Parameters

dimkey (string) – key of dimension to be evaluated for removal; if None, evaluate all. only singleton dimensions will be removed.

Returns

outf – instance with dimensions removed

Return type

PseudoNetCDFFile

renameDimension(oldkey, newkey, inplace=False)[source]

Rename dimension (oldkey) in dimensions and in all variables

Parameters
  • oldkey (string) – dimension to be renamed

  • newkey (string) – new dame for dimension

  • inplace (boolean) – create the new variable in this netcdf file (default False)

Returns

outf – instance with renamed variable (this file if inplace = True)

Return type

PseudoNetCDFFile

renameDimensions(inplace=False, **newkeys)[source]

Rename dimension (oldkey) in dimensions and in all variables

Parameters
  • **newkeys (dictionary) – where key is the oldkey and value is the newkey

  • inplace (boolean) – create the new variable in this netcdf file (default False)

Returns

outf – instance with renamed variable (this file if inplace = True)

Return type

PseudoNetCDFFile

renameVariable(oldkey, newkey, inplace=False, copyall=True)[source]

Rename variable (oldkey)

Parameters
  • oldkey (string) – variable to be renamed

  • newkey (string) – new dame for variable

  • inplace (boolean) – create the new variable in this netcdf file (default False)

  • copyall (boolean) – if not inplace, should all variables be copied to new file

Returns

outf – instance with renamed variable (this file if inplace = True)

Return type

PseudoNetCDFFile

renameVariables(inplace=False, copyall=True, **newkeys)[source]

Rename variables for each oldkey: newkey dictionary item

Parameters
  • **newkeys (dictionary) – where key is the oldkey and value is the newkey

  • inplace (boolean) – create the new variable in this netcdf file (default False)

  • copyall (boolean) – if not inplace, should all variables be copied to new file

Returns

outf – instance with renamed variable (this file if inplace = True)

Return type

PseudoNetCDFFile

reorderDimensions(oldorder, neworder, inplace=False)[source]

Evaluate expr and return a PseudoNetCDFFile object with resutl

Parameters
  • oldorder (iterable of strings) – dimension names in existing order

  • neworder (iterable of strings) – dimension names in new order

Returns

outf – instance with dimensions reordered in variables

Return type

PseudoNetCDFFile

save(*args, **kwds)[source]

Provides access to pncwrite for self

Parameters

Help pncwrite (see) –

Returns

Return type

see Help pncwrite

setCoords(keys, missing='ignore')[source]

Set a variable as a coordinate variable

Parameters
  • keys (iterable of strings) – keys for coord variables

  • missing (string) – action if missing ‘ignore’, ‘skip’ or ‘error’ ignore - add in case used later skip - do not add error - raise an error

Returns

Return type

None

Notes

Coordinate variables are excluded from math

set_dest(path, **options)[source]

Sets the path where a new file is created on some action

Parameters
  • path (str) – path for new file

  • **options (keywords for constructor) – options for new file creation

Returns

Return type

None

set_varopt(**options)[source]

Set options to be used when creating any Variable

Parameters

**options (options for createVariable) – optional keywords to be supplied when creating new variables in destination file

Returns

Return type

None

setncatts(attdict)[source]

Set ncattrs from attdict keys and values

Parameters

attdict (dictionary) – key/value pairs of properties

Returns

Return type

None

slice(newdims='POINTS', verbose=0, **dimslices)

Return a netcdflike object with dimensions sliced

Parameters
  • newdims (iterable of strings) – names for new dimensions. When more than one iterable applies to a variable slice, fancy indexing removes both dimensions and creates a new one of the iterable lengths

  • **dimslices (dictionary) – key value pairs where the key is a dimension and the value is a valid slice object (slices, ints or iterables) if iterables are provided, all iterables must be the same size and shape. If the arrays are not 1D, newdims must have ndim names

Returns

outf – instance with variables and dimensions sliced

Return type

PseudoNetCDFFile

sliceDimensions(newdims='POINTS', verbose=0, **dimslices)[source]

Return a netcdflike object with dimensions sliced

Parameters
  • newdims (iterable of strings) – names for new dimensions. When more than one iterable applies to a variable slice, fancy indexing removes both dimensions and creates a new one of the iterable lengths

  • **dimslices (dictionary) – key value pairs where the key is a dimension and the value is a valid slice object (slices, ints or iterables) if iterables are provided, all iterables must be the same size and shape. If the arrays are not 1D, newdims must have ndim names

Returns

outf – instance with variables and dimensions sliced

Return type

PseudoNetCDFFile

stack(other, stackdim)[source]

Concatenates all variables on stackdim

Parameters
  • other (instance or list of PseudoNetCDFFiles) – files to add to this file along stackdim

  • stackdim (str) – dimension name

Returns

outf – instance with stacked variables and dimension equal to new length

Return type

PseudoNetCDFFile

subset(varkeys, inplace=False, exclude=False, keepcoords=True)

Return a PseudoNetCDFFile with only varkeys

Parameters
  • varkeys (iterable of strings) – keys to keep

  • inplace (boolean) – if true (default false), then remove other variable from this file

  • exclude (boolean) – if True (default False), then remove just these variables

  • keepcoords (boolean) – if True (default True), keep coordinate variables

Returns

outf – instance with variables

Return type

PseudoNetCDFFile

subsetVariables(varkeys, inplace=False, exclude=False, keepcoords=True)[source]

Return a PseudoNetCDFFile with only varkeys

Parameters
  • varkeys (iterable of strings) – keys to keep

  • inplace (boolean) – if true (default false), then remove other variable from this file

  • exclude (boolean) – if True (default False), then remove just these variables

  • keepcoords (boolean) – if True (default True), keep coordinate variables

Returns

outf – instance with variables

Return type

PseudoNetCDFFile

sync()

Does nothing. Implemented for continuity with Scientific.IO.NetCDF

time2idx(time, dim='time', timekey=None, **kwds)[source]

Convert datetime objects to dimension indices

Parameters
  • time (array-like) – array of datetime.datetime objects

  • dim (str) – dimension name for val2idx

  • timekey (str) – time variable key. None defaults to dim

  • kwds (mappable) – see val2idx

Returns

idx – time index (0-based)

Return type

array-like

time2t(time, ttype='nearest', index=True)[source]
Parameters
  • time (array of datetime.datetime objects) –

  • interp ('nearest', 'bounds', 'bounds_close') –

  • index (return index) –

Returns

t

Return type

fractional time or if index, integers for indexing

val2idx(dim, val, method='nearest', bounds='warn', left=None, right=None, clean='mask')[source]

Convert coordinate values to indices

Parameters
  • dim (str) – name of dimensions, which must have a coordinate variable

  • val (array-like) – value in coordinate space

  • method (str) –

    nearest, bounds, exact - each calculates the index differently
    • nearest : uses interp with coord values and rounds

    • bounds : uses interp between bounding values and truncates

    • exactreturns indices for exact coord values with other

      indices masked (clean keyword has no effect)

  • bounds (str) – ignore, error, warn if i,j are out of domain

  • left (scalar) – see np.interp

  • right (scalar) – see np.interp

  • clean ({'none', 'mask'}) –

    none - return values regardless of bounds; mask - mask invalid values (use with left/right=np.nan);

    has no affect with method exact

Returns

i – indices (0-based) for variables

Return type

array-like

xy2ll(x, y)[source]

Converts x, y to lon, lat (no false easting/northing)

Parameters
  • x (scalar or iterable) – projected west-east coordinates

  • y (scalar or iterable) – projected south-north coordinates

Returns

lon, lat – longitudes and latitudes in decimal degrees

Return type

scalars or iterables

class PseudoNetCDF.PseudoNetCDFVariable(parent, name, typecode, dimensions, **kwds)[source]

PseudoNetCDFVariable presents the Scientific.IO.NetCDF.NetCDFVariable interface, but unlike that type, provides a contructor for variables that could be used without adding it to the parent file

Creates a variable using the dimensions as defined in the parent object

Parameters
  • parent (PseudoNetCDFFile) – an object with a dimensions variable

  • name (string) – name for variable

  • typecode (string or numpy.dtype) – numpy style typecode

  • dimensions (tuple of strings) – dimension names to be used from parent

  • **kwds (keywords) – Dictionary of keywords to be added as properties to the variable. The keyword ‘values’ is a special case that will be used as the starting values of the array

array()[source]
Returns

out – parent type view object

Return type

numpy.ndarray

assignValue(value)[source]
Parameters

value (scalar) – assign value to scalar variable

getValue()[source]
Returns

item – value

Return type

scalar

ncattrs()[source]
Returns

ncattrs – attributes that have been user defined

Return type

tuple of strings

swapaxes(axis1, axis2)[source]

Return a view of the array with axis1 and axis2 interchanged.

Refer to numpy.swapaxes for full documentation.

See also

numpy.swapaxes()

equivalent function

xarray(iscoord=False)[source]

Experimental function

Returns

out – object with dimensions and coordinates dims : string are set by self.get_coords() coords : are set by self.get_coords()

Return type

xarray.DataArray

Notes

When data is 2-d, the dimensions