2

I'm trying to create a r.sunmask raster for every hour of the year for a specific DEM, so 24 rasters per day for 365 days. I'm quite new to QGIS and would like to know if there was a way to automate the process and to create some sort of cycle in Python so that it will create all the output raster without me having to manually create each one of them.

1
  • You should be able to run a script in the grass console shell Plugins/GRASS/OpenGrassTools/ then Modules/GRASS MODULES/GRASS shell to invoke r.sunmask in a loop with different options. What operating system are you using, and what scheme of filenames would you like?
    – Dave X
    Commented Dec 8, 2020 at 20:49

1 Answer 1

3

You can use Python datetime module to generate the appropriate dates, then use processing module from QGIS (if you intend to use the Python interpreter of QGIS).

I have not tested the various parameters needed for r.sunmask, but the main logic could be as follows:

import datetime
from qgis import processing

start = datetime.datetime(2020, 1, 1)
end = datetime.datetime(2021, 1, 1)
delta = datetime.timedelta(hours = 1)

# the list of datetimes to create
requested_dt = []

current = start
while current <= end: # stop at '2021-01-01 00:00:00' included
    requested_dt.append(current)
    current += delta

# get a reference to your elevation model
dem = QgsProject.instance().mapLayersByName('name_of_your_dem_in_qgis')[0]
    
# Run r.sunmask algorithm for each datetime
for dt in requested_dt:
    y, m, d, h, _, _, _, _, _ = current.timetuple()
    parameters = {
        'elevation': dem,
        'year': y,
        'month': m,
        'day': d,
        'hour': h,
        'minute': 0,
        'second': 0,
        'output': '/path/to/output-{}-{}-{}-{}h.tiff'.format(y, m, d, h)
    }
    
    processing.run('grass:r.sunmask.datetime',parameters)

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.