From dd1c43c05f8e3691cea555c9b8de2e1eb059c47e Mon Sep 17 00:00:00 2001 From: Gustavo Hidalgo Date: Thu, 22 Aug 2024 17:09:46 -0400 Subject: [PATCH 1/4] Fixup attrs --- .gitignore | 1 + xstac/_xstac.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index a22ac7f..1d42d73 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ __pycache__ htmlcov dist .direnv +.venv diff --git a/xstac/_xstac.py b/xstac/_xstac.py index 91cfcd0..f9ce280 100644 --- a/xstac/_xstac.py +++ b/xstac/_xstac.py @@ -339,6 +339,8 @@ def xarray_to_stac( **additional_dimensions: A dictionary with keys ``extent``, ``values``, ``step``. """ + ds = fix_attrs(ds) + temporal_dimension = maybe_use_cf_standard_axis( temporal_dimension, "temporal_dimension", ds ) From b1cf57e24bf5fbafc650d25972e391838ac736a0 Mon Sep 17 00:00:00 2001 From: Gustavo Hidalgo Date: Thu, 22 Aug 2024 17:33:48 -0400 Subject: [PATCH 2/4] Add a unit test --- tests/test_xstac.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_xstac.py b/tests/test_xstac.py index c806611..358a5de 100644 --- a/tests/test_xstac.py +++ b/tests/test_xstac.py @@ -164,3 +164,13 @@ def test_disable_infer_temporal_extent(ds, item_template): # del item_template["properties"]["datetime"] result = xarray_to_stac(ds, item_template, temporal_dimension=False) assert "start_datetime" not in result.properties + +def test_fixup_numpy_attrs_by_default(ds, item_template): + ds.prcp.attrs["values"] = np.zeros(2) + result = xarray_to_stac(ds, item_template, temporal_dimension=False) + assert result.properties["cube:variables"]["prcp"]["attrs"]["values"] == [0.0, 0.0] + try: + import json + json.dumps(result.to_dict()) + except Exception: + pytest.fail("Failed to serialize to JSON") \ No newline at end of file From 8a4d302d53100631355b56460d704e1ed65b1f8f Mon Sep 17 00:00:00 2001 From: Gustavo Hidalgo Date: Fri, 23 Aug 2024 10:41:21 -0400 Subject: [PATCH 3/4] PR comment --- tests/test_xstac.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_xstac.py b/tests/test_xstac.py index 358a5de..6e4c699 100644 --- a/tests/test_xstac.py +++ b/tests/test_xstac.py @@ -1,3 +1,4 @@ +import json import pyproj import pytest import pystac @@ -169,8 +170,4 @@ def test_fixup_numpy_attrs_by_default(ds, item_template): ds.prcp.attrs["values"] = np.zeros(2) result = xarray_to_stac(ds, item_template, temporal_dimension=False) assert result.properties["cube:variables"]["prcp"]["attrs"]["values"] == [0.0, 0.0] - try: - import json - json.dumps(result.to_dict()) - except Exception: - pytest.fail("Failed to serialize to JSON") \ No newline at end of file + json.dumps(result.to_dict()) \ No newline at end of file From b6ad2660c8284015c902762480e5b1899045b646 Mon Sep 17 00:00:00 2001 From: Gustavo Hidalgo Date: Fri, 23 Aug 2024 13:11:00 -0400 Subject: [PATCH 4/4] pre-commit --- tests/test_xstac.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_xstac.py b/tests/test_xstac.py index 6e4c699..096c8d8 100644 --- a/tests/test_xstac.py +++ b/tests/test_xstac.py @@ -166,8 +166,9 @@ def test_disable_infer_temporal_extent(ds, item_template): result = xarray_to_stac(ds, item_template, temporal_dimension=False) assert "start_datetime" not in result.properties + def test_fixup_numpy_attrs_by_default(ds, item_template): ds.prcp.attrs["values"] = np.zeros(2) result = xarray_to_stac(ds, item_template, temporal_dimension=False) assert result.properties["cube:variables"]["prcp"]["attrs"]["values"] == [0.0, 0.0] - json.dumps(result.to_dict()) \ No newline at end of file + json.dumps(result.to_dict())