beamStats
changeset 0 229a0d72d2ab
child 18 bb7bb9f83db9
equal deleted inserted replaced
-1:000000000000 0:229a0d72d2ab
       
     1 #!/usr/bin/perl
       
     2 #======================================================================
       
     3 #                    L I S T B I N S 
       
     4 #                    doc: Fri Aug 25 15:57:05 2006
       
     5 #                    dlm: Thu Feb 21 15:05:40 2008
       
     6 #                    (c) 2006 A.M. Thurnherr
       
     7 #                    uE-Info: 32 57 NIL 0 0 72 0 2 4 NIL ofnI
       
     8 #======================================================================
       
     9 
       
    10 # Split data file into per-bin time series.
       
    11 
       
    12 # HISTORY:
       
    13 #	Aug 25, 2006: - created from [listEns]
       
    14 #	Aug 26, 2006: - added -M)agdecl
       
    15 #				  - changed -b to -f
       
    16 #	Aug 27, 2006: - added %bin
       
    17 #	Aug 28, 2006: - BUG: options were confused
       
    18 #	Jan  4, 2007: - improved usage message for -a
       
    19 #				  - added %mag_decl
       
    20 #				  - BUG: roundoff error in %pct_good_vels
       
    21 #	Sep 19, 2007: - adapted to new [RDI_BB_Read.pl]
       
    22 #	Feb  7, 2008: - added sound-speed correction
       
    23 #				  - enabled 3-beam solutions
       
    24 #	Feb  8, 2008: - added -d)iscard <beam>
       
    25 #				  - added -b)eam coordinate output
       
    26 #	Feb 12, 2008: - modified 3-beam output
       
    27 #				  - added -p)ct_good <min>
       
    28 #	Feb 13, 2008: - various improvements
       
    29 #	Feb 19, 2008: - BUG: division by zero
       
    30 #					BUG: min() did not work with 1st elt undef
       
    31 #	Feb 21, 2008: - BUG: had forgotten to undo debugging changes
       
    32 #				  - removed missing magdecl warning on -b
       
    33 
       
    34 # General Notes:
       
    35 #	- everything (e.g. beams) is numbered from 1
       
    36 #	- no support for BT data
       
    37 
       
    38 # Soundspeed Correction:
       
    39 #	- applied as described in the RDI coord-trans manual
       
    40 #	- sound-speed variation over range is ignored (valid for small gradients)
       
    41 #	=> - same simple correction for all velocity components
       
    42 #	   - simple correction for cell depths
       
    43 
       
    44 # Min %-good (min_pcg):
       
    45 #	- nan for records w/o valid velocities
       
    46 #	- min(%-good) of the beams used for the velocity solution
       
    47 #	- min_pcg does not have to decrease monotonically with distance,
       
    48 #	  at least when 3-beam solutions are allowed and when -p is used to
       
    49 #	  edit the data
       
    50 #	- non-monotonic min_pcg is particularly obvious with the DYNAMUCK BM_ADCP
       
    51 #	  data, where one of the beams performed much worse than the others
       
    52 
       
    53 require "getopts.pl";
       
    54 $0 =~ m{(.*/)[^/]+};
       
    55 require "$1RDI_BB_Read.pl";
       
    56 require "$1RDI_Coords.pl";
       
    57 require "$1RDI_Utils.pl";
       
    58 
       
    59 die("Usage: $0 [-r)ange <first_ens,last_ens>] " .
       
    60 			  "[output -f)ile <pat[bin%d.raw]>] " .
       
    61 			  "[-a)ll ens (not just those with good vels)] " .
       
    62 			  "[-M)agnetic <declination>] " .
       
    63 			  "[-S)oundspeed correction <salin|*,temp|*,depth|*> " .
       
    64 		 	  "[require -4)-beam solutions] [-d)iscard <beam#>] " .
       
    65 		 	  "[-%)good <min>] " .
       
    66 		 	  "[output -b)eam coordinates] " .
       
    67 			  "<RDI file>\n")
       
    68 	unless (&Getopts("4abd:f:M:p:r:S:") && @ARGV == 1);
       
    69 
       
    70 die("$0: -4 and -d are mutually exclusive\n")
       
    71 	if ($opt_4 && defined($opt_d));
       
    72 
       
    73 die("$0: -p and -b are mutually exclusive\n")
       
    74 	if ($opt_b && defined($opt_p));
       
    75 
       
    76 $opt_p = 0 unless defined($opt_p);
       
    77 
       
    78 $RDI_Coords::minValidVels = 4 if ($opt_4);			# no 3-beam solutions
       
    79 
       
    80 print(STDERR "WARNING: magnetic declination not set!\n")
       
    81 	unless defined($opt_M) || defined($opt_b);
       
    82 
       
    83 $opt_f = 'bin%d.raw' unless defined($opt_f);
       
    84 $ifn = $ARGV[0];
       
    85 
       
    86 ($first_ens,$last_ens) = split(',',$opt_r)
       
    87 	if defined($opt_r);
       
    88 
       
    89 ($salin,$temp,$depth) = split(',',$opt_S)
       
    90 	if defined($opt_S);
       
    91 $variable_ssCorr = ($salin eq '*' || $temp eq '*' || $depth eq '*');
       
    92 
       
    93 #----------------------------------------------------------------------
       
    94 
       
    95 sub ssCorr($$$$)						# sound velocity correction factor
       
    96 {
       
    97 	my($e,$S,$T,$D) = @_;
       
    98 	$S = $dta{ENSEMBLE}[$e]->{SALINITY} if ($S eq '*');
       
    99 	$T = $dta{ENSEMBLE}[$e]->{TEMPERATURE} if ($T eq '*');
       
   100 	if ($D eq '*') {
       
   101 		print(STDERR "WARNING: soundspeed correction using instrument pressure instead of depth!\n")
       
   102 			unless ($warned);
       
   103 		$warned = 1;
       
   104 		$D = $dta{ENSEMBLE}[$e]->{PRESSURE};
       
   105 	}
       
   106 	return soundSpeed($S,$T,$D) / $dta{ENSEMBLE}[$e]->{SPEED_OF_SOUND};
       
   107 }
       
   108 	
       
   109 sub min(@)								# return minimum
       
   110 {
       
   111 	my($min) = 99e99;
       
   112 	for (my($i)=0; $i<=$#_; $i++) {
       
   113 		$min = $_[$i] if defined($_[$i]) && ($_[$i] < $min);
       
   114 	}
       
   115 	return ($min == 99e99) ? nan : $min;
       
   116 }
       
   117 
       
   118 sub dumpBin($$$)						# write time series of single bin
       
   119 {
       
   120 	my($b,$fe,$le) = @_;
       
   121 	my($file) = sprintf($opt_f,$b+1);
       
   122 
       
   123 	open(P,">$file") || die("$file: $!\n");
       
   124 	print(P "#ANTS#PARAMS# ");
       
   125 	foreach my $k (keys(%P)) {
       
   126 		print(P "$k\{$P{$k}\} ");
       
   127 	}
       
   128 	my($pct3b) = ($good_vels[$b] > 0) ? 100*$three_beam[$b]/$good_vels[$b] : nan;
       
   129 	printf(STDERR "%.0f%%/%.0f%% ",100*$good_vels[$b]/($le-$fe+1),$pct3b);
       
   130 	printf(P "pct_good_vels{%.0f} ",100*$good_vels[$b]/($le-$fe+1));
       
   131 	printf(P "pct_3_beam{%.0f} ",$pct3b);
       
   132 	printf(P "bin{%d}",$b+1);
       
   133 	printf(P " soundspeed_correction{%s}",defined($opt_S) ? $opt_S : 'NONE!');
       
   134 	printf(P " dz{%g}",$dz[$b] *
       
   135 				(defined($opt_S) ? ssCorr($fe,$salin,$temp,$depth) : 1)
       
   136 	) unless ($variable_ssCorr);
       
   137 	print( P "\n");
       
   138 
       
   139 	print(P "#ANTS#FIELDS# " .
       
   140 			"{ensemble} {date} {time} {elapsed} " .
       
   141 			"{heading} {pitch} {roll} " .
       
   142 			"{sig_heading} {sig_pitch} {sig_roll} " .
       
   143 			"{xmit_current} {xmit_voltage} " .
       
   144 			"{temp} " .
       
   145 			($opt_b ? "{v1} {v2} {v3} {v4} " : "{u} {v} {w} {err_vel} ") .
       
   146 			"{corr1} {corr2} {corr3} {corr4} " .
       
   147 			"{amp1} {amp2} {amp3} {amp4} " .
       
   148 			($opt_b ? "{pcg1} {pcg2} {pcg3} {pcg4}" : "{3_beam} {min_pcg}")
       
   149 	);
       
   150 	print(P " {dz}") if ($variable_ssCorr);
       
   151 	print(P "\n");
       
   152 
       
   153 	my($t0) = $dta{ENSEMBLE}[$fe]->{UNIX_TIME};
       
   154 	for (my($e)=$fe; $e<=$le; $e++) {
       
   155 		next unless ($opt_a || $dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b]);
       
   156 
       
   157 		my($ssCorr) = defined($opt_S) ? ssCorr($e,$salin,$temp,$depth) : 1;
       
   158 		
       
   159 		print(P "$dta{ENSEMBLE}[$e]->{NUMBER} ");
       
   160 		print(P "$dta{ENSEMBLE}[$e]->{DATE} ");
       
   161 		print(P "$dta{ENSEMBLE}[$e]->{TIME} ");
       
   162 		printf(P "%d ",$dta{ENSEMBLE}[$e]->{UNIX_TIME}-$t0);
       
   163 		print(P "$dta{ENSEMBLE}[$e]->{HEADING} ");
       
   164 		print(P "$dta{ENSEMBLE}[$e]->{PITCH} ");
       
   165 		print(P "$dta{ENSEMBLE}[$e]->{ROLL} ");
       
   166 		print(P "$dta{ENSEMBLE}[$e]->{HEADING_STDDEV} ");
       
   167 		print(P "$dta{ENSEMBLE}[$e]->{PITCH_STDDEV} ");
       
   168 		print(P "$dta{ENSEMBLE}[$e]->{ROLL_STDDEV} ");
       
   169 		print(P "$dta{ENSEMBLE}[$e]->{ADC_XMIT_CURRENT} ");
       
   170 		print(P "$dta{ENSEMBLE}[$e]->{ADC_XMIT_VOLTAGE} ");
       
   171 		print(P "$dta{ENSEMBLE}[$e]->{TEMPERATURE} ");
       
   172 		if ($dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b]) {
       
   173 			printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0] * $ssCorr);
       
   174 			printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1] * $ssCorr);
       
   175 			printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2] * $ssCorr);
       
   176 			printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3] * $ssCorr);
       
   177 		} else {
       
   178 			print(P "nan nan nan nan ");
       
   179 		}
       
   180 		print(P "@{$dta{ENSEMBLE}[$e]->{CORRELATION}[$b]} ");
       
   181 		print(P "@{$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$b]} ");
       
   182 		if ($opt_b) {
       
   183 			print(P "@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]} ");
       
   184 		} else {
       
   185 			printf(P "%d ",$dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b]);
       
   186 			printf(P "%s ",min(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]}));
       
   187 		}
       
   188 		printf(P "%g ",$dz[$b]*$ssCorr) if ($variable_ssCorr);
       
   189 		print(P "\n");
       
   190 	}
       
   191 	close(P);
       
   192 }
       
   193 
       
   194 #----------------------------------------------------------------------
       
   195 # MAIN
       
   196 #----------------------------------------------------------------------
       
   197 
       
   198 $P{RDI_file} = $ifn;
       
   199 $P{mag_decl} = $opt_M if defined($opt_M);
       
   200 
       
   201 readData($ifn,\%dta);
       
   202 printf("%d complete ensembles...\n",scalar(@{$dta{ENSEMBLE}}));
       
   203 $dta{HEADING_BIAS} = -$opt_M;						# magnetic declination
       
   204 
       
   205 if ($dta{BEAM_COORDINATES}) {						# coords
       
   206 	$beamCoords = 1;
       
   207 } else {
       
   208 	die("$0: -b requires input in beam coordinates\n")
       
   209 		if ($opt_b);
       
   210 	die("$ifn: only beam and earth coordinates implemented so far\n")
       
   211 		if (!$dta{EARTH_COORDINATES});
       
   212 }
       
   213 
       
   214 $P{N_ensembles} = @{$dta{ENSEMBLE}};
       
   215 
       
   216 for (my($b)=0; $b<$dta{N_BINS}; $b++) {				# calc dz
       
   217 	$dz[$b] = $dta{DISTANCE_TO_BIN1_CENTER} + $b*$dta{BIN_LENGTH};
       
   218 }
       
   219 
       
   220 $lastGoodBin = 0;
       
   221 for ($e=0; $e<=$#{$dta{ENSEMBLE}}; $e++) {			# check/transform velocities
       
   222 	next if (defined($first_ens) &&
       
   223 			 $dta{ENSEMBLE}[$e]->{NUMBER} < $first_ens);
       
   224 	$P{first_ens} = $dta{ENSEMBLE}[$e]->{NUMBER},$fe = $e
       
   225 		unless defined($P{first_ens});
       
   226 	last if (defined($last_ens) &&
       
   227 			 $dta{ENSEMBLE}[$e]->{NUMBER} > $last_ens);
       
   228 	$P{last_ens} = $dta{ENSEMBLE}[$e]->{NUMBER};
       
   229 	$le = $e;
       
   230 
       
   231 	die("3-beams used in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
       
   232 		if ($dta{ENSEMBLE}[$e]->{N_BEAMS_USED} < 4);
       
   233 	die("BIT error in ensemble $dta{ENSEMBLE}[$e]->{NUMBER}\n")
       
   234 		if defined($dta{ENSEMBLE}[$e]->{BUILT_IN_TEST_ERROR});
       
   235 	die("Low gain in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
       
   236         if ($dta{ENSEMBLE}[$e]->{LOW_GAIN});
       
   237 
       
   238 	for (my($b)=0; $b<$dta{N_BINS}; $b++) {
       
   239 		if (defined($opt_d)) {
       
   240 			undef($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$opt_d-1]);
       
   241 			undef($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][$opt_d-1]);
       
   242 		}
       
   243 		for (my($i)=0; $i<4; $i++) {
       
   244 			if ($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$i] < $opt_p) {
       
   245 				undef($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$i]);
       
   246 				undef($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][$i]);
       
   247 			}
       
   248         }
       
   249 		@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = $beamCoords
       
   250 			? velInstrumentToEarth(\%dta,$e,
       
   251 				  velBeamToInstrument(\%dta,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
       
   252 			  )
       
   253 			: velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
       
   254 				unless ($opt_b);
       
   255 		unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0])) {
       
   256 			undef(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]});
       
   257 			next;
       
   258 		}
       
   259 		$dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b] = $RDI_Coords::threeBeamFlag;
       
   260 		$three_beam[$b] += $RDI_Coords::threeBeamFlag;
       
   261 		$dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b] = 1;
       
   262 		$good_vels[$b]++; 
       
   263 		$lastGoodBin = $b if ($b > $lastGoodBin);
       
   264 		$firstGoodEns = $e unless defined($firstGoodEns);
       
   265 		$lastGoodEns = $e;
       
   266     }
       
   267 }
       
   268 
       
   269 unless (defined($opt_r)) {
       
   270 	$fe = $firstGoodEns;
       
   271 	$le = $lastGoodEns;
       
   272 }
       
   273 
       
   274 $firstBin = 0;
       
   275 $lastBin = $lastGoodBin;
       
   276 
       
   277 print( STDERR "Start      : $dta{ENSEMBLE}[$fe]->{DATE} $dta{ENSEMBLE}[$fe]->{TIME}\n");
       
   278 print( STDERR "End        : $dta{ENSEMBLE}[$le]->{DATE} $dta{ENSEMBLE}[$le]->{TIME}\n");
       
   279 printf(STDERR "Bins       : %d-%d\n",$firstBin+1,$lastBin+1);
       
   280 printf(STDERR "3-Beam     : %d %d %d %d\n",$RDI_Coords::threeBeam_1,
       
   281 										   $RDI_Coords::threeBeam_2,
       
   282 										   $RDI_Coords::threeBeam_3,
       
   283 										   $RDI_Coords::threeBeam_4);
       
   284 
       
   285 print(STDERR "Good/3-beam: ");
       
   286 for ($b=$firstBin; $b<=$lastBin; $b++) {				# generate output
       
   287 	dumpBin($b,$fe,$le);
       
   288 }
       
   289 print(STDERR "\n");
       
   290 
       
   291 exit(0);