!+ Subroutine to apply a path-length scaling to the continuum.
!
! Method:
!	The scaling function is calculated. This is multpiled by a 
!	suitable "amount" of continuum incorporating a broadening
!	density.
!
! 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 RESCALE_CONTINUUM(N_PROFILE, N_LAYER, I_CONTINUUM
     &   , P, T, L_LAYER, I_TOP
     &   , DENSITY, MOLAR_DENSITY_WATER, MOLAR_DENSITY_FRN
     &   , WATER_FRAC
     &   , AMOUNT_CONTINUUM
     &   , I_FNC
     &   , P_REFERENCE, T_REFERENCE, SCALE_PARAMETER
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/physical_constants_0.ccf'
      INCLUDE 'incl/continuum.pcf'
      INCLUDE 'incl/scale_fnc.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(IN)
     &     N_PROFILE
!             NUMBER OF PROFILES
     &   , N_LAYER
!             NUMBER OF LAYERS
     &   , I_CONTINUUM
!             CONTINUUM TYPE
     &   , I_FNC
!             SCALING FUNCTION
     &   , I_TOP
!             TOP INDEX OF ARRAYS
      LOGICAL	!, INTENT(IN)
     &     L_LAYER
!             DATA ARE SUPPLIED IN LAYERS
      REAL*8	!, INTENT(IN)
     &     WATER_FRAC(NPD_PROFILE, 0: NPD_LAYER)
!             MASS FRACTION OF WATER
     &   , P(NPD_PROFILE, 0: NPD_LAYER)
!             PRESSURE
     &   , T(NPD_PROFILE, 0: NPD_LAYER)
!             TEMPERATURE
     &   , DENSITY(NPD_PROFILE, 0: NPD_LAYER)
!             OVERALL DENSITY
     &   , MOLAR_DENSITY_WATER(NPD_PROFILE, 0: NPD_LAYER)
!             MOLAR DENSITY OF WATER VAPOUR
     &   , MOLAR_DENSITY_FRN(NPD_PROFILE, 0: NPD_LAYER)
!             MOLAR DENSITY OF FOREIGN SPECIES
     &   , P_REFERENCE
!             REFERENCE PRESSURE
     &   , T_REFERENCE
!             REFERENCE PRESSURE
     &   , SCALE_PARAMETER(NPD_SCALE_VARIABLE)
!             SCALING PARAMTERS
      REAL*8	!, INTENT(OUT)
     &     AMOUNT_CONTINUUM(NPD_PROFILE, 0: NPD_LAYER)
!             AMOUNT OF CONTINUUM
!
!     LOCAL VARIABLES.
      INTEGER
     &     L
!             LOOP VARIABLE
     &   , I
!             LOOP VARIABLE
!
!
      IF (I_FNC.EQ.IP_SCALE_POWER_LAW) THEN
         DO I=I_TOP, N_LAYER
            DO L=1, N_PROFILE
               AMOUNT_CONTINUUM(L, I)
     &            =EXP(SCALE_PARAMETER(1)*LOG(P(L, I)/P_REFERENCE)
     &            +SCALE_PARAMETER(2)*LOG(T(L, I)/T_REFERENCE))
            ENDDO
         ENDDO
      ELSE IF(I_FNC.EQ.IP_SCALE_POWER_QUAD) THEN
         DO I=I_TOP, N_LAYER
            DO L=1, N_PROFILE
               AMOUNT_CONTINUUM(L, I)
     &            =EXP(SCALE_PARAMETER(1)*LOG(P(L, I)/P_REFERENCE))
     &            *(1.0D+00+SCALE_PARAMETER(2)*(T(L, I)
     &            /T_REFERENCE-1.0D+00)
     &            +SCALE_PARAMETER(3)*(T(L, I)
     &            /T_REFERENCE-1.0D+00)**2)
            ENDDO
         ENDDO
      ENDIF
!
      IF (L_LAYER) THEN
         IF (I_CONTINUUM.EQ.IP_SELF_CONTINUUM) THEN
            DO I=1, N_LAYER
               DO L=1, N_PROFILE
                  AMOUNT_CONTINUUM(L, I)=AMOUNT_CONTINUUM(L, I)
     &               *MOLAR_DENSITY_WATER(L, I)*WATER_FRAC(L, I)
               ENDDO
            ENDDO
         ELSE IF (I_CONTINUUM.EQ.IP_FRN_CONTINUUM) THEN
            DO I=1, N_LAYER
               DO L=1, N_PROFILE
                  AMOUNT_CONTINUUM(L, I)=AMOUNT_CONTINUUM(L, I)
     &               *MOLAR_DENSITY_FRN(L, I)*WATER_FRAC(L, I)
               ENDDO
            ENDDO
         ELSE IF (I_CONTINUUM.EQ.IP_N2_CONTINUUM) THEN
            DO I=1, N_LAYER
               DO L=1, N_PROFILE
                  AMOUNT_CONTINUUM(L, I)=AMOUNT_CONTINUUM(L, I)
     &               *N2_MASS_FRAC*DENSITY(L, I)
               ENDDO
            ENDDO
         ENDIF
      ELSE
!        IF VALUES ARE GIVEN ON LEVELS WE NOW INTERPOLATE TO AVERAGES 
!        ACROSS THE LAYER.
         IF (I_CONTINUUM.EQ.IP_SELF_CONTINUUM) THEN
            DO I=N_LAYER, 1, -1
               DO L=1, N_PROFILE
                  AMOUNT_CONTINUUM(L, I)=0.5D+00
     &               *(AMOUNT_CONTINUUM(L, I)
     &               *MOLAR_DENSITY_WATER(L, I)*WATER_FRAC(L, I)
     &               +AMOUNT_CONTINUUM(L, I-1)
     &               *MOLAR_DENSITY_WATER(L, I-1)*WATER_FRAC(L, I-1))
               ENDDO
            ENDDO
         ELSE IF (I_CONTINUUM.EQ.IP_FRN_CONTINUUM) THEN
            DO I=N_LAYER, 1, -1
               DO L=1, N_PROFILE
                  AMOUNT_CONTINUUM(L, I)=0.5D+00
     &               *(AMOUNT_CONTINUUM(L, I)
     &               *MOLAR_DENSITY_FRN(L, I)*WATER_FRAC(L, I)
     &               +AMOUNT_CONTINUUM(L, I-1)
     &               *MOLAR_DENSITY_FRN(L, I-1)*WATER_FRAC(L, I-1))
               ENDDO
            ENDDO
         ELSE IF (I_CONTINUUM.EQ.IP_N2_CONTINUUM) THEN
            DO I=N_LAYER, 1, -1
               DO L=1, N_PROFILE
                  AMOUNT_CONTINUUM(L, I)=0.5D+00
     &               *(AMOUNT_CONTINUUM(L, I)
     &               *N2_MASS_FRAC*DENSITY(L, I)
     &               +AMOUNT_CONTINUUM(L, I-1)
     &               *N2_MASS_FRAC*DENSITY(L, I-1))
               ENDDO
            ENDDO
         ENDIF
      ENDIF
!
!
      RETURN
      END
