Axis class reference

Axis.rtol float

Relative tolerance for identifying two values for this axis as the same point. Set automatically in Axis::__init__ but if this is too large for a given application this can be the source of unexpected errors. Setting this parameter to a new value on the Axis objects in question may resolve these issues.

Axis.formatstr string

The format to apply to the axis values when displaying on the screen. See the Python documentation on String Formatting for the options available.

If the axis needs a more complicated format (that changes depending on the value), it may also define a formatvalue() method to explicitly convert each value to a string. For example, the Lat axis uses a formatvalue method to append an ‘N’ or ‘S’ to the latitudes, depending on the sign of the value.

Axis.auxatts dictionary

Auxiliary attributes. These are preserved during merge/slice/etc operations.

Axis.auxarrays dictionary

A dictionary of 1D numpy arrays with the same length as the axis that carry auxiliary data associated with each element of the axis. These data need not be unique; for instance, the values of time axes (e.g. StandardTime) are given as offsets from a reference date, while they have auxarrays for the year, month, day, etc. Many elements of the time axis might have same value in the corresponding ‘month’ auxiliary array. These can be used to select by keyword.

See also: auxasvar(), Var.__call__()

Axis.plotatts dictionary

Dictionary of attributes for plotting; see plotting documentation.

Note: Due to current limitations in PyGeode, modifications to these attributes may be lost if you do further work on the axis (e.g. slicing, concatenation, etc.). It will revert back to the default class values. For example:

>>> import pygeode as pyg
>>> x = pyg.Lat([10,20,30])
>>> print(x)
lat <Lat>      :  10 N to 30 N (3 values)
>>> x.formatstr = '%d deg N'
>>> print(x)
lat <Lat>      :  10 deg N to 30 deg N (3 values)
>>> #               ^^^ yay!
>>> print(x(lat=(10,20)))
lat <Lat>      :  10 N to 20 N (2 values)
>>> #               ^^^ wtf?!

At the moment there isn’t a great way to address this beyond changing the configuration file pygrc.

Axis.__init__(values, name=None, atts=None, plotatts=None, rtol=None, **kwargs)[source]

Create a new Axis object with the given values.

Parameters
valuesnumpy.ndarray

A one-dimensional coordinate defining the axis grid.

namestring (optional)

What to call the axis (i.e. for plot titles & when saving to file)

attsdict (optional)

Any additional metadata to associate with the axis. The dictionary keys should be strings.

plotattsdict (optional)

Parameters that control plotting behaviour; default values are available. The dictionary keys should be strings.

rtolfloat

A relative tolerance used for identifying an element of this axis.

Notes

All subclasses of Axis need to call this __init__ method within their own __init__, to properly initialize all attributes.

Axis.argsort(reverse=None)[source]

Generates a list of indices that would sort the Axis.

Parameters
reverseboolean (optional)

If False, indices are in ascending order. If True, will produce indices for a reverse sort instead. By default, sign of self.plotorder is used.

Returns
indiceslist

The indices which will produces a sorted version of the Axis.

See also

sorted

Examples

>>> from pygeode import Lat
>>> x = Lat([20,30,10])
>>> print(x)
lat <Lat>      :  20 N to 10 N (3 values)
>>> indices = x.argsort()
>>> print(indices)
[2 0 1]
>>> print(x.slice[indices])
lat <Lat>      :  10 N to 30 N (3 values)
Axis.auxasvar(name)[source]

Returns auxiliary array as a new Var object.

Parameters
namestring

Name of auxiliary array to return

Returns
varVar

Variable with values of requested auxilliary array

See also

auxarrays
Axis.formatter()

Returns a matplotlib formatter (pygeode.AxisFormatter) for use in plotting.

Axis.locator()

Returns a matplotlib locator object for use in plotting.

Axis.rename(name)[source]

Assigns a new name to this axis.

Parameters
namestring

The new name of this axis.

Returns
renamed_axisAxis

An instance of the same axis class with the new name.

Axis.sorted(reverse=None)[source]

Sorts the points of the Axis.

Parameters
reverseboolean (optional)

If True, sorts in descending order. If False, sorts in ascending order. By default the sign of self.plotorder is used.

Returns
sorted_axisAxis

A sorted version of the input axis.

See also

argsort

Examples

>>> from pygeode import Lat
>>> x = Lat([30,20,10])
>>> print(x)
lat <Lat>      :  30 N to 10 N (3 values)
>>> y = x.sorted()
>>> print(y)
lat <Lat>      :  10 N to 30 N (3 values)
Axis.str_as_val(key, s)[source]

str_as_val(self, key, s) - converts string s to a value corresponding to this axis. Default implementation returns float(s); derived classes can return different conversions depending on whether key corresponds to the axis itself or an auxiliary array.

