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
from datetime import datetime, timezone
import httpx
titiler_endpoint = "https://staging.openveda.cloud/api/titiler-cmr" # staging endpoint
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}/statistics",
params=(
("concept_id", "C2837626477-GES_DISC"),
# Datetime for CMR granule query
("datetime", datetime(2021, 10, 10, tzinfo=timezone.utc).isoformat()),
# xarray backend query parameters
("backend", "xarray"),
("variable", "o3"),
("sel", "time={datetime}"), #
("sel", "lev=1000"),
("sel_method", "nearest"),
),
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": {
"2021-10-01T00:00:00.000000000": {
"min": 12.448402404785156,
"max": 30.805774688720703,
"mean": 25.221195220947266,
"count": 232.1199951171875,
"sum": 5854.34375,
"std": 3.496363112659205,
"median": 25.481826782226562,
"majority": 12.448402404785156,
"minority": 12.448402404785156,
"unique": 273.0,
"histogram": [
[
2,
1,
0,
7,
36,
38,
45,
46,
40,
58
],
[
12.448402404785156,
14.284139633178711,
16.119876861572266,
17.95561408996582,
19.791351318359375,
21.62708854675293,
23.462825775146484,
25.29856300354004,
27.134300231933594,
28.97003746032715,
30.805774688720703
]
],
"valid_percent": 94.79,
"masked_pixels": 15.0,
"valid_pixels": 273.0,
"percentile_2": 18.29606056213379,
"percentile_98": 30.397979736328125
}
}
}
}
]
}
You can chose a different time slice from the same granule simply by updating the datetime query parameter.
r = httpx.post(
f"{titiler_endpoint}/statistics",
params=(
("concept_id", "C2837626477-GES_DISC"),
# Datetime for CMR granule query
("datetime", datetime(2021, 12, 10, tzinfo=timezone.utc).isoformat()),
# xarray backend query parameters
("backend", "xarray"),
("variable", "o3"),
("sel", "time={datetime}"), #
("sel", "lev=1000"),
("sel_method", "nearest"),
),
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": {
"2021-12-01T00:00:00.000000000": {
"min": 18.230709075927734,
"max": 37.22050476074219,
"mean": 27.73312759399414,
"count": 188.1199951171875,
"sum": 5217.15576171875,
"std": 4.665600495935212,
"median": 28.000490188598633,
"majority": 18.230709075927734,
"minority": 18.230709075927734,
"unique": 225.0,
"histogram": [
[
1,
23,
40,
21,
17,
20,
44,
35,
15,
9
],
[
18.230709075927734,
20.129688262939453,
22.028667449951172,
23.92764663696289,
25.826627731323242,
27.72560691833496,
29.62458610534668,
31.52356719970703,
33.42254638671875,
35.32152557373047,
37.22050476074219
]
],
"valid_percent": 78.12,
"masked_pixels": 63.0,
"valid_pixels": 225.0,
"percentile_2": 20.764251708984375,
"percentile_98": 36.73855972290039
}
}
}
}
]
}