From 15a672d257e8cdb2b8478f80ece4294393e45ba0 Mon Sep 17 00:00:00 2001 From: sherrmann Date: Thu, 26 Aug 2021 09:56:07 +0200 Subject: [PATCH] Ensure crs attr is always available in saved files. Signed-off-by: sherrmann --- src/openeo_processes/cubes.py | 3 +++ tests/test_cubes.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/openeo_processes/cubes.py b/src/openeo_processes/cubes.py index 8a8e1481..340b5f96 100644 --- a/src/openeo_processes/cubes.py +++ b/src/openeo_processes/cubes.py @@ -658,6 +658,9 @@ def extract_single_timestamp(data_without_time: xr.DataArray, timestamp: datetim # This is a hack! ODC always(!) expectes to have a time dimension # set datetime to now if no other information is available tmp.attrs["datetime_from_dim"] = str(timestamp) if timestamp else str(datetime.now()) + if "crs" not in tmp.attrs: + first_data_var = tmp.data_vars[list(tmp.data_vars.keys())[0]] + tmp.attrs["crs"] = first_data_var.geobox.crs.to_wkt() return tmp def refactor_data(data: xr.DataArray) -> List[xr.Dataset]: diff --git a/tests/test_cubes.py b/tests/test_cubes.py index 999582de..3586576a 100644 --- a/tests/test_cubes.py +++ b/tests/test_cubes.py @@ -113,6 +113,8 @@ def test_save_result_from_file(self): assert ref_ds_0.variables == actual_ds_0.variables assert ref_ds_0.geobox == actual_ds_0.geobox assert ref_ds_0.extent == actual_ds_0.extent + assert "crs" in actual_ds_0.attrs and actual_ds_0.attrs["crs"] == 'PROJCRS["Azimuthal_Equidistant",BASEGEOGCRS["WGS 84",DATUM["World Geodetic System 1984",ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ID["EPSG",6326]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]]],CONVERSION["unnamed",METHOD["Modified Azimuthal Equidistant",ID["EPSG",9832]],PARAMETER["Latitude of natural origin",53,ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8801]],PARAMETER["Longitude of natural origin",24,ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8802]],PARAMETER["False easting",5837287.81977,LENGTHUNIT["metre",1],ID["EPSG",8806]],PARAMETER["False northing",2121415.69617,LENGTHUNIT["metre",1],ID["EPSG",8807]]],CS[Cartesian,2],AXIS["easting",east,ORDER[1],LENGTHUNIT["metre",1,ID["EPSG",9001]]],AXIS["northing",north,ORDER[2],LENGTHUNIT["metre",1,ID["EPSG",9001]]]]' + assert "datetime_from_dim" in actual_ds_0.attrs for i in range(10): os.remove(f"out_{str(i).zfill(5)}.nc") os.remove("product.yml")