0

I have written a basic code that can sum GDD for a specific date range for a single year as seen below:

var dataset = ee.ImageCollection('MODIS/006/MYD11A1')
                  .filter(ee.Filter.date('2017-04-15', '2017-05-20'));
var landSurfaceTemperature = dataset.select(['LST_Day_1km', 'LST_Night_1km']);

var tempToFahr = landSurfaceTemperature.map(function(image){
  var props = image.toDictionary(image.propertyNames());
  var Fahr = (image.multiply(0.02).subtract(273.15)).multiply(1.8).add(32);
  // Mask where one of Night or day temperature has no data
  var FahrMasks = Fahr.updateMask(Fahr.select('LST_Day_1km')).updateMask(Fahr.select('LST_Night_1km'));
  // we assume that the night temperature is the min temp, and the day temperature is the max temperature
  var meanFahr = FahrMasks.reduce('mean').rename('meanTemp');
  // Calculate the GGD and make all the negative values 0 (see: https://en.wikipedia.org/wiki/Growing_degree-day)
  var GDD = meanFahr.subtract(52.5).rename('GDD');
  var GDDnonNeg = GDD.where(GDD.lt(0), 0).rename('GDDnonNeg');
  return ee.Image(Fahr.addBands([meanFahr, GDD, GDDnonNeg]).setMulti(props));
});

// calculate the sum of GGD values
var summed = tempToFahr.select('GDDnonNeg').sum().rename('summedGDD');


var landSurfaceTemperatureVis = {
  min: 0,
  max: 15,
  bands: ['LST_Day_1km'],
  opacity: .6,
  palette: [
    '040274', '040281', '0502a3', '0502b8', '0502ce', '0502e6',
    '0602ff', '235cb1', '307ef3', '269db1', '30c8e2', '32d3ef',
    '3be285', '3ff38f', '86e26f', '3ae237', 'b5e22e', 'd6e21f',
    'fff705', 'ffd611', 'ffb613', 'ff8b13', 'ff6e08', 'ff500d',
    'ff0000', 'de0101', 'c21301', 'a71001', '911003'
  ],
};
Map.setCenter(-85.60371794450282,44.73590436363271, 10);
Map.addLayer(tempToFahr, landSurfaceTemperatureVis,'Land Surface Temperature', 1, 0.46);
Map.addLayer(summed.clip(features), {min: 0, max: 15, opacity: .6, palette: landSurfaceTemperatureVis.palette},'summed growing degree days', 1, 1);


// Export a cloud-optimized GeoTIFF.
Export.image.toDrive({
  image: summed,
  description: 'imageToCOGeoTiffExample',
  scale: 1000,
  region: features,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});

This works great for a specific date range in a single year, however I cannot for the life of me figure out a a way to take multiple date ranges over multiple years and then average them onto a single layer.

One suggestion I had seen earlier proposed this code:

// 1. add years in each image
var tempToFahr_Y  = tempToFahr.map(function(f) {
  var date = ee.Date(f.get("system:time_start"))
  var year=  date.format("Y")
  return f.set({date: date, year: year})
})


// 2. get years in data
var years_list = tempToFahr_Y.reduceColumns(ee.Reducer.frequencyHistogram(), ["year"]).get("histogram")
var years_dic = ee.Dictionary(years_list).keys()

// 3. now map over the years dic
var tempToFahr_Y_sum = years_dic.map(function(x) { 
  var res= tempToFahr_Y.filter(ee.Filter.eq("year", x))
                  .select('GDDnonNeg').sum();
  var res_name = ee.Image(res).rename(ee.String("summedGDD_").cat(ee.String(x)))
  return res_name
})

// 4. into one imageCollection
var tempToFahr_Y_sum1 = ee.ImageCollection.fromImages(tempToFahr_Y_sum)
print(tempToFahr_Y_sum1)

However, I cannot figure out how to enter multiple years and more importantly a specific date range within those years.

Any ideas?

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.