Skip to content

Instantly share code, notes, and snippets.

@hanslovsky
Last active February 21, 2023 03:57
Show Gist options
  • Save hanslovsky/01f926227f481940bf2e7a2bde0d6678 to your computer and use it in GitHub Desktop.
Save hanslovsky/01f926227f481940bf2e7a2bde0d6678 to your computer and use it in GitHub Desktop.
Recompress zarr array inplace using n5-zarr. Untested, use at own risk. Does not deal with non-existing blocks. Not multi-threaded. Run with kscript: https://github.com/kscripting/kscript
@file:Repository("https://maven.scijava.org/content/groups/public")
@file:DependsOn("org.janelia.saalfeldlab:n5-zarr:0.0.8")
import kotlin.math.ceil
import org.janelia.saalfeldlab.n5.zarr.N5ZarrWriter
import org.janelia.saalfeldlab.n5.zarr.ZArrayAttributes
import org.janelia.saalfeldlab.n5.zarr.ZarrCompressor
require(args.size == 2) { "Two arguments required, zarr group and dataset"}
val zarr = N5ZarrWriter(args[0], ".", false)
val dataset = args[1]
require(zarr.datasetExists(dataset)) {"$dataset is not a dataset"}
val attributes = zarr.getZArraryAttributes(dataset)
// hard-coded compressor
val newCompressor = ZarrCompressor.Gzip(5)
val newAttributes = attributes.run {
ZArrayAttributes(
zarrFormat,
shape,
chunks,
dType,
newCompressor,
fillValue,
order,
dimensionSeparator,
filters // might need to be configured, too
)
}
val dsAttributes = attributes.datasetAttributes
val newDsAttributes = newAttributes.datasetAttributes
val n = attributes.numDimensions
val shape = attributes.shape
val chunks = attributes.shape
val position = LongArray(n)
val gridSize = (shape zip chunks).map { (s, c) -> s.toDouble() / c.toDouble() }.map { ceil(it).toLong() }
var d = 0
while (d < n) {
val data = zarr.readBlock(dataset, dsAttributes, *position)
// zarr.deleteBlock(dataset, *position)
zarr.writeBlock(dataset, newDsAttributes, data)
d = 0
while (d < n) {
position[d] += 1L
if (position[d] < gridSize[d])
break
else
position[d] = 0L
++d
}
}
zarr.setZArrayAttributes(dataset, newAttributes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment