










      subroutine clm_tides (tile)      


      use Agrif_Util
      interface
        subroutine Sub_Loop_clm_tides(tile,N2d,A2d,Mmmpi,Lmmpi,N3dHz,N3d
     &)
          implicit none
      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      integer(4) :: N2d
      integer(4) :: Mmmpi
      integer(4) :: Lmmpi
      integer(4) :: N3dHz
      integer(4) :: N3d
      real, dimension(1:N2d,1:NSA,0:NPP-1) :: A2d
      integer(4) :: tile
        end subroutine Sub_Loop_clm_tides

      end interface
      integer(4) :: tile
      

        call Sub_Loop_clm_tides(tile, Agrif_tabvars_i(15)%iarray0, Agrif
     &_tabvars(131)%array3, Agrif_tabvars_i(8)%iarray0, Agrif_tabvars_i(
     &7)%iarray0, Agrif_tabvars_i(17)%iarray0, Agrif_tabvars_i(16)%iarra
     &y0)

      end


      subroutine Sub_Loop_clm_tides(tile,N2d,A2d,Mmmpi,Lmmpi,N3dHz,N3d)

      use Agrif_Types, only : Agrif_tabvars

      implicit none

      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      integer(4) :: N2d
      integer(4) :: Mmmpi
      integer(4) :: Lmmpi
      integer(4) :: N3dHz
      integer(4) :: N3d
      real, dimension(1:N2d,1:NSA,0:NPP-1) :: A2d
      integer(4) :: tile

                   

                                              
      integer(4) :: trd
      integer(4) :: omp_get_thread_num
                                            
                             
                                            

                                                             
                     
                        

                                                            
                                           
                                                               
                                    
                                      
                                                          

                       

                                    

                        
                              

                       
                          

                                                
                                             
                          

                        

                                 

                                                
                        

                                                              
                      
                              

                                       
                                                                     
                                          
                         

                             

                             

                            

                            

                                             

                            

                                     

                                                               

                         

                                                      
                                                      
                                                                  
                               

                               

                              

                              

                               

                              

                              

                                

                             

                                                                       
                                                                 
               
                                 
                                            
                                      
                                                            
      integer(4) :: chunk_size_X
      integer(4) :: margin_X
      integer(4) :: chunk_size_E
      integer(4) :: margin_E
                                             
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
      integer(4) :: i_X
      integer(4) :: 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 clm_tides_tile (Istr,Iend,Jstr,Jend,
     &                     A2d(1,1,trd),A2d(1,2,trd),A2d(1,3,trd),
     &                     A2d(1,4,trd),A2d(1,5,trd),A2d(1,6,trd),
     &                                               A2d(1,7,trd))
      return
       
      

      end subroutine Sub_Loop_clm_tides

      subroutine clm_tides_tile (Istr,Iend,Jstr,Jend,Cangle,Sangle,
     &                           Cphase,Sphase,Etide,Utide,Vtide)      


      use Agrif_Util
      interface
        subroutine Sub_Loop_clm_tides_tile(Istr,Iend,Jstr,Jend,Cangle,Sa
     &ngle,Cphase,Sphase,Etide,Utide,Vtide,padd_E,Mm,padd_X,Lm,POT_Tphas
     &e,POT_Tamp,Ptide,vbarbry_north,ubarbry_north,vbarbry_south,ubarbry
     &_south,vbarbry_east,ubarbry_east,vbarbry_west,ubarbry_west,vmask,u
     &mask,UV_Tminor,UV_Tmajor,UV_Tphase,angler,UV_Tangle,zetabry_north,
     &zetabry_south,zetabry_east,zetabry_west,rmask,SSH_Tphase,SSH_Tamp,
     &Tperiod,time,iic,dt,NORTH_INTER,Mmmpi,SOUTH_INTER,EAST_INTER,Lmmpi
     &,WEST_INTER)
          implicit none
      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      real :: time
      integer(4) :: iic
      real :: dt
      logical :: NORTH_INTER
      integer(4) :: Mmmpi
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      integer(4) :: Lmmpi
      logical :: WEST_INTER
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: POT_Tph
     &ase
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: POT_Tam
     &p
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: Ptide
      real, dimension(-1:Lm+2+padd_X) :: vbarbry_north
      real, dimension(-1:Lm+2+padd_X) :: ubarbry_north
      real, dimension(-1:Lm+2+padd_X) :: vbarbry_south
      real, dimension(-1:Lm+2+padd_X) :: ubarbry_south
      real, dimension(-1:Mm+2+padd_E) :: vbarbry_east
      real, dimension(-1:Mm+2+padd_E) :: ubarbry_east
      real, dimension(-1:Mm+2+padd_E) :: vbarbry_west
      real, dimension(-1:Mm+2+padd_E) :: ubarbry_west
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: vmask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: umask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tmin
     &or
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tmaj
     &or
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tpha
     &se
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: angler
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tang
     &le
      real, dimension(-1:Lm+2+padd_X) :: zetabry_north
      real, dimension(-1:Lm+2+padd_X) :: zetabry_south
      real, dimension(-1:Mm+2+padd_E) :: zetabry_east
      real, dimension(-1:Mm+2+padd_E) :: zetabry_west
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: rmask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: SSH_Tph
     &ase
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: SSH_Tam
     &p
      real, dimension(1:Ntides) :: Tperiod
      integer(4) :: Iend
      integer(4) :: Istr
      integer(4) :: Jend
      integer(4) :: Jstr
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Cangle
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Cphase
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Sangle
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Sphase
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Etide
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Utide
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Vtide
        end subroutine Sub_Loop_clm_tides_tile

      end interface
      integer(4) :: Iend
      integer(4) :: Istr
      integer(4) :: Jend
      integer(4) :: Jstr
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Cangle
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Cphase
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Sangle
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Sphase
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Etide
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Utide
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Vtide
      

        call Sub_Loop_clm_tides_tile(Istr,Iend,Jstr,Jend,Cangle,Sangle,C
     &phase,Sphase,Etide,Utide,Vtide, Agrif_tabvars_i(14)%iarray0, Agrif
     &_tabvars_i(4)%iarray0, Agrif_tabvars_i(13)%iarray0, Agrif_tabvars_
     &i(2)%iarray0, Agrif_tabvars(226)%array3, Agrif_tabvars(227)%array3
     &, Agrif_tabvars(225)%array2, Agrif_tabvars(183)%array1, Agrif_tabv
     &ars(185)%array1, Agrif_tabvars(195)%array1, Agrif_tabvars(197)%arr
     &ay1, Agrif_tabvars(207)%array1, Agrif_tabvars(209)%array1, Agrif_t
     &abvars(219)%array1, Agrif_tabvars(221)%array1, Agrif_tabvars(91)%a
     &rray2, Agrif_tabvars(92)%array2, Agrif_tabvars(229)%array3, Agrif_
     &tabvars(230)%array3, Agrif_tabvars(228)%array3, Agrif_tabvars(124)
     &%array2, Agrif_tabvars(231)%array3, Agrif_tabvars(187)%array1, Agr
     &if_tabvars(199)%array1, Agrif_tabvars(211)%array1, Agrif_tabvars(2
     &23)%array1, Agrif_tabvars(94)%array2, Agrif_tabvars(232)%array3, A
     &grif_tabvars(233)%array3, Agrif_tabvars(234)%array1, Agrif_tabvars
     &_r(5)%array0, Agrif_tabvars_i(147)%iarray0, Agrif_tabvars_r(3)%arr
     &ay0, Agrif_tabvars_l(13)%larray0, Agrif_tabvars_i(8)%iarray0, Agri
     &f_tabvars_l(14)%larray0, Agrif_tabvars_l(11)%larray0, Agrif_tabvar
     &s_i(7)%iarray0, Agrif_tabvars_l(12)%larray0)

      end


      subroutine Sub_Loop_clm_tides_tile(Istr,Iend,Jstr,Jend,Cangle,Sang
     &le,Cphase,Sphase,Etide,Utide,Vtide,padd_E,Mm,padd_X,Lm,POT_Tphase,
     &POT_Tamp,Ptide,vbarbry_north,ubarbry_north,vbarbry_south,ubarbry_s
     &outh,vbarbry_east,ubarbry_east,vbarbry_west,ubarbry_west,vmask,uma
     &sk,UV_Tminor,UV_Tmajor,UV_Tphase,angler,UV_Tangle,zetabry_north,ze
     &tabry_south,zetabry_east,zetabry_west,rmask,SSH_Tphase,SSH_Tamp,Tp
     &eriod,time,iic,dt,NORTH_INTER,Mmmpi,SOUTH_INTER,EAST_INTER,Lmmpi,W
     &EST_INTER)

      use Agrif_Types, only : Agrif_tabvars

      implicit none

      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      real :: time
      integer(4) :: iic
      real :: dt
      logical :: NORTH_INTER
      integer(4) :: Mmmpi
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      integer(4) :: Lmmpi
      logical :: WEST_INTER
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: POT_Tph
     &ase
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: POT_Tam
     &p
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: Ptide
      real, dimension(-1:Lm+2+padd_X) :: vbarbry_north
      real, dimension(-1:Lm+2+padd_X) :: ubarbry_north
      real, dimension(-1:Lm+2+padd_X) :: vbarbry_south
      real, dimension(-1:Lm+2+padd_X) :: ubarbry_south
      real, dimension(-1:Mm+2+padd_E) :: vbarbry_east
      real, dimension(-1:Mm+2+padd_E) :: ubarbry_east
      real, dimension(-1:Mm+2+padd_E) :: vbarbry_west
      real, dimension(-1:Mm+2+padd_E) :: ubarbry_west
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: vmask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: umask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tmin
     &or
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tmaj
     &or
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tpha
     &se
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: angler
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: UV_Tang
     &le
      real, dimension(-1:Lm+2+padd_X) :: zetabry_north
      real, dimension(-1:Lm+2+padd_X) :: zetabry_south
      real, dimension(-1:Mm+2+padd_E) :: zetabry_east
      real, dimension(-1:Mm+2+padd_E) :: zetabry_west
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E) :: rmask
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: SSH_Tph
     &ase
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:Ntides) :: SSH_Tam
     &p
      real, dimension(1:Ntides) :: Tperiod
      integer(4) :: Iend
      integer(4) :: Istr
      integer(4) :: Jend
      integer(4) :: Jstr
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Cangle
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Cphase
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Sangle
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Sphase
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Etide
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Utide
      real, dimension(Istr-2:Iend+2,Jstr-2:Jend+2) :: Vtide

                   

                                            
                             
                                            

                                                             
                     
                        

                                                            
                                           
                                                               
                                    
                                      
                                                          

                       

                                    

                        
                              

                       
                          

                                                
                                             
                          

                        

                                 

                                                
                        

                                                              
                      
                              

                                       
                                                                     
                                          
                         

                             

                             

                            

                            

                                             

                            

                                     

                                                               

                         

                                                      
                                                      
                                                                  
                               

                               

                              

                              

                               

                              

                              

                                

                             

                                              
                             
                                            
                                               
                                            
                                               
                                                                 
                                                 
                                 
                                               
                                               
                                               
                                               
                                               
                                               
                                             
                                             
                                             
                                             
                                             
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                               
                                              
                                                  
                                                  
                                                  
                                                  
                                                 
                                                 
                                               
                                               
                                                      
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                           
                                                           
                                                           
                                    
                                                
                                                
                                                
                                                
                                                 
                           
                           
                           
                           
                             
                                              
                          
                                                                  
                                                                       
                                                                 
                                 
                                
                                                                       
                                                                 
                                                                 
                                                                 
                                                        
                                                                       
                                                                 
                                                                 
            
                                              
                                                   
                     
                              
                               
                                 
                                                  
                                                                       
                                                                 
                                                                 
                        
                      
                           
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                    
                           
                                      
                             
                                                 
                                                                       
                                                                 
                     
                                                      
                                                                       
                                                                 
              
                                          
                                                                       
                                                                 
       
                                                           
      real(8) :: Cu_Nbq_X
      real(8) :: Cu_Nbq_Y
      real(8) :: Cu_Nbq_Z
                                             
                                                                       
                                                                 
              
                                                  
                                                                       
                                                                 
              
                                  
                                           
                                                  
                                                                   
                                                               
                                                      
                                                                       
                                                                 
                      
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                        
                                
                                                                       
                                                                 
                                                        

                                                                       
                                                                 
                  
                                                                       
                                                                 
                                                                 
                                                                 
                  

                          

              
                             

                  
                               

                 
                                

                       
                                   

                           
                                     
                                                          
                                       
                                                            
                                           
                                                           
                                         
                                                           
                                         
                                                           
                                         
                                                           
                                         
                                                          
                                       
                                                            
                                           
                                                
                                 
                           
                                    
                       
                                             
                                                                       
                                                                 
         
                     
                                          
                                                                       
                                                                       
                                                                 
                                                      
                                
                                            
                                                 
                                                             
                                           
                                                    
                                 
                                      
                                
                                            
                                                 
                                                             
                                           
                                                    
                                 
                                      
                                 
                                              
                                                   
                                                                
                                             
                                                       
                                  
                                        
                                 
                                              
                                                   
                                                                
                                             
                                                       
                                  
                                        
                                                                       
                                                                 
              
                                                           
                                                                       
                                                                 
                                                                 
                                  
                                                                       
                                                                 
                                                
                                                                       
                                                                 
                                                                 
                              
                                                                       
                                                                 
                              
                                                                       
                                                                 
                   
                                                  
                                                                       
                                                                 
              
                                                           
                                                                       
                                                                 
                                                                 
                                  
                                                                       
                                                                 
                                                
                                                                       
                                                                 
                                                                 
                              
                                                                       
                                                                 
                              
                                                                       
                                                                 
                  
                                                  
                                                                       
                                                                 
                
                                                              
                                                                       
                                                                 
                                                                 
                                      
                                                                       
                                                                 
                                                      
                                                                       
                                                                 
                                                                 
                                  
                                                                       
                                                                 
                                    
                                                                       
                                                                 
                    
                                                     
                                                                       
                                                                 
                
                                                              
                                                                       
                                                                 
                                                                 
                                      
                                                                       
                                                                 
                                                      
                                                                       
                                                                 
                                                                 
                                  
                                                                       
                                                                 
                                    
                                                                       
                                                                 
                    
                                                     
                                                               
      integer(4) :: i
      integer(4) :: j
      integer(4) :: itide
                                                         
      real :: angle
      real :: phase
      real :: omega_loc
      real :: ramp
      real :: cff
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                 
                                        
      integer(4) :: IstrR
      integer(4) :: IendR
      integer(4) :: JstrR
      integer(4) :: 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
      angle=0.D0
      phase=0.D0
      ramp=1.D0
      ramp=TANH(dt*sec2day*float(iic-1))
      cff=2.D0*pi*time
