-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from dcs4cop/forman-92-xcube_prune
Forman 92 xcube prune
- Loading branch information
Showing
7 changed files
with
372 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import os.path | ||
import sys | ||
import numpy as np | ||
import xarray as xr | ||
|
||
from test.cli.helpers import CliTest | ||
from xcube.api import assert_cube, new_cube | ||
from xcube.api.readwrite import write_cube | ||
from xcube.cli.prune import _delete_block_file | ||
from xcube.util.dsio import rimraf | ||
|
||
|
||
class PruneTest(CliTest): | ||
|
||
def test_help_option(self): | ||
result = self.invoke_cli(['prune', '--help']) | ||
self.assertEqual(0, result.exit_code) | ||
|
||
|
||
class PruneDataTest(CliTest): | ||
TEST_CUBE = "test.zarr" | ||
|
||
def setUp(self) -> None: | ||
rimraf(self.TEST_CUBE) | ||
cube = new_cube(time_periods=3, | ||
variables=dict(precipitation=np.nan, | ||
temperature=np.nan)).chunk(dict(time=1, lat=90, lon=90)) | ||
|
||
write_cube(cube, self.TEST_CUBE, "zarr", cube_asserted=True) | ||
|
||
def tearDown(self) -> None: | ||
rimraf(self.TEST_CUBE) | ||
|
||
def test_dry_run(self): | ||
result = self.invoke_cli(['prune', self.TEST_CUBE, "--dry-run"]) | ||
self.assertEqual(0, result.exit_code) | ||
self.assertEqual("Opening cube from 'test.zarr'...\n" | ||
"Identifying empty blocks...\n" | ||
"Deleting 24 empty block file(s) for variable 'precipitation'...\n" | ||
"Deleting 24 empty block file(s) for variable 'temperature'...\n" | ||
"Done, 48 block file(s) deleted.\n", | ||
result.stdout) | ||
expected_file_names = sorted(['.zarray', | ||
'.zattrs', | ||
'0.0.0', '0.0.1', '0.0.2', '0.0.3', '0.1.0', '0.1.1', '0.1.2', '0.1.3', | ||
'1.0.0', '1.0.1', '1.0.2', '1.0.3', '1.1.0', '1.1.1', '1.1.2', '1.1.3', | ||
'2.0.0', '2.0.1', '2.0.2', '2.0.3', '2.1.0', '2.1.1', '2.1.2', '2.1.3']) | ||
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/precipitation'))) | ||
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/temperature'))) | ||
ds = xr.open_zarr('test.zarr') | ||
assert_cube(ds) | ||
self.assertIn('precipitation', ds) | ||
self.assertEqual((3, 180, 360), ds.precipitation.shape) | ||
self.assertEqual(('time', 'lat', 'lon'), ds.precipitation.dims) | ||
self.assertIn('temperature', ds) | ||
self.assertEqual((3, 180, 360), ds.temperature.shape) | ||
self.assertEqual(('time', 'lat', 'lon'), ds.temperature.dims) | ||
|
||
def test_no_dry_run(self): | ||
result = self.invoke_cli(['prune', self.TEST_CUBE]) | ||
self.assertEqual(0, result.exit_code) | ||
self.assertEqual("Opening cube from 'test.zarr'...\n" | ||
"Identifying empty blocks...\n" | ||
"Deleting 24 empty block file(s) for variable 'precipitation'...\n" | ||
"Deleting 24 empty block file(s) for variable 'temperature'...\n" | ||
"Done, 48 block file(s) deleted.\n", | ||
result.stdout) | ||
expected_file_names = sorted(['.zarray', '.zattrs']) | ||
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/precipitation'))) | ||
self.assertEqual(expected_file_names, sorted(os.listdir('test.zarr/temperature'))) | ||
ds = xr.open_zarr('test.zarr') | ||
assert_cube(ds) | ||
self.assertIn('precipitation', ds) | ||
self.assertEqual((3, 180, 360), ds.precipitation.shape) | ||
self.assertEqual(('time', 'lat', 'lon'), ds.precipitation.dims) | ||
self.assertIn('temperature', ds) | ||
self.assertEqual((3, 180, 360), ds.temperature.shape) | ||
self.assertEqual(('time', 'lat', 'lon'), ds.temperature.dims) | ||
|
||
def test_delete_block_file(self): | ||
actual_message = None | ||
|
||
def monitor(message): | ||
nonlocal actual_message | ||
actual_message = message | ||
|
||
actual_message = None | ||
ok = _delete_block_file(self.TEST_CUBE, 'precipitation', (0, 3, 76), True, monitor=monitor) | ||
self.assertFalse(ok) | ||
self.assertEqual(f"error: could neither find block file " | ||
f"{os.path.join(self.TEST_CUBE, 'precipitation', '0.3.76')} nor " | ||
f"{os.path.join(self.TEST_CUBE, 'precipitation', '0', '3', '76')}", actual_message) | ||
|
||
actual_message = None | ||
ok = _delete_block_file(self.TEST_CUBE, 'precipitation', (1, 1, 0), True, monitor=monitor) | ||
self.assertTrue(ok) | ||
self.assertEqual(None, actual_message) | ||
|
||
actual_message = None | ||
ok = _delete_block_file(self.TEST_CUBE, 'precipitation', (1, 1, 0), False, monitor=monitor) | ||
self.assertTrue(ok) | ||
self.assertEqual(None, actual_message) | ||
self.assertFalse(os.path.exists(os.path.join(self.TEST_CUBE, 'precipitation', '1.1.0'))) | ||
|
||
if sys.platform == 'win32': | ||
path = os.path.join(self.TEST_CUBE, 'precipitation', '1.1.1') | ||
# Open block, so we cannot delete (Windows only) | ||
# noinspection PyUnusedLocal | ||
with open(path, 'wb') as fp: | ||
actual_message = None | ||
ok = _delete_block_file(self.TEST_CUBE, 'precipitation', (1, 1, 1), False, monitor=monitor) | ||
self.assertFalse(ok) | ||
self.assertIsNotNone(actual_message) | ||
self.assertTrue(actual_message.startswith(f'error: failed to delete block file {path}: ')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.