!********************************************************************
!     
  SUBROUTINE agrif_declare_variables

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE  agrif_declare_variable  ***
   !&E
   !&E ** Purpose : Declare profile ID for interpolation and/or update 
   !&E              variables
   !&E
   !&E ** Description : 
   !&E
   !&E ** Called by : 
   !&E
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E                   Original code
   !&E             2010-06  (F. Dumas ) 
   !&E             2011-11  (V. Garnier) Spatial extension of ssh(liminm2:limaxp2,ljminm2:ljmaxp2)
   !&E             2016-01  (S. Petton) set_bc /0,0/ to interpolate the mother field on the cell (only) around the zoom
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
#ifdef key_agrif
   USE parameters
   USE agrifcommonid

   !! * Arguments

   !! * local declarations
   INTEGER :: type_update_traceur, type_update_u_x, type_update_u_y, type_update_v_x, type_update_v_y

   INTEGER :: type_interp, type_interp_conserv

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

   ! LOW ORDER update schemes
   type_update_traceur = Agrif_Update_Average 
   type_update_u_x     = Agrif_Update_Copy
   type_update_u_y     = Agrif_Update_Average
   type_update_v_x     = Agrif_Update_Average
   type_update_v_y     = Agrif_Update_Copy

   ! HIGH ORDER update schemes
   !   type_update_traceur = Agrif_Update_Full_Weighting 
   ! au point u de la grille C dans la direction x
   !   type_update_u_x     = Agrif_Update_Average
   ! au point u de la grille C dans la direction y
   !   type_update_u_y     = Agrif_Update_Full_Weighting
   ! au point v de la grille C dans la direction x
   !   type_update_v_x     = Agrif_Update_Full_Weighting
   ! au point v de la grille C dans la direction y
   !   type_update_v_y     = Agrif_Update_Average
   !
   !  type_interp = Agrif_linear
   ! pour interpoler tout (ssh, traceurs et vitesses tangentielle aux frontieres
   ! voir dans AGRIF/AGRIF_FILES modtypes pour la liste de ce qui est disponible
   !
   type_interp = Agrif_lagrange
   
   ! pour interpoler les vitesses u a l est et ouest et v au nord et au sud (perpendiculaires aux frontieres)
   type_interp_conserv = Agrif_ppm

   ! profile dans Agrif_Declare_Variable precise sur quels indices on va faire les interpolations (et update ? cval verifier)
   ! comme dans MARS l ensemble du stencil est reellement utilise et initialise
   ! - le profile peut etre egal au stencil reel du tableau (donc plus grand que limin:limax,ljmin:ljmax)
   ! - si le profile correspond a la taille max du tableau, les echanges MPI deviennent inutiles

   ! Reminder
   !   CALL Agrif_Declare_Variable( 
   !        (/0,2,2/),            posvar : position of the variable on the cell = 1 for the border of the edge, 2 for the center
   !        (/0,2,2/),        firstpoint : index of the first point in the real domain
   !        (/'N','x','y'/),         raf : defined the type of dimension, space or not. relative to the structure array
   !        (/1,limin,ljmin/),        lb : Lower bounds of the array
   !        (/kmax,limax,ljmax/),     ub : Upper bounds of the array
   !        var_id)                varid : Id number of the newly created variable 

   ! Profil used for SSH update
   CALL Agrif_Declare_Variable((/2,2/),(/2,2/),(/'x','y'/),(/liminm2,ljminm2/),(/limaxp2,ljmaxp2/),ssh_id)
   CALL Agrif_Set_UpdateType(ssh_id,update=type_update_traceur)

   ! Profil used for SSH interpolation over boundary conditions 
   CALL Agrif_Declare_Variable((/2,2/),(/2,2/),(/'x','y'/),(/liminm2,ljminm2/),(/limaxp2,ljmaxp2/),ssh_interp_id)
   CALL Agrif_Set_bc(ssh_interp_id,(/0,0/))
   CALL Agrif_Set_bcinterp(ssh_interp_id,interp=type_interp)
   CALL Agrif_Set_interp(ssh_interp_id,interp=type_interp)

   ! Profil used for SSH interpolation of initial conditions
   CALL Agrif_Declare_Variable((/2,2/),(/2,2/),(/'x','y'/),(/liminm2,ljminm2/),(/limaxp2,ljmaxp2/),ssh_interp_init_id)
   CALL Agrif_Set_bc(ssh_interp_init_id,(/0,0/))
   CALL Agrif_Set_bcinterp(ssh_interp_init_id,interp=type_interp)
   CALL Agrif_Set_interp(ssh_interp_init_id,interp=type_interp)

   ! Profil used for surf update (at the initialisation of the child grid only)
   CALL Agrif_Declare_Variable((/2,2/),(/2,2/),(/'x','y'/),(/liminm2,ljminm2/),(/limaxp2,ljmaxp2/),surf_id)
   CALL Agrif_Set_UpdateType(surf_id,update=type_update_traceur)

   ! Profil used for fwet update 
   CALL Agrif_Declare_Variable((/2,2/),(/2,2/),(/'x','y'/),(/limin,ljmin/),(/limax,ljmax/),fwet_id)
   CALL Agrif_Set_UpdateType(fwet_id,update=type_update_traceur)

! NOT USED !   ! Profil used for surf (at beginning only) and fwet update 
! NOT USED !   CALL Agrif_Declare_Variable((/2,2/),(/2,2/),(/'x','y'/),(/limin,ljmin/),(/limax,ljmax/),h0_id)
! NOT USED !   CALL Agrif_Set_UpdateType(h0_id,update=type_update_traceur)
! NOT USED !   CALL Agrif_Set_UpdateType(h0_id,update=agrif_update_copy)
! NOT USED !   CALL Agrif_Set_bc(h0_id,(/-2*nb_connect*Agrif_irhox(),1/)) ! why -*2 ? ok not used in the code
! NOT USED !   CALL Agrif_Set_bcinterp(h0_id,interp=type_interp)
! NOT USED !   CALL Agrif_Set_interp(  h0_id,interp=type_interp)

! NOT USED !   CALL Agrif_Declare_Variable((/2,2,0/),(/2,2,0/),(/'x','y','N'/),(/liminm2,ljminm2,1/),(/limaxp2,ljmaxp2,2/),h0_interp_id)
! NOT USED !   CALL Agrif_Set_bc(h0_interp_id,(/-2*nb_connect*Agrif_irhox(),1/))
! NOT USED !   CALL Agrif_Set_bcinterp(h0_interp_id,interp=type_interp)
! NOT USED !   CALL Agrif_Set_interp(h0_interp_id,interp=type_interp)

! NOT USED !   CALL Agrif_Declare_Variable((/1,2/),(/1,2/),(/'x','y'/),(/liminm2,ljminm2/),(/limaxp2,ljmaxp2/),hx_id)
! NOT USED !   CALL Agrif_Set_UpdateType(hx_id,update1=type_update_u_x,update2=type_update_u_y)
 
! NOT USED !   CALL Agrif_Declare_Variable((/1,2,0/),(/1,2,0/),(/'x','y','N'/),(/liminm2,ljminm2,1/),(/limaxp2,ljmaxp2,2/),hx_interp_id)
! NOT USED !   CALL Agrif_Set_bc(hx_interp_id,(/-2*nb_connect*Agrif_irhox(),1/))
! NOT USED !   CALL Agrif_Set_bcinterp(hx_interp_id,interp1=Agrif_linear,interp2=type_interp)
! NOT USED !   CALL Agrif_Set_interp(hx_interp_id,interp1=Agrif_linear,interp2=type_interp)

! NOT USED !   CALL Agrif_Declare_Variable((/2,1/),(/2,1/),(/'x','y'/),(/liminm2,ljminm2/),(/limaxp2,ljmaxp2/),hy_id)
! NOT USED !   CALL Agrif_Set_UpdateType(hy_id,update2=type_update_v_y,update1=type_update_v_x)

! NOT USED !   CALL Agrif_Declare_Variable((/2,1,0/),(/2,1,0/),(/'x','y','N'/),(/liminm2,ljminm2,1/),(/limaxp2,ljmaxp2,2/),hy_interp_id)
! NOT USED !   CALL Agrif_Set_bc(hy_interp_id,(/-2*nb_connect*Agrif_irhox(),1/))
! NOT USED !   CALL Agrif_Set_bcinterp(hy_interp_id,interp2=Agrif_linear,interp1=type_interp)
! NOT USED !   CALL Agrif_Set_interp(hy_interp_id,interp2=Agrif_linear,interp1=type_interp)

! because in agrif_dyn3d_height_mask (in Agrif_User, used for hex_loc) the do loop runs on limin,limax
! while in interpu_stepu or interpv_stepv the do loop is on i1:i2 j1:j2 (which
! is defined by the Agrif_Declare_Variable function below
!  CALL Agrif_Declare_Variable((/1,2,0/),(/1,2,0/),(/'x','y','N'/),(/liminm2,ljminm2,1/),(/limaxp2,ljmaxp2,2/),u_id)

   ! Profil used for U interpolation and boundary conditions
   CALL Agrif_Declare_Variable((/1,2,0/),(/1,2,0/),(/'x','y','N'/),(/limin,ljmin,1/),(/limax,ljmax,2/),u_id)
   CALL Agrif_Set_bc(u_id,(/0,1/))
   CALL Agrif_Set_bcinterp(u_id,interp1=Agrif_linear,interp2=type_interp_conserv) !       linear in x direction (1st direction)
   CALL Agrif_Set_interp(  u_id,interp1=Agrif_linear,interp2=type_interp_conserv) ! conservative in y direction (2nd direction)
! NOT USED !   CALL Agrif_Set_UpdateType(u_id,update1=type_update_u_x,update2=type_update_u_y)

   ! uz_id and vz_id profiles are declared from minm1 to maxp1 in order to correctly compute the sponge in min and max
   ! en nombre de points fin : un nombre negatif signifie que l on va a l interieur du domaine fin
   ! donc la on y a va sur nbconnect mailles grossieres et vers l exterieur sur 1 maille fine

   ! Profil used for UZ interpolation and boundary conditions
!  CALL Agrif_Declare_Variable((/0,1,2/),(/1,2,0/),(/'N','x','y'/),(/1,liminm2,ljminm2/),(/kmax,limaxp2,ljmaxp2/),uz_id)
   CALL Agrif_Declare_Variable((/0,1,2/),(/0,1,2/),(/'N','x','y'/),(/1,liminm1,ljminm1/),(/kmax,limaxp1,ljmaxp1/),uz_id)
#if defined key_agrif_obc_sponge_uv
   CALL Agrif_Set_bc(uz_id,(/-nb_connect*Agrif_irhox(),1/))
#else
   CALL Agrif_Set_bc(uz_id,(/0,1/))
#endif
   CALL Agrif_Set_interp(  uz_id,interp2=Agrif_linear,interp3=type_interp_conserv) !       linear in x direction (2nd direction)
   CALL Agrif_Set_bcinterp(uz_id,interp2=Agrif_linear,interp3=type_interp_conserv) ! conservative in y direction (3rd direction)
  !CALL Agrif_Set_UpdateType(uz_id,update2=type_update_u_x,update3=type_update_u_y)

   ! Profil used for Fxeau update
   CALL Agrif_Declare_Variable((/0,1,2/),(/0,1,2/),(/'N','x','y'/),(/1,liminm2,ljminm2/),(/kmax,limaxp2,ljmaxp2/),uz_update_id)
   CALL Agrif_Set_UpdateType(uz_update_id,update2=type_update_u_x,update3=type_update_u_y)
   
   ! Profil used for Hxez update for coarsening only 
   CALL Agrif_Declare_Variable((/0,1,2/),(/0,1,2/),(/'N','x','y'/),(/1,liminm2,ljminm2/),(/kmax,limaxp2,ljmaxp2/),hzex_id)
   CALL Agrif_Set_UpdateType(hzex_id,update2=type_update_u_x,update3=type_update_u_y)

   ! Profil used for V interpolation and boundary conditions
!  CALL Agrif_Declare_Variable((/2,1,0/),(/2,1,0/),(/'x','y','N'/),(/liminm2,ljminm2,1/),(/limaxp2,ljmaxp2,2/),v_id)
   CALL Agrif_Declare_Variable((/2,1,0/),(/2,1,0/),(/'x','y','N'/),(/limin,ljmin,1/),(/limax,ljmax,2/),v_id)
   CALL Agrif_Set_bc(v_id,(/0,1/))
   CALL Agrif_Set_bcinterp(v_id,interp1=type_interp_conserv,interp2=Agrif_linear)  ! convervative in x direction (1st direction)
   CALL Agrif_Set_interp(  v_id,interp1=type_interp_conserv,interp2=Agrif_linear)  !       linear in y direction (2nd direction) 
! NOT USED !   CALL Agrif_Set_UpdateType(v_id,update2=type_update_v_y,update1=type_update_v_x)

! Here profiles are declared from minm1 to maxp1 
! in order to correctly compute the sponge in min and max

   ! Profil used for VZ interpolation and boundary conditions
!  CALL Agrif_Declare_Variable((/0,2,1/),(/0,2,1/),(/'N','x','y'/),(/1,liminm2,ljminm2/),(/kmax,limaxp2,ljmaxp2/),vz_id)
!  CALL Agrif_Declare_Variable((/0,2,1/),(/2,1,0/),(/'N','x','y'/),(/1,liminm1,ljminm1/),(/kmax,limaxp1,ljmaxp1/),vz_id)
   CALL Agrif_Declare_Variable((/0,2,1/),(/0,2,1/),(/'N','x','y'/),(/1,liminm1,ljminm1/),(/kmax,limaxp1,ljmaxp1/),vz_id)
#if defined key_agrif_obc_sponge_uv
   CALL Agrif_Set_bc(vz_id,(/-nb_connect*Agrif_irhox(),1/))
#else
   CALL Agrif_Set_bc(vz_id,(/0,1/))
#endif
   CALL Agrif_Set_interp(  vz_id,interp2=type_interp_conserv,interp3=Agrif_linear) ! convervative in x direction (2nd direction)
   CALL Agrif_Set_bcinterp(vz_id,interp2=type_interp_conserv,interp3=Agrif_linear) !       linear in y direction (3rd direction)
! NOT USED !   CALL Agrif_Set_UpdateType(vz_id,update3=type_update_v_y,update2=type_update_v_x)

   ! Profil used for Fyeau update
   CALL Agrif_Declare_Variable((/0,2,1/),(/0,2,1/),(/'N','x','y'/),(/1,liminm2,ljminm2/),(/kmax,limaxp2,ljmaxp2/),vz_update_id)
   CALL Agrif_Set_UpdateType(vz_update_id,update3=type_update_v_y,update2=type_update_v_x)

   ! Profil used for Hzey update for coarsening only 
   CALL Agrif_Declare_Variable((/0,2,1/),(/0,2,1/),(/'N','x','y'/),(/1,liminm2,ljminm2/),(/kmax,limaxp2,ljmaxp2/),hzey_id)
   CALL Agrif_Set_UpdateType(hzey_id,update3=type_update_v_y,update2=type_update_v_x)

   ! Profil used for Wz corrections (after update of Fxeau & Fyeau) 
   CALL Agrif_Declare_Variable((/0,2,2/),(/0,2,2/),(/'N','x','y'/),(/0,liminm1,ljminm1/),(/kmax,limaxp1,ljmaxp1/),wz_id)
   CALL Agrif_Set_UpdateType(wz_id,update=Agrif_Update_Copy)
   
   ! Profil used for Temp and Sal interpolation over boundary conditions
   CALL Agrif_Declare_Variable((/0,0,2,2/),(/0,0,2,2/),(/'N','N','x','y'/), &
                               (/1,1,liminm1,ljminm1/),(/2,kmax,limaxp2,ljmaxp2/),traceur_id)
#if defined key_agrif_obc_sponge_ts
   CALL Agrif_Set_bc(traceur_id,(/-nb_connect*Agrif_irhox(),0/))
#else
   CALL Agrif_Set_bc(traceur_id,(/0,0/))
#endif
   CALL Agrif_Set_bcinterp(traceur_id,interp=type_interp)
   CALL Agrif_Set_interp(  traceur_id,interp=type_interp)

   ! Profil used for Temp and Sal interpolation of initial condition
   CALL Agrif_Declare_Variable((/0,0,2,2/),(/0,0,2,2/),(/'N','N','x','y'/), &
                               (/1,1,liminm1,ljminm1/),(/2,kmax,limaxp2,ljmaxp2/),traceur_init_id)
   CALL Agrif_Set_bc(traceur_init_id,(/-nb_connect*Agrif_irhox(),0/))
   CALL Agrif_Set_bcinterp(traceur_init_id,interp=type_interp)
   CALL Agrif_Set_interp(  traceur_init_id,interp=type_interp)

   ! Profil used for Temp update
   CALL Agrif_Declare_Variable((/0,2,2/),(/0,2,2/),(/'N','x','y'/), &
                               (/1,liminm1,ljminm1/),(/kmax,limaxp2,ljmaxp2/),temp_id)
! NOT USED !   CALL Agrif_Set_bc(temp_id,(/-nb_connect*Agrif_irhox(),1/))
! NOT USED !   CALL Agrif_Set_bcinterp(temp_id,interp=type_interp)
! NOT USED !   CALL Agrif_Set_interp(  temp_id,interp=type_interp)
   CALL Agrif_Set_UpdateType(temp_id,update=type_update_traceur)

   ! Profil used for Sal update
   CALL Agrif_Declare_Variable((/0,2,2/),(/0,2,2/),(/'N','x','y'/), &
                               (/1,liminm1,ljminm1/),(/kmax,limaxp2,ljmaxp2/),sal_id)
! NOT USED !   CALL Agrif_Set_bc(sal_id,(/-nb_connect*Agrif_irhox(),1/))
! NOT USED !   CALL Agrif_Set_bcinterp(sal_id,interp=type_interp)
! NOT USED !   CALL Agrif_Set_interp(  sal_id,interp=type_interp)
   CALL Agrif_Set_UpdateType(sal_id,update=type_update_traceur)

   ! Profil used for PSI, ECT and EPS interpolation (and update when coarsening)
   CALL Agrif_Declare_Variable((/0,0,2,2/),(/0,0,2,2/),(/'N','N','x','y'/),(/1,1,limin,ljmin/),(/3,kmax,limax,ljmax/),turb_id)
   CALL Agrif_Set_bc(turb_id,(/0,0/))
   CALL Agrif_Set_bcinterp(turb_id,interp=type_interp)
   CALL Agrif_Set_interp(  turb_id,interp=type_interp)
   CALL Agrif_Set_UpdateType(turb_id,update=type_update_traceur)

   ! Profil used for kz interpolation (and update when coarsening)
   CALL Agrif_Declare_Variable((/0,2,2/),(/0,2,2/),(/'N','x','y'/),(/0,liminm1,ljminm1/),(/kmax,limaxp1,ljmaxp1/),kz_id)
   CALL Agrif_Set_bc(kz_id,(/0,0/))
   CALL Agrif_Set_bcinterp(kz_id,interp=type_interp)
   CALL Agrif_Set_interp(  kz_id,interp=type_interp)
   CALL Agrif_Set_UpdateType(kz_id,update=type_update_traceur)

   ! Profil used for nz interpolation (and update when coarsening)
   CALL Agrif_Declare_Variable((/0,2,2/),(/0,2,2/),(/'N','x','y'/),(/0,limin,ljmin/),(/kmax,limaxp1,ljmaxp1/),nz_id)
   CALL Agrif_Set_bc(nz_id,(/0,0/))
   CALL Agrif_Set_bcinterp(nz_id,interp=type_interp)
   CALL Agrif_Set_interp(  nz_id,interp=type_interp)
   CALL Agrif_Set_UpdateType(nz_id,update=type_update_traceur)

#ifdef key_substance
   ! Profil used for cv_wat interpolation and update
   CALL Agrif_Declare_Variable((/0,0,2,2/),(/0,0,2,2/),(/'N','N','x','y'/), &
                               (/1,1,liminm1,ljminm1/),(/nb_var,kmax,limaxp2,ljmaxp2/),cv_wat_id)
   CALL Agrif_Set_bc(cv_wat_id,(/0,0/))
   CALL Agrif_Set_bcinterp(cv_wat_id,interp=type_interp)
   CALL Agrif_Set_interp(  cv_wat_id,interp=type_interp)
   CALL Agrif_Set_UpdateType(cv_wat_id,update=type_update_traceur)

   ! Profil used for cv_wat interpolation of initial conditions
   CALL Agrif_Declare_Variable((/0,0,2,2/),(/0,0,2,2/),(/'N','N','x','y'/), &
                               (/1,1,liminm1,ljminm1/),(/nb_var,kmax,limaxp2,ljmaxp2/),cv_wat_init_id)
   CALL Agrif_Set_bc(cv_wat_init_id,(/0,0/))
   CALL Agrif_Set_bcinterp(cv_wat_init_id,interp=type_interp)
   CALL Agrif_Set_interp(  cv_wat_init_id,interp=type_interp)
#endif

#endif
  END SUBROUTINE agrif_declare_variables

