2

I am still new to the Google Earth Engine and try to calculate the daily precipitation based on the CHIRPS. I have created a code to calculate monthly precipitation.

Could you advise me on how I change this code to daily precipitation?

var chirps = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY"),
geometry = /* color: #d63000 */ee.Geometry.MultiPoint();
    
// set start and end year
var startyear = 2018; 
var endyear = 2020; 

// make a date object
var startdate = ee.Date.fromYMD(startyear, 1, 1);
var enddate = ee.Date.fromYMD(endyear + 1, 1, 1);

// make a list with years
var years = ee.List.sequence(startyear, endyear);
// make a list with months
var months = ee.List.sequence(1, 12);

// 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, 6);

    
// Get the geometry
var country = country.geometry();


var monthlyPrecip =  ee.ImageCollection.fromImages(
  years.map(function (y) {
    return months.map(function(m) {
      var w = chirps.filter(ee.Filter.calendarRange(y, y, 'year'))
                    .filter(ee.Filter.calendarRange(m, m, 'month'))
                    .sum();
      return w.set('year', y)
              .set('month', m)
              .set('system:time_start', ee.Date.fromYMD(y, m, 1));
                        
    });
  }).flatten()
);


var meanMonthlyP =  ee.ImageCollection.fromImages(
  months.map(function (m) {
    var w = monthlyPrecip.filter(ee.Filter.eq('month', m)).mean();
    return w.set('month', m)
            .set('system:time_start',ee.Date.fromYMD(2018, m, 1)); 
  }).flatten()
);


var title = {
  title: 'Monthly precipitation',
  hAxis: {title: 'Time'},
  vAxis: {title: 'Precipitation (mm)'},
};


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

print(chartMonthly);

1 Answer 1

5

Following code produces a paired list of dates and daily precipitation values for exporting to Google Drive (for your filterCountry variable) and print a chartDaily for the same region.

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("UCSB-CHG/CHIRPS/DAILY")
  .filterDate(startDate, endDate);

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

print(list_dataset);

var getPrecipitation = function(image) {

  var value_precipit = ee.Image(image)
    .reduceRegion(ee.Reducer.first(), country)
    .get('precipitation');
  
  var precipit_mm = ee.Number(value_precipit); 

  return precipit_mm;
};

var count = dataset.size();

var precipit_list = dataset.toList(count).map(getPrecipitation);

print("precipitation list", precipit_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 = precipit_list.size();

print(len);

var paired = allDatesSimple.zip(precipit_list);

print (paired);

var title = {
  title: 'Daily precipitation',
  hAxis: {title: 'Time'},
  vAxis: {title: 'Precipitation (mm)'},
};

var chartDaily = ui.Chart.image.seriesByRegion({
  imageCollection: dataset, 
  regions: country,
  reducer: ee.Reducer.mean(),
  band: 'precipitation',
  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,
"precipitation", //my task
"GEE_Folder", //my export folder
"daily_precipit",  //file name
"CSV");

After running above code in GEE code editor, result can be observed in following image:

enter image description here

An extract of obtained CSV looks as follows:

system:index,date,value,.geo
0,2018-01-01,0.0,
1,2018-01-02,0.0,
2,2018-01-03,0.0,
3,2018-01-04,1.2671680450439453,
4,2018-01-05,0.0,
5,2018-01-06,0.0,
6,2018-01-07,0.0,
7,2018-01-08,0.0,
8,2018-01-09,0.0,
9,2018-01-10,0.0,
10,2018-01-11,0.0,
11,2018-01-12,0.0,
12,2018-01-13,1.3613150119781494,
13,2018-01-14,0.6806575059890747,
14,2018-01-15,1.7016438245773315,
15,2018-01-16,0.9700509905815125,
16,2018-01-17,0.0,
17,2018-01-18,1.940101981163025,
.
.
.
4
  • Thank you very much
    – Zara
    Apr 30, 2021 at 7:29
  • If this is the expected answer consider mark it as accepted. Thank you.
    – xunilk
    May 1, 2021 at 14:37
  • Hiii @xunilk, How to extract the time series for multipolygon geometry like code.earthengine.google.co.in/?asset=users/abhilashaanu92/… Jun 12, 2021 at 5:30
  • @ Abhilash Singh Chauhan If you have another question please post it. This space is exclusively for comments but, I can say that you have to map over each feature in your multipolygon geometry.
    – xunilk
    Jun 12, 2021 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.