How to Calculate Dry and Wet Spell in Python?
Question: I have a random time series data with four columns like: year, month, day,
precipitation. I want to calculate dry/wet spell for different spell-length. I
am looking for a more convenient way to do that while currently doing with
some ugly codes like below:

    
    
    import numpy as np
    data = np.loadtxt('Data Series.txt', usecols=(1,3))
    dry = np.zeros(12)
    wet = np.zeros(12)
    
    rows,cols = data.shape #reading number of rows and columns into variables
    
    for i in xrange (0,rows):
        for m in xrange(0,12):
            if data[i,1] == 0 and data[i-1,1] == 0 and data[i-2,1] == 0:
                if data[i,0] == m+1:
                    dry[m] += 1.0
            if data[i,1] > 0 and data[i-1,1] > 0 and data[i-2,1] > 0:
                if data[i,0] == m+1:
                    wet[m] += 1.0
    print '3 Days Dry Spell\n', dry
    print '3 Days Wet Spell\n', wet
    

Now, if I want to calculate the 4,5,6 days spells, than "if data[i,1] == 0 and
data[i-1,1] == 0....." becomes a huge one. Can anyone please help me so that I
may give the spell-length only instead of this long-ugly line?


Answer: You may want to try something like this:

    
    
    # first extract precipitation data for later use
    precipitation = [data[i][1] for i in xrange(0, rows)]
    
    # then test the range (i, i+m)
    all_dry = all([(data==0) for data in precipitation[i:i+m]])
    all_wet = not any([(data==0) for data in precipitation[i:i+m]])
    # of course you can also use
    all_wet = all([(data>0) for data in precipitation[i:i+m]])
    

However please notice this method introduces redundant calculations when
testing adjacent days, and thus may be not suitable for processing a large
amount of data.

**EDITED:**

Okay this time let us look for a more efficient method.

    
    
    # still extract precipitation data for later use first
    precipitation = [data[i][1] for i in xrange(0, rows)]
    
    # let's start our calculations by counting the longest consecutive dry days 
    consecutive_dry = [1 if data == 0 else 0 for data in precipitation]
    for i in xrange(1, len(consecutive_dry))
        if consecutive_dry[i] == 1:
            consecutive_dry[i] += consecutive_dry[i - 1]
    
    # then you will see, if till day i there're m consecutive dry days, then:
    consecutive_dry[i] >= m    # here is the test
    
    # ...and it would be same for wet day testings.
    

This is obviously more efficient then the method above: for testing a total of
N days with M consecutive range, the previous one requires O(N * M) operations
to calculate and this one requires O(N).

**AGAIN EDITED:**

This is an edited version of your original code. As your code could run, this
shall also run on your PC or what.

    
    
    import numpy as np
    data = np.loadtxt('Data Series.txt', usecols=(1,3))
    dry = np.zeros(12)
    wet = np.zeros(12)
    
    rows,cols = data.shape #reading number of rows and columns into variables
    
    # prepare 
    precipitation = [data[i][1] for i in xrange(0, rows)]
    
    # collecting data for consecutive dry days
    consecutive_dry = [1 if data == 0 else 1 for data in precipitation]
    for i in xrange(1, len(consecutive_dry))
        if consecutive_dry[i] == 1:
            consecutive_dry[i] += consecutive_dry[i - 1]
    
    # ...and for wet days
    consecutive_wet = [1 if data > 0 else 0 for data in precipitation]
    for i in xrange(1, len(consecutive_wet))
        if consecutive_wet[i] == 1:
            consecutive_wet[i] += consecutive_wet[i - 1]
    
    # set your day range here. 
    day_range = 3
    
    for i in xrange (0,rows):
        if consecutive_dry[i] >= day_range:
            month_id = data[i,0]
            dry[month_id - 1] += 1
        if consecutive_wet[i] >= day_range:
            month_id = data[i,0]
            wet[month_id - 1] += 1
    
    print '3 Days Dry Spell\n', dry
    print '3 Days Wet Spell\n', wet
    

Please try this and let me know if there're any problems.

