#!/usr/bin/env python3

# this version does random reads to make a subset
# order of field is specified by code
# order of different "field" messages is same as grib file

import pywgrib2_s
in_file='a.grb'
out='a_subset_1.grb'
inv='@mem:0'

ierr = pywgrib2_s.mk_inv(in_file, inv)
print('mk_inv ierr=',ierr)

# search strings have to start/end with colons
# don't want '50 mb' to match '850 mb'
# or 'TMP' to match 'VTMP'

count=0
for field in (':TMP:2 m above ground:', ':PRATE:', ':UGRD:10 m above ground:') :
    print('processing: ',  field)
    nmatch=pywgrib2_s.inq(in_file,field, inv=inv, grib=out)
    if nmatch < 0:
        print("error in processing ", field)
    else:
        count += nmatch

print("done count=",count)

pywgrib2_s.close(out)

nmatch=pywgrib2_s.inq(in_file,':TMP:2 m above ground:',time0=2020013106,Data=True)
x=pywgrib2_s.data
d(x.T)