#!/usr/bin/env python3

# this version does random reads to make a subset
# order of grib messages is specified by code
#
# This version uses the wgrib2 code to find the match.
# This is slightly slower than using python to find the match.
# See cookbook_subset_4.py.
#
# Note that cookbook_subset_6.py is even faster.

import pywgrib2_s
in_file='a.grb'
out='a_subset_5.grb'
inv_file='@mem:0'

ierr = pywgrib2_s.mk_inv(in_file, inv_file)
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'

for field in (':TMP:2 m above ground:', ':PRATE:', ':UGRD:10 m above ground:'):
    for date in (20200101000000,20200101060000,20200101120000,20200101180000):
        field_date=':D='+str(date)+field
        print('processing: ',  field_date)
        err=pywgrib2_s.inq(in_file,field_date,inv=inv_file,grib=out)
print("done")