editPD0
author Andreas Thurnherr <ant@ldeo.columbia.edu>
Thu, 04 Mar 2021 08:38:53 -0500
changeset 55 540d6574caca
parent 43 b63fa355644c
child 61 69192495f0db
permissions -rwxr-xr-x
adapted to Nortek

#!/usr/bin/perl
#======================================================================
#                    E D I T P D 0 
#                    doc: Mon Nov 25 20:24:31 2013
#                    dlm: Wed Mar 14 21:15:51 2018
#                    (c) 2013 A.M. Thurnherr
#                    uE-Info: 417 0 NIL 0 0 72 2 2 4 NIL ofnI
#======================================================================

# edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values

# NOTES:
#
#	- editing instructions can be provided either in an editing file (primarily
#	  for ensemble-specific editing), or with the -x option on the command line
#	  (only or editing applied to all ensembles)
#
#	- Data-Editing Library:
#		p(<pitch>)				set pitch value (RDI not gimbal pitch) of current ensemble
#		r(<roll>)				set roll alue value of current ensemble
#		h(<heading>)			set heading alue value of current ensemble
#
#		swap_beams(<b1>,<b2>)	swap data from beams b1 and b2
#									- input in beam coords required
#									- beam rotation is equivalent to 3 consecutive beam swaps
#									- basic BT data are swapped as well (not RL and not SIGNAL_STRENGTH)
#
#		earth2beam()			transform beam to earth coordinates
#									- does not handle bin-remapping
#									- input in earth coords required
#
#		beam2earth()			transform earth to beam coordinates
#									- does not handle bin-remapping
#									- input in beam coords required
#
#		instrument2beam()		transform instrument to earth coordinates
#									- does not handle bin-remapping
#									- input in instrument coords required
#
#		ensure_UL()				correct data for wrong transducer orientation
#		ensure_DL()					- sets correct flag & negates roll value
#
#		dealias(<WV lim[m/s]>) 	correct data for erroneously low WV setting
#									- HEURISTIC, i.e. may not work
#
#	- -x notes:
#		- multiple perl expressions can be combined with ,
#
#	- Edit File Syntax:
#		- # comments ignored
#		- empty lines ignored
#       - [space] <ensemble-number|*> <space> <perl-expr>
#		- Examples:
#       	162     p(3), r(4), h(3.14)

# HISTORY:
#   Nov 25, 2013: - created
#   Dec 18, 2015: - added switch_beams()
#                 - added -x
#	Jan  9, 2016: - renamed switch_beams() to swap_beams()
#				  - wrote documentation
#				  - change output data-source ID from 0x7F to 0xE0
#				  - updated getopts to current perl version
#				  - adapted to [ADCP_tools_lib.pl]
#	Feb 15, 2016: - added ensure_UL() ensure_DL()
#	Feb 23, 2016: - added -k
#	Feb 26, 2016: - added basic BT data to swap_beams()
#				  - added earth2beam()
#	Apr 12, 2016: - added instrument2beam()
#	Jun  3, 2016: - added beam2earth()
#				  - BUG: instrument2earth() set wrong flag
#	Jun  8, 2016: - adapted to new interface of velInstrumentToBeam()
#				  - added %-good to beam2earth and earth2beam
#				  - made single-ping ensemble requirement for most routines
#	Jul 12, 2016: - updated ensure_{DL,UL} routines
#	Nov 15, 2016: - BUG: ensure_{DL,UL} routines did not negate heading data
#	Jul 27, 2017: - began working on dealias()
#	Dec  6, 2017: - cosmetics

use Getopt::Std;

($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
$ADCP_tools_minVersion = 2.1; 
require "$ADCP_TOOLS/ADCP_tools_lib.pl";

$USAGE = "$0 @ARGV";
die("Usage: $0 " .
    '-e) <edit-file> | -x) <expr> ' .
    '[-k)eep original data-source id] ' .
    "<input file> <output file>\n")
        unless (&getopts('ke:x:') && @ARGV == 2);

die("$0: -e <edit-file> or -x <expr> required\n")
    unless (defined($opt_x) || -r $opt_e);

print(STDERR "Reading $ARGV[0]...");                # read data
readData($ARGV[0],\%dta);
printf(STDERR "done (%d complete ensembles)\n",
	scalar(@{$dta{ENSEMBLE}}));

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

print(STDERR "Editing Data...");                

#--------------------------------------------------
# Data Editing Library
#--------------------------------------------------

#--------------------------------------------------
# override pitch/roll/heading
#--------------------------------------------------

sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; return 1; }
sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; return 1; }
sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; return 1;}

#--------------------------------------------------
# correct data for erroneously low WV limit
#--------------------------------------------------

