#!/bin/tcsh -f
@ loop = 41
while ($loop <= 70)
set yr = $loop
@ yr1 = $loop + 2000
echo 'YEAR=' $yr1
##setenv HD /Net/elninoshare/amishra/RSM_ROMS/ROARS/IAS-ROARS2/CCSM-ESAT/ROMS-ModExpt/ROMS
setenv HD /Net/water/amishra/RSM_ROMS/FL-DWN/RCP8.5/CLD2/O/mon
##mkdir  ${HD}/vert
cd ${HD}/vert
echo 'going to prepare file  GrADS readable using' $yr1  >> synopsis_t.$yr1
@ xdim = 129
@ ydim = 120
rm -rf tread_${yr1}.F
cat > tread_${yr1}.F << EOF
       program reading
       implicit none
       integer idim,jdim,kdim,tdim
       parameter(idim=$xdim,jdim=$ydim,kdim=30,tdim=12)
       real height(40)
       data height /-5.01, -15.07, -25.28, -35.76, -46.61,
     &              -57.98, -70.02, -82.92, -96.92, -112.32,
     &              -129.49, -148.96, -171.4, -197.79, -229.48,
     &              -268.46, -317.65, -381.39, -465.91, -579.3,
     &              -729.35, -918.37, -1139.15, -1378.57, -1625.7,
     &              -1875.11, -2125.01, -2375., -2625., -2875.,
     &              -3125., -3375., -3625., -3875., -4125.,
     &              -4375., -4625., -4875., -5125., -5375./
       real*8 sst8(idim,jdim,kdim)
       real*8 h8($xdim,$ydim),mask8(idim,jdim)
       real   h4(idim,jdim),srho(kdim),mask(idim,jdim)
       real   sst4(idim,jdim,kdim)
       real   sh(idim,jdim,kdim)
       real   sst1000(idim,jdim)
       integer iyear,imth,lev,idend,idy
       character*4 year
       character*2 month,day

       integer i,j,k,t

       print*,height

       open(11,file='../../scripts/h-rho.dat',form='unformatted')
       read(11)h8
       close(11)
       do j=1,jdim
        do i=1,idim
           h4(i,j)=h8(i,j)
        enddo
       enddo

       open(11,file='../../scripts/mask.dat',form='unformatted')
       read(11)mask8
       close(11)
       do j=1,jdim
        do i=1,idim
           mask(i,j)=mask8(i,j)
        enddo
       enddo
       call zs(h4,sh)
        do iyear=$yr1,$yr1
         write(year,'(i4)')iyear
          do imth=1,tdim
C ---          do imth=3,tdim
          write(month,'(i2)')imth
C ---        if((imth.eq.1) .OR. (imth.eq.3) .OR. (imth.eq.5).OR.(imth.eq.7)
C ---     1    .OR.(imth.eq.8) .OR. (imth.eq.10) .OR. (imth.eq.12)) idend=31
C ---        if((imth.eq.4) .OR. (imth.eq.6) .OR. (imth.eq.9).OR.
C ---     1     (imth.eq.11)) idend=30
C ---          IF (imth.eq.2)then
C ---           IF(MOD(iyear,100).NE.0.AND.MOD(iyear,4).EQ.0) THEN
C ---             WRITE(*,*) 'LEAP YEAR'
C ---             idend=29
C ---            ELSEIF(MOD(iyear,400).EQ.0) THEN
C ---             WRITE(*,*) 'LEAP YEAR'
C ---             idend=29
C ---            ELSE
C ---             WRITE(*,*) 'NOT A LEAP YEAR'
C ---             idend=28
C ---           ENDIF
C ---           ENDIF
C ---           print*,idend
          if(imth.lt.10) month(1:1)='0'
