      SUBROUTINE GTSS (DSO4DT,DSOADT,PEDDN,PEN0,PENUM,PEPHI0,PEPSI, &
                       PEWETRC,T,RHOA,H2SO4I,SOAPI,SGPR,OGPR,DT, &
                       ILGA,LEVA,CNDS,CNDO)
!-----------------------------------------------------------------------
!     PURPOSE:
!     --------
!     GAS-TO-PARTICLE CONVERSION FOR SEA SALT.
!
!     HISTORY:
!     --------
!     * OCT  1/2017 - K.VONSALZEN   NEW, BASED ON GTSOA
!
!     ************************ INDEX OF VARIABLES **********************
!
!     PRIMARY INPUT/OUTPUT VARIABLES (ARGUMENT LIST):
!
! O   DSO4DT   GAS-PHASE SULPHURIC ACID (H2SO4) TENDENCY (KG/KG/SEC)
! O   DSOADT   GAS-PHASE SOA AEROSOL PRRECURSOR (SOAP) TENDENCY (KG/KG/SEC)
! O   CNDO     SOA PRECURSOR GAS CONDENSATION RATE (KG/KG/SEC)
! O   CNDS     (NH4)2SO4 CONDENSATION RATE (KG/KG/SEC)
! I   DT       TIME STEP (SEC)
! I   H2SO4I   INITIAL H2SO4 MIXING RATIO (KG/KG)
! I   ILGA     NUMBER OF GRID POINTS IN HORIZONTAL DIRECTION
! I   LEVA     NUMBER OF GRID POINTA IN VERTICAL DIRECTION
! I   OGPR     GAS-PHASE PRODUCTION RATE OF SOA PRECURSOR GASES (KG/KG/SEC)
! I   PEDDN    DENSITY OF DRY AEROSOL PARTICLE (KG/M3), EXT. MIXTURE
! I   PEN0     1ST PLA SIZE DISTRIBUTION PARAMETER (AMPLITUDE), EXT.
!              MIXTURE
! I   PENUM    AEROSOL NUMBER CONCENTRATION (1/KG), EXT. MIXTURE
! I   PEPHI0   3RD PLA SIZE DISTRIBUTION PARAMETER (MODE SIZE), EXT.
!              MIXTURE
! I   PEPSI    2ND PLA SIZE DISTRIBUTION PARAMETER (WIDTH), EXT. MIXTURE
! I   PEWETRC  TOTAL/WET PARICLE RADIUS (M), EXT. MIXTURE
! I   RHOA     AIR DENSITY (KG/M3)
! I   SGPR     GAS-PHASE PRODUCTION RATE OF H2SO4 (KG/KG/SEC)
! I   SOAPI    INITIAL SOA PRECURSOR GAS MIXING RATIO (KG/KG)
! I   T        AIR TEMPERATURE (K)
!
!     SECONDARY INPUT/OUTPUT VARIABLES (VIA MODULES AND COMMON BLOCKS):
!
! I   AEXTF%TP(:)%DRYRC(:)
!              DRY PARTICLE RADIUS IN SECTION CENTRE (M), EXT. MIXTURE
! I   AEXTF%TP(:)%DRYR(:)%VL
!              LOWER DRY PARTICLE RADIUS FOR EACH SECTION (M), EXT.
!              MIXTURE
! I   AEXTF%TP(:)%DRYR(:)%VR
!              UPPER DRY PARTICLE RADIUS FOR EACH SECTION (M), EXT.
!              MIXTURE
! I   IEXSS    AEROSOL TRACER INDEX FOR SEA SALT, EXT. MIXTURE
! I   ISAEXT   NUMBER OF AEROSOL TRACERS, EXT. MIXTURE
! I   ISEXTSS  NUMBER OF SECTIONS FOR SEA SALT AEROSOL, EXT. MIXTURE
! I   KEXTSS   SPECIES INDEX FOR SEA SALT AEROSOL, EXT. MIXTURE
! I   PEDPHIS  SECTION WIDTH, EXT. MIXTURE
! I   PEPHISS  LOWER DRY PARTICLE SIZE (=LOG(R/R0)), EXT. MIXTURE
! I   YLARGE   LARGE NUMBER
! I   YSEC     SECURITY PARAMETER FOR ADJUSTMENT OF TIMESTEP
! I   YSMALL   SMALL NUMBER
! I   YTINY    SMALLEST VALID NUMBER
!
!-----------------------------------------------------------------------
!
      USE SDPARM
      USE FPDEF
