I am able to remove null value from a list using the following methods:
print(ee.List([1,7, null, 3,5]).filter(ee.Filter.gt('item',0)))
print(ee.List([1,7, null, 3,5]).filter(ee.Filter.neq('item', null)))
However, I got the error ImageCollection (Error) List.get: List is empty (index is 1).
in my code.
The daily LSTs are put into a list and I am trying to extract the second largest LST for study years.
What I am doing wrong here?
Thanks for your help in advance.
Here is the code:
var startDate = ee.Date('2001-01-01'); // set start time for analysis
var endDate = ee.Date('2001-12-31'); // set end time for analysis
// calculate the number of year to process
var nyears = ee.Number(endDate.difference(startDate,'year'));
//init a time band
var createTimeBand= function(image) {
return image.addBands(image.metadata('system:time_start')
.divide(1e18))
// .divide(1000*60*60*24*365))
}
var sst = ee.ImageCollection('MODIS/006/MOD11A1').select('LST_Day_1km')
.filterDate(startDate, endDate)
//.map(createTimeBand)
var byyearMin = ee.ImageCollection(
// map over each month
ee.List.sequence(0,nyears).map(function (n) {
// calculate the offset from startDate
var ini = startDate.advance(n,'year');
// advance just one month
var end = ini.advance(1,'year');
var sortedDays = sst.filterDate(ini,end)
.sort('LST_Day_1km').filter(ee.Filter.neq('item', null))//.filter(ee.Filter.neq('item', ""))
.toList(sst.size())
//print(ee.List(sst))
//var sortedDays_clean = sortedDays.replaceAll(" ", 0)
//var sortedDays_clean = sortedDays.filter(ee.Filter.gt('item',1))
var secondLargest = ee.List(sortedDays.get(1))
//var secondSmallest = ee.List(sortedDays.get(-1))
var collection = ee.Image(secondLargest)
//.addBands(secondSmallest).rename(['secondLargest', 'secondSmallest'])
.multiply(0.02)
.subtract(273.15)
return collection
.set('system:time_start', ini.millis()) //convert time to number
.set('Date', ee.Date(ini))
}));
print(byyearMin)
Updated Answer:
I did a little more research and was able to come up with a solution by using array sorting to select the second highest pixel value in a sorted array of pixel values for every pixel in the image. The developer documentation for Array Sorting and Reducing can be found here. Since this is a pixel level calculation it is very resource intensive to run, so for the purpose of demonstrating I constrained the code to a smaller area. If you plan on running this for the entire world, I highly recommend running this as an export task and be prepared to break your image into grids in the event that you run into memory errors.
You can see the script is working with a maximum value of 42.69
at the demo point, found from your original max method, and a second highest value of 41.09
at the demo point from the array sort method. The full code is found here:
https://code.earthengine.google.com/6aac0b43298bf93dc04d597733f022c2
Original Response:
Not enough reputation to comment, so I'll post it as an answer:
I am unsure why you need to filter null items in the list. There is no 'item' property in your image collection, so running this filter would give you an empty image collection as a result, which is why you're seeing an empty list error. Running your script without the filters on Line 26 works just fine.
Additionally, there is no 'LST_Day_1km' property on each image, so your .sort()
is not actually sorting the images by the values in the 'LST_Day_1km' band like you are expecting. If you are looking for the 2nd largest LST, you will need to reduce each image to a value for which to sort by since the LST varies over the entire image (i.e. min LST, mean LST).