#!/usr/bin/env perl
#--------------------------------------------------------------------
#    The MB-system:	mbm_rollover.perl	6/18/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_rollover
#
# Purpose:
#   Perl shellscript to read a multibeam bathymetry file, calculate the
#   noise in the vertical reference used by the sonar, and generate
#   a file containing roll corrections which can be applied to the
#   data.
#
# Usage:
#   mbm_rollover -Fformat -Wfilterwidth -Ifile
#
# Author:
#   David W. Caress
#   Lamont-Doherty Earth Observatory
#   Palisades, NY  10964
#   June 13, 1993
#
#
#
#
# Deal with command line arguments
&Getopts('I:i:F:f:W:w:');
$file =        ($opt_I || $opt_i);
$format =      ($opt_F || $opt_f);
$filterwidth = ($opt_W || $opt_w);

# get format if needed
if (!$format)
	{
	$line = `mbformat -I $file -L`;
	($format) = $line =~ /(\S+)/;
	if ($format == 0)
		{
		$format = -1;
		}
	}

# set filterwidth if needed
if (!$filterwidth)
	{
	$filterwidth = 60;
	}

print "file: ",$file,"\n";
print "format: ",$format,"\n";
$file || die "Input file not specified...\nMBM_ROLLERROR aborted.\n";

# Get filenames in order...
$datfile = "mbm_rollerror$$.dat";
$fltfile = "mbm_rollerror$$.flt";
$corfile = "$file.cor";

# Get going.
print "Macro mbm_rollerror running...\n\n";
print "Running mblist...\n";
`mblist -F$format -I$file -OMA > $datfile`;

# Filter the seafloor slope data.
print "Running filter1d...\n";
`filter1d $datfile -Fc$filterwidth -E -V > $fltfile`;

# Read the data and filtered data files,
# subtracting to get the high-passed signal.
print "Processing raw and filtered data to get residual...\n";
open(F1,"$datfile");
open(F2,"$fltfile");
open(FOUT,"> $corfile");
while ($line1 = <F1>)
	{
	($time1, $dat) = $line1 =~ /(\S+)\s(\S+)/;
	$line2 = <F2>;
	($time2, $flt) = $line2 =~ /(\S+)\s(\S+)/;
	$res = $dat - $flt;
	print FOUT $time1,"\t",$res,"\n";
	}
close(F1);
close(F2);
close(FOUT);

# remove excess files
`rm -f $datfile $fltfile`;

# Announce success whether it is deserved or not.
print "All 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;
