NOAA-MDL / grib2io

Python interface to the NCEP G2C Library for reading and writing GRIB2 messages.
https://noaa-mdl.github.io/grib2io
MIT License
30 stars 11 forks source link

Writing GRIB2 Messages Examples #73

Closed cjmelick closed 1 year ago

cjmelick commented 1 year ago

Greetings,

I have utilized the init(), addgrid(), and addfield() methods to create a new GRIB2 message with all of the constituent arguments required for each but it seems like my created message is not complete when trying to do some testing with decoding and was curious if there is a location which shows examples of generating GRIB2 products/data from scratch?

Thanks, Chris

EricEngle-NOAA commented 1 year ago

Hi Chris,

The methods you listed are a part of the grib2io v1.x API where init() creates the message (sections 0 and 1), addgrid() adds section 3, and addfield() adds sections 4, 5, 6, 7. There is 1 more method to formally end the grid message, end(), which adds section 8 (a string of '7777'). Note that the workflow for creating new GRIB2 message in v2.x will have changed significantly.

cjmelick commented 1 year ago

Hi Eric,

I did just add in the end() method but it seems like that all parts of the message are not filled out and so I have problems accessing the data array I just created. Also, I get no errors when setting up all of the methods for the new GRIB2 message. Has this issue been seen before?

Thanks, Chris

EricEngle-NOAA commented 1 year ago

Feel free to share your code here. It is possible there was an issue in addgrid or addfield that didnt make it back to you.

cjmelick commented 1 year ago

Okay, will do. I need to clean up my code some.

cjmelick commented 1 year ago

Eric, here is my code. I was just trying to build up from scratch an existing GRIB2 message from the GFS model. Thanks for your assistance!

!/usr/bin/env python3

import grib2io import sys import numpy as np import re // gfsfile --> 24-hour forecast GFS 0.5deg gridded data file in GRIB2 format obj=grib2io.open(gfsfile) msg = obj[1][0] dat = msg.data() //fullName = Pressure Reduced to MSL for msg #1 newmsg = grib2io.Grib2Message newmsg.init(newmsg,discipline=msg.discipline.value,idsect=msg.identificationSection) newmsg.addgrid(newmsg,msg.gridDefinitionSection,msg.gridDefinitionTemplate) newmsg.addfield(newmsg,dat,msg.productDefinitionTemplateNumber,msg.productDefinitionTemplate,packing='simple', binScaleFactor = msg.binScaleFactor, decScaleFactor = msg.decScaleFactor) newmsg.end(newmsg) print(newmsg) print(newmsg.data(newmsg))

EricEngle-NOAA commented 1 year ago

Try this...

#!/usr/bin/env python3
import grib2io
import sys
import numpy as np
import re

gfsfile = <GRIB2 File>
obj=grib2io.open(gfsfile)
msg = obj[1][0]
print(msg)
dat = msg.data()

newmsg = grib2io.Grib2Message(discipline=msg.discipline.value,idsect=msg.identificationSection)
newmsg.addgrid(msg.gridDefinitionSection,msg.gridDefinitionTemplate)
newmsg.addfield(dat,msg.productDefinitionTemplateNumber,msg.productDefinitionTemplate,
                packing='simple', binScaleFactor = msg.binScaleFactor, decScaleFactor = msg.decScaleFactor)
newmsg.end()
print(newmsg)
newmsg.unpack() # Necessary for newmsg to have attributes.
newdat = newmsg.data()
print(newdat)
print(np.nanmin(newdat),np.nanmax(newdat))

The main differences between yours and mine are the instantiation of the new Grib2Message object and calling each method. newmsg = grib2io.Grib2Message(...) returns an instance of Grib2Message as var newmsg. From there you call each method from the reference point of the instance var name (newmsg). You do not need to pass newmsg as the first argument of each method call.

Note that once the new Grib2Message has been created and finalized with .end(), to use the attributes of the new message, you will need to run .unpack().

Keep in mind that this workflow to create a new Grib2Message will change drastically in grib2io v2.X.

cjmelick commented 1 year ago

Hi Eric, that makes perfect sense! I completely understand what you did -- and sometimes it just takes another pair of eyes and some time away from the issue!

Thanks once again! Chris P.S. I will try what you suggested and will let you know one way or the other how things stand soon.

cjmelick commented 1 year ago

Hi Eric,

I had success with what you provided as a solution! This issue can be closed.

Thanks again! Chris