I would like to run this code that makes calculation at every loop between different input rasters and Itot that is a raster that in the loop remains always the same. The code doesn't give me any error but it doesn't work because at the end the writing:'invalid layer gdal provider cannot open the gdal database' appear. I guess the problem could be in how I treat Itot in the calculation, can someone help me? Here is the code:
import qgis
import gdal
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from qgis.core import QgsRasterLayer
p=[0.84, 0.83, 1.03, 1.11, 1.24, 1.25, 1.27, 1.18, 1.04, 0.96, 0.83, 0.81]
Itot = QgsRasterLayer('/Users/macbook/Desktop/TESI/CAPITANATA PROGETTO/Itot.tif')
suffix_output=['th_jan', 'th_feb', 'th_mar', 'th_apr', 'th_may', 'th_jun', 'th_jul', 'th_aug', 'th_sep', 'th_oct', 'th_nov' ,'th_dec']
suffix_input=['Tjan', 'Tfeb', 'Tmar', 'Tapr', 'Tmay', 'Tjun', 'Tjul', 'Taug', 'Tsep', 'Toct', 'Tnov', 'Tdec']
outputpath = r'/Users/macbook/Desktop/TESI/PROGETTO/PET_thornthwaite/'
inputpath = r'/Users/macbook/Desktop/TESI/PROGETTO/raster_T_average/'
list=[0,1,2,3,4,5,6,7,8,9,10,11]
for i in list:
inputrasterfile = QgsRasterLayer(inputpath + suffix_input[i] + ".tif")
entries = []
ras = QgsRasterCalculatorEntry()
ras.ref = 'ras@1'
ras.raster = inputrasterfile
ras.bandNumber = 1
entries.append( ras )
new_path = outputpath + suffix_output[i]+ '.tif'
th = str(p[i]) +'* 16* (10* ras@1 / Itot)^(0.5 + 0.016 * Itot)'
Pet = QgsRasterCalculator(th, new_path, 'GTiff', inputrasterfile.extent(), inputrasterfile.width(), inputrasterfile.height(), entries )
Pet.processCalculation()
iface.addRasterLayer(new_path)