!+ Program to integrate mixing ratios vertically through the atmosphere.
!
! Method:
!	A SAMSON-file is read in. Its contents are assumed to be mixing
!	ratios on pressure levels. These pressure levels give the
!	masses in the layers so defined. The mean mixing ratio in the
!	layer is taken as the average of the values at the boundaries.
!	This enables us to calculate the amount of the quantity
!	integrating downwards. The results are written to a SAMSON-file.
!
! 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 VERT_INT
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/dimensions_field.ucf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
      INCLUDE 'incl/physical_constants_1.ccf'
      INCLUDE 'incl/samson_pstar.ecf'
!
!     DECLARATION OF VARIABLES.
      CHARACTER
     &     FILE_MIX*80
!             NAME OF FILE WITH MIXING RATIO
     &   , FILE_OUT*80
!             NAME OF FILE FOR OUTPUT
     &   , TITLE_DUMMY*80
!             DUMMY TITLE
     &   , TITLE_OUT*80
!             OUTPUT TITLE
      INTEGER
     &     IERR
!             ERROR FLAG
     &   , N_PROFILE
!             NUMBER OF LEVELS
     &   , N_LEVEL
!             NUMBER OF LEVELS
     &   , I
!             LOOP VARIABLE
     &   , L
!             LOOP VARIABLE
      REAL*8
     &     P(1, NPD_LAYER+1)
!             PRESSURE LEVELS
     &   , MIX_RATIO(1, NPD_LAYER+1)
!             MIXING RATIO
     &   , AMOUNT(1, NPD_LAYER+1)
!             AMOUNTS OF QUANTITY
     &   , PSTAR
!             SURFACE PRESSURE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     READ_SAMSON_FIELD, WRITE_SAMSON_FIELD
!
!
      DATA TITLE_OUT(1:27)
     &   /'Vertically integrated Field'/
      DATA PSTAR/PSTAR_DEFAULT/
!
!
!
      WRITE(IU_USER, '(/A)')
     &     'ENTER NAME OF FILE CONTAINING THE MIXING RATIOS.'
      READ(IU_STDIN, '(A)') FILE_MIX
      CALL READ_SAMSON_FIELD(IERR, .TRUE., .FALSE.
     &   , FILE_MIX, TITLE_DUMMY
     &   , N_PROFILE, N_LEVEL, P, PSTAR, MIX_RATIO)
      IF (IERR.NE.I_NORMAL) STOP
      N_LEVEL=N_LEVEL+1
!
      DO L=1, N_PROFILE
         AMOUNT(L, 1)=0.0D+00
      ENDDO
      DO I=2, N_LEVEL
         DO L=1, N_PROFILE
            AMOUNT(L, I)=AMOUNT(L, I-1)
     &         +0.5D+00*(MIX_RATIO(L, I-1)+MIX_RATIO(L, I))
     &         *(P(L, I)-P(L, I-1))/GRAV_ACC
         ENDDO
      ENDDO
!
      WRITE(IU_USER, '(/A)') 'ENTER NAME OF OUTPUT FILE.'
      READ(IU_STDIN, '(A)') FILE_OUT
      CALL WRITE_SAMSON_FIELD(IERR, .FALSE.
     &   , FILE_OUT, TITLE_OUT
     &   , N_PROFILE, N_LEVEL-1, P, PSTAR, AMOUNT)
      IF (IERR.NE.I_NORMAL) STOP
!
!
      STOP
      END
