Loop conditional in raster calculator

391
2
12-04-2019 07:23 PM
BennyIstanto
New Contributor II

Hello,

I have daily rainfall data in milimeters in GeoTIFF format with naming convention chirps_YYYYMMDD.tif (example chirps_20100101.tif), and I also have 1 raster of dry-spell with name dslr_chirps_20091231.tif and both raster have same spatial resolution and extent.

Then I would like to calculate dry-spell for 1 Jan 2010, IF(rainfall>1,dslr=0,dslr+1). Using raster calculator I can use this formula: Con("chirps_20100101.tif" > 1, "dslr_chirps_20091231.tif" == 0, "dslr_chirps_20101231.tif" + 1)

The raster output will be dslr_chirps_20180101.tif

Problem:
I have 10 years daily rainfall data and would like to calculate daily dry-spell for all the available data period.

How to loop the above calculation using model builder, when the output for each calculation will use as input for the next calculation?

0 Kudos
2 Replies
JohannesBierer
Regular Contributor

I don't know how to do this in the model builder - but with python maybe you could define your input data and output with the following code?

It's from: datetime - Iterating through a range of dates in Python - Stack Overflow 

from datetime import timedelta, date

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = date(2009, 12, 31)
end_date = date(2010, 12, 31)
for single_date in daterange(start_date, end_date):
    
    d = single_date.strftime("%Y%m%d")
    
    d1 = single_date+timedelta(days=1)
    d2 = d1.strftime("%Y%m%d")
    
    input1 = "DSLR_CHIRPS_{}.tif".format(d)
    input2 = "CHIRPS_{}.tif".format(d2)
    output = "DSLR_CHIRPS_{}.tif".format(d2)

    print("{} - {} - {}".format(input1, input2, output))
0 Kudos
BennyIstanto
New Contributor II

Thank you for your reply, I will try it.

0 Kudos