Quantize data to improve compression. Data is quantized using around(scale*data)/scale, where scale is 2**bits, and bits is determined from the least_significant_digit. For example, if least_significant_digit=1, bits will be 4.
(data: npt.ArrayLike, least_significant_digit: int)
| 289 | # truncate data before calling __setitem__, to improve compression ratio |
| 290 | # this function is taken verbatim from netcdf4-python |
| 291 | def quantize(data: npt.ArrayLike, least_significant_digit: int): |
| 292 | """Quantize data to improve compression. |
| 293 | |
| 294 | Data is quantized using around(scale*data)/scale, where scale is |
| 295 | 2**bits, and bits is determined from the least_significant_digit. |
| 296 | |
| 297 | For example, if least_significant_digit=1, bits will be 4. |
| 298 | |
| 299 | """ |
| 300 | exp = -least_significant_digit |
| 301 | exp = math.floor(exp) if exp < 0 else math.ceil(exp) |
| 302 | bits = math.ceil(math.log2(10**-exp)) |
| 303 | scale = 2**bits |
| 304 | datout = np.around(scale * data) / scale |
| 305 | |
| 306 | return datout |
| 307 | |
| 308 | |
| 309 | # Utilities to detect leaked instances. See recipe 14.10 of the Python |