      parameter(nx1=168,ny1=180,nx=nx1,ny=ny1,imt=nx1,jmt=ny1,km=40)
      parameter(nbio=11)
      parameter(nyear1=2012,nyear2=2012,nday=365,nmon=12)

      dimension heff(imt,jmt),aiday(imt,jmt),snow(imt,jmt)
      dimension hiday(imt,jmt),hlate(imt,jmt),hsens(imt,jmt)
      dimension lwnet(imt,jmt),swnet(imt,jmt),sursalt(imt,jmt)
      dimension ostrx(imt,jmt),ostry(imt,jmt)
      dimension uice(imt,jmt),vice(imt,jmt),ssh(imt,jmt)
      dimension uo(imt,jmt,km),vo(imt,jmt,km),p_e(imt,jmt)
      dimension bio(imt,jmt,km,nbio),tair(imt,jmt)
      dimension zoo3(imt,jmt,km,3),to(imt,jmt,km),so(imt,jmt,km)
      dimension zoo3_eom(imt,jmt,km,3),winx(imt,jmt),winy(imt,jmt)
      dimension clon(imt,jmt),clat(imt,jmt),kmt(imt,jmt)
      dimension ulat(imt,jmt),ulon(imt,jmt),HTN(imt,jmt),HTE(imt,jmt)
     &,HUS(imt,jmt),HUW(imt,jmt),angle(imt,jmt),dxt(imt,jmt)
     &,dyt(imt,jmt)
      dimension dz(km),ZDZ (KM),ZDZZ(KM+1)
      character *80 fopen(5), f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12
      character *4 cyear(1900:2100),cyear1(1900:2100)
      character *12 char
      integer SLEN

c      f1='/export/miz5/zhang/output/aBering/40/2t3/hiday_168_180.H'  ! daily ice thickness (m)
      f1='hiday_168_180.H'  ! daily ice thickness (m)
      f2='aiday_168_180.H'  ! daily ice concentration (fraction)
c      f3='hlateday_168_180.H'  ! daily latent heat flux (W/m*m)
      f3='air_168_180.H'    ! daily NCEP/NCAR surface air temperature (K)
c      f4='hsensday_168_180.H'  ! daily sensible heat flux (W/m*m)
      f4='ssh_168_180.H'    ! daily sea surface height (cm)
c      f5='lwnetday_168_180.H'  ! daily net longwave radiation (W/m*m)
c      f5='p-e_168_180.H'    ! monthly NCEP/NCAR P minus E (m/s)
cc      f6='swnetday_168_180.H'  ! daily net shortwave radiation (W/m*m)
cc      f7='sursaltday_168_180.H'  ! daily ocean surface gain of salt in 0.001psu/s
c      f8='ostrday_168_180.H'  ! daily ocean surface stress in dyn/cm**2
      f8='sur_wind_168_180.H'  ! daily NCEP/NCAR surface winds (m/s)
      f9='to_168_180.H'       ! daily ocean temperature (C)
      f10='so_168_180.H'       ! daily ocean salinity (psu)
      f11='uo_168_180.H'       ! daily ocean velocity (cm/s)
      f12='uiday_168_180.H'    ! daily sea ice velocity (m/s)

c read lon and lat for scalar fields (like sea ice thickness and concentration)
      open(20,file='grid.dat.rot_168_180')
      read(20,'(10f8.2)') ((clon(i,j),i=1,nx1),j=1,ny1)
      read(20,'(10f8.2)') ((clat(i,j),i=1,nx1),j=1,ny1)
      close(20)

c read lon and lat for vector fields (like sea ice and ocean veclocities)
      open(24,file='grid.dat.pop_168_180')
        read(24,'(10f8.2)') ((ulat(i,j),i=1,nx),j=1,ny)
        read(24,'(10f8.2)') ((ulon(i,j),i=1,nx),j=1,ny)
c HTN, HTE are lengths of the northern and eastern sides of a scaler grid cell in km, HTN*HTE is the area of a scaler grid cell in km**2
c HUS, HUW are lengths of the southern and western sides of a vector grid cell in km
        read(24,'(10f8.2)') ((HTN  (i,j),i=1,nx),j=1,ny) 
        read(24,'(10f8.2)') ((HTE  (i,j),i=1,nx),j=1,ny)
        read(24,'(10f8.2)') ((HUS  (i,j),i=1,nx),j=1,ny)
        read(24,'(10f8.2)') ((HUW  (i,j),i=1,nx),j=1,ny)
c angle is the angle between latitude line and  grid cell x-coordinate line, needed for vector rotation for plotting the vectors in spherical lat-lon coordinate system
c** Do not use the angle variable any more because the rotation has already be made **
        read(24,'(10f8.2)') ((angle(i,j),i=1,nx),j=1,ny)
      close(24)

c read model grid mask with ocean levels; ocean: levels > 0, land: levels = 0
      open(20,file='levels_40_t_168_180')
      read(20,'(168i2)') kmt
      close(20)

