SUBROUTINE CH_ADD(POUT,P1,P2,KN,KNK,PREF,KMOD,NOMVAR) 3
C
      IMPLICIT NONE
C
C*    Declaration of arguments
C
      CHARACTER*(*) NOMVAR
      INTEGER KN,KNK,KMOD
      REAL*8 POUT(KN,KNK),P1(KN,KNK),P2(KN,KNK),PREF
C
*---------------------------------------------------------
#if defined (DOC)
*
***s/r CH_ADD - Add two arrays accounting for any special treatment.
*
**Author  : Y.J. Rochon ARQX/MSC July 2005
*
**Revisions:
*           Y. Yang ARQI Dec. 2006
*                   set minimum value of zref to 1.0e-20 to be consistent with 
*                   the minimum value for species set in the model
*           Y.J. Rochon ARQX 2007
*                   Set different ZMIN values for O3 and CH4.
*
*    -------------------
*
*Purpose: Add two arrays accounting for any special treatment.
*
*Arguments:
*
*   INPUT
* 
*     P1           Initial array
*     P2           Increment array
*     KN,KNK       Array dimensions
*     KMOD         Flag for specifying any special treatment.
*                  0 - no special treatment
*                  1 - force POUT values smaller than PREF to PREF. Reset P2
*                      accordingly.
*                  2 - force POUT values smaller than PREF*min(P1) to 
*                      PREF*min(P1) for each vertical level (PREF<=1.0 in 
*                      this case). Reset P2 accordingly.
*     PREF         Reference value for use when KMOD = 1 or 2
*
*   OUTPUT
*
*     POUT         Final array P1+P2
*     P2           Adjusted P2 if any special treatment applied to P1+P2
*
*Comments:
*
*-----------------------------------------------------------
#endif
C
C*    Declaration of local variables
C
      REAL*8 ZREF,ZMIN
      INTEGER J
C
      if (KMOD.eq.0) then
C
C        Straight addition - no special treatment
C
         pout=p1+p2
C
      else if (KMOD.eq.1.or.KMOD.eq.2) then
C
         IF (NOMVAR.EQ.'O3') THEN
            ZMIN=1.D-9
         ELSE IF (NOMVAR.EQ.'CH4') THEN
            ZMIN=1.D-8
         ELSE
            ZMIN=1.D-20
         END IF
         if (KMOD.eq.2) then
           zref=dmax1(pref*minval(p1(:,:)),ZMIN)
           write(6,*) 'ZMIN ',ZMIN,NOMVAR
         end if
C
         pout=p1+p2
         zref=pref
         do j=1,knk
            if (KMOD.eq.2) zref=dmax1(pref*minval(p1(:,j)),ZMIN)
            where (pout(:,j) < zref)
                  pout(:,j)=zref
                  p2(:,j)=pout(:,j)-p1(:,j)
            end where
         end do
         write(6,*) 'MINVAL ',minval(pout(:,:))
      end if
C
      RETURN
      END