Xarray
Overview of the Xarray library, common use cases, and implementation details.
Xarray is a library designed for working with labeled multi-dimensional arrays. Built on top of NumPy and Pandas, Xarray adds labels in the form of dimensions, coordinates, and attributes, enhancing the usability of raw NumPy-like arrays. This enables a more intuitive, concise, and less error-prone development experience. The library also includes a large and expanding collection of functions for advanced analytics and visualization.
An overview of the Xarray library and its suitability for N-dimensional data (such as Tilebox time series datasets) is available in the official Why Xarray? documentation page.
The Tilebox Python client provides access to satellite data as an xarray.Dataset. This approach offers a great number of benefits over custom Tilebox-specific data structures:
Example dataset
To understand how Xarray functions, below is a quick a look at a sample dataset as it might be retrieved from a Tilebox datasets client.
This basic dataset illustrates common use cases for Xarray. To follow along, you can download the dataset as a NetCDF file. The Reading and writing files section explains how to save and load Xarray datasets from NetCDF files.
Here’s an overview of the output:
- The
satellite_data
dataset contains dimensions, coordinates, and variables. - The
time
dimension has 514 elements, indicating that there are 514 data points in the dataset. - The
time
dimension coordinate contains datetime values representing when the data was measured. The*
indicates a dimension coordinate, which enables label-based indexing and alignment. - The
ingestion_time
non-dimension coordinate holds datetime values for when the data was ingested into Tilebox. Non-dimension coordinates carry coordinate data but are not used for label-based indexing. They can even be multidimensional. - The dataset includes 28 variables.
- The
bands
variable contains integers indicating how many bands the data contains. - The
sun_elevation
variable contains floating-point values representing the sun’s elevation when the data was measured.
Explore the xarray terminology overview to broaden your understanding of datasets, dimensions, coordinates, and variables.
Accessing data in a dataset
By index
You can access data in different ways. The Xarray documentation offers a comprehensive overview of these methods.
To access the sun_elevation
variable:
In the output, the first sun elevation value is 44.19904463
. It appears as an xarray.DataArray
object to allow access to the corresponding coordinates. To retrieve the plain Python object, use the item()
method:
You can access coordinates similarly. For datetime fields, Xarray provides a special dt
(datetime) accessor for formatting time as a string:
You can also retrieve an entire dataset containing all variables and coordinates for a single data point using the isel
method (index selection):
Subsets of data
You can access subsets of the data as well. Here are methods to retrieve the first three and last three sun elevations.
Filtering data
Xarray allows convenient filtering of datasets based on conditions. For example, filter a dataset to only include sun elevation values where cloud cover is 0
:
You can combine conditions to filter for sun elevation values between 45
and 90
with cloud cover 0
:
Selecting data by value
You can use dimension coordinate values to index your dataset. For instance, access the data point recorded at 2022-05-01T11:28:28.249000
:
Selecting data by value requires unique coordinate values. In case of duplicates, you will encounter an InvalidIndexError
. To avoid this, you can drop duplicates.
Attempting to access a value not in the dataset raises a KeyError
.
To return the closest value instead of raising an error, specify a method
.
Dropping duplicates
Xarray allows you to drop duplicate values from a dataset. For example, to drop duplicate timestamps:
De-duped datasets are required for certain operations, like selecting data by value.
Statistics
Xarray and NumPy include a wide range of statistical functions that you can apply to a dataset or DataArray. Here are some examples:
You can also directly apply many NumPy functions to datasets or DataArrays. For example, to find out how many unique bands the data contains, use np.unique:
Reading and writing files
Xarray provides a simple method for saving and loading datasets from files. This is useful for sharing your data or storing it for future use. Xarray supports many different file formats, including NetCDF, Zarr, GRIB, and more. For a complete list of supported formats, refer to the official documentation page.
To save the example dataset as a NetCDF file:
You may need to install the netcdf4
package first.
This creates a file named example_satellite_data.nc
in your current directory. You can then load this file back into memory with:
If you would like to follow along with the examples in this section, you can download the example dataset as a NetCDF file here.
Further reading
This section covers only a few common use cases for Xarray. The library offers many more functions and features. For more information, please see the Xarray documentation or explore the Xarray Tutorials.
Some useful capabilities not covered in this section include:
Was this page helpful?