SUBROUTINE SURAND1

!**** *SURAND1*  - Initialize stochastic physics parameters - part 1

!     Purpose.
!     --------
!           Initialize stochastic physics

!**   Interface.
!     ----------
!        *CALL* *SURAND1

!        Explicit arguments :
!        --------------------
!        None

!        Implicit arguments :
!        --------------------
!        COMMON STOPH_MIX

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        None

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        R Buizza and L Isaksen - ECMWF - 

!     Modifications.
!     --------------
!        Original : 98-01-01
!        M. Fisher: 99-04-16 Use SETRAN to set the random number seeds
!        J. Barkmeijer:00-11-06 Use LFORCENL to use perturbed tendencies
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        J.Berner 06-08-11 added stoch phys options for cellular automaton 
!                          backscatter and  spectral AR backscatter 
!        G. Shutts 09-03-4 spectral backscatter update
!        M. Steinheimer 09-03-05 added option RVP
!        M. Steinheimer 09-10-27 spectral backscatter update (changes to RVP,
!                          added option LSTOPH_SPBS_FAST)
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM, JPRB
USE YOMHOOK   ,ONLY : LHOOK, DR_HOOK

USE YOMCT0   , ONLY : LALLOPR, NPRINTLEV, NPROC, LSPBSBAL
USE YOMCST   , ONLY : RPI,RA
USE YOMDIM   , ONLY : NPROMA,NFLEVG,NGPBLKS,NSMAX,NSPEC2,NFLSUR
USE YOMDYN   , ONLY : TDT,HDIRVOR
USE YOMGEM   , ONLY : NGPTOT, VAF, VBF, VP00, VDELA, VDELB
USE YOMGRB   , ONLY : NENSFNB 
USE YOMLUN   , ONLY : NULOUT, NULNAM, NULTMP, NULERR
USE YOMMP    , ONLY : MYPROC
USE YOMMSC   , ONLY : N_DEFAULT_REAL_KIND, N_DOUBLE_KIND
USE YOMTAG   , ONLY : MTAGFCE 

USE YOMRANDOM_STREAMS, ONLY : YD_RANDOM_STREAM_STOCHPHYS, YD_RANDOM_STREAM_STOCHPHYS_CABS, & 
 & YD_RANDOM_STREAM_STOCHPHYS_SPBS, YD_RANDOM_STREAM_STOCHPHYS_RVP, &
 & YD_RANDOM_STREAM_STOPH_SPBS_T, YD_RANDOM_STREAM_STOPH_RVP_T
USE MPL_MODULE, ONLY : MPL_BROADCAST
USE STOPH_MIX , ONLY : LSTOPH, NFRSTOPH, NFRSTOPH_SPBS, NFRSTOPH_VC, NSTOCHOPT,&
 &  AMAGSTOPH, ADLATSTOPH, ADLONSTOPH, RSTOPHU, RSTOPHV, RSTOPHT, SQRTCORR, &
 & RSTOPHQ,  LFORCENL, NFORCESTART, NFORCEEND, & 
 & LSTOPH_SPBS,LEXTRAFIELDS,LVORTCON,LSTOPH_JBCOR,LSTOPH_UNCORR,LSTOPH_UNIFORM, &
 & SPSTREAM, SPVELPOT, SPSTREAM_FORC, SPVELPOT_FORC, SPG_AMP, RSMOOTH, &
 & GPTOTDISS, GPTOTDISS_SMOOTH, GPSTREAM, GPVELPOT, GPVORTGRAD, RATIO_APE2KE, &
 & SLDISSFAC,ALPHA_STO,ALPHA_DEEP_CONV,ALPHA_SHAL_CONV,RATIO_BACKSCAT,RATIO_BACKSCAT_CON2NUM, & 
 & LSTOPH_CASBS, RSTOPHCA, AMAGSTOPH_CASBS, ADLATSTOPH_CA, REXPONENT, &
 & ADLONSTOPH_CA,  MCELL, RWGHT, INITIALIZE_CELLS, RFLUX_DET_CLIP,    &
 & VC_CON, BIHARM, NIMRAN, LSTOPH_RVP, RVP_MULMIN, RVP_MULMAX, &
 & RVP_MULEXP, RVP_MULNSMAX, RVP_MULFACT, &
 & LSTOPH_TAPER, TAPER_SIGMATOP, TAPER_SIGMABOT, TAPER0, TAPER1, TAPER_FACT, &
 & TAPER2, TAPER3, LSTOPH_INI, LSTOPH_SPBS_VORT, &
 & RVP_MUL_A, RVP_MUL_B, RVP_MUL_C, RVP_MUL_D, RVP_MUL, ONEMINALPHA_NFRSPBS, &
 & RVP_MUL_1, RVP_MUL_2, RVP_MUL_1_T, RVP_MUL_2_T, &
 & LSTOPH_VARALPHA, LSTOPH_SPBS_FAST, NFRSTOPH_SPBS_PAT, &
 & LSTOPH_SPBS_T,REXPONENT_T,SPTEMP,SPTEMP_FORC,SPG_AMP_T,ALPHA_STO_T,ONEMINALPHA_NFRSPBS_T,GPTEMP, &
 & RVP_MULMIN_T, RVP_MULMAX_T, RVP_MULEXP_T, RVP_MULNSMAX_T, RVP_MULFACT_T, &
 & RVP_MUL_A_T, RVP_MUL_B_T, RVP_MUL_C_T, RVP_MUL_D_T, RVP_MUL_T, LSTOPH_GAUSS, RSMOOTHSCALE, &
 & LSPBSNORM, SPDP,RSPBS_TAU,SIGTOFACT,LSTOPH_RVPOLD,NSMAXSPBS

 USE YOE_CUCONVCA, ONLY : LCUCONV_CA
IMPLICIT NONE

#include "namstoph.h"

#include "fcttim.h"

!     ------------------------------------------------------------------

REAL(KIND=JPRB) ,DIMENSION(0:NSMAX) :: ZCHI

INTEGER(KIND=JPIM) :: IN,IFILT,ICUTOFF,IIP,JJP
INTEGER(KIND=JPIM) :: JN, I, IU, ISPINUP, ISTEP
INTEGER(KIND=JPIM) :: INLEVS,  JX, JY, ITAG, INFO, IOMASTER, J1, J2, JLEV

LOGICAL :: LLP
CHARACTER(LEN=10) :: CLID
CHARACTER(LEN=40) :: CLFILE

