C
C File 'crtin.for' for reading the input data for complete ray tracing
C
C Date: 1997, September 25
C Coded by Ludek Klimes
C
C.......................................................................
C
C This file consists of:
C     CRTIN...Subroutine designed to open the data files for complete
C             ray tracing and to read the input data sets CRT, MODEL,
C             DCRT and INIT.
C             CRTIN
C     UNIT... Subroutine designed to control connecting and
C             disconnecting the data files to logical units, and to
C             determine the logical units from which the given data sets
C             are to be read.  Called e.g. by the subroutine CRTIN.
C             UNIT
C
C.......................................................................
C
C                                                    
C Description of data files:
C
C Input data - main data set CRT:
C     This main data file contains the names of other input files and
C     the name of the output log file.  It may or may not contain other
C     input data.  The names of the output files with the computed
C     quantities (C.R.T.5.5) are specified in the subroutine file
C     'writ.for'.
C     The data are read in by the list directed input (free format).  In
C     the list of input data below, each numbered paragraph indicates
C     the beginning of a new input operation (new READ statement).  All
C     input variables are of the type CHARACTER.  Only the first 80
C     characters of the strings are significant.
C (1) TEXT
C     The string describing the input data.
C (2) FILE(2)...data set MODEL
C     The string containing the name of the file with the input data for
C     the model.  The data will be read in by the subroutine MODEL1.
C     If FILE(2) is blank, data set MODEL is appended to this file.
C     Description of data MODEL 
C     It is recommended to define only surfaces covering structural
C     interfaces (MODEL SURFACES) in data set MODEL, and to define
C     AUXILIARY SURFACES in data set DCRT.
C (3) FILE(3)...data set DCRT
C     The string containing the name of the file with the input data for
C     the complete ray tracing.  The data will be read in by the
C     subroutine RAY1.
C     If FILE(3) is blank, data set DCRT is appended to this file.
C     Description of data set DCRT
C (4) FILE(4)...data set INIT
C     The string containing the name of the file with the input data
C     specifying the initial conditions for rays.  The data will be read
C     in by the subroutine INIT1.
C     If FILE(4) is blank, data set INIT is appended to this file.
C     Description of data set INIT
C (5) FILE(5)...data set CODE
C     The string containing the name of the file with the codes of
C     elementary waves.  The data will be read in by the subroutine
C     CODE1.
C     If FILE(4) is blank, data set CODE is appended to this file.
C     It is recommended to append at most one of sets CODE, RPAR, WRIT.
C     Description of data set CODE
C (6) FILE(5)...data set RPAR
C     The string containing the name of the file with the data
C     specifying the take-off parameters of the required rays.  The data
C     will be read in by the subroutine RPAR1.
C     If FILE(5) is blank, data set CODE is appended to this file.
C     It is recommended to append at most one of sets CODE, RPAR, WRIT.
C     Description of data set RPAR
C (7) FILE(6)...data set WRIT
C     The string containing the name of the file specifying the names of
C     the output files with the computed quantities.  These data will be
C     read by the subroutine WRIT1.
C     If FILE(6) is blank, data set WRIT is appended to this file.
C     It is recommended to append at most one of sets CODE, RPAR, WRIT.
C     Description of data set WRIT
C (8) FILE(6)...data set LOG
C     The string containing the name of the output log file.  The data
C     will be written by the subroutines WRIT1, WRIT2, WRIT4 and WRIT5.
C The filenames FILE(1) to FILE(8) need not be mutually different,
C several data sets may be read from (or written to) the same data file.
C Each data file is closed when read over, and its logical unit number
C may be connected to another file to be opened.  When more than one
C elementary wave is computed, it is not recommended to write the LOG
C output data set to the file containing the CODE, RPAR or WRIT data
C set.
C Example of data CRT all data sets separated
C Example of data CRT with DCRT and INIT
C
C=======================================================================
C
C     
C
      SUBROUTINE CRTIN(FILE1,LUCODE,LURPAR,LUWRIT,LULOG)
      CHARACTER*(*) FILE1
      INTEGER LUCODE,LURPAR,LUWRIT,LULOG