!
      IMPLICIT REAL (A-H,O-Z), INTEGER (I-N)
!
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA) :: DSO4DT,DSOADT
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA) :: CNDS,CNDO
      REAL, INTENT(IN), DIMENSION(ILGA,LEVA) :: SGPR,OGPR,T,RHOA,H2SO4I, &
                                                SOAPI
!
      REAL, INTENT(IN), DIMENSION(ILGA,LEVA,ISAEXT) :: &
                            PEDDN,PEN0,PENUM,PEPHI0,PEPSI,PEWETRC
      INTEGER, INTENT(IN) :: ILGA,LEVA
      REAL(R8), ALLOCATABLE, DIMENSION(:,:,:) :: TECNDGP,TECT1,TECT2
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: &
                                    TEWETRC,TEDDN,TECGR1,TECGR2,TEN0, &
                                    TEPHI0,TEPSI,TEDPHIT,TEPHIST, &
                                    TEDCCS,TEDCCO
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: &
                                    TEN
      REAL(R8), ALLOCATABLE, DIMENSION(:,:) :: DFSO4,DBL1,DBL2,ACND
      REAL, ALLOCATABLE, DIMENSION(:,:) :: &
                                    PLFN,CNDMS,CNDMSX,TERMX,TERM1,TERM2, &
                                    FRP,PLAM,PERTC,EXPT,SADTS,SADTO, &
                                    CTMPS,CTMPO,CNDMO,CNDMOX
      REAL, ALLOCATABLE, DIMENSION(:) :: &
                                    TEDRYR,TEDRYRC