REAL(KIND=JPRB) :: ZCORRMAT(NFLEVG,NFLEVG)
REAL(KIND=JPRB) :: ZSQRTCORR2(NFLEVG,NFLEVG)
REAL(KIND=JPRB) :: ZGAMMAN,ZCONSTF0,ZSIGMA2_EPS
REAL(KIND=JPRB) :: ZDSIGMA
REAL(KIND=JPRB) :: ZSIGMA
REAL(KIND=JPRB) :: ZSIGMA_SMOOTH
REAL(KIND=JPRB) :: Zafilt
REAL(KIND=JPRB) :: ZP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

REAL(KIND=JPRB) :: ZSIGMA_NFRSPBS
REAL(KIND=JPRB) :: ZRVP_MULFACT
INTEGER(KIND=JPIM) :: IRVP_MULNSMAX

#include "abor1.intfb.h"
#include "posnam.intfb.h"
#include "setran.intfb.h"
#include "surand2.intfb.h"

!*    1. Set default values.
!     ----------------------

!     1.1 Set implicit default values for stochastic diabatic tendencies
!     LSTOPH     : logical, if .T. physical tendencies are perturbed
!     NFRSTOPH   : frequency (number of time steps) between two random 
!                  number generation calls
!     NFRSTOPH_SPBS : frequency (number of time steps) between calls to 
!                    refresh the streamfunction forcing in SPBS (must be >= 2)
!                    if CASBS run it must be set to 1
!     NFRSTOPH_VC   :  frequency (number of time steps) between calls to vorticity confinement
!     AMAGSTOPH  : magnitute of stochastic perturbation: denoting by
!                  p the tendency and by dp the tendency perturbation, then
!                  (1-AMAGSTOPH)<dp/p<(1+AMAGSTOPH)
!     ADLATSTOPH : latitudinal size of the box of grid points with the
!                  same random number (in degrees)
!     ADLONSTOPH : longitudinal size of the box of grid points with the
!                  same random number (in degrees)

!     1.2 Set implicit default values for CASBS and/or SPBS
!     ALPHA_STO       : Autoregressive parameter (function of n) 
!     ALPHA_DEEP_CONV : Entrainment cloud fraction for deep convection
!     ALPHA_SHAL_CONV : Entrainment cloud ascent fraction for shallow and mid-level convection
!     AMAGSTOPH_CASBS : Magnitude of forcing 
!     ADLATSTOPH_CA   : Gridsize of cellular automaton in zonal direction
!     ADLONSTOPH_CA   : Gridsize of cellular automaton in meridional direction
!     RATIO_BACKSCAT  : Backscatter ratio
!     RATIO_BACKSCAT_CON2NUM  : Convective Backscatter ratio/Numerical backscatter ratio
!     LSTOPH_CASBS    : .T. to add cellular automaton streamfunction perturbations
!     LSTOPH_SPBS     : .T. to add spectral backscatter streamfunction perturbations
!     LVORTCON        : .T. to switch on vorticity confinement
!     LEXTRAFIELDS    : .T. to print extrafields
!     LSTOPH_JBCOR    : .T. if perturbations are vertically correlated like Jb vorticity
!     LSTOPH_UNCORR   : .T. to make SPBS perturbations vertically uncorrelated
!     LSTOPH_UNIFORM  : .T. to make SPBS perturbations uniformly distributed, instead
!                        of Gaussian distributed
!     LSTOPH_RVP      : .T. to use random vertical profiles in SPBS
!     LSTOPH_SPBS_FAST: .T. pattern is only updated every NFRSTOPH_SPBS timesteps (statistically equivalent)
!     LSTOPH_VARALPHA : .T. to use scale-dependen decorrelation time in SPBS

IF (LHOOK) CALL DR_HOOK('SURAND1',0,ZHOOK_HANDLE)
!     -----------------------------------------------------------------
!*    1.1 Set default values for stochastic diabatic tendencies and vorticity confinement
!     -----------------------------------------------------------------
LSTOPH=.FALSE.
NFRSTOPH=6
AMAGSTOPH=0.5_JPRB
ADLATSTOPH=10._JPRB
ADLONSTOPH=10._JPRB
LFORCENL=.FALSE.
NFORCESTART=0
NFORCEEND=240

!     -----------------------------------------------------------------
!*    1.2 Set default values for spectral backscatter
!     -----------------------------------------------------------------
LEXTRAFIELDS=.FALSE.
LSTOPH_SPBS=.FALSE.
LSTOPH_JBCOR=.FALSE.
LSTOPH_UNCORR=.FALSE.
LSTOPH_UNIFORM=.FALSE.
LSTOPH_RVP=.TRUE.
LSTOPH_RVPOLD=.FALSE.
LSTOPH_TAPER=.TRUE.
LSTOPH_INI=.TRUE.
LSTOPH_VARALPHA=.FALSE.
LSTOPH_SPBS_FAST=.TRUE.
LSTOPH_SPBS_VORT=.FALSE.
LSTOPH_GAUSS=.TRUE.
LSPBSNORM=.TRUE.

NSMAXSPBS=NSMAX  !note default setting in scripts/modeleps might be different!

!   REXPONENT produces (3-2*REXPONENT)-kinetic energy forcing spectrum 
REXPONENT=-1.27_JPRB 
RATIO_BACKSCAT= 0.095_JPRB
RATIO_BACKSCAT_CON2NUM= 1.0_JPRB
RFLUX_DET_CLIP= 0.005_JPRB  !  upper limit for convective mass flux detrainment rate
RSPBS_TAU    =  2.5E04_JPRB 
ZSIGMA2_EPS=1._JPRB/12.0_JPRB
ISPINUP=1    

! set defaults for temperature backscatter

LSTOPH_SPBS_T=.FALSE.
REXPONENT_T= REXPONENT
RATIO_APE2KE= 0.2

!     -----------------------------------------------------------------
!*    1.2 Set default values for random vertical profiles in spectral backscatter
!     -----------------------------------------------------------------
!for N dependency
RVP_MULMIN=0._JPRB
RVP_MULMAX=1.00_JPRB
RVP_MULEXP=0.10_JPRB
RVP_MULNSMAX=255._JPRB
!for p dependency
RVP_MUL_A=1.6857964_JPRB
RVP_MUL_B=-0.0059995210E-02_JPRB
RVP_MUL_C=1.1070694E-09
RVP_MUL_D=-6.5077043E-15
RVP_MUL_1=18.0192_JPRB
RVP_MUL_2=-0.2159_JPRB

