c23456789012345678901234567890123456789012345678901234567890123456789012


      SUBROUTINE assignx(var)

c     ******************************************************************
c
c     This routine assigns - U,V,W + their errors from previous inversion
c                          - p     + its   error  from database 
c
c     ******************************************************************


c     -------------
c     Declarations
c     -------------

c     ''''''''''''''''''''''
      implicit none
      include 'commonvar.f'
c     ''''''''''''''''''''''

c---- Argument variables

      character*7 var


c---- Local variables

      integer	i,j,k,idum

      real	ran1




c     ------------------------------------------
c     Option iflag = 2: Assign values of U,V,W,r
c     ------------------------------------------

c---- Test

c++
      if (var.eq.'var_den') then
c++


c---- Open files

      open (201,file='U.'//tag_UVW,status='old')
      open (202,file='V.'//tag_UVW,status='old')
      open (203,file='W.'//tag_UVW,status='old')
      open (204,file=      tag_rrr,status='old')


c---- Transport component U

      do k=ktot,1,-1 

         read(201,*)
         read(201,*)  
         read(201,*)
         do j=jtot,1,-1
            read(201,*) (U(i,j,k),i=1,itot+1)
         enddo 

      enddo


c---- Transport component V 

      do k=ktot,1,-1

         read(202,*)
         read(202,*) 
         read(202,*)
         do j=jtot+1,1,-1
            read(202,*) (V(i,j,k),i=1,itot)
         enddo

      enddo


c---- Transport component W 

      do k=ktot+1,1,-1

         read(203,*)
         read(203,*) 
         read(203,*)
         do j=jtot,1,-1
            read(203,*) (W(i,j,k),i=1,itot)
         enddo

      enddo


c---- Gridded densities 

      do k=ktot+1,1,-1

         read(204,*)
         read(204,*) 
         read(204,*)
         do j=jtot+1,1,-1
            read(204,*) (r(i,j,k),i=1,itot+1)
         enddo

      enddo


c     Include compression effects
c
c      do i=1,itot+1
c      do j=1,jtot+1
c      do k=1,ktot+1
c         if (flag_r(i,j,k)) r(i,j,k) =
c     >                      r(i,j,k) - compr*z_l(k)
c      enddo
c      enddo
c      enddo


c     Density anomaly

      do i=1,itot+1
      do j=1,jtot+1
      do k=1,ktot+1
         if (flag_r(i,j,k)) r(i,j,k) =
     >                      r(i,j,k) - rho_0 
      enddo
      enddo
      enddo


c---- Gridded d13C(DIC)
c
c      do k=ktot+1,1,-1
c
c         read(205,*)
c         read(205,*)
c         read(205,*)
c         do j=jtot+1,1,-1
c            read(205,*) (p(i,j,k),i=1,itot+1)
c         enddo
c
c      enddo
c
c
c---- Close files

      close (201)
      close (202)
      close (203)
      close (204) 


c---- End of test

c++
      endif
c++




c     ------------------------------------------
c     Option iflag = 2: Assign errors in U,V,W,r 
c     ------------------------------------------

c---- Test

c++
      if (var.eq.'err_den') then
c++


c---- Transport component U

      do i=1,itot+1
      do j=1,jtot+1
      do k=1,ktot+1
         e_U(i,j,k) = e_U0
      enddo
      enddo
      enddo


c---- Transport component V 

      do i=1,itot+1
      do j=1,jtot+1
      do k=1,ktot+1
         e_V(i,j,k) = e_V0
      enddo
      enddo
      enddo


c---- Transport component W 

      do i=1,itot+1
      do j=1,jtot+1
      do k=1,ktot+1
         e_W(i,j,k) = e_W0
      enddo
      enddo
      enddo


c---- Gridded densities 

      open (204,file='e_'//tag_rrr,status='old')

      do k=ktot+1,1,-1

         read(204,*)
         read(204,*) 
         read(204,*)
         do j=jtot+1,1,-1
            read(204,*) (e_r(i,j,k),i=1,itot+1)
         enddo

      enddo

      close (204)


c---- End of test

c++
      endif
c++




c     ------------------------------------------
c     Option iflag = 3: Assign values of U,V,W,p 
c     ------------------------------------------

c---- Test

c++
      if (var.eq.'var_pos') then
c++


c---- Open files

      open (201,file='U.'//tag_UVW,status='old')
      open (202,file='V.'//tag_UVW,status='old')
      open (203,file='W.'//tag_UVW,status='old')
      open (204,file=      tag_ppp,status='old')


c---- Transport component U

      do k=ktot,1,-1 

         read(201,*)
         read(201,*)  
         read(201,*)
         do j=jtot,1,-1
            read(201,*) (U(i,j,k),i=1,itot+1)
         enddo 

      enddo


c---- Transport component V 

      do k=ktot,1,-1

         read(202,*)
         read(202,*) 
         read(202,*)
         do j=jtot+1,1,-1
            read(202,*) (V(i,j,k),i=1,itot)
         enddo

      enddo


c---- Transport component W 

      do k=ktot+1,1,-1

         read(203,*)
         read(203,*) 
         read(203,*)
         do j=jtot,1,-1
            read(203,*) (W(i,j,k),i=1,itot)
         enddo

      enddo


c---- Gridded d18Oc or d13C(DIC) derived from benthic d18O or d13C data
c
c     Note
c     ----
c     1) //tag_ppp is a file produced by objective maping from another program
c     2) It includes gridded d18Oc or d13C(DIC)

      do k=ktot+1,1,-1

         read(204,*)
         read(204,*) 
         read(204,*)
         do j=jtot+1,1,-1
            read(204,*) (p(i,j,k),i=1,itot+1)
         enddo

      enddo


c---- Close files

      close (201)
      close (202)
      close (203)
      close (204) 


c---- End of test

c++
      endif
c++




c     ------------------------------------------
c     Option iflag = 3: Assign errors in U,V,W,p 
c     ------------------------------------------

c---- Test

c++
      if (var.eq.'err_pos') then
c++


c---- Transport component U

      do i=1,itot+1
      do j=1,jtot+1
      do k=1,ktot+1
         e_U(i,j,k) = e_U0
      enddo
      enddo
      enddo


c---- Transport component V 

      do i=1,itot+1
      do j=1,jtot+1
      do k=1,ktot+1
         e_V(i,j,k) = e_V0
      enddo
      enddo
      enddo


c---- Transport component W 

      do i=1,itot+1
      do j=1,jtot+1
      do k=1,ktot+1
         e_W(i,j,k) = e_W0
      enddo
      enddo
      enddo


c---- Gridded d18Oc or d13C(DIC) derived from benthic d18O or d13C data 
c
c     Note
c     ----
c     1) 'e_'//tag_ppp is a file produced by objective mapping from another program
c     2) It includes error in gridded d18Oc or d13C(DIC)  

      open (204,file='e_'//tag_ppp,status='old')

      do k=ktot+1,1,-1

         read(204,*)
         read(204,*)  
         read(204,*)
         do j=jtot+1,1,-1
            read(204,*) (e_p(i,j,k),i=1,itot+1)
         enddo

      enddo

      close (204)


c---- End of test

c++
      endif
c++




c     ------------------
c     End of subroutine 
c     ------------------

      return
 
      end

c     ******************************************************************

