!+ Program to find the height of a pressure level.
!
! Method:
!	A SAMSON-file of heights is read and the user supplies
!	a pressure level. The height of this level is interpolated.
!
! 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.
!
!- ---------------------------------------------------------------------
      PROGRAM PHGT
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/samson_parm.ecf'
      INCLUDE 'incl/samson_pstar.ecf'
      INCLUDE 'incl/interp_mode.pcf'
      INCLUDE 'incl/error.pcf'
!
!     DECLARATION OF VARIABLES.
      CHARACTER
     &     FILE_IN*80
!             NAME OF INPUT FILE
     &   , TITLE_IN*80
!             TITLE OF INPUT FILE
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , IOS
!             I/O ERROR FLAG
     &   , N_PROFILE_IN
!             NUMBER OF INPUT LEVELS
     &   , N_LEVEL_IN
!             NUMBER OF INPUT LEVELS
     &   , I_MODE
!             MODE OF INTERPOLATION
     &   , I
!             LOOP VARIABLE
      LOGICAL
     &     L_SPLINED
!             TRUE WHEN SPLINE IS DONE
      REAL*8
     &     FIELD_IN(NPD_PROFILE, NPD_LAYER+1)
!             INPUT FIELD
     &   , HEIGHT
!             HEIGHT FOR EVALUATION
     &   , P_EVALUATED
!             EVALUATED PRESSURE
     &   , HEIGHT_FIELD(NPD_LAYER+1)
!             REVERSED HEIGHT FIELD
     &   , P_REVERSED(NPD_LAYER+1)
!             REVERSED PRESSURE FIELD
     &   , X_IN(NPD_LAYER+1)
!             ABSCISSA FOR INTERPOLATION
     &   , Y_IN(NPD_LAYER+1)
!             ORDINATE FOR INTERPOLATION
     &   , Y2_IN(NPD_LAYER+1)
!             2ND DERIVATIVE OF ORDINATE
     &   , P_IN(NPD_LAYER+1)
!             PRESSURE FOR INTERPOLATION
     &   , WORK(NPD_LAYER+1)
!             WORKING ARRAY
     &   , PSTAR
!             SURFACE PRESSURE
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, INTERPOLATE_P
!
!
      DATA L_SPLINED/.FALSE./, PSTAR/PSTAR_DEFAULT/
     &   , I_MODE/IP_1_LIN_LOG/
!
!
!
!     READ IN THE SAMSON FILE.
      WRITE(IU_USER, '(A)')
     &   'ENTER THE NAME OF THE SAMSON FILE CONTAINING THE HEIGHT.'
      READ(IU_STDIN, '(A)') FILE_IN
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_IN, TITLE_IN
     &   , N_PROFILE_IN, N_LEVEL_IN, P_IN, PSTAR, FIELD_IN)
      N_LEVEL_IN=N_LEVEL_IN+1
      IF (N_PROFILE_IN.NE.1) THEN
         WRITE(IU_ERR, '(/A)') 'THIS FILE CONTAINS MORE THAN ONE '
     &      //'PROFILE: EXECUTION TERMINATED. '
         IERR=I_ERR_FATAL
         STOP
      ENDIF
!
!     READ IN THE HEIGHT AT WHICH THE PRESSURE IS TO BE FOUND.
      WRITE(IU_USER, '(/A)')
     &   'ENTER THE HEIGHT AT WHICH THE PRESSURE IS TO BE EVALUATED.'
1     READ(IU_STDIN, *, IOSTAT=IOS) HEIGHT
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(A)') '+++ UNRECOGNIZED RESPONSE:'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error_main.finc'
         ELSE
            WRITE(IU_ERR, '(A)') 'PLEASE RE-ENTER.'
            GOTO 1
         ENDIF
      ENDIF
!
!
!     REVERSE THE HEIGHT AND PRESSURE ARRAYS SO THAT WE FIT ON AN ARRAY
!     OF ASCENDING VALUES.
      DO I=1, N_LEVEL_IN
         HEIGHT_FIELD(N_LEVEL_IN+1-I)=FIELD_IN(1, I)
         P_REVERSED(N_LEVEL_IN+1-I)=P_IN(I)
      ENDDO
      L_SPLINED=.FALSE.
      CALL INTERPOLATE_P(IERR, N_LEVEL_IN, HEIGHT_FIELD
     &   , P_REVERSED, X_IN, Y_IN, Y2_IN, WORK
     &   , HEIGHT, P_EVALUATED
     &   , I_MODE, L_SPLINED
     &   )
      IF (IERR.EQ.I_ERR_FATAL) STOP
!
!     PRINT THE FINAL FIELD.
      WRITE(IU_STDOUT, '(1PE14.7)') P_EVALUATED
!
!
      STOP
      END

