route cache, wcy, wwv. ann caching
[spider.git] / perl / Geomag.pm
1 #!/usr/bin/perl
2
3 # The geomagnetic information and calculation module
4 # a chanfe
5 #
6 # Copyright (c) 1998 - Dirk Koopman G1TLH
7 #
8 #
9 #
10
11 package Geomag;
12
13 use DXVars;
14 use DXUtil;
15 use DXLog;
16 use Julian;
17 use IO::File;
18 use DXDebug;
19 use DXDupe;
20
21 use strict;
22
23 use vars qw($date $sfi $k $a $r $forecast @allowed @denied $fp $node $from 
24             $dirprefix $param
25             $duplth $dupage $filterdef);
26
27 $fp = 0;                                                # the DXLog fcb
28 $date = 0;                                              # the unix time of the WWV (notional)
29 $sfi = 0;                                               # the current SFI value
30 $k = 0;                                                 # the current K value
31 $a = 0;                                                 # the current A value
32 $r = 0;                                                 # the current R value
33 $forecast = "";                                 # the current geomagnetic forecast
34 $node = "";                                             # originating node
35 $from = "";                                             # who this came from
36 @allowed = ();                                  # if present only these callsigns are regarded as valid WWV updators
37 @denied = ();                                   # if present ignore any wwv from these callsigns
38 $duplth = 20;                                   # the length of text to use in the deduping
39 $dupage = 12*3600;                              # the length of time to hold spot dups
40
41 $dirprefix = "$main::local_data/wwv";
42 $param = "$dirprefix/param";
43
44 our $maxcache = 10;
45 our @cache;
46
47
48 $filterdef = bless ([
49                           # tag, sort, field, priv, special parser 
50                           ['by', 'c', 0],
51                           ['origin', 'c', 1],
52                           ['channel', 'c', 2],
53                           ['by_dxcc', 'nc', 3],
54                           ['by_itu', 'ni', 4],
55                           ['by_zone', 'nz', 5],
56                           ['origin_dxcc', 'nc', 6],
57                           ['origin_itu', 'ni', 7],
58                           ['origin_zone', 'nz', 8],
59                          ], 'Filter::Cmd');
60
61 sub init
62 {
63         $fp = DXLog::new('wwv', 'dat', 'm');
64         do "$param" if -e "$param";
65         # read in existing data
66         @cache = readfile($main::systime);
67         shift @cache while @cache > $maxcache;  
68         dbg(sprintf "WWV read in last %d records into cache", scalar @cache);   
69         confess $@ if $@;
70 }
71
72 # write the current data away
73 sub store
74 {
75         my $fh = new IO::File;
76         open $fh, "> $param" or confess "can't open $param $!";
77         print $fh "# Geomagnetic data parameter file last mod:", scalar gmtime, "\n";
78         print $fh "\$date = $date;\n";
79         print $fh "\$sfi = $sfi;\n";
80         print $fh "\$a = $a;\n";
81         print $fh "\$k = $k;\n";
82         print $fh "\$r = $r;\n";
83         print $fh "\$from = '$from';\n";
84         print $fh "\$node = '$node';\n";
85         print $fh "\@denied = qw(", join(' ', @denied), ");\n" if @denied > 0;
86         print $fh "\@allowed = qw(", join(' ', @allowed), ");\n" if @allowed > 0;
87         close $fh;
88         
89         # log it
90         my $s ="$from^$date^$sfi^$a^$k^$forecast^$node^$r";
91         $fp->writeunix($date, $s);
92         push @cache, [ split /\^/, $s ];
93         shift @cache while @cache > $maxcache; 
94 }
95
96 # update WWV info in one go (usually from a PC23)
97 sub update
98 {
99         my ($mydate, $mytime, $mysfi, $mya, $myk, $myforecast, $myfrom, $mynode, $myr) = @_;
100         $myfrom =~ s/-\d+$//;
101         if ((@allowed && grep {$_ eq $myfrom} @allowed) || 
102                 (@denied && !grep {$_ eq $myfrom} @denied) ||
103                 (@allowed == 0 && @denied == 0)) {
104                 
105                 #       my $trydate = cltounix($mydate, sprintf("%02d18Z", $mytime));
106                 if ($mydate > $date) {
107                         if ($myr) {
108                                 $r = 0 + $myr;
109                         } else {
110                                 $r = 0 unless abs ($mysfi - $sfi) > 3;
111                         }
112                         $sfi = 0 + $mysfi;
113                         $k = 0 + $myk;
114                         $a = 0 + $mya;
115                         $forecast = $myforecast;
116                         $date = $mydate;
117                         $from = $myfrom;
118                         $node = $mynode;
119                         
120                         store();
121                 }
122         }
123 }
124
125 # add or substract an allowed callsign
126 sub allowed
127 {
128         my $flag = shift;
129         if ($flag eq '+') {
130                 push @allowed, map {uc $_} @_;
131         } else {
132                 my $c;
133                 foreach $c (@_) {
134                         @allowed = map {$_ ne uc $c} @allowed; 
135                 } 
136         }
137         store();
138 }
139
140 # add or substract a denied callsign
141 sub denied
142 {
143         my $flag = shift;
144         if ($flag eq '+') {
145                 push @denied, map {uc $_} @_;
146         } else {
147                 my $c;
148                 foreach $c (@_) {
149                         @denied = map {$_ ne uc $c} @denied; 
150                 } 
151         }
152         store();
153 }
154
155 # accessor routines (when I work how symbolic refs work I might use one of those!)
156 sub sfi
157 {
158         @_ ? $sfi = shift : $sfi ;
159 }
160
161 sub k
162 {
163         @_ ? $k = shift : $k ;
164 }
165
166 sub r
167 {
168         @_ ? $r = shift : $r ;
169 }
170
171 sub a
172 {
173         @_ ? $a = shift : $a ;
174 }
175
176 sub forecast
177 {
178         @_ ? $forecast = shift : $forecast ;
179 }
180
181
182 #
183 # print some items from the log backwards in time
184 #
185 # This command outputs a list of n lines starting from line $from to $to
186 #
187 sub search
188 {
189         my $from = shift;
190         my $to = shift;
191         my $t = shift;
192         my $date = $fp->unixtoj($t);
193         my $pattern = shift;
194         my $search;
195         my @out;
196         my $eval;
197         my $count;
198
199         if ($t == $main::systime && ($to <= $maxcache)) {
200                 dbg("using wwv cache") if isdbg('wwv');
201                 @out = reverse @cache;
202                 pop @out while @out > $to;
203         } else {
204                 dbg("using wwv file(s))") if isdbg('wwv');
205                 $search = 1;
206                 $eval = qq(
207                            my \$c;
208                            my \$ref;
209                            for (\$c = \$#in; \$c >= 0; \$c--) {
210                                         \$ref = \$in[\$c];
211                                         if ($search) {
212                                                 \$count++;
213                                                 next if \$count < \$from;
214                                                 push \@out, \$ref;
215                                                 last if \$count >= \$to; # stop after n
216                                         }
217                                 }
218                           );
219         
220                 $fp->close;                                     # close any open files
221                 
222                 my $fh = $fp->open($date); 
223                 for ($count = 0; $count < $to; ) {
224                         my @in = ();
225                         if ($fh) {
226                                 while (<$fh>) {
227                                         chomp;
228                                         push @in, [ split '\^' ] if length > 2;
229                                 }
230                                 eval $eval;                     # do the search on this file
231                                 return ("Geomag search error", $@) if $@;
232                                 last if $count >= $to; # stop after n
233                         }
234                         $fh = $fp->openprev();  # get the next file
235                         last if !$fh;
236                 }
237         }
238         
239         return @out;
240 }
241
242 #
243 # the standard log printing interpreting routine.
244 #
245 # every line that is printed should call this routine to be actually visualised
246 #
247 # Don't really know whether this is the correct place to put this stuff, but where
248 # else is correct?
249 #
250 # I get a reference to an array of items
251 #
252 sub print_item
253 {
254         my $r = shift;
255         my @ref = @$r;
256         my $d = cldate($ref[1]);
257         my ($t) = (gmtime($ref[1]))[2];
258         
259         return sprintf("$d   %02d %5d %3d %3d %-37s <%s>", $t, $ref[2], $ref[3], $ref[4], $ref[5], $ref[0]);
260 }
261
262 #
263 # read in this month's data
264 #
265 sub readfile
266 {
267         my $date = $fp->unixtoj(shift);
268         my $fh = $fp->open($date); 
269         my @spots = ();
270         my @in;
271         
272         if ($fh) {
273                 while (<$fh>) {
274                         chomp;
275                         push @in, [ split '\^' ] if length > 2;
276                 }
277         }
278         return @in;
279 }
280
281 # enter the spot for dup checking and return true if it is already a dup
282 sub dup
283 {
284         my ($d, $sfi, $k, $a, $text, $call) = @_; 
285
286         # dump if too old
287         return 2 if $d < $main::systime - $dupage;
288  
289         my $dupkey = "W$d|$sfi|$k|$a|$call";
290         return DXDupe::check($dupkey, $main::systime+$dupage);
291 }
292
293 sub listdups
294 {
295         return DXDupe::listdups('W', $dupage, @_);
296 }
297 1;
298 __END__;
299