C     Last change:  TJD  27 JUNE 2006   3:24 pm
C     --------------------
      SUBROUTINE SETFILES1
C     --------------------
C
C     SUBROUTINE AUTHORS:
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 IDENTIFIES THE FILES FOR A SIMULATION, CHECKS THE
C     IDENTIFICATION OF THE REQUIRED FILES, AND CHECKS FOR THE
C     EXISTANCE OF THE REQUIRED INPUT FILES. THIS SUBROUTINE IS CALLED
C     ONLY ONCE BY THE MAIN PROGRAM.
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     FLIST       NAME OF SUPER FILE.
C     FTYPE       FILE TYPE CODE.
C     IRSUP       SUPERFILE UNIT ASSIGNMENT FOR FLOW.
C     OUTPUT      NAME OF OUTFILE FOR FILE TYPE.
C     OUTEXT      EXTENSION FOR OUTPUT FILE.
C     XFNAME      TEMPORARY HOLDER FOR INPUT FILE NAME.
C     FILEFOUND   FLAG INDICATING WHETHER INPUT FILE EXISTS.
C     ENDFOUND    FLAG INDICATING WHETHER SUPER FILE ENDED PROPER CODE.
C     ERRORFOUND  FLAG INDICATING WHEREER ERROR FOUND IN SUPER FILE.
C
      CHARACTER*80 FLIST,XFNAME,OUTPUT,OUTEXT
      CHARACTER*3 FTYPE
      LOGICAL ERRORFOUND,FILEFOUND,ENDFOUND
C
C     COMMON BLOCKS.
C     FNAME()     NAMES OF INPUT FILES.
C     RNAME1      SIMULATION NAME FOR GROUNDWATER-FLOW FILES.
C     RNAME2      SIMULATION NAME FOR SOLUTE-TRANSPORT FILES.
C
      CHARACTER*12 RNAME1,RNAME2
      CHARACTER*80 FNAME(30)
      COMMON /NAMES/ FNAME,RNAME1,RNAME2
C
C     OPEN FLOW SUPER FILE.
      WRITE(*,900)
      READ(*,*) FLIST
      OUTEXT='.SUP.OUT'
      CALL OPENFILES(RNAME1,FLIST,OUTPUT,OUTEXT,IRSUP,0)
      READ(IRSUP,910) RNAME1
      WRITE(*,*)
      WRITE(*,*)'MODEL RUN NAME FOR FLOW:'
      WRITE(*,*) RNAME1
      WRITE(*,*)
C
C     INITIALIZE FILE NAMES.
      DO I=1,30
          FNAME(I)='NOTUSED'
      END DO
      ERRORFOUND=.FALSE.
C
C     INPUT FILE TYPES AND NAMES.
      WRITE(*,901)
      DO
          ENDFOUND=.TRUE.
          READ(IRSUP,902,END=101) FTYPE,XFNAME
          WRITE(*,903) FTYPE,XFNAME
          ENDFOUND=.FALSE.
101       CONTINUE
C
C         NAME OF BASIC INPUT FILE.
          IF (FTYPE.EQ.'BAS') THEN
              FNAME(1)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF NODE FILE.
          ELSEIF (FTYPE.EQ.'XYZ') THEN
              FNAME(2)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF ELEMENT FILE.
          ELSEIF (FTYPE.EQ.'ELE') THEN
              FNAME(3)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF PARAMETER FILE.
          ELSEIF (FTYPE.EQ.'PAR') THEN
              FNAME(4)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF INITIAL-HEADS FILE.
          ELSEIF (FTYPE.EQ.'INT') THEN
              FNAME(5)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF HEAD OUTPUT CONTROL FILE.
          ELSEIF (FTYPE.EQ.'HED') THEN
              FNAME(6)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF SPECIFIED-HEADS FILE.
          ELSEIF (FTYPE.EQ.'CHD') THEN
              FNAME(7)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF SPECIFIED-FLUXES FILE.
          ELSEIF (FTYPE.EQ.'FLX') THEN
              FNAME(8)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF GROUNDWATER-EVAPOTRANSPIRATION FILE.
          ELSEIF (FTYPE.EQ.'GET') THEN
              FNAME(9)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF RIVERS FILE.
          ELSEIF (FTYPE.EQ.'RIV') THEN
              FNAME(10)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF FAULTS FILE.
          ELSEIF (FTYPE.EQ.'FLT') THEN
              FNAME(11)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF FAULT INITIAL-HEADS FILE.
          ELSEIF (FTYPE.EQ.'FNT')THEN
              FNAME(12)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF(.NOT.FILEFOUND)THEN
                  WRITE(*,908)XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF MATRIX FILE.
          ELSEIF (FTYPE.EQ.'PAK')THEN
              FNAME(13)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF(.NOT.FILEFOUND)THEN
                  WRITE(*,908)XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         FOUND END-OF-FILE FLAG.
          ELSEIF (FTYPE.EQ.'END') THEN
              EXIT
