4

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.

  • 1
    There is a general solution: Buy ArcGis.. use Raster Calculator. There is a reason why so many people buy ArcGis rather than using free GDAL tools. It's that or lean to program/script GDAL and do the hard work yourself... I think you're looking a gift horse in the mouth here. – Michael Stimson May 29 '15 at 0:19
  • 2
    GDAL developers may be busy but they are friendly too. I suggest to ask a question from the gdal-dev mailing list and if that limit is confirmed to be hardcoded to make a feature request to GDAL trac trac.osgeo.org/gdal. You can also send your own fixes as a patch for review. – user30184 May 29 '15 at 5:06
  • note that you can use OTB (Bandmath) instead of gdalcalc; without restriction on the number of bands. No need to buy ArcGIS. see orfeo-toolbox.org/CookBook/Applications/app_BandMathX.html – radouxju Apr 20 '17 at 9:23
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.