Skip to content

Commit c3202e7

Browse files
committed
remove redundant bands from SH results file when transforming to parsed files
1 parent dc8df47 commit c3202e7

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

rest/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,12 @@ def add_job_to_queue(job_id):
597597
# START OF POST_PROCESSING
598598
# post-process gtiffs to appropriate formats
599599

600-
parse_sh_gtiff_to_format(job, bucket)
600+
try:
601+
parse_sh_gtiff_to_format(job, bucket)
602+
except Exception as e:
603+
print("parsing didn't succeed")
604+
print(e)
605+
601606

602607
# END OF POST_PROCESSING
603608

rest/post_processing/gtiff_parser.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
from processing.const import CustomMimeType
1111
from openeoerrors import Internal
1212

13+
import json
14+
1315
# assume it's only 1 time and 1 bands dimension
1416
def check_dimensions(time_dimensions, bands_dimensions):
1517
if len(time_dimensions) == 0:
16-
raise Internal("No time dimensions exist. Only 1 time dimension is supported.")
18+
print("No time dimensions exist. Only 1 time dimension is supported.")
19+
# raise Internal("No time dimensions exist. Only 1 time dimension is supported.")
1720

1821
if len(time_dimensions) > 1:
1922
raise Internal("More than 1 time dimension exist. Only 1 time dimension is supported.")
@@ -29,18 +32,52 @@ def get_timestamps_arrays(datacube_time_as_bands, time_dimensions, bands_dimensi
2932
num_of_img_bands = len(datacube_time_as_bands["band"])
3033
num_of_bands_dimension = len(bands_dimensions[0]["labels"])
3134

35+
num_time_labels = 0
36+
for time_dim in time_dimensions:
37+
num_time_labels += len(time_dim["labels"])
38+
39+
num_band_labels = 0
40+
for band_dim in bands_dimensions:
41+
num_band_labels += len(band_dim["labels"])
42+
43+
num_actual_img_bands = (num_time_labels or 1) * (num_band_labels or 1)
44+
3245
list_of_timestamps = []
3346
list_of_timestamp_arrays = []
3447

35-
for i in range(0, num_of_img_bands, num_of_bands_dimension):
36-
date = time_dimensions[0]["labels"][int(i / num_of_bands_dimension)]
48+
printdata = {
49+
"data": datacube_time_as_bands,
50+
"data_length": len(datacube_time_as_bands),
51+
"data_len/num_of_bands_dimension": len(datacube_time_as_bands) / num_of_bands_dimension,
52+
"bands_dimensions": bands_dimensions,
53+
"time_dimensions": time_dimensions,
54+
"num_img_bands": num_of_img_bands,
55+
"num_bands_dim": num_of_bands_dimension,
56+
"range": range(0, num_actual_img_bands, num_of_bands_dimension),
57+
"num_actual_img_bands": num_actual_img_bands,
58+
"num_band_labels": num_band_labels,
59+
"num_time_labels": num_time_labels,
60+
}
61+
62+
print("get_timestamps_arrays")
63+
print(json.dumps(printdata, sort_keys=True, indent=4, default=str))
64+
65+
for i in range(0, num_actual_img_bands, num_of_bands_dimension):
66+
67+
print("for loop", {
68+
"time_dims": time_dimensions,
69+
"time_dim_index": int(i / num_of_bands_dimension)
70+
})
71+
72+
73+
date = time_dimensions[0]["labels"][int(i / num_of_bands_dimension)] if num_time_labels > 0 else ""
3774
timestamp_array = datacube_time_as_bands[i : i + num_of_bands_dimension]
3875

3976
if output_format in [CustomMimeType.NETCDF, CustomMimeType.ZARR]:
40-
pandas_time = pd.to_datetime(parser.parse(date))
4177
timestamp_array = timestamp_array.assign_coords(band=bands_dimensions[0]["labels"])
42-
timestamp_array = timestamp_array.assign_coords(t=pandas_time)
43-
timestamp_array = timestamp_array.expand_dims(dim="t")
78+
if num_time_labels > 0:
79+
timestamp_array = timestamp_array.assign_coords(t=pd.to_datetime(parser.parse(date)))
80+
timestamp_array = timestamp_array.expand_dims(dim="t")
4481

4582
list_of_timestamps.append(date)
4683
list_of_timestamp_arrays.append(timestamp_array)
@@ -51,7 +88,8 @@ def get_timestamps_arrays(datacube_time_as_bands, time_dimensions, bands_dimensi
5188
def save_as_gtiff(list_of_timestamps, list_of_timestamp_arrays, output_dir, output_name):
5289
output_file_paths = []
5390
for array, date in zip(list_of_timestamp_arrays, list_of_timestamps):
54-
file_name = f"{output_name['name']}_{date}{output_name['ext']}"
91+
date_string = "" if date == "" else f"_{date}"
92+
file_name = f"{output_name['name']}{date_string}{output_name['ext']}"
5593
file_path = os.path.join(output_dir, file_name)
5694
output_file_paths.append(file_path)
5795

@@ -92,6 +130,9 @@ def parse_multitemporal_gtiff_to_format(input_tiff, input_metadata, output_dir,
92130
if len(bands_dimensions) == 0:
93131
bands_dimensions = [{"name": "bands", "type": "bands", "labels": ["results"]}]
94132

133+
# if len(time_dimensions) == 0:
134+
# time_dimensions = [{"name": "t", "type": "temporal", "labels": [""]}]
135+
95136
check_dimensions(time_dimensions, bands_dimensions)
96137

97138
list_of_timestamps, list_of_timestamp_arrays = get_timestamps_arrays(

0 commit comments

Comments
 (0)