C
C Subroutine CRTIN is designed to open the data files for complete ray
C tracing and to read the input data sets CRT, MODEL, DCRT and INIT.
C
C Input:
C     FILE1...The name of the main input data file CRT.  The file is
C             opened with the logical unit number LU(1)=5 defined in
C             this subroutine.  The name may be blank to use
C             preconnected input device.  Note that also logical units
C             LU(2)=4, LU(3)=3 and LU(4)=2 may be used to connect other
C             input data files always having non-blank filenames.
C
C Output:
C     LUCODE..The logical unit connected to the file with the CODE data.
C     LURPAR..The logical unit connected to the file with the RPAR data.
C     LUWRIT..The logical unit connected to the file with the WRIT data.
C     LULOG...The logical unit connected to the output LOG file.
C
C Subroutines and external functions required:
      EXTERNAL MODEL1,RAY1,INIT1,UNIT
C     MODEL1..File 'model.for' of the package 'model'.
C     RAY1... File 'ray.for'.
C     INIT1...File 'init.for'.
C     UNIT... This file.
C Note that the above subroutines reference many other external
C procedures from various subroutine files.  These indirectly
C referenced procedures are not named here, but are listed in the
C particular subroutine files.
C
C Date: 1997, September 6
C Coded by Ludek Klimes
C
C-----------------------------------------------------------------------
C
C     Local storage locations:
C
C     The name of the data:
      CHARACTER*80 TEXTC
C     TEXTC...String of 80 characters containing input data (1).
C
C     Auxiliary storage locations:
      INTEGER I
C     I...    Auxiliary loop variable
C
C     Quantities describing data files and logical units:
      INTEGER NFILE,IU,NU
      PARAMETER (NFILE=8)
      CHARACTER*80 FILE(NFILE)
      PARAMETER (NU=4)
      INTEGER LU(NU),KU(NU)
      DATA LU/5,4,3,2/
C     NFILE, FILE, IU, NU, LU, KU... For the description of these
C             quantities see the subroutine unit below.
C
C.......................................................................
C
C     The name of the main input file.  This file contains the names of
C     other input files
      FILE(1)=FILE1
      IF(NU.LT.4) THEN
C       102
        PAUSE 'Error 102 in CRTIN: Less than 4 logical units'
        STOP
C       Four logical units must be available to read the input data and
C       to write the output log file.
      END IF
C
C     (1) Main data file - contains the names of other input files
      CALL UNIT(1,NFILE,FILE,IU,NU,LU,KU,'OLD')
      READ(LU(IU),*) TEXTC
      DO 10 I=2,NFILE
        READ(LU(IU),*) FILE(I)
   10 CONTINUE
C
C     (2) Data for model
      CALL UNIT(2,NFILE,FILE,IU,NU,LU,KU,'OLD')
      CALL MODEL1(LU(IU))
C
C     (3) Data for complete ray tracing
      CALL UNIT(3,NFILE,FILE,IU,NU,LU,KU,'OLD')
      CALL RAY1(LU(IU))
C
C     (4) Data for initial points of rays
      CALL UNIT(4,NFILE,FILE,IU,NU,LU,KU,'OLD')
      CALL INIT1(LU(IU))
C
C     (5) File containing the codes of elementary waves
      CALL UNIT(5,NFILE,FILE,IU,NU,LU,KU,'OLD')
C     The data file for the subroutine CODE1 remains open
      LUCODE=LU(IU)
      IU=0
C
C     (6) File controlling the take-off parameters of rays
      CALL UNIT(6,NFILE,FILE,IU,NU,LU,KU,'OLD')
C     The data file for the subroutine RPAR1 remains open
      LURPAR=LU(IU)
      IU=0
C
C     (7) File specifying the names of files with the computed
C     quantities
      CALL UNIT(7,NFILE,FILE,IU,NU,LU,KU,'OLD')
C     The data file for the subroutine WRIT1 remains open
      LUWRIT=LU(IU)
      IU=0
C
C     (8) The output LOG file
      CALL UNIT(8,NFILE,FILE,IU,NU,LU,KU,'UNKNOWN')
C     The output file for the subroutines WRIT1, WRIT2, WRIT4 and WRIT5
C     remains open
      LULOG=LU(IU)
C
      RETURN
      END
C
C=======================================================================
C
C     
C
      SUBROUTINE UNIT(IFILE,NFILE,FILE,IU,NU,LU,KU,STATUS)
      INTEGER IFILE,NFILE,IU,NU,LU(NU),KU(NU)
      CHARACTER*(*) FILE(NFILE),STATUS
C
C Subroutine UNIT is designed to control connecting and disconnecting
C the data files to logical units, and to determine the logical units
C from which the given data sets are to be read.
C
C Input:
C     IFILE...Index of the data set to be read in.  The data sets are
C             indexed by integers from 1 to NFILE.
C     NFILE...The total number of data sets.
C     FILE... Character array containing the names of the files
C             containing individual data sets.  Different data sets may
C             be stored in the same file.  If IFILE=1, only FILE(1) has
C             to be defined.
C     IU...   Index of the logical unit connected to the file containing
C             the last read data set (i.e. the last data set was read
C             from the logical unit LU(IU) connected to the file
C             FILE(KU(IU)).  Zero if no data are read in, or if there is
C             no data file to close.  Need not be defined if IFILE=1.
C     NU...   The maximum number of available logical units.
C     LU...   Array containing reference numbers of logical units.
C     KU...   Auxiliary array of the dimension at least NU.
C             Its elements KU(1) to KU(NU) must not be modified between
C             two invocations of this subroutine.  Its values need not
C             be defined if IFILE=1.
C             KU(I)...Zero if the logical unit LU(I) is closed,
C               otherwise the sequential number of the next data set to
C               be read from this unit.
C
C Output:
C     IU...   Index of the logical unit connected to file with the data
C             set to be read in (i.e. the next data set will be read
C             from the logical unit LU(IU) connected to the file
C             FILE(IFILE)).  Zero if no logical unit is available.
C     KU...   Updated input values.
C
C Date: 1997, September 6
C Coded by Ludek Klimes
C
C-----------------------------------------------------------------------
C
C     Auxiliary storage locations:
      INTEGER IERR,JU,I
C
      IF(IFILE.EQ.1) THEN
C       No logical units are connected when opening the first data file
        DO 10 JU=1,NU
          KU(JU)=0
   10   CONTINUE
      ELSE
C       Updating indices of next data sets to be read from open files:
        DO 13 JU=1,NU
          IF(0.LT.KU(JU).AND.KU(JU).LT.IFILE) THEN
C           The data set from the file connected to the logical unit
C           LU(JU) has been read.  Determining the next data set
C           contained in the file:
            DO 11 I=IFILE,NFILE
              IF(FILE(KU(JU)).EQ.FILE(I).OR.
     *                            (KU(JU).EQ.1.AND.FILE(I).EQ.' ')) THEN
C               The I-th data set will also be read from the last file
C               connected to the logical unit LU(JU)
                KU(JU)=I
                GO TO 12
              END IF
   11       CONTINUE
C           No other data set will be read from the last file.  The file
C           may be closed and the logical unit LU(IU) disconnected
   12       CONTINUE
          END IF
   13   CONTINUE
C       Closing the data file:
        IF(IU.GT.0) THEN
C         There is a file submitted to be closed
          IF(0.LT.KU(IU).AND.KU(IU).LT.IFILE) THEN
C           No other data set will be read from this file.  The file
C           may be closed and the logical unit LU(IU) disconnected
            CLOSE(LU(IU))
            KU(IU)=0
          END IF
        END IF
      END IF
C
C     Opening new data file:
      IF(IFILE.GT.0) THEN
        DO 21 JU=1,NU
          IF(KU(JU).EQ.IFILE) THEN
C           The data file is already open
            IU=JU
            RETURN
          END IF
   21   CONTINUE
C       The data file has to be opened
        DO 22 JU=1,NU
          IF(KU(JU).EQ.0) THEN
C           The logical unit LU(JU) may be connected to the data file
            IU=JU
            KU(IU)=IFILE
            IF(FILE(IFILE).EQ.' ') THEN
              OPEN(LU(IU),FILE=' ',STATUS='OLD',IOSTAT=IERR,ERR=90)
            ELSE
              OPEN(LU(IU),FILE=FILE(IFILE)
     *                           ,STATUS=STATUS,IOSTAT=IERR,ERR=90)
            END IF
            RETURN
          END IF
   22   CONTINUE
C       No logical unit available
        IU=0
      END IF
      RETURN
C
   90 CONTINUE
C       101
        WRITE(*,'('' STATUS'',I5,'': '',A)') IERR,FILE(IFILE)
        PAUSE 'Error 101 in UNIT: Open file error'
        STOP
C       Error encountered during execution of the OPEN statement.
      END
C
C======================================================================
C