MODULE comvars2d

   !!======================================================================
   !!                   ***  MODULE comvars2d  ***
   !! 
   !! ** Description :
   !! 
   !! ** Reference :
   !! 
   !! ** History :
   !!              2008-08  (T. Odaka) MPI
   !!              2010-02  (G. Simon) add name_out_wave_dir,name_out_wave_hs,name_out_wave_foc
   !!        !  2010-07 (R. Verney) Introduction of orbital velocity and wave period variables
   !!        !  2015-09 (F. Ganthy) Introduction of a spatialized bottom roughness length
   !!        !  2016-12 (B. Thouvenin. A. Fiandrino) intoduction of key_lagoon
   !!
   !!======================================================================

#include "toolcpp.h"

   !! * Modules used
   USE parameters, ONLY : lchain,rsh,rlg,riolg,riosh,l_testcase
   USE parameters, ONLY : kmax,imin,imax,jmin,jmax ,nbchprox
   USE parameters, ONLY : liminm1,limaxp1,ljminm1,ljmaxp1,limin,limax,ljmin,ljmax
   USE parameters, ONLY : liminm2,limaxp2,ljminm2,ljmaxp2
#ifdef key_lagoon
   USE parameters, ONLY : nbgraumax
#endif

   !! * Shared or public module variables

   LOGICAL               :: l_bathy_comb,l_bathy_pit,l_tssubadv_over_smalldepth,l_bathy_meanlev,l_bathy_save
   CHARACTER(len=lchain) :: file_bathy,file_bathy_child
   CHARACTER(len=lchain) :: name_bathy_hx,name_bathy_hy,name_bathy_h0,name_bathy_meanlev
   INTEGER               :: bathy_nbsmooth
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:) :: hex,hey
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:) :: htx,hty
   REAL(kind=rsh),DIMENSION(imin:imax,jmin:jmax) :: hx_g,hy_g,dhx_g,dhy_g,h0_g,dhxy_g,hnivmoy_g
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: hx,hy,dhx,dhy,h0
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: dhxy, & ! maximum of denivelation at location u or v of a cell
                                                    fwet, & ! fraction of wet surface
                                                    fwetp   ! fraction of wet surface of previous time step
   REAL(kind=rsh)        :: mslshift,hrdef,hminfrot,hmaxfrot,h0fond,dhj,hminim,hminkxky,fwetmin
#if defined key_daily_climato_kpar
   REAL(kind=rsh),DIMENSION(imin:imax,jmin:jmax) :: kpar_sat
   INTEGER :: jjulien_last
#endif
   
   !---------------------------------------------------------------------
#ifdef key_lagoon
   INTEGER               :: nbgrau
   INTEGER,DIMENSION(nbgraumax) :: igrau,jgrau,type_grau
   LOGICAL,DIMENSION(nbgraumax) :: l_open_grau
   REAL(kind=rsh)        :: hnivmoy_lagoon
   REAL(kind=rsh),DIMENSION(imin:imax,jmin:jmax) :: hnivmoy
   REAL(kind=rsh),DIMENSION(:),allocatable  :: cl_heau
   REAL(kind=rsh),DIMENSION(nbgraumax) :: obc_sgrau,obc_tgrau
#endif
#ifdef key_lagoon_spec
! le 28/05/2013 Définition d'un masque dans les zones conch de la lagune de Thau
   REAL(kind=rsh),DIMENSION(:,:),ALLOCATABLE :: xmask_h0,xmask_hx,xmask_hy
#endif


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

#ifdef key_casbcvortex
   REAL(kind=rsh),PARAMETER                      :: radearth=6.367517e+6
#else
   REAL(kind=rsh),PARAMETER                      :: radearth=6.367e+6
#endif
#ifdef key_grid_curv
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: grid_angle2D
#endif
   REAL(kind=rsh)                                :: grid_angle
   LOGICAL                                       :: l_corio_cst,l_obc_cycl_x,l_obc_cycl_y
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: cosfiu,sin2fi,coscafi,sinfiu
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: fu,fv,tfivsr,tfiusr
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: dx,dx_u,dx_v,dx_f
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: dy,dy_u,dy_v,dy_f
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: cosg,sing,cos2g,sin2g
   REAL(kind=rlg),ALLOCATABLE,DIMENSION(:,:)     :: lon2d,lat2d,lon2du,lon2dv,lon2df,lat2du,lat2dv,lat2df
   REAL(kind=rlg)                                :: lonmin,latmin,lonmax,latmax
   REAL(kind=rlg),DIMENSION(imin:imax,jmin:jmax) :: lon2d_g,lat2d_g,lon2du_g,lon2dv_g,lon2df_g,lat2du_g,lat2dv_g,lat2df_g
#ifdef key_grid_curv
   REAL(kind=rsh),DIMENSION(imin:imax,jmin:jmax)     :: grid_angle2D_g
   REAL(kind=rsh),DIMENSION(imin:imax,jmin:jmax)     :: dx_g,dy_g,dxu_g,dyu_g,dxv_g,dyv_g,dxf_g,dyf_g 
#endif

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

   INTEGER                                       :: jhemisph
   INTEGER,DIMENSION(imin:imax)                  :: jb,jh
   INTEGER,DIMENSION(jmin:jmax)                  :: ig,id
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: surf,goulet,govlet

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

   LOGICAL :: l_saverestart_1file,l_initfromfile,l_saverestart_bydate,l_init_rtime, &
              l_init_rdt,l_init_rssh,l_init_rbtvel,l_init_r3dvel,l_init_rwz,        &
              l_restart_subs,l_init_rturb,l_init_rsal,l_init_rtemp,l_boolsauv,      &
              l_init_restart
   CHARACTER(len=lchain) :: file_saverestart,file_init
   REAL(kind=rlg)        :: saverestart_step,tsauv

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

   !  Names of input variables
   CHARACTER(len=lchain) :: name_in_dt,name_in_xe,name_in_xev,              &
                            name_in_u,name_in_v,name_in_uz,name_in_vz,      &
                            name_in_wz,name_in_temp,name_in_sal,name_in_psi,&
                            name_in_ect,name_in_kz,name_in_nz,name_in_vis2d,&
                            name_in_vis3d_xe,name_in_vis3d_phi,             &
                            ! name of output variables for offline purpose
                            name_in_mhex,name_in_mhey,name_in_mxe,name_in_mkz
   CHARACTER(len=lchain) :: name_in_longitude,name_in_longitude_u,            &
                            name_in_longitude_v,name_in_longitude_f,          &
                            name_in_latitude,name_in_latitude_u,              &
                            name_in_latitude_v,name_in_latitude_f

   !  Names of output variables
   CHARACTER(len=lchain) :: name_out_h0,name_out_hx,name_out_hy,name_out_u,        &
                            name_out_v,name_out_uz,name_out_vz,name_out_temp,      &
                            name_out_sal,name_out_xe,name_out_tosx,name_out_tosy,  &
                            name_out_vis2d,name_out_vishxe,name_out_vishphi,       &
                            name_out_nz,name_out_kz,name_out_bz,name_out_diag_f7_x,     &
                            name_out_diag_f1_x,name_out_diag_f2_x,name_out_diag_f3_x,     &
                            name_out_diag_f4_x,name_out_diag_f5_x,name_out_diag_f6_x,     &
                            name_out_diag_f8_x,name_out_diag_f9_x,name_out_diag_f10_x,    &
                            name_out_diag_f11_x,name_out_diag_f12_x,name_out_diag_f13_x,  &
                            name_out_diag_f1_y,name_out_diag_f2_y,name_out_diag_f3_y,     &
                            name_out_diag_f4_y,name_out_diag_f5_y,name_out_diag_f6_y,     &
                            name_out_diag_f7_y,name_out_diag_f8_y,name_out_diag_f9_y,     &
                            name_out_diag_f10_y,name_out_diag_f11_y,name_out_diag_f12_y,  &
                            name_out_diag_f13_y,                                          &
                            ! name of output variables for offline purpose
                            name_out_mhex,name_out_mhey,name_out_mxe,name_out_mkz, &
                            ! name of cumulative atmospherical fluxes
                            name_out_mrad,name_out_mirlos,name_out_mlatlos,        &
                            name_out_msenslos,name_out_mevap,name_out_mrain,       &
                            name_out_mtosx,name_out_mtosy
#ifdef key_sflx_analyse
   CHARACTER(len=lchain) :: name_out_mwindx,name_out_mwindy,name_out_mrh,          &
                            name_out_mfcc,name_out_msat,name_out_msst,name_out_mpa
#endif
   LOGICAL               :: l_out_subs_diag,l_boolsort,l_maxu
   CHARACTER(len=lchain) :: root,out_dir
   REAL(kind=rlg)        :: date_startshift

   !  Minimum and maximum valid values as attributes in netCDF file
   REAL(KIND=riosh) :: riog_valid_min_h0,riog_valid_max_h0,         &
                     riog_valid_min_u,riog_valid_max_u,             &
                     riog_valid_min_diag,riog_valid_max_diag,       &
                     riog_valid_min_temp,riog_valid_max_temp,       &
                     riog_valid_min_sal,riog_valid_max_sal,         &
                     riog_valid_min_xe,riog_valid_max_xe,           &
                     riog_valid_min_wstress,riog_valid_max_wstress, &
                     riog_valid_min_flux,riog_valid_max_flux,       &
                     riog_valid_min_visc,riog_valid_max_visc,       &
                     riog_valid_min_nz,riog_valid_max_nz,           &
                     riog_valid_min_kz,riog_valid_max_kz,           &
                     riog_valid_min_bz,riog_valid_max_bz
   REAL(KIND=riolg) :: riog_valid_min_dx,riog_valid_min_dy,         &
                     riog_valid_max_dx,riog_valid_max_dy

   ! Global attributes of output files
   CHARACTER(LEN=lchain) :: cg_conv_title,cg_conv_comment,cg_conv_source,      &
                            cg_conv_area,cg_conv_institution,                  &
                            cg_conv_institution_references,cg_conv_data_centre,&
                            cg_conv_data_centre_references,                    &
                            cg_conv_forecast_range,cg_conv_forecast_type,      &
                            cg_conv_field_type,cg_conv_operational_status

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

   LOGICAL          :: l_modele2d
   CHARACTER(len=4) :: nracin,comdir
   INTEGER          :: icon,nbouc,nbouc_macho
   REAL(kind=rsh)   :: hm,cflumaxmax,cflvmaxmax,cflcrt
   REAL(kind=rlg)   :: dt,dtmax,dtmaxi,tdeb3d,tfin,tdeb,tobs,tdebobs,dtmin,tdebagrif,halfdt,dt_minallow
#if defined key_agrif
   REAL(kind=rlg)   :: tdebagrif_messat
!$AGRIF_DO_NOT_TREAT
   CHARACTER(len=lchain) :: c_agrif_interp
   CHARACTER(len=lchain) :: c_agrif_update
   LOGICAL :: first_agrif_update
!$AGRIF_END_DO_NOT_TREAT
#endif

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

   LOGICAL        :: l_smagor,l_spongw,l_sponge,l_spongs,l_spongn
   INTEGER        :: sponge_nbcell
   REAL(kind=rsh) :: fvisc,vismul,vismin,vismax,cosmag,kx,ky,qmax

   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)   :: vis2d
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:,:) :: vish_phi,vish_xe

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

   LOGICAL                                       :: l_incbotstress
   REAL(kind=rsh)                                :: botstressmax,frot_lin,z0bot,z0surf
   REAL(kind=rsh),ALLOCATABLE,DIMENSION(:,:)     :: z0b,z0s,z0b_init   !!!FG(09/11/2018)
   LOGICAL               :: l_z0b_hom
   CHARACTER(len=lchain) :: file_z0b

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

   LOGICAL               :: l_obc2drank_save
   CHARACTER(len=lchain) :: filecl,fileclsv,confname_mother,confname,suffix, &
                            file_obc_3d,file_obc_ogcm_s,file_obc_ogcm_n,file_obc_ogcm_e,file_obc_ogcm_w, &
                            file_obc_3d_subs,file_obc_subs_s,file_obc_subs_n,file_obc_subs_e,file_obc_subs_w
#ifdef key_lagoon
   CHARACTER(len=lchain) :: file_obc_ogcm_ts
#endif
   INTEGER               :: inc,climnum,simunum

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

   ! suffix of the child grid
   CHARACTER(len=lchain),ALLOCATABLE,DIMENSION(:) :: obcrk_nameconf
   INTEGER                                   :: imaxac,jmaxac,jnac,jsac,ieac,iwac,obcrk_maxnbfile
   INTEGER        ,ALLOCATABLE, DIMENSION(:) :: obcrk_ie,obcrk_iw,obcrk_js,obcrk_jn
   REAL(kind=rlg)                            :: dyac,dfisv,dgsv,dfip,dgp,fissv,gwsv
   REAL(kind=riolg)                          :: finac,fisac,geac,gwac,dfiac,dgac,dxac

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

   CHARACTER(len=lchain) :: filename     ! listing file
   CHARACTER(len=19)     :: cdate
   CHARACTER(len=20)     :: date_ref
   INTEGER               :: ian,imois,ijour,iheure,iminu,isec,jjulien
   INTEGER               :: iscreen,iscreenlog,ierrorlog,iwarnlog
   REAL(kind=rlg)        :: t

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

   LOGICAL                                 :: l_chrono
   INTEGER                                 :: indchrono,nvalmax
   REAL(kind=rlg)                          :: perchrono,tchrono_deb
   REAL(kind=rlg),DIMENSION(:),ALLOCATABLE :: tchrono

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

