#
# Routines to convert from Babylonian notation to decimal.
#
# -- MWR 4/6/2001
#
# Fixed bug in "hhh_to_hms" which didn't convert "-00:xx:xx" properly
#   to a negative value.
# -- MWR 7/10/2001
#
# Fixed "hhh_to_hms" so that it gets rid of any leading zeroes in
#   expressions for hours, minutes and seconds, before performing
#   calculations.  Evidently,
#             expr 08/60.0
#   causes an error, whereas
#             expr 8/60.0
#   does not.  
# -- MWR 7/30/2001
#
# Fixed error in hms_to_hhh: old version gave error if "seconds" value
#   was 60 ... but 60 _is_ a valid value (especially after rounding).
#   Thanks to Michael Sallman for pointing this out.
# -- MWR 10/15/2001


############################################################################
# PROCEDURE: is_baby
#
# DESCRIPTION:  Checks to see if the argument is a time (or angle)
#               in Babylonian notation, like
#
#                         10:50:35.3
#
# RETURNS:
#    1          if the argument does look Babylonian
#    0          if not
#
proc is_baby { arg } {
  global debug

  if { $debug > 1 } {
    puts stdout "entering is_baby"
  }

  if { [regexp .*:.*:.* $arg] == 1 } {
    return 1
  } else {
    return 0
  }

}



############################################################################
# PROCEDURE: hms_to_hhh
#
# DESCRIPTION:  Given a value in Babylonian notation, like 
#
#                         10:50:35.3
#
#               convert to decimal format, like this:
#
#                         10.8431
#           
#               Place the converted value into the output arg 'hhh_value'
#
# RETURNS: 
#     0         if all goes well
#     1         if there's a problem
#
proc hms_to_hhh { hms_value hhh_value } {
  upvar $hhh_value hhh
  global debug

  if { $debug > 1 } {
    puts stdout "entering hms_to_hhh"
  }

  # parse the string -- we require Hours, Minutes, and Seconds
  set splitstr [split $hms_value :]
  if { [llength $splitstr] != 3 } {
    puts stderr "hms_to_hhh: bad input value $hms_value"
    return 1
  }

  # now convert each component into decimal 
  set hours [lindex $splitstr 0]
  # check for a negative sign
  if { [scan $hours "-%d" foo] == 1 } {
    set sign -1
  } else {
    set sign 1
  }
  set hours [format "%.0f" $hours]
  if { $debug > 3 } {
    puts "hms_to_hhh: hours is $hours"
  }
  set minutes [lindex $splitstr 1]
  set minutes [format "%.0f" $minutes]
  if { $debug > 3 } {
    puts "hms_to_hhh: minutes is $minutes"
  }
  if { ($minutes < 0) || ($minutes > 59) } {
    puts stderr "hms_to_hhh: bad input minutes value $minutes"
    return 1
  }
  set seconds [lindex $splitstr 2]
  set seconds [format "%.0f" $seconds]
  if { $debug > 3 } {
    puts "hms_to_hhh: seconds is $seconds"
  }
  if { ($seconds < 0) || ($seconds > 60) } {
    puts stderr "hms_to_hhh: bad input seconds value $seconds"
    return 1
  }

  # add all components together 
  set hhh [expr $hours + ($minutes/60.0) + ($seconds/3600.0)]

  # make sure that the value is negative, if there was a negative
  #   sign at the beginning of the original string.  We have to make
  #   this check (usually unnecessary) in case the hours are zero.
  if { $sign == -1 } {
    if { $hhh > 0 } {
	   set hhh [expr 0.0-$hhh]
	 }
  }
  
  return 0
}



############################################################################
# PROCEDURE: rahms_to_radeg
#
# DESCRIPTION:  Given an RA value in Babylonian notation, like 
#
#                         10:50:35.3
#
#               convert to RA in decimal degrees, like this:
#
#                         158.9021
#           
#               Place the converted value into the output arg 'radeg_value'
#
# RETURNS: 
#     0         if all goes well
#     1         if there's a problem
#
proc rahms_to_radeg { rahms_value radeg_value } {
  upvar $radeg_value radeg
  global debug

  if { $debug > 1 } {
    puts "entering rahms_to_radeg"
  }

  if { [hms_to_hhh $rahms_value rahhh] != 0 } {
    puts stderr "rahms_to_radeg: hms_to_hhh fails for $rahms_value"
    return 1
  }

  set radeg [expr $rahhh*15.0]

  return 0
}

