0

I want to export monthly mode composite of NDVI for 10 years, 2012-2022, using Landsat imageries. My code works fine till preparation of monthly NDVI but when I try to export the 120 scenes to drive I am getting following error message in the task manager:

Error: ImageCollection.toBands: Error in map(ID=1): Image.select: Pattern 'NDVI' did not match any bands. (Error code: 3)

Where am I getting wrong and what can I do to overcome the error?

var AREA = Table("users/hosseinbouali68/AREA");
var landsat5 = ee.ImageCollection("LANDSAT/LT05/C02/T1_L2");
var landsat7 = ee.ImageCollection("LANDSAT/LE07/C02/T1_L2");
var landsat8 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2");

Map.centerObject(AREA);
Map.addLayer(AREA);

var coefficients = {
  offset: ee.Image.constant([0.0003, 0.0088, 0.0061, 0.0412, 0.0254, 0.0172])
             .multiply(10000),
  gain: ee.Image.constant([0.8474, 0.8483, 0.9047, 0.8462, 0.8937, 0.9071])
};

var land5 = landsat5
.filterBounds(AREA)
.filter(ee.Filter.eq('WRS_PATH',163))
.filter(ee.Filter.eq('WRS_ROW',34))
.filter(ee.Filter.lt('CLOUD_COVER',45))
.map(function(img){
 var bands = img.select(
      ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B7'],
      ['blue', 'green', 'red', 'nir', 'swir1', 'swir2']);
      var calibration = bands.multiply(coefficients.gain).add(coefficients.offset)
      .round().toShort();
      var ref = calibration.multiply(2.75e-05).add(-0.2);
      return ref
      .copyProperties(img,img.propertyNames());
});

//print(land5);

var land7 = landsat7
.filterBounds(AREA)
.filter(ee.Filter.eq('WRS_PATH',163))
.filter(ee.Filter.eq('WRS_ROW',34))
.filter(ee.Filter.lt('CLOUD_COVER',45))
.map(function(img){
 var bands = img.select(
      ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B7'],
      ['blue', 'green', 'red', 'nir', 'swir1', 'swir2']);
      var calibration = bands.multiply(coefficients.gain).add(coefficients.offset)
      .round().toShort();
      var ref = calibration.multiply(2.75e-05).add(-0.2);
      return ref
      .copyProperties(img,img.propertyNames());
});

//print(land7);


var land8 = landsat8
.filterBounds(AREA)
.filter(ee.Filter.eq('WRS_PATH',163))
.filter(ee.Filter.eq('WRS_ROW',34))
.filter(ee.Filter.lt('CLOUD_COVER',45))
.map(function(img){
 var bands = img.select(
      ['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7'],
      ['blue', 'green', 'red', 'nir', 'swir1', 'swir2']);
      var ref = bands.multiply(2.75e-05).add(-0.2);
      return ref
      .copyProperties(img,img.propertyNames());
});

//print(land8);

///////data collection

var map = land5.merge(land7).merge(land8)
.sort('system:time_start')
.filterDate('2012-01-01', '2022-01-01');

 print('map', map);
 
/////////////////

var NDVI1 = map.map(function(image){
  var NDVI = image.expression(
    '((NIR-RED)/(NIR+RED))',{
'RED':image.select ('red'),
'NIR':image.select ('nir'),
}).rename('NDVI');
return image.addBands([NDVI]);
});

print('NDVI1', NDVI1);


////////////////////////

var temporalCollection = function(collection, start, count, interval, unit){
  
  var sequence = ee.List.sequence(0, ee.Number(count).subtract(1));
  var originDate = ee.Date(start);
  
  return ee.ImageCollection(sequence.map(function(i){
    var startDate = originDate.advance(ee.Number(interval).multiply(i), unit); 
    var endDate = originDate.advance(ee.Number(interval).multiply(ee.Number(i).add(1)), unit);
    var mosaicMap = collection.filterDate(startDate, endDate).mode()
                    .set('system:time_start', startDate.millis())
                    .set('system:time_end', endDate.millis());
        return mosaicMap//.rename(mosaicMap.date().format('YYYY-MM-dd'))
              .set('system:id', mosaicMap.date().format('YYYY-MM-dd'));
    })); 
};

var mapMonthly = temporalCollection(NDVI1.select('NDVI'), '2012', 120, 1, 'month');

print('mapMonthly',mapMonthly);

Export.image.toDrive({
  image: mapMonthly.select('NDVI').toBands(),
  description: 'mapMonthly',
  scale: 30,
  region: AREA,
  crs:'EPSG:4326',
  maxPixels: 1e13
  });

My code link https://code.earthengine.google.com/cc71ca4f3b74ee08963104705aa1d057

4
  • Hi @hossain, I cannot access your table (i.e., AREA). Please open the access to it so that others can run and test the code. You can allow access to by checking the box "Anyone can read" when you click on share.
    – Salit
    Sep 8 at 12:27
  • Hello I added the code for the area thank you for your attention
    – hossein
    Sep 8 at 18:24
  • Your question has closed, so others will not be able to write answer to it. Please edit your question by adding mode details, e.g. what are you trying to do, what you have done so far, at where are you stuck. I hope that would reopen the question.
    – Salit
    Sep 10 at 6:56
  • What bands are available? print(mapMonthly.first().bandNames() I wonder if there are any image in the collection. The filter on AREA and on path and row is a bit strange, do they intersect each other? I recommend using or the other. Sep 13 at 23:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy