author | A.M. Thurnherr <athurnherr@yahoo.com> |
Fri, 12 Jul 2013 12:23:25 +0000 | |
changeset 20 | 7caeef8595b0 |
parent 19 | 12aaf0962ee6 |
child 22 | f6635c0384b7 |
permissions | -rw-r--r-- |
1 | 1 |
#====================================================================== |
2 |
# L A D C P P R O C . L O A D C T D |
|
3 |
# doc: Thu Dec 9 18:39:01 2010 |
|
20 | 4 |
# dlm: Tue Jun 25 14:42:50 2013 |
1 | 5 |
# (c) 2010 A.M. Thurnherr |
20 | 6 |
# uE-Info: 25 0 NIL 0 0 72 2 2 4 NIL ofnI |
1 | 7 |
#====================================================================== |
8 |
||
9 |
# HISTORY: |
|
10 |
# Dec 9, 2010: - exported from LADCPproc |
|
11 |
# - added support for ASCII files |
|
12 |
# Dec 16, 2010: - BUG cnv read did not work any more |
|
2 | 13 |
# Jan 10, 2011: - added code to skip ANTS header |
14 |
# Jan 22, 2011: - adapted to new -g |
|
6 | 15 |
# Jul 15, 2011: - added $CTD{first_elapsed} |
16 |
# Feb 5, 2012: - BUG: ASCII file did not deal with leading spaces correctly |
|
9
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
17 |
# Apr 11, 2012: - BUG: ASCII file did not handle nan latlons correctly |
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
18 |
# Apr 17, 2012: - fiddled |
17 | 19 |
# Oct 19, 2012: - BUG: support for $CTD{first_elapsed} had not been implemented |
20 |
# for binary files |
|
21 |
# - BUG: CTD_badval had not been considered when setting $CTD{first_elapsed} |
|
22 |
# - BUG: CNV format error was not detected correctly any more |
|
19 | 23 |
# Jan 8, 2013: - added CTD_ASCII_header_lines |
20 | 24 |
# Jun 25, 2013: - adapted to :: %PARAM convention |
1 | 25 |
|
26 |
sub readCTD_ASCII($$) |
|
27 |
{ |
|
28 |
my($fn,$dtaR) = @_; |
|
29 |
||
30 |
croak("$fn: unknown pressure field\n") unless defined($CTD_ASCII_press_field); |
|
31 |
croak("$fn: unknown temperature field\n") unless defined($CTD_ASCII_temp_field); |
|
32 |
croak("$fn: unknown salinity field\n") unless defined($CTD_ASCII_salin_field); |
|
2 | 33 |
unless (numberp($dtaR->{lat})) { |
34 |
croak("$fn: unknown latitude field\n") unless defined($CTD_ASCII_lat_field); |
|
35 |
croak("$fn: unknown longitude field\n") unless defined($CTD_ASCII_lon_field); |
|
36 |
} |
|
1 | 37 |
|
38 |
$CTD_ASCII_badval = 9e99 unless defined($CTD_ASCII_badval); |
|
39 |
||
40 |
open(F,$fn) || croak("$fn: $!\n"); |
|
41 |
my($sumLat,$sumLon); my($nPos) = 0; |
|
42 |
my($ds); |
|
19 | 43 |
my($skip) = $CTD_ASCII_header_lines; |
1 | 44 |
while (chomp($ds = <F>)) { |
19 | 45 |
if (defined($CTD_ASCII_header_lines)) { # fixed header |
46 |
next if ($skip-- > 0); |
|
47 |
} else { # comments beginning with # allowed |
|
48 |
next if ($ds =~ /^#/); |
|
49 |
} |
|
50 |
$ds =~ s/^\s+//; # strip leading spaces |
|
1 | 51 |
my(@rec) = split('\s+',$ds); |
52 |
push(@{$dtaR->{press}},($rec[$CTD_ASCII_press_field-1] == $CTD_ASCII_badval) ? nan : $rec[$CTD_ASCII_press_field-1]); |
|
53 |
push(@{$dtaR->{temp}}, ($rec[$CTD_ASCII_temp_field-1] == $CTD_ASCII_badval) ? nan : $rec[$CTD_ASCII_temp_field-1]); |
|
54 |
push(@{$dtaR->{salin}},($rec[$CTD_ASCII_salin_field-1] == $CTD_ASCII_badval) ? nan : $rec[$CTD_ASCII_salin_field-1]); |
|
9
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
55 |
if (defined($CTD_ASCII_lat_field) && |
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
56 |
numberp($rec[$CTD_ASCII_lat_field-1]) && |
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
57 |
$rec[$CTD_ASCII_lat_field-1] != $CTD_ASCII_badval) { |
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
58 |
$nPos++; |
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
59 |
$sumLat += $rec[$CTD_ASCII_lat_field-1]; |
48b2d27aaebf
after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
6
diff
changeset
|
60 |
$sumLon += $rec[$CTD_ASCII_lon_field-1]; |
1 | 61 |
} |
62 |
} |
|
63 |
close(F); |
|
64 |
||
65 |
if ($nPos > 0) { |
|
66 |
$dtaR->{lat} = $sumLat / $nPos; |
|
67 |
$dtaR->{lon} = $sumLon / $nPos; |
|
68 |
} |
|
69 |
||
70 |
$dtaR->{sampint} = 1 / $CTD_ASCII_sampfreq; |
|
71 |
} |
|
72 |
||
73 |
sub readCTD_CNV($$) |
|
74 |
{ |
|
75 |
my($fn,$dtaR) = @_; |
|
6 | 76 |
my($CTD_nrecs,$CTD_nfields,$pressF,$tempF,$salinF,$elapsedF); |
1 | 77 |
my($CTD_badval,$CTD_file_type); |
78 |
||
79 |
open(F,$fn) || croak("$fn: $!\n"); |
|
80 |
while (1) { # parse header |
|
81 |
my($hdr); |
|
82 |
chomp($hdr = <F>); |
|
17 | 83 |
croak(" unexpected EOF (format error)!\n") unless defined($hdr); |
1 | 84 |
$hdr =~ s/\r*$//; |
85 |
last if ($hdr eq '*END*'); |
|
86 |
||
87 |
$CTD_nfields = $',next if ($hdr =~ /nquan = /); # Layout |
|
88 |
$CTD_nrecs = $',next if ($hdr =~ /nvalues = /); |
|
6 | 89 |
$elapsedF = $1,next if ($hdr =~ /name (\d+) = timeS:/); |
1 | 90 |
$pressF = $1,next if ($hdr =~ /name (\d+) = prDM:/); |
91 |
if ($opt_2) { |
|
92 |
$tempF = $1,next if ($hdr =~ /name (\d+) = t190C:/); |
|
93 |
$salinF = $1,next if ($hdr =~ /name (\d+) = sal11:/); |
|
94 |
} else { |
|
95 |
$tempF = $1,next if ($hdr =~ /name (\d+) = t090C:/); |
|
96 |
$salinF = $1,next if ($hdr =~ /name (\d+) = sal00:/); |
|
97 |
} |
|
98 |
||
20 | 99 |
&antsAddParams('LADCPproc::CTD_start_time',$1),next # selected metadata |
1 | 100 |
if ($hdr =~ /start_time = (.*)/); |
101 |
||
20 | 102 |
&antsAddParams('LADCPproc::CTD_station',$1),next |
1 | 103 |
if ($hdr =~ /Station\s*:\s*(.*)/); |
20 | 104 |
&antsAddParams('LADCPproc::ship',$1),next |
1 | 105 |
if ($hdr =~ /Ship\s*:\s*(.*)\s*$/); |
20 | 106 |
&antsAddParams('LADCPproc::cruise',$1),next |
1 | 107 |
if ($hdr =~ /Cruise\s*:\s*(.*)\s*$/); |
20 | 108 |
&antsAddParams('LADCPproc::CTD_time',$1),next |
1 | 109 |
if ($hdr =~ /Time\s*:\s*(.*)/); |
20 | 110 |
&antsAddParams('LADCPproc::CTD_date',$1),next |
1 | 111 |
if ($hdr =~ /Date\s*:\s*(.*)/); |
112 |
||
113 |
if ($hdr =~ /Latitude\s*[=:]\s*/) { |
|
114 |
($deg,$min,$NS) = split(/ /,$'); |
|
115 |
$dtaR->{lat} = $deg + $min/60; |
|
116 |
$dtaR->{lat} *= -1 if ($NS eq 'S'); |
|
117 |
next; |
|
118 |
} |
|
119 |
if ($hdr =~ /Longitude\s*[=:]\s*/) { |
|
120 |
($deg,$min,$EW) = split(/ /,$'); |
|
121 |
$dtaR->{lon} = $deg + $min/60; |
|
122 |
$dtaR->{lon} *= -1 if ($EW eq 'W'); |
|
123 |
next; |
|
124 |
} |
|
125 |
||
126 |
if ($hdr =~ /interval = seconds: /) { |
|
127 |
$dtaR->{sampint} = 1*$'; |
|
128 |
next; |
|
129 |
} |
|
130 |
||
131 |
$CTD_badval = $',next |
|
132 |
if ($hdr =~ /bad_flag = /); |
|
133 |
$CTD_file_type = $',next |
|
134 |
if ($hdr =~ /file_type = /); |
|
135 |
} |
|
136 |
||
137 |
croak("$CTD_file: cannot determine CTD file layout\n") |
|
138 |
unless ($CTD_nfields && $CTD_nrecs); |
|
139 |
croak("$CTD_file: cannot determine missing value\n") |
|
140 |
unless defined($CTD_badval); |
|
141 |
croak("$CTD_file: not a CTD time series file\n") |
|
142 |
unless ($dtaR->{sampint}); |
|
143 |
croak("$CTD_file: no pressure field\n") |
|
144 |
unless defined($pressF); |
|
145 |
croak("$CTD_file: no suitable temperature field\n") |
|
146 |
unless defined($tempF); |
|
147 |
croak("$CTD_file: no suitable salinity field\n") |
|
148 |
unless defined($salinF); |
|
149 |
||
150 |
if ($CTD_file_type eq 'ascii') { |
|
151 |
while (1) { |
|
152 |
last unless (my(@rec) = &antsFileIn(F)); |
|
6 | 153 |
$dtaR->{first_elapsed} = $rec[$elapsedF] |
17 | 154 |
if !defined($dtaR->{first_elapsed}) && defined($elapsedF) && $rec[$elapsedF]!=$CTD_badval; |
1 | 155 |
push(@{$dtaR->{press}},($rec[$pressF] == $CTD_badval) ? nan : $rec[$pressF]); |
156 |
push(@{$dtaR->{temp}}, ($rec[$tempF] == $CTD_badval) ? nan : $rec[$tempF]); |
|
157 |
push(@{$dtaR->{salin}},($rec[$salinF] == $CTD_badval) ? nan : $rec[$salinF]); |
|
158 |
} |
|
159 |
} elsif ($CTD_file_type eq 'binary') { |
|
160 |
||
161 |
my($fbits) = 8 * length(pack('f',0)); |
|
162 |
croak(sprintf("$0: incompatible native CPU float representation (%d instead of 32bits)\n",fbits)) |
|
163 |
unless ($fbits == 32); |
|
164 |
||
165 |
croak("$fn: can't read binary data\n") |
|
166 |
unless (read(F,my($dta),4*$CTD_nfields*$CTD_nrecs) == 4*$CTD_nfields*$CTD_nrecs); |
|
167 |
print(STDERR "$fn: WARNING: extraneous data at EOF\n") unless eof(F); |
|
168 |
||
169 |
$dta = pack('V*',unpack('N*',$dta)) # big-endian CPU |
|
170 |
if (unpack('h*', pack('s', 1)) =~ /01/); # c.f. perlport(1) |
|
171 |
||
172 |
my(@dta) = unpack("f*",$dta); |
|
173 |
||
174 |
for (my($r)=0; $r<$CTD_nrecs; $r++) { |
|
17 | 175 |
$dtaR->{first_elapsed} = $dta[$r*$CTD_nfields+$elapsedF] |
176 |
if !defined($dtaR->{first_elapsed}) && defined($elapsedF) && $dta[$r*$CTD_nfields+$elapsedF]!=$CTD_badval; |
|
1 | 177 |
push(@{$dtaR->{press}},($dta[$r*$CTD_nfields+$pressF] == $CTD_badval) ? nan : $dta[$r*$CTD_nfields+$pressF]); |
178 |
push(@{$dtaR->{temp}}, ($dta[$r*$CTD_nfields+$tempF] == $CTD_badval) ? nan : $dta[$r*$CTD_nfields+$tempF]); |
|
179 |
push(@{$dtaR->{salin}},($dta[$r*$CTD_nfields+$salinF] == $CTD_badval) ? nan : $dta[$r*$CTD_nfields+$salinF]); |
|
180 |
} |
|
181 |
} else { |
|
182 |
croak("$fn: unknown CTD file type $CTD_file_type\n"); |
|
183 |
} |
|
184 |
close(F); |
|
185 |
} |
|
186 |
||
187 |
sub readCTD($$) |
|
188 |
{ |
|
189 |
my($fn,$dtaR) = @_; |
|
190 |
||
191 |
if (defined($CTD_ASCII_sampfreq)) { |
|
192 |
readCTD_ASCII($fn,$dtaR); |
|
193 |
} else { |
|
194 |
readCTD_CNV($fn,$dtaR); |
|
195 |
} |
|
196 |
||
197 |
croak("$0: unknown latitude\n") unless defined($dtaR->{lat}); |
|
198 |
&antsAddParams('lat',$dtaR->{lat}); |
|
199 |
croak("$0: unknown longitude\n") unless defined($dtaR->{lon}); |
|
200 |
&antsAddParams('lon',$dtaR->{lon}); |
|
201 |
||
20 | 202 |
&antsAddParams('LADCPproc::CTD_sampfreq',1/$dtaR->{sampint}); |
1 | 203 |
&antsAddParams('ITS',$P{ITS} = 90); |
204 |
} |
|
205 |
||
206 |
1; |