      program iterator
      implicit none
      include 'grib_api_f77.h'
      integer ifile,outfile
      integer iret,iter
      real*8 lat,lon,value,missingValue
      integer n,flags
      character*256 filename,error,nev,nev0
      integer igrib
      integer szum
      open(10,file='output.txt')
      ifile=15
      outfile=10
      nev0='u'
      call grib_check(grib_open_file(ifile,'uv500.grb','r'))
      call grib_check(grib_open_file(outfile,'out.grib','w'))
      szum = 1
      
  10  iret=grib_new_from_file(ifile,igrib)
      if (igrib .eq. -1 )  then
        if (iret .ne.0) then
           call grib_check(iret)
        endif
        stop
      endif
      call grib_check(grib_get_real8(igrib,'missingValue',missingValue))
      flags = 0
      call grib_check(grib_iterator_new(igrib,iter,flags))
      call grib_check(grib_get_string(igrib,'shortName',nev))
      
      if (nev.eq.nev0) then
         call grib_check(grib_write(igrib,outfile))
      endif
      
  30  continue
      call grib_check(grib_iterator_delete(iter))
      goto 10
      call grib_check(grib_release(igrib))
      call grib_check(grib_close_file(ifile))
      call grib_check(grib_close_file(outfile))
      close(10)
      end