
!    <@(#)	 update.F	4.8 3/28/94>
      subroutine UPDATE( len, km, nx, nwater, nland, c2dt,
     &                   Hp, Up, Vp, Tp, Sp, 
     &                   H,  U,  V,  T,  S,  
     &                   Hn, Un, Vn, Tn, Sn, 
     &                   Bxx, Bn,
     &                   ieast, inorth, Bu,
     &                  Area_h,Area_h_r,Area_u_r,
     &                  Hd,Ud,Vd,Td,Sd)
      implicit none


      integer len
      integer km
      integer nx
      integer nwater
      integer nland
      real c2dt
      real Hp(LEN,KM)
      real Up(LEN,KM)
      real Vp(LEN,KM)
      real Tp(LEN,KM)
      real Sp(LEN,KM)
      real H(LEN,KM)
      real U(LEN,KM)
      real V(LEN,KM)
      real T(LEN,KM)
      real S(LEN,KM)
      real Hn(LEN,KM)
      real Un(LEN,KM)
      real Vn(LEN,KM)
      real Tn(LEN,KM)
      real Sn(LEN,KM)
      real Bxx(LEN,KM)
      real Bn(LEN,KM)
      integer ieast(LEN)
      integer inorth(LEN)
c      integer Bh(LEN)
      integer Bu(LEN)
      real Area_h(LEN)
      real Area_h_r(LEN)
      real Area_u_r(LEN)
      real Hd(LEN,KM)
      real Ud(LEN,KM)
      real Vd(LEN,KM)
      real Td(LEN,KM)
      real Sd(LEN,KM)
 
c==> Local Variables
      integer i,k,m
      
      real hu
      real hun
      real hunr

      real Wk1(LEN)
      real Wk2(LEN)
      real Hap(LEN)
      real Han(LEN)
      
      real        TIMEFAC  ! Time filtering coefficient
      parameter ( TIMEFAC = 0.05 )
      
      real BYNCY
CFPP$ EXPAND (BYNCY) R

c---------------------------------------------------------------------

      do i = nwater+1, nwater+nland
       Wk1(i) = 0.
       Wk2(i) = 0.
       Hap(i) = 0.
       Han(i) = 0.
      end do
      

      do k=1,KM

        do i=1,nwater
         Hd(i,k) = Hp(i,k) + Hd(i,k) * Area_h_r(i) * c2dt
         Ud(i,k) = Ud(i,k) * Area_u_r(i) * c2dt
         Vd(i,k) = Vd(i,k) * Area_u_r(i) * c2dt
         Td(i,k) = (Tp(i,k)*Hp(i,k) 
     &              + Td(i,k) * Area_h_r(i) * c2dt  ) / Hd(i,k)
         Sd(i,k) = (Sp(i,k)*Hp(i,k) 
     &              +Sd(i,k) * Area_h_r(i) * c2dt  ) / Hd(i,k)

         Hap(i)  = Hp(i,k) * Area_h(i)
         Han(i)  = Hd(i,k) * Area_h(i)

       end do
c       do m=1,nx
c        do i=1,nwater
c         Xd(i,k,m) = (Xp(i,k,1,m)*Hp(i,k) 
c     &              + Xd(i,k,m) * Area_h_r(i) * c2dt  ) / Hd(i,k)
c        enddo
c       enddo

       do i=1,nwater
         Wk1(i) = ( Hap(i) + Hap(ieast(i)) ) * .25 
         Wk2(i) = ( Han(i) + Han(ieast(i)) ) * .25 
       end do

       do i=1,nwater
         hu   = ( Wk1(i) + Wk1( inorth(i) ) ) * Area_u_r(i)
         hun  = ( Wk2(i) + Wk2( inorth(i) ) ) * Area_u_r(i)
         hunr = Bu(i) / ( hun + 1.0E-36 )
         Ud(i,k)  = ( Up(i,k)*hu + Ud(i,k) ) * hunr
         Vd(i,k)  = ( Vp(i,k)*hu + Vd(i,k) ) * hunr


! Time filtering on mass, heat momentum
        
        
         
         H(i,k) = H(i,k) * ( 1. - TIMEFAC )
     &          +  ( Hp(i,k)  + Hd(i,k) ) * TIMEFAC * 0.5
         
         U(i,k) = U(i,k) * ( 1. - TIMEFAC )
     &          +  ( Up(i,k) + Ud(i,k) ) * TIMEFAC * 0.5
     
         V(i,k) = V(i,k) * ( 1. - TIMEFAC )
     &          +  ( Vp(i,k) + Vd(i,k) ) * TIMEFAC * 0.5
     
         T(i,k) = T(i,k) * ( 1. - TIMEFAC )
     &               +  ( Tp(i,k) + Td(i,k) ) * TIMEFAC * 0.5
     
         S(i,k) = S(i,k) * ( 1. - TIMEFAC )
     &               +  ( Sp(i,k) + Sd(i,k) ) * TIMEFAC * 0.5


         Hn(i,k) = Hd(i,k)
         Un(i,k) = Ud(i,k)
         Vn(i,k) = Vd(i,k)
         Tn(i,k) = Td(i,k)
         Sn(i,k) = Sd(i,k)
          
         
         Bxx(i,k ) = BYNCY( T(i,k)  , S(i,k),   0.0 )
         Bn(i,k )  = BYNCY( Tn(i,k ), Sn(i,k), 0.0 )
         
       end do ! i
        
c        do m=1,nx
c        do i=1,nwater
c         X(i,k,1,m) = X(i,k,1,m) * ( 1. - TIMEFAC )
c     &               +  ( Xp(i,k,1,m) + Xd(i,k,m) ) * TIMEFAC * 0.5
c         Xn(i,k,1,m) = Xd(i,k,m)
c        enddo
c        enddo
        
      end do ! k

      end
