-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate.ts
112 lines (103 loc) · 2.71 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type { Mutable } from "@zarrita/storage";
import type {
ArrayMetadata,
Attributes,
CodecMetadata,
DataType,
GroupMetadata,
Scalar,
} from "./metadata.js";
import { json_encode_object } from "./util.js";
import { Array, Group, Location } from "./hierarchy.js";
interface CreateGroupOptions {
attributes?: Record<string, any>;
}
interface CreateArrayOptions<Dtype extends DataType> {
shape: number[];
chunk_shape: number[];
data_type: Dtype;
codecs?: CodecMetadata[];
fill_value?: Scalar<Dtype>;
chunk_separator?: "." | "/";
attributes?: Attributes;
}
export async function create<
Store extends Mutable,
Dtype extends DataType = DataType,
>(
location: Location<Store> | Store,
): Promise<Group<Store>>;
export async function create<
Store extends Mutable,
Dtype extends DataType = DataType,
>(
location: Location<Store> | Store,
options: CreateGroupOptions,
): Promise<Group<Store>>;
export async function create<
Store extends Mutable,
Dtype extends DataType,
>(
location: Location<Store> | Store,
options: CreateArrayOptions<Dtype>,
): Promise<Array<Dtype, Store>>;
export async function create<
Store extends Mutable,
Dtype extends DataType,
>(
location: Location<Store> | Store,
options: CreateArrayOptions<Dtype> | CreateGroupOptions = {},
): Promise<Array<Dtype, Store> | Group<Store>> {
let loc = "store" in location ? location : new Location(location);
if ("shape" in options) return create_array(loc, options) as any;
return create_group(loc, options);
}
async function create_group<Store extends Mutable>(
location: Location<Store>,
options: CreateGroupOptions = {},
): Promise<Group<Store>> {
let metadata = {
zarr_format: 3,
node_type: "group",
attributes: options.attributes ?? {},
} satisfies GroupMetadata;
await location.store.set(
location.resolve("zarr.json").path,
json_encode_object(metadata),
);
return new Group(location.store, location.path, metadata);
}
async function create_array<
Store extends Mutable,
Dtype extends DataType,
>(
location: Location<Store>,
options: CreateArrayOptions<Dtype>,
): Promise<Array<DataType, Store>> {
let metadata = {
zarr_format: 3,
node_type: "array",
shape: options.shape,
data_type: options.data_type,
chunk_grid: {
name: "regular",
configuration: {
chunk_shape: options.chunk_shape,
},
},
chunk_key_encoding: {
name: "default",
configuration: {
separator: options.chunk_separator ?? "/",
},
},
codecs: options.codecs ?? [],
fill_value: options.fill_value ?? null,
attributes: options.attributes ?? {},
} satisfies ArrayMetadata<Dtype>;
await location.store.set(
location.resolve("zarr.json").path,
json_encode_object(metadata),
);
return new Array(location.store, location.path, metadata);
}