Calculate NDVI for a Single Month over 36 years (1985-2019)

The Normalized Difference Vegetation Index (NDVI) is a simple graphical indicator that can be used to analyze remote sensing measurements, typically, but not necessarily, from a space platform, and assess whether the target being observed contains live green vegetation or not.

The NDVI is calculated from these individual measurements as follows:

NDVI= (NIR-Red) \ (NIR+Red)

In this tutorial, we will stack all the Landsat images for the month of September collected from year 1984 through 2019, and calculate NDVI.

var geometry = /* color: #ff0000 */geometry;// tiled automatic download Landsat 8.

var colLT5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR');
var colLE7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR');
//var colLC8 = ee.ImageCollection("LANDSAT/LC8_SR");
var colLC8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR');

var west = -78.719;
var east = -77.885; //-103;
var north = 38.825;
var south = 38.286;
var Name = "Pilot"; //Provide output file name
var Ear_date = '-09-01';
var Late_date = '-09-30';

var FileName = 'Sep_West-Virgina_1984-2018';
//-----------------------------------//
var resolution = 30;
var projection = "EPSG:5070"; 

var littleBox = ee.Geometry.Rectangle(west,north,east,south);
var polygon = ee.Feature(littleBox); 

Map.centerObject(littleBox,7);
Map.addLayer(polygon, {color: '800000'},"Extent");

var yearArray1 = [];
for (var year = 1984; year < 2019; year+=1){
    yearArray1.push(year);
}

function maskClouds(image) {
  image = image.updateMask(image.select('pixel_qa').bitwiseAnd(32).eq(0));
	image = image.updateMask(image.select('pixel_qa').bitwiseAnd(16).eq(0));
	image = image.updateMask(image.select('pixel_qa').bitwiseAnd(8).eq(0));
	return image;
}

function CleanMaxNDVI(Collect,nd85) {
    var MaxNdvi = Collect.map(function(img){
      var ndvi = img.select('NDVI');
      return img.mask(ndvi.lte(nd85));
      // return img.addBands(mask85);
});
return MaxNdvi.qualityMosaic('NDVI');
}

function maxNDVI(img){
  var ndvi = img.normalizedDifference(['nir','red']).rename(['NDVI']);
  ndvi = ndvi.multiply(1000).add(1000).int16();
  return img.addBands(ndvi);
  }

//var LonColl = ee.ImageCollection(); 
function comps(yearArray){
  //var collection = [];
  for (var i = 0; i < yearArray.length; i++) {
    var year = yearArray[i];
    print (year);
    if (year < 1999){
      var collection = colLT5.filterDate(year+Ear_date,year+Late_date)
      .map(maskClouds)
      .select([0,1,2,3,4,6],['blue','green','red','nir','swir1','swir2'])
      .filterBounds(littleBox);
      
    }else if (year >= 1999 & year <= 2018){
      var l5coll = colLT5.filterDate(year+Ear_date,year+Late_date)
      .map(maskClouds)
      .select([0,1,2,3,4,6],['blue','green','red','nir','swir1','swir2'])
      .filterBounds(littleBox);
      var l7coll = colLE7.filterDate(year+Ear_date,year+Late_date)
      .map(maskClouds)
      //.map(final)year+Ear_date,year+Late_date
      .select([1,2,3,4,5,6],['blue','green','red','nir','swir1','swir2'])
      .filterBounds(littleBox);
      collection = ee.ImageCollection(l7coll.merge(l5coll));

    }else if (year > 2012){
      //print (year+Ear_date,year+Late_date);
      collection = colLC8.filterDate(year+Ear_date,year+Late_date)
      .map(maskClouds)
      //.map(final)year+Ear_date,year+Late_date
      .select([1,2,3,4,5,6],['blue','green','red','nir','swir1','swir2'])
      .filterBounds(littleBox);
    } 
  
    //var Composite = ee.Algorithms.Landsat.simpleComposite(collection, 75, 3);
  var NDVIcomp = collection.map(maxNDVI);
    
    if (year == yearArray[0]){
      var LonColl = ee.ImageCollection(NDVIcomp.qualityMosaic('NDVI'));
    } else {
      var newAnnComp = ee.ImageCollection(NDVIcomp.qualityMosaic('NDVI'));
      //var LonColl = ee.ImageCollection(newAnnComp.merge(LonColl));
    }
  
  }
  
  var nd90 = LonColl.select('NDVI').reduce(ee.Reducer.percentile([75]));
  //Map.addLayer(nd90);
  
  var ndvi90 = CleanMaxNDVI(LonColl,nd90);
  //Map.addLayer(ndvi90,{'min': 50,'max': [10000,10000,10000],  'bands':'nir,red,green'});
  return ndvi90;
}

var map1 = comps(yearArray1);
Map.addLayer(map1,{'min': 50,'max': [10000,10000,10000],  'bands':'nir,red,green'},'NDVI_Composite');

var reMap = map1
    .reproject(projection, null, 30);

Export.image.toDrive({
  image: reMap,
  description: FileName,
  folder: 'LANDSAT',
  scale: resolution,
  crs: projection,
  region: littleBox,
  maxPixels: 52041951120
});

print("is it good?");
var now = new Date();
print(now);

Return to GEE NDVI Page Return to GEE Homepage

Leave a Reply

%d bloggers like this: