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