!for T-backscatter
!for N dependency
RVP_MULMIN_T=0._JPRB
RVP_MULMAX_T=1.00_JPRB
RVP_MULEXP_T=0.10_JPRB
RVP_MULNSMAX_T=255._JPRB
!for p dependency
RVP_MUL_A_T=1.6857964_JPRB
RVP_MUL_B_T=-0.0059995210E-02_JPRB
RVP_MUL_C_T=1.1070694E-09
RVP_MUL_D_T=-6.5077043E-15
RVP_MUL_1_T=10.5608_JPRB
RVP_MUL_2_T=-0.2959_JPRB

!     -----------------------------------------------------------------
!*    1.3 Set default values for cellular automaton stochastic backscatter
!     -----------------------------------------------------------------
LSTOPH_CASBS=.FALSE.
AMAGSTOPH_CASBS=2.0_JPRB
ADLATSTOPH_CA= 180._JPRB/NINT(90._JPRB*NSMAX/159.0_JPRB)
ADLONSTOPH_CA= ADLATSTOPH_CA
IIP= NINT(360._JPRB/ADLONSTOPH_CA)
JJP= NINT(180._JPRB/ADLATSTOPH_CA)

!     -----------------------------------------------------------------
!*    1.4 Set default values common to spectral backscatter and CASBS
!     -----------------------------------------------------------------
NFRSTOPH_SPBS=4     ! must be > 2
NFRSTOPH_SPBS_PAT=NFRSTOPH_SPBS
NSTOCHOPT=2 
SLDISSFAC= 3._JPRB
BIHARM=SLDISSFAC*(TDT/HDIRVOR)*(RA**2/(NSMAX*(NSMAX+1)))**2
RSMOOTHSCALE=350000._JPRB

!     -----------------------------------------------------------------
!*    1.5 Set default value for vorticity confinement constant
!     -----------------------------------------------------------------
LVORTCON=.FALSE.
NFRSTOPH_VC= 2
VC_CON= 0.3_JPRB
!     -----------------------------

IU = NULOUT

!*    2. Read Namelist
!     ----------------

CALL POSNAM(NULNAM,'NAMSTOPH')
READ(NULNAM,NAMSTOPH)

IF (LSTOPH_CASBS) NFRSTOPH_SPBS=1  ! variable frequency calls to CASBS not implemented yet

IF (NSTOCHOPT==1) THEN
  ALPHA_SHAL_CONV=2.6E-02_JPRB  
  ALPHA_DEEP_CONV=2.1E-03_JPRB 
ELSEIF (NSTOCHOPT==2) THEN
  ALPHA_SHAL_CONV=2.0_JPRB !not used at the moment (see callpar.F90)
  ALPHA_DEEP_CONV=2.0_JPRB 
ENDIF


WRITE(NULOUT,'('' >> Sub SURAND1 ,,'')')

IF (LFORCENL) WRITE(NULOUT,'('' >> add perturbation to model tendencies '')')

!     -----------------------------------------------------------------
!     2.1  If LSTOPH=.T. stochastic physics, option:  stochastic diabatic tendencies
!     -----------------------------------------------------------------

LLP = NPRINTLEV >= 1.OR. LALLOPR

IF (LSTOPH) THEN
  WRITE(NULOUT,'('' Logical to control stochastic physics, LSTOPH='',L1)') LSTOPH  
  WRITE(NULOUT,'('' >> initializing stochastic physics, option: stochastic diabatic tendencies,'')')


  WRITE(NULOUT,'('' >> Setup for stochastic physics '')')
  WRITE(NULOUT,'('' Variables in common STOPH_MIX'')')
  WRITE(NULOUT,'('' Frequency of random generation NFRSTOPH ='',I10)')  NFRSTOPH  
  WRITE(NULOUT,'(&
   & '' Magnitude of stochastic perturbation AMAGSTOPH ='',E12.5)')&
   & AMAGSTOPH  
  WRITE(NULOUT,'('' Box size in lat (deg) ADLATSTOPH ='',E12.5)') ADLATSTOPH
  WRITE(NULOUT,'('' Box size in long (deg) ADLONSTOPH='',E12.5)') ADLONSTOPH

