      program read_gfs22

      implicit none
      character(len=255)              :: gribfile,outdir
!_____________________________________________________________________________________________________________
      integer                         :: nx,ny,nz,np
      integer                         :: dx,dy 
      integer                         :: proj,LatS,LonW,LatN,LonE
      integer                         :: i,j,k,l,n,it,nsfcfld,id
      integer                         :: n1,n2,n3,ng,LenStr
      real                            :: xe,mrsat
      integer, dimension(200)         :: kgds

      REAL, PARAMETER                 :: Undef=9.9990003E+20 
      REAL, DIMENSION(15000:45000)    :: esat,es
      REAL,PARAMETER                  :: kappa=287./1004.
      real,    dimension(:,:,:) ,allocatable    :: ht                        !Isobaric heights (m)
      real,    dimension(:,:,:) ,allocatable    :: tp                        !Isobaric temps (K)
      real,    dimension(:,:,:) ,allocatable    :: th                        !Isobaric theta (K)
      real,    dimension(:,:,:) ,allocatable    :: uw                        !Isobaric u-wind (m/s)
      real,    dimension(:,:,:) ,allocatable    :: vw                        !Isobaric v-wind (m/s)
      real,    dimension(:,:,:) ,allocatable    :: rh                        !Isobaric rh,mr (%,kg/kg)
      real,    dimension(:)     ,allocatable    :: pr,pri                    !Isobaric pressures (mb)
!_____________________________________________________________________________________________________________
      real,    dimension(:,:,:) ,allocatable    :: slp
!_____________________________________________________________________________________________________________
      real,    dimension(:,:)   ,allocatable    :: dummy
!_____________________________________________________________________________________________________________
      character(len=255)              :: outfile,outfile2,gdsfile
      character(len=15)               :: ModelDriver
      character(len=20)               :: atime
      character(len=8)                :: atime_r
      character(len=7)                :: model,model2
      character(len=8)                :: timex
!_____________________________________________________________________________________________________________

      open(1,file='InputModelInf.txt',form='formatted',status='old')
      rewind 1
      read(1,'(a15)')ModelDriver
      read(1,*)nx,ny,nz
      read(1,*)proj
      read(1,*)LatS
      read(1,*)LonW
      read(1,*)LatN
      read(1,*)LonE
      read(1,*)dx
      read(1,*)dy

      allocate(ht(nx,ny,nz))
      allocate(tp(nx,ny,nz))                       
      allocate(th(nx,ny,nz))                     
      allocate(uw(nx,ny,nz))                 
      allocate(vw(nx,ny,nz))                
      allocate(rh(nx,ny,nz)) 
      allocate(slp(nx,ny,14)) 
      allocate(dummy(nx,ny))  
      allocate(pr(nz))
      allocate(pri(nz))	


    do k=1,nz
        read(1,*)pr(k)
    enddo
      close(1)

! *** Read in degrib data.
!
      open(11,file='/home/lucci/GEF/GEF-reta/GEF_tempo/gef_trunk/PRP/INIT_DATA/2017022700.bin',access='direct',form='unformatted',status='old',recl=1440*721*4)
 

       do 20 l=1,nz
        k=l
       
       read(11,rec=k) ((uw(i,j,l),i=1,nx),j=ny,1,-1) 
   20  continue 

!       print*,'uw=',uw(1,1,1)
      
       do 30 l=1,nz

       k=l+26
       read(11,rec=k) ((vw(i,j,l),i=1,nx),j=ny,1,-1)
   30  continue        
       
!c       print*,'vw=',vw(1,1,1)
       
       do 40 l=1,nz

       k=l+52
       read(11,rec=k) ((ht(i,j,l),i=1,nx),j=ny,1,-1) 
   40  continue 
 
 
!c       print*,'ht=',ht(1,1,1)
 
       do 50 l=1,nz

       k=l+78
       read(11,rec=k) ((tp(i,j,l),i=1,nx),j=ny,1,-1) 
   50  continue 
 
     
!c       print*,'tp=',tp(1,1,1)
     
      
       do 60 l=1,nz

       k=l+104
       read(11,rec=k) ((rh(i,j,l),i=1,nx),j=ny,1,-1) 
   60  continue
   
!c       print*,'rh=',rh(1,1,1)

     
       read(11,rec=131) ((slp(i,j,1),i=1,nx),j=1,ny) 
       read(11,rec=132) ((slp(i,j,2),i=1,nx),j=1,ny)
       read(11,rec=133) ((slp(i,j,3),i=1,nx),j=1,ny)
       read(11,rec=134) ((slp(i,j,4),i=1,nx),j=1,ny)
       
       
       read(11,rec=135) ((slp(i,j,5),i=1,nx),j=ny,1,-1)
       
       read(11,rec=136) ((slp(i,j,6),i=1,nx),j=ny,1,-1)
       
       read(11,rec=137) ((slp(i,j,7),i=1,nx),j=ny,1,-1)
            
       read(11,rec=138) ((slp(i,j,8),i=1,nx),j=ny,1,-1)
        
       read(11,rec=139) ((slp(i,j,9),i=1,nx),j=ny,1,-1)
             
       read(11,rec=140) ((slp(i,j,10),i=1,nx),j=ny,1,-1)
      
       read(11,rec=141) ((slp(i,j,11),i=1,nx),j=ny,1,-1)
              
       read(11,rec=142) ((slp(i,j,12),i=1,nx),j=ny,1,-1)

      close(11)

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

      do i=1,nx
          do j=1,ny
             do l=1,26
               if (rh(i,j,l).eq.0) print*,"UNDEF",i,j,l,rh(i,j,l)
             enddo
          enddo
        enddo

      end program read_gfs22



