[Numpy-discussion] 2d binning on regular grid

Andreas Hilboll lists@hilboll...
Wed Jun 2 10:40:15 CDT 2010

```Hi there,

I'm interested in the solution to a special case of the parallel thread
'2D binning', which is going on at the moment. My data is on a fine global
grid, say .125x.125 degrees. I'm looking for a way to do calculations on
coarser grids, e.g.

* calculate means()
* calculate std()
* ...

on a, say, 2.5x3.75 degree grid. One very crude approach would be to
iterate through latitudes and longitudes, like this:

latstep_orig = .125
lonstep_orig = .125
data_orig =
np.arange((180./latstep_orig)*(360./lonstep_orig)).reshape((180./latstep_orig,360./lonstep_orig))
latstep_new = 2.5
lonstep_new = 3.75
latstep = int(latstep_new / latstep_orig)
lonstep = int(lonstep_new / lonstep_orig)

print 'one new lat equals',latstep,'new lats'
print 'one new lon equals',lonstep,'new lons'

result = ma.zeros((180./latstep_new,360./lonstep_new))
latidx = 0
while latidx*latstep_new < 180.:
lonidx = 0
while lonidx*lonstep_new < 360.:
m = np.mean( \
data_orig[latidx*latstep:(latidx+1)*latstep, \
lonidx*lonstep:(lonidx+1)*lonstep])
result[latidx,lonidx] = m
lonidx += 1
latidx += 1

However, this is very crude, and I was wondering if there's any more
elegant way to do it ...