Monthly rainfall

What month has most rain?

Step 1: import the chirps data

Step 2: Set the date variables

// set start and end year
var startyear = 2000;
var endyear = 2014;

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

Step 3: Set the geography

// Specify Country names
var country_names = ['Indonesia'];

// Get the country boundaries
var countries = ee.FeatureCollection('ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw');

// Filter for country
var Indonesia = countries.filter(ee.Filter.inList('Country', country_names));

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

Step 4: Calculate monthly precipitation

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()
);

Step 5: Calculate monthly precipitation

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(1, m, 1));
  }).flatten()
);

Step 5: Calculate monthly precipitation

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(1, m, 1));
  }).flatten()
);

Step 6: Create and plot the chart

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

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

print(chartMonthly);

See an example here

8 comments

  1. It seems the x axis or years are 1901 onwards though the datasets are from 2000 onwards, am I missing something here?

    1. Its monthly rainfall related to the period 2000 – 2014.
      the statement in step 5 results in the startdate of 1901 (.set(‘system:time_start’,ee.Date.fromYMD(1, m, 1));)
      you e.g. change it to the year 2000: (.set(‘system:time_start’,ee.Date.fromYMD(2000, m, 1));)

  2. Hello sir,
    After I change from 1 to 2000,2001,2002…. but the results are always the same. I have something wrong?
    Thank a lot sir

  3. Hi, do you have any example on how to do monthly rainfall anomaly calculation using chirps daily data?
    Lets say the step to be done is below:
    – Calculate monthly long-term average (LTA) using data from 1981 – 2017 as base period
    – Calculate monthly accumulation for Jan 2018 (example)
    – Calculate rainfall anomaly for Jan 2018 by dividing Rainfall Jan 2018 with LTA Jan

  4. I run the example code but it appears: Error generating chart: Collection.loadTable: Collection asset ‘ft:1tdSwUL7MVpOauSgRzqVTOwdfy17KDbw-1d9omPw’ not found. What’s wrong? Can anyone tell me please

Leave a Reply