geodat.nc.Variable - Variable container

class geodat.nc.Variable(reader=None, varname=None, data=None, dims=None, attributes=None, history=None, parent=None, ensureMasked=False, **kwargs)[source]

A container for handling physical variable together with its dimensions so that while the variable is manipulated (e.g. averaged along one axis), the information of the dimensions change accordingly.

It can be indexed/sliced the same way as indexing a numpy array

__call__(**region)[source]

Same as self.getRegion

addHistory(string)[source]

Append to the history attribute in the variable. If history doesn’t exist, create one

apply_mask(mask)[source]

mask the variable’s last axes with a mask This function changes the variable

area_ave()[source]

Compute area average Same as wgt_ave(‘XY’)

climatology(*args, **kwargs)[source]

Compute the climatology

ensureMasked()[source]

If the data in the variable is not a masked array and missing_value is present Read the data and convert the numpy ndarray into masked array, note that this will be slow if the data is large. But this will only be done once.

Returns: None

getAxes()[source]

Return the dimensions of the variable as a list of numpy arrays In the order of dimension

getAxis(axis)[source]

Return a numpy array of an axis of a variable Input:

axis (int or str): if it is an integer, do nothing and return axis
if it is a str, look for index of the dimension which matches the required cartesian axis using the CAxes function
Returns:numpy array

See also

CAxes, getIAxis, getDim

getCAxes()[source]

get the cartesian axes for all the dimensions. Return a list of cartesian axes. if it is undefined, replace with dummy: A,B,C,...(excludes: T/Z/X/Y)

getDate(toggle='YmdHMS', no_continuous_duplicate_month=False)[source]

Return the time axis date in an array format of “Year,Month,Day,Hour,Minute,Second” Toggle one or many among Y/m/d/H/M/S to select a particular time format

Parameters:
  • toggle (iterable of str) – each item should be among Y/m/d/H/M/S
  • no_continuous_duplicate_month (bool) – used for toggle==”m” only

no_continuous_duplicate_month will check if there are adjacent months that are identical. If so, check if the data is a monthly series and correct for the duplicates

Examples

>>> # return an array of the month of the time axis
>>> var.getDate("m")
array([ 1, 2, 3, 4, 5, 6 ])
>>> # return an array with the first column showing the years,
>>> # second column showing the months, third column
>>> # for days
>>> getDate("Ymd")
array([[ 1990, 1, 15 ], [ 1990, 2, 15 ], [ 1990, 3, 15 ]])
getDim(axis)[source]

Return a the Dimension instance of an axis of a variable Input:

axis (int or str): if it is an integer, do nothing and return axis
if it is a str, look for index of the dimension which matches the required cartesian axis using the CAxes function
Returns:numpy array

See also

CAxes, getIAxis, getAxis

getDimnames()[source]

Return a list of dimension names

getDomain(axis=None)[source]

Return the domain of the variable

If the axis is a longitude axis, make all negative degree positive (only for output; the variable longitude data is unchanged)

Parameters:axis (str or int) – query the domain of a particular dimension. If it is not specified, the domains of all dimensions are returned
Returns:dict

Examples

>>> # var is a regional variable within (20S-20N, 140E-140W)
>>> var.getDomain()
{"X": (140.,220.), "Y": (-20.,20.)}
>>> var.getDomain("X")
{"X": (140.,220.)}
getIAxis(axis)[source]

Return the integer for the required cartesian axis Input:

axis (int or str): if it is an integer, do nothing and return axis
if it is a str, look for index of the dimension which matches the required cartesian axis using the CAxes function
Returns:int

See also

CAxes, getAxis, getDim

getLatitude()[source]

Return a numpy array that contains the latitude axis

getLongitude()[source]

Return a numpy array that contains the longitude axis

getMissingValue()[source]

Return “missing_value” if defined in the attributes Otherwise “_FillValue” will be used as missing value If both are undefined, the numpy default for the variable data type is returned

getRegion(**kwargs)[source]

Return a new Variable object within the region specified.

Values have to be a length-2 iterable that specifies the range

Keys “time”,”t”,”TIME”,”T” are all considered as “T” for time axis.

Keys “x”,”X”,”lon”,”LON”,”longitude”,”LONGITUDE” are all considered as
“X” for the longitude axis or an axis with an attribute of “cartesian_axis” set to “X”
Keys “y”,”Y”,”lat”,”LAT”,”latitude”,”LATITUDE” are all considered as “Y”
for latitude axis or an axis with an attribute of “cartesian_axis” set to “Y”

Examples

>>> # Extracts the region where -20. <= latitude <= 20.
>>> # and 100. <= longitude <= 200.
>>> var.getRegion(lat=(-20.,20.), lon=(100.,200.))
getSlice(**kwargs)[source]

Return a tuple of slice object corresponding a region specified. Example: variable.getSlice(lat=(-30.,30.))

getTime()[source]

Return a numpy array that contains the time axis

getattr(att, default=None)[source]

Return the value of an attribute of the variable

Java style getter

info(detailed=False, file_out=None)[source]

Print brief info about the variable

is_shape_matches_dims()[source]

Check if the shape of the data matches the dimensions

Raise ValueError if the dimensions do not match

lat_ave()[source]

Compute meridional average Same as wgt_ave(‘Y’)

runave(N, axis=0, step=None)[source]

Running mean along an axis. N specifies the size of the window

Parameters:N

size of the window if axis is int, N is treated as the number of

array elements along the axis
if axis is str, N is treated as the absolute value
of the size of window on the axis
Returns:geodat.nc.Variable

Examples

# Running average for every 5 elements on the first axis >>> var.runave(5, 0)

# Running average with a window of longitudinal-width of 40-degree >>> var.runave(40., “X”)

# Climatological running average with a window of 3 years # axis=0 for the time axis >>> var.runave(3, 0, step=12)

setRegion(**kwargs)[source]

Change the region of interest for the variable This function slices the data.

setRegion_value(value, **kwargs)[source]

Set values for a particular region Example: variable.setRegion_value(0.,lat=(-90.,-30.))

setattr(att, value)[source]

Set the value of an attribute of the variable

Java style setter

slicing(sliceobj)[source]

Perform the slicing operation on both the data and axes

Parameters:sliceobj (tuple) – slice object

Returns: None

squeeze()[source]

Remove singlet dimensions

time_ave()[source]

Compute the time average Same as wgt_ave(‘T’)

wgt_ave(axis=None)[source]

Compute averge on one or more axes Input: axis - either integer or a string (T/X/Y/Z/...) See getCAxes

zonal_ave()[source]

Compute the zonal average Same as wgt_ave(‘X’)

Fork me on GitHub