/
bare_soil.py
278 lines (185 loc) · 11.3 KB
/
bare_soil.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
#!/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 gdal
import logging
import luigi
import numpy
import os
from datacube.api.model import DatasetType, Fc25Bands, Ls57Arg25Bands, Satellite
from datacube.api.utils import NDV, empty_array, get_mask_pqa, get_dataset_data_masked, calculate_ndvi, get_mask_wofs
from datacube.api.utils import propagate_using_selected_pixel, get_dataset_metadata, raster_create, date_to_integer
from datacube.api.workflow.cell import Workflow, SummaryTask, CellTask
_log = logging.getLogger()
class BareSoilWorkflow(Workflow):
def __init__(self):
Workflow.__init__(self, name="Bare Soil Workflow")
def create_summary_tasks(self):
return [BareSoilSummaryTask(x_min=self.x_min, x_max=self.x_max, y_min=self.y_min, y_max=self.y_max,
acq_min=self.acq_min, acq_max=self.acq_max, satellites=self.satellites,
output_directory=self.output_directory, csv=self.csv, dummy=self.dummy,
mask_pqa_apply=self.mask_pqa_apply, mask_pqa_mask=self.mask_pqa_mask,
mask_wofs_apply=self.mask_wofs_apply, mask_wofs_mask=self.mask_wofs_mask)]
class BareSoilSummaryTask(SummaryTask):
def create_cell_tasks(self, x, y):
return BareSoilCellTask(x=x, y=y, acq_min=self.acq_min, acq_max=self.acq_max, satellites=self.satellites,
output_directory=self.output_directory, csv=self.csv, dummy=self.dummy,
mask_pqa_apply=self.mask_pqa_apply, mask_pqa_mask=self.mask_pqa_mask,
mask_wofs_apply=self.mask_wofs_apply, mask_wofs_mask=self.mask_wofs_mask)
class BareSoilCellTask(CellTask):
@staticmethod
def get_dataset_types():
return [DatasetType.ARG25, DatasetType.PQ25, DatasetType.FC25, DatasetType.NDVI]
def output(self):
def get_filenames():
return [self.get_dataset_filename(d) for d in ["FC", "NBAR", "SAT", "DATE"]]
return [luigi.LocalTarget(filename) for filename in get_filenames()]
def get_dataset_filename(self, dataset):
from datacube.api.workflow import format_date
from datacube.api.utils import get_satellite_string
satellites = get_satellite_string(self.satellites)
acq_min = format_date(self.acq_min)
acq_max = format_date(self.acq_max)
return os.path.join(self.output_directory,
"{satellites}_{dataset}_{x:03d}_{y:04d}_{acq_min}_{acq_max}.tif".format(
satellites=satellites,
dataset=dataset,
x=self.x, y=self.y,
acq_min=acq_min,
acq_max=acq_max))
def run(self):
shape = (4000, 4000)
no_data_value = NDV
best_pixel_fc = dict()
for band in Fc25Bands:
# best_pixel_fc[band] = empty_array(shape=shape, dtype=numpy.int16, ndv=INT16_MIN)
best_pixel_fc[band] = empty_array(shape=shape, dtype=numpy.int16, ndv=NDV)
best_pixel_nbar = dict()
for band in Ls57Arg25Bands:
best_pixel_nbar[band] = empty_array(shape=shape, dtype=numpy.int16, ndv=NDV)
best_pixel_satellite = empty_array(shape=shape, dtype=numpy.int16, ndv=NDV)
best_pixel_date = empty_array(shape=shape, dtype=numpy.int32, ndv=NDV)
current_satellite = empty_array(shape=shape, dtype=numpy.int16, ndv=NDV)
current_date = empty_array(shape=shape, dtype=numpy.int32, ndv=NDV)
SATELLITE_DATA_VALUES = {Satellite.LS5: 5, Satellite.LS7: 7, Satellite.LS8: 8}
metadata_nbar = None
metadata_fc = None
for tile in self.get_tiles():
pqa = tile.datasets[DatasetType.PQ25]
nbar = tile.datasets[DatasetType.ARG25]
fc = tile.datasets[DatasetType.FC25]
wofs = DatasetType.WATER in tile.datasets and tile.datasets[DatasetType.WATER] or None
_log.info("Processing [%s]", fc.path)
data = dict()
# Create an initial "no mask" mask
mask = numpy.ma.make_mask_none((4000, 4000))
# _log.info("### mask is [%s]", mask[1000][1000])
# Add the PQA mask if we are doing PQA masking
if self.mask_pqa_apply:
mask = get_mask_pqa(pqa, pqa_masks=self.mask_pqa_mask, mask=mask)
# _log.info("### mask PQA is [%s]", mask[1000][1000])
# Add the WOFS mask if we are doing WOFS masking
if self.mask_wofs_apply and wofs:
mask = get_mask_wofs(wofs, wofs_masks=self.mask_wofs_mask, mask=mask)
# _log.info("### mask PQA is [%s]", mask[1000][1000])
# Get NBAR dataset
data[DatasetType.ARG25] = get_dataset_data_masked(nbar, mask=mask)
# _log.info("### NBAR/RED is [%s]", data[DatasetType.ARG25][Ls57Arg25Bands.RED][1000][1000])
# Get the NDVI dataset
data[DatasetType.NDVI] = calculate_ndvi(data[DatasetType.ARG25][Ls57Arg25Bands.RED],
data[DatasetType.ARG25][Ls57Arg25Bands.NEAR_INFRARED])
# _log.info("### NDVI is [%s]", data[DatasetType.NDVI][1000][1000])
# Add the NDVI value range mask (to the existing mask)
mask = self.get_mask_range(data[DatasetType.NDVI], min_val=0.0, max_val=0.3, mask=mask)
# _log.info("### mask NDVI is [%s]", mask[1000][1000])
# Get FC25 dataset
data[DatasetType.FC25] = get_dataset_data_masked(fc, mask=mask)
# _log.info("### FC/BS is [%s]", data[DatasetType.FC25][Fc25Bands.BARE_SOIL][1000][1000])
# Add the bare soil value range mask (to the existing mask)
mask = self.get_mask_range(data[DatasetType.FC25][Fc25Bands.BARE_SOIL], min_val=0, max_val=8000, mask=mask)
# _log.info("### mask BS is [%s]", mask[1000][1000])
# Apply the final mask to the FC25 bare soil data
data_bare_soil = numpy.ma.MaskedArray(data=data[DatasetType.FC25][Fc25Bands.BARE_SOIL], mask=mask).filled(NDV)
# _log.info("### bare soil is [%s]", data_bare_soil[1000][1000])
# Compare the bare soil value from this dataset to the current "best" value
best_pixel_fc[Fc25Bands.BARE_SOIL] = numpy.fmax(best_pixel_fc[Fc25Bands.BARE_SOIL], data_bare_soil)
# _log.info("### best pixel bare soil is [%s]", best_pixel_fc[Fc25Bands.BARE_SOIL][1000][1000])
# Now update the other best pixel datasets/bands to grab the pixels we just selected
for band in Ls57Arg25Bands:
best_pixel_nbar[band] = propagate_using_selected_pixel(best_pixel_fc[Fc25Bands.BARE_SOIL],
data_bare_soil,
data[DatasetType.ARG25][band],
best_pixel_nbar[band])
for band in [Fc25Bands.PHOTOSYNTHETIC_VEGETATION, Fc25Bands.NON_PHOTOSYNTHETIC_VEGETATION, Fc25Bands.UNMIXING_ERROR]:
best_pixel_fc[band] = propagate_using_selected_pixel(best_pixel_fc[Fc25Bands.BARE_SOIL],
data_bare_soil,
data[DatasetType.FC25][band],
best_pixel_fc[band])
# And now the other "provenance" data
# Satellite "provenance" data
current_satellite.fill(SATELLITE_DATA_VALUES[fc.satellite])
best_pixel_satellite = propagate_using_selected_pixel(best_pixel_fc[Fc25Bands.BARE_SOIL],
data_bare_soil,
current_satellite,
best_pixel_satellite)
# Date "provenance" data
current_date.fill(date_to_integer(tile.end_datetime))
best_pixel_date = propagate_using_selected_pixel(best_pixel_fc[Fc25Bands.BARE_SOIL],
data_bare_soil,
current_date,
best_pixel_date)
# Grab the metadata from the input datasets for use later when creating the output datasets
if not metadata_nbar:
metadata_nbar = get_dataset_metadata(nbar)
if not metadata_fc:
metadata_fc = get_dataset_metadata(fc)
# Create the output datasets
# FC composite
raster_create(self.get_dataset_filename("FC"),
[best_pixel_fc[b] for b in Fc25Bands],
metadata_fc.transform, metadata_fc.projection,
metadata_fc.bands[Fc25Bands.BARE_SOIL].no_data_value,
metadata_fc.bands[Fc25Bands.BARE_SOIL].data_type)
# NBAR composite
raster_create(self.get_dataset_filename("NBAR"),
[best_pixel_nbar[b] for b in Ls57Arg25Bands],
metadata_nbar.transform, metadata_nbar.projection,
metadata_nbar.bands[Ls57Arg25Bands.BLUE].no_data_value,
metadata_nbar.bands[Ls57Arg25Bands.BLUE].data_type)
# Satellite "provenance" composites
raster_create(self.get_dataset_filename("SAT"),
[best_pixel_satellite],
metadata_nbar.transform, metadata_nbar.projection, no_data_value,
gdal.GDT_Int16)
# Date "provenance" composites
raster_create(self.get_dataset_filename("DATE"),
[best_pixel_date],
metadata_nbar.transform, metadata_nbar.projection, no_data_value,
gdal.GDT_Int32)
@staticmethod
def get_mask_range(input_data, min_val, max_val, mask=None, ndv=NDV):
# Create an empty mask if none provided - just to avoid an if below :)
if mask is None:
mask = numpy.ma.make_mask_none(numpy.shape(input_data))
# Mask out any no data values
data = numpy.ma.masked_equal(input_data, ndv)
# Mask out values outside the given range
mask = numpy.ma.mask_or(mask, numpy.ma.masked_outside(data, min_val, max_val, copy=False).mask)
return mask
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
BareSoilWorkflow().run()