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