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