1

I'm trying to map the max number of consecutive days with rain <1 mm in Google Earth Engine.

This is the link to the code

https://code.earthengine.google.com/22b5c20d2700a2ffb5989f892838ac58

First I reclassify the collection with 0 if rain <=1 and 1 if >1. Then I run the code that should count the days of the longest dry period, but it is able to do so only if the dry period reach the end of the time period.

For instance if I am looking for the longest dry period in 4 days timestep i get the following series:

rain days 1 2 3 4   output
          0,0,1,1 = 0 dry days
          0,1,0,0 = 2 dry days
0 = rain<=1 and 
1 = rain>1 (as per the first step)

Does anyone can help in solving this? Thanks

Gianca
  • 109
  • 10

1 Answers1

2

I don't think you were far off in your code that you provided. To keep track of the dry spells you have to use .iterate(). I took a stab at your application in a little different way where instead of classifying the data before the iteration, I calculate which pixels are dry each day and carry over the accumulated days that a pixel is dry, otherwise it is set to zero:

// DATA
var collection = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY");

// Define time range
var startyear = 2000;
var endyear = 2017;

var startmonth = 1;
var endmonth = 12;

// Set date in ee date format
var startdate = ee.Date.fromYMD(startyear,startmonth,1);
var enddate = ee.Date.fromYMD(endyear,endmonth,31);

// Filter data
var datain_t = collection.filterDate(startdate, enddate)
  .filter(ee.Filter.calendarRange(startmonth,endmonth, 'month'))
  .select("precipitation").map(function(img){
     return img.addBands(ee.Image.constant(0).uint8().rename('counter'));
  })
  .sort('system:time_start');

// // START 
var dataset = datain_t
.filterDate("2016-08-01","2016-08-30")
.sort('system:time_start:');
print(dataset,"dataset");

var precipThresh = 1; // mm

function drySpells(img, list){
  // get previous image
  var prev = ee.Image(ee.List(list).get(-1));
  // find areas gt precipitation threshold (gt==0, lt==1)
  var dry = img.select('precipitation').lt(precipThresh);
  // add previous day counter to today's counter
  var accum = prev.select('counter').add(dry).rename('counter');
  // create a result image for iteration
  // precip < thresh will equal the accumulation of counters
  // otherwise it will equal zero
  var out = img.select('precipitation').addBands(
        img.select('counter').where(dry.eq(1),accum)
      ).uint8();
  return ee.List(list).add(out);
}

// create first image for iteration
var first = ee.List([ee.Image(dataset.first())]);

// apply dry speall iteration function
var maxDrySpell = ee.ImageCollection.fromImages(
    dataset.iterate(drySpells,first)
).max(); // get the max value

// display results
Map.addLayer(maxDrySpell.select('counter'),{min:0,max:30,palette:'#9ecae1,#ffffff,#ffeda0,#feb24c,#f03b20'},'Max Dry Spells');

Here is the link to the code: https://code.earthengine.google.com/80b4c0f7e82a5f0da316af1d2a55dd59

Don't try to run this analysis for too long of a time period or Earth Engine will give an error. I hope this helps!

Kel Markert
  • 807
  • 4
  • 12
  • i am trying to implement the code for calculating the yearly basis longest dryspell on a timeseries. I have some problems, here is the code:https://code.earthengine.google.com/e5fb2c81dc989e13969af880ca60942e. Any help? – Gianca Nov 15 '18 at 14:34
  • 1
    You were almost there! The `drySpells` function requires that each image in the image collection has an initial band called 'counter' with a value of 0. I just added a mapping function to the `datain_t` variable that add that band. Here is the code: https://code.earthengine.google.com/099394bec07695fd465908b578a1dce9 . Let me know if I can help with anything else! – Kel Markert Nov 15 '18 at 19:57
  • it works very super well, thanks!!!!!!!! just the last point (i hope), I would need that the band "counter' keeps the same scale than the original one 'precipitation'. Now they are different as shows here: https://code.earthengine.google.com/c5e2633b7ec611d1f7e602be639e9fff . Any help? thanks for your continuous support. – Gianca Nov 16 '18 at 12:24
  • Glad to help! You can try [exporting the resulting image in the projection needed](https://code.earthengine.google.com/cf0f21876757928436068e97c53a5a7c), but check this against a CHIRPS dataset to make sure that the computations are done at the scale needed. This next approach may be ill advised by the EE team because of how EE internally handles projections ([see this page](https://developers.google.com/earth-engine/projections)), but, you would explicitly set the projection when you create the counter band ([code here](https://code.earthengine.google.com/2c667a5bb14174b77204cab8911d6372)) – Kel Markert Nov 16 '18 at 16:38
  • You are very welcome! Let me know if I can help with anything else. – Kel Markert Nov 19 '18 at 16:14
  • Hi, I think you remember me. i have another issue about dry days. I need to count the number of dry periods (by year) longer than 5 days. i have tried to adapt this code but no way!! any smart suggestion? Thanks!!! – Gianca Jun 27 '19 at 11:43