C ---          do idy=1,idend
C ---          write(day,'(i2)')idy
C ---          if(idy.lt.10) day(1:1)='0'
C ---          open(11,file='../wraw/vert-'//year//month//day//'-s.dat',
          open(11,file='../wraw/vert-'//year//month//'-s.dat',
     1         form='unformatted')
C ---          open(51,file='vert-'//year//month//day//'.dat',
          open(51,file='vert-'//year//month//'.dat',
     1         form='unformatted')
          read(11)sst8
          do k=1,kdim
           do j=1,jdim
            do i=1,idim
             sst4(i,j,k)=sst8(i,j,k)
            enddo
           enddo
          enddo
          do lev=1,40
            call vinterp(height(lev),mask,sh,sst4,sst1000)
            write(51)sst1000
          enddo ! lev
          close(11)
          close(51)
C ---         enddo  ! idy
        enddo  ! tdim
       enddo    ! iyear
      end
      subroutine zs(h, z_r)
       implicit none
       integer Lp, Mp, NL
       parameter(Lp=$xdim,Mp=$ydim,NL=30)
       real    ds, hc, hmin, Tcline, theta_s, theta_b
       parameter(hmin=30, Tcline=75, theta_s=5.0, theta_b=0.4)
       real    hinv, z_r0, z_r(Lp*Mp*NL), h(Lp*Mp), zeta(Lp*Mp)
       real    cff1, cff2
       real    sc_w(nl), Cs_w(nl), sc_r(nl), Cs_r(nl)
       integer i, j, k, ij, ip
!-----------------------------------------------------------------------
!  Depths from S-coordinates transformation.
!-----------------------------------------------------------------------
!  Define S-Curves in domain [-1 < sc < 0] at vertical W- and
!  RHO-points.
        ds=1.0/real(NL)
        hc=MIN(hmin,Tcline)
            print*, 'hmin, hc=', hmin, hc
        cff1=1.0/SINH(theta_s)
        cff2=0.5/TANH(0.5*theta_s)
        sc_w(0)=-1.0
        Cs_w(0)=-1.0
        do k=1,NL
          sc_w(k)=ds*real(k-NL)
          Cs_w(k)=(1.0-theta_b)*cff1*SINH(theta_s*sc_w(k))+
     &            theta_b*(cff2*TANH(theta_s*(sc_w(k)+0.5))-0.5)
          sc_r(k)=ds*(real(k-NL)-0.5)
          Cs_r(k)=(1.0-theta_b)*cff1*SINH(theta_s*sc_r(k))+
     &            theta_b*(cff2*TANH(theta_s*(sc_r(k)+0.5))-0.5)
        enddo
        do j=1,Mp
         do i=1,Lp
           ij=i+(j-1)*Lp
           zeta(ij)=0.
         enddo
        enddo
!
!  Compute model grid depths (meters) at RHO-points.
!
        do k=1,NL
          cff1=hc*(sc_r(k)-Cs_r(k))
          cff2=Cs_r(k)
          do j=1,Mp
            do i=1,Lp
              ij=i+(j-1)*Lp
              ip=ij+(k-1)*Lp*Mp
              hinv=1.0/h(ij)
              z_r0=cff1+cff2*h(ij)
              z_r(ip)=z_r0+zeta(ij)*(1.0+z_r0*hinv)
            enddo
          enddo
        enddo
        print*,'lihq end of z_s'
c           print*,z_r
!
      endsubroutine zs

      subroutine vinterp(height,mask,sh,input,output)
       implicit none
       integer idim,jdim,kdim,level
       parameter(idim=$xdim,jdim=$ydim,kdim=30)
       real    height,sh(idim,jdim,kdim),mask(idim,jdim)
       real    input(idim,jdim,kdim), output(idim,jdim)
       real    d1,d2
       integer i,j,k

       do j=1,jdim
        do i=1,idim
         if(mask(i,j).eq.1)then
          if(sh(i,j,1).ge.height) then
            output(i,j)=-9999.
          elseif(sh(i,j,kdim).le.height) then
            output(i,j)=input(i,j,kdim)
          else
           do k=2,kdim
            if((sh(i,j,k).ge.height).and.(sh(i,j,k-1).lt.height))then
              level=k
              goto 1000
            endif
           enddo
1000       continue
           d1=sh(i,j,level-1)-height
           d2=height-sh(i,j,level)
           output(i,j)=input(i,j,level-1)*d2/(d1+d2) +
     1                 input(i,j,level  )*d1/(d1+d2)
          endif  ! height vs. sh
         else    ! mask
            output(i,j)=-9999.
         endif   ! mask
        enddo
       enddo
      endsubroutine vinterp
EOF
################################
rm -rf tread_${yr1}.x
ifort tread_${yr1}.F -o tread_${yr1}.x
./tread_${yr1}.x
wait
#################################
echo 'prepared file  GrADS readable Vertical Momentum for' $yr1
 @ loop = $loop + 1
end
exit
