rivers/stack-years.pl

37 lines
957 B
Perl
Raw Normal View History

2023-10-25 11:25:00 +01:00
#!/usr/bin/perl -w
use strict;
2024-01-07 17:02:16 +00:00
# use diagnostics;
2023-10-25 11:25:00 +01:00
use DBI;
use POSIX;
use Data::Dumper;
my $station = shift @ARGV or die;
my $db = DBI->connect("dbi:SQLite:dbname=rivers.db") or die $DBI::errstr;
my $years_st = $db->prepare("select distinct substr(timestamp, 1, 4) from levels where stationid=?");
my $getdata_st = $db->prepare("select strftime('2000-%m-%dT%H:%M:%S.000Z', timestamp) as t, level from levels where stationid = ? and timestamp like ? || '-%'");
$years_st->execute($station);
my @years = @{$years_st->fetchall_arrayref};
my %data;
foreach my $y_ref (@years) {
my $y = $y_ref->[0];
$getdata_st->execute($station, $y);
while(my $row = $getdata_st->fetchrow_arrayref) {
$data{$row->[0]}{$y} = $row->[1];
}
}
# print Dumper(\%data);
foreach my $ts (sort keys %data) {
print "$ts\t";
foreach my $y_ref (@years) {
my $y = $y_ref->[0];
print $data{$ts}{$y} || '-', "\t";
}
print "\n";
}