      subroutine mrl_wci (tile)
      implicit none
      integer*4  tile, trd, omp_get_thread_num
      integer*4  LLm,Lm,MMm,Mm,N, LLm0,MMm0
      parameter (LLm0=41,   MMm0=42,   N=32)
      parameter (LLm=LLm0,  MMm=MMm0)
      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=2,  NP_ETA=2,  NNODES=NP_XI*NP_ETA)
      parameter (NPP=1)
      parameter (NSUB_X=1, NSUB_E=1)
      integer*4 NWEIGHT
      parameter (NWEIGHT=1000)
      integer*4 stdout, Np, padd_X,padd_E
      common /stdout/stdout
      parameter (Np=N+1)
      parameter (Lm=(LLm+NP_XI-1)/NP_XI, Mm=(MMm+NP_ETA-1)/NP_ETA)
      parameter (padd_X=(Lm+2)/2-(Lm+1)/2)
      parameter (padd_E=(Mm+2)/2-(Mm+1)/2)
      integer*4 NSA, N2d,N3d, size_XI,size_ETA
      integer*4 se,sse, sz,ssz
      parameter (NSA=28)
      parameter (size_XI=7+(Lm+NSUB_X-1)/NSUB_X)
      parameter (size_ETA=7+(Mm+NSUB_E-1)/NSUB_E)
      parameter (sse=size_ETA/Np, ssz=Np/size_ETA)
      parameter (se=sse/(sse+ssz), sz=1-se)
      parameter (N2d=size_XI*(se*size_ETA+sz*Np))
      parameter (N3d=size_XI*size_ETA*Np)
      real Vtransform
      parameter (Vtransform=2)
      integer*4   NT, itemp
      integer*4   ntrc_salt, ntrc_pas, ntrc_bio, ntrc_sed
      parameter (itemp=1)
      parameter (ntrc_salt=1)
      parameter (ntrc_pas=0)
      parameter (ntrc_bio=0)
      parameter (ntrc_sed=0)
      parameter (NT=itemp+ntrc_salt+ntrc_pas+ntrc_bio+ntrc_sed)
      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,7,0:NPP-1)
      integer*4 B2d(N2d,0:NPP-1)
      common/private_scratch/ A2d,A3d
      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 mrl_wci_tile (istr,iend,jstr,jend,
     &            A3d(1,1,trd),A3d(1,2,trd),A3d(1,3,trd),
     &            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) ,A2d(1,8,trd) ,A2d(1,9,trd) ,
     &            A2d(1,10,trd),A2d(1,11,trd),A2d(1,12,trd),
     &            A2d(1,13,trd)
     &            ,A2d(1,14,trd)
     &                  )
      return
      end
      subroutine mrl_wci_tile (istr,iend,jstr,jend,
     &            wrk1,wrk2,wrk3,
     &            wh,fr,kw,brk,stk,Dstp,act,kD,inv_d,inv_f,frc,ebrk,erol
     &           ,zob_my
     &     )
      implicit none
      integer*4  LLm,Lm,MMm,Mm,N, LLm0,MMm0
      parameter (LLm0=41,   MMm0=42,   N=32)
      parameter (LLm=LLm0,  MMm=MMm0)
      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=2,  NP_ETA=2,  NNODES=NP_XI*NP_ETA)
      parameter (NPP=1)
      parameter (NSUB_X=1, NSUB_E=1)
      integer*4 NWEIGHT
      parameter (NWEIGHT=1000)
      integer*4 stdout, Np, padd_X,padd_E
      common /stdout/stdout
      parameter (Np=N+1)
      parameter (Lm=(LLm+NP_XI-1)/NP_XI, Mm=(MMm+NP_ETA-1)/NP_ETA)
      parameter (padd_X=(Lm+2)/2-(Lm+1)/2)
      parameter (padd_E=(Mm+2)/2-(Mm+1)/2)
      integer*4 NSA, N2d,N3d, size_XI,size_ETA
      integer*4 se,sse, sz,ssz
      parameter (NSA=28)
      parameter (size_XI=7+(Lm+NSUB_X-1)/NSUB_X)
      parameter (size_ETA=7+(Mm+NSUB_E-1)/NSUB_E)
      parameter (sse=size_ETA/Np, ssz=Np/size_ETA)
      parameter (se=sse/(sse+ssz), sz=1-se)
      parameter (N2d=size_XI*(se*size_ETA+sz*Np))
      parameter (N3d=size_XI*size_ETA*Np)
      real Vtransform
      parameter (Vtransform=2)
      integer*4   NT, itemp
      integer*4   ntrc_salt, ntrc_pas, ntrc_bio, ntrc_sed
      parameter (itemp=1)
      parameter (ntrc_salt=1)
      parameter (ntrc_pas=0)
      parameter (ntrc_bio=0)
      parameter (ntrc_sed=0)
      parameter (NT=itemp+ntrc_salt+ntrc_pas+ntrc_bio+ntrc_sed)
      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 sustr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real svstr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_sustr/sustr /forces_svstr/svstr
      real twox(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real twoy(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real tawx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real tawy(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_twox/twox /forces_twoy/twoy
      common /forces_tawx/tawx /forces_tawy/tawy
      real sustrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real svstrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /smsdat_sustrg/sustrg /smsdat_svstrg/svstrg
      real    sustrp(2), svstrp(2), sms_time(2)
      real    sms_cycle, sms_scale
      integer*4 itsms, sms_ncycle, sms_rec, lsusgrd
      integer*4 lsvsgrd,sms_tid, susid, svsid
      common /smsdat1/ sustrp, svstrp, sms_time
      common /smsdat2/ sms_cycle, sms_scale
      common /smsdat3/ itsms, sms_ncycle, sms_rec, lsusgrd
      common /smsdat4/ lsvsgrd,sms_tid, susid, svsid
      integer*4 lwgrd, wid
      common /smsdat5/ lwgrd, wid
      real bustr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real bvstr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_bustr/bustr /forces_bvstr/bvstr
      real bustrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real bvstrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /bmsdat_bustrg/bustrg /bmsdat_bvstrg/bvstrg
      real bms_tintrp(2), bustrp(2),    bvstrp(2), tbms(2)
      real bmsclen, bms_tstart, bms_tend,  tsbms, sclbms
      integer*4 itbms,      bmstid,busid, bvsid,     tbmsindx
      logical bmscycle,   bms_onerec,   lbusgrd,   lbvsgrd
      common /bmsdat1/bms_tintrp, bustrp,       bvstrp,    tbms
      common /bmsdat2/bmsclen, bms_tstart, bms_tend, tsbms, sclbms
      common /bmsdat3/itbms,      bmstid,busid, bvsid,     tbmsindx
      common /bmsdat4/bmscycle,   bms_onerec,   lbusgrd,   lbvsgrd
      real stflx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,NT)
      common /forces_stflx/stflx
      real shflx_rsw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_rsw/shflx_rsw
      real shflx_rlw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_rlw/shflx_rlw
      real shflx_lat(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_lat/shflx_lat
      real shflx_sen(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_sen/shflx_sen
      real stflxg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2,NT)
      common /stfdat_stflxg/stflxg
      real stflxp(2,NT), stf_time(2,NT)
      real stf_cycle(NT), stf_scale(NT)
      integer*4 itstf(NT), stf_ncycle(NT), stf_rec(NT)
      integer*4 lstfgrd(NT), stf_tid(NT), stf_id(NT)
      common /stfdat1/ stflxp,  stf_time, stf_cycle, stf_scale
      common /stfdat2/ itstf, stf_ncycle, stf_rec, lstfgrd
      common /stfdat3/  stf_tid, stf_id
      real btflx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,NT)
      common /forces_btflx/btflx
      real tair(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real rhum(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real prate(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real radlw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real radsw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wspd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real uwnd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real vwnd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /bulk_tair/ tair
      common /bulk_rhum/ rhum
      common /bulk_prate/ prate
      common /bulk_radlw/ radlw
      common /bulk_radsw/ radsw
      common /bulk_wspd/ wspd
      common /bulk_uwnd/ uwnd
      common /bulk_vwnd/ vwnd
      real tairg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real rhumg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real prateg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real radlwg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real radswg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real wspdg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real uwndg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real vwndg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /bulkdat_tairg/tairg
      common /bulkdat_rhumg/rhumg
      common /bulkdat_prateg/prateg
      common /bulkdat_radlwg/radlwg
      common /bulkdat_radswg/radswg
      common /bulkdat_wspdg/wspdg
      common /bulk_uwndg/uwndg
      common /bulk_vwndg/vwndg
      real    tairp(2),rhump(2),pratep(2),radlwp(2),radswp(2)
      real    wspdp(2)
      real    uwndp(2),vwndp(2)
      real    bulk_time(2), bulk_cycle
      integer*4 tair_id,rhum_id,prate_id,radlw_id,radsw_id
      integer*4 ltairgrd,lrhumgrd,lprategrd,lradlwgrd,lradswgrd
      integer*4 wspd_id,lwspdgrd
      integer*4 uwnd_id,vwnd_id,luwndgrd,lvwndgrd
      integer*4 itbulk,bulk_ncycle,bulk_rec,bulk_tid
      integer*4 bulkunused
      common /bulkdat1_for/ tair_id,rhum_id,prate_id,radlw_id,radsw_id
      common /bulkdat1_grd
     &                 / ltairgrd,lrhumgrd,lprategrd,lradlwgrd,lradswgrd
      common /bulkdat1_tim/ itbulk, bulk_ncycle, bulk_rec, bulk_tid
      common /bulkdat1_uns/ bulkunused
      common /bulkdat1_wnd/ uwnd_id,vwnd_id,luwndgrd,lvwndgrd
      common /bulkdat1_wspd/ wspd_id,lwspdgrd
      common /bulkdat2_for/ tairp,rhump,pratep,radlwp,radswp
      common /bulkdat2_tim/ bulk_time, bulk_cycle
      common /bulkdat2_wnd/ uwndp,vwndp
      common /bulkdat2_wspd/ wspdp
      real srflx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_srflx/srflx
      real srflxg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /srfdat_srflxg/srflxg
      real srflxp(2),srf_time(2)
      real srf_cycle, srf_scale
      integer*4 itsrf, srf_ncycle, srf_rec
      integer*4 lsrfgrd, srf_tid, srf_id
      common /srfdat1/ srflxp, srf_time, srf_cycle, srf_scale
      common /srfdat2/ itsrf,srf_ncycle,srf_rec,lsrfgrd,srf_tid,srf_id
      real wfrq(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_wfrq/wfrq
      real wwkx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_wkx/wwkx
      real wwke(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_wke/wwke
      real uorb(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real vorb(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_uorb/uorb /forces_vorb/vorb
      real whrm(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wepb(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wepd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wdrx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wdre(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_whrm/whrm /forces_wepb/wepb
     &       /forces_wdrx/wdrx /forces_wdre/wdre
     &       /forces_wepd/wepd
      real brk2dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real brk2de(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real ust2d(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real vst2d(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real frc2dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real frc2de(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real sup(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_brk2dx/brk2dx /forces_brk2de/brk2de
      common /forces_ust2d/ust2d /forces_vst2d/vst2d
      common /forces_frc2dx/frc2dx /forces_frc2de/frc2de
      common /forces_sup/sup
      real calP(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real Kapsrf(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_calP/calP /forces_Kapsrf/Kapsrf
      real brk3dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real brk3de(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /forces_brk3dx/brk3dx /forces_brk3de/brk3de
      real frc3dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real frc3de(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /forces_frc3dx/frc3dx /forces_frc3de/frc3de
      real ust(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real vst(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real wst(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real kvf(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real Akb(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      real Akw(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      real E_pre(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      common /forces_stokes/ust, vst, wst
      common /forces_kvf/kvf /forces_Akb/Akb /forces_Akw/Akw
      common /forces_E_pre/E_pre
      real Awave(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real Dwave(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real Pwave(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /bbl_Awave/Awave /bbl_Dwave/Dwave /bbl_Pwave/Pwave
      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 dt, dtfast, time, time2, time_start, tdays
      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,
     &                       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
      integer*4 numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
      logical ldefhis
      logical got_tini(NT)
      logical got_inibed(2)
      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
     &      , numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
     &                      , got_tini
     &                      , got_inibed
     &                      , 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, ii,jj, p_W,p_E,p_S,p_N, p_SW,p_SE, p_NW,p_NE
      common /comm_setup/ mynode, 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
      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=0.0D0)
      logical mask_val
      parameter (mask_val = .true.)
      real zeta(-1:Lm+2+padd_X,-1:Mm+2+padd_E,4)
      real ubar(-1:Lm+2+padd_X,-1:Mm+2+padd_E,4)
      real vbar(-1:Lm+2+padd_X,-1:Mm+2+padd_E,4)
      common /ocean_zeta/zeta
      common /ocean_ubar/ubar
      common /ocean_vbar/vbar
      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
      integer*4 istr,iend,jstr,jend,i,j,imin,jmin
      real    cff,cff1,cff2,cff3,cff4,inv_g,khd,kh,umag,
     &   eps,wave_ramp,wramp2,kbrk,fb,fb0,fb1,fb2,inv_fbs,
     &   bconst,tauc,tauw,ka_f00,hz0,z_tide,
     &   dtinv,inv_zb,a_brk,a_kv,c1o3,c4o3,inv_k,dd,ust_ker,intfb,
     &   fbsrf,fn1,fn2,khmax,bz1,bz2,beta,abot,delta,
     &   a_frc,kfrc,Hrms,urms,fw,
     &             wh(Istr-2:Iend+2,Jstr-2:Jend+2),
     &             fr(Istr-2:Iend+2,Jstr-2:Jend+2),
     &             kw(Istr-2:Iend+2,Jstr-2:Jend+2),
     &            brk(Istr-2:Iend+2,Jstr-2:Jend+2),
     &            stk(Istr-2:Iend+2,Jstr-2:Jend+2),
     &           Dstp(Istr-2:Iend+2,Jstr-2:Jend+2),
     &            act(Istr-2:Iend+2,Jstr-2:Jend+2),
     &             kD(Istr-2:Iend+2,Jstr-2:Jend+2),
     &          inv_d(Istr-2:Iend+2,Jstr-2:Jend+2),
     &          inv_f(Istr-2:Iend+2,Jstr-2:Jend+2),
     &            frc(Istr-2:Iend+2,Jstr-2:Jend+2),
     &           ebrk(Istr-2:Iend+2,Jstr-2:Jend+2),
     &           erol(Istr-2:Iend+2,Jstr-2:Jend+2)
     &           ,zob_my(Istr-2:Iend+2,Jstr-2:Jend+2)
      integer*4  k,kk
      real     wrk1(Istr-2:Iend+2,Jstr-2:Jend+2,0:N),
     &         wrk2(Istr-2:Iend+2,Jstr-2:Jend+2,0:N),
     &         wrk3(Istr-2:Iend+2,Jstr-2:Jend+2,0:N),
     &         wrk4(0:N),wrk5(0:N),wrk6(0:N), kvsurf
      parameter ( eps=1.D-10,
     &            bconst=0.03D0,
     &            a_kv=1.2D0,
     &            a_brk=0.2D0,
     &            a_frc=3.D0,
     &            khmax=20.D0,
     &            c1o3=0.3333333333333333D0,
     &            c4o3=1.3333333333333333D0
     &           )
      real wave_gam,wave_btg,wave_roller
      parameter (wave_gam=0.4D0, wave_btg=0.8D0)
      parameter (wave_roller=0.0D0)
      integer*4 IstrR,IendR,JstrR,JendR
      integer*4 IstrU
      integer*4 JstrV
      if (.not.WEST_INTER) then
        IstrR=Istr-1
        IstrU=Istr+1
      else
        IstrR=Istr
        IstrU=Istr
      endif
      if (.not.EAST_INTER) then
        IendR=Iend+1
      else
        IendR=Iend
      endif
      if (.not.SOUTH_INTER) then
        JstrR=Jstr-1
        JstrV=Jstr+1
      else
        JstrR=Jstr
        JstrV=Jstr
      endif
      if (.not.NORTH_INTER) then
        JendR=Jend+1
      else
        JendR=Jend
      endif
      if (Zobt.gt.0.D0) then
        do j=jstr-1,jend+1
          do i=istr-1,iend+1
            Zob_my(i,j)=Zob(i,j)
          enddo
        enddo
      else
        Zob_my=1.D-3
      endif
      wave_ramp = tanh(12.0D0*dt*sec2day*float(iic-ntstart))
      if (iic.eq.0) wave_ramp=1.0D0
      wramp2= wave_ramp**2
      inv_g = 1.0D0/g
      do j=jstr-1,jend+1
        do i=istr-1,iend+1
          Dstp(i,j)=h(i,j)+z_w(i,j,N)
          inv_d(i,j)=1.0D0/Dstp(i,j)
          wh(i,j)=wave_ramp*whrm(i,j)
          fr(i,j)=wfrq(i,j)
          inv_f(i,j)=1.0D0/max(fr(i,j),eps)
          khd=Dstp(i,j)*(fr(i,j)**2)*inv_g
          kh = sqrt( khd*khd + khd/(1.0D0 + khd*(0.6666666666D0
     &               +khd*(0.3555555555D0 + khd*(0.1608465608D0
     &               +khd*(0.0632098765D0 + khd*(0.0217540484D0
     &                            +khd*0.0065407983D0)))))) )
          kD(i,j) =kh
          kw(i,j) =kh*inv_d(i,j)
          cff=3.0D0/16.0D0*sqrt(pi)*g*(wave_btg**3)/(2.0D0*pi)
          cff1=wh(i,j)/(wave_gam*Dstp(i,j))
          ebrk(i,j)=cff/Dstp(i,j)*(wh(i,j)**3)
     &              *( 1.0D0+tanh(8.0D0*(cff1-1.0D0)) )*
     &               ( 1.0D0-(1.0D0+cff1**2)**(-2.5D0) )
          ebrk(i,j)=wramp2*ebrk(i,j)*fr(i,j)
          cff=ebrk(i,j)
          brk(i,j)=cff*kw(i,j)*inv_f(i,j)
          abot=wh(i,j)/max(2.D0*sinh(min(kD(i,j),khmax)),eps)
          urms=fr(i,j)*abot
          fw=1.39D0*(Zob_my(i,j)/max(abot,eps))**0.52D0
          cff=0.5D0/sqrt(pi)*fw*urms**3
          frc(i,j)=wramp2*cff*kw(i,j)*inv_f(i,j)
          act(i,j)=inv_f(i,j)*(0.125D0*g*(wh(i,j)**2)
     &                         )
          stk(i,j) =act(i,j)*kw(i,j)*inv_d(i,j)
           kw(i,j) = kw(i,j)*rmask(i,j)
           kD(i,j) = kD(i,j)*rmask(i,j)
          act(i,j) =act(i,j)*rmask(i,j)
          stk(i,j) =stk(i,j)*rmask(i,j)
          brk(i,j) =brk(i,j)*rmask(i,j)
          frc(i,j) =frc(i,j)*rmask(i,j)
        enddo
      enddo
      do j=jstrR,jendR
        do i=istrR,iendR
          sup(i,j) =-0.125D0*(wh(i,j)**2)*kw(i,j)
     &              /max(sinh(2.D0*min(kD(i,j),khmax)),eps)
     &                                        *rmask(i,j)
        enddo
      enddo
      do j=jstrR,jendR
        do i=istrR,iendR
          wepb(i,j)=ebrk(i,j)
          cff = wramp2*frc(i,j)/kw(i,j)
          wepd(i,j)=cff*fr(i,j)
          wepb(i,j)=wepb(i,j)*rmask(i,j)
          wepd(i,j)=wepd(i,j)*rmask(i,j)
        enddo
      enddo
      do j=jstrR,jendR
        do i=istrR,iendR
          wwkx(i,j)=kw(i,j)*wdrx(i,j)
          wwke(i,j)=kw(i,j)*wdre(i,j)
        enddo
      enddo
      do j=jstrR,jendR
        do i=istr,iendR
          ust2d(i,j)  =0.5D0*(stk(i-1,j)*wdrx(i-1,j)+stk(i,j)*wdrx(i,j))
          brk2dx(i,j) =0.5D0*(brk(i-1,j)*wdrx(i-1,j)+brk(i,j)*wdrx(i,j))
          frc2dx(i,j) =0.5D0*(frc(i-1,j)*wdrx(i-1,j)+frc(i,j)*wdrx(i,j))
          ust2d(i,j)  = ust2d(i,j)*umask(i,j)
          brk2dx(i,j) =brk2dx(i,j)*umask(i,j)
          frc2dx(i,j) =frc2dx(i,j)*umask(i,j)
        enddo
      enddo
      do j=jstr,jendR
        do i=istrR,iendR
          vst2d(i,j)  =0.5D0*(stk(i,j-1)*wdre(i,j-1)+stk(i,j)*wdre(i,j))
          brk2de(i,j) =0.5D0*(brk(i,j-1)*wdre(i,j-1)+brk(i,j)*wdre(i,j))
          frc2de(i,j) =0.5D0*(frc(i,j-1)*wdre(i,j-1)+frc(i,j)*wdre(i,j))
          vst2d(i,j)  = vst2d(i,j)*vmask(i,j)
          brk2de(i,j) =brk2de(i,j)*vmask(i,j)
          frc2de(i,j) =frc2de(i,j)*vmask(i,j)
        enddo
      enddo
      do j=jstr-1,jend+1
        do i=istr-1,iend+1
          inv_f(i,j)=1.0D0/max(1.0D0-exp(-4.D0*kD(i,j)),eps)
          ust_ker=act(i,j)*kw(i,j)*inv_f(i,j)
          cff1 = exp( 2.D0*kw(i,j)*(z_w(i,j,0)+h(i,j)-Dstp(i,j)))
     &          -exp(-2.D0*kw(i,j)*(z_w(i,j,0)+h(i,j)+Dstp(i,j)))
          do k=1,N,+1
            wrk2(i,j,k)=1.D0/Hz(i,j,k)
            cff2 = exp( 2.D0*kw(i,j)*(z_w(i,j,k)+h(i,j)-Dstp(i,j)))
     &            -exp(-2.D0*kw(i,j)*(z_w(i,j,k)+h(i,j)+Dstp(i,j)))
            wrk1(i,j,k)=ust_ker*wrk2(i,j,k)*( cff2-cff1 )
            cff1=cff2
          enddo
        enddo
      enddo
      do k=1,N
        do j=jstrR,jendR
          do i=istr,iendR
            ust(i,j,k)=0.5D0*( wrk1(i-1,j,k)*wdrx(i-1,j)
     &                      +wrk1(i  ,j,k)*wdrx(i  ,j) )
          enddo
        enddo
        do j=jstr,jendR
          do i=istrR,iendR
            vst(i,j,k)=0.5D0*( wrk1(i,j-1,k)*wdre(i,j-1)
     &                      +wrk1(i,j  ,k)*wdre(i,j  ) )
          enddo
        enddo
      enddo
      do j=jstr,jend
        do i=istr,iend
          do k=1,N
            wrk4(k) =0.5D0*kw(i,j)*(
     &         wdrx(i,j)* ( u(i,j,k,nstp)+u(i+1,j,k,nstp) )
     &       + wdre(i,j)* ( v(i,j,k,nstp)+v(i,j+1,k,nstp) ) )
          enddo
          kvsurf =1.5D0*wrk4(N)-0.5D0*wrk4(N-1)
          do k=1,N-1
            wrk5(k) =2.0D0*(wrk4(k+1)-wrk4(k))/(Hz(i,j,k+1)+Hz(i,j,k))
          enddo
          wrk5(0) = wrk5(1)
          wrk5(N) = wrk5(N-1)
          do k=1,N
            wrk6(k) =wrk5(k)-wrk5(k-1)
          enddo
          cff3 = 0.0D0
          do k=1,N
            dd   = z_r(i,j,k)-z_w(i,j,N)
            cff3 = cff3 + wrk6(k)*(
     &             exp( 2.D0*kw(i,j)*(dd-Dstp(i,j)))
     &           + exp(-2.D0*kw(i,j)*(dd+Dstp(i,j))) )
          enddo
          cff1 =-2.0D0*exp(-2.D0*kD(i,j))*inv_f(i,j)*wrk5(N)
          cff2 = wrk5(0)/max(tanh(2.D0*kD(i,j)),eps)
          cff3 = cff3*inv_f(i,j)
          cff4 =-2.0D0*kw(i,j)*kvsurf
          umag =0.125D0*g*(wh(i,j)**2)/max(fr(i,j),eps)
          calP(i,j) = umag*inv_g*tanh(kD(i,j))
     &                    *( cff1+cff2+cff3+cff4 )
     &                                 *rmask(i,j)
          cff=0.0D0
          do k=1,N
            cff = cff +  wrk6(k)*
     &         ( exp( 2.D0*kw(i,j)*(z_w(i,j,N)-z_r(i,j,k)-Dstp(i,j)))
     &          -exp(-2.D0*kw(i,j)*(z_w(i,j,N)-z_r(i,j,k)+Dstp(i,j))) )
          enddo
          Kapsrf(i,j) = ( 0.5D0*Kapsrf(i,j)
     &                   +0.5D0*cff*umag*inv_f(i,j) )
     &                           *rmask(i,j)
        enddo
      enddo
      if (.not.WEST_INTER) then
        imin=IstrU-1
      else
        imin=IstrU
      endif
      if (.not.SOUTH_INTER) then
        jmin=JstrV-1
      else
        jmin=JstrV
      endif
      do k=1,N-1
        do j=JstrV-1,jend+1
          do i=IstrU-1,iend+1
            stk(i,j) = 2.0D0*(u(i,j,k+1,nstp)-u(i,j,k,nstp))
     &         /(z_r(i-1,j,k+1)+z_r(i,j,k+1)-z_r(i-1,j,k)-z_r(i,j,k))
            inv_f(i,j) = 2.0D0*(v(i,j,k+1,nstp)-v(i,j,k,nstp))
     &         /(z_r(i,j-1,k+1)+z_r(i,j,k+1)-z_r(i,j-1,k)-z_r(i,j,k))
          enddo
        enddo
        do j=jmin,jend
          do i=imin,iend
            wrk3(i,j,k) = 0.25D0*(wrk1(i,j,k)+wrk1(i,j,k+1))*
     &                   ( wdrx(i,j)*(stk(i,j)+stk(i+1,j))
     &                    +wdre(i,j)*(inv_f(i,j)+inv_f(i,j+1)) )
          enddo
        enddo
      enddo
      do j=jmin,jend
        do i=imin,iend
          wrk3(i,j,0)=0.D0
          wrk3(i,j,N)=2.D0*wrk3(i,j,N-1)-wrk3(i,j,N-2)
        enddo
      enddo
      do k=1,N
        do j=jmin,jend
          do i=imin,iend
            kvf(i,j,k)=0.5D0*( wrk3(i,j,k)+wrk3(i,j,k-1) )
     &                                     *rmask(i,j)
          enddo
        enddo
      enddo
      do j=jstrR,jendR
        do i=istrR,iendR
          cff1 = 0.5D0*wh(i,j)/max(1.D0-exp(-2.D0*kD(i,j)),eps)
          do k=0,N
            cff2 = cff1*( exp( kw(i,j)*(z_w(i,j,k)+h(i,j)-Dstp(i,j)))
     &                   -exp(-kw(i,j)*(z_w(i,j,k)+h(i,j)+Dstp(i,j))) )
            Akw(i,j,k) = 0.1D0*kw(i,j)*fr(i,j)*cff2**3
     &                               *rmask(i,j)
          enddo
        enddo
      enddo
      do j=jstr-1,jend+1
        do i=istr-1,iend+1
          inv_k =min(max(a_kv*wh(i,j),eps),Dstp(i,j))
          kbrk  =1.0D0/inv_k
          fn1   =exp(-2.D0*kbrk*Dstp(i,j))
          fb1   =2.D0*inv_k*fn1/(1.D0+fn1)
          intfb =inv_k - fb1
          fb0   =1.D0/intfb
          cff1    =ebrk(i,j)**c1o3
          cff2   =bconst*cff1*wh(i,j)
          do k=1,N,+1
            fn1 =exp(-2.D0*kbrk*(z_w(i,j,N)-z_w(i,j,k)))
            fb2 =2.D0*inv_k*fn1/(1.D0+fn1)
            fb =fb0*wrk2(i,j,k)*(fb2-fb1)
            wrk4(k) = cff2*fb*Dstp(i,j)
            fb1=fb2
          enddo
          do k=1,N-1
            Akb(i,j,k)=0.5D0*(wrk4(k)+wrk4(k+1))
          enddo
          Akb(i,j,0)=0.D0
          Akb(i,j,N)=max(1.5D0*wrk4(N)-0.5D0*wrk4(N-1),0.0D0)
          do k=0,N
            Akb(i,j,k)=Akb(i,j,k)*rmask(i,j)
          enddo
        enddo
      enddo
      do j=jstr-1,jend+1
        do i=istr-1,iend+1
          inv_k =min(max(a_brk*wh(i,j),eps),Dstp(i,j))
          kbrk  =1.0D0/inv_k
          fb1   =0.0D0
          fb0   =1.D0/(1.D0-exp(-2.D0*kbrk*Dstp(i,j)))
          do k=1,N,+1
            fb2 =exp( kbrk*(z_w(i,j,k)+h(i,j)-Dstp(i,j)))
     &          -exp(-kbrk*(z_w(i,j,k)+h(i,j)+Dstp(i,j)))
            fb =fb0*wrk2(i,j,k)*(fb2-fb1)
            wrk3(i,j,k) = fb*brk(i,j)
            fb1=fb2
          enddo
        enddo
      enddo
      do k=1,N
        do j=jstrR,jendR
          do i=istr,iendR
            brk3dx(i,j,k)=0.5D0*( wrk3(i-1,j,k)*wdrx(i-1,j)
     &                         +wrk3(i  ,j,k)*wdrx(i  ,j) )
     &                                         *umask(i,j)
          enddo
        enddo
        do j=jstr,jendR
          do i=istrR,iendR
            brk3de(i,j,k)=0.5D0*( wrk3(i,j-1,k)*wdre(i,j-1)
     &                         +wrk3(i,j  ,k)*wdre(i,j  ) )
     &                                         *vmask(i,j)
          enddo
        enddo
      enddo
      do j=jstr-1,jend+1
        do i=istr-1,iend+1
          abot  =wh(i,j)/max(2.D0*sinh(min(kD(i,j),khmax)),eps)
          delta =0.160455D0*((abot/Zob_my(i,j))**0.83D0)*Zob_my(i,j)
          inv_k =min(max(a_frc*delta,eps),Dstp(i,j))
          kfrc  =1.D0/inv_k
          fb1   =1.D0-exp(-2.D0*kfrc*Dstp(i,j))
          fb0   =1.D0/(exp(-2.D0*kfrc*Dstp(i,j))-1.0D0)
          do k=1,N,+1
            fb2 =exp( kfrc*(z_w(i,j,N)-z_w(i,j,k)-Dstp(i,j)))
     &          -exp(-kfrc*(z_w(i,j,N)-z_w(i,j,k)+Dstp(i,j)))
            fb =fb0*wrk2(i,j,k)*(fb2-fb1)
            wrk3(i,j,k) = fb*frc(i,j)
            fb1=fb2
          enddo
        enddo
      enddo
      do j=jstrR,jendR
        do i=istr,iendR
          do k=1,N
            frc3dx(i,j,k)=0.5D0*( wrk3(i-1,j,k)*wdrx(i-1,j)
     &                         +wrk3(i  ,j,k)*wdrx(i  ,j) )
     &                                         *umask(i,j)
          enddo
        enddo
      enddo
      do j=jstr,jendR
        do i=istrR,iendR
          do k=1,N
            frc3de(i,j,k)=0.5D0*( wrk3(i,j-1,k)*wdre(i,j-1)
     &                         +wrk3(i,j  ,k)*wdre(i,j  ) )
     &                                         *vmask(i,j)
          enddo
        enddo
      enddo
      call exchange_r2d_tile(istr,iend,jstr,jend,sup)
      call exchange_u2d_tile(istr,iend,jstr,jend,ust2d)
      call exchange_v2d_tile(istr,iend,jstr,jend,vst2d)
      call exchange_u2d_tile(istr,iend,jstr,jend,brk2dx)
      call exchange_v2d_tile(istr,iend,jstr,jend,brk2de)
      call exchange_u2d_tile(istr,iend,jstr,jend,frc2dx)
      call exchange_v2d_tile(istr,iend,jstr,jend,frc2de)
      call exchange_r2d_tile(istr,iend,jstr,jend,wepb)
      call exchange_r2d_tile(istr,iend,jstr,jend,wepd)
      call exchange_r2d_tile(istr,iend,jstr,jend,Kapsrf)
      call exchange_r2d_tile(istr,iend,jstr,jend,calP)
      call exchange_u3d_tile(istr,iend,jstr,jend,ust)
      call exchange_v3d_tile(istr,iend,jstr,jend,vst)
      call exchange_r3d_tile(istr,iend,jstr,jend,wst)
      call exchange_r3d_tile(istr,iend,jstr,jend,kvf)
      call exchange_w3d_tile(istr,iend,jstr,jend,Akb)
      call exchange_w3d_tile(istr,iend,jstr,jend,Akw)
      call exchange_w3d_tile(istr,iend,jstr,jend,E_pre)
      call exchange_u3d_tile (istr,iend,jstr,jend,brk3dx)
      call exchange_v3d_tile (istr,iend,jstr,jend,brk3de)
      call exchange_u3d_tile (istr,iend,jstr,jend,frc3dx)
      call exchange_v3d_tile (istr,iend,jstr,jend,frc3de)
      return
      end
      subroutine wstokes (tile)
      implicit none
      integer*4  LLm,Lm,MMm,Mm,N, LLm0,MMm0
      parameter (LLm0=41,   MMm0=42,   N=32)
      parameter (LLm=LLm0,  MMm=MMm0)
      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=2,  NP_ETA=2,  NNODES=NP_XI*NP_ETA)
      parameter (NPP=1)
      parameter (NSUB_X=1, NSUB_E=1)
      integer*4 NWEIGHT
      parameter (NWEIGHT=1000)
      integer*4 stdout, Np, padd_X,padd_E
      common /stdout/stdout
      parameter (Np=N+1)
      parameter (Lm=(LLm+NP_XI-1)/NP_XI, Mm=(MMm+NP_ETA-1)/NP_ETA)
      parameter (padd_X=(Lm+2)/2-(Lm+1)/2)
      parameter (padd_E=(Mm+2)/2-(Mm+1)/2)
      integer*4 NSA, N2d,N3d, size_XI,size_ETA
      integer*4 se,sse, sz,ssz
      parameter (NSA=28)
      parameter (size_XI=7+(Lm+NSUB_X-1)/NSUB_X)
      parameter (size_ETA=7+(Mm+NSUB_E-1)/NSUB_E)
      parameter (sse=size_ETA/Np, ssz=Np/size_ETA)
      parameter (se=sse/(sse+ssz), sz=1-se)
      parameter (N2d=size_XI*(se*size_ETA+sz*Np))
      parameter (N3d=size_XI*size_ETA*Np)
      real Vtransform
      parameter (Vtransform=2)
      integer*4   NT, itemp
      integer*4   ntrc_salt, ntrc_pas, ntrc_bio, ntrc_sed
      parameter (itemp=1)
      parameter (ntrc_salt=1)
      parameter (ntrc_pas=0)
      parameter (ntrc_bio=0)
      parameter (ntrc_sed=0)
      parameter (NT=itemp+ntrc_salt+ntrc_pas+ntrc_bio+ntrc_sed)
      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)
      integer*4 tile, trd, omp_get_thread_num
      real A2d(N2d,NSA,0:NPP-1), A3d(N3d,7,0:NPP-1)
      integer*4 B2d(N2d,0:NPP-1)
      common/private_scratch/ A2d,A3d
      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 wstokes_tile (istr,iend,jstr,jend, A2d(1,1,trd))
      return
      end
      subroutine wstokes_tile (istr,iend,jstr,jend,Wrk)
      implicit none
      integer*4  LLm,Lm,MMm,Mm,N, LLm0,MMm0
      parameter (LLm0=41,   MMm0=42,   N=32)
      parameter (LLm=LLm0,  MMm=MMm0)
      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=2,  NP_ETA=2,  NNODES=NP_XI*NP_ETA)
      parameter (NPP=1)
      parameter (NSUB_X=1, NSUB_E=1)
      integer*4 NWEIGHT
      parameter (NWEIGHT=1000)
      integer*4 stdout, Np, padd_X,padd_E
      common /stdout/stdout
      parameter (Np=N+1)
      parameter (Lm=(LLm+NP_XI-1)/NP_XI, Mm=(MMm+NP_ETA-1)/NP_ETA)
      parameter (padd_X=(Lm+2)/2-(Lm+1)/2)
      parameter (padd_E=(Mm+2)/2-(Mm+1)/2)
      integer*4 NSA, N2d,N3d, size_XI,size_ETA
      integer*4 se,sse, sz,ssz
      parameter (NSA=28)
      parameter (size_XI=7+(Lm+NSUB_X-1)/NSUB_X)
      parameter (size_ETA=7+(Mm+NSUB_E-1)/NSUB_E)
      parameter (sse=size_ETA/Np, ssz=Np/size_ETA)
      parameter (se=sse/(sse+ssz), sz=1-se)
      parameter (N2d=size_XI*(se*size_ETA+sz*Np))
      parameter (N3d=size_XI*size_ETA*Np)
      real Vtransform
      parameter (Vtransform=2)
      integer*4   NT, itemp
      integer*4   ntrc_salt, ntrc_pas, ntrc_bio, ntrc_sed
      parameter (itemp=1)
      parameter (ntrc_salt=1)
      parameter (ntrc_pas=0)
      parameter (ntrc_bio=0)
      parameter (ntrc_sed=0)
      parameter (NT=itemp+ntrc_salt+ntrc_pas+ntrc_bio+ntrc_sed)
      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)
      integer*4 istr,iend,jstr,jend, i,j,k
      real, dimension(Istr-2:Iend+2,0:N) :: Wrk
      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 sustr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real svstr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_sustr/sustr /forces_svstr/svstr
      real twox(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real twoy(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real tawx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real tawy(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_twox/twox /forces_twoy/twoy
      common /forces_tawx/tawx /forces_tawy/tawy
      real sustrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real svstrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /smsdat_sustrg/sustrg /smsdat_svstrg/svstrg
      real    sustrp(2), svstrp(2), sms_time(2)
      real    sms_cycle, sms_scale
      integer*4 itsms, sms_ncycle, sms_rec, lsusgrd
      integer*4 lsvsgrd,sms_tid, susid, svsid
      common /smsdat1/ sustrp, svstrp, sms_time
      common /smsdat2/ sms_cycle, sms_scale
      common /smsdat3/ itsms, sms_ncycle, sms_rec, lsusgrd
      common /smsdat4/ lsvsgrd,sms_tid, susid, svsid
      integer*4 lwgrd, wid
      common /smsdat5/ lwgrd, wid
      real bustr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real bvstr(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_bustr/bustr /forces_bvstr/bvstr
      real bustrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real bvstrg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /bmsdat_bustrg/bustrg /bmsdat_bvstrg/bvstrg
      real bms_tintrp(2), bustrp(2),    bvstrp(2), tbms(2)
      real bmsclen, bms_tstart, bms_tend,  tsbms, sclbms
      integer*4 itbms,      bmstid,busid, bvsid,     tbmsindx
      logical bmscycle,   bms_onerec,   lbusgrd,   lbvsgrd
      common /bmsdat1/bms_tintrp, bustrp,       bvstrp,    tbms
      common /bmsdat2/bmsclen, bms_tstart, bms_tend, tsbms, sclbms
      common /bmsdat3/itbms,      bmstid,busid, bvsid,     tbmsindx
      common /bmsdat4/bmscycle,   bms_onerec,   lbusgrd,   lbvsgrd
      real stflx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,NT)
      common /forces_stflx/stflx
      real shflx_rsw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_rsw/shflx_rsw
      real shflx_rlw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_rlw/shflx_rlw
      real shflx_lat(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_lat/shflx_lat
      real shflx_sen(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /frc_shflx_sen/shflx_sen
      real stflxg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2,NT)
      common /stfdat_stflxg/stflxg
      real stflxp(2,NT), stf_time(2,NT)
      real stf_cycle(NT), stf_scale(NT)
      integer*4 itstf(NT), stf_ncycle(NT), stf_rec(NT)
      integer*4 lstfgrd(NT), stf_tid(NT), stf_id(NT)
      common /stfdat1/ stflxp,  stf_time, stf_cycle, stf_scale
      common /stfdat2/ itstf, stf_ncycle, stf_rec, lstfgrd
      common /stfdat3/  stf_tid, stf_id
      real btflx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,NT)
      common /forces_btflx/btflx
      real tair(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real rhum(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real prate(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real radlw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real radsw(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wspd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real uwnd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real vwnd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /bulk_tair/ tair
      common /bulk_rhum/ rhum
      common /bulk_prate/ prate
      common /bulk_radlw/ radlw
      common /bulk_radsw/ radsw
      common /bulk_wspd/ wspd
      common /bulk_uwnd/ uwnd
      common /bulk_vwnd/ vwnd
      real tairg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real rhumg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real prateg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real radlwg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real radswg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real wspdg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real uwndg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      real vwndg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /bulkdat_tairg/tairg
      common /bulkdat_rhumg/rhumg
      common /bulkdat_prateg/prateg
      common /bulkdat_radlwg/radlwg
      common /bulkdat_radswg/radswg
      common /bulkdat_wspdg/wspdg
      common /bulk_uwndg/uwndg
      common /bulk_vwndg/vwndg
      real    tairp(2),rhump(2),pratep(2),radlwp(2),radswp(2)
      real    wspdp(2)
      real    uwndp(2),vwndp(2)
      real    bulk_time(2), bulk_cycle
      integer*4 tair_id,rhum_id,prate_id,radlw_id,radsw_id
      integer*4 ltairgrd,lrhumgrd,lprategrd,lradlwgrd,lradswgrd
      integer*4 wspd_id,lwspdgrd
      integer*4 uwnd_id,vwnd_id,luwndgrd,lvwndgrd
      integer*4 itbulk,bulk_ncycle,bulk_rec,bulk_tid
      integer*4 bulkunused
      common /bulkdat1_for/ tair_id,rhum_id,prate_id,radlw_id,radsw_id
      common /bulkdat1_grd
     &                 / ltairgrd,lrhumgrd,lprategrd,lradlwgrd,lradswgrd
      common /bulkdat1_tim/ itbulk, bulk_ncycle, bulk_rec, bulk_tid
      common /bulkdat1_uns/ bulkunused
      common /bulkdat1_wnd/ uwnd_id,vwnd_id,luwndgrd,lvwndgrd
      common /bulkdat1_wspd/ wspd_id,lwspdgrd
      common /bulkdat2_for/ tairp,rhump,pratep,radlwp,radswp
      common /bulkdat2_tim/ bulk_time, bulk_cycle
      common /bulkdat2_wnd/ uwndp,vwndp
      common /bulkdat2_wspd/ wspdp
      real srflx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_srflx/srflx
      real srflxg(-1:Lm+2+padd_X,-1:Mm+2+padd_E,2)
      common /srfdat_srflxg/srflxg
      real srflxp(2),srf_time(2)
      real srf_cycle, srf_scale
      integer*4 itsrf, srf_ncycle, srf_rec
      integer*4 lsrfgrd, srf_tid, srf_id
      common /srfdat1/ srflxp, srf_time, srf_cycle, srf_scale
      common /srfdat2/ itsrf,srf_ncycle,srf_rec,lsrfgrd,srf_tid,srf_id
      real wfrq(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_wfrq/wfrq
      real wwkx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_wkx/wwkx
      real wwke(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_wke/wwke
      real uorb(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real vorb(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_uorb/uorb /forces_vorb/vorb
      real whrm(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wepb(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wepd(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wdrx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real wdre(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_whrm/whrm /forces_wepb/wepb
     &       /forces_wdrx/wdrx /forces_wdre/wdre
     &       /forces_wepd/wepd
      real brk2dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real brk2de(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real ust2d(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real vst2d(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real frc2dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real frc2de(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real sup(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_brk2dx/brk2dx /forces_brk2de/brk2de
      common /forces_ust2d/ust2d /forces_vst2d/vst2d
      common /forces_frc2dx/frc2dx /forces_frc2de/frc2de
      common /forces_sup/sup
      real calP(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real Kapsrf(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /forces_calP/calP /forces_Kapsrf/Kapsrf
      real brk3dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real brk3de(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /forces_brk3dx/brk3dx /forces_brk3de/brk3de
      real frc3dx(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real frc3de(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      common /forces_frc3dx/frc3dx /forces_frc3de/frc3de
      real ust(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real vst(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real wst(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real kvf(-1:Lm+2+padd_X,-1:Mm+2+padd_E,N)
      real Akb(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      real Akw(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      real E_pre(-1:Lm+2+padd_X,-1:Mm+2+padd_E,0:N)
      common /forces_stokes/ust, vst, wst
      common /forces_kvf/kvf /forces_Akb/Akb /forces_Akw/Akw
      common /forces_E_pre/E_pre
      real Awave(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real Dwave(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      real Pwave(-1:Lm+2+padd_X,-1:Mm+2+padd_E)
      common /bbl_Awave/Awave /bbl_Dwave/Dwave /bbl_Pwave/Pwave
      real dt, dtfast, time, time2, time_start, tdays
      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,
     &                       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
      integer*4 numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
      logical ldefhis
      logical got_tini(NT)
      logical got_inibed(2)
      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
     &      , numthreads,     ntstart,   ntimes,  ninfo
     &      , nfast,  nrrec,     nrst,    nwrt
     &                                 , ntsavg,  navg
     &                      , got_tini
     &                      , got_inibed
     &                      , 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, ii,jj, p_W,p_E,p_S,p_N, p_SW,p_SE, p_NW,p_NE
      common /comm_setup/ mynode, 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
      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=0.0D0)
      logical mask_val
      parameter (mask_val = .true.)
      integer*4 IstrR,IendR,JstrR,JendR
      integer*4 IstrU
      integer*4 JstrV
      if (.not.WEST_INTER) then
        IstrR=Istr-1
        IstrU=Istr+1
      else
        IstrR=Istr
        IstrU=Istr
      endif
      if (.not.EAST_INTER) then
        IendR=Iend+1
      else
        IendR=Iend
      endif
      if (.not.SOUTH_INTER) then
        JstrR=Jstr-1
        JstrV=Jstr+1
      else
        JstrR=Jstr
        JstrV=Jstr
      endif
      if (.not.NORTH_INTER) then
        JendR=Jend+1
      else
        JendR=Jend
      endif
      do j=jstr,jend
        do i=istr,iend
          Wrk(i,0)=0.D0
        enddo
        do k=1,N,+1
          do i=istr,iend
            Wrk(i,k)=Wrk(i,k-1)-0.5D0*pm(i,j)*pn(i,j)*(
     &       (Hz(i+1,j  ,k)+Hz(i  ,j  ,k))*on_u(i+1,j  )*ust(i+1,j  ,k)
     &      -(Hz(i  ,j  ,k)+Hz(i-1,j  ,k))*on_u(i  ,j  )*ust(i  ,j  ,k)
     &      +(Hz(i  ,j+1,k)+Hz(i  ,j  ,k))*om_v(i  ,j+1)*vst(i  ,j+1,k)
     &      -(Hz(i  ,j  ,k)+Hz(i  ,j-1,k))*om_v(i  ,j  )*vst(i  ,j  ,k))
          enddo
        enddo
        do i=istr,iend
          wst(i,j,N)=+0.375D0*Wrk(i,N) +0.75D0*Wrk(i,N-1)
     &                              -0.125D0*Wrk(i,N-2)
        enddo
        do k=N-1,2,-1
          do i=istr,iend
            wst(i,j,k)=+0.5625D0*(Wrk(i,k  )+Wrk(i,k-1))
     &                 -0.0625D0*(Wrk(i,k+1)+Wrk(i,k-2))
          enddo
        enddo
        do i=istr,iend
          wst(i,j,  1)= -0.125D0*Wrk(i,2) +0.75D0*Wrk(i,1)
     &                                 +0.375D0*Wrk(i,0)
        enddo
      enddo
      if (.not.WEST_INTER) then
        do k=1,N
          do j=jstr,jend
            wst(istr-1,j,k)=wst(istr,j,k)
          enddo
        enddo
      endif
      if (.not.EAST_INTER) then
        do k=1,N
          do j=jstr,jend
            wst(iend+1,j,k)=wst(iend,j,k)
          enddo
        enddo
      endif
      if (.not.SOUTH_INTER) then
        do k=1,N
          do i=istr,iend
            wst(i,jstr-1,k)=wst(i,jstr,k)
          enddo
        enddo
      endif
      if (.not.NORTH_INTER) then
        do k=1,N
          do i=istr,iend
            wst(i,jend+1,k)=wst(i,jend,k)
          enddo
        enddo
      endif
      if (.not.WEST_INTER .and. .not.SOUTH_INTER) then
        do k=1,N
          wst(istr-1,jstr-1,k)=wst(istr,jstr,k)
        enddo
      endif
      if (.not.WEST_INTER .and. .not.NORTH_INTER) then
        do k=1,N
          wst(istr-1,jend+1,k)=wst(istr,jend,k)
        enddo
      endif
      if (.not.EAST_INTER .and. .not.SOUTH_INTER) then
        do k=1,N
          wst(iend+1, jstr-1,k)=wst(iend,jstr,k)
        enddo
      endif
      if (.not.EAST_INTER .and. .not.NORTH_INTER) then
        do k=1,N
          wst(iend+1,jend+1,k)=wst(iend,jend,k)
        enddo
      endif
      return
      end
