!+ Program to interpolate a SAMSON file on to a new grid.
!
! Method:
!	Two SAMSON files are read, one specifying the data and the
!	other the new grid. A method of interpolation is also 
!	supplied and the program used this method to interpolate
!	the data to the final grid.
!	
!
! 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 INTERP
!
!
!
      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
     &   , FILE_GRID*80
!             NAME OF GRID FILE
     &   , FILE_OUT*80
!             NAME OF OUTPUT FILE
     &   , TITLE_IN*80
!             TITLE OF INPUT FILE
     &   , TITLE_GRID*80
!             TITLE OF GRID FILE
     &   , TITLE_OUT*80
!             TITLE OF OUTPUT FILE
!
      LOGICAL
     &     LOCK_CODE
!             LOGICAL TO FORBID INTERACTIVE LOOPING
      EXTERNAL
     &     LOCK_CODE
!
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , IOS
!             I/O ERROR FLAG
     &   , N_PROFILE_IN
!             NUMBER OF INPUT PROFILES
     &   , N_PROFILE_GRID
!             NUMBER OF GRID PROFILES
     &   , N_LEVEL_IN
!             NUMBER OF INPUT LEVELS
     &   , N_LEVEL_GRID
!             NUMBER OF GRID LEVELS
     &   , N_FIELD_SIZE
!             NUMBER OF FIELDS
     &   , I_MODE
!             MODE OF INTERPOLATION
     &   , I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      LOGICAL
     &     L_SPLINED
!             TRUE WHEN SPLINE IS DONE
      REAL*8
     &     FIELD_IN(NPD_PROFILE, NPD_LAYER+1)
!             INPUT FIELD
     &   , FIELD_GRID(NPD_PROFILE, NPD_LAYER+1)
!             GRID FIELD
     &   , FIELD_OUT(NPD_PROFILE, NPD_LAYER+1)
!             OUTPUT FIELD
     &   , A_IN(NPD_LAYER+1)
!             GATHERED INTERPOLATION 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
     &   , P_GRID(NPD_LAYER+1)
!             PRESSURE LEVELS ON GRID
     &   , WORK(NPD_LAYER+1)
!             WORKING ARRAY
     &   , pstar
!             SURFACE PRESSURE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, INTERPOLATE_P
     &   , WRITE_SAMSON_FIELD
!
!
      DATA L_SPLINED/.FALSE./, PSTAR/PSTAR_DEFAULT/
!
!
!
!     READ IN THE BASIC FIELD.
      WRITE(IU_USER, '(/A)')
     &   'ENTER THE NAME OF THE FIELD TO BE INTERPOLATED.'
      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
      N_FIELD_SIZE=1
!     READ IN THE GRID ONTO WHICH INTERPOLATION IS DONE.
      WRITE(IU_USER, '(/A)')
     &   'ENTER THE NAME OF THE FILE CONTAINING THE GRID.'
      READ(IU_STDIN, '(A)') FILE_GRID
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_GRID, TITLE_GRID
     &   , N_PROFILE_GRID, N_LEVEL_GRID, P_GRID, PSTAR, FIELD_GRID)
      N_LEVEL_GRID=N_LEVEL_GRID+1
!     READ IN THE NAME OF THE OUTPUT FILE.
      WRITE(IU_USER, '(/A)')
     &   'ENTER THE NAME OF THE OUTPUT FILE.'
      READ(IU_STDIN, '(A)') FILE_OUT
!
      WRITE(IU_USER, '(/A)') 'ENTER MODE OF INTERPOLATION.'
1     READ(IU_STDIN, *, IOSTAT=IOS) I_MODE
      IF (IOS.NE.0) THEN
         WRITE(IU_ERR, '(A)') '+++ UNRECOGNIZED RESPONSE:'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error_main.finc'
         ELSE
            WRITE(IU_USER, '(A)') 'PLEASE RE-ENTER.'
            GOTO 1
         ENDIF
      ENDIF
      IF ( (I_MODE.NE.IP_1_LIN_LIN).AND.
     &     (I_MODE.NE.IP_1_LOG_LIN).AND.
     &     (I_MODE.NE.IP_1_LIN_LOG).AND.
     &     (I_MODE.NE.IP_1_LOG_LOG).AND.
     6     (I_MODE.NE.IP_3_LIN_LIN).AND.
     &     (I_MODE.NE.IP_3_LOG_LIN).AND.
     &     (I_MODE.NE.IP_3_LIN_LOG).AND.
     &     (I_MODE.NE.IP_3_LOG_LOG) ) THEN
         WRITE(IU_ERR, '(A)') '+++ UNSUPPORTED MODE:'
         IF (LOCK_CODE(.TRUE.)) THEN
            INCLUDE 'batch_error_main.finc'
         ELSE
            WRITE(IU_ERR, '(A)') 'PLEASE RE-ENTER.'
            GOTO 1
         ENDIF
      ENDIF
!
!     PERFORM THE INTERPOLATION.
      DO L=1, N_PROFILE_IN
         DO I=1, N_LEVEL_IN
            A_IN(I)=FIELD_IN(L, I)
         ENDDO
         L_SPLINED=.FALSE.
         DO I=1, N_LEVEL_GRID
            CALL INTERPOLATE_P(IERR, N_LEVEL_IN, P_IN
     &         , A_IN, X_IN, Y_IN, Y2_IN, WORK
     &         , P_GRID(I), FIELD_OUT(L, I)
     &         , I_MODE, L_SPLINED
     &         )
            IF (IERR.EQ.I_ERR_FATAL) STOP
         ENDDO
      ENDDO
!
!     SET UP THE OUTPUT FIELD.
      TITLE_OUT(1:80)='Interpolated Field: '//TITLE_IN(1:60)
!
      CALL WRITE_SAMSON_FIELD(IERR, .FALSE.
     &   , FILE_OUT, TITLE_OUT
     &   , N_PROFILE_IN, N_LEVEL_GRID-1, P_GRID, PSTAR, FIELD_OUT)
!
!
      STOP
      END
