Google Earth Engine – Get Image with highest max Scene NDWI

google-earth-enginemaximumnormalized-difference-water-index

By adapting code from Getting Image with highest max Scene NDVI from Image collection using Google Earth Engine I have been able to make a script that gives me the date and mean highest NDVI for each polygon in my collection in a CSV: https://code.earthengine.google.com/72168c350ff039fb7abe24e9fcfe55a8?noload=true

Now, I want to do the same thing again, but to show the mean highest NDWI with dates. I would have thought this would be as simple as changing the Band 4 (Red) to Band 11 (SWIR) in my NDVI function:

function toNdvi(image) {
  var newImg = image
    .normalizedDifference(['B8', 'B11'])
    .rename('ndvi')
    .updateMask(
      image.select('QA60').not()
    )
    return newImg.updateMask(
      newImg.select('ndvi').gte(0.1)
    )
}

However, upon making the one change, the exported CSV no longer provides a column with mean NDWI. This remains the case even when I remove the lower threshold (0.1). Why has highest mean been removed from my output and how can I restore it?

Best Answer

First of all, I searching for NDWI formula by using Sentinel-2 products and bands are B3, B8; not B8, B11. However, I used your provided formula for following modifications in your code (as your asset geometry was not accessible by me, I used an arbitrary polygon in USA).

.
.
.
function setMean(field, image) {
  var mean = image.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: field.geometry(),
    scale: 10,
    maxPixels: 1e13
  }).get('ndwi')
  return image
    .set('mean', mean)
}
.
.
.
function toNdvi(image) {
  var newImg = image
    .normalizedDifference(['B8', 'B11'])
    .rename('ndwi')
    .updateMask(
      image.select('QA60').not()
    )
    return newImg.updateMask(
      newImg.select('ndwi')
    )
}

Running complete code, the exported CSV provides a column with mean NDWI as expected; corroborated in following image for opened CSV (I deleted some columns for better visualization).

enter image description here

Related Question