-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Burn Scar example for Sentinel-2 NRT (#89)
* Add a dNBR example * Finish thumbnail work and some broader fixes. * Add GM indexing * Local dev environment simple dNBR * Implements 3-band nDBR transform with example * Resolve linting errors, update burnscar example configuration * Remove empty section Co-authored-by: Alex Leith <[email protected]>
- Loading branch information
1 parent
43f3622
commit a695f82
Showing
8 changed files
with
275 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
from typing import Dict | ||
|
||
import numpy | ||
from datacube import Datacube | ||
from datacube.virtual import Measurement, Transformation | ||
from xarray import Dataset, merge | ||
|
||
|
||
class DeltaNBR(Transformation): | ||
"""Return NBR""" | ||
|
||
def __init__(self): | ||
self.output_measurements = { | ||
"dnbr": {"name": "dnbr", "dtype": "float", "nodata": -9999, "units": ""} | ||
} | ||
|
||
def measurements(self, input_measurements) -> Dict[str, Measurement]: | ||
return self.output_measurements | ||
|
||
def compute(self, data) -> Dataset: | ||
base_year = data.time.dt.year.values[0] - 1 | ||
if base_year == 2021: | ||
base_year = 2020 | ||
if base_year == 2012: | ||
base_year = 2013 | ||
|
||
dc = Datacube() | ||
gm_data = dc.load( | ||
product="ls8_nbart_geomedian_annual", | ||
time=str(base_year), | ||
like=data.geobox, | ||
measurements=["nir", "swir2"], | ||
) | ||
|
||
pre = (gm_data.nir - gm_data.swir2) / (gm_data.nir + gm_data.swir2) | ||
data = merge([data, {"pre": pre.isel(time=0, drop=True)}]) | ||
|
||
data["post"] = (data.nir - data.swir2) / (data.nir + data.swir2) | ||
data["dnbr"] = data.pre - data.post | ||
|
||
data["dnbr"] = data.dnbr.where(data.nir != -999).astype(numpy.single) | ||
|
||
data = data.drop(["nir", "swir2", "pre", "post"]) | ||
|
||
return data | ||
|
||
|
||
class DeltaNBR_3band(Transformation): | ||
"""Return 3-Band NBR""" | ||
|
||
def __init__(self): | ||
self.output_measurements = { | ||
"delta_nbr": { | ||
"name": "dnbr", | ||
"dtype": "float", | ||
"nodata": -9999, | ||
"units": "", | ||
}, | ||
"delta_bsi": { | ||
"name": "bsi", | ||
"dtype": "float", | ||
"nodata": -9999, | ||
"units": "", | ||
}, | ||
"delta_ndvi": { | ||
"name": "ndvi", | ||
"dtype": "float", | ||
"nodata": -9999, | ||
"units": "", | ||
}, | ||
} | ||
|
||
def measurements(self, input_measurements) -> Dict[str, Measurement]: | ||
return self.output_measurements | ||
|
||
def compute(self, data) -> Dataset: | ||
|
||
""" | ||
Implementation ported from https://github.com/daleroberts/nrt-predict/blob/main/nrtmodels/burnscar.py#L39 | ||
""" | ||
|
||
gm_base_year = data.time.dt.year.values[0] - 1 | ||
if gm_base_year == 2021: | ||
gm_base_year = 2020 | ||
if gm_base_year == 2012: | ||
gm_base_year = 2013 | ||
|
||
dc = Datacube() | ||
gm_data = dc.load( | ||
product="ls8_nbart_geomedian_annual", | ||
time=str(gm_base_year), | ||
like=data.geobox, | ||
measurements=["blue", "red", "nir", "swir2"], # B02, B04, B08, B11 | ||
) | ||
|
||
# Delta Normalised Burn Ratio (dNBR) = (B08 - B11)/(B08 + B11) | ||
pre_nbr = (gm_data.nir - gm_data.swir2) / (gm_data.nir + gm_data.swir2) | ||
data = merge([data, {"pre_nbr": pre_nbr.isel(time=0, drop=True)}]) | ||
data["post_nbr"] = (data.nir - data.swir2) / (data.nir + data.swir2) | ||
data["delta_nbr"] = data.pre_nbr - data.post_nbr | ||
data["delta_nbr"] = data.delta_nbr.where(data.nir != -999).astype(numpy.single) | ||
|
||
# Burn Scar Index (BSI) = ((B11 + B04) - (B08 - B02)) / ((B11 + B04) + (B08 - B02)) | ||
pre_bsi = ((gm_data.swir2 + gm_data.red) - (gm_data.nir - gm_data.blue)) / ( | ||
(gm_data.swir2 + gm_data.red) + (gm_data.nir - gm_data.blue) | ||
) | ||
data = merge([data, {"pre_bsi": pre_bsi.isel(time=0, drop=True)}]) | ||
data["post_bsi"] = ((data.swir2 + data.red) - (data.nir - data.blue)) / ( | ||
(data.swir2 + data.red) + (data.nir - data.blue) | ||
) | ||
data["delta_bsi"] = data.pre_bsi - data.post_bsi | ||
data["delta_bsi"] = data.delta_bsi.where(data.nir != -999).astype(numpy.single) | ||
|
||
# Normalized Difference Vegetation Index (NDVI) = (B08 - B04)/(B08 + B04) | ||
pre_ndvi = (gm_data.nir - gm_data.red) / (gm_data.nir + gm_data.red) | ||
data = merge([data, {"pre_ndvi": pre_ndvi.isel(time=0, drop=True)}]) | ||
data["post_ndvi"] = (data.nir - data.red) / (data.nir + data.red) | ||
data["delta_ndvi"] = data.post_ndvi - data.pre_ndvi | ||
data["delta_ndvi"] = data.delta_ndvi.where(data.nir != -999).astype( | ||
numpy.single | ||
) | ||
|
||
data = data.drop( | ||
[ | ||
"nir", | ||
"swir2", | ||
"red", | ||
"blue", | ||
"post_nbr", | ||
"pre_nbr", | ||
"pre_bsi", | ||
"post_bsi", | ||
"pre_ndvi", | ||
"post_ndvi", | ||
] | ||
) | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
specification: | ||
products: | ||
- s2a_nrt_granule | ||
- s2b_nrt_granule | ||
measurements: ['nbart_nir_1', 'nbart_red_edge_1'] | ||
measurement_renames: | ||
nbart_nir_1: nir | ||
nbart_red_edge_1: swir2 | ||
|
||
aws_unsigned: True | ||
transform: datacube_alchemist.transforms.DeltaNBR | ||
transform_args: | ||
|
||
override_product_family: ard | ||
basis: nbart_nir_1 | ||
|
||
output: | ||
location: /tmp/alchemist | ||
preview_image: | ||
red: dnbr | ||
green: dnbr | ||
blue: dnbr | ||
static_stretch: [-1, 1] | ||
explorer_url: https://explorer.dev.dea.ga.gov.au | ||
write_data_settings: | ||
overview_resampling: average | ||
reference_source_dataset: True | ||
write_stac: True | ||
inherit_geometry: True | ||
|
||
metadata: | ||
product_family: dnbr | ||
producer: ga.gov.au | ||
dataset_version: 0.0.2 | ||
collection_number: 3 | ||
naming_conventions: dea_c3 | ||
properties: | ||
dea:dataset_maturity: interim | ||
|
||
processing: | ||
dask_chunks: | ||
x: -1 | ||
y: 4096 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
specification: | ||
products: | ||
- s2a_nrt_granule | ||
- s2b_nrt_granule | ||
measurements: ['nbart_nir_1', 'nbart_red_edge_1', 'nbart_blue', 'nbart_red'] | ||
measurement_renames: | ||
nbart_nir_1: nir | ||
nbart_red_edge_1: swir2 | ||
nbart_blue: blue | ||
nbart_red: red | ||
|
||
aws_unsigned: True | ||
transform: datacube_alchemist.transforms.DeltaNBR_3band | ||
|
||
override_product_family: ard | ||
basis: nbart_nir_1 | ||
|
||
output: | ||
location: /tmp/alchemist | ||
preview_image: | ||
red: delta_nbr | ||
green: delta_ndvi | ||
blue: delta_bsi | ||
static_stretch: [-1, 1] | ||
explorer_url: https://explorer.dev.dea.ga.gov.au | ||
write_data_settings: | ||
overview_resampling: average | ||
reference_source_dataset: True | ||
write_stac: True | ||
inherit_geometry: True | ||
|
||
metadata: | ||
product_family: bs | ||
producer: ga.gov.au | ||
dataset_version: 1.6.0 | ||
collection_number: 3 | ||
naming_conventions: dea_c3 | ||
properties: | ||
dea:dataset_maturity: interim | ||
|
||
processing: | ||
dask_chunks: | ||
x: -1 | ||
y: 4096 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters