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