stems.io package

Module contents

Input/output helpers

stems.io.open_dataset(paths, chunks='auto', concat_dim=None, crs=None, **kwds)[source]

Open an xarray dataset (somewhat) intelligently

Parameters
  • paths (str or List[str]) – Either a string glob in the form “path/to/my/files/*.nc” or an explicit list of files to open.

  • chunks ('auto', int or dict) – Number of chunks or the chunk size for each dimension. If ‘auto’, reads chunks from first file in paths and uses plants.io.chunking_.best_chunksizes() to guess an appropriate chunksize based on the most frequently used chunksize per dimension. Otherwise, pass chunks onto xr.open_mfdataset().

  • concat_dim (None, str, DataArray or Index, optional) – Dimension to concatenate files along. This argument is passed on to xarray.auto_combine() along with the dataset objects. You only need to provide this argument if the dimension along which you want to concatenate is not a dimension in the original datasets, e.g., if you want to stack a collection of 2D arrays along a third dimension. By default, xarray attempts to infer this argument by examining component files. Set concat_dim=None explicitly to disable concatenation.

  • crs (rasterio.crs.CRS, int, or str, optional) – Coordinate reference system to assign, if not already georeferenced. If type is not CRS, will be converted using plants.gis.convert.to_crs().

  • kwds (optional) – Options passed to xarray.open_mfdataset()

Returns

Multi-file dataset

Return type

xarray.Dataset

Raises

IOError – Raise if paths does not parse into any filenames (e.g., a bad glob)

stems.io.xarray_map(y, x, count, dtype, crs=None, nodata=-9999, dim_band='band')[source]

A raster map with count bands :param y: Y coordinates :type y: array-like :param x: X coordinates :type x: array-like :param count: Number of bands :type count: int :param dtype: Data type :type dtype: np.dtype :param crs: Optionally, give the raster a CRS :type crs: CRS, optional :param nodata: No Data Value to initialize data with :type nodata: float or int

Returns

3D (band, y, x,) “map” to write data into

Return type

xr.DataArray

class stems.io.VRTDataset(separate=True)[source]

Bases: object

Create a VRT from a band in one or more datasets

Parameters

separate (bool, optional) – Put input bands in separate, stacked bands in the output

add_band(self, path, src_bidx=1, vrt_bidx=None, validate=True, **band_kwds)[source]

Add a band to VRT dataset

Parameters
  • path (str) – List of paths to open as datasets

  • src_bidx (int, optional) – Band indices of datasets to include. Defaults to 1

  • vrt_bidx (int or None) – Destination band in VRT for new band. Only used if self.separate is True

  • validate (bool, optional) – Validate band forms to expected attributes before adding

  • band_kwds (dict) – Additional keyword arguments passed onto VRTSourceBand

Returns

vrt_bidx – VRT band index

Return type

int

Raises

ValueError – Raised if vrt_bidx is invalid

property bands

Bands organized by output VRT band

Type

dict[int, list[VRTSourceBand]]

property bounds

Bounding box of VRTDataset

Type

BoundingBox

close(self)[source]

Close any opened VRTSourceBand(s)

property count

Number of output bands in VRT

Type

int

property crs

VRTDataset coordinate reference system

Type

CRS

classmethod from_bands(paths, separate=True, bidx=1, **kwds)[source]
Parameters
  • paths (str or list[str]) – List of paths to open as datasets

  • separate (bool, optional) – Put input bands in separate, stacked bands in the output

  • bidx (int, list[int], optional) – Band indices of datasets to include. If int, all paths will use this band bidx. Otherwise, pass a list of band indices for each path in paths. Defaults to 1.

  • kwds (dict) – Keywords to pass to VRTSourceBand() for each band. Pass a list or tuple as a value to specify different values for each band.

Returns

VRTDataset initialized from input paths

Return type

VRTDataset

property height

Number of rows

Type

int

property shape

Number of rows and columns

Type

tuple[int, int]

property transform

Affine transform for VRTDataset

Type

Affine

property width

Number of columns

Type

int

write(self, path=None, relative=False)[source]

Save VRT XML data to a filename

Parameters
  • path (str, optional) – Save VRT to this filename. If None, returns the XML text

  • relative (bool, optional) – Reference VRT sources relative to the VRT

Returns

Filename

Return type

str

class stems.io.VRTSourceBand(path, src_bidx, description=None, nodata=None, keep_open=False)[source]

Bases: object

A VRT band originating from some other file

Note that all properties on this object return information used for VRT XML generation, but not XML elements (e.g., returns the SubElement name and this new element’s text value).

Parameters
  • path (str) – Filename of dataset containing band

  • src_bidx (int) – Source band index (begins on 1)

  • description (str, optional) – Override band description

  • nodata (float or int, optional) – Override NoDataValue

  • keep_open (bool, optional) – Keep dataset open

property blockxsize
property blockysize
property bounds
close(self)[source]

Close dataset reference, if open

property colorinterp
property crs
property description
property dtype
property height
property nodata
open(self)[source]
property shape
start(self)[source]

Open dataset, if closed

property transform
property width