C
C SUBROUTINE 'GETOPT'
C ===================
C
C CHECK AN INPUT OPTION AGAINST A LIST OF POSSIBLE OPTIONS. ONLY
C THE NUMBER OF CHARACTERS IN THE INPUT OPTION STRING ARE COMPARED
C AND IF THEY MATCH AN ENTRY IN THE OPTION LIST THEN THAT OPTION
C IS SELECTED UNLESS THE SAME STRING MATCHES MORE THAN ONE OPTION
C
      SUBROUTINE GETOPT(OPTS,NOPT,STRING,IOP,IERR)
C
C PARAMETERS
C
C        OPTS (I)   CHARACTER ARRAY DIMENSIONED FOR NOPT CONTAINING
C                   THE OPTIONS LIST. THE ELEMENT LENGTH IN THE
C                   ARRAY CAN HAVE ANY VALID VALUE                      
C        NOPT (I)   THE NUMBER OF OPTIONS IN THE OPTIONS LIST
C      STRING (I)   A CHARACTER VARIABLE CONTAINING THE OPTION TO
C                   BE LOOKED FOR IN THE OPTIONS LIST. ANY TRAILING
C                   BLANKS IN THIS STRING WILL BE IGNORED.
C         IOP (O)   THE NUMBER IN THE LIST OF THE REQUESTED OPTION
C                   (=0 IF NOT FOUND)
C        IERR (O)   ERROR FLAG = 0 OPTION FOUND
C                              = 1 AMBIGUOUS OPTION
C                              = 2 OPTION NOT FOUND
C
C SPECIFICATION STATEMENTS
C ------------------------
C
      LOGICAL FOUND
      CHARACTER*(*) OPTS(NOPT),STRING
C
C INITIALISATIONS
C ---------------
C
      IERR=0
      FOUND=.FALSE.
      IOP=0
C
C SEARCH FOR MATCHING OPTION
C --------------------------
C
      LL=LEN(OPTS(1))
      LS=LEN(STRING)
      DO 10 MM=LS,1,-1
         IF (STRING(MM:MM).NE.' ') GO TO 20
10    CONTINUE
      MM=1
20    IF(MM.GT.LL)MM=LL
      DO 50 N=1,NOPT
      IF(STRING(1:MM).EQ.OPTS(N)(1:MM))THEN
        IF(FOUND)THEN
          IOP=0
          IERR=1
          RETURN
        ELSE
          IOP=N
          FOUND=.TRUE.
        ENDIF
      ENDIF
50    CONTINUE
      IF(FOUND)RETURN
      IOP=0
      IERR=2
      RETURN
      END
