/
retrieve_dataset.py
executable file
·436 lines (311 loc) · 16.9 KB
/
retrieve_dataset.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
#!/usr/bin/env python
# ===============================================================================
# Copyright 2015 Geoscience Australia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
__author__ = "Simon Oldfield"
import argparse
import gdal
import itertools
import logging
import os
import subprocess
from datacube.api.model import DatasetType, Satellite, dataset_type_database, dataset_type_derived_nbar, BANDS
from datacube.api.query import list_tiles
from datacube.api.utils import PqaMask, raster_create, intersection, calculate_ndvi, calculate_evi, calculate_nbr
from datacube.api.utils import get_dataset_data, get_dataset_data_with_pq, get_dataset_metadata, NDV
from datacube.api.workflow import writeable_dir
from datacube.config import Config
_log = logging.getLogger()
def satellite_arg(s):
if s in Satellite._member_names_:
return Satellite[s]
raise argparse.ArgumentTypeError("{0} is not a supported satellite".format(s))
def pqa_mask_arg(s):
if s in PqaMask._member_names_:
return PqaMask[s]
raise argparse.ArgumentTypeError("{0} is not a supported PQA mask".format(s))
def dataset_type_arg(s):
if s in DatasetType._member_names_:
return DatasetType[s]
raise argparse.ArgumentTypeError("{0} is not a supported dataset type".format(s))
class DatasetRetrievalWorkflow(object):
application_name = None
x = None
y = None
acq_min = None
acq_max = None
process_min = None
process_max = None
ingest_min = None
ingest_max = None
satellites = None
apply_pqa_filter = None
pqa_mask = None
dataset_types = None
output_directory = None
overwrite = None
list_only = None
stack_vrt = None
def __init__(self, application_name):
self.application_name = application_name
def parse_arguments(self):
parser = argparse.ArgumentParser(prog=__name__, description=self.application_name)
group = parser.add_mutually_exclusive_group()
group.add_argument("--quiet", help="Less output", action="store_const", dest="log_level", const=logging.WARN)
group.add_argument("--verbose", help="More output", action="store_const", dest="log_level", const=logging.DEBUG)
parser.set_defaults(log_level=logging.INFO)
parser.add_argument("--x", help="X grid reference", action="store", dest="x", type=int, choices=range(110, 155+1), required=True, metavar="[110 - 155]")
parser.add_argument("--y", help="Y grid reference", action="store", dest="y", type=int, choices=range(-45, -10+1), required=True, metavar="[-45 - -10]")
parser.add_argument("--acq-min", help="Acquisition Date", action="store", dest="acq_min", type=str, required=True)
parser.add_argument("--acq-max", help="Acquisition Date", action="store", dest="acq_max", type=str, required=True)
# parser.add_argument("--process-min", help="Process Date", action="store", dest="process_min", type=str)
# parser.add_argument("--process-max", help="Process Date", action="store", dest="process_max", type=str)
#
# parser.add_argument("--ingest-min", help="Ingest Date", action="store", dest="ingest_min", type=str)
# parser.add_argument("--ingest-max", help="Ingest Date", action="store", dest="ingest_max", type=str)
parser.add_argument("--satellite", help="The satellite(s) to include", action="store", dest="satellite",
type=satellite_arg, nargs="+", choices=Satellite, default=[Satellite.LS5, Satellite.LS7], metavar=" ".join([s.name for s in Satellite]))
parser.add_argument("--apply-pqa", help="Apply PQA mask", action="store_true", dest="apply_pqa", default=False)
parser.add_argument("--pqa-mask", help="The PQA mask to apply", action="store", dest="pqa_mask",
type=pqa_mask_arg, nargs="+", choices=PqaMask, default=[PqaMask.PQ_MASK_CLEAR], metavar=" ".join([s.name for s in PqaMask]))
supported_dataset_types = dataset_type_database + dataset_type_derived_nbar
parser.add_argument("--dataset-type", help="The types of dataset to retrieve", action="store",
dest="dataset_type",
type=dataset_type_arg,
nargs="+",
choices=supported_dataset_types, default=DatasetType.ARG25, metavar=" ".join([s.name for s in supported_dataset_types]))
parser.add_argument("--output-directory", help="Output directory", action="store", dest="output_directory",
type=writeable_dir, required=True)
parser.add_argument("--overwrite", help="Over write existing output file", action="store_true", dest="overwrite", default=False)
parser.add_argument("--list-only", help="List the datasets that would be retrieved rather than retrieving them", action="store_true", dest="list_only", default=False)
# parser.add_argument("--stack-vrt", help="Create a band stack VRT", action="store_true", dest="stack_vrt", default=False)
args = parser.parse_args()
_log.setLevel(args.log_level)
self.x = args.x
self.y = args.y
def parse_date_min(s):
from datetime import datetime
if s:
if len(s) == len("YYYY"):
return datetime.strptime(s, "%Y").date()
elif len(s) == len("YYYY-MM"):
return datetime.strptime(s, "%Y-%m").date()
elif len(s) == len("YYYY-MM-DD"):
return datetime.strptime(s, "%Y-%m-%d").date()
return None
def parse_date_max(s):
from datetime import datetime
import calendar
if s:
if len(s) == len("YYYY"):
d = datetime.strptime(s, "%Y").date()
d = d.replace(month=12, day=31)
return d
elif len(s) == len("YYYY-MM"):
d = datetime.strptime(s, "%Y-%m").date()
first, last = calendar.monthrange(d.year, d.month)
d = d.replace(day=last)
return d
elif len(s) == len("YYYY-MM-DD"):
d = datetime.strptime(s, "%Y-%m-%d").date()
return d
return None
self.acq_min = parse_date_min(args.acq_min)
self.acq_max = parse_date_max(args.acq_max)
# self.process_min = parse_date_min(args.process_min)
# self.process_max = parse_date_max(args.process_max)
#
# self.ingest_min = parse_date_min(args.ingest_min)
# self.ingest_max = parse_date_max(args.ingest_max)
self.satellites = args.satellite
self.apply_pqa_filter = args.apply_pqa
self.pqa_mask = args.pqa_mask
self.dataset_types = args.dataset_type
self.output_directory = args.output_directory
self.overwrite = args.overwrite
self.list_only = args.list_only
# self.stack_vrt = args.stack_vrt
_log.info("""
x = {x:03d}
y = {y:04d}
acq = {acq_min} to {acq_max}
process = {process_min} to {process_max}
ingest = {ingest_min} to {ingest_max}
satellites = {satellites}
apply PQA filter = {apply_pqa_filter}
PQA mask = {pqa_mask}
datasets to retrieve = {dataset_type}
output directory = {output}
over write existing = {overwrite}
list only = {list_only}
stack (VRT) = {stack_vrt}
""".format(x=self.x, y=self.y,
acq_min=self.acq_min, acq_max=self.acq_max,
process_min=self.process_min, process_max=self.process_max,
ingest_min=self.ingest_min, ingest_max=self.ingest_max,
satellites=self.satellites,
apply_pqa_filter=self.apply_pqa_filter, pqa_mask=self.pqa_mask,
dataset_type=[decode_dataset_type(t) for t in self.dataset_types],
output=self.output_directory,
overwrite=self.overwrite,
list_only=self.list_only,
stack_vrt=self.stack_vrt))
def run(self):
self.parse_arguments()
config = Config()
_log.debug(config.to_str())
# Clear stack files
# TODO - filename consistency and safety and so on
if self.stack_vrt:
for satellite, dataset_type in itertools.product(self.satellites, self.dataset_types):
path = os.path.join(self.output_directory, get_filename_file_list(satellite, dataset_type, self.x, self.y))
check_overwrite_remove_or_fail(path, self.overwrite)
# TODO once WOFS is in the cube
for tile in list_tiles(x=[self.x], y=[self.y], acq_min=self.acq_min, acq_max=self.acq_max,
satellites=[satellite for satellite in self.satellites],
dataset_types=intersection(self.dataset_types, dataset_type_database),
database=config.get_db_database(),
user=config.get_db_username(),
password=config.get_db_password(),
host=config.get_db_host(), port=config.get_db_port()):
if self.list_only:
_log.info("Would retrieve datasets [%s]", [tile.datasets[t].path for t in intersection(self.dataset_types, dataset_type_database)])
continue
pqa = None
# Apply PQA if specified
if self.apply_pqa_filter:
pqa = tile.datasets[DatasetType.PQ25]
for dataset_type in intersection(self.dataset_types, dataset_type_database):
retrieve_data(tile.datasets[dataset_type], pqa, self.pqa_mask, self.get_output_filename(tile.datasets[dataset_type]), tile.x, tile.y, self.overwrite, self.stack_vrt)
nbar = tile.datasets[DatasetType.ARG25]
self.generate_derived_nbar(intersection(self.dataset_types, dataset_type_derived_nbar), nbar, pqa, self.pqa_mask, self.overwrite)
# Generate VRT stack
if self.stack_vrt:
for satellite, dataset_type in itertools.product(self.satellites, self.dataset_types):
path = os.path.join(self.output_directory, get_filename_file_list(satellite, dataset_type, self.x, self.y))
if os.path.exists(path):
for band in BANDS[dataset_type, satellite]:
path_vrt = os.path.join(self.output_directory, get_filename_stack_vrt(satellite, dataset_type, self.x, self.y, band))
_log.info("Generating VRT file [%s] for band [%s]", path_vrt, band)
# gdalbuildrt -separate -b <band> -input_file_list <input file> <vrt file>
subprocess.call(["gdalbuildvrt", "-separate", "-b", str(band.value), "-input_file_list", path, path_vrt])
def generate_derived_nbar(self, dataset_types, nbar, pqa, pqa_masks, overwrite=False):
for dataset_type in dataset_types:
filename = self.get_output_filename_derived_nbar(nbar, dataset_type)
_log.info("Generating data from [%s] with pq [%s] and pq mask [%s] to [%s]", nbar.path, pqa and pqa.path or "", pqa and pqa_masks or "", filename)
metadata = get_dataset_metadata(nbar)
data = None
if pqa:
data = get_dataset_data_with_pq(nbar, pqa, pq_masks=pqa_masks)
else:
data = get_dataset_data(nbar)
_log.debug("data is [%s]", data)
if dataset_type == DatasetType.NDVI:
ndvi = calculate_ndvi(data[nbar.bands.RED], data[nbar.bands.NEAR_INFRARED])
raster_create(filename, [ndvi], metadata.transform, metadata.projection, NDV, gdal.GDT_Float32)
elif dataset_type == DatasetType.EVI:
evi = calculate_evi(data[nbar.bands.RED], data[nbar.bands.BLUE], data[nbar.bands.NEAR_INFRARED])
raster_create(filename, [evi], metadata.transform, metadata.projection, NDV, gdal.GDT_Float32)
elif dataset_type == DatasetType.NBR:
nbr = calculate_nbr(data[nbar.bands.NEAR_INFRARED], data[nbar.bands.SHORT_WAVE_INFRARED_2])
raster_create(filename, [nbr], metadata.transform, metadata.projection, NDV, gdal.GDT_Float32)
def get_output_filename(self, dataset):
filename = os.path.basename(dataset.path)
if filename.endswith(".vrt"):
filename = filename.replace(".vrt", ".tif")
if self.apply_pqa_filter:
dataset_type_string = {
DatasetType.ARG25: "_NBAR_",
DatasetType.PQ25: "_PQA_",
DatasetType.FC25: "_FC_"
}[dataset.dataset_type]
filename = filename.replace(dataset_type_string, dataset_type_string + "WITH_PQA_")
return os.path.join(self.output_directory, filename)
def get_output_filename_derived_nbar(self, nbar, dataset_type):
filename = os.path.basename(nbar.path)
if filename.endswith(".vrt"):
filename = filename.replace(".vrt", ".tif")
dataset_type_string = {
DatasetType.NDVI: "_NDVI_",
DatasetType.EVI: "_EVI_",
DatasetType.NBR: "_NBR_"
}[dataset_type]
if self.apply_pqa_filter:
dataset_type_string += "WITH_PQA_"
filename = filename.replace("_NBAR_", dataset_type_string)
return os.path.join(self.output_directory, filename)
def decode_dataset_type(dataset_type):
return {DatasetType.ARG25: "Surface Reflectance",
DatasetType.PQ25: "Pixel Quality",
DatasetType.FC25: "Fractional Cover",
DatasetType.WATER: "WOFS Woffle",
DatasetType.NDVI: "NDVI",
DatasetType.EVI: "EVI",
DatasetType.NBR: "Normalised Burn Ratio"}[dataset_type]
def retrieve_data(dataset, pq, pq_masks, path, x, y, overwrite=False, stack=False):
_log.info("Retrieving data from [%s] with pq [%s] and pq mask [%s] to [%s]", dataset.path, pq and pq.path or "", pq and pq_masks or "", path)
if os.path.exists(path) and not overwrite:
_log.error("Output file [%s] exists", path)
raise Exception("Output file [%s] already exists" % path)
data = None
metadata = get_dataset_metadata(dataset)
if pq:
data = get_dataset_data_with_pq(dataset, pq, pq_masks=pq_masks)
else:
data = get_dataset_data(dataset)
_log.debug("data is [%s]", data)
raster_create(path, [data[b] for b in dataset.bands],
metadata.transform, metadata.projection, NDV, gdal.GDT_Int16)
# If we are creating a stack then also add to a file list file...
if stack:
path_file_list = os.path.join(os.path.dirname(path), get_filename_file_list(dataset.satellite, dataset.dataset_type, x, y))
_log.info("Also going to write file list to [%s]", path_file_list)
with open(path_file_list, "ab") as f:
print >>f, path
def get_filename_file_list(satellite, dataset_type, x, y):
satellite_str = "LS"
if satellite == Satellite.LS8 and dataset_type == DatasetType.ARG25:
satellite_str += "8"
dataset_type_str = {
DatasetType.ARG25: "NBAR",
DatasetType.PQ25: "PQA",
DatasetType.FC25: "FC",
DatasetType.NDVI: "NDVI",
DatasetType.EVI: "EVI",
DatasetType.NBR: "NBR"
}[dataset_type]
return "{satellite}_{dataset}_{x:03d}_{y:04d}.files.txt".format(satellite=satellite_str, dataset=dataset_type, x=x, y=y)
def get_filename_stack_vrt(satellite, dataset_type, x, y, band):
satellite_str = "LS"
if satellite == Satellite.LS8 and dataset_type == DatasetType.ARG25:
satellite_str += "8"
dataset_type_str = {
DatasetType.ARG25: "NBAR",
DatasetType.PQ25: "PQA",
DatasetType.FC25: "FC",
DatasetType.NDVI: "NDVI",
DatasetType.EVI: "EVI",
DatasetType.NBR: "NBR"
}[dataset_type]
return "{satellite}_{dataset}_{x:03d}_{y:04d}_{band}.vrt".format(satellite=satellite_str, dataset=dataset_type, x=x, y=y, band=band.name)
def check_overwrite_remove_or_fail(path, overwrite):
if os.path.exists(path):
if overwrite:
os.remove(path)
else:
raise Exception("File [%s] exists" % path)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
DatasetRetrievalWorkflow("Dataset Retrieval").run()