c read ocean level thickness dz(k) in cm
      open(20,file='dz.dta40')
      do k=1,km
        read(20,*)dz(k)
        dz(k)=dz(k)*0.01
      end do
      ZDZ(1)=DZ(1)
      do k=2,km
        ZDZ(K)=ZDZ(K-1)+DZ(K)
      end do
      do k=1,km
        zdzz(k)=zdz(k)-0.5*dz(k)
      end do
      close(20)

      do 999 iyear=nyear1,nyear2

      write(unit=cyear(iyear),fmt='(i4)') iyear

      i=slen(f1)
      open(1,file=f1(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')

      i=slen(f2)
      open(2,file=f2(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')

c      go to 4444

      i=slen(f3)
      open(3,file=f3(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')

      i=slen(f4)
      open(4,file=f4(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')
 
c      i=slen(f5)
c      open(5,file=f5(1:i)//cyear(iyear)
c     &,access='direct',form='unformatted',recl=nx1*ny1*4
c     &,status='old')

c      i=slen(f6)
c      open(6,file=f6(1:i)//cyear(iyear)
c     &,access='direct',form='unformatted',recl=nx1*ny1*4
c     &,status='old')

c      i=slen(f7)
c      open(7,file=f7(1:i)//cyear(iyear)
c     &,access='direct',form='unformatted',recl=nx1*ny1*4
c     &,status='old')

      i=slen(f8)
      open(8,file=f8(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')
4444  continue

      i=slen(f9)
      open(9,file=f9(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')

      i=slen(f10)
      open(10,file=f10(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')

      i=slen(f11)
      open(11,file=f11(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')

      i=slen(f11)
      open(12,file=f12(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')


      irec=0
      do iday=1,nday

        irec=irec+1
        write(*,*) iyear,iday,irec

c        read(1,rec=irec)((hiday(i,j),i=1,nx1),j=1,ny1)!forSomeComputers
        read(1)((hiday(i,j),i=1,nx1),j=1,ny1)
        read(2)((aiday(i,j),i=1,nx1),j=1,ny1)
c        read(3)((hlate(i,j),i=1,nx1),j=1,ny1)
        read(3)((tair(i,j),i=1,nx1),j=1,ny1)
c        read(4)((hsens(i,j),i=1,nx1),j=1,ny1)
        read(4)((ssh(i,j),i=1,nx1),j=1,ny1)
c        read(5)((lwnet(i,j),i=1,nx1),j=1,ny1)
cc        read(6)((swnet(i,j),i=1,nx1),j=1,ny1)
cc        read(7)((sursalt(i,j),i=1,nx1),j=1,ny1)
c        read(8)((ostrx(i,j),i=1,nx1),j=1,ny1) ! zonal wind
c        read(8)((ostry(i,j),i=1,nx1),j=1,ny1) ! meridional wind
        read(8)((winx(i,j),i=1,nx1),j=1,ny1) ! zonal wind
        read(8)((winy(i,j),i=1,nx1),j=1,ny1) ! meridional wind
        do k=1,km
          read(9)((to(i,j,k),i=1,nx1),j=1,ny1)
        end do
        do k=1,km
          read(10)((so(i,j,k),i=1,nx1),j=1,ny1)
        end do
        do k=1,km
          read(11)((uo(i,j,k),i=1,nx1),j=1,ny1) ! zonal ocean U
        end do
        do k=1,km
          read(11)((vo(i,j,k),i=1,nx1),j=1,ny1) ! meridional ocean V
        end do
        read(12)((uice(i,j),i=1,nx1),j=1,ny1) ! zonal ice velocity
        read(12)((vice(i,j),i=1,nx1),j=1,ny1) ! meridional ice velocity

      end do ! iday

c      do imon=1,nmon
c        read(5)((p_e(i,j),i=1,nx1),j=1,ny1)
c      end do

      i=nx/2
      j=ny/2
c      write(*,'(2i6, 2f12.3, 2f8.2)') iyear, iday
c     &, clon(i,j), clat(i,j), to(i,j,1), heff(i,j)

      close(1)
      close(2)
      close(3)
      close(4)
      close(5)
      close(6)
      close(7)
      close(8)
      close(10)
      close(11)
      close(12)
999   continue
      
      stop  
      end

      INTEGER FUNCTION slen (string)
C ---
C --- this function computes the length of a character string less
C --- trailing blanks
C --- slen > 0, length of string less trailing blanks
C ---      = 0, character string is blank
C ---
      CHARACTER*(*) string
      CHARACTER*1 cblank
      INTEGER i
      DATA cblank/' '/
C ---
      DO 50 i = LEN(string), 1, -1
         IF (string(i:i) .NE. ' ')  GO TO 100
50    CONTINUE
      i = 0
100   CONTINUE
      slen = i
      RETURN
      END
