editPD0
changeset 32 7155adf61d77
parent 31 b6ca27a1d19c
child 34 3b4bcd55e1ea
equal deleted inserted replaced
31:b6ca27a1d19c 32:7155adf61d77
     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: Sat Jan  9 20:07:59 2016
     5 #                    dlm: Fri Feb 26 17:24:55 2016
     6 #                    (c) 2013 A.M. Thurnherr
     6 #                    (c) 2013 A.M. Thurnherr
     7 #                    uE-Info: 128 0 NIL 0 0 72 2 2 4 NIL ofnI
     7 #                    uE-Info: 26 98 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 # NOTES:
    12 # NOTES:
    21 #		h(<heading>)			set heading alue value of current ensemble
    21 #		h(<heading>)			set heading alue value of current ensemble
    22 #
    22 #
    23 #		swap_beams(<b1>,<b2>)	swap data from beams b1 and b2
    23 #		swap_beams(<b1>,<b2>)	swap data from beams b1 and b2
    24 #								input in beam coords required
    24 #								input in beam coords required
    25 #								beam rotation is equivalent to 3 consecutive beam swaps
    25 #								beam rotation is equivalent to 3 consecutive beam swaps
       
    26 #								basic BT data are swapped as well (not RL and not SIGNAL_STRENGTH)
    26 #
    27 #
    27 #		earth2beam()			transform beam to earth coordinates
    28 #		earth2beam()			transform beam to earth coordinates
    28 #								does not handle bin-remapping
    29 #								does not handle bin-remapping
    29 #								input in beam coords required
    30 #								input in beam coords required
       
    31 #
       
    32 #		ensure_UL()				overwrite transducer-orientation flag in data file
       
    33 #		ensure_DL()
    30 #
    34 #
    31 #	- -x notes:
    35 #	- -x notes:
    32 #		- multiple perl expressions can be combined with ,
    36 #		- multiple perl expressions can be combined with ,
    33 #
    37 #
    34 #	- Edit File Syntax:
    38 #	- Edit File Syntax:
    45 #	Jan  9, 2016: - renamed switch_beams() to swap_beams()
    49 #	Jan  9, 2016: - renamed switch_beams() to swap_beams()
    46 #				  - wrote documentation
    50 #				  - wrote documentation
    47 #				  - change output data-source ID from 0x7F to 0xE0
    51 #				  - change output data-source ID from 0x7F to 0xE0
    48 #				  - updated getopts to current perl version
    52 #				  - updated getopts to current perl version
    49 #				  - adapted to [ADCP_tools_lib.pl]
    53 #				  - adapted to [ADCP_tools_lib.pl]
       
    54 #	Feb 15, 2016: - added ensure_UL() ensure_DL()
       
    55 #	Feb 23, 2016: - added -k
       
    56 #	Feb 26, 2016: - added basic BT data to swap_beams()
    50 
    57 
    51 use Getopt::Std;
    58 use Getopt::Std;
    52 
    59 
    53 ($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
    60 ($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
    54 $ADCP_tools_minVersion = 1.4; 
    61 $ADCP_tools_minVersion = 1.4; 
    55 require "$ADCP_TOOLS/ADCP_tools_lib.pl";
    62 require "$ADCP_TOOLS/ADCP_tools_lib.pl";
    56 
    63 
    57 $USAGE = "$0 @ARGV";
    64 $USAGE = "$0 @ARGV";
    58 die("Usage: $0 " .
    65 die("Usage: $0 " .
    59     '-e) <edit-file> | -x) <expr> ' .
    66     '-e) <edit-file> | -x) <expr> ' .
       
    67     '-k)eep original data-source id' .
    60     "<input file> <output file>\n")
    68     "<input file> <output file>\n")
    61         unless (&getopts('e:x:') && @ARGV == 2);
    69         unless (&getopts('ke:x:') && @ARGV == 2);
    62 
    70 
    63 die("$0: -e <edit-file> or -x <expr> required\n")
    71 die("$0: -e <edit-file> or -x <expr> required\n")
    64     unless (defined($opt_x) || -r $opt_e);
    72     unless (defined($opt_x) || -r $opt_e);
    65 
    73 
    66 print(STDERR "Reading $ARGV[0]...");                # read data
    74 print(STDERR "Reading $ARGV[0]...");                # read data
    73 
    81 
    74 #--------------------------------------------------
    82 #--------------------------------------------------
    75 # Data Editing Library
    83 # Data Editing Library
    76 #--------------------------------------------------
    84 #--------------------------------------------------
    77 
    85 
       
    86 #
       
    87 # override pitch/roll/heading
       
    88 #
    78 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; return 1; }
    89 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; return 1; }
    79 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; return 1; }
    90 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; return 1; }
    80 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; return 1;}
    91 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; return 1;}
    81 
    92 
       
    93 #
       
    94 # override transducer orientation
       
    95 #
       
    96 sub ensure_DL()
       
    97 {
       
    98 	$dta{ENSEMBLE}[$e]->{XDUCER_FACING_DOWN} = 1;
       
    99 	$dta{ENSEMBLE}[$e]->{XDUCER_FACING_UP} = undef;
       
   100 	return 1;
       
   101 }
       
   102 sub ensure_UL()
       
   103 {
       
   104 	$dta{ENSEMBLE}[$e]->{XDUCER_FACING_DOWN} = undef;
       
   105 	$dta{ENSEMBLE}[$e]->{XDUCER_FACING_UP} = 1;
       
   106 	return 1;
       
   107 }
       
   108 
       
   109 #
       
   110 # swap data from two mis-connected beams
       
   111 #
    82 sub swap_beams($$)
   112 sub swap_beams($$)
    83 {
   113 {
    84 	my($b1,$b2) = @_;
   114 	my($b1,$b2) = @_;
       
   115 	my($tmp);
    85 
   116 
    86 #	print(STDERR "\n entering swap_beams($b1,$b2) for ens = $e...");
   117 #	print(STDERR "\n entering swap_beams($b1,$b2) for ens = $e...");
    87 
   118 
    88 	die("$ARGV[0]: beam-coordinate data required\n")
   119 	die("$ARGV[0]: beam-coordinate data required\n")
    89 		unless ($dta{BEAM_COORDINATES});
   120 		unless ($dta{BEAM_COORDINATES});
    90 
   121 
       
   122 	if ($dta{BT_PRESENT}) {
       
   123 		$tmp = $dta{ENSEMBLE}[$e]->{BT_RANGE}[$b1-1];
       
   124 		$dta{ENSEMBLE}[$e]->{BT_RANGE}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_RANGE}[$b2-1];
       
   125 		$dta{ENSEMBLE}[$e]->{BT_RANGE}[$b2-1] = $tmp;
       
   126 		                
       
   127 		$tmp = $dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b1-1];
       
   128 		$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b2-1];
       
   129 		$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b2-1] = $tmp;
       
   130 		                
       
   131 		$tmp = $dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b1-1];
       
   132 		$dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b2-1];
       
   133 		$dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b2-1] = $tmp;
       
   134 		                
       
   135 		$tmp = $dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b1-1];
       
   136 		$dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b2-1];
       
   137 		$dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b2-1] = $tmp;
       
   138 		                
       
   139 		$tmp = $dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b1-1];
       
   140 		$dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b2-1];
       
   141 		$dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b2-1] = $tmp;
       
   142     }		                
       
   143 
    91 	for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
   144 	for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
    92 		my($tmp) = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
   145 		$tmp = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
    93 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
   146 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
    94 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;
   147 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;
    95 
   148 
    96 		$tmp = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1];
   149 		$tmp = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1];
    97 		$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1];
   150 		$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1];
   107 	}
   160 	}
   108 	return 1;
   161 	return 1;
   109 }
   162 }
   110 
   163 
   111 
   164 
       
   165 #
       
   166 # transform earth to beam coordinates
       
   167 #
   112 { my($checked);
   168 { my($checked);
   113 
   169 
   114 	sub earth2beam()
   170 	sub earth2beam()
   115 	{
   171 	{
   116 		unless ($checked) {
   172 		unless ($checked) {
   158 	}
   214 	}
   159 	close(EF);
   215 	close(EF);
   160 }
   216 }
   161 
   217 
   162 for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {						# local() needed for p(), r(), h()
   218 for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {						# local() needed for p(), r(), h()
   163 	$dta{ENSEMBLE}[$e]->{DATA_SOURCE_ID} = 0xE0;								# mark all ensembles
   219 	$dta{ENSEMBLE}[$e]->{DATA_SOURCE_ID} = 0xE0									# mark all ensembles except first
       
   220 		unless ($opt_k || $e==0);
   164 	if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {		# match => edit
   221 	if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {		# match => edit
   165 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
   222 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
   166 	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {						# next edit later in file => skip
   223 	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {						# next edit later in file => skip
   167 		next;
   224 		next;
   168 	} else {																	# need next edit
   225 	} else {																	# need next edit