#!/usr/bin/perl
#  edit_PEDR_table.pl -- This program reads in a MOLA PEDR ASCII table, 
#  and parses out data for desired latitudes and longitudes. The output
#  is to stdout.

use Getopt::Std;
getopts('a:b:c:d:e:');

$usage = "edit_PEDR_table.pl -a <PEDR ASCII table> -b <minimum longitude> -c <maximum longitude> -d <minimum latitude> -e <maximum latitude>";

if (! ($opt_a && $opt_b && $opt_c && $opt_d && $opt_e)) { die "\nUsage: $usage\n\n"; }

open(PEDRTABLE,"$opt_a") || die "Could not open file.\n";

while(<PEDRTABLE>) {

  $line = $_;
  if (( $line =~ /long/ ) || ( $line =~ /---/ )) {
     print $line; }                          #Print header    

  if ( $line =~ /^(.\d+\.\d+)\s+(\S+)\s/ ) {  #Grab the first two columns of data

     $longitude = $1;     
     $latitude = $2;

     if (( $longitude >= $opt_b ) && ( $longitude <= $opt_c ) && ( $latitude >= $opt_d ) && ( $latitude <= $opt_e )) {      #Check if the longitude and latitude
                                            #are within specified parameters
        print $line; }   

  }

}