!$acc kernels if(compute_on_device) default(present)
      do j=Jstr-1,Jend+1
        do i=Istr-1,Iend+1
          Etide(i,j)=0.D0
        enddo
      enddo
      do itide=1,Ntides
        if (Tperiod(itide).gt.0.D0) then
          omega_loc=cff/Tperiod(itide)
          do j=Jstr-1,Jend+1
            do i=Istr-1,Iend+1
              Etide(i,j)=Etide(i,j)+ramp*
     &                   SSH_Tamp(i,j,itide)*
     &                   COS(omega_loc-SSH_Tphase(i,j,itide))
              Etide(i,j)=Etide(i,j)*rmask(i,j)
            enddo
          enddo
        endif
      enddo
!$acc end kernels
!$acc kernels if(compute_on_device) default(present)
      if ((istr.eq.1 .and. .not.WEST_INTER)) then
        do j=JstrR,JendR
          zetabry_west(j)=zetabry_west(j)+Etide(Istr-1,j)
        enddo
      endif
      if ((iend.eq.Lmmpi .and. .not.EAST_INTER)) then
        do j=JstrR,JendR
          zetabry_east(j)=zetabry_east(j)+Etide(Iend+1,j)
        enddo
      endif
      if ((jstr.eq.1.and. .not.SOUTH_INTER)) then
        do i=IstrR,IendR
          zetabry_south(i)=zetabry_south(i)+Etide(i,Jstr-1)
        enddo
      endif
      if ((jend.eq.Mmmpi .and. .not.NORTH_INTER)) then
        do i=IstrR,IendR
          zetabry_north(i)=zetabry_north(i)+Etide(i,Jend+1)
        enddo
      endif
