Select to view content in your preferred language

Mann Kendall Test

8175
12
Jump to solution
12-27-2014 01:09 AM
AhsanAbbas
New Contributor III

Hello Guys, I need to perform Mann Kendall test on 5 series raster data yearly basis, from 2007 to 2011.

if need to iterate pixel by pixel on raster cell values for calculating the sign. How can i do it... Below is code but i have problem with numpy...

import arcpy
import numpy
from arcpy import env


env.workspace = r"D:\PythonProject\Data"
rasterlist = arcpy.ListRasters()


for raster in rasterlist:
    print raster


n = len(rasterlist)
pairs = (n * ( n - 1)) / 2
print "Pairs to be formed: ", pairs


def pairsRasters(raster1, raster2):
    print raster1, " - ", raster2
    
    inRas1 = arcpy.Raster(raster1)
    lowerLeft1 = arcpy.Point(inRas1.extent.XMin,inRas1.extent.YMin)
    #cellSize = inRas.meanCellWidth
    my_array1 = arcpy.RasterToNumPyArray(inRas1,nodata_to_value=0)


    inRas2 = arcpy.Raster(raster2)
    lowerLeft2 = arcpy.Point(inRas2.extent.XMin,inRas2.extent.YMin)
    #cellSize = inRas.meanCellWidth
    my_array2 = arcpy.RasterToNumPyArray(inRas2,nodata_to_value=0)
    


for current in reversed(rasterlist):
    for previous in rasterlist:
        if current == previous:
            pass
        elif current > previous:
            pairsRasters(current, previous)
        else:
            pass

Suppose the two raster of similar extent, i need to calculate difference, if it's greater than 1 the value store in output raster would be 1, if less than 1 the value would be -1 and else zero. How can i iterate on pixel by pixel of two rasters. so i can save it into new raster. That's it..

Column 1
Column 2Column 3Column 4Column 5
50259810298
1229810210100
5766922110
9845255959
5898173154

Message was edited by: Ahsan Abbas

12 Replies
XanderBakker
Esri Esteemed Contributor

I made some minor changes to the code:

  • I put the program flow in the def main
  • changed the code to create the list of pairs
  • used str.format to create the output raster name (please note that if the inputs are .img, you can better strip the extension to get a valid output name)
  • the numpy.sign is executed on the entire array, not for each cell individually
  • I used the os.path.join to create the output path, name for the raster

To assign an output projection, you will probably have to set the output coordinate system in the environments. The numpy to raster conversion seems to honor this setting (arcpy.env.outputCoordinateSystem):

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

0 Kudos
AhsanAbbas
New Contributor III

I tried this method but i get error, one problem more, the spatial extent of output raster is different, mean height is different but mean cell width is same, i don't understand ?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Do all the input rasters have the same dimension (width, height, resolution and location)? If not, you will have to account for these differences.

0 Kudos