I'm trying to average 30 greyscale netCDF files pixel-by-pixel using gdal_calc.py. However the script seems to require the input files to be enumerated with letters A-Z, so I'm four letters short.

The docs for gdal_calc lead me to believe I can instead supply a single raster file with all the bands and run an expression on that, but I haven't been able to find the correct syntax.

The closest I've come has been with this command ...

gdal_calc.py -A stacked.vrt --outfile=mean.nc --calc="mean(A, axis=0)"

... which produced a weird blocky-looking result unfortunately:

enter image description here

Has anyone done this before? Ideally I'm looking for a solution that would also work if it were more than 30 input files/bands.

  • 1
    Duplicate, but unanswered here: gis.stackexchange.com/questions/149006/… – phloem Apr 26 '18 at 22:40
  • 2
    I agree with the answer at the other question: do the averaging yourself. Either read all your rasters in as an NxMx30 array and use np.mean(array, axis=2), or if they're too big you can just read each one iteratively and update a "sum" raster. – Jon Apr 26 '18 at 23:01
  • You could read the rasters using GDAL in python (or C# or C++ even better) into a 30 x width array, row by row and output an average row by row. Another neat trick would be to average the first 15 bands, the second 15 bands and then average that - as long as your average is mean and not median, I don't think the math would work the same for median average (it's been a long time since high school statistical math for me). – Michael Stimson Apr 26 '18 at 23:21
  • Thanks for the tips -- if this is indeed impossible with the gdal shell scripts I'll do the averaging myself in Numpy. However I'd prefer to have confirmation on that first, and ideally an explanation on why the example code I posted doesn't work – zinfandel Apr 27 '18 at 6:51
  • When creating the VRT for the input to your workaround script, did you use the -separate GDAL creation option (places each raster going in to the VRT into a different band)? – Andy Harfoot Oct 26 '18 at 15:24

Your Answer

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

Browse other questions tagged or ask your own question.