[GIS] Does gdal_calc only support 26 input raster files at a time

gdalgdal-calcrasterraster-calculator

In gdal_calc.py, input files are specified with letters A-Z.

Here are two files, input.tif and input2.tif, being averaged into result.tif:

gdal_calc.py -A input.tif -B input2.tif --outfile=result.tif --calc="(A+B)/2"

I have more than 26 files. What can I do about this?

One possibility: keep adding up files, 25 at a time, and then divide the values by the total number of input files. I think my numbers will become too large.

It looks like the source code for gdal_calc.py could be adapted to process as many images as needed, but the script is a mammoth nested looping function called doit(), so that's a bit scary. Should I really be hacking source code, or is there a better way?

My images are all single-band GeoTIFFs, by the way.

EDIT: After posting, I realized this question is somewhat of a duplicate of this rather incoherent one. Fortunately, the other question does have a good answer showing how to average more than 26 images together from the command line. The answer only works for a predetermined number of images, though. It would be nice to have a general solution that supports a theoretically unlimited number of images.

Best Answer

Do the averaging yourself?

GDAL makes it very easy (especially with the python bindings) to read and write raster pixel data. Read your input files in chunks (if they are large), take the average (very easy with python as you can read the input to numpy arrays then just use numpy to take the average across a given axis), and write it out again.

Related Question