0

I am trying to export monthly NDVI values for 2007 for my asset 'M2007' which is a mangrove extent shapefile. I've been able to export following the same code, the monthly ndvi values for 2012, apart from one month that was missing a value (I'm not sure why either).

What can I do to acquire this data?

https://code.earthengine.google.com/3eb83ce8d46725356b871d94c663f97e

///////////////////////////////////////

function maskL457sr(image) {
  var qaMask = image.select('QA_PIXEL').bitwiseAnd(parseInt('11111', 2)).eq(0);
  var saturationMask = image.select('QA_RADSAT').eq(0);

  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var thermalBand = image.select('ST_B6').multiply(0.00341802).add(149.0);

  return image.addBands(opticalBands, null, true)
      .addBands(thermalBand, null, true)
      .updateMask(qaMask)
      .updateMask(saturationMask);
}


var l7 = ee.ImageCollection("LANDSAT/LE07/C02/T1_L2")
  .filterDate('2007-01-01', '2007-12-31')
  .filterBounds(roi)
  .map(maskL457sr)

var months = ee.List.sequence(1, 12);

var monthlyNDVIList = months.map(function (month) {
  var startDate = ee.Date.fromYMD(2007, month, 1);
  var endDate = startDate.advance(1, 'month');

  var l7Monthly = l7
    .filterDate(startDate, endDate)
    .median()
    .clip(roi);

  var ndvi = l7Monthly.normalizedDifference(['SR_B4', 'SR_B3']); 

  var meanNDVI = ndvi.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: roi,
    scale: 30,
    maxPixels: 1e9
  }).get('nd');

  return ee.Feature(null, {
    'Month': month,
    'Mean_NDVI': meanNDVI,
  });
});

var monthlyNDVICollection = ee.FeatureCollection(monthlyNDVIList);

Export.table.toDrive({
  collection: monthlyNDVICollection,
  description: 'Monthly_NDVI_2007',
  folder: 'GEE',
  fileFormat: 'CSV'
});

1 Answer 1

0

There are a couple of issue there.

  1. The reducer to get the median composite creates new band names, so you need to rename the bands back to use them (or use the renamed)
  2. When you filter the collection month by month, if there are no images in one month then you get an empty collection, therefore you cannot create your median composite. To solve this you can use ee.Algorithms.If function.

This is the relevant change

var bands = ['SR_B4', 'SR_B3']
var l7Monthly = l7.select(bands)
                  .filterDate(startDate, endDate)
var comp = ee.Image(ee.Algorithms.If(
  l7Monthly.size(),
  l7Monthly.median().rename(bands),
  ee.Image([0, 0]).rename(bands) // The median NDVI value for the empty month will be 0
))
var ndvi = comp.normalizedDifference(bands); 

https://code.earthengine.google.com/fc84f383b68aeeaefe5c96499e118d28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.