Modular landcover system step-2: shadow masking

A  shadow mask for sentinel 2

1. Create a new script called shadow_module with the code below and save in your repository

// zScoreThresh: Threshold for cloud shadow masking- lower number masks out
// less. Between -0.8 and -1.2 generally works well
var zScoreThresh = -1;

// shadowSumThresh: Sum of IR bands to include as shadows within TDOM and the
// shadow shift method (lower number masks out less)
var shadowSumThresh = 0.35;

// contractPixels: The radius of the number of pixels to contract (negative
// buffer) clouds and cloud shadows by. Intended to eliminate smaller cloud
// patches that are likely errors
// (1.5 results in a -1 pixel buffer)(0.5 results in a -0 pixel buffer)
// (1.5 or 2.5 generally is sufficient)
var contractPixels = 1.5;

// dilatePixels: The radius of the number of pixels to dilate (buffer) clouds
// and cloud shadows by. Intended to include edges of clouds/cloud shadows
// that are often missed
// (1.5 results in a 1 pixel buffer)(0.5 results in a 0 pixel buffer)
// (2.5 or 3.5 generally is sufficient)
var dilatePixels = 2.5;

// Function for finding dark outliers in time series.
// Original concept written by Carson Stam and adapted by Ian Housman.
// Adds a band that is a mask of pixels that are dark, and dark outliers.
exports.shadowMask = function(collection,studyArea) {

var inBands = ["B8",'B11'];
var shadowSumBands = ['nir','swir1'];

var allCollection = ee.ImageCollection('COPERNICUS/S2').filterBounds(studyArea).select(inBands,shadowSumBands);
// Get some pixel-wise stats for the time series
var irStdDev =;
var irMean =;

var maskDarkOutliers = function(img){
var zScore =;
var irSum =;
var TDOMMask =;

TDOMMask = TDOMMask.focal_min(contractPixels).focal_max(dilatePixels).rename('TDOMMask');
return img.updateMask(TDOMMask.not()).addBands(TDOMMask);

// Mask out dark dark outliers
collection =;

return collection;


2. Go to your main script and add the code below:

var shadowS2 = require("users/username/landcoverS2:shadowMask_module");

print("applying cloud shadow mask");
s2 = shadowS2.shadowMask(s2,region);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s