6. Implemented PC49 delete/full from outside (kill full on the inside)
[spider.git] / perl / Geomag.pm
index 1f77f5671f4114992e32550964fb8c0f6d50870a..a63d19b66bbdef6d473e19b0a675eedd802c11a9 100644 (file)
@@ -1,6 +1,7 @@
 #!/usr/bin/perl
 # 
 # The geomagnetic information and calculation module
+# a chanfe
 #
 # Copyright (c) 1998 - Dirk Koopman G1TLH
 #
@@ -11,17 +12,22 @@ package Geomag;
 
 use DXVars;
 use DXUtil;
+use DXLog;
+use Julian;
 use FileHandle;
 use Carp;
 
 use strict;
-use vars qw($date $sfi $k $a $forecast @allowed @denied);
+use vars qw($date $sfi $k $a $forecast @allowed @denied $fp $node $from);
 
+$fp = 0;            # the DXLog fcb
 $date = 0;          # the unix time of the WWV (notional)
 $sfi = 0;           # the current SFI value
 $k = 0;             # the current K value
 $a = 0;             # the current A value
 $forecast = "";     # the current geomagnetic forecast
+$node = "";         # originating node
+$from = "";         # who this came from
 @allowed = ();      # if present only these callsigns are regarded as valid WWV updators
 @denied = ();       # if present ignore any wwv from these callsigns
 my $dirprefix = "$main::data/wwv";
@@ -29,9 +35,10 @@ my $param = "$dirprefix/param";
 
 sub init
 {
-  mkdir $dirprefix, 0777 if !-e $dirprefix;
-  do "$param" if -e "$param";
-  confess $@ if $@;
+       $fp = DXLog::new('wwv', 'dat', 'm');
+       mkdir $dirprefix, 0777 if !-e $dirprefix;        # now unnecessary DXLog will create it
+       do "$param" if -e "$param";
+       confess $@ if $@;
 }
 
 # write the current data away
@@ -44,16 +51,20 @@ sub store
   print $fh "\$sfi = $sfi;\n";
   print $fh "\$a = $a;\n";
   print $fh "\$k = $k;\n";
-  print $fh "\$forecast = '$forecast';\n";
+  print $fh "\$from = '$from';\n";
+  print $fh "\$node = '$node';\n";
   print $fh "\@denied = qw(", join(' ', @denied), ");\n" if @denied > 0;
   print $fh "\@allowed = qw(", join(' ', @allowed), ");\n" if @allowed > 0;
   close $fh;
+
+  # log it
+  $fp->writeunix($date, "$from^$date^$sfi^$a^$k^$forecast^$node");
 }
 
 # update WWV info in one go (usually from a PC23)
 sub update
 {
-  my ($mydate, $mytime, $mysfi, $mya, $myk, $myforecast, $from, $node) = @_;
+  my ($mydate, $mytime, $mysfi, $mya, $myk, $myforecast, $myfrom, $mynode) = @_;
   if ((@allowed && grep {$_ eq $from} @allowed) || 
       (@denied && !grep {$_ eq $from} @denied) ||
          (@allowed == 0 && @denied == 0)) {
@@ -64,6 +75,10 @@ sub update
       $k = 0 + $myk;
       $a = 0 + $mya;
       $forecast = $myforecast;
+         $date = $trydate;
+         $from = $myfrom;
+         $node = $mynode;
+         
          store();
        }
   }
@@ -120,5 +135,79 @@ sub forecast
   @_ ? $forecast = shift : $forecast ;
 }
 
+#
+# print some items from the log backwards in time
+#
+# This command outputs a list of n lines starting from line $from to $to
+#
+sub print
+{
+       my $self = $fp;
+       my $from = shift;
+       my $to = shift;
+       my @date = $self->unixtoj(shift);
+       my $pattern = shift;
+       my $search;
+       my @in;
+       my @out;
+       my $eval;
+       my $count;
+           
+       $search = 1;
+       $eval = qq(
+                          my \$c;
+                          my \$ref;
+                          for (\$c = \$#in; \$c >= 0; \$c--) {
+                                       \$ref = \$in[\$c];
+                                       if ($search) {
+                                               \$count++;
+                                               next if \$count < $from;
+                                               push \@out, print_item(\$ref);
+                                               last LOOP if \$count >= \$to;                  # stop after n
+                                       }
+                               }
+                         );
+       
+       $self->close;                                      # close any open files
+
+       my $fh = $self->open(@date); 
+LOOP:
+       while ($count < $to) {
+               my @spots = ();
+               if ($fh) {
+                       while (<$fh>) {
+                               chomp;
+                               push @in, [ split '\^' ] if length > 2;
+                       }
+                       eval $eval;               # do the search on this file
+                       return ("Spot search error", $@) if $@;
+               }
+               $fh = $self->openprev();      # get the next file
+               last if !$fh;
+       }
+
+       return @out;
+}
+
+#
+# the standard log printing interpreting routine.
+#
+# every line that is printed should call this routine to be actually visualised
+#
+# Don't really know whether this is the correct place to put this stuff, but where
+# else is correct?
+#
+# I get a reference to an array of items
+#
+sub print_item
+{
+       my $r = shift;
+       my @ref = @$r;
+       my $d = cldate($ref[1]);
+       my ($t) = (gmtime($ref[1]))[2];
+
+       return sprintf("$d   %02d %5d %3d %3d %-37s <%s>", $t, $ref[2], $ref[3], $ref[4], $ref[5], $ref[0]);
+}
+
 1;
 __END__;