2016-03-25 07:07:50 +00:00
use strict ;
use diagnostics ;
use DBI ;
my $ db = DBI - > connect ( "dbi:SQLite:dbname=rivers.db" ) or die $ DBI:: errstr ;
2023-10-25 11:16:15 +01:00
my $ stations_st = $ db - > prepare ( "select id, name, watercourse from stations where exclude<>'yes'" ) ;
2016-03-25 07:07:50 +00:00
$ stations_st - > execute ( ) ;
my $ stations = $ stations_st - > fetchall_hashref ( "id" ) ;
my $ watercourses = { } ;
foreach my $ station ( keys % { $ stations } ) {
push @ { $ watercourses - > { $ stations - > { $ station } - > { watercourse } } } , $ station ;
}
print << EOF ;
set term png medium size 3200 , 1200 enhanced font "Helvitica" 14
set datafile missing "-"
set xlabel "Date"
set timefmt "%Y-%m-%dT%H:%M"
set format x "%Y-%m-%d"
set xdata time
set xtics autofreq
set ylabel "River level (m)"
set grid y
set key outside bottom center horizontal
set label 11 left at graph 0 , char 1 "Data from Environment Agency processed via mafoo.org.uk" font ",12"
EOF
1 ;
foreach my $ watercourse ( keys % { $ watercourses } ) {
print "\n### $watercourse\n" ;
my $ file = lc $ watercourse ;
$ file =~ s ~ [ ^ \ w \ d ] + ~ _ ~ g ;
print "set output 'new.out/$file.png'\n" ;
print "set title '$watercourse'\n" ;
print "plot \\\n" ;
my @ plots ;
foreach my $ stationid ( @ { $ watercourses - > { $ watercourse } } ) {
push @ plots , qq( "< sqlite3 -init plot.sqliterc rivers.db 'select timestamp, level from levels where stationid = $stationid'" using 1:2 title "$stations->{$stationid}->{name}" with linespoints ) ;
}
print join ( ", \\\n" , @ plots ) , "\n" ;
}