c ***************************************************************************
c        function case (string)
c ***************************************************************************
c
         function case (string)
c
c   Function which takes a string of 80 characters and converts the 
c   upper case letters in the string to lower case letters
c
      implicit double precision (a-h,o-z)
         character*80 string, case
         character * 1 xlett
c
         do 10 i = 1, 80
               xlett = string(i:i)
               itry = ichar (xlett)
                if (xlett .ge. 'A' .and. xlett .le. 'Z') then 
                    itry = itry + 32
                    string (i:i) = char (itry)
                endif
   10     continue
c
                case  = string 
c
          return
          end
C
C***********************************************************************
C     ICODAL
C***********************************************************************
      INTEGER FUNCTION ICODAL(A,I)
C
C     TRANSLATES THE ALPHANUMERIC CHARACTERS INTO AN INTEGER CODE
C
C     0-9      NUMERALS 0-9
C     -1       BLANK
C     -2       DASH
C     -3       EQUAL SIGN                                               0905YC96
C     -4       EVERYTHING ELSE                                          0905YC96
C
C     CALLED BY:
C              READINT
C 
      CHARACTER*1 A(80),B(13)
C
      DATA B /'0','1','2','3','4','5','6','7','8','9',' ','-','='/      0905YC96
C
      DO 100 J=1,13                                                     0905YC96
      IF(A(I).EQ.B(J)) THEN
      ICODAL=J-1
      IF(J.GT.10) ICODAL=10-J
      RETURN
      ENDIF
  100 CONTINUE
      ICODAL=-4                                                         0905YC96
      RETURN
      END
C
C**********************************************************************
C RDLN
C**********************************************************************
C
      SUBROUTINE rdln(IUNIT,WORD,NWORD,LSEC,LEOF,STRING)
C
      IMPLICIT DOUBLE PRECISION(A-H,O-Z)
C
CAMBP#ifdef MPI
CAMBPc     =========================== AMBER/MPI ===========================
CAMBP#     ifdef MPI_DOUBLE_PRECISION
CAMBP#       undef MPI_DOUBLE_PRECISION
CAMBP#     endif
CAMBP      include 'mpif.h'
CAMBP#     ifdef CRAY_PVP
CAMBP#       define MPI_DOUBLE_PRECISION MPI_REAL8
CAMBP#     endif
CAMBPc     ========================= END AMBER/MPI =========================
CAMBP#     include "parallel.h"
CAMBP#     include "extra.h"
CAMBP#endif
C
      CHARACTER*80  LINE,SENAME,STRING,UPCSE
      CHARACTER*80  WORD(40)
      LOGICAL       LEOF,LSEC
C
      LSEC = .FALSE.
      LEOF = .FALSE.
      NWORD = 0
C
CAMBP#ifdef MPI
CAMBP 10   continue
CAMBPc
CAMBP      if(master) READ(IUNIT,'(A80)',END=500) STRING
CAMBP      iend=0
CAMBP      call mpi_bcast(iend, 1, mpi_integer, 0, commsander, ier) 
CAMBP      if(iend.eq.1) goto 500
CAMBP      call mpi_bcast(string, 80, mpi_character, 0, commsander, ier) 
CAMBP#else
10    READ(IUNIT,'(A80)',END=500) STRING
CAMBP#endif
C
      IP = 0
C
C     Find the first nonblank character in a line
C
20    IP = IP + 1
      IF (STRING(IP:IP) .EQ. ' ' .AND. IP .LT. 80) GO TO 20
C
C     If it all blank or it is a comment line then read the next line
C
      IF (IP .EQ. 80 .OR. STRING(IP:IP) .EQ. '#')  GO TO 10
C
C     Convert to uppercase
C
      LINE = UPCSE(STRING)
C
50    ICOM = 80
C
55    IF (IP .LT. ICOM) THEN
         IBEG = IP
60       IP = IP + 1
         IF (LINE(IP:IP) .NE. ' ' .AND. IP .LT. ICOM) GOTO 60 
         NWORD = NWORD + 1
         WORD(NWORD) = LINE(IBEG:IP-1)
70       IP = IP + 1
         IF (LINE(IP:IP) .EQ. ' ' .AND. IP .LT. ICOM) GOTO 70
         GOTO 55
      ENDIF
C
      IF (WORD(1)(1:1) .EQ. '*') THEN
         SENAME = WORD(1)(2:)
         WORD(1) = SENAME
         LSEC = .TRUE.
      ENDIF
C
      RETURN
C
500   LEOF = .TRUE.
C
CAMBP#ifdef MPI
CAMBP      if(master) then
CAMBP         iend=1
CAMBP         call mpi_bcast(iend, 1, mpi_integer, 0, commsander, ier) 
CAMBP      end if
CAMBP#endif
C
      RETURN
C
      END
C
C**********************************************************************
C READLN
C**********************************************************************
C
      SUBROUTINE readln(IUNIT,WORD,NWORD,LSEC,LEOF)
C
      IMPLICIT DOUBLE PRECISION(A-H,O-Z)
C
CAMBP#ifdef MPI
CAMBPc     =========================== AMBER/MPI ===========================
CAMBP#     ifdef MPI_DOUBLE_PRECISION
CAMBP#       undef MPI_DOUBLE_PRECISION
CAMBP#     endif
CAMBP      include 'mpif.h'
CAMBP#     ifdef CRAY_PVP
CAMBP#       define MPI_DOUBLE_PRECISION MPI_REAL8
CAMBP#     endif
CAMBPc     ========================= END AMBER/MPI =========================
CAMBP#     include "parallel.h"
CAMBP#     include "extra.h"
CAMBP#endif
C
      CHARACTER*81  LINE,SENAME,STRING,UPCSE
      CHARACTER*80  WORD(40)
      LOGICAL       LEOF,LSEC
