      SUBROUTINE SSGEN (PEDNDT,PEDMDT,PIDFDT,PIDNDT,PIDMDT,PEMAS, &
                        PENUM,PIMAS,PINUM,PEDPHI0,PEPHIS0,PIDPHI0, &
                        PIPHIS0,PIFRC,PIDDN,ZSPD,FOCN, &
                        SICN,DT,SICN_CRT,ILGA,LEVA)
!-----------------------------------------------------------------------
!     PURPOSE:
!     --------
!     SEA SALT GENERATION
!
!     HISTORY:
!     --------
!     * FEB 10/2015 - K.VONSALZEN   INTRODUCTION OF SUBGRID OCEAN FRACTION
!     * MAR 11/2009 - K.VONSALZEN   CALCULATE TENDENCIES INSTEAD OF
!     *                             DIRECT ADJUSTMENT
!     * JAN 12/2009 - K.VONSALZEN   REMOVE DENSITY CALCULATIONS AND
!     *                             RESTRICT CALCULATIONS FOR INTERNALLY
!     *                             MIXED AEROSOL TO SURFACE
!     * JUN 14/2007 - K.VONSALZEN   NEW
!
!-----------------------------------------------------------------------
!
      USE SDPARM
!
      IMPLICIT REAL (A-H,O-Z), INTEGER (I-N)
!
      REAL, INTENT(IN),  DIMENSION(ILGA,LEVA,ISAEXT) :: PEDPHI0,PEPHIS0
      REAL, INTENT(IN),  DIMENSION(ILGA,LEVA,ISAINT) :: PIDPHI0,PIPHIS0
      REAL, INTENT(IN),  DIMENSION(ILGA,LEVA,ISAINT) :: PIMAS,PINUM, &
                                                        PIDDN
      REAL, INTENT(IN),  DIMENSION(ILGA,LEVA,ISAEXT) :: PEMAS,PENUM
      REAL, INTENT(IN),  DIMENSION(ILGA,LEVA,ISAINT,KINT) :: PIFRC
      REAL, INTENT(IN),  DIMENSION(ILGA) :: ZSPD,FOCN,SICN
      REAL, INTENT(IN) :: DT,SICN_CRT
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA,ISAEXT) :: PEDNDT,PEDMDT
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA,ISAINT) :: PIDNDT,PIDMDT
      REAL, INTENT(OUT), DIMENSION(ILGA,LEVA,ISAINT,KINT) :: PIDFDT
      REAL, ALLOCATABLE, DIMENSION(:,:) :: AEPHI0,AEN0,AEPSI
      REAL, ALLOCATABLE, DIMENSION(:,:) :: AIPHI0,AIN0,AIPSI
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: BEPHI0,BEN0,BEPSI,BEDDN, &
                                             BEDPHI0,BEPHIS0,BENUM, &
                                             BEMAS
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: BIPHI0,BIN0,BIPSI,BIDDN, &
                                             BIDPHI0,BIPHIS0,BINUM, &
                                             BIMAS
      REAL, ALLOCATABLE, DIMENSION(:,:) :: CIMAST,CINUMT
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: CIMAF
      REAL, ALLOCATABLE, DIMENSION(:) :: FWAT
