editPD0
changeset 28 7c7da52363c2
parent 14 8c79b38a7086
child 31 b6ca27a1d19c
--- a/editPD0
+++ b/editPD0
@@ -2,15 +2,17 @@
 #======================================================================
 #                    E D I T P D 0 
 #                    doc: Mon Nov 25 20:24:31 2013
-#                    dlm: Tue Nov 26 10:31:05 2013
+#                    dlm: Fri Dec 18 20:56:45 2015
 #                    (c) 2013 A.M. Thurnherr
-#                    uE-Info: 70 1 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 104 1 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values
 
 # HISTORY:
 #	Nov 25, 2013: - created
+#	Dec 18, 2015: - added switch_beams()
+#				  - added -x
 
 $0 =~ m{(.*)/[^/]+}; 
 require "$1/RDI_PD0_IO.pl";
@@ -18,12 +20,12 @@
 
 $USAGE = "$0 @ARGV";
 die("Usage: $0 " .
-	'-e) <edit-file> ' .
+	'-e) <edit-file> | -x) <expr> ' .
 	"<input file> <output file>\n")
-		unless (&Getopts('e:') && @ARGV == 2);
+		unless (&Getopts('e:x:') && @ARGV == 2);
 
-die("$0: -e <edit-file> is required\n")
-	unless (-r $opt_e);
+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);
@@ -31,46 +33,84 @@
 
 #----------------------------------------------------------------------
 
-print(STDERR "Editing according to <$opt_e>...");
+print(STDERR "Editing Data...");				
 
 #--------------------------------------------------
 # INTERFACE
-#	- example <edit_file> entry:
+#	- example <edit_file> entry for external attitude data
 #		162     p(3), r(4), h(3.14)
+#	- example <edit_file> entry for beam switching
+#		*		switch_beams(3,4)
 #--------------------------------------------------
 
 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; }
 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; }
 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; }
 
+sub switch_beams($$)
+{
+	my($b1,$b2) = @_;
+
+#	print(STDERR "\n entering switch_beams($b1,$b2) for ens = $e...");
+
+	for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
+		my($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;
+}
+
 #--------------------------------------------------
 # Main Routine
 #--------------------------------------------------
 
-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($opt_x)) {
+	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(@EE,$ens);
-	push(@EX,$expr);
-}
-close(EF);
+	push(@EX,$opt_x);
+}		
 
-for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {			# local() needed for p(), r(), h()
-	if ($EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {				# match => edit
+if (defined($opt_e)) {
+	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()
+#	print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]\n");
+	if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {		# match => edit
 #		print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]");
-#		print(STDERR "\n\tp($dta{ENSEMBLE}[$e]->{PITCH}), r($dta{ENSEMBLE}[$e]->{ROLL}), h($dta{ENSEMBLE}[$e]->{HEADING})");
 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
-#		print(STDERR "\n\tp($dta{ENSEMBLE}[$e]->{PITCH}), r($dta{ENSEMBLE}[$e]->{ROLL}), h($dta{ENSEMBLE}[$e]->{HEADING})...");
-	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {			# next edit later in file => skip
+	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {						# next edit later in file => skip
 		next;
-	} else {														# need next edit
+	} else {																	# need next edit
 		$eei++;
 		last if ($eei >= @EE);
 		redo;