Logging and Exceptions¶
Exception Handling¶
pywgrib2_xr defines exception WgribError
which is raised when a C function returns nonzero status. If that function
writes an error message to stderr
, it will be captured as exception value:
import pywgrib2_xr as pywgrib2
file = 'nonexistent'
try:
pywgrib2.wgrib('nonexistent')
except WgribError as e:
print(e)
finally:
pywgrib2.free_files(file)
*** FATAL ERROR: missing input file nonexistent *** *** arg list to wgrib2: wgrib2 nonexistent
The finally
clause ensures that file
is closed. The function wgrib
does not close files on exit. One can try to set wgrib2
arguments
'-transient', file
, but this does not work when wgrib
call fails:
file = 'gfs_tsoil.grib2'
try:
pywgrib2.wgrib(file, '-foo', '-transient', file)
except Exception as e:
print(e)
pywgrib2.status_open()
*** FATAL ERROR: unknown option -foo *** *** arg list to wgrib2: wgrib2 gfs_tsoil.grib2 -foo -transient gfs_tsoil.grib2 <stdin>:1: UserWarning: file: gfs_tsoil.grib2 r:perm file_offset=0 usage=0
The function status_open()
, intended for debugging,
prints files opened by the C code.
Logging¶
pywgrib2_xr uses Python module
logging.
By defaults, all emmited messages are output to Python’s stderr
. It is
possible to set logging level and a different output stream:
import logging
logging.basicConfig(filename='my.log', level=logging.DEBUG)
pywgrib2.wgrib('gfs.grib2', '-inv', 'gfs.inv')
cat my.log
DEBUG:pywgrib2_xr.wgrib2:args: ('gfs.grib2', '-inv', 'gfs.inv')
Setting level to DEBUG
outputs arguments passed to wgrib
.