-
Notifications
You must be signed in to change notification settings - Fork 19
/
datacube.py
executable file
·520 lines (436 loc) · 16.2 KB
/
datacube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python3
"""
STAC Data Processing Script
This Python script processes Sentinel-2, Sentinel-1, and Copernicus DEM
(Digital Elevation Model) data. It utilizes Microsoft's Planetary Computer API
for data retrieval and manipulation.
Constants:
- STAC_API: Planetary Computer API endpoint
- S2_BANDS: Bands used in Sentinel-2 data processing
Functions:
- get_surrounding_days(reference, interval_days):
Get the week range for a given date.
- search_sentinel2(date_range, aoi, cloud_cover_percentage, nodata_pixel_percentage):
Search for Sentinel-2 items within a given date range and area of interest.
- search_sentinel1(bbox, catalog, date_range):
Search for Sentinel-1 items within a given bounding box, STAC catalog,
and date range.
- search_dem(bbox, catalog):
Search for DEM items within a given bounding box.
- make_datasets(s2_item, s1_items, dem_items, resolution):
Create xarray Datasets for Sentinel-2, Sentinel-1, and DEM data.
- process(aoi, year, resolution, cloud_cover_percentage, nodata_pixel_percentage):
Process Sentinel-2, Sentinel-1, and DEM data for a specified time range,
area of interest, and resolution.
"""
import logging
import os
import random
from datetime import timedelta
import click
import geopandas as gpd
import numpy as np
import planetary_computer as pc
import pystac_client
import stackstac
import xarray as xr
from pystac import ItemCollection
from shapely.geometry import box
from tile import tiler
STAC_API = "https://planetarycomputer.microsoft.com/api/stac/v1"
S2_BANDS = ["B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12", "SCL"]
SPATIAL_RESOLUTION = 10
CLOUD_COVER_PERCENTAGE = 50
NODATA_PIXEL_PERCENTAGE = 20
NODATA = 0
S1_MATCH_ATTEMPTS = 40
DATES_PER_LOCATION = 4
logger = logging.getLogger("datacube")
hdr = logging.StreamHandler()
formatter = logging.Formatter("[%(asctime)s] %(levelname)s: %(message)s")
hdr.setFormatter(formatter)
logger.addHandler(hdr)
logger.setLevel(logging.INFO)
def get_surrounding_days(reference, interval_days):
"""
Get the days surrounding the input date.
Parameters:
- reference (datetime): The reference datetime.
- interval_days (int): The number of days to search ahead and back
Returns:
- str: A string representing the start and end dates of the date interval in the
format 'start_date/end_date'.
"""
start = reference - timedelta(days=interval_days)
end = reference + timedelta(days=interval_days)
return f"{start.date()}/{end.date()}"
def search_sentinel2(
catalog, date_range, aoi, cloud_cover_percentage, nodata_pixel_percentage, index=0
):
"""
Search for Sentinel-2 items within a given date range and area of interest (AOI)
with specified conditions.
Parameters:
- catalog (pystac.Catalog): STAC catalog containing Sentinel-2 items.
- date_range (str): The date range in the format 'start_date/end_date'.
- aoi (shapely.geometry.base.BaseGeometry): Geometry object for an Area of
Interest (AOI).
- cloud_cover_percentage (int): Maximum acceptable cloud cover percentage
for Sentinel-2 images.
- nodata_pixel_percentage (int): Maximum acceptable percentage of nodata
pixels in Sentinel-2 images.
- index: Which of the found scenes to select
Returns:
- tuple: A tuple containing the STAC catalog, Sentinel-2 items, and the
bounding box (bbox)
Note:
The function filters Sentinel-2 items based on the specified conditions
such as geometry, date, cloud cover, and nodata pixel percentage. Only one
result with the least cloud cover will be returned. The result is returned
as a tuple containing the STAC catalog, Sentinel-2 items, the bounding box
of the first item, and an EPSG code for the coordinate reference system.
"""
search: pystac_client.item_search.ItemSearch = catalog.search(
filter_lang="cql2-json",
filter={
"op": "and",
"args": [
{
"op": "s_intersects",
"args": [{"property": "geometry"}, aoi.centroid.__geo_interface__],
},
{"op": "anyinteracts", "args": [{"property": "datetime"}, date_range]},
{"op": "=", "args": [{"property": "collection"}, "sentinel-2-l2a"]},
{
"op": "<=",
"args": [{"property": "eo:cloud_cover"}, cloud_cover_percentage],
},
{
"op": "<=",
"args": [
{"property": "s2:nodata_pixel_percentage"},
nodata_pixel_percentage,
],
},
],
},
)
s2_items = search.item_collection()
logger.info(f"Found {len(s2_items)} Sentinel-2 items")
if not len(s2_items):
return None, None
s2_items_gdf = gpd.GeoDataFrame.from_features(s2_items.to_dict())
least_clouds = s2_items_gdf.sort_values(by=["eo:cloud_cover"], ascending=True).iloc[
index
]
# Get the datetime for the filtered Sentinel 2 dataframe
# containing the least nodata and least cloudy scene
for item in s2_items:
if item.properties["datetime"] == least_clouds["datetime"]:
s2_item = item
break
bbox = least_clouds.geometry.bounds
epsg = s2_item.properties["proj:epsg"]
logger.info(f"EPSG code based on Sentinel-2 item: {epsg}")
return s2_item, bbox
def search_sentinel1(bbox, catalog, date_range):
"""
Search for Sentinel-1 items within a given bounding box (bbox), STAC
catalog, and date range.
Parameters:
- bbox (tuple): Bounding box coordinates in the format
(minx, miny, maxx, maxy).
- catalog (pystac.Catalog): STAC catalog containing Sentinel-1 items.
- date_range (str): The date range in the format 'start_date/end_date'.
Returns:
- pystac.Collection: A collection of Sentinel-1 items filtered by specified
conditions.
Note:
This function retrieves Sentinel-1 items from the catalog that intersect
with the given bounding box and fall within the provided time window. The
function filters items based on orbit state and returns the collection of
Sentinel-1 items that meet the defined criteria.
"""
# Create poly geom object from the bbox
geom_bbox = box(*bbox)
search: pystac_client.item_search.ItemSearch = catalog.search(
filter_lang="cql2-json",
filter={
"op": "and",
"args": [
{
"op": "s_intersects",
"args": [{"property": "geometry"}, geom_bbox.__geo_interface__],
},
{"op": "anyinteracts", "args": [{"property": "datetime"}, date_range]},
{"op": "=", "args": [{"property": "collection"}, "sentinel-1-rtc"]},
],
},
)
s1_items = search.item_collection()
logger.info(f"Found {len(s1_items)} Sentinel-1 items")
if not len(s1_items):
return
else:
# Add id as property to persist in gdf
for item in s1_items:
item.properties["id"] = item.id
# Try to find enough scenes with same orbit that fully overlap
# the S2 bbox.
s1_gdf = gpd.GeoDataFrame.from_features(s1_items)
s1_gdf["overlap"] = s1_gdf.intersection(box(*bbox)).area
s1_gdf = s1_gdf.sort_values(by="overlap", ascending=False)
most_overlap_orbit = s1_gdf.iloc[0]["sat:orbit_state"]
logger.info(f"Most overlapped orbit: {most_overlap_orbit}")
selected_item_ids = []
intersection = None
orbit = None
for index, row in s1_gdf.iterrows():
orbit = row["sat:orbit_state"]
if intersection is None and orbit == most_overlap_orbit:
intersection = row.geometry
selected_item_ids.append(row.id)
intersection = intersection.intersection(row.geometry)
elif orbit == most_overlap_orbit and not intersection.covers(geom_bbox):
intersection = row.geometry
selected_item_ids.append(row.id)
intersection = intersection.intersection(row.geometry)
elif orbit == most_overlap_orbit and intersection.covers(geom_bbox):
# Stop adding scenes when the bbox is fully covered.
break
else:
pass
s1_items = ItemCollection(
[item for item in s1_items if item.id in selected_item_ids]
)
return s1_items
def search_dem(bbox, catalog):
"""
Search for Copernicus Digital Elevation Model (DEM) items within a given
bounding box (bbox), STAC catalog, and Sentinel-2 items.
Parameters:
- bbox (tuple): Bounding box coordinates in the format
(minx, miny, maxx, maxy).
- catalog (pystac.Catalog): STAC catalog containing DEM items.
Returns:
- pystac.Collection: A collection of Digital Elevation Model (DEM) items
filtered by specified conditions.
"""
search = catalog.search(collections=["cop-dem-glo-30"], bbox=bbox)
dem_items = search.item_collection()
logger.info(f"Found {len(dem_items)} DEM items")
return dem_items
def make_datasets(s2_items, s1_items, dem_items, resolution):
"""
Create xarray Datasets for Sentinel-2, Sentinel-1, and Copernicus DEM
data.
Parameters:
- s2_items (list): List of Sentinel-2 items.
- s1_items (list): List of Sentinel-1 items.
- dem_items (list): List of DEM items.
- resolution (int): Spatial resolution.
Returns:
- tuple: A tuple containing xarray Datasets for Sentinel-2, Sentinel-1,
and Copernicus DEM.
"""
da_sen2: xr.DataArray = stackstac.stack(
items=s2_items,
assets=S2_BANDS,
resolution=resolution,
dtype=np.uint16,
fill_value=0,
)
da_sen1: xr.DataArray = stackstac.stack(
items=s1_items,
assets=["vh", "vv"],
epsg=int(da_sen2.epsg),
bounds=da_sen2.spec.bounds,
resolution=resolution,
dtype=np.float32,
fill_value=np.nan,
)
da_dem: xr.DataArray = stackstac.stack(
items=dem_items,
epsg=int(da_sen2.epsg),
bounds=da_sen2.spec.bounds,
resolution=resolution,
dtype=np.float32,
fill_value=np.nan,
)
da_sen1: xr.DataArray = stackstac.mosaic(da_sen1, dim="time")
da_sen1 = da_sen1.drop_vars(
[var for var in da_sen1.coords if var not in da_sen1.dims]
)
da_sen2 = da_sen2.drop_vars(
[var for var in da_sen2.coords if var not in da_sen2.dims]
).squeeze()
del da_sen2.coords["time"]
da_dem: xr.DataArray = stackstac.mosaic(da_dem, dim="time").assign_coords(
{"band": ["dem"]}
)
da_dem = da_dem.drop_vars([var for var in da_dem.coords if var not in da_dem.dims])
return [da_sen2, da_sen1, da_dem]
def process(
aoi,
date_range,
resolution,
cloud_cover_percentage,
nodata_pixel_percentage,
):
"""
Process Sentinel-2, Sentinel-1, and Copernicus DEM data for a specified
date_range, area of interest (AOI), resolution, EPSG code, cloud cover
percentage, and nodata pixel percentage.
Parameters:
- aoi (shapely.geometry.base.BaseGeometry): Geometry object for an Area of
Interest (AOI).
- date_range (str): Date range string to pass to the catalog search.
- resolution (int): Spatial resolution.
- cloud_cover_percentage (int): Maximum acceptable cloud cover percentage
for Sentinel-2 images.
- nodata_pixel_percentage (int): Maximum acceptable percentage of nodata
pixels in Sentinel-2 images.
Returns:
- xr.Dataset: Merged xarray Dataset containing processed data.
"""
catalog = pystac_client.Client.open(STAC_API, modifier=pc.sign_inplace)
for i in range(S1_MATCH_ATTEMPTS):
s2_item, bbox = search_sentinel2(
catalog,
date_range,
aoi,
cloud_cover_percentage,
nodata_pixel_percentage,
index=i,
)
if not s2_item:
continue
surrounding_days = get_surrounding_days(s2_item.datetime, interval_days=3)
logger.info(f"Searching S1 in date range {surrounding_days}")
s1_items = search_sentinel1(bbox, catalog, surrounding_days)
if s1_items:
break
if i == S1_MATCH_ATTEMPTS - 1:
logger.info(
"No match for S1 scenes found for date range "
f"{date_range} after {S1_MATCH_ATTEMPTS} attempts."
)
return None, None
dem_items = search_dem(bbox, catalog)
date = s2_item.properties["datetime"][:10]
result = make_datasets(
s2_item,
s1_items,
dem_items,
resolution,
)
if 0 in (dat.shape[0] for dat in result):
logger.info("S2/S1 pixel coverages do not overlap although bounds do")
return None, None
return date, result
def convert_attrs_and_coords_objects_to_str(data):
"""
Convert attributes and coordinates that are objects to
strings.
This is required for storing the xarray in netcdf.
"""
for key, coord in data.coords.items():
if coord.dtype == "object":
data.coords[key] = str(coord.values)
for key, attr in data.attrs.items():
data.attrs[key] = str(attr)
for key, var in data.variables.items():
var.attrs = {}
@click.command()
@click.option(
"--sample",
required=False,
default="https://clay-mgrs-samples.s3.amazonaws.com/mgrs_sample_v02.fgb",
help="Location of MGRS tile sample",
)
@click.option(
"--index",
required=False,
default=None,
help="Index of MGRS tile from sample file that should be processed",
)
@click.option(
"--bucket",
required=False,
default="",
help="Specify the bucket for where to write the data.",
)
@click.option(
"--subset",
required=False,
default="",
help="For debugging, subset the MGRS tile data to this pixel window."
"Expects a comma separated string of 4 integers.",
type=str,
)
@click.option(
"--localpath",
required=False,
default=None,
help="If specified, this path will be used to write the tiles locally"
"Otherwise a temp dir will be used.",
)
@click.option(
"--dateranges",
required=False,
default="",
type=str,
help="Comma separated list of date ranges, each provided as YYYY-MM-DD/YYYY-MM-DD.",
)
@click.option("-v", "--verbose", is_flag=True)
def main(sample, index, subset, bucket, localpath, dateranges, verbose): # noqa: PLR0913
if verbose:
logger.setLevel(logging.DEBUG)
if index is None:
index = int(os.environ.get("AWS_BATCH_JOB_ARRAY_INDEX", 0))
else:
index = int(index)
tiles = gpd.read_file(sample)
tile = tiles.iloc[index]
mgrs = tile["name"]
logger.info(f"Starting algorithm for MGRS tile {tile['name']} with index {index}")
if subset:
subset = [int(dat) for dat in subset.split(",")]
if dateranges:
date_ranges = dateranges.split(",")
else:
# Shuffle years, use index as seed for reproducibility but no
# to have the same shuffle every time.
date_ranges = [
f"{year}-01-01/{year}-12-31"
for year in (2017, 2018, 2019, 2020, 2021, 2022, 2023)
]
random.seed(index)
random.shuffle(date_ranges)
match_count = 0
for date_range in date_ranges:
logger.info(f"Processing data for date range {date_range}")
date, pixels = process(
tile.geometry,
date_range,
SPATIAL_RESOLUTION,
CLOUD_COVER_PERCENTAGE,
NODATA_PIXEL_PERCENTAGE,
)
if date is None:
continue
else:
match_count += 1
if subset:
logger.info(f"Subsetting to {subset}")
pixels = [
part[:, subset[1] : subset[3], subset[0] : subset[2]] for part in pixels
]
pixels = [part.compute() for part in pixels]
tiler(pixels, date, mgrs, bucket, localpath)
if match_count == DATES_PER_LOCATION:
break
if not match_count:
logger.info("No matching data found")
if __name__ == "__main__":
main()