2 # a program to create a prefix file from a wpxloc.raw file
4 # Copyright (c) - Dirk Koopman G1TLH
11 # search local then perl directories
13 # root of directory tree for this system
15 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
17 mkdir "$root/local_data", 02777 unless -d "$root/local_data";
19 unshift @INC, "$root/perl"; # this IS the right way round!
20 unshift @INC, "$root/local";
32 my %loc = (); # the location unique hash
33 my $nextloc = 1; # the next location number
34 my %locn = (); # the inverse of the above
35 my %pre = (); # the prefix hash
36 my %pren = (); # the inverse
41 if (@ARGV && $ARGV[0] =~ /^-?-?syst?e?m?$/) {
42 $prefix = $main::data;
45 say "create_prefix.pl: creating SYSTEM prefix files";
47 $prefix = $main::local_data;
48 say "create_prefix.pl: creating LOCAL prefix files";
53 $ifn = $system ? "$main::data/wpxloc.raw" : "$prefix/wpxloc.raw";
54 unless (open (IN, $ifn)) {
55 $ifn = "$main::data/wpxloc.raw";
56 open(IN, $ifn) or die "can't open $ifn ($!)";
59 # first pass, find all the 'master' location records
61 next if /^\!/; # ignore comment lines
63 my @f = split; # get each 'word'
64 next if @f == 0; # ignore blank lines
66 if (($f[14] && $f[14] eq '@') || ($f[15] && $f[15] eq '@')) {
67 my $locstr = join ' ', @f[1..13];
68 my $loc = $loc{$locstr};
69 $loc = addloc($locstr) if !$loc;
73 #foreach $loc (sort {$a <=> $b;} keys %locn) {
74 # print "loc: $loc data: $locn{$loc}\n";
77 # go back to the beginning and this time add prefixes (adding new location entries, if required)
84 next if /^\s*\!/; # ignore comment lines
87 my @f = split; # get each 'word'
88 next if @f == 0; # ignore blank lines
91 my $locstr = join ' ', @f[1..13];
92 my $loc = $loc{$locstr};
93 $loc = addloc($locstr) if !$loc;
95 my @prefixes = split /,/, $f[0];
96 foreach my $p (@prefixes) {
101 for ($i = 0; $i < 9; ++$i) {
114 #print Data::Dumper->Dump([\%pre, \%locn], [qw(pre locn)]);
116 # now open the cty.dat file if it is there
118 $ifn = $system ? "$main::data/cty.dat" : "$prefix/cty.dat";
119 unless ($r = open (IN, $ifn)) {
120 $ifn = "$main::data/cty.dat";
140 } elsif ($state == 1) {
145 push @a, split /\s*,/;
146 $f[7] =~ s/^\*\s*//; # remove any preceeding '*' before a callsign
147 ct($_, uc $f[7], @a) if @a;
150 push @a, split /\s*,/;
158 open(OUT, ">$prefix/prefix_data.pl") or die "Can't open $prefix/prefix_data.pl ($!)";
160 print OUT "\%pre = (\n";
161 foreach my $k (sort keys %pre) {
162 my $ans = printpre($k);
163 print OUT " '$k' => '$ans',\n";
167 print OUT "\n\%prefix_loc = (\n";
168 foreach my $l (sort {$a <=> $b} keys %locn) {
169 print OUT " $l => bless( {";
170 my ($name, $dxcc, $itu, $cq, $utcoff, $latd, $latm, $lats, $latl, $longd, $longm, $longs, $longl) = split /\s+/, $locn{$l};
172 $longd += ($longm/60);
173 $longd = 0-$longd if (uc $longl) eq 'W';
175 $latd = 0-$latd if (uc $latl) eq 'S';
176 my $qra = DXBearing::lltoqra($latd, $longd);
177 print OUT " name => '$name',";
178 print OUT " dxcc => $dxcc,";
179 print OUT " itu => $itu,";
180 print OUT " cq => $cq,";
181 print OUT " utcoff => $utcoff,";
182 print OUT " lat => $latd,";
183 print OUT " long => $longd,";
184 print OUT " qra => '$qra'";
185 print OUT " }, 'Prefix'),\n";
195 $ref = $pre{$p} = [] if !$ref;
206 foreach $r (@{$ref}) {
222 # for now remove (nn) [nn]
223 my ($itu) = $a =~ /(\(\d+\))/; $a =~ s/(\(\d+\))//g;
224 my ($cq) = $a =~ /(\[\d+\])/; $a =~ s/(\[\d+\])//g;
225 my ($lat, $long) = $a =~ m{(<[-+\d.]+/[-+\d.]+>)}; $a =~ s{(<[-+\d.]+/[-+\d.]+>)}{}g;
226 my ($cont) = $a =~ /(\{[A-Z]{2}\})/; $a =~ s/(\{[A-Z]{2}\})//g;
229 print "line $line: blank prefix on $l in cty.dat\n";
232 next if $a eq $p; # ignore if we have it already
234 $pre{$a} = $ref if !$nref; # copy the original ref if new
237 print "line $line: unknown prefix '$p' on $l in cty.dat\n";
244 $locstr =~ s/\'/\\'/g;
245 my $loc = $loc{$locstr} = $nextloc++;
246 $locn{$loc} = $locstr;