How to encode a 3D level #66
-
Where and how to encode the vertical levels of a 3D array into a series of grib messages in the same file: for lev in range(nlev):
msg = grib2io.Grib2Message(discipline=<int>,idsect=<list>)
# what section holds the vertical level information?
msg.end() # Add Section 8 (End Section). This terminates the GRIB2 message in it binary encoded form.
g2file.write(msg) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 16 replies
-
Have you tried this workflow with v2.0 beta of grib2io? Some of the API has changed with v2.0. |
Beta Was this translation helpful? Give feedback.
-
I'm studying the JN mentioned above and adjusted my code to follow it's example. But I'm having a bit of trouble understanding and updating sections 4 and 5 with my info. I've taken a NCEP grib and extracted a message record in order to use it as a template to write my grib. For the other sections, i.e., 1 and 3, updating the parameters is simple. nsg.ny = nlat
nsg.nx = nlon
nsg.latitudeFirstGridpoint = lat_first
nsg.longitudeFirstGridpoint = lon_first
nsg.latitudeLastGridpoint = lat_last
nsg.longitudeLastGridpoint = lon_last
nsg.gridlengthXDirection = xincr
nsg.gridlengthYDirection = -yincr But problems arose when I tried the same in sections 4 and 5. I got around this by assigning my new parameters: type_of_process = 0 # https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table4-3.shtml
# Section 4 encoding
pdt = {'fullName': name, 'units': units, 'shortName': shortname, 'leadTime': leadtime, 'unitOfFirstFixedSurface': 102,
'valueOfFirstFixedSurface': 1.0, 'unitOfSecondFixedSurface': None,
'validDate': datetime.strptime(str(time.values[0]).split('.')[0], '%Y-%m-%dT%H:%M:%S'),
'duration': None, 'level': levels[0], 'parameterCategory': param_category, 'parameterNumber': param_number,
'typeOfGeneratingProcess': type_of_process, 'generatingProcess': 133, 'forecastTime': leadtime,
'typeOfFirstFixedSurface': levelType, 'scaleFactorOfFirstFixedSurface': 0, 'scaledValueOfFirstFixedSurface': 1,
'typeOfSecondFixedSurface': 255, 'scaleFactorOfSecondFixedSurface': 0, 'scaledValueOfSecondFixedSurface': 0}
for attr,key in zip(msg.attrs_by_section(4), pdt.keys()):
try:
setattr(msg, attr, pdt[key])
except:
pass But as you probably already know, some of the parameters cannot be set this way therefore a Section 5 is particularly confusing: dptt = {
'numberOfPackedValues': nlat * nlon,
'dataRepresentationTemplateNumber': 0, #- Grid Point Data - Simple Packing (see Template 5.0)
'typeOfValues': 0, # - Floating Point
'refValue': 0.0,
'binScaleFactor': decimal_scale_factor,
'decScaleFactor': bit_width,
'nBitsPacking': 0}
#msg.section5 = np.zeros([nlat, nlon], dtype=np.float32)
#msg.section5[0] = nlat * nlon
for attr,key in zip(msg.attrs_by_section(5), dptt.keys()):
try:
setattr(msg, attr, dptt[key])
except:
pass Could I get some help in understanding the API in these two areas? |
Beta Was this translation helpful? Give feedback.
-
OK. Lets take 1 section at a time. Lets start at section 4. What template do you need to use for section 4? Keep in mind that not all metadata need to filled in by the user. For example, |
Beta Was this translation helpful? Give feedback.
-
Was thinking about this and I think you already have a similar method in place that can be used as an example. myvalues = {'key': value}
for attr, key in zip(msg.mutable_attrs_by_section(3), myvalues.keys()):
setattr(msg, attr, myvalues[key]) |
Beta Was this translation helpful? Give feedback.
-
Just want to follow up on #66 (reply in thread). |
Beta Was this translation helpful? Give feedback.
The biggest change from grib2io v1.x to v2 is the way you create a Grib2Message object from scratch. Please see the grib2io v2 demo IPython Notebook, specifically the section "Creating Grib2Message Object from Scratch".