Skip to content
Snippets Groups Projects
ini_modeln.f90 81.7 KiB
Newer Older
    IF (LHORELAX_RV .OR. LHORELAX_RC .OR. LHORELAX_RR .OR. LHORELAX_RI    &
         .OR. LHORELAX_RS .OR. LHORELAX_RG .OR. LHORELAX_RH               &
       ) THEN
      NSIZELBXR_ll=2* NRIMX+2*JPHEXT
      ALLOCATE(XLBXRM(IISIZEXF,IJSIZEXF,IKU,NRR))
    ELSE
      NSIZELBXR_ll=2*JPHEXT  ! 2
      ALLOCATE(XLBXRM(IISIZEX2,IJSIZEX2,IKU,NRR))
    ENDIF
  ELSE
    NSIZELBXR_ll=0
    ALLOCATE(XLBXRM(0,0,0,0))
  END IF
  !
  IF ( NSV > 0 ) THEN
    IF ( ANY( LHORELAX_SV(:)) ) THEN
      NSIZELBXSV_ll=2* NRIMX+2*JPHEXT
      ALLOCATE(XLBXSVM(IISIZEXF,IJSIZEXF,IKU,NSV))
    ELSE
      NSIZELBXSV_ll=2*JPHEXT  ! 2
      ALLOCATE(XLBXSVM(IISIZEX2,IJSIZEX2,IKU,NSV))
    END IF
  ELSE
    NSIZELBXSV_ll=0
    ALLOCATE(XLBXSVM(0,0,0,0))
  END IF
!
ELSE                                   ! 3D case
!
!
  CALL GET_SIZEX_LB(HLUOUT,NIMAX_ll,NJMAX_ll,NRIMX,   &
       IISIZEXF,IJSIZEXF,IISIZEXFU,IJSIZEXFU,          &
       IISIZEX4,IJSIZEX4,IISIZEX2,IJSIZEX2)
  CALL GET_SIZEY_LB(HLUOUT,NIMAX_ll,NJMAX_ll,NRIMY,   &
       IISIZEYF,IJSIZEYF,IISIZEYFV,IJSIZEYFV,          &
       IISIZEY4,IJSIZEY4,IISIZEY2,IJSIZEY2)
!
! check if local domain not to small for NRIMX NRIMY
!
  IF ( CLBCX(1) /= 'CYCL' )  THEN
        WRITE(*,'(A,I8,A/A,2I8,/A)') "Processor=", IP-1, &
             " :: INI_MODEL_n ERROR:  ( NRIMX  > IDIMX )  ", &
             " Local domain to small for relaxation NRIMX,IDIMX ", &
             NRIMX,IDIMX ,&
             " change relaxation parameters or number of processors "
        !callabortstop
        CALL ABORT
        STOP    
     END IF
  END IF
  IF ( CLBCY(1) /= 'CYCL' ) THEN
        WRITE(*,'(A,I8,A/A,2I8,/A)') "Processor=", IP-1, &
             " :: INI_MODEL_n ERROR:  ( NRIMY > IDIMY )  ", &
             " Local domain to small for relaxation NRIMY,IDIMY ", &
             NRIMY,IDIMY ,&
             " change relaxation parameters or number of processors "
        !callabortstop
        CALL ABORT
        STOP    
     END IF
  END IF
IF ( LHORELAX_UVWTH ) THEN
    NSIZELBX_ll=2*NRIMX+2*JPHEXT
    NSIZELBXU_ll=2*NRIMX+2*JPHEXT
    NSIZELBY_ll=2*NRIMY+2*JPHEXT
    NSIZELBYV_ll=2*NRIMY+2*JPHEXT
    ALLOCATE(XLBXUM(IISIZEXFU,IJSIZEXFU,IKU))
    ALLOCATE(XLBYUM(IISIZEYF,IJSIZEYF,IKU))
    ALLOCATE(XLBXVM(IISIZEXF,IJSIZEXF,IKU))
    ALLOCATE(XLBYVM(IISIZEYFV,IJSIZEYFV,IKU))
    ALLOCATE(XLBXWM(IISIZEXF,IJSIZEXF,IKU))
    ALLOCATE(XLBYWM(IISIZEYF,IJSIZEYF,IKU))
    ALLOCATE(XLBXTHM(IISIZEXF,IJSIZEXF,IKU))
    ALLOCATE(XLBYTHM(IISIZEYF,IJSIZEYF,IKU))
  ELSE
    NSIZELBX_ll=2*JPHEXT  ! 2
    NSIZELBXU_ll=2*(JPHEXT+1) ! 4
    NSIZELBY_ll=2*JPHEXT  ! 2
    NSIZELBYV_ll=2*(JPHEXT+1) ! 4
    ALLOCATE(XLBXUM(IISIZEX4,IJSIZEX4,IKU))
    ALLOCATE(XLBYUM(IISIZEY2,IJSIZEY2,IKU))
    ALLOCATE(XLBXVM(IISIZEX2,IJSIZEX2,IKU))
    ALLOCATE(XLBYVM(IISIZEY4,IJSIZEY4,IKU))
    ALLOCATE(XLBXWM(IISIZEX2,IJSIZEX2,IKU))
    ALLOCATE(XLBYWM(IISIZEY2,IJSIZEY2,IKU))
    ALLOCATE(XLBXTHM(IISIZEX2,IJSIZEX2,IKU))
    ALLOCATE(XLBYTHM(IISIZEY2,IJSIZEY2,IKU))
  END IF
  !
  IF (CTURB /= 'NONE') THEN
    IF ( LHORELAX_TKE) THEN
      NSIZELBXTKE_ll=2*NRIMX+2*JPHEXT
      NSIZELBYTKE_ll=2*NRIMY+2*JPHEXT
      ALLOCATE(XLBXTKEM(IISIZEXF,IJSIZEXF,IKU))
      ALLOCATE(XLBYTKEM(IISIZEYF,IJSIZEYF,IKU))
    ELSE
      NSIZELBXTKE_ll=2*JPHEXT  ! 2
      NSIZELBYTKE_ll=2*JPHEXT  ! 2
      ALLOCATE(XLBXTKEM(IISIZEX2,IJSIZEX2,IKU))
      ALLOCATE(XLBYTKEM(IISIZEY2,IJSIZEY2,IKU))
    END IF
  ELSE
    NSIZELBXTKE_ll=0
    NSIZELBYTKE_ll=0
    ALLOCATE(XLBXTKEM(0,0,0))
    ALLOCATE(XLBYTKEM(0,0,0))
  END IF
  !
  IF ( NRR > 0 ) THEN
    IF (LHORELAX_RV .OR. LHORELAX_RC .OR. LHORELAX_RR .OR. LHORELAX_RI    &
          .OR. LHORELAX_RS .OR. LHORELAX_RG .OR. LHORELAX_RH              &
       ) THEN
      NSIZELBXR_ll=2*NRIMX+2*JPHEXT
      NSIZELBYR_ll=2*NRIMY+2*JPHEXT
      ALLOCATE(XLBXRM(IISIZEXF,IJSIZEXF,IKU,NRR))
      ALLOCATE(XLBYRM(IISIZEYF,IJSIZEYF,IKU,NRR))
    ELSE
      NSIZELBXR_ll=2*JPHEXT  ! 2
      NSIZELBYR_ll=2*JPHEXT  ! 2
      ALLOCATE(XLBXRM(IISIZEX2,IJSIZEX2,IKU,NRR))
      ALLOCATE(XLBYRM(IISIZEY2,IJSIZEY2,IKU,NRR))
    ENDIF
  ELSE
    NSIZELBXR_ll=0
    NSIZELBYR_ll=0
    ALLOCATE(XLBXRM(0,0,0,0))
    ALLOCATE(XLBYRM(0,0,0,0))
  END IF
  !
  IF ( NSV > 0 ) THEN
    IF ( ANY( LHORELAX_SV(:)) ) THEN
      NSIZELBXSV_ll=2*NRIMX+2*JPHEXT
      NSIZELBYSV_ll=2*NRIMY+2*JPHEXT
      ALLOCATE(XLBXSVM(IISIZEXF,IJSIZEXF,IKU,NSV))
      ALLOCATE(XLBYSVM(IISIZEYF,IJSIZEYF,IKU,NSV))
    ELSE
      NSIZELBXSV_ll=2*JPHEXT  ! 2
      NSIZELBYSV_ll=2*JPHEXT  ! 2
      ALLOCATE(XLBXSVM(IISIZEX2,IJSIZEX2,IKU,NSV))
      ALLOCATE(XLBYSVM(IISIZEY2,IJSIZEY2,IKU,NSV))
    END IF
  ELSE
    NSIZELBXSV_ll=0
    NSIZELBYSV_ll=0
    ALLOCATE(XLBXSVM(0,0,0,0))
    ALLOCATE(XLBYSVM(0,0,0,0))
  END IF
