From 5d10342fb31f801d3c7e4e443391a11bd701c304 Mon Sep 17 00:00:00 2001 From: sherrmann Date: Thu, 26 Aug 2021 15:37:21 +0200 Subject: [PATCH] Fix dimension order to (extra_dim, y, x) as expected by ODC. Signed-off-by: sherrmann --- src/openeo_processes/cubes.py | 10 +++++++++- tests/test_cubes.py | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/openeo_processes/cubes.py b/src/openeo_processes/cubes.py index 389bbcf8..17e1436d 100644 --- a/src/openeo_processes/cubes.py +++ b/src/openeo_processes/cubes.py @@ -625,7 +625,6 @@ def exec_xar(data, output_filepath='out', format='GTiff', options={}, write_prod data format (default: GTiff) """ - def extract_single_timestamp(data_without_time: xr.DataArray, timestamp: datetime = None) -> xr.Dataset: """Create a xarray Dataset.""" coords = {dim: getattr(data_without_time, dim) for dim in data_without_time.dims if dim != 'bands'} @@ -646,6 +645,15 @@ def extract_single_timestamp(data_without_time: xr.DataArray, timestamp: datetim data_var = data_without_time.where(data_without_time != np.nan, -9999) data_var.attrs["nodata"] = -9999 tmp['result'] = (data_var.dims, data_var) + + # fix dimension order + current_dims = tuple(tmp.dims) + additional_dim = list(set(current_dims).difference({"bands", "y", "x"})) + if additional_dim and current_dims != (additional_dim[0], "y", "x"): + tmp = tmp.transpose(additional_dim[0], "y", "x") + elif current_dims != ("y", "x"): + tmp = tmp.transpose("y", "x") + tmp.attrs = data_without_time.attrs # This is a hack! ODC always(!) expectes to have a time dimension # set datetime to now if no other information is available diff --git a/tests/test_cubes.py b/tests/test_cubes.py index c202120c..2631442a 100644 --- a/tests/test_cubes.py +++ b/tests/test_cubes.py @@ -111,6 +111,7 @@ def test_save_result_from_file(self): 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 + assert actual_ds_0.result.dims == ("y", "x") for i in range(10): os.remove(f"out_{str(i).zfill(5)}.nc") os.remove("product.yml")