Harmonized Landsat Sentinel (HLS) QA decode

Hello. I use statistical API with HLS data.

I take the vegetation index.
Need to decode QA according to this user guide. I don’t understand how to do it in evalscript. How to decode QA into comprehensible variables?

“maxcc=0.8” - not used because as I understand it applies to the whole tile and not the area of interest

I need to determine the percentage of pixels that I am not interested in. The script example is not correct, I added it to make it more clear what I need.

function setup() {
  return {
    input: [{
      bands: [
        "Red",
        "NIR_Narrow",
        "QA",
        "dataMask"
      ]
    }],
    output: [
      {
        id: "data",
        sampleType: "FLOAT32",
        bands: 2
      },
      {
        id: "dataMask",
        bands: 1
      }]
  };
}

function evaluatePixel(samples) {
    let index = (samples.NIR_Narrow - samples.Red) / (samples.NIR_Narrow + samples.Red);

    let cloud_percent = 0;
    if (samples.QA == 1) {
      cloud_percent = 1;
    }  else if (samples.QA == 2) {
      cloud_percent = 1;
    } else if (samples.QA == 3) {
      cloud_percent = 1;
    }
    return {
        data: [index, cloud_percent],
        dataMask: [samples.dataMask]
    };
}

Thanks

Hi Alexey,

As you are trying to decode the BQA bitmap layer, I can direct you to the documentation about this which can be found here. In addition, I authored an evalscript that decoded and visualised this band that you can find here.

If you need more help then let us know :+1:

2 Likes

Yes, it works. But decodes wrong, bqa.cloud = shadow

function setup() {
  return {
    input: ["Red", "Green", "Blue", "QA"],
    output: { bands: 3},
  };
}

function evaluatePixel(sample) {
  // define the decoder function

  var bqa = decodeL8C2Qa(sample.QA);

  // defining the colormap

  if (bqa.cloud == 1) {
    return [0, 0, 100];
  } else {
    return [2.5 * sample.Red, 2.5 * sample.Green, 2.5 * sample.Blue];
  }
}

Hi Alexey,

Thanks, I did a little more research, and as we don’t yet have an example of this in our documentation I have written a function to decode the QA band in the HLS data collection for you:

//VERSION=3

function setup() {
  return {
    input: [{
      bands: [
        "Red",
        "Green",
        "Blue",
        "QA",
        "dataMask"
      ]
    }],
    output: {
      bands: 4
    }
  }
}


function evaluatePixel(sample) {
    let f = 3.5;
  	var cloud = (sample.QA >> 1 & 1)
    var adj_cld_shadow = (sample.QA >> 2 & 1)
    var cloud_shadow = (sample.QA >> 3 & 1)
    var snow_ice = (sample.QA >> 4 & 1)
    var water = (sample.QA >> 5 & 1)


    if (cloud == 1) {
      return [0.9, 0.9, 0.9, sample.dataMask];
    }  else if (cloud_shadow == 1) {
      return [0.6, 0.35, 0.2, sample.dataMask];   
    }  else if (adj_cld_shadow == 1) {
      return [0.5, 0.5, 0.5, sample.dataMask];   
    }  else if (snow_ice == 1) {
      return [1, 0, 1, sample.dataMask];   
    }  else if (water == 1) {
      return [0, 0, 0.7, sample.dataMask];   
    } else {
      return [f * sample.Red, f * sample.Green, f * sample.Blue, sample.dataMask]
}
}

You will be most interested in the variables defined at the start of the evaluatePixel function which decode the QA bitmap. For example var cloud = (sample.QA >> 1 & 1).

The rest of the evalscript is just to show how you can visualise the Quality Assessment band. This is how it looks in EO Browser.

This should be all the information you need to achieve what you want to do in your evalscript :+1:

1 Like

Thank you. This is what i need. :slight_smile: