I've been searching around a long time to make ECMWF's GRIB API work on my Ubuntu 12.04 and finally managed to set it up properly following the steps described in this tutorial.
Version note 1: While working with Fortran 90 on 64-bit Ubuntu I kept on running into compiler problems like this
. There is a GRIB API binary available
for 64-bit Ubuntu, so the installation SHOULD work on all systems. However, I suggest that you use 32-bit version until compiler compatibility issues get solved.
Note 2: There is an uncertainty among Ubuntu distributions between /usr/include and /usr/local/include as default path for header and mod files. If a compiler asks for an included file or a header that you can find in any of these libraries, simply copying to the another one usually solves the problem. (The same issue with /usr/lib exists).
First, install the compilers you will need for your work.
Then you have to download and install the Jasper package.
sudo apt-get update
sudo apt-get install gcc gfortran g++ build-essential
To install the GRIB API package (documentation provided here), type:
sudo apt-get install libgrib-api-dev
GRIB API is now installed on your system. To test your installation, download a Fortran 90 sample file from here and a GFS grib file from here. Modify the filename in the get_data.f90 code to the path of your grib file. Compile your code with linking the necessary libraries:
You have to see the lat-lon-lev coordinates of all the grid points of your grib. Ctrl-C out of the script and start working with your gribs!
gfortran -c -I/usr/include get_data.f90 -o get_data.o
gfortran get_data.o -L/usr/lib -lgrib_api_f90 -lgrib_api -o get_data.out
Troubleshoot: if the compiler requires the grib_api.mod file or complains about undefined reference, check if the grib_api.mod file exists in your /usr/include folder and the libgrib_api_f90.so, libgrib_api.so files exist in your /usr/lib folder. If not, try to find them somewhere else on your computer (eg. in /usr/local/bin, /usr/local/lib) and link to that folder when compiling. If they can't be found on your computer then your installation may be wrong, try to reinstall the libgrib_api_dev package or make the tarred distribution through the steps explained here
You will probably need the g95 compiler that is used to make the GRIB API and other f90/95 files. It is NOT NECESSARY for the installation though as we used a deb package. You can get g95 from here.
The symbolic linking described in the docs of g95 didn't work for me. Simply copying the bin and lib libraries to your /bin and /lib folder will work fine (with renaming the binary to g95):
You can check your installation with calling the compiler without arguments. You have to see this:
sudo cp /...your path to g95 unzipped folder.../g95-install/bin/*g95* /bin/g95
sudo cp -r /...your path to g95 unzipped folder.../g95-install/lib/* /lib
g95: no input files