!
!-----------------------------------------------------------------------
!     * ALLOCATE MEMORY FOR LOCAL FIELDS.
!
      IF ( ISEXTSS > 0 ) THEN
        ALLOCATE(TEWETRC(ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEN    (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEDDN  (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEN0   (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEPHI0 (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEPSI  (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TECT1  (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TECT2  (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TECGR1 (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TECGR2 (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TECNDGP(ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEDCCO (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEDCCS (ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEDPHIT(ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEPHIST(ILGA,LEVA,ISEXTSS))
        ALLOCATE(TEDRYR (ISEXTSS+1))
        ALLOCATE(TEDRYRC(ISEXTSS))
      ENDIF
      ALLOCATE(CTMPO  (ILGA,LEVA))
      ALLOCATE(CTMPS  (ILGA,LEVA))
      ALLOCATE(ACND   (ILGA,LEVA))
      ALLOCATE(PLFN   (ILGA,LEVA))
      ALLOCATE(CNDMS  (ILGA,LEVA))
      ALLOCATE(CNDMSX (ILGA,LEVA))
      ALLOCATE(CNDMO  (ILGA,LEVA))
      ALLOCATE(CNDMOX (ILGA,LEVA))
      ALLOCATE(TERMX  (ILGA,LEVA))
      ALLOCATE(TERM1  (ILGA,LEVA))
      ALLOCATE(TERM2  (ILGA,LEVA))
      ALLOCATE(DFSO4  (ILGA,LEVA))
      ALLOCATE(FRP    (ILGA,LEVA))
      ALLOCATE(PLAM   (ILGA,LEVA))
      ALLOCATE(PERTC  (ILGA,LEVA))
      ALLOCATE(EXPT   (ILGA,LEVA))
      ALLOCATE(SADTO  (ILGA,LEVA))
      ALLOCATE(SADTS  (ILGA,LEVA))
      ALLOCATE(DBL1   (ILGA,LEVA))
      ALLOCATE(DBL2   (ILGA,LEVA))
!
!     * TEMPORARY AEROSOL FIELDS FOR SEA SALT AEROSOL FOR DRY
!     * AND WET PARTICLE SIZES FOR EXTERNALLY MIXED AEROSOL TYPE.
!
      IF ( KEXTSS > 0 ) THEN
        DO IS=1,ISEXTSS
          TEWETRC(:,:,IS)=PEWETRC(:,:,IEXSS(IS))
          TEN    (:,:,IS)=PENUM  (:,:,IEXSS(IS))
          TEDDN  (:,:,IS)=PEDDN  (:,:,IEXSS(IS))
          TEN0   (:,:,IS)=PEN0   (:,:,IEXSS(IS))
          TEPHI0 (:,:,IS)=PEPHI0 (:,:,IEXSS(IS))
          TEPSI  (:,:,IS)=PEPSI  (:,:,IEXSS(IS))
          TEDPHIT(:,:,IS)=PEDPHIS(IEXSS(IS))
          TEPHIST(:,:,IS)=PEPHISS(IEXSS(IS))
          TEDRYRC(IS)=AEXTF%TP(KEXTSS)%DRYRC(IS)
          TEDRYR(IS)=AEXTF%TP(KEXTSS)%DRYR(IS)%VL
        ENDDO
        TEDRYR(ISEXTSS+1)=AEXTF%TP(KEXTSS)%DRYR(ISEXTSS)%VR
      ENDIF
!
!-----------------------------------------------------------------------
!     * DIFFUSIVITY OF H2SO4, MEAN FREE PATH, AND RATE COEFFICIENTS
!     * FOR CONDENSATION OF GASES ONTO PRE-EXISTING AEROSOL
!     * (CNDGP, IN 1/S).
!
      TERM1=SQRT(T)
      TERM1=TERM1*T
      DFSO4 = 2.150E-09_R8*TERM1
      DBL1=1.62864E-25/T
      DBL2=SQRT(DBL1)
      FRP = 3.372999311E+11_R8*DFSO4*DBL2
      WRAT=WAMSUL/WH2SO4
      ACND=0.
      IF ( ISEXTSS > 0 ) THEN
        CALL GRWTRM(TECNDGP,TECGR1,TECGR2,TECT1,TECT2,TEN0,TEN,TEWETRC, &
                    TEDRYRC,TEDRYR,TEDDN,TEPHI0,TEPSI,TEPHIST,TEDPHIT, &
                    FRP,DFSO4,WRAT,RHOA,LEVA,ILGA,ISEXTSS)
        ACND=ACND+SUM(TECNDGP,DIM=3)
      ENDIF
!
!-----------------------------------------------------------------------
!     * PARAMETERS FOR TIME-EVOLUTION OF GAS CONCENTRATIONS
!     * TO EQUILIBRIUM SOLUTION (CTMP).
!
      CTMPS=SGPR/MAX(ACND,YTINY)
      CTMPO=OGPR/MAX(ACND,YTINY)
      PLAM=ACND
      DBL1=-PLAM*DT
      DBL2=EXP(DBL1)
      EXPT=DBL2
!
!     * TIME-INTEGRATED H2SO4 CONCENTRATION (SADTS).
!
      PERTC=H2SO4I-CTMPS
      TERM2=PERTC*(EXPT-1.)
      WHERE ( PLAM <= MAX(ABS(TERM2)/YLARGE,YTINY) )
        SADTS=H2SO4I*DT+.5*SGPR*DT**2
      ELSEWHERE
        PLFN=TERM2/PLAM
        SADTS=CTMPS*DT-PLFN
      ENDWHERE
      SADTS=MAX(SADTS,0.)
!
!     * TIME-INTEGRATED SOA CONCENTRATION (SADTO).
!
      PERTC=SOAPI-CTMPO
      TERM2=PERTC*(EXPT-1.)
      WHERE ( PLAM <= MAX(ABS(TERM2)/YLARGE,YTINY) )
        SADTO=SOAPI*DT+.5*OGPR*DT**2
      ELSEWHERE
        PLFN=TERM2/PLAM
        SADTO=CTMPO*DT-PLFN
      ENDWHERE
      SADTO=MAX(SADTO,0.)
!
!-----------------------------------------------------------------------
!     * CHANGE IN GAS CONCENTRATION FROM CONDENSATION.
!
      IF ( ISEXTSS > 0 ) THEN
        DO IS=1,ISEXTSS
          TEDCCS(:,:,IS)=-TECNDGP(:,:,IS)*SADTS(:,:)/RHOA(:,:)
          TEDCCO(:,:,IS)=-TECNDGP(:,:,IS)*SADTO(:,:)/RHOA(:,:)
        ENDDO
      ENDIF
!
!     * DIAGNOSE CONDENSATION RATES.
!
      CNDS=0.
      CNDMS=0.
      IF ( ISEXTSS > 0 ) THEN
        TERMX=SUM(TEDCCS,DIM=3)
        CNDMS=CNDMS+TERMX
        CNDS=CNDS-TERMX/DT
      ENDIF
      CNDO=0.
      CNDMO=0.
      IF ( ISEXTSS > 0 ) THEN
        TERMX=SUM(TEDCCO,DIM=3)
        CNDMO=CNDMO+TERMX
        CNDO=CNDO-TERMX/DT
      ENDIF
!
!     * ADJUST CONDENSATION RATES TO MAKE SURE THAT TRUNCATION ERRORS
!     * IN CALCULATIONS OF CONDENSATION RATES DO NOT LEAD TO ANY
!     * NEGATIVE GAS CONCENTRATIONS.
!
      CNDMSX=-H2SO4I-SGPR*DT
      WHERE ( CNDMS < CNDMSX .AND. CNDMS < MIN(CNDMSX/YLARGE,-YTINY) )
        TERMX=CNDMSX/CNDMS
        CNDMS=CNDMSX
        CNDS=CNDS*TERMX
      ENDWHERE
      CNDMOX=-SOAPI-OGPR*DT
      WHERE ( CNDMO < CNDMOX .AND. CNDMO < MIN(CNDMOX/YLARGE,-YTINY) )
        TERMX=CNDMOX/CNDMO
        CNDMO=CNDMOX
        CNDO=CNDO*TERMX
      ENDWHERE
!
!-----------------------------------------------------------------------
!     * GAS TENDENCIES.
!
      DSO4DT=(SGPR*DT+CNDMS)/DT
      DSOADT=(OGPR*DT+CNDMO)/DT
!
!     * DEALLOCATION.
!
      IF ( ISEXTSS > 0 ) THEN
        DEALLOCATE(TEWETRC)
        DEALLOCATE(TEN    )
        DEALLOCATE(TEDDN  )
        DEALLOCATE(TEN0   )
        DEALLOCATE(TEPHI0 )
        DEALLOCATE(TEPSI  )
        DEALLOCATE(TECT1  )
        DEALLOCATE(TECT2  )
        DEALLOCATE(TECGR1 )
        DEALLOCATE(TECGR2 )
        DEALLOCATE(TECNDGP)
        DEALLOCATE(TEDCCO )
        DEALLOCATE(TEDCCS )
        DEALLOCATE(TEDPHIT)
        DEALLOCATE(TEPHIST)
        DEALLOCATE(TEDRYR )
        DEALLOCATE(TEDRYRC)
      ENDIF
      DEALLOCATE(CTMPO  )
      DEALLOCATE(CTMPS  )
      DEALLOCATE(ACND   )
      DEALLOCATE(PLFN   )
      DEALLOCATE(CNDMS  )
      DEALLOCATE(CNDMSX )
      DEALLOCATE(CNDMO  )
      DEALLOCATE(CNDMOX )
      DEALLOCATE(TERMX  )
      DEALLOCATE(TERM1  )
      DEALLOCATE(TERM2  )
      DEALLOCATE(DFSO4  )
      DEALLOCATE(FRP    )
      DEALLOCATE(PLAM   )
      DEALLOCATE(PERTC  )
      DEALLOCATE(EXPT   )
      DEALLOCATE(SADTO  )
      DEALLOCATE(SADTS  )
      DEALLOCATE(DBL1   )
      DEALLOCATE(DBL2   )
!
      END SUBROUTINE GTSS
