In GEE, how do I map over a collection of Landsat images using a specific range of dates?

201 Views Asked by At

I want to map over a collection of Landsat images by a specific date range (filterdate) and compute NDVI (or any other function). Specifically, I want to increment the "filterdate" by year, advancing from 2013 - 2022 (keeping day and month steady), and then map over the resultant image collection with the code shown below? In this example I have start date as: " .filterDate('2013-05-01', '2013-06-30') " I want the end date to be ('2022-05-01', '2022-06-30'). Ideally, I could store the NDVI output with a date stamp to an image band, create a ndvi collection, add unique NDVI images to playground, and print the mean NDVI by year to a chart.

Here is the GEE link: https://code.earthengine.google.com/c19bdfbb0521b59edf4e7dcc59bb6e6c Below is my GEE code:

var Salt = ee.Geometry.Polygon(
        [[[-111.00997924804688, 33.67378281830218],
          [-111.01478576660156, 33.63463083134138],
          [-110.95367431640625, 33.60575555447343],
          [-110.9183120727539, 33.606613375388086],
          [-110.91384887695312, 33.63520252129719],
          [-110.96122741699219, 33.669496972795535]]]);
 
// choose a location and zoom level to focus the initial map display
Map.setCenter(-110.98,33.65,12); // Display Salt.

/////////////////////  Landsat 8  /////////////////////////////////////////////

// SimpleCloudScore, an example of computing a cloud-free composite with L8
// by selecting the least-cloudy pixel from the collection.

// A mapping from a common name to the sensor-specific bands.
var LC8_BANDS = ['B2',   'B3',    'B4',  'B5',  'B6',    'B7',    'B10'];
var STD_NAMES = ['blue', 'green', 'red', 'nir', 'swir1', 'swir2', 'temp'];

// Compute a cloud score.  This expects the input image to have the common
// band names: ["red", "blue", etc], so it can work across sensors.
var cloudScore = function(img) {
  // A helper to apply an expression and linearly rescale the output.
  var rescale = function(img, exp, thresholds) {
    return img.expression(exp, {img: img})
        .subtract(thresholds[0]).divide(thresholds[1] - thresholds[0]);
  };

  // Compute several indicators of cloudyness and take the minimum of them.
  var score = ee.Image(1.0);
  // Clouds are reasonably bright in the blue band.
  score = score.min(rescale(img, 'img.blue', [0.1, 0.3]));

  // Clouds are reasonably bright in all visible bands.
  score = score.min(rescale(img, 'img.red + img.green + img.blue', [0.2, 0.8]));

  // Clouds are reasonably bright in all infrared bands.
  score = score.min(
      rescale(img, 'img.nir + img.swir1 + img.swir2', [0.3, 0.8]));

  // Clouds are reasonably cool in temperature.
  score = score.min(rescale(img, 'img.temp', [300, 290]));

  // However, clouds are not snow.
  var ndsi = img.normalizedDifference(['green', 'swir1']);
  return score.min(rescale(ndsi, 'img', [0.8, 0.6]));
};

// Filter the TOA collection to a time-range and add the cloudscore band.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
    .filterDate('2013-05-01', '2013-06-30')
    .map(function(img) {
      // Invert the cloudscore so 1 is least cloudy, and rename the band.
      var score = cloudScore(img.select(LC8_BANDS, STD_NAMES));
      score = ee.Image(1).subtract(score).select([0], ['cloudscore']);
      return img.addBands(score);
    });

// Define visualization parameters for a true color image.
var vizParams = {bands: ['B6', 'B5', 'B4'], max: 0.4, gamma: 1.6};

var ndvi = collection.qualityMosaic('cloudscore').normalizedDifference(['B5', 'B4']);
var ndvi_clip = ndvi.clip(Salt);

Map.addLayer(Salt, {min: 0.0, max: 1, palette: ['gray'],opacity:0.5}, 'Salt mask');
Map.addLayer(ndvi_clip,
        {min: 0.0, max: 1, palette: ['F0000F','00FF00','00FFFF'],opacity:0.5}, 'NDVI clipped to Salt');
0

There are 0 best solutions below