C
C         FILE TYPE NOT RECOGNIZEABLE.
          ELSE
              WRITE(*,904)
              ERRORFOUND=.TRUE.
          ENDIF
      END DO
C
C     CHECK FOR REQUIRED FILES.
C     CHECK FOR INCLUSION OF BASIC FILE.
      IF (FNAME(1).EQ.'NOTUSED') THEN
          WRITE(*,905) 'BAS'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF NODES FILE.
      IF (FNAME(2).EQ.'NOTUSED') THEN
          WRITE(*,905) 'XYZ'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF ELEMENTS FILE.
      IF (FNAME(3).EQ.'NOTUSED') THEN
          WRITE(*,905) 'ELE'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF PARAMETER FILE.
      IF (FNAME(4).EQ.'NOTUSED') THEN
          WRITE(*,905) 'PAR'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF INITIAL-HEADS FILE.
      IF (FNAME(5).EQ.'NOTUSED') THEN
          WRITE(*,905) 'INT'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF HEAD OUPTUT CONTROL FILE.
      IF (FNAME(6).EQ.'NOTUSED') THEN
          WRITE(*,905) 'HED'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR FAULT INITIAL HEADS.
      IF (FNAME(11).NE.'NOTUSED') THEN
          IF(FNAME(12).EQ.'NOTUSED')THEN
              WRITE(*,905)'FNT'
              ERRORFOUND=.TRUE.
          ENDIF
      ENDIF
C
C     CHECK FOR END-OF-FILE ENCOUNTERED.
      IF (ENDFOUND) THEN
          WRITE(*,909)
          ERRORFOUND=.TRUE.
      ENDIF
C
C     TERMINATE SIMULATION IF ERROR IN RUN SUPER FILE.
      IF (ERRORFOUND) THEN
          WRITE(*,906)
          STOP
      ENDIF
      RETURN
C
C     FORMATS.
900   FORMAT(1X,'ENTER NAME FOR FLOW SUPER FILE: ')
901   FORMAT(1X,'FLOW INPUT FILES:'/
     +    1X,'TYPE',6X,'NAME')
902   FORMAT(A3,6X,A60)
903   FORMAT(1X,A3,6X,A60)
904   FORMAT(1X,'*ERROR* FILE TYPE NOT RECOGNIZED')
905   FORMAT(1X,'*ERROR* REQUIRED FILE MISSSING ',A3)
906   FORMAT(1X,'*ERROR* SIMULATION TERMINATED FOR ERROR IN SETFILE')
908   FORMAT(1X,'*ERROR* FILE NOT FOUND ',A80)
909   FORMAT(1X,'*ERROR* NO END FLAG IN FILE')
910   FORMAT(A12)
C
C     ---------------
      ENTRY SETFILES2
C     ---------------
C
C     OPEN TRANSPORT SUPER FILE.
      WRITE(*,911)
      READ(*,*) FLIST
      OUTEXT='.SUP.OUT'
      CALL OPENFILES(RNAME2,FLIST,OUTPUT,OUTEXT,IRSUP,0)
      READ(IRSUP,910) RNAME2
      WRITE(*,*)
      WRITE(*,*)'MODEL RUN NAME FOR TRANSPORT:'
      WRITE(*,*) RNAME2
      WRITE(*,*)
C
C     CHECK THAT RUN NAMES ARE DIFFERENT.
      IF (RNAME1.EQ.RNAME2) THEN
          WRITE(*,913)
          WRITE(*,906)
          STOP
      ENDIF
C
C     INITIALIZE FILE NAMES.
      DO I=16,30
          FNAME(I)='NOTUSED'
      END DO
      ERRORFOUND=.FALSE.
C
C     INPUT FILE TYPES AND NAMES.
      WRITE(*,912)
      DO
          ENDFOUND=.TRUE.
          READ(IRSUP,902,END=102) FTYPE,XFNAME
          WRITE(*,903) FTYPE,XFNAME
          ENDFOUND=.FALSE.
