C     test program to read the output of the degribbing software
C     UNPKGRB1.FOR
C
C                                                   Schwartz
C                                                   lots of NMC help
C
C     this is a skeleton program only to read 40km data
C     this routine is set up to read the output of the degribbing
c     software **for 40km MAPS output, ie, array is 151x 113.
C     it reads the headers and the data into the array data.
C     you ned to know how many 2-d and 3-d fields there are.
C     In this example, we read all 14 3-d 40km fields one at a time

      real data(151,113)
      character*1 PDS(100)
      INTEGER*4 KGDS(200)
      parameter(nzp = 40)
c
c     this open is for the output file from unpkgrb1.
c     output from unpkgrb1 tells you how much data and what is
c     in grib file

      open(unit=11,file='degrib.out',status='old',
     *access='sequential',form='unformatted')
c 
      if(KERR.NE.0) THEN
         PRINT 50, INFILE, KERR
 50      FORMAT(' ERROR OPENING UNIT 11, FILE= ',a44,' IOSTAT= ',i8)
         STOP 50
                    ENDIF
C
      nvar3 = 0
      nvar2 = 42
C
      if(nvar3.eq.0) go to 400
      do 300 n=1,nvar3
c
      Do 200 k=1,nzp !for each level
              
      READ(11) LENPDS, LENKGDS, NWORDS
      PRINT *,'lenpds,lenkgds,nwords = ',lenpds,lenkgds,nwords
      READ(11) (PDS(j),J=1,LENPDS)
c      print *,'pds='
c      print *,(pds(i),i=1,LENPDS)
      if(LENKGDS.GT.0) then
         READ(11) (KGDS(J),J=1,LENKGDS)
         print *,'KGDS= '
         print *,(KGDS(j),j=1,LENKGDS)
                       endif
c
c     read the unformatted data
      read(11) data
c
c     up to you to look at the data here
c
 200  continue !next level this 3-d variable
 300  continue ! next 3-d variable
c
      if(nvar2.eq.0) go to 700
c
 400  do 500 i = 1,nvar2
c
      READ(11,END=700) LENPDS, LENKGDS, NWORDS
      PRINT *,'lenpds,lenkgds,nwords = ',lenpds,lenkgds,nwords
      READ(11) (PDS(j),J=1,LENPDS)
c      print *,'pds='
c      print *,(pds(i),i=1,LENPDS)
      if(LENKGDS.GT.0) then
         READ(11) (KGDS(J),J=1,LENKGDS)
         print *,'KGDS= '
         print *,(KGDS(j),j=1,LENKGDS)
                       endif
c
c     read the unformatted data
      read(11) data
c
 500  continue
c      
 700  stop
      end
