2

I am working with MODIS-NDVI data (MODIS/006/MOD13Q1) in Google Earth Engine. I have a script which exports each image in the image collection for given area and time period. I need to upgrade script which will composite band by years to have a single file for each year. Later, I need to export them Google Drive. Could anyone please assist me on upgrading below shown code?

var col = ee.ImageCollection("MODIS/006/MOD13Q1")
    .select("NDVI")
    .filterDate("2000-01-01","2018-06-01");


var ExportCol = function(col, folder, scale, type,
                         nimg, maxPixels, region) {
    type = type || "float";
    nimg = nimg || 500;

    var colList = col.toList(nimg);
    var n = colList.size().getInfo();

    for (var i = 0; i < n; i++) {
      var img = ee.Image(colList.get(i));
      var id = img.id().getInfo();

      var imgtype = {"float":img.toFloat(), 
                     "byte":img.toByte(), 
                     "int":img.toInt(),
                     "double":img.toDouble()
                    }

      Export.image.toDrive({
        image:imgtype[type],
        description: id,
        folder: folder,
        fileNamePrefix: id,
        region: studyarea2,
        scale: scale,
        maxPixels: maxPixels})
    }
  }

  ExportCol(col, "NDVI_MODIS_NKz_KZ", 250);
2
  • What kind of composite are you aiming for - the mean value for each band and year?
    – Kersten
    Jun 22, 2018 at 11:40
  • I would like to stack all layer in one year to single file like a bands and to be able export the stacked layers as single file Jun 22, 2018 at 13:43

1 Answer 1

2

try this code: https://code.earthengine.google.com/9052c17efac7990cf1c5449654186d08

Script:

var col = ee.ImageCollection("MODIS/006/MOD13Q1")
    .select("NDVI")
    .filterDate("2000-01-01","2018-06-01");

// Functions to stack colletion series into image bands  

var stackCollection = function(collection) {
  var first = ee.Image(collection.first()).select([]);
  var appendBands = function(image, previous) 
  {return ee.Image(previous).addBands(image);};
  return ee.Image(collection.iterate(appendBands, first));};

// Function to generate mask for each Scene.

var getSceneMask = function(scene) {
  var dateString = ee.Date(scene.get('system:time_start')).format('yyyy-MM-dd');
  var mask = scene.select('NDVI').gt(-9999).updateMask(ee.Image(1));
  return mask.rename(dateString);};

//Stacked mask for collection

var MaskCol = stackCollection(col.map(getSceneMask));

// Generate a datelist for collection 

var datelist = ee.List(MaskCol.bandNames()).map(function(date){return ee.Date(date).format('yyyy-MM-dd')});

print(datelist)

// Stack bands and rename by date  

var ndvi_stacked = stackCollection(col.select('NDVI')).divide(10000).rename(datelist);

Map.addLayer(ndvi_stacked,{min:-1,max:1},'ndvi_stacked');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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