libconv.pl
changeset 38 15c603bc4f70
parent 36 04e8cb4f8073
child 47 dde46143288c
equal deleted inserted replaced
37:b24d11f7dfc4 38:15c603bc4f70
     1 #======================================================================
     1 #======================================================================
     2 #                    L I B C O N V . P L 
     2 #                    L I B C O N V . P L 
     3 #                    doc: Sat Dec  4 13:03:49 1999
     3 #                    doc: Sat Dec  4 13:03:49 1999
     4 #                    dlm: Tue May 22 11:19:54 2018
     4 #                    dlm: Fri Feb 15 13:21:08 2019
     5 #                    (c) 1999 A.M. Thurnherr
     5 #                    (c) 1999 A.M. Thurnherr
     6 #                    uE-Info: 68 41 NIL 0 0 70 2 2 4 NIL ofnI
     6 #                    uE-Info: 529 24 NIL 0 0 70 2 2 4 NIL ofnI
     7 #======================================================================
     7 #======================================================================
     8 
     8 
     9 # HISTORY:
     9 # HISTORY:
    10 #   Dec 12, 1999: - created for the Rainbow CM data as libdate
    10 #   Dec 12, 1999: - created for the Rainbow CM data as libdate
    11 #   Jul 07, 2000: - renamed to libconv and added lat/lon conversions
    11 #   Jul 07, 2000: - renamed to libconv and added lat/lon conversions
    64 #	Aug  7, 2014: - finally cleaned up date conversions
    64 #	Aug  7, 2014: - finally cleaned up date conversions
    65 #	Jan 27, 2017: - BUG: dayNo() numeric month could have leading/trailing whitespace
    65 #	Jan 27, 2017: - BUG: dayNo() numeric month could have leading/trailing whitespace
    66 #	Jul  6, 2017: - BUG: date conversion routines did not parse 1/5/12 correctly
    66 #	Jul  6, 2017: - BUG: date conversion routines did not parse 1/5/12 correctly
    67 #	Dec 18, 2017: - removed ambiguous-date warning
    67 #	Dec 18, 2017: - removed ambiguous-date warning
    68 #	May 22, 2018: - added NMEA2dec_time()
    68 #	May 22, 2018: - added NMEA2dec_time()
       
    69 #	Jan 17, 2019: - added ISO_Datetime()
       
    70 #	Feb 15, 3019: - added deg2lat, deg2lon
    69 
    71 
    70 require "$ANTS/libEOS83.pl";                        # &sigma()
    72 require "$ANTS/libEOS83.pl";                        # &sigma()
    71 require "$ANTS/libPOSIX.pl";                        # &floor()
    73 require "$ANTS/libPOSIX.pl";                        # &floor()
    72 require "$ANTS/libstats.pl";                        # &min(),&max()
    74 require "$ANTS/libstats.pl";                        # &min(),&max()
    73 
    75 
   308 
   310 
   309 	return $time;
   311 	return $time;
   310 }
   312 }
   311 
   313 
   312 #----------------------------------------------------------------------
   314 #----------------------------------------------------------------------
   313 # Decimal Time to Strin Conversion
   315 # Decimal Time to String Conversion
   314 #----------------------------------------------------------------------
   316 #----------------------------------------------------------------------
   315 
   317 
   316 { my(@fc);
   318 { my(@fc);
   317 
   319 
   318 	sub Date(@) 										# day number -> date
   320 	sub Date(@) 										# day number -> date
   371 		$min++,$sec=0 if ($sec == 60);
   373 		$min++,$sec=0 if ($sec == 60);
   372 		$hour++,$min=0 if ($min == 60);
   374 		$hour++,$min=0 if ($min == 60);
   373 		$day++,$hour=0 if ($hour == 24);
   375 		$day++,$hour=0 if ($hour == 24);
   374 	
   376 	
   375 		return sprintf('%02d:%02d:%02d',$hour,$min,$sec);
   377 		return sprintf('%02d:%02d:%02d',$hour,$min,$sec);
       
   378 	}
       
   379 }
       
   380 
       
   381 { my(@fc);
       
   382 
       
   383 	sub ISO_Datetime(@)									# day number -> ISO 8601
       
   384 	{
       
   385 	
       
   386 		my($dnf);										# find std dn field & epoch
       
   387 		if (@_ == 0) {
       
   388 			for (my($i)=0; $i<@antsLayout; $i++) {
       
   389 				next unless ($antsLayout[$i] =~ /^dn(\d\d)$/);
       
   390 				$dnf = $antsLayout[$i]; push(@_,$1);
       
   391 				last;
       
   392 	        }
       
   393 	    }
       
   394 	    
       
   395 		my($year,$fday) = &antsFunUsage(2,"cf","epoch, dayNo",\@fc,undef,$dnf,@_);
       
   396 	
       
   397 		$year += ($year < 50) ? 2000 : 1900 			# Y2K
       
   398 			if ($year < 100);
       
   399 	
       
   400 		$day = int($fday);								# prevent runover on last day of month
       
   401 		$fday -= $day;
       
   402 		
       
   403 		while ($day > 365+&leapYearP($year)) {			# adjust year
       
   404 			$day -= 365 + &leapYearP($year);
       
   405 			$year++;
       
   406 		}
       
   407 	
       
   408 		my($month) = 1;
       
   409 		while ($day > &monthLength($year,$month)) {
       
   410 			$day -= &monthLength($year,$month);
       
   411 			$month++;
       
   412 		}
       
   413 	
       
   414 		my($hour) = int(24*$fday);
       
   415 		$fday -= $hour/24;
       
   416 		my($min) = int(24*60*$fday);
       
   417 		$fday -= $min/24/60;
       
   418 		my($sec) = round(24*3600*$fday);
       
   419 		$min++,$sec=0 if ($sec == 60);
       
   420 		$hour++,$min=0 if ($min == 60);
       
   421 		$day++,$hour=0 if ($hour == 24);
       
   422 
       
   423 		return sprintf('%04d-%02d-%02dT%02d:%02d:%02d',$year,$month,$day,$hour,$min,$sec);
   376 	}
   424 	}
   377 }
   425 }
   378 
   426 
   379 #----------------------------------------------------------------------
   427 #----------------------------------------------------------------------
   380 # Other Misc Date Conversions
   428 # Other Misc Date Conversions
   457     my($deg,$a,$b) = ($s =~ m{^([-\d]+)[\s:]([\d\.]+)\s*([NSEW])$});
   505     my($deg,$a,$b) = ($s =~ m{^([-\d]+)[\s:]([\d\.]+)\s*([NSEW])$});
   458 #    print(STDERR "--> $deg, $a, $b\n");
   506 #    print(STDERR "--> $deg, $a, $b\n");
   459 	return ($b eq "") ? &dmh2d($deg,0,$a) : &dmh2d($deg,$a,$b);
   507 	return ($b eq "") ? &dmh2d($deg,0,$a) : &dmh2d($deg,$a,$b);
   460 }
   508 }
   461 
   509 
   462 sub GMT2deg(@)	# GMT degree format to decimal
   510 sub GMT2deg(@)	    # GMT degree format to decimal
   463 {
   511 {
   464 	my($GMT) = &antsFunUsage(1,".","GMT-degs ",@_);
   512 	my($GMT) = &antsFunUsage(1,".","GMT-degs ",@_);
   465 	return (substr($1,0,1) eq "-") ? $1-$2/60.0 : $1+$2/60.0
   513 	return (substr($1,0,1) eq "-") ? $1-$2/60.0 : $1+$2/60.0
   466 		if ($GMT =~ /\s*([^:]+):([^:]+)/);
   514 		if ($GMT =~ /\s*([^:]+):([^:]+)/);
   467 	return $GMT;
   515 	return $GMT;
       
   516 }
       
   517 
       
   518 sub deg2lat(@)		# decimal latitude to degrees:min.xx NS
       
   519 {
       
   520 	my($deg) = &antsFunUsage(1,'f','decimal latitude',@_);
       
   521 	return sprintf("%02d:%06.3f'%s",abs(int($deg)),
       
   522 								 (abs($deg)-abs(int($deg)))*60,
       
   523 	   							 $deg>=0 ? "N" : "S");
       
   524 }
       
   525 
       
   526 sub deg2lon(@)		# decimal longitude to degrees:min.xx EW
       
   527 {
       
   528 	my($deg) = &antsFunUsage(1,'f','decimal longitude',@_);
       
   529 	return sprintf("%03d:%06.3f'%s",abs(int($deg)),
       
   530 								 (abs($deg)-abs(int($deg)))*60,
       
   531 	   							 $deg>=0 ? "E" : "W");
   468 }
   532 }
   469 
   533 
   470 #----------------------------------------------------------------------
   534 #----------------------------------------------------------------------
   471 # Temp-Scale Conversion
   535 # Temp-Scale Conversion
   472 #----------------------------------------------------------------------
   536 #----------------------------------------------------------------------