(
variable: Variable,
raise_on_invalid=False,
lsd_okay=True,
h5py_okay=False,
backend="netCDF4",
unlimited_dims=None,
)
| 253 | |
| 254 | |
| 255 | def _extract_nc4_variable_encoding( |
| 256 | variable: Variable, |
| 257 | raise_on_invalid=False, |
| 258 | lsd_okay=True, |
| 259 | h5py_okay=False, |
| 260 | backend="netCDF4", |
| 261 | unlimited_dims=None, |
| 262 | ) -> dict[str, Any]: |
| 263 | if unlimited_dims is None: |
| 264 | unlimited_dims = () |
| 265 | |
| 266 | encoding = variable.encoding.copy() |
| 267 | |
| 268 | safe_to_drop = {"source", "original_shape"} |
| 269 | valid_encodings = { |
| 270 | "zlib", |
| 271 | "complevel", |
| 272 | "fletcher32", |
| 273 | "contiguous", |
| 274 | "chunksizes", |
| 275 | "shuffle", |
| 276 | "_FillValue", |
| 277 | "dtype", |
| 278 | "compression", |
| 279 | "significant_digits", |
| 280 | "quantize_mode", |
| 281 | "blosc_shuffle", |
| 282 | "szip_coding", |
| 283 | "szip_pixels_per_block", |
| 284 | "endian", |
| 285 | } |
| 286 | if lsd_okay: |
| 287 | valid_encodings.add("least_significant_digit") |
| 288 | if h5py_okay: |
| 289 | valid_encodings.add("compression_opts") |
| 290 | |
| 291 | if not raise_on_invalid and encoding.get("chunksizes") is not None: |
| 292 | # It's possible to get encoded chunksizes larger than a dimension size |
| 293 | # if the original file had an unlimited dimension. This is problematic |
| 294 | # if the new file no longer has an unlimited dimension. |
| 295 | chunksizes = encoding["chunksizes"] |
| 296 | chunks_too_big = any( |
| 297 | c > d and dim not in unlimited_dims |
| 298 | for c, d, dim in zip( |
| 299 | chunksizes, variable.shape, variable.dims, strict=False |
| 300 | ) |
| 301 | ) |
| 302 | has_original_shape = "original_shape" in encoding |
| 303 | changed_shape = ( |
| 304 | has_original_shape and encoding.get("original_shape") != variable.shape |
| 305 | ) |
| 306 | if chunks_too_big or changed_shape: |
| 307 | del encoding["chunksizes"] |
| 308 | |
| 309 | var_has_unlim_dim = any(dim in unlimited_dims for dim in variable.dims) |
| 310 | if not raise_on_invalid and var_has_unlim_dim and "contiguous" in encoding.keys(): |
| 311 | del encoding["contiguous"] |
| 312 |
searching dependent graphs…