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