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

Commit

Permalink
Merge pull request #47 from Open-EO/fix-save_result
Browse files Browse the repository at this point in the history
Fix save result dimensions
  • Loading branch information
sophieherrmann authored Aug 26, 2021
2 parents b8d773a + 5d10342 commit 8b64dc2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/openeo_processes/cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,35 +625,35 @@ 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,
additional_dims: List[str] = None) -> xr.Dataset:
def extract_single_timestamp(data_without_time: xr.DataArray, timestamp: datetime = None) -> xr.Dataset:
"""Create a xarray Dataset."""
coords = {'y': data_without_time.y, 'x': data_without_time.x}
dims_tmp = ['y', 'x']
if additional_dims:
for dim in additional_dims:
coords[dim] = getattr(data_without_time, dim)
dims_tmp.append(dim)
dims = tuple(dims_tmp)

coords = {dim: getattr(data_without_time, dim) for dim in data_without_time.dims if dim != 'bands'}
tmp = xr.Dataset(coords=coords)
if 'bands' in data_without_time.coords:
try:
for var in data_without_time['bands'].values:
data_var = data_without_time.loc[dict(bands=var)]\
.where(data_without_time.loc[dict(bands=var)] != np.nan, -9999)
data_var.attrs["nodata"] = -9999
tmp[str(var)] = (dims, data_var)
tmp[str(var)] = (data_var.dims, data_var)
except Exception as e:
print(e)
data_var = data_without_time.where(data_without_time != np.nan, -9999)
data_var.attrs["nodata"] = -9999
tmp[str((data_without_time['bands'].values))] = (dims, data_var)
tmp[str((data_without_time['bands'].values))] = (data_var.dims, data_var)
else:
data_var = data_without_time.where(data_without_time != np.nan, -9999)
data_var.attrs["nodata"] = -9999
tmp['result'] = (dims, data_var)
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 All @@ -667,13 +667,12 @@ def refactor_data(data: xr.DataArray) -> List[xr.Dataset]:
"""Recreate a Dataset from the final result as Dataarray, to ensure a well formatted netCDF."""
all_tmp = []
# TODO this must be improved once `rename_dimension` is supported!
additional_dims = set(data.dims).difference({'bands', 'y', 'x', 'time'})
if 'time' in data.coords:
for timestamp in data.time.values:
data_at_timestamp = data.loc[dict(time=timestamp)]
all_tmp.append(extract_single_timestamp(data_at_timestamp, timestamp, additional_dims))
all_tmp.append(extract_single_timestamp(data_at_timestamp, timestamp))
else:
all_tmp.append(extract_single_timestamp(data, additional_dims=additional_dims))
all_tmp.append(extract_single_timestamp(data))

return all_tmp

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 8b64dc2

Please sign in to comment.