Search code examples
google-earth-engine

Date of max and min value extraction from time series using Google Earth Engine


I'm trying to extract the day of maximum and minimum value of time series. The day of minimum value must be after the day of the max value. The day of max can be get by using ".qualityMosaic" in a function. I have troubles in make a working function for the detection of the date if the min.

How can I implement the right function?

/////////THESE FUNCTIONS WORKS FOR A SINGLE YEAR (EXAMPLE)
var max = example.qualityMosaic('precipitation').select('day');
print(max,'max')

var min = example.map(
  function(img) {
  var date = img.date().millis()
  return img.updateMask(max.lt(date))
}).select('precipitation', 'day').reduce(ee.Reducer.min(2)).rename('precipitation','day')
print(min,'min')


///////////HERE IS AS I HAVE IMPLEMENTED FOR A TIME-SERIES WITH > 1 YEAR
// Find the day of max for each year
var max = ee.ImageCollection(
    years.map(function (y) {
      var start = ee.Date.fromYMD(y, startmonth,startday);
      var stop = ee.Date.fromYMD (y,endmonth,endday);
      var x = collection.filterDate(start, stop)
      var w = x.qualityMosaic('precipitation').select('day')
    return w.set({'year': y})
}).flatten());
print(max,'max')
Map.addLayer(max, {min: 1, max: 365}, 'max')



// Find the day of min after day of max for each year
var min = ee.ImageCollection(
    years.map(function (y) {
      var start = ee.Date.fromYMD(y, startmonth,startday);
      var stop = ee.Date.fromYMD (y,endmonth,endday);
      var x = collection.filterDate(start, stop)
      var z = max.filter(ee.Filter.calendarRange(y, y, 'year'))
      var w = x.map(
        function(img) {
        var date = img.date().millis()
        var k = img.updateMask(z.lt(date))
      return k
}).select('precipitation', 'day').reduce(ee.Reducer.min(2)).rename('precipitation','day')
  return w
}).flatten());

print(min,'min')
Map.addLayer(min, {min: 1, max: 365}, 'min')

It looks quite complex because for the detection of the date of min for each of the year i need to use the respective date of max calculated in the previous step.

here is the link to the code https://code.earthengine.google.com/03e80671a65c95a60535dbd1c50ebe93

Any suggestion? thanks!!


Solution

  • This could work https://code.earthengine.google.com/6a035a4a301f5bebcf7f40d4be9a551c Changed line 119

    var z = ee.Image(max
        .filterMetadata('year', 'equals', y).first());