Skip to content

Commit 8b2dbc7

Browse files
committed
dev
1 parent 66be46d commit 8b2dbc7

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

cf/test/test_quantization.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import faulthandler
44
import os
5+
import shutil
56
import tempfile
67
import unittest
78

@@ -20,6 +21,13 @@
2021
]
2122
[tmpfile1, tmpfile2] = tmpfiles
2223

24+
# Set up temporary directories
25+
tmpdirs = [
26+
tempfile.mkdtemp("_test_quantization.zarr", dir=os.getcwd())
27+
for i in range(1)
28+
]
29+
[tmpdir] = tmpdirs
30+
2331

2432
def _remove_tmpfiles():
2533
"""Remove temporary files created during tests."""
@@ -29,6 +37,13 @@ def _remove_tmpfiles():
2937
except OSError:
3038
pass
3139

40+
for d in tmpdirs:
41+
try:
42+
shutil.rmtree(d)
43+
os.rmdir(d)
44+
except OSError:
45+
pass
46+
3247

3348
atexit.register(_remove_tmpfiles)
3449

@@ -222,6 +237,24 @@ def test_quantization_copy(self):
222237
g = f.copy()
223238
self.assertTrue(g.get_quantization().equals(q))
224239

240+
def test_quantization_backends(self):
241+
"""Test that quantization-on-write with different backends."""
242+
f = self.f1.copy()
243+
f.set_quantize_on_write(
244+
algorithm="granular_bitround", quantization_nsd=8
245+
)
246+
247+
# Backends that allow quantisation-on-write
248+
for backend in ("netCDF4",):
249+
cf.write(f, tmpfile1, netcdf_backend=backend)
250+
251+
# Backends that do not allow quantisation-on-write
252+
with self.assertRaises(NotImplementedError):
253+
cf.write(f, tmpdir, fmt="ZARR3", netcdf_backend="zarr")
254+
255+
with self.assertRaises(NotImplementedError):
256+
cf.write(f, tmpfile1, netcdf_backend="h5netcdf-h5py")
257+
225258

226259
if __name__ == "__main__":
227260
print("Run date:", datetime.datetime.now())

cf/test/test_read_write.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,20 +1025,29 @@ def test_write_h5py_options(self):
10251025

10261026
cf.write(
10271027
f,
1028-
tmpfile0,
1028+
tmpfile1,
10291029
netcdf_backend="h5netcdf-h5py",
10301030
h5py_options=h5py_options,
10311031
)
1032-
self.assertTrue(os.path.getsize(tmpfile0) > size)
1033-
1034-
cf.write(
1035-
f, tmpfile1, netcdf_backend="netCDF4", h5py_options=h5py_options
1036-
)
1032+
self.assertTrue(os.path.getsize(tmpfile1) > size)
10371033

10381034
f0 = cf.read(tmpfile0)[0]
10391035
f1 = cf.read(tmpfile1)[0]
10401036
self.assertTrue(f1.equals(f0))
10411037

1038+
with self.assertRaises(ValueError):
1039+
cf.write(
1040+
f,
1041+
tmpfile0,
1042+
netcdf_backend="netCDF4",
1043+
h5py_options=h5py_options,
1044+
)
1045+
1046+
with self.assertRaises(ValueError):
1047+
cf.write(
1048+
f, tmpfile0, fmt="NETCDF3_CLASSIC", h5py_options=h5py_options
1049+
)
1050+
10421051
def test_read_netcdf_file(self):
10431052
"""Test cf.read for differing the netcdf_file backend."""
10441053
f = self.f0

0 commit comments

Comments
 (0)