I have modified code from Google Earth Engine - counts days of the longest dry period in a time period to look at the longest consecutive number of days without rain in an image collection.
I am now trying to modify this analysis. I have a set of prescribed fire records (FeatureCollection) and I am trying to calculate the number of consecutive days without rain prior to the day of the fire for each record.
Below is my current working script, which is set to just calculate the longest consecutive number days without rain in the entire time series, relative to the start and end of the image collection.
// REFERENCE
// Center the map to an area of interest (ex. Tall Timbers)
Map.setCenter(-84.20875, 30.6565, 12)
// REFORMAT DATES FOR ALL POINTS IN THE SHAPEFILE
var BurnPermits = BurnPermits.map(function (feature) {
return feature.set('system:time_start', ee.Date(feature.get('DATE')))
})
// FILTER TO 2020 FOR TESTING
var Permits2020 = BurnPermits.filterDate('2020-01-01','2020-12-30')
// PRINT 2020 PERMITS TO CONSOLE
print(Permits2020)
// FILTER GRIDMET DATA TO DESIRED SPATIAL AND TEMPORAL EXTENTS
var GRIDMET = GRIDMET.select('pr').filterBounds(TT).filterDate('2020-01-01','2020-12-30')
// THIS FUNCTION ADDS A BAND FOR COUNTING NO RAIN DAYS
var addBands = function(image) {
return image.addBands(ee.Image.constant(0).uint8().rename('counter'));
};
// APPLY THE FUNCTION TO THE GRIDMET IMAGE COLLECTION
var GRIDMET = GRIDMET.map(addBands)
print(GRIDMET)
// SET A PRECIPITATION THRESHOLD (mm)
var PR_BIN = 1;
function PrecipFreeInterval(img, list){
// get previous image
var prev = ee.Image(ee.List(list).get(-1));
// find areas less than FIRE_BIN threshold (gt==0, lt==1)
var PrecipFree = img.select('pr').lt(PR_BIN);
// add previous day counter to today's counter
var accum = prev.select('counter').add(PrecipFree).rename('counter');
// create a result image for iteration
// PrecipFree < thresh will equall the accumulation of counters
// otherwise it will equal zero
var out = img.select('pr').addBands(
img.select('counter').where(PrecipFree.eq(1),accum)
).uint8();
return ee.List(list).add(out);
}
// CREATE FIRST IMAGE FOR ITERATION
var first = ee.List([ee.Image(GRIDMET.first())]);
// APPLY DRY SPELL ITERATION FUNCTION
var LPFI = ee.ImageCollection.fromImages(
GRIDMET.iterate(PrecipFreeInterval,first)
).max(); // GET THE MAX VALUE
// DISPLAY RESULTS
Map.addLayer(LPFI.select('counter').clip(TT), {},'Longest Precipitation Free Interval');
Functionally, I am trying to write a function that iterates over each feature in the collection, finds the start date of the feature, and then counts back until rain has been detected (in the GRIDMET product) and records the consecutive days without rain for that feature.
Would anyone be able to steer me in the right direction here? I know that the function has to iterate over features instead of images, I am just not sure the best way to accomplish this task.