0

I have two image collections of MonthlyMean of MODIS LST Day and NIght. from this I am trying to extract the diurnal range by subtracting LST_Night from Lst_Day. I have managed to calculate monthly and annual mean LST for Day and Night but got stuck in the subtraction process. being a novice to coding hereby I would like to seek some help to get over this problem from the expert community. below is the code I have used :

//Scale to Kelvin and convert to Celsius, set image acquisition time.
var modLSTc = function(img) {
  return img
    .multiply(0.02)
    .subtract(273.15)
    .copyProperties(img, ['system:time_start']);
};

// Import LST image collection.
var modis_day = ee.ImageCollection("MODIS/006/MOD11A1")
                  .filterDate('2000-01-01', '2017-12-31')
                //.select('LST_Night_1km')
                .select('LST_Day_1km')
                //.mean()
                .filterBounds(geometry)
                .map(modLSTc);

//print(modis_day)

var months = ee.List.sequence(1, 12);
var years = ee.List.sequence(2000, 2017);

var byMonthYear_day = ee.ImageCollection.fromImages(
  years.map(function(y) {
    return months.map(function (m) {
      return modis_day
        .filter(ee.Filter.calendarRange(y, y, 'year'))
        .filter(ee.Filter.calendarRange(m, m, 'month'))
        .mean()
        .set('month', m)
        .set('year', y)
        .set('system:time_start', ee.Date.fromYMD(y,m,1).millis())
  }); 
}).flatten());
//print(byMonthYear_day)


var modis_night = LST.filterDate('2000-01-01', '2017-12-31')
                .select('LST_Night_1km')
                //.select('LST_Day_1km')
                //.mean()
                .filterBounds(geometry)
                .map(modLSTc);

//print(modis_day);

var byMonthYear_night = ee.ImageCollection.fromImages(
  years.map(function(y) {
    return months.map(function (m) {
      return modis_day
        .filter(ee.Filter.calendarRange(y, y, 'year'))
        .filter(ee.Filter.calendarRange(m, m, 'month'))
        .mean()
        .set('month', m)
        .set('year', y)
        .set('system:time_start', ee.Date.fromYMD(y,m,1).millis())
  }); 
}).flatten());
//print(byMonthYear_night)

////////Subtraction between image collections image by image///////////////////
// First make a list of a useful numerical property of the image collection 
// and make sure they are ordered by time
var lstday = byMonthYear_day.sort('system:time_start');
var lstnight = byMonthYear_night.sort('system:time_start');

var dateListday = ee.List(lstday.aggregate_array('system:time_start'));//.slice(1); // remove the first date
var dateListnight = ee.List(lstnight.aggregate_array('system:time_start'));
// Now map over that list, so we now the index of each image
var LSTdiffday = ee.ImageCollection.fromImages(dateListday.map(function(date){

  // get the image corresponding to the date
  var currentImageDay = lstday.filter(ee.Filter.eq('system:time_start', date)).first();
      return currentImageDay;
}));

//print(LSTdiffday)

var LSTdiffnight = ee.ImageCollection.fromImages(dateListnight.map(function(date){

  // get the image corresponding to the date
  var currentImageNight = lstnight.filter(ee.Filter.eq('system:time_start', date)).first();
      return currentImageNight;
}))

//print(LSTdiffnight)

// Subtract the current image from the previous
  var diffImage = LSTdiffday.subtract(LSTdiffnight); //.select(['NDVI'], ['NDVIdiff'])
//    return diffImages
//})
0

Your code isn't complete so I can't test, but you can probably just combine() the two collections to get the same number of images but with two bands. Then you just map over that and subtract band-1 from band-0.

var result = byMonthYear_day.combine(byMonthYear_night)
    .map(function(img) {
        return img.select(0).subtract(img.select(1))
           .copyProperties(img, ['system:time_start']);
    })

All that said, it would have just been easier if you hadn't bothered to separate the two bands to begin with; the processing you're applying to them is the same, so just do it once, with both bands, by adding the night band to the first select()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.