LADCP_w_CTD
changeset 35 54b8bb450e5f
parent 34 e550db661c17
child 39 91458506d56f
equal deleted inserted replaced
34:e550db661c17 35:54b8bb450e5f
     1 #!/usr/bin/perl
     1 #!/usr/bin/perl
     2 #======================================================================
     2 #======================================================================
     3 #                    L A D C P _ W _ C T D 
     3 #                    L A D C P _ W _ C T D 
     4 #                    doc: Mon Nov  3 17:34:19 2014
     4 #                    doc: Mon Nov  3 17:34:19 2014
     5 #                    dlm: Mon Jan 25 09:32:32 2016
     5 #                    dlm: Fri Feb 19 08:01:38 2016
     6 #                    (c) 2014 A.M. Thurnherr
     6 #                    (c) 2014 A.M. Thurnherr
     7 #                    uE-Info: 51 0 NIL 0 0 72 2 2 4 NIL ofnI
     7 #                    uE-Info: 241 0 NIL 0 0 72 2 2 4 NIL ofnI
     8 #======================================================================
     8 #======================================================================
     9 
     9 
    10 $antsSummary = 'pre-process SBE 9plus CTD data for LADCP_w';
    10 $antsSummary = 'pre-process SBE 9plus CTD data for LADCP_w';
    11 
    11 
    12 # HISTORY:
    12 # HISTORY:
    46 #					(requring halving of resolution)
    46 #					(requring halving of resolution)
    47 #	Oct 12, 2015: - require ANTSlibs V6.2 for release
    47 #	Oct 12, 2015: - require ANTSlibs V6.2 for release
    48 #	Oct 13, 2015: - added code to deal with CNV files with wrong number of scans in header
    48 #	Oct 13, 2015: - added code to deal with CNV files with wrong number of scans in header
    49 #   Oct 13, 2015: - adapted to [version.pl]
    49 #   Oct 13, 2015: - adapted to [version.pl]
    50 #   Jan 25, 2016: - added software version %PARAM
    50 #   Jan 25, 2016: - added software version %PARAM
       
    51 #	Feb 16, 2016: - BUG: %sampling_interval was erroenously called %sampling_frequency
       
    52 #	Feb 19, 2016: - improved on-surface data detection (conductivity <= 10mS/cm)
       
    53 #				  - BUG: temperatures < 0 were not allowed
       
    54 #				  - temperature histogram resolution increased from 1degC to 0.5degC for no good reason
    51 
    55 
    52 ($ANTS)  = (`which ANTSlib`   =~ m{^(.*)/[^/]*$});
    56 ($ANTS)  = (`which ANTSlib`   =~ m{^(.*)/[^/]*$});
    53 ($WCALC) = ($0                =~ m{^(.*)/[^/]*$});
    57 ($WCALC) = ($0                =~ m{^(.*)/[^/]*$});
    54 $WCALC   = '.' if ($WCALC eq '');
    58 $WCALC   = '.' if ($WCALC eq '');
    55 
    59 
   167 sub validRange($)
   171 sub validRange($)
   168 {
   172 {
   169 	my($guess_bin) = @_;
   173 	my($guess_bin) = @_;
   170 	my($min_bin,$max_bin);
   174 	my($min_bin,$max_bin);
   171 
   175 
   172 	croak("assertion failed") unless ($hist[$guess_bin]);
   176 	die("assertion failed") unless ($hist[$guess_bin]);
   173 	for ($max_bin=$guess_bin; $hist[$max_bin]; $max_bin++) { }
   177 	for ($max_bin=$guess_bin; $hist[$max_bin]; $max_bin++) { }
   174 	for ($min_bin=$guess_bin; $hist[$min_bin]; $min_bin--) { }
   178 	for ($min_bin=$guess_bin; $hist[$min_bin]; $min_bin--) { }
   175 	return ($min_bin,$max_bin+1);
   179 	return ($min_bin,$max_bin+1);
   176 }
   180 }
   177 
   181 
   178 unless ($opt_r) {
   182 unless ($opt_r) {
   179 	print(STDERR "Editing Data...") if ($opt_v);
   183 	print(STDERR "Editing Data...") if ($opt_v);
   180 
   184 
   181 	#----------------------------------------
   185 	#----------------------------------------
   182 	# trim initial records with nan pressures
   186 	# trim initial records with
       
   187 	# 	- nan pressure
       
   188 	#	- nan conductivity
       
   189 	#	- conductivity <= 10 mS/cm
   183 	#----------------------------------------
   190 	#----------------------------------------
   184 	my($trimmed) = 0;												# trim leading nan pressures
   191 	my($trimmed) = 0;												# trim leading nan pressures
   185 	shift(@ants_),$trimmed++
   192 	shift(@ants_),$trimmed++
   186 		until numberp($ants_[0][$pressF]) && numberp($ants_[0][$condF]);
   193 		until numberp($ants_[0][$pressF]) &&
   187 	printf(STDERR "\n\t%d initial records with nan pressure and/or conductivity trimmed",$trimmed) if ($opt_v > 1);
   194 			  numberp($ants_[0][$condF]) &&
       
   195 			  (($P{cond.unit} eq 'mS/cm' && $ants_[0][$condF] > 10) ||
       
   196 			   ($P{cond.unit} eq 'S/m'   && $ants_[0][$condF] > 1));
       
   197 	printf(STDERR "\n\t%d initial at-surface records trimmed",$trimmed) if ($opt_v > 1);
   188 	my($lvp) = $ants_[0][$pressF];
   198 	my($lvp) = $ants_[0][$pressF];
   189 	my($lvc) = $ants_[0][$condF];
   199 	my($lvc) = $ants_[0][$condF];
   190 	
   200 	
   191 	#------------------------------------------------
   201 	#------------------------------------------------
   192 	# edit pressure outliers outside contiguous range
   202 	# edit pressure outliers outside contiguous range
   225 		$hist[$b]++;
   235 		$hist[$b]++;
   226 		next unless ($hist[$b] > $modeSamp);
   236 		next unless ($hist[$b] > $modeSamp);
   227 		$modeSamp = $hist[$b]; $modeBin = $b;
   237 		$modeSamp = $hist[$b]; $modeBin = $b;
   228 	}
   238 	}
   229 	($min,$max) = validRange($modeBin);
   239 	($min,$max) = validRange($modeBin);
   230 	$min /= $condHistRes; $max /= $condHistRes; 
   240 	$min /= $condHistRes; $max /= $condHistRes;
   231 	for (my($s)=0; $s<$nscans; $s++) {
   241 	for (my($s)=0; $s<$nscans; $s++) {
   232 		if ($ants_[$s][$condF] > $max) { $outliers++; $ants_[$s][$condF] = nan; }
   242 		if ($ants_[$s][$condF] > $max) { $outliers++; $ants_[$s][$condF] = nan; }
   233 		if ($ants_[$s][$condF] < $min) { $outliers++; $ants_[$s][$condF] = nan; }
   243 		if ($ants_[$s][$condF] < $min) { $outliers++; $ants_[$s][$condF] = nan; }
   234 	}
   244 	}
   235 	&antsAddParams("conductivity_outliers",sprintf("%d",$outliers));
   245 	&antsAddParams("conductivity_outliers",sprintf("%d",$outliers));
   239 
   249 
   240 	#----------------------------------------------------
   250 	#----------------------------------------------------
   241 	# edit temperature outliers outside contiguous range
   251 	# edit temperature outliers outside contiguous range
   242 	#	- Stan's NBP0901 profiles require resolution of 1deg
   252 	#	- Stan's NBP0901 profiles require resolution of 1deg
   243 	#	- otherwise 0.2deg seems to be fine
   253 	#	- otherwise 0.2deg seems to be fine
       
   254 	#	- however, on Feb 19, 2016 it was found that the
       
   255 	#     resolution had been left at 1degC without any
       
   256 	#     apparent adverse effects
   244 	#----------------------------------------------------
   257 	#----------------------------------------------------
   245 	$outliers = $modeSamp = 0;
   258 	$outliers = $modeSamp = 0;
   246 	undef(@hist);
   259 	undef(@hist);
   247 	for (my($s)=0; $s<$nscans; $s++) {
   260 	for (my($s)=0; $s<$nscans; $s++) {
   248 		next unless ($ants_[$s][$tempF] > 0);
   261 		next unless ($ants_[$s][$tempF] >= -10);
   249 		my($b) = $ants_[$s][$tempF]*1;								# 10th of a degree histogram resolution
   262 		my($b) = ($ants_[$s][$tempF] + 10) * 0.5;
   250 		$hist[$b]++;
   263 		$hist[$b]++;
   251 		next unless ($hist[$b] > $modeSamp);
   264 		next unless ($hist[$b] > $modeSamp);
   252 		$modeSamp = $hist[$b]; $modeBin = $b;
   265 		$modeSamp = $hist[$b]; $modeBin = $b;
   253 	}
   266 	}
   254 	printf(STDERR "\n\ttemperature mode: %.1f degC (%d samples)",$modeBin/10,$modeSamp)
   267 #	printf(STDERR "\n\ttemperature mode: %.1f degC (%d samples)",$modeBin/0.5-10,$modeSamp)
   255 		if ($opt_v > 1);
   268 #		if ($opt_v > 1);
   256 	($min,$max) = validRange($modeBin);
   269 	($min,$max) = validRange($modeBin);
   257 	$min /= 1; $max /= 1; 
   270 	$min = ($min / 0.5) - 10;
       
   271 	$max = ($max / 0.5) - 10;
   258 	for (my($s)=0; $s<$nscans; $s++) {
   272 	for (my($s)=0; $s<$nscans; $s++) {
   259 		if ($ants_[$s][$tempF] > $max) { $outliers++; $ants_[$s][$tempF] = nan; }
   273 		if ($ants_[$s][$tempF] > $max) { $outliers++; $ants_[$s][$tempF] = nan; }
   260 		if ($ants_[$s][$tempF] < $min) { $outliers++; $ants_[$s][$tempF] = nan; }
   274 		if ($ants_[$s][$tempF] < $min) { $outliers++; $ants_[$s][$tempF] = nan; }
   261 	}
   275 	}
   262 	&antsAddParams("temperature_outliers",sprintf("%d",$outliers));
   276 	&antsAddParams("temperature_outliers",sprintf("%d",$outliers));
   393 # Binning data
   407 # Binning data
   394 #----------------------------------------------------------------------
   408 #----------------------------------------------------------------------
   395 
   409 
   396 my($sps) = round(1 / $sampint / $opt_s);
   410 my($sps) = round(1 / $sampint / $opt_s);
   397 print(STDERR "Creating ${opt_s}Hz time series ($sps samples per bin)...") if ($opt_v);
   411 print(STDERR "Creating ${opt_s}Hz time series ($sps samples per bin)...") if ($opt_v);
   398 &antsAddParams('sampling_frequency',1/$opt_s);
   412 &antsAddParams('sampling_interval',1/$opt_s);
   399 &antsAddParams('sampling_rate',$opt_s);
   413 &antsAddParams('sampling_frequency',$opt_s);
   400 
   414 
   401 my(@press,@temp,@cond);
   415 my(@press,@temp,@cond);
   402 my($sp,$np,$st,$nt,$sc,$nc);
   416 my($sp,$np,$st,$nt,$sc,$nc);
   403 
   417 
   404 $sp = $st = $sc = $np = $nt = $nc = 0;
   418 $sp = $st = $sc = $np = $nt = $nc = 0;