!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
      program readLINUX
!     ******************************************************************
!     *                                                                *
!     *  Read output data on Linux                                     *
!     *                                                                *
!     *      NOTE:  Can read the fields derived from octagon grid,     *
!     *             interpolate them to ll grid and ouput them         *
!     *             for grads. (Still mising appropriate progragm that *
!     *             create the 'ctl' file, and there is no 'nml' file) *
!     *                                                                *
!     ******************************************************************

      implicit none
      include 'param_o.h'

      integer::lsd,npes,nhmax,nhstep
!-----------------------------------------------------------------------
!      parameter( lsd = 18 )
      parameter( lsd = 26 )
!-----------------------------------------------------------------------
!***********
      parameter(npes=nsub*nsub*nm,nhmax=10,nhstep=1) 
!***********
!-----------------------------------------------------------------------
      real, dimension(0:ilm+1,0:jlm+1):: dum2,dum9
      real, dimension(0:ilm+1,0:jlm+1,lsd):: dum3,dum4,dum5,dum6,dum7  &
                                ,dum8
      real, dimension(0:ilm+1,0:jlm+1)::dum10,dum12,dum13,dum14,dum15
      real, dimension(0:ilm+1,0:jlm+1,4)::dum18 
      real, dimension(0:im+1,0:jm+1,nm):: psd,hgt,sm ,tssd
      real, dimension(0:im+1,0:jm+1,nm,lsd):: fsd,tsd, usd,vsd, ull, vll
      real, dimension(0:im+1,0:jm+1,nm,lsd):: qsd,wsd
      real, dimension(0:im+1,0:jm+1,nm)::acprecq,cuprecq,acsnowq 
      real,dimension(0:im+1,0:jm+1,nm,4)::stcd
      real,dimension(igm,jgm,4)::stcp
      real, dimension(igm, jgm):: psp ,hgtp,smp,tssp
      real, dimension(igm,jgm,lsd):: fsp,tsp,usp,vsp,qsp,wsp
      real, dimension(igm,jgm)::acprecll,cuprecll,acsnowll
!-----------------------------------------------------------------------   
      real::glon,glat,xmp0,ymp0
      integer::kmp0,k
                               common /graph/  &
       GLON(igm,jgm),GLAT(igm,jgm)  &
      ,XMP0(igm,jgm),YMP0(igm,jgm),KMP0(igm,jgm)
!-----------------------------------------------------------------------   
       real::u2us,v2us,u2vs,v2vs
                               common /wind_convert/  &
       u2us (0:im+1,0:jm+1,nm), v2us (0:im+1,0:jm+1,nm)  &
      ,u2vs (0:im+1,0:jm+1,nm), v2vs (0:im+1,0:jm+1,nm)
!-----------------------------------------------------------------------
      character (len=01):: cres
      character (len=03):: c_mype, c_nh
      character (len=80):: c_filename1
      character (len=40):: cfile2
      integer::list2,nh,mype,n,mype0,jy,ix,list,imaxv,imaxh,imin,jmaxv  &
        ,jmaxh,jmin,i,j,l,nres,m1,m2
      logical :: lvrbl

      lvrbl=.true.
!-----------------------------------------------------------------------   

! *** Open file for  visualization
      nres=1
      write(cres,910) nres
 910  format(i1.1)
      cfile2="../data_in/grid/grph.dat"

      open(unit=list2,file=cfile2,form="unformatted")
      Read (list2) GLON,GLAT,XMP0,YMP0,KMP0, u2us, v2us, u2vs, v2vs
      Close(list2)
      print *,xmp0(180,91),ymp0(180,91),kmp0(180,91)

!-----------------------------------------------------------------------

! *** Open file for grads

      open(9, file='../data_out/globrun.dat', status='unknown',  &
                                              form = 'unformatted')
      open(10,file='../data_out/hgt500UG.dat',status='unknown', &
                                           form='unformatted')
      open(110,file='../data_out/tmp500UG.dat',status='unknown', &
                                           form='unformatted')



!#######################################################################

      do nh = 0, nhmax, nhstep
!      do nh = 0, 4 !nhmax, nhstep

!#######################################################################
      write(c_nh,1000) nh
 1000 format(i3.3)
!-----------------------------------------------------------------------   
        do mype = 0, npes-1
!-----------------------------------------------------------------------   

          n = mype/nxy + 1
          mype0 = mype - (n-1)*nxy
          jy = (mype0/ixm) + 1
          ix = mod(mype0, ixm)+1
          list = 10 + mype + 1

!-----------------------------------------------------------------------   
              write(c_mype,1000) mype
              c_filename1='../../RUN/data/data_out/GEFfcst_'//   &
                     c_mype//"."//c_nh
              open(unit=list, file=c_filename1, form='unformatted')
!-----------------------------------------------------------------------   
         imaxV = ix * ildom
         imaxH = imaxV + 1
         imin  = imaxH - ildom
         jmaxV = jy * jldom
         jmaxH = jmaxV + 1
         jmin  = jmaxH - jldom
!-----------------------------------------------------------------------   
         read(list) dum2 
         psd(imin:imaxH, jmin:jmaxH,n) = dum2(1:ilm,1:jlm)
	 read(list)dum3
	 read(list)dum4
         fsd(imin:imaxH, jmin:jmaxH,n,:) = dum3(1:ilm,1:jlm,:)
         tsd(imin:imaxH, jmin:jmaxH,n,:) = dum4(1:ilm,1:jlm,:)
	 read(list)dum2
         acprecq(imin:imaxH, jmin:jmaxH,n) = dum2(1:ilm,1:jlm)
	 read(list)dum2
         cuprecq(imin:imaxH, jmin:jmaxH,n) = dum2(1:ilm,1:jlm)
	 read(list)dum2
         acsnowq(imin:imaxH, jmin:jmaxH,n) = dum2(1:ilm,1:jlm)

         close(list)
