Skip to content

Commit b0ba816

Browse files
committed
netCDF: address TODO and update/fix more batch job tests #1111
1 parent 369eb88 commit b0ba816

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

openeogeotrellis/geopysparkdatacube.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,7 @@ def add_gdalinfo_objects(assets_original):
22882288
)
22892289
return return_netcdf_items(java_items, bands, nodata)
22902290

2291-
else: # TODO: return items from this code path
2291+
else:
22922292
if not tiled:
22932293
result=self._collect_as_xarray(max_level, crop_bounds, crop_dates)
22942294
else:
@@ -2299,11 +2299,20 @@ def add_gdalinfo_objects(assets_original):
22992299
asset = {
23002300
"href": filename,
23012301
"roles": ["data"],
2302-
"type": "application/x-netcdf"
2302+
"type": "application/x-netcdf",
23032303
}
23042304
if bands is not None:
23052305
asset["bands"] = bands
2306-
return {filename_tmp: asset}
2306+
2307+
item_id = str(uuid.uuid4())
2308+
return {
2309+
item_id: {
2310+
"id": item_id,
2311+
"assets": {
2312+
"openEO": asset,
2313+
},
2314+
}
2315+
}
23072316

23082317
elif format == "JSON":
23092318
# saving to json, this is potentially big in memory

tests/test_download.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,10 @@ def test_write_assets_samples_tile_grid_batch(self, tmp_path):
232232
@pytest.mark.parametrize("space_type", ["spacetime", "spatial"])
233233
@pytest.mark.parametrize("stitch", [False, True])
234234
@pytest.mark.parametrize("catalog", [False, True])
235-
@pytest.mark.parametrize("sample_by_feature", [False, True])
236235
@pytest.mark.parametrize("format_arg", ["netCDF"]) # "GTIFF" behaves different from "netCDF", so not testing now
237236
def test_write_assets_parameterize_batch(self, tmp_path, imagecollection_with_two_bands_and_three_dates,
238237
imagecollection_with_two_bands_spatial_only,
239-
format_arg, sample_by_feature, catalog, stitch, space_type,
238+
format_arg, catalog, stitch, space_type,
240239
tile_grid, filename_prefix):
241240
d = locals()
242241
d = {i: d[i] for i in d if i != 'self' and i != "tmp_path" and i != "d"}
@@ -248,7 +247,7 @@ def test_write_assets_parameterize_batch(self, tmp_path, imagecollection_with_tw
248247
imagecollection = imagecollection_with_two_bands_spatial_only
249248

250249
geometries = geojson_to_geometry(self.features)
251-
assets = imagecollection.write_assets(
250+
items = imagecollection.write_assets(
252251
str(tmp_path / "ignored<\0>.extension"), # null byte to cause error if filename would be written to fs
253252
format=format_arg,
254253
format_options={
@@ -261,6 +260,8 @@ def test_write_assets_parameterize_batch(self, tmp_path, imagecollection_with_tw
261260
"tile_grid": tile_grid,
262261
}
263262
)
263+
264+
assets = [(asset_key, asset) for item in items.values() for asset_key, asset in item["assets"].items()]
264265
with open(self.test_write_assets_parameterize_batch_path + test_name + ".json", 'w') as fp:
265266
json.dump(assets, fp, indent=2)
266267

@@ -269,13 +270,15 @@ def test_write_assets_parameterize_batch(self, tmp_path, imagecollection_with_tw
269270
else:
270271
extension = ".tif"
271272
assert len(assets) >= 3
272-
assert len(assets) <= geometries.length
273+
assert len(assets) <= geometries.length # a netCDF asset contains all dates
274+
assert {asset_key for asset_key, _ in assets} == {"openEO"}
275+
asset_filenames = {Path(asset["href"]).name for _, asset in assets}
273276
if format_arg == "netCDF":
274277
if filename_prefix:
275-
assert assets[filename_prefix + "_0" + extension]
278+
assert filename_prefix + "_0" + extension in asset_filenames
276279
else:
277-
assert assets["openEO_0" + extension]
278-
name, asset = next(iter(assets.items()))
280+
assert "openEO_0" + extension in asset_filenames
281+
_, asset = assets[0]
279282
assert Path(asset['href']).parent == tmp_path
280283
if filename_prefix:
281284
assert filename_prefix in asset['href']
@@ -340,7 +343,7 @@ def test_write_assets_parameterize(self, tmp_path, imagecollection_with_two_band
340343
assert False
341344
filename = "test_download_result" + extension
342345
geometries = geojson_to_geometry(self.features)
343-
assets_all = imagecollection.write_assets(
346+
items_all = imagecollection.write_assets(
344347
str(tmp_path / filename),
345348
format=format_arg,
346349
format_options={
@@ -365,18 +368,19 @@ def test_write_assets_parameterize(self, tmp_path, imagecollection_with_two_band
365368
# with open(self.test_write_assets_parameterize_path + test_name + ".json", 'w') as fp:
366369
# json.dump(assets, fp, indent=2)
367370

368-
assets_data = {k: v for (k, v) in assets_all.items() if "data" in v["roles"]}
369-
name, asset = next(iter(assets_data.items()))
371+
assets_all = [(asset_key, asset) for item in items_all.values() for asset_key, asset in item["assets"].items()]
372+
assets_data = [(asset_key, asset) for asset_key, asset in assets_all if "data" in asset["roles"]]
373+
name, asset = assets_data[0]
370374
print("href of first asset: " + asset["href"])
371-
assets_metadata = {k: v for (k, v) in assets_all.items() if "data" not in v["roles"]}
375+
assets_metadata = [(asset_key, asset) for asset_key, asset in assets_all if "data" not in asset["roles"]]
372376
if format_arg == "GTIFF" and not catalog:
373377
if attach_gdalinfo_assets:
374378
assert len(assets_metadata) == len(assets_data)
375379
else:
376380
assert len(assets_metadata) == 0
377381

378382
if len(assets_data) == 1:
379-
assert assets_data[filename]
383+
assert assets_data[0][0] == "openEO"
380384
assert filename in asset['href']
381385
else:
382386
if filename_prefix:

0 commit comments

Comments
 (0)