pygrib icon indicating copy to clipboard operation
pygrib copied to clipboard

1x1 grids handling

Open exfizik opened this issue 7 years ago • 4 comments

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

1x1sample.zip

exfizik avatar Dec 09 '17 20:12 exfizik

Hmm. A 1x1 array is just a single value, so I guess that's what I would expect.

jswhit avatar Dec 09 '17 21:12 jswhit

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.

exfizik avatar Dec 09 '17 22:12 exfizik

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
()
>>>

jswhit avatar Dec 10 '17 02:12 jswhit

I look at it this way:

a = ones((1,1))
print(a.shape)   # (1,1)

exfizik avatar Dec 10 '17 05:12 exfizik