END IF      ! END OF THE IF STRUCTURE ON THE MODEL DIMENSION
!
!
IF ( KMI > 1 ) THEN 
  ! it has been assumed that the THeta field used the largest rim area compared
  ! to the others prognostic variables, if it is not the case, you must change
  ! these lines
  ALLOCATE(XCOEFLIN_LBXM(SIZE(XLBXTHM,1),SIZE(XLBXTHM,2),SIZE(XLBXTHM,3)))
  ALLOCATE(   NKLIN_LBXM(SIZE(XLBXTHM,1),SIZE(XLBXTHM,2),SIZE(XLBXTHM,3)))
  ALLOCATE(XCOEFLIN_LBYM(SIZE(XLBYTHM,1),SIZE(XLBYTHM,2),SIZE(XLBYTHM,3)))
  ALLOCATE(   NKLIN_LBYM(SIZE(XLBYTHM,1),SIZE(XLBYTHM,2),SIZE(XLBYTHM,3)))
  ALLOCATE(XCOEFLIN_LBXU(SIZE(XLBXUM,1),SIZE(XLBXUM,2),SIZE(XLBXUM,3)))
  ALLOCATE(   NKLIN_LBXU(SIZE(XLBXUM,1),SIZE(XLBXUM,2),SIZE(XLBXUM,3)))
  ALLOCATE(XCOEFLIN_LBYU(SIZE(XLBYUM,1),SIZE(XLBYUM,2),SIZE(XLBYUM,3)))
  ALLOCATE(   NKLIN_LBYU(SIZE(XLBYUM,1),SIZE(XLBYUM,2),SIZE(XLBYUM,3)))
  ALLOCATE(XCOEFLIN_LBXV(SIZE(XLBXVM,1),SIZE(XLBXVM,2),SIZE(XLBXVM,3)))
  ALLOCATE(   NKLIN_LBXV(SIZE(XLBXVM,1),SIZE(XLBXVM,2),SIZE(XLBXVM,3)))
  ALLOCATE(XCOEFLIN_LBYV(SIZE(XLBYVM,1),SIZE(XLBYVM,2),SIZE(XLBYVM,3)))
  ALLOCATE(   NKLIN_LBYV(SIZE(XLBYVM,1),SIZE(XLBYVM,2),SIZE(XLBYVM,3)))
  ALLOCATE(XCOEFLIN_LBXW(SIZE(XLBXWM,1),SIZE(XLBXWM,2),SIZE(XLBXWM,3)))
  ALLOCATE(   NKLIN_LBXW(SIZE(XLBXWM,1),SIZE(XLBXWM,2),SIZE(XLBXWM,3)))
  ALLOCATE(XCOEFLIN_LBYW(SIZE(XLBYWM,1),SIZE(XLBYWM,2),SIZE(XLBYWM,3)))
  ALLOCATE(   NKLIN_LBYW(SIZE(XLBYWM,1),SIZE(XLBYWM,2),SIZE(XLBYWM,3)))
END IF
!
!  allocation of the LS fields for vertical relaxation and numerical diffusion
IF( .NOT. LSTEADYLS )  THEN
!
  ALLOCATE(XLSUS(SIZE(XLSUM,1),SIZE(XLSUM,2),SIZE(XLSUM,3)))
  ALLOCATE(XLSVS(SIZE(XLSVM,1),SIZE(XLSVM,2),SIZE(XLSVM,3)))
  ALLOCATE(XLSWS(SIZE(XLSWM,1),SIZE(XLSWM,2),SIZE(XLSWM,3)))
  ALLOCATE(XLSTHS(SIZE(XLSTHM,1),SIZE(XLSTHM,2),SIZE(XLSTHM,3)))
  ALLOCATE(XLSRVS(SIZE(XLSRVM,1),SIZE(XLSRVM,2),SIZE(XLSRVM,3)))
!
ELSE
!
  ALLOCATE(XLSUS(0,0,0))
  ALLOCATE(XLSVS(0,0,0))
  ALLOCATE(XLSWS(0,0,0))
  ALLOCATE(XLSTHS(0,0,0))
  ALLOCATE(XLSRVS(0,0,0))
!
END IF
!  allocation of the LB fields for horizontal relaxation and Lateral Boundaries
IF( .NOT. ( LSTEADYLS .AND. KMI==1 ) )  THEN
!
  ALLOCATE(XLBXTKES(SIZE(XLBXTKEM,1),SIZE(XLBXTKEM,2),SIZE(XLBXTKEM,3)))
  ALLOCATE(XLBYTKES(SIZE(XLBYTKEM,1),SIZE(XLBYTKEM,2),SIZE(XLBYTKEM,3)))
  ALLOCATE(XLBXUS(SIZE(XLBXUM,1),SIZE(XLBXUM,2),SIZE(XLBXUM,3)))
  ALLOCATE(XLBYUS(SIZE(XLBYUM,1),SIZE(XLBYUM,2),SIZE(XLBYUM,3)))
  ALLOCATE(XLBXVS(SIZE(XLBXVM,1),SIZE(XLBXVM,2),SIZE(XLBXVM,3)))
  ALLOCATE(XLBYVS(SIZE(XLBYVM,1),SIZE(XLBYVM,2),SIZE(XLBYVM,3)))
  ALLOCATE(XLBXWS(SIZE(XLBXWM,1),SIZE(XLBXWM,2),SIZE(XLBXWM,3)))
  ALLOCATE(XLBYWS(SIZE(XLBYWM,1),SIZE(XLBYWM,2),SIZE(XLBYWM,3)))
  ALLOCATE(XLBXTHS(SIZE(XLBXTHM,1),SIZE(XLBXTHM,2),SIZE(XLBXTHM,3)))
  ALLOCATE(XLBYTHS(SIZE(XLBYTHM,1),SIZE(XLBYTHM,2),SIZE(XLBYTHM,3)))
  ALLOCATE(XLBXRS(SIZE(XLBXRM,1),SIZE(XLBXRM,2),SIZE(XLBXRM,3),SIZE(XLBXRM,4)))
  ALLOCATE(XLBYRS(SIZE(XLBYRM,1),SIZE(XLBYRM,2),SIZE(XLBYRM,3),SIZE(XLBYRM,4)))
  ALLOCATE(XLBXSVS(SIZE(XLBXSVM,1),SIZE(XLBXSVM,2),SIZE(XLBXSVM,3),SIZE(XLBXSVM,4)))
  ALLOCATE(XLBYSVS(SIZE(XLBYSVM,1),SIZE(XLBYSVM,2),SIZE(XLBYSVM,3),SIZE(XLBYSVM,4)))
