C......................................................................
C     SUB OPEN_FILE
      SUBROUTINE OPEN_FILE(IUNIT,INNAME,CSTRING,LERROR)
C     IMPLICIT NONE
C INPUT
C CSTATUS: 'OLD' OR 'NEW' OR 'UNKNOWN'
C CACCESS:  'APPEND' 'DIRECT'
C FORM:     'FORMATTED' OR 'UNFORMATTED'
C IRECLEN: RECORD LENGTH
C NOTE: AFTER OPENING A "OLD" OR "UNKNOWN" FILE (NO DIRECT ACESS): 
C       REWIND THE FILE, BECAUSE SOME STRANGE COMPILERS PUT THE FILE 
C       POINTER AT THE END !
C
      CHARACTER*(*) INNAME,CSTRING
      INTEGER IUNIT,IRECLEN
c output: lerror is true if open error
      LOGICAL LERROR
c internal
      CHARACTER*200 TEMPSTRING,CTEMP,FILENAME
      CHARACTER*10 CNUMBER
      LOGICAL LNEW,LAPPEND,LUNKNOWN
      LOGICAL LUNFORMATTED,LDIRECT
      LOGICAL LOPENDONE,LSILENT
      INTEGER LENGTH,I,J,K
c init
      TEMPSTRING=' '
      FILENAME=' '
      LNEW=.FALSE.
      LAPPEND=.FALSE.
      LERROR=.FALSE.
      LUNKNOWN=.FALSE.
      LUNFORMATTED=.FALSE.
      LDIRECT=.FALSE.
      LOPENDONE=.FALSE.
      LSILENT=.FALSE.
      IRECLEN=137
      TEMPSTRING(1:)=CSTRING(1:)
      CNUMBER='0123456789'
c
      LENGTH=LEN(TEMPSTRING)
      CALL LOWTOUP(TEMPSTRING,LENGTH)
      IF (INDEX(TEMPSTRING,'NEW').NE.0) THEN
         LNEW=.TRUE.
      ENDIF
      IF (INDEX(TEMPSTRING,'UNKNOWN').NE.0) THEN
         LUNKNOWN=.TRUE.
      ENDIF
      IF (INDEX(TEMPSTRING,'UNFORMATTED').NE.0) THEN
         LUNFORMATTED=.TRUE.
      ENDIF
      IF (INDEX(TEMPSTRING,'DIRECT').NE.0) THEN
         LDIRECT=.TRUE.
      ENDIF
      IF (INDEX(TEMPSTRING,'APPEND').NE.0) THEN
         LAPPEND=.TRUE.
      ENDIF
      IF (INDEX(TEMPSTRING,'SILENT').NE.0) THEN
         LSILENT=.TRUE.
      ENDIF
      IF (INDEX(TEMPSTRING,'RECL=').NE.0) THEN
         CTEMP=' '
         K=INDEX(TEMPSTRING,'RECL=')+5
         CTEMP(1:)=TEMPSTRING(K:)
         CALL STRPOS(CTEMP,I,J)
         J=I
         DO WHILE (INDEX(CNUMBER,CTEMP(J:J)).NE.0 )
            J=J+1
         ENDDO
         J=J-1
         CALL READ_INT_FROM_STRING(CTEMP(I:J),IRECLEN)
      ENDIF

      CALL STRPOS(INNAME,IBEG,IEND)
      FILENAME(1:)=INNAME(IBEG:IEND)

      IF (LNEW) THEN
         CALL DEL_OLDFILE(IUNIT,FILENAME)
      ENDIF
      IF (LNEW .AND. LUNFORMATTED .AND. LDIRECT) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='NEW',FORM='UNFORMATTED',
     +        ACCESS='DIRECT',RECL=IRECLEN,ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='NEW',FORM='UNFORMATTED',
C     +	       ACCESS='DIRECT',RECL=IRECLEN)
         REWIND(IUNIT)
         LOPENDONE=.TRUE.
      ELSEIF (LNEW .AND. LUNFORMATTED ) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='NEW',FORM='UNFORMATTED',
     +        ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='NEW',FORM='UNFORMATTED')
         REWIND(IUNIT)
         LOPENDONE=.TRUE.
      ELSEIF (LNEW .AND. (.NOT. LUNFORMATTED) .AND. LDIRECT ) THEN
         OPEN(IUNIT,FILE=FILENAME,ACCESS='DIRECT',STATUS='NEW',
     +        FORM='FORMATTED',RECL=IRECLEN,ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,ACCESS='DIRECT',STATUS='NEW',
C     +         FORM='FORMATTED',RECL=IRECLEN)
         LOPENDONE=.TRUE.
      ELSEIF (.NOT. LNEW .AND. LUNFORMATTED .AND. LDIRECT) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',FORM='UNFORMATTED',
     +        ACCESS='DIRECT',RECL=IRECLEN,ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',FORM='UNFORMATTED',
C     +	       ACCESS='DIRECT',RECL=IRECLEN)
         LOPENDONE=.TRUE.
      ELSEIF (.NOT. LNEW .AND. .NOT. LUNFORMATTED .AND. LDIRECT) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',FORM='FORMATTED',
     +        ACCESS='DIRECT',RECL=IRECLEN,ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',FORM='FORMATTED',
C     +	       ACCESS='DIRECT',RECL=IRECLEN)
         LOPENDONE=.TRUE.
      ELSEIF (.NOT. LNEW .AND. LUNFORMATTED ) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',FORM='UNFORMATTED',
     +        ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',FORM='UNFORMATTED')
         REWIND(IUNIT)
         LOPENDONE=.TRUE.
      ELSEIF (LNEW) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='NEW',ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='NEW')
         REWIND(IUNIT)                         
         LOPENDONE=.TRUE.
      ELSEIF (LUNKNOWN .AND. LAPPEND) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='UNKNOWN',ACCESS='APPEND',
     +        ERR=999)
C	  OPEN(IUNIT,FILE=FILENAME,STATUS='UNKNOWN',ACCESS='APPEND')
C	  OPEN(IUNIT,FILE=FILENAME,STATUS='UNKNOWN')
         LOPENDONE=.TRUE.
      ELSEIF (.NOT. LNEW .AND. LAPPEND) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',ACCESS='APPEND',ERR=999)
C	  OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',ACCESS='APPEND')
         LOPENDONE=.TRUE.
      ELSEIF (.NOT. LNEW) THEN
         OPEN(IUNIT,FILE=FILENAME,STATUS='OLD',ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='OLD')
         REWIND(IUNIT)
         LOPENDONE=.TRUE.
      ELSE
         OPEN(IUNIT,FILE=FILENAME,STATUS='UNKNOWN',ERR=999)
C          OPEN(IUNIT,FILE=FILENAME,STATUS='UNKNOWN')
         REWIND(IUNIT)
         LOPENDONE=.TRUE.
      ENDIF
      IF (.NOT. LOPENDONE) THEN
         WRITE(*,*)' ERROR IN OPEN_FILE: FILE NOT OPENED'
         WRITE(*,*)'  unknown specifier combination !'
         STOP
      ENDIF
      RETURN
 999  IF (.NOT. LSILENT) THEN
         WRITE(*,*)' ERROR: open file error for file: '
         WRITE(*,*)'name: ',FILENAME
         WRITE(*,*)'unit: ',iunit
      ENDIF
      LERROR=.TRUE.
      RETURN
      END
C     END OPEN_FILE
C......................................................................

