NDWI add DataMask

Hey, I am trying to add dataMask to NDWI index from custom scripts, but the whole screen is blue. I’m adding the code below.

var colorRamp1 = [
  	[0, 0xFFFFFF],
  	[1, 0x008000]
  ];
var colorRamp2 = [
  	[0, 0xFFFFFF],
  	[1, 0x0000CC]
  ];
  
let viz1 = new ColorRampVisualizer(colorRamp1);
let viz2 = new ColorRampVisualizer(colorRamp2);
	
function evaluatePixel(samples) {
 
    var val = index(samples.B03, samples.B08, samples.dataMask);
    val.push(samples.dataMask);
	if (val < -0) {
    return viz1.process(-val);
  } else {
    return viz2.process(Math.sqrt(Math.sqrt(val)));
  }
}

function setup() {
  return {
    input: [{
      bands: [
        "B03",
        "B08",
        "dataMask"
      ]
    }],
    output: {
      bands: 4
    }
  }
}

Dear @remote,

you correctly added a 4th output band to hold the dataMask but the index function only works with two inputs. It’s easiest if you put the dataMask directly in the return statement. I adjusted the script for you below:

var colorRamp1 = [
  	[0, 0xFFFFFF],
  	[1, 0x008000]
  ];
var colorRamp2 = [
  	[0, 0xFFFFFF],
  	[1, 0x0000CC]
  ];
  
let viz1 = new ColorRampVisualizer(colorRamp1);
let viz2 = new ColorRampVisualizer(colorRamp2);
	
function evaluatePixel(samples) {
 
    var val = index(samples.B03, samples.B08);
	if (val < -0) {
    return [...viz1.process(-val), samples.dataMask];
  } else {
    return [...viz2.process(Math.sqrt(Math.sqrt(val))), samples.dataMask];
  }
}

function setup() {
  return {
    input: [{
      bands: [
        "B03",
        "B08",
        "dataMask"
      ]
    }],
    output: {
      bands: 4
    }
  }
}
1 Like

It works! Thank you.

Can you explain the reason to take squareroot two times of the index output