!
ELSE
!
  ALLOCATE(XLBXTKES(0,0,0))
  ALLOCATE(XLBYTKES(0,0,0))
  ALLOCATE(XLBXUS(0,0,0))
  ALLOCATE(XLBYUS(0,0,0))
  ALLOCATE(XLBXVS(0,0,0))
  ALLOCATE(XLBYVS(0,0,0))
  ALLOCATE(XLBXWS(0,0,0))
  ALLOCATE(XLBYWS(0,0,0))
  ALLOCATE(XLBXTHS(0,0,0))
  ALLOCATE(XLBYTHS(0,0,0))
  ALLOCATE(XLBXRS(0,0,0,0))
  ALLOCATE(XLBYRS(0,0,0,0))
  ALLOCATE(XLBXSVS(0,0,0,0))
  ALLOCATE(XLBYSVS(0,0,0,0))
!
END IF
!
!
!*       3.7   Module MODD_RADIATIONS_n (except XOZON and XAER)
!
!
NSWB_MNH = 6
ALLOCATE(XSW_BANDS (NSWB_MNH))
ALLOCATE(XZENITH   (IIU,IJU))
ALLOCATE(XAZIM     (IIU,IJU))
ALLOCATE(XALBUV    (IIU,IJU))
ALLOCATE(XDIRSRFSWD(IIU,IJU,NSWB_MNH))
ALLOCATE(XSCAFLASWD(IIU,IJU,NSWB_MNH))
ALLOCATE(XFLALWD   (IIU,IJU))
!
IF (CRAD /= 'NONE') THEN
  ALLOCATE(XSLOPANG(IIU,IJU))
  ALLOCATE(XSLOPAZI(IIU,IJU))
  ALLOCATE(XDTHRAD(IIU,IJU,IKU))
  ALLOCATE(XDIRFLASWD(IIU,IJU,NSWB_MNH))
  ALLOCATE(XDIR_ALB(IIU,IJU,NSWB_MNH))
  ALLOCATE(XSCA_ALB(IIU,IJU,NSWB_MNH))
  ALLOCATE(XEMIS  (IIU,IJU))
  ALLOCATE(XTSRAD (IIU,IJU))    ; XTSRAD = 0.0
  ALLOCATE(XSEA (IIU,IJU))
  ALLOCATE(XZS_XY (IIU,IJU))
  ALLOCATE(NCLEARCOL_TM1(IIU,IJU))
  ALLOCATE(XSWU(IIU,IJU,IKU))
  ALLOCATE(XSWD(IIU,IJU,IKU))
  ALLOCATE(XLWU(IIU,IJU,IKU))
  ALLOCATE(XLWD(IIU,IJU,IKU))
  ALLOCATE(XDTHRADSW(IIU,IJU,IKU))
  ALLOCATE(XDTHRADLW(IIU,IJU,IKU))
  ALLOCATE(XRADEFF(IIU,IJU,IKU))
ELSE
  ALLOCATE(XSLOPANG(0,0))
  ALLOCATE(XSLOPAZI(0,0))
  ALLOCATE(XDTHRAD(0,0,0))
  ALLOCATE(XDIRFLASWD(0,0,0))
  ALLOCATE(XDIR_ALB(0,0,0))
  ALLOCATE(XSCA_ALB(0,0,0))
  ALLOCATE(XEMIS  (0,0))
  ALLOCATE(XTSRAD (0,0))
  ALLOCATE(XSEA (0,0))
  ALLOCATE(XZS_XY (0,0))
  ALLOCATE(NCLEARCOL_TM1(0,0))
  ALLOCATE(XSWU(0,0,0))
  ALLOCATE(XSWD(0,0,0))
  ALLOCATE(XLWU(0,0,0))
  ALLOCATE(XLWD(0,0,0))
  ALLOCATE(XDTHRADSW(0,0,0))
  ALLOCATE(XDTHRADLW(0,0,0))
  ALLOCATE(XRADEFF(0,0,0))
END IF

IF (CRAD == 'ECMW') THEN
  ALLOCATE(XSTROATM(31,6))
  ALLOCATE(XSMLSATM(31,6))
  ALLOCATE(XSMLWATM(31,6))
  ALLOCATE(XSPOSATM(31,6))
  ALLOCATE(XSPOWATM(31,6))
  ALLOCATE(XSTATM(31,6))
ELSE
  ALLOCATE(XSTROATM(0,0))
  ALLOCATE(XSMLSATM(0,0))
  ALLOCATE(XSMLWATM(0,0))
  ALLOCATE(XSPOSATM(0,0))
  ALLOCATE(XSPOWATM(0,0))
  ALLOCATE(XSTATM(0,0))
END IF
!
!*       3.8   Module MODD_DEEP_CONVECTION_n
!
IF (CDCONV /= 'NONE' .OR. CSCONV == 'KAFR') THEN
  ALLOCATE(NCOUNTCONV(IIU,IJU))
  ALLOCATE(XDTHCONV(IIU,IJU,IKU))
  ALLOCATE(XDRVCONV(IIU,IJU,IKU))
  ALLOCATE(XDRCCONV(IIU,IJU,IKU))
  ALLOCATE(XDRICONV(IIU,IJU,IKU))
  ALLOCATE(XPRCONV(IIU,IJU))
  ALLOCATE(XPACCONV(IIU,IJU))
  ALLOCATE(XPRSCONV(IIU,IJU))
  ! diagnostics
  IF (LCH_CONV_LINOX) THEN
    ALLOCATE(XIC_RATE(IIU,IJU))
    ALLOCATE(XCG_RATE(IIU,IJU))
    ALLOCATE(XIC_TOTAL_NUMBER(IIU,IJU))
    ALLOCATE(XCG_TOTAL_NUMBER(IIU,IJU))
  ELSE
    ALLOCATE(XIC_RATE(0,0))
    ALLOCATE(XCG_RATE(0,0))
    ALLOCATE(XIC_TOTAL_NUMBER(0,0))
    ALLOCATE(XCG_TOTAL_NUMBER(0,0))
  END IF
  IF ( LDIAGCONV )  THEN
    ALLOCATE(XUMFCONV(IIU,IJU,IKU))
    ALLOCATE(XDMFCONV(IIU,IJU,IKU))
    ALLOCATE(XPRLFLXCONV(IIU,IJU,IKU))
    ALLOCATE(XPRSFLXCONV(IIU,IJU,IKU))
    ALLOCATE(XCAPE(IIU,IJU))
    ALLOCATE(NCLTOPCONV(IIU,IJU))
    ALLOCATE(NCLBASCONV(IIU,IJU))
  ELSE
    ALLOCATE(XUMFCONV(0,0,0))
    ALLOCATE(XDMFCONV(0,0,0))
    ALLOCATE(XPRLFLXCONV(0,0,0))
    ALLOCATE(XPRSFLXCONV(0,0,0))
    ALLOCATE(XCAPE(0,0))
    ALLOCATE(NCLTOPCONV(0,0))
    ALLOCATE(NCLBASCONV(0,0))
  END IF
ELSE
  ALLOCATE(XPRCONV(0,0))
  ALLOCATE(XPACCONV(0,0))
  ALLOCATE(XPRSCONV(0,0))
