#!/usr/bin/env python3

# reading sequentially is much faster than random access reading 
# reading from beginning to end (with skips) is fast than random access reading
#
# to read sequentially,
#   do not use any search terms
#   set sequential = 0 for first read
#   set sequential > 0 subsequent reads
#
# to read from beginning to end
#   set search terms
#   set sequential = 0 for first search
#   set sequential > 0 subsequent searches

import pywgrib2_s
import os.path

in_file='a.grb'
in_inv_file='a.inv'

# if in_inv_file does not exist, make inv file
if not os.path.isfile(in_inv_file) :
  err=pywgrib2_s.mk_inv(in_file,in_inv_file)
  print("mk_inv err=", err)

# for the first read, sequential = 0, for following reads, sequential > 0

sequential=0
while pywgrib2_s.inq(in_file, ':TMP:2 m above ground:', inv=in_inv_file, sequential=sequential, Data=True, 
  Latlon=True, Matched=True) == 1:
  print ("inv=", pywgrib2_s.matched[0], ' data[0,10]=', pywgrib2_s.data[0,10],' lat/lon=',pywgrib2_s.lat[0,10],'  ',
  pywgrib2_s.lon[0,10])
  sequential = sequential + 1

print("done count=",sequential)