/*
   ###########################################################################
   This function converts a DMS time string into equivalent decimal degrees.

   INPUT: 1, 2 or 3-element DMS (Degrees Minutes Seconds) separated by spaces
          as angular string as 'deg min sec' values.

   Example: Given DMS string = '9 12 50.306'
            Returns: 9.2139738888888889

   NO DEPENDENCIES

   ERRORS:
   No error checking is done.
   ###########################################################################
*/

   function DMS_to_Degrees ($DMSstr, $Decimals=16)
{
   $dms = StrToLower(trim($DMSstr));

   $Q = 40;
   $q = trim($Decimals);

/* ----------------------------------------------------
   Normalize the spacing and then split and extract the
   individual angular string elements (dd mm ss).
*/
   $dms = PReg_Replace("/\s+/", " ", trim($dms));
   $wdms = PReg_Split("[ ]", $dms);
   $wdmscount = count($wdms);
   $dd = ($wdmscount >= 1)? bcAdd($wdms[0],"0", $Q) : "0";
   $mm = ($wdmscount >= 2)? bcAdd($wdms[1],"0", $Q) : "0";
   $ss = ($wdmscount >= 3)? bcAdd($wdms[2],"0", $Q) : "0";

// ------------------------------------------------
// Remember and then remove any numerical (±) sign.

   $NumSign = (substr($DMSstr,0,1) == '-')? '-' : '';
   $dd = Str_Replace('-', '', $dd);
   $dd = Str_Replace('+', '', $dd);

/* -------------------------------------------------------------
   If original angle argument began with a + sign, then preserve
   it so that all returned positive results will have a + sign.
   Otherwise, positive results will NOT have a + sign.
*/
   if (substr($DMSstr,0,1) == '+') {$NumSign = '+';}

/* ----------------------------------------------
   Compute decimal degrees/hours value equivalent
   to the given DHMS argument elements.
*/
   $w2 = bcAdd(bcAdd(bcMul($dd,"3600",$Q),bcMul($mm,"60",$Q),$Q),$ss,$Q);

// -----------------------------------------------------------
// If result equates to zero, then suppress any numerical sign.

   if (bcComp($w2, '0', $Q) == 0) {$NumSign = '';}

// ---------------------------------------------------------
// Round off result to 16 decimals, recalling original sign.

   $z = Str_Repeat('0', $q);
   $w = $NumSign . bcAdd(bcDiv($w2,"3600",$Q), "0.".$z."5", $q);
   $w = RTrim($w, '0');
   $w = RTrim($w, '.');

   return $w;

} // End of  DMS_to_Degrees (...)