User:Mysid/ClimateCharts

From Wikipedia, the free encyclopedia

I have made a few climate charts for Wikipedia articles in gnuplot and perl; here's the source.

This works in Linux and is used thusly:

perl climate.pl "http://www.bbc.co.uk/weather/world/city_guides/results.shtml?tt=TT003520" > data.txt
gnuplot climate.plt

[edit] Perl source

The perl script climate.pl uses LWP::Simple to scrape the data from BBC Weather Centre.

use LWP::Simple;
$joo = 0;
for (get($ARGV[0])) {
    if (/conditions statistics open/) {
        $joo = 1;
    } elsif (/conditions statistics close/) {
        $joo = 0;
    }
    if ($joo) {
        if (/<\/tr><tr/ && $_ !~ /img/) {
            s/<\/tr><tr[^>]*>//;
            s/<t[hd] [^>]+><strong>/,/g;
            s/<\/strong><\/t[hd]>//g;
            s/^\,//;
            #print;
            @a = split(/,/,$_);
            unless (/Average|Max/) {
                $i++;
                print "$i ".substr($a[0],0,1)." $a[2] $a[3] $a[9]\n";
            }
        }
    }
}

[edit] Gnuplot source

climate.plt plots the chart.

set tics scale 0
set ytics
set y2tics 0,30,300
set grid ytics
set xrange [0.5:12.5]
set yrange [-20:40]
set y2range [-60:120]
set ylabel "Avg temperature (°C)"
set y2label "Avg precipitation (mm)"
set style fill solid
set boxwidth 0.6
set key off
set term svg size 640,500 font "Arial,18"
set output "climate.svg"
set object 1 rectangle from graph 0,0 to graph 1,1 behind fc rgbcolor "#EEEEff" lw 0
set object 2 rectangle from graph 0.81,0.8 to graph 0.99,0.99 fc rgbcolor "#ddddff"
set object 3 rectangle from graph 0.82,0.81 to graph 0.98,0.86 fc rgbcolor "#bbbbff" lw 0
set label "max °C" at character 36,17 tc rgbcolor "#FF0000"
set label "min °C" at character 36,16 tc rgbcolor "#0000FF"
set label "rainfall" at character 36,15 tc rgbcolor "#000000"
plot 'data.txt' using 1:5 with boxes axes x1y2 lc rgbcolor "#bbbbff",\
     'data.txt' using 1:3:xtic(2) with lines lc 3 lw 2,\
     'data.txt' using 1:4 with lines lc 1 lw 2