|
| 1 | +import * as zarr from 'zarrita' |
| 2 | +import type { LatticeMatrix, VolumeData } from '../types.ts' |
| 3 | + |
| 4 | +/** OME-NGFF + ELvis-custom metadata captured by `convert-to-zarr.py`. */ |
| 5 | +export interface ZarrVolumeMetadata { |
| 6 | + multiscales: { |
| 7 | + name: string |
| 8 | + version: string |
| 9 | + axes: { name: string; type: string }[] |
| 10 | + datasets: { path: string; coordinateTransformations: { type: string; scale: number[] }[] }[] |
| 11 | + }[] |
| 12 | + elvis: { |
| 13 | + material_id: string |
| 14 | + role: 'input' | 'label' |
| 15 | + /** 3x3 lattice matrix, rows = a/b/c vectors */ |
| 16 | + lattice: number[][] |
| 17 | + atoms: { element: string; frac: [number, number, number] }[] |
| 18 | + stats: { min: number; max: number; mean: number } |
| 19 | + /** 201 equally-spaced quantiles, sampled from the full grid; matches client convention. */ |
| 20 | + quantiles: number[] |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export interface OpenedZarrVolume { |
| 25 | + url: string |
| 26 | + meta: ZarrVolumeMetadata |
| 27 | + /** Number of pyramid levels; level 0 is full resolution. */ |
| 28 | + levels: number |
| 29 | + /** Per-level shape from each level's .zarray, ordered [level0, level1, ...]. */ |
| 30 | + levelShapes: [number, number, number][] |
| 31 | +} |
| 32 | + |
| 33 | +/** Open a multi-resolution Zarr store and read its metadata + per-level shapes. */ |
| 34 | +export async function openZarrVolume(url: string): Promise<OpenedZarrVolume> { |
| 35 | + const store = new zarr.FetchStore(url) |
| 36 | + const root = await zarr.open(store, { kind: 'group' }) |
| 37 | + const meta = root.attrs as unknown as ZarrVolumeMetadata |
| 38 | + const levels = meta.multiscales[0].datasets.length |
| 39 | + const levelShapes: [number, number, number][] = [] |
| 40 | + for (let i = 0; i < levels; i++) { |
| 41 | + const arr = await zarr.open(root.resolve(String(i)), { kind: 'array' }) |
| 42 | + levelShapes.push(arr.shape as [number, number, number]) |
| 43 | + } |
| 44 | + return { url, meta, levels, levelShapes } |
| 45 | +} |
| 46 | + |
| 47 | +/** Read one pyramid level into a flat Float32Array. */ |
| 48 | +export async function readZarrLevel( |
| 49 | + opened: OpenedZarrVolume, |
| 50 | + level: number, |
| 51 | +): Promise<{ data: Float32Array; dims: [number, number, number] }> { |
| 52 | + const store = new zarr.FetchStore(opened.url) |
| 53 | + const root = await zarr.open(store, { kind: 'group' }) |
| 54 | + const arr = await zarr.open(root.resolve(String(level)), { kind: 'array' }) |
| 55 | + const result = await zarr.get(arr) |
| 56 | + // zarrita returns C-ordered raw bytes; pymatgen->Zarr writes in C-order too. |
| 57 | + // VASP/marching-cubes expects F-order (flat[i + j*Nx + k*Nx*Ny] = data[i,j,k]). |
| 58 | + const dims = arr.shape as [number, number, number] |
| 59 | + const data = transposeToFortran(result.data as Float32Array, dims) |
| 60 | + return { data, dims } |
| 61 | +} |
| 62 | + |
| 63 | +/** Convert a C-ordered (Nx,Ny,Nz) flat array to F-ordered: flat[i + j*Nx + k*Nx*Ny] = a[i,j,k]. */ |
| 64 | +function transposeToFortran(c: Float32Array, dims: [number, number, number]): Float32Array { |
| 65 | + const [nx, ny, nz] = dims |
| 66 | + const f = new Float32Array(c.length) |
| 67 | + for (let i = 0; i < nx; i++) { |
| 68 | + for (let j = 0; j < ny; j++) { |
| 69 | + const cBase = (i * ny + j) * nz |
| 70 | + for (let k = 0; k < nz; k++) { |
| 71 | + f[i + j * nx + k * nx * ny] = c[cBase + k] |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return f |
| 76 | +} |
| 77 | + |
| 78 | +/** Convert ELvis Zarr metadata + level data into the existing VolumeData shape. */ |
| 79 | +export function zarrToVolumeData( |
| 80 | + opened: OpenedZarrVolume, |
| 81 | + level: { data: Float32Array; dims: [number, number, number] }, |
| 82 | +): VolumeData { |
| 83 | + const { meta } = opened |
| 84 | + const m = meta.elvis.lattice |
| 85 | + const lattice: LatticeMatrix = [ |
| 86 | + m[0][0], m[0][1], m[0][2], |
| 87 | + m[1][0], m[1][1], m[1][2], |
| 88 | + m[2][0], m[2][1], m[2][2], |
| 89 | + ] |
| 90 | + const counts = new Map<string, number>() |
| 91 | + for (const a of meta.elvis.atoms) counts.set(a.element, (counts.get(a.element) ?? 0) + 1) |
| 92 | + const elements = Array.from(counts.keys()) |
| 93 | + return { |
| 94 | + title: `${meta.elvis.material_id}/${meta.elvis.role}`, |
| 95 | + scaleFactor: 1.0, |
| 96 | + lattice, |
| 97 | + structure: { |
| 98 | + elements, |
| 99 | + counts: elements.map((e) => counts.get(e)!), |
| 100 | + atoms: meta.elvis.atoms.map((a) => ({ element: a.element, fracCoords: a.frac })), |
| 101 | + }, |
| 102 | + grid: { dims: level.dims, data: level.data }, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** Convenience: open + read level 0 + assemble VolumeData in one call. */ |
| 107 | +export async function fetchZarrVolume(url: string, level = 0): Promise<VolumeData> { |
| 108 | + const opened = await openZarrVolume(url) |
| 109 | + const lvl = await readZarrLevel(opened, level) |
| 110 | + return zarrToVolumeData(opened, lvl) |
| 111 | +} |
0 commit comments