Published
Edited
Sep 22, 2020
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
zchunkstore = fetch(url + '.zchunkstore').then(response => response.text())
Insert cell
Insert cell
class HTTPFileChunkStore {
constructor(url, chunk_offsets) {
this.url = url;
this.chunk_offsets = chunk_offsets;
}

async getItem(key) {
const res = this.chunk_offsets[key];

if (!res) {
throw new zarr.KeyError();
}
if (!("offset" in res)) {
// metadata may just be in the offsets JSON and not in the binary file
const meta = JSON.stringify(res);
const enc = new TextEncoder().encode(meta);
return enc.buffer;
}
const { offset, size } = res;
// make range request
const chunk = await fetch(this.url, {
headers: { Range: `bytes=${offset}-${offset + size - 1}` }
}).then(res => res.arrayBuffer());

return chunk;
}

async containsItem(key) {
const res = this.chunk_offsets[key];
if (!res) return false;
return true;
}
}
Insert cell
Insert cell
store = new HTTPFileChunkStore(url, JSON.parse(zchunkstore))
Insert cell
Insert cell
z = zarr.openArray({ store })
Insert cell
Insert cell
arr = z.getRaw([null, null, null]) // read entire image from store
Insert cell
image = {
// Create image data from array selection
const { data, shape } = arr;
const img = new ImageData(
new Uint8ClampedArray(data.buffer),
shape[1],
shape[0]
);

// Draw image
const context = DOM.context2d(img.width / 2, img.height / 2);
context.putImageData(img, 0, 0);
return context.canvas;
}
Insert cell
Insert cell
Insert cell
zarr = import('zarr@0.3')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more