9df5e4a70b31c1c6319a998687c24acc19006c1f
[spider.git] / cmd / show / vhftable.pl
1 #
2 # do an VHFSpot table 
3 #
4 # Copyright (c) 2001 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 my ($self, $line) = @_;
10 my @f = split /\s+/, $line;
11 my @calls;
12 my $days = 31;
13 my @dxcc;
14 my $limit = 100;
15 my %list;
16 my $i;
17 my $now;
18 my @pref;
19 my @out;
20 my $date;
21 my $all;
22
23 #$DB::single = 1;
24
25 while (@f) {
26         my $f = shift @f;
27
28         if ($f =~ /^\d+$/ && $f < 366) {                # no of days
29                 $days = $f;
30                 next;
31         }
32         if (my $utime = Date::Parse::str2time($f)) {    # is it a parseable date?
33                 $utime += 3600;
34                 $now = Julian::Day->new($utime);
35                 $date = cldate($utime);
36                 next;
37         }
38         $f = uc $f;
39         if (is_callsign($f)) {
40                 push @dxcc, [$f, 0];
41                 push @pref, $f;
42         } else {
43                 if ($f eq 'ALL' ) {
44                         $all++;
45                         push @pref, $f;
46                         next;
47                 }
48                 if (my @ciz = Prefix::to_ciz('nc', $f)) {
49                         push @dxcc, map {[$_, 2]} @ciz;
50                         push @pref, $f;
51                 } else {
52                         push @out, $self->msg('e27', $f);
53                 }
54         }
55 }
56
57 # return error messages if any
58 return (1, @out) if @out;
59
60 # default prefixes
61 unless (@pref) {                                        # no prefix or callsign, use default prefix
62         push @dxcc, [$_, 2] for @main::my_cc;
63         push @pref, $main::mycall;
64 }
65
66 # default date
67 unless ($now) {
68         $now = Julian::Day->new(time); #no starting date
69         $date = cldate(time);
70 }
71
72 # generate the spot list
73 for ($i = 0; $i < $days; $i++) {
74         my $fh = $Spot::statp->open($now); # get the next file
75         unless ($fh) {
76                 Spot::genstats($now);
77                 $fh = $Spot::statp->open($now);
78         }
79         while (<$fh>) {
80                 chomp;
81                 my @l = split /\^/;
82                 next if $l[0] eq 'TOTALS';
83                 next unless $all || grep $l[$_->[1]] eq $_->[0], @dxcc;
84                 my $ref = $list{$l[0]} || [0,0,0,0,0,0,0,0,0,0];
85                 my $j = 1;
86                 foreach my $item (@l[14..16, 18..23]) {
87                         $ref->[$j] += $item;
88                         $ref->[0] += $item;
89                         $j++;
90                 }
91                 $list{$l[0]} = $ref if $ref->[0];
92         }
93         $now = $now->sub(1);
94 }
95
96 my @tot;
97 my $nocalls;
98
99 my $l = join ',', @pref;
100 push @out, $self->msg('statvhft', $l, $date, $days);
101 #push @out, $self->msg('statvhft', join(',', @dxcc), cldate(time));
102 push @out, sprintf "%10s|%4s|%4s|%4s|%4s|%4s|%4s|%4s|%4s|%4s|%4s|", qw(Callsign Tot 6m 4m 2m 70cm 23cm 13cm 9cm 6cm 3cm);
103
104 for (sort {$list{$b}->[0] <=> $list{$a}->[0] || $a cmp $b} keys %list) {
105         my $ref = $list{$_};
106         $nocalls++;
107         my @list = (sprintf "%10s", $_);
108         foreach my $j (0..9) {
109                 my $r = $ref->[$j];
110                 if ($r) {
111                         $tot[$j] += $r;
112                         $r = sprintf("%4d", $r);
113                 } else {
114                         $r = '    ';
115                 }
116                 push @list, $r;
117         }
118         push @out, join('|', @list, "");
119         last if $limit && $nocalls >= $limit;
120 }
121
122 $nocalls = sprintf "%10s", "$nocalls calls";
123 @tot = map {$_ ?  sprintf("%4d", $_) : '    ' } @tot;
124 push @out, join('|', $nocalls, @tot, "");
125
126 return (1, @out);