author | A.M. Thurnherr <athurnherr@yahoo.com> |
Tue, 27 Nov 2018 16:59:05 -0500 | |
changeset 49 | 5006e9158207 |
parent 48 | d9309804b6cf |
child 51 | 0f6d9e64cc4f |
permissions | -rwxr-xr-x |
18 | 1 |
#!/usr/bin/perl |
2 |
#====================================================================== |
|
29 | 3 |
# L A D C P _ W _ C T D |
18 | 4 |
# doc: Mon Nov 3 17:34:19 2014 |
49 | 5 |
# dlm: Fri Oct 5 14:52:00 2018 |
18 | 6 |
# (c) 2014 A.M. Thurnherr |
49 | 7 |
# uE-Info: 85 93 NIL 0 0 72 2 2 4 NIL ofnI |
18 | 8 |
#====================================================================== |
9 |
||
10 |
$antsSummary = 'pre-process SBE 9plus CTD data for LADCP_w'; |
|
11 |
||
12 |
# HISTORY: |
|
13 |
# Nov 3, 2014: - created |
|
14 |
# Nov 4, 2014: - improved |
|
15 |
# Nov 6, 2014: - BUG: sound speed was not calculated correctly |
|
16 |
# - added -a |
|
17 |
# - added conductivity & temperature editing |
|
18 |
# Nov 7, 2014: - loosened outlier editing |
|
19 |
# - added no-valid-data error message |
|
20 |
# - modified binning criterion to allow any sampling |
|
21 |
# frequency (not just divisors of 24) |
|
28
b07b23485336
after folding in EGU flight mods (empirical Sv correction)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
26
diff
changeset
|
22 |
# Apr 17, 2015: - added in-situ temperature to output |
29 | 23 |
# Apr 23, 2015: - had to losen unrealistic-soundspeed criterion |
24 |
# because of DUK2#45 |
|
25 |
# May 13, 2015: - BUG: did not work with casts beginning at depth |
|
26 |
# - added errors when editing edits too much |
|
27 |
# May 14, 2015: - BUG: depth was wrong for partial-depth casts |
|
28 |
# - BUG: $badval had not been handled correctly |
|
29 |
# - BUG: editing errors were too tight |
|
30 |
# Jun 16, 2015: - added STDOUT redirection (default output file) on tty stdout |
|
31 |
# - added -p)lot option |
|
32 |
# Jun 17, 2015: - added cond to std output |
|
33 |
# - added conductivity spike filter (different from pressure) |
|
34 |
# Jun 18, 2015: - renamed from SBE_w |
|
35 |
# Jun 19, 2015: - added press & salin to make output suitable for u/v processing |
|
36 |
# - removed a couple of data assertions |
|
37 |
# Jul 20, 2015: - allow for non-numeric -i |
|
31 | 38 |
# Aug 17, 2015: - modified plots |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
39 |
# Sep 21, 2015: - added lat/lon to output if available in input |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
40 |
# Sep 22, 2015: - added error on non-numeric station information |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
41 |
# - changed plot output name |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
42 |
# Sep 23, 2015: - added -o |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
43 |
# - improved pressure editing |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
44 |
# - modified editing algorithm, primarily reducing temperature resolution |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
45 |
# from 0.1deg to 1deg and finding contiguous range up to single gap in histogram |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
46 |
# (requring halving of resolution) |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
47 |
# Oct 12, 2015: - require ANTSlibs V6.2 for release |
33 | 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] |
|
34 | 50 |
# Jan 25, 2016: - added software version %PARAM |
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
51 |
# Feb 16, 2016: - BUG: %sampling_interval was erroenously called %sampling_frequency |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
52 |
# Feb 19, 2016: - improved on-surface data detection (conductivity <= 10mS/cm) |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
53 |
# - BUG: temperatures < 0 were not allowed |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
54 |
# - temperature histogram resolution increased from 1degC to 0.5degC for no good reason |
39 | 55 |
# Mar 13, 2016: - added simple ASCII file format |
56 |
# - BUG: Layout error when input had no lat/lon |
|
40 | 57 |
# - added lon to simple ASCII format |
41 | 58 |
# Mar 16, 2016: - adapted to gmt5 |
42 | 59 |
# Mar 19, 2016: - added support for $VERB environment var |
60 |
# - added $libSBE_quiet flag |
|
43 | 61 |
# Mar 30, 2016: - added station number of ASCII format |
62 |
# Mar 31, 2016: - changed version %PARAM |
|
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
63 |
# May 26, 2016: - renamed w[.raw] to CTD_w[.raw] |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
64 |
# - added winch_w |
48 | 65 |
# Jul 29, 2016: - change w_CTD plot label to reflect sign convention |
49 | 66 |
# Oct 3, 2017: - cosmetics |
67 |
# - added -q to suppress plots when generating 1Hz files |
|
68 |
# Dec 9, 2017: - added $antsSuppressCommonOptions = 1; |
|
69 |
# Dec 17, 2017: - added dependencies |
|
70 |
# Mar 8, 2018: - made error messages work with -v |
|
71 |
# - renamed -l)owpass option to -c)utoff |
|
72 |
# - added new -l)atitude |
|
73 |
# Mar 9, 2018: - changed -l) to location (include lon as well) |
|
74 |
# May 22, 2018: - added code to remove initial at surface interval |
|
75 |
# Jul 24, 2018: - improved CTD load error message |
|
76 |
# Oct 4, 2018: - minor improvement in log |
|
77 |
# - BUG: profiles collected with LOTS of wave heave (AAIW 2005, 004) |
|
78 |
# failed the contiguous pressure test at 2dbar resolution |
|
79 |
# => increased to 3dbar |
|
80 |
# Oct 5, 2018: - BUG: previous bug "fix" did not solve a real problem, because |
|
81 |
# problem with AAIW data is lack of pressure resolution |
|
82 |
# => returned to 2dbar |
|
83 |
# - added plotting errors |
|
84 |
# - improved log message |
|
85 |
# - BUG: initial in-air scans were not handled correctly (nscans not updated) |
|
86 |
||
48 | 87 |
|
88 |
# NOTES: |
|
89 |
# w_CTD is positive during the downcast to make the sign of the apparent |
|
90 |
# water velocity consistent with w_ocean |
|
18 | 91 |
|
33 | 92 |
($ANTS) = (`which ANTSlib` =~ m{^(.*)/[^/]*$}); |
93 |
($WCALC) = ($0 =~ m{^(.*)/[^/]*$}); |
|
94 |
$WCALC = '.' if ($WCALC eq ''); |
|
18 | 95 |
|
33 | 96 |
require "$WCALC/version.pl"; |
18 | 97 |
require "$ANTS/ants.pl"; |
98 |
require "$ANTS/fft.pl"; |
|
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
99 |
require "$ANTS/libstats.pl"; |
49 | 100 |
require "$ANTS/libconv.pl"; |
29 | 101 |
require "$ANTS/libGMT.pl"; |
18 | 102 |
require "$ANTS/libSBE.pl"; |
103 |
require "$ANTS/libEOS83.pl"; |
|
43 | 104 |
&antsAddParams('LADCP_w_CTD::version',$VERSION); |
18 | 105 |
|
106 |
$antsParseHeader = 0; # usage |
|
49 | 107 |
$antsSuppressCommonOptions = 1; |
108 |
&antsUsage('ai:l:orp:qs:v:w:',1, |
|
18 | 109 |
'[-v)erbosity <level[0]>]', |
110 |
'[use -a)lternate sensor pair]', |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
111 |
'[-r)etain all data (no editing)] [allow infinite -o)utliers]', |
18 | 112 |
'[-s)ampling <rate[6Hz]>]', |
49 | 113 |
'[lowpass w_CTD -c)utoff <limit[2s]>] [-w)inch-speed <granularity[10s]>]', |
114 |
'[profile -i)d <id>] [station -l)ocation <lat/lon>]', |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
115 |
'[-p)lot_basenames <[%03d_w_CTD.ps],[%03d_sspd.ps]>]', |
49 | 116 |
'[-q)uiet (no plots)]', |
18 | 117 |
'<SBE CNV file>'); |
118 |
||
49 | 119 |
&antsFloatOpt(\$opt_c,2); # default low-pass cutoff for w_CTD |
120 |
&antsCardOpt(\$opt_s,6); # default output sampling rate (Hz) |
|
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
121 |
&antsFloatOpt(\$opt_w,10); # winch velocity granularity |
42 | 122 |
&antsCardOpt(\$opt_v,$ENV{VERB}); # support VERB env variable |
18 | 123 |
|
124 |
$CNVfile = $ARGV[0]; # open CNV file |
|
125 |
open(F,&antsFileArg()); |
|
49 | 126 |
&antsAddDeps($CNVfile); |
28
b07b23485336
after folding in EGU flight mods (empirical Sv correction)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
26
diff
changeset
|
127 |
&antsActivateOut(); # activate ANTS file |
18 | 128 |
|
129 |
#---------------------------------------------------------------------- |
|
130 |
# Read Data |
|
131 |
#---------------------------------------------------------------------- |
|
132 |
||
49 | 133 |
sub _croak(@) |
134 |
{ |
|
135 |
print(STDERR "\n") if ($opt_v); |
|
136 |
croak(@_); |
|
137 |
} |
|
138 |
||
18 | 139 |
print(STDERR "Reading $CNVfile...") if ($opt_v); |
140 |
||
39 | 141 |
chomp($rec = <F>); |
49 | 142 |
_croak("$CNVfile: no data\n") |
39 | 143 |
unless defined($rec); |
18 | 144 |
|
39 | 145 |
if ($rec =~ /^\*/) { # SBE CNV file |
42 | 146 |
$libSBE_quiet = 1; # suppress diagnostic messages |
39 | 147 |
($nfields,$nscans,$sampint,$badval,$ftype,$lat,$lon) = # decode SBE header |
148 |
SBE_parseHeader(F,0,0); # SBE field names, no time check |
|
149 |
||
49 | 150 |
_croak("$CNVfile: unexpected sampling interval $sampint\n") |
39 | 151 |
unless (abs($sampint-1/24) < 1e-5); |
49 | 152 |
|
153 |
if (defined($opt_l)) { # set/override station location with -l |
|
154 |
my($slat,$slon) = split('[,/]',$opt_l); |
|
155 |
$lat = GMT2deg($slat); |
|
156 |
$lon = GMT2deg($slon); |
|
157 |
_croak("$0: cannot decode -l $opt_l\n") |
|
158 |
unless numberp($lat) && numberp($lon); |
|
159 |
} |
|
160 |
_croak("$CNVfile: no latitude in header => -l required\n") |
|
39 | 161 |
unless numberp($lat); |
49 | 162 |
|
163 |
&antsAddParams('lat',$lat); |
|
164 |
&antsAddParams('lon',$lon); |
|
39 | 165 |
|
166 |
$pressF = fnr('prDM'); |
|
167 |
||
168 |
if ($opt_a) { # temp/cond alternate sensor pair |
|
169 |
$tempF = fnr('t190C'); |
|
170 |
$condF = fnrNoErr('c1S/m'); |
|
171 |
if (defined($condF)) { |
|
172 |
$condHistRes = 20; # 0.2 S/m bins |
|
173 |
} else { |
|
174 |
$condF = fnr('c1mS/cm'); |
|
175 |
$condHistRes = 2; # 2.0 mS/cm bins |
|
176 |
} |
|
177 |
} else { # primary sensor pair |
|
178 |
$tempF = fnr('t090C'); |
|
179 |
$condF = fnrNoErr('c0S/m'); |
|
180 |
if (defined($condF)) { |
|
181 |
$condHistRes = 20; |
|
182 |
} else { |
|
183 |
$condF = fnr('c0mS/cm'); |
|
184 |
$condHistRes = 2; |
|
185 |
} |
|
18 | 186 |
} |
39 | 187 |
|
188 |
$latF = &fnrNoErr('lat'); # GPS data if available (to make files useful for u/v processing) |
|
189 |
$lonF = &fnrNoErr('lon'); |
|
190 |
||
191 |
&antsInstallBufFull(0); # read entire CNV file |
|
192 |
&SBEin(F,$ftype,$nfields,$nscans,$badval); |
|
193 |
||
194 |
if (@ants_ != $nscans) { |
|
195 |
if ($opt_v > 1) { |
|
49 | 196 |
printf(STDERR "\n\nWARNING: $CNVfile has wrong number of scans in header (%d instead of %d)\n",$nscans,scalar(@ants_)); |
39 | 197 |
} else { |
49 | 198 |
printf(STDERR "WARNING: $CNVfile has wrong number of scans in header (%d instead of %d)\n",$nscans,scalar(@ants_)); |
39 | 199 |
} |
200 |
$nscans = @ants_; |
|
18 | 201 |
} |
43 | 202 |
} else { # simple CSV ASCII file format: |
203 |
($lat,$lon,$station) = split(',',$rec); |
|
49 | 204 |
_croak("$CNVfile: ASCII file format error (1st rec must be lat,lon[,id])\n") # header: lat,lon[,station] |
40 | 205 |
unless numberp($lat) && numberp($lon) && |
206 |
$lat>=-90 && $lat<=90 && |
|
207 |
$lon>=-360 && $lon<=360; |
|
43 | 208 |
&antsAddParams('station',$station) if defined($station); |
40 | 209 |
&antsAddParams('lat',$lat,'lon',$lon); |
39 | 210 |
$sampint = 1/24; $condHistRes = 2; # assumptions: 24Hz |
211 |
&antsAddParams('ITS',90,'cond.unit','mS/cm'); # ITS-90, mS/cm |
|
212 |
$pressF = 1; $tempF = 2; $condF = 3; # Layout: scan press temp cond |
|
213 |
for ($nscans=0; <F>; $nscans++) { |
|
214 |
chomp; |
|
215 |
@{$ants_[$nscans]} = split(','); # CSV format |
|
49 | 216 |
_croak(sprintf("$CNVfile: unexpected scan #$ants_[$nscans][0] (%d expected)\n",$nscans+1)) |
39 | 217 |
unless ($ants_[$nscans][0] == $nscans+1); |
218 |
$ants_[$nscans][$pressF] = nan unless defined($ants_[$nscans][$pressF]); # missing values |
|
219 |
$ants_[$nscans][$tempF] = nan unless defined($ants_[$nscans][$tempF]); |
|
220 |
$ants_[$nscans][$condF] = nan unless defined($ants_[$nscans][$condF]); |
|
33 | 221 |
} |
222 |
} |
|
223 |
||
49 | 224 |
printf(STDERR "\n\t%d scans (%d seconds)",$nscans,int($nscans*$sampint)) |
225 |
if ($opt_v > 1); |
|
18 | 226 |
printf(STDERR "\n") if ($opt_v); |
227 |
||
228 |
#---------------------------------------------------------------------- |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
229 |
# Redirect STDOUT to %.6Hz & create %_w_CTD.ps,%_sspd.ps if STDOUT is a tty |
29 | 230 |
#---------------------------------------------------------------------- |
231 |
||
232 |
$id = defined($opt_i) ? $opt_i : &antsParam('station'); |
|
49 | 233 |
_croak("$CNVfile: no station information in header => -i required\n") |
29 | 234 |
unless defined($id); |
49 | 235 |
_croak("$CNVfile: non-numeric station information <$id> in header => -i required\n") |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
236 |
unless numberp($id); |
29 | 237 |
|
238 |
if (-t STDOUT) { |
|
239 |
if (numberp($id)) { |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
240 |
$opt_p = '%03d_w_CTD.ps,%03d_sspd.ps' |
29 | 241 |
unless defined($opt_p); |
242 |
$outfile = sprintf('%03d.%dHz',$id,$opt_s); |
|
243 |
} else { |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
244 |
$opt_p = '%s_w_CTD.ps,%s_sspd.ps' |
29 | 245 |
unless defined($opt_p); |
246 |
$outfile = sprintf('%s.%dHz',$id,$opt_s); |
|
247 |
} |
|
248 |
open(STDOUT,">$outfile") || die("$outfile: $!\n"); |
|
249 |
} |
|
250 |
||
49 | 251 |
undef($opt_p) if $opt_q; # suppress all plots on -q |
252 |
||
29 | 253 |
#---------------------------------------------------------------------- |
18 | 254 |
# Edit Data |
255 |
# - pressure outliers & spikes |
|
256 |
# - conductivity outliers & spikes |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
257 |
# - max_bin+1 is returned to ensure full range |
18 | 258 |
#---------------------------------------------------------------------- |
259 |
||
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
260 |
sub validRange($) |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
261 |
{ |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
262 |
my($guess_bin) = @_; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
263 |
my($min_bin,$max_bin); |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
264 |
|
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
265 |
die("assertion failed") unless ($hist[$guess_bin]); |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
266 |
for ($max_bin=$guess_bin; $hist[$max_bin]; $max_bin++) { } |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
267 |
for ($min_bin=$guess_bin; $hist[$min_bin]; $min_bin--) { } |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
268 |
return ($min_bin,$max_bin+1); |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
269 |
} |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
270 |
|
18 | 271 |
unless ($opt_r) { |
272 |
print(STDERR "Editing Data...") if ($opt_v); |
|
273 |
||
274 |
#---------------------------------------- |
|
49 | 275 |
# trim initial scans with |
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
276 |
# - nan pressure |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
277 |
# - nan conductivity |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
278 |
# - conductivity <= 10 mS/cm |
18 | 279 |
#---------------------------------------- |
280 |
my($trimmed) = 0; # trim leading nan pressures |
|
281 |
shift(@ants_),$trimmed++ |
|
42 | 282 |
# ,printf(STDERR "-> p=$ants_[0][$pressF] c=$ants_[0][$condF]\n") |
39 | 283 |
until !@ants_ || |
284 |
numberp($ants_[0][$pressF]) && |
|
285 |
numberp($ants_[0][$condF]) && |
|
286 |
(($P{'cond.unit'} eq 'mS/cm' && $ants_[0][$condF] > 10) || |
|
287 |
($P{'cond.unit'} eq 'S/m' && $ants_[0][$condF] > 1)); |
|
49 | 288 |
_croak("\n$CNVfile: no valid scans (wrong conductivity units?)\n") |
289 |
unless (@ants_); |
|
290 |
$nscans -= $trimmed; |
|
291 |
printf(STDERR "\n\t%d initial in-air scans trimmed",$trimmed) if ($opt_v > 1); |
|
18 | 292 |
my($lvp) = $ants_[0][$pressF]; |
29 | 293 |
my($lvc) = $ants_[0][$condF]; |
18 | 294 |
|
49 | 295 |
#------------------------------------------------------------------------- |
18 | 296 |
# edit pressure outliers outside contiguous range |
49 | 297 |
# - 2dbar resolution increased to 3dbar because of 2005 AAIW profile 004 |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
298 |
# - histogram shifted by 100dbar to allow for negative values |
49 | 299 |
#------------------------------------------------------------------------- |
300 |
my($press_rez) = 2; |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
301 |
my($outliers) = my($modeSamp) = 0; my($modeBin,$min,$max); local(@hist); |
18 | 302 |
for (my($s)=0; $s<$nscans; $s++) { |
29 | 303 |
next unless ($ants_[$s][$pressF]>=-100 && $ants_[$s][$pressF]<=6500); |
49 | 304 |
my($b) = ($ants_[$s][$pressF]+100) / $press_rez; |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
305 |
$hist[$b]++; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
306 |
next unless ($hist[$b] > $modeSamp); |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
307 |
$modeSamp = $hist[$b]; $modeBin = $b; |
18 | 308 |
} |
49 | 309 |
printf(STDERR "\n\tvalid pressure guess: %d dbar (%d samples)",$press_rez*$modeBin-100,$modeSamp) |
33 | 310 |
if ($opt_v > 1); |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
311 |
($min,$max) = validRange($modeBin); |
49 | 312 |
$min = $press_rez*$min-100; $max = $press_rez*$max-100; |
18 | 313 |
for (my($s)=0; $s<$nscans; $s++) { |
314 |
if ($ants_[$s][$pressF] > $max) { $outliers++; $ants_[$s][$pressF] = nan; } |
|
315 |
if ($ants_[$s][$pressF] < $min) { $outliers++; $ants_[$s][$pressF] = nan; } |
|
316 |
} |
|
317 |
&antsAddParams("pressure_outliers",sprintf("%d",$outliers)); |
|
318 |
printf(STDERR "\n\tcontinuous pressure range: %d..%d dbar (%d outliers removed)",$min,$max,$outliers) if ($opt_v > 1); |
|
49 | 319 |
_croak("$CNVfile: pressure editing removed too many 'outliers'\n") |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
320 |
unless ($opt_o || $outliers < 100); |
18 | 321 |
|
322 |
#---------------------------------------------------- |
|
323 |
# edit conductivity outliers outside contiguous range |
|
324 |
#---------------------------------------------------- |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
325 |
$outliers = $modeSamp = 0; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
326 |
undef(@hist); |
18 | 327 |
for (my($s)=0; $s<$nscans; $s++) { |
328 |
next unless ($ants_[$s][$condF] > 0); |
|
329 |
my($b) = $ants_[$s][$condF]*$condHistRes; # 1/10 S/m histogram resolution (1 mS/cm) |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
330 |
$hist[$b]++; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
331 |
next unless ($hist[$b] > $modeSamp); |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
332 |
$modeSamp = $hist[$b]; $modeBin = $b; |
18 | 333 |
} |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
334 |
($min,$max) = validRange($modeBin); |
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
335 |
$min /= $condHistRes; $max /= $condHistRes; |
18 | 336 |
for (my($s)=0; $s<$nscans; $s++) { |
337 |
if ($ants_[$s][$condF] > $max) { $outliers++; $ants_[$s][$condF] = nan; } |
|
338 |
if ($ants_[$s][$condF] < $min) { $outliers++; $ants_[$s][$condF] = nan; } |
|
339 |
} |
|
340 |
&antsAddParams("conductivity_outliers",sprintf("%d",$outliers)); |
|
341 |
printf(STDERR "\n\tcontinuous conductivity range: %.1f..%.1f S/m (%d outliers removed)",$min,$max,$outliers) if ($opt_v > 1); |
|
49 | 342 |
_croak("$CNVfile: conductivity editing removed too many 'outliers'\n") |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
343 |
unless ($opt_o || $outliers/$nscans < 0.4); |
18 | 344 |
|
345 |
#---------------------------------------------------- |
|
346 |
# edit temperature outliers outside contiguous range |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
347 |
# - Stan's NBP0901 profiles require resolution of 1deg |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
348 |
# - otherwise 0.2deg seems to be fine |
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
349 |
# - however, on Feb 19, 2016 it was found that the |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
350 |
# resolution had been left at 1degC without any |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
351 |
# apparent adverse effects |
18 | 352 |
#---------------------------------------------------- |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
353 |
$outliers = $modeSamp = 0; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
354 |
undef(@hist); |
18 | 355 |
for (my($s)=0; $s<$nscans; $s++) { |
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
356 |
next unless ($ants_[$s][$tempF] >= -10); |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
357 |
my($b) = ($ants_[$s][$tempF] + 10) * 0.5; |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
358 |
$hist[$b]++; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
359 |
next unless ($hist[$b] > $modeSamp); |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
360 |
$modeSamp = $hist[$b]; $modeBin = $b; |
18 | 361 |
} |
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
362 |
# printf(STDERR "\n\ttemperature mode: %.1f degC (%d samples)",$modeBin/0.5-10,$modeSamp) |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
363 |
# if ($opt_v > 1); |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
364 |
($min,$max) = validRange($modeBin); |
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
365 |
$min = ($min / 0.5) - 10; |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
366 |
$max = ($max / 0.5) - 10; |
18 | 367 |
for (my($s)=0; $s<$nscans; $s++) { |
368 |
if ($ants_[$s][$tempF] > $max) { $outliers++; $ants_[$s][$tempF] = nan; } |
|
369 |
if ($ants_[$s][$tempF] < $min) { $outliers++; $ants_[$s][$tempF] = nan; } |
|
370 |
} |
|
371 |
&antsAddParams("temperature_outliers",sprintf("%d",$outliers)); |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
372 |
printf(STDERR "\n\tcontinuous temperature range: %.1f..%.1f degC (%d outliers removed)",$min,$max,$outliers) |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
373 |
if ($opt_v > 1); |
49 | 374 |
_croak("$CNVfile: temperature editing removed too many 'outliers'\n") |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
375 |
unless ($opt_o || $outliers/$nscans < 0.4); |
18 | 376 |
|
377 |
#---------------------------------------- |
|
378 |
# edit pressure spikes based on gradients |
|
379 |
#---------------------------------------- |
|
380 |
||
29 | 381 |
for (my($s)=1; $s<$nscans; $s++) { # calculate pressure gradients (across gaps) |
18 | 382 |
if (numberp($ants_[$s][$pressF])) { |
383 |
$dp[$s-1] = $ants_[$s][$pressF] - $lvp; |
|
384 |
$lvp = $ants_[$s][$pressF]; |
|
385 |
} else { |
|
386 |
$dp[$s-1] = nan; |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
my($ns1,$ns2) = (0,0); |
|
391 |
for (my($s)=0; $s<$nscans-2; $s++) { # consecutive large pressure gradients of opposite sign |
|
392 |
if (($dp[$s]*$dp[$s+1] < 0) && # tests return false if either of the dps is not defined |
|
393 |
(abs($dp[$s]) > 10) && |
|
394 |
(abs($dp[$s+1]) > 10)) { |
|
395 |
$ants_[$s+1][$pressF] = nan; |
|
396 |
$dp[$s] = $dp[$s+1] = undef; |
|
397 |
$ns1++; |
|
398 |
} |
|
399 |
} |
|
400 |
for (my($s)=0; $s<$nscans-3; $s++) { # 3 consecutive large pressure gradients of opposite sign |
|
401 |
if (($dp[$s]>2 && $dp[$s+1]<-4 && $dp[$s+2]>2) || |
|
402 |
($dp[$s]<-2 && $dp[$s+1]>4 && $dp[$s+2]<-2)) { |
|
403 |
$ants_[$s+1][$pressF] = $ants_[$s+2][$pressF] = nan; |
|
404 |
$dp[$s] = $dp[$s+1] = $dp[$s+2] = undef; |
|
405 |
$ns2+=2; |
|
406 |
} |
|
407 |
} |
|
408 |
&antsAddParams("pressure_spikes_removed",sprintf("%d+%d",$ns1,$ns2)); |
|
49 | 409 |
printf(STDERR "\n\t%d+%d pressure spikes removed",$ns1,$ns2) if ($opt_v>1 && $ns1+$ns2>0); |
18 | 410 |
|
29 | 411 |
#-------------------------------------------------- |
412 |
# edit conductivity spikes based on large gradients |
|
413 |
# - $lvc = $ants_[0] is guaranteed numeric here |
|
414 |
#-------------------------------------------------- |
|
415 |
||
416 |
my($nv) = my($ns) = my($last_dcond) = my($lvcs) = 0; |
|
417 |
my($run_start) = my($run_dcond) = 0; |
|
418 |
for (my($s)=1; $s<$nscans; $s++) { # calculate conductivity gradients (across gaps) |
|
419 |
next unless numberp($ants_[$s][$condF]); |
|
420 |
||
421 |
my($dcond) = $ants_[$s][$condF] - $lvc; # integrate gradient across runs |
|
422 |
# print(STDERR "ldc/dc/rdc: $last_dcond/$dcond/$run_dcond\n"); |
|
423 |
if ($last_dcond*$dcond >= 0) { # run is continuing (gradient does not change sign) |
|
424 |
$run_dcond += $dcond; |
|
425 |
} else { # run has ended |
|
426 |
# print(STDERR "new run at $lvcs (run_dcond = $last_dcond)\n"); |
|
427 |
$run_start = $lvcs; |
|
428 |
$run_dcond = $dcond; |
|
429 |
} |
|
430 |
||
431 |
if (abs($run_dcond) <= 0.02) { # small integrated gradient => okay |
|
432 |
$lvc = $ants_[$s][$condF]; # update stored previous values |
|
433 |
$lvcs = $s; |
|
434 |
$last_dcond = $dcond; |
|
435 |
next; # process next scan |
|
436 |
} |
|
437 |
||
438 |
# print(STDERR "large gradient ($ants_[$run_start][$condF]-$ants_[$s][$condF], $run_dcond) run $run_start-$s\n"); |
|
439 |
my($i); |
|
440 |
my($max_spike_length) = 24; |
|
441 |
for ($i=$s; $i<=$run_start+$max_spike_length && $i<$nscans-1; $i++) { # large gradient => check whether values return within 10s |
|
442 |
next unless (numberp($ants_[$i][$condF]) && |
|
443 |
numberp($ants_[$i+1][$condF])); |
|
444 |
last if ((abs($ants_[$i][$condF]-$ants_[$run_start][$condF]) <= 0.005) && # 2 vals to avoid large gradients |
|
445 |
(abs($ants_[$i+1][$condF]-$ants_[$run_start][$condF]) <= 0.005)); |
|
446 |
} |
|
447 |
||
448 |
if ($i>$run_start+$max_spike_length || $i==$nscans-1) { # values don't return => leave data alone |
|
449 |
# print(STDERR "values don't return => new run at $s\n"); |
|
450 |
$run_start = $s; $run_dcond = 0; |
|
451 |
$lvc = $ants_[$run_start][$condF]; # start new run |
|
452 |
$lvcs = $run_start; |
|
453 |
$last_dcond = 0; |
|
454 |
next; # process next scan |
|
455 |
} |
|
456 |
||
457 |
$ns++; |
|
458 |
for (my($j)=$run_start+1; $j<$i; $j++) { # values return => remove bad spike |
|
459 |
$ants_[$j][$condF] = nan; |
|
460 |
$nv++; |
|
461 |
} |
|
462 |
# print(STDERR "values return at $i ($ants_[$i][$condF]) => deleted $run_start+1-$i; new run\n"); |
|
463 |
$run_start = $i; $run_dcond = 0; |
|
464 |
$lvc = $ants_[$run_start][$condF]; |
|
465 |
$lvcs = $run_start; |
|
466 |
$last_dcond = 0; |
|
467 |
} |
|
468 |
&antsAddParams("conductivity_spikes_removed",$ns); |
|
469 |
printf(STDERR "\n\t%d conductivity values removed from %d spikes",$nv,$ns) if ($opt_v > 1); |
|
470 |
||
18 | 471 |
printf(STDERR "\n") if ($opt_v); |
472 |
||
473 |
} # if $opt_r |
|
474 |
||
475 |
#---------------------------------------------------------------------- |
|
476 |
# Correcting for pressure bias |
|
477 |
#---------------------------------------------------------------------- |
|
478 |
||
479 |
print(STDERR "Correcting for pressure bias...") if ($opt_v); |
|
480 |
||
481 |
my($minP) = 9e99; |
|
482 |
for (my($s)=0; $s<$nscans; $s++) { |
|
483 |
$minP = $ants_[$s][$pressF] |
|
484 |
if numberp($ants_[$s][$pressF]) && ($ants_[$s][$pressF] < $minP); |
|
485 |
} |
|
49 | 486 |
_croak("$CNVfile: no valid CTD pressure data below 25dbar\n") |
18 | 487 |
unless ($minP < 9e99); |
29 | 488 |
|
489 |
if ($minP < 25) { |
|
490 |
&antsAddParams('pressure_bias',$minP); |
|
491 |
printf(STDERR "\n\tsubtracting %.1f dbar",$minP) if ($opt_v > 1); |
|
492 |
for (my($s)=0; $s<$nscans; $s++) { |
|
493 |
$ants_[$s][$pressF] -= $minP |
|
494 |
if numberp($ants_[$s][$pressF]); |
|
495 |
} |
|
496 |
} else { |
|
497 |
printf(STDERR "\n\tpartial-depth cast below %.1f dbar (no correction applied)",$minP) if ($opt_v > 1); |
|
18 | 498 |
} |
499 |
printf(STDERR "\n") if ($opt_v); |
|
500 |
||
501 |
#---------------------------------------------------------------------- |
|
49 | 502 |
# Removing Initial At-Surface Data |
503 |
#---------------------------------------------------------------------- |
|
504 |
||
505 |
print(STDERR "Removing intial at-surface data...") if ($opt_v); |
|
506 |
||
507 |
my($trimmed) = 0; |
|
508 |
while ($ants_[$trimmed][$pressF] < 0.5) { $trimmed++; } |
|
509 |
for (my($r)=$trimmed; $r<$nscans; $r++) { |
|
510 |
$ants_[$r][$elapsedF] -= $ants_[$trimmed][$elapsedF]; |
|
511 |
} |
|
512 |
splice(@ants_,0,$trimmed); |
|
513 |
$nscans -= $trimmed; |
|
514 |
||
515 |
&antsAddParams('surface_data_trimmed',int($trimmed*$sampint)); |
|
516 |
&antsAddParams('cast_duration',int($nscans*$sampint)); |
|
517 |
||
518 |
printf(STDERR "\n\t%d seconds of data trimmed",int($trimmed*$sampint)) if ($opt_v > 1); |
|
519 |
||
520 |
printf(STDERR "\n") if ($opt_v); |
|
521 |
||
522 |
#---------------------------------------------------------------------- |
|
18 | 523 |
# Binning data |
524 |
#---------------------------------------------------------------------- |
|
525 |
||
526 |
my($sps) = round(1 / $sampint / $opt_s); |
|
527 |
print(STDERR "Creating ${opt_s}Hz time series ($sps samples per bin)...") if ($opt_v); |
|
35
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
528 |
&antsAddParams('sampling_interval',1/$opt_s); |
54b8bb450e5f
Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset
|
529 |
&antsAddParams('sampling_frequency',$opt_s); |
18 | 530 |
|
531 |
my(@press,@temp,@cond); |
|
532 |
my($sp,$np,$st,$nt,$sc,$nc); |
|
533 |
||
534 |
$sp = $st = $sc = $np = $nt = $nc = 0; |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
535 |
$slat = $slon = $nGPS = 0; |
18 | 536 |
for (my($rec)=1,my($s)=0; $s<$nscans; $s++) { |
537 |
if ($s*$sampint > $rec/$opt_s) { |
|
538 |
$rec++; |
|
539 |
push(@press,$np>0?$sp/$np:nan); |
|
540 |
push(@temp, $nt>0?$st/$nt:nan); |
|
541 |
push(@cond, $nc>0?$sc/$nc:nan); |
|
542 |
$sp = $st = $sc = $np = $nt = $nc = 0; |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
543 |
if (defined($latF)) { |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
544 |
push(@lat,$nGPS>0?$slat/$nGPS:nan); |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
545 |
push(@lon,$nGPS>0?$slon/$nGPS:nan); |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
546 |
$slat = $slon = $nGPS = 0; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
547 |
} |
18 | 548 |
} |
549 |
$sp+=$ants_[$s][$pressF],$np++ if numberp($ants_[$s][$pressF]); |
|
550 |
$st+=$ants_[$s][$tempF],$nt++ if numberp($ants_[$s][$tempF]); |
|
551 |
$sc+=$ants_[$s][$condF],$nc++ if numberp($ants_[$s][$condF]); |
|
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
552 |
if (defined($latF) && numberp($ants_[$s][$latF])) { |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
553 |
$slat += $ants_[$s][$latF]; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
554 |
$slon += $ants_[$s][$lonF]; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
555 |
$nGPS++; |
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
556 |
} |
18 | 557 |
} |
558 |
||
559 |
printf(STDERR "\n") if ($opt_v); |
|
560 |
||
561 |
#---------------------------------------------------------------------- |
|
562 |
# Calculating derived quantities |
|
563 |
#---------------------------------------------------------------------- |
|
564 |
||
565 |
print(STDERR "Calculating vertical package velocity & sound speed...") if ($opt_v); |
|
566 |
||
29 | 567 |
my($maxP) = -9e99; my($atBtm); |
568 |
my($min_sspd) = 9e99; my($max_sspd) = -9e99; |
|
18 | 569 |
for (my($r)=0; $r<@press; $r++) { |
29 | 570 |
$maxP=$press[$r],$atBtm=$r if ($press[$r] > $maxP); |
18 | 571 |
$elapsed[$r] = $r/$opt_s; |
572 |
$depth[$r] = &depth($press[$r],$lat); |
|
39 | 573 |
# print(STDERR "depth[$r] = depth($press[$r],$lat) = $depth[$r]\n"); |
29 | 574 |
$salin[$r] = &salin($cond[$r],$temp[$r],$press[$r]); |
575 |
$sspd[$r] = &sVel($salin[$r],$temp[$r],$press[$r]); |
|
576 |
$min_sspd = $sspd[$r] if ($sspd[$r] < $min_sspd); |
|
577 |
$max_sspd = $sspd[$r] if ($sspd[$r] > $max_sspd); |
|
18 | 578 |
} |
579 |
||
580 |
$w[0] = nan; |
|
581 |
for (my($r)=1; $r<@depth-1; $r++) { |
|
582 |
$w[$r] = numbersp($depth[$r-1],$depth[$r+1]) |
|
583 |
? ($depth[$r+1] - $depth[$r-1]) * $opt_s |
|
584 |
: nan; |
|
585 |
} |
|
586 |
push(@w,nan); |
|
587 |
||
588 |
printf(STDERR "\n") if ($opt_v); |
|
589 |
||
590 |
#---------------------------------------------------------------------- |
|
591 |
# Low-pass filter velocity data |
|
592 |
# - interpolate missing vertical velocities first |
|
593 |
#---------------------------------------------------------------------- |
|
594 |
||
49 | 595 |
if ($opt_c > 0) { |
18 | 596 |
print(STDERR "Low-pass filtering vertical package velocity...") if ($opt_v); |
49 | 597 |
&antsAddParams('w_lowpass_cutoff',$opt_c); |
18 | 598 |
|
599 |
my($trimmed) = 0; |
|
600 |
shift(@w),shift(@depth),shift(@elapsed),shift(@sspd),$trimmed++ |
|
601 |
until numberp($w[0]); |
|
602 |
my($interpolated) = 0; |
|
603 |
for ($r=1; $r<@w; $r++) { |
|
604 |
next if numberp($w[$r]); |
|
605 |
my($lv) = $r-1; |
|
606 |
for ($nv=$r+1; $nv<@depth && !numberp($w[$nv]); $nv++) {} |
|
607 |
if ($nv < @depth) { |
|
608 |
while ($r < $nv) { |
|
609 |
$w[$r] = $w[$lv] + ($r-$lv)/($nv-$lv) * ($w[$nv]-$w[$lv]); |
|
610 |
$interpolated++; |
|
611 |
$r++; |
|
612 |
} |
|
613 |
||
614 |
} else { |
|
615 |
$trimmed += @w-$r; |
|
616 |
splice(@w,$r); splice(@depth,$r); |
|
617 |
splice(@elapsed,$r); splice(@sspd,$r); |
|
618 |
} |
|
619 |
} |
|
620 |
&antsAddParams('w_interpolated',$interpolated); |
|
621 |
printf(STDERR "\n\t%d/%d vertical velocities trimmed/interpolated",$trimmed,$interpolated) if ($opt_v > 1); |
|
622 |
||
623 |
||
624 |
#-------------------- |
|
625 |
# Zero Pad Data |
|
626 |
#-------------------- |
|
627 |
||
628 |
for ($pot=1; $pot<@w; $pot<<=1) {} # determine power of two |
|
629 |
||
630 |
for ($r=0; $r<@w; $r++) { # copy data |
|
631 |
$fftbuf[2*$r] = $w[$r]; |
|
632 |
$fftbuf[2*$r+1] = 0; |
|
633 |
} |
|
49 | 634 |
printf(STDERR "\n\tzero-padded %d scans",$pot-$r) if ($opt_v > 1); |
18 | 635 |
while ($r < $pot) { # pad with zeroes |
636 |
$fftbuf[2*$r] = $fftbuf[2*$r+1] = 0; |
|
637 |
$r++; |
|
638 |
} |
|
639 |
||
640 |
#-------------------- |
|
641 |
# Low-Pass Filter |
|
642 |
#-------------------- |
|
643 |
||
644 |
@fco = &FOUR1(-1,@fftbuf); # forward FFT |
|
645 |
$n = @fco/2; |
|
646 |
for (my($ip)=2; $ip<=$n; $ip+=2) { # +ve freq fco |
|
647 |
my($in) = 2*$n-$ip; # -ve freq fco |
|
648 |
my($f) = $ip/2/$n*$opt_s; # frequency |
|
649 |
$fco[$ip] = $fco[$ip+1] = $fco[$in] = $fco[$in+1] = 0 |
|
49 | 650 |
if ($f > 1/$opt_c); # low-pass filter |
18 | 651 |
} |
652 |
@w_lp = &FOUR1(1,@fco); # inverse FFT |
|
653 |
||
654 |
printf(STDERR "\n") if ($opt_v); |
|
655 |
} else { |
|
656 |
@w_lp = @w; |
|
657 |
} |
|
658 |
||
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
659 |
#---------------------------------------- |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
660 |
# Estimate winch speed |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
661 |
#---------------------------------------- |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
662 |
|
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
663 |
print(STDERR "Estimating winch velocity...") if ($opt_v); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
664 |
&antsAddParams('winch_velocity_granularity',$opt_w); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
665 |
|
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
666 |
my($from_r) = 0; my($to_r); # step 1: bin average in time |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
667 |
for (my($from_r)=my($to_r)=0; $from_r<@elapsed; $from_r=$to_r+1) { |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
668 |
my($sumw) = $w_lp[2*$from_r]/@w_lp; my($n) = 1; |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
669 |
for ($to_r=$from_r+1; $to_r<@elapsed && $elapsed[$to_r]-$elapsed[$from_r]<$opt_w; $to_r++) { |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
670 |
$sumw += $w_lp[2*$to_r]/@w_lp; $n++; |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
671 |
} |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
672 |
$winch[$from_r] = $sumw/$n; |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
673 |
} |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
674 |
|
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
675 |
my($pwinch) = $winch[0]; |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
676 |
for (my($to_r),my($from_r)=0; $from_r<@elapsed; ) { # step 2: fill after median filtering |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
677 |
for ($to_r=$from_r+1; $to_r<@elapsed && !defined($winch[$to_r]); $to_r++) {} |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
678 |
my($nwinch) = $to_r<@elapsed ? $winch[$to_r] : $winch[$from_r]; |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
679 |
my($winch) = median($pwinch,$winch[$from_r],$nwinch); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
680 |
$pwinch = $winch[$from_r]; $winch[$from_r] = $winch; |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
681 |
while (++$from_r < $to_r) { $winch[$from_r] = $winch[$from_r-1]; } |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
682 |
} |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
683 |
|
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
684 |
printf(STDERR "\n") if ($opt_v); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
685 |
|
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
686 |
#---------------------------------------- |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
687 |
# Plot Data |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
688 |
#---------------------------------------- |
18 | 689 |
|
29 | 690 |
if (defined($opt_p)) { |
691 |
print(STDERR "Plotting data...\n") if ($opt_v); |
|
692 |
my(@pfmt) = split(',',$opt_p); |
|
49 | 693 |
_croak("$0: cannot decode -p $opt_p\n") |
29 | 694 |
unless (@pfmt == 2); |
695 |
||
696 |
my($xmin) = $elapsed[0]/60; |
|
697 |
my($xmax) = $elapsed[$#elapsed]/60; |
|
698 |
my($ymin) = -3; my($ymax) = 3; |
|
699 |
my($plotsize) = '13c'; |
|
49 | 700 |
|
701 |
_croak(sprintf("%s: invalid region of interest (-R$xmin/$xmax/$ymin/$ymax)\n",sprintf($pfmt[0],$id))) |
|
702 |
unless ($xmax > $xmin && $ymax > $ymin); |
|
31 | 703 |
GMT_begin(sprintf($pfmt[0],$id),"-JX${plotsize}","-R$xmin/$xmax/$ymin/$ymax",'-X6 -Y4 -P'); |
41 | 704 |
GMT_psxy('-W1,coral'); |
29 | 705 |
for ($r=0; $r<@w; $r++) { |
706 |
printf(GMT "%f %f\n",$elapsed[$r]/60,$w_lp[2*$r]/@w_lp); |
|
41 | 707 |
GMT_psxy('-W1,SeaGreen') if ($r == $atBtm); |
29 | 708 |
} |
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
709 |
GMT_psxy('-W1,magenta'); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
710 |
for ($r=0; $r<@w; $r++) { |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
711 |
printf(GMT "%f %f\n",$elapsed[$r]/60,$winch[$r]); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
712 |
} |
48 | 713 |
GMT_psbasemap('-Bg60a30f5:"Elapsed Time [min]":/g1a1f0.1:"Downward Package Velocity [ms@+-1@+]":WeSn'); |
29 | 714 |
GMT_unitcoords(); |
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
715 |
GMT_pstext('-F+f14,Helvetica,coral+jBR'); print(GMT "0.98 0.96 dc\n"); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
716 |
GMT_pstext('-F+f14,Helvetica,SeaGreen+jBR'); print(GMT "0.98 0.93 uc\n"); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
717 |
GMT_pstext('-F+f14,Helvetica,magenta+jBR'); print(GMT "0.98 0.89 winch\n"); |
41 | 718 |
GMT_pstext('-F+f14,Helvetica,blue+jTL -N'); |
719 |
if (defined($outfile)) { printf(GMT "0.01 1.06 $outfile\n",$id); } |
|
720 |
else { printf(GMT "0.01 1.06 %03d\n",$id); } |
|
29 | 721 |
GMT_end(); |
722 |
||
723 |
my($xmin) = round($min_sspd-3,5); |
|
724 |
my($xmax) = round($max_sspd+3,5); |
|
725 |
my($ymin) = 0; my($ymax) = round($depth[$atBtm]+70,100); |
|
726 |
my($plotsize) = '13c'; |
|
49 | 727 |
_croak(sprintf("%s: invalid region of interest (-R$xmin/$xmax/$ymin/$ymax)\n",sprintf($pfmt[1],$id))) |
728 |
unless ($xmax > $xmin && $ymax > $ymin); |
|
31 | 729 |
GMT_begin(sprintf($pfmt[1],$id),"-JX${plotsize}/-${plotsize}","-R$xmin/$xmax/$ymin/$ymax",'-X6 -Y4 -P'); |
29 | 730 |
GMT_psbasemap('-Bg10a10f1:"Speed of Sound [m/s]":/g1000a500f100:"Depth [m]":WeSn'); |
41 | 731 |
GMT_psxy('-W2,coral'); |
29 | 732 |
for ($r=0; $r<@w; $r++) { |
733 |
printf(GMT "%f %f\n",$sspd[$r],$depth[$r]); |
|
41 | 734 |
GMT_psxy('-W1.5,SeaGreen') if ($r == $atBtm); |
29 | 735 |
} |
736 |
GMT_unitcoords(); |
|
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
737 |
GMT_pstext('-F+f14,Helvetica,coral+jTR'); print(GMT "0.98 0.02 dc\n"); |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
738 |
GMT_pstext('-F+f14,Helvetica,SeaGreen+jTR'); print(GMT "0.98 0.06 uc\n"); |
41 | 739 |
GMT_pstext('-F+f14,Helvetica,blue+jTL -N'); |
740 |
if (defined($outfile)) { printf(GMT "0.01 -0.06 $outfile\n",$id); } |
|
741 |
else { printf(GMT "0.01 -0.06 %03d\n",$id); } |
|
29 | 742 |
GMT_end(); |
743 |
} |
|
744 |
||
745 |
#---------------------------------------------------------------------- |
|
746 |
||
18 | 747 |
print(STDERR "Writing output...\n") if ($opt_v); |
748 |
||
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
749 |
@antsNewLayout = ('elapsed','press','temp','cond','depth','salin','sspd','w_CTD.raw','w_CTD','w_winch','lat','lon'); |
18 | 750 |
for ($r=0; $r<@w; $r++) { |
32
6041a20feb39
V1.0 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
31
diff
changeset
|
751 |
&antsOut($elapsed[$r],$press[$r],$temp[$r],$cond[$r],$depth[$r],$salin[$r], |
47
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
752 |
$sspd[$r],$w[$r],$w_lp[2*$r]/@w_lp,$winch[$r], |
2ccb81b7cea5
version found on whoosher after repair
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
43
diff
changeset
|
753 |
$lat[$r],$lon[$r]); |
18 | 754 |
} |
755 |
||
756 |
exit(0); # don't flush @ants_ |