DEA WOfS and Water Classifier Notebook

Description: This notebook allows users to perform water classification. The following steps will allow users to connect to a data cube, define the analysis location and time period (extent of latitude/longitude and dates), and then run the Australian Water Observations from Space (WOfS) algorithm. The outputs of the WOfS algorithm include static and time series pixel-level water observations for any pixel. These results provide critical information for water management that will allow users to assess water cycle dynamics, historical water extent and the risk of floods and droughts.

This notebook is based on a CEOS notebook

The water classifier is based on Mueller et al. (2015) github link to water classifier code

Requirements: You need to run the following commands from the command line prior to launching jupyter notebooks from the same terminal so that the required libraries and paths are set:

module use /g/data/v10/public/modules/modulefiles

module load dea

If you find an error or bug in this notebook, please either create an ‘Issue’ in the Github repository, or fix it yourself and create a ‘Pull’ request to contribute the updated notebook back into the repository (See the repository README for instructions on creating a Pull request).

Date: August 2018

Authors: Erin Telfer, Bex Dunn

Import modules from standard libraries, datacube and files

from matplotlib import pyplot as plt
import numpy as np
import os
import sys
import warnings
import xarray as xr
import matplotlib as mpl

#modules for datacube
import datacube
from import masking
from datacube.helpers import write_geotiff

# set datacube alias (just a string with what you're doing)
dc = datacube.Datacube(app='dc-WOfS and water classifier')

# Import external functions from dea-notebooks
import DEADataHandling, water_classifier_and_wofs, DEAPlotting

#ignore datacube warnings (needs to be last import statement)
import warnings
warnings.filterwarnings('ignore', module='datacube')

Edit query and extract data from DEA

User input: define details below

#Use this to manually define an upper left/lower right coords
lat_max = -12.4 #
lat_min = -12.7
lon_max = 135.2
lon_min = 134.9

#define temporal range
start_of_epoch = '2016-01-01'
end_of_epoch =  '2016-05-01'

#define Landsat sensors of interest
sensors = ['ls8','ls7','ls5']
#Query is created
query = {'time': (start_of_epoch, end_of_epoch),}
query['x'] = (lon_min, lon_max)
query['y'] = (lat_max, lat_min)
query['crs'] = 'EPSG:4326'

print (query)
{'time': ('2016-01-01', '2016-05-01'), 'x': (134.9, 135.2), 'y': (-12.4, -12.7), 'crs': 'EPSG:4326'}
mask_dict = {'cloud_acca': 'no_cloud',
             'cloud_fmask': 'no_cloud',

#using the load function from DEADataHandling to get the data and filter
nbart = DEADataHandling.load_clearlandsat(dc, query,
    Skipping ls5
Ignoring SLC-off observations for ls7
Loading ls7 PQ
    Loading 0 filtered ls7 timesteps
Loading ls8 PQ
    Loading 8 filtered ls8 timesteps
Combining and sorting ls5, ls7 and ls8 data

Classify water and calculate Water Observations from Space (WOfS)

#Use water clasifier function
warnings.filterwarnings('ignore') #turn off warnings
water_class = water_classifier_and_wofs.water_classifier(nbart)
warnings.filterwarnings('always') #turn on warnings
Dimensions:  (time: 8, x: 1358, y: 1337)
  * time     (time) datetime64[ns] 2016-01-01T01:04:21 2016-01-17T01:04:20 ...
  * y        (y) float64 -1.311e+06 -1.311e+06 -1.311e+06 -1.311e+06 ...
  * x        (x) float64 3.203e+05 3.203e+05 3.204e+05 3.204e+05 3.204e+05 ...
Data variables:
    wofs     (time, y, x) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...

Calculate and plot WOfS

#note, this is using only one band for the count, and this isn't robust.
total_water_obs = water_class.wofs.sum(dim='time')
nbar_count ='time')
wofs = ((total_water_obs / nbar_count)*100)


xarray dataset has no spatial data; defaulting to plotting without coordinates. This can often be resolved by adding `keep_attrs = True` during an aggregation step
(<Figure size 720x720 with 2 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7fbb1ca98a90>)

Save WOfS output to netCDF

DEADataHandling.write_your_netcdf(wofs, 'wofs', 'wofs_{}_{}.nc'.format(start_of_epoch,end_of_epoch), crs =

Save WOfS output to GeoTiff

# Convert to a dataset and restore spatial attributes
dataset = wofs.to_dataset(name='wofs')
dataset.attrs['affine'] = nbart.affine

write_geotiff('wofs_{}_{}.tif'.format(start_of_epoch,end_of_epoch), dataset)


  1. Mueller, et al. 2015., Water observations from space: Mapping surface water from 25 years of Landsat imagery across Australia, Remote Sensing of Environment.