Graphing weather statistics with rrdtool
A little introduction
I wanted to learn how to make cool graphs and graphing local weather seemed good for public interest also. University of Jyväskylä had and still has a weather station going so I decided to make graphs of that data. Unfortunately the original graphs and data were destroyed when the contemporary server running statistics collection discontinued.
Some time ago I got excited about recreating the graphing project and now I thought it would be wise to document it for the future and others. To be clear, this article assumes you have some kind of Unix system running and you are familiar with it.
About the topic
I'm not going to explain
rrdtool in this article so please read
if something about its workings is unclear.
rrdtool implementation mainly consists of two separate tasks:
collecting the data and then graphing it. But first we have to create
the round robin database.
Of the measurements the JYU weather station reports I decided to collect three of them: air temperature outside, barometric pressure, and relative humidity inside. Humidity is probably affected by ventilation so collecting it won't probably make much sense from the weather viewpoint. Whatever, I included it anyway.
For database creation there is
create.sh Bourne Shell script which
creates own database for each of temperature, pressure, and humidity.
rrdtool documentation about data sources and command syntax.
I based my files simply at
~/wx/ and so the rrd databases are at
Now that we have the database to collect data to, we can focus on the
collection itself. Noteworthy is that this step depends entirely on
where do you want to retrieve the information because
of course dictate how you get your data. It just wants to munch
sweet, understandable numbers at known interval.
So if you are doing this yourself then you have to deal with data fetching and parsing according to the situation. If by any means obtainable, XML files or queriable databases are your friend, as well as plain text files with precisely predefined structure.
Now, this part does the graphing. Run the script and get new graphs based on the latest data. Graphing script can be run whenever and how often one wants as it does not affect data collection.
First there are some definitions in the file to ease making changes
to the graphs, then
rrdtool with corresponding parameters is run
to generate each graph.
Running the scripts
Cron is really well suited for running these scripts. The personal
crontab of a user can be edited by:
$ crontab -u <user> -e
And it should look something like this:
# m h dom mon dow command */5 * * * * /path/to/wx/collect.sh */15 * * * * /path/to/wx/graph.sh > /dev/null 2>&1
It says that the collection script is ran every day at every five minutes
and graphing script every fifteen minutes. Remember, rrd databases need
to be fed data at
step interval so the collection script needs to be
step interval too.
As said beore, creating the graphs won't affect data collection so it can be ran at any time - how (in)frequently it ever suits you. Of course graphing at shorter interval than collecting data is very silly.
If you lack
cron access you could also create infinite loop shell
sleep and execute them once at startup.
But be careful with that, results from those scripts hanging could
be unpleasant at least.
The post has shown a way and introduced resources to collecting and graphing weather data from a web source. When the collection and graphing automation works, another thing to do would be presenting the graphs. There's an example site with graphs at wx.may.fi.