This notebook is inspired by a publication titled Assessment of Forest Degradation in Vietnam Using Landsat Time Series Data authored by Vogelmann et al. You can retrieve a copy from mdpi
by following this link.
For a select year:
Flow Diagram
In [1]:
def NDVI(dataset):
return (dataset.nir - dataset.red)/(dataset.nir + dataset.red).rename("NDVI")
In [2]:
import xarray as xr
import numpy as np
def _where_not_nan(arr):
return np.where(np.isfinite(arr))
def _flatten_shallow(arr):
return arr.reshape(arr.shape[0] * arr.shape[1])
def per_pixel_linear_trend(pixel_da: xr.DataArray) -> xr.DataArray:
time_index_length = len(pixel_da.time)
ys = _flatten_shallow(pixel_da.values)
xs = np.array(list(range(time_index_length)))
not_nan = _where_not_nan(ys)[0].astype(int)
xs = xs[not_nan]
ys = ys[not_nan]
pf = np.polyfit(xs,ys, 1)
return xr.DataArray(pf[0])
def trend_product(da: xr.DataArray) -> xr.DataArray:
stacked = da.stack(allpoints = ['latitude', 'longitude'])
trend = stacked.groupby('allpoints').apply(per_pixel_linear_trend)
unstacked = trend.unstack('allpoints')
return unstacked.rename(dict(allpoints_level_0 = "latitude", allpoints_level_1 = "longitude"))
In [3]:
import numpy as np
def is_clear_l7(dataset):
#Create boolean Masks for clear and water pixels
clear_pixels = dataset.pixel_qa.values == 2 + 64
water_pixels = dataset.pixel_qa.values == 4 + 64
a_clean_mask = np.logical_or(clear_pixels, water_pixels)
return a_clean_mask
In [4]:
# Zanzibar, Tanzania
latitude = (-6.2238, -6.1267)
longitude = (39.2298, 39.2909)
date_range = ('2000-01-01', '2001-12-31')
In [5]:
from utils.data_cube_utilities.dc_display_map import display_map
display_map(latitude = latitude, longitude = longitude)
Out[5]:
In [6]:
import datacube
dc = datacube.Datacube()
In [7]:
data = dc.load( latitude = latitude,
longitude = longitude,
product = "ls7_usgs_sr_scene",
measurements = ['red', 'nir', 'pixel_qa'])
In [8]:
mask = is_clear_l7(data)
data = data.drop(['pixel_qa'])
In [9]:
data = NDVI(data)
In [10]:
data = data.where(mask)
del mask
In [11]:
from time import time
t1 = time()
data = trend_product(data)
t2 = time()
In [12]:
print(t2 - t1)
In [13]:
%matplotlib inline
data.where(data< 0).plot(figsize = (16,11), cmap = 'Reds_r')
Out[13]:
In [14]:
(-data).plot(figsize = (16,11))
Out[14]: