#!/usr/bin/env python3
#
# this version uses wgrib2 to regrid the files to a set of points
#   see  https://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/new_grid.html
# for more details
#
# wgrib2 -new_grid can interpolate to a list of locations
#  (default: bilinear interpolation)
#
# write data as binary to out_inv

import pywgrib2_s
in_file='a.grb'
out='a_regrid_3.bin'
out_inv='@mem:1'
UID = '0'

# values for lon[i] and lat[i]
lon = [ 10.1, 20.2, 350.9 ]
lat = [ -1.9, 22.2, -33.3 ]
len_lon = len(lon)

# make list of locations
for i in range(len_lon):
  if i == 0:
    loc=str(lon[0]) + ":" + str(lat[0])
  else:
    loc=loc + ':' + str(lon[i]) + ":" + str(lat[i])

# interpolate all the PRATEs
# binary interpolated data -> @mem:0
# text date codes -> @mem:1

err = pywgrib2_s.wgrib2( [in_file, '-rewind_init', in_file, '-inv', out_inv, '-new_grid_winds', 'earth',
   '-match', ':PRATE:surface:', '-new_grid_format' , 'bin', '-new_grid', 'location', loc,  UID, out] )
print("regrid err=", err)