I am a bit new when it comes to Python programming.
I'm trying to run the GRASS r.sun plugin tool to calculate the global irradiance for my chosen area for the whole year. The problem is that r.sun calculates the irradiance for a day. The GRASS program isn't an option for me becouse it doesn't work properly on my computer.
So the solution that I am looking for is a script that puts every number from 1 to 365 into the tool and makes a .tif raster result with the number of the day in its name. The numbers that have to change are in bold.
Basically this but in python:
https://grasswiki.osgeo.org/wiki/R.sun#Automation
Example of script taken from History:
import processing
dir(processing)
elev_raster = "C:\Qgis_proj\data\DSM.tif"
aspect_raster = "C:\Qgis_proj\pod\aspect.tif"
slope_raster = "C:\Qgis_proj\pod\slope.tif"
irradiance_raster = "C:\GRASS\results\glob_irr_1.tif"
processing.runalg("grass7:r.sun",elev_raster, aspect_raster, slope_raster,None,None,None,None,None,None,1,0.5,0,1,False,False,"454999.5,457000.5,95999.5,98000.5", 0,None,None,None,None, irradiance_raster)
1 Answer 1
Basically, you just have to add a for
loop. The following code should work:
import processing
dir(processing)
elev_raster = "C:\Qgis_proj\data\DSM.tif"
aspect_raster = "C:\Qgis_proj\pod\aspect.tif"
slope_raster = "C:\Qgis_proj\pod\slope.tif"
for i in range(1,365):
irradiance_raster = "C:\GRASS\results\glob_irr_" + str(i) + ".tif"
processing.runalg("grass7:r.sun",elev_raster, aspect_raster, slope_raster,None,None,None,None,None,None,i,0.5,0,1,False,False,"454999.5,457000.5,95999.5,98000.5", 0,None,None,None,None, irradiance_raster)
-
Thank you very very much :) I knew the range would be useful.Shai-Hulud– Shai-Hulud2016年04月05日 14:06:33 +00:00Commented Apr 5, 2016 at 14:06
-
3If you want fancier filenames (e.g. zero padding), have a look at docs.python.org/3/tutorial/inputoutput.html. E.g. str(i).zfill(3) instead of str(i) gives '001', '002' etc. filenames. Also the format() method for strings is nice. '{0:03d}'.format(i) instead of str(i) again gives '001', '002' etc.Robin– Robin2016年04月05日 14:14:02 +00:00Commented Apr 5, 2016 at 14:14
-
@Robin: nice catch! I didn't know
zfill
.ArMoraer– ArMoraer2016年04月05日 14:22:31 +00:00Commented Apr 5, 2016 at 14:22