C
C     -------------------------------------------------------
      SUBROUTINE OPENFILES(RNAME,INFILE,OUTFILE,OUTEXT,IR,IW)
C     -------------------------------------------------------
C
C     SUBROUTINE AUTHOR:
C     TIM DURBIN
C     TIMOTHY J. DURBIN, INC.
C     5330 PRIMROSE DRIVE, SUITE 228
C     FAIR OAKS, CA 95628
C     (916) 536-2314
C
C     PURPOSE:
C     THIS SUBROUTINE OPENS INPUT AND OUTPUT FILES, AND IT CREATES THE
C     NAME OF THE OUTPUT FILE, BASED ON THE SIMULATION NAME AND A
C     SPECIFIED EXTENSION. THIS SUBROUTINE IS CALLED ONE OR MORE TIMES
C     BY THE MAIN PROGRAM AND THE SUBROUTINES BASIC, NODES, ELEMS,
C     WPARAMS1, WSTART1, WCHEAD1, WFLUX1, WEVAP1, WRIVER1, WFAULT1, AND
C     WOUTPUT1.
C
      INCLUDE 'DIMEN.INC'
      INCLUDE 'WUNITS.INC'
      IMPLICIT REAL*4 (A-H,O-Z)
      IMPLICIT INTEGER*4 (I-N)
C
C     INTERNAL SCALARS AND ARRAYS:
C     INFILE      NAME OF INPUT FILE.
C     OUTFILE     NAME OF OUTPUT FILE.
C     OUTEXT      EXTENSION FOR OUTPUT FILE.
C     RNAME       NAME OF SIMULATION.
C     IR          UNIT NUMBER FOR INPUT FILE.
C     IW          UNIT NUMBER FOR OUTPUT FILE.
C
      CHARACTER*80 INFILE,OUTFILE,OUTEXT
      CHARACTER*12 RNAME
C
C     OPEN INOUT FILE.
      IF (IR.NE.0) THEN
          OPEN(UNIT=IR,FILE=INFILE,STATUS='OLD')
      END IF
C
C     OPEN OUTPUT FILE.
      IF (IW.NE.0) THEN
C
C         FIND LENGTH OF SIMULATION NAME AND COPY TO OUTPUT FILE NAME.
          DO I=1,12
              ILAST=12
              IF (RNAME(I:I).EQ.' ') THEN
                  ILAST=I-1
                  EXIT
              ENDIF
          END DO
          J=0
          DO I=1,ILAST
              J=J+1
              OUTFILE(J:J)=RNAME(I:I)
          END DO
C
C         ADD EXTENSION TO OUTPUT FILE.
          DO I=1,20
              J=J+1
              OUTFILE(J:J)=OUTEXT(I:I)
          END DO
C
C         OPEN FILE.
          OPEN(UNIT=IW,FILE=OUTFILE(1:J),STATUS='UNKNOWN')
      ENDIF
      RETURN
      END SUBROUTINE
