=head1 DESCRIPTION Add latitude and longitude to tab-separated data =cut $usage = "Usage:\n\tperl -w $0 waypoint-file name_col col1 col2 [ --dskip pattern ] [ data-file ]"; =head1 COMMAND LINE PARAMETERS perl -w ... waypoint-file name_col col1 col2 [ --dskip pattern ] [ data-file ] waypoint-file has one waypoint per line, in the format: name[tab]coord1[tab]coord2 lines beginning with "#" are ignored. data-file contains the tab-separated data; if not given, standard input is used. This file should have waypoint names in column name_col. The coordinates are inserted at columns col1 and col2, respectively. Lines in the input which contain the pattern, given on the command line by "--dskip pattern", are ignored. Use "--d-ignore-prefix xxx" to ignore a prefix on waypoint names within the data file, the default is ignore "#" for example treat "#001" like "001" when looking up the waypoint. =cut sub errx { print STDERR $_[0], "\n"; exit 1; } ## ## get the command-line parameters ## $wfile = shift or &errx($usage); $name_col = shift or &errx($usage); $col1 = shift or &errx($usage); $col2 = shift or &errx($usage); $debug = 0; $dskip_pattern = qr/^YEAR|^\s*$/; $d_ignore_prefix = "#"; while (@ARGV+0 && $ARGV[0] =~ /^-/) { my $aa = $ARGV[0]; last if $aa eq '--'; if ($aa eq '--dskip') { $dskip_pattern = $ARGV[1]; shift @ARGV; shift @ARGV; } elsif ($aa eq '--d-ignore-prefix') { $d_ignore_prefix = $ARGV[1]; shift @ARGV; shift @ARGV; } elsif ($aa eq '-d') { $debug = 1; shift @ARGV; } else { print STDERR "bad arg: $aa\n"; &errx($usage); } } # $dfile = shift or &errx($usage); ## ## read in the waypoints ## open(WFILE, "< $wfile") or &errx("cannot open $wfile: $!"); $format = "simple"; %wp = (); WPLINE: while () { print STDERR "WPLINE: $_" if $debug; chomp; if ($format eq 'simple') { next WPLINE if /^#/; @f = split /\t/; ($name,$aaa,$bbb) = ($f[0],$f[1],$f[2]); print STDERR ":$name:", "\n" if $debug; } $wp{$name} = [ $aaa, $bbb ]; } close(WFILE); ## ## filter the tab-separated data ## $chomped = 1; $nout = 0; DLINE: while (<>) { chomp; #print STDERR "line $.\n"; @f = split "\t"; if ( defined($dskip_pattern) && $_ =~ $dskip_pattern ) { print $_; print "\n" if $chomped; $nout++; next DLINE; } $wpname = $f[$name_col]; if ( defined($d_ignore_prefix) ) { $wpname =~ s/^$d_ignore_prefix//; } $wp = $wp{$wpname}; ref $wp or errx "unknown waypoint '$wpname' at row $."; $aaa = $$wp[0]; $bbb = $$wp[1]; $f[$col1] = $aaa; $f[$col2] = $bbb; print join("\t", @f); print "\n" if $chomped; $nout++; print STDERR "Input line $., output line $nout\n"; }