{ my(@target);												# static scope, undef initially

sub dealias_V0($)
{
	my($WV) = @_;

	if (@target) {											# dealias everything but first ensemble
		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
			for (my($beam)=0; $beam<4; $beam++) {
				next unless defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]);
				my($dealiased) = 0;
				$dealiased = ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] - 2*$WV)
					if ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] > 0);
				$dealiased = ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] + 2*$WV)
					if ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] < 0);
				if (abs($target[$beam]-$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]) >
					abs($target[$beam]-$dealiased)) {
						$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] = undef; #$dealiased;
						$dealiased++;
				}
			}
    	}
    } else { # @target is undef
    	$dealiased = 0;
    }

	@target = (0,0,0,0);									# calc ref-lr average target for next ens
	for (my($bin)=1; $bin<=5; $bin++) {						# should work even if N_BINS < 5
		for (my($beam)=0; $beam<4; $beam++) {
			$target[$beam] += $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]/4
				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]);
		}
    }

    return 1;
}

} # static scope

#--------------------------------------------------
# override transducer orientation
#
#	These routines are intended to correct ADCP data for
#	erroneous orientation switch readings, primarily because
#	of a stuck switch.
#		Roll: Based on text from the coord trans manual,
#			  it seems likely that the roll data need to
#			  be negated. In case of 2007(?) CLIVAR I08S
#			  profile #1 w gets much better with negated
#			  roll. Also, in 2016 CLIVAR P18 profile 003
#			  the instrument-offset calculation from
#			  compass and pitch/roll only agree with the
#			  roll negated.
#		Hdg: Based on the time-series of headings recorded
#		     during P18 profile 003 the heading needs
#			 to be negated. Doing so yields a good profile.
#--------------------------------------------------

sub ensure_DL()
{
	if ($dta{ENSEMBLE}[$e]->{XDUCER_FACING_UP}) {
		$dta{ENSEMBLE}[$e]->{ROLL} *= -1;
		$dta{ENSEMBLE}[$e]->{HEADING} *= -1; 
		$dta{ENSEMBLE}[$e]->{HEADING} += 360
			if ($dta{ENSEMBLE}[$e]->{HEADING} < 0);
		$dta{ENSEMBLE}[$e]->{XDUCER_FACING_DOWN} = 1;
		$dta{ENSEMBLE}[$e]->{XDUCER_FACING_UP} = undef;
	}
	return 1;
}

sub ensure_UL()
{
	if ($dta{ENSEMBLE}[$e]->{XDUCER_FACING_DOWN}) {
		$dta{ENSEMBLE}[$e]->{ROLL} *= -1;
		$dta{ENSEMBLE}[$e]->{HEADING} *= -1; 
		$dta{ENSEMBLE}[$e]->{HEADING} += 360
			if ($dta{ENSEMBLE}[$e]->{HEADING} < 0);
		$dta{ENSEMBLE}[$e]->{XDUCER_FACING_UP} = 1;
		$dta{ENSEMBLE}[$e]->{XDUCER_FACING_DOWN} = undef;
	}
	return 1;
}

#--------------------------------------------------
# swap data from two mis-connected beams
#--------------------------------------------------

sub swap_beams($$)
{
	my($b1,$b2) = @_;
	my($tmp);

#	print(STDERR "\n entering swap_beams($b1,$b2) for ens = $e...");

	die("$ARGV[0]: beam-coordinate data required\n")
		unless ($dta{BEAM_COORDINATES});
	die("$ARGV[0]: single-ping ensembles required\n")
		unless ($dta{PINGS_PER_ENSEMBLE} == 1);

	if ($dta{BT_PRESENT}) {
		$tmp = $dta{ENSEMBLE}[$e]->{BT_RANGE}[$b1-1];
		$dta{ENSEMBLE}[$e]->{BT_RANGE}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_RANGE}[$b2-1];
		$dta{ENSEMBLE}[$e]->{BT_RANGE}[$b2-1] = $tmp;
		                
		$tmp = $dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b1-1];
		$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b2-1];
		$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b2-1] = $tmp;
		                
		$tmp = $dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b1-1];
		$dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b2-1];
		$dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b2-1] = $tmp;
		                
		$tmp = $dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b1-1];
		$dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b2-1];
		$dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b2-1] = $tmp;
		                
		$tmp = $dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b1-1];
		$dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b2-1];
		$dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b2-1] = $tmp;
    }		                

	for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
		$tmp = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;

		$tmp = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1];
		$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1];
		$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1] = $tmp;

		$tmp = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1];
		$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1];
		$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1] = $tmp;

		$tmp = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1];
		$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1];
		$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1] = $tmp;
	}
	return 1;
}