!
!-----------------------------------------------------------------------
!
!     * ALLOCATION OF ARRAYS AND INITIALIZATION.
!
      ALLOCATE (FWAT (ILGA))
      IF ( ISEXTSS > 0 ) THEN
        ALLOCATE (AEPHI0 (ILGA,ISEXTSS))
        ALLOCATE (AEN0   (ILGA,ISEXTSS))
        ALLOCATE (AEPSI  (ILGA,ISEXTSS))
        ALLOCATE (BEPHI0 (ILGA,1,ISEXTSS))
        ALLOCATE (BEN0   (ILGA,1,ISEXTSS))
        ALLOCATE (BEPSI  (ILGA,1,ISEXTSS))
        ALLOCATE (BEDDN  (ILGA,1,ISEXTSS))
        ALLOCATE (BEDPHI0(ILGA,1,ISEXTSS))
        ALLOCATE (BEPHIS0(ILGA,1,ISEXTSS))
        ALLOCATE (BENUM  (ILGA,1,ISEXTSS))
        ALLOCATE (BEMAS  (ILGA,1,ISEXTSS))
      ENDIF
      IF ( ISINTSS > 0 ) THEN
        ALLOCATE (AIPHI0 (ILGA,ISINTSS))
        ALLOCATE (AIN0   (ILGA,ISINTSS))
        ALLOCATE (AIPSI  (ILGA,ISINTSS))
        ALLOCATE (BIPHI0 (ILGA,1,ISINTSS))
        ALLOCATE (BIN0   (ILGA,1,ISINTSS))
        ALLOCATE (BIPSI  (ILGA,1,ISINTSS))
        ALLOCATE (BIDDN  (ILGA,1,ISINTSS))
        ALLOCATE (BIDPHI0(ILGA,1,ISINTSS))
        ALLOCATE (BIPHIS0(ILGA,1,ISINTSS))
        ALLOCATE (BINUM  (ILGA,1,ISINTSS))
        ALLOCATE (BIMAS  (ILGA,1,ISINTSS))
        ALLOCATE (CIMAST (ILGA,ISINTSS))
        ALLOCATE (CINUMT (ILGA,ISINTSS))
      ENDIF
      IF ( ISAINT > 0 ) THEN
        ALLOCATE (CIMAF  (ILGA,ISAINT,KINT))
      ENDIF
      IF ( KEXT > 0 ) THEN
        PEDNDT=0.
        PEDMDT=0.
      ENDIF
      IF ( KINT > 0 ) THEN
        PIDNDT=0.
        PIDMDT=0.
        PIDFDT=0.
      ENDIF
!
!     * SEA SALT EQUILIBRIUM SIZE DISTRIBUTIONS.
!
      FWAT=0.
      DO IL=1,ILGA
        IF ( SICN(IL) .LT. SICN_CRT ) THEN
          FWAT(IL)=FOCN(IL)*(1.-SICN(IL))
        ENDIF
      ENDDO
      IF ( KEXTSS > 0 ) THEN
        CALL SSFCC(AEN0,AEPHI0,AEPSI,ZSPD,FWAT,ILGA,ISEXTSS)
      ENDIF
      IF ( KINTSS > 0 ) THEN
        CALL SSFCC(AIN0,AIPHI0,AIPSI,ZSPD,FWAT,ILGA,ISINTSS)
      ENDIF
!
!     * CONVERT RESULTS.
!
      IF ( KEXTSS > 0 ) THEN
        BEN0   (:,1,:)=AEN0 (:,:)
        BEPHI0 (:,1,:)=AEPHI0(:,:)
        BEPSI  (:,1,:)=AEPSI (:,:)
        DO IS=1,ISEXTSS
          BEDDN  (:,1,IS)=AEXTF%TP(KEXTSS)%DENS
          BEDPHI0(:,1,IS)=PEDPHI0(:,LEVA,IEXSS(IS))
          BEPHIS0(:,1,IS)=PEPHIS0(:,LEVA,IEXSS(IS))
        ENDDO
      ENDIF
      IF ( KINTSS > 0 ) THEN
        BIN0   (:,1,:)=AIN0 (:,:)
        BIPHI0 (:,1,:)=AIPHI0(:,:)
        BIPSI  (:,1,:)=AIPSI (:,:)
        DO IS=1,ISINTSS
          BIDDN  (:,1,IS)=AINTF%TP(KINTSS)%DENS
          BIDPHI0(:,1,IS)=PIDPHI0(:,LEVA,IINSS(IS))
          BIPHIS0(:,1,IS)=PIPHIS0(:,LEVA,IINSS(IS))
        ENDDO
      ENDIF
!
!     * MASS AND NUMBER CONCENTRATIONS.
!
      IF ( KEXTSS > 0 ) THEN
        CALL PLA2NM(BENUM,BEMAS,BEN0,BEPHI0,BEPSI,BEPHIS0,BEDPHI0, &
                    BEDDN,ILGA,1,ISEXTSS)
      ENDIF
      IF ( KINTSS > 0 ) THEN
        CALL PLA2NM(BINUM,BIMAS,BIN0,BIPHI0,BIPSI,BIPHIS0,BIDPHI0, &
                    BIDDN,ILGA,1,ISINTSS)
      ENDIF