Axis.__call__(**kwargs)[source]

Keyword-based data subsetting.

Parameters
ignore_mismatchboolean (optional)

If True, any keywords that don’t match an axis are ignored. Default is False

**kwargsone or more keyword parameters

The keys are the Axis names (or Axis class names), and the values are either a tuple of the desired (lower,upper) range, or a single Axis value. E.g., lat = (-45,45) or lat = 10.5

Returns
subset_varVar

A new Var, restricted to the specified domain.

Notes

There are a couple of special prefixes which can be prepended to each keyword to alter the subsetting behaviour. They can be used together.

  • i_ indicates that the values are indices into the axis, and not the axis values themselves. Indices start at 0. E.g. myvar(i_time = 0) selects the first time step of the variable, and myvar(i_lon=(10,20)) selects the 11th through 21st longitudes.

  • l_ indicates that you are providing an explicit list of coordinates, instead of a range. E.g. myvar(l_lon = (105.,106.,107.,108))

  • n_ returns the complement of the set you request; that is, everything except the specified selection. E.g. myvar(n_lat = (60, 90)) returns all latitudes except those between 60 and 90N.

  • m_ triggers an arithmetic mean over the specified range. E.g., myvar(m_lon = (10, 80)) is a shortcut for doing myvar(lon = (10,80)).mean('lon').

  • s_ triggers a call to squeeze on the specified axis, so that if only one value is selected the degenerate axis is removed. E.g., myvar(s_lon = 5) is a shortcut for doing myvar(lon = 5).squeeze() or myvar.squeeze(lon=5).

Examples

>>> from pygeode.tutorial import t1
>>> print(t1.vars)
[<Var 'Temp'>]
>>> T = t1.Temp
>>> print(T)
<Var 'Temp'>:
  Units: K  Shape:  (lat,lon)  (31,60)
  Axes:
    lat <Lat>      :  90 S to 90 N (31 values)
    lon <Lon>      :  0 E to 354 E (60 values)
  Attributes:
    {}
  Type:  Add_Var (dtype="float64")
>>> print(T(lat=30,lon=(100,200)))
<Var 'Temp'>:
  Units: K  Shape:  (lat,lon)  (1,17)
  Axes:
    lat <Lat>      :  30 N
    lon <Lon>      :  102 E to 198 E (17 values)
  Attributes:
    {}
  Type:  SlicedVar (dtype="float64")
Axis._getitem_asvar(slices)[source]

Slice-based data subsetting.

Parameters
sliceslist of slices
Returns
subset_varVar

A new Var, restricted to the specified domain.

Notes

A helper function so that standard python slicing notation can be used to subset a Var without loading the underlying data. A new Var object is returned.

Examples

>>> from pygeode.tutorial import t1
>>> print(t1.Temp)
<Var 'Temp'>:
  Units: K  Shape:  (lat,lon)  (31,60)
  Axes:
    lat <Lat>      :  90 S to 90 N (31 values)
    lon <Lon>      :  0 E to 354 E (60 values)
  Attributes:
    {}
  Type:  Add_Var (dtype="float64")
>>> print(t1.Temp.slice[10:-10, ::10])
<Var 'Temp'>:
  Units: K  Shape:  (lat,lon)  (11,6)
  Axes:
    lat <Lat>      :  30 S to 30 N (11 values)
    lon <Lon>      :  0 E to 300 E (6 values)
  Attributes:
    {}
  Type:  SlicedVar (dtype="float64")
>>> print(t1.Temp.slice[17, :])
<Var 'Temp'>:
  Units: K  Shape:  (lat,lon)  (1,60)
  Axes:
    lat <Lat>      :  12 N
    lon <Lon>      :  0 E to 354 E (60 values)
  Attributes:
    {}
  Type:  SlicedVar (dtype="float64")
classmethod Axis.class_has_alias(name)[source]
classmethod Axis.isparentof(other)[source]

Determines if an axis object is an instance of a base class (or the same class) of another axis.

Parameters
otherAxis object to compare against this one.
Returns
boolboolean

True if other is an instance of this object’s class

Axis.map_to(other)[source]

Returns indices of this axis which correspond to the axis other.

Parameters
otherAxis

Axis to find mapping to

Returns
mappinginteger array or None

Notes

Returns an ordered indices of the elements of this axis that correspond to those of the axis other, if one exists, otherwise None. This axis must be a parent class of other or vice versa in order for the mapping to exist. The mapping may include only a subset of this axis object, but must be as long as the other axis, if it is not None. The mapping identifies equivalent elements based on equality up to a tolerance specified by self.rtol.