I don't believe you can do it directly through gdal_calc
unfortunately (though I'm happy to be proved wrong). However you can use the gdal_calc.py script as a basis to read the data into Python and do the operation through scipy.ndimage which has a pile of inbuilt focal filter operations. Unfortunately it doesn't have a mean filter, but if you're happy with median you could use:
result = scipy.ndimage(your_raster_as_numpy_array, size=3)
Alternately if you do need the mean you could use a convolution filter and divide the result by the number of cells covered:
kernel = numpy.ones((3,3))
result = scipy.ndimage.convolve(your_raster_as_numpy_array, weights=kernel) / kernel.size
You should then be able to write that back out to a file in the same way the gdal_calc script does.
A couple of notes though:
- Edge effects are still going to be an issue. Happily the filter methods in scipy.ndimage have a number of ways to deal with these
- Large rasters may be problematic - you might need to read them in by chunks and process them that way. You will have to deal with edge overlaps though, so that you avoid even more edge effects
- No data gaps are likely to be a pain. You will have to think about how you're going to deal with holes in your raster, either by interpolating to fill the gaps, or by setting the nodata values to
numpy.NaN
, which you will then have to interpret when writing back out through gdal
Good luck, and hope it helps!