Skip to content

Commit

Permalink
Add handle property to array field (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandorkertesz authored Sep 18, 2024
1 parent d6e7f47 commit 4bc67d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/earthkit/data/sources/array_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def write(self, f, **kwargs):

write(f, self.to_numpy(flatten=True), self._metadata, **kwargs)

@property
def handle(self):
return self._metadata._handle

def __getstate__(self) -> dict:
ret = {}
ret["_array"] = self._array
Expand Down Expand Up @@ -232,14 +236,6 @@ def __setstate__(self, state: dict):
array_backend = ensure_backend(array_backend)
super().__init__(array_backend, raw_values_backend=array_backend, metadata=self._metadata)

# def __getstate__(self):
# for f in self:

# def __setstate__(self, state):
# self._array = state[0]
# self._metadata = state[1]
# self.array_backend = state[2]


# class MultiUnwindMerger:
# def __init__(self, sources):
Expand Down
29 changes: 29 additions & 0 deletions tests/grib/test_grib_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,35 @@ def test_grib_output_tp():
assert np.allclose(ds[0].to_numpy(), data, rtol=EPSILON, atol=EPSILON)


@pytest.mark.skipif(
sys.version_info < (3, 10),
reason="ignore_cleanup_errors requires Python 3.10 or later",
)
@pytest.mark.parametrize("array", [True, False])
def test_grib_output_field_template(array):
data = np.random.random((7, 12))

with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp:
ds = from_source("file", earthkit_examples_file("test6.grib"))
if array:
ds = ds.to_fieldlist()

path = os.path.join(tmp, "a.grib")
f = earthkit.data.new_grib_output(path, template=ds[0], date=20010101)
f.write(data, param="pt", bitsPerValue=16)
f.close()

ds = earthkit.data.from_source("file", path)

assert ds[0].metadata("date") == 20010101
assert ds[0].metadata("param") == "pt"
assert ds[0].metadata("levtype") == "pl"
assert ds[0].metadata("edition") == 1
assert ds[0].metadata("generatingProcessIdentifier") == 255

assert np.allclose(ds[0].to_numpy(), data, rtol=1e-2, atol=1e-2)


if __name__ == "__main__":
from earthkit.data.testing import main

Expand Down

0 comments on commit 4bc67d4

Please sign in to comment.