      subroutine bulk_flux (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 bulk_flux_tile (Istr,Iend,Jstr,Jend,
     &                     A2d(1,1,trd),A2d(1,2,trd))
      return
      end
      subroutine bulk_flux_tile (Istr,Iend,Jstr,Jend, aer,cer)
      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 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 i,j, Istr,Iend,Jstr,Jend,
     &        imin,imax,jmin,jmax
      integer*4 IterMax,Iter
      real a,cff
      real rho0i,cpi
      real TseaC,TseaK,Qsea
      real TairC,TairK,rhoAir,Qair
      real Q,RH,VisAir,Hlv
      real delW,delT,delQ
      real u10,Zo10,Cd10,Ch10,Ct10,Cd
      real Ct,CC,Ri,Ribcu,Zetu,L10
      real Wstar,Tstar,Qstar
      real ZoW,ZoT,ZoT10,ZoQ,ZoL,L,Rr,Bf
      real Wpsi,Tpsi,Qpsi
      real wspd0,Wgus,charn
      real bulk_psiu,bulk_psit
      real hfsen,hflat,hflw,upvel,evap
      real blk_Rgas,blk_ZW,blk_ZT,blk_ZQ,blk_beta,blk_Cpa,blk_Zabl
      parameter (blk_Rgas=287.1D0)
      parameter (blk_ZW=10.0D0)
      parameter (blk_ZT=10.0D0)
      parameter (blk_ZQ=10.0D0)
      parameter (blk_Zabl=600.0D0)
      parameter (blk_beta=1.2D0)
      parameter (blk_Cpa=1004.67D0)
      real emiss_lw,SigmaSB
      parameter (emiss_lw=0.985D0)
      parameter (SigmaSB=5.6697D-8)
      real rhow,patm
      parameter (rhow=1000.0D0)
      parameter (patm=1010.0D0)
      real eps,r3
      parameter (eps=1.D-20)
      parameter (r3=1.0D0/3.0D0)
      real aer(Istr-2:Iend+2,Jstr-2:Jend+2),
     &     cer(Istr-2:Iend+2,Jstr-2:Jend+2)
      if (.not.WEST_INTER) then
        imin=Istr-1
      else
        imin=Istr-2
      endif
      if (.not.EAST_INTER) then
        imax=Iend+1
      else
        imax=Iend+2
      endif
      if (.not.SOUTH_INTER) then
        jmin=Jstr-1
      else
        jmin=Jstr-2
      endif
      if (.not.NORTH_INTER) then
        jmax=Jend+1
      else
        jmax=Jend+2
      endif
      rho0i=1.0D0/rho0
      cpi=1.0D0/cp
      do j=jmin,jmax
        do i=imin,imax
          wspd0=wspd(i,j)
          TairC=tair(i,j)
          TairK=TairC+273.16D0
          TseaC=t(i,j,N,nrhs,itemp)
          TseaK=TseaC+273.16D0
          RH=rhum(i,j)
          hflw=radlw(i,j)
     &        - emiss_lw*rho0i*cpi*SigmaSB*TseaK*TseaK*TseaK*TseaK
          cff=(1.0007D0+3.46D-6*patm)*6.1121D0*
     &        exp(17.502D0*TairC/(240.97D0+TairC))
          Qair=0.62197D0*(cff/(patm-0.378D0*cff))
          if (RH.lt.2.0D0) then
            cff=cff*RH
            Q=0.62197D0*(cff/(patm-0.378D0*cff))
          else
            Q=RH/1000.0D0
          endif
          cff=(1.0007D0+3.46D-6*patm)*6.1121D0*
     &            exp(17.502D0*TseaC/(240.97D0+TseaC))
          cff=cff*0.98D0
          Qsea=0.62197D0*(cff/(patm-0.378D0*cff))
          rhoAir=patm*100.0D0/(blk_Rgas*TairK*
     &                              (1.0D0+0.61D0*Q))
          VisAir=1.326D-5*(1.0D0+TairC*(6.542D-3+TairC*
     &               (8.301D-6-4.84D-9*TairC)))
          Hlv=(2.501D0-0.00237D0*TseaC)*1.0D+6
          Wgus=0.5D0
          delW=SQRT(wspd0*wspd0+Wgus*Wgus)
          delQ=Qsea-Q
          delT=TseaC-TairC
          ZoW=0.0001D0
          u10=delW*LOG(10.0D0/ZoW)/LOG(blk_ZW/ZoW)
          Wstar=0.035D0*u10
          Zo10=0.011D0*Wstar*Wstar/g+0.11D0*VisAir/Wstar
          Cd10=(vonKar/LOG(10.0D0/Zo10))**2
          Ch10=0.00115D0
          Ct10=Ch10/sqrt(Cd10)
          ZoT10=10.0D0/exp(vonKar/Ct10)
          Cd=(vonKar/LOG(blk_ZW/Zo10))**2
          Ct=vonKar/LOG(blk_ZT/ZoT10)
          CC=vonKar*Ct/Cd
          Ribcu=-blk_ZW/(blk_Zabl*0.004D0*blk_beta**3)
          Ri=-g*blk_ZW*(delT+0.61D0*TairK*delQ)/
     &          (TairK*delW*delW)
          if (Ri.lt.0.0D0) then
            Zetu=CC*Ri/(1.0D0+Ri/Ribcu)
          else
            Zetu=CC*Ri/(1.0D0+3.0D0*Ri/CC)
          endif
          L10=blk_ZW/Zetu
          if (Zetu.gt.50.0D0) then
            IterMax=1
          else
            IterMax=3
          endif
          Wstar=delW*vonKar/(LOG(blk_ZW/Zo10)-
     &                             bulk_psiu(blk_ZW/L10,pi))
          Tstar=-delT*vonKar/(LOG(blk_ZT/ZoT10)-
     &              bulk_psit(blk_ZT/L10,pi))
          Qstar=-delQ*vonKar/(LOG(blk_ZQ/ZoT10)-
     &              bulk_psit(blk_ZQ/L10,pi))
          if (delW.gt.18.0D0) then
            charn=0.018D0
          elseif ((10.0D0.lt.delW).and.(delW.le.18.0D0)) then
            charn=0.011D0+0.125D0*(0.018D0-0.011D0)*(delW-10.D0)
          else
            charn=0.011D0
          endif
        do Iter=1,IterMax
          ZoW=charn*Wstar*Wstar/g+0.11D0*VisAir/(Wstar+eps)
          Rr=ZoW*Wstar/VisAir
          ZoQ=MIN(1.15D-4,5.5D-5/Rr**0.6D0)
          ZoT=ZoQ
          ZoL=vonKar*g*blk_ZW*
     &             (Tstar*(1.0D0+0.61D0*Q)+0.61D0*TairK*Qstar)/
     &             (TairK*Wstar*Wstar*(1.0D0+0.61D0*Q)+eps)
          L=blk_ZW/(ZoL+eps)
          Wpsi=bulk_psiu(ZoL,pi)
          Tpsi=bulk_psit(blk_ZT/L,pi)
          Qpsi=bulk_psit(blk_ZQ/L,pi)
          Wstar=MAX(eps,delW*vonKar/(LOG(blk_ZW/ZoW)-Wpsi))
          Tstar=-delT*vonKar/(LOG(blk_ZT/ZoT)-Tpsi)
          Qstar=-delQ*vonKar/(LOG(blk_ZQ/ZoQ)-Qpsi)
          Bf=-g/TairK*Wstar*(Tstar+0.61D0*TairK*Qstar)
          if (Bf.gt.0.0D0) then
            Wgus=blk_beta*(Bf*blk_Zabl)**r3
          else
            Wgus=0.2D0
          endif
          delW=SQRT(wspd0*wspd0+Wgus*Wgus)
        enddo
          wspd0=SQRT(wspd0*wspd0+Wgus*Wgus)
          Cd=Wstar*Wstar/(wspd0*wspd0+eps)
          hfsen=-blk_Cpa*rhoAir*Wstar*Tstar
          hflat=-Hlv*rhoAir*Wstar*Qstar
          upvel=-1.61D0*Wstar*Qstar-(1.0D0+1.61D0*Q)*Wstar*Tstar/TairK
          hflat=hflat+rhoAir*Hlv*upvel*Q
          hflat=-hflat*rho0i*cpi
          hfsen=-hfsen*rho0i*cpi
          stflx(i,j,itemp)=srflx(i,j)+hflw+hflat+hfsen
          evap=-cp*hflat/Hlv
          stflx(i,j,isalt)=(evap-prate(i,j))*t(i,j,N,nrhs,isalt)
          stflx(i,j,itemp)=stflx(i,j,itemp)*rmask(i,j)
          stflx(i,j,isalt)=stflx(i,j,isalt)*rmask(i,j)
          aer(i,j)=rhoAir*wspd0*rho0i
          cer(i,j)=Cd
          shflx_rsw(i,j)=srflx(i,j)
          shflx_lat(i,j)=hflat
          shflx_sen(i,j)=hfsen
          shflx_rlw(i,j)=hflw
        enddo
      enddo
      do j=jmin,jmax
        do i=imin+1,imax
          a=0.5D0*(aer(i-1,j)+aer(i,j))
          cff=0.5D0*(cer(i-1,j)+cer(i,j))
          sustr(i,j)=a*cff*uwnd(i,j)
          sustr(i,j)=sustr(i,j)*umask(i,j)
        enddo
      enddo
      do j=jmin+1,jmax
        do i=imin,imax
          a=0.5D0*(aer(i,j-1)+aer(i,j))
          cff=0.5D0*(cer(i,j-1)+cer(i,j))
          svstr(i,j)=a*cff*vwnd(i,j)
          svstr(i,j)=svstr(i,j)*vmask(i,j)
        enddo
      enddo
      return
      end
      FUNCTION bulk_psiu (ZoL, pi)
      real bulk_psiu
      real ZoL, pi
      real r3
      parameter (r3=1.0D0/3.0D0)
      real Fw, cff, psic, psik, x, y
      if (ZoL.lt.0.0D0) then
        x=(1.0D0-15.0D0*ZoL)**0.25D0
        psik=2.0D0*LOG(0.5D0*(1.0D0+x))+LOG(0.5D0*(1.0D0+x*x))-
     &       2.0D0*ATAN(x)+0.5D0*pi
        cff=SQRT(3.0D0)
        y=(1.0D0-10.15D0*ZoL)**r3
        psic=1.5D0*LOG(r3*(1.0D0+y+y*y))-
     &       cff*ATAN((1.0D0+2.0D0*y)/cff)+pi/cff
        cff=ZoL*ZoL
        Fw=cff/(1.0D0+cff)
        bulk_psiu=(1.0D0-Fw)*psik+Fw*psic
      else
        cff=MIN(50.0D0,0.35D0*ZoL)
        bulk_psiu=-((1.0D0+ZoL)+0.6667D0*(ZoL-14.28D0)/
     &            EXP(cff)+8.525D0)
      endif
      return
      END FUNCTION bulk_psiu
      FUNCTION bulk_psit (ZoL, pi)
      real bulk_psit
      real ZoL, pi
      real r3
      parameter (r3=1.0D0/3.0D0)
      real Fw, cff, psic, psik, x, y
      if (ZoL.lt.0.0D0) then
        x=(1.0D0-15.0D0*ZoL)**0.5D0
        psik=2.0D0*LOG(0.5D0*(1.0D0+x))
        cff=SQRT(3.0D0)
        y=(1.0D0-34.15D0*ZoL)**r3
        psic=1.5D0*LOG(r3*(1.0D0+y+y*y))-
     &       cff*ATAN((1.0D0+2.0D0*y)/cff)+pi/cff
        cff=ZoL*ZoL
        Fw=cff/(1.0D0+cff)
        bulk_psit=(1.0D0-Fw)*psik+Fw*psic
      else
        cff=MIN(50.0D0,0.35D0*ZoL)
        bulk_psit=-((1.0D0+2.0D0*ZoL)**1.5D0+
     &            0.6667D0*(ZoL-14.28D0)/EXP(cff)+8.525D0)
      endif
      return
      END FUNCTION bulk_psit
