# Calculate multiple bands average raster in QGIS with high number of bands

I am a new user of raster layers in QGIS.

I need to generate a new raster file with the average value for each pixel, from a raster file with 600 bands. The average has to be calculated from all the 600 bands, for each pixel.

The Raster Calculator only allows to add one band at a time, that would mean too much time and a risky process, considering that I have several similar raster layers that I have to work with.

Is there a way to add multiple bands at the same time in Raster Calculator in order to calculate it? Otherwise, is there another way to do it?

I tried also with RASS r.series but the output does not seem to be what I need, the layer is not shown in the canvas and the values are not what I expect to be (the data is about temperature)

In case someone faces the same situation, I solved it generating the calculation expression with Excel and Word. I had to make a sum of each of 600 bands from the same layer. First, with Excel:

Column A: make 600 rows with the same layer name.

Column B: make a column with the band's numbers (from 1 to 600)

Column C: Concatenate columns A and B

The " symbol at the beginning and the @ at the end of the layer name, as well as the " and + symbols at the end of the concat function are necessary in order to identifying the bands and summing them, in Qgis.

Then, in Word

1) Paste the generated rows in a new document

2) Search and replace the full stops after each band with a space or with nothing.

3) Delete the last + symbol in the last band.

Back In Qgis Raster Calculator, paste the whole expression.

• I correct myself, there is no need to do the Word steps. You can paste directly the Excell cells from the CONCAT column to the raster calculator. Commented Oct 29, 2019 at 18:07

You can use python to build the expression. This should work for any number of bands:

``````rasterlayer = QgsProject.instance().mapLayersByName('infra-red')[0] #Change infra-red to the name of your layer
bandcount = rasterlayer.bandCount() #Count the bands

tempexp = ['"{0}@{1}"'.format(rasterlayer.name(), bandnum) for bandnum in range(1,bandcount+1)] #['"infra-red@1"', '"infra-red@2"', '"infra-red@3"', '"infra-red@4"']
tempexp = '+'.join(tempexp) #'"infra-red@1"+"infra-red@2"+"infra-red@3"+"infra-red@4"'
exp = '('+tempexp+')/{0}'.format(bandcount) #'("infra-red@1"+"infra-red@2"+"infra-red@3"+"infra-red@4")/4'