@dclimate/zarr-map 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +374 -0
- package/dist/core/index.cjs +1603 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +50 -0
- package/dist/core/index.d.ts +50 -0
- package/dist/core/index.js +1575 -0
- package/dist/core/index.js.map +1 -0
- package/dist/dclimate/index.cjs +1859 -0
- package/dist/dclimate/index.cjs.map +1 -0
- package/dist/dclimate/index.d.cts +80 -0
- package/dist/dclimate/index.d.ts +80 -0
- package/dist/dclimate/index.js +1856 -0
- package/dist/dclimate/index.js.map +1 -0
- package/dist/index.cjs +3071 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3038 -0
- package/dist/index.js.map +1 -0
- package/dist/jaxray-CZWT_ZgD.d.ts +57 -0
- package/dist/jaxray-D_mmLPHk.d.cts +57 -0
- package/dist/react/index.cjs +3953 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +71 -0
- package/dist/react/index.d.ts +71 -0
- package/dist/react/index.js +3945 -0
- package/dist/react/index.js.map +1 -0
- package/dist/renderers/index.cjs +903 -0
- package/dist/renderers/index.cjs.map +1 -0
- package/dist/renderers/index.d.cts +115 -0
- package/dist/renderers/index.d.ts +115 -0
- package/dist/renderers/index.js +899 -0
- package/dist/renderers/index.js.map +1 -0
- package/dist/types-DEZwfJNY.d.cts +210 -0
- package/dist/types-DEZwfJNY.d.ts +210 -0
- package/docs/README.md +12 -0
- package/docs/api-design.md +185 -0
- package/docs/architecture.md +246 -0
- package/docs/decision-record-renderer.md +144 -0
- package/docs/package-boundaries.md +50 -0
- package/docs/release-checklist.md +31 -0
- package/package.json +121 -0
|
@@ -0,0 +1,1859 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/core/errors.ts
|
|
4
|
+
var GridError = class extends Error {
|
|
5
|
+
code;
|
|
6
|
+
cause;
|
|
7
|
+
dimension;
|
|
8
|
+
variable;
|
|
9
|
+
sourceId;
|
|
10
|
+
context;
|
|
11
|
+
constructor(details) {
|
|
12
|
+
super(details.message);
|
|
13
|
+
this.name = "GridError";
|
|
14
|
+
this.code = details.code;
|
|
15
|
+
this.cause = details.cause;
|
|
16
|
+
this.dimension = details.dimension;
|
|
17
|
+
this.variable = details.variable;
|
|
18
|
+
this.sourceId = details.sourceId;
|
|
19
|
+
this.context = details.context;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/core/validation.ts
|
|
24
|
+
var xAliases = /* @__PURE__ */ new Set(["x", "lon", "lng", "longitude"]);
|
|
25
|
+
var yAliases = /* @__PURE__ */ new Set(["y", "lat", "latitude"]);
|
|
26
|
+
var numericDtypes = /* @__PURE__ */ new Set([
|
|
27
|
+
"int8",
|
|
28
|
+
"uint8",
|
|
29
|
+
"int16",
|
|
30
|
+
"uint16",
|
|
31
|
+
"int32",
|
|
32
|
+
"uint32",
|
|
33
|
+
"float32",
|
|
34
|
+
"float64",
|
|
35
|
+
"i1",
|
|
36
|
+
"u1",
|
|
37
|
+
"i2",
|
|
38
|
+
"u2",
|
|
39
|
+
"i4",
|
|
40
|
+
"u4",
|
|
41
|
+
"f4",
|
|
42
|
+
"f8"
|
|
43
|
+
]);
|
|
44
|
+
function findVariable(source, variableName) {
|
|
45
|
+
return source.variables.find(
|
|
46
|
+
(variable) => variable.name === variableName || variable.path === variableName
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
function findDimension(source, dimensionName) {
|
|
50
|
+
return source.dimensions.find((dimension) => dimension.name === dimensionName);
|
|
51
|
+
}
|
|
52
|
+
function inferSpatialDimensions(source, variableName = source.variables[0]?.name) {
|
|
53
|
+
const variable = variableName ? findVariable(source, variableName) : source.variables[0];
|
|
54
|
+
if (source.spatialDimensions) {
|
|
55
|
+
return variableIncludesSpatialDimensions(variable, source.spatialDimensions) ? source.spatialDimensions : void 0;
|
|
56
|
+
}
|
|
57
|
+
const candidateNames = variable?.dimensions ?? source.dimensions.map((dimension) => dimension.name);
|
|
58
|
+
let x;
|
|
59
|
+
let y;
|
|
60
|
+
for (const name of candidateNames) {
|
|
61
|
+
const dimension = findDimension(source, name);
|
|
62
|
+
const normalizedName = name.toLowerCase();
|
|
63
|
+
const standardName = String(dimension?.standardName ?? "").toLowerCase();
|
|
64
|
+
const kind = dimension?.kind;
|
|
65
|
+
if (!x && (kind === "x" || xAliases.has(normalizedName) || standardName === "longitude")) {
|
|
66
|
+
x = name;
|
|
67
|
+
}
|
|
68
|
+
if (!y && (kind === "y" || yAliases.has(normalizedName) || standardName === "latitude")) {
|
|
69
|
+
y = name;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return x && y ? { x, y } : void 0;
|
|
73
|
+
}
|
|
74
|
+
function variableIncludesSpatialDimensions(variable, spatialDimensions) {
|
|
75
|
+
if (!variable) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return variable.dimensions.includes(spatialDimensions.x) && variable.dimensions.includes(spatialDimensions.y);
|
|
79
|
+
}
|
|
80
|
+
function validateGridDataSource(source, options = {}) {
|
|
81
|
+
const errors = [];
|
|
82
|
+
const warnings = [];
|
|
83
|
+
if (source.variables.length === 0) {
|
|
84
|
+
errors.push(
|
|
85
|
+
new GridError({
|
|
86
|
+
code: "MISSING_VARIABLE",
|
|
87
|
+
message: "GridDataSource must expose at least one variable.",
|
|
88
|
+
sourceId: source.id
|
|
89
|
+
})
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const variable = options.variable ? findVariable(source, options.variable) : source.variables[0];
|
|
93
|
+
if (options.variable && !variable) {
|
|
94
|
+
errors.push(
|
|
95
|
+
new GridError({
|
|
96
|
+
code: "MISSING_VARIABLE",
|
|
97
|
+
message: `Variable "${options.variable}" is not present in the grid source.`,
|
|
98
|
+
sourceId: source.id,
|
|
99
|
+
variable: options.variable
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
for (const dimension of source.dimensions) {
|
|
104
|
+
if (!Number.isInteger(dimension.size) || dimension.size <= 0) {
|
|
105
|
+
errors.push(
|
|
106
|
+
new GridError({
|
|
107
|
+
code: "UNSUPPORTED_DIMENSION",
|
|
108
|
+
message: `Dimension "${dimension.name}" must have a positive integer size.`,
|
|
109
|
+
dimension: dimension.name,
|
|
110
|
+
sourceId: source.id
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
if (dimension.coordinates && dimension.coordinates.length !== dimension.size) {
|
|
115
|
+
errors.push(
|
|
116
|
+
new GridError({
|
|
117
|
+
code: "MISSING_COORDINATES",
|
|
118
|
+
message: `Dimension "${dimension.name}" has ${dimension.coordinates.length} coordinates for size ${dimension.size}.`,
|
|
119
|
+
dimension: dimension.name,
|
|
120
|
+
sourceId: source.id
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (variable) {
|
|
126
|
+
if (!isNumericDType(variable.dtype)) {
|
|
127
|
+
errors.push(
|
|
128
|
+
new GridError({
|
|
129
|
+
code: "UNSUPPORTED_DTYPE",
|
|
130
|
+
message: `Variable "${variable.name}" has unsupported dtype "${variable.dtype}".`,
|
|
131
|
+
variable: variable.name,
|
|
132
|
+
sourceId: source.id
|
|
133
|
+
})
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
if (variable.dimensions.length !== variable.shape.length) {
|
|
137
|
+
errors.push(
|
|
138
|
+
new GridError({
|
|
139
|
+
code: "MISSING_DIMENSION",
|
|
140
|
+
message: `Variable "${variable.name}" has ${variable.dimensions.length} dimensions but ${variable.shape.length} shape entries.`,
|
|
141
|
+
variable: variable.name,
|
|
142
|
+
sourceId: source.id
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
variable.dimensions.forEach((dimensionName, index) => {
|
|
147
|
+
const dimension = findDimension(source, dimensionName);
|
|
148
|
+
if (!dimension) {
|
|
149
|
+
errors.push(
|
|
150
|
+
new GridError({
|
|
151
|
+
code: "MISSING_DIMENSION",
|
|
152
|
+
message: `Variable "${variable.name}" references missing dimension "${dimensionName}".`,
|
|
153
|
+
dimension: dimensionName,
|
|
154
|
+
variable: variable.name,
|
|
155
|
+
sourceId: source.id
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const expectedSize = variable.shape[index];
|
|
161
|
+
if (expectedSize !== dimension.size) {
|
|
162
|
+
warnings.push(
|
|
163
|
+
`Variable "${variable.name}" shape for dimension "${dimensionName}" is ${expectedSize}, but the dimension size is ${dimension.size}.`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
const spatialDimensions = inferSpatialDimensions(source, variable.name);
|
|
168
|
+
if (!spatialDimensions) {
|
|
169
|
+
errors.push(
|
|
170
|
+
new GridError({
|
|
171
|
+
code: "MISSING_SPATIAL_DIMENSIONS",
|
|
172
|
+
message: `Variable "${variable.name}" needs latitude/longitude or x/y spatial dimensions before it can render.`,
|
|
173
|
+
variable: variable.name,
|
|
174
|
+
sourceId: source.id
|
|
175
|
+
})
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
if (options.requireRenderable && !source.bounds) {
|
|
179
|
+
errors.push(
|
|
180
|
+
new GridError({
|
|
181
|
+
code: "MISSING_BOUNDS",
|
|
182
|
+
message: "Renderable grid sources must include explicit [west, south, east, north] bounds.",
|
|
183
|
+
variable: variable.name,
|
|
184
|
+
sourceId: source.id
|
|
185
|
+
})
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
ok: errors.length === 0,
|
|
191
|
+
errors,
|
|
192
|
+
warnings
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function assertValidGridDataSource(source, options = {}) {
|
|
196
|
+
const result = validateGridDataSource(source, options);
|
|
197
|
+
if (!result.ok) {
|
|
198
|
+
throw result.errors[0];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function isNumericDType(dtype) {
|
|
202
|
+
return numericDtypes.has(dtype.toLowerCase()) || /^[<>|]?[ifu]\d+$/.test(dtype.toLowerCase());
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/core/selectors.ts
|
|
206
|
+
function normalizeSelectors(source, variableName, selectors = {}) {
|
|
207
|
+
const variable = findVariable(source, variableName);
|
|
208
|
+
if (!variable) {
|
|
209
|
+
throw new GridError({
|
|
210
|
+
code: "MISSING_VARIABLE",
|
|
211
|
+
message: `Variable "${variableName}" is not present in the grid source.`,
|
|
212
|
+
variable: variableName,
|
|
213
|
+
sourceId: source.id
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
const normalized = {};
|
|
217
|
+
for (const dimensionName of variable.dimensions) {
|
|
218
|
+
const dimension = findDimension(source, dimensionName);
|
|
219
|
+
if (!dimension) {
|
|
220
|
+
throw new GridError({
|
|
221
|
+
code: "MISSING_DIMENSION",
|
|
222
|
+
message: `Selector normalization cannot find dimension "${dimensionName}".`,
|
|
223
|
+
dimension: dimensionName,
|
|
224
|
+
variable: variableName,
|
|
225
|
+
sourceId: source.id
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
const input = selectors[dimensionName];
|
|
229
|
+
if (input === void 0) {
|
|
230
|
+
normalized[dimensionName] = defaultSelectorForDimension(dimension);
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
normalized[dimensionName] = normalizeSelectorValue(dimension, input, source.id);
|
|
234
|
+
}
|
|
235
|
+
return normalized;
|
|
236
|
+
}
|
|
237
|
+
function normalizeSelectorValue(dimension, input, sourceId) {
|
|
238
|
+
if (input instanceof Date) {
|
|
239
|
+
return { kind: "coordinate", value: input.toISOString() };
|
|
240
|
+
}
|
|
241
|
+
if (typeof input !== "object") {
|
|
242
|
+
return normalizeCoordinateSelector(dimension, input, sourceId);
|
|
243
|
+
}
|
|
244
|
+
if (input.kind === "coordinate") {
|
|
245
|
+
const value = input.value instanceof Date ? input.value.toISOString() : input.value;
|
|
246
|
+
return normalizeCoordinateSelector(dimension, value, sourceId);
|
|
247
|
+
}
|
|
248
|
+
if (input.kind === "index") {
|
|
249
|
+
assertIndexInRange(dimension, input.index, sourceId);
|
|
250
|
+
const value = dimension.coordinates?.[input.index];
|
|
251
|
+
return {
|
|
252
|
+
kind: "index",
|
|
253
|
+
index: input.index,
|
|
254
|
+
value: value instanceof Date ? value.toISOString() : value
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
if (input.kind === "isoTime") {
|
|
258
|
+
const value = findCoordinateForIso(dimension, input.iso);
|
|
259
|
+
return value === void 0 ? { kind: "isoTime", iso: input.iso } : { kind: "isoTime", iso: input.iso, value };
|
|
260
|
+
}
|
|
261
|
+
throw new GridError({
|
|
262
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
263
|
+
message: `Unsupported selector for dimension "${dimension.name}".`,
|
|
264
|
+
dimension: dimension.name,
|
|
265
|
+
sourceId
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
function selectorToIndex(dimension, selector) {
|
|
269
|
+
if (selector.kind === "index") {
|
|
270
|
+
return selector.index;
|
|
271
|
+
}
|
|
272
|
+
const selected = selector.kind === "isoTime" ? selector.value ?? selector.iso : selector.value;
|
|
273
|
+
const index = dimension.coordinates?.findIndex((coordinate) => sameCoordinate(coordinate, selected)) ?? -1;
|
|
274
|
+
if (index >= 0) {
|
|
275
|
+
return index;
|
|
276
|
+
}
|
|
277
|
+
throw new GridError({
|
|
278
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
279
|
+
message: `Selector value "${String(selected)}" does not match coordinates for dimension "${dimension.name}".`,
|
|
280
|
+
dimension: dimension.name
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
function defaultSelectorForDimension(dimension) {
|
|
284
|
+
const firstValue = dimension.coordinates?.[0];
|
|
285
|
+
if (firstValue !== void 0) {
|
|
286
|
+
return {
|
|
287
|
+
kind: "coordinate",
|
|
288
|
+
value: firstValue instanceof Date ? firstValue.toISOString() : firstValue
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return { kind: "index", index: 0 };
|
|
292
|
+
}
|
|
293
|
+
function normalizeCoordinateSelector(dimension, value, sourceId) {
|
|
294
|
+
if (dimension.coordinates && !dimension.coordinates.some((coordinate) => sameCoordinate(coordinate, value))) {
|
|
295
|
+
throw new GridError({
|
|
296
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
297
|
+
message: `Selector value "${String(value)}" does not match coordinates for dimension "${dimension.name}".`,
|
|
298
|
+
dimension: dimension.name,
|
|
299
|
+
sourceId
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
return { kind: "coordinate", value };
|
|
303
|
+
}
|
|
304
|
+
function assertIndexInRange(dimension, index, sourceId) {
|
|
305
|
+
if (!Number.isInteger(index) || index < 0 || index >= dimension.size) {
|
|
306
|
+
throw new GridError({
|
|
307
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
308
|
+
message: `Index selector ${index} is outside dimension "${dimension.name}" size ${dimension.size}.`,
|
|
309
|
+
dimension: dimension.name,
|
|
310
|
+
sourceId
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function findCoordinateForIso(dimension, iso) {
|
|
315
|
+
return dimension.coordinates?.map((coordinate) => decodeTimeCoordinate(coordinate, dimension.units)).find((coordinate) => coordinate.iso === iso || coordinate.value === iso)?.value;
|
|
316
|
+
}
|
|
317
|
+
function decodeTimeCoordinate(coordinate, units) {
|
|
318
|
+
if (coordinate instanceof Date) {
|
|
319
|
+
return { value: coordinate.toISOString(), iso: coordinate.toISOString() };
|
|
320
|
+
}
|
|
321
|
+
if (typeof coordinate === "string") {
|
|
322
|
+
const date = new Date(coordinate);
|
|
323
|
+
return Number.isNaN(date.getTime()) ? { value: coordinate } : { value: coordinate, iso: date.toISOString() };
|
|
324
|
+
}
|
|
325
|
+
const cfTime = decodeCfTime(coordinate, units);
|
|
326
|
+
return cfTime ? { value: coordinate, iso: cfTime } : { value: coordinate };
|
|
327
|
+
}
|
|
328
|
+
function decodeCfTime(value, units) {
|
|
329
|
+
if (!units) {
|
|
330
|
+
return void 0;
|
|
331
|
+
}
|
|
332
|
+
const match = /^(seconds|minutes|hours|days) since ([0-9]{4}-[0-9]{2}-[0-9]{2})(?:[ T]([0-9:.Z+-]+))?/i.exec(
|
|
333
|
+
units
|
|
334
|
+
);
|
|
335
|
+
if (!match) {
|
|
336
|
+
return void 0;
|
|
337
|
+
}
|
|
338
|
+
const unit = match[1]?.toLowerCase();
|
|
339
|
+
const date = match[2];
|
|
340
|
+
const time = match[3] ?? "00:00:00Z";
|
|
341
|
+
const origin = /* @__PURE__ */ new Date(`${date}T${time.replace(/Z?$/, "Z")}`);
|
|
342
|
+
if (Number.isNaN(origin.getTime())) {
|
|
343
|
+
return void 0;
|
|
344
|
+
}
|
|
345
|
+
const multiplier = unit === "seconds" ? 1e3 : unit === "minutes" ? 6e4 : unit === "hours" ? 36e5 : 864e5;
|
|
346
|
+
return new Date(origin.getTime() + value * multiplier).toISOString();
|
|
347
|
+
}
|
|
348
|
+
function sameCoordinate(left, right) {
|
|
349
|
+
const normalizedLeft = left instanceof Date ? left.toISOString() : left;
|
|
350
|
+
return normalizedLeft === right || String(normalizedLeft) === String(right);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// src/core/jaxray.ts
|
|
354
|
+
function createJaxraySource(input, options = {}) {
|
|
355
|
+
const dataset = isDatasetLike(input) ? input : arrayToDataset(input);
|
|
356
|
+
const variables = inferVariables(dataset, options);
|
|
357
|
+
const dimensions = inferDimensions(dataset, variables, options);
|
|
358
|
+
const source = {
|
|
359
|
+
id: options.id ?? dataset.id,
|
|
360
|
+
label: options.label,
|
|
361
|
+
source: options.source,
|
|
362
|
+
store: options.store ?? dataset.store,
|
|
363
|
+
zarrVersion: options.zarrVersion,
|
|
364
|
+
crs: options.crs ?? stringAttr(dataset.attrs, "crs") ?? "EPSG:4326",
|
|
365
|
+
proj4: options.proj4 ?? stringAttr(dataset.attrs, "proj4"),
|
|
366
|
+
bounds: options.bounds ?? inferBounds(dimensions, dataset.attrs),
|
|
367
|
+
spatialDimensions: options.spatialDimensions,
|
|
368
|
+
dimensions,
|
|
369
|
+
variables,
|
|
370
|
+
metadata: dataset.attrs,
|
|
371
|
+
readSlice: async (request) => readJaxraySlice(dataset, source, request.variable, request.selectors)
|
|
372
|
+
};
|
|
373
|
+
const firstSpatialVariable = variables.find(
|
|
374
|
+
(variable) => inferSpatialDimensions(source, variable.name)
|
|
375
|
+
);
|
|
376
|
+
source.spatialDimensions = options.spatialDimensions ?? inferSpatialDimensions(source) ?? (firstSpatialVariable ? inferSpatialDimensions(source, firstSpatialVariable.name) : void 0);
|
|
377
|
+
assertValidGridDataSource(source, {
|
|
378
|
+
variable: firstSpatialVariable?.name ?? variables[0]?.name
|
|
379
|
+
});
|
|
380
|
+
return source;
|
|
381
|
+
}
|
|
382
|
+
function isDatasetLike(input) {
|
|
383
|
+
return "data_vars" in input || "dataVars" in input || "variables" in input && !("shape" in input) || "sizes" in input;
|
|
384
|
+
}
|
|
385
|
+
function arrayToDataset(array) {
|
|
386
|
+
const name = array.name ?? "variable";
|
|
387
|
+
return {
|
|
388
|
+
data_vars: { [name]: array },
|
|
389
|
+
coords: array.coords,
|
|
390
|
+
attrs: array.attrs
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function inferVariables(dataset, options) {
|
|
394
|
+
return Array.from(variableEntries(dataset)).map(([name, array]) => {
|
|
395
|
+
const dimensions = array.dims ?? array.dimensions;
|
|
396
|
+
if (!dimensions) {
|
|
397
|
+
throw new GridError({
|
|
398
|
+
code: "MISSING_DIMENSION",
|
|
399
|
+
message: `Jaxray variable "${name}" does not expose dims/dimensions metadata.`,
|
|
400
|
+
variable: name
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
const shape = array.shape ?? dimensions.map((dimensionName) => dimensionSize(dataset, dimensionName));
|
|
404
|
+
if (!shape) {
|
|
405
|
+
throw new GridError({
|
|
406
|
+
code: "MISSING_DIMENSION",
|
|
407
|
+
message: `Jaxray variable "${name}" does not expose shape metadata.`,
|
|
408
|
+
variable: name
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
const override = options.variables?.[name] ?? {};
|
|
412
|
+
return {
|
|
413
|
+
name,
|
|
414
|
+
path: stringAttr(array.attrs, "path"),
|
|
415
|
+
dtype: override.dtype ?? array.dtype ?? stringAttr(array.attrs, "dtype") ?? "float32",
|
|
416
|
+
dimensions,
|
|
417
|
+
shape,
|
|
418
|
+
chunks: override.chunks ?? array.chunks,
|
|
419
|
+
units: override.units ?? stringAttr(array.attrs, "units"),
|
|
420
|
+
longName: override.longName ?? stringAttr(array.attrs, "long_name"),
|
|
421
|
+
standardName: override.standardName ?? stringAttr(array.attrs, "standard_name"),
|
|
422
|
+
fillValue: override.fillValue ?? numberAttr(array.attrs, "_FillValue") ?? numberAttr(array.attrs, "fill_value") ?? null,
|
|
423
|
+
scaleFactor: override.scaleFactor ?? numberAttr(array.attrs, "scale_factor"),
|
|
424
|
+
addOffset: override.addOffset ?? numberAttr(array.attrs, "add_offset"),
|
|
425
|
+
attrs: array.attrs
|
|
426
|
+
};
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
function inferDimensions(dataset, variables, options) {
|
|
430
|
+
const dimensions = /* @__PURE__ */ new Map();
|
|
431
|
+
const coords = dataset.coords;
|
|
432
|
+
for (const variable of variables) {
|
|
433
|
+
variable.dimensions.forEach((name, index) => {
|
|
434
|
+
const existing = dimensions.get(name);
|
|
435
|
+
const coordinates = normalizeCoordinateArray(coordinateValue(coords, name));
|
|
436
|
+
const override = options.dimensions?.[name] ?? {};
|
|
437
|
+
const inferredKind = inferDimensionKind(name);
|
|
438
|
+
const size = variable.shape[index] ?? coordinates?.length ?? 0;
|
|
439
|
+
dimensions.set(name, {
|
|
440
|
+
...existing,
|
|
441
|
+
name,
|
|
442
|
+
size: override.size ?? existing?.size ?? size,
|
|
443
|
+
kind: override.kind ?? existing?.kind ?? inferredKind,
|
|
444
|
+
coordinates: override.coordinates ?? existing?.coordinates ?? coordinates,
|
|
445
|
+
units: override.units ?? existing?.units,
|
|
446
|
+
calendar: override.calendar ?? existing?.calendar,
|
|
447
|
+
longName: override.longName ?? existing?.longName,
|
|
448
|
+
standardName: override.standardName ?? existing?.standardName,
|
|
449
|
+
ascending: override.ascending ?? existing?.ascending ?? inferAscending(coordinates),
|
|
450
|
+
attrs: override.attrs ?? existing?.attrs
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
return Array.from(dimensions.values());
|
|
455
|
+
}
|
|
456
|
+
function inferDimensionKind(name) {
|
|
457
|
+
const normalized = name.toLowerCase();
|
|
458
|
+
if (["lon", "lng", "longitude", "x"].includes(normalized)) {
|
|
459
|
+
return "x";
|
|
460
|
+
}
|
|
461
|
+
if (["lat", "latitude", "y"].includes(normalized)) {
|
|
462
|
+
return "y";
|
|
463
|
+
}
|
|
464
|
+
if (["time", "valid_time", "date"].includes(normalized)) {
|
|
465
|
+
return "time";
|
|
466
|
+
}
|
|
467
|
+
if (["band", "month"].includes(normalized)) {
|
|
468
|
+
return "band";
|
|
469
|
+
}
|
|
470
|
+
if (["level", "height", "pressure"].includes(normalized)) {
|
|
471
|
+
return "vertical";
|
|
472
|
+
}
|
|
473
|
+
return "other";
|
|
474
|
+
}
|
|
475
|
+
function inferBounds(dimensions, attrs) {
|
|
476
|
+
const attrBounds = attrs?.bounds;
|
|
477
|
+
if (Array.isArray(attrBounds) && attrBounds.length === 4 && attrBounds.every((value) => typeof value === "number")) {
|
|
478
|
+
return attrBounds;
|
|
479
|
+
}
|
|
480
|
+
const x = dimensions.find((dimension) => dimension.kind === "x");
|
|
481
|
+
const y = dimensions.find((dimension) => dimension.kind === "y");
|
|
482
|
+
const xCoordinates = x?.coordinates?.filter(
|
|
483
|
+
(value) => typeof value === "number"
|
|
484
|
+
);
|
|
485
|
+
const yCoordinates = y?.coordinates?.filter(
|
|
486
|
+
(value) => typeof value === "number"
|
|
487
|
+
);
|
|
488
|
+
if (!xCoordinates?.length || !yCoordinates?.length) {
|
|
489
|
+
return void 0;
|
|
490
|
+
}
|
|
491
|
+
return [
|
|
492
|
+
Math.min(...xCoordinates),
|
|
493
|
+
Math.min(...yCoordinates),
|
|
494
|
+
Math.max(...xCoordinates),
|
|
495
|
+
Math.max(...yCoordinates)
|
|
496
|
+
];
|
|
497
|
+
}
|
|
498
|
+
async function readJaxraySlice(dataset, source, variableName, selectors = {}) {
|
|
499
|
+
const entry = variableEntries(dataset).find(([name2]) => name2 === variableName);
|
|
500
|
+
if (!entry) {
|
|
501
|
+
throw new GridError({
|
|
502
|
+
code: "MISSING_VARIABLE",
|
|
503
|
+
message: `Jaxray source cannot read missing variable "${variableName}".`,
|
|
504
|
+
variable: variableName,
|
|
505
|
+
sourceId: source.id
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
const [name, array] = entry;
|
|
509
|
+
const variable = source.variables.find((candidate) => candidate.name === name);
|
|
510
|
+
if (!variable) {
|
|
511
|
+
throw new GridError({
|
|
512
|
+
code: "MISSING_VARIABLE",
|
|
513
|
+
message: `Jaxray source metadata is missing variable "${name}".`,
|
|
514
|
+
variable: name,
|
|
515
|
+
sourceId: source.id
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
const normalizedSelectors = isNormalizedSelectors(selectors) ? selectors : normalizeSelectors(source, variable.name, selectors);
|
|
519
|
+
const selection = variable.dimensions.reduce(
|
|
520
|
+
(accumulator, dimensionName) => {
|
|
521
|
+
const dimension = findDimension(source, dimensionName);
|
|
522
|
+
if (!dimension) {
|
|
523
|
+
return accumulator;
|
|
524
|
+
}
|
|
525
|
+
accumulator[dimensionName] = selectorToIndex(
|
|
526
|
+
dimension,
|
|
527
|
+
normalizedSelectors[dimensionName] ?? { kind: "index", index: 0 }
|
|
528
|
+
);
|
|
529
|
+
return accumulator;
|
|
530
|
+
},
|
|
531
|
+
{}
|
|
532
|
+
);
|
|
533
|
+
const value = await readArrayValue(array, variable, selection);
|
|
534
|
+
return {
|
|
535
|
+
variable: name,
|
|
536
|
+
selectors: normalizedSelectors,
|
|
537
|
+
dimensions: [],
|
|
538
|
+
shape: [],
|
|
539
|
+
data: [value],
|
|
540
|
+
unit: variable.units,
|
|
541
|
+
fillValue: variable.fillValue
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
async function readArrayValue(array, variable, selection) {
|
|
545
|
+
if (array.get) {
|
|
546
|
+
return array.get(selection);
|
|
547
|
+
}
|
|
548
|
+
if (array.isel) {
|
|
549
|
+
const selected = await array.isel(selection);
|
|
550
|
+
const computed = selected.isLazy && selected.compute ? await selected.compute() : selected;
|
|
551
|
+
return scalarFromArrayLike(computed.data ?? computed.values);
|
|
552
|
+
}
|
|
553
|
+
const data = array.data ?? array.values;
|
|
554
|
+
if (!data) {
|
|
555
|
+
throw new GridError({
|
|
556
|
+
code: "SOURCE_LOAD_FAILED",
|
|
557
|
+
message: `Jaxray variable "${variable.name}" does not expose readable data for query helpers.`,
|
|
558
|
+
variable: variable.name
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
let offset = 0;
|
|
562
|
+
let stride = 1;
|
|
563
|
+
for (let index = variable.dimensions.length - 1; index >= 0; index -= 1) {
|
|
564
|
+
const dimensionName = variable.dimensions[index];
|
|
565
|
+
if (!dimensionName) {
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
offset += (selection[dimensionName] ?? 0) * stride;
|
|
569
|
+
stride *= variable.shape[index] ?? 1;
|
|
570
|
+
}
|
|
571
|
+
return data[offset] ?? Number.NaN;
|
|
572
|
+
}
|
|
573
|
+
function scalarFromArrayLike(value) {
|
|
574
|
+
if (typeof value === "number") {
|
|
575
|
+
return value;
|
|
576
|
+
}
|
|
577
|
+
if (Array.isArray(value) || ArrayBuffer.isView(value)) {
|
|
578
|
+
const first = Array.from(value)[0];
|
|
579
|
+
return scalarFromArrayLike(first);
|
|
580
|
+
}
|
|
581
|
+
return Number.NaN;
|
|
582
|
+
}
|
|
583
|
+
function variableEntries(dataset) {
|
|
584
|
+
if (Array.isArray(dataset.dataVars) && typeof dataset.getVariable === "function") {
|
|
585
|
+
return dataset.dataVars.map((name) => [name, dataset.getVariable?.(name) ?? {}]);
|
|
586
|
+
}
|
|
587
|
+
if (Array.isArray(dataset.dataVars)) {
|
|
588
|
+
throw new GridError({
|
|
589
|
+
code: "MISSING_VARIABLE",
|
|
590
|
+
message: "Jaxray Dataset exposes dataVars names but no getVariable(name) method."
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
const variables = dataset.data_vars ?? dataset.dataVars ?? dataset.variables ?? {};
|
|
594
|
+
if (variables instanceof Map) {
|
|
595
|
+
return Array.from(variables.entries());
|
|
596
|
+
}
|
|
597
|
+
return Object.entries(variables);
|
|
598
|
+
}
|
|
599
|
+
function normalizeCoordinateArray(input) {
|
|
600
|
+
const raw = Array.isArray(input) ? input : ArrayBuffer.isView(input) ? Array.from(input) : typeof input === "object" && input !== null && "values" in input && isArrayLike(input.values) ? Array.from(input.values) : typeof input === "object" && input !== null && "data" in input && isArrayLike(input.data) ? Array.from(input.data) : void 0;
|
|
601
|
+
return raw?.filter(
|
|
602
|
+
(value) => typeof value === "number" || typeof value === "string"
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
function coordinateValue(coords, name) {
|
|
606
|
+
if (!coords) {
|
|
607
|
+
return void 0;
|
|
608
|
+
}
|
|
609
|
+
return coords instanceof Map ? coords.get(name) : coords[name];
|
|
610
|
+
}
|
|
611
|
+
function dimensionSize(dataset, dimensionName) {
|
|
612
|
+
const sizes = dataset.sizes;
|
|
613
|
+
if (!sizes) {
|
|
614
|
+
return 0;
|
|
615
|
+
}
|
|
616
|
+
return sizes instanceof Map ? sizes.get(dimensionName) ?? 0 : sizes[dimensionName] ?? 0;
|
|
617
|
+
}
|
|
618
|
+
function isArrayLike(value) {
|
|
619
|
+
return Array.isArray(value) || ArrayBuffer.isView(value) || typeof value === "object" && value !== null && "length" in value && typeof value.length === "number";
|
|
620
|
+
}
|
|
621
|
+
function inferAscending(coordinates) {
|
|
622
|
+
if (!coordinates || coordinates.length < 2) {
|
|
623
|
+
return void 0;
|
|
624
|
+
}
|
|
625
|
+
const first = coordinates[0];
|
|
626
|
+
const second = coordinates[1];
|
|
627
|
+
return typeof first === "number" && typeof second === "number" ? second > first : void 0;
|
|
628
|
+
}
|
|
629
|
+
function stringAttr(attrs, name) {
|
|
630
|
+
const value = attrs?.[name];
|
|
631
|
+
return typeof value === "string" ? value : void 0;
|
|
632
|
+
}
|
|
633
|
+
function numberAttr(attrs, name) {
|
|
634
|
+
const value = attrs?.[name];
|
|
635
|
+
return typeof value === "number" ? value : void 0;
|
|
636
|
+
}
|
|
637
|
+
function isNormalizedSelectors(selectors) {
|
|
638
|
+
return Object.values(selectors).every(
|
|
639
|
+
(value) => typeof value === "object" && value !== null && "kind" in value
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// src/core/preflight.ts
|
|
644
|
+
var dtypeByteSizes = {
|
|
645
|
+
int8: 1,
|
|
646
|
+
uint8: 1,
|
|
647
|
+
i1: 1,
|
|
648
|
+
u1: 1,
|
|
649
|
+
int16: 2,
|
|
650
|
+
uint16: 2,
|
|
651
|
+
i2: 2,
|
|
652
|
+
u2: 2,
|
|
653
|
+
int32: 4,
|
|
654
|
+
uint32: 4,
|
|
655
|
+
float32: 4,
|
|
656
|
+
i4: 4,
|
|
657
|
+
u4: 4,
|
|
658
|
+
f4: 4,
|
|
659
|
+
float64: 8,
|
|
660
|
+
f8: 8
|
|
661
|
+
};
|
|
662
|
+
function preflightGridRequest(options) {
|
|
663
|
+
const errors = [];
|
|
664
|
+
const warnings = [];
|
|
665
|
+
const dimensions = {};
|
|
666
|
+
const variable = findVariable(options.source, options.variable);
|
|
667
|
+
if (!variable) {
|
|
668
|
+
errors.push(
|
|
669
|
+
new GridError({
|
|
670
|
+
code: "MISSING_VARIABLE",
|
|
671
|
+
message: `Variable "${options.variable}" is not present in the grid source.`,
|
|
672
|
+
sourceId: options.source.id,
|
|
673
|
+
variable: options.variable
|
|
674
|
+
})
|
|
675
|
+
);
|
|
676
|
+
return { ok: false, dimensions, warnings, errors };
|
|
677
|
+
}
|
|
678
|
+
const spatialDimensions = inferSpatialDimensions(options.source, variable.name);
|
|
679
|
+
for (const dimensionName of variable.dimensions) {
|
|
680
|
+
const dimension = findDimension(options.source, dimensionName);
|
|
681
|
+
if (!dimension) {
|
|
682
|
+
errors.push(
|
|
683
|
+
new GridError({
|
|
684
|
+
code: "MISSING_DIMENSION",
|
|
685
|
+
message: `Variable "${variable.name}" references missing dimension "${dimensionName}".`,
|
|
686
|
+
dimension: dimensionName,
|
|
687
|
+
sourceId: options.source.id,
|
|
688
|
+
variable: variable.name
|
|
689
|
+
})
|
|
690
|
+
);
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
dimensions[dimension.name] = selectedDimensionSize({
|
|
694
|
+
bounds: options.bounds,
|
|
695
|
+
dimension,
|
|
696
|
+
isSpatialX: spatialDimensions?.x === dimension.name,
|
|
697
|
+
isSpatialY: spatialDimensions?.y === dimension.name,
|
|
698
|
+
selectorIsPresent: options.selectors?.[dimension.name] !== void 0,
|
|
699
|
+
sourceBounds: options.source.bounds,
|
|
700
|
+
timeRange: options.timeRange,
|
|
701
|
+
warnings
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
const cells = Object.values(dimensions).reduce((total, size) => total * size, 1);
|
|
705
|
+
const byteSize = dtypeByteSize(variable.dtype);
|
|
706
|
+
const bytes = byteSize === void 0 ? void 0 : cells * byteSize;
|
|
707
|
+
if (byteSize === void 0) {
|
|
708
|
+
warnings.push(`Variable "${variable.name}" has unknown dtype byte size "${variable.dtype}".`);
|
|
709
|
+
}
|
|
710
|
+
if (options.limits?.maxCells !== void 0 && cells > options.limits.maxCells) {
|
|
711
|
+
errors.push(
|
|
712
|
+
new GridError({
|
|
713
|
+
code: "QUERY_TOO_EXPENSIVE",
|
|
714
|
+
message: `Grid request would select approximately ${cells} cells, above the limit of ${options.limits.maxCells}.`,
|
|
715
|
+
context: { cells, maxCells: options.limits.maxCells },
|
|
716
|
+
sourceId: options.source.id,
|
|
717
|
+
variable: variable.name
|
|
718
|
+
})
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
if (bytes !== void 0 && options.limits?.maxBytes !== void 0 && bytes > options.limits.maxBytes) {
|
|
722
|
+
errors.push(
|
|
723
|
+
new GridError({
|
|
724
|
+
code: "QUERY_TOO_EXPENSIVE",
|
|
725
|
+
message: `Grid request would read approximately ${bytes} uncompressed bytes, above the limit of ${options.limits.maxBytes}.`,
|
|
726
|
+
context: { bytes, maxBytes: options.limits.maxBytes },
|
|
727
|
+
sourceId: options.source.id,
|
|
728
|
+
variable: variable.name
|
|
729
|
+
})
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
return {
|
|
733
|
+
ok: errors.length === 0,
|
|
734
|
+
cells,
|
|
735
|
+
bytes,
|
|
736
|
+
dimensions,
|
|
737
|
+
warnings,
|
|
738
|
+
errors
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
function selectedDimensionSize(options) {
|
|
742
|
+
if (options.selectorIsPresent) {
|
|
743
|
+
return 1;
|
|
744
|
+
}
|
|
745
|
+
if (options.bounds && options.isSpatialX) {
|
|
746
|
+
return spatialCount(options.dimension, options.bounds[0], options.bounds[2], {
|
|
747
|
+
axis: "x",
|
|
748
|
+
sourceBounds: options.sourceBounds,
|
|
749
|
+
warnings: options.warnings
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
if (options.bounds && options.isSpatialY) {
|
|
753
|
+
return spatialCount(options.dimension, options.bounds[1], options.bounds[3], {
|
|
754
|
+
axis: "y",
|
|
755
|
+
sourceBounds: options.sourceBounds,
|
|
756
|
+
warnings: options.warnings
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
if (options.timeRange && options.dimension.kind === "time") {
|
|
760
|
+
return timeRangeCount(options.dimension, options.timeRange, options.warnings);
|
|
761
|
+
}
|
|
762
|
+
return options.dimension.size;
|
|
763
|
+
}
|
|
764
|
+
function spatialCount(dimension, lowerBound, upperBound, options) {
|
|
765
|
+
const low = Math.min(lowerBound, upperBound);
|
|
766
|
+
const high = Math.max(lowerBound, upperBound);
|
|
767
|
+
const numericCoordinates2 = dimension.coordinates?.filter(
|
|
768
|
+
(coordinate) => typeof coordinate === "number"
|
|
769
|
+
);
|
|
770
|
+
if (numericCoordinates2?.length) {
|
|
771
|
+
return numericCoordinates2.filter((coordinate) => coordinate >= low && coordinate <= high).length;
|
|
772
|
+
}
|
|
773
|
+
const sourceRange = spatialRange(options.sourceBounds, options.axis);
|
|
774
|
+
if (!sourceRange) {
|
|
775
|
+
options.warnings.push(
|
|
776
|
+
`Dimension "${dimension.name}" has no coordinates or source bounds, so preflight used the full dimension size.`
|
|
777
|
+
);
|
|
778
|
+
return dimension.size;
|
|
779
|
+
}
|
|
780
|
+
const [sourceLow, sourceHigh] = sourceRange;
|
|
781
|
+
const span = sourceHigh - sourceLow;
|
|
782
|
+
if (span <= 0 || dimension.size <= 0) {
|
|
783
|
+
return 0;
|
|
784
|
+
}
|
|
785
|
+
const overlap = Math.max(0, Math.min(high, sourceHigh) - Math.max(low, sourceLow));
|
|
786
|
+
if (overlap === 0) {
|
|
787
|
+
return 0;
|
|
788
|
+
}
|
|
789
|
+
return Math.max(1, Math.min(dimension.size, Math.ceil(overlap / span * dimension.size)));
|
|
790
|
+
}
|
|
791
|
+
function timeRangeCount(dimension, timeRange, warnings) {
|
|
792
|
+
if (!dimension.coordinates?.length) {
|
|
793
|
+
warnings.push(
|
|
794
|
+
`Time dimension "${dimension.name}" has no coordinates, so preflight used the full time size.`
|
|
795
|
+
);
|
|
796
|
+
return dimension.size;
|
|
797
|
+
}
|
|
798
|
+
const start = timeToMillis(timeRange.start, dimension.units);
|
|
799
|
+
const end = timeToMillis(timeRange.end, dimension.units);
|
|
800
|
+
if (start === void 0 || end === void 0) {
|
|
801
|
+
warnings.push(
|
|
802
|
+
`Time range for "${dimension.name}" could not be parsed, so preflight used the full time size.`
|
|
803
|
+
);
|
|
804
|
+
return dimension.size;
|
|
805
|
+
}
|
|
806
|
+
const low = Math.min(start, end);
|
|
807
|
+
const high = Math.max(start, end);
|
|
808
|
+
let count = 0;
|
|
809
|
+
for (const coordinate of dimension.coordinates) {
|
|
810
|
+
const value = timeToMillis(coordinate, dimension.units);
|
|
811
|
+
if (value !== void 0 && value >= low && value <= high) {
|
|
812
|
+
count += 1;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
return count;
|
|
816
|
+
}
|
|
817
|
+
function timeToMillis(value, units) {
|
|
818
|
+
if (value instanceof Date) {
|
|
819
|
+
return value.getTime();
|
|
820
|
+
}
|
|
821
|
+
if (typeof value === "string") {
|
|
822
|
+
const parsed = Date.parse(value);
|
|
823
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
824
|
+
}
|
|
825
|
+
if (typeof value === "number") {
|
|
826
|
+
return numericTimeToMillis(value, units);
|
|
827
|
+
}
|
|
828
|
+
return void 0;
|
|
829
|
+
}
|
|
830
|
+
function numericTimeToMillis(value, units) {
|
|
831
|
+
if (!units) {
|
|
832
|
+
return void 0;
|
|
833
|
+
}
|
|
834
|
+
const match = /^(seconds?|minutes?|hours?|days?) since (.+)$/i.exec(units.trim());
|
|
835
|
+
if (!match) {
|
|
836
|
+
return void 0;
|
|
837
|
+
}
|
|
838
|
+
const [, unit, epoch] = match;
|
|
839
|
+
if (!unit || !epoch) {
|
|
840
|
+
return void 0;
|
|
841
|
+
}
|
|
842
|
+
const epochMillis = Date.parse(epoch);
|
|
843
|
+
if (Number.isNaN(epochMillis)) {
|
|
844
|
+
return void 0;
|
|
845
|
+
}
|
|
846
|
+
const multiplier = unit.toLowerCase().startsWith("second") ? 1e3 : unit.toLowerCase().startsWith("minute") ? 60 * 1e3 : unit.toLowerCase().startsWith("hour") ? 60 * 60 * 1e3 : 24 * 60 * 60 * 1e3;
|
|
847
|
+
return epochMillis + value * multiplier;
|
|
848
|
+
}
|
|
849
|
+
function spatialRange(bounds, axis) {
|
|
850
|
+
if (!bounds) {
|
|
851
|
+
return void 0;
|
|
852
|
+
}
|
|
853
|
+
return axis === "x" ? [bounds[0], bounds[2]] : [bounds[1], bounds[3]];
|
|
854
|
+
}
|
|
855
|
+
function dtypeByteSize(dtype) {
|
|
856
|
+
const normalized = dtype.toLowerCase().replace(/^[<>|]/, "");
|
|
857
|
+
return dtypeByteSizes[normalized];
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// src/dclimate/index.ts
|
|
861
|
+
var DEFAULT_TIME_KEYS = [
|
|
862
|
+
"time",
|
|
863
|
+
"valid_time",
|
|
864
|
+
"datetime",
|
|
865
|
+
"date",
|
|
866
|
+
"forecast_reference_time",
|
|
867
|
+
"forecast_time",
|
|
868
|
+
"analysis_time",
|
|
869
|
+
"initial_time",
|
|
870
|
+
"verification_time",
|
|
871
|
+
"step",
|
|
872
|
+
"t"
|
|
873
|
+
];
|
|
874
|
+
var DClimateAdapterError = class extends GridError {
|
|
875
|
+
constructor(code, message, cause, context) {
|
|
876
|
+
super({
|
|
877
|
+
code: code === "unsupported" ? "UNSUPPORTED_DIMENSION" : "SOURCE_LOAD_FAILED",
|
|
878
|
+
message,
|
|
879
|
+
cause,
|
|
880
|
+
context: {
|
|
881
|
+
adapter: "dclimate",
|
|
882
|
+
adapterCode: code,
|
|
883
|
+
...context
|
|
884
|
+
}
|
|
885
|
+
});
|
|
886
|
+
this.name = "DClimateAdapterError";
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
function reportProgress(options, progress) {
|
|
890
|
+
options.onProgress?.({
|
|
891
|
+
...progress,
|
|
892
|
+
percent: Math.max(0, Math.min(100, progress.percent))
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
function runDClimatePreflight(source, options) {
|
|
896
|
+
const config = options.preflight;
|
|
897
|
+
if (!config) {
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
const selectionBounds = options.selection?.bounds ? normalizeSelectionBounds(options.selection.bounds, options.selection.boundsOptions).bounds : void 0;
|
|
901
|
+
const result = preflightGridRequest({
|
|
902
|
+
bounds: config.bounds ?? selectionBounds,
|
|
903
|
+
limits: config.limits,
|
|
904
|
+
selectors: config.selectors,
|
|
905
|
+
source,
|
|
906
|
+
timeRange: config.timeRange ?? options.selection?.timeRange,
|
|
907
|
+
variable: config.variable ?? source.variables[0]?.name ?? ""
|
|
908
|
+
});
|
|
909
|
+
config.onResult?.(result);
|
|
910
|
+
if (!result.ok) {
|
|
911
|
+
throw result.errors[0];
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
async function createDClimateSource(request, options = {}) {
|
|
915
|
+
try {
|
|
916
|
+
reportProgress(options, {
|
|
917
|
+
stage: "request",
|
|
918
|
+
message: "Preparing dClimate request",
|
|
919
|
+
percent: 2
|
|
920
|
+
});
|
|
921
|
+
const loaded = await loadDClimateDataset(request, options);
|
|
922
|
+
reportProgress(options, {
|
|
923
|
+
stage: "dataset",
|
|
924
|
+
message: "Dataset metadata loaded",
|
|
925
|
+
percent: 45
|
|
926
|
+
});
|
|
927
|
+
const metadata = extractMetadata(loaded);
|
|
928
|
+
const cid = extractString(loaded, ["cid", "rootCid", "hash"]) ?? metadata?.cid ?? request.cid;
|
|
929
|
+
const dataset = extractDataset(loaded);
|
|
930
|
+
const selectedForRendering = Boolean(options.selection?.bounds);
|
|
931
|
+
if (!dataset) {
|
|
932
|
+
const existingStore = options.store ?? extractStore(loaded);
|
|
933
|
+
const hasExternalSource = Boolean(
|
|
934
|
+
existingStore || options.source || request.cid || options.openIpfsStore || options.gatewayUrl
|
|
935
|
+
);
|
|
936
|
+
throw new DClimateAdapterError(
|
|
937
|
+
"metadata",
|
|
938
|
+
hasExternalSource ? "dClimate CID/store loading requires dataset metadata before it can be normalized. Provide a dClimate client response with variables, dimensions, coordinates, bounds, CRS, fill values, and units, or use createJaxraySource with explicit metadata overrides." : "dClimate response did not include a dataset, readable store, source URL, or CID.",
|
|
939
|
+
void 0,
|
|
940
|
+
{ request, cid, hasStore: Boolean(existingStore), source: options.source }
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
reportProgress(options, {
|
|
944
|
+
stage: "source",
|
|
945
|
+
message: "Normalizing raster metadata",
|
|
946
|
+
percent: 55
|
|
947
|
+
});
|
|
948
|
+
const source = createJaxraySource(dataset, {
|
|
949
|
+
...options,
|
|
950
|
+
id: options.id ?? sourceIdForRequest(request),
|
|
951
|
+
source: options.source ?? (selectedForRendering ? void 0 : gatewaySourceForCid(cid, options.gatewayUrl)),
|
|
952
|
+
store: options.store ?? (selectedForRendering ? void 0 : extractStore(loaded)),
|
|
953
|
+
label: options.label ?? labelForRequest(request)
|
|
954
|
+
});
|
|
955
|
+
runDClimatePreflight(source, options);
|
|
956
|
+
reportProgress(options, {
|
|
957
|
+
stage: "store",
|
|
958
|
+
message: selectedForRendering ? "Preparing bounded raster chunks" : "Preparing raster source",
|
|
959
|
+
percent: selectedForRendering ? 62 : 72
|
|
960
|
+
});
|
|
961
|
+
const selectedStore = selectedForRendering ? await createInMemoryZarrStore(dataset) : void 0;
|
|
962
|
+
const selectedStoreByteLength = selectedStore?.byteLength;
|
|
963
|
+
reportProgress(options, {
|
|
964
|
+
stage: "store",
|
|
965
|
+
message: selectedForRendering ? "Prepared bounded raster chunks" : "Prepared raster source",
|
|
966
|
+
percent: selectedForRendering ? 78 : 80,
|
|
967
|
+
byteLength: selectedStoreByteLength
|
|
968
|
+
});
|
|
969
|
+
reportProgress(options, {
|
|
970
|
+
stage: "source",
|
|
971
|
+
message: "Normalizing raster metadata",
|
|
972
|
+
percent: 84,
|
|
973
|
+
byteLength: selectedStoreByteLength
|
|
974
|
+
});
|
|
975
|
+
const store = options.store ?? selectedStore ?? (selectedForRendering ? void 0 : source.store ?? extractStore(loaded)) ?? (!selectedForRendering && cid && (options.gatewayUrl || options.openIpfsStore) ? await openStore(cid, options) : void 0);
|
|
976
|
+
reportProgress(options, {
|
|
977
|
+
stage: "ready",
|
|
978
|
+
message: "dClimate source ready",
|
|
979
|
+
percent: 100,
|
|
980
|
+
byteLength: selectedStoreByteLength
|
|
981
|
+
});
|
|
982
|
+
return {
|
|
983
|
+
...source,
|
|
984
|
+
store,
|
|
985
|
+
metadata: {
|
|
986
|
+
...source.metadata,
|
|
987
|
+
dclimate: {
|
|
988
|
+
request,
|
|
989
|
+
cid,
|
|
990
|
+
clientMetadata: metadata,
|
|
991
|
+
gatewayUrl: options.gatewayUrl,
|
|
992
|
+
selectedStoreByteLength
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
};
|
|
996
|
+
} catch (error) {
|
|
997
|
+
if (error instanceof DClimateAdapterError || error instanceof GridError) {
|
|
998
|
+
throw error;
|
|
999
|
+
}
|
|
1000
|
+
throw new DClimateAdapterError(
|
|
1001
|
+
"catalog",
|
|
1002
|
+
"Failed to resolve or load the dClimate dataset.",
|
|
1003
|
+
error,
|
|
1004
|
+
{
|
|
1005
|
+
request
|
|
1006
|
+
}
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
async function loadDClimateDataset(request, options) {
|
|
1011
|
+
if (options.client) {
|
|
1012
|
+
reportProgress(options, {
|
|
1013
|
+
stage: "client",
|
|
1014
|
+
message: "Using injected dClimate client",
|
|
1015
|
+
percent: 8
|
|
1016
|
+
});
|
|
1017
|
+
return callClient(options.client, request, options);
|
|
1018
|
+
}
|
|
1019
|
+
if (request.cid && !request.collection && !request.dataset) {
|
|
1020
|
+
reportProgress(options, {
|
|
1021
|
+
stage: "dataset",
|
|
1022
|
+
message: "Using CID dataset reference",
|
|
1023
|
+
percent: 35
|
|
1024
|
+
});
|
|
1025
|
+
return { cid: request.cid };
|
|
1026
|
+
}
|
|
1027
|
+
reportProgress(options, {
|
|
1028
|
+
stage: "client",
|
|
1029
|
+
message: "Loading dClimate client",
|
|
1030
|
+
percent: 8
|
|
1031
|
+
});
|
|
1032
|
+
const module = await import('@dclimate/dclimate-client-js');
|
|
1033
|
+
const Client = module.DClimateClient ?? module.default;
|
|
1034
|
+
if (!Client) {
|
|
1035
|
+
throw new DClimateAdapterError(
|
|
1036
|
+
"catalog",
|
|
1037
|
+
"@dclimate/dclimate-client-js did not export DClimateClient."
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
reportProgress(options, {
|
|
1041
|
+
stage: "dataset",
|
|
1042
|
+
message: "Resolving dClimate dataset",
|
|
1043
|
+
percent: 18
|
|
1044
|
+
});
|
|
1045
|
+
return callClient(new Client(options.clientOptions), request, options);
|
|
1046
|
+
}
|
|
1047
|
+
async function callClient(client, request, options) {
|
|
1048
|
+
if (options.selection?.bounds && client.loadDataset) {
|
|
1049
|
+
return loadBoundedDataset(client, request, options);
|
|
1050
|
+
}
|
|
1051
|
+
if (options.selection) {
|
|
1052
|
+
if (client.selectDataset) {
|
|
1053
|
+
return client.selectDataset({
|
|
1054
|
+
request,
|
|
1055
|
+
selection: options.selection,
|
|
1056
|
+
options: {
|
|
1057
|
+
...options.loadDatasetOptions,
|
|
1058
|
+
gatewayUrl: options.gatewayUrl,
|
|
1059
|
+
returnJaxrayDataset: false
|
|
1060
|
+
}
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
if (options.selection.bounds) {
|
|
1064
|
+
return loadBoundedDataset(client, request, options);
|
|
1065
|
+
}
|
|
1066
|
+
throw new DClimateAdapterError(
|
|
1067
|
+
"unsupported",
|
|
1068
|
+
"Injected dClimate client must expose selectDataset when source selection is requested."
|
|
1069
|
+
);
|
|
1070
|
+
}
|
|
1071
|
+
if (client.loadDataset) {
|
|
1072
|
+
return client.loadDataset({
|
|
1073
|
+
request,
|
|
1074
|
+
options: {
|
|
1075
|
+
...options.loadDatasetOptions,
|
|
1076
|
+
gatewayUrl: options.gatewayUrl,
|
|
1077
|
+
returnJaxrayDataset: true
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
if (client.getDataset) {
|
|
1082
|
+
return client.getDataset(request);
|
|
1083
|
+
}
|
|
1084
|
+
if (client.resolveDataset) {
|
|
1085
|
+
return client.resolveDataset(request);
|
|
1086
|
+
}
|
|
1087
|
+
throw new DClimateAdapterError(
|
|
1088
|
+
"catalog",
|
|
1089
|
+
"Injected dClimate client must expose loadDataset, selectDataset, getDataset, or resolveDataset."
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
async function loadBoundedDataset(client, request, options) {
|
|
1093
|
+
if (!client.loadDataset) {
|
|
1094
|
+
throw new DClimateAdapterError(
|
|
1095
|
+
"unsupported",
|
|
1096
|
+
"Injected dClimate client must expose loadDataset when bounds selection is requested."
|
|
1097
|
+
);
|
|
1098
|
+
}
|
|
1099
|
+
const loaded = await client.loadDataset({
|
|
1100
|
+
request,
|
|
1101
|
+
options: {
|
|
1102
|
+
...options.loadDatasetOptions,
|
|
1103
|
+
gatewayUrl: options.gatewayUrl,
|
|
1104
|
+
returnJaxrayDataset: false
|
|
1105
|
+
}
|
|
1106
|
+
});
|
|
1107
|
+
reportProgress(options, {
|
|
1108
|
+
stage: "selection",
|
|
1109
|
+
message: "Selecting requested time and bounds",
|
|
1110
|
+
percent: 35
|
|
1111
|
+
});
|
|
1112
|
+
const selected = await applyBoundedSelection(loaded, options.selection);
|
|
1113
|
+
reportProgress(options, {
|
|
1114
|
+
stage: "selection",
|
|
1115
|
+
message: "Selected bounded raster window",
|
|
1116
|
+
percent: 52
|
|
1117
|
+
});
|
|
1118
|
+
return replaceLoadedDataset(loaded, selected);
|
|
1119
|
+
}
|
|
1120
|
+
async function applyBoundedSelection(loaded, selection) {
|
|
1121
|
+
const dataset = extractGeoTemporalDataset(loaded);
|
|
1122
|
+
if (!dataset) {
|
|
1123
|
+
throw new DClimateAdapterError(
|
|
1124
|
+
"unsupported",
|
|
1125
|
+
"dClimate bounds selection requires a GeoTemporalDataset response."
|
|
1126
|
+
);
|
|
1127
|
+
}
|
|
1128
|
+
const timeSelected = selection?.timeRange ? await applyTimeRangeSelection(dataset, selection.timeRange) : dataset;
|
|
1129
|
+
const coordinateSelected = await applyCoordinateSelection(timeSelected, selection?.coordinates);
|
|
1130
|
+
if (!selection?.bounds) {
|
|
1131
|
+
return coordinateSelected;
|
|
1132
|
+
}
|
|
1133
|
+
const { bounds, boundsOptions } = normalizeSelectionBounds(
|
|
1134
|
+
selection.bounds,
|
|
1135
|
+
selection.boundsOptions
|
|
1136
|
+
);
|
|
1137
|
+
const [west, south, east, north] = bounds;
|
|
1138
|
+
const gridSelected = await selectGriddedBounds(coordinateSelected, bounds, boundsOptions);
|
|
1139
|
+
if (gridSelected) {
|
|
1140
|
+
return gridSelected;
|
|
1141
|
+
}
|
|
1142
|
+
if (!hasRectangleSelection(coordinateSelected)) {
|
|
1143
|
+
throw new DClimateAdapterError(
|
|
1144
|
+
"unsupported",
|
|
1145
|
+
"dClimate bounds selection requires rectangle or gridded axis selection support."
|
|
1146
|
+
);
|
|
1147
|
+
}
|
|
1148
|
+
return coordinateSelected.rectangle(south, west, north, east, boundsOptions);
|
|
1149
|
+
}
|
|
1150
|
+
async function applyTimeRangeSelection(value, timeRange) {
|
|
1151
|
+
const gridSelected = await selectGriddedTimeRange(value, timeRange);
|
|
1152
|
+
if (gridSelected) {
|
|
1153
|
+
return gridSelected;
|
|
1154
|
+
}
|
|
1155
|
+
if (!hasTimeRange(value)) {
|
|
1156
|
+
return value;
|
|
1157
|
+
}
|
|
1158
|
+
return value.timeRange(timeRange);
|
|
1159
|
+
}
|
|
1160
|
+
async function selectGriddedTimeRange(value, timeRange) {
|
|
1161
|
+
const grid = extractSelectableGrid(value);
|
|
1162
|
+
if (!grid?.coords || typeof grid.sel !== "function") {
|
|
1163
|
+
return void 0;
|
|
1164
|
+
}
|
|
1165
|
+
const timeKey = inferCoordinateKey(grid.coords, DEFAULT_TIME_KEYS);
|
|
1166
|
+
if (!timeKey) {
|
|
1167
|
+
return void 0;
|
|
1168
|
+
}
|
|
1169
|
+
const coordinates = coordinateValues(grid.coords[timeKey]);
|
|
1170
|
+
if (!coordinates?.length) {
|
|
1171
|
+
return void 0;
|
|
1172
|
+
}
|
|
1173
|
+
const range = timeCoordinateRange(coordinates, timeRange, timeCoordinateUnits(grid, timeKey));
|
|
1174
|
+
if (!range) {
|
|
1175
|
+
return void 0;
|
|
1176
|
+
}
|
|
1177
|
+
return grid.sel({
|
|
1178
|
+
[timeKey]: range
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
async function applyCoordinateSelection(value, coordinates) {
|
|
1182
|
+
if (!coordinates || Object.keys(coordinates).length === 0) {
|
|
1183
|
+
return value;
|
|
1184
|
+
}
|
|
1185
|
+
const grid = extractSelectableGrid(value);
|
|
1186
|
+
if (!grid || typeof grid.sel !== "function") {
|
|
1187
|
+
throw new DClimateAdapterError(
|
|
1188
|
+
"unsupported",
|
|
1189
|
+
"dClimate coordinate selection requires gridded axis selection support."
|
|
1190
|
+
);
|
|
1191
|
+
}
|
|
1192
|
+
return grid.sel(normalizeCoordinateSelection(coordinates));
|
|
1193
|
+
}
|
|
1194
|
+
function normalizeCoordinateSelection(coordinates) {
|
|
1195
|
+
return Object.fromEntries(
|
|
1196
|
+
Object.entries(coordinates).map(([dimension, value]) => [
|
|
1197
|
+
dimension,
|
|
1198
|
+
normalizeCoordinateSelectionValue(value)
|
|
1199
|
+
])
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
function normalizeCoordinateSelectionValue(value) {
|
|
1203
|
+
if (value instanceof Date) {
|
|
1204
|
+
return value.toISOString();
|
|
1205
|
+
}
|
|
1206
|
+
if (typeof value !== "object") {
|
|
1207
|
+
return value;
|
|
1208
|
+
}
|
|
1209
|
+
return {
|
|
1210
|
+
start: value.start instanceof Date ? value.start.toISOString() : value.start,
|
|
1211
|
+
stop: value.stop instanceof Date ? value.stop.toISOString() : value.stop
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
async function selectGriddedBounds(value, bounds, boundsOptions) {
|
|
1215
|
+
const grid = extractSelectableGrid(value);
|
|
1216
|
+
if (!grid?.coords || typeof grid.sel !== "function") {
|
|
1217
|
+
return void 0;
|
|
1218
|
+
}
|
|
1219
|
+
const latitudeKey = boundsOptions?.latitudeKey ?? inferCoordinateKey(grid.coords, ["latitude", "lat", "y"]);
|
|
1220
|
+
const longitudeKey = boundsOptions?.longitudeKey ?? inferCoordinateKey(grid.coords, ["longitude", "lon", "lng", "x"]);
|
|
1221
|
+
if (!latitudeKey || !longitudeKey) {
|
|
1222
|
+
return void 0;
|
|
1223
|
+
}
|
|
1224
|
+
const latitudeCoords = numericCoordinates(grid.coords[latitudeKey]);
|
|
1225
|
+
const longitudeCoords = numericCoordinates(grid.coords[longitudeKey]);
|
|
1226
|
+
if (!latitudeCoords || !longitudeCoords || !hasGriddedSpatialAxes(grid, latitudeKey, longitudeKey)) {
|
|
1227
|
+
return void 0;
|
|
1228
|
+
}
|
|
1229
|
+
const [west, south, east, north] = bounds;
|
|
1230
|
+
return grid.sel({
|
|
1231
|
+
[latitudeKey]: coordinateRange(latitudeCoords, south, north),
|
|
1232
|
+
[longitudeKey]: coordinateRange(longitudeCoords, west, east)
|
|
1233
|
+
});
|
|
1234
|
+
}
|
|
1235
|
+
function extractSelectableGrid(value) {
|
|
1236
|
+
if (!isRecord(value)) {
|
|
1237
|
+
return void 0;
|
|
1238
|
+
}
|
|
1239
|
+
const data = value.data;
|
|
1240
|
+
if (isSelectableGrid(data)) {
|
|
1241
|
+
return data;
|
|
1242
|
+
}
|
|
1243
|
+
return isSelectableGrid(value) ? value : void 0;
|
|
1244
|
+
}
|
|
1245
|
+
function isSelectableGrid(value) {
|
|
1246
|
+
return isRecord(value) && isRecord(value.coords) && typeof value.sel === "function";
|
|
1247
|
+
}
|
|
1248
|
+
function timeCoordinateRange(coordinates, timeRange, units) {
|
|
1249
|
+
const start = timeRangeBoundaryMillis(timeRange.start);
|
|
1250
|
+
const end = timeRangeBoundaryMillis(timeRange.end);
|
|
1251
|
+
if (start === void 0 || end === void 0) {
|
|
1252
|
+
return void 0;
|
|
1253
|
+
}
|
|
1254
|
+
const lowerBound = Math.min(start, end);
|
|
1255
|
+
const upperBound = Math.max(start, end);
|
|
1256
|
+
const matchingCoordinates = coordinates.map((value) => ({ value, millis: timeCoordinateMillis(value, units) })).filter(
|
|
1257
|
+
(coordinate) => coordinate.millis !== void 0 && coordinate.millis >= lowerBound && coordinate.millis <= upperBound
|
|
1258
|
+
);
|
|
1259
|
+
if (matchingCoordinates.length === 0) {
|
|
1260
|
+
throw new DClimateAdapterError(
|
|
1261
|
+
"unsupported",
|
|
1262
|
+
"dClimate time range selection did not include any available time coordinates.",
|
|
1263
|
+
void 0,
|
|
1264
|
+
{ timeRange }
|
|
1265
|
+
);
|
|
1266
|
+
}
|
|
1267
|
+
return {
|
|
1268
|
+
start: matchingCoordinates[0]?.value,
|
|
1269
|
+
stop: matchingCoordinates.at(-1)?.value
|
|
1270
|
+
};
|
|
1271
|
+
}
|
|
1272
|
+
function timeCoordinateUnits(grid, timeKey) {
|
|
1273
|
+
const attrs = grid.coordAttrs?.[timeKey];
|
|
1274
|
+
if (isRecord(attrs) && typeof attrs.units === "string") {
|
|
1275
|
+
return attrs.units;
|
|
1276
|
+
}
|
|
1277
|
+
return void 0;
|
|
1278
|
+
}
|
|
1279
|
+
function timeRangeBoundaryMillis(value) {
|
|
1280
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
1281
|
+
return Number.isNaN(date.getTime()) ? void 0 : date.getTime();
|
|
1282
|
+
}
|
|
1283
|
+
function timeCoordinateMillis(value, units) {
|
|
1284
|
+
if (value instanceof Date) {
|
|
1285
|
+
return value.getTime();
|
|
1286
|
+
}
|
|
1287
|
+
if (typeof value === "string") {
|
|
1288
|
+
const date = new Date(value);
|
|
1289
|
+
return Number.isNaN(date.getTime()) ? void 0 : date.getTime();
|
|
1290
|
+
}
|
|
1291
|
+
if (typeof value === "number" && Number.isFinite(value) && units) {
|
|
1292
|
+
return cfTimeCoordinateMillis(value, units);
|
|
1293
|
+
}
|
|
1294
|
+
return void 0;
|
|
1295
|
+
}
|
|
1296
|
+
function cfTimeCoordinateMillis(value, units) {
|
|
1297
|
+
const match = /^(seconds?|minutes?|hours?|days?) since ([0-9]{4}-[0-9]{2}-[0-9]{2})(?:[ T]([0-9:.Z+-]+))?/i.exec(
|
|
1298
|
+
units
|
|
1299
|
+
);
|
|
1300
|
+
if (!match) {
|
|
1301
|
+
return void 0;
|
|
1302
|
+
}
|
|
1303
|
+
const unit = match[1]?.toLowerCase();
|
|
1304
|
+
const date = match[2];
|
|
1305
|
+
const time = match[3] ?? "00:00:00Z";
|
|
1306
|
+
const origin = /* @__PURE__ */ new Date(`${date}T${time.replace(/Z?$/, "Z")}`);
|
|
1307
|
+
if (Number.isNaN(origin.getTime())) {
|
|
1308
|
+
return void 0;
|
|
1309
|
+
}
|
|
1310
|
+
return origin.getTime() + value * cfTimeUnitMultiplier(unit);
|
|
1311
|
+
}
|
|
1312
|
+
function cfTimeUnitMultiplier(unit) {
|
|
1313
|
+
if (unit?.startsWith("second")) {
|
|
1314
|
+
return 1e3;
|
|
1315
|
+
}
|
|
1316
|
+
if (unit?.startsWith("minute")) {
|
|
1317
|
+
return 6e4;
|
|
1318
|
+
}
|
|
1319
|
+
if (unit?.startsWith("hour")) {
|
|
1320
|
+
return 36e5;
|
|
1321
|
+
}
|
|
1322
|
+
return 864e5;
|
|
1323
|
+
}
|
|
1324
|
+
function inferCoordinateKey(coords, candidates) {
|
|
1325
|
+
const keys = Object.keys(coords);
|
|
1326
|
+
const normalizedKeys = keys.map((key) => key.toLowerCase().replace(/[_\-\s]+/g, ""));
|
|
1327
|
+
for (const candidate of candidates) {
|
|
1328
|
+
const index = normalizedKeys.indexOf(candidate.toLowerCase().replace(/[_\-\s]+/g, ""));
|
|
1329
|
+
if (index !== -1) {
|
|
1330
|
+
return keys[index];
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
return void 0;
|
|
1334
|
+
}
|
|
1335
|
+
function numericCoordinates(values) {
|
|
1336
|
+
const raw = coordinateValues(values);
|
|
1337
|
+
if (!raw?.length) {
|
|
1338
|
+
return void 0;
|
|
1339
|
+
}
|
|
1340
|
+
const coordinates = raw.map((value) => typeof value === "number" ? value : Number(value));
|
|
1341
|
+
return coordinates.every((value) => Number.isFinite(value)) ? coordinates : void 0;
|
|
1342
|
+
}
|
|
1343
|
+
function hasGriddedSpatialAxes(grid, latitudeKey, longitudeKey) {
|
|
1344
|
+
if (!Array.isArray(grid.dataVars) || typeof grid.getVariable !== "function") {
|
|
1345
|
+
return false;
|
|
1346
|
+
}
|
|
1347
|
+
return grid.dataVars.some((name) => {
|
|
1348
|
+
if (typeof name !== "string") {
|
|
1349
|
+
return false;
|
|
1350
|
+
}
|
|
1351
|
+
const variable = grid.getVariable?.(name);
|
|
1352
|
+
return isRecord(variable) && Array.isArray(variable.dims) ? variable.dims.includes(latitudeKey) && variable.dims.includes(longitudeKey) : false;
|
|
1353
|
+
});
|
|
1354
|
+
}
|
|
1355
|
+
function coordinateRange(coordinates, lowerBound, upperBound) {
|
|
1356
|
+
const first = coordinates[0];
|
|
1357
|
+
const last = coordinates.at(-1);
|
|
1358
|
+
const ascending = first === void 0 || last === void 0 || first <= last;
|
|
1359
|
+
const start = ascending ? firstCoordinateAtOrAbove(coordinates, lowerBound) : firstCoordinateAtOrBelow(coordinates, upperBound);
|
|
1360
|
+
const stop = ascending ? lastCoordinateAtOrBelow(coordinates, upperBound) : lastCoordinateAtOrAbove(coordinates, lowerBound);
|
|
1361
|
+
return { start, stop };
|
|
1362
|
+
}
|
|
1363
|
+
function firstCoordinateAtOrAbove(coordinates, bound) {
|
|
1364
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate >= bound, "first") ?? nearestCoordinate(coordinates, bound);
|
|
1365
|
+
}
|
|
1366
|
+
function firstCoordinateAtOrBelow(coordinates, bound) {
|
|
1367
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate <= bound, "first") ?? nearestCoordinate(coordinates, bound);
|
|
1368
|
+
}
|
|
1369
|
+
function lastCoordinateAtOrBelow(coordinates, bound) {
|
|
1370
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate <= bound, "last") ?? nearestCoordinate(coordinates, bound);
|
|
1371
|
+
}
|
|
1372
|
+
function lastCoordinateAtOrAbove(coordinates, bound) {
|
|
1373
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate >= bound, "last") ?? nearestCoordinate(coordinates, bound);
|
|
1374
|
+
}
|
|
1375
|
+
function binarySearchCoordinate(coordinates, matches, position) {
|
|
1376
|
+
let low = 0;
|
|
1377
|
+
let high = coordinates.length - 1;
|
|
1378
|
+
let found;
|
|
1379
|
+
while (low <= high) {
|
|
1380
|
+
const middle = Math.floor((low + high) / 2);
|
|
1381
|
+
const coordinate = coordinates[middle];
|
|
1382
|
+
if (coordinate === void 0) {
|
|
1383
|
+
break;
|
|
1384
|
+
}
|
|
1385
|
+
if (matches(coordinate)) {
|
|
1386
|
+
found = coordinate;
|
|
1387
|
+
if (position === "first") {
|
|
1388
|
+
high = middle - 1;
|
|
1389
|
+
} else {
|
|
1390
|
+
low = middle + 1;
|
|
1391
|
+
}
|
|
1392
|
+
continue;
|
|
1393
|
+
}
|
|
1394
|
+
if (position === "first") {
|
|
1395
|
+
low = middle + 1;
|
|
1396
|
+
} else {
|
|
1397
|
+
high = middle - 1;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
return found;
|
|
1401
|
+
}
|
|
1402
|
+
function nearestCoordinate(coordinates, value) {
|
|
1403
|
+
return coordinates.reduce(
|
|
1404
|
+
(nearest, coordinate) => Math.abs(coordinate - value) < Math.abs(nearest - value) ? coordinate : nearest
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
function replaceLoadedDataset(loaded, dataset) {
|
|
1408
|
+
if (Array.isArray(loaded)) {
|
|
1409
|
+
return [dataset, loaded[1]];
|
|
1410
|
+
}
|
|
1411
|
+
return dataset;
|
|
1412
|
+
}
|
|
1413
|
+
async function createInMemoryZarrStore(dataset) {
|
|
1414
|
+
if (!dataset?.coords || !Array.isArray(dataset.dataVars) || typeof dataset.getVariable !== "function") {
|
|
1415
|
+
return void 0;
|
|
1416
|
+
}
|
|
1417
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1418
|
+
const encoder = new TextEncoder();
|
|
1419
|
+
const addJson = (path, value) => {
|
|
1420
|
+
entries.set(path, encoder.encode(JSON.stringify(value)));
|
|
1421
|
+
};
|
|
1422
|
+
const addBytes = (path, value) => {
|
|
1423
|
+
entries.set(path, value);
|
|
1424
|
+
};
|
|
1425
|
+
addJson(".zgroup", { zarr_format: 2 });
|
|
1426
|
+
addJson(".zattrs", {});
|
|
1427
|
+
for (const [dimensionName, coordinateValues2] of coordinateEntries(dataset.coords)) {
|
|
1428
|
+
const coordinates = numericCoordinates(coordinateValues2);
|
|
1429
|
+
const strings = coordinates ? void 0 : stringCoordinates(coordinateValues2);
|
|
1430
|
+
if (!coordinates && !strings) {
|
|
1431
|
+
continue;
|
|
1432
|
+
}
|
|
1433
|
+
if (strings) {
|
|
1434
|
+
addJson(`${dimensionName}/.zarray`, {
|
|
1435
|
+
zarr_format: 2,
|
|
1436
|
+
shape: [strings.length],
|
|
1437
|
+
chunks: [strings.length],
|
|
1438
|
+
dtype: "|O",
|
|
1439
|
+
compressor: null,
|
|
1440
|
+
fill_value: null,
|
|
1441
|
+
order: "C",
|
|
1442
|
+
filters: [{ id: "vlen-utf8" }]
|
|
1443
|
+
});
|
|
1444
|
+
addJson(`${dimensionName}/.zattrs`, {
|
|
1445
|
+
_ARRAY_DIMENSIONS: [dimensionName]
|
|
1446
|
+
});
|
|
1447
|
+
addBytes(`${dimensionName}/0`, encodeVLenUtf8(strings));
|
|
1448
|
+
continue;
|
|
1449
|
+
}
|
|
1450
|
+
if (!coordinates) {
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
addJson(`${dimensionName}/.zarray`, {
|
|
1454
|
+
zarr_format: 2,
|
|
1455
|
+
shape: [coordinates.length],
|
|
1456
|
+
chunks: [coordinates.length],
|
|
1457
|
+
dtype: "<f8",
|
|
1458
|
+
compressor: null,
|
|
1459
|
+
fill_value: null,
|
|
1460
|
+
order: "C",
|
|
1461
|
+
filters: null
|
|
1462
|
+
});
|
|
1463
|
+
addJson(`${dimensionName}/.zattrs`, {
|
|
1464
|
+
_ARRAY_DIMENSIONS: [dimensionName]
|
|
1465
|
+
});
|
|
1466
|
+
addBytes(`${dimensionName}/0`, encodeFloat64(coordinates));
|
|
1467
|
+
}
|
|
1468
|
+
for (const variableName of dataset.dataVars) {
|
|
1469
|
+
if (typeof variableName !== "string") {
|
|
1470
|
+
continue;
|
|
1471
|
+
}
|
|
1472
|
+
const variable = dataset.getVariable(variableName);
|
|
1473
|
+
if (!variable?.dims?.length || !variable.shape?.length) {
|
|
1474
|
+
continue;
|
|
1475
|
+
}
|
|
1476
|
+
const computed = await variable.compute?.();
|
|
1477
|
+
const values = flattenNumericValues(
|
|
1478
|
+
computed?.data ?? computed?.values ?? variable.data ?? variable.values
|
|
1479
|
+
);
|
|
1480
|
+
if (!values) {
|
|
1481
|
+
return void 0;
|
|
1482
|
+
}
|
|
1483
|
+
const chunks = chunkShapeFor(variable.shape);
|
|
1484
|
+
addJson(`${variableName}/.zarray`, {
|
|
1485
|
+
zarr_format: 2,
|
|
1486
|
+
shape: variable.shape,
|
|
1487
|
+
chunks,
|
|
1488
|
+
dtype: "<f4",
|
|
1489
|
+
compressor: null,
|
|
1490
|
+
fill_value: "NaN",
|
|
1491
|
+
order: "C",
|
|
1492
|
+
filters: null
|
|
1493
|
+
});
|
|
1494
|
+
addJson(`${variableName}/.zattrs`, {
|
|
1495
|
+
...variable.attrs,
|
|
1496
|
+
_ARRAY_DIMENSIONS: variable.dims
|
|
1497
|
+
});
|
|
1498
|
+
addVariableChunks(entries, variableName, values, variable.shape, chunks);
|
|
1499
|
+
}
|
|
1500
|
+
const byteLength = Array.from(entries.values()).reduce(
|
|
1501
|
+
(total, value) => total + value.byteLength,
|
|
1502
|
+
0
|
|
1503
|
+
);
|
|
1504
|
+
return {
|
|
1505
|
+
byteLength,
|
|
1506
|
+
get: async (key) => entries.get(normalizeStoreKey(key))
|
|
1507
|
+
};
|
|
1508
|
+
}
|
|
1509
|
+
function coordinateEntries(coords) {
|
|
1510
|
+
if (!coords) {
|
|
1511
|
+
return [];
|
|
1512
|
+
}
|
|
1513
|
+
return coords instanceof Map ? Array.from(coords.entries()) : Object.entries(coords);
|
|
1514
|
+
}
|
|
1515
|
+
function coordinateValues(values) {
|
|
1516
|
+
if (Array.isArray(values)) {
|
|
1517
|
+
return values;
|
|
1518
|
+
}
|
|
1519
|
+
if (ArrayBuffer.isView(values)) {
|
|
1520
|
+
return arrayBufferViewValues(values);
|
|
1521
|
+
}
|
|
1522
|
+
if (!isRecord(values)) {
|
|
1523
|
+
return void 0;
|
|
1524
|
+
}
|
|
1525
|
+
if (Array.isArray(values.values)) {
|
|
1526
|
+
return values.values;
|
|
1527
|
+
}
|
|
1528
|
+
if (ArrayBuffer.isView(values.values)) {
|
|
1529
|
+
return arrayBufferViewValues(values.values);
|
|
1530
|
+
}
|
|
1531
|
+
if (Array.isArray(values.data)) {
|
|
1532
|
+
return values.data;
|
|
1533
|
+
}
|
|
1534
|
+
if (ArrayBuffer.isView(values.data)) {
|
|
1535
|
+
return arrayBufferViewValues(values.data);
|
|
1536
|
+
}
|
|
1537
|
+
return void 0;
|
|
1538
|
+
}
|
|
1539
|
+
function arrayBufferViewValues(values) {
|
|
1540
|
+
return "length" in values ? Array.from(values) : void 0;
|
|
1541
|
+
}
|
|
1542
|
+
function stringCoordinates(values) {
|
|
1543
|
+
const raw = coordinateValues(values);
|
|
1544
|
+
if (!raw?.length) {
|
|
1545
|
+
return void 0;
|
|
1546
|
+
}
|
|
1547
|
+
const coordinates = raw.map((value) => {
|
|
1548
|
+
if (value instanceof Date) {
|
|
1549
|
+
return value.toISOString();
|
|
1550
|
+
}
|
|
1551
|
+
return typeof value === "string" ? value : void 0;
|
|
1552
|
+
});
|
|
1553
|
+
return coordinates.every((value) => value !== void 0) ? coordinates : void 0;
|
|
1554
|
+
}
|
|
1555
|
+
function normalizeStoreKey(key) {
|
|
1556
|
+
return key.replace(/^\/+/, "");
|
|
1557
|
+
}
|
|
1558
|
+
function chunkShapeFor(shape) {
|
|
1559
|
+
if (shape.length === 0) {
|
|
1560
|
+
return [];
|
|
1561
|
+
}
|
|
1562
|
+
return shape.map((size, index) => index === 0 && shape.length > 2 ? 1 : size);
|
|
1563
|
+
}
|
|
1564
|
+
function addVariableChunks(entries, variableName, values, shape, chunks) {
|
|
1565
|
+
if (shape.length === 0) {
|
|
1566
|
+
entries.set(`${variableName}/0`, encodeFloat32(values));
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
const chunkCounts = shape.map((size, index) => Math.ceil(size / (chunks[index] ?? size)));
|
|
1570
|
+
const chunkIndices = enumerateChunkIndices(chunkCounts);
|
|
1571
|
+
for (const chunkIndex of chunkIndices) {
|
|
1572
|
+
const contiguousRange = leadingContiguousChunkRange(shape, chunks, chunkIndex);
|
|
1573
|
+
const bytes = contiguousRange ? encodeFloat32(values, contiguousRange.start, contiguousRange.length) : encodeFloat32(extractChunk(values, shape, chunks, chunkIndex));
|
|
1574
|
+
entries.set(`${variableName}/${chunkIndex.join(".")}`, bytes);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
function leadingContiguousChunkRange(shape, chunks, chunkIndex) {
|
|
1578
|
+
if (shape.length === 0) {
|
|
1579
|
+
return { start: 0, length: 1 };
|
|
1580
|
+
}
|
|
1581
|
+
const starts = chunkIndex.map((index, dimension) => index * (chunks[dimension] ?? 1));
|
|
1582
|
+
const stops = starts.map(
|
|
1583
|
+
(start, dimension) => Math.min(start + (chunks[dimension] ?? 1), shape[dimension] ?? start)
|
|
1584
|
+
);
|
|
1585
|
+
for (let dimension = 1; dimension < shape.length; dimension += 1) {
|
|
1586
|
+
if ((starts[dimension] ?? 0) !== 0 || (stops[dimension] ?? 0) !== (shape[dimension] ?? 0)) {
|
|
1587
|
+
return void 0;
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
const trailingSize = shape.slice(1).reduce((total, size) => total * size, 1);
|
|
1591
|
+
const leadingStart = starts[0] ?? 0;
|
|
1592
|
+
const leadingStop = stops[0] ?? leadingStart;
|
|
1593
|
+
return {
|
|
1594
|
+
start: leadingStart * trailingSize,
|
|
1595
|
+
length: Math.max(0, leadingStop - leadingStart) * trailingSize
|
|
1596
|
+
};
|
|
1597
|
+
}
|
|
1598
|
+
function enumerateChunkIndices(chunkCounts) {
|
|
1599
|
+
const results = [];
|
|
1600
|
+
const visit = (prefix, dimension) => {
|
|
1601
|
+
if (dimension === chunkCounts.length) {
|
|
1602
|
+
results.push(prefix);
|
|
1603
|
+
return;
|
|
1604
|
+
}
|
|
1605
|
+
const count = chunkCounts[dimension] ?? 0;
|
|
1606
|
+
for (let index = 0; index < count; index += 1) {
|
|
1607
|
+
visit([...prefix, index], dimension + 1);
|
|
1608
|
+
}
|
|
1609
|
+
};
|
|
1610
|
+
visit([], 0);
|
|
1611
|
+
return results;
|
|
1612
|
+
}
|
|
1613
|
+
function extractChunk(values, shape, chunks, chunkIndex) {
|
|
1614
|
+
const starts = chunkIndex.map((index, dimension) => index * (chunks[dimension] ?? 1));
|
|
1615
|
+
const stops = starts.map(
|
|
1616
|
+
(start, dimension) => Math.min(start + (chunks[dimension] ?? 1), shape[dimension] ?? start)
|
|
1617
|
+
);
|
|
1618
|
+
const result = [];
|
|
1619
|
+
const visit = (indices, dimension) => {
|
|
1620
|
+
if (dimension === shape.length) {
|
|
1621
|
+
result.push(values[flatIndex(indices, shape)] ?? Number.NaN);
|
|
1622
|
+
return;
|
|
1623
|
+
}
|
|
1624
|
+
const start = starts[dimension] ?? 0;
|
|
1625
|
+
const stop = stops[dimension] ?? start;
|
|
1626
|
+
for (let index = start; index < stop; index += 1) {
|
|
1627
|
+
visit([...indices, index], dimension + 1);
|
|
1628
|
+
}
|
|
1629
|
+
};
|
|
1630
|
+
visit([], 0);
|
|
1631
|
+
return result;
|
|
1632
|
+
}
|
|
1633
|
+
function flatIndex(indices, shape) {
|
|
1634
|
+
return indices.reduce((offset, index, dimension) => offset * (shape[dimension] ?? 1) + index, 0);
|
|
1635
|
+
}
|
|
1636
|
+
function flattenNumericValues(value) {
|
|
1637
|
+
if (value == null) {
|
|
1638
|
+
return void 0;
|
|
1639
|
+
}
|
|
1640
|
+
if (typeof value === "number") {
|
|
1641
|
+
return [value];
|
|
1642
|
+
}
|
|
1643
|
+
if (isFlatNumericArrayLike(value)) {
|
|
1644
|
+
return value;
|
|
1645
|
+
}
|
|
1646
|
+
if (!Array.isArray(value)) {
|
|
1647
|
+
return void 0;
|
|
1648
|
+
}
|
|
1649
|
+
const result = [];
|
|
1650
|
+
const visit = (current) => {
|
|
1651
|
+
if (typeof current === "number") {
|
|
1652
|
+
result.push(current);
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1655
|
+
if (isFlatNumericArrayLike(current)) {
|
|
1656
|
+
for (let index = 0; index < current.length; index += 1) {
|
|
1657
|
+
result.push(current[index] ?? Number.NaN);
|
|
1658
|
+
}
|
|
1659
|
+
return;
|
|
1660
|
+
}
|
|
1661
|
+
if (Array.isArray(current)) {
|
|
1662
|
+
for (const item of current) {
|
|
1663
|
+
visit(item);
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
};
|
|
1667
|
+
visit(value);
|
|
1668
|
+
return result;
|
|
1669
|
+
}
|
|
1670
|
+
function isFlatNumericArrayLike(value) {
|
|
1671
|
+
if (Array.isArray(value)) {
|
|
1672
|
+
return value.every((item) => typeof item === "number");
|
|
1673
|
+
}
|
|
1674
|
+
return ArrayBuffer.isView(value) && "length" in value && typeof value.length === "number" && !isBigIntArray(value);
|
|
1675
|
+
}
|
|
1676
|
+
function isBigIntArray(value) {
|
|
1677
|
+
return typeof BigInt64Array !== "undefined" && value instanceof BigInt64Array ? true : typeof BigUint64Array !== "undefined" && value instanceof BigUint64Array;
|
|
1678
|
+
}
|
|
1679
|
+
function encodeFloat32(values, start = 0, length = values.length - start) {
|
|
1680
|
+
if (length <= 0) {
|
|
1681
|
+
return new Uint8Array();
|
|
1682
|
+
}
|
|
1683
|
+
if (values instanceof Float32Array && isNativeLittleEndian()) {
|
|
1684
|
+
return new Uint8Array(values.buffer, values.byteOffset + start * 4, length * 4);
|
|
1685
|
+
}
|
|
1686
|
+
const bytes = new Uint8Array(length * 4);
|
|
1687
|
+
const view = new DataView(bytes.buffer);
|
|
1688
|
+
for (let index = 0; index < length; index += 1) {
|
|
1689
|
+
view.setFloat32(index * 4, values[start + index] ?? Number.NaN, true);
|
|
1690
|
+
}
|
|
1691
|
+
return bytes;
|
|
1692
|
+
}
|
|
1693
|
+
var nativeLittleEndian;
|
|
1694
|
+
function isNativeLittleEndian() {
|
|
1695
|
+
nativeLittleEndian ??= new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1696
|
+
return nativeLittleEndian;
|
|
1697
|
+
}
|
|
1698
|
+
function encodeFloat64(values) {
|
|
1699
|
+
const bytes = new Uint8Array(values.length * 8);
|
|
1700
|
+
const view = new DataView(bytes.buffer);
|
|
1701
|
+
values.forEach((value, index) => view.setFloat64(index * 8, value, true));
|
|
1702
|
+
return bytes;
|
|
1703
|
+
}
|
|
1704
|
+
function encodeVLenUtf8(values) {
|
|
1705
|
+
const encoder = new TextEncoder();
|
|
1706
|
+
const encodedValues = values.map((value) => encoder.encode(value));
|
|
1707
|
+
const byteLength = 4 + encodedValues.reduce((total, value) => total + 4 + value.byteLength, 0);
|
|
1708
|
+
const bytes = new Uint8Array(byteLength);
|
|
1709
|
+
const view = new DataView(bytes.buffer);
|
|
1710
|
+
let offset = 0;
|
|
1711
|
+
view.setUint32(offset, encodedValues.length, true);
|
|
1712
|
+
offset += 4;
|
|
1713
|
+
for (const value of encodedValues) {
|
|
1714
|
+
view.setUint32(offset, value.byteLength, true);
|
|
1715
|
+
offset += 4;
|
|
1716
|
+
bytes.set(value, offset);
|
|
1717
|
+
offset += value.byteLength;
|
|
1718
|
+
}
|
|
1719
|
+
return bytes;
|
|
1720
|
+
}
|
|
1721
|
+
function normalizeSelectionBounds(bounds, fallbackOptions) {
|
|
1722
|
+
const isTuple = isSelectionBoundsTuple(bounds);
|
|
1723
|
+
const normalizedBounds = isTuple ? bounds : [bounds.west, bounds.south, bounds.east, bounds.north];
|
|
1724
|
+
if (normalizedBounds.length !== 4 || normalizedBounds.some((value) => typeof value !== "number" || !Number.isFinite(value))) {
|
|
1725
|
+
throw new DClimateAdapterError(
|
|
1726
|
+
"metadata",
|
|
1727
|
+
"dClimate selection bounds must be finite [west, south, east, north] numbers."
|
|
1728
|
+
);
|
|
1729
|
+
}
|
|
1730
|
+
const [west, south, east, north] = normalizedBounds;
|
|
1731
|
+
if (west >= east || south >= north) {
|
|
1732
|
+
throw new DClimateAdapterError(
|
|
1733
|
+
"metadata",
|
|
1734
|
+
"dClimate selection bounds must satisfy west < east and south < north."
|
|
1735
|
+
);
|
|
1736
|
+
}
|
|
1737
|
+
return {
|
|
1738
|
+
bounds: [west, south, east, north],
|
|
1739
|
+
boundsOptions: isTuple ? fallbackOptions : bounds.options ?? fallbackOptions
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
function isSelectionBoundsTuple(bounds) {
|
|
1743
|
+
return Array.isArray(bounds);
|
|
1744
|
+
}
|
|
1745
|
+
async function openStore(cid, options) {
|
|
1746
|
+
try {
|
|
1747
|
+
if (options.openIpfsStore) {
|
|
1748
|
+
return options.openIpfsStore(cid, { gatewayUrl: options.gatewayUrl });
|
|
1749
|
+
}
|
|
1750
|
+
const module = await import('@dclimate/jaxray');
|
|
1751
|
+
const opened = await module.openIpfsStore?.(cid, { gatewayUrl: options.gatewayUrl });
|
|
1752
|
+
if (isRecord(opened) && "store" in opened && isReadableStore(opened.store)) {
|
|
1753
|
+
return opened.store;
|
|
1754
|
+
}
|
|
1755
|
+
return isReadableStore(opened) ? opened : void 0;
|
|
1756
|
+
} catch (error) {
|
|
1757
|
+
throw new DClimateAdapterError(
|
|
1758
|
+
"gateway",
|
|
1759
|
+
`Failed to open dClimate CID "${cid}" through Jaxray.`,
|
|
1760
|
+
error,
|
|
1761
|
+
{
|
|
1762
|
+
cid,
|
|
1763
|
+
gatewayUrl: options.gatewayUrl
|
|
1764
|
+
}
|
|
1765
|
+
);
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
function extractDataset(value) {
|
|
1769
|
+
if (Array.isArray(value)) {
|
|
1770
|
+
return extractDataset(value[0]);
|
|
1771
|
+
}
|
|
1772
|
+
if (!isRecord(value)) {
|
|
1773
|
+
return void 0;
|
|
1774
|
+
}
|
|
1775
|
+
const candidate = value.data ?? value.dataset ?? value.jaxrayDataset ?? value.geoTemporalDataset ?? (hasDatasetShape(value) ? value : void 0);
|
|
1776
|
+
return isRecord(candidate) ? candidate : void 0;
|
|
1777
|
+
}
|
|
1778
|
+
function extractGeoTemporalDataset(value) {
|
|
1779
|
+
if (Array.isArray(value)) {
|
|
1780
|
+
return extractGeoTemporalDataset(value[0]);
|
|
1781
|
+
}
|
|
1782
|
+
if (!isRecord(value)) {
|
|
1783
|
+
return void 0;
|
|
1784
|
+
}
|
|
1785
|
+
return hasRectangleSelection(value) || hasTimeRange(value) ? value : void 0;
|
|
1786
|
+
}
|
|
1787
|
+
function hasTimeRange(value) {
|
|
1788
|
+
return isRecord(value) && typeof value.timeRange === "function";
|
|
1789
|
+
}
|
|
1790
|
+
function hasRectangleSelection(value) {
|
|
1791
|
+
return isRecord(value) && typeof value.rectangle === "function";
|
|
1792
|
+
}
|
|
1793
|
+
function extractMetadata(value) {
|
|
1794
|
+
if (Array.isArray(value)) {
|
|
1795
|
+
return isRecord(value[1]) ? value[1] : void 0;
|
|
1796
|
+
}
|
|
1797
|
+
if (!isRecord(value)) {
|
|
1798
|
+
return void 0;
|
|
1799
|
+
}
|
|
1800
|
+
const metadata = value.metadata ?? value.clientMetadata;
|
|
1801
|
+
return isRecord(metadata) ? metadata : void 0;
|
|
1802
|
+
}
|
|
1803
|
+
function extractStore(value) {
|
|
1804
|
+
if (!isRecord(value)) {
|
|
1805
|
+
return void 0;
|
|
1806
|
+
}
|
|
1807
|
+
const candidate = value.store ?? value.zarrStore ?? value.ipfsStore;
|
|
1808
|
+
return isReadableStore(candidate) ? candidate : void 0;
|
|
1809
|
+
}
|
|
1810
|
+
function extractString(value, keys) {
|
|
1811
|
+
const metadata = extractMetadata(value);
|
|
1812
|
+
if (metadata) {
|
|
1813
|
+
for (const key of keys) {
|
|
1814
|
+
const candidate = metadata[key];
|
|
1815
|
+
if (typeof candidate === "string") {
|
|
1816
|
+
return candidate;
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
if (!isRecord(value)) {
|
|
1821
|
+
return void 0;
|
|
1822
|
+
}
|
|
1823
|
+
for (const key of keys) {
|
|
1824
|
+
const candidate = value[key];
|
|
1825
|
+
if (typeof candidate === "string") {
|
|
1826
|
+
return candidate;
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
return void 0;
|
|
1830
|
+
}
|
|
1831
|
+
function gatewaySourceForCid(cid, gatewayUrl) {
|
|
1832
|
+
if (!cid || !gatewayUrl) {
|
|
1833
|
+
return void 0;
|
|
1834
|
+
}
|
|
1835
|
+
return `${gatewayUrl.replace(/\/$/, "")}/ipfs/${cid}`;
|
|
1836
|
+
}
|
|
1837
|
+
function sourceIdForRequest(request) {
|
|
1838
|
+
if (request.cid) {
|
|
1839
|
+
return `dclimate-${request.cid.slice(0, 12)}`;
|
|
1840
|
+
}
|
|
1841
|
+
return [request.collection, request.dataset, request.variant].filter(Boolean).join("-");
|
|
1842
|
+
}
|
|
1843
|
+
function labelForRequest(request) {
|
|
1844
|
+
return [request.collection, request.dataset, request.variant].filter(Boolean).join(" / ") || request.cid;
|
|
1845
|
+
}
|
|
1846
|
+
function isRecord(value) {
|
|
1847
|
+
return typeof value === "object" && value !== null;
|
|
1848
|
+
}
|
|
1849
|
+
function hasDatasetShape(value) {
|
|
1850
|
+
return "data_vars" in value || "dataVars" in value || "variables" in value;
|
|
1851
|
+
}
|
|
1852
|
+
function isReadableStore(value) {
|
|
1853
|
+
return isRecord(value) && typeof value.get === "function";
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
exports.DClimateAdapterError = DClimateAdapterError;
|
|
1857
|
+
exports.createDClimateSource = createDClimateSource;
|
|
1858
|
+
//# sourceMappingURL=index.cjs.map
|
|
1859
|
+
//# sourceMappingURL=index.cjs.map
|