#!/usr/bin/perl
#******************************************************************************
#  Copyright (C) 2000 ImageLinks Inc.
# 
# License:  LGPL
# 
# See LICENSE.txt file in the top level directory for more details.
# 
#  Author:  David Burken
#
# DESCRIPTION:
#     This script is used to convert decimal degrees to degress, minutes,
#     seconds (DMS).
#
#***************************************************************************** 
# $Id: deg2dms 6825 2004-09-30 15:13:58Z dburken $

print ("\nEnter \"q\" to quit.\n");

while (1)
{
   
   &getDegrees;

   # Compute the answer.
   $deg = int ($floatDeg);
   if ($deg)
   {
      $floatMin = (($floatDeg - $deg) * 60); 
   }
   else
   { 
      $floatMin = $floatDeg * 60;
   } 

   $min = int ($floatMin);
   if ($min)
   {
      $floatSec = ($floatMin - $min) * 60;
   }
   else
   {
      $floatSec = $floatMin * 60;
   }

   if ($negativeHemisphere)
   {
      $deg = $deg * -1;
   }
   print ("$floatDeg = $deg degrees, $min minutes, $floatSec seconds. \n");
}

# Get degrees
sub getDegrees
{
   {
      print ("\nEnter degrees:  ");
      $floatDeg = <STDIN>;
      chop ($floatDeg);

      if ($floatDeg =~ /q/i)
      {
	  exit(0);
      }

      # Check for negative.  Some users will use negative to indicate 
      # hemisphere.

      $negativeHemisphere = 0;
      if ($floatDeg < 0)
      { 
         $negativeHemisphere = 1;
         $floatDeg = abs ($floatDeg);
      }

      # Check the range.
      if ($floatDeg > 179)
      {
         die ("Degrees must be 179 or less.  Exiting\n");
      }

   }
}
