I am trying to create a code that could be helpful for making quarterly composites from 2013 to 2016 in order to calculate NDVI and EVI. I have figured out this code:

var start = ee.Date('2013-01-01');
var end = ee.Date('2019-01-01');
var numbQuarters = end.difference(start, 'month').divide(3).ceil();
var ndvi = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .map(function(image) {
    return image.select().addBands(image.normalizedDifference(['B5', 'B4']));
// make a composite image for every quarter
var quarterlyImages = ee.ImageCollection.fromImages(
            ee.List.sequence(1, numbQuarters).map(function(quarter){
              var startTemp = start.advance(ee.Number(quarter).subtract(1).multiply(3), 'month');
              var endTemp = start.advance(ee.Number(quarter).multiply(3), 'month');
              //var image = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
                                //.filterDate(startTemp, endTemp)
                                // define a way to composite an image
                                //.mosaic(); // .mean(); .mossaic(); .max(); etc
              var subset = ndvi.filterDate(startTemp,endTemp);
              //return image.set('system:time_start', startTemp.millis(),
                               //'system:time_end', endTemp.millis());
              return subset.mean().set('system:time_start',startTemp);
//var median = quarterlyImages.median();
//Map.addLayer(median,{ min:-1, max: 1, palette: ['blue', 'white', 'green']},'median');
//Lay anh trong imageCollection
var sort = quarterlyImages.toList(100);
var clip1 = ee.Image(sort.get(1)).clip(table);//1st image 2nd quarter of 2013
var clip2 = ee.Image(sort.get(2)).clip(table);//2nd image 3rd quarter of 2013
var clip3 = ee.Image(sort.get(3)).clip(table);//3rd image 4th quarter of 2013

// print some stuff
///print('image collection', quarterlyImages)
//print('startDates', quarterlyImages.aggregate_array('system:time_start'))
//print('sendDates', quarterlyImages.aggregate_array('system:time_end'))
Map.addLayer(clip1,{ min:-1, max: 1, palette: ['blue', 'white', 'green']}, 'Phu Quoc 01');
Map.addLayer(clip2,{ min:-1, max: 1, palette: ['blue', 'white', 'green']}, 'Phu Quoc 02');
Map.addLayer(clip3,{ min:-1, max: 1, palette: ['blue', 'white', 'green']}, 'Phu Quoc 03');
Map.centerObject(table, 7);

however when I apply this code above to extract images, some of them have too many clouds so that I could not see the image clearly. I am thinking of using a cloud masking function to solve this problem but I assume that I did not make it correctly and therefore it did not work well. Here is the code:

var cloud_pixel_qa =  image.select('pixel_qa').bitwiseAnd(32).neq(0);//2^5
var cloud_shadow_pixel_qa =  image.select('pixel_qa').bitwiseAnd(8).neq(0);//2^3
var maskedClouds = ((cloud_pixel_qa).or(cloud_shadow_pixel_qa));
return image.updateMask((maskedClouds.add(1).unmask(0)).eq(1));
var L8_coll_mask=L8_coll.map(masking_cloud)
Map.addLayer(L8_coll_mask.median().clip(LĐ_T),vizParams,'false color composite cloud mask');

Does anyone have any suggestions?

Your Answer

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

Browse other questions tagged or ask your own question.