C
      LSEC = .FALSE.
      LEOF = .FALSE.
      NWORD = 0
C
CAMBP#ifdef MPI
CAMBP 10   continue
CAMBPc
CAMBP      if(master) READ(IUNIT,'(A80)',END=500) STRING
CAMBP      iend=0
CAMBP      call mpi_bcast(iend, 1, mpi_integer, 0, commsander, ier) 
CAMBP      if(iend.eq.1) goto 500
CAMBP      call mpi_bcast(string, 80, mpi_character, 0, commsander, ier) 
CAMBP#else
10    READ(IUNIT,'(A80)',END=500) STRING
CAMBP#endif
C
      IP = 0
C
C     Find the first nonblank character in a line
C
20    IP = IP + 1
      IF (STRING(IP:IP) .EQ. ' ' .AND. IP .LE. 80) GO TO 20           
C
C     If it all blank or it is a comment line then read the next line
C
      IF (IP .GT. 80 .OR. STRING(IP:IP) .EQ. '#')  GO TO 10
C
C     Convert to uppercase
C
      LINE = UPCSE(STRING)
C
C     Find the position of a comment character, if any,
C     in a nonblank noncomment line
C
      ICOM = IP 
50    ICOM = ICOM + 1
      IF (LINE(ICOM:ICOM) .NE. '#' .AND. ICOM .LE. 80)  GOTO 50         1103WH94
C
55    IF (IP .LT. ICOM) THEN
         IBEG = IP
60       IP = IP + 1
         IF (LINE(IP:IP) .NE. ' ' .AND. IP .LT. ICOM) GOTO 60 
         NWORD = NWORD + 1
         WORD(NWORD) = LINE(IBEG:IP-1)
70       IP = IP + 1
         IF (LINE(IP:IP) .EQ. ' ' .AND. IP .LT. ICOM) GOTO 70
         GOTO 55
      ENDIF
C
      IF (WORD(1)(1:1) .EQ. '*') THEN
         SENAME = WORD(1)(2:)
         WORD(1) = SENAME
         LSEC = .TRUE.
      ENDIF
C
      RETURN
C
500   LEOF = .TRUE.
CAMBP#ifdef MPI
CAMBP      if(master) then
CAMBP         iend=1
CAMBP         call mpi_bcast(iend, 1, mpi_integer, 0, commsander, ier) 
CAMBP      end if
CAMBP#endif
C
      RETURN
C
      END
c
c ***************************************************************************
c     rword
c ***************************************************************************
c
      subroutine rword(string,istrt,ierr)
c
c     This subroutine is a utility routine that takes a character string
c     and searches from the current character (istrt) for the first
c     character that occurs after at least one blank (ie for the next 
c     word on a line).  It returns the location of that character in
c     istrt.  ierr is set to 1 if no other characters were found in 
c     the string.
c
      implicit double precision (a-h,o-z)
      character * 80 string
c
c     clear flags
      jblank = 0
      ierr = 0
c
c     search for the first blank - signaling the end of the current word
c
      do while (istrt.le.79.and.jblank.eq.0)
         istrt = istrt+1
         if (string(istrt:istrt).eq.' ') then
c                                               find first letter of next word
            jblank = 1
            do while (string(istrt:istrt).eq.' '.and.ierr.eq.0)
               istrt = istrt + 1
               if (istrt.gt.80) ierr=1
            end do
         end if
         if (istrt.eq.80) ierr=1
      end do
      return
      end
C
C***********************************************************************
C UPCAS
C***********************************************************************
C
      SUBROUTINE upcas(STRING,LENSTR)
C
C     CALLED BY:
C              READINT
C
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)
C
      CHARACTER*(*) STRING
      CHARACTER*26 UCASE,LCASE
C
      DATA UCASE /'ABCDEFGHIJKLMNOPQRSTUVWXYZ'/
      DATA LCASE /'abcdefghijklmnopqrstuvwxyz'/
C
C     CONVERTS LOWER CASE TO UPPER CASE IN THE GIVEN STRING.
C           (ADAPTED FROM GAMESS)
C
      DO 100 I=1,LENSTR
         IC = INDEX(LCASE,STRING(I:I))
         IF (IC.GT.0) STRING(I:I) = UCASE(IC:IC)
  100 CONTINUE
      RETURN
      END
C
C***************************************************************************
C UPCSE 
C***************************************************************************
C
      CHARACTER*80 FUNCTION upcse(STRING)
C
C   Function which takes a string of 80 characters and converts the 
C   lower case letters in the string to upper case letters
C   This functions is a modified version of CASE which was written
C   by Rozeanne Steckler
C
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)
C
      CHARACTER*80 STRING,LINE
      CHARACTER * 1 XLETT
C
      LINE = STRING
      DO 10 I = 1, 80
         XLETT = LINE(I:I)
         ITRY = ICHAR (XLETT)
         IF (XLETT .GE. 'a' .AND. XLETT .LE. 'z') THEN 
            ITRY = ITRY - 32
            LINE(I:I) = CHAR (ITRY)
         ENDIF
10    CONTINUE
C
      UPCSE  = LINE 
C
      RETURN
C
      END