END IF
!
IF ((CDCONV == 'KAFR' .OR. CSCONV == 'KAFR') &
    .AND. LSUBG_COND .AND. LSIG_CONV) THEN
  ALLOCATE(XMFCONV(IIU,IJU,IKU))
ELSE
  ALLOCATE(XMFCONV(0,0,0))
ENDIF
!
IF ((CDCONV == 'KAFR' .OR. CSCONV == 'KAFR') &
    .AND. LCHTRANS .AND. NSV > 0 ) THEN
  ALLOCATE(XDSVCONV(IIU,IJU,IKU,NSV))
ELSE
  ALLOCATE(XDSVCONV(0,0,0,0))
END IF
!
ALLOCATE(XCF_MF(IIU,IJU,IKU)) ; XCF_MF=0.0
ALLOCATE(XRC_MF(IIU,IJU,IKU)) ; XRC_MF=0.0
ALLOCATE(XRI_MF(IIU,IJU,IKU)) ; XRI_MF=0.0
!
!*       3.9   Local variables
!
ALLOCATE(ZJ(IIU,IJU,IKU))
!
!*      3.10 Forcing variables (Module MODD_FRC)
!
IF (KMI == 1) THEN
  IF ( LFORCING ) THEN
    ALLOCATE(TDTFRC(NFRC))
    ALLOCATE(XUFRC(IKU,NFRC))
    ALLOCATE(XVFRC(IKU,NFRC))
    ALLOCATE(XWFRC(IKU,NFRC))
    ALLOCATE(XTHFRC(IKU,NFRC))
    ALLOCATE(XRVFRC(IKU,NFRC))
    ALLOCATE(XTENDTHFRC(IKU,NFRC))
    ALLOCATE(XTENDRVFRC(IKU,NFRC))
    ALLOCATE(XGXTHFRC(IKU,NFRC))
    ALLOCATE(XGYTHFRC(IKU,NFRC))
    ALLOCATE(XPGROUNDFRC(NFRC))
    ALLOCATE(XTENDUFRC(IKU,NFRC))
    ALLOCATE(XTENDVFRC(IKU,NFRC))    
  ELSE
    ALLOCATE(TDTFRC(0))
    ALLOCATE(XUFRC(0,0))
    ALLOCATE(XVFRC(0,0))
    ALLOCATE(XWFRC(0,0))
    ALLOCATE(XTHFRC(0,0))
    ALLOCATE(XRVFRC(0,0))
    ALLOCATE(XTENDTHFRC(0,0))
    ALLOCATE(XTENDRVFRC(0,0))
    ALLOCATE(XGXTHFRC(0,0))
    ALLOCATE(XGYTHFRC(0,0))
    ALLOCATE(XPGROUNDFRC(0))
    ALLOCATE(XTENDUFRC(0,0))
    ALLOCATE(XTENDVFRC(0,0))
  END IF
  IF ( LFORCING ) THEN
    ALLOCATE(XWTFRC(IIU,IJU,IKU))
    ALLOCATE(XUFRC_PAST(IIU,IJU,IKU)) ; XUFRC_PAST = XUNDEF
    ALLOCATE(XVFRC_PAST(IIU,IJU,IKU)) ; XVFRC_PAST = XUNDEF
  ELSE
    ALLOCATE(XWTFRC(0,0,0))
    ALLOCATE(XUFRC_PAST(0,0,0))
    ALLOCATE(XVFRC_PAST(0,0,0))
  END IF
END IF
! ----------------------------------------------------------------------
!
IF (L2D_ADV_FRC) THEN
  WRITE(ILUOUT,*) 'L2D_ADV_FRC IS SET TO', L2D_ADV_FRC
  WRITE(ILUOUT,*) 'ADV FRC WILL BE SET'
  ALLOCATE(TDTADVFRC(NADVFRC))
  ALLOCATE(XDTHFRC(IIU,IJU,IKU,NADVFRC))  ; XDTHFRC=0.
  ALLOCATE(XDRVFRC(IIU,IJU,IKU,NADVFRC))  ; XDRVFRC=0.
ELSE
  ALLOCATE(TDTADVFRC(0))
  ALLOCATE(XDTHFRC(0,0,0,0))
  ALLOCATE(XDRVFRC(0,0,0,0))
ENDIF

IF (L2D_REL_FRC) THEN
  WRITE(ILUOUT,*) 'L2D_REL_FRC IS SET TO', L2D_REL_FRC
  WRITE(ILUOUT,*) 'REL FRC WILL BE SET'
  ALLOCATE(TDTRELFRC(NRELFRC))
  ALLOCATE(XTHREL(IIU,IJU,IKU,NRELFRC))  ; XTHREL=0.
  ALLOCATE(XRVREL(IIU,IJU,IKU,NRELFRC))  ; XRVREL=0.
ELSE
  ALLOCATE(TDTRELFRC(0))
  ALLOCATE(XTHREL(0,0,0,0))
  ALLOCATE(XRVREL(0,0,0,0))
ENDIF
!
!*      4.11 BIS: Eddy fluxes allocation
!
IF ( LTH_FLX ) THEN
   ALLOCATE(XVTH_FLUX_M(IIU,IJU,IKU)) ; XVTH_FLUX_M = 0.
   ALLOCATE(XWTH_FLUX_M(IIU,IJU,IKU)) ; XWTH_FLUX_M = 0.
   IF (KMI /= 1) THEN
      ALLOCATE(XRTHS_EDDY_FLUX(IIU,IJU,IKU))
      XRTHS_EDDY_FLUX = 0.
   ENDIF
ELSE
   ALLOCATE(XVTH_FLUX_M(0,0,0)) ; XVTH_FLUX_M = 0.
   ALLOCATE(XWTH_FLUX_M(0,0,0)) ; XWTH_FLUX_M = 0.
END IF
!
IF ( LUV_FLX) THEN
   ALLOCATE(XVU_FLUX_M(IIU,IJU,IKU)) ; XVU_FLUX_M  = 0.
   IF (KMI /= 1) THEN
      ALLOCATE(XRVS_EDDY_FLUX(IIU,IJU,IKU))
      XRVS_EDDY_FLUX = 0.
   ENDIF
ELSE
   ALLOCATE(XVU_FLUX_M(0,0,0)) ; XVU_FLUX_M  = 0.
END IF
!
!*      3.11   Module MODD_ICE_CONC_n
!
IF (     (CCLOUD == 'ICE3'.OR.CCLOUD == 'ICE4') .AND.   &
     (CPROGRAM == 'DIAG  '.OR.CPROGRAM == 'MESONH')) THEN
  ALLOCATE(XCIT(IIU,IJU,IKU))
ELSE
  ALLOCATE(XCIT(0,0,0))
END IF
!
IF ( CCLOUD == 'KHKO' .OR. CCLOUD == 'C2R2') THEN
   ALLOCATE(XSUPSAT(IIU,IJU,IKU))
   ALLOCATE(XNACT(IIU,IJU,IKU))
   ALLOCATE(XNPRO(IIU,IJU,IKU))
   ALLOCATE(XSSPRO(IIU,IJU,IKU))
END IF
!
!*      3.12   Module MODD_TURB_CLOUD
!
IF (.NOT.(ALLOCATED(XCEI))) ALLOCATE(XCEI(0,0,0))
IF (KMI == NMODEL_CLOUD .AND. CTURBLEN_CLOUD/='NONE' ) THEN
  DEALLOCATE(XCEI)
  ALLOCATE(XCEI(IIU,IJU,IKU))
