listVels
changeset 34 3b4bcd55e1ea
child 42 80d039881d2c
equal deleted inserted replaced
33:307630665c6c 34:3b4bcd55e1ea
       
     1 #!/usr/bin/perl
       
     2 #======================================================================
       
     3 #                    L I S T V E L S 
       
     4 #                    doc: Mon Apr 25 21:12:54 2016
       
     5 #                    dlm: Mon Apr 25 21:48:18 2016
       
     6 #                    (c) 2016 A.M. Thurnherr
       
     7 #                    uE-Info: 180 0 NIL 0 0 72 10 2 4 NIL ofnI
       
     8 #======================================================================
       
     9 
       
    10 # list water-track velocity samples as ANTS records (PD02ANTS)
       
    11 
       
    12 # HISTORY:
       
    13 #	Apr 25, 2016: - created from [listBins]
       
    14 
       
    15 # General Notes:
       
    16 #	- everything (e.g. beams) is numbered from 1
       
    17 #	- no support for BT data
       
    18 
       
    19 use Getopt::Std;
       
    20 
       
    21 $ADCP_tools_minVersion = 1.4;
       
    22 ($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
       
    23 require "$ADCP_TOOLS/ADCP_tools_lib.pl";
       
    24 
       
    25 die("Usage: $0 [-r)ange <first_ens,last_ens>] [-b)ins <first_bin,last_bin>] " .
       
    26 			  "[-M)agnetic <declination>] " .
       
    27 			  "[-S)oundspeed correction <salin|*,temp|*,depth|*> " .
       
    28 			  "[-P)itch/Roll <bias/bias>] [-B)eamvel <bias/bias/bias/bias>] " .
       
    29 		 	  "[require -4)-beam solutions] [-d)iscard <beam#>] " .
       
    30 			  "<RDI file>\n")
       
    31 	unless (&getopts("4b:B:d:M:r:P:S:") && @ARGV == 1);
       
    32 
       
    33 ($P{pitch_bias},$P{roll_bias}) = split('[,/]',$opt_P);
       
    34 ($P{velbias_b1},$P{velbias_b2},$P{velbias_b3},$P{velbias_b4}) = split('[,/]',$opt_B);
       
    35 
       
    36 die("$0: -4 and -d are mutually exclusive\n")
       
    37 	if ($opt_4 && defined($opt_d));
       
    38 
       
    39 $RDI_Coords::minValidVels = 4 if ($opt_4);			# no 3-beam solutions
       
    40 
       
    41 print(STDERR "WARNING: magnetic declination not set!\n")
       
    42 	unless defined($opt_M);
       
    43 
       
    44 $ifn = $ARGV[0];
       
    45 
       
    46 ($first_ens,$last_ens) = split(',',$opt_r) if defined($opt_r);
       
    47 ($first_bin,$last_bin) = split(',',$opt_b) if defined($opt_b);
       
    48 
       
    49 if (defined($opt_S)) {
       
    50 	($SS_salin,$SS_temp,$SS_depth) = split(',',$opt_S);
       
    51 	$variable_ssCorr = ($SS_salin eq '*' || $SS_temp eq '*' || $SS_depth eq '*');
       
    52 } else {
       
    53 	print(STDERR "WARNING: no soundspeed correction applied!\n");
       
    54 }
       
    55 
       
    56 #----------------------------------------------------------------------
       
    57 # MAIN
       
    58 #----------------------------------------------------------------------
       
    59 
       
    60 $P{RDI_file} = $ifn;
       
    61 $P{mag_decl} = $opt_M if defined($opt_M);
       
    62 
       
    63 readData($ifn,\%dta);
       
    64 printf(STDERR "%d complete ensembles...\n",scalar(@{$dta{ENSEMBLE}}));
       
    65 $dta{HEADING_BIAS} = -$opt_M;						# magnetic declination
       
    66 
       
    67 if ($dta{BEAM_COORDINATES}) {						# coords
       
    68 	$beamCoords = 1;
       
    69 } else {
       
    70 	die("$ifn: only beam and earth coordinates supported\n")
       
    71 		if (!$dta{EARTH_COORDINATES});
       
    72 	die("$ifn: -d requires beam-coordinate data\n")
       
    73 		if defined($opt_d);
       
    74 	die("$ifn: -B requires beam-coordinate data\n")
       
    75 		if defined($opt_B);
       
    76 }
       
    77 
       
    78 $first_bin = 1 				unless ($first_bin > 0);
       
    79 $last_bin  = $dta{N_BINS}+1 unless ($last_bin >= $first_bin && $last_bin <= $dta{N_BINS}+1);
       
    80 
       
    81 $lastGoodBin = 0;
       
    82 for ($e=0; $e<=$#{$dta{ENSEMBLE}}; $e++) {				# check/transform velocities
       
    83 	next if (defined($first_ens) &&
       
    84 			 $dta{ENSEMBLE}[$e]->{NUMBER} < $first_ens);
       
    85 
       
    86 	$dta{ENSEMBLE}[$e]->{PITCH} -= $P{pitch_bias}
       
    87 		if defined($dta{ENSEMBLE}[$e]->{PITCH});
       
    88 	$dta{ENSEMBLE}[$e]->{ROLL}  -= $P{roll_bias}
       
    89 		if defined($dta{ENSEMBLE}[$e]->{ROLL});
       
    90 
       
    91 	$P{first_ens} = $dta{ENSEMBLE}[$e]->{NUMBER},$fe = $e
       
    92 		unless defined($P{first_ens});
       
    93 	last if (defined($last_ens) &&
       
    94 			 $dta{ENSEMBLE}[$e]->{NUMBER} > $last_ens);
       
    95 	$P{last_ens} = $dta{ENSEMBLE}[$e]->{NUMBER};
       
    96 	$le = $e;
       
    97 
       
    98 	die("3-beams used in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
       
    99 		if ($dta{ENSEMBLE}[$e]->{N_BEAMS_USED} < 4);
       
   100 	die("BIT error in ensemble $dta{ENSEMBLE}[$e]->{NUMBER}\n")
       
   101 		if defined($dta{ENSEMBLE}[$e]->{BUILT_IN_TEST_ERROR});
       
   102 	die("Low gain in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
       
   103         if ($dta{ENSEMBLE}[$e]->{LOW_GAIN});
       
   104 
       
   105 	for (my($b)=0; $b<$dta{N_BINS}; $b++) {
       
   106 		if ($beamCoords) {
       
   107 			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0] -= $P{velbias_b1}			# beam-velocity biases (-B)
       
   108 				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0]);
       
   109 			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1] -= $P{velbias_b2}
       
   110 				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1]);
       
   111 			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2] -= $P{velbias_b3}
       
   112 				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2]);
       
   113 			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3] -= $P{velbias_b4}
       
   114 				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3]);
       
   115 
       
   116 			if (defined($opt_d)) {											# discard data from given beam (-d)
       
   117 				undef($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$opt_d-1]);
       
   118 				undef($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][$opt_d-1]);
       
   119 			}
       
   120 
       
   121 			@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]} =					# save beam velocities
       
   122 				@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]};
       
   123 
       
   124 			@{$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b]} =				# calculate w12, w34
       
   125 				velBeamToBPEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
       
   126 
       
   127 			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = 						# calculate earth velocities
       
   128 				velBeamToEarth(\%dta,@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
       
   129 			$dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b] = $RDI_Coords::threeBeamFlag;
       
   130 			$three_beam[$b] += $RDI_Coords::threeBeamFlag;
       
   131 
       
   132 			unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0])) {
       
   133 				undef(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]});			# not sure when this can happen
       
   134 				next;
       
   135 			}
       
   136 		} else { 															# Earth coordinates
       
   137 			@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]} =					# calculate beam velocities
       
   138 				velEarthToBeam(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]});
       
   139 				                                            
       
   140 			@{$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b]} =				# calculate w12, w34
       
   141 				velBeamToBPEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
       
   142 
       
   143 			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = 						# correct for heading bias
       
   144 				velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]});
       
   145 
       
   146 			unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0])) {
       
   147 				undef(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]});			# not sure when/if this can happen
       
   148 				next;
       
   149 			}
       
   150 
       
   151 		}
       
   152 
       
   153 		$dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b] = 1;
       
   154 		$good_vels[$b]++; 
       
   155 		$lastGoodBin = $b if ($b > $lastGoodBin);
       
   156 		$firstGoodEns = $e unless defined($firstGoodEns);
       
   157 		$lastGoodEns = $e;
       
   158     }
       
   159 }
       
   160 
       
   161 unless (defined($opt_r)) {
       
   162 	$fe = $firstGoodEns;
       
   163 	$le = $lastGoodEns;
       
   164 }
       
   165 
       
   166 $P{N_ensembles} = $le - $fe + 1;
       
   167 
       
   168 $firstBin = 0;
       
   169 $lastBin = $lastGoodBin;
       
   170 
       
   171 print( STDERR "Start      : $dta{ENSEMBLE}[$fe]->{DATE} $dta{ENSEMBLE}[$fe]->{TIME}\n");
       
   172 print( STDERR "End        : $dta{ENSEMBLE}[$le]->{DATE} $dta{ENSEMBLE}[$le]->{TIME}\n");
       
   173 printf(STDERR "Bins       : %d-%d\n",$firstBin+1,$lastBin+1);
       
   174 if ($beamCoords) {
       
   175 	printf(STDERR "3-Beam     : %d %d %d %d\n",$RDI_Coords::threeBeam_1,
       
   176 											   $RDI_Coords::threeBeam_2,
       
   177 											   $RDI_Coords::threeBeam_3,
       
   178 											   $RDI_Coords::threeBeam_4);
       
   179 	
       
   180 }
       
   181 
       
   182 
       
   183 #----------------------------------------------------------------------
       
   184 # generate output
       
   185 #----------------------------------------------------------------------
       
   186 
       
   187 print("#ANTS#PARAMS# ");														# %PARAMs
       
   188 foreach my $k (keys(%P)) {
       
   189 	print(P "$k\{$P{$k}\} ");
       
   190 }
       
   191 printf(" soundspeed_correction{%s}",defined($opt_S) ? $opt_S : 'NONE!');
       
   192 print("\n");
       
   193 
       
   194 print("#ANTS#FIELDS# " .														# LAYOUT
       
   195 		"{ensemble} {heading} {pitch} {roll} {bin} " .
       
   196 		"{b1} {b2} {b3} {b4} {u} {v} {w} {err_vel} " .
       
   197 		"{v12} {w12} {v34} {w34} " .
       
   198 		"{corr1} {corr2} {corr3} {corr4} " .
       
   199 		"{amp1} {amp2} {amp3} {amp4} " .
       
   200 		"{pcg1} {pcg2} {pcg3} {pcg4}\n"
       
   201 );
       
   202 
       
   203 for (my($e)=$fe; $e<=$le; $e++) {
       
   204 	my($ssCorr) = defined($opt_S) ? ssCorr($dta{ENSEMBLE}[$e],$SS_salin,$SS_temp,$SS_depth) : 1;
       
   205 	for (my($b)=$first_bin=1; $b<=$last_bin-1; $b++) {
       
   206 		print("$dta{ENSEMBLE}[$e]->{NUMBER} ");
       
   207 		print(defined($dta{ENSEMBLE}[$e]->{HEADING}) ? "$dta{ENSEMBLE}[$e]->{HEADING} " : 'nan ');
       
   208 		print(defined($dta{ENSEMBLE}[$e]->{PITCH}) ? "$dta{ENSEMBLE}[$e]->{PITCH} " : 'nan ');
       
   209 		print(defined($dta{ENSEMBLE}[$e]->{ROLL}) ? "$dta{ENSEMBLE}[$e]->{ROLL} " : 'nan ');
       
   210 		printf("%d ",$b+1);
       
   211 
       
   212 		if ($dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b]) {
       
   213 			printf("%g %g %g %g ",@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
       
   214 			printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0] * $ssCorr);
       
   215 			printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1] * $ssCorr);
       
   216 			printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2] * $ssCorr);
       
   217 			if ($dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b]) {
       
   218 				print("nan ");
       
   219 			} else {
       
   220 				printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3] * $ssCorr);
       
   221 			}
       
   222 			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][0]) ? "%g " : "nan ",
       
   223 						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][0]);
       
   224 			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][1]) ? "%g " : "nan ",
       
   225 						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][1]);
       
   226 			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][2]) ? "%g " : "nan ",
       
   227 						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][2]);
       
   228 			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][3]) ? "%g " : "nan ",
       
   229 						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][3]);
       
   230 		} else {
       
   231 			print("nan nan nan nan ");
       
   232 			print("nan nan nan nan ");
       
   233 			print("nan nan nan nan ");
       
   234 	    }
       
   235 	    
       
   236 		print("@{$dta{ENSEMBLE}[$e]->{CORRELATION}[$b]} ");
       
   237 		print("@{$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$b]} ");
       
   238 
       
   239 		if ($beamCoords) {
       
   240 			print("@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]} ");
       
   241 		} else {
       
   242 			printf("nan nan nan nan ");
       
   243 		}
       
   244 		print("\n");
       
   245 	}
       
   246 }
       
   247 
       
   248 exit(0);