Instead of using the function made by Rodrigo Principe, I created a for
loop to export monthly images for several years. The only problem is that I can't download every month for the entire ImageCollection
, so I have to download it (the collection) in chunks. The code is:
var landsat = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')
Map.centerObject(table);
//Create mask function
function maskL8sr(image) {
// Bits 2, 3 and 5 are water, cloud shadow and cloud, respectively.
var cloudShadowBitMask = (1 << 3);
var cloudsBitMask = (1 << 5);
var waterBitMask = (1 << 2);
// 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)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
return image.updateMask(mask);
}
for (var y = 2019; y < 2020; y++) {
for (var i = 2; i < 3; i++) {
var landsat = landsat.filter(ee.Filter.calendarRange(i, i, 'month'))
.filter(ee.Filter.calendarRange(y, y, 'year'))
.filterBounds(table)
.map(maskL8sr);
var landsat = landsat.select('SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7', 'ST_B10')
print (landsat)
var mean2 = landsat.select('ST_B10').reduce(ee.Reducer.mean()).multiply(0.00341802).add(149.0).clip(table);
//Create variable for each band
var B2 = landsat.select('SR_B2').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B3 = landsat.select('SR_B3').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B4 = landsat.select('SR_B4').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B5 = landsat.select('SR_B5').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B6 = landsat.select('SR_B6').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B7 = landsat.select('SR_B7').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var B10 = landsat.select('ST_B10').reduce(ee.Reducer.mean()).multiply(0.00341802).add(149.0).clip(table);
var B10 = B10.subtract(273.15)
var ndvi = B5.subtract(B4).divide(B5.add(B4)).rename('ndvi');
var ndbi = B6.subtract(B5).divide(B6.add(B5)).rename('ndbi');
var mean1 = landsat.select('SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7').reduce(ee.Reducer.mean()).multiply(0.0000275).add(-0.2).clip(table);
var ebbi = mean1.expression('(SWIR - NIR)/ 10 * sqrt(SWIR + TIRS)',
{
'SWIR':B6,
'NIR':B5,
'TIRS': B10
}).rename('ebbi');
var nbi = mean1.expression('(RED - SWIR1) / (NIR)',
{
'RED':B4,
'NIR':B5,
'SWIR1':B6,
}).rename('nbi');
var gndvi = mean1.expression('(NIR - GREEN) / (NIR + GREEN)',
{
'NIR':B5,
'GREEN':B3,
}).rename('gndvi');
var nbai = mean1.expression('((SWIR2 - SWIR1) / GREEN) / ((SWIR2 + SWIR1) / GREEN)',
{
'SWIR2':B7,
'SWIR1':B6,
'GREEN':B2
}).rename('nbai');
var mbai = mean1.expression('(NIR + (1.57 * GREEN) + (2.4 * SWIR1)) / (1 + NIR)',
{
'NIR':B5,
'SWIR1':B6,
'GREEN':B3
}).rename('mbai');
var evi = mean1.expression('2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',
{
'NIR':B5,
'RED':B4,
'BLUE':B2
}).rename('evi');
var bandSpectralList = [B4, B5, B10, evi, ndvi, gndvi, ndbi, ebbi, nbi, nbai, nbi];
var desc = "";
for (var banda = 0; banda < bandSpectralList.length; banda++) {
switch (bandSpectralList[banda]) {
case B2:
desc = "blue";
break;
case B3:
desc = "green";
break;
case B4:
desc = "red";
break;
case B5:
desc = "nir";
break;
case B6:
desc = "swir1";
break;
case B7:
desc = "swir2";
break;
case B10:
desc = "tirs";
break;
case B10:
desc = "tirs";
break;
case B10:
desc = "tirs";
break;
case evi:
desc = "evi";
break;
case ndvi:
desc = "ndvi";
break;
case gndvi:
desc = "gndvi";
break;
case ndbi:
desc = "ndbi";
break;
case ebbi:
desc = "ebbi";
break;
case nbi:
desc = "nbi";
break;
case nbai:
desc = "nbai";
break;
case mbai:
desc = "mbai";
break;
// Add more cases as needed
default:
desc = "wrong_name";
break;
}
var currentBand = bandSpectralList[banda];
Export.image.toDrive({
image: currentBand,
description: desc.toString() + "_" + y + "_" + ee.Number(i).format('%02d').getInfo(),
scale: 130, //100 for Band10
maxPixels: 1000000000000,
region: table,
folder: 'Landsat-8',
crs: 'EPSG:3309'
})
}
}
}