editPD0
changeset 31 b6ca27a1d19c
parent 28 7c7da52363c2
child 32 7155adf61d77
equal deleted inserted replaced
30:184133e916be 31:b6ca27a1d19c
     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: Fri Dec 18 20:56:45 2015
     5 #                    dlm: Sat Jan  9 20:07:59 2016
     6 #                    (c) 2013 A.M. Thurnherr
     6 #                    (c) 2013 A.M. Thurnherr
     7 #                    uE-Info: 104 1 NIL 0 0 72 2 2 4 NIL ofnI
     7 #                    uE-Info: 128 0 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:
       
    13 #
       
    14 #	- editing instructions can be provided either in an editing file (primarily
       
    15 #	  for ensemble-specific editing), or with the -x option on the command line
       
    16 #	  (only or editing applied to all ensembles)
       
    17 #
       
    18 #	- Data-Editing Library:
       
    19 #		p(<pitch>)				set pitch value (RDI not gimbal pitch) of current ensemble
       
    20 #		r(<roll>)				set roll alue value of current ensemble
       
    21 #		h(<heading>)			set heading alue value of current ensemble
       
    22 #
       
    23 #		swap_beams(<b1>,<b2>)	swap data from beams b1 and b2
       
    24 #								input in beam coords required
       
    25 #								beam rotation is equivalent to 3 consecutive beam swaps
       
    26 #
       
    27 #		earth2beam()			transform beam to earth coordinates
       
    28 #								does not handle bin-remapping
       
    29 #								input in beam coords required
       
    30 #
       
    31 #	- -x notes:
       
    32 #		- multiple perl expressions can be combined with ,
       
    33 #
       
    34 #	- Edit File Syntax:
       
    35 #		- # comments ignored
       
    36 #		- empty lines ignored
       
    37 #       - [space] <ensemble-number|*> <space> <perl-expr>
       
    38 #		- Examples:
       
    39 #       	162     p(3), r(4), h(3.14)
       
    40 
    12 # HISTORY:
    41 # HISTORY:
    13 #	Nov 25, 2013: - created
    42 #   Nov 25, 2013: - created
    14 #	Dec 18, 2015: - added switch_beams()
    43 #   Dec 18, 2015: - added switch_beams()
    15 #				  - added -x
    44 #                 - added -x
       
    45 #	Jan  9, 2016: - renamed switch_beams() to swap_beams()
       
    46 #				  - wrote documentation
       
    47 #				  - change output data-source ID from 0x7F to 0xE0
       
    48 #				  - updated getopts to current perl version
       
    49 #				  - adapted to [ADCP_tools_lib.pl]
    16 
    50 
    17 $0 =~ m{(.*)/[^/]+}; 
    51 use Getopt::Std;
    18 require "$1/RDI_PD0_IO.pl";
    52 
    19 require "getopts.pl";
    53 ($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
       
    54 $ADCP_tools_minVersion = 1.4; 
       
    55 require "$ADCP_TOOLS/ADCP_tools_lib.pl";
    20 
    56 
    21 $USAGE = "$0 @ARGV";
    57 $USAGE = "$0 @ARGV";
    22 die("Usage: $0 " .
    58 die("Usage: $0 " .
    23 	'-e) <edit-file> | -x) <expr> ' .
    59     '-e) <edit-file> | -x) <expr> ' .
    24 	"<input file> <output file>\n")
    60     "<input file> <output file>\n")
    25 		unless (&Getopts('e:x:') && @ARGV == 2);
    61         unless (&getopts('e:x:') && @ARGV == 2);
    26 
    62 
    27 die("$0: -e <edit-file> or -x <expr> required\n")
    63 die("$0: -e <edit-file> or -x <expr> required\n")
    28 	unless (defined($opt_x) || -r $opt_e);
    64     unless (defined($opt_x) || -r $opt_e);
    29 
    65 
    30 print(STDERR "Reading $ARGV[0]...");				# read data
    66 print(STDERR "Reading $ARGV[0]...");                # read data
    31 readData($ARGV[0],\%dta);
    67 readData($ARGV[0],\%dta);
    32 print(STDERR "done\n");
    68 print(STDERR "done\n");
    33 
    69 
    34 #----------------------------------------------------------------------
    70 #----------------------------------------------------------------------
    35 
    71 
    36 print(STDERR "Editing Data...");				
    72 print(STDERR "Editing Data...");                
    37 
    73 
    38 #--------------------------------------------------
    74 #--------------------------------------------------
    39 # INTERFACE
    75 # Data Editing Library
    40 #	- example <edit_file> entry for external attitude data
       
    41 #		162     p(3), r(4), h(3.14)
       
    42 #	- example <edit_file> entry for beam switching
       
    43 #		*		switch_beams(3,4)
       
    44 #--------------------------------------------------
    76 #--------------------------------------------------
    45 
    77 
    46 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; }
    78 sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; return 1; }
    47 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; }
    79 sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; return 1; }
    48 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; }
    80 sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; return 1;}
    49 
    81 
    50 sub switch_beams($$)
    82 sub swap_beams($$)
    51 {
    83 {
    52 	my($b1,$b2) = @_;
    84 	my($b1,$b2) = @_;
    53 
    85 
    54 #	print(STDERR "\n entering switch_beams($b1,$b2) for ens = $e...");
    86 #	print(STDERR "\n entering swap_beams($b1,$b2) for ens = $e...");
       
    87 
       
    88 	die("$ARGV[0]: beam-coordinate data required\n")
       
    89 		unless ($dta{BEAM_COORDINATES});
    55 
    90 
    56 	for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
    91 	for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
    57 		my($tmp) = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
    92 		my($tmp) = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
    58 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
    93 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
    59 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;
    94 		$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;
    68 
   103 
    69 		$tmp = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1];
   104 		$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];
   105 		$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;
   106 		$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1] = $tmp;
    72 	}
   107 	}
       
   108 	return 1;
       
   109 }
    73 
   110 
    74 	return 1;
   111 
       
   112 { my($checked);
       
   113 
       
   114 	sub earth2beam()
       
   115 	{
       
   116 		unless ($checked) {
       
   117 			die("$ARGV[0]: earth-coordinate data required\n")
       
   118 				unless ($dta{EARTH_COORDINATES});
       
   119 			$dta{BEAM_COORDINATES} = 1; undef($dta{EARTH_COORDINATES});
       
   120 			$checked = 1;
       
   121 		}
       
   122 	    
       
   123 		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
       
   124 			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
       
   125 				velEarthToBeam(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
       
   126 		}
       
   127 	
       
   128 		return 1;
       
   129 	}
       
   130 
    75 }
   131 }
    76 
   132 
    77 #--------------------------------------------------
   133 #--------------------------------------------------
    78 # Main Routine
   134 # Main Routine
    79 #--------------------------------------------------
   135 #--------------------------------------------------
    80 
   136 
    81 if (defined($opt_x)) {
   137 if (defined($opt_x)) {															# edit instructions on the command line
    82 	push(@EE,'*');
   138 	push(@EE,'*');
    83 	my($id) = ($opt_x =~ m/^([A-Z]+)\s/);										# e.g. PITCH, ROLL, HEADING
   139 	my($id) = ($opt_x =~ m/^([A-Z]+)\s/);										# e.g. PITCH, ROLL, HEADING
    84 	$opt_x = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
   140 	$opt_x = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
    85 		if defined($id);
   141 		if defined($id);
    86 	push(@EX,$opt_x);
   142 	push(@EX,$opt_x);
    87 }		
   143 }		
    88 
   144 
    89 if (defined($opt_e)) {
   145 if (defined($opt_e)) {															# edit instructions in edit file
    90 	open(EF,$opt_e) || die("$opt_e: $!\n");
   146 	open(EF,$opt_e) || die("$opt_e: $!\n");
    91 	while (<EF>) {
   147 	while (<EF>) {
    92 		s/\#.*//;
   148 		s/\#.*//;
    93 		next if m/^\s+$/;
   149 		next if m/^\s+$/;
    94 		my($ens,$expr) = m/^\s*(\*|\d+)\s+(.*)$/;
   150 		my($ens,$expr) = m/^\s*(\*|\d+)\s+(.*)$/;
   102 	}
   158 	}
   103 	close(EF);
   159 	close(EF);
   104 }
   160 }
   105 
   161 
   106 for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {						# local() needed for p(), r(), h()
   162 for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) {						# local() needed for p(), r(), h()
   107 #	print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]\n");
   163 	$dta{ENSEMBLE}[$e]->{DATA_SOURCE_ID} = 0xE0;								# mark all ensembles
   108 	if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {		# match => edit
   164 	if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) {		# match => edit
   109 #		print(STDERR "\n\@ens=$EE[$eei]: $EX[$eei]");
       
   110 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
   165 		eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
   111 	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {						# next edit later in file => skip
   166 	} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) {						# next edit later in file => skip
   112 		next;
   167 		next;
   113 	} else {																	# need next edit
   168 	} else {																	# need next edit
   114 		$eei++;
   169 		$eei++;