ENDIF
!
!*      3.13  Module MODD_CH_PH_n
IF (LUSECHAQ.AND.(CPROGRAM == 'DIAG  '.OR.CPROGRAM == 'MESONH')) THEN
  IF (LCH_PH) THEN
    ALLOCATE(XPHC(IIU,IJU,IKU))
    IF (NRRL==2) THEN
      ALLOCATE(XPHR(IIU,IJU,IKU))
      ALLOCATE(XACPHR(IIU,IJU))
      XACPHR(:,:) =  0.
  IF (NRRL==2) THEN
    ALLOCATE(XACPRAQ(IIU,IJU,NSV_CHAC/2))
    XACPRAQ(:,:,:) = 0.
  ENDIF
IF ((LUSECHEM).AND.(CPROGRAM == 'DIAG  ')) THEN
  ALLOCATE(XCHFLX(IIU,IJU,NSV_CHEM))
  XCHFLX(:,:,:) = 0.
END IF
!
!-------------------------------------------------------------------------------
!
!*       4.    INITIALIZE BUDGET VARIABLES
!              ---------------------------
!
IF ( CBUTYPE /= "NONE" .AND. NBUMOD == KMI ) THEN
             LNUMDIFU,LNUMDIFTH,LNUMDIFSV,                                    &
             LHORELAX_UVWTH,LHORELAX_RV, LHORELAX_RC,LHORELAX_RR,             &
             LHORELAX_RI,LHORELAX_RS,LHORELAX_RG, LHORELAX_RH,LHORELAX_TKE,   &
             LHORELAX_SV,LVE_RELAX,LCHTRANS,LNUDGING,LDRAGTREE,LDEPOTREE,     &
             CRAD,CDCONV,CSCONV,CTURB,CTURBDIM,CCLOUD                         )
END IF
!
!-------------------------------------------------------------------------------
!
!
!*       5.    INITIALIZE INTERPOLATION COEFFICIENTS
!
CALL INI_BIKHARDT_n (NDXRATIO_ALL(KMI),NDYRATIO_ALL(KMI),KMI)
!
!-------------------------------------------------------------------------------
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*       6.     BUILT THE GENERIC OUTPUT NAME
!               ----------------------------
!
IF (KMI == 1) THEN
  DO IMI = 1 , NMODEL
    WRITE(IO_SURF_MNH_MODEL(IMI)%COUTFILE,'(A,".",I1,".",A)') CEXP,IMI,TRIM(ADJUSTL(CSEG))
    WRITE(YNAME, '(A,".",I1,".",A)') CEXP,IMI,TRIM(ADJUSTL(CSEG))//'.000'
    CALL IO_FILE_ADD2LIST(LUNIT_MODEL(IMI)%TDIAFILE,YNAME,'DIACHRONIC','WRITE', &
                          KLFINPRAR=50,KLFITYPE=1,KLFIVERB=NVERB,               &
                          TPDADFILE=LUNIT_MODEL(NDAD(IMI))%TDIAFILE )
  TDIAFILE => LUNIT_MODEL(KMI)%TDIAFILE !Necessary because no call to GOTO_MODEL before needing it
  !
WAUTELET Philippe's avatar
WAUTELET Philippe committed
  IF (CPROGRAM=='MESONH') THEN
    IF ( NDAD(KMI) == 1)  CDAD_NAME(KMI) = CEXP//'.1.'//CSEG
    IF ( NDAD(KMI) == 2)  CDAD_NAME(KMI) = CEXP//'.2.'//CSEG
    IF ( NDAD(KMI) == 3)  CDAD_NAME(KMI) = CEXP//'.3.'//CSEG
    IF ( NDAD(KMI) == 4)  CDAD_NAME(KMI) = CEXP//'.4.'//CSEG
    IF ( NDAD(KMI) == 5)  CDAD_NAME(KMI) = CEXP//'.5.'//CSEG
    IF ( NDAD(KMI) == 6)  CDAD_NAME(KMI) = CEXP//'.6.'//CSEG
    IF ( NDAD(KMI) == 7)  CDAD_NAME(KMI) = CEXP//'.7.'//CSEG
    IF ( NDAD(KMI) == 8)  CDAD_NAME(KMI) = CEXP//'.8.'//CSEG
  END IF
END IF
!
!-------------------------------------------------------------------------------
!
!*       7.    INITIALIZE GRIDS AND METRIC COEFFICIENTS
!              ----------------------------------------
!
CALL SET_GRID(KMI,TPINIFILE,HLUOUT,IIU,IJU,IKU,NIMAX_ll,NJMAX_ll,        &
              XBMX1,XBMX2,XBMX3,XBMX4,XBMY1,XBMY2,XBMY3,XBMY4,           &
              XBFX1,XBFX2,XBFX3,XBFX4,XBFY1,XBFY2,XBFY3,XBFY4,           &
              NXOR_ALL(KMI),NYOR_ALL(KMI),NXEND_ALL(KMI),NYEND_ALL(KMI), &
              NDXRATIO_ALL(KMI),NDYRATIO_ALL(KMI),                       &
              CLBCX,CLBCY,                                               &
              XTSTEP,XSEGLEN,                                            &
              XLONORI,XLATORI,XLON,XLAT,                                 &
              XXHAT,XYHAT,XDXHAT,XDYHAT, XMAP,                           &
              XZS,XZZ,XZHAT,LSLEVE,XLEN1,XLEN2,XZSMT,                    &
              ZJ,                                                        &
              TDTMOD,TDTCUR,NSTOP,NBAK_NUMB,NOUT_NUMB,TBACKUPN,TOUTPUTN)
!
CALL METRICS(XMAP,XDXHAT,XDYHAT,XZZ,XDXX,XDYY,XDZX,XDZY,XDZZ)
!
!* update halos of metric coefficients
!
!
CALL UPDATE_METRICS(CLBCX,CLBCY,XDXX,XDYY,XDZX,XDZY,XDZZ)
!
!
CALL SET_DIRCOS(CLBCX,CLBCY,XDXX,XDYY,XDZX,XDZY,TZINITHALO2D_ll,   &
                XDIRCOSXW,XDIRCOSYW,XDIRCOSZW,XCOSSLOPE,XSINSLOPE  )
!
! grid nesting initializations
IF ( KMI == 1 ) THEN
  XTSTEP_MODEL1=XTSTEP
END IF
!
NDT_2_WAY(KMI)=4
!
!-------------------------------------------------------------------------------
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*      8.    INITIALIZE DATA FOR JVALUES AND AEROSOLS 
!
IF ( LUSECHEM .OR. LCHEMDIAG ) THEN
  IF ((KMI==1).AND.(CPROGRAM == "MESONH".OR.CPROGRAM == "DIAG  "))  &
    CALL CH_INIT_JVALUES(TDTCUR%TDATE%DAY, TDTCUR%TDATE%MONTH,      &
                         TDTCUR%TDATE%YEAR, ILUOUT, XCH_TUV_DOBNEW)
!
  IF (LORILAM) THEN
    CALL CH_AER_MOD_INIT
  ELSE
    IF (.NOT.(ASSOCIATED(XSOLORG))) ALLOCATE(XSOLORG(0,0,0,0))  
    IF (.NOT.(ASSOCIATED(XMI))) ALLOCATE(XMI(0,0,0,0))  
  ENDIF
ELSE
  IF (.NOT.(ASSOCIATED(XMI))) ALLOCATE(XMI(0,0,0,0))
  IF (.NOT.(ASSOCIATED(XSOLORG))) ALLOCATE(XSOLORG(0,0,0,0))
