      subroutine t3dpremix (tile)
      implicit none
      integer*4 tile, trd, omp_get_thread_num
      integer*4  LLm,Lm,MMm,Mm,N, LLm0,MMm0
      integer*4 LLmm2, MMmm2
      parameter (LLm0=41,   MMm0=42,   N=32)
      common /scrum_physical_grid/ LLm,Lm,LLmm2,MMm,Mm,MMmm2
      integer*4 N_sl
      parameter (N_sl=0)
      integer*4 Lmmpi,Mmmpi,iminmpi,imaxmpi,jminmpi,jmaxmpi
      common /comm_setup_mpi1/ Lmmpi,Mmmpi
      common /comm_setup_mpi2/ iminmpi,imaxmpi,jminmpi,jmaxmpi
      integer*4 NSUB_X, NSUB_E, NPP
      integer*4 NP_XI, NP_ETA, NNODES
      parameter (NP_XI=1,  NP_ETA=4,  NNODES=NP_XI*NP_ETA)
      parameter (NPP=1)
      parameter (NSUB_X=1, NSUB_E=1)
      integer*4 NWEIGHT
      parameter (NWEIGHT=1000)
      integer*4 Ntides
      parameter (Ntides=8)
      integer*4 stdout, Np, NpHz, padd_X,padd_E
      common/scrum_deriv_param/padd_X,padd_E
      parameter (stdout=6)
      parameter (Np=N+1)
      parameter (NpHz=(N+1+N_sl))
      integer*4 NSA, N2d,N3d,N3dHz,N1dXI,N1dETA
      parameter (NSA=28)
      common /scrum_private_param/ N2d,N3d,N3dHz,N1dXI,N1dETA
      real Vtransform
      parameter (Vtransform=2)
      integer*4   NT, NTA, itemp, NTot
      integer*4   ntrc_temp, ntrc_salt, ntrc_pas, ntrc_bio, ntrc_sed
      integer*4   ntrc_subs, ntrc_substot
      parameter (itemp=1)
      parameter (ntrc_temp=1)
      parameter (ntrc_salt=1)
      parameter (ntrc_pas=0)
      parameter (ntrc_bio=0)
      parameter (ntrc_subs=0, ntrc_substot=0)
      parameter (ntrc_sed=0)
      parameter (NTA=itemp+ntrc_salt)
      parameter (NT=itemp+ntrc_salt+ntrc_pas+ntrc_bio+ntrc_sed)
      parameter (NTot=NT)
      integer*4   ntrc_diats, ntrc_diauv, ntrc_diabio
      integer*4   ntrc_diavrt, ntrc_diaek, ntrc_diapv
      integer*4   ntrc_diaeddy, ntrc_surf
     &          , isalt
      parameter (isalt=itemp+1)
      parameter (ntrc_diabio=0)
      parameter (ntrc_diats=0)
      parameter (ntrc_diauv=0)
      parameter (ntrc_diavrt=0)
      parameter (ntrc_diaek=0)
      parameter (ntrc_diapv=0)
      parameter (ntrc_diaeddy=0)
      parameter (ntrc_surf=0)
      real A2d(N2d,NSA,0:NPP-1), A3d(N3d,9,0:NPP-1)
     &    ,A3dHz(N3dHz,4,0:NPP-1)
      integer*4 B2d(N2d,0:NPP-1)
      common/private_scratch/ A2d,A3d,A3dHz
      common/private_scratch_bis/ B2d
      integer*4 chunk_size_X,margin_X,chunk_size_E,margin_E
      integer*4 Istr,Iend,Jstr,Jend, i_X,j_E
      chunk_size_X=(Lmmpi+NSUB_X-1)/NSUB_X
      margin_X=(NSUB_X*chunk_size_X-Lmmpi)/2
      chunk_size_E=(Mmmpi+NSUB_E-1)/NSUB_E
      margin_E=(NSUB_E*chunk_size_E-Mmmpi)/2
      j_E=tile/NSUB_X
      i_X=tile-j_E*NSUB_X
      Istr=1+i_X*chunk_size_X-margin_X
      Iend=Istr+chunk_size_X-1
      Istr=max(Istr,1)
      Iend=min(Iend,Lmmpi)
      Jstr=1+j_E*chunk_size_E-margin_E
      Jend=Jstr+chunk_size_E-1
      Jstr=max(Jstr,1)
      Jend=min(Jend,Mmmpi)
      trd=omp_get_thread_num()
        call t3dpremix_tile (Istr,Iend,Jstr,Jend)
      return
      end
      subroutine t3dpremix_tile (Istr,Iend,Jstr,Jend)
      implicit none
      integer*4  LLm,Lm,MMm,Mm,N, LLm0,MMm0
      integer*4 LLmm2, MMmm2
      parameter (LLm0=41,   MMm0=42,   N=32)
      common /scrum_physical_grid/ LLm,Lm,LLmm2,MMm,Mm,MMmm2
      integer*4 N_sl
      parameter (N_sl=0)
      integer*4 Lmmpi,Mmmpi,iminmpi,imaxmpi,jminmpi,jmaxmpi
      common /comm_setup_mpi1/ Lmmpi,Mmmpi
      common /comm_setup_mpi2/ iminmpi,imaxmpi,jminmpi,jmaxmpi
      integer*4 NSUB_X, NSUB_E, NPP
      integer*4 NP_XI, NP_ETA, NNODES
      parameter (NP_XI=1,  NP_ETA=4,  NNODES=NP_XI*NP_ETA)
      parameter (NPP=1)
      parameter (NSUB_X=1, NSUB_E=1)
      integer*4 NWEIGHT
      parameter (NWEIGHT=1000)
      integer*4 Ntides
      parameter (Ntides=8)
      integer*4 stdout, Np, NpHz, padd_X,padd_E
      common/scrum_deriv_param/padd_X,padd_E
      parameter (stdout=6)
      parameter (Np=N+1)
      parameter (NpHz=(N+1+N_sl))
      integer*4 NSA, N2d,N3d,N3dHz,N1dXI,N1dETA
      parameter (NSA=28)
      common /scrum_private_param/ N2d,N3d,N3dHz,N1dXI,N1dETA
      real Vtransform
      parameter (Vtransform=2)
      integer*4   NT, NTA, itemp, NTot
      integer*4   ntrc_temp, ntrc_salt, ntrc_pas, ntrc_bio, ntrc_sed
      integer*4   ntrc_subs, ntrc_substot
      parameter (itemp=1)
      parameter (ntrc_temp=1)
      parameter (ntrc_salt=1)
      parameter (ntrc_pas=0)
      parameter (ntrc_bio=0)
      parameter (ntrc_subs=0, ntrc_substot=0)
      parameter (ntrc_sed=0)
      parameter (NTA=itemp+ntrc_salt)
      parameter (NT=itemp+ntrc_salt+ntrc_pas+ntrc_bio+ntrc_sed)
      parameter (NTot=NT)
      integer*4   ntrc_diats, ntrc_diauv, ntrc_diabio
      integer*4   ntrc_diavrt, ntrc_diaek, ntrc_diapv
      integer*4   ntrc_diaeddy, ntrc_surf
     &          , isalt
      parameter (isalt=itemp+1)
      parameter (ntrc_diabio=0)
      parameter (ntrc_diats=0)
      parameter (ntrc_diauv=0)
      parameter (ntrc_diavrt=0)
      parameter (ntrc_diaek=0)
      parameter (ntrc_diapv=0)
      parameter (ntrc_diaeddy=0)
      parameter (ntrc_surf=0)
      real h(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real hinv(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real f(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real fomn(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /grid_h/h /grid_hinv/hinv /grid_f/f /grid_fomn/fomn
      real angler(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /grid_angler/angler
      real latr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real lonr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real latu(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real lonu(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real latv(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real lonv(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /grid_latr/latr /grid_lonr/lonr
      common /grid_latu/latu /grid_lonu/lonu
      common /grid_latv/latv /grid_lonv/lonv
      real pm(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pn(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real om_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real on_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real om_u(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real on_u(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real om_v(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real on_v(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real om_p(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real on_p(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pn_u(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pm_v(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pm_u(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pn_v(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /metrics_pm/pm    /metrics_pn/pn
      common /metrics_omr/om_r /metrics_on_r/on_r
      common /metrics_omu/om_u /metrics_on_u/on_u
      common /metrics_omv/om_v /metrics_on_v/on_v
      common /metrics_omp/om_p /metrics_on_p/on_p
      common /metrics_pnu/pn_u /metrics_pmv/pm_v
      common /metrics_pmu/pm_u /metrics_pnv/pn_v
      real dmde(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real dndx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /metrics_dmde/dmde    /metrics_dndx/dndx
      real pmon_p(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pmon_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pmon_u(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pnom_p(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pnom_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pnom_v(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real grdscl(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /metrics_pmon_p/pmon_p /metrics_pnom_p/pnom_p
      common /metrics_pmon_r/pmon_r /metrics_pnom_r/pnom_r
      common /metrics_pmon_u/pmon_u /metrics_pnom_v/pnom_v
      common /metrics_grdscl/grdscl
      real rmask(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pmask(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real umask(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real vmask(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real pmask2(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /mask_r/rmask
      common /mask_p/pmask
      common /mask_u/umask
      common /mask_v/vmask
      common /mask_p2/pmask2
      real zob(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /Z0B_VAR/zob
      real u(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,3)
      real v(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,3)
      real t(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,3,NT)
      common /ocean_u/u /ocean_v/v /ocean_t/t
      real Hz(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real Hz_bak(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real z_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real z_w(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      real Huon(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real Hvom(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /grid_Hz_bak/Hz_bak /grid_zw/z_w /grid_Huon/Huon
      common /grid_Hvom/Hvom
      real We(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      common /grid_Hz/Hz /grid_zr/z_r /grid_We/We
      real rho1(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real rho(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /ocean_rho1/rho1 /ocean_rho/rho
      real qp1(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /ocean_qp1/qp1
      real qp2
      parameter (qp2=0.0000172D0)
      real visc2_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real visc2_p(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real visc2_sponge_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real visc2_sponge_p(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /mixing_visc2_r/visc2_r /mixing_visc2_p/visc2_p
      common /mixing_visc2_sponge_r/visc2_sponge_r
      common /mixing_visc2_sponge_p/visc2_sponge_p
      real diff2_sponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real diff2(-1:Lm+2+padd_X,-1:Mm+2+padd_E,NT)
      common /mixing_diff2_sponge/diff2_sponge
      common /mixing_diff2/diff2
      real diff4_sponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real diff4(-1:Lm+2+padd_X,-1:Mm+2+padd_E,NT)
      common /mixing_diff4_sponge/diff4_sponge
      common /mixing_diff4/diff4
      real diff3d_u(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real diff3d_v(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /mixing_diff3d_u/diff3d_u
      common /mixing_diff3d_v/diff3d_v
      real dRdx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real dRde(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real idRz(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      common /mixing_dRdx/dRdx
      common /mixing_dRde/dRde
      common /mixing_idRz/idRz
      real Rslope_max,Gslope_max
      parameter (Gslope_max=1.D0, Rslope_max=0.05D0)
      integer*4 ismooth
      real csmooth
      common /mixing_csmooth/ csmooth
      common /mixing_ismooth/ ismooth
      real Akv(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      real Akt(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N,2)
      common /mixing_Akv/Akv /mixing_Akt/Akt
      real bvf(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      common /mixing_bvf/ bvf
      real ustar(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /lmd_kpp_ustar/ustar
      integer*4 kbl(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      integer*4 kbbl(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real hbbl(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /lmd_kpp_kbl/ kbl
      common /lmd_kpp_hbbl/ hbbl
      common /lmd_kpp_kbbl/ kbbl
      real hbls(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /lmd_kpp_hbl/ hbls
      real ghats(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      common /lmd_kpp_ghats/ghats
      real ssh(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /climat_ssh/ssh
      real dt, dtfast, time, time2, time_start, tdays, start_time
      integer*4 ndtfast, iic, kstp, krhs, knew, next_kstp
     &      , iif, nstp, nrhs, nnew, nbstep3d
      logical PREDICTOR_2D_STEP
      common /time_indices/  dt,dtfast, time, time2,time_start, tdays,
     &     ndtfast, iic, kstp, krhs, knew, next_kstp,
     &     start_time,
     &                       iif, nstp, nrhs, nnew, nbstep3d,
     &                       PREDICTOR_2D_STEP
      real time_avg, time2_avg, rho0
     &               , rdrg, rdrg2, Cdb_min, Cdb_max, Zobt
     &               , xl, el, visc2, visc4, gamma2
      real  theta_s,   theta_b,   Tcline,  hc
      real  sc_w(0:N), Cs_w(0:N), sc_r(N), Cs_r(N)
      real  rx0, rx1
      real  tnu2(NT),tnu4(NT)
      real weight(6,0:NWEIGHT)
      real  x_sponge,   v_sponge
       real  tauT_in, tauT_out, tauM_in, tauM_out
      integer*4 numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
      logical ldefhis
      logical got_tini(NT)
      common /scalars_main/
     &             time_avg, time2_avg,  rho0,      rdrg,    rdrg2
     &           , Zobt,       Cdb_min,   Cdb_max
     &           , xl, el,    visc2,     visc4,   gamma2
     &           , theta_s,   theta_b,   Tcline,  hc
     &           , sc_w,      Cs_w,      sc_r,    Cs_r
     &           , rx0,       rx1
     &           ,       tnu2,    tnu4
     &                      , weight
     &                      , x_sponge,   v_sponge
     &                      , tauT_in, tauT_out, tauM_in, tauM_out
     &      , numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
     &                      , got_tini
     &                      , ldefhis
      logical synchro_flag
      common /sync_flag/ synchro_flag
      integer*4 may_day_flag
      integer*4 tile_count, first_time, bc_count
      common /communicators_i/
     &        may_day_flag, tile_count, first_time, bc_count
      real hmin, hmax, grdmin, grdmax, Cu_min, Cu_max
      common /communicators_r/
     &     hmin, hmax, grdmin, grdmax, Cu_min, Cu_max
      real lonmin, lonmax, latmin, latmax
      common /communicators_lonlat/
     &     lonmin, lonmax, latmin, latmax
      real*8 Cu_Adv3d,  Cu_W, Cu_Nbq_X, Cu_Nbq_Y, Cu_Nbq_Z
      integer*4 i_cx_max, j_cx_max, k_cx_max
      common /diag_vars/ Cu_Adv3d,  Cu_W,
     &        i_cx_max, j_cx_max, k_cx_max
      real*8 volume, avgke, avgpe, avgkp, bc_crss
      common /communicators_rq/
     &          volume, avgke, avgpe, avgkp, bc_crss
      real*4 CPU_time(0:31,0:NPP)
      integer*4 proc(0:31,0:NPP),trd_count
      common /timers_roms/CPU_time,proc,trd_count
      logical EAST_INTER2, WEST_INTER2, NORTH_INTER2, SOUTH_INTER2
      logical EAST_INTER, WEST_INTER, NORTH_INTER, SOUTH_INTER
      logical CORNER_SW,CORNER_NW,CORNER_NE,CORNER_SE
      integer*4 mynode, mynode2, ii,jj, p_W,p_E,p_S,p_N, p_SW,p_SE,
     & p_NW,p_NE,NNODES2
      common /comm_setup/ mynode, mynode2, ii,jj, p_W,p_E,p_S,p_N,
     & p_SW,p_SE, p_NW,p_NE, EAST_INTER, WEST_INTER, NORTH_INTER,
     & SOUTH_INTER, EAST_INTER2, WEST_INTER2, NORTH_INTER2, 
     &                                                     SOUTH_INTER2,
     & CORNER_SW,CORNER_NW,CORNER_NE,CORNER_SE,NNODES2
      real pi, deg2rad, rad2deg
      parameter (pi=3.14159265358979323846D0, deg2rad=pi/180.D0,
     &                                      rad2deg=180.D0/pi)
      real Eradius, Erotation, g, day2sec,sec2day, jul_off,
     &     year2day,day2year
      parameter (Eradius=6371315.0D0,  Erotation=7.292115090D-5,
     &           day2sec=86400.D0, sec2day=1.D0/86400.D0,
     &           year2day=365.25D0, day2year=1.D0/365.25D0,
     &           jul_off=2440000.D0)
      parameter (g=9.81D0)
      real Cp
      parameter (Cp=3985.0D0)
      real vonKar
      parameter (vonKar=0.41D0)
      real spval
      parameter (spval=-999.0D0)
      logical mask_val
      parameter (mask_val = .true.)
      real T_west(0:1,-1:Mm+2+padd_E,N,4,NT)
      common/zoom3D_TW/T_west
      real U_west(1:1,-1:Mm+2+padd_E,N,4)
      common/zoom3D_UW/U_west
      real V_west(0:0,-1:Mm+2+padd_E,N,4)
      common/zoom3D_VW/V_west
      real T_east(Lmmpi:Lmmpi+1,-1:Mm+2+padd_E,N,4,NT)
      common/zoom3D_TE/T_east
      real U_east(Lmmpi+1:Lmmpi+1,-1:Mm+2+padd_E,N,4)
      common/zoom3D_UE/U_east
      real V_east(Lmmpi+1:Lmmpi+1,-1:Mm+2+padd_E,N,4)
      common/zoom3D_VE/V_east
      real T_south(-1:Lm+2+padd_X,0:1,N,4,NT)
      common/zoom3D_TS/T_south
      real U_south(-1:Lm+2+padd_X,0:0,N,4)
      common/zoom3D_US/U_south
      real V_south(-1:Lm+2+padd_X,1:1,N,4)
      common/zoom3D_VS/V_south
      real T_north(-1:Lm+2+padd_X,Mmmpi:Mmmpi+1,N,4,NT)
      common/zoom3D_TN/T_north
      real U_north(-1:Lm+2+padd_X,Mmmpi+1:Mmmpi+1,N,4)
      common/zoom3D_UN/U_north
      real V_north(-1:Lm+2+padd_X,Mmmpi+1:Mmmpi+1,N,4)
      common/zoom3D_VN/V_north
      integer*4 Zetatimeindex, Zetatimeindex2
      common/zoom2D_ZetaT/Zetatimeindex, Zetatimeindex2
      integer*4 U2DTimeindex, U2DTimeindex2
      common/zoom2D_UT/U2DTimeindex, U2DTimeindex2
      integer*4 V2DTimeindex, V2DTimeindex2
      common/zoom2D_VT/V2DTimeindex, V2DTimeindex2
      integer*4 Ttimeindex
      common/zoom3D_TT/Ttimeindex
      integer*4 Utimeindex
      common/zoom3D_UT/Utimeindex
      integer*4 Vtimeindex
      common/zoom3D_VT/Vtimeindex
      real weight2(0:NWEIGHT,0:NWEIGHT)
      common/weighting/weight2
      real updateTprof(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
      common/updateTprofile/updateTprof
      integer*4 indupdate
      real myvalues(3*7*(2*(Lm+3)+2*(Mm+3)),0:NWEIGHT)
      common/updatevalues/myvalues,indupdate
      integer*4 nbcoarse
      common/nestingmanag/nbcoarse
      real myfx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
      real myfy(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
      common/myfluxes/myfx,myfy
      real Zt_avg2(-1:Lm+2+padd_X,-1:Mm+2+padd_E,4)
      common/averagebaro/Zt_avg2
      real dZtinterp(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real dUinterp(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real dVinterp(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common/zoombc2D/dZtinterp,dUinterp,dVinterp
       logical Alreadyupdated(-1:Lm+2+padd_X,-1:Mm+2+padd_E,3)
       common/updateprestep/Alreadyupdated
       real usponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       real vsponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       real wsponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
       real tsponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
       common/sponge_com/usponge, vsponge, tsponge,wsponge
       real Huonagrif(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       real Hvomagrif(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       common/huvagrif/Huonagrif,Hvomagrif
      real Zt_avg3(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:NWEIGHT)
      common/zoom2D_Zeta2/Zt_avg3
      real DU_avg3(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:NWEIGHT)
      common/zoom2D_U2/DU_avg3
      real DV_avg3(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:NWEIGHT)
      common/zoom2D_V2/DV_avg3
      real T_sponge_west(0:10,-1:Mm+2+padd_E,N,2,NT)
      common/zoom3D_sponge_TW/T_sponge_west
      real U_sponge_west(1:11,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_UW/U_sponge_west
      real V_sponge_west(0:10,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_VW/V_sponge_west
      real W_sponge_west(0:10,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_WW/W_sponge_west
      real T_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2,NT)
      common/zoom3D_sponge_TE/T_sponge_east
      real U_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_UE/U_sponge_east
      real V_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_VE/V_sponge_east
      real W_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_WE/W_sponge_east
      real T_sponge_south(-1:Lm+2+padd_X,0:10,N,2,NT)
      common/zoom3D_sponge_TS/T_sponge_south
      real U_sponge_south(-1:Lm+2+padd_X,0:10,N,2)
      common/zoom3D_sponge_US/U_sponge_south
      real V_sponge_south(-1:Lm+2+padd_X,1:11,N,2)
      common/zoom3D_sponge_VS/V_sponge_south
      real W_sponge_south(-1:Lm+2+padd_X,0:10,N,2)
      common/zoom3D_sponge_WS/W_sponge_south
      real T_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2,NT)
      common/zoom3D_sponge_TN/T_sponge_north
      real U_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2)
      common/zoom3D_sponge_UN/U_sponge_north
      real V_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2)
      common/zoom3D_sponge_VN/V_sponge_north
      real W_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2)
      common/zoom3D_sponge_WN/W_sponge_north
      integer*4 TTimesponge, UVTimesponge
      common/zoom3D_sponge_times/TTimesponge, UVTimesponge
      real A1dXI(-1:Lm+2+padd_X,10*NWEIGHT)
      real A1dETA(-1:Mm+2+padd_E,10*NWEIGHT)
      common/work_agrif/A1dXI,A1dETA
      integer*4 TspongeTimeindex, TspongeTimeindex2
      integer*4 UVspongeTimeindex, UVspongeTimeindex2
      common/zoom3D_sponge/TspongeTimeindex, UVspongeTimeindex,
     &      TspongeTimeindex2, UVspongeTimeindex2
      real,dimension(:,:),allocatable :: finevalues
      real,dimension(:,:),allocatable :: coarsevalues
      common/gridinter/finevalues,coarsevalues
      real,dimension(:,:),allocatable :: coarsevaluesinterp
      common/gridinter2/coarsevaluesinterp
      integer*4 j1t,i1t,i2t,j2t
      integer*4 i1u,i2u,j1v,j2v
      integer*4 common_index
      common/arraysindices/i1t,j1t,i2t,j2t,
     &     i1u,i2u,j1v,j2v,common_index
      integer*4 hid, zetaid,ubarid,vbarid,uid,vid,tid
      integer*4 rmaskid
      integer*4 tspongeid, uspongeid, vspongeid,wspongeid
      common/varids/hid,zetaid,ubarid,vbarid,uid,vid,tid,
     &  tspongeid, uspongeid, vspongeid, rmaskid,wspongeid
      integer*4 updatezetaid, updateubarid, updatevbarid
      integer*4 updateduavg2id, updatedvavg2id
      integer*4 updatetid, updateuid, updatevid
      integer*4 updatemyfxid, updatemyfyid
      integer*4 updatehuonid, updatehvomid
      common/varidsupdate/updatezetaid, updateubarid, updatevbarid,
     &       updateduavg2id, updatedvavg2id,
     &       updatetid, updateuid, updatevid, updatemyfxid,
     &       updatemyfyid,updatehuonid, updatehvomid
!$AGRIF_DO_NOT_TREAT
      integer*4 :: iind
      integer*4 :: sortedint(0:10000)
      integer*4 :: whichstep(0:10000)
      integer*4 :: grids_at_level(0:20,0:100)
      integer*4 :: parent_grid(0:20)
      integer*4 :: coeff_ref_time(0:20)
      integer*4 :: nbtimes, nbmaxtimes
      common/rootintegrate/nbtimes, nbmaxtimes,
     &    iind,sortedint,whichstep,
     &    grids_at_level,parent_grid,coeff_ref_time
!$AGRIF_END_DO_NOT_TREAT
      integer*4 itrc, Istr,Iend,Jstr,Jend, i,j,k
       integer*4 decal
       real maxdiff
       real tinterp,onemtinterp, rrhot
       integer*4 :: nold
       integer*4 :: irhot
       external interpsponget
       integer*4 :: parentnbstep
      integer*4 IstrR,IendR,JstrR,JendR
      integer*4 IstrU
      integer*4 JstrV
      if ((istr.eq.1 .and. .not.WEST_INTER)) then
        IstrR=Istr-1
        IstrU=Istr+1
      else
        IstrR=Istr
        IstrU=Istr
      endif
      if ((iend.eq.Lmmpi .and. .not.EAST_INTER)) then
        IendR=Iend+1
      else
        IendR=Iend
      endif
      if ((jstr.eq.1.and. .not.SOUTH_INTER)) then
        JstrR=Jstr-1
        JstrV=Jstr+1
      else
        JstrR=Jstr
        JstrV=Jstr
      endif
      if ((jend.eq.Mmmpi .and. .not.NORTH_INTER)) then
        JendR=Jend+1
      else
        JendR=Jend
      endif
      irhot = Agrif_Irhot()
      rrhot = real(irhot)
      decal = 2*max(Agrif_Irhox(),Agrif_Irhoy())
      parentnbstep=Agrif_Parent_Nb_Step()
C$OMP BARRIER
C$OMP MASTER
      If ((nbcoarse == 1).AND.(TspongeTimeindex .NE. parentnbstep)) THEN
        Call Agrif_Set_bc(tspongeid,(/-decal,0/),
     &     InterpolationShouldbemade=.TRUE.)
        Agrif_UseSpecialvalue=.true.
        Agrif_Specialvalue=0.D0
        tinterp = 1.D0
        Call Agrif_Bc_Variable(tspongeid,calledweight=tinterp,
     &                              procname=interpsponget)
        Agrif_UseSpecialvalue=.false.
        TTimesponge = 3 - TTimesponge
        TspongeTimeindex = parentnbstep
        TspongeTimeindex2 = agrif_nb_step()
      ENDIF
C$OMP END MASTER
C$OMP BARRIER
      if (agrif_nb_step() .EQ. TspongeTimeindex2) then
        if ((jstr.eq.1.and. .not.SOUTH_INTER)) then
          do itrc=1,NT
            do k=1,N
              do j=JstrR,JstrR+decal
                do i=IstrR,IendR
                  T_sponge_south(i,j,k,TTimesponge,itrc)=
     &                                tsponge(i,j,k,itrc)
                enddo
              enddo
            enddo
          enddo
        endif
        if ((jend.eq.Mmmpi .and. .not.NORTH_INTER)) then
          do itrc=1,NT
            do k=1,N
              do j=JendR-decal,JendR
                do i=IstrR,IendR
                  T_sponge_north(i,j,k,TTimesponge,itrc)=
     &                                tsponge(i,j,k,itrc)
                enddo
              enddo
            enddo
          enddo
        endif
        if ((istr.eq.1 .and. .not.WEST_INTER)) then
          do itrc=1,NT
            do k=1,N
              do j=JstrR,JendR
                do i=IstrR,IstrR+decal
                  T_sponge_west(i,j,k,TTimesponge,itrc)=
     &                               tsponge(i,j,k,itrc)
                enddo
              enddo
            enddo
          enddo
        endif
        if ((iend.eq.Lmmpi .and. .not.EAST_INTER)) then
          do itrc=1,NT
            do k=1,N
              do j=JstrR,JendR
                do i=IendR-decal,IendR
                  T_sponge_east(i,j,k,TTimesponge,itrc)=
     &                               tsponge(i,j,k,itrc)
                enddo
              enddo
            enddo
          enddo
        endif
      ENDIF
      tinterp = 0.5D0+(real(nbcoarse)-0.5D0)/rrhot
      IF (nbstep3d .LT. irhot) tinterp = 1.D0
      onemtinterp = 1.D0-tinterp
      nold = 3 - TTimesponge
      if ((jstr.eq.1.and. .not.SOUTH_INTER)) then
        do itrc=1,NT
          do k=1,N
            do j=JstrR,JstrR+decal
              do i=IstrR,IendR
                tsponge(i,j,k,itrc) =
     &                   onemtinterp*T_sponge_south(i,j,k,nold,itrc)
     &               +tinterp*T_sponge_south(i,j,k,TTimesponge,itrc)
              enddo
            enddo
          enddo
        enddo
      endif
      if ((jend.eq.Mmmpi .and. .not.NORTH_INTER)) then
        do itrc=1,NT
          do k=1,N
            do j=JendR-decal,JendR
              do i=IstrR,IendR
                tsponge(i,j,k,itrc) =
     &                  onemtinterp*T_sponge_north(i,j,k,nold,itrc)
     &              +tinterp*T_sponge_north(i,j,k,TTimesponge,itrc)
              enddo
            enddo
          enddo
        enddo
      endif
      if ((istr.eq.1 .and. .not.WEST_INTER)) then
        do itrc=1,NT
          do k=1,N
            do j=JstrR,JendR
              do i=IstrR,IstrR+decal
                tsponge(i,j,k,itrc) =
     &                    onemtinterp*T_sponge_west(i,j,k,nold,itrc)
     &               + tinterp*T_sponge_west(i,j,k,TTimesponge,itrc)
              enddo
            enddo
          enddo
        enddo
      endif
      if ((iend.eq.Lmmpi .and. .not.EAST_INTER)) then
        do itrc=1,NT
          do k=1,N
            do j=JstrR,JendR
              do i=IendR-decal,IendR
                tsponge(i,j,k,itrc)=
     &                    onemtinterp*T_sponge_east(i,j,k,nold,itrc)
     &               + tinterp*T_sponge_east(i,j,k,TTimesponge,itrc)
              enddo
            enddo
          enddo
        enddo
      endif
      do itrc=1,NT
        call exchange_r3d_tile (Istr,Iend,Jstr,Jend,
     &                          tsponge(-1,-1,1,itrc))
      enddo
      return
      end
      subroutine interpsponget(tabres,i1,i2,j1,j2,k1,k2,m1,m2,before)
      implicit none
      integer*4  LLm,Lm,MMm,Mm,N, LLm0,MMm0
      integer*4 LLmm2, MMmm2
      parameter (LLm0=41,   MMm0=42,   N=32)
      common /scrum_physical_grid/ LLm,Lm,LLmm2,MMm,Mm,MMmm2
      integer*4 N_sl
      parameter (N_sl=0)
      integer*4 Lmmpi,Mmmpi,iminmpi,imaxmpi,jminmpi,jmaxmpi
      common /comm_setup_mpi1/ Lmmpi,Mmmpi
      common /comm_setup_mpi2/ iminmpi,imaxmpi,jminmpi,jmaxmpi
      integer*4 NSUB_X, NSUB_E, NPP
      integer*4 NP_XI, NP_ETA, NNODES
      parameter (NP_XI=1,  NP_ETA=4,  NNODES=NP_XI*NP_ETA)
      parameter (NPP=1)
      parameter (NSUB_X=1, NSUB_E=1)
      integer*4 NWEIGHT
      parameter (NWEIGHT=1000)
      integer*4 Ntides
      parameter (Ntides=8)
      integer*4 stdout, Np, NpHz, padd_X,padd_E
      common/scrum_deriv_param/padd_X,padd_E
      parameter (stdout=6)
      parameter (Np=N+1)
      parameter (NpHz=(N+1+N_sl))
      integer*4 NSA, N2d,N3d,N3dHz,N1dXI,N1dETA
      parameter (NSA=28)
      common /scrum_private_param/ N2d,N3d,N3dHz,N1dXI,N1dETA
      real Vtransform
      parameter (Vtransform=2)
      integer*4   NT, NTA, itemp, NTot
      integer*4   ntrc_temp, ntrc_salt, ntrc_pas, ntrc_bio, ntrc_sed
      integer*4   ntrc_subs, ntrc_substot
      parameter (itemp=1)
      parameter (ntrc_temp=1)
      parameter (ntrc_salt=1)
      parameter (ntrc_pas=0)
      parameter (ntrc_bio=0)
      parameter (ntrc_subs=0, ntrc_substot=0)
      parameter (ntrc_sed=0)
      parameter (NTA=itemp+ntrc_salt)
      parameter (NT=itemp+ntrc_salt+ntrc_pas+ntrc_bio+ntrc_sed)
      parameter (NTot=NT)
      integer*4   ntrc_diats, ntrc_diauv, ntrc_diabio
      integer*4   ntrc_diavrt, ntrc_diaek, ntrc_diapv
      integer*4   ntrc_diaeddy, ntrc_surf
     &          , isalt
      parameter (isalt=itemp+1)
      parameter (ntrc_diabio=0)
      parameter (ntrc_diats=0)
      parameter (ntrc_diauv=0)
      parameter (ntrc_diavrt=0)
      parameter (ntrc_diaek=0)
      parameter (ntrc_diapv=0)
      parameter (ntrc_diaeddy=0)
      parameter (ntrc_surf=0)
      real u(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,3)
      real v(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,3)
      real t(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,3,NT)
      common /ocean_u/u /ocean_v/v /ocean_t/t
      real Hz(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real Hz_bak(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real z_r(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real z_w(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      real Huon(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real Hvom(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /grid_Hz_bak/Hz_bak /grid_zw/z_w /grid_Huon/Huon
      common /grid_Hvom/Hvom
      real We(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      common /grid_Hz/Hz /grid_zr/z_r /grid_We/We
      real rho1(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real rho(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /ocean_rho1/rho1 /ocean_rho/rho
      real qp1(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /ocean_qp1/qp1
      real qp2
      parameter (qp2=0.0000172D0)
      real dt, dtfast, time, time2, time_start, tdays, start_time
      integer*4 ndtfast, iic, kstp, krhs, knew, next_kstp
     &      , iif, nstp, nrhs, nnew, nbstep3d
      logical PREDICTOR_2D_STEP
      common /time_indices/  dt,dtfast, time, time2,time_start, tdays,
     &     ndtfast, iic, kstp, krhs, knew, next_kstp,
     &     start_time,
     &                       iif, nstp, nrhs, nnew, nbstep3d,
     &                       PREDICTOR_2D_STEP
      real time_avg, time2_avg, rho0
     &               , rdrg, rdrg2, Cdb_min, Cdb_max, Zobt
     &               , xl, el, visc2, visc4, gamma2
      real  theta_s,   theta_b,   Tcline,  hc
      real  sc_w(0:N), Cs_w(0:N), sc_r(N), Cs_r(N)
      real  rx0, rx1
      real  tnu2(NT),tnu4(NT)
      real weight(6,0:NWEIGHT)
      real  x_sponge,   v_sponge
       real  tauT_in, tauT_out, tauM_in, tauM_out
      integer*4 numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
      logical ldefhis
      logical got_tini(NT)
      common /scalars_main/
     &             time_avg, time2_avg,  rho0,      rdrg,    rdrg2
     &           , Zobt,       Cdb_min,   Cdb_max
     &           , xl, el,    visc2,     visc4,   gamma2
     &           , theta_s,   theta_b,   Tcline,  hc
     &           , sc_w,      Cs_w,      sc_r,    Cs_r
     &           , rx0,       rx1
     &           ,       tnu2,    tnu4
     &                      , weight
     &                      , x_sponge,   v_sponge
     &                      , tauT_in, tauT_out, tauM_in, tauM_out
     &      , numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
     &                      , got_tini
     &                      , ldefhis
      logical synchro_flag
      common /sync_flag/ synchro_flag
      integer*4 may_day_flag
      integer*4 tile_count, first_time, bc_count
      common /communicators_i/
     &        may_day_flag, tile_count, first_time, bc_count
      real hmin, hmax, grdmin, grdmax, Cu_min, Cu_max
      common /communicators_r/
     &     hmin, hmax, grdmin, grdmax, Cu_min, Cu_max
      real lonmin, lonmax, latmin, latmax
      common /communicators_lonlat/
     &     lonmin, lonmax, latmin, latmax
      real*8 Cu_Adv3d,  Cu_W, Cu_Nbq_X, Cu_Nbq_Y, Cu_Nbq_Z
      integer*4 i_cx_max, j_cx_max, k_cx_max
      common /diag_vars/ Cu_Adv3d,  Cu_W,
     &        i_cx_max, j_cx_max, k_cx_max
      real*8 volume, avgke, avgpe, avgkp, bc_crss
      common /communicators_rq/
     &          volume, avgke, avgpe, avgkp, bc_crss
      real*4 CPU_time(0:31,0:NPP)
      integer*4 proc(0:31,0:NPP),trd_count
      common /timers_roms/CPU_time,proc,trd_count
      logical EAST_INTER2, WEST_INTER2, NORTH_INTER2, SOUTH_INTER2
      logical EAST_INTER, WEST_INTER, NORTH_INTER, SOUTH_INTER
      logical CORNER_SW,CORNER_NW,CORNER_NE,CORNER_SE
      integer*4 mynode, mynode2, ii,jj, p_W,p_E,p_S,p_N, p_SW,p_SE,
     & p_NW,p_NE,NNODES2
      common /comm_setup/ mynode, mynode2, ii,jj, p_W,p_E,p_S,p_N,
     & p_SW,p_SE, p_NW,p_NE, EAST_INTER, WEST_INTER, NORTH_INTER,
     & SOUTH_INTER, EAST_INTER2, WEST_INTER2, NORTH_INTER2, 
     &                                                     SOUTH_INTER2,
     & CORNER_SW,CORNER_NW,CORNER_NE,CORNER_SE,NNODES2
      real pi, deg2rad, rad2deg
      parameter (pi=3.14159265358979323846D0, deg2rad=pi/180.D0,
     &                                      rad2deg=180.D0/pi)
      real Eradius, Erotation, g, day2sec,sec2day, jul_off,
     &     year2day,day2year
      parameter (Eradius=6371315.0D0,  Erotation=7.292115090D-5,
     &           day2sec=86400.D0, sec2day=1.D0/86400.D0,
     &           year2day=365.25D0, day2year=1.D0/365.25D0,
     &           jul_off=2440000.D0)
      parameter (g=9.81D0)
      real Cp
      parameter (Cp=3985.0D0)
      real vonKar
      parameter (vonKar=0.41D0)
      real spval
      parameter (spval=-999.0D0)
      logical mask_val
      parameter (mask_val = .true.)
      real T_west(0:1,-1:Mm+2+padd_E,N,4,NT)
      common/zoom3D_TW/T_west
      real U_west(1:1,-1:Mm+2+padd_E,N,4)
      common/zoom3D_UW/U_west
      real V_west(0:0,-1:Mm+2+padd_E,N,4)
      common/zoom3D_VW/V_west
      real T_east(Lmmpi:Lmmpi+1,-1:Mm+2+padd_E,N,4,NT)
      common/zoom3D_TE/T_east
      real U_east(Lmmpi+1:Lmmpi+1,-1:Mm+2+padd_E,N,4)
      common/zoom3D_UE/U_east
      real V_east(Lmmpi+1:Lmmpi+1,-1:Mm+2+padd_E,N,4)
      common/zoom3D_VE/V_east
      real T_south(-1:Lm+2+padd_X,0:1,N,4,NT)
      common/zoom3D_TS/T_south
      real U_south(-1:Lm+2+padd_X,0:0,N,4)
      common/zoom3D_US/U_south
      real V_south(-1:Lm+2+padd_X,1:1,N,4)
      common/zoom3D_VS/V_south
      real T_north(-1:Lm+2+padd_X,Mmmpi:Mmmpi+1,N,4,NT)
      common/zoom3D_TN/T_north
      real U_north(-1:Lm+2+padd_X,Mmmpi+1:Mmmpi+1,N,4)
      common/zoom3D_UN/U_north
      real V_north(-1:Lm+2+padd_X,Mmmpi+1:Mmmpi+1,N,4)
      common/zoom3D_VN/V_north
      integer*4 Zetatimeindex, Zetatimeindex2
      common/zoom2D_ZetaT/Zetatimeindex, Zetatimeindex2
      integer*4 U2DTimeindex, U2DTimeindex2
      common/zoom2D_UT/U2DTimeindex, U2DTimeindex2
      integer*4 V2DTimeindex, V2DTimeindex2
      common/zoom2D_VT/V2DTimeindex, V2DTimeindex2
      integer*4 Ttimeindex
      common/zoom3D_TT/Ttimeindex
      integer*4 Utimeindex
      common/zoom3D_UT/Utimeindex
      integer*4 Vtimeindex
      common/zoom3D_VT/Vtimeindex
      real weight2(0:NWEIGHT,0:NWEIGHT)
      common/weighting/weight2
      real updateTprof(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
      common/updateTprofile/updateTprof
      integer*4 indupdate
      real myvalues(3*7*(2*(Lm+3)+2*(Mm+3)),0:NWEIGHT)
      common/updatevalues/myvalues,indupdate
      integer*4 nbcoarse
      common/nestingmanag/nbcoarse
      real myfx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
      real myfy(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
      common/myfluxes/myfx,myfy
      real Zt_avg2(-1:Lm+2+padd_X,-1:Mm+2+padd_E,4)
      common/averagebaro/Zt_avg2
      real dZtinterp(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real dUinterp(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real dVinterp(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common/zoombc2D/dZtinterp,dUinterp,dVinterp
       logical Alreadyupdated(-1:Lm+2+padd_X,-1:Mm+2+padd_E,3)
       common/updateprestep/Alreadyupdated
       real usponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       real vsponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       real wsponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
       real tsponge(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N,NT)
       common/sponge_com/usponge, vsponge, tsponge,wsponge
       real Huonagrif(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       real Hvomagrif(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
       common/huvagrif/Huonagrif,Hvomagrif
      real Zt_avg3(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:NWEIGHT)
      common/zoom2D_Zeta2/Zt_avg3
      real DU_avg3(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:NWEIGHT)
      common/zoom2D_U2/DU_avg3
      real DV_avg3(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:NWEIGHT)
      common/zoom2D_V2/DV_avg3
      real T_sponge_west(0:10,-1:Mm+2+padd_E,N,2,NT)
      common/zoom3D_sponge_TW/T_sponge_west
      real U_sponge_west(1:11,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_UW/U_sponge_west
      real V_sponge_west(0:10,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_VW/V_sponge_west
      real W_sponge_west(0:10,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_WW/W_sponge_west
      real T_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2,NT)
      common/zoom3D_sponge_TE/T_sponge_east
      real U_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_UE/U_sponge_east
      real V_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_VE/V_sponge_east
      real W_sponge_east(Lmmpi-9:Lmmpi+1,-1:Mm+2+padd_E,N,2)
      common/zoom3D_sponge_WE/W_sponge_east
      real T_sponge_south(-1:Lm+2+padd_X,0:10,N,2,NT)
      common/zoom3D_sponge_TS/T_sponge_south
      real U_sponge_south(-1:Lm+2+padd_X,0:10,N,2)
      common/zoom3D_sponge_US/U_sponge_south
      real V_sponge_south(-1:Lm+2+padd_X,1:11,N,2)
      common/zoom3D_sponge_VS/V_sponge_south
      real W_sponge_south(-1:Lm+2+padd_X,0:10,N,2)
      common/zoom3D_sponge_WS/W_sponge_south
      real T_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2,NT)
      common/zoom3D_sponge_TN/T_sponge_north
      real U_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2)
      common/zoom3D_sponge_UN/U_sponge_north
      real V_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2)
      common/zoom3D_sponge_VN/V_sponge_north
      real W_sponge_north(-1:Lm+2+padd_X,Mmmpi-9:Mmmpi+1,N,2)
      common/zoom3D_sponge_WN/W_sponge_north
      integer*4 TTimesponge, UVTimesponge
      common/zoom3D_sponge_times/TTimesponge, UVTimesponge
      real A1dXI(-1:Lm+2+padd_X,10*NWEIGHT)
      real A1dETA(-1:Mm+2+padd_E,10*NWEIGHT)
      common/work_agrif/A1dXI,A1dETA
      integer*4 TspongeTimeindex, TspongeTimeindex2
      integer*4 UVspongeTimeindex, UVspongeTimeindex2
      common/zoom3D_sponge/TspongeTimeindex, UVspongeTimeindex,
     &      TspongeTimeindex2, UVspongeTimeindex2
      real,dimension(:,:),allocatable :: finevalues
      real,dimension(:,:),allocatable :: coarsevalues
      common/gridinter/finevalues,coarsevalues
      real,dimension(:,:),allocatable :: coarsevaluesinterp
      common/gridinter2/coarsevaluesinterp
      integer*4 j1t,i1t,i2t,j2t
      integer*4 i1u,i2u,j1v,j2v
      integer*4 common_index
      common/arraysindices/i1t,j1t,i2t,j2t,
     &     i1u,i2u,j1v,j2v,common_index
      integer*4 hid, zetaid,ubarid,vbarid,uid,vid,tid
      integer*4 rmaskid
      integer*4 tspongeid, uspongeid, vspongeid,wspongeid
      common/varids/hid,zetaid,ubarid,vbarid,uid,vid,tid,
     &  tspongeid, uspongeid, vspongeid, rmaskid,wspongeid
      integer*4 updatezetaid, updateubarid, updatevbarid
      integer*4 updateduavg2id, updatedvavg2id
      integer*4 updatetid, updateuid, updatevid
      integer*4 updatemyfxid, updatemyfyid
      integer*4 updatehuonid, updatehvomid
      common/varidsupdate/updatezetaid, updateubarid, updatevbarid,
     &       updateduavg2id, updatedvavg2id,
     &       updatetid, updateuid, updatevid, updatemyfxid,
     &       updatemyfyid,updatehuonid, updatehvomid
!$AGRIF_DO_NOT_TREAT
      integer*4 :: iind
      integer*4 :: sortedint(0:10000)
      integer*4 :: whichstep(0:10000)
      integer*4 :: grids_at_level(0:20,0:100)
      integer*4 :: parent_grid(0:20)
      integer*4 :: coeff_ref_time(0:20)
      integer*4 :: nbtimes, nbmaxtimes
      common/rootintegrate/nbtimes, nbmaxtimes,
     &    iind,sortedint,whichstep,
     &    grids_at_level,parent_grid,coeff_ref_time
!$AGRIF_END_DO_NOT_TREAT
      integer*4 i1,i2,j1,j2,k1,k2,m1,m2
      logical :: before
      real tabres(i1:i2,j1:j2,k1:k2,m1:m2)
      if (before) then
          tabres(i1:i2,j1:j2,k1:k2,m1:m2) =
     &               t(i1:i2,j1:j2,k1:k2,nrhs,m1:m2)
      else
          tsponge(i1:i2,j1:j2,k1:k2,m1:m2) = 
     &                                   tabres(i1:i2,j1:j2,k1:k2,m1:m2)
      endif
      return
      end