#--------------------------------------------------
# transform earth to beam coordinates
#--------------------------------------------------

{ my($checked);

	sub earth2beam()
	{
		unless ($checked) {
			die("$ARGV[0]: earth-coordinate data required\n")
				unless ($dta{EARTH_COORDINATES});
			die("$ARGV[0]: single-ping ensembles required\n")
				unless ($dta{PINGS_PER_ENSEMBLE} == 1);
			$dta{BEAM_COORDINATES} = 1; undef($dta{EARTH_COORDINATES});
			$checked = 1;
		}
	    
		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
			if ($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][3] == 100) {			# 4-beam solution
               	@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
					velEarthToBeam(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
				@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin]} = (100,100,100,100);
			} else {															# 3-beam solution or no solution
				undef(@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
				@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin]} = (0,0,0,0);
			}
		}
	
		return 1;
	}

}

#--------------------------------------------------
# transform instrument to beam coordinates
#--------------------------------------------------

{ my($checked);

	sub instrument2beam()
	{
		unless ($checked) {
			die("$ARGV[0]: instrument-coordinate data required\n")
				unless ($dta{INSTRUMENT_COORDINATES});
			die("$ARGV[0]: single-ping ensembles required\n")
				unless ($dta{PINGS_PER_ENSEMBLE} == 1);
			$dta{BEAM_COORDINATES} = 1; undef($dta{INSTRUMENT_COORDINATES});
			$checked = 1;
		}
	    
		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
				velInstrumentToBeam(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
		}
	
		return 1;
	}

}

#--------------------------------------------------
# transform instrument to earth coordinates
#--------------------------------------------------

{ my($checked);

	sub instrument2earth()
	{
		unless ($checked) {
			die("$ARGV[0]: instrument-coordinate data required\n")
				unless ($dta{INSTRUMENT_COORDINATES});
			die("$ARGV[0]: single-ping ensembles required\n")
				unless ($dta{PINGS_PER_ENSEMBLE} == 1);
			$dta{EARTH_COORDINATES} = 1; undef($dta{INSTRUMENT_COORDINATES});
			$checked = 1;
		}
	    
		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
				velInstrumentToEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
		}
	
		return 1;
	}

}

#--------------------------------------------------
# transform beam to earth coordinates
#--------------------------------------------------

{ my($checked);

	sub beam2earth()
	{
		unless ($checked) {
			die("$ARGV[0]: beam-coordinate data required\n")
				unless ($dta{BEAM_COORDINATES});
			die("$ARGV[0]: single-ping ensembles required\n")
				unless ($dta{PINGS_PER_ENSEMBLE} == 1);
			$dta{EARTH_COORDINATES} = 1; undef($dta{BEAM_COORDINATES});
			$checked = 1;
		}
	    
		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
				velBeamToEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
			$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][0] = 100*$RDI_Coords::threeBeamFlag;	# 3-beam solution
			$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][1] = 0;								# error velocity not checked
			$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][2] =									# no solution -> more than 1 bad beam
								@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} ? 0 : 100;
			$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][2] =									# 4-beam solution
								100 -  $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][0];
		}
	
		return 1;
	}

}

#--------------------------------------------------
# Main Routine
#--------------------------------------------------

if (defined($opt_x)) {															# edit instructions on the command line
	push(@EE,'*');
	my($id) = ($opt_x =~ m/^([A-Z]+)\s/);										# e.g. PITCH, ROLL, HEADING
	$opt_x = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
		if defined($id);
	push(@EX,$opt_x);
}		

if (defined($opt_e)) {															# edit instructions in edit file
	open(EF,$opt_e) || die("$opt_e: $!\n");
	while (<EF>) {
		s/\#.*//;
		next if m/^\s+$/;
		my($ens,$expr) = m/^\s*(\*|\d+)\s+(.*)$/;
	
		my($id) = ($expr =~ m/^([A-Z]+)\s/);									# e.g. PITCH, ROLL, HEADING
		$expr = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
			if defined($id);
		    
		push(@EE,$ens);
		push(@EX,$expr);
	}
	close(EF);
}

for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {						# local() needed for p(), r(), h()
	$dta{ENSEMBLE}[$e]->{DATA_SOURCE_ID} = 0xE0									# mark all ensembles except first
		unless ($opt_k || $e==0);
	if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {		# match => edit
		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {						# next edit later in file => skip
		next;
	} else {																	# need next edit
		$eei++;
		last if ($eei >= @EE);
		redo;
	}
}

print(STDERR "done\n");

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

print(STDERR "Writing $ARGV[1]...");				# write data
writeData($ARGV[1],\%dta);
print(STDERR "done\n");

exit(0);