2

I have 30 raster files from 1981 to 2010. Every raster has 12 bands, again corresponding to dates from June 1 to June 12. I want to calculate the mean for all the bands from 1981 to 2010. Normally, I would have manually clicked on the bands via raster calculator but since there are a total of 360 bands in total, manually it's a little cumbersome. Is there a way where I can batch process the mean calculation?

0

2 Answers 2

2

For the raster calculator, you need an expression like this:

"raster_1981@1" + "raster_1982@1" + "raster_1983@1" + (...) 
+ "raster_2008@12" + "raster_2009@12" + "raster_2010@12"

You can use QGIS expressions to create such a string and than copy/paste it in raster calculator. The expression gets all layers, using the variable @layers, returns their layernames and concatenates all of them with a series of numbers for the bands: 1 to 12, in your case.

Open a new QGIS project with the only layers loaded being the rasters you want. Then go to Menu Processing -> Toolbox -> Field Calculator. See below for the expression that creates the string you want: copy it from the Preview at the bottom (right click) and insert it into the raster calculator.

enter image description here

array_to_string (
    array_foreach(
        generate_series(1,12),
        with_variable(
            'no',
            @element,
            array_to_string (
                array_foreach(
                    @layers,
                    '"'  || 
                    layer_property (@element,'name')
                    || '@' || @no  || '"'
                ),
                '+'
            )
        )
    ),
    '+'
)
1

Save the bands as separate raster files, than use Menu Processing -> Toolbox -> Cell statistics. Select all your layers in the tool, set statistics to Mean define a reference layer (for the extent) and run it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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