END IF
!
Gaelle TANGUY's avatar
Gaelle TANGUY committed
IF (CCLOUD=='LIMA') CALL INIT_AEROSOL_PROPERTIES
!
!-------------------------------------------------------------------------------
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*       9.    INITIALIZE THE PROGNOSTIC FIELDS
!              --------------------------------
!
CALL MPPDB_CHECK3D(XUT,"INI_MODEL_N-before read_field::XUT",PRECISION)
CALL READ_FIELD(TPINIFILE,HLUOUT,IMASDEV, IIU,IJU,IKU,XTSTEP,                 &
                CGETTKET,CGETRVT,CGETRCT,CGETRRT,CGETRIT,CGETCIT,             &
                CGETRST,CGETRGT,CGETRHT,CGETSVT,CGETSRCT,CGETSIGS,CGETCLDFR,  &
                CGETBL_DEPTH,CGETSBL_DEPTH,CGETPHC,CGETPHR,CUVW_ADV_SCHEME,   &
                CTEMP_SCHEME,NSIZELBX_ll,NSIZELBXU_ll,NSIZELBY_ll,NSIZELBYV_ll,&
                NSIZELBXTKE_ll,NSIZELBYTKE_ll,                                &
                NSIZELBXR_ll,NSIZELBYR_ll,NSIZELBXSV_ll,NSIZELBYSV_ll,        &
                XUM,XVM,XWM,XDUM,XDVM,XDWM,                                   &
                XUT,XVT,XWT,XTHT,XPABST,XPABSM,XTKET,XRTKEMS,                 &
                XRT,XSVT,XCIT,XDRYMASST,                                      &
                XSIGS,XSRCT,XCLDFR,XBL_DEPTH,XSBL_DEPTH,XWTHVMF,XPHC,XPHR,    &
                XLSUM,XLSVM,XLSWM,XLSTHM,XLSRVM,                              &
                XLBXUM,XLBXVM,XLBXWM,XLBXTHM,XLBXTKEM,                        &
                XLBXRM,XLBXSVM,                                               &
                XLBYUM,XLBYVM,XLBYWM,XLBYTHM,XLBYTKEM,                        &
                XLBYRM,XLBYSVM,                                               &
                NFRC,TDTFRC,XUFRC,XVFRC,XWFRC,XTHFRC,XRVFRC,                  &
                XTENDTHFRC,XTENDRVFRC,XGXTHFRC,XGYTHFRC,                      &
                XPGROUNDFRC, XATC,                                            &
                XTENDUFRC, XTENDVFRC,                                         &
                NADVFRC,TDTADVFRC,XDTHFRC,XDRVFRC,                            &
                NRELFRC,TDTRELFRC,XTHREL,XRVREL,                              &
                XVTH_FLUX_M,XWTH_FLUX_M,XVU_FLUX_M,                           &
                XRUS_PRES,XRVS_PRES,XRWS_PRES,XRTHS_CLD,XRRS_CLD,XRSVS_CLD    )
!
!-------------------------------------------------------------------------------
!
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*        10.  INITIALIZE REFERENCE STATE
!              ---------------------------
!
!
             XZZ,XZHAT,ZJ,XDXX,XDYY,CLBCX,CLBCY,    &
             XREFMASS,XMASS_O_PHI0,XLINMASS,        &
             XRHODREF,XTHVREF,XRVREF,XEXNREF,XRHODJ )
!
!-------------------------------------------------------------------------------
!
!*       10.1    INITIALIZE THE TURBULENCE VARIABLES
!               -----------------------------------
!
IF ((CTURB == 'TKEL').AND.(CCONF=='START')) THEN
  CALL MPPDB_CHECK3D(XUT,"INI_MODEL_N-before ini_tke_eps::XUT",PRECISION)
  CALL INI_TKE_EPS(CGETTKET,XTHVREF,XZZ, &
                   XUT,XVT,XTHT,                  &
                   XTKET,TZINITHALO3D_ll    )
  CALL MPPDB_CHECK3D(XUT,"INI_MODEL_N-after ini_tke_eps::XUT",PRECISION)
!*       10.2   INITIALIZE THE LES VARIABLES
!               ----------------------------
!
CALL INI_LES_n
!
!-------------------------------------------------------------------------------
!
!*       11.    INITIALIZE THE SOURCE OF TOTAL DRY MASS Md
!               ------------------------------------------
!
IF((KMI==1).AND.LSTEADYLS) THEN
  XDRYMASSS = 0.
END IF
!
!-------------------------------------------------------------------------------
!
!*       12.    INITIALIZE THE MICROPHYSICS                   
!               ----------------------------
!
IF (CELEC == 'NONE') THEN
!
!-------------------------------------------------------------------------------
!
!*       13.    INITIALIZE THE ATMOSPHERIC ELECTRICITY                   
!               --------------------------------------
!
ELSE
  CALL INI_ELEC_n(ILUOUT, CELEC, CCLOUD, HLUOUT, TPINIFILE, &
                  XTSTEP, XZZ,                              &
                  XDXX, XDYY, XDZZ, XDZX, XDZY              )
!
  WRITE (UNIT=ILUOUT,&
  FMT='(/,"ELECTRIC VARIABLES ARE BETWEEN INDEX",I2," AND ",I2)')&
  NSV_ELECBEG, NSV_ELECEND
! 
    IF( CGETSVT(NSV_ELECBEG)=='INIT' ) THEN
      XSVT(:,:,:,NSV_ELECBEG) = XCION_POS_FW(:,:,:)                  ! Nb/kg
      XSVT(:,:,:,NSV_ELECEND) = XCION_NEG_FW(:,:,:)
!
      XSVT(:,:,:,NSV_ELECBEG+1:NSV_ELECEND-1) = 0.0
    ELSE  ! Convert elec_variables per m3 into elec_variables per kg of air
      DO JSV = NSV_ELECBEG, NSV_ELECEND
         XSVT(:,:,:,JSV) = XSVT(:,:,:,JSV) / XRHODREF(:,:,:)
      ENDDO
    END IF
END IF
!
!-------------------------------------------------------------------------------
!
!*       14.   INITIALIZE THE LARGE SCALE SOURCES
!              ----------------------------------
!
IF ((KMI==1).AND.(.NOT. LSTEADYLS)) THEN
  CALL MPPDB_CHECK3D(XUT,"INI_MODEL_N-before ini_cpl::XUT",PRECISION)
  CALL INI_CPL(HLUOUT,NSTOP,XTSTEP,LSTEADYLS,CCONF,                           &
               CGETTKET,                                                      &
               CGETRVT,CGETRCT,CGETRRT,CGETRIT,                               &
               CGETRST,CGETRGT,CGETRHT,CGETSVT,LCH_INIT_FIELD,                &
               NSV,NIMAX_ll,NJMAX_ll,                                         &
               NSIZELBX_ll,NSIZELBXU_ll,NSIZELBY_ll,NSIZELBYV_ll,             &
               NSIZELBXTKE_ll,NSIZELBYTKE_ll,                                 &
               NSIZELBXR_ll,NSIZELBYR_ll,NSIZELBXSV_ll,NSIZELBYSV_ll,         &
               XLSUM,XLSVM,XLSWM,XLSTHM,XLSRVM,XDRYMASST,                     &
               XLBXUM,XLBXVM,XLBXWM,XLBXTHM,XLBXTKEM,XLBXRM,XLBXSVM,          &
               XLBYUM,XLBYVM,XLBYWM,XLBYTHM,XLBYTKEM,XLBYRM,XLBYSVM,          &
               XLSUS,XLSVS,XLSWS,XLSTHS,XLSRVS,XDRYMASSS,                     &
               XLBXUS,XLBXVS,XLBXWS,XLBXTHS,XLBXTKES,XLBXRS,XLBXSVS,          &
               XLBYUS,XLBYVS,XLBYWS,XLBYTHS,XLBYTKES,XLBYRS,XLBYSVS           )
  CALL MPPDB_CHECK3D(XUT,"INI_MODEL_N-after ini_cpl::XUT",PRECISION)
