some detail changes in the send routines and spot formatting
[spider.git] / perl / Spot.pm
1 #
2 # the dx spot handler
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package Spot;
10
11 use FileHandle;
12 use DXVars;
13 use DXDebug;
14 use DXUtil;
15 use Julian;
16 use Prefix;
17 use Carp;
18
19 @ISA = qw(Julian);
20
21 use strict;
22 use vars qw($fp $maxspots $defaultspots $maxdays $dirprefix);
23
24 $fp = undef;
25 $maxspots = 50;      # maximum spots to return
26 $defaultspots = 10;    # normal number of spots to return
27 $maxdays = 35;        # normal maximum no of days to go back
28 $dirprefix = "$main::data/spots";
29
30 sub prefix
31 {
32   return $dirprefix;
33 }
34
35 # add a spot to the data file (call as Spot::add)
36 sub add
37 {
38   my @spot = @_;    # $freq, $call, $t, $comment, $spotter = @_
39
40   # sure that the numeric things are numeric now (saves time later)
41   $spot[0] = 0 + $spot[0];
42   $spot[2] = 0 + $spot[2];
43   
44   # remove ssid if present on spotter
45   $spot[4] =~ s/-\d+$//o;
46
47   # compare dates to see whether need to open another save file (remember, redefining $fp 
48   # automagically closes the output file (if any))
49   my @date = Julian::unixtoj($spot[2]);
50   $fp = Spot->open(@date, ">>") if (!$fp || Julian::cmp(@date, $fp->{year}, $fp->{day}));
51
52   # save it
53   my $fh = $fp->{fh};
54
55   # add the 'dxcc' country on the end
56   my @dxcc = Prefix::extract($spot[1]);
57   push @spot, (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
58
59   my $buf = join("\^", @spot);
60   $fh->print($buf, "\n");
61   
62   return $buf;
63 }
64
65 # search the spot database for records based on the field no and an expression
66 # this returns a set of references to the spots
67 #
68 # the expression is a legal perl 'if' statement with the possible fields indicated
69 # by $f<n> where :-
70 #
71 #   $f0 = frequency
72 #   $f1 = call
73 #   $f2 = date in unix format
74 #   $f3 = comment
75 #   $f4 = spotter
76 #   $f5 = dxcc country
77 #
78 # In addition you can specify a range of days, this means that it will start searching
79 # from <n> days less than today to <m> days less than today
80 #
81 # Also you can select a range of entries so normally you would get the 0th (latest) entry
82 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
83 #
84 # This routine is designed to be called as Spot::search(..)
85 #
86
87 sub search
88 {
89   my ($expr, $dayfrom, $dayto, $from, $to) = @_;
90   my $eval;
91   my @out;
92   my $ref;
93   my $i;
94   my $count;
95   my @today = Julian::unixtoj(time);
96   my @fromdate;
97   my @todate;
98   
99   if ($dayfrom > 0) {
100     @fromdate = Julian::sub(@today, $dayfrom);
101   } else {
102     @fromdate = @today;
103         $dayfrom = 0;
104   }
105   if ($dayto > 0) {
106     @todate = Julian::sub(@fromdate, $dayto);
107   } else {
108     @todate = Julian::sub(@fromdate, $maxdays);
109   }
110   if ($from || $to) {
111     $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
112   } else {
113     $from = 0;
114         $to = $defaultspots;
115   }
116
117   $expr =~ s/\$f(\d)/\$ref->[$1]/g;               # swap the letter n for the correct field name
118 #  $expr =~ s/\$f(\d)/\$spots[$1]/g;               # swap the letter n for the correct field name
119   
120   dbg("search", "expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n");
121   
122   # build up eval to execute
123   $eval = qq(
124 #    while (<\$fh>) {
125 #         chomp;
126 #         my \@spots = split /\\^/o;
127 #         if ($expr) {                # note NO \$expr
128 #           \$count++;
129 #               next if \$count < \$from;                  # wait until from 
130 #               push(\@out, \\\@spots);
131 #               last LOOP if \$count >= \$to;                  # stop after to
132 #         }
133 #       }
134     my \$c;
135         my \$ref;
136     for (\$c = \$#spots; \$c >= 0; \$c--) {
137           \$ref = \$spots[\$c];
138           if ($expr) {
139             \$count++;
140                 next if \$count < \$from;                  # wait until from 
141         push(\@out, \$ref);
142                 last LOOP if \$count >= \$to;                  # stop after to
143           }
144     }
145   );
146
147 LOOP:
148   for ($i = 0; $i < $maxdays; ++$i) {             # look thru $maxdays worth of files only
149     my @now = Julian::sub(@fromdate, $i);         # but you can pick which $maxdays worth
150         last if Julian::cmp(@now, @todate) <= 0;         
151         
152         my @spots = ();
153         my $fp = Spot->open(@now);  # get the next file
154         if ($fp) {
155           my $fh = $fp->{fh};
156           my $in;
157           foreach $in (<$fh>) {
158             chomp $in;
159        push @spots, [ split('\^', $in) ];
160           }
161           eval $eval;               # do the search on this file
162           return ("Spot search error", $@) if $@;
163         }
164   }
165
166   return @out;
167 }
168
169 # open a spot file of the Julian day
170 sub open
171 {
172   my $pkg = shift;
173   return Julian::open("spot", $dirprefix, @_);
174 }
175
176 # close a spot file
177 sub close
178 {
179   # do nothing, unreferencing or overwriting the $self will close it  
180 }
181
182 # format a spot for user output in 'broadcast' mode
183 sub formatb
184 {
185   my @dx = @_;
186   my $t = ztime($dx[2]);
187   return sprintf "DX de %-7.7s: %13.1f %-12.12s %-30s<%s>", $dx[4], $dx[0], $dx[1], $dx[3], $t ;
188 }
189
190 # format a spot for user output in list mode
191 sub formatl
192 {
193   my @dx = @_;
194   my $t = ztime($dx[2]);
195   my $d = cldate($dx[2]);
196   return sprintf "%9.1f %-12s %s %s %-30s<%s>", $dx[0], $dx[1], $d, $t, $dx[3], $dx[4] ;
197 }
198
199 1;