Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Fix dimension order to (extra_dim, y, x) as expected by ODC.
Browse files Browse the repository at this point in the history
Signed-off-by: sherrmann <sophie.herrmann@eodc.eu>
  • Loading branch information
sophieherrmann committed Aug 26, 2021
1 parent 020716d commit 5d10342
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/openeo_processes/cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/test_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5d10342

Please sign in to comment.