      subroutine dms2rad(str,rad,error)
C
C Description:
C 
C This routine converts a string that specifies a declination in
C degree, minute, second format to an angle specified in South
C Polor Distance (SPD) radians.
C
C Programming interface:
C
C Name   In/out  Type           Structure  Meaning
C
C str    in      character*(*)  scalar     The string specifying the angle
C rad    out     real*8         scalar     The angle expressed in SPD radians
C error  out     logical        scalar     TRUE if an error occurs.
C
C Errors:
C 
C If the angle is in a format that cannot be converted, or if the 
C string specification is in a non-standard format, or if the specified
C angle is out of the standard range, then error is
C set TRUE and the routine immediately terminates.
C
C Routines accessed:
C
C err_msg
C fnblnk
C is_fp_num
C is_int_num
C left_justify
C
      implicit none
      include 'std_io.for'
      include 'include_.for'
      real*8 rad,rdum1,rdum2,rdum3
      integer ptr1,ptr2,fnblnk
      logical error,negative,is_fp_num,is_int_num
      character*64 string,substring1,substring2,substring3,
     $     left_justify
      character*(*) str
      rad=0.0d0
      string=str
      ptr1=fnblnk(string)
      if (ptr1 .eq. 0) then
         call err_msg('dms2rad','The Decl. string is empty.')
         error=.true.
      else
         if (string(ptr1:ptr1) .eq. '-') then
            negative=.true.
            string(ptr1:ptr1)=' '
         else
            negative=.false.
         end if
         if (index(string,'-') .ne. 0) then
            call err_msg('dms2rad',
     $           'Only one minus sign may preceed Decl.')
            error=.true.
         else
            string=left_justify(string)
            ptr1=fnblnk(string)
            ptr2=index(string,' ')
            if (ptr1 .eq. 0) then
               call err_msg('dms2rad','The Decl. contains no number.')
               error=.true.
            else
               substring1=string(1:ptr2-1)
               string=left_justify(string(ptr2:))
               ptr1=fnblnk(string)
               ptr2=index(string,' ')
               if (ptr1 .eq. 0) then
                  substring2='0'
                  substring3='0'
               else
                  substring2=string(1:ptr2-1)
                  string=left_justify(string(ptr2:))
                  ptr1=fnblnk(string)
                  ptr2=index(string,' ')
                  if (ptr1 .eq. 0) then
                     substring3='0'
                  else
                     substring3=string(1:ptr2-1)
                     string=left_justify(string(ptr2:))
                     if (string .ne. ' ') then
                        call err_msg('dms2rad',
     $                       'Too many tokens in the Decl. string.')
                        error=.true.
                     end if
                  end if
               end if
            end if
         end if
      end if
      if (.not. error) then
         if ((is_fp_num(substring1) .or.
     $        is_int_num(substring1)) .and.
     $        (is_fp_num(substring2) .or.
     $        is_int_num(substring2)) .and.
     $        (is_fp_num(substring3) .or.
     $        is_int_num(substring3))) then
            call str2r8(substring1,rdum1,error)
            call str2r8(substring2,rdum2,error)
            call str2r8(substring3,rdum3,error)
            if (error) goto 400
            if (rdum1 .le. 90.0d0) then
               if (rdum2 .le. 60.0d0) then
                  if (rdum3 .le. 60.0d0) then
                     rad=rdum3/3600.0d0+rdum2/60.0d0+rdum1
                     if (negative) then 
                        rad=-rad
                     end if
                     rad=pio180*rad+pio2
                  else
                     error=.true.
                     call err_msg('dms2rad',
     $                    'Decl. seconds must be less than 60.')
                  end if
               else
                  error=.true.
                  call err_msg('dms2rad',
     $                 'Decl. minutes must be less than 60.')
               end if
            else
               error=.true.
               call err_msg('dms2rad',
     $              '|Decl. degrees| must be less than 90.')
            end if
         else
            call err_msg('dms2rad','The Decl. contains a non-number.')
            error=.true.
         end if
      end if
 400  continue
      if (error) then
         call err_msg('dms2rad','Traceback')
      end if
      return
      end
