GeoEco.Datasets.Virtual.DerivedGrid

class GeoEco.Datasets.Virtual.DerivedGrid(grids, func, displayName, dataType=None, noDataValue=None, queryableAttributes=None, queryableAttributeValues=None, allowUnmasking=False)

Bases: Grid

A Grid that that derives its values from other Grids with a function you provide.

Parameters:
  • grids (list of Grid) – List of one or more Grids from which values will be derived. Minimum length꞉ 1.

  • func (function) –

    Python function or lambda with the signature func(grids, slices) and the parameters:

    • grids - list of Grids provided to the constructor.

    • slices - tuple of slice or int that define the slices of data to be returned as a numpy array. These may be used to retrieve the values of each grid i like this: grids[i].Data.__getitem__(slices).

    func should return a numpy array of derived values for the range specified by slices. Usually, this involves retrieving the data from the grids for those slices and then performing a mathematical calculation. For example, if you have a grid representing sea surface temperature values in kelvin and you want to convert them to degrees Celsius, you could define this function:

    def k2C(grids, slices):
        return grids[0].Data.__getitem__(slices) - 273.15
    

    Then, you could construct your DerivedGrid like this:

    celsiusGrid = DerivedGrid(grids=[kelvinGrid],
                              func=k2C,
                              displayName=kelvinGrid.DisplayName + ' in C')
    

    Sometimes you may need to derive values from multiple grids. For example, if you have grids representing horizontal and vertical components of water velocity, commonly symbolized u and v, respectively, and want to compute the magnitude of velocity using the Pythagorean theorem:

    def mag(grids, slices):
        u = grids[0].Data.__getitem__(slices)
        v = grids[1].Data.__getitem__(slices)
        return (u**2 + v**2)**0.5
    

    You could construct a magnitude grid like this:

    magGrid = DerivedGrid(grids=[uGrid, vGrid],
                          func=mag,
                          displayName='current magnitude',
                          noDataValue=-9999.)
    

    Here, we decided to set the NoDataValue of magGrid to be -9999. We could have omitted that line, in which case the NoDataValue would have been set to uGrid.NoDataValue.

    Note that DerivedGrid automatically coerces the values returned by func to the DataType of the first Grid in the grids, and automatically sets any cells to NoData that are NoData in any of the grids. Use dataType and allowUnmasking to override these behaviors.

  • displayName (str) – Informal name of this object, suitable to be displayed to the user. Minimum length꞉ 1.

  • dataType (str, optional) – Numeric data type of the data returned by this grid. If not provided or None is provided, the DataType of the first Grid in grids will be used. Allowed values꞉ 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'float32', 'float64'. Case sensitive.

  • noDataValue (object, optional) – int, float, or single-element numpy array giving the value that indicates that cells of of this grid should be interpreted as having no data (these are also known as missing, NA, or NULL cells). If not provided or None is provided, the NoDataValue of the first Grid in grids will be used.

  • queryableAttributes (tuple of QueryableAttribute, optional) – Queryable attributes defined for this object.

  • queryableAttributeValues (dict mapping str to object, optional) – Values of the queryable attributes, expressed as a dictionary mapping the case-insensitive names of queryable attributes to their values.

  • allowUnmasking (bool, optional) – If False, the default, DerivedGrid will automatically set a cell to NoData if it is NoData in any of the provided grids. This is done as a convenience, so your func does not have to handle the grids having NoData values. If True, DerivedGrid will not automatically set any cell to NoData, and you must ensure that func does so appropriately.

Returns:

DerivedGrid instance.

Return type:

DerivedGrid

Properties

property CenterCoords

(object) Coordinates of the grid cell centers, indexed using the 1-character dimension of interest and optionally a range to retrieve a numpy.ndarray of coordinates (e.g. CenterCoords['x', 0:4]) or an integer to retrieve a float for a single coordinate (e.g. CenterCoords['x', 10]). Coordinates for the t dimension are returned as datetime instances. Read only.

property CoordDependencies

(tuple of str) Same length as Dimensions. Dimensions that each dimension depends on for determining its coordinates. None for dimensions that have a constant coordinate increment. Read only.

property CoordIncrements

(tuple of float) Same length as Dimensions. Coordinate increment for each dimension. None for dimensions that do not have a constant coordinate increment. Read only.

property Data

(object) This grid’s data, indexable using slices (e.g. grid.Data[:, 5:10, -10:]) or integers (e.g. grid.Data[0,1,-2]) or both in combination. Strides and negative indexes are supported in the traditional manner. If the grid is writable, Data can be assigned to write values to the grid, e.g. grid.Data[0,1] = 5 or grid.Data[:,:] = numpy.zeros(grid.Shape). Returns and accepts numpy.ndarray, float, and int. Read only.

property DataIsScaled

(bool) If True, the underlying raw data are stored as the UnscaledDataType to save storage space and then transformed by a scaling equation on the fly when they are returned by Data. The raw data can be accessed with UnscaledData. If False, the raw data are returned as is, with no transformation needed, and UnscaledDataType and DataType are the same, and UnscaledData returns the same values as Data. Read only.

