#!/usr/bin/env python3
# The append parameter in pywgrib2_s.write(..) refers to how the file
# will be opened. Don;t forget, the file may have been opened in a
# previous call to another routine (write, inq, mk_inv).
#
# This example shows the effect of Append=True, and how
# to do an append using mk_inv.
#
import pywgrib2_s
from shutil import copyfile
in_file='a.grb'
in_inv='a.inv'
tmp_file='a.tmp'
append1='a_append_false.grb'
append2='a_append_true.grb'
append3='a_append_mk_inv.grb'
template='@mem:0'
template='junk'
# make inventory
pywgrib2_s.mk_inv(in_file,in_inv)
# make tmp_file
# note that you need to rewind in_file because mk_inv opened
# the file and read the file to the end
err=pywgrib2_s.wgrib2([ in_file, '-rewind_init', in_file, '-d', '1', '-grib', tmp_file] )
print("wgrib2 err=",err)
pywgrib2_s.close(tmp_file)
# copy tmp_file to append?=1
copyfile(tmp_file, append1)
copyfile(tmp_file, append2)
copyfile(tmp_file, append3)
nmatch=pywgrib2_s.inq(in_file,":WEASD:",":6 hour fcst:", ":d=2020013118:",
Data=True,Matched=True,grib=template)
if nmatch != 1:
print("expected 1 match, found nmatch=",nmatch)
quit()
# version 1
field=pywgrib2_s.write(append1,template,1,new_data=pywgrib2_s.data,var='WEASD',time0=2020013118,
lev='surface',ftime='6 hour forecast')
print("write field=",field)
field=pywgrib2_s.write(append1,template,1,new_data=pywgrib2_s.data,var='WEASD',time0=2020013119,
lev='surface',ftime='12 hour forecast')
print("write field=",field)
# version 2
field=pywgrib2_s.write(append2,template,1,new_data=pywgrib2_s.data,var='WEASD',time0=2020013118,
lev='surface',ftime='6 hour forecast',Append=True)
print("write field=",field)
field=pywgrib2_s.write(append2,template,1,new_data=pywgrib2_s.data,var='WEASD',time0=2020013119,
lev='surface',ftime='12 hour forecast',Append=True)
print("write field=",field)
# version 3
err=pywgrib2_s.mk_inv(append3,'@mem:1')
print("mk_inv err",err)
field=pywgrib2_s.write(append3,template,1,new_data=pywgrib2_s.data,var='WEASD',time0=2020013118,
lev='surface',ftime='6 hour forecast')
print("write field=",field)
field=pywgrib2_s.write(append3,template,1,new_data=pywgrib2_s.data,var='WEASD',time0=2020013119,
lev='surface',ftime='12 hour forecast')
print("write field=",field)
print("done")