Developing custom GRASS programs

From Rhessys
Jump to navigationJump to search

As the majority of the RHESSys group is using OS X, this article targets that OS. However, most of the content should apply to any operating system.

Get a compiler

Development tools are not installed by default on OS X. You can install the dev tools from any OS X installation CD. For the most recent version, go Apple's Mac Dev Center and download the latest version of XCode. You will be required to create a free account before you can download the tools.

Get the GRASS source

In order to write your own custom GRASS programs, you will need the GRASS header files and libraries. Even if you already have GRASS on your computer, these may not be installed. Additionally, the actual GRASS source code is an excellent resource for learning to write new GRASS modules, and should be downloaded even if your installation already has the necessary headers and libraries.

First, check to see if your GRASS install has the necessary headers and libraries.

  1. Open a terminal window
  2. cd to the folder containing your GRASS executable
  3. $> ls <GRASS app name>/Contents/MacOS/include/grass
  4. If you see several files ending in .h, one of which is gis.h, then you have the GRASS headers.
  5. $> ls <GRASS app name>/Contents/MacOS/lib
  6. You should see a long list of files ending in .dylib. Make sure that one of them is libgrass_gis.dylib.
  7. If your GRASS install had both the .h and .dylib files, then you already have the necessary files and do not need to download the source.


Next, getting the GRASS source. To download the GRASS source, go to http://grass.itc.it/download/index.php. It is best to download the RC code, as svn code may have issues compiling. If you did not find the necessary .h and .dylib files, it will be necessary to compile GRASS. Unfortunately, GRASS has a long list of dependencies, and building can be rather difficult.

An explanation of building software and all it's dependancies is beyond the scope of this tutorial. However, learning to build the software yourself can be a useful skill. For example, GRASS can support NetCDF, a format we intend to use more in the future. However, most pre-built GRASS installations do not use this functionality. By building GRASS yourself, you can have access to useful tools before they become mainstream.

Examining the GRASS source

GRASS is very different than a program like MATLAB or MSWord. Where those programs are a monolithic single application, GRASS is actually a collection of very small programs. This makes reading through the source code and expanding GRASS functionality very easy. Every individual GRASS command, such as g.region, d.mon, r.mapcalc, etc. is actually a small self contained program. These smaller programs all interact with each other by accessing the GRASS map data stored in your projects LOCATION folder. One can think of this as similar to using RHESSys, where tools such as g2w, cf, and rhessys all work together to form the larger RHESSys program.