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
//})