I am trying to make a correction for the NDVI index calculated from Sentinel-2 using GEE. I found out that for pixels with NDVI meaning in the range (0.5, 0.6) it should be subtracted 0.122..., and for range (0.6, 0.8) it should be added this. But for pixels < 0.5 or > 0.8 it should stay without changes. So I put it to my function through if/else. But only the first condition is working: if I put any condition after if - it will work, but nothing works after else if or else.
Can you please help me find what the wrong with this?
var tiles = [
'20170601T060631_20170601T061510_T42TYM',
'20170601T060631_20170601T061510_T43TBG',
'20170608T055641_20170608T060226_T42TYM',
'20170608T055641_20170608T060226_T43TBG',
'20170621T060631_20170621T061507_T42TYM',
'20170621T060631_20170621T061507_T43TBG'
]
var ndvi_palette = 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400, ' + '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301';
var S2_display = {bands: ['B4', 'B3', 'B2'], min: 0, max: 3000};
function addnd(input) {
var nd = input.normalizedDifference(['B8', 'B4']).rename('ndvi');
var nd_n;
if (nd.gte(0.5).and(nd.lte(0.6))){
nd_n = nd.expression(
'nd-0.122869881',
{'nd':nd});
return input.addBands(nd_n)
}
else if (nd.gt(0.6).and(nd.lte(0.8))){
nd_n = nd.expression(
'nd+0.122869881',
{'nd':nd})
return input.addBands(nd_n);
}
else {
nd_n = nd
return input.addBands(nd_n);
}
}
var S2_collection = ee.ImageCollection("COPERNICUS/S2")
.filter(ee.Filter.inList('system:index', tiles))
.filterBounds(geometry)
.map(addnd);
print(S2_collection);
var S2_mosaic = S2_collection.mean().clip(geometry);
print(S2_mosaic);
var NDVI_mean = S2_mosaic.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: geometry,
scale:10
})
Map.addLayer(S2_mosaic, S2_display, "Sentinel-2");
var ndvi_S2 = S2_collection.select('ndvi').mean().clip(geometry);
Map.addLayer(ndvi_S2, {min: -0.1, max: 1, palette: ndvi_palette}, 'NDVI S2',false);
Map.centerObject(geometry);
print("NDVI",NDVI_mean)