!+ Subroutine to form composite array of pressures.
!
! Method:
!	Pressures in the background array are used as levels,
!	except in the region where the merged data are to be taken.
!
! 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 MERGE_PRESSURE(IERR
     &   , N_LEVEL_FRONT, P_FRONT, N_LEVEL_BACK, P_BACK
     &   , N_LEVEL_COMPOSITE, P_COMPOSITE
     &   , P_FRONT_LOW, P_FRONT_HIGH
     &   , L_TOP_ONLY
     &   )
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision.pcf'
      INCLUDE 'incl/samson_parm.ecf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N_LEVEL_FRONT
!             NUMBER OF LEVELS MERGING ARRAY
     &   , N_LEVEL_BACK
!             NUMBER OF LEVELS IN BACKGROUND
     &   , N_LEVEL_COMPOSITE
!             NUMBER OF COMPOSITE LEVELS
      LOGICAL
     &     L_TOP_ONLY
!             LOGICAL TO MERGE ONLY UPPER LEVELS
      REAL*8
     &     P_FRONT(NPD_LAYER+1)
!             MERGING PRESSURES
     &   , P_BACK(NPD_LAYER+1)
!             BACKGROUND PRESSURES
     &   , P_COMPOSITE(NPD_LAYER+1)
!             COMPOSITE ARRAY
     &   , P_FRONT_LOW
!             LOW PRESSURE OF MERGING FIELD
     &   , P_FRONT_HIGH
!             HIGH PRESSURE OF MERGING FIELD
!
!     LOCAL VARIABLES.
      INTEGER
     &     N_BACK
!             TEMPORARY NUMBER OF BACKGROUND LEVS
     &   , N_NEW
!             PUTATIVE SIZE OF MERGED ARRAY
     &   , I
!             LOOP VARIABLE
!
!
!     PRESSURES FORM THE ARRAY TO BE MERGED ARE INSERTED INTO THE
!     BACKGROUND ARRAY REPLACING OLD LEVELS IN THIS RANGE.
      N_BACK=1
1     IF (P_BACK(N_BACK).LT.P_FRONT_LOW*(1.0D+00-TOL_TEST)) THEN
         N_BACK=N_BACK+1
         IF (N_BACK.LT.N_LEVEL_BACK) GOTO 1
      ENDIF
      N_BACK=N_BACK-1
      DO I=1, N_BACK
         P_COMPOSITE(I)=P_BACK(I)
      ENDDO
      N_LEVEL_COMPOSITE=N_BACK
      N_NEW=N_LEVEL_COMPOSITE+N_LEVEL_FRONT
      IF (N_NEW.GT.NPD_LAYER+1) THEN
         WRITE(IU_ERR, '(/A)')
     &      '** ERROR: MERGED ARRAY EXCEEDS AVAILABLE SIZE: '
     &      //'INCREASE NPD_LAYER AND RECOMPILE.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
      DO I=1, N_LEVEL_FRONT
         P_COMPOSITE(N_BACK+I)=P_FRONT(I)
      ENDDO
      N_LEVEL_COMPOSITE=N_NEW
!
!     RETURN NOW UNLESS THE BOTTOM OF THE BACKGROUND FILE IS NEEDED.
      IF (L_TOP_ONLY) RETURN
!
!     P(N_BACK) MUST SATISFY THE TEST BELOW, BUT WE NEED TO INCREMENT
!     EXPLICITLY SINCE N_BACK MAY STILL BE 0.
      N_BACK=N_BACK+1
2     IF (P_BACK(N_BACK).LT.P_FRONT_HIGH*(1.0D+00+TOL_TEST)) THEN
         IF (N_BACK.LT.N_LEVEL_BACK) THEN
            N_BACK=N_BACK+1
            GOTO 2
         ELSE
!           NO MORE POINTS ARE NEEDED.
            RETURN
         ENDIF
      ENDIF
      N_NEW=N_LEVEL_COMPOSITE+N_LEVEL_BACK-N_BACK+1
      IF (N_NEW.GT.NPD_LAYER+1) THEN
         WRITE(IU_ERR, '(/A)')
     &      '*** ERROR: MERGED ARRAY EXCEEDS AVAILABLE SIZE: '
     &      //'INCREASE NPD_LAYER AND RECOMPILE.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
      DO I=1, N_LEVEL_BACK-N_BACK+1
         P_COMPOSITE(N_LEVEL_COMPOSITE+I)=P_BACK(N_BACK-1+I)
      ENDDO
      N_LEVEL_COMPOSITE=N_NEW
!
!
      RETURN
      END