!
!     * MASS AND NUMBER TENDENCIES FOR EXTERNALLY MIXED AEROSOL.
!
      DO IS=1,ISEXTSS
      DO IL=1,ILGA
        IF (       ABS(BENUM(IL,1,IS)-YNA) > YTINY &
             .AND. ABS(BEMAS(IL,1,IS)-YNA) > YTINY ) THEN
          PEDNDT(IL,LEVA,IEXSS(IS))=FWAT(IL)*(BENUM(IL,1,IS) &
                                    -PENUM(IL,LEVA,IEXSS(IS)))/DT
          PEDMDT(IL,LEVA,IEXSS(IS))=FWAT(IL)*(BEMAS(IL,1,IS) &
                                    -PEMAS(IL,LEVA,IEXSS(IS)))/DT
        ENDIF
      ENDDO
      ENDDO
!
!     * TOTAL AEROSOL MASS AND NUMBER FOR INTERNALLY MIXED TYPES OF
!     * AEROSOL AFTER EXTRACTION OF SEA SALT FOR INITIAL AEROSOL
!     * SIZE DISTRIBUTIONS. THE IMPLICIT ASSUMPTION FOR THE CHANGE
!     * IN AEROSOL NUMBER IS THAT THE AEROSOL NUMBER FOR EACH SPECIES
!     * IS PROPORTIONAL TO THE VOLUME FRACTION OF THE SPECIES.
!
      IF ( KINTSS > 0 ) THEN
        DO IS=1,ISINTSS
          CIMAST(:,IS)=PIMAS(:,LEVA,IINSS(IS))
          CINUMT(:,IS)=PINUM(:,LEVA,IINSS(IS))
        ENDDO
        DO IS=1,ISINTSS
        DO IL=1,ILGA
          IF (          ABS(BINUM(IL,1,IS)-YNA) > YTINY &
                  .AND. ABS(BIMAS(IL,1,IS)-YNA) > YTINY ) THEN
            CIMAST(IL,IS)=CIMAST(IL,IS) &
              -PIFRC(IL,LEVA,IINSS(IS),KINTSS)*PIMAS(IL,LEVA,IINSS(IS))
            CINUMT(IL,IS)=CINUMT(IL,IS) &
             -PINUM(IL,LEVA,IINSS(IS))*(PIFRC(IL,LEVA,IINSS(IS),KINTSS) &
                       *PIDDN(IL,LEVA,IINSS(IS))/AINTF%TP(KINTSS)%DENS)
            CIMAST(IL,IS)=MAX(CIMAST(IL,IS),0.)
            CINUMT(IL,IS)=MAX(CINUMT(IL,IS),0.)
          ENDIF
        ENDDO
        ENDDO
      ENDIF
!
!     * TOTAL AEROSOL MASS AND NUMBER AFTER ADDITION OF EQUILIBRIUM
!     * SEA SALT AEROSOL CONCENTRATIONS.
!
      IF ( KINTSS > 0 ) THEN
        DO IS=1,ISINTSS
        DO IL=1,ILGA
          IF (          ABS(BINUM(IL,1,IS)-YNA) > YTINY &
                  .AND. ABS(BIMAS(IL,1,IS)-YNA) > YTINY ) THEN
            CIMAST(IL,IS)=CIMAST(IL,IS)+BIMAS(IL,1,IS)
            CINUMT(IL,IS)=CINUMT(IL,IS)+BINUM(IL,1,IS)
          ENDIF
        ENDDO
        ENDDO
      ENDIF
