I have a large set of radar .tif files with several bands, and I would like to have some Python scripts to perform some basic operations that I would usually perform using the Raster Calculator in QGIS 3.22.6, namely:
- Create a new set of .tif files with only one of the bands of the input files, with the name equal to the original raster with a suffix (like here)
- Create a new set of .tif files in which the output is a single band resulting from applying (a+b+c+d)/4 to the first 4 bands of the input files.
- Create a new set of .tif files in which the output is a single band resulting from applying d/b to the 2nd and 4th input file bands.
I am currently trying to do this based on this similar question, but I don't know how to use other layers than the first one. My attempt did not produce the expected results:
from qgis.analysis import QgsRasterCalculatorEntry, QgsRasterCalculator from qgis.core import QgsProject lddLrs = [tree_layer.layer() for tree_layer in QgsProject.instance().layerTreeRoot().findLayers()] path = "path/to/output" for lyr in lddLrs: entries =  ras = QgsRasterCalculatorEntry() ras.ref = 'ras@1' ras.raster = lyr ras.bandNumber = 1 entries.append(ras) calc = QgsRasterCalculator('("infra-red@1"+"infra-red@2"+"infra-red@3"+"infra-red@4")/4', path + lyr.name() + "_suffix.tif", 'GTiff', lyr.extent(), lyr.width(), lyr.height(), entries) calc.processCalculation()
This other question seemed to have a similar problem, but there doesn't seem to be an answer to it yet.