property DataType

(str) Numeric data type of the grid, after the scaling function (if any) has been applied to the raw data. numpy.ndarrays returned by Data have this type. Read only. Allowed values꞉ 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'float32', 'float64'. Case sensitive.

property Dimensions

(str) Dimensions of this grid. Read only. Allowed values꞉ 'yx', 'zyx', 'tyx', 'tzyx'. Case sensitive.

property DisplayName

(str) Informal name of this object, suitable to be displayed to the user. Read only. Minimum length꞉ 1.

property MaxCoords

(object) Maximum coordinate value for each cell (i.e., the coordinates of the cells’ right edges), indexed using the 1-character dimension of interest and optionally a range to retrieve a numpy.ndarray of coordinates (e.g. MaxCoords['x', 0:4]) or an integer to retrieve a float for a single coordinate (e.g. MaxCoords['x', 10]). Coordinates for the t dimension are returned as datetime instances. Read only.

property MinCoords

(object) Minimum coordinate value for each cell (i.e., the coordinates of the cells’ left edges), indexed using the 1-character dimension of interest and optionally a range to retrieve a numpy.ndarray of coordinates (e.g. MinCoords['x', 0:4]) or an integer to retrieve a float for a single coordinate (e.g. MinCoords['x', 10]). Coordinates for the t dimension are returned as datetime instances. Read only.

property NoDataValue

(object or None) int, float, or single-element numpy array giving the value that indicates that cells of Data should be interpreted as having no data (these are also known as missing, NA, or NULL cells), or None if all cells must have data. Read only.

property ParentCollection

(DatasetCollection or None) Parent DatasetCollection that this object is part of (if any). Read only.

property Shape

(tuple of int) Same length as Dimensions. Length (number of grid cells) of each dimension. Read only.

property TCountPerSemiRegularPeriod

(int or None) Number of time slices per semi-regular period (i.e. per year). None if the grid’s dimensions do not contain a t coordinate or the t coordinate is not semi-regular. Read only.

property TIncrementUnit

(str or None) Unit of the t coordinate. None if the grid’s dimensions do not contain a t coordinate. Read only. Allowed values꞉ 'year', 'month', 'day', 'hour', 'minute', 'second'. Case sensitive.

property TSemiRegularity

(str or None) Type of semi-regularity used for the t coordinate. None if the grid’s dimensions do not contain a t coordinate or the t coordinate is not semi-regular. Read only. Allowed values꞉ 'annual'. Case sensitive.

property UnscaledData

(object) This grid’s data underlying raw data, before it has been transformed by a scaling equation. UnscaledData is indexable using slices (e.g. grid.UnscaledData[:, 5:10, -10:]) or integers (e.g. grid.UnscaledData[0,1,-2]) or both in combination. Strides and negative indexes are supported in the traditional manner. If the grid is writable, UnscaledData can be assigned to write values to the grid, e.g. grid.UnscaledData[0,1] = 5 or grid.UnscaledData[:,:] = numpy.zeros(grid.Shape). Returns and accepts numpy.ndarray, float, and int. Read only.

property UnscaledDataType

(str) Numeric data type of the grid’s raw data, before it has been transformed by a scaling equation. numpy.ndarrays returned by UnscaledData have this type. If no transformation is needed (DataIsScaled is False), then UnscaledDataType and ScaledDataType are the same, and UnscaledData returns the same values as Data. Read only. Allowed values꞉ 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'float32', 'float64'. Case sensitive.

property UnscaledNoDataValue

(object or None) int or float value that indicates that cells of UnscaledData should be interpreted as having no data (these are also known as missing, NA, or NULL cells), or None if all cells must have data. Read only.

Methods

Close

Closes any open files or connections associated with this object and releases any other resources allocated to access it.

ConvertSpatialReference

Converts a spatial reference from one format to another, such as an OGC WKT string to a Proj4 string.

DeleteLazyPropertyValue

Deletes the lazy property with the specified name.

GetAllQueryableAttributes

Returns a list of all queryable attributes.

GetIndicesForCoords

Given a tuple or list of coordinates, returns a list of int indices into Data for the cell that contains the coordinates.

GetLazyPropertyValue

Returns the value of the lazy property with the specified name.

GetQueryableAttribute

Returns the queryable attribute with the specified name.

GetQueryableAttributeValue

Returns the value of the queryable attribute with the specified name.

GetQueryableAttributesWithDataType

Returns a list queryable attributes having the specified data type.

GetSpatialReference

Returns the spatial reference of this dataset.

HasLazyPropertyValue

Returns True if the specified lazy property has a value.

SetLazyPropertyValue

Sets the lazy property with the specified name to the specified value.

SetSpatialReference

Sets the spatial reference of this dataset.

TestCapability

Tests whether a capability is supported by this class or an instance of it.

numpy_equal_nan