#if defined key_ts_diffh_geo || defined key_ts_diffh_rho
   ! j-limit for switching thread for tracer diffusion calculation along geopotentials
   INTEGER,DIMENSION(:),ALLOCATABLE :: jbound_omp
#endif

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

   LOGICAL                              :: l_champroche
   INTEGER                              :: ntot
   INTEGER       ,DIMENSION(nbchprox+1) :: numcon
   REAL(kind=rsh),DIMENSION(nbchprox+1) :: xprx,yprx,qte
   REAL(kind=rlg)                       :: agemax
   REAL(kind=rlg),DIMENSION(nbchprox+1) :: age

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

   LOGICAL                                                              :: l_tide_surge
   CHARACTER(len=lchain)                                                :: file_tide_harmcp_surge, &
                                                                           file_surge_meteo,file_surge_nometeo
   !----------------------------------------------------------------------

#if defined key_MPI || defined key_MPI_2D
    INTEGER                            :: type_mpi_rsh, type_mpi_rlg
#endif
 
   !---------------------------------------------------------

#if defined key_toseelater
   ! canal cyclique avec sedim et morphodynamic
   ! force motrice terme a ajouter dans l equation Dt/2*CD*U*U/h (15 juin 2012)
   REAL(kind=rsh)                           :: CDU2_h
#endif

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

   ! testcase flume (cyclic or not)
   REAL(kind=rsh) :: uforcing


 CONTAINS

   !!==============================================================================

  SUBROUTINE alloc_comvars2d

   !&E--------------------------------------------------------------------------
   !&E                 ***  ROUTINE alloc_comvars2d  ***
   !&E  
   !&E--------------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !!----------------------------------------------------------------------
   !! * Executable part

!   IF (.NOT.l_modele2d) THEN
     ALLOCATE(vish_xe(kmax,limin:limaxp1,ljmin:ljmaxp1) )
     ALLOCATE(vish_phi(kmax,liminm1:limax,ljminm1:ljmax) )
     vish_xe(:,:,:)=0.0_rsh
     vish_phi(:,:,:)=0.0_rsh
!   ENDIF
     ALLOCATE(vis2d(liminm1:limaxp1,ljminm1:ljmaxp1) )
     vis2d(:,:)=0.0_rsh
      

  END SUBROUTINE alloc_comvars2d

   !!==============================================================================

  SUBROUTINE dealloc_comvars2d

   !&E--------------------------------------------------------------------------
   !&E                 ***  ROUTINE dealloc_comvars2d  ***
   !&E  
   !&E--------------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !!----------------------------------------------------------------------
   !! * Executable part

!   IF (.NOT.l_modele2d) THEN
     DEALLOCATE(vish_phi,vish_xe)
