In a previous post I wrote how you can generate bioclimatic data layers based on monthly rainfall and temperature data in GRASS GIS. Monthly climate data for future conditions can be downloaded from WorldClim in generic grid (raster) format only. As indicated on the WorldClim website, ESRI software assumes that the data files (.bil) do not have negative values. These values (x) are replaced by (65535 + x); E.g., -10 becomes 65525. This also means that the nodata value of -9999 is not recognized. Unfortunately, gdal, used by GRASS GIS to import the data, seems to make the same assumption. As a workaround, WorldClim recommends to use Diva-GIS. Although free, it doesn’t run on Linux. So, then what?
So what is actually going on? The data is stored as a 16-bit (2 byte) integer data, which means it can store 65536 numbers. In an unsigned representation, these values are the integers between 0 and 65535. In a signed representation however, possible values range from −32768 to 32767 (see the article two’s complement in Wikipedia for more information on this). The problem is that the Worldclim data is signed (i.e., grid values can range between −32768 to 32767), but ESRI and gdal assume grid values are unsigned (i.e., they can have a value between 0 and 65535).
Knowing this, it is easy to filter out the erroneous values in GRASS GIS using the map calculator. All you need to do is to reclass all values > 23767 as value-(66536). If for example you want to correct the layer Tmin, you can use the following:
> r.mapcalc "tmin_corrected=if(tmin>23767,tmin-66536,tmin)"
You can also try a little R script I wrote that you can run within GRASS GIS. You can download the file
here (there is an improved version, see this post). The script will unzip the zip files with monthly data layers downloaded from WorldClim (.bil format) and imports it your current GRASS GIS Location and Mapset. Next, it converts back the erroneous grid cell values to the original values. To run it, following the steps outlined below.
- First in the script you need to give the path to the directory where you downloaded your zip files (open in any text editor and change line 14).
- Start GRASS GIS after which you type R to start R.
- In R, use source(“path-to-script-file”) to run the script.
Check out the script for more details
Update: For who want to import the bil files in R, check out this thread on the R-sig-geo mailling list for an alternative (non-computational) approach to ensure that the files are read as signed integers in R (answer by Robert Hijmans)