added 4W and TX0 to wpxloc.raw and rsgb.cty
[spider.git] / perl / create_prefix.pl
index d21f30f7eb0f585505c441ec46882dec85bd0894..9c98a46c81ebdf0bfd617fd686d9bd4382360be0 100755 (executable)
@@ -20,30 +20,31 @@ BEGIN {
 
 use DXVars;
 use Data::Dumper;
+use strict;
 
-%loc = ();        # the location unique hash
-$nextloc = 1;     # the next location number
-%locn = ();       # the inverse of the above
-%pre = ();        # the prefix hash
-%pren = ();       # the inverse
+my %loc = ();                                          # the location unique hash
+my $nextloc = 1;                                       # the next location number
+my %locn = ();                                         # the inverse of the above
+my %pre = ();                                          # the prefix hash
+my %pren = ();                                         # the inverse
 
 # open the input file
-$ifn = $ARGV[0] if $ARGV[0];
-$ifn = "$data/wpxloc.raw" if !$fn;
+my $ifn = $ARGV[0] if $ARGV[0];
+$ifn = "$main::data/wpxloc.raw" if !$ifn;
 open (IN, $ifn) or die "can't open $ifn ($!)";
 
 # first pass, find all the 'master' location records
 while (<IN>) {
-  next if /^\!/;    # ignore comment lines
-  chomp;
-  @f  = split;       # get each 'word'
-  next if @f == 0;   # ignore blank lines
-
-  if ($f[14] eq '@' || $f[15] eq '@') {
-    $locstr = join ' ', @f[1..13];
-    $loc = $loc{$locstr};
-    $loc = addloc($locstr) if !$loc;
-  }
+       next if /^\!/;                          # ignore comment lines
+       chomp;
+       my @f  = split;                         # get each 'word'
+       next if @f == 0;                        # ignore blank lines
+
+       if (($f[14] && $f[14] eq '@') || ($f[15] && $f[15] eq '@')) {
+               my $locstr = join ' ', @f[1..13];
+               my $loc = $loc{$locstr};
+               $loc = addloc($locstr) if !$loc;
+       }
 }
 
 #foreach $loc (sort {$a <=> $b;} keys %locn) {
@@ -53,33 +54,36 @@ while (<IN>) {
 # go back to the beginning and this time add prefixes (adding new location entries, if required)
 seek(IN, 0, 0);
 
+my $line;
 while (<IN>) {
-  $line++;
-  next if /^\!/;    # ignore comment lines
-  chomp;
-  @f  = split;       # get each 'word'
-  next if @f == 0;   # ignore blank lines
+       $line++;
+       chomp;
+       next if /^\s*\!/;                               # ignore comment lines
+       next if /^\s*$/;
+       
+       my @f  = split;                         # get each 'word'
+       next if @f == 0;                        # ignore blank lines
   
-  # location record
-  $locstr = join ' ', @f[1..13];
-  $loc = $loc{$locstr};
-  $loc = addloc($locstr) if !$loc;
+       # location record
+       my $locstr = join ' ', @f[1..13];
+       my $loc = $loc{$locstr};
+       $loc = addloc($locstr) if !$loc;
   
-  @prefixes = split /,/, $f[0];
-  foreach $p (@prefixes) {
-    my $ref;
+       my @prefixes = split /,/, $f[0];
+       foreach my $p (@prefixes) {
+               my $ref;
        
-       if ($p =~ /#/) {
-         my $i;
-         for ($i = 0; $i < 9; ++$i) {
-           my $t = $p;
-               $t =~ s/#/$i/;
-               addpre($t, $loc);
-         }
-       } else {
-         addpre($p, $loc);
-    }  
-  }
+               if ($p =~ /#/) {
+                       my $i;
+                       for ($i = 0; $i < 9; ++$i) {
+                               my $t = $p;
+                               $t =~ s/#/$i/;
+                               addpre($t, $loc);
+                       }
+               } else {
+                       addpre($p, $loc);
+               }       
+       }
 }
 
 close(IN);
@@ -87,52 +91,57 @@ close(IN);
 #print Data::Dumper->Dump([\%pre, \%locn], [qw(pre locn)]);
 
 # now open the rsgb.cty file and process that again the prefix file we have
-open(IN, "$data/rsgb.cty") or die "Can't open $data/rsgb.cty ($!)";
+open(IN, "$main::data/rsgb.cty") or die "Can't open $main::data/rsgb.cty ($!)";
+$line = 0;
 while (<IN>) {
-  chomp;
-  @f = split /:\s+|;/;
-  my $p = uc $f[4];
-  my $ref = $pre{$p};
-  if ($ref) {
-    # split up the alias string
-       my @alias = split /=/, $f[5];
-       my $a;
-       foreach $a (@alias) {
-         next if $a eq $p;  # ignore if we have it already
-         my $nref = $pre{$a};
-         $pre{$a} = $ref if !$nref;       # copy the original ref if new 
+       $line++;
+       next if /^\s*#/;
+       next if /^\s*$/;
+       my $l = $_;
+       chomp;
+       my @f = split /:\s+|;/;
+       my $p = uc $f[4];
+       my $ref = $pre{$p};
+       if ($ref) {
+               # split up the alias string
+               my @alias = split /=/, $f[5];
+               my $a;
+               foreach $a (@alias) {
+                       next if $a eq $p;       # ignore if we have it already
+                       my $nref = $pre{$a};
+                       $pre{$a} = $ref if !$nref; # copy the original ref if new 
+               }
+       } else {
+               print "line $line: unknown prefix '$p' on $l in rsgb.cty\n";
        }
-  } else {
-    print "unknown prefix $p\n";
-  }
 }
 
-open(OUT, ">$data/prefix_data.pl") or die "Can't open $data/prefix_data.pl ($!)";
+open(OUT, ">$main::data/prefix_data.pl") or die "Can't open $main::data/prefix_data.pl ($!)";
 
 print OUT "\%pre = (\n";
-foreach $k (sort keys %pre) {
-  my $ans = printpre($k);
-  print OUT "  '$k' => '$ans',\n";
+foreach my $k (sort keys %pre) {
+       my $ans = printpre($k);
+       print OUT "  '$k' => '$ans',\n";
 }
 print OUT ");\n\n";
 
 print OUT "\n\%prefix_loc = (\n";
-foreach $l (sort {$a <=> $b} keys %locn) {
-  print OUT "   $l => bless( {";
-  my ($name, $dxcc, $itu, $cq, $utcoff, $latd, $latm, $lats, $latl, $longd, $longm, $longs, $longl) = split /\s+/, $locn{$l};
+foreach my $l (sort {$a <=> $b} keys %locn) {
+       print OUT "   $l => bless( {";
+       my ($name, $dxcc, $itu, $cq, $utcoff, $latd, $latm, $lats, $latl, $longd, $longm, $longs, $longl) = split /\s+/, $locn{$l};
   
-  $longd += ($longm/60);
-  $longd = 0-$longd if (uc $longl) eq 'W'; 
-  $latd += ($latm/60);
-  $latd = 0-$latd if (uc $latl) eq 'S';
-  print OUT " name => '$name',";
-  print OUT " dxcc => $dxcc,";
-  print OUT " itu => $itu,";
-  print OUT " cq => $cq,";
-  print OUT " utcoff => $utcoff,";
-  print OUT " lat => $latd,";
-  print OUT " long => $longd";
-  print OUT " }, 'Prefix'),\n";
+       $longd += ($longm/60);
+       $longd = 0-$longd if (uc $longl) eq 'W'; 
+       $latd += ($latm/60);
+       $latd = 0-$latd if (uc $latl) eq 'S';
+       print OUT " name => '$name',";
+       print OUT " dxcc => $dxcc,";
+       print OUT " itu => $itu,";
+       print OUT " cq => $cq,";
+       print OUT " utcoff => $utcoff,";
+       print OUT " lat => $latd,";
+       print OUT " long => $longd";
+       print OUT " }, 'Prefix'),\n";
 }
 print OUT ");\n\n";
 
@@ -140,31 +149,31 @@ close(OUT);
 
 sub addpre
 {
-  my ($p, $ent) = @_;
-  my $ref = $pre{$p};
-  $ref = $pre{$p} = [] if !$ref;
-  push @{$ref}, $ent;;
+       my ($p, $ent) = @_;
+       my $ref = $pre{$p};
+       $ref = $pre{$p} = [] if !$ref;
+       push @{$ref}, $ent;;
 }
 
 sub printpre
 {
-  my $p = shift;
-  my $ref = $pre{$p};
-  my $out;
-  my $r;
+       my $p = shift;
+       my $ref = $pre{$p};
+       my $out;
+       my $r;
   
-  foreach $r (@{$ref}) {
-    $out .= "$r,";
-  }
-  chop $out;
-  return $out;
+       foreach $r (@{$ref}) {
+               $out .= "$r,";
+       }
+       chop $out;
+       return $out;
 }
 
 sub addloc
 {
-  my $locstr = shift;
-  $locstr =~ s/\'/\\'/g;
-  my $loc = $loc{$locstr} = $nextloc++;
-  $locn{$loc} = $locstr;
-  return $loc;
+       my $locstr = shift;
+       $locstr =~ s/\'/\\'/g;
+       my $loc = $loc{$locstr} = $nextloc++;
+       $locn{$loc} = $locstr;
+       return $loc;
 }