4

I am still new to the Google Earth Engine and try to calculate the daily max and min temperature based on the GLDAS. Could you advise me on how I correct this code to calculate daily max and min temperature?

var startDate = ee.Date('2018-01-01');
var endDate = ee.Date('2021-01-01');

// Specify Country names
var worldcountries = ee.FeatureCollection('FAO/GAUL/2015/level2');

var filterCountry = ee.Filter.eq('ADM2_NAME', 'Kurgantepa district');

var country = worldcountries.filter(filterCountry);

Map.addLayer(country);
Map.centerObject(country, 10);

var dataset = ee.ImageCollection("NASA/GLDAS/V021/NOAH/G025/T3H")
  .select('Tair_f_inst')
  .filterDate(startDate, endDate);

var list_dataset = dataset.toList(dataset.size());

print(list_dataset);


var getTemperature = function(image) {

  var value_temperat = ee.Image(image)
    .reduceRegion(ee.Reducer.first(), country)
    .get('Tair_f_inst');

var temperat_K = ee.Number(value_temperat); 

  return temperat_K;
};

var count = dataset.size();

var temperat_list = dataset.toList(count).map(getTemperature);

print("Tair_f_inst list", temperat_list);

var allDates = ee.List(dataset.aggregate_array('system:time_start'));

var allDatesSimple = allDates.map(function(date){
  return ee.Date(date).format().slice(0,10);
  });

print(allDatesSimple);

var len = temperat_list.size();

print(len);

var paired = allDatesSimple.zip(temperat_list);

print (paired);

var title = {
  title: 'Daily Temperature',
  hAxis: {title: 'Time'},
  vAxis: {title: 'Temperature (K)'},
};

var chartDaily = ui.Chart.image.seriesByRegion({
  imageCollection: dataset, 
  regions: country,
  reducer: ee.Reducer.mean(),
  band: 'Tair_f_inst',
  scale: 5000,
  xProperty: 'system:time_start',
  seriesProperty: 'SITE'
}).setOptions(title)
  .setChartType('ColumnChart');

print(chartDaily);

var myFeatures = ee.FeatureCollection(paired.map(function(el){
  el = ee.List(el); // cast every element of the list
  var geom = country;
  return ee.Feature(null, {
    'date': ee.String(el.get(0)),
    'value':ee.Number(el.get(1))
  });
}));

//print(myFeatures);

// Export features, specifying corresponding names.
Export.table.toDrive(myFeatures,
"Temperature", //my task
"GEE_Folder", //my export folder
"daily_temperat",  //file name
"CSV");
5

GLDAS products have images with climatic elements for each three hours so, you will have 8 values per day of temperatures. In this case, you need to map your temperatures list for slicing each eight values. In these slices, you can apply a min reducer for minimum temperatures and a max reducer for maximum temperatures. However, produced temperatures list must be paired with distinct dates list (not with original dates list with 8 times values). Following script works as expected.

var startDate = ee.Date('2018-01-01');
var endDate = ee.Date('2021-01-01'); 

// Specify Country names
var worldcountries = ee.FeatureCollection('FAO/GAUL/2015/level2');

var filterCountry = ee.Filter.eq('ADM2_NAME', 'Kurgantepa district');

var country = worldcountries.filter(filterCountry);

Map.addLayer(country);
Map.centerObject(country, 10);

var dataset = ee.ImageCollection("NASA/GLDAS/V021/NOAH/G025/T3H")
  .select('Tair_f_inst')
  .filterDate(startDate, endDate);

var list_dataset = dataset.toList(dataset.size());

print(list_dataset);

var getTemperature = function(image) {

  var value_temperat = ee.Image(image)
    .reduceRegion(ee.Reducer.mean(), country)
    .get('Tair_f_inst');

  return value_temperat;

};

var count = dataset.size();

var temperat_list = dataset.toList(count).map(getTemperature);

//print("Tair_f_inst list", temperat_list);

var len = temperat_list.size();

var list = ee.List.sequence(0, len.subtract(1), 8);

var temp_list = list.map(function(ele){

  var start = ee.Number(ele).int(); 
  var end = ee.Number(ele).add(8).int(); 

  var new_list = ee.List([]);
  var element = ee.List(temperat_list.slice(start, end)).reduce(ee.Reducer.min());

  new_list = new_list.add(element);
  
  var element2 = ee.List(temperat_list.slice(start, end)).reduce(ee.Reducer.max());

  new_list = new_list.add(element2);

  return new_list;

});

print("temp_list", temp_list);

var allDates = ee.List(dataset.aggregate_array('system:time_start'));

var allDatesSimple = allDates.map(function(date){
  return ee.Date(date).format().slice(0,10);
  }).distinct();

print("distinct dates list", allDatesSimple);

var len = temperat_list.size();

//print(len);

var paired = allDatesSimple.zip(temp_list);

print ("paired list", paired);

var title = {
  title: 'Daily Temperature',
  hAxis: {title: 'Time'},
  vAxis: {title: 'Temperature (K)'},
};

var chartDaily = ui.Chart.image.seriesByRegion({
  imageCollection: dataset, 
  regions: country,
  reducer: ee.Reducer.mean(),
  band: 'Tair_f_inst',
  scale: 5000,
  xProperty: 'system:time_start',
  seriesProperty: 'SITE'
}).setOptions(title)
  .setChartType('ColumnChart');

//print(chartDaily);

var myFeatures = ee.FeatureCollection(paired.map(function(el){
  el = ee.List(el); // cast every element of the list
  var geom = country;
  return ee.Feature(null, {
    'date': ee.String(el.get(0)),
    'Tmin':ee.Number(ee.List(el.get(1)).get(0)),
    'Tmax':ee.Number(ee.List(el.get(1)).get(1))
  });
}));

//print(myFeatures);

// Export features, specifying corresponding names.
Export.table.toDrive(myFeatures,
"Temperature", //my task
"GEE_Folder", //my export folder
"daily_temperat",  //file name
"CSV");

After running above script in GEE console editor, you can see an extract of obtained CSV as follows:

system:index,Tmax,Tmin,date,.geo
0,283.12159660270623,274.85638262774495,2018-01-01,
1,283.22070862366274,274.42199638297967,2018-01-02,
2,283.75842133943024,275.1772416948198,2018-01-03,
3,281.7471648894989,275.3844425785649,2018-01-04,
4,280.8100086933858,274.43567458144184,2018-01-05,
5,282.165957064242,273.98338400351037,2018-01-06,
6,282.7080238960885,275.0355672750387,2018-01-07,
7,281.14597712336354,274.04015618401604,2018-01-08,
8,280.4689321432028,273.21697956806906,2018-01-09,
9,281.3570821951102,273.7503255208333,2018-01-10,
10,275.70246337340757,273.3154177279086,2018-01-11, 
.
.
.
2
  • Thank you very much xunilk – Zara May 1 at 16:32
  • You're welcome. – xunilk May 1 at 16:33

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.