0

I am trying to calculate a water index from Sentinel 2 dataset from Sentinel 2, the error is 'Error: reduce.median: Error in map(ID=20180101T041721_20180101T041718_T47SND): Image.select: Pattern 'nd' did not match any bands.' I'm not clear what 'nd' means? How to export the index value from the image collection (S2)?

Here's my code,

// Function to calculate and add an AWEInsh band
var addAWEInsh = function(image) {
return image.addBands(image.expression(
    '4*(GREEN - SWIR1)-(0.25*NIR+2.75*SWIR2)', {
      'GREEN': image.select('B3'),
      'SWIR1': image.select('B11'),
      'SWIR2': image.select('B12'),
      'NIR': image.select('B8'),
}));
};

// Add NDWI band to image collection
var S2 = S2.map(addAWEInsh);
// Extract NDWI band and creat NDWI median composite image
var AWEInsh = S2.select(['nd']);
var AWEInsh = AWEInsh.median();
2
  • Could you please add the line where you define S2 so we can test your code fully? Also how can you not know what 'nd' means if you wrote yourself the command '.select(['nd'])'?
    – M. Nicolas
    Commented Feb 10, 2021 at 23:06
  • The OP doesn't know because the code was written by someone else mdpi.com/2072-4292/9/12/1315/s1
    – user2856
    Commented Feb 11, 2021 at 2:00

1 Answer 1

4

S2.select(['nd']) is asking for the band named 'nd' in each image in the collection. It only makes sense if you have a band named 'nd'. There's nothing special about that particular name.

It sounds like a reasonable name for your band is AWEInsh, so you'd do that like this:

var addAWEInsh = function(image) {
  return image.addBands(image.expression(
      'AWEInsh = 4*(GREEN - SWIR1)-(0.25*NIR+2.75*SWIR2)', {  // Changed expression
        'GREEN': image.select('B3'),
        'SWIR1': image.select('B11'),
        'SWIR2': image.select('B12'),
        'NIR': image.select('B8'),
  }));
};

var S2 = S2.map(addAWEInsh);
var AWEInsh = S2.select(['AWEInsi']);                         // Changed select()
var AWEInsh = AWEInsh.median();

Note that I added AWEInsh = to your image expression. That's a way to set the band name inside an expression. You can also use .rename() if you have an image with a band that you didn't use expression() to create.

Or, in your particular case, if you just need a median and aren't doing more work, then it can simpler to just work with one-band images and not worry about the name:

var computeAWEInsh = function(image) {
  return image.expression(                                    // Removed addBands
      '4*(GREEN - SWIR1)-(0.25*NIR+2.75*SWIR2)', {
        'GREEN': image.select('B3'),
        'SWIR1': image.select('B11'),
        'SWIR2': image.select('B12'),
        'NIR': image.select('B8'),
  }));
};

var AWEInsh = S2.map(computeAWEInsh).median();

The result will have only one band, which means you don't necessarily need to worry about what it's named (or if the name does end up mattering, you can .rename('AWEInsh')).

1
  • It works. Thank you very much! ^^
    – ran ran
    Commented Feb 14, 2021 at 21:02

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.