!   ENDIF
     DEALLOCATE(vis2d)

  END SUBROUTINE dealloc_comvars2d
   !!==============================================================================

  SUBROUTINE alloc_comvars2d_grid

   !&E--------------------------------------------------------------------------
   !&E                 ***  ROUTINE alloc_comvars2d_grid  ***
   !&E
   !&E--------------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !!----------------------------------------------------------------------
   !! * Executable part

     ALLOCATE(h0(liminm2:limaxp2,ljminm2:ljmaxp2))
     ALLOCATE(hx(liminm2:limaxp2,ljminm2:ljmaxp2))
     ALLOCATE(hy(liminm2:limaxp2,ljminm2:ljmaxp2))

     ALLOCATE(dhx(liminm2:limaxp2,ljminm2:ljmaxp2)) ! larger extension for agrif
     ALLOCATE(dhy(liminm2:limaxp2,ljminm2:ljmaxp2)) ! larger extension for agrif
     ALLOCATE(dhxy(limin:limax,ljmin:ljmax))
     ALLOCATE( fwet(limin:limax,ljmin:ljmax))
     ALLOCATE(fwetp(limin:limax,ljmin:ljmax))

     ALLOCATE(surf(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil to avoid MPI exchanges
#ifdef key_grid_curv
     ALLOCATE(grid_angle2D(limin:limax,ljmin:ljmax) )
#endif 
     ! variables with stencil larger than usefull to allow estimate on larger stencil and avoid exchanges of 3D variables
     ! or to avoid problems with sleeping cpus
     ALLOCATE(cosg(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(sing(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(cos2g(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(sin2g(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(goulet(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(govlet(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(cosfiu(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(sinfiu(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(sin2fi(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(coscafi(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(fu(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(fv(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(tfiusr(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(tfivsr(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     !ALLOCATE(dx(liminm1:limaxp1,ljmin:ljmax) )
     ! larger extension because of avance routine in trajectools.F90
     ALLOCATE(dx(liminm2:limaxp2,ljminm2:ljmaxp2) )
     ALLOCATE(dx_u(liminm2:limaxp2,ljminm2:ljmaxp2))
     ALLOCATE(dx_v(liminm2:limaxp2,ljminm2:ljmaxp2) )
     ALLOCATE(dx_f(liminm2:limaxp2,ljminm2:ljmaxp2))
     !ALLOCATE(dy(limin:limax,ljminm1:ljmaxp1) )
     ! larger extension because of avance routine in trajectools.F90
     ALLOCATE(dy(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(dy_u(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(dy_v(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(dy_f(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lon2d(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lon2du(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lon2dv(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lon2df(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lat2d(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lat2du(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lat2dv(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ALLOCATE(lat2df(liminm2:limaxp2,ljminm2:ljmaxp2) ) ! enlarged stencil
     ! end of variable with too large stencil

     IF (.NOT. l_testcase) THEN
       h0(:,:) = h0_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       hx(:,:) = hx_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       hy(:,:) = hy_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       dhx(:,:) = dhx_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       dhy(:,:) = dhy_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       dhxy(:,:) = dhxy_g(limin:limax,ljmin:ljmax)
       fwet(:,:) = 1.0_rsh
       fwetp(:,:) = 1.0_rsh
       lon2d(:,:) = lon2d_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       lon2du(:,:) = lon2du_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       lon2dv(:,:) = lon2dv_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       lon2df(:,:) = lon2df_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       lat2d(:,:) = lat2d_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       lat2du(:,:) = lat2du_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       lat2dv(:,:) = lat2dv_g(liminm2:limaxp2,ljminm2:ljmaxp2)
       lat2df(:,:) = lat2df_g(liminm2:limaxp2,ljminm2:ljmaxp2)
#ifdef key_grid_curv
       grid_angle2D(:,:)=grid_angle2D_g(limin:limax,ljmin:ljmax)
#endif
     ENDIF

  END SUBROUTINE alloc_comvars2d_grid

   !!==============================================================================

  SUBROUTINE alloc_comvars2d_rough

   !&E--------------------------------------------------------------------------
   !&E                 ***  ROUTINE alloc_comvars2d_rough  ***
   !&E
   !&E--------------------------------------------------------------------------
   !! * Modules used
     IMPLICIT NONE
   !! * Local declarations

   !!----------------------------------------------------------------------
   !! * Executable part
   ALLOCATE(z0b(limin:limax,ljmin:ljmax) )
   ALLOCATE(z0s(limin:limax,ljmin:ljmax) )
   !!!FG(09/11/2018)
   ALLOCATE(z0b_init(limin:limax,ljmin:ljmax) )
   z0s(:,:)=1e-8_rsh

   IF (.NOT.l_testcase) THEN

     ! initialize the roughness length at the bottom
     ! ---------------------------------------------
     IF (l_z0b_hom) THEN
       z0b(:,:)=z0bot
     ELSE
       CALL init_bottom_roughness
     ENDIF
     z0s(:,:)=z0surf
     !!!FG(09/11/2018)
     z0b_init(:,:)=z0b(:,:)

   END IF

  END SUBROUTINE alloc_comvars2d_rough

   !!==============================================================================

  SUBROUTINE dealloc_comvars2d_grid

   !&E--------------------------------------------------------------------------
   !&E                 ***  ROUTINE dealloc_comvars2d_grid  ***
   !&E
   !&E--------------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !!----------------------------------------------------------------------
   !! * Executable part

     DEALLOCATE(cosg,sing,cos2g,sin2g,surf,govlet,goulet,cosfiu,sinfiu,sin2fi,coscafi)
     DEALLOCATE(fu,fv,tfiusr,tfivsr)
     DEALLOCATE(dx,dx_u,dx_v,dx_f,dy,dy_u,dy_v,dy_f)
  END SUBROUTINE dealloc_comvars2d_grid

  !!==============================================================================

  SUBROUTINE dealloc_comvars2d_rough

   !&E--------------------------------------------------------------------------
   !&E                 ***  ROUTINE dealloc_comvars2d_rough  ***
   !&E
   !&E--------------------------------------------------------------------------
   !! * Modules used
   IMPLICIT NONE

   !!----------------------------------------------------------------------
   !! * Executable part

     DEALLOCATE(z0b,z0s)

  END SUBROUTINE dealloc_comvars2d_rough

 END MODULE comvars2d