!$acc end kernels
!$acc kernels if(compute_on_device) default(present)
      do j=JstrR,JendR
        do i=Istr,IendR
          Utide(i,j)=0.D0
        enddo
      enddo
      do j=Jstr,JendR
        do i=IstrR,IendR
          Vtide(i,j)=0.D0
        enddo
      enddo
      do itide=1,Ntides
        if (Tperiod(itide).gt.0.D0) then
          omega_loc=cff/Tperiod(itide)
          do j=Jstr-1,JendR
            do i=Istr-1,IendR
              angle=UV_Tangle(i,j,itide)
     &                      -angler(i,j)
              phase=omega_loc-UV_Tphase(i,j,itide)
              Cangle(i,j)=COS(angle)
              Cphase(i,j)=COS(phase)
              Sangle(i,j)=SIN(angle)
              Sphase(i,j)=SIN(phase)
            enddo
          enddo
          do j=JstrR,JendR
            do i=Istr,IendR
              Utide(i,j)=Utide(i,j)+ramp*
     &                        0.125D0*((UV_Tmajor(i-1,j,itide)+
     &                                UV_Tmajor(i  ,j,itide))*
     &                               (Cangle(i-1,j)+Cangle(i,j))*
     &                               (Cphase(i-1,j)+Cphase(i,j))-
     &                               (UV_Tminor(i-1,j,itide)+
     &                                UV_Tminor(i  ,j,itide))*
     &                               (Sangle(i-1,j)+Sangle(i,j))*
     &                               (Sphase(i-1,j)+Sphase(i,j)))
             Utide(i,j)=Utide(i,j)*umask(i,j)
            enddo
          enddo
          do j=Jstr,JendR
            do i=IstrR,IendR
              Vtide(i,j)=Vtide(i,j)+ramp*
     &                        0.125D0*((UV_Tmajor(i,j-1,itide)+
     &                                UV_Tmajor(i,j  ,itide))*
     &                               (Sangle(i,j-1)+Sangle(i,j))*
     &                               (Cphase(i,j-1)+Cphase(i,j))+
     &                               (UV_Tminor(i,j-1,itide)+
     &                                UV_Tminor(i,j  ,itide))*
     &                               (Cangle(i,j-1)+Cangle(i,j))*
     &                               (Sphase(i,j-1)+Sphase(i,j)))
              Vtide(i,j)=Vtide(i,j)*vmask(i,j)
            enddo
          enddo
        endif
      enddo
      if ((istr.eq.1 .and. .not.WEST_INTER)) THEN
        do j=JstrR,JendR
          ubarbry_west(j)=ubarbry_west(j)+Utide(Istr,j)
        enddo
        do j=Jstr,JendR
          vbarbry_west(j)=vbarbry_west(j)+Vtide(Istr-1,j)
        enddo
      endif
      if ((iend.eq.Lmmpi .and. .not.EAST_INTER)) THEN
        do j=JstrR,JendR
          ubarbry_east(j)=ubarbry_east(j)+Utide(Iend+1,j)
        enddo
        do j=Jstr,JendR
          vbarbry_east(j)=vbarbry_east(j)+Vtide(Iend+1,j)
        enddo
      endif
      if ((jstr.eq.1.and. .not.SOUTH_INTER)) THEN
        do i=Istr,IendR
          ubarbry_south(i)=ubarbry_south(i)+Utide(i,Jstr-1)
        enddo
        do i=IstrR,IendR
          vbarbry_south(i)=vbarbry_south(i)+Vtide(i,Jstr)
        enddo
      endif
      if ((jend.eq.Mmmpi .and. .not.NORTH_INTER)) THEN
        do i=Istr,IendR
          ubarbry_north(i)=ubarbry_north(i)+Utide(i,Jend+1)
        enddo
        do i=IstrR,IendR
          vbarbry_north(i)=vbarbry_north(i)+Vtide(i,Jend+1)
        enddo
      endif
!$acc end kernels
!$acc kernels if(compute_on_device) default(present)
      do j=JstrR,JendR
        do i=IstrR,IendR
          Ptide(i,j)=0.D0
        enddo
      enddo
      do itide=1,Ntides
        if (Tperiod(itide).gt.0.D0) then
          omega_loc=cff/Tperiod(itide)
          do j=JstrR,JendR
            do i=IstrR,IendR
              Ptide(i,j)=Ptide(i,j)+
     &                   ramp*POT_Tamp(i,j,itide)*
     &                   COS(omega_loc-POT_Tphase(i,j,itide))
              Ptide(i,j)=Ptide(i,j)*rmask(i,j)
            enddo
          enddo
        endif
      enddo
!$acc end kernels
      call exchange_r2d_tile (Istr,Iend,Jstr,Jend,
     &                        Ptide(-1,-1))
      return
       
      

      end subroutine Sub_Loop_clm_tides_tile

