get all the debugging finally into the debug files when things go wrong
[spider.git] / perl / Geomag.pm
index d68e724d436214b64825b2052164122e22628397..ca16e363f445536b19a356d56652affa852c6774 100644 (file)
@@ -14,17 +14,18 @@ use DXVars;
 use DXUtil;
 use DXLog;
 use Julian;
-use FileHandle;
-use Carp;
+use IO::File;
+use DXDebug;
 
 use strict;
-use vars qw($date $sfi $k $a $forecast @allowed @denied $fp $node $from);
+use vars qw($date $sfi $k $a $r $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
+$r = 0;                                                        # the current R value
 $forecast = "";                                        # the current geomagnetic forecast
 $node = "";                                            # originating node
 $from = "";                                            # who this came from
@@ -44,13 +45,14 @@ sub init
 # write the current data away
 sub store
 {
-       my $fh = new FileHandle;
+       my $fh = new IO::File;
        open $fh, "> $param" or confess "can't open $param $!";
        print $fh "# Geomagnetic data parameter file last mod:", scalar gmtime, "\n";
        print $fh "\$date = $date;\n";
        print $fh "\$sfi = $sfi;\n";
        print $fh "\$a = $a;\n";
        print $fh "\$k = $k;\n";
+       print $fh "\$r = $r;\n";
        print $fh "\$from = '$from';\n";
        print $fh "\$node = '$node';\n";
        print $fh "\@denied = qw(", join(' ', @denied), ");\n" if @denied > 0;
@@ -58,19 +60,24 @@ sub store
        close $fh;
        
        # log it
-       $fp->writeunix($date, "$from^$date^$sfi^$a^$k^$forecast^$node");
+       $fp->writeunix($date, "$from^$date^$sfi^$a^$k^$forecast^$node^$r");
 }
 
 # update WWV info in one go (usually from a PC23)
 sub update
 {
-       my ($mydate, $mytime, $mysfi, $mya, $myk, $myforecast, $myfrom, $mynode) = @_;
+       my ($mydate, $mytime, $mysfi, $mya, $myk, $myforecast, $myfrom, $mynode, $myr) = @_;
        if ((@allowed && grep {$_ eq $from} @allowed) || 
                (@denied && !grep {$_ eq $from} @denied) ||
                (@allowed == 0 && @denied == 0)) {
                
                #       my $trydate = cltounix($mydate, sprintf("%02d18Z", $mytime));
                if ($mydate >= $date) {
+                       if ($myr) {
+                               $r = 0 + $myr;
+                       } else {
+                               $r = 0 unless abs ($mysfi - $sfi) > 3;
+                       }
                        $sfi = 0 + $mysfi;
                        $k = 0 + $myk;
                        $a = 0 + $mya;
@@ -125,6 +132,11 @@ sub k
        @_ ? $k = shift : $k ;
 }
 
+sub r
+{
+       @_ ? $r = shift : $r ;
+}
+
 sub a
 {
        @_ ? $a = shift : $a ;
@@ -135,6 +147,7 @@ sub forecast
        @_ ? $forecast = shift : $forecast ;
 }
 
+
 #
 # print some items from the log backwards in time
 #
@@ -155,7 +168,7 @@ sub search
        $eval = qq(
                           my \$c;
                           my \$ref;
-                          for (\$c = \$        #in; \$c >= 0; \$c--) {
+                          for (\$c = \$#in; \$c >= 0; \$c--) {
                                        \$ref = \$in[\$c];
                                        if ($search) {
                                                \$count++;