pygrib
pygrib copied to clipboard
1x1 grids handling
While working with 1x1 (single grid point) grib files, I noticed that .values returns a single value instead of a 1x1 2d array. Is this an expected behavior? Example:
import pygrib
g = pygrib.open("DOWNLOAD_001.grib")
gg = g.message(1)
print(gg.latlons()) # (array([[ 45.]]), array([[ 296.5]]))
print(gg.values) # 24100.0
Hmm. A 1x1 array is just a single value, so I guess that's what I would expect.
Conceptually, yes, it's a single point, but I believe it should return the same data structure regardless of dimension. 1xN grids do not degenerate to 1D arrays, do they? the latlons() method returns 2D arrays. So there is inconsistency here. In my case, I don't know how many points my grid contains so I have to add this special case processing.
It's consistent with numpy slicing
Python 2.7.14 (default, Sep 22 2017, 00:05:22)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> a = ones((10,10))
>>> a.shape
(10, 10)
>>> b = a[1,1]
>>> b.shape
()
>>>
I look at it this way:
a = ones((1,1))
print(a.shape) # (1,1)