Create a cloud mask function for Landsat

Remove all clouds in the image collection

This introductory training is part of the official training materials prepared by the United States Department of Agriculture.

The Code Editor offers access to the full power of Earth Engine; however, a basic understanding of the fundamentals of coding and JavaScript is required. In this exercise, you will continue to learn about JavaScript syntax and some new Earth Engine spatial data concepts. You will build what you learned in exercise two about image objects; however you will now turn your focus to working with collections of images, or stacks of similar image objects. In this exercise, you will focus on basic concepts and methods associated with image collections in Earth Engine. This is an introduction to get users writing some simple JavaScript scripts.

Create a cloud mask function

1. Copy and paste the lines below into an empty Code Editor window. This should look familiar, as they are taken from Exercise 2. Run the script to make sure it works

// Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(LC8_image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = LC8_image.mask().and(cloud01.not());
//mask those pixels from the image
var LC8_imageNoClouds = LC8_image.mask(cloudmask);
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

2. Now edit it to turn it into a function. First, on line 6, insert a line that says the following:

var cloudfunction = function(){

3. Before the comment line reading ‘Review the result’, add the other curly brace. Your code should look like this:

 // Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

var cloudfunction = function(){
//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(LC8_image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = LC8_image.mask().and(cloud01.not());
//mask those pixels from the image
var LC8_imageNoClouds = LC8_image.mask(cloudmask);
}
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

4. Next we need to set the input parameter. Inside the parenthesis on line 7 (the line declaring the function), add the word image.

5. Next replace the variable LC8_image with image. There are three instances that need to be updated. See example code below.

// Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

var cloudfunction = function(image){
//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = image.mask().and(cloud01.not());
//mask those pixels from the image
var LC8_imageNoClouds = image.mask(cloudmask);
}
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

6. Finally, we need to add a return statement so that the output, LC8_imageNoClouds is a global variable that we can use outside of the function. Change the following line:

  //mask those pixels from the image
var LC8_imageNoClouds = LC8_image.mask(cloudmask);

To a return statement that looks like this:

return LC8_image.mask(cloudmask);

7. Now we are ready to call the function. After the closing curly bracket at the end of our function, create a new variable called LC8_imageNoClouds. Then run the function on the image we have stored as LC8_image. See the example statement below.

var LC8_imageNoClouds = cloudfunction(LC8_image);

8. Click Run to see if our function works. Refer to the full script below to make sure you have everything written correctly.

// Get an image and display in map window.
var LC8_image = ee.Image('LANDSAT/LC8_L1T_TOA/LC81290502013110LGN01');

//Specify the cloud likelihood threshold -
var cloud_thresh = 40;

var cloudfunction = function(image){
//use add the cloud likelihood band to the image
var CloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
//isolate the cloud likelihood band
var quality = CloudScore.select('cloud');
//get pixels above the threshold
var cloud01 = quality.gt(cloud_thresh);
//create a mask from high likelihood pixels
var cloudmask = image.mask().and(cloud01.not());
//mask those pixels from the image
return image.mask(cloudmask);
}


var LC8_imageNoClouds = cloudfunction(LC8_image);
//Review the result
addToMap(LC8_imageNoClouds, {bands:['B6','B5','B4'],min:0.1, max:0.5},'Landsat8scene_cloudmasked');

9. Save your script.

One thought on “Create a cloud mask function for Landsat”

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s