@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
package/dist/index.js
ADDED
|
@@ -0,0 +1,3038 @@
|
|
|
1
|
+
// src/core/errors.ts
|
|
2
|
+
var GridError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
cause;
|
|
5
|
+
dimension;
|
|
6
|
+
variable;
|
|
7
|
+
sourceId;
|
|
8
|
+
context;
|
|
9
|
+
constructor(details) {
|
|
10
|
+
super(details.message);
|
|
11
|
+
this.name = "GridError";
|
|
12
|
+
this.code = details.code;
|
|
13
|
+
this.cause = details.cause;
|
|
14
|
+
this.dimension = details.dimension;
|
|
15
|
+
this.variable = details.variable;
|
|
16
|
+
this.sourceId = details.sourceId;
|
|
17
|
+
this.context = details.context;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function isGridError(error) {
|
|
21
|
+
return error instanceof GridError;
|
|
22
|
+
}
|
|
23
|
+
function toGridError(details) {
|
|
24
|
+
return details instanceof GridError ? details : new GridError(details);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/core/color.ts
|
|
28
|
+
var builtinPalettes = {
|
|
29
|
+
viridis: ["#440154", "#31688e", "#35b779", "#fde725"],
|
|
30
|
+
inferno: ["#000004", "#57106e", "#bc3754", "#f98e09", "#fcffa4"],
|
|
31
|
+
magma: ["#000004", "#3b0f70", "#8c2981", "#de4968", "#fe9f6d", "#fcfdbf"],
|
|
32
|
+
plasma: ["#0d0887", "#7e03a8", "#cc4778", "#f89540", "#f0f921"],
|
|
33
|
+
cividis: ["#00204c", "#31446b", "#666870", "#958f78", "#c6b866", "#ffe945"],
|
|
34
|
+
turbo: ["#30123b", "#466be3", "#35ab6b", "#faba39", "#7a0403"],
|
|
35
|
+
greys: ["#000000", "#777777", "#ffffff"],
|
|
36
|
+
grays: ["#000000", "#777777", "#ffffff"],
|
|
37
|
+
temperature: ["#2c5ab4", "#67b0f0", "#e0e4dc", "#ffb266", "#cd4434"],
|
|
38
|
+
precipitation: ["#bae6fd", "#60a5fa", "#2563eb", "#4f46e5", "#6d28d9"],
|
|
39
|
+
humidity: ["#374151", "#2d5a6e", "#1890b0", "#2dd4bf", "#99f6e4"],
|
|
40
|
+
vegetation: ["#f7fee7", "#bef264", "#4ade80", "#15803d", "#064e3b"],
|
|
41
|
+
wind: ["#e2e8f0", "#7dd3fc", "#2dd4bf", "#14b8a6"]
|
|
42
|
+
};
|
|
43
|
+
function validateColorScale(colorScale) {
|
|
44
|
+
const [min, max] = colorScale.domain;
|
|
45
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min >= max) {
|
|
46
|
+
throw new GridError({
|
|
47
|
+
code: "INVALID_COLOR_SCALE",
|
|
48
|
+
message: "Color scale domain must be finite and ordered as [min, max].",
|
|
49
|
+
context: { domain: colorScale.domain }
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (!colorScale.palette) {
|
|
53
|
+
throw new GridError({
|
|
54
|
+
code: "INVALID_COLOR_SCALE",
|
|
55
|
+
message: "Color scale must include a palette name or color stop array."
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
paletteToRendererColorMap(colorScale.palette);
|
|
59
|
+
return colorScale;
|
|
60
|
+
}
|
|
61
|
+
function colorScaleToRendererOptions(colorScale) {
|
|
62
|
+
if (!colorScale) {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
validateColorScale(colorScale);
|
|
66
|
+
return {
|
|
67
|
+
colormap: paletteToRendererColorMap(colorScale.palette),
|
|
68
|
+
clim: colorScale.domain
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function paletteToRendererColorMap(palette) {
|
|
72
|
+
if (typeof palette !== "string") {
|
|
73
|
+
if (palette.length < 2) {
|
|
74
|
+
throw new GridError({
|
|
75
|
+
code: "INVALID_COLOR_SCALE",
|
|
76
|
+
message: "Custom color palettes must include at least two color stops.",
|
|
77
|
+
context: { palette }
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const colors2 = palette.map((color) => color.trim());
|
|
81
|
+
const invalid = colors2.find((color) => color.length === 0);
|
|
82
|
+
if (invalid !== void 0) {
|
|
83
|
+
throw new GridError({
|
|
84
|
+
code: "INVALID_COLOR_SCALE",
|
|
85
|
+
message: "Custom color palettes cannot include empty color stops.",
|
|
86
|
+
context: { palette }
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return colors2;
|
|
90
|
+
}
|
|
91
|
+
const normalized = palette.toLowerCase();
|
|
92
|
+
const colors = builtinPalettes[normalized];
|
|
93
|
+
if (!colors) {
|
|
94
|
+
throw new GridError({
|
|
95
|
+
code: "INVALID_COLOR_SCALE",
|
|
96
|
+
message: `Unsupported renderer palette "${palette}". Use one of: ${Object.keys(builtinPalettes).join(", ")}.`,
|
|
97
|
+
context: { palette }
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return colors;
|
|
101
|
+
}
|
|
102
|
+
function colorScaleDisplayDomain(colorScale) {
|
|
103
|
+
const { displayUnit, quantity, unit } = colorScale;
|
|
104
|
+
if (!displayUnit || !unit || displayUnit === unit || !quantity) {
|
|
105
|
+
return colorScale.domain;
|
|
106
|
+
}
|
|
107
|
+
return [
|
|
108
|
+
convertDisplayValue(colorScale.domain[0], {
|
|
109
|
+
fromUnit: unit,
|
|
110
|
+
quantity,
|
|
111
|
+
toUnit: displayUnit
|
|
112
|
+
}),
|
|
113
|
+
convertDisplayValue(colorScale.domain[1], {
|
|
114
|
+
fromUnit: unit,
|
|
115
|
+
quantity,
|
|
116
|
+
toUnit: displayUnit
|
|
117
|
+
})
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
function convertDisplayValue(value, options) {
|
|
121
|
+
if (!Number.isFinite(value)) {
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
if (options.quantity === "temperature") {
|
|
125
|
+
return temperatureFromKelvin(temperatureToKelvin(value, options.fromUnit), options.toUnit);
|
|
126
|
+
}
|
|
127
|
+
return precipitationFromMeters(precipitationToMeters(value, options.fromUnit), options.toUnit);
|
|
128
|
+
}
|
|
129
|
+
function temperatureToKelvin(value, unit) {
|
|
130
|
+
const normalized = normalizeUnit(unit);
|
|
131
|
+
if (normalized === "k" || normalized === "kelvin") {
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
if (["c", "degc", "celsius"].includes(normalized)) {
|
|
135
|
+
return value + 273.15;
|
|
136
|
+
}
|
|
137
|
+
if (["f", "degf", "fahrenheit"].includes(normalized)) {
|
|
138
|
+
return (value - 32) * 5 / 9 + 273.15;
|
|
139
|
+
}
|
|
140
|
+
throw unsupportedUnit(unit, "temperature");
|
|
141
|
+
}
|
|
142
|
+
function temperatureFromKelvin(value, unit) {
|
|
143
|
+
const normalized = normalizeUnit(unit);
|
|
144
|
+
if (normalized === "k" || normalized === "kelvin") {
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
if (["c", "degc", "celsius"].includes(normalized)) {
|
|
148
|
+
return value - 273.15;
|
|
149
|
+
}
|
|
150
|
+
if (["f", "degf", "fahrenheit"].includes(normalized)) {
|
|
151
|
+
return (value - 273.15) * 9 / 5 + 32;
|
|
152
|
+
}
|
|
153
|
+
throw unsupportedUnit(unit, "temperature");
|
|
154
|
+
}
|
|
155
|
+
function precipitationToMeters(value, unit) {
|
|
156
|
+
const normalized = normalizeUnit(unit);
|
|
157
|
+
if (["m", "meter", "meters"].includes(normalized)) {
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
if (["mm", "millimeter", "millimeters"].includes(normalized)) {
|
|
161
|
+
return value / 1e3;
|
|
162
|
+
}
|
|
163
|
+
if (isKilogramPerSquareMeter(normalized)) {
|
|
164
|
+
return value / 1e3;
|
|
165
|
+
}
|
|
166
|
+
if (["in", "inch", "inches"].includes(normalized)) {
|
|
167
|
+
return value * 0.0254;
|
|
168
|
+
}
|
|
169
|
+
throw unsupportedUnit(unit, "precipitation");
|
|
170
|
+
}
|
|
171
|
+
function precipitationFromMeters(value, unit) {
|
|
172
|
+
const normalized = normalizeUnit(unit);
|
|
173
|
+
if (["m", "meter", "meters"].includes(normalized)) {
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
if (["mm", "millimeter", "millimeters"].includes(normalized)) {
|
|
177
|
+
return value * 1e3;
|
|
178
|
+
}
|
|
179
|
+
if (isKilogramPerSquareMeter(normalized)) {
|
|
180
|
+
return value * 1e3;
|
|
181
|
+
}
|
|
182
|
+
if (["in", "inch", "inches"].includes(normalized)) {
|
|
183
|
+
return value / 0.0254;
|
|
184
|
+
}
|
|
185
|
+
throw unsupportedUnit(unit, "precipitation");
|
|
186
|
+
}
|
|
187
|
+
function isKilogramPerSquareMeter(normalizedUnit) {
|
|
188
|
+
const compact = normalizedUnit.replace(/\s+/g, "");
|
|
189
|
+
return ["kgm**-2", "kgm^-2", "kgm-2", "kg/m2", "kg/m^2"].includes(compact);
|
|
190
|
+
}
|
|
191
|
+
function normalizeUnit(unit) {
|
|
192
|
+
return unit.trim().toLowerCase().replace(/^degrees?_?/, "deg").replace("\xB0", "deg");
|
|
193
|
+
}
|
|
194
|
+
function unsupportedUnit(unit, quantity) {
|
|
195
|
+
return new GridError({
|
|
196
|
+
code: "UNSUPPORTED_UNIT",
|
|
197
|
+
message: `Unsupported ${quantity} display unit "${unit}".`,
|
|
198
|
+
context: { quantity, unit }
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/core/validation.ts
|
|
203
|
+
var xAliases = /* @__PURE__ */ new Set(["x", "lon", "lng", "longitude"]);
|
|
204
|
+
var yAliases = /* @__PURE__ */ new Set(["y", "lat", "latitude"]);
|
|
205
|
+
var numericDtypes = /* @__PURE__ */ new Set([
|
|
206
|
+
"int8",
|
|
207
|
+
"uint8",
|
|
208
|
+
"int16",
|
|
209
|
+
"uint16",
|
|
210
|
+
"int32",
|
|
211
|
+
"uint32",
|
|
212
|
+
"float32",
|
|
213
|
+
"float64",
|
|
214
|
+
"i1",
|
|
215
|
+
"u1",
|
|
216
|
+
"i2",
|
|
217
|
+
"u2",
|
|
218
|
+
"i4",
|
|
219
|
+
"u4",
|
|
220
|
+
"f4",
|
|
221
|
+
"f8"
|
|
222
|
+
]);
|
|
223
|
+
function findVariable(source, variableName) {
|
|
224
|
+
return source.variables.find(
|
|
225
|
+
(variable) => variable.name === variableName || variable.path === variableName
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
function findDimension(source, dimensionName) {
|
|
229
|
+
return source.dimensions.find((dimension) => dimension.name === dimensionName);
|
|
230
|
+
}
|
|
231
|
+
function inferSpatialDimensions(source, variableName = source.variables[0]?.name) {
|
|
232
|
+
const variable = variableName ? findVariable(source, variableName) : source.variables[0];
|
|
233
|
+
if (source.spatialDimensions) {
|
|
234
|
+
return variableIncludesSpatialDimensions(variable, source.spatialDimensions) ? source.spatialDimensions : void 0;
|
|
235
|
+
}
|
|
236
|
+
const candidateNames = variable?.dimensions ?? source.dimensions.map((dimension) => dimension.name);
|
|
237
|
+
let x;
|
|
238
|
+
let y;
|
|
239
|
+
for (const name of candidateNames) {
|
|
240
|
+
const dimension = findDimension(source, name);
|
|
241
|
+
const normalizedName = name.toLowerCase();
|
|
242
|
+
const standardName = String(dimension?.standardName ?? "").toLowerCase();
|
|
243
|
+
const kind = dimension?.kind;
|
|
244
|
+
if (!x && (kind === "x" || xAliases.has(normalizedName) || standardName === "longitude")) {
|
|
245
|
+
x = name;
|
|
246
|
+
}
|
|
247
|
+
if (!y && (kind === "y" || yAliases.has(normalizedName) || standardName === "latitude")) {
|
|
248
|
+
y = name;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return x && y ? { x, y } : void 0;
|
|
252
|
+
}
|
|
253
|
+
function variableIncludesSpatialDimensions(variable, spatialDimensions) {
|
|
254
|
+
if (!variable) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
return variable.dimensions.includes(spatialDimensions.x) && variable.dimensions.includes(spatialDimensions.y);
|
|
258
|
+
}
|
|
259
|
+
function validateGridDataSource(source, options = {}) {
|
|
260
|
+
const errors = [];
|
|
261
|
+
const warnings = [];
|
|
262
|
+
if (source.variables.length === 0) {
|
|
263
|
+
errors.push(
|
|
264
|
+
new GridError({
|
|
265
|
+
code: "MISSING_VARIABLE",
|
|
266
|
+
message: "GridDataSource must expose at least one variable.",
|
|
267
|
+
sourceId: source.id
|
|
268
|
+
})
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
const variable = options.variable ? findVariable(source, options.variable) : source.variables[0];
|
|
272
|
+
if (options.variable && !variable) {
|
|
273
|
+
errors.push(
|
|
274
|
+
new GridError({
|
|
275
|
+
code: "MISSING_VARIABLE",
|
|
276
|
+
message: `Variable "${options.variable}" is not present in the grid source.`,
|
|
277
|
+
sourceId: source.id,
|
|
278
|
+
variable: options.variable
|
|
279
|
+
})
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
for (const dimension of source.dimensions) {
|
|
283
|
+
if (!Number.isInteger(dimension.size) || dimension.size <= 0) {
|
|
284
|
+
errors.push(
|
|
285
|
+
new GridError({
|
|
286
|
+
code: "UNSUPPORTED_DIMENSION",
|
|
287
|
+
message: `Dimension "${dimension.name}" must have a positive integer size.`,
|
|
288
|
+
dimension: dimension.name,
|
|
289
|
+
sourceId: source.id
|
|
290
|
+
})
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
if (dimension.coordinates && dimension.coordinates.length !== dimension.size) {
|
|
294
|
+
errors.push(
|
|
295
|
+
new GridError({
|
|
296
|
+
code: "MISSING_COORDINATES",
|
|
297
|
+
message: `Dimension "${dimension.name}" has ${dimension.coordinates.length} coordinates for size ${dimension.size}.`,
|
|
298
|
+
dimension: dimension.name,
|
|
299
|
+
sourceId: source.id
|
|
300
|
+
})
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (variable) {
|
|
305
|
+
if (!isNumericDType(variable.dtype)) {
|
|
306
|
+
errors.push(
|
|
307
|
+
new GridError({
|
|
308
|
+
code: "UNSUPPORTED_DTYPE",
|
|
309
|
+
message: `Variable "${variable.name}" has unsupported dtype "${variable.dtype}".`,
|
|
310
|
+
variable: variable.name,
|
|
311
|
+
sourceId: source.id
|
|
312
|
+
})
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
if (variable.dimensions.length !== variable.shape.length) {
|
|
316
|
+
errors.push(
|
|
317
|
+
new GridError({
|
|
318
|
+
code: "MISSING_DIMENSION",
|
|
319
|
+
message: `Variable "${variable.name}" has ${variable.dimensions.length} dimensions but ${variable.shape.length} shape entries.`,
|
|
320
|
+
variable: variable.name,
|
|
321
|
+
sourceId: source.id
|
|
322
|
+
})
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
variable.dimensions.forEach((dimensionName, index) => {
|
|
326
|
+
const dimension = findDimension(source, dimensionName);
|
|
327
|
+
if (!dimension) {
|
|
328
|
+
errors.push(
|
|
329
|
+
new GridError({
|
|
330
|
+
code: "MISSING_DIMENSION",
|
|
331
|
+
message: `Variable "${variable.name}" references missing dimension "${dimensionName}".`,
|
|
332
|
+
dimension: dimensionName,
|
|
333
|
+
variable: variable.name,
|
|
334
|
+
sourceId: source.id
|
|
335
|
+
})
|
|
336
|
+
);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const expectedSize = variable.shape[index];
|
|
340
|
+
if (expectedSize !== dimension.size) {
|
|
341
|
+
warnings.push(
|
|
342
|
+
`Variable "${variable.name}" shape for dimension "${dimensionName}" is ${expectedSize}, but the dimension size is ${dimension.size}.`
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
const spatialDimensions = inferSpatialDimensions(source, variable.name);
|
|
347
|
+
if (!spatialDimensions) {
|
|
348
|
+
errors.push(
|
|
349
|
+
new GridError({
|
|
350
|
+
code: "MISSING_SPATIAL_DIMENSIONS",
|
|
351
|
+
message: `Variable "${variable.name}" needs latitude/longitude or x/y spatial dimensions before it can render.`,
|
|
352
|
+
variable: variable.name,
|
|
353
|
+
sourceId: source.id
|
|
354
|
+
})
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
if (options.requireRenderable && !source.bounds) {
|
|
358
|
+
errors.push(
|
|
359
|
+
new GridError({
|
|
360
|
+
code: "MISSING_BOUNDS",
|
|
361
|
+
message: "Renderable grid sources must include explicit [west, south, east, north] bounds.",
|
|
362
|
+
variable: variable.name,
|
|
363
|
+
sourceId: source.id
|
|
364
|
+
})
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return {
|
|
369
|
+
ok: errors.length === 0,
|
|
370
|
+
errors,
|
|
371
|
+
warnings
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
function assertValidGridDataSource(source, options = {}) {
|
|
375
|
+
const result = validateGridDataSource(source, options);
|
|
376
|
+
if (!result.ok) {
|
|
377
|
+
throw result.errors[0];
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function isNumericDType(dtype) {
|
|
381
|
+
return numericDtypes.has(dtype.toLowerCase()) || /^[<>|]?[ifu]\d+$/.test(dtype.toLowerCase());
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// src/core/selectors.ts
|
|
385
|
+
function indexSelector(index) {
|
|
386
|
+
return { kind: "index", index };
|
|
387
|
+
}
|
|
388
|
+
function coordinateSelector(value) {
|
|
389
|
+
return { kind: "coordinate", value };
|
|
390
|
+
}
|
|
391
|
+
function isoTimeSelector(iso) {
|
|
392
|
+
return { kind: "isoTime", iso };
|
|
393
|
+
}
|
|
394
|
+
function normalizeSelectors(source, variableName, selectors = {}) {
|
|
395
|
+
const variable = findVariable(source, variableName);
|
|
396
|
+
if (!variable) {
|
|
397
|
+
throw new GridError({
|
|
398
|
+
code: "MISSING_VARIABLE",
|
|
399
|
+
message: `Variable "${variableName}" is not present in the grid source.`,
|
|
400
|
+
variable: variableName,
|
|
401
|
+
sourceId: source.id
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
const normalized = {};
|
|
405
|
+
for (const dimensionName of variable.dimensions) {
|
|
406
|
+
const dimension = findDimension(source, dimensionName);
|
|
407
|
+
if (!dimension) {
|
|
408
|
+
throw new GridError({
|
|
409
|
+
code: "MISSING_DIMENSION",
|
|
410
|
+
message: `Selector normalization cannot find dimension "${dimensionName}".`,
|
|
411
|
+
dimension: dimensionName,
|
|
412
|
+
variable: variableName,
|
|
413
|
+
sourceId: source.id
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
const input = selectors[dimensionName];
|
|
417
|
+
if (input === void 0) {
|
|
418
|
+
normalized[dimensionName] = defaultSelectorForDimension(dimension);
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
normalized[dimensionName] = normalizeSelectorValue(dimension, input, source.id);
|
|
422
|
+
}
|
|
423
|
+
return normalized;
|
|
424
|
+
}
|
|
425
|
+
function normalizeSelectorValue(dimension, input, sourceId) {
|
|
426
|
+
if (input instanceof Date) {
|
|
427
|
+
return { kind: "coordinate", value: input.toISOString() };
|
|
428
|
+
}
|
|
429
|
+
if (typeof input !== "object") {
|
|
430
|
+
return normalizeCoordinateSelector(dimension, input, sourceId);
|
|
431
|
+
}
|
|
432
|
+
if (input.kind === "coordinate") {
|
|
433
|
+
const value = input.value instanceof Date ? input.value.toISOString() : input.value;
|
|
434
|
+
return normalizeCoordinateSelector(dimension, value, sourceId);
|
|
435
|
+
}
|
|
436
|
+
if (input.kind === "index") {
|
|
437
|
+
assertIndexInRange(dimension, input.index, sourceId);
|
|
438
|
+
const value = dimension.coordinates?.[input.index];
|
|
439
|
+
return {
|
|
440
|
+
kind: "index",
|
|
441
|
+
index: input.index,
|
|
442
|
+
value: value instanceof Date ? value.toISOString() : value
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
if (input.kind === "isoTime") {
|
|
446
|
+
const value = findCoordinateForIso(dimension, input.iso);
|
|
447
|
+
return value === void 0 ? { kind: "isoTime", iso: input.iso } : { kind: "isoTime", iso: input.iso, value };
|
|
448
|
+
}
|
|
449
|
+
throw new GridError({
|
|
450
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
451
|
+
message: `Unsupported selector for dimension "${dimension.name}".`,
|
|
452
|
+
dimension: dimension.name,
|
|
453
|
+
sourceId
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
function toRendererSelectors(selectors) {
|
|
457
|
+
const output = {};
|
|
458
|
+
for (const [dimensionName, selector] of Object.entries(selectors)) {
|
|
459
|
+
if (selector.kind === "index") {
|
|
460
|
+
output[dimensionName] = { selected: selector.index, type: "index" };
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (selector.kind === "isoTime") {
|
|
464
|
+
output[dimensionName] = {
|
|
465
|
+
selected: selector.value ?? selector.iso,
|
|
466
|
+
type: "value"
|
|
467
|
+
};
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
output[dimensionName] = { selected: selector.value, type: "value" };
|
|
471
|
+
}
|
|
472
|
+
return output;
|
|
473
|
+
}
|
|
474
|
+
function selectorToIndex(dimension, selector) {
|
|
475
|
+
if (selector.kind === "index") {
|
|
476
|
+
return selector.index;
|
|
477
|
+
}
|
|
478
|
+
const selected = selector.kind === "isoTime" ? selector.value ?? selector.iso : selector.value;
|
|
479
|
+
const index = dimension.coordinates?.findIndex((coordinate) => sameCoordinate(coordinate, selected)) ?? -1;
|
|
480
|
+
if (index >= 0) {
|
|
481
|
+
return index;
|
|
482
|
+
}
|
|
483
|
+
throw new GridError({
|
|
484
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
485
|
+
message: `Selector value "${String(selected)}" does not match coordinates for dimension "${dimension.name}".`,
|
|
486
|
+
dimension: dimension.name
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
function listTimeCoordinates(source, dimensionName = "time") {
|
|
490
|
+
const dimension = findDimension(source, dimensionName);
|
|
491
|
+
if (!dimension) {
|
|
492
|
+
throw new GridError({
|
|
493
|
+
code: "MISSING_DIMENSION",
|
|
494
|
+
message: `Time dimension "${dimensionName}" is not present in the grid source.`,
|
|
495
|
+
dimension: dimensionName,
|
|
496
|
+
sourceId: source.id
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
if (!dimension.coordinates) {
|
|
500
|
+
throw new GridError({
|
|
501
|
+
code: "MISSING_COORDINATES",
|
|
502
|
+
message: `Time dimension "${dimensionName}" needs coordinate values for a slider.`,
|
|
503
|
+
dimension: dimensionName,
|
|
504
|
+
sourceId: source.id
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
const coordinates = dimension.coordinates.map((coordinate, index) => {
|
|
508
|
+
const decoded = decodeTimeCoordinate(coordinate, dimension.units);
|
|
509
|
+
const forecastLabel = formatForecastStepLabel(source, dimension, coordinate);
|
|
510
|
+
return {
|
|
511
|
+
index,
|
|
512
|
+
value: decoded.value,
|
|
513
|
+
label: forecastLabel ?? (decoded.iso ? formatHumanUtcDateTime(decoded.iso) : String(decoded.value)),
|
|
514
|
+
...decoded.iso ? { iso: decoded.iso } : {}
|
|
515
|
+
};
|
|
516
|
+
});
|
|
517
|
+
assertMonotonicTimeCoordinates(coordinates, dimension.name, source.id);
|
|
518
|
+
return coordinates;
|
|
519
|
+
}
|
|
520
|
+
function formatForecastStepLabel(source, dimension, coordinate) {
|
|
521
|
+
if (!isForecastStepDimension(dimension.name)) {
|
|
522
|
+
return void 0;
|
|
523
|
+
}
|
|
524
|
+
const reference = forecastReferenceDate(source);
|
|
525
|
+
const offsetMs = forecastStepOffsetMs(coordinate, dimension.units);
|
|
526
|
+
if (!reference || offsetMs === void 0) {
|
|
527
|
+
return void 0;
|
|
528
|
+
}
|
|
529
|
+
return formatHumanUtcDateTime(new Date(reference.getTime() + offsetMs));
|
|
530
|
+
}
|
|
531
|
+
function isForecastStepDimension(name) {
|
|
532
|
+
return normalizeDimensionName(name) === "step";
|
|
533
|
+
}
|
|
534
|
+
function forecastReferenceDate(source) {
|
|
535
|
+
const referenceDimension = source.dimensions.find(
|
|
536
|
+
(dimension) => ["forecastreferencetime", "forecastreferencedate"].includes(
|
|
537
|
+
normalizeDimensionName(dimension.name)
|
|
538
|
+
)
|
|
539
|
+
);
|
|
540
|
+
if (!referenceDimension) {
|
|
541
|
+
return void 0;
|
|
542
|
+
}
|
|
543
|
+
const referenceCoordinate = referenceDimension.coordinates?.[0];
|
|
544
|
+
if (referenceCoordinate === void 0) {
|
|
545
|
+
return void 0;
|
|
546
|
+
}
|
|
547
|
+
const decoded = decodeTimeCoordinate(referenceCoordinate, referenceDimension.units);
|
|
548
|
+
const referenceValue = decoded.iso ?? (typeof decoded.value === "string" ? decoded.value : void 0);
|
|
549
|
+
if (!referenceValue) {
|
|
550
|
+
return void 0;
|
|
551
|
+
}
|
|
552
|
+
const date = new Date(referenceValue);
|
|
553
|
+
return Number.isNaN(date.getTime()) ? void 0 : date;
|
|
554
|
+
}
|
|
555
|
+
function forecastStepOffsetMs(coordinate, units) {
|
|
556
|
+
const value = numericCoordinateValue(coordinate);
|
|
557
|
+
if (value === void 0) {
|
|
558
|
+
return void 0;
|
|
559
|
+
}
|
|
560
|
+
return value * forecastStepUnitMultiplier(units);
|
|
561
|
+
}
|
|
562
|
+
function numericCoordinateValue(coordinate) {
|
|
563
|
+
if (typeof coordinate === "number") {
|
|
564
|
+
return Number.isFinite(coordinate) ? coordinate : void 0;
|
|
565
|
+
}
|
|
566
|
+
if (typeof coordinate !== "string") {
|
|
567
|
+
return void 0;
|
|
568
|
+
}
|
|
569
|
+
const value = Number(coordinate);
|
|
570
|
+
return Number.isFinite(value) ? value : void 0;
|
|
571
|
+
}
|
|
572
|
+
function forecastStepUnitMultiplier(units) {
|
|
573
|
+
const normalizedUnits = units?.toLowerCase() ?? "";
|
|
574
|
+
if (normalizedUnits.includes("second")) {
|
|
575
|
+
return 1e3;
|
|
576
|
+
}
|
|
577
|
+
if (normalizedUnits.includes("minute")) {
|
|
578
|
+
return 6e4;
|
|
579
|
+
}
|
|
580
|
+
if (normalizedUnits.includes("day")) {
|
|
581
|
+
return 864e5;
|
|
582
|
+
}
|
|
583
|
+
return 36e5;
|
|
584
|
+
}
|
|
585
|
+
function normalizeDimensionName(name) {
|
|
586
|
+
return name.toLowerCase().replace(/[_\-\s]+/g, "");
|
|
587
|
+
}
|
|
588
|
+
var UTC_MONTH_LABELS = [
|
|
589
|
+
"Jan",
|
|
590
|
+
"Feb",
|
|
591
|
+
"Mar",
|
|
592
|
+
"Apr",
|
|
593
|
+
"May",
|
|
594
|
+
"Jun",
|
|
595
|
+
"Jul",
|
|
596
|
+
"Aug",
|
|
597
|
+
"Sep",
|
|
598
|
+
"Oct",
|
|
599
|
+
"Nov",
|
|
600
|
+
"Dec"
|
|
601
|
+
];
|
|
602
|
+
function formatHumanUtcDateTime(input) {
|
|
603
|
+
const date = input instanceof Date ? input : new Date(input);
|
|
604
|
+
return [
|
|
605
|
+
UTC_MONTH_LABELS[date.getUTCMonth()],
|
|
606
|
+
" ",
|
|
607
|
+
date.getUTCDate(),
|
|
608
|
+
", ",
|
|
609
|
+
date.getUTCFullYear(),
|
|
610
|
+
", ",
|
|
611
|
+
padDatePart(date.getUTCHours()),
|
|
612
|
+
":",
|
|
613
|
+
padDatePart(date.getUTCMinutes()),
|
|
614
|
+
" UTC"
|
|
615
|
+
].join("");
|
|
616
|
+
}
|
|
617
|
+
function padDatePart(value) {
|
|
618
|
+
return String(value).padStart(2, "0");
|
|
619
|
+
}
|
|
620
|
+
function createDebouncer(callback, waitMs) {
|
|
621
|
+
let timeout;
|
|
622
|
+
let lastArgs;
|
|
623
|
+
const flush = () => {
|
|
624
|
+
if (!lastArgs) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
const args = lastArgs;
|
|
628
|
+
lastArgs = void 0;
|
|
629
|
+
if (timeout) {
|
|
630
|
+
clearTimeout(timeout);
|
|
631
|
+
timeout = void 0;
|
|
632
|
+
}
|
|
633
|
+
callback(...args);
|
|
634
|
+
};
|
|
635
|
+
return {
|
|
636
|
+
call(...args) {
|
|
637
|
+
lastArgs = args;
|
|
638
|
+
if (timeout) {
|
|
639
|
+
clearTimeout(timeout);
|
|
640
|
+
}
|
|
641
|
+
timeout = setTimeout(flush, waitMs);
|
|
642
|
+
},
|
|
643
|
+
flush,
|
|
644
|
+
cancel() {
|
|
645
|
+
if (timeout) {
|
|
646
|
+
clearTimeout(timeout);
|
|
647
|
+
}
|
|
648
|
+
timeout = void 0;
|
|
649
|
+
lastArgs = void 0;
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
function defaultSelectorForDimension(dimension) {
|
|
654
|
+
const firstValue = dimension.coordinates?.[0];
|
|
655
|
+
if (firstValue !== void 0) {
|
|
656
|
+
return {
|
|
657
|
+
kind: "coordinate",
|
|
658
|
+
value: firstValue instanceof Date ? firstValue.toISOString() : firstValue
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
return { kind: "index", index: 0 };
|
|
662
|
+
}
|
|
663
|
+
function normalizeCoordinateSelector(dimension, value, sourceId) {
|
|
664
|
+
if (dimension.coordinates && !dimension.coordinates.some((coordinate) => sameCoordinate(coordinate, value))) {
|
|
665
|
+
throw new GridError({
|
|
666
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
667
|
+
message: `Selector value "${String(value)}" does not match coordinates for dimension "${dimension.name}".`,
|
|
668
|
+
dimension: dimension.name,
|
|
669
|
+
sourceId
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
return { kind: "coordinate", value };
|
|
673
|
+
}
|
|
674
|
+
function assertIndexInRange(dimension, index, sourceId) {
|
|
675
|
+
if (!Number.isInteger(index) || index < 0 || index >= dimension.size) {
|
|
676
|
+
throw new GridError({
|
|
677
|
+
code: "UNSUPPORTED_SELECTOR",
|
|
678
|
+
message: `Index selector ${index} is outside dimension "${dimension.name}" size ${dimension.size}.`,
|
|
679
|
+
dimension: dimension.name,
|
|
680
|
+
sourceId
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
function findCoordinateForIso(dimension, iso) {
|
|
685
|
+
return dimension.coordinates?.map((coordinate) => decodeTimeCoordinate(coordinate, dimension.units)).find((coordinate) => coordinate.iso === iso || coordinate.value === iso)?.value;
|
|
686
|
+
}
|
|
687
|
+
function decodeTimeCoordinate(coordinate, units) {
|
|
688
|
+
if (coordinate instanceof Date) {
|
|
689
|
+
return { value: coordinate.toISOString(), iso: coordinate.toISOString() };
|
|
690
|
+
}
|
|
691
|
+
if (typeof coordinate === "string") {
|
|
692
|
+
const date = new Date(coordinate);
|
|
693
|
+
return Number.isNaN(date.getTime()) ? { value: coordinate } : { value: coordinate, iso: date.toISOString() };
|
|
694
|
+
}
|
|
695
|
+
const cfTime = decodeCfTime(coordinate, units);
|
|
696
|
+
return cfTime ? { value: coordinate, iso: cfTime } : { value: coordinate };
|
|
697
|
+
}
|
|
698
|
+
function decodeCfTime(value, units) {
|
|
699
|
+
if (!units) {
|
|
700
|
+
return void 0;
|
|
701
|
+
}
|
|
702
|
+
const match = /^(seconds|minutes|hours|days) since ([0-9]{4}-[0-9]{2}-[0-9]{2})(?:[ T]([0-9:.Z+-]+))?/i.exec(
|
|
703
|
+
units
|
|
704
|
+
);
|
|
705
|
+
if (!match) {
|
|
706
|
+
return void 0;
|
|
707
|
+
}
|
|
708
|
+
const unit = match[1]?.toLowerCase();
|
|
709
|
+
const date = match[2];
|
|
710
|
+
const time = match[3] ?? "00:00:00Z";
|
|
711
|
+
const origin = /* @__PURE__ */ new Date(`${date}T${time.replace(/Z?$/, "Z")}`);
|
|
712
|
+
if (Number.isNaN(origin.getTime())) {
|
|
713
|
+
return void 0;
|
|
714
|
+
}
|
|
715
|
+
const multiplier = unit === "seconds" ? 1e3 : unit === "minutes" ? 6e4 : unit === "hours" ? 36e5 : 864e5;
|
|
716
|
+
return new Date(origin.getTime() + value * multiplier).toISOString();
|
|
717
|
+
}
|
|
718
|
+
function assertMonotonicTimeCoordinates(coordinates, dimensionName, sourceId) {
|
|
719
|
+
let direction;
|
|
720
|
+
for (let index = 1; index < coordinates.length; index += 1) {
|
|
721
|
+
const previous = comparableTimeValue(coordinates[index - 1]);
|
|
722
|
+
const current = comparableTimeValue(coordinates[index]);
|
|
723
|
+
if (previous === current) {
|
|
724
|
+
throw new GridError({
|
|
725
|
+
code: "UNSUPPORTED_DIMENSION",
|
|
726
|
+
message: `Time dimension "${dimensionName}" has duplicate coordinate values.`,
|
|
727
|
+
dimension: dimensionName,
|
|
728
|
+
sourceId
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
const pairDirection = current > previous ? "ascending" : "descending";
|
|
732
|
+
direction ??= pairDirection;
|
|
733
|
+
if (pairDirection !== direction) {
|
|
734
|
+
throw new GridError({
|
|
735
|
+
code: "UNSUPPORTED_DIMENSION",
|
|
736
|
+
message: `Time dimension "${dimensionName}" must be monotonic for slider controls.`,
|
|
737
|
+
dimension: dimensionName,
|
|
738
|
+
sourceId
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
function comparableTimeValue(coordinate) {
|
|
744
|
+
if (!coordinate) {
|
|
745
|
+
return 0;
|
|
746
|
+
}
|
|
747
|
+
if (coordinate.iso) {
|
|
748
|
+
return new Date(coordinate.iso).getTime();
|
|
749
|
+
}
|
|
750
|
+
if (typeof coordinate.value === "number") {
|
|
751
|
+
return coordinate.value;
|
|
752
|
+
}
|
|
753
|
+
const parsed = new Date(coordinate.value).getTime();
|
|
754
|
+
return Number.isNaN(parsed) ? coordinate.index : parsed;
|
|
755
|
+
}
|
|
756
|
+
function sameCoordinate(left, right) {
|
|
757
|
+
const normalizedLeft = left instanceof Date ? left.toISOString() : left;
|
|
758
|
+
return normalizedLeft === right || String(normalizedLeft) === String(right);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// src/core/jaxray.ts
|
|
762
|
+
function createJaxraySource(input, options = {}) {
|
|
763
|
+
const dataset = isDatasetLike(input) ? input : arrayToDataset(input);
|
|
764
|
+
const variables = inferVariables(dataset, options);
|
|
765
|
+
const dimensions = inferDimensions(dataset, variables, options);
|
|
766
|
+
const source = {
|
|
767
|
+
id: options.id ?? dataset.id,
|
|
768
|
+
label: options.label,
|
|
769
|
+
source: options.source,
|
|
770
|
+
store: options.store ?? dataset.store,
|
|
771
|
+
zarrVersion: options.zarrVersion,
|
|
772
|
+
crs: options.crs ?? stringAttr(dataset.attrs, "crs") ?? "EPSG:4326",
|
|
773
|
+
proj4: options.proj4 ?? stringAttr(dataset.attrs, "proj4"),
|
|
774
|
+
bounds: options.bounds ?? inferBounds(dimensions, dataset.attrs),
|
|
775
|
+
spatialDimensions: options.spatialDimensions,
|
|
776
|
+
dimensions,
|
|
777
|
+
variables,
|
|
778
|
+
metadata: dataset.attrs,
|
|
779
|
+
readSlice: async (request) => readJaxraySlice(dataset, source, request.variable, request.selectors)
|
|
780
|
+
};
|
|
781
|
+
const firstSpatialVariable = variables.find(
|
|
782
|
+
(variable) => inferSpatialDimensions(source, variable.name)
|
|
783
|
+
);
|
|
784
|
+
source.spatialDimensions = options.spatialDimensions ?? inferSpatialDimensions(source) ?? (firstSpatialVariable ? inferSpatialDimensions(source, firstSpatialVariable.name) : void 0);
|
|
785
|
+
assertValidGridDataSource(source, {
|
|
786
|
+
variable: firstSpatialVariable?.name ?? variables[0]?.name
|
|
787
|
+
});
|
|
788
|
+
return source;
|
|
789
|
+
}
|
|
790
|
+
function isDatasetLike(input) {
|
|
791
|
+
return "data_vars" in input || "dataVars" in input || "variables" in input && !("shape" in input) || "sizes" in input;
|
|
792
|
+
}
|
|
793
|
+
function arrayToDataset(array) {
|
|
794
|
+
const name = array.name ?? "variable";
|
|
795
|
+
return {
|
|
796
|
+
data_vars: { [name]: array },
|
|
797
|
+
coords: array.coords,
|
|
798
|
+
attrs: array.attrs
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
function inferVariables(dataset, options) {
|
|
802
|
+
return Array.from(variableEntries(dataset)).map(([name, array]) => {
|
|
803
|
+
const dimensions = array.dims ?? array.dimensions;
|
|
804
|
+
if (!dimensions) {
|
|
805
|
+
throw new GridError({
|
|
806
|
+
code: "MISSING_DIMENSION",
|
|
807
|
+
message: `Jaxray variable "${name}" does not expose dims/dimensions metadata.`,
|
|
808
|
+
variable: name
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
const shape = array.shape ?? dimensions.map((dimensionName) => dimensionSize(dataset, dimensionName));
|
|
812
|
+
if (!shape) {
|
|
813
|
+
throw new GridError({
|
|
814
|
+
code: "MISSING_DIMENSION",
|
|
815
|
+
message: `Jaxray variable "${name}" does not expose shape metadata.`,
|
|
816
|
+
variable: name
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
const override = options.variables?.[name] ?? {};
|
|
820
|
+
return {
|
|
821
|
+
name,
|
|
822
|
+
path: stringAttr(array.attrs, "path"),
|
|
823
|
+
dtype: override.dtype ?? array.dtype ?? stringAttr(array.attrs, "dtype") ?? "float32",
|
|
824
|
+
dimensions,
|
|
825
|
+
shape,
|
|
826
|
+
chunks: override.chunks ?? array.chunks,
|
|
827
|
+
units: override.units ?? stringAttr(array.attrs, "units"),
|
|
828
|
+
longName: override.longName ?? stringAttr(array.attrs, "long_name"),
|
|
829
|
+
standardName: override.standardName ?? stringAttr(array.attrs, "standard_name"),
|
|
830
|
+
fillValue: override.fillValue ?? numberAttr(array.attrs, "_FillValue") ?? numberAttr(array.attrs, "fill_value") ?? null,
|
|
831
|
+
scaleFactor: override.scaleFactor ?? numberAttr(array.attrs, "scale_factor"),
|
|
832
|
+
addOffset: override.addOffset ?? numberAttr(array.attrs, "add_offset"),
|
|
833
|
+
attrs: array.attrs
|
|
834
|
+
};
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
function inferDimensions(dataset, variables, options) {
|
|
838
|
+
const dimensions = /* @__PURE__ */ new Map();
|
|
839
|
+
const coords = dataset.coords;
|
|
840
|
+
for (const variable of variables) {
|
|
841
|
+
variable.dimensions.forEach((name, index) => {
|
|
842
|
+
const existing = dimensions.get(name);
|
|
843
|
+
const coordinates = normalizeCoordinateArray(coordinateValue(coords, name));
|
|
844
|
+
const override = options.dimensions?.[name] ?? {};
|
|
845
|
+
const inferredKind = inferDimensionKind(name);
|
|
846
|
+
const size = variable.shape[index] ?? coordinates?.length ?? 0;
|
|
847
|
+
dimensions.set(name, {
|
|
848
|
+
...existing,
|
|
849
|
+
name,
|
|
850
|
+
size: override.size ?? existing?.size ?? size,
|
|
851
|
+
kind: override.kind ?? existing?.kind ?? inferredKind,
|
|
852
|
+
coordinates: override.coordinates ?? existing?.coordinates ?? coordinates,
|
|
853
|
+
units: override.units ?? existing?.units,
|
|
854
|
+
calendar: override.calendar ?? existing?.calendar,
|
|
855
|
+
longName: override.longName ?? existing?.longName,
|
|
856
|
+
standardName: override.standardName ?? existing?.standardName,
|
|
857
|
+
ascending: override.ascending ?? existing?.ascending ?? inferAscending(coordinates),
|
|
858
|
+
attrs: override.attrs ?? existing?.attrs
|
|
859
|
+
});
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
return Array.from(dimensions.values());
|
|
863
|
+
}
|
|
864
|
+
function inferDimensionKind(name) {
|
|
865
|
+
const normalized = name.toLowerCase();
|
|
866
|
+
if (["lon", "lng", "longitude", "x"].includes(normalized)) {
|
|
867
|
+
return "x";
|
|
868
|
+
}
|
|
869
|
+
if (["lat", "latitude", "y"].includes(normalized)) {
|
|
870
|
+
return "y";
|
|
871
|
+
}
|
|
872
|
+
if (["time", "valid_time", "date"].includes(normalized)) {
|
|
873
|
+
return "time";
|
|
874
|
+
}
|
|
875
|
+
if (["band", "month"].includes(normalized)) {
|
|
876
|
+
return "band";
|
|
877
|
+
}
|
|
878
|
+
if (["level", "height", "pressure"].includes(normalized)) {
|
|
879
|
+
return "vertical";
|
|
880
|
+
}
|
|
881
|
+
return "other";
|
|
882
|
+
}
|
|
883
|
+
function inferBounds(dimensions, attrs) {
|
|
884
|
+
const attrBounds = attrs?.bounds;
|
|
885
|
+
if (Array.isArray(attrBounds) && attrBounds.length === 4 && attrBounds.every((value) => typeof value === "number")) {
|
|
886
|
+
return attrBounds;
|
|
887
|
+
}
|
|
888
|
+
const x = dimensions.find((dimension) => dimension.kind === "x");
|
|
889
|
+
const y = dimensions.find((dimension) => dimension.kind === "y");
|
|
890
|
+
const xCoordinates = x?.coordinates?.filter(
|
|
891
|
+
(value) => typeof value === "number"
|
|
892
|
+
);
|
|
893
|
+
const yCoordinates = y?.coordinates?.filter(
|
|
894
|
+
(value) => typeof value === "number"
|
|
895
|
+
);
|
|
896
|
+
if (!xCoordinates?.length || !yCoordinates?.length) {
|
|
897
|
+
return void 0;
|
|
898
|
+
}
|
|
899
|
+
return [
|
|
900
|
+
Math.min(...xCoordinates),
|
|
901
|
+
Math.min(...yCoordinates),
|
|
902
|
+
Math.max(...xCoordinates),
|
|
903
|
+
Math.max(...yCoordinates)
|
|
904
|
+
];
|
|
905
|
+
}
|
|
906
|
+
async function readJaxraySlice(dataset, source, variableName, selectors = {}) {
|
|
907
|
+
const entry = variableEntries(dataset).find(([name2]) => name2 === variableName);
|
|
908
|
+
if (!entry) {
|
|
909
|
+
throw new GridError({
|
|
910
|
+
code: "MISSING_VARIABLE",
|
|
911
|
+
message: `Jaxray source cannot read missing variable "${variableName}".`,
|
|
912
|
+
variable: variableName,
|
|
913
|
+
sourceId: source.id
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
const [name, array] = entry;
|
|
917
|
+
const variable = source.variables.find((candidate) => candidate.name === name);
|
|
918
|
+
if (!variable) {
|
|
919
|
+
throw new GridError({
|
|
920
|
+
code: "MISSING_VARIABLE",
|
|
921
|
+
message: `Jaxray source metadata is missing variable "${name}".`,
|
|
922
|
+
variable: name,
|
|
923
|
+
sourceId: source.id
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
const normalizedSelectors = isNormalizedSelectors(selectors) ? selectors : normalizeSelectors(source, variable.name, selectors);
|
|
927
|
+
const selection = variable.dimensions.reduce(
|
|
928
|
+
(accumulator, dimensionName) => {
|
|
929
|
+
const dimension = findDimension(source, dimensionName);
|
|
930
|
+
if (!dimension) {
|
|
931
|
+
return accumulator;
|
|
932
|
+
}
|
|
933
|
+
accumulator[dimensionName] = selectorToIndex(
|
|
934
|
+
dimension,
|
|
935
|
+
normalizedSelectors[dimensionName] ?? { kind: "index", index: 0 }
|
|
936
|
+
);
|
|
937
|
+
return accumulator;
|
|
938
|
+
},
|
|
939
|
+
{}
|
|
940
|
+
);
|
|
941
|
+
const value = await readArrayValue(array, variable, selection);
|
|
942
|
+
return {
|
|
943
|
+
variable: name,
|
|
944
|
+
selectors: normalizedSelectors,
|
|
945
|
+
dimensions: [],
|
|
946
|
+
shape: [],
|
|
947
|
+
data: [value],
|
|
948
|
+
unit: variable.units,
|
|
949
|
+
fillValue: variable.fillValue
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
async function readArrayValue(array, variable, selection) {
|
|
953
|
+
if (array.get) {
|
|
954
|
+
return array.get(selection);
|
|
955
|
+
}
|
|
956
|
+
if (array.isel) {
|
|
957
|
+
const selected = await array.isel(selection);
|
|
958
|
+
const computed = selected.isLazy && selected.compute ? await selected.compute() : selected;
|
|
959
|
+
return scalarFromArrayLike(computed.data ?? computed.values);
|
|
960
|
+
}
|
|
961
|
+
const data = array.data ?? array.values;
|
|
962
|
+
if (!data) {
|
|
963
|
+
throw new GridError({
|
|
964
|
+
code: "SOURCE_LOAD_FAILED",
|
|
965
|
+
message: `Jaxray variable "${variable.name}" does not expose readable data for query helpers.`,
|
|
966
|
+
variable: variable.name
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
let offset = 0;
|
|
970
|
+
let stride = 1;
|
|
971
|
+
for (let index = variable.dimensions.length - 1; index >= 0; index -= 1) {
|
|
972
|
+
const dimensionName = variable.dimensions[index];
|
|
973
|
+
if (!dimensionName) {
|
|
974
|
+
continue;
|
|
975
|
+
}
|
|
976
|
+
offset += (selection[dimensionName] ?? 0) * stride;
|
|
977
|
+
stride *= variable.shape[index] ?? 1;
|
|
978
|
+
}
|
|
979
|
+
return data[offset] ?? Number.NaN;
|
|
980
|
+
}
|
|
981
|
+
function scalarFromArrayLike(value) {
|
|
982
|
+
if (typeof value === "number") {
|
|
983
|
+
return value;
|
|
984
|
+
}
|
|
985
|
+
if (Array.isArray(value) || ArrayBuffer.isView(value)) {
|
|
986
|
+
const first = Array.from(value)[0];
|
|
987
|
+
return scalarFromArrayLike(first);
|
|
988
|
+
}
|
|
989
|
+
return Number.NaN;
|
|
990
|
+
}
|
|
991
|
+
function variableEntries(dataset) {
|
|
992
|
+
if (Array.isArray(dataset.dataVars) && typeof dataset.getVariable === "function") {
|
|
993
|
+
return dataset.dataVars.map((name) => [name, dataset.getVariable?.(name) ?? {}]);
|
|
994
|
+
}
|
|
995
|
+
if (Array.isArray(dataset.dataVars)) {
|
|
996
|
+
throw new GridError({
|
|
997
|
+
code: "MISSING_VARIABLE",
|
|
998
|
+
message: "Jaxray Dataset exposes dataVars names but no getVariable(name) method."
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
const variables = dataset.data_vars ?? dataset.dataVars ?? dataset.variables ?? {};
|
|
1002
|
+
if (variables instanceof Map) {
|
|
1003
|
+
return Array.from(variables.entries());
|
|
1004
|
+
}
|
|
1005
|
+
return Object.entries(variables);
|
|
1006
|
+
}
|
|
1007
|
+
function normalizeCoordinateArray(input) {
|
|
1008
|
+
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;
|
|
1009
|
+
return raw?.filter(
|
|
1010
|
+
(value) => typeof value === "number" || typeof value === "string"
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
function coordinateValue(coords, name) {
|
|
1014
|
+
if (!coords) {
|
|
1015
|
+
return void 0;
|
|
1016
|
+
}
|
|
1017
|
+
return coords instanceof Map ? coords.get(name) : coords[name];
|
|
1018
|
+
}
|
|
1019
|
+
function dimensionSize(dataset, dimensionName) {
|
|
1020
|
+
const sizes = dataset.sizes;
|
|
1021
|
+
if (!sizes) {
|
|
1022
|
+
return 0;
|
|
1023
|
+
}
|
|
1024
|
+
return sizes instanceof Map ? sizes.get(dimensionName) ?? 0 : sizes[dimensionName] ?? 0;
|
|
1025
|
+
}
|
|
1026
|
+
function isArrayLike(value) {
|
|
1027
|
+
return Array.isArray(value) || ArrayBuffer.isView(value) || typeof value === "object" && value !== null && "length" in value && typeof value.length === "number";
|
|
1028
|
+
}
|
|
1029
|
+
function inferAscending(coordinates) {
|
|
1030
|
+
if (!coordinates || coordinates.length < 2) {
|
|
1031
|
+
return void 0;
|
|
1032
|
+
}
|
|
1033
|
+
const first = coordinates[0];
|
|
1034
|
+
const second = coordinates[1];
|
|
1035
|
+
return typeof first === "number" && typeof second === "number" ? second > first : void 0;
|
|
1036
|
+
}
|
|
1037
|
+
function stringAttr(attrs, name) {
|
|
1038
|
+
const value = attrs?.[name];
|
|
1039
|
+
return typeof value === "string" ? value : void 0;
|
|
1040
|
+
}
|
|
1041
|
+
function numberAttr(attrs, name) {
|
|
1042
|
+
const value = attrs?.[name];
|
|
1043
|
+
return typeof value === "number" ? value : void 0;
|
|
1044
|
+
}
|
|
1045
|
+
function isNormalizedSelectors(selectors) {
|
|
1046
|
+
return Object.values(selectors).every(
|
|
1047
|
+
(value) => typeof value === "object" && value !== null && "kind" in value
|
|
1048
|
+
);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
// src/core/preflight.ts
|
|
1052
|
+
var dtypeByteSizes = {
|
|
1053
|
+
int8: 1,
|
|
1054
|
+
uint8: 1,
|
|
1055
|
+
i1: 1,
|
|
1056
|
+
u1: 1,
|
|
1057
|
+
int16: 2,
|
|
1058
|
+
uint16: 2,
|
|
1059
|
+
i2: 2,
|
|
1060
|
+
u2: 2,
|
|
1061
|
+
int32: 4,
|
|
1062
|
+
uint32: 4,
|
|
1063
|
+
float32: 4,
|
|
1064
|
+
i4: 4,
|
|
1065
|
+
u4: 4,
|
|
1066
|
+
f4: 4,
|
|
1067
|
+
float64: 8,
|
|
1068
|
+
f8: 8
|
|
1069
|
+
};
|
|
1070
|
+
function preflightGridRequest(options) {
|
|
1071
|
+
const errors = [];
|
|
1072
|
+
const warnings = [];
|
|
1073
|
+
const dimensions = {};
|
|
1074
|
+
const variable = findVariable(options.source, options.variable);
|
|
1075
|
+
if (!variable) {
|
|
1076
|
+
errors.push(
|
|
1077
|
+
new GridError({
|
|
1078
|
+
code: "MISSING_VARIABLE",
|
|
1079
|
+
message: `Variable "${options.variable}" is not present in the grid source.`,
|
|
1080
|
+
sourceId: options.source.id,
|
|
1081
|
+
variable: options.variable
|
|
1082
|
+
})
|
|
1083
|
+
);
|
|
1084
|
+
return { ok: false, dimensions, warnings, errors };
|
|
1085
|
+
}
|
|
1086
|
+
const spatialDimensions = inferSpatialDimensions(options.source, variable.name);
|
|
1087
|
+
for (const dimensionName of variable.dimensions) {
|
|
1088
|
+
const dimension = findDimension(options.source, dimensionName);
|
|
1089
|
+
if (!dimension) {
|
|
1090
|
+
errors.push(
|
|
1091
|
+
new GridError({
|
|
1092
|
+
code: "MISSING_DIMENSION",
|
|
1093
|
+
message: `Variable "${variable.name}" references missing dimension "${dimensionName}".`,
|
|
1094
|
+
dimension: dimensionName,
|
|
1095
|
+
sourceId: options.source.id,
|
|
1096
|
+
variable: variable.name
|
|
1097
|
+
})
|
|
1098
|
+
);
|
|
1099
|
+
continue;
|
|
1100
|
+
}
|
|
1101
|
+
dimensions[dimension.name] = selectedDimensionSize({
|
|
1102
|
+
bounds: options.bounds,
|
|
1103
|
+
dimension,
|
|
1104
|
+
isSpatialX: spatialDimensions?.x === dimension.name,
|
|
1105
|
+
isSpatialY: spatialDimensions?.y === dimension.name,
|
|
1106
|
+
selectorIsPresent: options.selectors?.[dimension.name] !== void 0,
|
|
1107
|
+
sourceBounds: options.source.bounds,
|
|
1108
|
+
timeRange: options.timeRange,
|
|
1109
|
+
warnings
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
const cells = Object.values(dimensions).reduce((total, size) => total * size, 1);
|
|
1113
|
+
const byteSize = dtypeByteSize(variable.dtype);
|
|
1114
|
+
const bytes = byteSize === void 0 ? void 0 : cells * byteSize;
|
|
1115
|
+
if (byteSize === void 0) {
|
|
1116
|
+
warnings.push(`Variable "${variable.name}" has unknown dtype byte size "${variable.dtype}".`);
|
|
1117
|
+
}
|
|
1118
|
+
if (options.limits?.maxCells !== void 0 && cells > options.limits.maxCells) {
|
|
1119
|
+
errors.push(
|
|
1120
|
+
new GridError({
|
|
1121
|
+
code: "QUERY_TOO_EXPENSIVE",
|
|
1122
|
+
message: `Grid request would select approximately ${cells} cells, above the limit of ${options.limits.maxCells}.`,
|
|
1123
|
+
context: { cells, maxCells: options.limits.maxCells },
|
|
1124
|
+
sourceId: options.source.id,
|
|
1125
|
+
variable: variable.name
|
|
1126
|
+
})
|
|
1127
|
+
);
|
|
1128
|
+
}
|
|
1129
|
+
if (bytes !== void 0 && options.limits?.maxBytes !== void 0 && bytes > options.limits.maxBytes) {
|
|
1130
|
+
errors.push(
|
|
1131
|
+
new GridError({
|
|
1132
|
+
code: "QUERY_TOO_EXPENSIVE",
|
|
1133
|
+
message: `Grid request would read approximately ${bytes} uncompressed bytes, above the limit of ${options.limits.maxBytes}.`,
|
|
1134
|
+
context: { bytes, maxBytes: options.limits.maxBytes },
|
|
1135
|
+
sourceId: options.source.id,
|
|
1136
|
+
variable: variable.name
|
|
1137
|
+
})
|
|
1138
|
+
);
|
|
1139
|
+
}
|
|
1140
|
+
return {
|
|
1141
|
+
ok: errors.length === 0,
|
|
1142
|
+
cells,
|
|
1143
|
+
bytes,
|
|
1144
|
+
dimensions,
|
|
1145
|
+
warnings,
|
|
1146
|
+
errors
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
function selectedDimensionSize(options) {
|
|
1150
|
+
if (options.selectorIsPresent) {
|
|
1151
|
+
return 1;
|
|
1152
|
+
}
|
|
1153
|
+
if (options.bounds && options.isSpatialX) {
|
|
1154
|
+
return spatialCount(options.dimension, options.bounds[0], options.bounds[2], {
|
|
1155
|
+
axis: "x",
|
|
1156
|
+
sourceBounds: options.sourceBounds,
|
|
1157
|
+
warnings: options.warnings
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1160
|
+
if (options.bounds && options.isSpatialY) {
|
|
1161
|
+
return spatialCount(options.dimension, options.bounds[1], options.bounds[3], {
|
|
1162
|
+
axis: "y",
|
|
1163
|
+
sourceBounds: options.sourceBounds,
|
|
1164
|
+
warnings: options.warnings
|
|
1165
|
+
});
|
|
1166
|
+
}
|
|
1167
|
+
if (options.timeRange && options.dimension.kind === "time") {
|
|
1168
|
+
return timeRangeCount(options.dimension, options.timeRange, options.warnings);
|
|
1169
|
+
}
|
|
1170
|
+
return options.dimension.size;
|
|
1171
|
+
}
|
|
1172
|
+
function spatialCount(dimension, lowerBound, upperBound, options) {
|
|
1173
|
+
const low = Math.min(lowerBound, upperBound);
|
|
1174
|
+
const high = Math.max(lowerBound, upperBound);
|
|
1175
|
+
const numericCoordinates2 = dimension.coordinates?.filter(
|
|
1176
|
+
(coordinate) => typeof coordinate === "number"
|
|
1177
|
+
);
|
|
1178
|
+
if (numericCoordinates2?.length) {
|
|
1179
|
+
return numericCoordinates2.filter((coordinate) => coordinate >= low && coordinate <= high).length;
|
|
1180
|
+
}
|
|
1181
|
+
const sourceRange = spatialRange(options.sourceBounds, options.axis);
|
|
1182
|
+
if (!sourceRange) {
|
|
1183
|
+
options.warnings.push(
|
|
1184
|
+
`Dimension "${dimension.name}" has no coordinates or source bounds, so preflight used the full dimension size.`
|
|
1185
|
+
);
|
|
1186
|
+
return dimension.size;
|
|
1187
|
+
}
|
|
1188
|
+
const [sourceLow, sourceHigh] = sourceRange;
|
|
1189
|
+
const span = sourceHigh - sourceLow;
|
|
1190
|
+
if (span <= 0 || dimension.size <= 0) {
|
|
1191
|
+
return 0;
|
|
1192
|
+
}
|
|
1193
|
+
const overlap = Math.max(0, Math.min(high, sourceHigh) - Math.max(low, sourceLow));
|
|
1194
|
+
if (overlap === 0) {
|
|
1195
|
+
return 0;
|
|
1196
|
+
}
|
|
1197
|
+
return Math.max(1, Math.min(dimension.size, Math.ceil(overlap / span * dimension.size)));
|
|
1198
|
+
}
|
|
1199
|
+
function timeRangeCount(dimension, timeRange, warnings) {
|
|
1200
|
+
if (!dimension.coordinates?.length) {
|
|
1201
|
+
warnings.push(
|
|
1202
|
+
`Time dimension "${dimension.name}" has no coordinates, so preflight used the full time size.`
|
|
1203
|
+
);
|
|
1204
|
+
return dimension.size;
|
|
1205
|
+
}
|
|
1206
|
+
const start = timeToMillis(timeRange.start, dimension.units);
|
|
1207
|
+
const end = timeToMillis(timeRange.end, dimension.units);
|
|
1208
|
+
if (start === void 0 || end === void 0) {
|
|
1209
|
+
warnings.push(
|
|
1210
|
+
`Time range for "${dimension.name}" could not be parsed, so preflight used the full time size.`
|
|
1211
|
+
);
|
|
1212
|
+
return dimension.size;
|
|
1213
|
+
}
|
|
1214
|
+
const low = Math.min(start, end);
|
|
1215
|
+
const high = Math.max(start, end);
|
|
1216
|
+
let count = 0;
|
|
1217
|
+
for (const coordinate of dimension.coordinates) {
|
|
1218
|
+
const value = timeToMillis(coordinate, dimension.units);
|
|
1219
|
+
if (value !== void 0 && value >= low && value <= high) {
|
|
1220
|
+
count += 1;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
return count;
|
|
1224
|
+
}
|
|
1225
|
+
function timeToMillis(value, units) {
|
|
1226
|
+
if (value instanceof Date) {
|
|
1227
|
+
return value.getTime();
|
|
1228
|
+
}
|
|
1229
|
+
if (typeof value === "string") {
|
|
1230
|
+
const parsed = Date.parse(value);
|
|
1231
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
1232
|
+
}
|
|
1233
|
+
if (typeof value === "number") {
|
|
1234
|
+
return numericTimeToMillis(value, units);
|
|
1235
|
+
}
|
|
1236
|
+
return void 0;
|
|
1237
|
+
}
|
|
1238
|
+
function numericTimeToMillis(value, units) {
|
|
1239
|
+
if (!units) {
|
|
1240
|
+
return void 0;
|
|
1241
|
+
}
|
|
1242
|
+
const match = /^(seconds?|minutes?|hours?|days?) since (.+)$/i.exec(units.trim());
|
|
1243
|
+
if (!match) {
|
|
1244
|
+
return void 0;
|
|
1245
|
+
}
|
|
1246
|
+
const [, unit, epoch] = match;
|
|
1247
|
+
if (!unit || !epoch) {
|
|
1248
|
+
return void 0;
|
|
1249
|
+
}
|
|
1250
|
+
const epochMillis = Date.parse(epoch);
|
|
1251
|
+
if (Number.isNaN(epochMillis)) {
|
|
1252
|
+
return void 0;
|
|
1253
|
+
}
|
|
1254
|
+
const multiplier = unit.toLowerCase().startsWith("second") ? 1e3 : unit.toLowerCase().startsWith("minute") ? 60 * 1e3 : unit.toLowerCase().startsWith("hour") ? 60 * 60 * 1e3 : 24 * 60 * 60 * 1e3;
|
|
1255
|
+
return epochMillis + value * multiplier;
|
|
1256
|
+
}
|
|
1257
|
+
function spatialRange(bounds, axis) {
|
|
1258
|
+
if (!bounds) {
|
|
1259
|
+
return void 0;
|
|
1260
|
+
}
|
|
1261
|
+
return axis === "x" ? [bounds[0], bounds[2]] : [bounds[1], bounds[3]];
|
|
1262
|
+
}
|
|
1263
|
+
function dtypeByteSize(dtype) {
|
|
1264
|
+
const normalized = dtype.toLowerCase().replace(/^[<>|]/, "");
|
|
1265
|
+
return dtypeByteSizes[normalized];
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
// src/core/query.ts
|
|
1269
|
+
async function queryGrid(request) {
|
|
1270
|
+
if (request.signal?.aborted) {
|
|
1271
|
+
throw abortedError();
|
|
1272
|
+
}
|
|
1273
|
+
if (request.source.queryGeometry) {
|
|
1274
|
+
return request.source.queryGeometry({
|
|
1275
|
+
variable: request.variable,
|
|
1276
|
+
geometry: request.geometry,
|
|
1277
|
+
selectors: request.selectors,
|
|
1278
|
+
maxCells: request.maxCells,
|
|
1279
|
+
signal: request.signal
|
|
1280
|
+
});
|
|
1281
|
+
}
|
|
1282
|
+
switch (request.geometry.type) {
|
|
1283
|
+
case "Point":
|
|
1284
|
+
return queryPoint(
|
|
1285
|
+
request.source,
|
|
1286
|
+
request.variable,
|
|
1287
|
+
request.geometry.coordinates,
|
|
1288
|
+
request.selectors,
|
|
1289
|
+
request.signal
|
|
1290
|
+
);
|
|
1291
|
+
case "BoundingBox":
|
|
1292
|
+
return queryBoundingBox({
|
|
1293
|
+
...request,
|
|
1294
|
+
geometry: request.geometry
|
|
1295
|
+
});
|
|
1296
|
+
case "Polygon":
|
|
1297
|
+
return queryPolygon({
|
|
1298
|
+
...request,
|
|
1299
|
+
geometry: request.geometry
|
|
1300
|
+
});
|
|
1301
|
+
default:
|
|
1302
|
+
throw new GridError({
|
|
1303
|
+
code: "UNSUPPORTED_GEOMETRY",
|
|
1304
|
+
message: "Unsupported grid query geometry."
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
async function queryPoint(source, variableName, coordinates, selectors = {}, signal) {
|
|
1309
|
+
const context = queryContext(source, variableName, selectors);
|
|
1310
|
+
const xIndex = nearestCoordinateIndex(context.xDimension, coordinates[0], source.bounds, "x");
|
|
1311
|
+
const yIndex = nearestCoordinateIndex(context.yDimension, coordinates[1], source.bounds, "y");
|
|
1312
|
+
const sample = await readSample(source, context, xIndex, yIndex, signal);
|
|
1313
|
+
return {
|
|
1314
|
+
variable: variableName,
|
|
1315
|
+
selectors: context.normalizedSelectors,
|
|
1316
|
+
geometry: { type: "Point", coordinates },
|
|
1317
|
+
unit: context.variable.units,
|
|
1318
|
+
samples: [sample],
|
|
1319
|
+
warnings: []
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
async function queryBoundingBox(request) {
|
|
1323
|
+
const context = queryContext(request.source, request.variable, request.selectors);
|
|
1324
|
+
const [west, south, east, north] = request.geometry.bounds;
|
|
1325
|
+
const xIndexes = coordinateIndexesInBounds(context.xDimension, west, east);
|
|
1326
|
+
const yIndexes = coordinateIndexesInBounds(context.yDimension, south, north);
|
|
1327
|
+
const estimatedCells = xIndexes.length * yIndexes.length;
|
|
1328
|
+
const maxCells = request.maxCells ?? 1e4;
|
|
1329
|
+
if (estimatedCells > maxCells) {
|
|
1330
|
+
throw new GridError({
|
|
1331
|
+
code: "QUERY_TOO_EXPENSIVE",
|
|
1332
|
+
message: `Bounding-box query would inspect ${estimatedCells} cells, above the limit of ${maxCells}.`,
|
|
1333
|
+
context: { estimatedCells, maxCells }
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
const samples = [];
|
|
1337
|
+
for (const yIndex of yIndexes) {
|
|
1338
|
+
for (const xIndex of xIndexes) {
|
|
1339
|
+
if (request.signal?.aborted) {
|
|
1340
|
+
throw abortedError();
|
|
1341
|
+
}
|
|
1342
|
+
samples.push(await readSample(request.source, context, xIndex, yIndex, request.signal));
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
return {
|
|
1346
|
+
variable: request.variable,
|
|
1347
|
+
selectors: context.normalizedSelectors,
|
|
1348
|
+
geometry: request.geometry,
|
|
1349
|
+
unit: context.variable.units,
|
|
1350
|
+
samples,
|
|
1351
|
+
warnings: estimatedCells > 1e3 ? ["Large bounded query; consider lowering maxCells or querying a coarser grid."] : []
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
async function queryPolygon(request) {
|
|
1355
|
+
const bounds = polygonBounds(request.geometry.coordinates);
|
|
1356
|
+
const bounded = await queryBoundingBox({
|
|
1357
|
+
...request,
|
|
1358
|
+
geometry: { type: "BoundingBox", bounds }
|
|
1359
|
+
});
|
|
1360
|
+
return {
|
|
1361
|
+
...bounded,
|
|
1362
|
+
geometry: request.geometry,
|
|
1363
|
+
samples: bounded.samples.filter(
|
|
1364
|
+
(sample) => pointInPolygon(sample.coordinates, request.geometry.coordinates)
|
|
1365
|
+
),
|
|
1366
|
+
warnings: [
|
|
1367
|
+
...bounded.warnings,
|
|
1368
|
+
"Polygon queries are evaluated by scanning the polygon bounding box."
|
|
1369
|
+
]
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
function queryContext(source, variableName, selectors = {}) {
|
|
1373
|
+
const variable = findVariable(source, variableName);
|
|
1374
|
+
if (!variable) {
|
|
1375
|
+
throw new GridError({
|
|
1376
|
+
code: "MISSING_VARIABLE",
|
|
1377
|
+
message: `Variable "${variableName}" is not present in the grid source.`,
|
|
1378
|
+
variable: variableName,
|
|
1379
|
+
sourceId: source.id
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
const spatialDimensions = inferSpatialDimensions(source, variable.name);
|
|
1383
|
+
if (!spatialDimensions) {
|
|
1384
|
+
throw new GridError({
|
|
1385
|
+
code: "MISSING_SPATIAL_DIMENSIONS",
|
|
1386
|
+
message: `Variable "${variable.name}" needs spatial dimensions for map queries.`,
|
|
1387
|
+
variable: variable.name,
|
|
1388
|
+
sourceId: source.id
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
const xDimension = findDimension(source, spatialDimensions.x);
|
|
1392
|
+
const yDimension = findDimension(source, spatialDimensions.y);
|
|
1393
|
+
if (!xDimension || !yDimension) {
|
|
1394
|
+
throw new GridError({
|
|
1395
|
+
code: "MISSING_SPATIAL_DIMENSIONS",
|
|
1396
|
+
message: "Spatial dimensions are missing from the grid source.",
|
|
1397
|
+
variable: variable.name,
|
|
1398
|
+
sourceId: source.id
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
if (!source.readSlice) {
|
|
1402
|
+
throw new GridError({
|
|
1403
|
+
code: "SOURCE_LOAD_FAILED",
|
|
1404
|
+
message: "GridDataSource must implement readSlice or queryGeometry before query helpers can read values.",
|
|
1405
|
+
sourceId: source.id,
|
|
1406
|
+
variable: variable.name
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
1409
|
+
return {
|
|
1410
|
+
variable,
|
|
1411
|
+
normalizedSelectors: normalizeSelectors(source, variable.name, selectors),
|
|
1412
|
+
xDimension,
|
|
1413
|
+
yDimension,
|
|
1414
|
+
spatialDimensions
|
|
1415
|
+
};
|
|
1416
|
+
}
|
|
1417
|
+
async function readSample(source, context, xIndex, yIndex, signal) {
|
|
1418
|
+
if (signal?.aborted) {
|
|
1419
|
+
throw abortedError();
|
|
1420
|
+
}
|
|
1421
|
+
const selectors = selectorsWithSpatialIndexes(
|
|
1422
|
+
context.normalizedSelectors,
|
|
1423
|
+
context.spatialDimensions.x,
|
|
1424
|
+
xIndex,
|
|
1425
|
+
context.spatialDimensions.y,
|
|
1426
|
+
yIndex
|
|
1427
|
+
);
|
|
1428
|
+
const slice = await source.readSlice?.({
|
|
1429
|
+
variable: context.variable.name,
|
|
1430
|
+
selectors,
|
|
1431
|
+
signal
|
|
1432
|
+
});
|
|
1433
|
+
const rawValue = slice?.data[0];
|
|
1434
|
+
const value = rawValue === void 0 || rawValue === context.variable.fillValue ? null : rawValue * (context.variable.scaleFactor ?? 1) + (context.variable.addOffset ?? 0);
|
|
1435
|
+
return {
|
|
1436
|
+
coordinates: [
|
|
1437
|
+
coordinateAt(context.xDimension, xIndex, source.bounds, "x"),
|
|
1438
|
+
coordinateAt(context.yDimension, yIndex, source.bounds, "y")
|
|
1439
|
+
],
|
|
1440
|
+
indexes: {
|
|
1441
|
+
[context.spatialDimensions.x]: xIndex,
|
|
1442
|
+
[context.spatialDimensions.y]: yIndex
|
|
1443
|
+
},
|
|
1444
|
+
rawValue: rawValue ?? null,
|
|
1445
|
+
value
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
function selectorsWithSpatialIndexes(selectors, xDimension, xIndex, yDimension, yIndex) {
|
|
1449
|
+
return {
|
|
1450
|
+
...selectors,
|
|
1451
|
+
[xDimension]: { kind: "index", index: xIndex },
|
|
1452
|
+
[yDimension]: { kind: "index", index: yIndex }
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
function nearestCoordinateIndex(dimension, value, bounds, axis) {
|
|
1456
|
+
if (!dimension.coordinates) {
|
|
1457
|
+
return boundedCoordinateIndex(dimension, value, bounds, axis);
|
|
1458
|
+
}
|
|
1459
|
+
let bestIndex = 0;
|
|
1460
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
1461
|
+
dimension.coordinates.forEach((coordinate, index) => {
|
|
1462
|
+
if (typeof coordinate !== "number") {
|
|
1463
|
+
return;
|
|
1464
|
+
}
|
|
1465
|
+
const distance = Math.abs(coordinate - value);
|
|
1466
|
+
if (distance < bestDistance) {
|
|
1467
|
+
bestDistance = distance;
|
|
1468
|
+
bestIndex = index;
|
|
1469
|
+
}
|
|
1470
|
+
});
|
|
1471
|
+
return bestIndex;
|
|
1472
|
+
}
|
|
1473
|
+
function coordinateIndexesInBounds(dimension, min, max) {
|
|
1474
|
+
if (!dimension.coordinates) {
|
|
1475
|
+
return range(
|
|
1476
|
+
clamp(Math.floor(min), 0, dimension.size - 1),
|
|
1477
|
+
clamp(Math.ceil(max), 0, dimension.size - 1)
|
|
1478
|
+
);
|
|
1479
|
+
}
|
|
1480
|
+
const low = Math.min(min, max);
|
|
1481
|
+
const high = Math.max(min, max);
|
|
1482
|
+
return dimension.coordinates.reduce((indexes, coordinate, index) => {
|
|
1483
|
+
if (typeof coordinate === "number" && coordinate >= low && coordinate <= high) {
|
|
1484
|
+
indexes.push(index);
|
|
1485
|
+
}
|
|
1486
|
+
return indexes;
|
|
1487
|
+
}, []);
|
|
1488
|
+
}
|
|
1489
|
+
function coordinateAt(dimension, index, bounds, axis) {
|
|
1490
|
+
const coordinate = dimension.coordinates?.[index];
|
|
1491
|
+
if (typeof coordinate === "number") {
|
|
1492
|
+
return coordinate;
|
|
1493
|
+
}
|
|
1494
|
+
return boundedCoordinateAt(dimension, index, bounds, axis);
|
|
1495
|
+
}
|
|
1496
|
+
function boundedCoordinateIndex(dimension, value, bounds, axis) {
|
|
1497
|
+
const range2 = spatialRange2(bounds, axis);
|
|
1498
|
+
if (!range2) {
|
|
1499
|
+
return clamp(Math.round(value), 0, dimension.size - 1);
|
|
1500
|
+
}
|
|
1501
|
+
const [min, max] = range2;
|
|
1502
|
+
const span = max - min;
|
|
1503
|
+
if (span <= 0 || dimension.size <= 1) {
|
|
1504
|
+
return 0;
|
|
1505
|
+
}
|
|
1506
|
+
const ascending = dimension.ascending !== false;
|
|
1507
|
+
const ratio = ascending ? (value - min) / span : (max - value) / span;
|
|
1508
|
+
return clamp(Math.round(ratio * (dimension.size - 1)), 0, dimension.size - 1);
|
|
1509
|
+
}
|
|
1510
|
+
function boundedCoordinateAt(dimension, index, bounds, axis) {
|
|
1511
|
+
const range2 = spatialRange2(bounds, axis);
|
|
1512
|
+
if (!range2 || dimension.size <= 1) {
|
|
1513
|
+
return index;
|
|
1514
|
+
}
|
|
1515
|
+
const [min, max] = range2;
|
|
1516
|
+
const ratio = clamp(index, 0, dimension.size - 1) / (dimension.size - 1);
|
|
1517
|
+
return dimension.ascending === false ? max - ratio * (max - min) : min + ratio * (max - min);
|
|
1518
|
+
}
|
|
1519
|
+
function spatialRange2(bounds, axis) {
|
|
1520
|
+
if (!bounds) {
|
|
1521
|
+
return void 0;
|
|
1522
|
+
}
|
|
1523
|
+
return axis === "x" ? [bounds[0], bounds[2]] : [bounds[1], bounds[3]];
|
|
1524
|
+
}
|
|
1525
|
+
function range(start, end) {
|
|
1526
|
+
const values = [];
|
|
1527
|
+
for (let index = start; index <= end; index += 1) {
|
|
1528
|
+
values.push(index);
|
|
1529
|
+
}
|
|
1530
|
+
return values;
|
|
1531
|
+
}
|
|
1532
|
+
function clamp(value, min, max) {
|
|
1533
|
+
return Math.min(max, Math.max(min, value));
|
|
1534
|
+
}
|
|
1535
|
+
function polygonBounds(coordinates) {
|
|
1536
|
+
return coordinates.reduce(
|
|
1537
|
+
(bounds, [x, y]) => [
|
|
1538
|
+
Math.min(bounds[0], x),
|
|
1539
|
+
Math.min(bounds[1], y),
|
|
1540
|
+
Math.max(bounds[2], x),
|
|
1541
|
+
Math.max(bounds[3], y)
|
|
1542
|
+
],
|
|
1543
|
+
[
|
|
1544
|
+
Number.POSITIVE_INFINITY,
|
|
1545
|
+
Number.POSITIVE_INFINITY,
|
|
1546
|
+
Number.NEGATIVE_INFINITY,
|
|
1547
|
+
Number.NEGATIVE_INFINITY
|
|
1548
|
+
]
|
|
1549
|
+
);
|
|
1550
|
+
}
|
|
1551
|
+
function pointInPolygon(point, polygon) {
|
|
1552
|
+
let inside = false;
|
|
1553
|
+
for (let index = 0, previous = polygon.length - 1; index < polygon.length; previous = index, index += 1) {
|
|
1554
|
+
const currentPoint = polygon[index];
|
|
1555
|
+
const previousPoint = polygon[previous];
|
|
1556
|
+
if (!currentPoint || !previousPoint) {
|
|
1557
|
+
continue;
|
|
1558
|
+
}
|
|
1559
|
+
const intersects = currentPoint[1] > point[1] !== previousPoint[1] > point[1] && point[0] < (previousPoint[0] - currentPoint[0]) * (point[1] - currentPoint[1]) / (previousPoint[1] - currentPoint[1]) + currentPoint[0];
|
|
1560
|
+
if (intersects) {
|
|
1561
|
+
inside = !inside;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
return inside;
|
|
1565
|
+
}
|
|
1566
|
+
function abortedError() {
|
|
1567
|
+
return new GridError({
|
|
1568
|
+
code: "ABORTED",
|
|
1569
|
+
message: "Grid query was aborted."
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
// src/dclimate/index.ts
|
|
1574
|
+
var DEFAULT_TIME_KEYS = [
|
|
1575
|
+
"time",
|
|
1576
|
+
"valid_time",
|
|
1577
|
+
"datetime",
|
|
1578
|
+
"date",
|
|
1579
|
+
"forecast_reference_time",
|
|
1580
|
+
"forecast_time",
|
|
1581
|
+
"analysis_time",
|
|
1582
|
+
"initial_time",
|
|
1583
|
+
"verification_time",
|
|
1584
|
+
"step",
|
|
1585
|
+
"t"
|
|
1586
|
+
];
|
|
1587
|
+
var DClimateAdapterError = class extends GridError {
|
|
1588
|
+
constructor(code, message, cause, context) {
|
|
1589
|
+
super({
|
|
1590
|
+
code: code === "unsupported" ? "UNSUPPORTED_DIMENSION" : "SOURCE_LOAD_FAILED",
|
|
1591
|
+
message,
|
|
1592
|
+
cause,
|
|
1593
|
+
context: {
|
|
1594
|
+
adapter: "dclimate",
|
|
1595
|
+
adapterCode: code,
|
|
1596
|
+
...context
|
|
1597
|
+
}
|
|
1598
|
+
});
|
|
1599
|
+
this.name = "DClimateAdapterError";
|
|
1600
|
+
}
|
|
1601
|
+
};
|
|
1602
|
+
function reportProgress(options, progress) {
|
|
1603
|
+
options.onProgress?.({
|
|
1604
|
+
...progress,
|
|
1605
|
+
percent: Math.max(0, Math.min(100, progress.percent))
|
|
1606
|
+
});
|
|
1607
|
+
}
|
|
1608
|
+
function runDClimatePreflight(source, options) {
|
|
1609
|
+
const config = options.preflight;
|
|
1610
|
+
if (!config) {
|
|
1611
|
+
return;
|
|
1612
|
+
}
|
|
1613
|
+
const selectionBounds = options.selection?.bounds ? normalizeSelectionBounds(options.selection.bounds, options.selection.boundsOptions).bounds : void 0;
|
|
1614
|
+
const result = preflightGridRequest({
|
|
1615
|
+
bounds: config.bounds ?? selectionBounds,
|
|
1616
|
+
limits: config.limits,
|
|
1617
|
+
selectors: config.selectors,
|
|
1618
|
+
source,
|
|
1619
|
+
timeRange: config.timeRange ?? options.selection?.timeRange,
|
|
1620
|
+
variable: config.variable ?? source.variables[0]?.name ?? ""
|
|
1621
|
+
});
|
|
1622
|
+
config.onResult?.(result);
|
|
1623
|
+
if (!result.ok) {
|
|
1624
|
+
throw result.errors[0];
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
async function createDClimateSource(request, options = {}) {
|
|
1628
|
+
try {
|
|
1629
|
+
reportProgress(options, {
|
|
1630
|
+
stage: "request",
|
|
1631
|
+
message: "Preparing dClimate request",
|
|
1632
|
+
percent: 2
|
|
1633
|
+
});
|
|
1634
|
+
const loaded = await loadDClimateDataset(request, options);
|
|
1635
|
+
reportProgress(options, {
|
|
1636
|
+
stage: "dataset",
|
|
1637
|
+
message: "Dataset metadata loaded",
|
|
1638
|
+
percent: 45
|
|
1639
|
+
});
|
|
1640
|
+
const metadata = extractMetadata(loaded);
|
|
1641
|
+
const cid = extractString(loaded, ["cid", "rootCid", "hash"]) ?? metadata?.cid ?? request.cid;
|
|
1642
|
+
const dataset = extractDataset(loaded);
|
|
1643
|
+
const selectedForRendering = Boolean(options.selection?.bounds);
|
|
1644
|
+
if (!dataset) {
|
|
1645
|
+
const existingStore = options.store ?? extractStore(loaded);
|
|
1646
|
+
const hasExternalSource = Boolean(
|
|
1647
|
+
existingStore || options.source || request.cid || options.openIpfsStore || options.gatewayUrl
|
|
1648
|
+
);
|
|
1649
|
+
throw new DClimateAdapterError(
|
|
1650
|
+
"metadata",
|
|
1651
|
+
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.",
|
|
1652
|
+
void 0,
|
|
1653
|
+
{ request, cid, hasStore: Boolean(existingStore), source: options.source }
|
|
1654
|
+
);
|
|
1655
|
+
}
|
|
1656
|
+
reportProgress(options, {
|
|
1657
|
+
stage: "source",
|
|
1658
|
+
message: "Normalizing raster metadata",
|
|
1659
|
+
percent: 55
|
|
1660
|
+
});
|
|
1661
|
+
const source = createJaxraySource(dataset, {
|
|
1662
|
+
...options,
|
|
1663
|
+
id: options.id ?? sourceIdForRequest(request),
|
|
1664
|
+
source: options.source ?? (selectedForRendering ? void 0 : gatewaySourceForCid(cid, options.gatewayUrl)),
|
|
1665
|
+
store: options.store ?? (selectedForRendering ? void 0 : extractStore(loaded)),
|
|
1666
|
+
label: options.label ?? labelForRequest(request)
|
|
1667
|
+
});
|
|
1668
|
+
runDClimatePreflight(source, options);
|
|
1669
|
+
reportProgress(options, {
|
|
1670
|
+
stage: "store",
|
|
1671
|
+
message: selectedForRendering ? "Preparing bounded raster chunks" : "Preparing raster source",
|
|
1672
|
+
percent: selectedForRendering ? 62 : 72
|
|
1673
|
+
});
|
|
1674
|
+
const selectedStore = selectedForRendering ? await createInMemoryZarrStore(dataset) : void 0;
|
|
1675
|
+
const selectedStoreByteLength = selectedStore?.byteLength;
|
|
1676
|
+
reportProgress(options, {
|
|
1677
|
+
stage: "store",
|
|
1678
|
+
message: selectedForRendering ? "Prepared bounded raster chunks" : "Prepared raster source",
|
|
1679
|
+
percent: selectedForRendering ? 78 : 80,
|
|
1680
|
+
byteLength: selectedStoreByteLength
|
|
1681
|
+
});
|
|
1682
|
+
reportProgress(options, {
|
|
1683
|
+
stage: "source",
|
|
1684
|
+
message: "Normalizing raster metadata",
|
|
1685
|
+
percent: 84,
|
|
1686
|
+
byteLength: selectedStoreByteLength
|
|
1687
|
+
});
|
|
1688
|
+
const store = options.store ?? selectedStore ?? (selectedForRendering ? void 0 : source.store ?? extractStore(loaded)) ?? (!selectedForRendering && cid && (options.gatewayUrl || options.openIpfsStore) ? await openStore(cid, options) : void 0);
|
|
1689
|
+
reportProgress(options, {
|
|
1690
|
+
stage: "ready",
|
|
1691
|
+
message: "dClimate source ready",
|
|
1692
|
+
percent: 100,
|
|
1693
|
+
byteLength: selectedStoreByteLength
|
|
1694
|
+
});
|
|
1695
|
+
return {
|
|
1696
|
+
...source,
|
|
1697
|
+
store,
|
|
1698
|
+
metadata: {
|
|
1699
|
+
...source.metadata,
|
|
1700
|
+
dclimate: {
|
|
1701
|
+
request,
|
|
1702
|
+
cid,
|
|
1703
|
+
clientMetadata: metadata,
|
|
1704
|
+
gatewayUrl: options.gatewayUrl,
|
|
1705
|
+
selectedStoreByteLength
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
};
|
|
1709
|
+
} catch (error) {
|
|
1710
|
+
if (error instanceof DClimateAdapterError || error instanceof GridError) {
|
|
1711
|
+
throw error;
|
|
1712
|
+
}
|
|
1713
|
+
throw new DClimateAdapterError(
|
|
1714
|
+
"catalog",
|
|
1715
|
+
"Failed to resolve or load the dClimate dataset.",
|
|
1716
|
+
error,
|
|
1717
|
+
{
|
|
1718
|
+
request
|
|
1719
|
+
}
|
|
1720
|
+
);
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
async function loadDClimateDataset(request, options) {
|
|
1724
|
+
if (options.client) {
|
|
1725
|
+
reportProgress(options, {
|
|
1726
|
+
stage: "client",
|
|
1727
|
+
message: "Using injected dClimate client",
|
|
1728
|
+
percent: 8
|
|
1729
|
+
});
|
|
1730
|
+
return callClient(options.client, request, options);
|
|
1731
|
+
}
|
|
1732
|
+
if (request.cid && !request.collection && !request.dataset) {
|
|
1733
|
+
reportProgress(options, {
|
|
1734
|
+
stage: "dataset",
|
|
1735
|
+
message: "Using CID dataset reference",
|
|
1736
|
+
percent: 35
|
|
1737
|
+
});
|
|
1738
|
+
return { cid: request.cid };
|
|
1739
|
+
}
|
|
1740
|
+
reportProgress(options, {
|
|
1741
|
+
stage: "client",
|
|
1742
|
+
message: "Loading dClimate client",
|
|
1743
|
+
percent: 8
|
|
1744
|
+
});
|
|
1745
|
+
const module = await import('@dclimate/dclimate-client-js');
|
|
1746
|
+
const Client = module.DClimateClient ?? module.default;
|
|
1747
|
+
if (!Client) {
|
|
1748
|
+
throw new DClimateAdapterError(
|
|
1749
|
+
"catalog",
|
|
1750
|
+
"@dclimate/dclimate-client-js did not export DClimateClient."
|
|
1751
|
+
);
|
|
1752
|
+
}
|
|
1753
|
+
reportProgress(options, {
|
|
1754
|
+
stage: "dataset",
|
|
1755
|
+
message: "Resolving dClimate dataset",
|
|
1756
|
+
percent: 18
|
|
1757
|
+
});
|
|
1758
|
+
return callClient(new Client(options.clientOptions), request, options);
|
|
1759
|
+
}
|
|
1760
|
+
async function callClient(client, request, options) {
|
|
1761
|
+
if (options.selection?.bounds && client.loadDataset) {
|
|
1762
|
+
return loadBoundedDataset(client, request, options);
|
|
1763
|
+
}
|
|
1764
|
+
if (options.selection) {
|
|
1765
|
+
if (client.selectDataset) {
|
|
1766
|
+
return client.selectDataset({
|
|
1767
|
+
request,
|
|
1768
|
+
selection: options.selection,
|
|
1769
|
+
options: {
|
|
1770
|
+
...options.loadDatasetOptions,
|
|
1771
|
+
gatewayUrl: options.gatewayUrl,
|
|
1772
|
+
returnJaxrayDataset: false
|
|
1773
|
+
}
|
|
1774
|
+
});
|
|
1775
|
+
}
|
|
1776
|
+
if (options.selection.bounds) {
|
|
1777
|
+
return loadBoundedDataset(client, request, options);
|
|
1778
|
+
}
|
|
1779
|
+
throw new DClimateAdapterError(
|
|
1780
|
+
"unsupported",
|
|
1781
|
+
"Injected dClimate client must expose selectDataset when source selection is requested."
|
|
1782
|
+
);
|
|
1783
|
+
}
|
|
1784
|
+
if (client.loadDataset) {
|
|
1785
|
+
return client.loadDataset({
|
|
1786
|
+
request,
|
|
1787
|
+
options: {
|
|
1788
|
+
...options.loadDatasetOptions,
|
|
1789
|
+
gatewayUrl: options.gatewayUrl,
|
|
1790
|
+
returnJaxrayDataset: true
|
|
1791
|
+
}
|
|
1792
|
+
});
|
|
1793
|
+
}
|
|
1794
|
+
if (client.getDataset) {
|
|
1795
|
+
return client.getDataset(request);
|
|
1796
|
+
}
|
|
1797
|
+
if (client.resolveDataset) {
|
|
1798
|
+
return client.resolveDataset(request);
|
|
1799
|
+
}
|
|
1800
|
+
throw new DClimateAdapterError(
|
|
1801
|
+
"catalog",
|
|
1802
|
+
"Injected dClimate client must expose loadDataset, selectDataset, getDataset, or resolveDataset."
|
|
1803
|
+
);
|
|
1804
|
+
}
|
|
1805
|
+
async function loadBoundedDataset(client, request, options) {
|
|
1806
|
+
if (!client.loadDataset) {
|
|
1807
|
+
throw new DClimateAdapterError(
|
|
1808
|
+
"unsupported",
|
|
1809
|
+
"Injected dClimate client must expose loadDataset when bounds selection is requested."
|
|
1810
|
+
);
|
|
1811
|
+
}
|
|
1812
|
+
const loaded = await client.loadDataset({
|
|
1813
|
+
request,
|
|
1814
|
+
options: {
|
|
1815
|
+
...options.loadDatasetOptions,
|
|
1816
|
+
gatewayUrl: options.gatewayUrl,
|
|
1817
|
+
returnJaxrayDataset: false
|
|
1818
|
+
}
|
|
1819
|
+
});
|
|
1820
|
+
reportProgress(options, {
|
|
1821
|
+
stage: "selection",
|
|
1822
|
+
message: "Selecting requested time and bounds",
|
|
1823
|
+
percent: 35
|
|
1824
|
+
});
|
|
1825
|
+
const selected = await applyBoundedSelection(loaded, options.selection);
|
|
1826
|
+
reportProgress(options, {
|
|
1827
|
+
stage: "selection",
|
|
1828
|
+
message: "Selected bounded raster window",
|
|
1829
|
+
percent: 52
|
|
1830
|
+
});
|
|
1831
|
+
return replaceLoadedDataset(loaded, selected);
|
|
1832
|
+
}
|
|
1833
|
+
async function applyBoundedSelection(loaded, selection) {
|
|
1834
|
+
const dataset = extractGeoTemporalDataset(loaded);
|
|
1835
|
+
if (!dataset) {
|
|
1836
|
+
throw new DClimateAdapterError(
|
|
1837
|
+
"unsupported",
|
|
1838
|
+
"dClimate bounds selection requires a GeoTemporalDataset response."
|
|
1839
|
+
);
|
|
1840
|
+
}
|
|
1841
|
+
const timeSelected = selection?.timeRange ? await applyTimeRangeSelection(dataset, selection.timeRange) : dataset;
|
|
1842
|
+
const coordinateSelected = await applyCoordinateSelection(timeSelected, selection?.coordinates);
|
|
1843
|
+
if (!selection?.bounds) {
|
|
1844
|
+
return coordinateSelected;
|
|
1845
|
+
}
|
|
1846
|
+
const { bounds, boundsOptions } = normalizeSelectionBounds(
|
|
1847
|
+
selection.bounds,
|
|
1848
|
+
selection.boundsOptions
|
|
1849
|
+
);
|
|
1850
|
+
const [west, south, east, north] = bounds;
|
|
1851
|
+
const gridSelected = await selectGriddedBounds(coordinateSelected, bounds, boundsOptions);
|
|
1852
|
+
if (gridSelected) {
|
|
1853
|
+
return gridSelected;
|
|
1854
|
+
}
|
|
1855
|
+
if (!hasRectangleSelection(coordinateSelected)) {
|
|
1856
|
+
throw new DClimateAdapterError(
|
|
1857
|
+
"unsupported",
|
|
1858
|
+
"dClimate bounds selection requires rectangle or gridded axis selection support."
|
|
1859
|
+
);
|
|
1860
|
+
}
|
|
1861
|
+
return coordinateSelected.rectangle(south, west, north, east, boundsOptions);
|
|
1862
|
+
}
|
|
1863
|
+
async function applyTimeRangeSelection(value, timeRange) {
|
|
1864
|
+
const gridSelected = await selectGriddedTimeRange(value, timeRange);
|
|
1865
|
+
if (gridSelected) {
|
|
1866
|
+
return gridSelected;
|
|
1867
|
+
}
|
|
1868
|
+
if (!hasTimeRange(value)) {
|
|
1869
|
+
return value;
|
|
1870
|
+
}
|
|
1871
|
+
return value.timeRange(timeRange);
|
|
1872
|
+
}
|
|
1873
|
+
async function selectGriddedTimeRange(value, timeRange) {
|
|
1874
|
+
const grid = extractSelectableGrid(value);
|
|
1875
|
+
if (!grid?.coords || typeof grid.sel !== "function") {
|
|
1876
|
+
return void 0;
|
|
1877
|
+
}
|
|
1878
|
+
const timeKey = inferCoordinateKey(grid.coords, DEFAULT_TIME_KEYS);
|
|
1879
|
+
if (!timeKey) {
|
|
1880
|
+
return void 0;
|
|
1881
|
+
}
|
|
1882
|
+
const coordinates = coordinateValues(grid.coords[timeKey]);
|
|
1883
|
+
if (!coordinates?.length) {
|
|
1884
|
+
return void 0;
|
|
1885
|
+
}
|
|
1886
|
+
const range2 = timeCoordinateRange(coordinates, timeRange, timeCoordinateUnits(grid, timeKey));
|
|
1887
|
+
if (!range2) {
|
|
1888
|
+
return void 0;
|
|
1889
|
+
}
|
|
1890
|
+
return grid.sel({
|
|
1891
|
+
[timeKey]: range2
|
|
1892
|
+
});
|
|
1893
|
+
}
|
|
1894
|
+
async function applyCoordinateSelection(value, coordinates) {
|
|
1895
|
+
if (!coordinates || Object.keys(coordinates).length === 0) {
|
|
1896
|
+
return value;
|
|
1897
|
+
}
|
|
1898
|
+
const grid = extractSelectableGrid(value);
|
|
1899
|
+
if (!grid || typeof grid.sel !== "function") {
|
|
1900
|
+
throw new DClimateAdapterError(
|
|
1901
|
+
"unsupported",
|
|
1902
|
+
"dClimate coordinate selection requires gridded axis selection support."
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
return grid.sel(normalizeCoordinateSelection(coordinates));
|
|
1906
|
+
}
|
|
1907
|
+
function normalizeCoordinateSelection(coordinates) {
|
|
1908
|
+
return Object.fromEntries(
|
|
1909
|
+
Object.entries(coordinates).map(([dimension, value]) => [
|
|
1910
|
+
dimension,
|
|
1911
|
+
normalizeCoordinateSelectionValue(value)
|
|
1912
|
+
])
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
function normalizeCoordinateSelectionValue(value) {
|
|
1916
|
+
if (value instanceof Date) {
|
|
1917
|
+
return value.toISOString();
|
|
1918
|
+
}
|
|
1919
|
+
if (typeof value !== "object") {
|
|
1920
|
+
return value;
|
|
1921
|
+
}
|
|
1922
|
+
return {
|
|
1923
|
+
start: value.start instanceof Date ? value.start.toISOString() : value.start,
|
|
1924
|
+
stop: value.stop instanceof Date ? value.stop.toISOString() : value.stop
|
|
1925
|
+
};
|
|
1926
|
+
}
|
|
1927
|
+
async function selectGriddedBounds(value, bounds, boundsOptions) {
|
|
1928
|
+
const grid = extractSelectableGrid(value);
|
|
1929
|
+
if (!grid?.coords || typeof grid.sel !== "function") {
|
|
1930
|
+
return void 0;
|
|
1931
|
+
}
|
|
1932
|
+
const latitudeKey = boundsOptions?.latitudeKey ?? inferCoordinateKey(grid.coords, ["latitude", "lat", "y"]);
|
|
1933
|
+
const longitudeKey = boundsOptions?.longitudeKey ?? inferCoordinateKey(grid.coords, ["longitude", "lon", "lng", "x"]);
|
|
1934
|
+
if (!latitudeKey || !longitudeKey) {
|
|
1935
|
+
return void 0;
|
|
1936
|
+
}
|
|
1937
|
+
const latitudeCoords = numericCoordinates(grid.coords[latitudeKey]);
|
|
1938
|
+
const longitudeCoords = numericCoordinates(grid.coords[longitudeKey]);
|
|
1939
|
+
if (!latitudeCoords || !longitudeCoords || !hasGriddedSpatialAxes(grid, latitudeKey, longitudeKey)) {
|
|
1940
|
+
return void 0;
|
|
1941
|
+
}
|
|
1942
|
+
const [west, south, east, north] = bounds;
|
|
1943
|
+
return grid.sel({
|
|
1944
|
+
[latitudeKey]: coordinateRange(latitudeCoords, south, north),
|
|
1945
|
+
[longitudeKey]: coordinateRange(longitudeCoords, west, east)
|
|
1946
|
+
});
|
|
1947
|
+
}
|
|
1948
|
+
function extractSelectableGrid(value) {
|
|
1949
|
+
if (!isRecord(value)) {
|
|
1950
|
+
return void 0;
|
|
1951
|
+
}
|
|
1952
|
+
const data = value.data;
|
|
1953
|
+
if (isSelectableGrid(data)) {
|
|
1954
|
+
return data;
|
|
1955
|
+
}
|
|
1956
|
+
return isSelectableGrid(value) ? value : void 0;
|
|
1957
|
+
}
|
|
1958
|
+
function isSelectableGrid(value) {
|
|
1959
|
+
return isRecord(value) && isRecord(value.coords) && typeof value.sel === "function";
|
|
1960
|
+
}
|
|
1961
|
+
function timeCoordinateRange(coordinates, timeRange, units) {
|
|
1962
|
+
const start = timeRangeBoundaryMillis(timeRange.start);
|
|
1963
|
+
const end = timeRangeBoundaryMillis(timeRange.end);
|
|
1964
|
+
if (start === void 0 || end === void 0) {
|
|
1965
|
+
return void 0;
|
|
1966
|
+
}
|
|
1967
|
+
const lowerBound = Math.min(start, end);
|
|
1968
|
+
const upperBound = Math.max(start, end);
|
|
1969
|
+
const matchingCoordinates = coordinates.map((value) => ({ value, millis: timeCoordinateMillis(value, units) })).filter(
|
|
1970
|
+
(coordinate) => coordinate.millis !== void 0 && coordinate.millis >= lowerBound && coordinate.millis <= upperBound
|
|
1971
|
+
);
|
|
1972
|
+
if (matchingCoordinates.length === 0) {
|
|
1973
|
+
throw new DClimateAdapterError(
|
|
1974
|
+
"unsupported",
|
|
1975
|
+
"dClimate time range selection did not include any available time coordinates.",
|
|
1976
|
+
void 0,
|
|
1977
|
+
{ timeRange }
|
|
1978
|
+
);
|
|
1979
|
+
}
|
|
1980
|
+
return {
|
|
1981
|
+
start: matchingCoordinates[0]?.value,
|
|
1982
|
+
stop: matchingCoordinates.at(-1)?.value
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
function timeCoordinateUnits(grid, timeKey) {
|
|
1986
|
+
const attrs = grid.coordAttrs?.[timeKey];
|
|
1987
|
+
if (isRecord(attrs) && typeof attrs.units === "string") {
|
|
1988
|
+
return attrs.units;
|
|
1989
|
+
}
|
|
1990
|
+
return void 0;
|
|
1991
|
+
}
|
|
1992
|
+
function timeRangeBoundaryMillis(value) {
|
|
1993
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
1994
|
+
return Number.isNaN(date.getTime()) ? void 0 : date.getTime();
|
|
1995
|
+
}
|
|
1996
|
+
function timeCoordinateMillis(value, units) {
|
|
1997
|
+
if (value instanceof Date) {
|
|
1998
|
+
return value.getTime();
|
|
1999
|
+
}
|
|
2000
|
+
if (typeof value === "string") {
|
|
2001
|
+
const date = new Date(value);
|
|
2002
|
+
return Number.isNaN(date.getTime()) ? void 0 : date.getTime();
|
|
2003
|
+
}
|
|
2004
|
+
if (typeof value === "number" && Number.isFinite(value) && units) {
|
|
2005
|
+
return cfTimeCoordinateMillis(value, units);
|
|
2006
|
+
}
|
|
2007
|
+
return void 0;
|
|
2008
|
+
}
|
|
2009
|
+
function cfTimeCoordinateMillis(value, units) {
|
|
2010
|
+
const match = /^(seconds?|minutes?|hours?|days?) since ([0-9]{4}-[0-9]{2}-[0-9]{2})(?:[ T]([0-9:.Z+-]+))?/i.exec(
|
|
2011
|
+
units
|
|
2012
|
+
);
|
|
2013
|
+
if (!match) {
|
|
2014
|
+
return void 0;
|
|
2015
|
+
}
|
|
2016
|
+
const unit = match[1]?.toLowerCase();
|
|
2017
|
+
const date = match[2];
|
|
2018
|
+
const time = match[3] ?? "00:00:00Z";
|
|
2019
|
+
const origin = /* @__PURE__ */ new Date(`${date}T${time.replace(/Z?$/, "Z")}`);
|
|
2020
|
+
if (Number.isNaN(origin.getTime())) {
|
|
2021
|
+
return void 0;
|
|
2022
|
+
}
|
|
2023
|
+
return origin.getTime() + value * cfTimeUnitMultiplier(unit);
|
|
2024
|
+
}
|
|
2025
|
+
function cfTimeUnitMultiplier(unit) {
|
|
2026
|
+
if (unit?.startsWith("second")) {
|
|
2027
|
+
return 1e3;
|
|
2028
|
+
}
|
|
2029
|
+
if (unit?.startsWith("minute")) {
|
|
2030
|
+
return 6e4;
|
|
2031
|
+
}
|
|
2032
|
+
if (unit?.startsWith("hour")) {
|
|
2033
|
+
return 36e5;
|
|
2034
|
+
}
|
|
2035
|
+
return 864e5;
|
|
2036
|
+
}
|
|
2037
|
+
function inferCoordinateKey(coords, candidates) {
|
|
2038
|
+
const keys = Object.keys(coords);
|
|
2039
|
+
const normalizedKeys = keys.map((key) => key.toLowerCase().replace(/[_\-\s]+/g, ""));
|
|
2040
|
+
for (const candidate of candidates) {
|
|
2041
|
+
const index = normalizedKeys.indexOf(candidate.toLowerCase().replace(/[_\-\s]+/g, ""));
|
|
2042
|
+
if (index !== -1) {
|
|
2043
|
+
return keys[index];
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
return void 0;
|
|
2047
|
+
}
|
|
2048
|
+
function numericCoordinates(values) {
|
|
2049
|
+
const raw = coordinateValues(values);
|
|
2050
|
+
if (!raw?.length) {
|
|
2051
|
+
return void 0;
|
|
2052
|
+
}
|
|
2053
|
+
const coordinates = raw.map((value) => typeof value === "number" ? value : Number(value));
|
|
2054
|
+
return coordinates.every((value) => Number.isFinite(value)) ? coordinates : void 0;
|
|
2055
|
+
}
|
|
2056
|
+
function hasGriddedSpatialAxes(grid, latitudeKey, longitudeKey) {
|
|
2057
|
+
if (!Array.isArray(grid.dataVars) || typeof grid.getVariable !== "function") {
|
|
2058
|
+
return false;
|
|
2059
|
+
}
|
|
2060
|
+
return grid.dataVars.some((name) => {
|
|
2061
|
+
if (typeof name !== "string") {
|
|
2062
|
+
return false;
|
|
2063
|
+
}
|
|
2064
|
+
const variable = grid.getVariable?.(name);
|
|
2065
|
+
return isRecord(variable) && Array.isArray(variable.dims) ? variable.dims.includes(latitudeKey) && variable.dims.includes(longitudeKey) : false;
|
|
2066
|
+
});
|
|
2067
|
+
}
|
|
2068
|
+
function coordinateRange(coordinates, lowerBound, upperBound) {
|
|
2069
|
+
const first = coordinates[0];
|
|
2070
|
+
const last = coordinates.at(-1);
|
|
2071
|
+
const ascending = first === void 0 || last === void 0 || first <= last;
|
|
2072
|
+
const start = ascending ? firstCoordinateAtOrAbove(coordinates, lowerBound) : firstCoordinateAtOrBelow(coordinates, upperBound);
|
|
2073
|
+
const stop = ascending ? lastCoordinateAtOrBelow(coordinates, upperBound) : lastCoordinateAtOrAbove(coordinates, lowerBound);
|
|
2074
|
+
return { start, stop };
|
|
2075
|
+
}
|
|
2076
|
+
function firstCoordinateAtOrAbove(coordinates, bound) {
|
|
2077
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate >= bound, "first") ?? nearestCoordinate(coordinates, bound);
|
|
2078
|
+
}
|
|
2079
|
+
function firstCoordinateAtOrBelow(coordinates, bound) {
|
|
2080
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate <= bound, "first") ?? nearestCoordinate(coordinates, bound);
|
|
2081
|
+
}
|
|
2082
|
+
function lastCoordinateAtOrBelow(coordinates, bound) {
|
|
2083
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate <= bound, "last") ?? nearestCoordinate(coordinates, bound);
|
|
2084
|
+
}
|
|
2085
|
+
function lastCoordinateAtOrAbove(coordinates, bound) {
|
|
2086
|
+
return binarySearchCoordinate(coordinates, (coordinate) => coordinate >= bound, "last") ?? nearestCoordinate(coordinates, bound);
|
|
2087
|
+
}
|
|
2088
|
+
function binarySearchCoordinate(coordinates, matches, position) {
|
|
2089
|
+
let low = 0;
|
|
2090
|
+
let high = coordinates.length - 1;
|
|
2091
|
+
let found;
|
|
2092
|
+
while (low <= high) {
|
|
2093
|
+
const middle = Math.floor((low + high) / 2);
|
|
2094
|
+
const coordinate = coordinates[middle];
|
|
2095
|
+
if (coordinate === void 0) {
|
|
2096
|
+
break;
|
|
2097
|
+
}
|
|
2098
|
+
if (matches(coordinate)) {
|
|
2099
|
+
found = coordinate;
|
|
2100
|
+
if (position === "first") {
|
|
2101
|
+
high = middle - 1;
|
|
2102
|
+
} else {
|
|
2103
|
+
low = middle + 1;
|
|
2104
|
+
}
|
|
2105
|
+
continue;
|
|
2106
|
+
}
|
|
2107
|
+
if (position === "first") {
|
|
2108
|
+
low = middle + 1;
|
|
2109
|
+
} else {
|
|
2110
|
+
high = middle - 1;
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
return found;
|
|
2114
|
+
}
|
|
2115
|
+
function nearestCoordinate(coordinates, value) {
|
|
2116
|
+
return coordinates.reduce(
|
|
2117
|
+
(nearest, coordinate) => Math.abs(coordinate - value) < Math.abs(nearest - value) ? coordinate : nearest
|
|
2118
|
+
);
|
|
2119
|
+
}
|
|
2120
|
+
function replaceLoadedDataset(loaded, dataset) {
|
|
2121
|
+
if (Array.isArray(loaded)) {
|
|
2122
|
+
return [dataset, loaded[1]];
|
|
2123
|
+
}
|
|
2124
|
+
return dataset;
|
|
2125
|
+
}
|
|
2126
|
+
async function createInMemoryZarrStore(dataset) {
|
|
2127
|
+
if (!dataset?.coords || !Array.isArray(dataset.dataVars) || typeof dataset.getVariable !== "function") {
|
|
2128
|
+
return void 0;
|
|
2129
|
+
}
|
|
2130
|
+
const entries = /* @__PURE__ */ new Map();
|
|
2131
|
+
const encoder = new TextEncoder();
|
|
2132
|
+
const addJson = (path, value) => {
|
|
2133
|
+
entries.set(path, encoder.encode(JSON.stringify(value)));
|
|
2134
|
+
};
|
|
2135
|
+
const addBytes = (path, value) => {
|
|
2136
|
+
entries.set(path, value);
|
|
2137
|
+
};
|
|
2138
|
+
addJson(".zgroup", { zarr_format: 2 });
|
|
2139
|
+
addJson(".zattrs", {});
|
|
2140
|
+
for (const [dimensionName, coordinateValues2] of coordinateEntries(dataset.coords)) {
|
|
2141
|
+
const coordinates = numericCoordinates(coordinateValues2);
|
|
2142
|
+
const strings = coordinates ? void 0 : stringCoordinates(coordinateValues2);
|
|
2143
|
+
if (!coordinates && !strings) {
|
|
2144
|
+
continue;
|
|
2145
|
+
}
|
|
2146
|
+
if (strings) {
|
|
2147
|
+
addJson(`${dimensionName}/.zarray`, {
|
|
2148
|
+
zarr_format: 2,
|
|
2149
|
+
shape: [strings.length],
|
|
2150
|
+
chunks: [strings.length],
|
|
2151
|
+
dtype: "|O",
|
|
2152
|
+
compressor: null,
|
|
2153
|
+
fill_value: null,
|
|
2154
|
+
order: "C",
|
|
2155
|
+
filters: [{ id: "vlen-utf8" }]
|
|
2156
|
+
});
|
|
2157
|
+
addJson(`${dimensionName}/.zattrs`, {
|
|
2158
|
+
_ARRAY_DIMENSIONS: [dimensionName]
|
|
2159
|
+
});
|
|
2160
|
+
addBytes(`${dimensionName}/0`, encodeVLenUtf8(strings));
|
|
2161
|
+
continue;
|
|
2162
|
+
}
|
|
2163
|
+
if (!coordinates) {
|
|
2164
|
+
continue;
|
|
2165
|
+
}
|
|
2166
|
+
addJson(`${dimensionName}/.zarray`, {
|
|
2167
|
+
zarr_format: 2,
|
|
2168
|
+
shape: [coordinates.length],
|
|
2169
|
+
chunks: [coordinates.length],
|
|
2170
|
+
dtype: "<f8",
|
|
2171
|
+
compressor: null,
|
|
2172
|
+
fill_value: null,
|
|
2173
|
+
order: "C",
|
|
2174
|
+
filters: null
|
|
2175
|
+
});
|
|
2176
|
+
addJson(`${dimensionName}/.zattrs`, {
|
|
2177
|
+
_ARRAY_DIMENSIONS: [dimensionName]
|
|
2178
|
+
});
|
|
2179
|
+
addBytes(`${dimensionName}/0`, encodeFloat64(coordinates));
|
|
2180
|
+
}
|
|
2181
|
+
for (const variableName of dataset.dataVars) {
|
|
2182
|
+
if (typeof variableName !== "string") {
|
|
2183
|
+
continue;
|
|
2184
|
+
}
|
|
2185
|
+
const variable = dataset.getVariable(variableName);
|
|
2186
|
+
if (!variable?.dims?.length || !variable.shape?.length) {
|
|
2187
|
+
continue;
|
|
2188
|
+
}
|
|
2189
|
+
const computed = await variable.compute?.();
|
|
2190
|
+
const values = flattenNumericValues(
|
|
2191
|
+
computed?.data ?? computed?.values ?? variable.data ?? variable.values
|
|
2192
|
+
);
|
|
2193
|
+
if (!values) {
|
|
2194
|
+
return void 0;
|
|
2195
|
+
}
|
|
2196
|
+
const chunks = chunkShapeFor(variable.shape);
|
|
2197
|
+
addJson(`${variableName}/.zarray`, {
|
|
2198
|
+
zarr_format: 2,
|
|
2199
|
+
shape: variable.shape,
|
|
2200
|
+
chunks,
|
|
2201
|
+
dtype: "<f4",
|
|
2202
|
+
compressor: null,
|
|
2203
|
+
fill_value: "NaN",
|
|
2204
|
+
order: "C",
|
|
2205
|
+
filters: null
|
|
2206
|
+
});
|
|
2207
|
+
addJson(`${variableName}/.zattrs`, {
|
|
2208
|
+
...variable.attrs,
|
|
2209
|
+
_ARRAY_DIMENSIONS: variable.dims
|
|
2210
|
+
});
|
|
2211
|
+
addVariableChunks(entries, variableName, values, variable.shape, chunks);
|
|
2212
|
+
}
|
|
2213
|
+
const byteLength = Array.from(entries.values()).reduce(
|
|
2214
|
+
(total, value) => total + value.byteLength,
|
|
2215
|
+
0
|
|
2216
|
+
);
|
|
2217
|
+
return {
|
|
2218
|
+
byteLength,
|
|
2219
|
+
get: async (key) => entries.get(normalizeStoreKey(key))
|
|
2220
|
+
};
|
|
2221
|
+
}
|
|
2222
|
+
function coordinateEntries(coords) {
|
|
2223
|
+
if (!coords) {
|
|
2224
|
+
return [];
|
|
2225
|
+
}
|
|
2226
|
+
return coords instanceof Map ? Array.from(coords.entries()) : Object.entries(coords);
|
|
2227
|
+
}
|
|
2228
|
+
function coordinateValues(values) {
|
|
2229
|
+
if (Array.isArray(values)) {
|
|
2230
|
+
return values;
|
|
2231
|
+
}
|
|
2232
|
+
if (ArrayBuffer.isView(values)) {
|
|
2233
|
+
return arrayBufferViewValues(values);
|
|
2234
|
+
}
|
|
2235
|
+
if (!isRecord(values)) {
|
|
2236
|
+
return void 0;
|
|
2237
|
+
}
|
|
2238
|
+
if (Array.isArray(values.values)) {
|
|
2239
|
+
return values.values;
|
|
2240
|
+
}
|
|
2241
|
+
if (ArrayBuffer.isView(values.values)) {
|
|
2242
|
+
return arrayBufferViewValues(values.values);
|
|
2243
|
+
}
|
|
2244
|
+
if (Array.isArray(values.data)) {
|
|
2245
|
+
return values.data;
|
|
2246
|
+
}
|
|
2247
|
+
if (ArrayBuffer.isView(values.data)) {
|
|
2248
|
+
return arrayBufferViewValues(values.data);
|
|
2249
|
+
}
|
|
2250
|
+
return void 0;
|
|
2251
|
+
}
|
|
2252
|
+
function arrayBufferViewValues(values) {
|
|
2253
|
+
return "length" in values ? Array.from(values) : void 0;
|
|
2254
|
+
}
|
|
2255
|
+
function stringCoordinates(values) {
|
|
2256
|
+
const raw = coordinateValues(values);
|
|
2257
|
+
if (!raw?.length) {
|
|
2258
|
+
return void 0;
|
|
2259
|
+
}
|
|
2260
|
+
const coordinates = raw.map((value) => {
|
|
2261
|
+
if (value instanceof Date) {
|
|
2262
|
+
return value.toISOString();
|
|
2263
|
+
}
|
|
2264
|
+
return typeof value === "string" ? value : void 0;
|
|
2265
|
+
});
|
|
2266
|
+
return coordinates.every((value) => value !== void 0) ? coordinates : void 0;
|
|
2267
|
+
}
|
|
2268
|
+
function normalizeStoreKey(key) {
|
|
2269
|
+
return key.replace(/^\/+/, "");
|
|
2270
|
+
}
|
|
2271
|
+
function chunkShapeFor(shape) {
|
|
2272
|
+
if (shape.length === 0) {
|
|
2273
|
+
return [];
|
|
2274
|
+
}
|
|
2275
|
+
return shape.map((size, index) => index === 0 && shape.length > 2 ? 1 : size);
|
|
2276
|
+
}
|
|
2277
|
+
function addVariableChunks(entries, variableName, values, shape, chunks) {
|
|
2278
|
+
if (shape.length === 0) {
|
|
2279
|
+
entries.set(`${variableName}/0`, encodeFloat32(values));
|
|
2280
|
+
return;
|
|
2281
|
+
}
|
|
2282
|
+
const chunkCounts = shape.map((size, index) => Math.ceil(size / (chunks[index] ?? size)));
|
|
2283
|
+
const chunkIndices = enumerateChunkIndices(chunkCounts);
|
|
2284
|
+
for (const chunkIndex of chunkIndices) {
|
|
2285
|
+
const contiguousRange = leadingContiguousChunkRange(shape, chunks, chunkIndex);
|
|
2286
|
+
const bytes = contiguousRange ? encodeFloat32(values, contiguousRange.start, contiguousRange.length) : encodeFloat32(extractChunk(values, shape, chunks, chunkIndex));
|
|
2287
|
+
entries.set(`${variableName}/${chunkIndex.join(".")}`, bytes);
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
function leadingContiguousChunkRange(shape, chunks, chunkIndex) {
|
|
2291
|
+
if (shape.length === 0) {
|
|
2292
|
+
return { start: 0, length: 1 };
|
|
2293
|
+
}
|
|
2294
|
+
const starts = chunkIndex.map((index, dimension) => index * (chunks[dimension] ?? 1));
|
|
2295
|
+
const stops = starts.map(
|
|
2296
|
+
(start, dimension) => Math.min(start + (chunks[dimension] ?? 1), shape[dimension] ?? start)
|
|
2297
|
+
);
|
|
2298
|
+
for (let dimension = 1; dimension < shape.length; dimension += 1) {
|
|
2299
|
+
if ((starts[dimension] ?? 0) !== 0 || (stops[dimension] ?? 0) !== (shape[dimension] ?? 0)) {
|
|
2300
|
+
return void 0;
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
const trailingSize = shape.slice(1).reduce((total, size) => total * size, 1);
|
|
2304
|
+
const leadingStart = starts[0] ?? 0;
|
|
2305
|
+
const leadingStop = stops[0] ?? leadingStart;
|
|
2306
|
+
return {
|
|
2307
|
+
start: leadingStart * trailingSize,
|
|
2308
|
+
length: Math.max(0, leadingStop - leadingStart) * trailingSize
|
|
2309
|
+
};
|
|
2310
|
+
}
|
|
2311
|
+
function enumerateChunkIndices(chunkCounts) {
|
|
2312
|
+
const results = [];
|
|
2313
|
+
const visit = (prefix, dimension) => {
|
|
2314
|
+
if (dimension === chunkCounts.length) {
|
|
2315
|
+
results.push(prefix);
|
|
2316
|
+
return;
|
|
2317
|
+
}
|
|
2318
|
+
const count = chunkCounts[dimension] ?? 0;
|
|
2319
|
+
for (let index = 0; index < count; index += 1) {
|
|
2320
|
+
visit([...prefix, index], dimension + 1);
|
|
2321
|
+
}
|
|
2322
|
+
};
|
|
2323
|
+
visit([], 0);
|
|
2324
|
+
return results;
|
|
2325
|
+
}
|
|
2326
|
+
function extractChunk(values, shape, chunks, chunkIndex) {
|
|
2327
|
+
const starts = chunkIndex.map((index, dimension) => index * (chunks[dimension] ?? 1));
|
|
2328
|
+
const stops = starts.map(
|
|
2329
|
+
(start, dimension) => Math.min(start + (chunks[dimension] ?? 1), shape[dimension] ?? start)
|
|
2330
|
+
);
|
|
2331
|
+
const result = [];
|
|
2332
|
+
const visit = (indices, dimension) => {
|
|
2333
|
+
if (dimension === shape.length) {
|
|
2334
|
+
result.push(values[flatIndex(indices, shape)] ?? Number.NaN);
|
|
2335
|
+
return;
|
|
2336
|
+
}
|
|
2337
|
+
const start = starts[dimension] ?? 0;
|
|
2338
|
+
const stop = stops[dimension] ?? start;
|
|
2339
|
+
for (let index = start; index < stop; index += 1) {
|
|
2340
|
+
visit([...indices, index], dimension + 1);
|
|
2341
|
+
}
|
|
2342
|
+
};
|
|
2343
|
+
visit([], 0);
|
|
2344
|
+
return result;
|
|
2345
|
+
}
|
|
2346
|
+
function flatIndex(indices, shape) {
|
|
2347
|
+
return indices.reduce((offset, index, dimension) => offset * (shape[dimension] ?? 1) + index, 0);
|
|
2348
|
+
}
|
|
2349
|
+
function flattenNumericValues(value) {
|
|
2350
|
+
if (value == null) {
|
|
2351
|
+
return void 0;
|
|
2352
|
+
}
|
|
2353
|
+
if (typeof value === "number") {
|
|
2354
|
+
return [value];
|
|
2355
|
+
}
|
|
2356
|
+
if (isFlatNumericArrayLike(value)) {
|
|
2357
|
+
return value;
|
|
2358
|
+
}
|
|
2359
|
+
if (!Array.isArray(value)) {
|
|
2360
|
+
return void 0;
|
|
2361
|
+
}
|
|
2362
|
+
const result = [];
|
|
2363
|
+
const visit = (current) => {
|
|
2364
|
+
if (typeof current === "number") {
|
|
2365
|
+
result.push(current);
|
|
2366
|
+
return;
|
|
2367
|
+
}
|
|
2368
|
+
if (isFlatNumericArrayLike(current)) {
|
|
2369
|
+
for (let index = 0; index < current.length; index += 1) {
|
|
2370
|
+
result.push(current[index] ?? Number.NaN);
|
|
2371
|
+
}
|
|
2372
|
+
return;
|
|
2373
|
+
}
|
|
2374
|
+
if (Array.isArray(current)) {
|
|
2375
|
+
for (const item of current) {
|
|
2376
|
+
visit(item);
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
};
|
|
2380
|
+
visit(value);
|
|
2381
|
+
return result;
|
|
2382
|
+
}
|
|
2383
|
+
function isFlatNumericArrayLike(value) {
|
|
2384
|
+
if (Array.isArray(value)) {
|
|
2385
|
+
return value.every((item) => typeof item === "number");
|
|
2386
|
+
}
|
|
2387
|
+
return ArrayBuffer.isView(value) && "length" in value && typeof value.length === "number" && !isBigIntArray(value);
|
|
2388
|
+
}
|
|
2389
|
+
function isBigIntArray(value) {
|
|
2390
|
+
return typeof BigInt64Array !== "undefined" && value instanceof BigInt64Array ? true : typeof BigUint64Array !== "undefined" && value instanceof BigUint64Array;
|
|
2391
|
+
}
|
|
2392
|
+
function encodeFloat32(values, start = 0, length = values.length - start) {
|
|
2393
|
+
if (length <= 0) {
|
|
2394
|
+
return new Uint8Array();
|
|
2395
|
+
}
|
|
2396
|
+
if (values instanceof Float32Array && isNativeLittleEndian()) {
|
|
2397
|
+
return new Uint8Array(values.buffer, values.byteOffset + start * 4, length * 4);
|
|
2398
|
+
}
|
|
2399
|
+
const bytes = new Uint8Array(length * 4);
|
|
2400
|
+
const view = new DataView(bytes.buffer);
|
|
2401
|
+
for (let index = 0; index < length; index += 1) {
|
|
2402
|
+
view.setFloat32(index * 4, values[start + index] ?? Number.NaN, true);
|
|
2403
|
+
}
|
|
2404
|
+
return bytes;
|
|
2405
|
+
}
|
|
2406
|
+
var nativeLittleEndian;
|
|
2407
|
+
function isNativeLittleEndian() {
|
|
2408
|
+
nativeLittleEndian ??= new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
2409
|
+
return nativeLittleEndian;
|
|
2410
|
+
}
|
|
2411
|
+
function encodeFloat64(values) {
|
|
2412
|
+
const bytes = new Uint8Array(values.length * 8);
|
|
2413
|
+
const view = new DataView(bytes.buffer);
|
|
2414
|
+
values.forEach((value, index) => view.setFloat64(index * 8, value, true));
|
|
2415
|
+
return bytes;
|
|
2416
|
+
}
|
|
2417
|
+
function encodeVLenUtf8(values) {
|
|
2418
|
+
const encoder = new TextEncoder();
|
|
2419
|
+
const encodedValues = values.map((value) => encoder.encode(value));
|
|
2420
|
+
const byteLength = 4 + encodedValues.reduce((total, value) => total + 4 + value.byteLength, 0);
|
|
2421
|
+
const bytes = new Uint8Array(byteLength);
|
|
2422
|
+
const view = new DataView(bytes.buffer);
|
|
2423
|
+
let offset = 0;
|
|
2424
|
+
view.setUint32(offset, encodedValues.length, true);
|
|
2425
|
+
offset += 4;
|
|
2426
|
+
for (const value of encodedValues) {
|
|
2427
|
+
view.setUint32(offset, value.byteLength, true);
|
|
2428
|
+
offset += 4;
|
|
2429
|
+
bytes.set(value, offset);
|
|
2430
|
+
offset += value.byteLength;
|
|
2431
|
+
}
|
|
2432
|
+
return bytes;
|
|
2433
|
+
}
|
|
2434
|
+
function normalizeSelectionBounds(bounds, fallbackOptions) {
|
|
2435
|
+
const isTuple = isSelectionBoundsTuple(bounds);
|
|
2436
|
+
const normalizedBounds = isTuple ? bounds : [bounds.west, bounds.south, bounds.east, bounds.north];
|
|
2437
|
+
if (normalizedBounds.length !== 4 || normalizedBounds.some((value) => typeof value !== "number" || !Number.isFinite(value))) {
|
|
2438
|
+
throw new DClimateAdapterError(
|
|
2439
|
+
"metadata",
|
|
2440
|
+
"dClimate selection bounds must be finite [west, south, east, north] numbers."
|
|
2441
|
+
);
|
|
2442
|
+
}
|
|
2443
|
+
const [west, south, east, north] = normalizedBounds;
|
|
2444
|
+
if (west >= east || south >= north) {
|
|
2445
|
+
throw new DClimateAdapterError(
|
|
2446
|
+
"metadata",
|
|
2447
|
+
"dClimate selection bounds must satisfy west < east and south < north."
|
|
2448
|
+
);
|
|
2449
|
+
}
|
|
2450
|
+
return {
|
|
2451
|
+
bounds: [west, south, east, north],
|
|
2452
|
+
boundsOptions: isTuple ? fallbackOptions : bounds.options ?? fallbackOptions
|
|
2453
|
+
};
|
|
2454
|
+
}
|
|
2455
|
+
function isSelectionBoundsTuple(bounds) {
|
|
2456
|
+
return Array.isArray(bounds);
|
|
2457
|
+
}
|
|
2458
|
+
async function openStore(cid, options) {
|
|
2459
|
+
try {
|
|
2460
|
+
if (options.openIpfsStore) {
|
|
2461
|
+
return options.openIpfsStore(cid, { gatewayUrl: options.gatewayUrl });
|
|
2462
|
+
}
|
|
2463
|
+
const module = await import('@dclimate/jaxray');
|
|
2464
|
+
const opened = await module.openIpfsStore?.(cid, { gatewayUrl: options.gatewayUrl });
|
|
2465
|
+
if (isRecord(opened) && "store" in opened && isReadableStore(opened.store)) {
|
|
2466
|
+
return opened.store;
|
|
2467
|
+
}
|
|
2468
|
+
return isReadableStore(opened) ? opened : void 0;
|
|
2469
|
+
} catch (error) {
|
|
2470
|
+
throw new DClimateAdapterError(
|
|
2471
|
+
"gateway",
|
|
2472
|
+
`Failed to open dClimate CID "${cid}" through Jaxray.`,
|
|
2473
|
+
error,
|
|
2474
|
+
{
|
|
2475
|
+
cid,
|
|
2476
|
+
gatewayUrl: options.gatewayUrl
|
|
2477
|
+
}
|
|
2478
|
+
);
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
function extractDataset(value) {
|
|
2482
|
+
if (Array.isArray(value)) {
|
|
2483
|
+
return extractDataset(value[0]);
|
|
2484
|
+
}
|
|
2485
|
+
if (!isRecord(value)) {
|
|
2486
|
+
return void 0;
|
|
2487
|
+
}
|
|
2488
|
+
const candidate = value.data ?? value.dataset ?? value.jaxrayDataset ?? value.geoTemporalDataset ?? (hasDatasetShape(value) ? value : void 0);
|
|
2489
|
+
return isRecord(candidate) ? candidate : void 0;
|
|
2490
|
+
}
|
|
2491
|
+
function extractGeoTemporalDataset(value) {
|
|
2492
|
+
if (Array.isArray(value)) {
|
|
2493
|
+
return extractGeoTemporalDataset(value[0]);
|
|
2494
|
+
}
|
|
2495
|
+
if (!isRecord(value)) {
|
|
2496
|
+
return void 0;
|
|
2497
|
+
}
|
|
2498
|
+
return hasRectangleSelection(value) || hasTimeRange(value) ? value : void 0;
|
|
2499
|
+
}
|
|
2500
|
+
function hasTimeRange(value) {
|
|
2501
|
+
return isRecord(value) && typeof value.timeRange === "function";
|
|
2502
|
+
}
|
|
2503
|
+
function hasRectangleSelection(value) {
|
|
2504
|
+
return isRecord(value) && typeof value.rectangle === "function";
|
|
2505
|
+
}
|
|
2506
|
+
function extractMetadata(value) {
|
|
2507
|
+
if (Array.isArray(value)) {
|
|
2508
|
+
return isRecord(value[1]) ? value[1] : void 0;
|
|
2509
|
+
}
|
|
2510
|
+
if (!isRecord(value)) {
|
|
2511
|
+
return void 0;
|
|
2512
|
+
}
|
|
2513
|
+
const metadata = value.metadata ?? value.clientMetadata;
|
|
2514
|
+
return isRecord(metadata) ? metadata : void 0;
|
|
2515
|
+
}
|
|
2516
|
+
function extractStore(value) {
|
|
2517
|
+
if (!isRecord(value)) {
|
|
2518
|
+
return void 0;
|
|
2519
|
+
}
|
|
2520
|
+
const candidate = value.store ?? value.zarrStore ?? value.ipfsStore;
|
|
2521
|
+
return isReadableStore(candidate) ? candidate : void 0;
|
|
2522
|
+
}
|
|
2523
|
+
function extractString(value, keys) {
|
|
2524
|
+
const metadata = extractMetadata(value);
|
|
2525
|
+
if (metadata) {
|
|
2526
|
+
for (const key of keys) {
|
|
2527
|
+
const candidate = metadata[key];
|
|
2528
|
+
if (typeof candidate === "string") {
|
|
2529
|
+
return candidate;
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
if (!isRecord(value)) {
|
|
2534
|
+
return void 0;
|
|
2535
|
+
}
|
|
2536
|
+
for (const key of keys) {
|
|
2537
|
+
const candidate = value[key];
|
|
2538
|
+
if (typeof candidate === "string") {
|
|
2539
|
+
return candidate;
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
return void 0;
|
|
2543
|
+
}
|
|
2544
|
+
function gatewaySourceForCid(cid, gatewayUrl) {
|
|
2545
|
+
if (!cid || !gatewayUrl) {
|
|
2546
|
+
return void 0;
|
|
2547
|
+
}
|
|
2548
|
+
return `${gatewayUrl.replace(/\/$/, "")}/ipfs/${cid}`;
|
|
2549
|
+
}
|
|
2550
|
+
function sourceIdForRequest(request) {
|
|
2551
|
+
if (request.cid) {
|
|
2552
|
+
return `dclimate-${request.cid.slice(0, 12)}`;
|
|
2553
|
+
}
|
|
2554
|
+
return [request.collection, request.dataset, request.variant].filter(Boolean).join("-");
|
|
2555
|
+
}
|
|
2556
|
+
function labelForRequest(request) {
|
|
2557
|
+
return [request.collection, request.dataset, request.variant].filter(Boolean).join(" / ") || request.cid;
|
|
2558
|
+
}
|
|
2559
|
+
function isRecord(value) {
|
|
2560
|
+
return typeof value === "object" && value !== null;
|
|
2561
|
+
}
|
|
2562
|
+
function hasDatasetShape(value) {
|
|
2563
|
+
return "data_vars" in value || "dataVars" in value || "variables" in value;
|
|
2564
|
+
}
|
|
2565
|
+
function isReadableStore(value) {
|
|
2566
|
+
return isRecord(value) && typeof value.get === "function";
|
|
2567
|
+
}
|
|
2568
|
+
|
|
2569
|
+
// src/renderers/maplibre.ts
|
|
2570
|
+
function createMapLibreGridLayer(options) {
|
|
2571
|
+
return new MapLibreGridLayerController(options);
|
|
2572
|
+
}
|
|
2573
|
+
function buildZarrLayerOptions(options) {
|
|
2574
|
+
assertValidGridDataSource(options.source, {
|
|
2575
|
+
variable: options.variable,
|
|
2576
|
+
requireRenderable: true
|
|
2577
|
+
});
|
|
2578
|
+
const variable = options.source.variables.find(
|
|
2579
|
+
(candidate) => candidate.name === options.variable || candidate.path === options.variable
|
|
2580
|
+
);
|
|
2581
|
+
if (!variable) {
|
|
2582
|
+
throw new GridError({
|
|
2583
|
+
code: "MISSING_VARIABLE",
|
|
2584
|
+
message: `Variable "${options.variable}" is not present in the grid source.`,
|
|
2585
|
+
variable: options.variable,
|
|
2586
|
+
sourceId: options.source.id
|
|
2587
|
+
});
|
|
2588
|
+
}
|
|
2589
|
+
const spatialDimensions = inferSpatialDimensions(options.source, variable.name);
|
|
2590
|
+
if (!spatialDimensions) {
|
|
2591
|
+
throw new GridError({
|
|
2592
|
+
code: "MISSING_SPATIAL_DIMENSIONS",
|
|
2593
|
+
message: `Variable "${variable.name}" needs spatial dimensions before rendering.`,
|
|
2594
|
+
variable: variable.name,
|
|
2595
|
+
sourceId: options.source.id
|
|
2596
|
+
});
|
|
2597
|
+
}
|
|
2598
|
+
const colorOptions = colorScaleToRendererOptions(options.colorScale);
|
|
2599
|
+
const rendererVariable = variable.path ?? variable.name;
|
|
2600
|
+
const normalizedSelectors = normalizeSelectors(options.source, variable.name, options.selectors);
|
|
2601
|
+
const rendererSelectors = omitSpatialSelectors(normalizedSelectors, spatialDimensions);
|
|
2602
|
+
const yDimension = options.source.dimensions.find(
|
|
2603
|
+
(dimension) => dimension.name === spatialDimensions.y
|
|
2604
|
+
);
|
|
2605
|
+
const shaderOptions = buildColorScaleShaderOptions(options.colorScale, rendererVariable);
|
|
2606
|
+
return {
|
|
2607
|
+
id: options.layerId ?? `zarr-map-${options.source.id ?? variable.name}`,
|
|
2608
|
+
source: options.source.source,
|
|
2609
|
+
store: options.source.store,
|
|
2610
|
+
variable: rendererVariable,
|
|
2611
|
+
selector: toRendererSelectors(rendererSelectors),
|
|
2612
|
+
spatialDimensions: { lon: spatialDimensions.x, lat: spatialDimensions.y },
|
|
2613
|
+
zarrVersion: options.source.zarrVersion,
|
|
2614
|
+
crs: options.source.crs,
|
|
2615
|
+
proj4: options.source.proj4,
|
|
2616
|
+
bounds: options.source.bounds,
|
|
2617
|
+
fillValue: variable.fillValue,
|
|
2618
|
+
colormap: colorOptions?.colormap,
|
|
2619
|
+
clim: colorOptions?.clim,
|
|
2620
|
+
opacity: options.opacity,
|
|
2621
|
+
latIsAscending: yDimension?.ascending,
|
|
2622
|
+
customFrag: shaderOptions?.customFrag,
|
|
2623
|
+
uniforms: shaderOptions?.uniforms,
|
|
2624
|
+
onLoad: () => {
|
|
2625
|
+
options.onLoadingChange?.(false);
|
|
2626
|
+
options.onLoadingStateChange?.(completeLoadingState());
|
|
2627
|
+
},
|
|
2628
|
+
onLoadingStateChange: (state) => {
|
|
2629
|
+
const loadingState = normalizeLoadingState(state);
|
|
2630
|
+
options.onLoadingChange?.(loadingState.loading);
|
|
2631
|
+
options.onLoadingStateChange?.(loadingState);
|
|
2632
|
+
},
|
|
2633
|
+
onError: (error) => options.onError?.(
|
|
2634
|
+
new GridError({
|
|
2635
|
+
code: "CHUNK_LOAD_FAILED",
|
|
2636
|
+
message: "Zarr renderer reported a metadata or chunk loading error.",
|
|
2637
|
+
cause: error,
|
|
2638
|
+
sourceId: options.source.id,
|
|
2639
|
+
variable: options.variable
|
|
2640
|
+
})
|
|
2641
|
+
)
|
|
2642
|
+
};
|
|
2643
|
+
}
|
|
2644
|
+
function buildColorScaleShaderOptions(colorScale, rendererVariable) {
|
|
2645
|
+
const transparentPalette = transparentPaletteKind(colorScale);
|
|
2646
|
+
if (!transparentPalette || !isValidGlslIdentifier(rendererVariable)) {
|
|
2647
|
+
return void 0;
|
|
2648
|
+
}
|
|
2649
|
+
return {
|
|
2650
|
+
customFrag: transparentPalette === "vegetation" ? vegetationOverlayShader(rendererVariable) : transparentPalette === "magma" ? magmaTransparentLowerBoundShader(rendererVariable) : precipitationOverlayShader(rendererVariable),
|
|
2651
|
+
uniforms: {}
|
|
2652
|
+
};
|
|
2653
|
+
}
|
|
2654
|
+
function precipitationOverlayShader(rendererVariable) {
|
|
2655
|
+
return `
|
|
2656
|
+
if (${rendererVariable} <= 0.0) {
|
|
2657
|
+
discard;
|
|
2658
|
+
}
|
|
2659
|
+
|
|
2660
|
+
float rescaled = clamp((${rendererVariable} - clim.x) / (clim.y - clim.x), 0.0, 1.0);
|
|
2661
|
+
vec4 c = texture(colormap, vec2(rescaled, 0.5));
|
|
2662
|
+
float precipAlpha = opacity * mix(0.22, 1.0, rescaled);
|
|
2663
|
+
fragColor = vec4(c.rgb, precipAlpha);
|
|
2664
|
+
fragColor.rgb *= fragColor.a;
|
|
2665
|
+
`;
|
|
2666
|
+
}
|
|
2667
|
+
function vegetationOverlayShader(rendererVariable) {
|
|
2668
|
+
return `
|
|
2669
|
+
float vegetationRange = clim.y - clim.x;
|
|
2670
|
+
float noVegetationCutoff = clim.x + vegetationRange * 0.04;
|
|
2671
|
+
if (${rendererVariable} <= noVegetationCutoff) {
|
|
2672
|
+
discard;
|
|
2673
|
+
}
|
|
2674
|
+
|
|
2675
|
+
float rescaled = clamp((${rendererVariable} - clim.x) / vegetationRange, 0.0, 1.0);
|
|
2676
|
+
vec4 c = texture(colormap, vec2(rescaled, 0.5));
|
|
2677
|
+
float vegetationAlpha = opacity * mix(0.14, 1.0, smoothstep(0.04, 0.85, rescaled));
|
|
2678
|
+
fragColor = vec4(c.rgb, vegetationAlpha);
|
|
2679
|
+
fragColor.rgb *= fragColor.a;
|
|
2680
|
+
`;
|
|
2681
|
+
}
|
|
2682
|
+
function magmaTransparentLowerBoundShader(rendererVariable) {
|
|
2683
|
+
return `
|
|
2684
|
+
float rescaled = clamp((${rendererVariable} - clim.x) / (clim.y - clim.x), 0.0, 1.0);
|
|
2685
|
+
vec4 c = texture(colormap, vec2(rescaled, 0.5));
|
|
2686
|
+
float magmaAlpha = opacity * smoothstep(0.0, 0.2, rescaled);
|
|
2687
|
+
fragColor = vec4(c.rgb, magmaAlpha);
|
|
2688
|
+
fragColor.rgb *= fragColor.a;
|
|
2689
|
+
`;
|
|
2690
|
+
}
|
|
2691
|
+
function transparentPaletteKind(colorScale) {
|
|
2692
|
+
if (typeof colorScale?.palette !== "string") {
|
|
2693
|
+
return void 0;
|
|
2694
|
+
}
|
|
2695
|
+
const palette = colorScale.palette.toLowerCase();
|
|
2696
|
+
if (palette === "magma" || palette === "precipitation" || palette === "vegetation") {
|
|
2697
|
+
return palette;
|
|
2698
|
+
}
|
|
2699
|
+
return void 0;
|
|
2700
|
+
}
|
|
2701
|
+
function isValidGlslIdentifier(value) {
|
|
2702
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(value);
|
|
2703
|
+
}
|
|
2704
|
+
function colorScaleShaderSignature(options) {
|
|
2705
|
+
const variable = options.source.variables.find(
|
|
2706
|
+
(candidate) => candidate.name === options.variable || candidate.path === options.variable
|
|
2707
|
+
);
|
|
2708
|
+
const rendererVariable = variable?.path ?? variable?.name ?? options.variable;
|
|
2709
|
+
return buildColorScaleShaderOptions(options.colorScale, rendererVariable)?.customFrag ?? "";
|
|
2710
|
+
}
|
|
2711
|
+
var MapLibreGridLayerController = class {
|
|
2712
|
+
id;
|
|
2713
|
+
ownsMap;
|
|
2714
|
+
options;
|
|
2715
|
+
map;
|
|
2716
|
+
layer;
|
|
2717
|
+
removed = false;
|
|
2718
|
+
constructor(options) {
|
|
2719
|
+
this.id = options.layerId ?? `zarr-map-${options.source.id ?? options.variable}`;
|
|
2720
|
+
this.options = { ...options, layerId: this.id };
|
|
2721
|
+
this.map = options.map;
|
|
2722
|
+
this.ownsMap = !options.map;
|
|
2723
|
+
}
|
|
2724
|
+
async mount() {
|
|
2725
|
+
if (this.layer || this.removed) {
|
|
2726
|
+
return;
|
|
2727
|
+
}
|
|
2728
|
+
this.options.onLoadingChange?.(true);
|
|
2729
|
+
this.options.onLoadingStateChange?.({
|
|
2730
|
+
loading: true,
|
|
2731
|
+
metadata: false,
|
|
2732
|
+
chunks: false,
|
|
2733
|
+
percent: 5,
|
|
2734
|
+
stage: "map"
|
|
2735
|
+
});
|
|
2736
|
+
try {
|
|
2737
|
+
this.map ??= await this.createMap();
|
|
2738
|
+
await this.whenMapReady(this.map);
|
|
2739
|
+
this.options.onLoadingStateChange?.({
|
|
2740
|
+
loading: true,
|
|
2741
|
+
metadata: false,
|
|
2742
|
+
chunks: false,
|
|
2743
|
+
percent: 25,
|
|
2744
|
+
stage: "layer"
|
|
2745
|
+
});
|
|
2746
|
+
this.layer = await (this.options.layerFactory ?? createDefaultZarrLayer)(
|
|
2747
|
+
buildZarrLayerOptions(this.options)
|
|
2748
|
+
);
|
|
2749
|
+
this.map.addLayer(this.layer, this.options.beforeId);
|
|
2750
|
+
this.options.onLoadingStateChange?.({
|
|
2751
|
+
loading: true,
|
|
2752
|
+
metadata: false,
|
|
2753
|
+
chunks: true,
|
|
2754
|
+
percent: 65,
|
|
2755
|
+
stage: "chunks"
|
|
2756
|
+
});
|
|
2757
|
+
} catch (error) {
|
|
2758
|
+
this.options.onLoadingChange?.(false);
|
|
2759
|
+
this.handleError("RENDERER_SETUP_FAILED", "Failed to create the MapLibre Zarr layer.", error);
|
|
2760
|
+
}
|
|
2761
|
+
}
|
|
2762
|
+
async update(update) {
|
|
2763
|
+
const nextOptions = {
|
|
2764
|
+
...this.options,
|
|
2765
|
+
...update
|
|
2766
|
+
};
|
|
2767
|
+
if (!this.layer) {
|
|
2768
|
+
this.options = nextOptions;
|
|
2769
|
+
await this.mount();
|
|
2770
|
+
return;
|
|
2771
|
+
}
|
|
2772
|
+
try {
|
|
2773
|
+
if (update.source && update.source !== this.options.source) {
|
|
2774
|
+
await this.replaceLayer(nextOptions);
|
|
2775
|
+
this.options = nextOptions;
|
|
2776
|
+
return;
|
|
2777
|
+
}
|
|
2778
|
+
if (colorScaleShaderSignature(nextOptions) !== colorScaleShaderSignature(this.options)) {
|
|
2779
|
+
await this.replaceLayer(nextOptions);
|
|
2780
|
+
this.options = nextOptions;
|
|
2781
|
+
return;
|
|
2782
|
+
}
|
|
2783
|
+
this.options = nextOptions;
|
|
2784
|
+
if (update.colorScale) {
|
|
2785
|
+
const colorOptions = colorScaleToRendererOptions(update.colorScale);
|
|
2786
|
+
this.layer.setColormap?.(colorOptions?.colormap ?? []);
|
|
2787
|
+
this.layer.setClim?.(colorOptions?.clim ?? update.colorScale.domain);
|
|
2788
|
+
}
|
|
2789
|
+
if (typeof update.opacity === "number") {
|
|
2790
|
+
this.layer.setOpacity?.(update.opacity);
|
|
2791
|
+
}
|
|
2792
|
+
if (update.variable) {
|
|
2793
|
+
this.layer.setVariable?.(update.variable);
|
|
2794
|
+
}
|
|
2795
|
+
if (update.selectors || update.variable || update.source) {
|
|
2796
|
+
const variable = update.variable ?? this.options.variable;
|
|
2797
|
+
const source = update.source ?? this.options.source;
|
|
2798
|
+
const spatialDimensions = inferSpatialDimensions(source, variable);
|
|
2799
|
+
const normalizedSelectors = normalizeSelectors(source, variable, this.options.selectors);
|
|
2800
|
+
const rendererSelectors = spatialDimensions ? omitSpatialSelectors(normalizedSelectors, spatialDimensions) : normalizedSelectors;
|
|
2801
|
+
this.layer.setSelector?.(toRendererSelectors(rendererSelectors));
|
|
2802
|
+
}
|
|
2803
|
+
} catch (error) {
|
|
2804
|
+
this.handleError("RENDERER_SETUP_FAILED", "Failed to update the MapLibre Zarr layer.", error);
|
|
2805
|
+
}
|
|
2806
|
+
}
|
|
2807
|
+
async replaceLayer(nextOptions) {
|
|
2808
|
+
if (!this.map) {
|
|
2809
|
+
this.options = nextOptions;
|
|
2810
|
+
await this.mount();
|
|
2811
|
+
return;
|
|
2812
|
+
}
|
|
2813
|
+
this.options.onLoadingChange?.(true);
|
|
2814
|
+
this.options.onLoadingStateChange?.({
|
|
2815
|
+
loading: true,
|
|
2816
|
+
metadata: false,
|
|
2817
|
+
chunks: false,
|
|
2818
|
+
percent: 5,
|
|
2819
|
+
stage: "layer"
|
|
2820
|
+
});
|
|
2821
|
+
const previousLayer = this.layer;
|
|
2822
|
+
if (previousLayer && this.map.getLayer(previousLayer.id)) {
|
|
2823
|
+
this.map.removeLayer(previousLayer.id);
|
|
2824
|
+
}
|
|
2825
|
+
if (previousLayer?.id && this.map.getSource?.(previousLayer.id)) {
|
|
2826
|
+
this.map.removeSource?.(previousLayer.id);
|
|
2827
|
+
}
|
|
2828
|
+
this.layer = void 0;
|
|
2829
|
+
try {
|
|
2830
|
+
this.options.onLoadingStateChange?.({
|
|
2831
|
+
loading: true,
|
|
2832
|
+
metadata: false,
|
|
2833
|
+
chunks: false,
|
|
2834
|
+
percent: 35,
|
|
2835
|
+
stage: "layer"
|
|
2836
|
+
});
|
|
2837
|
+
const nextLayer = await (nextOptions.layerFactory ?? createDefaultZarrLayer)(
|
|
2838
|
+
buildZarrLayerOptions(nextOptions)
|
|
2839
|
+
);
|
|
2840
|
+
this.layer = nextLayer;
|
|
2841
|
+
this.map.addLayer(nextLayer, nextOptions.beforeId);
|
|
2842
|
+
this.options.onLoadingStateChange?.({
|
|
2843
|
+
loading: true,
|
|
2844
|
+
metadata: false,
|
|
2845
|
+
chunks: true,
|
|
2846
|
+
percent: 65,
|
|
2847
|
+
stage: "chunks"
|
|
2848
|
+
});
|
|
2849
|
+
} catch (error) {
|
|
2850
|
+
this.options.onLoadingChange?.(false);
|
|
2851
|
+
this.handleError("RENDERER_SETUP_FAILED", "Failed to update the MapLibre Zarr layer.", error);
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
remove() {
|
|
2855
|
+
if (this.removed) {
|
|
2856
|
+
return;
|
|
2857
|
+
}
|
|
2858
|
+
this.removed = true;
|
|
2859
|
+
if (this.layer && this.map?.getLayer(this.layer.id)) {
|
|
2860
|
+
this.map.removeLayer(this.layer.id);
|
|
2861
|
+
}
|
|
2862
|
+
if (this.layer?.id && this.map?.getSource?.(this.layer.id)) {
|
|
2863
|
+
this.map.removeSource?.(this.layer.id);
|
|
2864
|
+
}
|
|
2865
|
+
if (this.ownsMap) {
|
|
2866
|
+
this.map?.remove?.();
|
|
2867
|
+
}
|
|
2868
|
+
this.layer = void 0;
|
|
2869
|
+
this.map = void 0;
|
|
2870
|
+
}
|
|
2871
|
+
async query(geometry) {
|
|
2872
|
+
if (!this.layer?.queryData) {
|
|
2873
|
+
throw new GridError({
|
|
2874
|
+
code: "UNSUPPORTED_GEOMETRY",
|
|
2875
|
+
message: "The active renderer layer does not expose queryData."
|
|
2876
|
+
});
|
|
2877
|
+
}
|
|
2878
|
+
return this.layer.queryData(geometry);
|
|
2879
|
+
}
|
|
2880
|
+
getMap() {
|
|
2881
|
+
return this.map;
|
|
2882
|
+
}
|
|
2883
|
+
getLayer() {
|
|
2884
|
+
return this.layer;
|
|
2885
|
+
}
|
|
2886
|
+
async createMap() {
|
|
2887
|
+
if (!this.options.mapConfig) {
|
|
2888
|
+
throw new GridError({
|
|
2889
|
+
code: "RENDERER_SETUP_FAILED",
|
|
2890
|
+
message: "Provide either an existing map instance or mapConfig to create one."
|
|
2891
|
+
});
|
|
2892
|
+
}
|
|
2893
|
+
if (this.options.mapFactory) {
|
|
2894
|
+
return this.options.mapFactory(this.options.mapConfig);
|
|
2895
|
+
}
|
|
2896
|
+
const maplibre = await import('maplibre-gl');
|
|
2897
|
+
return new maplibre.Map(
|
|
2898
|
+
this.options.mapConfig
|
|
2899
|
+
);
|
|
2900
|
+
}
|
|
2901
|
+
async whenMapReady(map) {
|
|
2902
|
+
if (this.isStyleReady(map) || !map.once) {
|
|
2903
|
+
return;
|
|
2904
|
+
}
|
|
2905
|
+
if (map.isStyleLoaded) {
|
|
2906
|
+
await new Promise((resolve) => {
|
|
2907
|
+
let resolved = false;
|
|
2908
|
+
const resolveWhenStyleReady = () => {
|
|
2909
|
+
if (resolved) {
|
|
2910
|
+
return;
|
|
2911
|
+
}
|
|
2912
|
+
if (this.isStyleReady(map)) {
|
|
2913
|
+
resolved = true;
|
|
2914
|
+
resolve();
|
|
2915
|
+
return;
|
|
2916
|
+
}
|
|
2917
|
+
map.once?.("styledata", resolveWhenStyleReady);
|
|
2918
|
+
};
|
|
2919
|
+
map.once?.("styledata", resolveWhenStyleReady);
|
|
2920
|
+
map.once?.("load", resolveWhenStyleReady);
|
|
2921
|
+
});
|
|
2922
|
+
return;
|
|
2923
|
+
}
|
|
2924
|
+
if (!map.loaded || map.loaded()) {
|
|
2925
|
+
return;
|
|
2926
|
+
}
|
|
2927
|
+
await new Promise((resolve) => {
|
|
2928
|
+
map.once?.("load", () => resolve());
|
|
2929
|
+
});
|
|
2930
|
+
}
|
|
2931
|
+
isStyleReady(map) {
|
|
2932
|
+
return map.isStyleLoaded?.() ?? map.loaded?.() ?? true;
|
|
2933
|
+
}
|
|
2934
|
+
handleError(code, message, cause) {
|
|
2935
|
+
const gridError = cause instanceof GridError ? cause : new GridError({
|
|
2936
|
+
code,
|
|
2937
|
+
message: errorMessageWithCause(message, cause),
|
|
2938
|
+
cause,
|
|
2939
|
+
sourceId: this.options.source.id,
|
|
2940
|
+
variable: this.options.variable
|
|
2941
|
+
});
|
|
2942
|
+
this.options.onError?.(gridError);
|
|
2943
|
+
throw gridError;
|
|
2944
|
+
}
|
|
2945
|
+
};
|
|
2946
|
+
function errorMessageWithCause(message, cause) {
|
|
2947
|
+
if (cause instanceof Error && cause.message) {
|
|
2948
|
+
return `${message} ${cause.message}`;
|
|
2949
|
+
}
|
|
2950
|
+
return message;
|
|
2951
|
+
}
|
|
2952
|
+
function omitSpatialSelectors(selectors, spatialDimensions) {
|
|
2953
|
+
return Object.fromEntries(
|
|
2954
|
+
Object.entries(selectors).filter(([dimensionName]) => {
|
|
2955
|
+
return dimensionName !== spatialDimensions.x && dimensionName !== spatialDimensions.y;
|
|
2956
|
+
})
|
|
2957
|
+
);
|
|
2958
|
+
}
|
|
2959
|
+
function normalizeLoadingState(state) {
|
|
2960
|
+
if (typeof state === "boolean") {
|
|
2961
|
+
return {
|
|
2962
|
+
loading: state,
|
|
2963
|
+
metadata: state,
|
|
2964
|
+
chunks: false,
|
|
2965
|
+
percent: state ? 35 : 100,
|
|
2966
|
+
stage: state ? "metadata" : "complete"
|
|
2967
|
+
};
|
|
2968
|
+
}
|
|
2969
|
+
const loading = Boolean(state.loading ?? state.metadata ?? state.chunks);
|
|
2970
|
+
const metadata = Boolean(state.metadata);
|
|
2971
|
+
const chunks = Boolean(state.chunks);
|
|
2972
|
+
return {
|
|
2973
|
+
loading,
|
|
2974
|
+
metadata,
|
|
2975
|
+
chunks,
|
|
2976
|
+
stage: state.stage ?? loadingStage(loading, metadata, chunks),
|
|
2977
|
+
percent: typeof state.percent === "number" ? Math.max(0, Math.min(100, state.percent)) : loading ? loadingPercent(metadata, chunks) : 100
|
|
2978
|
+
};
|
|
2979
|
+
}
|
|
2980
|
+
function loadingStage(loading, metadata, chunks) {
|
|
2981
|
+
if (!loading) {
|
|
2982
|
+
return "complete";
|
|
2983
|
+
}
|
|
2984
|
+
if (chunks) {
|
|
2985
|
+
return "chunks";
|
|
2986
|
+
}
|
|
2987
|
+
if (metadata) {
|
|
2988
|
+
return "metadata";
|
|
2989
|
+
}
|
|
2990
|
+
return "layer";
|
|
2991
|
+
}
|
|
2992
|
+
function loadingPercent(metadata, chunks) {
|
|
2993
|
+
if (chunks) {
|
|
2994
|
+
return 85;
|
|
2995
|
+
}
|
|
2996
|
+
if (metadata) {
|
|
2997
|
+
return 45;
|
|
2998
|
+
}
|
|
2999
|
+
return 65;
|
|
3000
|
+
}
|
|
3001
|
+
function completeLoadingState() {
|
|
3002
|
+
return {
|
|
3003
|
+
loading: false,
|
|
3004
|
+
metadata: false,
|
|
3005
|
+
chunks: false,
|
|
3006
|
+
percent: 100,
|
|
3007
|
+
stage: "complete"
|
|
3008
|
+
};
|
|
3009
|
+
}
|
|
3010
|
+
var zarrLayerConstructorPromise;
|
|
3011
|
+
async function preloadDefaultZarrLayer() {
|
|
3012
|
+
await loadDefaultZarrLayerConstructor();
|
|
3013
|
+
}
|
|
3014
|
+
async function createDefaultZarrLayer(options) {
|
|
3015
|
+
const ZarrLayer = await loadDefaultZarrLayerConstructor();
|
|
3016
|
+
return new ZarrLayer(options);
|
|
3017
|
+
}
|
|
3018
|
+
async function loadDefaultZarrLayerConstructor() {
|
|
3019
|
+
zarrLayerConstructorPromise ??= import('@carbonplan/zarr-layer').then((module) => {
|
|
3020
|
+
const zarrLayerModule = module;
|
|
3021
|
+
const ZarrLayer = zarrLayerModule.ZarrLayer ?? zarrLayerModule.default;
|
|
3022
|
+
if (!ZarrLayer) {
|
|
3023
|
+
throw new GridError({
|
|
3024
|
+
code: "RENDERER_SETUP_FAILED",
|
|
3025
|
+
message: "@carbonplan/zarr-layer did not export ZarrLayer."
|
|
3026
|
+
});
|
|
3027
|
+
}
|
|
3028
|
+
return ZarrLayer;
|
|
3029
|
+
}).catch((error) => {
|
|
3030
|
+
zarrLayerConstructorPromise = void 0;
|
|
3031
|
+
throw error;
|
|
3032
|
+
});
|
|
3033
|
+
return zarrLayerConstructorPromise;
|
|
3034
|
+
}
|
|
3035
|
+
|
|
3036
|
+
export { DClimateAdapterError, GridError, assertValidGridDataSource, buildZarrLayerOptions, colorScaleDisplayDomain, colorScaleToRendererOptions, convertDisplayValue, coordinateSelector, createDClimateSource, createDebouncer, createJaxraySource, createMapLibreGridLayer, findDimension, findVariable, indexSelector, inferSpatialDimensions, isGridError, isNumericDType, isoTimeSelector, listTimeCoordinates, normalizeSelectorValue, normalizeSelectors, paletteToRendererColorMap, preflightGridRequest, preloadDefaultZarrLayer, queryGrid, queryPoint, selectorToIndex, toGridError, toRendererSelectors, validateColorScale, validateGridDataSource };
|
|
3037
|
+
//# sourceMappingURL=index.js.map
|
|
3038
|
+
//# sourceMappingURL=index.js.map
|