I am trying to use NASA GLDAS temperature data for producing annual and long term statistics. Data is 3 hourly and I need to get the max value by day. Then I want to calculate the long term average of daily data (i.e.2010 to 2018). The code works for very short periods (some months) but fails with a longer time-series.
The error message is: User memory limit exceeded or Computation timed out.
May be there are better ways to write the code in order to avoid the error. Any help?
here is the link to GEE code https://code.earthengine.google.com/a83616530a8ed4af96dc4118328691e1
Here below the code
// USER SETTINGS //////////////////////////////////////////////////////////////////////////////
//
var ROI = geometry;
// Define time range
var startyear = 2010;
var endyear = 2018;
var startmonth = 1
var endmonth = 12
var startday = 1
var endday = 31
var vis = {min: 10.0,max: 30.0,palette: ['1303ff', '42fff6', 'f3ff40', 'ff5d0f'],};
var vis_D = {min: -2.0,max: 2.0,palette: ['1303ff', '42fff6', 'f3ff40', 'ff5d0f'],};
////////////////////////////////////////////////////////////////////////////////////////////////
var clip = function(img) {return img.clip(ROI);}
var collection = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
.filterBounds(ROI)
.map(clip)
.select('Tair_f_inst');
// Set date in ee date format
var startdate = ee.Date.fromYMD(startyear,startmonth,startday);
var enddate = ee.Date.fromYMD(endyear,endmonth,endday).advance(1, 'day');
// create list for years
var years = ee.List.sequence(startyear,endyear);
// create list for months
var months = ee.List.sequence(startmonth,endmonth);
// get days in month
function getDaysInMonth(y,m) {
var dt = ee.Date.fromYMD(y,m,1);
var n = dt.advance(1,"month").difference(dt,'day');
return ee.List.sequence(1,n);
}
// get days in month for AVG
function getDaysInMonth2(m) {
var dt = ee.Date.fromYMD(1970,m,1);
var n = dt.advance(1,"month").difference(dt,'day');
return ee.List.sequence(1,n);
}
//get projection of the dataset
var proj = ee.Image(collection.first()).projection();
// Filter data
var datain = collection.filterDate(startdate, enddate)
// .filter(ee.Filter.calendarRange(startmonth,endmonth, 'month'))
// convert to Celsius
var datain = datain.select('Tair_f_inst').map(function(image) {
return image.subtract(273.15).rename('tmp')
.set('system:time_start', image.get('system:time_start'))
.set('doy', image.date().format('YYYY-MM-dd'))
})
// print(datain,'datain')
// aggregate hourly to daily
var daily = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function(m) {
var days = getDaysInMonth(y,m)
return days.map(function(d) {
var filtered_Daily = datain.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.filter(ee.Filter.calendarRange(d, d, 'day_of_month'))
.max();
return filtered_Daily.set('year', y)
.set('month', m)
.set('day', d)
.set('date', ee.Date.fromYMD(y, m, d))
.set('system:time_start', ee.Date.fromYMD(y, m, d).millis());
});
});
}).flatten()
);
// print (daily,'daily');
// Map.addLayer(daily.first(), vis,'Air_Tmp');
var daily_LTA = ee.ImageCollection.fromImages(
months.map(function(m) {
var days = getDaysInMonth2(m)
return days.map(function(d) {
var filtered_Daily = datain
.filter(ee.Filter.calendarRange(m, m, 'month'))
.filter(ee.Filter.calendarRange(d, d, 'day_of_month'))
.mean()
.rename('tmp_LTA');
return filtered_Daily.set('year', 1970)
.set('month', m)
.set('day', d)
.set('date', ee.Date.fromYMD(1970, m, d))
.set('system:time_start', ee.Date.fromYMD(1970, m, d).millis());
});
}).flatten()
);
print (daily_LTA,'daily_LTA');
Map.addLayer(daily_LTA.first(), vis,'Air_Tmp_LTA');
Script can be pretty simple by using my package, and be highly efficient even for global application.
// USER SETTINGS //////////////////////////////////////////////////////////////////////////////
var pkg_trend = require('users/kongdd/pkgs:/pkgs.js');
function add_date(img){
var date = ee.Date(img.get('system:time_start'));
var date_daily = date.format('YYYY-MM-dd');
return img.set('date_daily', date_daily);
}
function k2C(img){
return img.subtract(273.15);
}
var clip = function(img) { return img.clip(ROI); };
/** END OF FUNCTIONS -------------------------------------------------------- */
// CREATE GLOBAL ROI
// var ROI = ee.Geometry.Polygon([-180, 90, 0, 90, 180, 90, 180, -90, 10, -90, -180, -90], null, false)
// Map.addLayer(ROI,{}, 'ROI')
// IF YOU DRAW A POLY OR POINT
var ROI = geometry;
// Set date in ee date format
var date_start = ee.Date('2016-01-01');
var date_end = ee.Date('2018-12-31');
var imgcol = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
// .filterBounds(ROI) // this not work
.filterDate(date_start, date_end)
.map(add_date)
// .map(clip)
.select('Tair_f_inst');
var imgcol_daily = pkg_trend.aggregate_prop(imgcol, "date_daily", 'mean')
.map(k2C);
// imgcol = pkg_trend.imgcol_addSeasonProb(imgcol);
print(imgcol.limit(3), imgcol.size());
print(imgcol_daily.limit(3), imgcol_daily.size());
var vis = { min: 10.0, max: 30.0, palette: ['1303ff', '42fff6', 'f3ff40', 'ff5d0f'], };
var vis_D = { min: -2.0, max: 2.0, palette: ['1303ff', '42fff6', 'f3ff40', 'ff5d0f'], };
/** VISUALIZATION ------------------------------------------------------------*/
// print (daily,'daily');
// Map.addLayer(daily.first(), vis,'Air_Tmp');
print(imgcol_daily.size(), 'daily_LTA');
Map.addLayer(imgcol_daily.first(), vis, 'Air_Tmp_LTA');
An another example can be found here.