Skip to content

Commit

Permalink
Merge pull request #239 from martindurant/grib_1d_coords
Browse files Browse the repository at this point in the history
Fix for lat/lon 1D array in grib2
  • Loading branch information
martindurant authored Oct 24, 2022
2 parents 9761018 + 592d142 commit 54165a5
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions kerchunk/grib2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from ast import Import
import base64
import logging

try:
import cfgrib
except ModuleNotFoundError as err:
if err.name == 'cfgrib': raise ImportError('cfgrib is needed to kerchunk GRIB2 files. Please install it with `conda install -c conda-forge cfgrib`. See https://github.com/ecmwf/cfgrib for more details.')
if err.name == "cfgrib":
raise ImportError(
"cfgrib is needed to kerchunk GRIB2 files. Please install it with "
"`conda install -c conda-forge cfgrib`. See https://github.com/ecmwf/cfgrib "
"for more details."
)

import eccodes
import fsspec
Expand Down Expand Up @@ -186,20 +190,36 @@ def scan_grib(
x = m[coord2]
else:
continue
inline_extra = 0
if isinstance(x, np.ndarray) and x.size == vals.size:
x = x.reshape(vals.shape)
if (
m["gridType"]
in cfgrib.dataset.GRID_TYPES_2D_NON_DIMENSION_COORDS
):
dims = ["x", "y"]
x = x.reshape(vals.shape)
else:
dims = [coord]
if coord == "latitude":
x = x.reshape(vals.shape)[0].copy()
inline_extra = x.nbytes + 1
elif coord == "longitude":
x = x.reshape(vals.shape)[:, 0].copy()
inline_extra = x.nbytes + 1
else:
x = np.array([x])
dims = [coord]
attrs = cfgrib.dataset.COORD_ATTRS[coord]
_store_array(store, z, x, coord, inline_threshold, offset, size, attrs)
_store_array(
store,
z,
x,
coord,
inline_threshold + inline_extra,
offset,
size,
attrs,
)
z[coord].attrs["_ARRAY_DIMENSIONS"] = dims

out.append(
Expand Down

0 comments on commit 54165a5

Please sign in to comment.