author | A.M. Thurnherr <athurnherr@yahoo.com> |
Fri, 18 Nov 2022 12:54:01 -0500 | |
changeset 54 | 00ed1e8dcfa4 |
parent 50 | 4b59a02e6518 |
permissions | -rw-r--r-- |
0 | 1 |
#====================================================================== |
50
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
2 |
# . . / L I B / L I B C O N V . P L |
0 | 3 |
# doc: Sat Dec 4 13:03:49 1999 |
50
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
4 |
# dlm: Mon Jun 13 15:27:55 2022 |
0 | 5 |
# (c) 1999 A.M. Thurnherr |
50
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
6 |
# uE-Info: 74 60 NIL 0 0 70 2 2 4 NIL ofnI |
0 | 7 |
#====================================================================== |
8 |
||
9 |
# HISTORY: |
|
10 |
# Dec 12, 1999: - created for the Rainbow CM data as libdate |
|
11 |
# Jul 07, 2000: - renamed to libconv and added lat/lon conversions |
|
12 |
# Aug 21, 2000: - added &s2d() |
|
13 |
# Aug 24, 2000: - added &DDMMXh2d() and &DDDMMXh2d() |
|
14 |
# Aug 28, 2000: - moved &DDMMXh2d() and &DDDMMXh2d() to [libNODC.pl] |
|
15 |
# Sep 04, 2000: - added &GMT2d() |
|
16 |
# Sep 20, 2000: - added &fmtdate(), &fmttime() |
|
17 |
# Oct 16, 2000: - added &dmsh2d() |
|
18 |
# Oct 31, 2000: - added &T90(), &T68() |
|
19 |
# Jan 22, 2001: - BUG: &GMT2d() wrongly +ve-ized stuff like -00:05 |
|
20 |
# Feb 28, 2001: - added &O2mlpl2umpkg() |
|
21 |
# Aug 3, 2001: - made &O2mlpl2umpkg() return NaN on -ve input |
|
22 |
# Aug 7, 2001: - added &O2umpkg2mlpl() |
|
23 |
# - replaced temp_scale by ITS |
|
24 |
# Aug 8, 2001: - BUG: allowed for NaN O2 values |
|
25 |
# Aug 19, 2001: - change temp-conversions to allow nop; changed names |
|
26 |
# Sep 1, 2001: - BUG: allow for nan in addition to NaN |
|
27 |
# Dec 30, 2001: - generalized s2d() |
|
28 |
# - BUG: allow NaNs on temp conversions |
|
29 |
# Feb 4, 2002: - allow for -ve O2 values in conversions to handle offsets |
|
30 |
# Aug 14, 2002: - moved ITS checks in ITS_68() & ITS_90() |
|
31 |
# Oct 9, 2002: - added &fmttime1(),&fmtdate1() |
|
32 |
# Jan 23, 2003: - added more time conversion routines |
|
33 |
# Apr 14, 2003: - removed antsReplaceParam() call |
|
34 |
# May 21, 2004: - added ``-'' as a valid date separator |
|
35 |
# Jun 22, 2004: - added HSV2RGB() |
|
36 |
# Jun 27, 2004: - renamed degree-conversion routines |
|
37 |
# - added wraplon() |
|
38 |
# Dec 1, 2005: - cosmetics |
|
39 |
# Dec 9, 2005: - Version 3.2 [HISTORY] |
|
40 |
# Apr 4, 2006: - made epoch optional in &dayNo() |
|
41 |
# Apr 28, 2006: - added &dn2date() |
|
42 |
# Jul 1, 2006: - Version 3.3 [HISTORY] |
|
43 |
# Nov 9, 2006: - added dec_time() |
|
44 |
# - added dn2date_time() |
|
45 |
# - moved year forward in dn2date() |
|
46 |
# Oct 17, 2007: - dn2date -> Date |
|
47 |
# - dn2date_time -> Time |
|
48 |
# - adapted Date/Time to antsFunUsage() with default fields |
|
49 |
# Oct 19, 2007: - removed antsFunUsage() from non-UI funs |
|
50 |
# Nov 17, 2007: - added "." as another legal date separator |
|
51 |
# Jul 10, 2008: - added support for month names in dayNo() |
|
52 |
# Dec 1, 2008: - added dec_time_long() |
|
53 |
# Dec 3, 2008: - renamed many of the date-conversion routines |
|
54 |
# - added frac_day() |
|
55 |
# Jan 8, 2009: - BUG: &Date() returned wrong date for any time after |
|
56 |
# midnite on last day of a month |
|
57 |
# Oct 27, 2010: - added &day_secs() |
|
58 |
# Jan 3, 2010: - extended frac_day() to allow a single string time-spec |
|
59 |
# Jul 19, 2011: - made epoch aptional in mmddyy2dec_time() |
|
60 |
# Aug 2, 2011: - enhanced yymmdd2dec_time() |
|
61 |
# Apr 17, 2012: - added space as another date separator in ddmmyy2dec_time |
|
1 | 62 |
# May 22, 2012: - BUG: illegal time spec error was also produced on missing seconds |
63 |
# - BUG: mmddyy2dec_time() did not allow for optional epoch argument |
|
13 | 64 |
# Aug 7, 2014: - finally cleaned up date conversions |
33
f891ea33a728
after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
30
diff
changeset
|
65 |
# Jan 27, 2017: - BUG: dayNo() numeric month could have leading/trailing whitespace |
f891ea33a728
after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
30
diff
changeset
|
66 |
# Jul 6, 2017: - BUG: date conversion routines did not parse 1/5/12 correctly |
35 | 67 |
# Dec 18, 2017: - removed ambiguous-date warning |
36 | 68 |
# May 22, 2018: - added NMEA2dec_time() |
38 | 69 |
# Jan 17, 2019: - added ISO_Datetime() |
47 | 70 |
# Feb 15, 2019: - added deg2lat, deg2lon |
71 |
# Aug 27, 2021: - improved Date to take dn from %PARAMs |
|
72 |
# Nov 15, 2021: - modified O2 coversion routines to return nan on missing press, temp, salin |
|
73 |
# Apr 6, 2022: - made =Date work with %dn params as well |
|
50
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
74 |
# Jun 6, 2022: - moved deg2lat, deg2lon to [antsutils.pl] |
47 | 75 |
# HISTORY END |
0 | 76 |
|
77 |
require "$ANTS/libEOS83.pl"; # &sigma() |
|
78 |
require "$ANTS/libPOSIX.pl"; # &floor() |
|
79 |
require "$ANTS/libstats.pl"; # &min(),&max() |
|
80 |
||
81 |
#---------------------------------------------------------------------- |
|
82 |
# Date Conversion |
|
83 |
#---------------------------------------------------------------------- |
|
84 |
||
85 |
sub leapYearP(@) # leap year? |
|
86 |
{ |
|
87 |
my($y) = @_; |
|
88 |
||
89 |
$y += ($y < 50) ? 2000 : 1900 if ($y < 100); # Y2K |
|
90 |
||
91 |
return 0 if ($y%4 != 0); |
|
92 |
return 1 if ($y%100 != 0); |
|
93 |
return 0 if ($y%400 > 0); |
|
94 |
return 1; |
|
95 |
} |
|
96 |
||
97 |
sub monthLength(@) # #days in given month/year |
|
98 |
{ |
|
99 |
my($y,$m) = @_; |
|
100 |
||
101 |
return 31 if ($m==1 || $m==3 || $m==5 || $m==7 || |
|
102 |
$m==8 || $m==10 || $m==12); |
|
103 |
return 30 if ($m==4 || $m==6 || $m==9 || $m==11); |
|
104 |
return 28 + &leapYearP($y) if ($m == 2); |
|
105 |
croak("$0: &monthLength(): Illegal month\n"); |
|
106 |
} |
|
107 |
||
108 |
sub dayNo(@) # day number, starting at 1 |
|
109 |
{ |
|
13 | 110 |
my($y,$m,$d,$epoch) = |
111 |
&antsFunUsage(-3,"c..","year, month, day[, epoch]",@_); |
|
112 |
$epoch = $y unless defined($epoch); |
|
0 | 113 |
|
30 | 114 |
$m =~ s/\s//g; |
115 |
||
0 | 116 |
unless (cardinalp($m)) { |
117 |
$m = lc($m); |
|
118 |
if ($m =~ /^jan/) { $m = 1; } |
|
119 |
elsif ($m =~ /^feb/) { $m = 2; } |
|
120 |
elsif ($m =~ /^mar/) { $m = 3; } |
|
121 |
elsif ($m =~ /^apr/) { $m = 4; } |
|
122 |
elsif ($m =~ /^may/) { $m = 5; } |
|
123 |
elsif ($m =~ /^jun/) { $m = 6; } |
|
124 |
elsif ($m =~ /^jul/) { $m = 7; } |
|
125 |
elsif ($m =~ /^aug/) { $m = 8; } |
|
126 |
elsif ($m =~ /^sep/) { $m = 9; } |
|
127 |
elsif ($m =~ /^oct/) { $m = 10; } |
|
128 |
elsif ($m =~ /^nov/) { $m = 11; } |
|
129 |
elsif ($m =~ /^dec/) { $m = 12; } |
|
130 |
else { croak("$0: unknown month $m\n"); } |
|
131 |
} |
|
132 |
||
133 |
my($dn) = 0; |
|
134 |
||
135 |
$epoch += ($epoch < 50) ? 2000 : 1900 # Y2K |
|
136 |
if ($epoch < 100); |
|
137 |
$y += ($y < 50) ? 2000 : 1900 |
|
138 |
if ($y < 100); |
|
139 |
||
140 |
croak("$0: &dayNo(): Error: epoch > year\n") # only positive times |
|
141 |
if ($y < $epoch); |
|
142 |
while ($epoch < $y) { # entire years |
|
143 |
$dn += 365 + &leapYearP($epoch); |
|
144 |
$epoch++; |
|
145 |
} |
|
146 |
||
147 |
croak("$0: &dayNo(): Error: day > #days of month\n") # current month |
|
148 |
if ($d > &monthLength($y,$m)); |
|
149 |
$dn += $d; |
|
150 |
$m--; |
|
151 |
||
152 |
while ($m > 0) { # current year |
|
153 |
$dn += &monthLength($y,$m); |
|
154 |
$m--; |
|
155 |
} |
|
156 |
||
157 |
return $dn |
|
158 |
} |
|
159 |
||
160 |
sub frac_day(@) # fractional day |
|
161 |
{ |
|
162 |
my($h,$m,$s); |
|
163 |
if (@_ == 1) { |
|
164 |
($h,$m,$s) = split(':',$_[0]); |
|
165 |
} else { |
|
166 |
($h,$m,$s) = &antsFunUsage(3,'ccf',"<h:m:s>|<hour> <min> <sec>",@_); |
|
167 |
} |
|
168 |
||
169 |
croak("$0: &frac_day_long(): illegal time spec $h:$m:$s\n") |
|
1 | 170 |
unless ((defined($h) && $h>=0 && $h<24) && |
171 |
(defined($m) && $m>=0 && $m<60) && |
|
172 |
(!defined($s) || ($s>=0 && $s<60))); |
|
0 | 173 |
return $h/24 + $m/24/60 + $s/24/3600; |
174 |
} |
|
175 |
||
176 |
sub day_secs(@) # seconds since daystart |
|
177 |
{ |
|
178 |
my($h,$m,$s) = &antsFunUsage(3,'ccf',"<hour> <min> <sec>",@_); |
|
179 |
||
180 |
croak("$0: &frac_day_long(): illegal time spec $h:$m:$s\n") |
|
1 | 181 |
unless ((defined($h) && $h>=0 && $h<24) && |
182 |
(defined($m) && $m>=0 && $m<60) && |
|
183 |
(!defined($s) || ($s>=0 && $s<60))); |
|
0 | 184 |
return $h*3600 + $m*60 + $s; |
185 |
} |
|
186 |
||
187 |
sub dec_time(@) # decimal time |
|
188 |
{ |
|
189 |
my($epoch,$yy,$mm,$dd,$h,$m,$s) = |
|
190 |
&antsFunUsage(7,'ccccccf',"<epoch> <year> <month> <day> <hour> <min> <sec>",@_); |
|
13 | 191 |
return &dayNo($yy,$mm,$dd,$epoch) + &frac_day($h,$m,$s); |
0 | 192 |
} |
193 |
||
13 | 194 |
#---------------------------------------------------------------------- |
195 |
# String to Decimal Time Conversion |
|
196 |
#---------------------------------------------------------------------- |
|
197 |
||
36 | 198 |
sub NMEA2dec_time(@) |
199 |
{ |
|
200 |
my($NMEA_string,$epoch) = &antsFunUsage(-1,'.','<NMEA string>[, epoch]',@_); |
|
201 |
||
202 |
# Mar 17 2018 01:23:09 |
|
203 |
my($month,$day,$year,$time) = split(/\s+/,$NMEA_string); |
|
204 |
$epoch = $year unless defined($epoch); |
|
205 |
return dayNo($year,$month,$day,$epoch) + frac_day(split(':',$time)); |
|
206 |
} |
|
207 |
||
13 | 208 |
{ my($date_fmt); |
209 |
||
210 |
sub str2dec_time(@) # heuristic |
|
211 |
{ |
|
212 |
my($ds,$ts,$epoch) = |
|
213 |
&antsFunUsage(-2,"..","date, hh:mm[:ss][, epoch]",@_); |
|
30 | 214 |
$ds =~ s/\s//g; |
215 |
$ts =~ s/\s//g; |
|
13 | 216 |
|
217 |
croak("$0 str2dec_time: date required\n") unless ($ds ne ''); |
|
218 |
||
219 |
unless (defined($date_fmt)) { |
|
220 |
my($X,$Y,$Z) = split('[-/\.]',$ds); |
|
221 |
if ($X > 31) { # YY/MM//DD |
|
222 |
$date_fmt = 1; |
|
223 |
} elsif ($X > 12) { # DD/MM/YY |
|
224 |
$date_fmt = 2; |
|
225 |
} elsif ($Y > 12) { # MM/DD/YY |
|
226 |
$date_fmt = 3; |
|
227 |
} else { |
|
35 | 228 |
# &antsInfo("str2dec_time: ambiguous date <$ds>; MM/DD/YY assumed"); |
13 | 229 |
$date_fmt = 3; |
230 |
} |
|
231 |
} |
|
232 |
||
233 |
if ($date_fmt == 1) { return yymmdd2dec_time($ds,$ts,$epoch); } |
|
234 |
elsif ($date_fmt == 2) { return ddmmyy2dec_time($ds,$ts,$epoch); } |
|
235 |
else { return mmddyy2dec_time($ds,$ts,$epoch); } |
|
236 |
} |
|
237 |
||
238 |
} |
|
239 |
||
240 |
sub mmddyy2dec_time(@) |
|
0 | 241 |
{ |
242 |
my($ds,$ts,$epoch) = |
|
13 | 243 |
&antsFunUsage(-2,"..","MM/DD/[YY]YY, hh:mm[:ss][, epoch]",@_); |
0 | 244 |
|
245 |
my($time) = 0; |
|
13 | 246 |
if ($ds ne '') { |
247 |
my($yy,$mm,$dd); |
|
33
f891ea33a728
after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
30
diff
changeset
|
248 |
if (length($ds) == 6 && $ds =~ m{^\d+$}) { |
13 | 249 |
$mm = substr($ds,0,2); |
250 |
$dd = substr($ds,2,2); |
|
251 |
$yy = substr($ds,4,2); |
|
252 |
} else { |
|
253 |
($mm,$dd,$yy) = split('[-/\.]',$ds); |
|
0 | 254 |
} |
13 | 255 |
$time = dayNo($yy,$mm,$dd,$epoch); |
256 |
} |
|
257 |
if ($ts ne '') { |
|
258 |
my($h,$m,$s) = split(':',$ts); |
|
259 |
$s = 0 unless defined($s); |
|
260 |
return $time + &frac_day($h,$m,$s); |
|
261 |
} |
|
262 |
return $time; |
|
263 |
} |
|
264 |
||
265 |
sub ddmmyy2dec_time(@) |
|
266 |
{ |
|
267 |
my($ds,$ts,$epoch) = |
|
268 |
&antsFunUsage(-2,"..","DD/MM/[YY]YY, hh:mm[:ss][, epoch]",@_); |
|
269 |
||
270 |
my($time) = 0; |
|
271 |
if ($ds ne '') { |
|
272 |
my($yy,$mm,$dd); |
|
33
f891ea33a728
after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
30
diff
changeset
|
273 |
if (length($ds) == 6 && $ds =~ m{^\d+$}) { |
13 | 274 |
$dd = substr($ds,0,2); |
275 |
$mm = substr($ds,2,2); |
|
276 |
$yy = substr($ds,4,2); |
|
277 |
} else { |
|
278 |
($dd,$mm,$yy) = split('[-/\.]',$ds); |
|
279 |
} |
|
280 |
$time = dayNo($yy,$mm,$dd,$epoch); |
|
0 | 281 |
} |
282 |
||
13 | 283 |
if ($ts ne '') { |
284 |
my($h,$m,$s) = split(':',$ts); |
|
285 |
$s = 0 unless defined($s); |
|
286 |
return $time + &frac_day($h,$m,$s); |
|
287 |
} |
|
0 | 288 |
|
289 |
return $time; |
|
290 |
} |
|
291 |
||
13 | 292 |
sub yymmdd2dec_time(@) |
0 | 293 |
{ |
13 | 294 |
my($ds,$ts,$epoch) = |
295 |
&antsFunUsage(-2,"..","[YY]YY/MM/DD, hh:mm[:ss][, epoch]",@_); |
|
0 | 296 |
|
297 |
my($time) = 0; |
|
13 | 298 |
if ($ds ne '') { |
0 | 299 |
my($yy,$mm,$dd); |
33
f891ea33a728
after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
30
diff
changeset
|
300 |
if (length($ds) == 6 && $ds =~ m{^\d+$}) { |
0 | 301 |
$yy = substr($ds,0,2); |
302 |
$mm = substr($ds,2,2); |
|
303 |
$dd = substr($ds,4,2); |
|
304 |
} else { |
|
305 |
($yy,$mm,$dd) = split('[-/\.]',$ds); |
|
306 |
} |
|
13 | 307 |
$time = dayNo($yy,$mm,$dd,$epoch); |
0 | 308 |
} |
309 |
||
310 |
if ($ts ne '') { |
|
311 |
my($h,$m,$s) = split(':',$ts); |
|
13 | 312 |
$s = 0 unless defined($s); |
313 |
return $time + &frac_day($h,$m,$s); |
|
0 | 314 |
} |
315 |
||
316 |
return $time; |
|
317 |
} |
|
318 |
||
13 | 319 |
#---------------------------------------------------------------------- |
38 | 320 |
# Decimal Time to String Conversion |
13 | 321 |
#---------------------------------------------------------------------- |
322 |
||
323 |
{ my(@fc); |
|
324 |
||
325 |
sub Date(@) # day number -> date |
|
326 |
{ |
|
327 |
||
328 |
my($dnf); # find std dn field & epoch |
|
329 |
if (@_ == 0) { |
|
330 |
for (my($i)=0; $i<@antsLayout; $i++) { |
|
331 |
next unless ($antsLayout[$i] =~ /^dn(\d\d)$/); |
|
332 |
$dnf = $antsLayout[$i]; push(@_,$1); |
|
333 |
last; |
|
334 |
} |
|
335 |
} |
|
336 |
||
47 | 337 |
unless (defined($dnf)) { |
338 |
foreach my $p (keys(%P)) { |
|
339 |
next unless ($p =~ /^dn(\d\d)$/); |
|
340 |
$year = ($1 > 70) ? 1900 + $1 : 2000 + $1; |
|
341 |
$day = int($P{$p}); |
|
342 |
while ($day > 365+&leapYearP($year)) { # adjust year |
|
343 |
$day -= 365 + &leapYearP($year); |
|
344 |
$year++; |
|
345 |
} |
|
346 |
my($month) = 1; |
|
347 |
while ($day > &monthLength($year,$month)) { |
|
348 |
$day -= &monthLength($year,$month); |
|
349 |
$month++; |
|
350 |
} |
|
351 |
return sprintf('%04d/%02d/%02d',$year,$month,$day); |
|
352 |
} |
|
353 |
} |
|
354 |
||
13 | 355 |
my($year,$day) = &antsFunUsage(2,"cf","epoch, dayNo",\@fc,undef,$dnf,@_); |
356 |
||
357 |
$year += ($year < 50) ? 2000 : 1900 # Y2K |
|
358 |
if ($year < 100); |
|
359 |
||
360 |
$day = int($day); # prevent runover on last day of month |
|
361 |
while ($day > 365+&leapYearP($year)) { # adjust year |
|
362 |
$day -= 365 + &leapYearP($year); |
|
363 |
$year++; |
|
364 |
} |
|
365 |
||
366 |
my($month) = 1; |
|
367 |
while ($day > &monthLength($year,$month)) { |
|
368 |
$day -= &monthLength($year,$month); |
|
369 |
$month++; |
|
370 |
} |
|
371 |
||
372 |
return sprintf('%04d/%02d/%02d',$year,$month,$day); |
|
373 |
} |
|
374 |
} |
|
375 |
||
376 |
{ my(@fc); |
|
377 |
||
378 |
sub Time(@) # day number -> date/time |
|
379 |
{ |
|
380 |
my($dnf); # find standard dn field |
|
381 |
for (my($i)=0; $i<@antsLayout; $i++) { |
|
382 |
next unless ($antsLayout[$i] =~ /^dn\d\d$/); |
|
383 |
$dnf = $antsLayout[$i]; |
|
384 |
last; |
|
385 |
} |
|
386 |
||
47 | 387 |
unless (defined($dnf)) { |
388 |
foreach my $p (keys(%P)) { |
|
389 |
next unless ($p =~ /^dn(\d\d)$/); |
|
390 |
my($fday) = $P{$p}; |
|
391 |
my($day) = int($fday); |
|
392 |
$fday -= $day; |
|
393 |
||
394 |
my($hour) = int(24*$fday); |
|
395 |
$fday -= $hour/24; |
|
396 |
my($min) = int(24*60*$fday); |
|
397 |
$fday -= $min/24/60; |
|
398 |
my($sec) = round(24*3600*$fday); |
|
399 |
$min++,$sec=0 if ($sec == 60); |
|
400 |
$hour++,$min=0 if ($min == 60); |
|
401 |
$day++,$hour=0 if ($hour == 24); |
|
402 |
||
403 |
return sprintf('%02d:%02d:%02d',$hour,$min,$sec); |
|
404 |
} |
|
405 |
} |
|
406 |
||
13 | 407 |
my($fday) = &antsFunUsage(1,"f","dayNo",\@fc,$dnf,@_); |
408 |
my($day) = int($fday); |
|
409 |
$fday -= $day; |
|
410 |
||
411 |
my($hour) = int(24*$fday); |
|
412 |
$fday -= $hour/24; |
|
413 |
my($min) = int(24*60*$fday); |
|
414 |
$fday -= $min/24/60; |
|
415 |
my($sec) = round(24*3600*$fday); |
|
416 |
$min++,$sec=0 if ($sec == 60); |
|
417 |
$hour++,$min=0 if ($min == 60); |
|
418 |
$day++,$hour=0 if ($hour == 24); |
|
419 |
||
420 |
return sprintf('%02d:%02d:%02d',$hour,$min,$sec); |
|
421 |
} |
|
422 |
} |
|
423 |
||
38 | 424 |
{ my(@fc); |
425 |
||
426 |
sub ISO_Datetime(@) # day number -> ISO 8601 |
|
427 |
{ |
|
428 |
||
429 |
my($dnf); # find std dn field & epoch |
|
430 |
if (@_ == 0) { |
|
431 |
for (my($i)=0; $i<@antsLayout; $i++) { |
|
432 |
next unless ($antsLayout[$i] =~ /^dn(\d\d)$/); |
|
433 |
$dnf = $antsLayout[$i]; push(@_,$1); |
|
434 |
last; |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
my($year,$fday) = &antsFunUsage(2,"cf","epoch, dayNo",\@fc,undef,$dnf,@_); |
|
439 |
||
440 |
$year += ($year < 50) ? 2000 : 1900 # Y2K |
|
441 |
if ($year < 100); |
|
442 |
||
443 |
$day = int($fday); # prevent runover on last day of month |
|
444 |
$fday -= $day; |
|
445 |
||
446 |
while ($day > 365+&leapYearP($year)) { # adjust year |
|
447 |
$day -= 365 + &leapYearP($year); |
|
448 |
$year++; |
|
449 |
} |
|
450 |
||
451 |
my($month) = 1; |
|
452 |
while ($day > &monthLength($year,$month)) { |
|
453 |
$day -= &monthLength($year,$month); |
|
454 |
$month++; |
|
455 |
} |
|
456 |
||
457 |
my($hour) = int(24*$fday); |
|
458 |
$fday -= $hour/24; |
|
459 |
my($min) = int(24*60*$fday); |
|
460 |
$fday -= $min/24/60; |
|
461 |
my($sec) = round(24*3600*$fday); |
|
462 |
$min++,$sec=0 if ($sec == 60); |
|
463 |
$hour++,$min=0 if ($min == 60); |
|
464 |
$day++,$hour=0 if ($hour == 24); |
|
465 |
||
466 |
return sprintf('%04d-%02d-%02dT%02d:%02d:%02d',$year,$month,$day,$hour,$min,$sec); |
|
467 |
} |
|
468 |
} |
|
469 |
||
13 | 470 |
#---------------------------------------------------------------------- |
471 |
# Other Misc Date Conversions |
|
472 |
#---------------------------------------------------------------------- |
|
473 |
||
0 | 474 |
sub date2str(@) |
475 |
{ |
|
13 | 476 |
my($MM,$DD,$YYYY) = &antsFunUsage(3,'ccc','month, day, year',@_); |
0 | 477 |
$YYYY += 2000 if ($YYYY < 50); |
478 |
$YYYY += 1900 if ($YYYY < 100); |
|
13 | 479 |
return sprintf('%02d',$MM) . '/' . |
480 |
sprintf('%02d',$DD) . '/' . $YYYY; |
|
0 | 481 |
} |
482 |
||
483 |
sub card_date2str(@) |
|
484 |
{ |
|
13 | 485 |
my($DDMMYY) = &antsFunUsage(1,'c','ddmmyy',@_); |
486 |
$DDMMYY = sprintf('%06d',$DDMMYY); |
|
0 | 487 |
return &fmtdate(substr($DDMMYY,2,2),substr($DDMMYY,0,2),substr($DDMMYY,4,2)); |
488 |
} |
|
489 |
||
490 |
sub time2str(@) |
|
491 |
{ |
|
13 | 492 |
my($HH,$MM) = &antsFunUsage(2,'cc','hr, min',@_); |
493 |
return sprintf('%02d',$HH) . ':' . sprintf('%02d',$MM); |
|
0 | 494 |
} |
495 |
||
496 |
sub card_time2str(@) |
|
497 |
{ |
|
13 | 498 |
my($HHMM) = &antsFunUsage(1,'c','hrmin',@_); |
0 | 499 |
return &fmttime(int($HHMM/100),$HHMM%100); |
500 |
} |
|
501 |
||
502 |
#---------------------------------------------------------------------- |
|
503 |
# Lat/Lon Conversion |
|
504 |
#---------------------------------------------------------------------- |
|
505 |
||
506 |
sub wraplon(@) # get sign of longitudes right |
|
507 |
{ |
|
508 |
my($deg) = &antsFunUsage(1,'f','deg',@_); |
|
509 |
return ($deg > 180) ? $deg - 360 : $deg; |
|
510 |
} |
|
511 |
||
512 |
sub dmh2deg(@) # dd mm.m NSEW -> dd.d |
|
513 |
{ |
|
514 |
my($deg,$min,$hemisph) = |
|
13 | 515 |
&antsFunUsage(3,'ff1','deg, min, hemisphere',@_); |
0 | 516 |
croak("$0 dmh2d(): <deg> may not be -ve\n") if ($deg < 0); |
517 |
croak("$0 dmh2d(): <min> may not be -ve\n") if ($min < 0); |
|
518 |
$deg += $min/60; |
|
519 |
$_ = $hemisph; |
|
520 |
SWITCH: { |
|
521 |
$deg = -$deg, last SWITCH if (/[sSwW]/); |
|
522 |
last SWITCH if (/[nNeE]/); |
|
523 |
croak("$0 dmh2d(): $hemisph is an invalid hemisphere id\n"); |
|
524 |
} |
|
525 |
return $deg; |
|
526 |
} |
|
527 |
||
528 |
sub dmsh2deg(@) # dd mm ss NSEW -> dd.d |
|
529 |
{ |
|
530 |
my($deg,$min,$sec,$hemisph) = |
|
13 | 531 |
&antsFunUsage(4,'fff1','deg, min, sec, hemisphere',@_); |
0 | 532 |
croak("$0 dmsh2d(): <deg> may not be -ve\n") if ($deg < 0); |
533 |
croak("$0 dmsh2d(): <min> may not be -ve\n") if ($min < 0); |
|
534 |
croak("$0 dmsh2d(): <sec> may not be -ve\n") if ($sec < 0); |
|
535 |
$deg += $min/60 + $sec/3600; |
|
536 |
$_ = $hemisph; |
|
537 |
SWITCH: { |
|
538 |
$deg = -$deg, last SWITCH if (/[sSwW]/); |
|
539 |
last SWITCH if (/[nNeE]/); |
|
540 |
croak("$0 dmh2d(): $hemisph is an invalid hemisphere id\n"); |
|
541 |
} |
|
542 |
return $deg; |
|
543 |
} |
|
544 |
||
545 |
sub str2deg(@) # string containing dd [mm.m] [NSEW] -> dd.d |
|
546 |
{ |
|
13 | 547 |
my($s) = &antsFunUsage(1,'.',"'deg[ :][min][ ]hemisphere'",@_); |
0 | 548 |
my($deg,$a,$b) = ($s =~ m{^([-\d]+)[\s:]([\d\.]+)\s*([NSEW])$}); |
549 |
# print(STDERR "--> $deg, $a, $b\n"); |
|
550 |
return ($b eq "") ? &dmh2d($deg,0,$a) : &dmh2d($deg,$a,$b); |
|
551 |
} |
|
552 |
||
38 | 553 |
sub GMT2deg(@) # GMT degree format to decimal |
0 | 554 |
{ |
555 |
my($GMT) = &antsFunUsage(1,".","GMT-degs ",@_); |
|
556 |
return (substr($1,0,1) eq "-") ? $1-$2/60.0 : $1+$2/60.0 |
|
557 |
if ($GMT =~ /\s*([^:]+):([^:]+)/); |
|
558 |
return $GMT; |
|
559 |
} |
|
560 |
||
50
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
561 |
# 06/13/2022: moved code to [antsutils.pl] |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
562 |
# |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
563 |
#sub deg2lat(@) # decimal latitude to degrees:min.xx NS |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
564 |
#{ |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
565 |
# my($deg) = &antsFunUsage(1,'f','decimal latitude',@_); |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
566 |
# $deg -= 360 if ($deg > 180); |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
567 |
# return sprintf("%02d:%06.3f %s",abs(int($deg)), |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
568 |
# (abs($deg)-abs(int($deg)))*60, |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
569 |
# $deg>=0 ? "N" : "S"); |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
570 |
#} |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
571 |
# |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
572 |
#sub deg2lon(@) # decimal longitude to degrees:min.xx EW |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
573 |
#{ |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
574 |
# my($deg) = &antsFunUsage(1,'f','decimal longitude',@_); |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
575 |
# $deg -= 360 if ($deg > 180); |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
576 |
# return sprintf("%03d:%06.3f %s",abs(int($deg)), |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
577 |
# (abs($deg)-abs(int($deg)))*60, |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
578 |
# $deg>=0 ? "E" : "W"); |
4b59a02e6518
updated from F104 on P2 cruise (important changes to antsnc.pl)
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
47
diff
changeset
|
579 |
#} |
38 | 580 |
|
0 | 581 |
#---------------------------------------------------------------------- |
582 |
# Temp-Scale Conversion |
|
583 |
#---------------------------------------------------------------------- |
|
584 |
||
585 |
{ my($ITS); |
|
586 |
||
587 |
sub ITS_68(@) # T90|T68 -> T68 |
|
588 |
{ |
|
589 |
unless (defined($ITS)) { |
|
590 |
$ITS = &antsRequireParam('ITS'); |
|
591 |
croak("$0 ITS_68(): ITS == $ITS???\n") |
|
592 |
unless ($ITS == 68 || $ITS ==90); |
|
593 |
unless ($ITS == 68) { |
|
594 |
croak("$0 ITS_68(): can't change %ITS after flushing header\n") |
|
595 |
if ($antsHeadersPrinted); |
|
596 |
&antsAddParams('ITS',68); |
|
597 |
} |
|
598 |
} |
|
599 |
my($temp) = &antsFunUsage(1,".","temp",@_); |
|
600 |
return nan unless (numberp($temp)); |
|
601 |
return($temp) if ($ITS == 68); |
|
602 |
return $temp * 1.00024; |
|
603 |
} |
|
604 |
||
605 |
} # static scope |
|
606 |
||
607 |
{ my($ITS); |
|
608 |
||
609 |
sub ITS_90(@) # T90|T68 -> T90 |
|
610 |
{ |
|
611 |
unless (defined($ITS)) { |
|
612 |
$ITS = &antsRequireParam('ITS'); |
|
613 |
croak("$0 ITS_90(): ITS == $ITS???\n") |
|
614 |
unless ($ITS == 68 || $ITS ==90); |
|
615 |
unless ($ITS == 90) { |
|
616 |
croak("$0 ITS_90(): can't change %ITS after flushing header\n") |
|
617 |
if ($antsHeadersPrinted); |
|
618 |
&antsAddParams('ITS',90); |
|
619 |
} |
|
620 |
} |
|
621 |
my($temp) = &antsFunUsage(1,".","temp",@_); |
|
622 |
return nan unless (numberp($temp)); |
|
623 |
return($temp) if ($ITS == 90); |
|
624 |
return $temp / 1.00024; |
|
625 |
} |
|
626 |
||
627 |
} |
|
628 |
||
629 |
#---------------------------------------------------------------------- |
|
630 |
# Oxygen Unit Conversion |
|
631 |
# |
|
632 |
# - old units (e.g. sd2) are ml/l |
|
633 |
# - new units (e.g. WOCE) are umol/kg => independent of pressure! |
|
634 |
# - conversion (from [http://sea-mat.whoi.edu/robbins/ox_units.m]; Paul |
|
635 |
# Robbins) uses potential density ref'd to surface --- makes sense |
|
636 |
# because titration is presumably done at atmospheric pressure |
|
637 |
# - constant divisor is volume of one mole derived from gas law |
|
638 |
# (PV = nRT) in the right units (whatever) |
|
639 |
#---------------------------------------------------------------------- |
|
640 |
||
641 |
{ my(@fc); |
|
642 |
sub O2mlpl2umpkg(@) |
|
643 |
{ |
|
47 | 644 |
# return nan if isnan($_[3]); |
645 |
return nan |
|
646 |
if (@_ == 4 && !numberp($_[0]+$_[1]+$_[2]+$_[3])); |
|
0 | 647 |
my($S,$T,$P,$mlpl) = |
648 |
&antsFunUsage(4,'ffff','[S, T, P [dbar], O2 [ml/l]]', |
|
649 |
\@fc,'salin','temp','press','O2',@_); |
|
650 |
return $mlpl * 1000/(1000+sigma($S,$T,$P,0)) / .022403; |
|
651 |
} |
|
652 |
} |
|
653 |
||
654 |
{ my(@fc); |
|
655 |
sub O2umpkg2mlpl(@) |
|
656 |
{ |
|
47 | 657 |
# return nan if isnan($_[3]); |
658 |
return nan |
|
659 |
if (@_ == 4 && !numberp($_[0]+$_[1]+$_[2]+$_[3])); |
|
0 | 660 |
my($S,$T,$P,$umpkg) = |
661 |
&antsFunUsage(4,'ffff','[S, T, P [dbar], O2 [ml/l]]', |
|
662 |
\@fc,'salin','temp','press','O2',@_); |
|
663 |
return .022403 * $umpkg * (1000+sigma($S,$T,$P,0))/1000; |
|
664 |
} |
|
665 |
} |
|
666 |
||
667 |
#---------------------------------------------------------------------- |
|
668 |
# Color Conversion |
|
669 |
# |
|
670 |
# - algorithms taken from the web; source given alternatively as ACM |
|
671 |
# and Foley and VanDam |
|
672 |
# - from the available GMT default cpt files, it looks like |
|
673 |
# the range for hue is 0 - 359 (angles on a circle) |
|
674 |
# - ACM implementation uses a hue range of 0-6 with pure red being 0 or 6 |
|
675 |
# - ACM implementation uses range of 0-1 for R,G,B |
|
676 |
# - in HSV, gray scales are not uniquely defined; I extended the |
|
677 |
# algorithms to behave like matlab in this case, i.e. return a hue of |
|
678 |
# pure red (0) |
|
679 |
#---------------------------------------------------------------------- |
|
680 |
||
681 |
sub HSV2RGB(@) |
|
682 |
{ |
|
683 |
my($H,$S,$V) = &antsFunUsage(3,"fff","H (0-360), S (0-1), V (0-1), ",@_); |
|
684 |
my($m,$n,$f,$i); |
|
685 |
||
686 |
$H = 0 if ($H < 0 && $H >= -$PRACTICALLY_ZERO); |
|
687 |
croak("$0 HSV2RGB(): H=$H out of range\n") if ($H < 0 || $H > 360); |
|
688 |
croak("$0 HSV2RGB(): S=$S out of range\n") if ($S < 0 || $S > 1); |
|
689 |
croak("$0 HSV2RGB(): V=$V out of range\n") if ($V < 0 || $V > 1); |
|
690 |
||
691 |
$i = POSIX::floor($H/60); # ACM implementation uses [0-6] with red = 0 = 6 |
|
692 |
$f = $H/60 - $i; |
|
693 |
$f = 1 - $f if (!($i & 1)); # if i is even |
|
694 |
$m = $V * (1 - $S); |
|
695 |
$n = $V * (1 - $S * $f); |
|
696 |
return (int(255*$V+0.5),int(255*$n+0.5),int(255*$m+0.5)) if ($i==0 || $i==6); |
|
697 |
return (int(255*$n+0.5),int(255*$V+0.5),int(255*$m+0.5)) if ($i == 1); |
|
698 |
return (int(255*$m+0.5),int(255*$V+0.5),int(255*$n+0.5)) if ($i == 2); |
|
699 |
return (int(255*$m+0.5),int(255*$n+0.5),int(255*$V+0.5)) if ($i == 3); |
|
700 |
return (int(255*$n+0.5),int(255*$m+0.5),int(255*$V+0.5)) if ($i == 4); |
|
701 |
return (int(255*$V+0.5),int(255*$m+0.5),int(255*$n+0.5)) if ($i == 5); |
|
702 |
croak("$0 HSV2RGB(): implementation error"); |
|
703 |
} |
|
704 |
||
705 |
sub RGB2HSV(@) |
|
706 |
{ |
|
707 |
my($R,$G,$B) = &antsFunUsage(3,"cc","R, G, B",@_); |
|
708 |
my($V,$x,$f,$i,$H); |
|
709 |
||
710 |
$R /= 255; $G /= 255; $B /= 255; |
|
711 |
croak("$0 RGB2HSV(): R out of range\n") if ($R < 0 || $R > 1); |
|
712 |
croak("$0 RGB2HSV(): G out of range\n") if ($G < 0 || $G > 1); |
|
713 |
croak("$0 RGB2HSV(): B out of range\n") if ($B < 0 || $B > 1); |
|
714 |
||
715 |
$x = min($R,$G,$B); |
|
716 |
$V = max($R,$G,$B); |
|
717 |
return (0,0,$V) if ($V == $x); # any hue is valid |
|
718 |
||
719 |
$f = ($R == $x) ? $G - $B : (($G == $x) ? $B - $R : $R - $G); |
|
720 |
$i = ($R == $x) ? 3 : (($G == $x) ? 5 : 1); |
|
721 |
||
722 |
$H = 60 * ($i - $f / ($V - $x)); |
|
723 |
$H = 0 if ($H == 360); |
|
724 |
||
725 |
return ($H, ($V - $x)/$V, $V); |
|
726 |
} |
|
727 |
||
728 |
1; |