102       CONTINUE
C
C         NAME OF PARAMETER FILE.
          IF (FTYPE.EQ.'PAR') THEN
              FNAME(16)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF INITIAL-HEADS FILE.
          ELSEIF (FTYPE.EQ.'INT') THEN
              FNAME(17)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF HEAD OUTPUT CONTROL FILE.
          ELSEIF (FTYPE.EQ.'CON') THEN
              FNAME(18)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF SPECIFIED-HEADS FILE.
          ELSEIF (FTYPE.EQ.'CHD') THEN
              FNAME(19)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF SPECIFIED-FLUXES FILE.
          ELSEIF (FTYPE.EQ.'FLX') THEN
              FNAME(20)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF RIVERS FILE.
          ELSEIF (FTYPE.EQ.'RIV') THEN
              FNAME(21)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF FAULTS FILE.
          ELSEIF (FTYPE.EQ.'FLT') THEN
              FNAME(22)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         NAME OF FAULT INITIAL-CONCENTRATION FILE.
          ELSEIF (FTYPE.EQ.'FNT') THEN
              FNAME(23)=XFNAME
              INQUIRE(FILE=XFNAME,EXIST=FILEFOUND)
              IF (.NOT.FILEFOUND) THEN
                  WRITE(*,908) XFNAME
                  ERRORFOUND=.TRUE.
              ENDIF
C
C         FOUND END-OF-FILE FLAG.
          ELSEIF (FTYPE.EQ.'END') THEN
              EXIT
C
C         FILE TYPE NOT RECOGNIZEABLE.
          ELSE
              WRITE(*,904)
              ERRORFOUND=.TRUE.
          ENDIF
      END DO
C
C     CHECK FOR INCLUSION OF PARAMETER FILE.
      IF (FNAME(16).EQ.'NOTUSED') THEN
          WRITE(*,905) 'PAR'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF INITIAL CONCENTRATION FILE.
      IF (FNAME(17).EQ.'NOTUSED') THEN
          WRITE(*,905) 'INT'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF CONCENTRATION OUPTUT CONTROL FILE, IF REQUIRED.
      IF (FNAME(18).EQ.'NOTUSED') THEN
          WRITE(*,905) 'HED'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF SPECIFIED HEAD FILE FILE, IF REQUIRED.
      IF (FNAME(19).EQ.'NOTUSED' .AND. FNAME(7).NE.'NOTUSED') THEN
          WRITE(*,905) 'CHD'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF SPECIFIED FLUX FILE, IF REQUIRED.
      IF (FNAME(20).EQ.'NOTUSED' .AND. FNAME(8).NE.'NOTUSED') THEN
          WRITE(*,905) 'FLX'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF RIVER FILE, IF REQUIRED.
      IF (FNAME(21).EQ.'NOTUSED' .AND. FNAME(10).NE.'NOTUSED') THEN
          WRITE(*,905) 'RIV'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF FAULT FILE, IF REQUIRED.
      IF (FNAME(22).EQ.'NOTUSED' .AND. FNAME(11).NE.'NOTUSED') THEN
          WRITE(*,905) 'FLT'
          ERRORFOUND=.TRUE.
      ENDIF
C
C     CHECK FOR INCLUSION OF FAULT INITIAL-CONCENTRATION FILE, IF REQUIRED.
      IF (FNAME(22).NE.'NOTUSED') THEN
          IF (FNAME(23).EQ.'NOTUSED') THEN
              WRITE(*,905) 'FNT'
              ERRORFOUND=.TRUE.
          ENDIF
      ENDIF
C
C     CHECK FOR END-OF-FILE ENCOUNTERED.
      IF (ENDFOUND) THEN
          WRITE(*,909)
          ERRORFOUND=.TRUE.
      ENDIF
C
C     TERMINATE SIMULATION IF ERROR IN RUN SUPER FILE.
      IF (ERRORFOUND) THEN
          WRITE(*,906)
          STOP
      ENDIF
C
C     FORMATS:
911   FORMAT(1X,'ENTER NAME FOR TRANSPORT SUPER FILE: ')
912   FORMAT(1X,'TRANSPORT INPUT FILES:'/
     +    1X,'TYPE',6X,'NAME')
913   FORMAT(1X,'*ERROR* FLOW AND TRANSPORT RUN NAMES THE SAME')
      RETURN
      END SUBROUTINE