!
      DO JSV=NSV_CHEMBEG,NSV_CHEMEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
      DO JSV=NSV_LNOXBEG,NSV_LNOXEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
      DO JSV=NSV_AERBEG,NSV_AEREND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
      DO JSV=NSV_DSTBEG,NSV_DSTEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
      DO JSV=NSV_DSTDEPBEG,NSV_DSTDEPEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
      DO JSV=NSV_SLTBEG,NSV_SLTEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
      DO JSV=NSV_SLTDEPBEG,NSV_SLTDEPEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
      DO JSV=NSV_PPBEG,NSV_PPEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
#ifdef MNH_FOREFIRE
      DO JSV=NSV_FFBEG,NSV_FFEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
      !
#endif
      DO JSV=NSV_CSBEG,NSV_CSEND
        XLBXSVS(:,:,:,JSV)=MAX(XLBXSVS(:,:,:,JSV),0.)
        XLBYSVS(:,:,:,JSV)=MAX(XLBYSVS(:,:,:,JSV),0.)
      ENDDO
!
END IF
!
IF ( KMI > 1) THEN
  ! Use dummy pointers to correct an ifort BUG
  DPTR_XBMX1=>XBMX1
  DPTR_XBMX2=>XBMX2
  DPTR_XBMX3=>XBMX3
  DPTR_XBMX4=>XBMX4
  DPTR_XBMY1=>XBMY1
  DPTR_XBMY2=>XBMY2
  DPTR_XBMY3=>XBMY3
  DPTR_XBMY4=>XBMY4
  DPTR_XBFX1=>XBFX1
  DPTR_XBFX2=>XBFX2
  DPTR_XBFX3=>XBFX3
  DPTR_XBFX4=>XBFX4
  DPTR_XBFY1=>XBFY1
  DPTR_XBFY2=>XBFY2
  DPTR_XBFY3=>XBFY3
  DPTR_XBFY4=>XBFY4
  DPTR_CLBCX=>CLBCX
  DPTR_CLBCY=>CLBCY
  !
  DPTR_XZZ=>XZZ
  DPTR_XZHAT=>XZHAT
  DPTR_XLSUM=>XLSUM
  DPTR_XLSVM=>XLSVM
  DPTR_XLSWM=>XLSWM
  DPTR_XLSTHM=>XLSTHM
  DPTR_XLSRVM=>XLSRVM
  DPTR_XLSUS=>XLSUS
  DPTR_XLSVS=>XLSVS
  DPTR_XLSWS=>XLSWS
  DPTR_XLSTHS=>XLSTHS
  DPTR_XLSRVS=>XLSRVS
  !
  DPTR_NKLIN_LBXU=>NKLIN_LBXU
  DPTR_XCOEFLIN_LBXU=>XCOEFLIN_LBXU
  DPTR_NKLIN_LBYU=>NKLIN_LBYU
  DPTR_XCOEFLIN_LBYU=>XCOEFLIN_LBYU
  DPTR_NKLIN_LBXV=>NKLIN_LBXV
  DPTR_XCOEFLIN_LBXV=>XCOEFLIN_LBXV
  DPTR_NKLIN_LBYV=>NKLIN_LBYV
  DPTR_XCOEFLIN_LBYV=>XCOEFLIN_LBYV
  DPTR_NKLIN_LBXW=>NKLIN_LBXW
  DPTR_XCOEFLIN_LBXW=>XCOEFLIN_LBXW
  DPTR_NKLIN_LBYW=>NKLIN_LBYW
  DPTR_XCOEFLIN_LBYW=>XCOEFLIN_LBYW
  DPTR_NKLIN_LBXM=>NKLIN_LBXM
  DPTR_XCOEFLIN_LBXM=>XCOEFLIN_LBXM
  DPTR_NKLIN_LBYM=>NKLIN_LBYM
  DPTR_XCOEFLIN_LBYM=>XCOEFLIN_LBYM
  !
  CALL INI_SPAWN_LS_n(NDAD(KMI),XTSTEP,KMI,                                 &
       DPTR_XBMX1,DPTR_XBMX2,DPTR_XBMX3,DPTR_XBMX4,DPTR_XBMY1,DPTR_XBMY2,DPTR_XBMY3,DPTR_XBMY4,      &
       DPTR_XBFX1,DPTR_XBFX2,DPTR_XBFX3,DPTR_XBFX4,DPTR_XBFY1,DPTR_XBFY2,DPTR_XBFY3,DPTR_XBFY4,      &
       NDXRATIO_ALL(KMI),NDYRATIO_ALL(KMI),                  &
       DPTR_CLBCX,DPTR_CLBCY,DPTR_XZZ,DPTR_XZHAT,                                &
       LSLEVE,XLEN1,XLEN2,                                   &
       DPTR_XLSUM,DPTR_XLSVM,DPTR_XLSWM,DPTR_XLSTHM,DPTR_XLSRVM,                      &
       DPTR_XLSUS,DPTR_XLSVS,DPTR_XLSWS,DPTR_XLSTHS,DPTR_XLSRVS,                      &
       DPTR_NKLIN_LBXU,DPTR_XCOEFLIN_LBXU,DPTR_NKLIN_LBYU,DPTR_XCOEFLIN_LBYU,    &
       DPTR_NKLIN_LBXV,DPTR_XCOEFLIN_LBXV,DPTR_NKLIN_LBYV,DPTR_XCOEFLIN_LBYV,    &
       DPTR_NKLIN_LBXW,DPTR_XCOEFLIN_LBXW,DPTR_NKLIN_LBYW,DPTR_XCOEFLIN_LBYW,    &
       DPTR_NKLIN_LBXM,DPTR_XCOEFLIN_LBXM,DPTR_NKLIN_LBYM,DPTR_XCOEFLIN_LBYM     )
  !
  DPTR_XLBXUM=>XLBXUM
  DPTR_XLBYUM=>XLBYUM
  DPTR_XLBXVM=>XLBXVM
  DPTR_XLBYVM=>XLBYVM
  DPTR_XLBXWM=>XLBXWM
  DPTR_XLBYWM=>XLBYWM
  DPTR_XLBXTHM=>XLBXTHM
  DPTR_XLBYTHM=>XLBYTHM
  DPTR_XLBXTKEM=>XLBXTKEM
  DPTR_XLBYTKEM=>XLBYTKEM
  DPTR_XLBXRM=>XLBXRM
  DPTR_XLBYRM=>XLBYRM
  DPTR_XLBXSVM=>XLBXSVM
  DPTR_XLBYSVM=>XLBYSVM
  CALL INI_ONE_WAY_n(NDAD(KMI),CLUOUT,XTSTEP,KMI,1,                         &
       DPTR_XBMX1,DPTR_XBMX2,DPTR_XBMX3,DPTR_XBMX4,DPTR_XBMY1,DPTR_XBMY2,DPTR_XBMY3,DPTR_XBMY4,        &
       DPTR_XBFX1,DPTR_XBFX2,DPTR_XBFX3,DPTR_XBFX4,DPTR_XBFY1,DPTR_XBFY2,DPTR_XBFY3,DPTR_XBFY4,        &
       NDXRATIO_ALL(KMI),NDYRATIO_ALL(KMI),NDTRATIO(KMI),      &
       DPTR_CLBCX,DPTR_CLBCY,NRIMX,NRIMY,                                &
       DPTR_NKLIN_LBXU,DPTR_XCOEFLIN_LBXU,DPTR_NKLIN_LBYU,DPTR_XCOEFLIN_LBYU,      &
       DPTR_NKLIN_LBXV,DPTR_XCOEFLIN_LBXV,DPTR_NKLIN_LBYV,DPTR_XCOEFLIN_LBYV,      &
       DPTR_NKLIN_LBXW,DPTR_XCOEFLIN_LBXW,DPTR_NKLIN_LBYW,DPTR_XCOEFLIN_LBYW,      &
       DPTR_NKLIN_LBXM,DPTR_XCOEFLIN_LBXM,DPTR_NKLIN_LBYM,DPTR_XCOEFLIN_LBYM,      &
       CCLOUD, LUSECHAQ, LUSECHIC,                                                 &
       DPTR_XLBXUM,DPTR_XLBYUM,DPTR_XLBXVM,DPTR_XLBYVM,DPTR_XLBXWM,DPTR_XLBYWM,    &
       DPTR_XLBXTHM,DPTR_XLBYTHM,                                                  &
       DPTR_XLBXTKEM,DPTR_XLBYTKEM,                                                &
       DPTR_XLBXRM,DPTR_XLBYRM,DPTR_XLBXSVM,DPTR_XLBYSVM                           )
