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