!+ Subroutine to caclulate weights for a filter function.
!
! Method:
!	Appropriate weights for each band in the spectral file
!	are calculted from a filter function which is read in.
!	Splines are used to represent the function, so many
!	points should be used for functions which are not smooth,
!
! 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 CONVOLVE_FUNCTION(IERR, N_BAND
     &   , WAVE_LENGTH_SHORT, WAVE_LENGTH_LONG
     &   , WEIGHT_BAND)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_obs.ucf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N_BAND
!             NUMBER OF BANDS
      REAL*8	!, INTENT(IN)
     &     WAVE_LENGTH_SHORT(NPD_BAND)
!             SHORTWAVE LIMITS OF EACH BAND
     &   , WAVE_LENGTH_LONG(NPD_BAND)
!             LONGWAVE LIMITS OF EACH BAND
      REAL*8	!, INTENT(OUT)
     &     WEIGHT_BAND(NPD_BAND)
!             WEIGHTS FOR EACH BAND
!
!     LOCAL VARIABLES
      INTEGER
     &     N_FILTER
!             NUMBER OF FILTER POINTS
     &   , I
!             LOOP VARIABLE
     &   , POINTER(NPD_FILTER)
!             POINTER TO ORDER FILTER
      REAL*8
     &     WAVENUMBER_FILTER(NPD_FILTER)
!             WAVENUMBERS OF FILTER POINTS
     &   , WAVENUMBER_FILTER_ORD(NPD_FILTER)
!             ORD. WAVENUMBERS OF FILTER POINTS
     &   , TRANS_FILTER(NPD_FILTER)
!             TRANSMITTANCE OF FILTER
     &   , TRANS_FILTER_ORD(NPD_FILTER)
!             ORD. TRANSMITTANCE OF FILTER
     &   , SECOND_DERIVATIVE(NPD_FILTER)
!             SECOND DERIVATIVE FOR SPLINE
     &   , WORK(NPD_FILTER)
!             WORKING ARRAY
     &   , WAVENUMBER_LOW
!             LOW WAVENUMBER OF BAND
     &   , WAVENUMBER_HIGH
!             HIGH WAVENUMBER OF BAND
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_FILTER, SHELL_SORT, SPLINE_FIT, INTEGRATE_SPLINE
!
!
!
!     READ THE FILTER FUNCTION
      CALL READ_FILTER(IERR
     &   , N_FILTER, WAVENUMBER_FILTER, TRANS_FILTER)
!
!     ORDER THE POINTS OF THE FILTER.
!     INITIALIZE THE POINTER TO THE PRESENT ORDER.
      DO I=1, N_FILTER
         POINTER(I)=I
      ENDDO
      CALL SHELL_SORT(N_FILTER, POINTER, WAVENUMBER_FILTER)
      DO I=1, N_FILTER
         WAVENUMBER_FILTER_ORD(I)=WAVENUMBER_FILTER(POINTER(I))
         TRANS_FILTER_ORD(I)=TRANS_FILTER(POINTER(I))
      ENDDO
!
!     ESTABLISH A SPLINE FIT TO THE FILTER FUNCTION.
      CALL SPLINE_FIT(N_FILTER, WAVENUMBER_FILTER_ORD, TRANS_FILTER_ORD
     &   , SECOND_DERIVATIVE, WORK)
!
!     CALCULATE THE WEIGHTING FUNCTION FOR EACH BAND BY INTEGRATING
!     THE SPLINE FIT
      DO I=1, N_BAND
         WAVENUMBER_LOW=1.0D+00/WAVE_LENGTH_LONG(I)
         WAVENUMBER_HIGH=1.0D+00/WAVE_LENGTH_SHORT(I)
         CALL INTEGRATE_SPLINE(IERR
     &      , WAVENUMBER_LOW, WAVENUMBER_HIGH
     &      , N_FILTER, WAVENUMBER_FILTER_ORD, TRANS_FILTER_ORD
     &      , SECOND_DERIVATIVE, WEIGHT_BAND(I))
         IF (IERR.NE.I_NORMAL) THEN
            IF (IERR.EQ.I_ERR_RANGE) THEN
!              IF THE BAND LIES OUTSIDE THE SPLINE RANGE THE WEIGHTING IS 0.
!              WE RECOVER FROM THIS ERROR.
               WEIGHT_BAND(I)=0.0D+00
               IERR=I_NORMAL
            ELSE
               IERR=I_ERR_FATAL
            ENDIF
         ENDIF
         WEIGHT_BAND(I)=WEIGHT_BAND(I)/(WAVENUMBER_HIGH-WAVENUMBER_LOW)
      ENDDO
!
!
      RETURN
      END
