1
var collection = ee.ImageCollection('MODIS/006/MYD13A1').select('NDVI');

// Define reference conditions from the first 10 years of data.
var reference = collection.filterDate('2001-01-01', '2010-12-31')
  // Sort chronologically in descending order.
  .sort('system:time_start', false);

// Compute the mean of the first 10 years.
var mean = reference.mean();

// Compute anomalies by subtracting the 2001-2010 mean from each image in a
// collection of 2011-2014 images. Copy the date metadata over to the
// computed anomaly images in the new collection.

var series = collection.filterDate('2011-01-01', '2014-12-31').map(function(image) {return image.subtract(mean).set('system:time_start', image.get('system:time_start'));
});

// Display cumulative anomalies.
Map.setCenter(84, 27, 5);
Map.addLayer(series.sum().clip(Da),
    {min: -60000, max: 60000, palette: ['FF0000', '000000', '00FF00']}, 'NDVI anomaly');

// Get the timestamp from the most recent image in the reference collection.
var time0 = reference.first().get('system:time_start');

// Use imageCollection.iterate() to make a collection of cumulative anomaly over time.
// The initial value for iterate() is a list of anomaly images already processed.
// The first anomaly image in the list is just 0, with the time0 timestamp.
var first = ee.List([
  // Rename the first band 'NDVI'.
  ee.Image(0).set('system:time_start', time0).select([0], ['NDVI'])
]);

// This is a function to pass to Iterate().
// As anomaly images are computed, add them to the list.
var accumulate = function(image, list) {
  // Get the latest cumulative anomaly image from the end of the list with
  // get(-1).  Since the type of the list argument to the function is unknown,
  // it needs to be cast to a List.  Since the return type of get() is unknown,
  // cast it to Image.
  var previous = ee.Image(ee.List(list).get(-1));
  // Add the current anomaly to make a new cumulative anomaly image.
  var added = image.add(previous)
    // Propagate metadata to the new image.
    .set('system:time_start', image.get('system:time_start')).float();
  // Return the list with the cumulative anomaly inserted.
  return ee.List(list).add(added);
};

// };
// Create an ImageCollection of cumulative anomaly images by iterating.
// Since the return type of iterate is unknown, it needs to be cast to a List.


var cumulative = ee.ImageCollection(ee.List(series.iterate(accumulate, first)));

print (cumulative);

var counter = 0;

// loop through the year
for (var y = 2011; y < 2015 ; y++) {
  // loop through the months
  for (var m = 1; m < 12 ; m++) {
  
  // get the image
  var img = ee.Image(cumulative.toList(1, counter).get(0));
 
  // store the image
  Export.image.toDrive({
       image: img,
       description: y.toString() + m.toString(),
       scale: 1000,
    region: Da
 });
}}

I am getting this error while running this code Error: Pixel type not supported: Type<MaskOnly>. Convert the image to a floating point type or a smaller integer type, for example, using ee.Image.toDouble(). I tried to convert image to float but still getting this error. Please help. I am stuck

1

Earth Engine is getting confused when it tries to export that first image and finds that it's constant and all zeros. The error can be fixed by casting the first image with .float().

var first = ee.List([
  // Rename the first band 'NDVI'.
  ee.Image(0).set('system:time_start', time0).select([0], ['NDVI']).float()
]);

In fact, take a close look at the export loop. I don't think it's doing what you want. Don't export an image of all zeros. Don't convert cumulative to an ImageCollection and back to a list again. (Avoid converting to a list in general as described here). The counter variable is not updated. (Should it be?)

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.