4 # Copyright (c) - 1998 Dirk Koopman G1TLH
21 use vars qw($fp $maxspots $defaultspots $maxdays $dirprefix);
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
31 mkdir "$dirprefix", 0777 if !-e "$dirprefix";
32 $fp = DXLog::new($dirprefix, "dat", 'd');
40 # add a spot to the data file (call as Spot::add)
43 my @spot = @_; # $freq, $call, $t, $comment, $spotter = @_
44 my @out = @spot[0..4]; # just up to the spotter
46 # sure that the numeric things are numeric now (saves time later)
47 $spot[0] = 0 + $spot[0];
48 $spot[2] = 0 + $spot[2];
50 # remove ssids if present on spotter
51 $out[4] =~ s/-\d+$//o;
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 my $spotted_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
56 my $spotted_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
57 my $spotted_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
58 push @out, $spotted_dxcc;
59 @dxcc = Prefix::extract($out[4]);
60 my $spotter_dxcc = (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
61 my $spotter_itu = (@dxcc > 0 ) ? $dxcc[1]->itu() : 0;
62 my $spotter_cq = (@dxcc > 0 ) ? $dxcc[1]->cq() : 0;
63 push @out, $spotter_dxcc;
66 my $buf = join("\^", @out);
68 # compare dates to see whether need to open another save file (remember, redefining $fp
69 # automagically closes the output file (if any)).
70 $fp->writeunix($out[2], $buf);
72 return (@out, $spotted_itu, $spotted_cq, $spotter_itu, $spotter_cq);
75 # search the spot database for records based on the field no and an expression
76 # this returns a set of references to the spots
78 # the expression is a legal perl 'if' statement with the possible fields indicated
83 # $f2 = date in unix format
86 # $f5 = spotted dxcc country
87 # $f6 = spotter dxcc country
91 # In addition you can specify a range of days, this means that it will start searching
92 # from <n> days less than today to <m> days less than today
94 # Also you can select a range of entries so normally you would get the 0th (latest) entry
95 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
97 # This routine is designed to be called as Spot::search(..)
102 my ($expr, $dayfrom, $dayto, $from, $to) = @_;
108 my @today = Julian::unixtoj(time());
112 $dayfrom = 0 if !$dayfrom;
113 $dayto = $maxdays if !$dayto;
114 @fromdate = Julian::sub(@today, $dayfrom);
115 @todate = Julian::sub(@fromdate, $dayto);
116 $from = 0 unless $from;
117 $to = $defaultspots unless $to;
119 $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
121 $expr =~ s/\$f(\d)/\$ref->[$1]/g; # swap the letter n for the correct field name
122 # $expr =~ s/\$f(\d)/\$spots[$1]/g; # swap the letter n for the correct field name
124 dbg("search", "expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n");
126 # build up eval to execute
130 for (\$c = \$#spots; \$c >= 0; \$c--) {
131 \$ref = \$spots[\$c];
134 next if \$count < \$from; # wait until from
136 last if \$count >= \$to; # stop after to
141 $fp->close; # close any open files
143 for ($i = $count = 0; $i < $maxdays; ++$i) { # look thru $maxdays worth of files only
144 my @now = Julian::sub(@fromdate, $i); # but you can pick which $maxdays worth
145 last if Julian::cmp(@now, @todate) <= 0;
148 my $fh = $fp->open(@now); # get the next file
153 push @spots, [ split '\^' ];
155 eval $eval; # do the search on this file
156 last if $count >= $to; # stop after to
157 return ("Spot search error", $@) if $@;
164 # format a spot for user output in 'broadcast' mode
168 my $t = ztime($dx[2]);
169 return sprintf "DX de %-7.7s%11.1f %-12.12s %-30s %s", "$dx[4]:", $dx[0], $dx[1], $dx[3], $t ;
172 # format a spot for user output in list mode
176 my $t = ztime($dx[2]);
177 my $d = cldate($dx[2]);
178 return sprintf "%8.1f %-11s %s %s %-28.28s%7s>", $dx[0], $dx[1], $d, $t, $dx[3], "<$dx[4]" ;
182 # return all the spots from a day's file as an array of references
183 # the parameter passed is a julian day
188 my $fh = $fp->open(@_);
193 push @spots, [ split '\^' ];