!+ Subroutine to make spectral blocks of type 2.
!
! Method:
!	A solar spectrum, giving the irradiance against wavelength,
!	is read in. The total flux in the spectrum is determined,
!	using a Rayleigh-Jeans distribution for the far infra-red.
!	The fraction of this flux in each band is calculated. The
!	band will probably not cover the whole spectrum. If 
!	simulating observations this is as required, since the
!	spectral cut-offs of the observing instruments must be 
!	matched. In atmospheric models the whole flux should be
!	calculated and there is therefore an option to enhance
!	the fluxes in the outside bands to  the full flux.
!
! 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 MAKE_BLOCK_2(IERR
     &   , N_BAND, WAVE_LENGTH_SHORT, WAVE_LENGTH_LONG
     &   , L_EXCLUDE, N_BAND_EXCLUDE, INDEX_EXCLUDE
     &   , L_SOLAR_SPECTRUM, N_SOLAR_POINTS
     &   , SOLAR_WAVELENGTH, SOLAR_IRRAD
     &   , SOLAR_FLUX_BAND, L_PRESENT_2
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N_BAND
!             NUMBER OF BANDS
      INTEGER	!, INTENT(INOUT)
     &     N_SOLAR_POINTS
!             NUMBER OF POINTS IN SPECTRUM
      LOGICAL	!, INTENT(INOUT)
     &     L_SOLAR_SPECTRUM
!             SOLAR SPECTRAL FLAG
      LOGICAL	!, INTENT(OUT)
     &     L_EXCLUDE
!             FLAG FOR EXCLUSION OF BANDS
      INTEGER	!, INTENT(IN)
     &     N_BAND_EXCLUDE(NPD_BAND)
!             NUMBER OF BANDS EXCLUDED FROM SPECIFIC BANDS
     &   , INDEX_EXCLUDE(NPD_EXCLUDE, NPD_BAND)
!             INDICES OF EXCLUDED BANDS
      LOGICAL	!, INTENT(OUT)
     &     L_PRESENT_2
!             PRESENCE FLAG
      REAL*8	!, INTENT(OUT)
     &     SOLAR_FLUX_BAND(NPD_BAND)
!             SOLAR FLUX IN EACH BAND
      REAL*8	!, INTENT(IN)
     &     WAVE_LENGTH_SHORT(NPD_BAND)
!             BOTTOM OF EACH BAND
     &   , WAVE_LENGTH_LONG(NPD_BAND)
!             TOP OF EACH BAND
      REAL*8	!, INTENT(INOUT)
     &     SOLAR_WAVELENGTH(NPD_SOLAR_POINTS)
!             WAVELENGTHS OF SOLAR SPECTRUM
     &   , SOLAR_IRRAD(NPD_SOLAR_POINTS)
!             SOLAR IRRADIANCE AT TOA
!
!     LOCAL VARIABLES
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     CHAR_YN
!             CHARACTER RESPONSE VARIABLE
      INTEGER
     &     I_SHORT
!             BEGINNING OF BAND
     &   , I_LONG
!             END OF BAND
     &   , N_POINTS_BAND
!             NUMBER OF POINTS WITHIN BAND
     &   , I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
     &   , I_LAST_BAND
!             INDEX OF LAST BAND
      LOGICAL
     &     L_ENHANCE
!             ENHANCE OUTER BANDS
      REAL*8
     &     TOTAL_SOLAR_IRRADIANCE
!             TOTAL IRRADIANCE
     &   , X(NPD_SOLAR_POINTS)
!             ABSICISSAE OF WAVELENGTH
     &   , Y(NPD_SOLAR_POINTS)
!             ORDINATES OF SPECTRUM
     &   , IRRADIANCE_TAIL
!             IRRADIANCE IN TAIL
     &   , WAVE_LENGTH_BEGIN
!             BEGINNING OF RANGE
     &   , WAVE_LENGTH_END
!             END OF RANGE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SOLAR_SPECTRUM, INNER_BRACKET
!     FUNCTIONS CALLED:
      REAL*8
     &     SOLAR_INTENSITY
!             SOLAR INTENSITY AT GIVEN W.LENG.
     &   , TRAPEZOID
!             INTEGRATING FUNCTION
     &   , RAYLEIGH_JEANS_TAIL
!             TAIL OF RAYLEIGH JEANS FUNCTION
      EXTERNAL
     &     SOLAR_INTENSITY, TRAPEZOID, RAYLEIGH_JEANS_TAIL
!
!
!
!     OBTAIN THE SOLAR SPECTRUM IF DATA ARE NOT ALREADY PRESENT.
      IF (L_SOLAR_SPECTRUM) THEN
         WRITE(IU_USER, '(/A/)')
     &      'PREVIOUS SOLAR SPECTRUM WILL BE USED.'
      ELSE
         CALL READ_SOLAR_SPECTRUM(IERR, L_SOLAR_SPECTRUM
     &   , N_SOLAR_POINTS, SOLAR_WAVELENGTH, SOLAR_IRRAD)
         IF (IERR.NE.I_NORMAL) RETURN
      ENDIF
!
!     THE RADIANCE OUTSIDE THE NOMINAL LIMITS OF THE SPECTRUM CAN BE
!     ASSIGNED TO THE EDGING BANDS.
      WRITE(IU_USER, '(/A)') 'ASSIGN SOLAR FLUX OUTSIDE GIVEN BANDS '
     &   //'TO OUTSIDE BANDS? (Y/N)'
1     READ(IU_STDIN, '(A)') CHAR_YN
      IF ( (CHAR_YN.EQ.'Y').OR.(CHAR_YN.EQ.'y') ) THEN
         L_ENHANCE=.TRUE.
      ELSE IF ( (CHAR_YN.EQ.'N').OR.(CHAR_YN.EQ.'n') ) THEN
         L_ENHANCE=.FALSE.
      ELSE
         WRITE(IU_ERR, '(A)') '+++ UNRECOGNISED RESPONSE: '
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(A)') 'PLEASE RE-TYPE.'
            GOTO 1
         ENDIF
      ENDIF
!
!     CALCULATE THE TOTAL INTEGRATED IRRADIANCE OF THIS SPECTRUM.
      TOTAL_SOLAR_IRRADIANCE=TRAPEZOID(N_SOLAR_POINTS
     &   , SOLAR_WAVELENGTH, SOLAR_IRRAD)
!     ADD ON THE TAIL OF THE DISTRIBUTION USING A RAYLEIGH-JEANS LAW.
      IRRADIANCE_TAIL=
     &   RAYLEIGH_JEANS_TAIL(SOLAR_WAVELENGTH(N_SOLAR_POINTS))
      TOTAL_SOLAR_IRRADIANCE=TOTAL_SOLAR_IRRADIANCE+IRRADIANCE_TAIL
!
!     FOR EACH BAND FIND THE ARRAYS OF WAVELENGTH AND IRRADIANCE.
      DO I=1, N_BAND
!
!        FIND THE POINTS OF THE SPECTRUM JUST WITHIN THE BAND.
         IF (((I.NE.1).AND.(I.NE.N_BAND)).OR.(.NOT.L_ENHANCE)) THEN
            WAVE_LENGTH_BEGIN=WAVE_LENGTH_SHORT(I)
            WAVE_LENGTH_END=WAVE_LENGTH_LONG(I)
         ELSE IF (I.EQ.1) THEN
            WAVE_LENGTH_BEGIN=MIN(SOLAR_WAVELENGTH(1)
     &         , WAVE_LENGTH_SHORT(1))
            WAVE_LENGTH_END=WAVE_LENGTH_LONG(1)
         ELSE IF (I.EQ.N_BAND) THEN
            WAVE_LENGTH_BEGIN=WAVE_LENGTH_SHORT(N_BAND)
            WAVE_LENGTH_END=MAX(SOLAR_WAVELENGTH(N_SOLAR_POINTS)
     &         , WAVE_LENGTH_LONG(N_BAND))
         ENDIF
!
         CALL INNER_BRACKET(IERR
     &      , WAVE_LENGTH_BEGIN, WAVE_LENGTH_END
     &      , N_SOLAR_POINTS, SOLAR_WAVELENGTH, I_SHORT, I_LONG)
         IF (IERR.NE.I_NORMAL) RETURN
         N_POINTS_BAND=3+I_LONG-I_SHORT
         X(1)=WAVE_LENGTH_BEGIN
         Y(1)=SOLAR_INTENSITY(X(1)
     &      , N_SOLAR_POINTS, SOLAR_WAVELENGTH, SOLAR_IRRAD)
         X(N_POINTS_BAND)=WAVE_LENGTH_END
         Y(N_POINTS_BAND)=SOLAR_INTENSITY(X(N_POINTS_BAND)
     &      , N_SOLAR_POINTS, SOLAR_WAVELENGTH, SOLAR_IRRAD)
         DO J=I_SHORT, I_LONG
            X(J-I_SHORT+2)=SOLAR_WAVELENGTH(J)
            Y(J-I_SHORT+2)=SOLAR_IRRAD(J)
         ENDDO
!        INTEGRATE ACROSS THE BAND AND NORMALIZE BY 
!        THE TOTAL IRRADIANCE.
         SOLAR_FLUX_BAND(I)=TRAPEZOID(N_POINTS_BAND, X, Y)
     &      /TOTAL_SOLAR_IRRADIANCE
      ENDDO
!
!     ADD THE TAIL TO THE LAST BAND IF REQUIRED. THE SETTING OF LIMITS
!     ABOVE DEALT WITH THE CONTRIBUTIONS WITHIN THE REGION OF SOLAR
!     DATA.
      IF (L_ENHANCE) THEN
         I_LAST_BAND=1
         DO I=1, N_BAND
            IF (WAVE_LENGTH_LONG(I).GT.WAVE_LENGTH_LONG(I_LAST_BAND))
     &         I_LAST_BAND=I
         ENDDO
         SOLAR_FLUX_BAND(I_LAST_BAND)=SOLAR_FLUX_BAND(I_LAST_BAND)
     &      +IRRADIANCE_TAIL/TOTAL_SOLAR_IRRADIANCE
      ENDIF
!
!     REDUCE THE SOLAR FLUX IN BANDS WHICH CONTAIN EXCLUSIONS IF
!     NECESSARY.
      IF (L_EXCLUDE) THEN
         DO I=1, N_BAND
            DO J=1, N_BAND_EXCLUDE(I)
               SOLAR_FLUX_BAND(I)=SOLAR_FLUX_BAND(I)
     &            -SOLAR_FLUX_BAND(INDEX_EXCLUDE(J, I))
            ENDDO
         ENDDO
      ENDIF
!
      L_PRESENT_2=.TRUE.
!
!
      RETURN
      END
