C** adjust_soln
C
C Compute the optimal step size and then adjust the boundary conditions
C and errors accordingly.  
C
C Output: dbc_best(i,k): the present best elevation boundary conditions
C         uerr_best, verr_best: the present best velocity errors
C
C CGH  Nov 1998 
C      16 Jan 1999 
C      20 Aug 1999: added Wrho_t cgh  
C      12 Nov 2002: add elevation errors
C

      subroutine adjust_soln(iter)
	 integer iter

	 include 'CASCO.DIM' 
	 include 'errors.h'
	 include 'deltabcs.h'
 	 include 'covariance.h' 
 	 include 'wrho.h'
	 include 'wind.h' 

         real step
	 integer nobs, nr
         integer n_eobs
	 real  size1,size2,size3
	 real  size1u,size2u,size3u
	 real  size1v,size2v,size3v
	 real  size1e,size2e,size3e
	 real   scratch1(nobsdim)
	 real   scratch3(nelevobsdim)
	 real   scratch2(nrdim,ntbcdim)
         integer i, k 
	 real   costreduce

         real   stepold

C functions
	 real   sup_norm2d, sup_norm1d
	 real   stepsize5 
	 real   stepsize4 
	 real   exp_costreduce2
	 integer nobservations, nelev_bcs
	 integer nelevobservations

	 nobs = nobservations()
	 n_eobs = nelevobservations()
	 nr   = nelev_bcs()

         write(*,*)'ADJUST_SOLN: nobs, n_eobs ',nobs, n_eobs
         write(*,*)'ADJUST_SOLN: nr, ntbc ',nr,ntbc
	 write(*,*)nobsdim, nelevobsdim

         step = stepsize5(uerr_best,verr_best,uerror,verror,w_delta,
     $                    nobs,eerr_best,eerror,w_eps,n_eobs,
     $                    dbc_best, dbc, Wrho,Wrho_t,IQrho, 
     $                    JQrho, nr,ntbc,nrdim,ntbcdim,
     $                    nwrhodim, scratch1,scratch3,scratch2)
	 write(*,*)'STEP: ',iter, step

         size1 = sup_norm2d(dbc_best,nr,ntbc,NRDIM,NTBCDIM)
         size2 = sup_norm2d(dbc,nr,ntbc,NRDIM,NTBCDIM)


C adjust the boundary conditions 
         do k = 1, ntbc
            do i = 1, nr
               dbc_best(i,k) = (1.0-step)*dbc_best(i,k) + step*dbc(i,k) 
               dbc(i,k) = 0.0  
            enddo
         enddo
	 
c 
c adjust lagged wind response coeficent estimates
c

        do L=1,w_nseries
	 L0=(L-1)*nlags
         do k = 1, nlags
          do i = 1, nr
           dbcA_best(i,k+L0) = (1.0-step)*dbcA_best(i,k+L0) + 
     &                                     step*dbcA(i,k+L0) 
           dbcA(i,k+L0) = 0.0  
          enddo
         enddo
	enddo
         size3 = sup_norm2d(dbc_best,nr,ntbc,NRDIM,NTBCDIM)
	 write(*,*)'ADJUST: size1,size2,size3 ', size1,size2,size3
	
C adjust the velocity errors
          size1u = sup_norm1d(uerr_best,nobs)
          size2u = sup_norm1d(uerror,nobs)
          size1v = sup_norm1d(verr_best,nobs)
          size2v = sup_norm1d(verror,nobs)
            do i = 1, nobs
               uerr_best(i) = (1.0-step)*uerr_best(i) + step*uerror(i) 
               verr_best(i) = (1.0-step)*verr_best(i) + step*verror(i) 
            enddo

          size3u = sup_norm1d(uerr_best,nobs)
          size3v = sup_norm1d(verr_best,nobs)
	  write(*,*)'ADJUST: uerrors size ', size1u,size2u,size3u
	  write(*,*)'ADJUST: verrors size ', size1v,size2v,size3v

C adjust the elevation errors
          size1e = sup_norm1d(eerr_best,n_eobs)
          size2e = sup_norm1d(eerror,n_eobs)
          write(*,*)'ADJUST: n_eobs', n_eobs

          do i = 1, n_eobs
             eerr_best(i) = (1.0-step)*eerr_best(i) + step*eerror(i) 
          enddo

          size3e = sup_norm1d(eerr_best,n_eobs)
	  write(*,*)'ADJUST: eerrors size ', size1e,size2e,size3e

C

      return 
      end
