Data selection (including pre processing) is the first step of remote sensing analysis. In this practice, we will focus on the pre processing part, especially in how to create a cloud free mosaic. We are going to create a cloud free image using the Landsat 8 Level 2 Collection 1 Tier 1, then creating a cloud mask and modifying the reducer.
First, we need to remember what we did in the first practice
In the first practice we learned how to filter the data (in green) and then to transform the image collection into a single image (in purple). To remove clouds we need to add another step after Select bands (in green) and change the Reducer type (in purple)
We will need to the Cloud Mask (in green) function after the select bands, and change the Reducer type (in yellow) to get the cloud free image
In a brand new Code Editor
> Draw your AOI > change the variable name from geometry to AOI
> Add the code below into the Code Editor > click Run
After clicking run, you can change bands and color stretch on the Layers Panel to SR_B4, SR_B3, SR_B2; and Stretch: 98%
As you can see, this image has a lot of clouds.
So for the next step lets try to get rid of these clouds!
Q1. What is different in this code compared to the previous ones?
Q2. Can you explain the difference?
There several approached to remove clouds
In this tutorial we are going to focus in the last 2: Apply a cloud mask, and change the reducer
What is a cloud mask? When you think X multiplies 0, the result is always 0 regardless of the X value. The mask layer is a binary layer that has 0 or 1. Therefore, when you apply a mask layer to an image, the image value will be 0 in areas where the mask layer value is 0. Thinking about cloud masks, the cloud and cloud shadow areas is 0 and the other areas are 1.
Landsat 8 has a Quality Assessment band (QA_PIXEL) that includes cloud and cloud shadow locations. It is a 16bit (including 0bit) image consisting of 2-bit flag information.
Flag 1: means yes
Flag 0: means no
To remove the cloud you can use Bit 3 (Cloud Flag) and Bit 4 (Cloud shadow flag) values.
You can use the flags to remove the cloud areas. The cloud mask is applied for each image in the image collection. Each image has its own cloud mask.
Let's create the function for Landsat 8 Image Collection
> Add the following code on line 27
This code block defines a new function named: maskL8sr. This function create the cloud mask for a image.
Now that we have this function, we need to apply to every image we filtered inside the image collection.
Since we need to apply these functions to the filtered images from the image collection, we are going to paste the following code on line 19, after .select(bands)
> Add the following code on line 19
The function .map applies the maskL8sr function to all filtered images from the image collection.
If you Run, you will get the following error
The error says that the 'QA_PIXEL' band is missing, so we need to add it on line 2, where we defined the bands
> Add 'QA_PIXEL' inside the band list on line 2
> Click Run and change the band combination to SR_B4, SR_B3, SR_B2, and stretch to 98% on the Layers Panel
You will see the image below, with some areas without data. That means that there was no cloud free image during the selected period
To fix that, you can increase the date range to select more images in the image collection. This time let's change on line 5 to 2020
You will be able to see the following image
You use the .median() as a reducer. The .median() function calculates the median value of each pixel from all images in an image collection. The calculation of representative values is affected by higher or lower values. So, one way to try to eliminate clouds is to use a different reducer such as ee.Reducer.intervalMean()
This reducer can exclude higher and lower values from the calculation using two thresholds of percentile, generating a more robust result.
Let's try in our code
In line 23, replace the median reducer to .reduce(ee.Reducer.intervalMean(25,75))
> Replace the code below on line 23
You can play with threshold values to see if the results improve like: .reduce(ee.Reducer.intervalMean(25,75))
You can export your composite images, indices, and classification results to Google Drive and then download them to use in your GIS.
> Copy the code below at the end of the code, in this case, line 40 > click Run
> Go to Task Panel > click Run
You can see the next image and change the settings. The file will be stored in your Google Drive
Code for Landsat 8
// Defining bands to be selected
var bands = ['SR_B2','SR_B3','SR_B4','SR_B5','SR_B6','SR_B7','QA_PIXEL'];
// Defining date range
var startYear = 2020;
var endYear = 2021;
var startMonth = 1;
var endMonth = 12;
// Image collection
var image_collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2');
// Filtering image collection
var filtered = image_collection
.filter(ee.Filter.calendarRange(startYear, endYear, 'year'))
.filter(ee.Filter.calendarRange(startMonth, endMonth, 'month'))
.filterBounds(AOI)
.select(bands)
.map(maskL8sr);
// Reducing filtered image collection
var composite = filtered
.reduce(ee.Reducer.intervalMean(25,75))
.clip(AOI);
Map.addLayer(composite);
function maskL8sr(image) {
// Bits 3 and 4 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = (1 << 4);
var cloudsBitMask = (1 << 3);
// Get the pixel QA band.
var qa = image.select('QA_PIXEL');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) //shift 1 to 4th bit
.and(qa.bitwiseAnd(cloudsBitMask).eq(0)); //shift 1 to 3rd bit
return image.updateMask(mask);
}
// Export a clipped Landsat8 to Google drive
Export.image.toDrive({
image: composite, //variable that includes the final image
description: 'name your image', //define the image name
folder: "Result_GEE", //folder name in your google drive
scale: 30, //spatial resolution in meter.
region: AOI //you can clip the image
});
Code for Sentinel 2 (the cloud mask function is a little bit different)
// Initial settings
// Set time period and study area by Pathrow
var iniyear = 2020;
var endyear = 2020;
var inimonth = 1;
var endmonth = 12;
function maskS2clouds(image) {
var qa = image.select('QA60');
// Bits 10 and 11 are clouds and cirrus, respectively.
var cloudBitMask = (1 << 10);
var cirrusBitMask = (1 << 11);
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
.and(qa.bitwiseAnd(cirrusBitMask).eq(0));
return image.updateMask(mask).divide(10000);
}
function maskS2clouds2(image) {
var qa = image.select('SCL');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.eq(4).or(qa.eq(5)).or(qa.eq(6));
return image.updateMask(mask).divide(10000);
}
var dataset = ee.ImageCollection('COPERNICUS/S2_SR')
.filter(ee.Filter.calendarRange(iniyear, endyear,'year'))
.filter(ee.Filter.calendarRange(inimonth, endmonth, 'month'))
.filterBounds(AOI)
//.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',50))
.map(maskS2clouds);
// Create the Composite image using percentile function
var selected_dataset = dataset.select(['B2','B3','B4']);
//var selected_dataset = dataset.select(['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B11','B12']);
//var Composite = selected_dataset.reduce(ee.Reducer.median());
var Composite = selected_dataset.reduce(ee.Reducer.intervalMean(10,60));
var clipped_Composite = Composite.clip(AOI);
//Map.addLayer(Masked_count,countVisParam,'Count after masked');
Map.addLayer(clipped_Composite,
{bands: ['B4_mean', 'B3_mean', 'B2_mean'], min: 0, max: 0.3,gamma: 2},
'Composite');
Map.centerObject(AOI);