#!/usr/local/bin/perl -w
#
# navtime.prl: Eric Firing, 96/08/31
# This program is for quickly checking the time of day as
# recorded from the BB ADCP by Transect.  It requires R and
# N files.  The R files must first be scanned by SCANBBS, and
# the output of that is the controlling input file for
# navtime.prl.  Navtime.prl compares the times of the starting
# and ending ensemble to the corresponding times of the first
# GGA messages received at these points.
# Output at present is to stdout only.  For each raw file in
# the scanbbs output, it gives the file name and the gga
# time minus the ensemble time in seconds, start and end.
#
# This is no more than barely working code, with no attempt
# at cleanup, refinement, etc.  It is very inefficient, and
# contains at least twice as many lines as needed.

open(LIST, "test.scn") || die("Can't open list file\n");

while (<LIST>)
{
   tr/\r//d;             # If MSDOS, dump CR.
   s/#.*//;             # Dump pound-sign comments.
   s/^[ \t\f]*//;       # Dump leading white space.
   s/[ \t\f]*$//;       # Dump trailing white space.
   s/ +/ /;             # compress to single spaces between
   ($name, $date0, $time0, $date1, $time1, $dd0, $dd1,
   $ens0, $ens1, $nens, $dens, $tens, $tdif, $tfile) = split(" ");
   push(@namelist, "$name");
   $dd0{$name} = $dd0;
   $dd1{$name} = $dd1;
   $ens0{$name} = $ens0;
   $ens1{$name} = $ens1;
   ($root{$name},$extension{$name}) = split(/\./,$name);
}
close(LIST);

# The next block is not needed at present, but may come in
# handy later.
$last_root = "";
foreach $name (@namelist)
{
   $this_root = $root{$name};
   if ($this_root ne $last_root)
   {
      push(@rootlist, "$this_root");
      $files_by_root{$this_root} = "$name";
      $last_root = $this_root;
   }
   else
   {
      $files_by_root{$this_root} = "$files_by_root{$this_root} $name";
   }
#   $ens = $ens0{$name};
#   print(" $name $ens $root{$name} $extension{$name}\n");
}

foreach $root (@rootlist)
{
#   print "$root:  $files_by_root{$root}\n";
}

foreach $name (@namelist)
{
   print "$name ";
   $ens0 = $ens0{$name};
   $ens1 = $ens1{$name};
   $ens_found = 0;
   $position = "";
   $nav_file = $name;
   $nav_file =~ s/r\./n./;
   ($rt,$ext) = split(/\./,$nav_file);
   $nav_time0 = &search_nav($ens0 - 1);
   if ($position eq "ahead")
   {
      $position = "";
      $nav_file = sprintf("$rt.%03d", $ext -1);
      $nav_time0  = &search_nav($ens0 - 1);
   }
   $ens_found = 0;
   $position = "";
   $nav_file = sprintf("$rt.%03d", $ext);
   $nav_time1 = &search_nav($ens1 - 1);
   if ($nav_time1 eq "")
   {
      $position = "";
      $nav_file = sprintf("$rt.%03d", $ext +1);
      $nav_time1  = &search_nav($ens1 - 1);
   }
   if ($nav_time0)
   {
      $nav_dd0 = &dday($nav_time0);
      $dif_dd0 = $nav_dd0 - ($dd0{$name} - int($dd0{$name}));
   }
   if ($nav_time1)
   {
      $nav_dd1 = &dday($nav_time1);
      $dif_dd1 = $nav_dd1 - ($dd1{$name} - int($dd1{$name}));
   }
   printf(" %5.1lf  %5.1lf\n", $dif_dd0*86400,  $dif_dd1*86400);

}

sub dday
{
   local($hms) = @_;
   local($hh, $mm, $ss);
   $hh = int($hms / 10000);
   $hms = $hms - $hh * 10000;
   $mm = int($hms / 100);
   $ss = $hms - $mm * 100;
   $dd = $hh/24 + $mm/1440 + $ss/86400;
   return($dd);
}


sub search_nav
{
   local($ens_target) = @_;
   local($nav_time) = "";
   open(NAV, $nav_file) || return("");
   while (<NAV>)
   {
      if (m/ENSEMBLE +(\d+)/)
      {
         $this_ens = $1;
         if ($this_ens > $ens_target && !$ens_found)
         {
            $position = "ahead";
            last;
         }
         elsif ($this_ens == $ens_target)
         {
            $ens_found = 1;
         }
      }
      if ($ens_found)
      {
         if (m/GPGGA,(\d+)/)
         {
            $nav_time = $1;
            last;
         }
      }
   }
   close(NAV);
   return $nav_time;
}
