SUBROUTINE ALLOC

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE ALLOC  ***
   !&E
   !&E ** Purpose : 
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       ! 2010-02 (G. Simon) add wave module
   !&E       ! 2011-11 (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E       ! 2017-09 (F. Ganthy) Turbulence advection (k-epsilon)
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters,ONLY : kmax,valmanq,rsh,                    &
                         limin,limax,liminm1,limaxp1,limaxp2, &
                         ljmin,ljmax,ljminm1,ljmaxp1,ljmaxp2, &
                         liminm2,ljminm2,limaxp3,ljmaxp3
   USE comvars2d, ONLY : l_modele2d,alloc_comvars2d,hex,hey,  &
                         htx,hty,l_initfromfile
   USE comfluxdo, ONLY : alloc_comfluxdo,alloc_comfluxdo3d,alloc_fsalxy
   USE comvars3d, ONLY : alloc_comvars3d,wz
#ifdef key_diag_physwz
   USE comvars3d, ONLY : wz_phys 
#endif    
#ifdef key_profil_back
   USE comvars3d, ONLY : boz2,rho2,sali_back,temp_back
#endif    
   USE cometemp,  ONLY : alloc_cometemp,alloc_ftempxy
   USE sflxatm,   ONLY : alloc_sflxatm
   USE comdem,    ONLY : alloc_comdem
   USE output,    ONLY : output_alloc
   USE cometdyn,  ONLY : alloc_cometdyn
   USE comvarp2d, ONLY : sal,temp,u,v,ssh
   USE comvarp2d, ONLY : sal2,temp2
#ifdef key_tssub_macho3d
   USE comvarp2d, ONLY : sal3,temp3
#endif
#if defined key_tssub_adv_fifthorder || defined key_tssub_adv_fourthorder
   USE comvarp2d, ONLY : sal5,temp5  !VALERIE PROVISOIRE
#endif
   USE comvarp2d, ONLY : alloc_comvarp2d
#ifdef key_turbadv
   USE comvarp3d, ONLY : boz,uz,vz,ect,ect2,psi,psi2,pn2
#else
   USE comvarp3d, ONLY : boz,uz,vz,ect,psi,pn2
#endif
   USE obccombine,ONLY : alloc_obc
   USE comturb,   ONLY : alloc_comturb
   USE comaree,   ONLY : alloc_comaree
#if defined key_tssub_wquickupwind || defined key_tssub_wcompact
   USE comfluvert,ONLY : alloc_comfluvert
#endif
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : hzex,hzey
   USE comsiggen, ONLY : alloc_comsiggen
#endif
#ifdef key_wave
   USE wave,      ONLY : wave_alloc
#endif

#ifdef key_diagdyn
  USE  diagdyn,   ONLY : diagdyn_alloc
#endif
#ifdef key_wave_qeuler_diag
  USE  wavediag,   ONLY : wave_diag_qeuler_alloc
#endif
   !! * Arguments

   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   CALL alloc_comvars2d
   CALL output_alloc
   CALL alloc_comdem
   CALL alloc_cometdyn
#ifdef key_wave
   CALL wave_alloc
#endif
   CALL alloc_cometemp
   CALL alloc_comfluxdo
   CALL alloc_comvars3d
   CALL alloc_sflxatm
   CALL alloc_obc
   CALL alloc_ftempxy
   CALL alloc_fsalxy
   ! Allocation of variables : eps
   CALL alloc_comturb
   ! Allocation of variables : potgene
   CALL alloc_comaree
   ! Allocation of variables : u_vtmp,v_utmp
   CALL alloc_comvarp2d
#if defined key_tssub_wquickupwind || defined key_tssub_wcompact
   CALL alloc_comfluvert
#endif
   ! Allocation of variables of the different terms of the moment equations
   ! for diagnoses purpose
#ifdef key_diagdyn
    CALL diagdyn_alloc
#endif
#ifdef key_wave_qeuler_diag
    CALL wave_diag_qeuler_alloc
#endif

   IF(.NOT.l_modele2d) CALL alloc_comfluxdo3d

   ! Allocate dynamics variables
#ifdef key_profil_back
   ALLOCATE( boz2(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) ) 
   boz2(:,:,:)=valmanq
   ALLOCATE( rho2(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) ) 
   ALLOCATE( temp_back(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) ) 
   ALLOCATE( sali_back(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) ) 
#endif

   ALLOCATE( boz(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) )
   boz(:,:,:)=valmanq
   ALLOCATE( pn2(1:kmax,limin:limax,ljmin:ljmax) )
   pn2(:,:,:)=0.0_rsh
   ALLOCATE( hex(liminm2:limaxp2,ljmin:ljmax) )
   ALLOCATE( hey(limin:limax,ljminm2:ljmaxp2) )
   hex(:,:)=0.0_rsh
   hey(:,:)=0.0_rsh
   !ALLOCATE( htx(liminm1:limax  ,ljminm1:ljmaxp1) )
   !ALLOCATE( hty(liminm1:limaxp1,ljminm1:ljmax  ) )
   ! larger because of interpvit (in avance) in trajectools.F90
   ALLOCATE( htx(liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( hty(liminm2:limaxp2,ljminm2:ljmaxp2) )
   htx(:,:)=0.0_rsh ! to manage with sleeping cpu
   hty(:,:)=0.0_rsh
   ALLOCATE( ect(1:kmax,limin:limax,ljmin:ljmax) )
   ALLOCATE( psi(1:kmax,limin:limax,ljmin:ljmax) )
   ect(:,:,:)=0.0_rsh
   psi(:,:,:)=0.0_rsh
#ifdef key_turbadv
   ALLOCATE( ect2(1:kmax,limin:limax,ljmin:ljmax) )
   ALLOCATE( psi2(1:kmax,limin:limax,ljmin:ljmax) )
   ect2(:,:,:)=0.0_rsh
   psi2(:,:,:)=0.0_rsh
#endif

   ALLOCATE( u(liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( v(liminm2:limaxp2,ljminm2:ljmaxp2) )
   u(:,:)=0.0_rsh ! to manage with sleeping cpu
   v(:,:)=0.0_rsh
   ALLOCATE( uz(1:kmax,liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( vz(1:kmax,liminm2:limaxp2,ljminm2:ljmaxp2) )
   !ALLOCATE( wz(0:kmax,limin:limaxp1,ljmin:ljmaxp1) )
   ! larger extension because of traj_3d routine
   ! might be not sufficient because of the medium location
   ALLOCATE( wz(0:kmax,liminm1:limaxp1,ljminm1:ljmaxp1) )
   wz(:,:,:)=0.0_rsh
#ifdef key_diag_physwz
   ALLOCATE( wz_phys(0:kmax,limin:limax,ljmin:ljmax) ) 
   wz_phys(:,:,:)=0.0_rsh
#endif
   ALLOCATE( ssh(liminm2:limaxp2,ljminm2:ljmaxp2) )
   ssh(:,:)=0.0_rsh
   
#if defined key_agrif
   ALLOCATE( ssh_old(liminm2:limaxp2,ljminm2:ljmaxp2) )
   ssh_old(:,:) = 0.0_rsh
#endif

   ! Allocate thermodynamics variables
   ALLOCATE( sal(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) )
   ALLOCATE( temp(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) )
   ALLOCATE( sal2(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) )
   ALLOCATE( temp2(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) )
   sal2(:,:,:)=0.0_rsh
   temp2(:,:,:)=0.0_rsh
#ifdef key_tssub_macho3d
   ALLOCATE( temp3(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) )
   ALLOCATE( sal3(1:kmax,liminm1:limaxp2,ljminm1:ljmaxp2) )
   sal3(:,:,:)=0.0_rsh
   temp3(:,:,:)=0.0_rsh
#endif
#if defined key_tssub_adv_fifthorder || defined key_tssub_adv_fourthorder
   ALLOCATE( temp5(1:kmax,liminm2:limaxp3,ljminm2:ljmaxp3) ) !VALERIE PROVISOIRE
   ALLOCATE( sal5(1:kmax,liminm2:limaxp3,ljminm2:ljmaxp3) )
   sal5(:,:,:)=0.0_rsh
   temp5(:,:,:)=0.0_rsh
#endif

   ! Variables relative to generalized vertical coordinates
#if defined key_siggen || defined key_gencoord
   ! Allocation of variables : hzutx,hzuty,hzwtx,hzwty
   CALL alloc_comsiggen
   ALLOCATE(hzex(1:kmax,liminm1:limax,ljmin  :ljmax) )
   ALLOCATE(hzey(1:kmax,limin  :limax,ljminm1:ljmax) )
   hzex(:,:,:)=0.0_rsh
   hzey(:,:,:)=0.0_rsh
#endif

#ifdef key_plus
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
   ALLOCATE( (liminm2:limaxp2,ljminm2:ljmaxp2) )
#endif

  END SUBROUTINE ALLOC
