Moving average in Model Builder

1768
1
07-02-2015 12:36 AM
JanRussnak
New Contributor

Dear GIS experts,

I have many rasters of the same bounding box and I would like to calculate moving average from them. For example from 100 rasters I need to calculate average from rasters 1;2;3, then from 2;3;4 and so on till 98;99;100. Do you know, if it is possible to do it using Model Builder?

Many thanks !

0 Kudos
1 Reply
DanPatterson_Retired
MVP Emeritus

Could you clarify

  • Do you want a single number for the moving window
  • Do you want an output raster for the moving window that would have an average per sequence

This demo will exemplify the difference...(I have used numpy arrays to demonstrate)

Of course this can be greatly simplified is you use RasterToNumPyArray—Help | ArcGIS for Desktop to get the rasters into array format then use NumPyArrayToRaster—Help | ArcGIS for Desktop to get any needed back to raster format.  Mean operation can be put through a 'moving' sequence so that you don't have to manually perform any of the steps. 

So the result you need is important

import numpy as np
r1 = np.arange(9).reshape(3,3)
r2 = np.arange(1,10).reshape(3,3)
r3 = np.arange(2,11).reshape(3,3)
print("\ninputs\n{}\n{}\n{}".format(r1,r2,r3))
print("\nmoving average 1 {}".format(np.mean((r1,r2,r3))))
print("\nmoving,focal average\n{}".format(np.mean((r1,r2,r3),axis=0)))
​
Which yields
inputs
[[0 1 2]
[3 4 5]
[6 7 8]]

[[1 2 3]
[4 5 6]
[7 8 9]]

[[ 2  3  4]
[ 5  6  7]
[ 8  9 10]]

moving average 1 5.0

moving,focal average
[[ 1.  2.  3.]
[ 4.  5.  6.]
[ 7.  8.  9.]]