C----------------------------------------------------------------------
C-       9-APR-1991 
C-   Modified from D0$OFFLINE_UTIL:D0OPEN.FOR:
C-      Remove calls to compack, for inclusion in TRICS
C-
C----------------------------------------------------------------------
      SUBROUTINE D0OPEN(LUN,FILNAM,CHOPT,OK)
C----------------------------------------------------------------------
C-
C-   Purpose and Methods : Open a file (machine independent)
C-
C-   Inputs  :
C-   LUN   = unit number
C-   FILNAM= file name
C-   CHOPT = character option
C-           'I' input  (VAX status OLD, READONLY)
C-           'O' output (VAX status NEW)
C-           'A' append (this option may not be available on all machines) 
C-           'F' formatted
C-           'U' unformatted
C-           'D' delete after close (only allowed combined with O,
C-                                   i.e. 'OD' or 'DO')
C-           combinations allowed but some are illegal (i.e. 'UF')
C-           defaults 'I' and 'F' (i.e. ' ' equivalent to 'IF' or 'FI')
C-   Outputs :
C-   OK    = set to false if there is a problem opening file
C-           true otherwise
C-
C-     Serban D. Protopopescu Nov. 2,1988
C-
C----------------------------------------------------------------------
C    
      IMPLICIT NONE
      INTEGER LUN,L,IOPT,I
      CHARACTER*(*) FILNAM,CHOPT
      CHARACTER*4 STATUS
      CHARACTER*12 FORM
      LOGICAL OK
C----------------------------------------------------------------------
C
      L=LEN(CHOPT)
      OK=.TRUE.
      STATUS='OLD'
      FORM='FORMATTED'
C
C       find options
C
      IF(L.EQ.0.OR.CHOPT.EQ.' ') THEN
        IOPT=1
      ELSE
        IOPT=0
C
        DO 1 I=1,L
          IF(CHOPT(I:I).EQ.'U') FORM='UNFORMATTED'
    1   CONTINUE
C
        DO 2 I =1,L
          IF(CHOPT(I:I).EQ.'F') THEN
            IF(FORM.EQ.'UNFORMATTED') GOTO 200
          ENDIF
    2   CONTINUE
C
        DO 3 I =1,L
          IF(CHOPT(I:I).EQ.'I') IOPT=1
    3   CONTINUE
C
        DO 4 I=1,L
          IF(CHOPT(I:I).EQ.'O') THEN
            IF(IOPT.EQ.0) STATUS='NEW'
            IOPT=2
          ENDIF
    4   CONTINUE
C
        DO 5 I=1,L
          IF(CHOPT(I:I).EQ.'A') THEN
            IOPT=3
          ENDIF
    5   CONTINUE
C
        DO 6 I=1,L
          IF(CHOPT(I:I).EQ.'D') THEN
            IOPT=4
          ENDIF
    6   CONTINUE
C
      ENDIF
C
C         now open file (this section is machine dependent)
C
      IF(IOPT.LT.2) THEN
        OPEN(UNIT=LUN,FORM=FORM,STATUS=STATUS,READONLY,
     &      FILE=FILNAM,ERR=100)
      ELSEIF(IOPT.EQ.2) THEN
        OPEN(UNIT=LUN,FORM=FORM,STATUS=STATUS,
     &      FILE=FILNAM,ERR=100)
      ELSEIF(IOPT.EQ.3) THEN
        OPEN(UNIT=LUN,FORM=FORM,STATUS=STATUS,ACCESS='APPEND',
     &      FILE=FILNAM,ERR=100)
      ELSEIF(IOPT.EQ.4) THEN
        OPEN(UNIT=LUN,FORM=FORM,STATUS=STATUS,DISPOSE='DELETE',
     &      FILE=FILNAM,ERR=100)
      ENDIF
C
  999 RETURN
C
C           handle errors
C
  100 OK=.FALSE.
      RETURN
  200 CONTINUE
      END
