ARPA-SIMC / dballe

Fast on-disk database for meteorological observed and forecast data.
Other
19 stars 6 forks source link

example to use python API #203

Closed pat1 closed 2 years ago

pat1 commented 4 years ago

I need to change report in some bufr messages. bufr are here https://github.com/r-map/rmap/blob/master/python/testdata/arkimet/temp.bufr

I try with this code but it do not work

import dballe

memdb = dballe.DB.connect("mem:")

importer = dballe.Importer("BUFR")
with importer.from_file("temp.bufr") as f:
    with memdb.transaction() as tr:
        tr.import_messages(f)

with memdb.transaction() as tr:
    for rec in tr.query_data({"report":"temp"}):
        data=rec.data
        data["report"]="sound"
        #rec.remove()
        tr.insert_data(data,True,True)
    for rec in tr.query_station_data({"report":"temp"}):
        data=rec.data
        data["report"]="sound"
        #rec.remove()
        tr.insert_station_data(data,True,True)

exporter = dballe.Exporter("BUFR")
with open("sound.bufr", "wb") as outfile:
    with memdb.transaction() as tr:
        for row in tr.query_messages({"report":"sound","p1":0}):
            outfile.write(exporter.to_binary(row.message))
        for row in tr.query_messages({"report":"sound","p1":12*3600}):
            outfile.write(exporter.to_binary(row.message))
        for row in tr.query_messages({"report":"sound","p1":24*3600}):
            outfile.write(exporter.to_binary(row.message))
        for row in tr.query_messages({"report":"sound","p1":36*3600}):
            outfile.write(exporter.to_binary(row.message))
        for row in tr.query_messages({"report":"sound","p1":48*3600}):
            outfile.write(exporter.to_binary(row.message))
        for row in tr.query_messages({"report":"sound","p1":60*3600}):
            outfile.write(exporter.to_binary(row.message))
        for row in tr.query_messages({"report":"sound","p1":72*3600}):
            outfile.write(exporter.to_binary(row.message))
edigiacomo commented 4 years ago

I think that the problem here is that you have set p1 in hours, but it must be specified in seconds

edigiacomo commented 4 years ago

I think that the problem here is that you have set p1 in hours, but it must be specified in seconds

@pat1 could you please test if my solution works for you and then close the issue or else write a feedback?

pat1 commented 4 years ago

ho corretto il programma di test ma ovviamente continua a non funzionare (il problema non è dovuto a P1)

dbamsg dump sound.bufr|grep Report|sort -u 001194 Report mnemonic(CCITTIA5): temp

edigiacomo commented 4 years ago

Immagino sia legato in qualche modo al fatto che "temp" è anche un tipo di BUFR. L'unico modo che ho trovato per forzare il rep_memo nel BUFR di uscita è:

exporter = dballe.Exporter("BUFR")
with open("sound.bufr", "wb") as outfile:
    with memdb.transaction() as tr:
        for row in tr.query_messages({"report":"sound","p1":0}):
            row.message.set_named("rep_memo", "sound")
            outfile.write(exporter.to_binary(row.message))

Ma mi puzza un po' di hack.

spanezz commented 4 years ago

Quello mi sembra il modo corretto se state esportando generici.

Se state esportando BUFR standard, c'è anche l'opzione di settare template_name nell'exporter.

spanezz commented 2 years ago

Dalla discussione qui mi sembra si sia piú o meno chiarita la situazione: chiudo il ticket