A native Swift library for reading and writing Zarr V2 and V3 arrays. Inspired by and related to the zarrs Rust library.
- Swift 6.0+
- Swift Package Manager
- macOS 15+ / iOS 18+
Add SwiftZarr as a dependency in your Package.swift:
dependencies: [
.package(url: "https://github.com/open-meteo/swift-zarr", from: "0.2.1"),
],
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "SwiftZarr", package: "swift-zarr"),
]
),
]- Zarr V2 and V3 — read and write arrays and groups
- Codecs — Blosc (blosclz, LZ4/LZ4HC, zlib, zstd), V2 LZ4 (size-prefixed blocks), gzip/zlib (system zlib), zstd, bzip2, and uncompressed
- Storage backends — local filesystem, S3-compatible (AWS, GCS, etc.)
- Typed reads and writes —
retrieveChunk,retrieveArraySubset, andstoreChunk(_:elements:)with typed Swift arrays - Slice/subset reading — retrieve arbitrary N-dimensional ranges without loading full chunks
- C and F order — row-major and column-major chunk layouts
- Fill values — missing chunks transparently return the array fill value, including NaN/Infinity/hex
- Parallel chunk fetching — concurrent chunk reads using Swift structured concurrency
import SwiftZarr
let storage = try S3CompatibleStorage(baseURL: "https://storage.googleapis.com/gcp-public-data-arco-era5")
let path = "ar/1959-2022-6h-64x32_equiangular_conservative.zarr"
let group = try await ZarrGroup(storage: storage, path: path)
let children = try await group.listChildren()
let lat = try await group.openArray(name: "latitude")
let values: [Double] = try await lat.retrieveChunk([0])
let t2m = try await group.openArray(name: "2m_temperature")
let slice: [Float] = try await t2m.retrieveArraySubset([0..<10, 0..<64, 0..<8])Write typed chunks to a local store:
let outputStorage = LocalFileStorage(basePath: "/path/to/output.zarr")
let array = try ZarrArray(
metadata: V2ArrayMetadata(shape: [3], chunks: [2], dtype: ">i4"),
storage: outputStorage,
path: ""
)
try await array.storeMetadata()
try await array.storeChunk([0], elements: [Int32(10), 20])
try await array.storeChunk([1], elements: [Int32(30), 0]) // Includes edge padding.Supply a full chunk, including padding beyond the array bounds, in the declared C/F order (V3 uses C order with the supported bytes codec). The library validates element type and count and converts byte order automatically.
Group attributes are editable local snapshots for both V2 and V3. Save edits explicitly:
let localStorage = LocalFileStorage(basePath: "/path/to/store")
var group = try await ZarrGroup(storage: localStorage, path: "example")
group.attributes["description"] = .string("Updated description")
try await group.storeMetadata()Copies keep independent snapshots. Reopen a group to read external changes. Failed saves retain local edits for retry; V2 metadata and attributes are written as separate files. This replaces the previous attributes() and storeAttributes(_:) methods.
swift build
swift testswift test runs unit tests, committed Zarr-Python fixture tests, and live GCS tests
(which require network access).
See interoperability testing for coverage and fixture regeneration.
- V2 filter support (nonempty filter chains are currently rejected)
- Zstd codec (requires a separate dependency not included in SWCompression)
- Consolidated metadata (
.zmetadata) to reduce round-trips on remote storage - LRU chunk cache to avoid redundant decompression for overlapping reads
- bfloat16 data type support
MIT. See LICENSE.