How to Use the "sel" Parameter for Datasets with >3 Dimensions¶
Datasets with more than the three spatio-temporal dimensions (e.g. x, y, time) will require the use of the sel parameter to pick a particular value of those non-spatio-temporal dimensions. The sel parameter can be used more than once in a given request. The sel parameter can also be used to specify a time within a single granule's data file.
This notebook demonstrates how to get statistics for a single time slice of a granule from the TROPESS O3 dataset, which also includes a fourth dimension lev. This dataset has annual granules each with dimensions for time (monthly). Queries within a single year will return the same granule. If time={datetime} is present in a sel query parameter value, titiler-cmr will pass the datetime query parameter value to the xarray sel parameter for the time dimension.
See also: xarray.DataArray.sel.
Setup¶
import json
import os
from datetime import datetime, timezone
import httpx
titiler_endpoint = os.getenv(
"TITILER_CMR_ENDPOINT", "https://openveda.cloud/api/titiler-cmr"
)
geojson_dict = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[-20.79973248834736, 83.55979308678764],
[-20.79973248834736, 75.0115425216471],
[14.483337068956956, 75.0115425216471],
[14.483337068956956, 83.55979308678764],
[-20.79973248834736, 83.55979308678764],
]
],
"type": "Polygon",
},
}
],
}
r = httpx.post(
f"{titiler_endpoint}/xarray/statistics",
params=(
("collection_concept_id", "C2837626477-GES_DISC"),
# Temporal query for CMR granule query
("temporal", datetime(2021, 10, 10, tzinfo=timezone.utc).isoformat()),
# xarray backend query parameters
("variables", "o3"),
("sel", "time=nearest::{datetime}"), #
("sel", "lev=1000"),
),
json=geojson_dict,
timeout=None,
).json()
print(json.dumps(r, indent=2))
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-20.79973248834736,
83.55979308678764
],
[
-20.79973248834736,
75.0115425216471
],
[
14.483337068956956,
75.0115425216471
],
[
14.483337068956956,
83.55979308678764
],
[
-20.79973248834736,
83.55979308678764
]
]
]
},
"properties": {
"statistics": {
"b1": {
"min": 12.448402404785156,
"max": 30.638206481933594,
"mean": 25.00276756286621,
"count": 236.0,
"sum": 5900.6533203125,
"std": 3.4685499803764728,
"median": 25.134218215942383,
"majority": 12.448402404785156,
"minority": 12.448402404785156,
"unique": 236.0,
"histogram": [
[
2,
1,
0,
6,
33,
39,
37,
42,
32,
44
],
[
12.448402404785156,
14.267382621765137,
16.086362838745117,
17.905344009399414,
19.724323272705078,
21.543304443359375,
23.362285614013672,
25.18126678466797,
27.000246047973633,
28.81922721862793,
30.638206481933594
]
],
"valid_percent": 95.16,
"masked_pixels": 12.0,
"valid_pixels": 236.0,
"description": "0",
"percentile_2": 18.29606056213379,
"percentile_98": 30.242084503173828
}
},
"used_assets": [
"TRPSCRO3M3D.1:TROPESS_reanalysis_mon_o3_2021.nc"
]
}
}
]
}
You can chose a different time slice from the same granule simply by updating the datetime query parameter.
r = httpx.post(
f"{titiler_endpoint}/xarray/statistics",
params=(
("collection_concept_id", "C2837626477-GES_DISC"),
# Datetime for CMR granule query
("temporal", datetime(2021, 12, 10, tzinfo=timezone.utc).isoformat()),
# xarray backend query parameters
("variables", "o3"),
("sel", "time=nearest::{datetime}"), #
("sel", "lev=1000"),
),
json=geojson_dict,
timeout=None,
).json()
print(json.dumps(r, indent=2))
{
"detail": "CMR granule search timed out"
}