-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
542bb93
commit 441c2a4
Showing
6 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from dataclasses import dataclass | ||
from typing import Self | ||
|
||
from gribberish import parse_grib_array, parse_grib_message_metadata | ||
from zarr.abc.codec import ArrayBytesCodec | ||
from zarr.core.array_spec import ArraySpec | ||
from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer | ||
from zarr.core.common import JSON, parse_named_configuration | ||
from zarr.registry import register_codec | ||
|
||
|
||
@dataclass(frozen=True) | ||
class GribberishCodec(ArrayBytesCodec): | ||
"""Transform GRIB2 bytes into zarr arrays using gribberish library""" | ||
|
||
var: str | None | ||
|
||
def __init__(self, var: str | None) -> Self: | ||
object.__setattr__(self, "var", var) | ||
|
||
@classmethod | ||
def from_dict(cls, data: dict[str, JSON]) -> Self: | ||
_, configuration_parsed = parse_named_configuration( | ||
data, "gribberish", require_configuration=False | ||
) | ||
configuration_parsed = configuration_parsed or {} | ||
return cls(**configuration_parsed) # type: ignore[arg-type] | ||
|
||
def to_dict(self) -> dict[str, JSON]: | ||
if not self.var: | ||
return {"name": "gribberish"} | ||
else: | ||
return {"name": "gribberish", "configuration": {"var": self.var}} | ||
|
||
async def _decode_single( | ||
self, | ||
chunk_data: Buffer, | ||
chunk_spec: ArraySpec, | ||
) -> NDBuffer: | ||
assert isinstance(chunk_data, Buffer) | ||
chunk_bytes = chunk_data.to_bytes() | ||
|
||
if self.var == 'latitude' or self.var == 'longitude': | ||
message = parse_grib_message_metadata(chunk_bytes, 0) | ||
lat, lng = message.latlng() | ||
data: NDArrayLike = lat if self.var == 'latitude' else lng | ||
else: | ||
data: NDArrayLike = parse_grib_array(chunk_bytes, 0) | ||
|
||
if chunk_spec.dtype != data.dtype: | ||
data = data.astype(chunk_spec.dtype) | ||
if data.shape != chunk_spec.shape: | ||
data = data.reshape(chunk_spec.shape) | ||
|
||
return data | ||
|
||
async def _encode_single( | ||
self, | ||
chunk_data: NDBuffer, | ||
chunk_spec: ArraySpec, | ||
) -> Buffer | None: | ||
# This is a read-only codec | ||
raise NotImplementedError | ||
|
||
register_codec("gribberish", GribberishCodec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
|
||
import numpy as np | ||
|
||
zarr = pytest.importorskip("zarr") | ||
|
||
|
||
async def test_decode_data_var_gribberish(): | ||
from gribberish.zarr.codec import GribberishCodec | ||
from zarr.core.array_spec import ArraySpec | ||
from zarr.core.buffer import default_buffer_prototype | ||
|
||
with open("./../gribberish/tests/data/hrrr.t06z.wrfsfcf01-UGRD.grib2", "rb") as f: | ||
raw_data = f.read() | ||
|
||
buffer = default_buffer_prototype().buffer.from_bytes(raw_data) | ||
codec = GribberishCodec(var="UGRD") | ||
data = await codec._decode_single( | ||
buffer, | ||
ArraySpec( | ||
shape=(1059, 1799), | ||
dtype="float64", | ||
fill_value=0, | ||
order="C", | ||
prototype=np.ndarray, | ||
), | ||
) | ||
|
||
assert data.shape == (1059, 1799) | ||
assert data.dtype == np.dtype("float64") | ||
( | ||
np.testing.assert_almost_equal(data[0][1000], -4.46501350402832), | ||
"Data not decoded correctly", | ||
) |