!*    2.1.1  Allocate 
!     ---------------

  ALLOCATE(RSTOPHU(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHU  ',SIZE(RSTOPHU  ),SHAPE(RSTOPHU  )
  ALLOCATE(RSTOPHV(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHV  ',SIZE(RSTOPHV  ),SHAPE(RSTOPHV  )
  ALLOCATE(RSTOPHT(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHT  ',SIZE(RSTOPHT  ),SHAPE(RSTOPHT  )
  ALLOCATE(RSTOPHQ(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHQ  ',SIZE(RSTOPHQ  ),SHAPE(RSTOPHQ  )

!*    2.1.2  Seed for random number generation
!       Generate a unique number from the date and the ensemble member
!       number (NENSFNB from yomgrb.h)
!     ----------------------------------------

  CALL SETRAN (NENSFNB,YD_RANDOM_STREAM_STOCHPHYS)

!       Generate first set of random numbers

ELSE
  ALLOCATE(RSTOPHU(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHU  ',SIZE(RSTOPHU  ),SHAPE(RSTOPHU  )
  ALLOCATE(RSTOPHV(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHV  ',SIZE(RSTOPHV  ),SHAPE(RSTOPHV  )
  ALLOCATE(RSTOPHT(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHT  ',SIZE(RSTOPHT  ),SHAPE(RSTOPHT  )
  ALLOCATE(RSTOPHQ(NGPTOT))
  IF(LLP)WRITE(IU,9) 'RSTOPHQ  ',SIZE(RSTOPHQ  ),SHAPE(RSTOPHQ  )
9 FORMAT(1X,'ARRAY ',A10,' ALLOCATED ',8I8)
ENDIF

!     -----------------------------------------------------------------
!     2.2  If LSTOPH_CASBS=.T. stochastic physics, option:  CASBS
!     -----------------------------------------------------------------
IF (LSTOPH_CASBS) THEN
  WRITE(NULOUT,'('' Logical to control stochastic physics, LSTOPH_CASBS='',L1)') LSTOPH_CASBS  
  WRITE(NULOUT,'('' >> initializing stochastic physics, option:  CASBS ,,'')')

  WRITE(NULOUT,'('' >> Setup for stochastic physics '')')
  WRITE(NULOUT,'('' Variables in common STOPH_MIX'')')
  WRITE(NULOUT,'(&
   & '' Magnitude of stochastic perturbation AMAGSTOPH_CASBS ='',E12.5)')&
   & AMAGSTOPH_CASBS
  WRITE(NULOUT,'('' Box size in lat (deg) ADLATSTOPH_CA ='',E12.5)') ADLATSTOPH_CA
  WRITE(NULOUT,'('' Box size in long (deg) ADLONSTOPH_CA='',E12.5)') ADLONSTOPH_CA

!*    2.2.1  Allocate 
!     ---------------
  ALLOCATE( SPSTREAM_FORC(NFLSUR, NSPEC2) ) 

!*    2.2.2  Allocate CASBS only
!     --------------------------
  ALLOCATE(RSTOPHCA(NGPTOT))
  RSTOPHCA=0.0
  ALLOCATE(MCELL(IIP*4,JJP*4))
  ALLOCATE(RWGHT(IIP,JJP))

!*   2.2.3  Seed for random number generation
!       Generate a unique number from the date and the ensemble member
!       number (NENSFNB from yomgrb.h) and the processor element PE
!       initialise the cellular automaton by randomly seeding with living cell clumps
!     ----------------------------------------

  CALL SETRAN (NENSFNB,YD_RANDOM_STREAM_STOCHPHYS_CABS)

  IF (.NOT. LCUCONV_CA) CALL INITIALIZE_CELLS(MCELL,RWGHT,IIP,JJP)

ENDIF

!     -----------------------------------------------------------------
!     2.3  If LSTOPH_SPBS=.T. stochastic physics, option:  SPBS
!     -----------------------------------------------------------------
IF (LSTOPH_SPBS) THEN
  IF (NSMAXSPBS > NSMAX .OR. NSMAXSPBS < 1) THEN
     NSMAXSPBS=NSMAX
  ENDIF

  WRITE(NULOUT,'('' Logical to control new stochastic physics, LSTOPH_SPBS='',L1)') LSTOPH_SPBS
  WRITE(NULOUT,'('' >> initializing stochastic physics, option:  SPBS ,,'')')
  WRITE(NULOUT,'('' >> Setup for stochastic physics SPBS '')')
  WRITE(NULOUT,'('' Variables in module STOPH_MIX'')')
  WRITE(NULOUT,'('' Logical to control vorticity ansatz, LSTOPH_SPBS_VORT='',L1)') LSTOPH_SPBS_VORT
  WRITE(NULOUT,'('' Forcing only wavenumbers 1 to '',I5)')  NSMAXSPBS
  WRITE(NULOUT,'('' Backscatter ratio, RATIO_BACKSCAT '',E12.5)') RATIO_BACKSCAT
  WRITE(NULOUT,'('' Backscatter ratio, RATIO_BACKSCAT_CON2NUM '',E12.5)') RATIO_BACKSCAT_CON2NUM
  WRITE(NULOUT,'('' Exponent for energy spectra, REXPONENT ='',E12.5)') REXPONENT
  WRITE(NULOUT,'('' Decorrelation time of noise, RSPBS_TAU ='',E12.5)') RSPBS_TAU
  WRITE(NULOUT,'('' Variance of noise, ZSIGMA2_EPS  ='',E12.5)') ZSIGMA2_EPS
  WRITE(NULOUT,'('' Frequency of random generation NFRSTOPH_SPBS ='',I10)')  NFRSTOPH_SPBS
  WRITE(NULOUT,'('' Calling pattern update only every NFRSTOPH_SPBS timestep ='',L1)')  LSTOPH_SPBS_FAST
  WRITE(NULOUT,'('' Bi-harmonic diffusion coefficient (BIHARM) x dt x SLDISSFAC ='',E13.5)')  BIHARM
  WRITE(NULOUT,'('' Force balanced stochastic perturbations ='',L1)')  LSPBSBAL
  WRITE(NULOUT,'('' If true, random numbers drawn from uniform distribution,'',&
   & ''otherwise from a Gaussian distribution='',L1)')  LSTOPH_UNIFORM
  WRITE(NULOUT,'('' Total dissipion rate computed using: '')') 
  WRITE(NULOUT,'('' Convective Mass Flux clipped at '',E12.5)') RFLUX_DET_CLIP
  IF (NSTOCHOPT==1) THEN
    WRITE(NULOUT,'(''   Massflux-scheme '')') 
  ELSEif (NSTOCHOPT==2) THEN
    WRITE(NULOUT,'(''   Updraft scheme '')') 
  ENDIF
  WRITE(NULOUT,'('' PBL tapering, LSTOPH_TAPER '',L1)')  LSTOPH_TAPER
  WRITE(NULOUT,'('' Markov chain initialization, LSTOPH_INI '',L1)')  LSTOPH_INI
  WRITE(NULOUT,'('' Gaussian smoothing of dissipation rate, LSTOPH_GAUSS '',L1)')  LSTOPH_GAUSS
  WRITE(NULOUT,'(''    smoothing decorrelation scale, RSMOOTHSCALE '',E12.5)')  RSMOOTHSCALE
  WRITE(NULOUT,'('' Pattern correlation for SPBS, LSTOPH_JBCOR, LSTOPH_UNCORR, LSTOPH_RVP '',3L1)') &
                & LSTOPH_JBCOR, LSTOPH_UNCORR, LSTOPH_RVP 
  IF (LSTOPH_JBCOR) THEN
    WRITE(NULOUT,'(''   Vorticity correlation matrix used for SPBS random perturbation correlation '')')
  ELSEIF (LSTOPH_UNCORR) THEN
    WRITE(NULOUT,'(''   Each model level is perturbed with uncorrelated random variables '')')
  ELSEIF (LSTOPH_RVP) THEN
    WRITE(NULOUT,'(''   Random profiles are used for vertical correlation in SPBS '')')
    WRITE(NULOUT,'(''      Parameters used for RVP: '')')
    WRITE(NULOUT,'(''         RVP_MULMIN= '',E12.5)') RVP_MULMIN
    WRITE(NULOUT,'(''         RVP_MULMAX= '',E12.5)') RVP_MULMAX
    WRITE(NULOUT,'(''         RVP_MULEXP= '',E12.5)') RVP_MULEXP
    WRITE(NULOUT,'(''         RVP_MULNSMAX= '',E12.5)') RVP_MULNSMAX
    IF (LSTOPH_RVPOLD) THEN
      WRITE(NULOUT,'(''      Old p-dependency is used '')')
      WRITE(NULOUT,'(''         RVP_MUL_A= '',E12.5)') RVP_MUL_A
      WRITE(NULOUT,'(''         RVP_MUL_B= '',E12.5)') RVP_MUL_B
      WRITE(NULOUT,'(''         RVP_MUL_C= '',E12.5)') RVP_MUL_C
      WRITE(NULOUT,'(''         RVP_MUL_D= '',E12.5)') RVP_MUL_D
    ELSE
      WRITE(NULOUT,'(''         RVP_MUL_1= '',E12.5)') RVP_MUL_1
      WRITE(NULOUT,'(''         RVP_MUL_2= '',E12.5)') RVP_MUL_2
    ENDIF
  ELSEIF ((.not.LSTOPH_UNCORR).and.(.not.LSTOPH_JBCOR).and.(.not.LSTOPH_RVP)) then
    WRITE(NULOUT,'(''   Each model level is perturbed with perfectly correlated random variables '')')
  ENDIF
  IF ((LSTOPH_UNCORR .and. LSTOPH_JBCOR).or.(LSTOPH_UNCORR .and. LSTOPH_RVP).or.(LSTOPH_JBCOR .and. LSTOPH_RVP)) THEN
    CALL ABOR1('SURAND1: Pattern correlation settings in surand1.F90 are inconsistent: & 
    & Pattern can only be one option of uncorrelated, jb-correlated &
    & or random vertical profile')
  ENDIF
  IF (LSTOPH_VARALPHA) THEN
    WRITE(NULOUT,'(''   Scale-dependent decorrelation time used for SPBS '')')
  ENDIF
  WRITE(NULOUT,'('' ------------------- vorticity confinement -------------------'')')
  WRITE(NULOUT,'('' Logical to control vorticity confinement, LVORTCON='',L1)') LVORTCON
  WRITE(NULOUT,'('' ------------------- temperature backscatter -------------------'')')
  WRITE(NULOUT,'('' Logical to control T-backscatter, LSTOPH_SPBS_T='',L1)') LSTOPH_SPBS_T
  IF (LSTOPH_SPBS_T) THEN
    WRITE(NULOUT,'('' settings for T-backscatter:'')')
    WRITE(NULOUT,'(''   Exponent for T spectra, REXPONENT_T ='',E12.5)') REXPONENT_T
    WRITE(NULOUT,'(''   RATIO_APE2KE ='',E12.5)') RATIO_APE2KE
    IF (LSTOPH_RVP) THEN
      WRITE(NULOUT,'(''   Random profiles are used for vertical correlation in T-backscatter '')')
      WRITE(NULOUT,'(''      Parameters used for RVP: '')')
      WRITE(NULOUT,'(''         RVP_MULMIN_T= '',E12.5)') RVP_MULMIN_T
      WRITE(NULOUT,'(''         RVP_MULMAX_T= '',E12.5)') RVP_MULMAX_T
      WRITE(NULOUT,'(''         RVP_MULEXP_T= '',E12.5)') RVP_MULEXP_T
      WRITE(NULOUT,'(''         RVP_MULNSMAX_T= '',E12.5)') RVP_MULNSMAX_T
      IF (LSTOPH_RVPOLD) THEN
        WRITE(NULOUT,'(''      Old p-dependency is used '')')
        WRITE(NULOUT,'(''         RVP_MUL_A_T= '',E12.5)') RVP_MUL_A_T
        WRITE(NULOUT,'(''         RVP_MUL_B_T= '',E12.5)') RVP_MUL_B_T
        WRITE(NULOUT,'(''         RVP_MUL_C_T= '',E12.5)') RVP_MUL_C_T
        WRITE(NULOUT,'(''         RVP_MUL_D_T= '',E12.5)') RVP_MUL_D_T
      ELSE
        WRITE(NULOUT,'(''         RVP_MUL_1_T= '',E12.5)') RVP_MUL_1_T
        WRITE(NULOUT,'(''         RVP_MUL_2_T= '',E12.5)') RVP_MUL_2_T
      ENDIF
    ENDIF
  ENDIF
!*    2.3.1  Allocate SPBS only
!     -------------------------
  ALLOCATE( SPSTREAM(NFLSUR, NSPEC2) )
  ALLOCATE( SPSTREAM_FORC(NFLSUR, NSPEC2) )   !  and CASBS
  ALLOCATE( SPVELPOT(NFLSUR, NSPEC2) ) 
  ALLOCATE( SPVELPOT_FORC(NFLSUR, NSPEC2) )
  ALLOCATE( SPG_AMP(0:NSMAX) )
  ALLOCATE(RSTOPHCA(NGPTOT))
  ALLOCATE( ONEMINALPHA_NFRSPBS(0:NSMAX) )
  ALLOCATE( ALPHA_STO(0:NSMAX) )

  SPSTREAM=  0._JPRB 
  SPSTREAM_FORC=  0._JPRB
  SPVELPOT=  0._JPRB 
  SPVELPOT_FORC=  0._JPRB
  ALPHA_STO=0._JPRB
  ONEMINALPHA_NFRSPBS=0._JPRB

  IF (LSTOPH_SPBS_T) THEN
    ALLOCATE( SPTEMP(NFLSUR, NSPEC2) )
    ALLOCATE( SPTEMP_FORC(NFLSUR, NSPEC2) )
    ALLOCATE( SPG_AMP_T(0:NSMAX) )
    ALLOCATE( ONEMINALPHA_NFRSPBS_T(0:NSMAX) )
    ALLOCATE( ALPHA_STO_T(0:NSMAX) )

    SPTEMP=0._JPRB 
    SPTEMP_FORC=0._JPRB
    ALPHA_STO_T=0._JPRB
    ONEMINALPHA_NFRSPBS_T=0._JPRB
  ENDIF


  IF (LSTOPH_SPBS_FAST) THEN
     NFRSTOPH_SPBS_PAT=NFRSTOPH_SPBS
  ELSE
     NFRSTOPH_SPBS_PAT=1
  ENDIF



  DO IN=0, NSMAXSPBS
!    ALPHA_STO(IN)   =  (1._JPRB- EXP(-TDT/RSPBS_TAU))*TANH(((IN+1)/40._JPRB))/TANH(1._JPRB)  
    IF (LSTOPH_VARALPHA) THEN  
        ALPHA_STO(IN)   = (1+IN)*TDT/(30*86400.0)  !  4 hrs < tau < 30 days
    ELSE
        ALPHA_STO(IN)   = 1._JPRB- EXP(-TDT/RSPBS_TAU) 
    ENDIF

   ONEMINALPHA_NFRSPBS(IN)=(1-ALPHA_STO(IN))**NFRSTOPH_SPBS_PAT
    !WRITE(NULOUT,*) 'n=',IN,'  alpha_sto(n)=', ALPHA_STO(IN)

    IF (LSTOPH_SPBS_T) THEN
      IF (LSTOPH_VARALPHA) THEN  
        ALPHA_STO_T(IN)   = (1+IN)*TDT/(30*86400.0)  !  4 hrs < tau < 30 days
      ELSE
        ALPHA_STO_T(IN)   = 1._JPRB- EXP(-TDT/RSPBS_TAU) 
      ENDIF
      ONEMINALPHA_NFRSPBS_T(IN)=(1-ALPHA_STO_T(IN))**NFRSTOPH_SPBS_PAT
    ENDIF

  ENDDO


!*    2.3.2.  More setup for stochastic backscatter: Set up forcing spectrum
!     ---------------------------------------------------------------------------
  ZCHI    =  0._JPRB
 
  do IN=1,NSMAXSPBS
    ZCHI(IN)=(IN)**REXPONENT
  enddo
  
  ZGAMMAN  =  0._JPRB
  DO IN=0,NSMAXSPBS
    ZGAMMAN= ZGAMMAN + IN*(IN+1)*(2*IN+1)*ZCHI(IN)**2/ALPHA_STO(IN)
  ENDDO

  ZCONSTF0=RA*SQRT( 8._JPRB*RPI/(TDT*ZSIGMA2_EPS*ZGAMMAN) ) 


  SPG_AMP= 0._JPRB
  DO JN=1,NSMAXSPBS
      ZSIGMA_NFRSPBS=1.0
      DO IN=1,NFRSTOPH_SPBS_PAT-1
        ZSIGMA_NFRSPBS=ZSIGMA_NFRSPBS+(1-ALPHA_STO(JN))**(2*IN)
      ENDDO
      ZSIGMA_NFRSPBS=SQRT(ZSIGMA_NFRSPBS)
      SPG_AMP(JN) = ZSIGMA_NFRSPBS*SQRT(ALPHA_STO(JN))*ZCONSTF0*ZCHI(JN)
  ENDDO

  IF (LSTOPH_SPBS_T) THEN
    ZCHI    =  0._JPRB
 
    do IN=1,NSMAXSPBS
      ZCHI(IN)=(IN)**REXPONENT_T
    enddo
  
    ZGAMMAN  =  0._JPRB
    DO IN=0,NSMAXSPBS
      ZGAMMAN= ZGAMMAN + IN*(IN+1)*(2*IN+1)*ZCHI(IN)**2/ALPHA_STO_T(IN)
    ENDDO

    ZCONSTF0=RA*SQRT( 8._JPRB*RPI/(TDT*ZSIGMA2_EPS*ZGAMMAN) ) 


    SPG_AMP_T = 0._JPRB
    DO JN=1,NSMAXSPBS
      ZSIGMA_NFRSPBS=1.0
      DO IN=1,NFRSTOPH_SPBS_PAT-1
        ZSIGMA_NFRSPBS=ZSIGMA_NFRSPBS+(1-ALPHA_STO_T(JN))**(2*IN)
      ENDDO
      ZSIGMA_NFRSPBS=SQRT(ZSIGMA_NFRSPBS)
      SPG_AMP_T(JN) = ZSIGMA_NFRSPBS*SQRT(ALPHA_STO_T(JN))*ZCONSTF0*ZCHI(JN)
    ENDDO
  ENDIF

!*    2.3.3   Setup index in a global spectral array for random numbers (m, n=m)
!        For random number arrays wavnumbers m must be in increasing order
!        ([m=0,n=0], [0,1], [0,2],...,[1,1], [1,2],...,[2,2], [2,3],..,[NSMAX,NSMAX])
!        to insure reproducibility of random numbers for different numbers of processors

  ALLOCATE(NIMRAN(0:NSMAX))

  IN=0
  DO JN=0,NSMAX
      NIMRAN(JN)=IN+1
      IN=IN+(NSMAX-JN+1)*2
  ENDDO



!*   2.3.4  Seed for random number generation
!       Generate a unique number from the date and the ensemble member
!       number (NENSFNB from yomgrb.h)
!     ----------------------------------------

  CALL SETRAN (NENSFNB,YD_RANDOM_STREAM_STOCHPHYS_SPBS) 
  IF (LSTOPH_SPBS_T)  CALL SETRAN ((NENSFNB+24)*420,YD_RANDOM_STREAM_STOPH_SPBS_T) 

!*    2.3.5  For random vertical profiles of SPBS perturbations: 
!       - Setup of parameters
!       - Generate a unique random number seed from the date, the ensemble member
!       number (NENSFNB from yomgrb.h) and the wavenumber.
!     ----------------------------------------

  IF (LSTOPH_RVP) THEN
    ALLOCATE(RVP_MULFACT(0:NSMAX))
    RVP_MULFACT=RVP_MULMAX
    ZRVP_MULFACT=(RVP_MULMAX-RVP_MULMIN)/((1.*RVP_MULNSMAX)**RVP_MULEXP)
    IRVP_MULNSMAX=MIN(NSMAX,INT(RVP_MULNSMAX))
    DO JN=0,IRVP_MULNSMAX
      RVP_MULFACT(JN)=ZRVP_MULFACT*(1.*JN)**RVP_MULEXP+RVP_MULMIN
      !WRITE(NULOUT,*) 'n=',JN,'  RVP_MULFACT(n)=', RVP_MULFACT(JN)
    ENDDO

    ALLOCATE(RVP_MUL(NFLEVG))

    IF(LSTOPH_RVPOLD) THEN
      DO JN=1,NFLEVG
          !calculate levelpressure
          ZP=VAF(JN)+VBF(JN)*VP00
          !WRITE (NULOUT,*) '  P(',JN,')=',ZP
          !pressure depentent random number scaling
          RVP_MUL(JN)=RVP_MUL_A+RVP_MUL_B*ZP+RVP_MUL_C*ZP**2+RVP_MUL_D*ZP**3
      ENDDO
    ELSE
      DO JN=2,NFLEVG
          !calculate levelpressure
          ZP=((VAF(JN)+VBF(JN)*VP00)*(VAF(JN-1)+VBF(JN-1)*VP00))**0.5_JPRB !middle (in terms of log(p/ps)) between full levels

          !pressure depentent random number scaling
          RVP_MUL(JN)=SQRT((RVP_MUL_1*(-LOG(ZP/VP00))**RVP_MUL_2)* &
                    &  LOG((VAF(JN)+VBF(JN)*VP00)/(VAF(JN-1)+VBF(JN-1)*VP00)))
          !WRITE(NULOUT,*) 'lev=',JN,'  RVP_MUL(lev)=',RVP_MUL(JN),'  VP00=',VP00
          RVP_MUL(JN)=SIGTOFACT(RVP_MUL(JN))
          !WRITE(NULOUT,*) 'lev=',JN,'  RVP_MUL(lev)=',RVP_MUL(JN)
      ENDDO
    ENDIF

    CALL SETRAN (NENSFNB+100,YD_RANDOM_STREAM_STOCHPHYS_RVP) 

    IF (LSTOPH_SPBS_T) THEN
      RVP_MULFACT_T=(RVP_MULMAX_T-RVP_MULMIN_T)/((1.*RVP_MULNSMAX_T)**RVP_MULEXP_T)
      ALLOCATE(RVP_MULFACT_T(0:NSMAX))
      RVP_MULFACT_T=RVP_MULMAX_T
      ZRVP_MULFACT=(RVP_MULMAX_T-RVP_MULMIN_T)/((1.*RVP_MULNSMAX_T)**RVP_MULEXP_T)
      IRVP_MULNSMAX=MIN(NSMAX,INT(RVP_MULNSMAX_T))
      DO JN=0,IRVP_MULNSMAX
        RVP_MULFACT_T(JN)=ZRVP_MULFACT*(1.*JN)**RVP_MULEXP_T+RVP_MULMIN_T
        !WRITE(NULOUT,*) 'n=',JN,'  RVP_MULFACT_T(n)=', RVP_MULFACT_T(JN)
      ENDDO

      ALLOCATE(RVP_MUL_T(NFLEVG))

      IF(LSTOPH_RVPOLD) THEN
        DO JN=1,NFLEVG
          !calculate levelpressure
          ZP=VAF(JN)+VBF(JN)*VP00
          !pressure depentent random number scaling
          RVP_MUL_T(JN)=RVP_MUL_A_T+RVP_MUL_B_T*ZP+RVP_MUL_C_T*ZP**2+RVP_MUL_D_T*ZP**3
        ENDDO
      ELSE
        DO JN=2,NFLEVG
          !calculate levelpressure
          ZP=((VAF(JN)+VBF(JN)*VP00)*(VAF(JN-1)+VBF(JN-1)*VP00))**0.5_JPRB !middle (in terms of log(p/ps)) between full levels

          !pressure depentent random number scaling
          RVP_MUL_T(JN)=SQRT((RVP_MUL_1_T*(-LOG(ZP/VP00))**RVP_MUL_2_T)* &
                      &  LOG((VAF(JN)+VBF(JN)*VP00)/(VAF(JN-1)+VBF(JN-1)*VP00)))
          !WRITE(NULOUT,*) 'lev=',JN,'  RVP_MUL_T(lev)=',RVP_MUL_T(JN),'  VP00=',VP00
          RVP_MUL_T(JN)=SIGTOFACT(RVP_MUL_T(JN))
          !WRITE(NULOUT,*) 'lev=',JN,'  RVP_MUL_T(lev)=',RVP_MUL_T(JN)
        ENDDO
      ENDIF

      CALL SETRAN (NENSFNB+420,YD_RANDOM_STREAM_STOPH_RVP_T) 
    ENDIF
  ENDIF


!*    2.3.6  For JB type vertical correlations of SPBS perturbations read a vorticity correlation matrix
!            Calculate sqrt of matrix, distribute result and use in surand2.F90
!             to calculate vertically correlated patterns
!     ----------------------------------------

  IF (LSTOPH_JBCOR) THEN

    !       Open the correlation matrix file and skip past the header

    IOMASTER=1
    ITAG = MTAGFCE+1

    IF (ALLOCATED(SQRTCORR)) DEALLOCATE(SQRTCORR)
    ALLOCATE(SQRTCORR(NFLEVG,NFLEVG))

    IF (MYPROC==IOMASTER) THEN
      CLFILE='vorcorrmatrix.cv'
      WRITE (NULOUT,*) '  Opening input file ',CLFILE
      OPEN  (NULTMP,FILE=clfile,FORM='unformatted')
      READ  (NULTMP) CLID
      IF (CLID(1:7)/='VORCORR') CALL ABOR1('Bad Vorticity correlation matrix for SPBS')
      READ  (NULTMP)
      READ  (NULTMP)

      READ (NULTMP) INLEVS
      READ (NULTMP)

      IF (INLEVS /= NFLEVG) THEN
        WRITE (NULOUT,*) 'INLEVS=',INLEVS,'   NFLEVG=',NFLEVG
        CALL ABOR1('Vorticity correlation matrix size not NFLEVGxNFLEVG')
      ENDIF
      DO JY=1,NFLEVG
        READ (NULTMP) (ZCORRMAT(JX,JY),JX=1,NFLEVG)
      ENDDO

      WRITE(NULOUT,'(/35X,'' Vorticity correlations (*100) '')')
      WRITE(NULOUT,832) (JLEV, JLEV=1,NFLEVG)
      DO J1=1,NFLEVG
        WRITE(NULOUT,831) (NINT(100._JPRB*ZCORRMAT(J2,J1)),J2=1,NFLEVG)
      ENDDO
      831 FORMAT(1X,100I3)
      832 FORMAT(/1X,100I3)

      if (JPRB == N_DEFAULT_REAL_KIND) then
        call SPOTRF ('U',NFLEVG,ZCORRMAT,NFLEVG,INFO)
      ELSEIF (JPRB == N_DOUBLE_KIND) then
        call DPOTRF ('U',NFLEVG,ZCORRMAT,NFLEVG,INFO)
      else
       call ABOR1 ('REAL(KIND=JPRB) is neither default real nor double precision')
      endif

      if (info > 0) then
        write (NULERR,*)'surand1: error computing Cholesky decomposition'
        write (NULERR,*) 'SPOTRF/DPOTRF returns info=',info
        write (NULERR,*) 'The matrix is is not positive definite'
        call ABOR1('surand1: error computing Cholesky decomposition')
      elseif (info < 0) then
        write (NULERR,*)'surand1: error computing Cholesky decomposition'
        write (NULERR,*) 'SPOTRF/DPOTRF returns info=',info
        write (NULERR,*) 'Illegal value found in matrix'
        call ABOR1('surand1: error computing Cholesky decomposition')
      endif

      SQRTCORR(:,:)=ZCORRMAT(:,:)


      DO J1=2,NFLEVG
        DO J2=1,J1-1
          SQRTCORR(J1,J2)=0.
        ENDDO
      ENDDO


      DO J2=1,NFLEVG
        DO J1=1,NFLEVG
          ZSQRTCORR2(J1,J2)=SQRTCORR(J1,J2)**2
        ENDDO
      ENDDO

      IF (LLP)  THEN
        WRITE(NULOUT,'(/35X,'' SQRT Vorticity correlations (*100) '')')
        WRITE(NULOUT,832) (JLEV, JLEV=1,NFLEVG)
        DO J1=1,NFLEVG
          WRITE(NULOUT,831) (NINT(100._JPRB*SQRTCORR(J2,J1)),J2=1,NFLEVG)
        ENDDO

        WRITE(NULOUT,'(/35X,'' SQRT Vorticity variance estimate for all levels '')')
        WRITE(NULOUT,833) (JLEV, JLEV=1,NFLEVG)
        WRITE(NULOUT,834) (SUM(ZSQRTCORR2(:,J2)),J2=1,NFLEVG)
        833 FORMAT(1X,100(I4,2x))
        834 FORMAT(/1X,100(F5.3,1x))
      ENDIF

    ENDIF

    ! Distribute the sqrt of the correlation matrix to all PEs

    IF (NPROC>1) THEN
      CALL MPL_BROADCAST (SQRTCORR,ITAG,IOMASTER,CDSTRING='SURAND1:')
    ENDIF

  ENDIF

!*    2.3.7  Setup for PBL tapering.
!     ----------------------------------------

  IF (LSTOPH_TAPER) THEN
    TAPER_SIGMATOP=0.87_JPRB
    TAPER_SIGMABOT=0.97_JPRB

    ZDSIGMA = 1._jprb/(TAPER_SIGMABOT-TAPER_SIGMATOP)**3

   TAPER3 =  2._jprb                      *ZDSIGMA
   TAPER2 = -3._jprb*(TAPER_SIGMATOP+TAPER_SIGMABOT)*ZDSIGMA
   TAPER1 =  6._jprb* TAPER_SIGMATOP*TAPER_SIGMABOT *ZDSIGMA
   TAPER0 =  TAPER_SIGMABOT**2 * &
       &  (TAPER_SIGMABOT - 3._jprb*TAPER_SIGMATOP) *ZDSIGMA

   ALLOCATE(TAPER_FACT(NFLEVG))

    DO JN=1,NFLEVG
       !calculate levelsigma
       ZSIGMA=VAF(JN)/VP00+VBF(JN)
       IF (ZSIGMA > TAPER_SIGMABOT) THEN
          !
          !   below transition layer
          !
              TAPER_FACT(JN) =0._JPRB
       ELSE
          IF (ZSIGMA > TAPER_SIGMATOP) THEN
             !
             !   in transition layer
             !
             TAPER_FACT(JN)=((TAPER3*ZSIGMA+TAPER2)*ZSIGMA+TAPER1)*ZSIGMA+TAPER0
          ELSE
             TAPER_FACT(JN) =1._JPRB
          ENDIF
       ENDIF
    ENDDO
  ENDIF

ENDIF

!--------------------------------------------------------------------------

IF (LSTOPH_SPBS.OR.LSTOPH_CASBS.OR.LVORTCON) THEN
!     -----------------------------------------------------------------
!*    2.x.1  Allocate  fields common to SPBS and CASBS
!     -----------------------------------------------------------------
  WRITE(NULOUT,'('' Entrainment cloud fraction for deep convection, ALPHA_DEEP_CONV  ='',E12.5)') & 
    &  ALPHA_DEEP_CONV
  WRITE(NULOUT,'('' Entrainment cloud fraction for shallow convection, ALPHA_DEEP_CONV  ='',E12.5)') & 
    &  ALPHA_SHAL_CONV
  WRITE(NULOUT,'('' Factor for numerical dissipation from semi-Lagrangian scheme, SLDISSFAC  ='',E12.5)') & 
    &  SLDISSFAC

 

  ALLOCATE(GPVORTGRAD(NPROMA,3*NFLEVG,NGPBLKS)) 
  ALLOCATE( GPTOTDISS(NPROMA,NFLEVG,NGPBLKS) )
  ALLOCATE( GPTOTDISS_SMOOTH(NPROMA,NFLEVG,NGPBLKS) )
  ALLOCATE( GPSTREAM(NPROMA,NFLEVG,NGPBLKS) )
  ALLOCATE( GPVELPOT(NPROMA,NFLEVG,NGPBLKS) )
  ALLOCATE( RSMOOTH(0:NSMAX) )
  

  GPVORTGRAD=  0._JPRB
  GPTOTDISS=  0._JPRB
  GPTOTDISS_SMOOTH = 0._JPRB
  GPSTREAM =  0._JPRB
  GPTOTDISS_SMOOTH = 0._JPRB
  GPVELPOT = 0._JPRB

  IF (LSTOPH_SPBS_T) THEN
    ALLOCATE( GPTEMP(NPROMA,NFLEVG,NGPBLKS) )
    GPTEMP =  0._JPRB
  ENDIF

  ALLOCATE(SPDP(NFLEVG))
  DO  JN=1,NFLEVG
    SPDP(JN)=VDELA(JN)+VDELB(JN)*101325._JPRB
  ENDDO

!*    2.x.2  More setup: Spectral filter for dissipation rates 
!     ---------------------------------------------------------


  IF (LSTOPH_GAUSS) THEN
    ZSIGMA_SMOOTH=0.25_JPRB*(RSMOOTHSCALE/RA)**2
    DO IN=0,NSMAX
      RSMOOTH(IN)= EXP(-ZSIGMA_SMOOTH*IN*(IN+1))
    ENDDO
  ELSE
    IFILT= 30
    ICUTOFF= IFILT + 10
    Zafilt= 3._JPRB/(ICUTOFF*(ICUTOFF+1) - IFILT*(IFILT+1))
    WRITE(NULOUT,*) 'IFILT=',IFILT,'   ICUTOFF=',ICUTOFF

    DO IN=0,NSMAX
      IF(IN>=IFILT.AND.IN<=ICUTOFF) THEN
        RSMOOTH(IN)= EXP(-Zafilt*(IN*(IN+1)-IFILT*(IFILT+1)))
      ELSEIF (IN<IFILT) THEN
        RSMOOTH(IN)= 1._JPRB
      ELSE
        RSMOOTH(IN)= 0._JPRB
      ENDIF
    ENDDO
  ENDIF
ENDIF

!SPIN UP
ISTEP=0_JPIM
IF (LSTOPH .or. LSTOPH_SPBS) THEN
   CALL SURAND2(ISTEP)
ENDIF
IF (LSTOPH_CASBS .AND. (.NOT. LCUCONV_CA)) THEN
  do i=1,2*ISPINUP
   CALL SURAND2(ISTEP)
  enddo
ENDIF

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('SURAND1',1,ZHOOK_HANDLE)
END SUBROUTINE SURAND1
