One of my projects at the Blue Oak Ranch Reserve involves making spatial predictions of evapotranspiration. Many of the ET models (such as Priestly-Taylor) require solar radiation as input, which means I need solar radiation maps to perform the calculations. Both ArcGIS and GRASS offer solar radiation models, but I find GRASS advantageous because the documentation is well-developed and the radiation model can incorporate a number of features that the ArcGIS radiation model cannot, such as terrain shadowing.

When I say the documentation is well-developed, I mean the command manuals in the function reference specifically; documentation for setup and GRASS in general leaves a lot to be desired. My biggest issue was that I needed to run the radiation model for a number of days of the year, and running it manually would be tedious and time-consuming. I wanted to develop a simple loop that would run the radiation model for a specified sequence of days. GRASS supports this kind of functionality; in addition to being callable in text mode, it also interfaces with both R and Python.

Unfortunately, I could not get much of anything to run using Bash or R. A combination of complicated setup requirements, poorly-developed help files and my limited experience with GRASS resulted in repeated failures. I couldn’t even set up the environment variables properly to initialize GRASS and create a new mapset. But I did find a way with Python! It’s a bit primitive, but hey—it works, and that’s all I care about at this point.

I’m going to assume you know how to set up a mapset in GRASS and add/import some layers; the documentation will get you that far at least. The tricky part can be the region setting, but I made things easy for myself by defining the extent based on my DEM. The next thing I did was run r.slope.aspect, since r.sun (the radiation model) uses slope and aspect as well as elevation to computer solar radiation.

If you’re in the Layer Manager window, you should see the Python Shell tab at the bottom of the window. Going to this tab gives you a Python interface, and you can get access to GRASS functions by importing the grass.script module. The module includes the run_command() function, which can be used to run basically any GRASS function. It supports flags through the flags argument, as well as any other named arguments relevant to the function you’re calling.

So let’s keep things simple and just run a script by first navigating to its parent folder (using chdir() from the os module), and then calling it using execfile():

And the script itself:

And that’s it! The script is running as I type this, and I’m free to write about it on my blog without having to stop every 15 minutes to set up another run of the solar radiation model.