!+ Subroutine to set pressures and temperatures at the edges of layers.
!
! Method:
!	When the code is run with properties given in layers, the
!	edges of the layers still need to be defined by pressure
!	levels, and in the infra-red temperatures at these levels
!	are also needed. Ideally, these values should be set carefully 
!	by the user and read from a file. However, for rough 
!	calculations an option is provided to estimate resonable
!	values.
!
! 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 SET_PT_LEVEL(IERR
     &   , BASE_NAME, LENGTH_NAME, N_LAYER, PSTAR
     &   , ISOLIR
     &   , P, T
     &   , P_LEVEL, T_LEVEL
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/gas_list.pcf'
      INCLUDE 'incl/aerosol_component.pcf'
      INCLUDE 'incl/input_head.pcf'
      INCLUDE 'incl/input_head.ncf'
      INCLUDE 'incl/spectral_region.pcf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      CHARACTER	!, INTENT(IN)
     &     BASE_NAME*80
!             BASE NAME OF FILES
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N_LAYER
!             NUMBER OF LAYERS
     &   , LENGTH_NAME
!             LENGTH OF BASE NAME
     &   , ISOLIR
!             SPECTRAL REGION
      REAL*8	!, INTENT(IN)
     &     PSTAR(NPD_PROFILE)
!             SURFACE PRESSURE
     &   , P(NPD_PROFILE, 0: NPD_LAYER)
!             PRESSURES ON LEVELS
     &   , T(NPD_PROFILE, 0: NPD_LAYER)
!             TEMPERATURES ON LEVELS
      REAL*8	!, INTENT(OUT)
     &     P_LEVEL(NPD_PROFILE, 0: NPD_LAYER)
!             PRESSURES AT EDGES OF LAYERS
     &   , T_LEVEL(NPD_PROFILE, 0: NPD_LAYER)
!             TEMPERATURES AT EDGES OF LAYERS
!
!     LOCAL VARIABLES
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      CHARACTER
     &     CHAR_YN*1
!             CHARACTER RESPONSE
     &   , FILE_NAME*80
!             NAME OF INPUT FILE
     &   , TITLE_DUMMY*80
!             TITLE OF INPUT FILE
      INTEGER
     &     NDUM
!             DUMMY VARIABLE
     &   , I
!             LOOP VARIABLE
      REAL*8
     &     D2_T(NPD_PROFILE, NPD_LAYER)
     &   , LOG_P(NPD_PROFILE, 0: NPD_LAYER)
!             LOGARITHMS OF PRESSURES IN LAYERS (FOR INTERPOLATION)
     &   , LOG_P_LEVEL
!             LOGARITHM OF PRESSURE ON LEVEL
     &   , WORK(NPD_LAYER)
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, SPLINE_FIT, SPLINE_EVALUATE
!
!
      INCLUDE 'incl/input_head.dcf'
!
!
!
!     PRESSURES AT THE BOUNDARIES OF LAYERS MAY BE SET EITHER FROM
!     AN EXTERNAL FILE OF TEMPERATURES AT THE BOUNDARIES OR BY
!     INTERPOLATION: THE FORMER IS RECOMMENDED, BUT THE LATTER MAY BE
!     MORE CONVENIENT.
!
      WRITE(IU_USER, '(/A, /A)')
     &   'ARE PRESSURES (AND TEMPERATURES) AT THE BOUNDARIES OF LAYERS '
     &   , 'TO BE READ IN OR SET BY INTERPOLATION ? (F/I)'
1     READ(IU_STDIN, '(A)') CHAR_YN
!
      IF ( (CHAR_YN.EQ.'F').OR.(CHAR_YN.EQ.'f') ) THEN
         FILE_NAME(1: LENGTH_NAME+7)=BASE_NAME(1: LENGTH_NAME)
     &      //'.'//PHYS_SUFFIX(IP_T_LEVEL)
         CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &      , FILE_NAME, TITLE_DUMMY
     &      , NDUM, N_LAYER, P_LEVEL, PSTAR, T_LEVEL)
         IF (IERR.EQ.I_MISSING_DATA) THEN
            WRITE(IU_ERR, '(A)')
     &         '*** ERROR: FILE OF TEMPERATURES ON LEVELS IS MISSING.'
            WRITE(IU_ERR, '(A)') 'NO SIMULATION IS POSSIBLE.'
            IERR=I_ERR_FATAL
            RETURN
         ENDIF
!
      ELSE IF ( (CHAR_YN.EQ.'I').OR.(CHAR_YN.EQ.'i') ) THEN
!
         P_LEVEL(1, N_LAYER)=PSTAR(1)
         DO I=N_LAYER, 2, -1
            P_LEVEL(1, I-1)=0.5D+00*(P(1, I)+P(1, I-1))
         ENDDO
         P_LEVEL(1, 0)=0.0D+00
!
         IF (ISOLIR.EQ.IP_INFRA_RED) THEN
!           LEVEL TEMPERATURES MUST BE ASSIGNED. WE USE A SPLINE FIT IN THE
!           MIDDLE OF THE LAYER AND LINEAR EXTRAPOLATION AT THE BOTTOM OF
!           THE COLUMN: ISOTHERMALITY IS ASSUMED AT THE TOP.
!           THE SPLINED FIT IS BASED ON T VARYING SMOOTHLY IN LOG(P).
            DO I=1, N_LAYER
               LOG_P(1, I)=LOG(P(1, I))
            ENDDO
            CALL SPLINE_FIT(N_LAYER, LOG_P(1, 1)
     &         , T(1, 1), D2_T(1, 1), WORK)
            DO I=1, N_LAYER-1
               LOG_P_LEVEL=LOG(P_LEVEL(1, I))
               CALL SPLINE_EVALUATE(IERR, N_LAYER, LOG_P(1, 1), T(1, 1)
     &            , D2_T(1, 1), LOG_P_LEVEL, T_LEVEL(1, I))
            ENDDO
            T_LEVEL(1, 0)=T(1, 1)
            T_LEVEL(1, N_LAYER)=(PSTAR(1)
     &         *(T(1, N_LAYER)-T(1, N_LAYER-1))
     &         +P(1, N_LAYER)*T(1, N_LAYER-1)
     &         -P(1, N_LAYER-1)*T(1, N_LAYER))
     &         /(P(1, N_LAYER)-P(1, N_LAYER-1))
         ENDIF
!
      ELSE
         WRITE(IU_ERR, '(A)') '+++ INVALID RESPONSE'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error.finc'
         ELSE
            WRITE(IU_USER, '(A)') 'PLEASE RE_ENTER'
            GOTO 1
         ENDIF
      ENDIF
!
!
      RETURN
      END
