!+ Subroutine to read the schema describing a spectral file.
!
! Method:
!	The schema is read to find directives which are interpreted.
!	A separate subroutine is called to process the directive
!	*BAND.
!
! 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 READ_SCHEMA_SPECTRUM(IERR
     &   , N_BAND, N_ABSORB, TYPE_ABSORB
     &   , N_AEROSOL, TYPE_AEROSOL
     &   , WAVE_LENGTH_SHORT, WAVE_LENGTH_LONG
     &   , N_BAND_ABSORB, INDEX_ABSORB
     &   , N_BAND_CONTINUUM, INDEX_CONTINUUM
     &   , L_EXCLUDE, N_BAND_EXCLUDE, INDEX_EXCLUDE
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_io.ucf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/gas_list.pcf'
      INCLUDE 'incl/def_std_io.icf'
!
!
!     DUMMY ARGUMENTS.
!
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
!
!     ELEMENTS OF SPECTRUM
      INTEGER	!, INTENT(OUT)
     &     N_BAND
!             NUMBER OF SPECTRAL BANDS
!     LIST OF EXCLUDED BANDS
      LOGICAL	!, INTENT(OUT)
     &     L_EXCLUDE
!             FLAG TO EXCLUDE BANDS
      INTEGER	!, INTENT(OUT)
     &     N_BAND_EXCLUDE(NPD_BAND)
!             NUMBER BANDS EXCLUDED FROM THIS BAND
     &   , INDEX_EXCLUDE(NPD_EXCLUDE, NPD_BAND)
!             LIST OF EXCLUDED BANDS
!     GASEOUS ABSORPTION
      INTEGER	!, INTENT(OUT)
     &     N_ABSORB
!             NUMBER OF ABSORBERS
     &   , TYPE_ABSORB(NPD_SPECIES)
!             TYPE NUMBERS OF ABSORBING SPECIES
     &   , N_BAND_ABSORB(NPD_BAND)
!             NUMBER OF ABSORBERS IN THIS BAND
     &   , INDEX_ABSORB(NPD_SPECIES, NPD_BAND)
!             LIST OF ABSORBERS ACTIVE IN SPECIFIC BANDS
!     CONTINUUM PROCESSES
      INTEGER	!, INTENT(OUT)
     &     N_BAND_CONTINUUM(NPD_BAND)
!             NUMBER OF CONTINUA IN THIS BAND
     &   , INDEX_CONTINUUM(NPD_BAND, NPD_CONTINUUM)
!             LIST OF CONTINUA ACTIVE IN SPECIFIC BANDS
!     AEROSOL PROCESSES
      INTEGER	!, INTENT(OUT)
     &     N_AEROSOL
!             NUMBER OF AEROSOLS
     &   , TYPE_AEROSOL(NPD_AEROSOL_SPECIES)
!             TYPES OF AEROSOLS
      REAL*8	!, INTENT(OUT)
     &     WAVE_LENGTH_SHORT(NPD_BAND)
!             SHORT END OF BANDS
     &   , WAVE_LENGTH_LONG(NPD_BAND)
!             LONG END OF BANDS
!
!
!     LOCAL VARIABLES.
      CHARACTER
     &     LINE*80
!             LINE READ FROM FILE
     &   , CH_UNIT*6
!             CHARACTER STRING IDENTIFYING UNIT FOR LIMITS OF BANDS
      LOGICAL
     &     L_SET_UNIT
!             FLAG SET TO TRUE WHEN UNITS HAVE BEEN DEFINED
      INTEGER
     &     IOS
!             FLAG RECORDING STATUS OF I/O
     &   , INDEX_TYPE(NPD_GASES)
!             INDEXING NUMBERS OF TYPES OF GASES
     &   , I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     OPEN_FILE_IN, SET_BAND, READ_LIST
!
!
      DATA INDEX_TYPE/NPD_GASES*0/
!
!
!
!     OPEN THE FILE CONTAINING THE SCHEMA FOR THE SPECTRUM.
      CALL OPEN_FILE_IN(IERR, IU_FILE_IN
     &   , 'GIVE THE NAME OF THE FILE CONTAINING THE SCHEMA FOR '
     &   //'THE SPECTRUM.')
!
!     INITIALIZE THE PROPERTIES OF THE SPECTRUM.
      N_BAND=0
      N_ABSORB=0
      N_AEROSOL=0
      L_EXCLUDE=.FALSE.
!
!     READ EACH LINE LOOKING FOR DIRECTIVES. BEFORE ANY BANDS ARE SET
!     WE NEED ONE SETTING THE UNIT FOR WAVELENGTH.
      L_SET_UNIT=.FALSE.
      IOS=0
      DO WHILE (IOS.EQ.0)
         READ(IU_FILE_IN, '(A)', IOSTAT=IOS) LINE
         IF (IOS.NE.0) GOTO 1
!
!        CHECK FOR DIRECTIVES SETTING WAVELENGTHS.
         IF (LINE(1:6).EQ.'*METRE') THEN
            L_SET_UNIT=.TRUE.
            CH_UNIT='METRE '
         ELSE IF (LINE(1:7).EQ.'*MICRON') THEN
            L_SET_UNIT=.TRUE.
            CH_UNIT='MICRON'
         ELSE IF (LINE(1:7).EQ.'*INV_CM') THEN
            L_SET_UNIT=.TRUE.
            CH_UNIT='INV_CM'
         ELSE IF (LINE(1:5).EQ.'*BAND') THEN
            IF (L_SET_UNIT) THEN
               N_BAND=N_BAND+1
               CALL SET_BAND(IERR
     &            , LINE(7:80), CH_UNIT
     &            , N_BAND, N_ABSORB
     &            , N_BAND_ABSORB, INDEX_ABSORB
     &            , N_BAND_CONTINUUM, INDEX_CONTINUUM
     &            , WAVE_LENGTH_SHORT, WAVE_LENGTH_LONG
     &            , TYPE_ABSORB, INDEX_TYPE
     &            , L_EXCLUDE, N_BAND_EXCLUDE, INDEX_EXCLUDE
     &            )
               IF (IERR.NE.I_NORMAL) RETURN
            ELSE
               WRITE(IU_ERR, '(/A)') 'A UNIT FOR WAVELENGTH MUST BE '
     &            //'SET BEFORE BANDS MAY BE DECLARED.'
               IERR=I_ERR_FATAL
               RETURN
            ENDIF
         ELSE IF (LINE(1:8).EQ.'*AEROSOL') THEN
            CALL READ_LIST(IERR, LINE(10:80), 70
     &         , N_AEROSOL, TYPE_AEROSOL)
            IF (IERR.EQ.I_ERR_IO) THEN
               WRITE(IU_ERR, '(/A)') '*** ERROR: INCORRECT AEROSOL DATA'
               IERR=I_ERR_FATAL
               RETURN
            ENDIF
            IF (IERR.NE.I_NORMAL) RETURN
         ENDIF
      ENDDO
!
!     CHECK THAT EXCLUDED BANDS ARE IN RANGE.
      DO I=1, N_BAND
         DO J=1, N_BAND_EXCLUDE(I)
            IF ( (INDEX_EXCLUDE(J, I).LT.1).OR.
     &           (INDEX_EXCLUDE(J, I).GT.N_BAND) ) THEN
               WRITE(IU_ERR, '(/A, I5, A)') 'AN EXCLUDED BAND IN BAND '
     &            , I, ' IS OUT OF RANGE.'
               IERR=I_ERR_FATAL
               RETURN
            ENDIF
         ENDDO
      ENDDO
!
!
1     RETURN
      END
