-
Notifications
You must be signed in to change notification settings - Fork 51
/
ls_public_bucket.py
470 lines (381 loc) · 15 KB
/
ls_public_bucket.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
import logging
import re
import uuid
from multiprocessing import Manager, Process, cpu_count, current_process
from queue import Empty
import os
import boto3
import click
import datacube
from botocore import UNSIGNED
from botocore.config import Config
from datacube.index.hl import Doc2Dataset
from datacube.utils import changes
from osgeo import osr
from copy import deepcopy
from collections import Counter
import json
from odc.index import eo3_grid_spatial, odc_uuid
# Need to check if we're on new gdal for coordinate order
import osgeo.gdal
from packaging import version
from ruamel.yaml import YAML
GUARDIAN = "GUARDIAN_QUEUE_EMPTY"
AWS_PDS_TXT_SUFFIX = "MTL.txt"
LON_LAT_ORDER = version.parse(osgeo.gdal.__version__) < version.parse("3.0.0")
MTL_PAIRS_RE = re.compile(r'(\w+)\s=\s(.*)')
bands_ls8 = [('1', 'coastal_aerosol'),
('2', 'blue'),
('3', 'green'),
('4', 'red'),
('5', 'nir'),
('6', 'swir1'),
('7', 'swir2'),
('8', 'panchromatic'),
('9', 'cirrus'),
('10', 'lwir1'),
('11', 'lwir2'),
('QUALITY', 'quality')]
bands_ls7 = [('1', 'blue'),
('2', 'green'),
('3', 'red'),
('4', 'nir'),
('5', 'swir1'),
('7', 'swir2'),
('QUALITY', 'quality')]
bands_s2 = {
"B01": 'coastal_aerosol',
"B02": 'blue',
"B03": 'green',
"B04": 'red',
"B05": 'red_edge_1',
"B06": 'red_edge_2',
"B07": 'red_edge_3',
"B08": 'nir_1',
"B8A": 'nir_2',
"B09": 'water_vapour',
"B11": 'swir_1',
"B12": 'swir_2',
"SCL": 'scl'
}
# STACproduct lookup
def _stac_lookup(item):
# "sentinel:product_id": "S2A_MSIL2A_20191203T102401_N0213_R065_T30NYN_20191203T121856"
product_id = "unknown"
product_type = "unknown"
region_code = "nnTTT"
if "sentinel:product_id" in item.properties:
product_id = item.properties["sentinel:product_id"]
product_split = product_id.split("_")
if product_split[0] in ["S2A", "S2B"]:
# product = "Sentinel-2"
product_type = "{}_{}".format(product_split[0], product_split[1])
region_code = "{}{}{}".format(
str(item.properties["proj:epsg"])[-2:],
item.properties["sentinel:latitude_band"],
item.properties["sentinel:grid_square"]
)
else:
logging.error("Failed to recognise product.")
return product_id, product_type, region_code
def _parse_value(s):
s = s.strip('"')
for parser in [int, float]:
try:
return parser(s)
except ValueError:
pass
return s
def _parse_group(lines):
tree = {}
for line in lines:
match = MTL_PAIRS_RE.findall(line)
if match:
key, value = match[0]
if key == 'GROUP':
tree[value] = _parse_group(lines)
elif key == 'END_GROUP':
break
else:
tree[key] = _parse_value(value)
return tree
def get_geo_ref_points(info):
return {
'ul': {'x': info['CORNER_UL_PROJECTION_X_PRODUCT'], 'y': info['CORNER_UL_PROJECTION_Y_PRODUCT']},
'ur': {'x': info['CORNER_UR_PROJECTION_X_PRODUCT'], 'y': info['CORNER_UR_PROJECTION_Y_PRODUCT']},
'll': {'x': info['CORNER_LL_PROJECTION_X_PRODUCT'], 'y': info['CORNER_LL_PROJECTION_Y_PRODUCT']},
'lr': {'x': info['CORNER_LR_PROJECTION_X_PRODUCT'], 'y': info['CORNER_LR_PROJECTION_Y_PRODUCT']},
}
def get_stac_geo_ref_points(bounds):
return {
'ul': {'x': bounds.left, 'y': bounds.top},
'ur': {'x': bounds.right, 'y': bounds.top},
'll': {'x': bounds.left, 'y': bounds.bottom},
'lr': {'x': bounds.right, 'y': bounds.bottom},
}
def get_coords(geo_ref_points, spatial_ref):
t = osr.CoordinateTransformation(spatial_ref, spatial_ref.CloneGeogCS())
def transform(p):
# GDAL 3 reverses coordinate order, because... standards
if LON_LAT_ORDER:
# GDAL 2.0 order
lon, lat, z = t.TransformPoint(p['x'], p['y'])
else:
# GDAL 3.0 order
lat, lon, z = t.TransformPoint(p['x'], p['y'])
return {'lon': lon, 'lat': lat}
return {key: transform(p) for key, p in geo_ref_points.items()}
def geographic_to_projected(geometry, target_srs):
spatial_ref = osr.SpatialReference()
spatial_ref.ImportFromEPSG(4326)
target_ref = osr.SpatialReference()
target_ref.ImportFromEPSG(target_srs)
t = osr.CoordinateTransformation(spatial_ref, target_ref)
def transform(p):
# GDAL 3 reverses coordinate order, because... standards
if LON_LAT_ORDER:
# GDAL 2.0 order
x, y, z = t.TransformPoint(p[0], p[1])
else:
# GDAL 3.0 order
y, x, z = t.TransformPoint(p[1], p[0])
return [x, y]
new_geometry = deepcopy(geometry)
new_geometry['coordinates'][0] = [transform(p) for p in new_geometry['coordinates'][0]]
return new_geometry
def satellite_ref(sat):
"""
To load the band_names for referencing either LANDSAT8 or LANDSAT7 bands
"""
if sat == 'LANDSAT_8':
sat_img = bands_ls8
elif sat == 'LANDSAT_7' or sat == 'LANDSAT_5':
sat_img = bands_ls7
else:
raise ValueError('Satellite data Not Supported')
return sat_img
def absolutify_paths(doc, bucket_name, obj_key):
objt_key = format_obj_key(obj_key)
for band in doc['image']['bands'].values():
band['path'] = get_s3_url(bucket_name, objt_key + '/' + band['path'])
return doc
def relativise_path(href):
return os.path.split(href)[1]
def get_stac_bands(item, default_grid='g10m'):
bands = {}
grids = {}
assets = item.assets
for band in bands_s2:
asset = assets[band]
transform = asset['proj:transform']
grid = "g{}m".format(transform[0])
if grid not in grids:
grids[grid] = {
'shape': asset['proj:shape'],
'transform': asset['proj:transform']
}
band_info = {
'path': relativise_path(asset['href']),
}
if grid != default_grid:
band_info['grid'] = grid
bands[bands_s2[band]] = band_info
grids['default'] = grids[default_grid]
del grids[default_grid]
return bands, grids
def make_stac_metadata_doc(item):
# Dodgy lookup
product_id, product_type, region_code = _stac_lookup(item)
# Make a proper deterministic UUID
deterministic_uuid = str(odc_uuid("sentinel2_stac_process", "1.0.0", [product_id]))
# Get grids and bands
bands, grids = get_stac_bands(item)
doc = {
'$schema': 'https://schemas.opendatacube.org/dataset',
'id': deterministic_uuid,
'crs': "epsg:{}".format(item.properties['proj:epsg']),
'geometry': geographic_to_projected(item.geometry, item.properties['proj:epsg']),
'grids': grids,
'product': {
'name': product_type.lower() # This is not right
},
'label': product_id,
'properties': {
'datetime': item.properties['datetime'].replace("000+00:00", "Z"),
'odc:processing_datetime': item.properties['datetime'].replace("000+00:00", "Z"),
'eo:cloud_cover': item.properties['eo:cloud_cover'],
'eo:gsd': item.properties['gsd'],
'eo:instrument': item.properties['instruments'][0],
'eo:platform': item.properties['platform'],
'odc:file_format': 'GeoTIFF',
'odc:region_code': region_code
},
'measurements': bands,
'lineage': {}
}
# with open(f'/opt/odc/data/{item}.json', 'w') as outfile:
# json.dump(doc, outfile, indent=4)
return dict(**doc,
**eo3_grid_spatial(doc))
def make_metadata_doc(mtl_data, bucket_name, object_key):
mtl_product_info = mtl_data['PRODUCT_METADATA']
mtl_metadata_info = mtl_data['METADATA_FILE_INFO']
satellite = mtl_product_info['SPACECRAFT_ID']
instrument = mtl_product_info['SENSOR_ID']
acquisition_date = mtl_product_info['DATE_ACQUIRED']
scene_center_time = mtl_product_info['SCENE_CENTER_TIME']
level = mtl_product_info['DATA_TYPE']
product_type = 'L1TP'
sensing_time = acquisition_date + ' ' + scene_center_time
cs_code = 32600 + mtl_data['PROJECTION_PARAMETERS']['UTM_ZONE']
label = mtl_metadata_info['LANDSAT_SCENE_ID']
spatial_ref = osr.SpatialReference()
spatial_ref.ImportFromEPSG(cs_code)
geo_ref_points = get_geo_ref_points(mtl_product_info)
coordinates = get_coords(geo_ref_points, spatial_ref)
bands = satellite_ref(satellite)
doc = {
'id': str(uuid.uuid5(uuid.NAMESPACE_URL, get_s3_url(bucket_name, object_key))),
'processing_level': level,
'product_type': product_type,
'creation_dt': str(acquisition_date),
'label': label,
'platform': {'code': satellite},
'instrument': {'name': instrument},
'extent': {
'from_dt': sensing_time,
'to_dt': sensing_time,
'center_dt': sensing_time,
'coord': coordinates,
},
'format': {'name': 'GeoTiff'},
'grid_spatial': {
'projection': {
'geo_ref_points': geo_ref_points,
'spatial_reference': 'EPSG:%s' % cs_code,
}
},
'image': {
'bands': {
band[1]: {
'path': mtl_product_info['FILE_NAME_BAND_' + band[0]],
'layer': 1,
} for band in bands
}
},
'lineage': {'source_datasets': {}},
}
doc = absolutify_paths(doc, bucket_name, object_key)
return doc
def format_obj_key(obj_key):
obj_key = '/'.join(obj_key.split("/")[:-1])
return obj_key
def get_s3_url(bucket_name, obj_key):
return 's3://{bucket_name}/{obj_key}'.format(
bucket_name=bucket_name, obj_key=obj_key)
def archive_document(doc, uri, index, sources_policy):
def get_ids(dataset):
ds = index.datasets.get(dataset.id, include_sources=True)
for source in ds.sources.values():
yield source.id
yield dataset.id
resolver = Doc2Dataset(index)
dataset, _ = resolver(doc, uri)
index.datasets.archive(get_ids(dataset))
logging.info("Archiving %s and all sources of %s", dataset.id, dataset.id)
def add_dataset(doc, uri, index, sources_policy):
logging.info("Indexing %s", uri)
resolver = Doc2Dataset(index)
dataset, err = resolver(doc, uri)
existing_dataset = index.datasets.get(doc['id'])
if not existing_dataset:
logging.info("Indexing dataset...")
if err is not None:
logging.error("%s", err)
else:
try:
index.datasets.add(dataset, with_lineage=False)
except Exception as e:
logging.error("Unhandled exception %s", e)
else:
logging.info("Updating dataset...")
try:
index.datasets.update(dataset, {tuple(): changes.allow_any})
except Exception as e:
logging.error("Unhandled exception %s", e)
logging.info(f"Dataset {doc['id']} indexed.")
return dataset, err
def worker(config, bucket_name, prefix, suffix, start_date, end_date, func, unsafe, sources_policy, queue):
dc = datacube.Datacube(config=config)
index = dc.index
s3 = boto3.resource("s3", config=Config(signature_version=UNSIGNED))
safety = 'safe' if not unsafe else 'unsafe'
while True:
try:
key = queue.get(timeout=60)
if key == GUARDIAN:
break
logging.info("Processing %s %s", key, current_process())
obj = s3.Object(bucket_name, key).get()
raw = obj['Body'].read()
if suffix == AWS_PDS_TXT_SUFFIX:
# Attempt to process text document
raw_string = raw.decode('utf8')
txt_doc = _parse_group(iter(raw_string.split("\n")))['L1_METADATA_FILE']
data = make_metadata_doc(txt_doc, bucket_name, key)
else:
yaml = YAML(typ=safety, pure=False)
yaml.default_flow_style = False
data = yaml.load(raw)
uri = get_s3_url(bucket_name, key)
cdt = data['creation_dt']
# Use the fact lexicographical ordering matches the chronological ordering
if cdt >= start_date and cdt < end_date:
logging.info("calling %s", func)
func(data, uri, index, sources_policy)
queue.task_done()
except Empty:
break
except EOFError:
break
def iterate_datasets(bucket_name, config, prefix, suffix, start_date, end_date, func, unsafe, sources_policy):
manager = Manager()
queue = manager.Queue()
s3 = boto3.resource('s3', config=Config(signature_version=UNSIGNED))
bucket = s3.Bucket(bucket_name)
logging.info("Bucket : %s prefix: %s ", bucket_name, str(prefix))
# safety = 'safe' if not unsafe else 'unsafe'
worker_count = cpu_count() * 2
processess = []
for i in range(worker_count):
proc = Process(target=worker, args=(config, bucket_name, prefix, suffix, start_date, end_date, func, unsafe, sources_policy, queue,))
processess.append(proc)
proc.start()
for obj in bucket.objects.filter(Prefix=str(prefix)):
if (obj.key.endswith(suffix)):
queue.put(obj.key)
for i in range(worker_count):
queue.put(GUARDIAN)
for proc in processess:
proc.join()
@click.command(help="Enter Bucket name. Optional to enter configuration file to access a different database")
@click.argument('bucket_name')
@click.option(
'--config',
'-c',
help="Pass the configuration file to access the database",
type=click.Path(exists=True)
)
@click.option('--prefix', '-p', help="Pass the prefix of the object to the bucket")
@click.option('--suffix', '-s', default=".yaml", help="Defines the suffix of the metadata_docs that will be used to load datasets. For AWS PDS bucket use MTL.txt")
@click.option('--start_date', help="Pass the start acquisition date, in YYYY-MM-DD format")
@click.option('--end_date', help="Pass the end acquisition date, in YYYY-MM-DD format")
@click.option('--archive', is_flag=True, help="If true, datasets found in the specified bucket and prefix will be archived")
@click.option('--unsafe', is_flag=True, help="If true, YAML will be parsed unsafely. Only use on trusted datasets. Only valid if suffix is yaml")
@click.option('--sources_policy', default="verify", help="verify, ensure, skip")
def main(bucket_name, config, prefix, suffix, start_date, end_date, archive, unsafe, sources_policy):
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
action = archive_document if archive else add_dataset
iterate_datasets(bucket_name, config, prefix, suffix, start_date, end_date, action, unsafe, sources_policy)
if __name__ == "__main__":
main()