GRIB specificities

[1]:
%matplotlib inline
# for figures in notebook

# import & initialize epygram
import epygram
epygram.init_env()

import os
INPUTS_DIR = os.path.join('..', 'inputs')
[2]:
r = epygram.formats.resource(os.path.join(INPUTS_DIR, 'grid.arome-forecast.guyane0025+0000:00.grib'), 'r')

Several ways of iterating over all fields/messages

[3]:
# Actual iterator, returns H2DField's
n = 0
for f in r:
    n += 1
print(n)
459
[4]:
# Manual iteration on fields
n = 0
while True:
    f = r.iter_fields()
    if f is None:
        break
    elif n == 0:
        print(type(f))
    n += 1
print(n)
0
[5]:
# Manual iterator on messages; returns epygram.formats.GRIB.GRIBmessage objects, i.e. data is not decoded
n = 0
while True:
    m = r.iter_messages()
    if m is None:
        break
    elif n == 0:
        print(type(m))
    n += 1
print(n)
<class 'epygram.formats.GRIB.GRIBmessage'>
459
[6]:
# Pick message at a certain index
m = r.get_message_at_position(15)
print(type(m))
<class 'epygram.formats.GRIB.GRIBmessage'>
[7]:
# in a GRIBmessage, GRIB key/values are read only when requested one by one,
print(list(m.keys()))
print('centre', m.get('centre'))
print('typeOfProcessedData', m['typeOfProcessedData'])
print(list(m.keys()))
# or if the whole message is requested to be read
m.readmessage()
print(list(m.keys())[:10])
[]
centre 85
typeOfProcessedData fc
['centre', 'typeOfProcessedData']
['globalDomain', 'GRIBEditionNumber', 'tablesVersionLatestOfficial', 'tablesVersionLatest', 'grib2divider', 'angleSubdivisions', 'missingValue', 'ieeeFloats', 'isHindcast', 'section0Length']
ECCODES ERROR   :   wrong size (1) for SPD it contains 3 values
ECCODES ERROR   :   wrong size (1) for groupWidths it contains 5764 values
ECCODES ERROR   :   wrong size (1) for groupLengths it contains 5764 values
ECCODES ERROR   :   wrong size (1) for firstOrderValues it contains 5764 values
[8]:
# Message can then be transformed into epygram H2DField
f = m.as_field()
print(type(f))
<class 'epygram.fields.H2DField.H2DField'>
[9]:
# And to find what are the fields present with a partial filter
r.find_fields_in_resource('shortName:2t')
[9]:
[{'editionNumber': 2,
  'name': '2 metre temperature',
  'shortName': '2t',
  'discipline': 0,
  'parameterCategory': 0,
  'parameterNumber': 0,
  'typeOfFirstFixedSurface': 103,
  'level': 2,
  'typeOfSecondFixedSurface': 255,
  'tablesVersion': 15,
  'productDefinitionTemplateNumber': 0}]
[ ]: