Skip to content

Commit

Permalink
add __len__, add ellipsis to doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit committed Jul 2, 2024
1 parent 1b1a63c commit 256568f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/pygrib/_pygrib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ cdef extern from "numpy/arrayobject.h":
npy_intp PyArray_ISCONTIGUOUS(ndarray arr)
npy_intp PyArray_ISALIGNED(ndarray arr)

cdef extern from "grib_api.h":
cdef extern from "eccodes.h":
ctypedef struct grib_handle
ctypedef struct grib_index
ctypedef struct grib_keys_iterator
Expand Down Expand Up @@ -193,6 +193,7 @@ def _get_grib_api_version():
major = v
return "%d.%d.%d" % (major,minor,revision)
grib_api_version = _get_grib_api_version()
eccodes_version = grib_api_version
if grib_api_version < "2.19.1":
msg="Warning: ecCodes 2.19.1 or higher is recommended. You are running"
warnings.warn('%s %s.' % (msg,grib_api_version))
Expand Down Expand Up @@ -382,6 +383,8 @@ cdef class open(object):
else:
self.has_multi_field_msgs=False
fseek(self._fd, self._offset, SEEK_SET)
def __len__(self):
return self.messages
def __iter__(self):
return self
def __next__(self):
Expand Down
8 changes: 6 additions & 2 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def test():
[1:Precipitation rate:kg m**-2 s**-1 (avg):regular_gg:surface:level 0:fcst time 108-120 hrs (avg):from 200402291200, 2:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 120 hrs:from 200402291200, 3:Maximum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200, 4:Minimum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200]
>>> grbs = pygrib.open('../sampledata/flux.grb')
number of messages in file
>>> len(grbs)
4
acts like a file object
>>> grbs.tell()
0
Expand Down Expand Up @@ -234,8 +238,8 @@ def test():
>>> str('min/max %5.2f %5.2f' % (data.min(), data.max()))
'min/max 295.40 308.10'
>>> grbs = pygrib.open('../sampledata/no-radius-shapeOfEarth-7.grb2')
>>> for grb in grbs: print(grb)
1:Total precipitation:kg m-2 (accum):lambert:surface:level 0:fcst time 15-30 mins (accum):from 201804100000
>>> for grb in grbs: print(grb) # doctest:+ELLIPSIS
1:Total precipitation:kg m-2 (accum):lambert:surface:level 0:fcst time 15...-30... mins (accum):from 201804100000
>>> str(grb.packingType)
'grid_simple'
>>> grbs.close()
Expand Down

0 comments on commit 256568f

Please sign in to comment.