C FILE 'CRTIN.FOR' FOR READING THE INPUT DATA FOR COMPLETE RAY TRACING
C
C BY VLASTISLAV CERVENY, LUDEK KLIMES, IVAN PSENCIK
C
C THIS FILE CONSISTS OF:
C     CRTB... BLOCK DATA SUBROUTINE DEFINING COMMON BLOCK /CRTT/ TO
C             STORE THE TEXT DESCRIBING THE MAIN INPUT DATA SET CRT FOR
C             THE COMPLETE RAY TRACING PROGRAM.
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     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
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 (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 (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 (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 (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 (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 (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
C STORAGE IN THE MEMORY:
C     THE INPUT DATA (1) ARE STORED IN THE COMMON BLOCK /CRTT/ DEFINED
C     IN THE FOLLOWING SUBROUTINE:
C     ------------------------------------------------------------------
      BLOCK DATA CRTB
      CHARACTER*80 TEXTC
      COMMON/CRTT/ TEXTC
      SAVE  /CRTT/
      END
C     ------------------------------------------------------------------
C     TEXTC...THE NAME OF THE DATA. STRING OF 80 CHARACTERS.
C     COMMON BLOCK /CRTT/ IS INCLUDED IN EXTERNAL PROCEDURE CRTIN.
C
C DATE: 1991, MAY 21
C CODED BY LUDEK KLIMES
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 COMMON BLOCK /CRTT/:
      CHARACTER*80 TEXTC
      COMMON/CRTT/ TEXTC
C ALL THE STORAGE LOCATIONS OF THE COMMON BLOCK ARE DEFINED IN THIS
C SUBROUTINE.
C
C
C SUBROUTINES AND EXTERNAL FUNCTIONS REQUIRED:
      EXTERNAL CRTB,MODEL1,RAY1,INIT1,UNIT
C     CRTB... BLOCK DATA SUBROUTINE OF THIS FILE.
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 ERRORS:
C     102...  LESS THAN 4 LOGICAL UNITS:
C             FOUR LOGICAL UNITS MUST BE AVAILABLE TO READ THE INPUT
C             DATA AND TO WRITE THE OUTPUT LOG FILE.
C
C DATE: 1993, DECEMBER 18
C CODED BY LUDEK KLIMES
C
C-----------------------------------------------------------------------
C
C     LOCAL STORAGE LOCATIONS:
C
C     AUXILIARY STORAGE LOCATIONS:
      INTEGER I
C
C     I...    AUXILIARY LOOP VARIABLE
C
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
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
        PAUSE 'ERROR 102 IN CRTIN: LESS THAN 4 LOGICAL UNITS'
        STOP
      END IF
C
C     (1) MAIN DATA FILE - CONTAINS THE NAMES OF OTHER INPUT FILES
      CALL UNIT(1,NFILE,FILE,IU,NU,LU,KU)
      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)
      CALL MODEL1(LU(IU))
C
C     (3) DATA FOR COMPLETE RAY TRACING
      CALL UNIT(3,NFILE,FILE,IU,NU,LU,KU)
      CALL RAY1(LU(IU))
C
C     (4) DATA FOR INITIAL POINTS OF RAYS
      CALL UNIT(4,NFILE,FILE,IU,NU,LU,KU)
      CALL INIT1(LU(IU))
C
C     (5) FILE CONTAINING THE CODES OF ELEMENTARY WAVES
      CALL UNIT(5,NFILE,FILE,IU,NU,LU,KU)
C     THE DATA FILE FOR THE SUBROUTINE CODE1 REMAINS OPEN
      LUCODE=LU(IU)
      IU=0
C
C     (6) FILE CONTROLING THE TAKE-OFF PARAMETERS OF RAYS
      CALL UNIT(6,NFILE,FILE,IU,NU,LU,KU)
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)
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)
C     THE OUTPUT FILE FOR THE SUBROUTINES WRIT1, WRIT2, WRIT4 AND WRIT5
C     REMAINS OPEN
      LULOG=LU(IU)
C
      RETURN
      END
C
C=======================================================================
C
      SUBROUTINE UNIT(IFILE,NFILE,FILE,IU,NU,LU,KU)
      INTEGER IFILE,NFILE,IU,NU,LU(NU),KU(NU)
      CHARACTER*(*) FILE(NFILE)
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 ERRORS:
C     101...  OPEN FILE ERROR:
C             ERROR ENCOUNTERED DURING EXECUTION OF THE OPEN STATEMENT.
C
C DATE: 1992, DECEMBER 3
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) DICONNECTED
   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) DICONNECTED
            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),IOSTAT=IERR,ERR=90)
            END IF
            RETURN
          END IF
   22   CONTINUE
C       NO LOGICAL UNIT AVAILABLE
        IU=0
      END IF
      RETURN
C
   90 CONTINUE
      WRITE(*,'('' STATUS'',I5,'': '',A)') IERR,FILE(IFILE)
      PAUSE 'ERROR 101 IN UNIT: OPEN FILE ERROR'
      STOP
      END
C
C======================================================================
C
