Calculate Slope Between Pixels Through Time: A Time Series Trend Analysis

Discussion created by mbs7038 on May 26, 2014
I have a 9 year weekly time series (~500 raster grids of equal cell size and extent). I'm interested in obtaining the regression line slope between pixels (Imagine stacking all 500 grids on top of one another and running a linear regression between each individual pixel). Essentially, this would measure the net change between pixels through my time series.

My main question is about the coefficients defined in the first answer here. Is the coefficient defined as 12 due to the temporal scale of the original question being at the monthly level? And if so, should my code's coefficient be set to 7 due to the weekly temporal scale between my grids?

I have been referencing these posts that are similar:

Representing trends over time:

Make linear regression across multiple layers:

I wrote this python code after reading the links referenced above:
from __future__ import division
import arcpy
import os
from arcpy import env
from arcpy.sa import*

# define workspace

# enable overwriting

# check spatial analyst extension

# define output paths

# list all rasters in the workspace
# sort rasters numerically

# get the number of rasters
print n

# setup index

# define division
print 'the global seed is {0}'.format(seed)

for raster in rasters:
    print i

    print 'Raster {0} is {1}:'.format(i,raster)
    print 'the coef for raster {0} is {1}'.format(i,coef)

    # Multiply raster by coefficient and add to output slope grid
    if i==1:
        i+=1  # same as saying i=i+1
        print 'adding {0} to outSlope'.format(raster)
    if i==6:

# Save final slope grid
print 'saving final slope grid'
outSlope.save(slopePath + "/" + "floodChange_py")

print 'script is complete'

I created this code as a test which appeared to work on a 5 week subset of my data. If what I have referenced above is correct then this code will work on a time series (of equal cell size and extent) of any length. Although there were no bugs in the code, I have a feeling I may have made a mistake someplace and would appreciate extra eyes on my code.

Any advice or recommendations?

On a side note I'm using PyScripter, hence the "from future import division" in the first line. This disables the automatic floor function which rounds floating point numbers to the nearest integer away from zero.