END IF
!
!
!-------------------------------------------------------------------------------
!
!*       15.    INITIALIZE THE SCALAR VARIABLES
!               -------------------------------
!
IF (LLG .AND. LINIT_LG .AND. CPROGRAM=='MESONH') &
  CALL INI_LG(XXHAT,XYHAT,XZZ,XSVT,XLBXSVM,XLBYSVM)

!
!-------------------------------------------------------------------------------
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*       16.    INITIALIZE THE PARAMETERS FOR THE DYNAMICS
!               ------------------------------------------
!
CALL INI_DYNAMICS(HLUOUT,XLON,XLAT,XRHODJ,XTHVREF,XMAP,XZZ,XDXHAT,XDYHAT,     &
             XZHAT,CLBCX,CLBCY,XTSTEP,                                        &
             LVE_RELAX,LVE_RELAX_GRD,LHORELAX_UVWTH,LHORELAX_RV,              &
             LHORELAX_RC,LHORELAX_RR,LHORELAX_RI,LHORELAX_RS,LHORELAX_RG,     &
             LHORELAX_RH,LHORELAX_TKE,LHORELAX_SV,                            &
             LHORELAX_SVC2R2,LHORELAX_SVC1R3,LHORELAX_SVELEC,LHORELAX_SVLG,   &
             LHORELAX_SVCHEM,LHORELAX_SVAER,LHORELAX_SVDST,LHORELAX_SVSLT,    &
             LHORELAX_SVPP,LHORELAX_SVCS,LHORELAX_SVCHIC,                     &
             XRIMKMAX,NRIMX,NRIMY,                                            &
             XALKTOP,XALKGRD,XALZBOT,XALZBAS,                                 &
             XT4DIFU,XT4DIFTH,XT4DIFSV,                                       &
             XCORIOX,XCORIOY,XCORIOZ,XCURVX,XCURVY,                           &
             XDXHATM,XDYHATM,XRHOM,XAF,XBFY,XCF,XTRIGSX,XTRIGSY,NIFAXX,NIFAXY,&
             XALK,XALKW,NALBOT,XALKBAS,XALKWBAS,NALBAS,                       &
             LMASK_RELAX,XKURELAX,XKVRELAX,XKWRELAX,                          &
             XDK2U,XDK4U,XDK2TH,XDK4TH,XDK2SV,XDK4SV,                         &
             LZDIFFU,XZDIFFU_HALO2,                                           &
             XBFB,XBF_SXP2_YP1_Z                                              ) 
!
!-------------------------------------------------------------------------------
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*      17.    SURFACE FIELDS
!              --------------
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*      17.1   Radiative setup
!              ---------------
!
IF (CRAD   /= 'NONE') THEN
  IF (CGETRAD =='INIT') THEN
    GINIRAD  =.TRUE.
  ELSE
    GINIRAD  =.FALSE.
  END IF
  CALL INI_RADIATIONS(TPINIFILE,HLUOUT,GINIRAD,TDTCUR,TDTEXP,XZZ, &
                      XDXX, XDYY,                         &
                      XSINDEL,XCOSDEL,XTSIDER,XCORSOL,    &
                      XSLOPANG,XSLOPAZI,                  &
                      XDTHRAD,XDIRFLASWD,XSCAFLASWD,      &
                      XFLALWD,XDIRSRFSWD,NCLEARCOL_TM1,   &
                      XZENITH,XAZIM,                      &
                      TDTRAD_FULL,TDTRAD_CLONLY,          &
                      TZINITHALO2D_ll,                    &
                      XRADEFF,XSWU,XSWD,XLWU,             &
                      XLWD,XDTHRADSW,XDTHRADLW           )
  !
  IF (GINIRAD) CALL SUNPOS_n(XZENITH,PAZIMSOL=XAZIM)
  CALL SURF_SOLAR_GEOM    (XZS, XZS_XY)
  !
  ALLOCATE(XXHAT_ll                 (IIU_ll))
  ALLOCATE(XYHAT_ll                 (IJU_ll))
  ALLOCATE(XZS_ll                   (IIU_ll,IJU_ll))
  ALLOCATE(XZS_XY_ll                (IIU_ll,IJU_ll))
  !
  CALL GATHERALL_FIELD_ll('XY',XZS,XZS_ll,IRESP)
  CALL GATHERALL_FIELD_ll('XY',XZS_XY,XZS_XY_ll,IRESP)
  CALL GATHERALL_FIELD_ll('XX',XXHAT,XXHAT_ll,IRESP)
  CALL GATHERALL_FIELD_ll('YY',XYHAT,XYHAT_ll,IRESP)
  XZS_MAX_ll=MAXVAL(XZS_ll)
ELSE
  XAZIM       = XPI
  XZENITH     = XPI/2.
  XDIRSRFSWD  = 0.
  XSCAFLASWD  = 0.
  XFLALWD     = 300.  ! W/m2
  XTSIDER     = 0.
END IF
!
!
CALL INI_SW_SETUP (CRAD,NSWB_MNH,XSW_BANDS)
!
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!       17.1.1 Special initialisation for CO2 content
!              CO2 (molar mass=44) horizontally and vertically homogeneous at 360 ppm
!
XCCO2 = 360.0E-06 * 44.0E-03 / XMD
!
!
WAUTELET Philippe's avatar
WAUTELET Philippe committed
!*      17.2   Externalized surface fields
!              ---------------------------
!
ALLOCATE(ZCO2(IIU,IJU))
ZCO2(:,:) = XCCO2
!

ALLOCATE(ZDIR_ALB(IIU,IJU,NSWB_MNH))
ALLOCATE(ZSCA_ALB(IIU,IJU,NSWB_MNH))
ALLOCATE(ZEMIS  (IIU,IJU))
ALLOCATE(ZTSRAD (IIU,IJU))
!
IF (IMASDEV>=46) THEN
  CALL IO_READ_FIELD(TPINIFILE,'SURF',CSURF)