!
!     * ADJUST TOTAL CONCENTRATIONS AFTER EMISSION OF SEA SALT TO
!     * ACCOUNT FOR LACK OF EMISSIONS FROM NON-OCEAN FRACTION OF GRID CELL.
!
      IF ( KINTSS > 0 ) THEN
        DO IS=1,ISINTSS
          CIMAST(:,IS)=(1.-FWAT(:))*PIMAS(:,LEVA,IINSS(IS)) + &
                       FWAT(:)*CIMAST(:,IS)
          CINUMT(:,IS)=(1.-FWAT(:))*PINUM(:,LEVA,IINSS(IS)) + &
                       FWAT(:)*CINUMT(:,IS)
        ENDDO
      ENDIF
!
!     * NEW SEA SALT MASS FRACTION FOR INTERNALLY MIXED TYPES OF
!     * AEROSOL.
!
      IF ( KINT > 0 ) THEN
        CIMAF=PIFRC(:,LEVA,:,:)
      ENDIF
      IF ( KINTSS > 0 ) THEN
        DO IS=1,ISINTSS
        DO IL=1,ILGA
          IF (             CIMAST(IL,IS) > YTINY &
                  .AND. ABS(BINUM(IL,1,IS)-YNA) > YTINY &
                  .AND. ABS(BIMAS(IL,1,IS)-YNA) > YTINY ) THEN
            CIMAF(IL,IINSS(IS),:)=CIMAF(IL,IINSS(IS),:) &
                                *PIMAS(IL,LEVA,IINSS(IS))/CIMAST(IL,IS)
            CIMAF(IL,IINSS(IS),KINTSS)= &
                                (1.-FWAT(IL))*CIMAF(IL,IINSS(IS),KINTSS) &
                                  +FWAT(IL)*BIMAS(IL,1,IS)/CIMAST(IL,IS)
          ELSE
            CIMAF(IL,IINSS(IS),:)=PIFRC(IL,LEVA,IINSS(IS),:)
          ENDIF
        ENDDO
        ENDDO
      ENDIF
!
!     * MASS, NUMBER, AND MASS FRACTION TENDENCIES FOR INTERNALLY
!     * MIXED TYPES OF AEROSOL.
!
      IF ( KINT > 0 ) THEN
        PIDFDT(:,LEVA,:,:)=(CIMAF-PIFRC(:,LEVA,:,:))/DT
      ENDIF
      IF ( KINTSS > 0 ) THEN
        DO IS=1,ISINTSS
          PIDNDT(:,LEVA,IINSS(IS))=(CINUMT(:,IS) &
                                   -PINUM(:,LEVA,IINSS(IS)))/DT
          PIDMDT(:,LEVA,IINSS(IS))=(CIMAST(:,IS) &
                                   -PIMAS(:,LEVA,IINSS(IS)))/DT
        ENDDO
      ENDIF
!
!     * DEALLOCATION OF WORK ARRAYS.
!
      DEALLOCATE (FWAT )
      IF ( ISEXTSS > 0 ) THEN
        DEALLOCATE (AEPHI0 )
        DEALLOCATE (AEN0   )
        DEALLOCATE (AEPSI  )
        DEALLOCATE (BEPHI0 )
        DEALLOCATE (BEN0   )
        DEALLOCATE (BEPSI  )
        DEALLOCATE (BEDDN  )
        DEALLOCATE (BEDPHI0)
        DEALLOCATE (BEPHIS0)
        DEALLOCATE (BENUM  )
        DEALLOCATE (BEMAS  )
      ENDIF
      IF ( ISINTSS > 0 ) THEN
        DEALLOCATE (AIPHI0 )
        DEALLOCATE (AIN0   )
        DEALLOCATE (AIPSI  )
        DEALLOCATE (BIPHI0 )
        DEALLOCATE (BIN0   )
        DEALLOCATE (BIPSI  )
        DEALLOCATE (BIDDN  )
        DEALLOCATE (BIDPHI0)
        DEALLOCATE (BIPHIS0)
        DEALLOCATE (BINUM  )
        DEALLOCATE (BIMAS  )
        DEALLOCATE (CIMAST )
        DEALLOCATE (CINUMT )
      ENDIF
      IF ( ISAINT > 0 ) THEN
        DEALLOCATE (CIMAF)
      ENDIF
!
      END SUBROUTINE SSGEN
