!+ Subroutine to print information at end of iteration.
!
! Method:
!	Straightforward.
!
! 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 TERMINATE_SCALE(N_SET, LENGTH_SET
     &   , U_SET, TRANS_SET, WEIGHT_POINT
     &   , I_TYPE_RESIDUAL, SCALING
     &   , N_TERM, W_ESFT, K_ESFT
     &   , I_SCALE_FUNCTION, SCALE_VECTOR, P_ARRAY, T_ARRAY
     &   , SCALE_VECTOR_LONG
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_pp.ucf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/scale_fnc.pcf'
      INCLUDE 'incl/def_std_io.icf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(IN)
     &     N_SET
!             NUMBER OF SETS OF DATA
     &   , LENGTH_SET(NPD_SET_SCALE)
!             LENGTHS OF SETS
     &   , N_TERM
!             NUMBER OF TERMS OF FIT
     &   , I_SCALE_FUNCTION
!             TYPE OF SCALING FUNCTION
      LOGICAL	!, INTENT(IN)
     &     I_TYPE_RESIDUAL
!             FIT TO THE SCALING FUNCTION
      REAL*8	!, INTENT(IN)
     &     U_SET(NPD_AMOUNT, NPD_SET_SCALE)
!             AMOUNTS OF ABSORBERS
     &   , TRANS_SET(NPD_AMOUNT, NPD_SET_SCALE)
!             TRANSMISSIVITIES
     &   , SCALING(NPD_AMOUNT, NPD_SET_SCALE)
!             SCALINGS REQUIRED
     &   , WEIGHT_POINT(NPD_SET_SCALE, NPD_AMOUNT)
!             WEIGHTS FOR TRANSMISSIVITIES
     &   , W_ESFT(NPD_ESFT_TERM)
!             ESFT WIEGHTS
     &   , K_ESFT(NPD_ESFT_TERM)
!             ESFT EXPONENTS
     &   , SCALE_VECTOR(NPD_SCALE_VARIABLE)
!             SCALING PARAMETERS
     &   , P_ARRAY(NPD_SET_SCALE)
!             PRESSURE ARRAY
     &   , T_ARRAY(NPD_SET_SCALE)
!             TEMPERATURE ARRAY
      REAL*8	!, INTENT(OUT)
     &     SCALE_VECTOR_LONG(NPD_SCALE_VARIABLE, NPD_ESFT_TERM)
!             OUT  SCALING PARAMETERS
!
!     LOCAL VARIABLES.
      INTEGER
     &     I
!             LOOP VARIABLE
     &   , J
!             LOOP VARIABLE
      REAL*8
     &     SCALE_VECTOR_TEST(NPD_SCALE_VARIABLE)
!             TEMPORARY SCALING VARIABLES
     &   , RESIDUAL_TEST
!             VALUE OF RESIDUAL AT POINT
!
!     FUNCTIONS CALLED:
      REAL*8
     &     RESIDUAL_TRANS
!             RESIDUAL FUNCTION
      EXTERNAL
     &     RESIDUAL_TRANS
!
!
      INCLUDE 'incl/scale_fnc.dcf'
!
!
!     DISPLAY RMS RESIDUAL ERRORS FOR DIFFERENT CHOICES OF SCALING
!     PARAMETERS.
      WRITE(IU_STDOUT, '(/A)') 'FINAL SCALING CALCULATED:'
      WRITE(IU_MONITOR, '(/A)') 'FINAL SCALING CALCULATED:'
      IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_LAW) THEN
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR(2)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR(2)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_QUAD) THEN
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR(1)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = ', (SCALE_VECTOR(I), I=2, 3)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR(1)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = ', (SCALE_VECTOR(I), I=2, 3)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_DOPPLER_QUAD) THEN
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR(2)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = ', (SCALE_VECTOR(I), I=3, 4)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR(2)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = ', (SCALE_VECTOR(I), I=3, 4)
      ENDIF
      RESIDUAL_TEST=RESIDUAL_TRANS(N_SET, LENGTH_SET
     &   , U_SET, TRANS_SET, WEIGHT_POINT
     &   , I_TYPE_RESIDUAL, SCALING
     &   , N_TERM, W_ESFT, K_ESFT
     &   , I_SCALE_FUNCTION, SCALE_VECTOR, P_ARRAY, T_ARRAY)
      RESIDUAL_TEST=SQRT(RESIDUAL_TEST)
      WRITE(IU_STDOUT, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
      WRITE(IU_MONITOR, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
!
      WRITE(IU_STDOUT, '(/A)') 'PRESSURE SCALING ALONE:'
      WRITE(IU_MONITOR, '(/A)') 'PRESSURE SCALING ALONE:'
      IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_LAW) THEN
         SCALE_VECTOR_TEST(1)=SCALE_VECTOR(1)
         SCALE_VECTOR_TEST(2)=0.0D+00
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR_TEST(2)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_QUAD) THEN
         SCALE_VECTOR_TEST(1)=SCALE_VECTOR(1)
         SCALE_VECTOR_TEST(2)=0.0D+00
         SCALE_VECTOR_TEST(3)=0.0D+00
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=2, 3)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=2, 3)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_DOPPLER_QUAD) THEN
         SCALE_VECTOR_TEST(1)=SCALE_VECTOR(1)
         SCALE_VECTOR_TEST(2)=SCALE_VECTOR(2)
         SCALE_VECTOR_TEST(3)=0.0D+00
         SCALE_VECTOR_TEST(4)=0.0D+00
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=3, 4)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=3, 4)
      ENDIF
      RESIDUAL_TEST=RESIDUAL_TRANS(N_SET, LENGTH_SET
     &   , U_SET, TRANS_SET, WEIGHT_POINT
     &   , I_TYPE_RESIDUAL, SCALING
     &   , N_TERM, W_ESFT, K_ESFT
     &   , I_SCALE_FUNCTION, SCALE_VECTOR_TEST, P_ARRAY, T_ARRAY)
      RESIDUAL_TEST=SQRT(RESIDUAL_TEST)
      WRITE(IU_STDOUT, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
      WRITE(IU_MONITOR, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
!
      WRITE(IU_STDOUT, '(/A)') 'TEMPERATURE SCALING ALONE:'
      WRITE(IU_MONITOR, '(/A)') 'TEMPERATURE SCALING ALONE:'
      IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_LAW) THEN
         SCALE_VECTOR_TEST(1)=0.0D+00
         SCALE_VECTOR_TEST(2)=SCALE_VECTOR(2)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR_TEST(2)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_QUAD) THEN
         SCALE_VECTOR_TEST(1)=0.0D+00
         SCALE_VECTOR_TEST(2)=SCALE_VECTOR(2)
         SCALE_VECTOR_TEST(3)=SCALE_VECTOR(3)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=2, 3)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=2, 3)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_DOPPLER_QUAD) THEN
         SCALE_VECTOR_TEST(1)=0.0D+00
         SCALE_VECTOR_TEST(2)=0.0D+00
         SCALE_VECTOR_TEST(3)=SCALE_VECTOR(3)
         SCALE_VECTOR_TEST(4)=SCALE_VECTOR(4)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=3, 4)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=3, 4)
      ENDIF
      RESIDUAL_TEST=RESIDUAL_TRANS(N_SET, LENGTH_SET
     &   , U_SET, TRANS_SET, WEIGHT_POINT
     &   , I_TYPE_RESIDUAL, SCALING
     &   , N_TERM, W_ESFT, K_ESFT
     &   , I_SCALE_FUNCTION, SCALE_VECTOR_TEST, P_ARRAY, T_ARRAY)
      RESIDUAL_TEST=SQRT(RESIDUAL_TEST)
      WRITE(IU_STDOUT, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
      WRITE(IU_MONITOR, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
!
      WRITE(IU_STDOUT, '(/A)') 'NO SCALING AT ALL:'
      WRITE(IU_MONITOR, '(/A)') 'NO SCALING AT ALL:'
      IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_LAW) THEN
         SCALE_VECTOR_TEST(1)=0.0D+00
         SCALE_VECTOR_TEST(2)=0.0D+00
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF TEMPERATURE = ', SCALE_VECTOR_TEST(2)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_POWER_QUAD) THEN
         SCALE_VECTOR_TEST(1)=0.0D+00
         SCALE_VECTOR_TEST(2)=0.0D+00
         SCALE_VECTOR_TEST(3)=0.0D+00
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=2, 3)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=2, 3)
      ELSE IF (I_SCALE_FUNCTION.EQ.IP_SCALE_DOPPLER_QUAD) THEN
         SCALE_VECTOR_TEST(1)=0.0D+00
         SCALE_VECTOR_TEST(2)=0.0D+00
         SCALE_VECTOR_TEST(3)=0.0D+00
         SCALE_VECTOR_TEST(4)=0.0D+00
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_STDOUT, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_STDOUT, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=3, 4)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'EXPONENT OF PRESSURE = ', SCALE_VECTOR_TEST(1)
         WRITE(IU_MONITOR, '(6X, A, 1PE10.3)')
     &      'OFFSET TO NORMALIZED PRESSURE = ', SCALE_VECTOR_TEST(2)
         WRITE(IU_MONITOR, '(6X, A, 3(1PE10.3, 2X))')
     &      'COEFFICIENTS OF QUADRATIC = '
     &      , (SCALE_VECTOR_TEST(I), I=3, 4)
      ENDIF
      RESIDUAL_TEST=RESIDUAL_TRANS(N_SET, LENGTH_SET
     &   , U_SET, TRANS_SET, WEIGHT_POINT
     &   , I_TYPE_RESIDUAL, SCALING
     &   , N_TERM, W_ESFT, K_ESFT
     &   , I_SCALE_FUNCTION, SCALE_VECTOR_TEST, P_ARRAY, T_ARRAY)
      RESIDUAL_TEST=SQRT(RESIDUAL_TEST)
      WRITE(IU_STDOUT, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
      WRITE(IU_MONITOR, '(6X, A, 1PE12.5)') 'R.M.S. RESIDUAL ERROR IN'
     &   //' TRANSMISSION = ', RESIDUAL_TEST
!
!     COPY THE SHORT SCALING VECTOR INTO THE LONG ARRAY.
      DO I=1, N_TERM
         DO J=1, N_SCALE_VARIABLE(I_SCALE_FUNCTION)
            SCALE_VECTOR_LONG(J, I)=SCALE_VECTOR(J)
         ENDDO
      ENDDO
!
!
      RETURN
      END
