editPD0
changeset 28 7c7da52363c2
parent 14 8c79b38a7086
child 31 b6ca27a1d19c
equal deleted inserted replaced
27:1c128aaaca6f 28:7c7da52363c2
     1 #!/usr/bin/perl
     1 #!/usr/bin/perl
     2 #======================================================================
     2 #======================================================================
     3 #                    E D I T P D 0 
     3 #                    E D I T P D 0 
     4 #                    doc: Mon Nov 25 20:24:31 2013
     4 #                    doc: Mon Nov 25 20:24:31 2013
     5 #                    dlm: Tue Nov 26 10:31:05 2013
     5 #                    dlm: Fri Dec 18 20:56:45 2015
     6 #                    (c) 2013 A.M. Thurnherr
     6 #                    (c) 2013 A.M. Thurnherr
     7 #                    uE-Info: 70 1 NIL 0 0 72 2 2 4 NIL ofnI
     7 #                    uE-Info: 104 1 NIL 0 0 72 2 2 4 NIL ofnI
     8 #======================================================================
     8 #======================================================================
     9 
     9 
    10 # edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values
    10 # edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values
    11 
    11 
    12 # HISTORY:
    12 # HISTORY:
    13 #	Nov 25, 2013: - created
    13 #	Nov 25, 2013: - created
       
    14 #	Dec 18, 2015: - added switch_beams()
       
    15 #				  - added -x
    14 
    16 
    15 $0 =~ m{(.*)/[^/]+}; 
    17 $0 =~ m{(.*)/[^/]+}; 
    16 require "$1/RDI_PD0_IO.pl";
    18 require "$1/RDI_PD0_IO.pl";
    17 require "getopts.pl";
    19 require "getopts.pl";
    18 
    20 
    19 $USAGE = "$0 @ARGV";
    21 $USAGE = "$0 @ARGV";
    20 die("Usage: $0 " .
    22 die("Usage: $0 " .
    21 	'-e) <edit-file> ' .
    23 	'-e) <edit-file> | -x) <expr> ' .
    22 	"<input file> <output file>\n")
    24 	"<input file> <output file>\n")
    23 		unless (&Getopts('e:') && @ARGV == 2);
    25 		unless (&Getopts('e:x:') && @ARGV == 2);
    24 
    26 
    25 die("$0: -e <edit-file> is required\n")
    27 die("$0: -e <edit-file> or -x <expr> required\n")
    26 	unless (-r $opt_e);
    28 	unless (defined($opt_x) || -r $opt_e);
    27 
    29 
    28 print(STDERR "Reading $ARGV[0]...");				# read data
    30 print(STDERR "Reading $ARGV[0]...");				# read data
    29 readData($ARGV[0],\%dta);
    31 readData($ARGV[0],\%dta);
    30 print(STDERR "done\n");
    32 print(STDERR "done\n");
    31 
    33 
    32 #----------------------------------------------------------------------
    34 #----------------------------------------------------------------------
    33 
    35 
    34 print(STDERR "Editing according to <$opt_e>...");
    36 print(STDERR "Editing Data...");				
    35 
    37 
    36 #--------------------------------------------------
    38 #--------------------------------------------------
    37 # INTERFACE
    39 # INTERFACE
    38 #	- example <edit_file> entry:
    40 #	- example <edit_file> entry for external attitude data
    39 #		162     p(3), r(4), h(3.14)
    41 #		162     p(3), r(4), h(3.14)
       
    42 #	- example <edit_file> entry for beam switching
       
    43 #		*		switch_beams(3,4)
    40 #--------------------------------------------------
    44 #--------------------------------------------------
    41 
    45 
    42 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; }
    46 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; }
    43 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; }
    47 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; }
    44 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; }
    48 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; }
    45 
    49 
       
    50 sub switch_beams($$)
       
    51 {
       
    52 	my($b1,$b2) = @_;
       
    53 
       
    54 #	print(STDERR "\n entering switch_beams($b1,$b2) for ens = $e...");
       
    55 
       
    56 	for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
       
    57 		my($tmp) = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
       
    58 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
       
    59 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;
       
    60 
       
    61 		$tmp = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1];
       
    62 		$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1];
       
    63 		$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1] = $tmp;
       
    64 
       
    65 		$tmp = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1];
       
    66 		$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1];
       
    67 		$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1] = $tmp;
       
    68 
       
    69 		$tmp = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1];
       
    70 		$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1];
       
    71 		$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1] = $tmp;
       
    72 	}
       
    73 
       
    74 	return 1;
       
    75 }
       
    76 
    46 #--------------------------------------------------
    77 #--------------------------------------------------
    47 # Main Routine
    78 # Main Routine
    48 #--------------------------------------------------
    79 #--------------------------------------------------
    49 
    80 
    50 open(EF,$opt_e) || die("$opt_e: $!\n");
    81 if (defined($opt_x)) {
    51 while (<EF>) {
    82 	push(@EE,'*');
    52 	s/\#.*//;
    83 	my($id) = ($opt_x =~ m/^([A-Z]+)\s/);										# e.g. PITCH, ROLL, HEADING
    53 	next if m/^\s+$/;
    84 	$opt_x = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
    54 	my($ens,$expr) = m/^\s*(\d+)\s+(.*)$/;
    85 		if defined($id);
       
    86 	push(@EX,$opt_x);
       
    87 }		
    55 
    88 
    56 	my($id) = ($expr =~ m/^([A-Z]+)\s/);							# e.g. PITCH, ROLL, HEADING
    89 if (defined($opt_e)) {
    57 	$expr = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
    90 	open(EF,$opt_e) || die("$opt_e: $!\n");
    58 		if defined($id);
    91 	while (<EF>) {
    59 		
    92 		s/\#.*//;
    60 	push(@EE,$ens);
    93 		next if m/^\s+$/;
    61 	push(@EX,$expr);
    94 		my($ens,$expr) = m/^\s*(\*|\d+)\s+(.*)$/;
       
    95 	
       
    96 		my($id) = ($expr =~ m/^([A-Z]+)\s/);										# e.g. PITCH, ROLL, HEADING
       
    97 		$expr = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
       
    98 			if defined($id);
       
    99 		    
       
   100 		push(@EE,$ens);
       
   101 		push(@EX,$expr);
       
   102 	}
       
   103 	close(EF);
    62 }
   104 }
    63 close(EF);
       
    64 
   105 
    65 for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {			# local() needed for p(), r(), h()
   106 for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {						# local() needed for p(), r(), h()
    66 	if ($EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {				# match => edit
   107 #	print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]\n");
       
   108 	if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {		# match => edit
    67 #		print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]");
   109 #		print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]");
    68 #		print(STDERR "\n\tp($dta{ENSEMBLE}[$e]->{PITCH}), r($dta{ENSEMBLE}[$e]->{ROLL}), h($dta{ENSEMBLE}[$e]->{HEADING})");
       
    69 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
   110 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
    70 #		print(STDERR "\n\tp($dta{ENSEMBLE}[$e]->{PITCH}), r($dta{ENSEMBLE}[$e]->{ROLL}), h($dta{ENSEMBLE}[$e]->{HEADING})...");
   111 	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {						# next edit later in file => skip
    71 	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {			# next edit later in file => skip
       
    72 		next;
   112 		next;
    73 	} else {														# need next edit
   113 	} else {																	# need next edit
    74 		$eei++;
   114 		$eei++;
    75 		last if ($eei >= @EE);
   115 		last if ($eei >= @EE);
    76 		redo;
   116 		redo;
    77 	}
   117 	}
    78 }
   118 }