14
|
1 |
#======================================================================
|
|
2 |
# L I B S B E . P L
|
|
3 |
# doc: Mon Nov 3 12:42:14 2014
|
|
4 |
# dlm: Mon Nov 3 19:35:53 2014
|
|
5 |
# (c) 2014 A.M. Thurnherr
|
|
6 |
# uE-Info: 83 13 NIL 0 0 72 2 2 4 NIL ofnI
|
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# HISTORY:
|
|
10 |
# Nov 3, 2014: - exported from [importCNV]
|
|
11 |
|
|
12 |
#----------------------------------------------------------------------
|
|
13 |
# fname_SBE2std($)
|
|
14 |
# - standardize field names (also adds correct unit %PARAMs)
|
|
15 |
#----------------------------------------------------------------------
|
|
16 |
|
|
17 |
sub fname_SBE2std($)
|
|
18 |
{
|
|
19 |
$_ = $_[0];
|
|
20 |
|
|
21 |
return 'lat' if /^lat/;
|
|
22 |
return 'lon' if /^lon/;
|
|
23 |
return 'press' if /^prDM/;
|
|
24 |
return 'depth' if /^depSM/;
|
|
25 |
return 'O2' if /^sbeox0/;
|
|
26 |
return 'alt_O2' if /^sbeox1/;
|
|
27 |
return 'salin' if /^sal00/;
|
|
28 |
return 'alt_salin' if /^sal11/;
|
|
29 |
return 'elapsed' if /^timeS/;
|
|
30 |
|
|
31 |
if (/^t090/) { # temperatures with different scales
|
|
32 |
croak("$0: inconsistent temperature scales\n")
|
|
33 |
if defined($P{ITS}) && ($P{ITS} != 90);
|
|
34 |
&antsAddParams('ITS',90); $P{ITS} = 90;
|
|
35 |
return 'temp';
|
|
36 |
} elsif (/^t068/) {
|
|
37 |
croak("$0: inconsistent temperature scales\n")
|
|
38 |
if defined($P{ITS}) && ($P{ITS} != 68);
|
|
39 |
&antsAddParams('ITS',68); $P{ITS} = 68;
|
|
40 |
return 'temp';
|
|
41 |
}
|
|
42 |
|
|
43 |
if (/^t190/) {
|
|
44 |
croak("$0: inconsistent temperature scales\n")
|
|
45 |
if defined($P{ITS}) && ($P{ITS} != 90);
|
|
46 |
&antsAddParams('ITS',90); $P{ITS} = 90;
|
|
47 |
return 'alt_temp';
|
|
48 |
} elsif (/^t168/) {
|
|
49 |
croak("$0: inconsistent temperature scales\n")
|
|
50 |
if defined($P{ITS}) && ($P{ITS} != 68);
|
|
51 |
&antsAddParams('ITS',68); $P{ITS} = 68;
|
|
52 |
return 'alt_temp';
|
|
53 |
}
|
|
54 |
|
|
55 |
if (m{^c0S/m}) { # conductivity with different units
|
|
56 |
croak("$0: inconsistent conductivity units\n")
|
|
57 |
if defined($P{cond.unit}) && ($P{cond.unit} ne 'S/m');
|
|
58 |
&antsAddParams('cond.unit','S/m'); $P{cond.unit} = 'S/m';
|
|
59 |
return 'cond';
|
|
60 |
} elsif (m{^c0mS/cm}) {
|
|
61 |
croak("$0: inconsistent conductivity units\n")
|
|
62 |
if defined($P{cond.unit}) && ($P{cond.unit} != 'mS/cm');
|
|
63 |
&antsAddParams('cond.unit','mS/cm'); $P{cond.unit} = 'mS/cm';
|
|
64 |
return 'cond';
|
|
65 |
}
|
|
66 |
|
|
67 |
if (m{^c1S/m}) {
|
|
68 |
croak("$0: inconsistent conductivity units\n")
|
|
69 |
if defined($P{cond.unit}) && ($P{cond.unit} != 'S/m');
|
|
70 |
&antsAddParams('cond.unit','S/m'); $P{cond.unit} = 'S/m';
|
|
71 |
return 'alt_cond';
|
|
72 |
} elsif (m{^c1mS/cm}) {
|
|
73 |
croak("$0: inconsistent conductivity units\n")
|
|
74 |
if defined($P{cond.unit}) && ($P{cond.unit} != 'mS/cm');
|
|
75 |
&antsAddParams('cond.unit','mS/cm'); $P{cond.unit} = 'mS/cm';
|
|
76 |
return 'alt_cond';
|
|
77 |
}
|
|
78 |
|
|
79 |
return $_;
|
|
80 |
}
|
|
81 |
|
|
82 |
# same as above but leaving names in place (only setting %PARAMs)
|
|
83 |
sub fname_SBE($)
|
|
84 |
{
|
|
85 |
$_ = $_[0];
|
|
86 |
|
|
87 |
if (/^t090/) { # temperatures with different scales
|
|
88 |
croak("$0: inconsistent temperature scales\n")
|
|
89 |
if defined($P{ITS}) && ($P{ITS} != 90);
|
|
90 |
&antsAddParams('ITS',90); $P{ITS} = 90;
|
|
91 |
} elsif (/^t068/) {
|
|
92 |
croak("$0: inconsistent temperature scales\n")
|
|
93 |
if defined($P{ITS}) && ($P{ITS} != 68);
|
|
94 |
&antsAddParams('ITS',68); $P{ITS} = 68;
|
|
95 |
}
|
|
96 |
|
|
97 |
if (/^t190/) {
|
|
98 |
croak("$0: inconsistent temperature scales\n")
|
|
99 |
if defined($P{ITS}) && ($P{ITS} != 90);
|
|
100 |
&antsAddParams('ITS',90); $P{ITS} = 90;
|
|
101 |
} elsif (/^t168/) {
|
|
102 |
croak("$0: inconsistent temperature scales\n")
|
|
103 |
if defined($P{ITS}) && ($P{ITS} != 68);
|
|
104 |
&antsAddParams('ITS',68); $P{ITS} = 68;
|
|
105 |
}
|
|
106 |
|
|
107 |
if (m{^c0S/m}) { # conductivity with different units
|
|
108 |
croak("$0: inconsistent conductivity units\n")
|
|
109 |
if defined($P{cond.unit}) && ($P{cond.unit} ne 'S/m');
|
|
110 |
&antsAddParams('cond.unit','S/m'); $P{cond.unit} = 'S/m';
|
|
111 |
} elsif (m{^c0mS/cm}) {
|
|
112 |
croak("$0: inconsistent conductivity units\n")
|
|
113 |
if defined($P{cond.unit}) && ($P{cond.unit} != 'mS/cm');
|
|
114 |
&antsAddParams('cond.unit','mS/cm'); $P{cond.unit} = 'mS/cm';
|
|
115 |
}
|
|
116 |
|
|
117 |
if (m{^c1S/m}) {
|
|
118 |
croak("$0: inconsistent conductivity units\n")
|
|
119 |
if defined($P{cond.unit}) && ($P{cond.unit} != 'S/m');
|
|
120 |
&antsAddParams('cond.unit','S/m'); $P{cond.unit} = 'S/m';
|
|
121 |
} elsif (m{^c1mS/cm}) {
|
|
122 |
croak("$0: inconsistent conductivity units\n")
|
|
123 |
if defined($P{cond.unit}) && ($P{cond.unit} != 'mS/cm');
|
|
124 |
&antsAddParams('cond.unit','mS/cm'); $P{cond.unit} = 'mS/cm';
|
|
125 |
}
|
|
126 |
|
|
127 |
return $_;
|
|
128 |
}
|
|
129 |
|
|
130 |
#----------------------------------------------------------------------
|
|
131 |
# SBE_checkTime($$)
|
|
132 |
# - make sure all times are (roughly) the same
|
|
133 |
#----------------------------------------------------------------------
|
|
134 |
|
|
135 |
{ # static scope
|
|
136 |
my($target_month,$target_day,$target_year,$target_time);
|
|
137 |
|
|
138 |
sub SBE_checkTime($$)
|
|
139 |
{
|
|
140 |
return unless $_[1];
|
|
141 |
my($mo,$dy,$yr,$tm) = split('\s+',$_[0]);
|
|
142 |
|
|
143 |
unless (defined($target_month)) {
|
|
144 |
$target_month = $mo;
|
|
145 |
$target_day = $dy;
|
|
146 |
$target_year = $yr;
|
|
147 |
$target_time = $tm;
|
|
148 |
return;
|
|
149 |
}
|
|
150 |
|
|
151 |
croak("$0: inconsistent dates in header ($target_month $target_day $target_year vs $mo $dy $yr)\n")
|
|
152 |
unless ($target_month eq $mo && $target_day == $dy && $target_year == $yr);
|
|
153 |
croak("$0: inconsistent times in header ($target_time vs $tm)\n")
|
|
154 |
unless (abs(frac_day(split(':',$target_time))-frac_day(split(':',$tm))) < 1/60/24);
|
|
155 |
}
|
|
156 |
|
|
157 |
} # static scope
|
|
158 |
|
|
159 |
#----------------------------------------------------------------------
|
|
160 |
# sub SBE_parseHeader(FP,std-field-names,time-check)
|
|
161 |
# - parse header information
|
|
162 |
#----------------------------------------------------------------------
|
|
163 |
|
|
164 |
sub SBE_parseHeader($$$)
|
|
165 |
{
|
|
166 |
my($FP,$sfn,$tc) = @_;
|
|
167 |
my($hdr,$nfields,$nrecs,$deg,$min,$NS,$EW,$lat,$lon,$sampint,$badval,$ftype);
|
|
168 |
|
|
169 |
while (1) { # parse header
|
|
170 |
chomp($hdr = <$FP>);
|
|
171 |
$hdr =~ s/\r*$//;
|
|
172 |
die("$0: unexpected EOF (format error)\n") unless defined($hdr);
|
|
173 |
last if ($hdr eq '*END*');
|
|
174 |
|
|
175 |
$nfields = $',next if ($hdr =~ /nquan = /); # Layout
|
|
176 |
$nrecs = $',next if ($hdr =~ /nvalues = /);
|
|
177 |
if ($hdr =~ /name (\d+) = ([^:]+):/) {
|
|
178 |
$antsNewLayout[$1] = $sfn ? fname_SBE2std($2) : fname_SBE($2);
|
|
179 |
next;
|
|
180 |
}
|
|
181 |
|
|
182 |
SBE_checkTime($1,$tc),next # sanity time check
|
|
183 |
if ($hdr =~ /NMEA UTC \(Time\) = (.*)/);
|
|
184 |
SBE_checkTime($1,$tc),next
|
|
185 |
if ($hdr =~ /System UpLoad Time = (.*)/);
|
|
186 |
|
|
187 |
&antsAddParams('CNV_File',$1),next # selected metadata
|
|
188 |
if ($hdr =~ /FileName = (.*)$/);
|
|
189 |
SBE_checkTime($1,$tc),&antsAddParams('start_time',$1),next
|
|
190 |
if ($hdr =~ /start_time = (.*)/);
|
|
191 |
|
|
192 |
&antsAddParams('station',$1),next
|
|
193 |
if ($hdr =~ /Station\s*:\s*(.*)/);
|
|
194 |
&antsAddParams('ship',$1),next
|
|
195 |
if ($hdr =~ /Ship\s*:\s*(.*)/);
|
|
196 |
&antsAddParams('cruise',$1),next
|
|
197 |
if ($hdr =~ /Cruise\s*:\s*(.*)/);
|
|
198 |
&antsAddParams('time',$1),next
|
|
199 |
if ($hdr =~ /Time\s*:\s*(.*)/);
|
|
200 |
&antsAddParams('date',$1),next
|
|
201 |
if ($hdr =~ /Date\s*:\s*(.*)/);
|
|
202 |
|
|
203 |
if (($hdr =~ /Latitude\s*[:=]\s*/) && !defined($lat)) {
|
|
204 |
($deg,$min,$NS) = split(/\s+/,$');
|
|
205 |
croak("$0: cannot decode latitude ($')\n")
|
|
206 |
unless ($NS eq 'N' || $NS eq 'S');
|
|
207 |
$lat = $deg + $min/60;
|
|
208 |
$lat *= -1 if ($NS eq 'S');
|
|
209 |
&antsAddParams('lat',$lat);
|
|
210 |
next;
|
|
211 |
}
|
|
212 |
if (($hdr =~ /Longitude\s*[:=]\s*/) && !defined($lon)) {
|
|
213 |
($deg,$min,$EW) = split(/\s+/,$');
|
|
214 |
croak("$0: cannot decode longitude ($')\n")
|
|
215 |
unless ($EW eq 'E' || $EW eq 'W');
|
|
216 |
$lon = $deg + $min/60;
|
|
217 |
$lon *= -1 if ($EW eq 'W');
|
|
218 |
&antsAddParams('lon',$lon);
|
|
219 |
next;
|
|
220 |
}
|
|
221 |
|
|
222 |
if ($hdr =~ /interval = seconds: /) {
|
|
223 |
$sampint = 1*$';
|
|
224 |
&antsAddParams('sampling_frequency',1/$sampint);
|
|
225 |
next;
|
|
226 |
}
|
|
227 |
if ($hdr =~ /interval = decibars: /) {
|
|
228 |
$sampint = 1*$';
|
|
229 |
&antsAddParams('sampling_press_interval',$sampint);
|
|
230 |
next;
|
|
231 |
}
|
|
232 |
|
|
233 |
$badval = $',next
|
|
234 |
if ($hdr =~ /bad_flag = /);
|
|
235 |
$ftype = $',next
|
|
236 |
if ($hdr =~ /file_type = /);
|
|
237 |
}
|
|
238 |
|
|
239 |
croak("$0: cannot determine file layout\n")
|
|
240 |
unless (@antsNewLayout && defined($nfields) && defined($nrecs));
|
|
241 |
croak("$0: cannot determine missing value\n")
|
|
242 |
unless defined($badval);
|
|
243 |
|
|
244 |
@antsLayout = @antsNewLayout;
|
|
245 |
return ($nfields,$nrecs,$sampint,$badval,$ftype,$lat,$lon);
|
|
246 |
}
|
|
247 |
|
|
248 |
#----------------------------------------------------------------------
|
|
249 |
# SBEin($$)
|
|
250 |
# - read SBE CTD data
|
|
251 |
#----------------------------------------------------------------------
|
|
252 |
|
|
253 |
{ my(@dta); my($nextR)=0; # static scope
|
|
254 |
|
|
255 |
sub SBEin($$$$$)
|
|
256 |
{
|
|
257 |
my($FP,$ftype,$nf,$nr,$bad) = @_;
|
|
258 |
my(@add);
|
|
259 |
|
|
260 |
splice(@ants_,0,$antsBufSkip); # shift buffers
|
|
261 |
|
|
262 |
if ($ftype eq 'ascii') {
|
|
263 |
until ($#ants_>=0 && &antsBufFull()) {
|
|
264 |
return undef unless (@add = &antsFileIn($FP));
|
|
265 |
for (my($f)=0; $f<$nf; $f++) {
|
|
266 |
$add[$f] = nan if ($add[$f] == $bad);
|
|
267 |
}
|
|
268 |
push(@ants_,[@add]);
|
|
269 |
}
|
|
270 |
} elsif ($ftype eq 'binary') {
|
|
271 |
unless (@dta) { # read binary data once
|
|
272 |
my($fbits) = 8 * length(pack('f',0));
|
|
273 |
croak(sprintf("$0: incompatible native CPU float representation (%d instead of 32bits)\n",fbits))
|
|
274 |
unless ($fbits == 32);
|
|
275 |
my($dta);
|
|
276 |
croak("$0: can't read binary data\n")
|
|
277 |
unless (read($FP,$dta,4*$nf*$nr) == 4*$nf*$nr);
|
|
278 |
print(STDERR "WARNING: extraneous data at EOF\n")
|
|
279 |
unless eof($FP);
|
|
280 |
$dta = pack('V*',unpack('N*',$dta)) # big-endian CPU
|
|
281 |
if (unpack('h*', pack('s', 1)) =~ /01/); # c.f. perlport(1)
|
|
282 |
@dta = unpack("f*",$dta);
|
|
283 |
for ($r=0; $r<$nr; $r++) {
|
|
284 |
for ($f=0; $f<$nf; $f++) {
|
|
285 |
@add[$f] = $dta[$r*$nf+$f] == $bad ? nan : $dta[$r*$nf+$f];
|
|
286 |
}
|
|
287 |
}
|
|
288 |
}
|
|
289 |
until ($#ants_>=0 && &antsBufFull()) { # copy next out
|
|
290 |
return undef unless ($nextR < $nr);
|
|
291 |
@add = $dta[$nextR*$nf..($nextR+1)*$nr+$nf];
|
|
292 |
push(@ants_,[@add]);
|
|
293 |
$nextR++;
|
|
294 |
}
|
|
295 |
} else {
|
|
296 |
croak("$0: unknown file type $ftype\n");
|
|
297 |
}
|
|
298 |
|
|
299 |
return $#ants_+1; # ok
|
|
300 |
}
|
|
301 |
|
|
302 |
} # static scope
|
|
303 |
|
|
304 |
#----------------------------------------------------------------------
|
|
305 |
|
|
306 |
1; # return true
|