!-----------------------------------------------------------------------   
        end do

        print *,fsd(40,34,5,13),fsd(40,35,5,13),fsd(41,34,5,13),  &
	        fsd(41,35,5,13)

!-----------------------------------------------------------------------   

! *** Define values on a spherical grid
        
       if(nm.eq.14)then
        call octa2llH(psd, psp, 1) 
        call octa2llH(fsd, fsp, lsd) 
        call octa2llH(tsd, tsp, lsd) 
        call octa2llH(acprecq, acprecll, 1) 
        call octa2llH(cuprecq, cuprecll, 1) 
        call octa2llH(acsnowq, acsnowll, 1) 
     
!        call llwinds_oc(usd, vsd, ull, vll,lsd)
!        call bocovs_oc(ull,im,jm,lsd)
!        call bocovs_oc(vll,im,jm,lsd)
!        call avrv_oc(ull, usd,lsd)
!        call avrv_oc(vll, vsd,lsd)
!        usd = usd * .25
!        vsd = vsd * .25

!        do n=1,6
!	do i=0,im+1
!	do j=0,jm+1
!	do l=1,lsd
!!	  print *,i,j,n,l,tsd(i,j,n,l)
!        enddo
!        enddo
!        enddo
!        enddo

!        do k=1,nm
!	  if(k.eq.1.or.k.eq.2.or.k.eq.7.or.k.eq.8.or.k.eq.9  &
!               .or.k.eq.12)then
!              usd(1 ,1 ,k,:)=usd(1 ,1 ,k,:)*4./3.
!              vsd(1 ,1 ,k,:)=vsd(1 ,1 ,k,:)*4./3.
!          endif
!
!	  if(k.eq.1.or.k.eq.4.or.k.eq.6.or.k.eq.7.or.k.eq.11  &
!                .or.k.eq.14)then
!             usd(im,1 ,k,:)=usd(im,1 ,k,:)*4./3.
!             vsd(im,1 ,k,:)=vsd(im,1 ,k,:)*4./3.
!          endif
!
!	  if(k.eq.2.or.k.eq.5.or.k.eq.6.or.k.eq.9.or.k.eq.13  &
!              .or.k.eq.14)then
!             usd(1 ,jm,k,:)=usd(1 ,jm,k,:)*4./3.
!             vsd(1 ,jm,k,:)=vsd(1 ,jm,k,:)*4./3.
!          endif
!
!	  if(k.eq.4.or.k.eq.5.or.k.eq.8.or.k.eq.11.or.k.eq.12  &
!              .or.k.eq.13)then
!             usd(im,jm,k,:)=usd(im,jm,k,:)*4./3.
!             vsd(im,jm,k,:)=vsd(im,jm,k,:)*4./3.
!           endif
!        enddo
!         
!        call octa2llH(usd, usp, lsd) 
!        call octa2llH(vsd, vsp, lsd) 
       else if(nm.eq.6)then
        call cube2llH(psd, psp, 1) 
        call cube2llH(fsd, fsp, lsd) 
        call cube2llH(tsd, tsp, lsd) 
        call cube2llH(acprecq, acprecll, 1) 
        call cube2llH(cuprecq, cuprecll, 1) 
        call cube2llH(acsnowq, acsnowll, 1) 
 
!        call cube2llH(qsd, qsp, lsd) 
!        call cube2llH(wsd, wsp, lsd) 
!	call cube2llH(tssd,tssp,1)
!!	call cube2llH(stcd,stcp,1)
!	call cube2llH(q2sd,q2sp,38)
!	call cube2llH(train,trainp,38)
!	call cube2llH(stcd,stcp,4)
       
!        call llwinds(usd, vsd, ull, vll,lsd)
!        call bocovs(ull,lsd)
!        call bocovs(vll,lsd)
!        call avrv(ull, usd,lsd)
!        call avrv(vll, vsd,lsd)
!
!        usd = usd * .25
!        usd(1 ,1 ,:,:)=usd(1 ,1 ,:,:)*4./3.
!        usd(im,1 ,:,:)=usd(im,1 ,:,:)*4./3.
!        usd(1 ,jm,:,:)=usd(1 ,jm,:,:)*4./3.
!        usd(im,jm,:,:)=usd(im,jm,:,:)*4./3.
!
!        vsd = vsd * .25
!        vsd(1 ,1 ,:,:)=vsd(1 ,1 ,:,:)*4./3.
!        vsd(im,1 ,:,:)=vsd(im,1 ,:,:)*4./3.
!        vsd(1 ,jm,:,:)=vsd(1 ,jm,:,:)*4./3.
!        vsd(im,jm,:,:)=vsd(im,jm,:,:)*4./3.
!         
!        call cube2llH(usd, usp, lsd) 
!        call cube2llH(vsd, vsp, lsd) 
       endif

       if(lvrbl)then
        write(9) psp
!       write(9)tssp

        do i=1,lsd
!        write(9) fsp(:,:,i)
        enddo
	write(10)fsp(:,:,13)
	do m1=1,360
	do m2=1,181
!	  print *,m1,m2,fsp(m1,m2,13)
        enddo
        enddo

        do i=1,lsd
!        write(9) tsp(:,:,i)
        enddo
!	write(110)tsp(:,:,13)

!        write(9)acprecll
!        write(9)cuprecll
!        write(9)acsnowll
        do i=1,lsd
!        write(9)usp(:,:,i)
        enddo

        do i=1,lsd
!       write(9)vsp(:,:,i)
        enddo

	endif

!#######################################################################

      end do

!#######################################################################
      close(9)
      close(10)
      close(110)


!-----------------------------------------------------------------------   
      end  program readLINUX


