| 1 |
# CDSETool |
| 2 |
|
| 3 |
## About CDSETool |
| 4 |
This script downloads copernicus data from the Copernicus Data Space Ecosystem |
| 5 |
|
| 6 |
## Quick start |
| 7 |
|
| 8 |
```python |
| 9 |
from cdsetool.query import query_features, shape_to_wkt |
| 10 |
from cdsetool.credentials import Credentials |
| 11 |
from cdsetool.download import download_features |
| 12 |
from cdsetool.monitor import StatusMonitor |
| 13 |
from datetime import date |
| 14 |
|
| 15 |
features = query_features( |
| 16 |
"SENTINEL-1", |
| 17 |
{ |
| 18 |
"contentDateStartGe": "2020-12-20", |
| 19 |
"contentDateStartLe": date(2020, 12, 25), |
| 20 |
"processingLevel": "LEVEL1", |
| 21 |
"productType": "IW_GRDH_1S", |
| 22 |
"geometry": shape_to_wkt("path/to/shapefile.shp"), |
| 23 |
}, |
| 24 |
) |
| 25 |
|
| 26 |
list( |
| 27 |
download_features( |
| 28 |
features, |
| 29 |
"path/to/output/folder/", |
| 30 |
{ |
| 31 |
"concurrency": 4, |
| 32 |
"monitor": StatusMonitor(), |
| 33 |
"credentials": Credentials("username", "password"), |
| 34 |
}, |
| 35 |
) |
| 36 |
) |
| 37 |
``` |
| 38 |
|
| 39 |
Or use the CLI: |
| 40 |
|
| 41 |
```bash |
| 42 |
cdsetool query search SENTINEL-2 --search-term contentDateStartGe=2020-01-01 --search-term contentDateStartLe=2020-01-10 --search-term productType=S2MSI2A |
| 43 |
|
| 44 |
cdsetool download SENTINEL-2 PATH/TO/DIR --concurrency 4 --search-term contentDateStartGe=2020-01-01 --search-term contentDateStartLe=2020-01-10 --search-term productType=S2MSI2A |
| 45 |
``` |
| 46 |
|
| 47 |
## Table of Contents |
| 48 |
|
| 49 |
- [CDSETool](#cdsetool) |
| 50 |
* [About CDSETool](#about-cdsetool) |
| 51 |
* [Quick Start](#quick-start) |
| 52 |
* [Table of Contents](#table-of-contents) |
| 53 |
* [Installation](#installation) |
| 54 |
* [Usage](#usage) |
| 55 |
+ [Querying features](#querying-features) |
| 56 |
- [Querying by shapes](#querying-by-shapes) |
| 57 |
- [Querying by lists of parameters](#querying-by-lists-of-parameters) |
| 58 |
- [Querying by dates](#querying-by-dates) |
| 59 |
- [Listing search terms](#listing-search-terms) |
| 60 |
+ [Downloading features](#downloading-features) |
| 61 |
- [Authenticating](#authenticating) |
| 62 |
- [Concurrently downloading features](#concurrently-downloading-features) |
| 63 |
- [Sequentially downloading features](#sequentially-downloading-features) |
| 64 |
* [Roadmap](#roadmap) |
| 65 |
* [Contributing](#contributing) |
| 66 |
* [LICENSE](#license) |
| 67 |
|
| 68 |
## Installation |
| 69 |
|
| 70 |
Install `cdsetool` using pip: |
| 71 |
|
| 72 |
```bash |
| 73 |
pip install cdsetool==0.3.2 |
| 74 |
``` |
| 75 |
|
| 76 |
## Usage |
| 77 |
|
| 78 |
### Querying features |
| 79 |
|
| 80 |
Querying is always done in batches, returning `len(results) <= top` records each time. |
| 81 |
A local buffer is filled and gradually emptied as results are yielded. When the buffer is empty, |
| 82 |
more results will be requested and the process repeated until no more results are available, or |
| 83 |
the iterator is discarded. |
| 84 |
|
| 85 |
**Important**: The API has a pagination limit of 10,000 results per query. If your query returns more results, you'll need to narrow your search criteria (e.g., use smaller date ranges). |
| 86 |
|
| 87 |
Since downloading features is the most common use-case, `query_features` assumes that the query will run till the end. |
| 88 |
Because of this, the batch size is set to `1000`, which is the size limit set by CDSE. |
| 89 |
|
| 90 |
```python |
| 91 |
from cdsetool.query import query_features |
| 92 |
|
| 93 |
collection = "SENTINEL-2" |
| 94 |
search_terms = { |
| 95 |
"top": 100, # batch size, between 1 and 1000 (default: 1000) |
| 96 |
"contentDateStartGe": "2024-01-01", |
| 97 |
"productType": "S2MSI1C" |
| 98 |
} |
| 99 |
|
| 100 |
# wait for a single batch to finish, yield results immediately |
| 101 |
for feature in query_features(collection, search_terms): |
| 102 |
# do something with feature |
| 103 |
|
| 104 |
# wait for all batch requests to complete, returning list |
| 105 |
features = list(query_features(collection, search_terms)) |
| 106 |
|
| 107 |
# manually iterate |
| 108 |
iterator = query_features(collection, search_terms) |
| 109 |
|
| 110 |
featureA = next(iterator) |
| 111 |
featureB = next(iterator) |
| 112 |
# ... |
| 113 |
``` |
| 114 |
|
| 115 |
#### Query Options |
| 116 |
|
| 117 |
**Product Format** |
| 118 |
|
| 119 |
Query results are returned directly from the Copernicus API. Each product has the following structure: |
| 120 |
|
| 121 |
```python |
| 122 |
product = { |
| 123 |
"Id": "uuid-string", |
| 124 |
"Name": "S2A_MSIL2A_20240110T105421_...", |
| 125 |
"Collection": "SENTINEL-2", |
| 126 |
"ContentDate": {"Start": "2024-01-10T10:54:21Z", "End": "..."}, |
| 127 |
"Online": True, |
| 128 |
"ContentLength": 1043654649, |
| 129 |
"GeoFootprint": {"type": "Polygon", "coordinates": [...]}, |
| 130 |
"Attributes": [ |
| 131 |
{"Name": "productType", "Value": "S2MSI2A"}, |
| 132 |
{"Name": "cloudCover", "Value": 5.2}, |
| 133 |
..., |
| 134 |
], |
| 135 |
} |
| 136 |
``` |
| 137 |
|
| 138 |
**Expand Product Attributes** |
| 139 |
|
| 140 |
By default, query results do not include product attributes (productType, cloudCover, platform, instrument, etc.). To include product attributes, you need to request this from the server using the option `expand_attributes` and can then access the attributes using the function `get_product_attribute()`: |
| 141 |
|
| 142 |
```python |
| 143 |
from cdsetool.query import query_features, get_product_attribute |
| 144 |
|
| 145 |
features = query_features( |
| 146 |
"SENTINEL-2", |
| 147 |
{"contentDateStartGe": "2024-01-01"}, |
| 148 |
options={"expand_attributes": True}, |
| 149 |
) |
| 150 |
feature = features[0] |
| 151 |
|
| 152 |
# Access basic properties directly |
| 153 |
print(feature["Name"]) # Product name |
| 154 |
print(feature["Id"]) # Product UUID |
| 155 |
|
| 156 |
# Access attributes using helper function |
| 157 |
cloud_cover = get_product_attribute(feature, "cloudCover") |
| 158 |
product_type = get_product_attribute(feature, "productType") |
| 159 |
``` |
| 160 |
|
| 161 |
#### Querying by geometry |
| 162 |
|
| 163 |
To query by shapes, you must first convert your shape to Well Known Text (WKT). The included |
| 164 |
`shape_to_wkt` can solve this. |
| 165 |
|
| 166 |
```python |
| 167 |
from cdsetool.query import query_features, shape_to_wkt |
| 168 |
|
| 169 |
geometry = shape_to_wkt("path/to/shape.shp") |
| 170 |
features = query_features("SENTINEL-3", {"geometry": geometry}) |
| 171 |
``` |
| 172 |
|
| 173 |
#### Querying by lists of parameters |
| 174 |
|
| 175 |
Most search terms only accept a single argument. To query by a list of arguments, loop the arguments |
| 176 |
and pass them one by one to the query function. |
| 177 |
|
| 178 |
```python |
| 179 |
from cdsetool.query import query_features |
| 180 |
|
| 181 |
tile_ids = ["32TPT", "32UPU", "32UPU", "31RFL", "37XDA"] |
| 182 |
|
| 183 |
for tile_id in tile_ids: |
| 184 |
features = query_features("SENTINEL-2", {"tileId": tile_id}) |
| 185 |
for feature in features: |
| 186 |
# do things with feature |
| 187 |
``` |
| 188 |
|
| 189 |
#### Querying by dates |
| 190 |
|
| 191 |
Its quite common to query for features created before, after or between dates. |
| 192 |
|
| 193 |
Search terms support comparison operator suffixes: |
| 194 |
|
| 195 |
| Suffix | Meaning | Example | |
| 196 |
|--------|----------------------------|----------------------| |
| 197 |
| `Eq` | equals (=) | `contentDateStartEq` | |
| 198 |
| `Gt` | greater than (>) | `contentDateStartGt` | |
| 199 |
| `Ge` | greater than or equal (>=) | `contentDateStartGe` | |
| 200 |
| `Lt` | less than (<) | `contentDateStartLt` | |
| 201 |
| `Le` | less than or equal (<=) | `contentDateStartLe` | |
| 202 |
|
| 203 |
`Eq` can be applied on any field but the other suffixes can only be applied to numeric and date fields. |
| 204 |
|
| 205 |
Interval syntax is only allowed on the base name, not on suffixed variants, and only on numeric and date fields. |
| 206 |
|
| 207 |
| Interval notation | Suffixes to combine | Meaning | |
| 208 |
|-------------------|---------------------|--------------------------------| |
| 209 |
| [a, b] | `Ge` + `Le` | a <= value <= b (closed) | |
| 210 |
| (a, b) | `Gt` + `Lt` | a < value < b (open) | |
| 211 |
| [a, b) | `Ge` + `Lt` | a <= value < b (half-open) | |
| 212 |
| (a, b] | `Gt` + `Le` | a < value <= b (half-open) | |
| 213 |
|
| 214 |
```python |
| 215 |
from cdsetool.query import query_features |
| 216 |
from datetime import date, datetime |
| 217 |
|
| 218 |
date_from = date( |
| 219 |
2020, 1, 1 |
| 220 |
) # or datetime(2020, 1, 1, 23, 59, 59, 123456) or "2020-01-01" or "2020-01-01T23:59:59.123456Z" |
| 221 |
date_to = date(2020, 12, 31) |
| 222 |
|
| 223 |
features = query_features( |
| 224 |
"SENTINEL-2", |
| 225 |
{ |
| 226 |
"contentDateStartGe": date_from, |
| 227 |
"contentDateStartLe": date_to, |
| 228 |
"cloudCover": "[0, 30]", |
| 229 |
}, |
| 230 |
) |
| 231 |
``` |
| 232 |
|
| 233 |
#### Listing search terms |
| 234 |
|
| 235 |
To get a list of all search terms for a given collection, you may either use the `describe_collection` function or |
| 236 |
use the CLI: |
| 237 |
|
| 238 |
```python |
| 239 |
from cdsetool.query import describe_collection |
| 240 |
|
| 241 |
search_terms = describe_collection("SENTINEL-2").keys() |
| 242 |
print(search_terms) |
| 243 |
``` |
| 244 |
|
| 245 |
And the CLI: |
| 246 |
```bash |
| 247 |
$ cdsetool query search-terms SENTINEL-2 |
| 248 |
``` |
| 249 |
|
| 250 |
### Downloading features |
| 251 |
|
| 252 |
#### Authenticating |
| 253 |
|
| 254 |
An account is required to download features from the Copernicus distribution service. |
| 255 |
|
| 256 |
To authenticate using an account, instantiate `Credentials` and pass your username and password |
| 257 |
|
| 258 |
```python |
| 259 |
from cdsetool.credentials import Credentials |
| 260 |
|
| 261 |
username = "[email protected]" |
| 262 |
password = "password123" |
| 263 |
credentials = Credentials(username, password) |
| 264 |
``` |
| 265 |
|
| 266 |
Alternatively, `Credentials` can pull from `~/.netrc` when username and password are left blank. |
| 267 |
|
| 268 |
```python |
| 269 |
# ~/.netrc |
| 270 |
machine https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token |
| 271 |
login [email protected] |
| 272 |
password password123 |
| 273 |
|
| 274 |
# main.py |
| 275 |
from cdsetool.credentials import Credentials |
| 276 |
|
| 277 |
credentials = Credentials() |
| 278 |
``` |
| 279 |
|
| 280 |
The credentials object may then be passed to a download function. If left out, the download |
| 281 |
functions will default to using `.netrc`. |
| 282 |
|
| 283 |
```python |
| 284 |
credentials = Credentials() |
| 285 |
|
| 286 |
download_features(features, "/some/download/path", {"credentials": credentials}) |
| 287 |
``` |
| 288 |
|
| 289 |
Credentials can be validated using the `validate_credentials` function which will return a boolean. |
| 290 |
|
| 291 |
```python |
| 292 |
from cdsetool.credentials import validate_credentials |
| 293 |
|
| 294 |
validate_credentials(username="user", password="password") |
| 295 |
``` |
| 296 |
|
| 297 |
If None are passed to username and password, `validate_credentials` will validate `.netrc` |
| 298 |
|
| 299 |
#### Concurrently downloading features |
| 300 |
|
| 301 |
CDSETool provides a method for concurrently downloading features. The concurrency level |
| 302 |
should match your accounts privileges. |
| 303 |
See [CDSE quotas](https://documentation.dataspace.copernicus.eu/Quotas.html) |
| 304 |
|
| 305 |
The downloaded feature ids are yielded, so its required to await the results. |
| 306 |
|
| 307 |
```python |
| 308 |
from cdsetool.query import query_features |
| 309 |
from cdsetool.download import download_features |
| 310 |
|
| 311 |
features = query_features( |
| 312 |
"SENTINEL-2", |
| 313 |
{"contentDateStartGe": "2024-01-01", "contentDateStartLe": "2024-01-10"}, |
| 314 |
) |
| 315 |
|
| 316 |
download_path = "/path/to/download/folder" |
| 317 |
downloads = download_features(features, download_path, {"concurrency": 4}) |
| 318 |
|
| 319 |
for id in downloads: |
| 320 |
print(f"feature {id} downloaded") |
| 321 |
|
| 322 |
# or |
| 323 |
|
| 324 |
list(downloads) |
| 325 |
``` |
| 326 |
|
| 327 |
#### Sequentially downloading features |
| 328 |
|
| 329 |
Its possible to download features sequentially in a single thread if desired. |
| 330 |
|
| 331 |
```python |
| 332 |
from cdsetool.query import query_features |
| 333 |
from cdsetool.download import download_feature |
| 334 |
|
| 335 |
features = query_features( |
| 336 |
"SENTINEL-2", |
| 337 |
{"contentDateStartGe": "2024-01-01", "contentDateStartLe": "2024-01-10"}, |
| 338 |
) |
| 339 |
|
| 340 |
download_path = "/path/to/download/folder" |
| 341 |
for feature in features: |
| 342 |
download_feature(feature, download_path) |
| 343 |
``` |
| 344 |
|
| 345 |
#### Download specific files within features |
| 346 |
|
| 347 |
It's possible to download specific files within products bundles using Unix filename pattern matching. |
| 348 |
|
| 349 |
It can be used in CDSETool: |
| 350 |
|
| 351 |
- Through the `filter_pattern` option of `download_features` and `download_feature`: |
| 352 |
```python |
| 353 |
from cdsetool.query import query_features |
| 354 |
from cdsetool.download import download_features |
| 355 |
|
| 356 |
features = query_features( |
| 357 |
"SENTINEL-2", |
| 358 |
{"contentDateStartGe": "2024-01-01", "contentDateStartLe": "2024-01-10"}, |
| 359 |
) |
| 360 |
|
| 361 |
download_path = "/path/to/download/folder" |
| 362 |
filter_pattern = "*TCI.jp2" |
| 363 |
downloads = download_features( |
| 364 |
features, download_path, {"filter_pattern": filter_pattern} |
| 365 |
) |
| 366 |
|
| 367 |
for id in downloads: |
| 368 |
print(f"feature {id} downloaded") |
| 369 |
``` |
| 370 |
|
| 371 |
- Or through the CLI: |
| 372 |
``` |
| 373 |
cdsetool download SENTINEL-2 PATH/TO/DIR --filter-pattern *TCI.jp2 --concurrency 4 --search-term contentDateStartGe=2024-01-01 --search-term contentDateStartLe=2024-01-10 --search-term productType=S2MSI2A |
| 374 |
``` |
| 375 |
|
| 376 |
## Roadmap |
| 377 |
|
| 378 |
- [X] Query schema validation |
| 379 |
- [ ] High-level API |
| 380 |
- [ ] Query features |
| 381 |
- [ ] Download features |
| 382 |
- [ ] Download single feature |
| 383 |
- [ ] Download list of features |
| 384 |
- [ ] Download by ID |
| 385 |
- [ ] Download by URL |
| 386 |
- [ ] Command-Line Interface |
| 387 |
- [ ] Update to match the high-level API |
| 388 |
- [ ] Better `--help` messages |
| 389 |
- [ ] Quickstart guide in README.md |
| 390 |
- [ ] Test suite |
| 391 |
- [ ] Query |
| 392 |
- [ ] Credentials |
| 393 |
- [ ] Download |
| 394 |
- [ ] Monitor |
| 395 |
- [ ] Strategy for handling HTTP and connection errors |
| 396 |
|
| 397 |
## Contributing |
| 398 |
|
| 399 |
Any contributions you make are **greatly appreciated**. |
| 400 |
|
| 401 |
If you have a suggestion that would make this better, please fork the repo and create a pull request. |
| 402 |
You can also simply open an issue with the tag "enhancement". |
| 403 |
|
| 404 |
1. Fork the Project |
| 405 |
2. Create your Feature Branch (`git checkout -b feature/cool-new-feature`) |
| 406 |
3. Commit your Changes (`git commit -m 'Add some feature'`) |
| 407 |
4. Push to the Branch (`git push origin feature/cool-new-feature`) |
| 408 |
5. Open a Pull Request |
| 409 |
|
| 410 |
## LICENSE |
| 411 |
|
| 412 |
Distributed under the MIT License. See [LICENSE](LICENSE) for more information. |