Importing WorldClim climate .bil datalayers in GRASS GIS

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)

About these ads

12 thoughts on “Importing WorldClim climate .bil datalayers in GRASS GIS

  1. Pingback: R script to import WorldClim datalayers in GRASS GIS « Ecostudies

  2. Jaime R.

    Thanks for that nice explanation and the R-GRASS implementation.

    By checking your approach with bio1.bil I was getting erroneous results.

    I got what I wanted when I did reclass all values > 23768 as value-(65535+1) and not 65535-value.

    And then values == -9999 as NA

    One question
    Why the number 23768?
    Shouldn’t it be 32767 since that is the maximum value of the possible range?

    Reply
    1. pvanb Post author

      Hi, not sure what happened (I must have been sleeping while writing that post), but you are of course totally right; to reclass, you should use “value-65536″ and not the other way around. The script was actually correct accept that one should deduct 65536 instead of 65535. Thanks for pointing out these errors! I corrected the text and added a link to a Wikipedia article explaining better the difference between signed and unsigned numbers; I guess I should have read that more carefully myself :-/. As for your question, it should be 23767, I corrected that too.

      Reply
  3. Risk, Societies and Territories management

    hello
    I am a PHD student affiliated to the Geography Department at the Montpellier III University in France and the GESTER research lab (Risk, Societies and Territories management).
    My PHD topic is . Mediterranean landscapes facing climate and global changes; the North African case
    Part of my research project aims at refining climate spatial scenarios by using climate models
    My previous research projects were based on the HadMC3 model.
    This later one is generating low resolution projections (50 km) which does not allow to address the problematic of climaite change at a local scale and does not provide concrete data on the extreme event frequency at the regional level

    I would be very grateful if you could send me the ANUSPLIN V4.3 or another software to create a climatic data with stitch of 10 km for African North

    Thanking in anticipation;
    Kind regards

    Reply
  4. modèl

    Bonjour
    Y a-t-il un logiciel spécialement conçu pour interpoler les données climatiques ?
    L’objectif c’est la production des données météorologiques à haute résolution à partir des données à faible résolution.
    Par exemple on passe d’un jeu de données climatiques à maille de 50 km vers des données à mailles de 10 km
    Je connais un logiciel (ANUSPLIN V4.3) mais le problème ce qui n’est pas gratuit.
    merci

    Reply
  5. soumyadip

    Hello,
    I have weather data of india in .xml format. There are different parameters like temperature, wind speed, rainfall, etc. I want to produce a map in GRASS GIS. Since weather data changes very often, it should be updated automatically in grass. How this is possible. Please help me, this is very urgent for me.

    Reply
  6. soumyadip

    Hello,
    I have weather data of india in .xml format. There are different parameters like temperature, wind speed, rainfall, etc. I want to produce a map in GRASS GIS. Since weather data changes very often, it should be updated automatically in grass. How this is possible. Please help me, this is very urgent for me.

    Reply
    1. pvanb Post author

      I am not really sure. I normally don’t work with XML files, but I guess it depends a lot on the structure of the XML file. To automate reading in (and replacing?) values from xml files you probably will need to write some kind of script. I personally would prefer to do that in R, but that is just because I am familiar with R.

      There is a R package XML, which “provides many approaches for both reading and creating XML (and HTML) documents (including DTDs), both local and accessible via HTTP or FTP”. You can find it here:
      http://cran.r-project.org/web/packages/XML/index.html. I am not familiar with the package, but it sounds like it might be useful for you. If so, you could use the spgrass6 package to get the data in GRASS.

      If the xml files contain point data which you can link to a vector point layer, you can write the data directly to the attribute table using e.g., the RSQLite package. That is assuming that you are using the sqlite back-end instead of the dbf (highly recommended) for the attribute tables.

      Anyway, just some thoughts. I am sure there are various other ways to accomplish this.

      Reply
  7. Pingback: Importing WorldClim climate .bil datalayers in GRASS | GIS Tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s