#!/usr/local/bin/perl -w
# take one or more files and run nmea_gps.cnt on them
#   specify data by file list 
#
# usage:
#  nmea_gps.prl -h file1 [file2 ...]
# -h                   "help!!"
#
# eg: nmea_gps.prl 

# JH 2000/08/17 (hacked from newday.prl)

use Getopt::Std;
use Cwd;
use  Time::Local;

$PROGRAM = "nmea_gps.prl";

if ($#ARGV < 0) {
  (system("/home/ulili/programs/matlab/rdi/$PROGRAM -h ") 
   && die "NOTE: ---> error reading commandline <$orig_commandline>\n");
}

$| = 1;         #print immediately: no buffering of output
$orig_commandline = join(' ',@ARGV);

$programs_dir = "/home/ulili/programs";

###-----------------------------------------------------------------------###

$opt_h = 0;        #no help
$opt_r = 0;
getopts("hr") || 
  (system("/home/ulili/programs/matlab/rdi/$PROGRAM -h ") 
   && die "NOTE: ---> error reading commandline <$orig_commandline>\n");


if ($opt_r) {
   $require_checksum = "REQUIRE_CHECKSUM";
}
else {
   $require_checksum = "";
}

#now use the values from the switches


$ostype = $^O;
if ($ostype eq "solaris") {
   $bin_dir = "$programs_dir/codas3/bin/sol/";
} elsif ($ostype eq "linux") {
   $bin_dir = "$programs_dir/codas3/bin/lnx/";
}
else {
   die "only solaris and linux are set up at the moment\n";
}

@filelist = @ARGV;
   
if ( $opt_h)     { &usage("you asked for help");    exit(1);}


foreach $filename (@filelist)      {

   #get yearbase, month, and day from file start
   @fileparts = split(/\./, $filename);
   $basename = $fileparts[0];
   open(FID, "$filename");
   
   $count = 1;
   while (<FID>) {
      if ($count == 1) {
         $first_line = $_;
         $count = -1;
      }
   }
   close(FID);
   #this line looks like Tue Oct 17 07:26:00 2000
   @lineparts = split(/ /, $first_line);
   $yearbase = pop @lineparts;
   $junk = pop @lineparts;
   $day = pop @lineparts;
   $month = pop @lineparts;
   $junk = pop @lineparts;

   if ($month eq "Jan") {$monthnum = 1;}
   if ($month eq "Feb") {$monthnum = 2;}
   if ($month eq "Mar") {$monthnum = 3;}
   if ($month eq "Apr") {$monthnum = 4;}
   if ($month eq "May") {$monthnum = 5;}
   if ($month eq "Jun") {$monthnum = 6;}
   if ($month eq "Jul") {$monthnum = 7;}
   if ($month eq "Aug") {$monthnum = 8;}
   if ($month eq "Sep") {$monthnum = 9;}
   if ($month eq "Oct") {$monthnum = 10;}
   if ($month eq "Nov") {$monthnum = 11;}
   if ($month eq "Dec") {$monthnum = 12;}

   $datestr = sprintf("%d/%02d/%02d", $yearbase, $monthnum, $day);


   $cntfile = "nmea_gps.tmp";
   open(TMP, ">$cntfile");
   print TMP <<"END_TMP";
   YMD_BASE:              $datestr
    TIME_RANGE:            ALL
   MESSAGE:              GPGGA
   $require_checksum
   OUTPUT_MAT_FILE:      $basename
    subsamples=         1
    max_HDOP=           6
    SAVE_HDOP
    SAVE_NUM_OF_SAT
    GAP_INCLUDED
  end

  INPUT_GPS_FILES:

   ${basename}.aux
END_TMP

   close TMP;
   print "parsing $filename\n";
#   &cat($cntfile);
   system("$bin_dir/nmea_gps $cntfile");
}

##############################################################################
sub cat
  {
    local($cntfile) = shift(@_);
    open(CAT, "$cntfile") and  print <CAT>;
    close(CAT);
  }



sub usage
  {
    print <<"END_STDOUT";
    run nmea_gps on a list of files
    usage:
      nmea_gps.prl -r file1 [file2 ...]
       -r     (require checksum)
       -h                   "help!!"
 
      eg: nmea_gps.prl *.aux

END_STDOUT

  }
