ecmwf / eccodes-python

Python interface to the ecCodes GRIB/BUFR decoder/encoder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add method to access the result of the json bufr_dump

sandorkertesz opened this issue · comments

Is your feature request related to a problem? Please describe.

At the moment, only the bufr_dump command line tool can generate a json dump for a BUFR message. If I want to use the dump in Python I need to run the command line tool e.g. with subprocess, redirect the output to a (json) file and read it back as json.

Describe the solution you'd like

It would be really useful if there was a method in the Python API generating the same dump as a dictionary.

Describe alternatives you've considered

No response

Additional context

It would help the implementation of ecmwf/earthkit-data#115

Organisation

ECMWF

import eccodes
import sys

f = open("my.bufr", 'rb')
handle = eccodes.codes_bufr_new_from_file(f)
eccodes.codes_set(handle, 'unpack', 1)
eccodes.codes_dump(handle, sys.stdout, "json") 

You could then take the JSON output and convert it to a dictionary

Unfortunately, it does not work on my MacBook in a jupyter notebook (using python 3.8.17). I use ecCodes Version 2.30.2.

image

However, this code works in the same environment:

h = eccodes.codes_bufr_new_from_samples("BUFR4")
eccodes.codes_set(h, "unpack", 1)
p = "dump_bufr_1.txt"
with open(p, "w") as fout:
    eccodes.codes_dump(h, fout, "json")
eccodes.codes_release(h)

When I run the sys.stdout version as as script it works. So the problem seems to be related to jupyter notebooks.