Reputation: 1
I am writing a code in Google Earth Engine (GEE) where I get the monthly precipitation from the NASA/GPM_L3/IMERG_MONTHLY_V06 between 2000 and 2021. Then, I build an image collection of 12 images which contains the highest observed value of each month for each pixel.
What I am trying to do is to create a new image collection which will contain the rank index of each image's value compared to the values of the other images.
So for example if the image collection gives this result: Jan: 0.25, Feb: 0.23 Mar: 0.45, ... Dec: 0.1
to get a new image collection which will contain the values: Jan: 2, Feb: 3, Mar: 1, ... Dec: 12
What I have done until now is this but I am stack in ranking:
var startDate = '2000-06-01';
var endDate = '2021-09-01';
var im_col = 'NASA/GPM_L3/IMERG_MONTHLY_V06';
var band = 'precipitation';
var dataset = ee.ImageCollection(im_col)
.filterDate(startDate, endDate)
.select(band)
.map(function(image) {
return image.set('system:time_start', image.get('system:time_start'));
});
var startYear = ee.Date(startDate).get('year');
var endYear = ee.Date(endDate).get('year');
var totalYears = endYear.subtract(startYear);
var addAttributes = function(image) {
var year = ee.Date(image.get('system:time_start')).get('year');
return image.set({
'year': year,
'n': totalYears
});
};
var datasetWithAttributes = dataset.map(addAttributes);
var yearRange = ee.List.sequence(startYear, endYear);
var monthlyMaxCollection = ee.Dictionary({});
for (var month = 1; month <= 12; month++) {
var maxImage = dataset
.filter(ee.Filter.calendarRange(month, month, 'month'))
.max();
monthlyMaxCollection = monthlyMaxCollection.set(ee.Number(month).format(), maxImage);
}
var monthlyMaxList = monthlyMaxCollection.values();
var monthlyMaxListCol = ee.ImageCollection.fromImages(monthlyMaxList);
print(monthlyMaxListCol);
Does anyone have any idea?
Upvotes: 0
Views: 76
Reputation: 1
This worked:
var arrayImage = monthlyMaxListCol.toArray();
var sortedArray = arrayImage.arraySort();
var numImages = monthlyMaxListCol.size();
var nthLowestCollection = ee.ImageCollection(ee.List.sequence(0, numImages.subtract(1)).map(function(n) {
var intN = ee.Number(n).toInt();
var nthLowest = sortedArray.arraySlice(0, intN, intN.add(1)).arrayProject([0]).arrayFlatten([['prec']]);
return nthLowest;
}));
Upvotes: 0