      parameter(nx1=168,ny1=180,nx=nx1,ny=ny1,imt=nx1,jmt=ny1,km=40)
      parameter(nbio=11)
c      parameter(nyear1=2012,nyear2=2012,nday=365,nmon=12)
      parameter(nyear1=1992,nyear2=2018,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 data1(imt,jmt),data2(imt,jmt),data3(imt,jmt)
      dimension data4(imt,jmt),data5(imt,jmt),data6(imt,jmt)
      dimension data7(imt,jmt),data8(imt,jmt),data9(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

      f1='heff_168_180.H'  ! monthly sea ice thickness (m)
      f2='we_168_180.H'  ! monthly upwelling velocity (cm/day)
      f3='fw_cont_168_180_100m.H'   ! monthly freshwater content in the upper 100 m
      f4='no3_168_180_100m.H'   ! monthly nitrate in the upper 100 m (mmol_N/m*m*m)
      f5='pp_168_180_100m.H'   ! monthly primary productivity in the upper 100 m (mmol_N/m*m/day)
      f6='phy_168_180_100m.H'   ! monthly phytoplankton biomass in the upper 100 m (mmol_N/m*m)
      f7='zoo_168_180_100m.H'   ! monthly zooplankton biomass in the upper 100 m (mmol_N/m*m)
      f8='to_168_180_100m.H'   ! monthly ocean temperature in the upper 100 m (C)
c      f9='to_168_180.H'       ! monthly ocean temperature (C)
c      f10='so_168_180.H'       ! monthly ocean salinity (psu)
c      f11='uo_168_180.H'       ! monthly ocean velocity (cm/s)
c      f12='uiday_168_180.H'    ! monthly 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')

      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')
 
      i=slen(f5)
      open(5,file=f5(1:i)//cyear(iyear)
     &,access='direct',form='unformatted',recl=nx1*ny1*4
     &,status='old')

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

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

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

      go to 4444
      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')

4444  continue

      irec=0
c      do iday=1,nday
      do iday=1,nmon

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

c the next commented line is for some computers to read
c        read(1,rec=irec)((data1(i,j),i=1,nx1),j=1,ny1)!forSomeComputers
c the following lines are for some other computers to read
        read(1)((data1(i,j),i=1,nx1),j=1,ny1)
        read(2)((data2(i,j),i=1,nx1),j=1,ny1)
        read(3)((data3(i,j),i=1,nx1),j=1,ny1)
        read(4)((data4(i,j),i=1,nx1),j=1,ny1)
        read(5)((data5(i,j),i=1,nx1),j=1,ny1)
        read(6)((data6(i,j),i=1,nx1),j=1,ny1)
        read(7)((data7(i,j),i=1,nx1),j=1,ny1)
        read(8)((data8(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
c        read(8)((winx(i,j),i=1,nx1),j=1,ny1) ! zonal wind
c        read(8)((winy(i,j),i=1,nx1),j=1,ny1) ! meridional wind
c        do k=1,km
c          read(9)((to(i,j,k),i=1,nx1),j=1,ny1)
c        end do
c        do k=1,km
c          read(10)((so(i,j,k),i=1,nx1),j=1,ny1)
c        end do
c        do k=1,km
c          read(11)((uo(i,j,k),i=1,nx1),j=1,ny1) ! zonal ocean U
c        end do
c        do k=1,km
c          read(11)((vo(i,j,k),i=1,nx1),j=1,ny1) ! meridional ocean V
c        end do
c        read(12)((uice(i,j),i=1,nx1),j=1,ny1) ! zonal ice velocity
c        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
