0

I am trying to figure out GDD over a period of multiple years given a period of time for each year, and the code provided below allows me to do that.

Now that I have 10 years of data points, I am stuck trying to use a reducer.mean function to average all of these variables into one final image layer.

So my basic question: how do i employ the reducer.mean function to average 10 or so variables?

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']);

// Create a geometry representing an export region.
var features = ee.Geometry.Rectangle([-85.1417893413635, 45.31413490213395, -86.125065708551, 44.65070625463291]);

// ######### ADDED CODE ############

// According to: https://gis.stackexchange.com/questions/307548/getting-temperature-data-of-given-point-using-modis-lst-data
// map over the image collection and use server side functions
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').reduce(ee.Reducer.sum()).rename('summedGDD');



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

// Create a geometry representing an export region.
var features2 = ee.Geometry.Rectangle([-85.1417893413635, 45.31413490213395, -86.125065708551, 44.65070625463291]);

// ######### ADDED CODE ############

// According to: https://gis.stackexchange.com/questions/307548/getting-temperature-data-of-given-point-using-modis-lst-data
// map over the image collection and use server side functions
var tempToFahr2 = landSurfaceTemperature2.map(function(image){
  var props2 = image.toDictionary(image.propertyNames());
  var Fahr2 = (image.multiply(0.02).subtract(273.15)).multiply(1.8).add(32);
  // Mask where one of Night or day temperature has no data
  var FahrMasks2 = Fahr2.updateMask(Fahr2.select('LST_Day_1km')).updateMask(Fahr2.select('LST_Night_1km'));
  // we assume that the night temperature is the min temp, and the day temperature is the max temperature
  var meanFahr2 = FahrMasks2.reduce('mean').rename('meanTemp2');
  // Calculate the GGD and make all the negative values 0 (see: https://en.wikipedia.org/wiki/Growing_degree-day)
  var GDD2 = meanFahr2.subtract(52.5).rename('GDD2');
  var GDDnonNeg2 = GDD2.where(GDD2.lt(0), 0).rename('GDDnonNeg2');
  return ee.Image(Fahr2.addBands([meanFahr2, GDD2, GDDnonNeg2]).setMulti(props2));
});

// calculate the sum of GGD values
var summed2 = tempToFahr2.select('GDDnonNeg2').reduce(ee.Reducer.sum()).rename('summedGDD2');


// ######### END ADDED CODE ############


Map.setCenter(-85.60371794450282,44.73590436363271, 10);


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

// Create a geometry representing an export region.
var features3 = ee.Geometry.Rectangle([-85.1417893413635, 45.31413490213395, -86.125065708551, 44.65070625463291]);

// ######### ADDED CODE ############

// According to: https://gis.stackexchange.com/questions/307548/getting-temperature-data-of-given-point-using-modis-lst-data
// map over the image collection and use server side functions
var tempToFahr3 = landSurfaceTemperature3.map(function(image){
  var props3 = image.toDictionary(image.propertyNames());
  var Fahr3 = (image.multiply(0.02).subtract(273.15)).multiply(1.8).add(32);
  // Mask where one of Night or day temperature has no data
  var FahrMasks3 = Fahr3.updateMask(Fahr3.select('LST_Day_1km')).updateMask(Fahr3.select('LST_Night_1km'));
  // we assume that the night temperature is the min temp, and the day temperature is the max temperature
  var meanFahr3 = FahrMasks3.reduce('mean').rename('meanTemp3');
  // Calculate the GGD and make all the negative values 0 (see: https://en.wikipedia.org/wiki/Growing_degree-day)
  var GDD3 = meanFahr3.subtract(52.5).rename('GDD3');
  var GDDnonNeg3 = GDD3.where(GDD3.lt(0), 0).rename('GDDnonNeg3');
  return ee.Image(Fahr3.addBands([meanFahr3, GDD3, GDDnonNeg3]).setMulti(props3));
});

// calculate the sum of GGD values
var summed3 = tempToFahr3.select('GDDnonNeg3').reduce(ee.Reducer.sum()).rename('summedGDD3');


// ######### END ADDED CODE ############

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);



var final = summed.mean(summed.add(summed2).add(summed3));

Map.addLayer(final.clip(features), {min: 0, max: 15, opacity: .6, palette: landSurfaceTemperatureVis.palette},'summed growing degree days_all', 1, 1);

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.