#!/usr/local/bin/perl
##-------------------------------------------------------------------------
## 
## info_nav -- 
## 
## Author          : Jaccard Pierre
## Created On      : 1999/11/05 12:23:44
## Last Modified By: Jaccard Pierre
## Last Modified On: 1999/11/05 16:30:36
## Update Count    : 73
## Directory       : /dataOS/jaccard/codas/codas3c/gfi/bin/perl/
## Version         : 0.0
## Status          : Unknown
## --------------------------------------------------------------------- ++
## DESCRIPTION: 
## 
##    Undocumented.
## 
## --------------------------------------------------------------------- ++
## REVISIONS: 
## --------------------------------------------------------------------- ++
## CHANGES: 
##-------------------------------------------------------------------------

use strict;
use Getopt::Std;
use IO::File;

#------------------------------------------------------------------
# File Variables
#------------------------------------------------------------------

my(%OPTS, $action);

%OPTS = ();
$action = '';

#------------------------------------------------------------------
# Options
#------------------------------------------------------------------
{

  my($opt_str, $tmp_str, $c);

  $opt_str = 't:';

  ## 1999/09/20 Pierre Jaccard
  if($] < 5.004){
    my($tmp_str);
    
    $tmp_str = $opt_str;
    while($tmp_str){
      $c = chop($tmp_str);
      next if($c !~ /\w/);
      eval( 'use vars qw($opt_' . $c . '); $opt_' . $c . ' = 0;' );
    }
    getopts($opt_str) || die("\n\n");
    $tmp_str = $opt_str;
    while($tmp_str){
      $c = chop($tmp_str);
      next if($c !~ /\w/);
      eval( '$OPTS{' . $c . '} = $opt_' . $c . ' if($opt_' . $c .');' );
    }
  }
  else{
    getopts($opt_str, \%OPTS) || die("\n\n");
  }
    
}

#--- Arguments:
$action = $ARGV[0];
die("\nNo specified action\n") unless($action);

#------------------------------------------------------------------
# Select scanning method according to file type
#------------------------------------------------------------------
{
  
  my($ih);

  $ih = new IO::File "< -";
  die("\nOpening stdin for reading file names\n") unless(defined $ih);

 SWITCH:{
   
   my $t = $OPTS{'t'};

   if($t eq 'gga'){
     info_gga($ih);
     last SWITCH;
   }
   
   die("\nFile type \"$t\" undefined\n");
 }

  close($ih) || die("\nClosing stdin for reading file names\n");

}

exit(0);

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

sub info_gga {

  my($reg);

  my($ih) = @_ ;

  $reg = '^...GGA\,' . '(\d\d)(\d\d)(\d\d)\.(\d+)\,'
    . '(\d\d)(\d\d)\.(\d+)\,([NS])\,'
      . '(\d\d\d)(\d\d)\.(\d+)\,([WE])\,'
        . '(\d+)\,' . '(\d+)\,' . '([\d\.]+)\,' . '([\d\.]+)\,';
  

  #--- Select what to do according to action:
 SWITCH:{
   
   #-------------------------------------------------------
   if($action eq 'file_time_range'){

     my($f, $fh, @beg, @end);
     
     while ($f = <$ih>){
       chomp($f);

       #--- Skip non-file names:
       next unless(-s $f);

       #--- Get first and last time from file:
       $fh = new IO::File "< $f";
       die("\nOpening input file $f\n") unless(defined $fh);
       @beg = get_matching_line( 1, $fh, $reg);
       @end = get_matching_line(-1, $fh, $reg);
       close($fh) || die("\nClosing input file $f\n");
       
       #--- Arrange the time ranges:
       $end[1]++;
       if($end[1] > 59){
         $end[1] = '00';
         $end[0]++;
         $end[0] = '00' if($end[0] > 23);
       }

       #--- Print the time range:
       printf "%s:  YYYY/MM/DD  %02d:%02d  to  YYYY/MM/DD  %02d:%02d\n",
       $f, $beg[0], $beg[1], $end[0], $end[1];
     }

     last SWITCH;

   }
     
   #-------------------------------------------------------
   if($action eq 'transect_time_range'){
     
     my($f, $prev, $this, $f_beg, $f_end);

     #--- 
     # Block to print the time range 
     #---
     my $print_time_range = sub {
       
       my($fh, $t, $n, @beg, @end);

       #--- Get times:
       $fh = new IO::File "< $f_beg";
       die("\nOpening input file $f\n") unless(defined $fh);
       @beg = get_matching_line( 1, $fh, $reg);
       close($fh) || die("\nClosing input file $f\n");
       $fh = new IO::File "< $f_end";
       die("\nOpening input file $f\n") unless(defined $fh);
       @end = get_matching_line(-1, $fh, $reg);
       close($fh) || die("\nClosing input file $f\n");
       
       #--- Get transect and file sub-numbers:
       ($t,$n) = ($f_beg =~ /(\d{3})\D\.(\d+)$/);
       push(@beg, $t, $n);
       ($t,$n) = ($f_end =~ /(\d{3})\D\.(\d+)$/);
       push(@end, $t, $n);
       
       #--- Arrange the time ranges:
       $end[1]++;
       if($end[1] > 59){
         $end[1] = 0;
         $end[0]++;
         $end[0] = 0 if($end[0] > 23);
       }
         
       #--- Print the time range:
       printf "%03d:  %03d  YYYY/MM/DD  %02d:%02d  to  " .
         "%03d  YYYY/MM/DD  %02d:%02d\n",
         $t, pop(@beg), $beg[0], $beg[1], pop(@end), $end[0], $end[1];
       
       return(1);
     };

     while($f = <$ih>){
       chomp($f);

       #--- Skip non-file names:
       next unless(-s $f);

       #--- Get transect number:
       ($this) = ($f =~ /(\d{3})\D\./);
       
       #--- Initialize first transect:
       if(!defined($prev)){
         $prev  = $this;
         $f_beg = $f;
       }
       
       #--- Check for transect changes:
       if($this != $prev){
         &$print_time_range();
         $f_beg = $f;
       }
       $prev  = $this;
       $f_end = $f;
     }
     &$print_time_range();
     last SWITCH;
   }
     
   
   #-------------------------------------------------------
   die("\nUndefined action \"$action\"\n");
     
   }

  return(1);

}
  
#------------------------------------------------------------------

sub get_matching_line {

  my(@match);

  my($pos, $fh, $reg) = @_ ;

  @match = ();

  #--- Select:
 SWITCH: {
   
   if($pos == 1){
     seek($fh, 0, SEEK_SET);
     @match = get_next_matching_line($fh, $reg);
     last SWITCH;
   }
   if($pos == 0.0){
     @match = get_next_matching_line($fh, $reg);
     last SWITCH;
   }
   if($pos == -1){
     seek($fh, -1, SEEK_END);
     @match = get_last_matching_line($fh, $reg);
     last SWITCH;
   }
   
 }

  return(@match);

}

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

sub get_next_matching_line {
 
  my($l, @m);

  my($fh, $reg) = @_ ;

  @m = ();

  while($l = <$fh>){
    last if(@m = ($l =~ /$reg/));
  }

  return(@m);
}

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

sub get_last_matching_line {

  my($l, $c, @m);

  my($fh, $reg) = @_ ;

  @m = ();

  while(1){
    $l = '';
    while((($c = getc($fh)) ne "") && ($c ne "\n")){
      $l = $c . $l;
      seek($fh,-2,SEEK_CUR);
    }
    seek($fh,-2,SEEK_CUR);
    last if(@m = ($l =~ /$reg/o));
  }

  return(@m);

}




