!+ Subroutine to open a SAMSON-file for output.
!
! Method:
!	Straightforward.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	Version		Date			Comment
!	1.0		12-04-95		First Version under RCS
!						(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77  with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      SUBROUTINE OPEN_SAMSON_OUT(IERR, IUNIT, FILE_OUT)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     IUNIT
!             I/O UNIT NUMBER
      CHARACTER	!, INTENT(IN)
     &     FILE_OUT*80
!             NAME OF OUTPUT FILE
!
!     LOCAL VARAIBLES.
      INTEGER
     &     IOS
!             I/O ERROR FLAG
     &   , LENGTH
!             LENGTH OF FILE NAME
      LOGICAL
     &     L_OPEN
!             OPEN FLAG
     &   , L_EXIST
!             EXISTENCE FLAG
!
!     FUNCTIONS CALLED:
      INTEGER
     &     NAME_LENGTH
!             FUNCTION TO FIND LENGTH OF NAME
!
!
!
!     DETERMINE THE LENGTH OF THE FILE NAME
      LENGTH=NAME_LENGTH(FILE_OUT)
!
!     CHECK WHETHER THE UNIT FOR THE OUTPUT FILE IS ALREADY OPEN.
      INQUIRE(UNIT=IUNIT, OPENED=L_OPEN)
      IF (L_OPEN) THEN
         WRITE(IU_ERR, '(/, A16, I5, A)') '*** ERROR: UNIT ', IUNIT
     &      , ' IS ALREADY OPEN ELSEWHERE.'
         WRITE(IU_ERR, '(A, A)')  FILE_OUT(1: LENGTH)
     &      , 'WILL NOT BE WRITTEN.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     CHECK WHETHER THE FILE EXISTS.
100   INQUIRE(FILE=FILE_OUT(1: LENGTH), EXIST=L_EXIST)
      IF (L_EXIST) THEN
         INQUIRE(FILE=FILE_OUT(1: LENGTH), OPENED=L_OPEN)
         WRITE(IU_ERR, '(/A)') 'THIS FILE ALREADY EXISTS.'
         IF (L_OPEN) THEN
            WRITE(IU_ERR, '(A)') 'THIS FILE IS ALREADY OPEN.'
         ENDIF
         WRITE(IU_ERR, '(3A)') '*** ERROR: ', FILE_OUT
     &      , ' WILL NOT BE OVERWRITTEN.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!     FILE MAY NOW SAFELY BE OPENED.
      OPEN(UNIT=IUNIT, FILE=FILE_OUT(1: LENGTH), IOSTAT=IOS
     &   )
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(/A/)')
     &      '*** ERROR IN SUBROUTINE OPEN_SAMSON_OUT.'
         WRITE(IU_ERR, '(/, 3A)') '*** ERROR: FILE'
     &      , FILE_OUT(1: LENGTH), ' COULD NOT BE OPENED.'
         IERR=I_ERR_IO
         RETURN
      ENDIF
!
!
      RETURN
      END
