From 83c3b88124c38391347f16e09e5edaa6dc99e130 Mon Sep 17 00:00:00 2001 From: Timothy Cera Date: Mon, 1 Jul 2024 01:57:49 -0400 Subject: [PATCH 1/5] fix dtype to work with xarray > 202401 --- src/grib2io/xarray_backend.py | 3 +-- tests/test_to_grib2.py | 37 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/grib2io/xarray_backend.py b/src/grib2io/xarray_backend.py index 2143d79..525f3bb 100755 --- a/src/grib2io/xarray_backend.py +++ b/src/grib2io/xarray_backend.py @@ -7,7 +7,6 @@ from dataclasses import dataclass, field, astuple import itertools import logging -from pathlib import Path import typing import numpy as np @@ -100,7 +99,7 @@ class GribBackendArray(BackendArray): def __init__(self, array, lock): self.array = array self.shape = array.shape - self.dtype = array.dtype + self.dtype = np.dtype(array.dtype) self.lock = lock diff --git a/tests/test_to_grib2.py b/tests/test_to_grib2.py index 52c431f..f2a36ef 100755 --- a/tests/test_to_grib2.py +++ b/tests/test_to_grib2.py @@ -48,7 +48,6 @@ def test_da_write(tmp_path, request): engine="grib2io", filters=filters, ) - Path(target_file).touch() with pytest.raises(FileExistsError): @@ -393,3 +392,39 @@ def test_ds_to_netcdf(tmp_path, request): da1 = ds1["TMP"].sel(indexers=filters) da2 = ds2["TMP"].sel(indexers=filters) _test_any_differences(da1, da2) + + +def test_da_repr(tmp_path, request): + """Test writing a single DataArray to a single grib2 message.""" + target_dir = tmp_path / "test_to_grib2" + target_dir.mkdir() + + datadir = request.config.rootdir / "tests" / "data" / "gfs_20221107" + + filters = { + "productDefinitionTemplateNumber": 0, + "typeOfFirstFixedSurface": 1, + "shortName": "TMP", + } + ds1 = xr.open_dataset( + datadir / "gfs.t00z.pgrb2.1p00.f012_subset", + engine="grib2io", + filters=filters, + ) + assert ( + repr(ds1) + == """ Size: 1MB +Dimensions: (y: 181, x: 360) +Coordinates: + refDate datetime64[ns] 8B ... + leadTime timedelta64[ns] 8B ... + valueOfFirstFixedSurface float64 8B ... + latitude (y, x) float64 521kB ... + longitude (y, x) float64 521kB ... + validDate datetime64[ns] 8B ... +Dimensions without coordinates: y, x +Data variables: + TMP (y, x) float32 261kB ... +Attributes: + engine: grib2io""" + ) From ac210e735a6e8ee8b0f49679a7e499685ec18ec1 Mon Sep 17 00:00:00 2001 From: Timothy Cera Date: Mon, 1 Jul 2024 08:25:09 -0400 Subject: [PATCH 2/5] move print dataset test to new file --- tests/test_print_dataset.py | 37 +++++++++++++++++++++++++++++++++++++ tests/test_to_grib2.py | 37 +------------------------------------ 2 files changed, 38 insertions(+), 36 deletions(-) create mode 100755 tests/test_print_dataset.py diff --git a/tests/test_print_dataset.py b/tests/test_print_dataset.py new file mode 100755 index 0000000..a241766 --- /dev/null +++ b/tests/test_print_dataset.py @@ -0,0 +1,37 @@ +import xarray as xr + + +def test_da_repr(tmp_path, request): + """Test writing a single DataArray to a single grib2 message.""" + target_dir = tmp_path / "test_to_grib2" + target_dir.mkdir() + + datadir = request.config.rootdir / "tests" / "data" / "gfs_20221107" + + filters = { + "productDefinitionTemplateNumber": 0, + "typeOfFirstFixedSurface": 1, + "shortName": "TMP", + } + ds1 = xr.open_dataset( + datadir / "gfs.t00z.pgrb2.1p00.f012_subset", + engine="grib2io", + filters=filters, + ) + assert ( + repr(ds1) + == """ Size: 1MB +Dimensions: (y: 181, x: 360) +Coordinates: + refDate datetime64[ns] 8B ... + leadTime timedelta64[ns] 8B ... + valueOfFirstFixedSurface float64 8B ... + latitude (y, x) float64 521kB ... + longitude (y, x) float64 521kB ... + validDate datetime64[ns] 8B ... +Dimensions without coordinates: y, x +Data variables: + TMP (y, x) float32 261kB ... +Attributes: + engine: grib2io""" + ) diff --git a/tests/test_to_grib2.py b/tests/test_to_grib2.py index f2a36ef..52c431f 100755 --- a/tests/test_to_grib2.py +++ b/tests/test_to_grib2.py @@ -48,6 +48,7 @@ def test_da_write(tmp_path, request): engine="grib2io", filters=filters, ) + Path(target_file).touch() with pytest.raises(FileExistsError): @@ -392,39 +393,3 @@ def test_ds_to_netcdf(tmp_path, request): da1 = ds1["TMP"].sel(indexers=filters) da2 = ds2["TMP"].sel(indexers=filters) _test_any_differences(da1, da2) - - -def test_da_repr(tmp_path, request): - """Test writing a single DataArray to a single grib2 message.""" - target_dir = tmp_path / "test_to_grib2" - target_dir.mkdir() - - datadir = request.config.rootdir / "tests" / "data" / "gfs_20221107" - - filters = { - "productDefinitionTemplateNumber": 0, - "typeOfFirstFixedSurface": 1, - "shortName": "TMP", - } - ds1 = xr.open_dataset( - datadir / "gfs.t00z.pgrb2.1p00.f012_subset", - engine="grib2io", - filters=filters, - ) - assert ( - repr(ds1) - == """ Size: 1MB -Dimensions: (y: 181, x: 360) -Coordinates: - refDate datetime64[ns] 8B ... - leadTime timedelta64[ns] 8B ... - valueOfFirstFixedSurface float64 8B ... - latitude (y, x) float64 521kB ... - longitude (y, x) float64 521kB ... - validDate datetime64[ns] 8B ... -Dimensions without coordinates: y, x -Data variables: - TMP (y, x) float32 261kB ... -Attributes: - engine: grib2io""" - ) From c437a63c5938c37666130f99313ca959e0f9485a Mon Sep 17 00:00:00 2001 From: Timothy Cera Date: Mon, 1 Jul 2024 09:14:10 -0400 Subject: [PATCH 3/5] fixed test that wasn't working if xarray<2024.2.0 --- tests/test_print_dataset.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/test_print_dataset.py b/tests/test_print_dataset.py index a241766..31750b1 100755 --- a/tests/test_print_dataset.py +++ b/tests/test_print_dataset.py @@ -18,9 +18,8 @@ def test_da_repr(tmp_path, request): engine="grib2io", filters=filters, ) - assert ( - repr(ds1) - == """ Size: 1MB + + new_print = """ Dimensions: (y: 181, x: 360) Coordinates: refDate datetime64[ns] 8B ... @@ -29,9 +28,25 @@ def test_da_repr(tmp_path, request): latitude (y, x) float64 521kB ... longitude (y, x) float64 521kB ... validDate datetime64[ns] 8B ... -Dimensions without coordinates: y, x + Dimensions without coordinates: y, x Data variables: TMP (y, x) float32 261kB ... Attributes: engine: grib2io""" - ) + + old_print = """ +Dimensions: (y: 181, x: 360) +Coordinates: + refDate datetime64[ns] ... + leadTime timedelta64[ns] ... + valueOfFirstFixedSurface float64 ... + latitude (y, x) float64 ... + longitude (y, x) float64 ... + validDate datetime64[ns] ... + Dimensions without coordinates: y, x +Data variables: + TMP (y, x) float32 ... +Attributes: + engine: grib2io""" + + assert (repr(ds1) == new_print) or (repr(ds1) == old_print) From 19faa24586b77f2232b99d315098c6a6097a9b09 Mon Sep 17 00:00:00 2001 From: Timothy Cera Date: Mon, 1 Jul 2024 09:27:44 -0400 Subject: [PATCH 4/5] simplify print test --- tests/test_print_dataset.py | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/tests/test_print_dataset.py b/tests/test_print_dataset.py index 31750b1..d189baa 100755 --- a/tests/test_print_dataset.py +++ b/tests/test_print_dataset.py @@ -22,31 +22,11 @@ def test_da_repr(tmp_path, request): new_print = """ Dimensions: (y: 181, x: 360) Coordinates: - refDate datetime64[ns] 8B ... - leadTime timedelta64[ns] 8B ... - valueOfFirstFixedSurface float64 8B ... - latitude (y, x) float64 521kB ... - longitude (y, x) float64 521kB ... - validDate datetime64[ns] 8B ... - Dimensions without coordinates: y, x -Data variables: - TMP (y, x) float32 261kB ... -Attributes: - engine: grib2io""" + refDate""" - old_print = """ + new_print = """ Size: 1MB Dimensions: (y: 181, x: 360) Coordinates: - refDate datetime64[ns] ... - leadTime timedelta64[ns] ... - valueOfFirstFixedSurface float64 ... - latitude (y, x) float64 ... - longitude (y, x) float64 ... - validDate datetime64[ns] ... - Dimensions without coordinates: y, x -Data variables: - TMP (y, x) float32 ... -Attributes: - engine: grib2io""" + refDate""" - assert (repr(ds1) == new_print) or (repr(ds1) == old_print) + assert repr(ds1).startswith(new_print) From fa2c739f9b47b22207d8cd9a9933770376b9872d Mon Sep 17 00:00:00 2001 From: Timothy Cera Date: Mon, 1 Jul 2024 09:38:26 -0400 Subject: [PATCH 5/5] just need to see of repr(ds1) works, not test the results --- tests/test_print_dataset.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/test_print_dataset.py b/tests/test_print_dataset.py index d189baa..ffcb2ae 100755 --- a/tests/test_print_dataset.py +++ b/tests/test_print_dataset.py @@ -19,14 +19,4 @@ def test_da_repr(tmp_path, request): filters=filters, ) - new_print = """ -Dimensions: (y: 181, x: 360) -Coordinates: - refDate""" - - new_print = """ Size: 1MB -Dimensions: (y: 181, x: 360) -Coordinates: - refDate""" - - assert repr(ds1).startswith(new_print) + _ = repr(ds1)