#!/usr/bin/env perl
#--------------------------------------------------------------------
#    The MB-system:	mbm_stat.perl	8/14/93
#
#    Copyright (c) 1993-2023 by
#    David W. Caress (caress@mbari.org)
#      Monterey Bay Aquarium Research Institute
#      Moss Landing, California, USA
#    Dale N. Chayes 
#      Center for Coastal and Ocean Mapping
#      University of New Hampshire
#      Durham, New Hampshire, USA
#    Christian dos Santos Ferreira
#      MARUM
#      University of Bremen
#      Bremen Germany
#      
#    MB-System was created by Caress and Chayes in 1992 at the
#      Lamont-Doherty Earth Observatory
#      Columbia University
#      Palisades, NY 10964
#
#    See README.md file for copying and redistribution conditions.
#--------------------------------------------------------------------
#
# Command:
#   mbm_stat
#
# Purpose:
#   Perl shellscript to extract beam statistics from mbinfo output.
#
# Usage:
#   mbm_stat -Ifile [-V -H]
#
# Author:
#   David W. Caress
#   Lamont-Doherty Earth Observatory
#   Palisades, NY  10964
#   August 14, 1993
#
#
#
#
$program_name = "mbm_stat";

# Deal with command line arguments
&Getopts('I:i:VvHh');
$file =    ($opt_I || $opt_i);
$help =    ($opt_H || $opt_h);
$verbose = ($opt_V || $opt_v);

# print out help message if required
if ($help)
	{
	print "\n$program_name:\n";
	print "\nPerl shellscript to extract beam statistics from the ouput of mbinfo.\n";
	print "\nUsage: $program_name -Ifile [-V -H]\n";
	exit 0;
	}

# check for defined parameters
if (!$file)
	{
	die "No input file specified - $program_name aborted\n";
	}

# Read and deal with the data
open(F1,"$file") || die "$program_name: Can't find input file $file \n";
$count = 0;

if ($verbose)
	{
	print "\nProgram ",$program_name,"\n";
	print "\nInput File: ",$file,"\n";
	}
printf("File\t\t\tHours\t  Drops\t  Flags\n");
$done = 0;

while (<F1>)
	{
		$count++;
		if (/^Multibeam Data File:  (.*)/) {
			$data_file = $1;
			@parts=split(/\//,$data_file);
			$file = @parts[scalar(@parts)-1];
			$done = 0;
			}


		elsif (/^  Number of Beams:(.*)/) {
			if ( $done == 0 ) {
				$beams = $1;
				}
			}

		elsif (/^  Number of Zero Beams:(.*)/) {
			if ( $done == 0)
				{
				$zeros = $1;
				}
			}
		elsif (/^  Number of flagged beams:(.*)/) {
			if ( $done == 0)
				{
				$flags = $1;
				$done = 1;
				}
			}
		elsif (/^Total Time: (.*)/) {
				$duration = $1;
				printf("%s\t%7.4f\t%6.2f\t%6.2f\n", $file,
				$duration, $zeros/$beams*100.,
				$flags/$beams*100.);
			}

#	if ($done == 1) {
#		printf("%s\t%7.4f\t%6.2f\t%6.2f\n", $file, $duration,
#			$zeros/$beams*100., $flags/$beams*100.);
#	$done = 0;
#	}

	if ( $verbose )
		{
		printf("%4d\tData file:\t%s\n",$count,$data_file);
		printf("%6d\t%6d\t%6d\n", $beams, $zeros, $flags);
		}

}


close(F1);

# Announce success whether it is deserved or not.
print "\nAll done\n";
exit 0;

#-----------------------------------------------------------------------
# This should be loaded from the library but the shipboard installation
# of Perl is screwed up so....
#
;# getopts.pl - a better getopt.pl

;# Usage:
;#      do Getopts('a:bc');  # -a takes arg. -b & -c not. Sets opt_* as a
;#                           #  side effect.

sub Getopts {
    local($argumentative) = @_;
    local(@args,$_,$first,$rest);
    local($errs) = 0;

    @args = split( / */, $argumentative );
    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
	($first,$rest) = ($1,$2);
	$pos = index($argumentative,$first);
	if($pos >= $[) {
	    if($args[$pos+1] eq ':') {
		shift(@ARGV);
		if($rest eq '') {
		    ++$errs unless @ARGV;
		    $rest = shift(@ARGV);
		}
		eval "\$opt_$first = \$rest;";
	    }
	    else {
		eval "\$opt_$first = 1";
		if($rest eq '') {
		    shift(@ARGV);
		}
		else {
		    $ARGV[0] = "-$rest";
		}
	    }
	}
	else {
	    print STDERR "Unknown option: $first\n";
	    ++$errs;
	    if($rest ne '') {
		$ARGV[0] = "-$rest";
	    }
	    else {
		shift(@ARGV);
	    }
	}
    }
    $errs == 0;
}

1;
