@loaders.gl/scientific 0.0.0-stage → 5.0.0-alpha.13
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/README.md +21 -2
- package/dist/index.cjs +4313 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/netcdf/bundled.cjs +1394 -0
- package/dist/netcdf/bundled.d.ts +2 -0
- package/dist/netcdf/bundled.d.ts.map +1 -0
- package/dist/netcdf/bundled.js +5 -0
- package/dist/netcdf/bundled.js.map +1 -0
- package/dist/netcdf/index.cjs +1394 -0
- package/dist/netcdf/index.cjs.map +6 -0
- package/dist/netcdf/index.d.ts +7 -0
- package/dist/netcdf/index.d.ts.map +1 -0
- package/dist/netcdf/index.js +8 -0
- package/dist/netcdf/index.js.map +1 -0
- package/dist/netcdf/iobuffer/iobuffer.d.ts +253 -0
- package/dist/netcdf/iobuffer/iobuffer.d.ts.map +1 -0
- package/dist/netcdf/iobuffer/iobuffer.js +450 -0
- package/dist/netcdf/iobuffer/iobuffer.js.map +1 -0
- package/dist/netcdf/netcdf-format.cjs +39 -0
- package/dist/netcdf/netcdf-format.d.ts +11 -0
- package/dist/netcdf/netcdf-format.d.ts.map +1 -0
- package/dist/netcdf/netcdf-format.js +14 -0
- package/dist/netcdf/netcdf-format.js.map +1 -0
- package/dist/netcdf/netcdf-loader-with-parser.cjs +995 -0
- package/dist/netcdf/netcdf-loader-with-parser.d.ts +60 -0
- package/dist/netcdf/netcdf-loader-with-parser.d.ts.map +1 -0
- package/dist/netcdf/netcdf-loader-with-parser.js +35 -0
- package/dist/netcdf/netcdf-loader-with-parser.js.map +1 -0
- package/dist/netcdf/netcdf-loader.cjs +84 -0
- package/dist/netcdf/netcdf-loader.d.ts +81 -0
- package/dist/netcdf/netcdf-loader.d.ts.map +1 -0
- package/dist/netcdf/netcdf-loader.js +41 -0
- package/dist/netcdf/netcdf-loader.js.map +1 -0
- package/dist/netcdf/netcdf-source-loader-types.cjs +78 -0
- package/dist/netcdf/netcdf-source-loader-types.d.ts +28 -0
- package/dist/netcdf/netcdf-source-loader-types.d.ts.map +1 -0
- package/dist/netcdf/netcdf-source-loader-types.js +32 -0
- package/dist/netcdf/netcdf-source-loader-types.js.map +1 -0
- package/dist/netcdf/netcdf-source-loader.cjs +1319 -0
- package/dist/netcdf/netcdf-source-loader.d.ts +59 -0
- package/dist/netcdf/netcdf-source-loader.d.ts.map +1 -0
- package/dist/netcdf/netcdf-source-loader.js +421 -0
- package/dist/netcdf/netcdf-source-loader.js.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.d.ts +68 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.js +172 -0
- package/dist/netcdf/netcdfjs/netcdf-reader.js.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-types.d.ts +70 -0
- package/dist/netcdf/netcdfjs/netcdf-types.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/netcdf-types.js +3 -0
- package/dist/netcdf/netcdfjs/netcdf-types.js.map +1 -0
- package/dist/netcdf/netcdfjs/read-data.d.ts +19 -0
- package/dist/netcdf/netcdfjs/read-data.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/read-data.js +51 -0
- package/dist/netcdf/netcdfjs/read-data.js.map +1 -0
- package/dist/netcdf/netcdfjs/read-header.d.ts +16 -0
- package/dist/netcdf/netcdfjs/read-header.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/read-header.js +227 -0
- package/dist/netcdf/netcdfjs/read-header.js.map +1 -0
- package/dist/netcdf/netcdfjs/read-type.d.ts +36 -0
- package/dist/netcdf/netcdfjs/read-type.d.ts.map +1 -0
- package/dist/netcdf/netcdfjs/read-type.js +135 -0
- package/dist/netcdf/netcdfjs/read-type.js.map +1 -0
- package/dist/netcdf/unbundled.cjs +1394 -0
- package/dist/netcdf/unbundled.d.ts +2 -0
- package/dist/netcdf/unbundled.d.ts.map +1 -0
- package/dist/netcdf/unbundled.js +5 -0
- package/dist/netcdf/unbundled.js.map +1 -0
- package/dist/netcdf.dev.js +3153 -0
- package/dist/netcdf.min.js +12 -0
- package/dist/zarr/bundled.d.ts +2 -0
- package/dist/zarr/bundled.d.ts.map +1 -0
- package/dist/zarr/bundled.js +5 -0
- package/dist/zarr/bundled.js.map +1 -0
- package/dist/zarr/geo-zarr-source-loader.d.ts +121 -0
- package/dist/zarr/geo-zarr-source-loader.d.ts.map +1 -0
- package/dist/zarr/geo-zarr-source-loader.js +692 -0
- package/dist/zarr/geo-zarr-source-loader.js.map +1 -0
- package/dist/zarr/index.cjs +14747 -0
- package/dist/zarr/index.cjs.map +6 -0
- package/dist/zarr/index.d.ts +11 -0
- package/dist/zarr/index.d.ts.map +1 -0
- package/dist/zarr/index.js +10 -0
- package/dist/zarr/index.js.map +1 -0
- package/dist/zarr/lib/consolidated-zarr.d.ts +31 -0
- package/dist/zarr/lib/consolidated-zarr.d.ts.map +1 -0
- package/dist/zarr/lib/consolidated-zarr.js +113 -0
- package/dist/zarr/lib/consolidated-zarr.js.map +1 -0
- package/dist/zarr/lib/load-zarr.d.ts +11 -0
- package/dist/zarr/lib/load-zarr.d.ts.map +1 -0
- package/dist/zarr/lib/load-zarr.js +22 -0
- package/dist/zarr/lib/load-zarr.js.map +1 -0
- package/dist/zarr/lib/spatial-multiscales.d.ts +14 -0
- package/dist/zarr/lib/spatial-multiscales.d.ts.map +1 -0
- package/dist/zarr/lib/spatial-multiscales.js +34 -0
- package/dist/zarr/lib/spatial-multiscales.js.map +1 -0
- package/dist/zarr/lib/utils.d.ts +28 -0
- package/dist/zarr/lib/utils.d.ts.map +1 -0
- package/dist/zarr/lib/utils.js +137 -0
- package/dist/zarr/lib/utils.js.map +1 -0
- package/dist/zarr/lib/zarr-data-cache.d.ts +13 -0
- package/dist/zarr/lib/zarr-data-cache.d.ts.map +1 -0
- package/dist/zarr/lib/zarr-data-cache.js +70 -0
- package/dist/zarr/lib/zarr-data-cache.js.map +1 -0
- package/dist/zarr/lib/zarr-pixel-source.d.ts +41 -0
- package/dist/zarr/lib/zarr-pixel-source.d.ts.map +1 -0
- package/dist/zarr/lib/zarr-pixel-source.js +97 -0
- package/dist/zarr/lib/zarr-pixel-source.js.map +1 -0
- package/dist/zarr/lib/zarrita-pixel-source.d.ts +40 -0
- package/dist/zarr/lib/zarrita-pixel-source.d.ts.map +1 -0
- package/dist/zarr/lib/zarrita-pixel-source.js +96 -0
- package/dist/zarr/lib/zarrita-pixel-source.js.map +1 -0
- package/dist/zarr/ome-zarr-source-loader.d.ts +195 -0
- package/dist/zarr/ome-zarr-source-loader.d.ts.map +1 -0
- package/dist/zarr/ome-zarr-source-loader.js +523 -0
- package/dist/zarr/ome-zarr-source-loader.js.map +1 -0
- package/dist/zarr/spatial-data-source.d.ts +95 -0
- package/dist/zarr/spatial-data-source.d.ts.map +1 -0
- package/dist/zarr/spatial-data-source.js +242 -0
- package/dist/zarr/spatial-data-source.js.map +1 -0
- package/dist/zarr/types.d.ts +105 -0
- package/dist/zarr/types.d.ts.map +1 -0
- package/dist/zarr/types.js +5 -0
- package/dist/zarr/types.js.map +1 -0
- package/dist/zarr/unbundled.d.ts +2 -0
- package/dist/zarr/unbundled.d.ts.map +1 -0
- package/dist/zarr/unbundled.js +5 -0
- package/dist/zarr/unbundled.js.map +1 -0
- package/dist/zarr/zarr-array-source.d.ts +102 -0
- package/dist/zarr/zarr-array-source.d.ts.map +1 -0
- package/dist/zarr/zarr-array-source.js +132 -0
- package/dist/zarr/zarr-array-source.js.map +1 -0
- package/dist/zarr/zarr-loader-types.d.ts +236 -0
- package/dist/zarr/zarr-loader-types.d.ts.map +1 -0
- package/dist/zarr/zarr-loader-types.js +128 -0
- package/dist/zarr/zarr-loader-types.js.map +1 -0
- package/dist/zarr.dev.js +16574 -0
- package/dist/zarr.min.js +79 -0
- package/package.json +62 -4
- package/src/index.ts +52 -0
- package/src/netcdf/bundled.ts +5 -0
- package/src/netcdf/index.ts +14 -0
- package/src/netcdf/iobuffer/iobuffer.ts +510 -0
- package/src/netcdf/netcdf-format.ts +16 -0
- package/src/netcdf/netcdf-loader-with-parser.ts +54 -0
- package/src/netcdf/netcdf-loader.ts +62 -0
- package/src/netcdf/netcdf-source-loader-types.ts +45 -0
- package/src/netcdf/netcdf-source-loader.ts +526 -0
- package/src/netcdf/netcdfjs/LICENSE +24 -0
- package/src/netcdf/netcdfjs/netcdf-reader.ts +196 -0
- package/src/netcdf/netcdfjs/netcdf-types.ts +75 -0
- package/src/netcdf/netcdfjs/read-data.ts +71 -0
- package/src/netcdf/netcdfjs/read-header.ts +277 -0
- package/src/netcdf/netcdfjs/read-type.ts +147 -0
- package/src/netcdf/unbundled.ts +5 -0
- package/src/zarr/bundled.ts +5 -0
- package/src/zarr/geo-zarr-source-loader.ts +998 -0
- package/src/zarr/index.ts +60 -0
- package/src/zarr/lib/consolidated-zarr.ts +190 -0
- package/src/zarr/lib/load-zarr.ts +31 -0
- package/src/zarr/lib/spatial-multiscales.ts +47 -0
- package/src/zarr/lib/utils.ts +162 -0
- package/src/zarr/lib/zarr-data-cache.ts +91 -0
- package/src/zarr/lib/zarr-pixel-source.ts +134 -0
- package/src/zarr/lib/zarrita-pixel-source.ts +141 -0
- package/src/zarr/ome-zarr-source-loader.ts +837 -0
- package/src/zarr/spatial-data-source.ts +370 -0
- package/src/zarr/types.ts +129 -0
- package/src/zarr/unbundled.ts +5 -0
- package/src/zarr/zarr-array-source.ts +225 -0
- package/src/zarr/zarr-loader-types.ts +166 -0
|
@@ -0,0 +1,1394 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/netcdf/unbundled.ts
|
|
31
|
+
var unbundled_exports = {};
|
|
32
|
+
__export(unbundled_exports, {
|
|
33
|
+
NetCDFFormat: () => NetCDFFormat,
|
|
34
|
+
NetCDFLoaderWithParser: () => NetCDFLoaderWithParser,
|
|
35
|
+
NetCDFReader: () => NetCDFReader,
|
|
36
|
+
NetCDFSource: () => NetCDFSource,
|
|
37
|
+
NetCDFSourceLoader: () => NetCDFSourceLoaderWithParser,
|
|
38
|
+
NetCDFSourceLoaderWithParser: () => NetCDFSourceLoaderWithParser,
|
|
39
|
+
NetCDFWorkerLoaderWithParser: () => NetCDFWorkerLoaderWithParser
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(unbundled_exports);
|
|
42
|
+
|
|
43
|
+
// src/netcdf/iobuffer/iobuffer.ts
|
|
44
|
+
var DEFAULT_BYTE_LENGTH = 1024 * 8;
|
|
45
|
+
var IOBuffer = class _IOBuffer {
|
|
46
|
+
/**
|
|
47
|
+
* Reference to the internal ArrayBuffer object.
|
|
48
|
+
*/
|
|
49
|
+
buffer;
|
|
50
|
+
/**
|
|
51
|
+
* Byte length of the internal ArrayBuffer.
|
|
52
|
+
*/
|
|
53
|
+
byteLength;
|
|
54
|
+
/**
|
|
55
|
+
* Byte offset of the internal ArrayBuffer.
|
|
56
|
+
*/
|
|
57
|
+
byteOffset;
|
|
58
|
+
/**
|
|
59
|
+
* Byte length of the internal ArrayBuffer.
|
|
60
|
+
*/
|
|
61
|
+
length;
|
|
62
|
+
/**
|
|
63
|
+
* The current offset of the buffer's pointer.
|
|
64
|
+
*/
|
|
65
|
+
offset;
|
|
66
|
+
lastWrittenByte;
|
|
67
|
+
littleEndian;
|
|
68
|
+
_data;
|
|
69
|
+
_mark;
|
|
70
|
+
_marks;
|
|
71
|
+
textDecoder = new TextDecoder();
|
|
72
|
+
textEncoder = new TextEncoder();
|
|
73
|
+
/**
|
|
74
|
+
* @param data - The data to construct the IOBuffer with.
|
|
75
|
+
* If data is a number, it will be the new buffer's length<br>
|
|
76
|
+
* If data is `undefined`, the buffer will be initialized with a default length of 8Kb<br>
|
|
77
|
+
* If data is an ArrayBuffer, SharedArrayBuffer, an ArrayBufferView (Typed Array), an IOBuffer instance,
|
|
78
|
+
* or a Node.js Buffer, a view will be created over the underlying ArrayBuffer.
|
|
79
|
+
* @param options
|
|
80
|
+
*/
|
|
81
|
+
constructor(data = DEFAULT_BYTE_LENGTH, options = {}) {
|
|
82
|
+
let dataIsGiven = false;
|
|
83
|
+
if (typeof data === "number") {
|
|
84
|
+
data = new ArrayBuffer(data);
|
|
85
|
+
} else {
|
|
86
|
+
dataIsGiven = true;
|
|
87
|
+
this.lastWrittenByte = data.byteLength;
|
|
88
|
+
}
|
|
89
|
+
const offset = options.offset ? options.offset >>> 0 : 0;
|
|
90
|
+
const byteLength = data.byteLength - offset;
|
|
91
|
+
let dvOffset = offset;
|
|
92
|
+
if (ArrayBuffer.isView(data) || data instanceof _IOBuffer) {
|
|
93
|
+
if (data.byteLength !== data.buffer.byteLength) {
|
|
94
|
+
dvOffset = data.byteOffset + offset;
|
|
95
|
+
}
|
|
96
|
+
data = data.buffer;
|
|
97
|
+
}
|
|
98
|
+
if (dataIsGiven) {
|
|
99
|
+
this.lastWrittenByte = byteLength;
|
|
100
|
+
} else {
|
|
101
|
+
this.lastWrittenByte = 0;
|
|
102
|
+
}
|
|
103
|
+
this.buffer = data;
|
|
104
|
+
this.length = byteLength;
|
|
105
|
+
this.byteLength = byteLength;
|
|
106
|
+
this.byteOffset = dvOffset;
|
|
107
|
+
this.offset = 0;
|
|
108
|
+
this.littleEndian = true;
|
|
109
|
+
this._data = new DataView(this.buffer, dvOffset, byteLength);
|
|
110
|
+
this._mark = 0;
|
|
111
|
+
this._marks = [];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Checks if the memory allocated to the buffer is sufficient to store more
|
|
115
|
+
* bytes after the offset.
|
|
116
|
+
* @param byteLength - The needed memory in bytes.
|
|
117
|
+
* @returns `true` if there is sufficient space and `false` otherwise.
|
|
118
|
+
*/
|
|
119
|
+
available(byteLength = 1) {
|
|
120
|
+
return this.offset + byteLength <= this.length;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Check if little-endian mode is used for reading and writing multi-byte
|
|
124
|
+
* values.
|
|
125
|
+
* @returns `true` if little-endian mode is used, `false` otherwise.
|
|
126
|
+
*/
|
|
127
|
+
isLittleEndian() {
|
|
128
|
+
return this.littleEndian;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Set little-endian mode for reading and writing multi-byte values.
|
|
132
|
+
*/
|
|
133
|
+
setLittleEndian() {
|
|
134
|
+
this.littleEndian = true;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Check if big-endian mode is used for reading and writing multi-byte values.
|
|
139
|
+
* @returns `true` if big-endian mode is used, `false` otherwise.
|
|
140
|
+
*/
|
|
141
|
+
isBigEndian() {
|
|
142
|
+
return !this.littleEndian;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Switches to big-endian mode for reading and writing multi-byte values.
|
|
146
|
+
*/
|
|
147
|
+
setBigEndian() {
|
|
148
|
+
this.littleEndian = false;
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Move the pointer n bytes forward.
|
|
153
|
+
* @param n - Number of bytes to skip.
|
|
154
|
+
*/
|
|
155
|
+
skip(n = 1) {
|
|
156
|
+
this.offset += n;
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Move the pointer to the given offset.
|
|
161
|
+
* @param offset
|
|
162
|
+
*/
|
|
163
|
+
seek(offset) {
|
|
164
|
+
this.offset = offset;
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Store the current pointer offset.
|
|
169
|
+
* @see {@link IOBuffer#reset}
|
|
170
|
+
*/
|
|
171
|
+
mark() {
|
|
172
|
+
this._mark = this.offset;
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Move the pointer back to the last pointer offset set by mark.
|
|
177
|
+
* @see {@link IOBuffer#mark}
|
|
178
|
+
*/
|
|
179
|
+
reset() {
|
|
180
|
+
this.offset = this._mark;
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Push the current pointer offset to the mark stack.
|
|
185
|
+
* @see {@link IOBuffer#popMark}
|
|
186
|
+
*/
|
|
187
|
+
pushMark() {
|
|
188
|
+
this._marks.push(this.offset);
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Pop the last pointer offset from the mark stack, and set the current
|
|
193
|
+
* pointer offset to the popped value.
|
|
194
|
+
* @see {@link IOBuffer#pushMark}
|
|
195
|
+
*/
|
|
196
|
+
popMark() {
|
|
197
|
+
const offset = this._marks.pop();
|
|
198
|
+
if (offset === void 0) {
|
|
199
|
+
throw new Error("Mark stack empty");
|
|
200
|
+
}
|
|
201
|
+
this.seek(offset);
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Move the pointer offset back to 0.
|
|
206
|
+
*/
|
|
207
|
+
rewind() {
|
|
208
|
+
this.offset = 0;
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Make sure the buffer has sufficient memory to write a given byteLength at
|
|
213
|
+
* the current pointer offset.
|
|
214
|
+
* If the buffer's memory is insufficient, this method will create a new
|
|
215
|
+
* buffer (a copy) with a length that is twice (byteLength + current offset).
|
|
216
|
+
* @param byteLength
|
|
217
|
+
*/
|
|
218
|
+
ensureAvailable(byteLength = 1) {
|
|
219
|
+
if (!this.available(byteLength)) {
|
|
220
|
+
const lengthNeeded = this.offset + byteLength;
|
|
221
|
+
const newLength = lengthNeeded * 2;
|
|
222
|
+
const newArray = new Uint8Array(newLength);
|
|
223
|
+
newArray.set(new Uint8Array(this.buffer));
|
|
224
|
+
this.buffer = newArray.buffer;
|
|
225
|
+
this.length = this.byteLength = newLength;
|
|
226
|
+
this._data = new DataView(this.buffer);
|
|
227
|
+
}
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Read a byte and return false if the byte's value is 0, or true otherwise.
|
|
232
|
+
* Moves pointer forward by one byte.
|
|
233
|
+
*/
|
|
234
|
+
readBoolean() {
|
|
235
|
+
return this.readUint8() !== 0;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Read a signed 8-bit integer and move pointer forward by 1 byte.
|
|
239
|
+
*/
|
|
240
|
+
readInt8() {
|
|
241
|
+
return this._data.getInt8(this.offset++);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Read an unsigned 8-bit integer and move pointer forward by 1 byte.
|
|
245
|
+
*/
|
|
246
|
+
readUint8() {
|
|
247
|
+
return this._data.getUint8(this.offset++);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Alias for {@link IOBuffer#readUint8}.
|
|
251
|
+
*/
|
|
252
|
+
readByte() {
|
|
253
|
+
return this.readUint8();
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Read `n` bytes and move pointer forward by `n` bytes.
|
|
257
|
+
*/
|
|
258
|
+
readBytes(n = 1) {
|
|
259
|
+
const bytes = new Uint8Array(n);
|
|
260
|
+
for (let i = 0; i < n; i++) {
|
|
261
|
+
bytes[i] = this.readByte();
|
|
262
|
+
}
|
|
263
|
+
return bytes;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Read a 16-bit signed integer and move pointer forward by 2 bytes.
|
|
267
|
+
*/
|
|
268
|
+
readInt16() {
|
|
269
|
+
const value = this._data.getInt16(this.offset, this.littleEndian);
|
|
270
|
+
this.offset += 2;
|
|
271
|
+
return value;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Read a 16-bit unsigned integer and move pointer forward by 2 bytes.
|
|
275
|
+
*/
|
|
276
|
+
readUint16() {
|
|
277
|
+
const value = this._data.getUint16(this.offset, this.littleEndian);
|
|
278
|
+
this.offset += 2;
|
|
279
|
+
return value;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Read a 32-bit signed integer and move pointer forward by 4 bytes.
|
|
283
|
+
*/
|
|
284
|
+
readInt32() {
|
|
285
|
+
const value = this._data.getInt32(this.offset, this.littleEndian);
|
|
286
|
+
this.offset += 4;
|
|
287
|
+
return value;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Read a 32-bit unsigned integer and move pointer forward by 4 bytes.
|
|
291
|
+
*/
|
|
292
|
+
readUint32() {
|
|
293
|
+
const value = this._data.getUint32(this.offset, this.littleEndian);
|
|
294
|
+
this.offset += 4;
|
|
295
|
+
return value;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Read a 32-bit floating number and move pointer forward by 4 bytes.
|
|
299
|
+
*/
|
|
300
|
+
readFloat32() {
|
|
301
|
+
const value = this._data.getFloat32(this.offset, this.littleEndian);
|
|
302
|
+
this.offset += 4;
|
|
303
|
+
return value;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Read a 64-bit floating number and move pointer forward by 8 bytes.
|
|
307
|
+
*/
|
|
308
|
+
readFloat64() {
|
|
309
|
+
const value = this._data.getFloat64(this.offset, this.littleEndian);
|
|
310
|
+
this.offset += 8;
|
|
311
|
+
return value;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Read a 1-byte ASCII character and move pointer forward by 1 byte.
|
|
315
|
+
*/
|
|
316
|
+
readChar() {
|
|
317
|
+
return String.fromCharCode(this.readInt8());
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Read `n` 1-byte ASCII characters and move pointer forward by `n` bytes.
|
|
321
|
+
*/
|
|
322
|
+
readChars(n = 1) {
|
|
323
|
+
let result = "";
|
|
324
|
+
for (let i = 0; i < n; i++) {
|
|
325
|
+
result += this.readChar();
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Read the next `n` bytes, return a UTF-8 decoded string and move pointer
|
|
331
|
+
* forward by `n` bytes.
|
|
332
|
+
*/
|
|
333
|
+
readUtf8(n = 1) {
|
|
334
|
+
return this.textDecoder.decode(this.readBytes(n));
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Write 0xff if the passed value is truthy, 0x00 otherwise and move pointer
|
|
338
|
+
* forward by 1 byte.
|
|
339
|
+
*/
|
|
340
|
+
writeBoolean(value) {
|
|
341
|
+
this.writeUint8(value ? 255 : 0);
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Write `value` as an 8-bit signed integer and move pointer forward by 1 byte.
|
|
346
|
+
*/
|
|
347
|
+
writeInt8(value) {
|
|
348
|
+
this.ensureAvailable(1);
|
|
349
|
+
this._data.setInt8(this.offset++, value);
|
|
350
|
+
this._updateLastWrittenByte();
|
|
351
|
+
return this;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Write `value` as an 8-bit unsigned integer and move pointer forward by 1
|
|
355
|
+
* byte.
|
|
356
|
+
*/
|
|
357
|
+
writeUint8(value) {
|
|
358
|
+
this.ensureAvailable(1);
|
|
359
|
+
this._data.setUint8(this.offset++, value);
|
|
360
|
+
this._updateLastWrittenByte();
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* An alias for {@link IOBuffer#writeUint8}.
|
|
365
|
+
*/
|
|
366
|
+
writeByte(value) {
|
|
367
|
+
return this.writeUint8(value);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Write all elements of `bytes` as uint8 values and move pointer forward by
|
|
371
|
+
* `bytes.length` bytes.
|
|
372
|
+
*/
|
|
373
|
+
writeBytes(bytes) {
|
|
374
|
+
this.ensureAvailable(bytes.length);
|
|
375
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
376
|
+
this._data.setUint8(this.offset++, bytes[i]);
|
|
377
|
+
}
|
|
378
|
+
this._updateLastWrittenByte();
|
|
379
|
+
return this;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Write `value` as a 16-bit signed integer and move pointer forward by 2
|
|
383
|
+
* bytes.
|
|
384
|
+
*/
|
|
385
|
+
writeInt16(value) {
|
|
386
|
+
this.ensureAvailable(2);
|
|
387
|
+
this._data.setInt16(this.offset, value, this.littleEndian);
|
|
388
|
+
this.offset += 2;
|
|
389
|
+
this._updateLastWrittenByte();
|
|
390
|
+
return this;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Write `value` as a 16-bit unsigned integer and move pointer forward by 2
|
|
394
|
+
* bytes.
|
|
395
|
+
*/
|
|
396
|
+
writeUint16(value) {
|
|
397
|
+
this.ensureAvailable(2);
|
|
398
|
+
this._data.setUint16(this.offset, value, this.littleEndian);
|
|
399
|
+
this.offset += 2;
|
|
400
|
+
this._updateLastWrittenByte();
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Write `value` as a 32-bit signed integer and move pointer forward by 4
|
|
405
|
+
* bytes.
|
|
406
|
+
*/
|
|
407
|
+
writeInt32(value) {
|
|
408
|
+
this.ensureAvailable(4);
|
|
409
|
+
this._data.setInt32(this.offset, value, this.littleEndian);
|
|
410
|
+
this.offset += 4;
|
|
411
|
+
this._updateLastWrittenByte();
|
|
412
|
+
return this;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Write `value` as a 32-bit unsigned integer and move pointer forward by 4
|
|
416
|
+
* bytes.
|
|
417
|
+
*/
|
|
418
|
+
writeUint32(value) {
|
|
419
|
+
this.ensureAvailable(4);
|
|
420
|
+
this._data.setUint32(this.offset, value, this.littleEndian);
|
|
421
|
+
this.offset += 4;
|
|
422
|
+
this._updateLastWrittenByte();
|
|
423
|
+
return this;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Write `value` as a 32-bit floating number and move pointer forward by 4
|
|
427
|
+
* bytes.
|
|
428
|
+
*/
|
|
429
|
+
writeFloat32(value) {
|
|
430
|
+
this.ensureAvailable(4);
|
|
431
|
+
this._data.setFloat32(this.offset, value, this.littleEndian);
|
|
432
|
+
this.offset += 4;
|
|
433
|
+
this._updateLastWrittenByte();
|
|
434
|
+
return this;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Write `value` as a 64-bit floating number and move pointer forward by 8
|
|
438
|
+
* bytes.
|
|
439
|
+
*/
|
|
440
|
+
writeFloat64(value) {
|
|
441
|
+
this.ensureAvailable(8);
|
|
442
|
+
this._data.setFloat64(this.offset, value, this.littleEndian);
|
|
443
|
+
this.offset += 8;
|
|
444
|
+
this._updateLastWrittenByte();
|
|
445
|
+
return this;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Write the charCode of `str`'s first character as an 8-bit unsigned integer
|
|
449
|
+
* and move pointer forward by 1 byte.
|
|
450
|
+
*/
|
|
451
|
+
writeChar(str) {
|
|
452
|
+
return this.writeUint8(str.charCodeAt(0));
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Write the charCodes of all `str`'s characters as 8-bit unsigned integers
|
|
456
|
+
* and move pointer forward by `str.length` bytes.
|
|
457
|
+
*/
|
|
458
|
+
writeChars(str) {
|
|
459
|
+
for (let i = 0; i < str.length; i++) {
|
|
460
|
+
this.writeUint8(str.charCodeAt(i));
|
|
461
|
+
}
|
|
462
|
+
return this;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* UTF-8 encode and write `str` to the current pointer offset and move pointer
|
|
466
|
+
* forward according to the encoded length.
|
|
467
|
+
*/
|
|
468
|
+
writeUtf8(str) {
|
|
469
|
+
const bytes = this.textEncoder.encode(str);
|
|
470
|
+
return this.writeBytes(bytes);
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Export a Uint8Array view of the internal buffer.
|
|
474
|
+
* The view starts at the byte offset and its length
|
|
475
|
+
* is calculated to stop at the last written byte or the original length.
|
|
476
|
+
*/
|
|
477
|
+
toArray() {
|
|
478
|
+
return new Uint8Array(this.buffer, this.byteOffset, this.lastWrittenByte);
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Update the last written byte offset
|
|
482
|
+
* @private
|
|
483
|
+
*/
|
|
484
|
+
_updateLastWrittenByte() {
|
|
485
|
+
if (this.offset > this.lastWrittenByte) {
|
|
486
|
+
this.lastWrittenByte = this.offset;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// src/netcdf/netcdfjs/read-type.ts
|
|
492
|
+
var TYPES = {
|
|
493
|
+
BYTE: 1,
|
|
494
|
+
CHAR: 2,
|
|
495
|
+
SHORT: 3,
|
|
496
|
+
INT: 4,
|
|
497
|
+
FLOAT: 5,
|
|
498
|
+
DOUBLE: 6
|
|
499
|
+
};
|
|
500
|
+
function readType(buffer, type, size) {
|
|
501
|
+
switch (type) {
|
|
502
|
+
case TYPES.BYTE:
|
|
503
|
+
return buffer.readBytes(size);
|
|
504
|
+
case TYPES.CHAR:
|
|
505
|
+
return trimNull(buffer.readChars(size));
|
|
506
|
+
case TYPES.SHORT:
|
|
507
|
+
return readNumber(size, buffer.readInt16.bind(buffer));
|
|
508
|
+
case TYPES.INT:
|
|
509
|
+
return readNumber(size, buffer.readInt32.bind(buffer));
|
|
510
|
+
case TYPES.FLOAT:
|
|
511
|
+
return readNumber(size, buffer.readFloat32.bind(buffer));
|
|
512
|
+
case TYPES.DOUBLE:
|
|
513
|
+
return readNumber(size, buffer.readFloat64.bind(buffer));
|
|
514
|
+
/* istanbul ignore next */
|
|
515
|
+
default:
|
|
516
|
+
throw new Error(`NetCDF: non valid type ${type}`);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
function num2str(type) {
|
|
520
|
+
switch (Number(type)) {
|
|
521
|
+
case TYPES.BYTE:
|
|
522
|
+
return "byte";
|
|
523
|
+
case TYPES.CHAR:
|
|
524
|
+
return "char";
|
|
525
|
+
case TYPES.SHORT:
|
|
526
|
+
return "short";
|
|
527
|
+
case TYPES.INT:
|
|
528
|
+
return "int";
|
|
529
|
+
case TYPES.FLOAT:
|
|
530
|
+
return "float";
|
|
531
|
+
case TYPES.DOUBLE:
|
|
532
|
+
return "double";
|
|
533
|
+
/* istanbul ignore next */
|
|
534
|
+
default:
|
|
535
|
+
return "undefined";
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
function num2bytes(type) {
|
|
539
|
+
switch (Number(type)) {
|
|
540
|
+
case TYPES.BYTE:
|
|
541
|
+
return 1;
|
|
542
|
+
case TYPES.CHAR:
|
|
543
|
+
return 1;
|
|
544
|
+
case TYPES.SHORT:
|
|
545
|
+
return 2;
|
|
546
|
+
case TYPES.INT:
|
|
547
|
+
return 4;
|
|
548
|
+
case TYPES.FLOAT:
|
|
549
|
+
return 4;
|
|
550
|
+
case TYPES.DOUBLE:
|
|
551
|
+
return 8;
|
|
552
|
+
/* istanbul ignore next */
|
|
553
|
+
default:
|
|
554
|
+
return -1;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
function str2num(type) {
|
|
558
|
+
switch (String(type)) {
|
|
559
|
+
case "byte":
|
|
560
|
+
return TYPES.BYTE;
|
|
561
|
+
case "char":
|
|
562
|
+
return TYPES.CHAR;
|
|
563
|
+
case "short":
|
|
564
|
+
return TYPES.SHORT;
|
|
565
|
+
case "int":
|
|
566
|
+
return TYPES.INT;
|
|
567
|
+
case "float":
|
|
568
|
+
return TYPES.FLOAT;
|
|
569
|
+
case "double":
|
|
570
|
+
return TYPES.DOUBLE;
|
|
571
|
+
/* istanbul ignore next */
|
|
572
|
+
default:
|
|
573
|
+
return -1;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
function readNumber(size, bufferReader) {
|
|
577
|
+
if (size !== 1) {
|
|
578
|
+
const numbers = new Array(size);
|
|
579
|
+
for (let i = 0; i < size; i++) {
|
|
580
|
+
numbers[i] = bufferReader();
|
|
581
|
+
}
|
|
582
|
+
return numbers;
|
|
583
|
+
}
|
|
584
|
+
return bufferReader();
|
|
585
|
+
}
|
|
586
|
+
function trimNull(value) {
|
|
587
|
+
if (value.charCodeAt(value.length - 1) === 0) {
|
|
588
|
+
return value.substring(0, value.length - 1);
|
|
589
|
+
}
|
|
590
|
+
return value;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// src/netcdf/netcdfjs/read-header.ts
|
|
594
|
+
var ZERO = 0;
|
|
595
|
+
var NC_DIMENSION = 10;
|
|
596
|
+
var NC_VARIABLE = 11;
|
|
597
|
+
var NC_ATTRIBUTE = 12;
|
|
598
|
+
var NC_UNLIMITED = 0;
|
|
599
|
+
function readNetCDFHeader(buffer, version) {
|
|
600
|
+
const recordDimensionLength = buffer.readUint32();
|
|
601
|
+
const dimList = readDimensionsList(buffer);
|
|
602
|
+
const attributes = readAttributesList(buffer);
|
|
603
|
+
const variableList = readVariablesList(buffer, dimList.recordId, version);
|
|
604
|
+
const header = {
|
|
605
|
+
version,
|
|
606
|
+
recordDimension: {
|
|
607
|
+
length: recordDimensionLength,
|
|
608
|
+
id: dimList.recordId,
|
|
609
|
+
// id of the unlimited dimension
|
|
610
|
+
name: dimList.recordName,
|
|
611
|
+
// name of the unlimited dimension
|
|
612
|
+
recordStep: variableList.recordStep
|
|
613
|
+
},
|
|
614
|
+
dimensions: dimList.dimensions,
|
|
615
|
+
variables: variableList.variables,
|
|
616
|
+
attributes
|
|
617
|
+
};
|
|
618
|
+
return header;
|
|
619
|
+
}
|
|
620
|
+
function readDimensionsList(buffer) {
|
|
621
|
+
const dimList = buffer.readUint32();
|
|
622
|
+
if (dimList === ZERO) {
|
|
623
|
+
if (buffer.readUint32() !== ZERO) {
|
|
624
|
+
throw new Error("NetCDF: wrong empty tag for list of dimensions");
|
|
625
|
+
}
|
|
626
|
+
return {
|
|
627
|
+
recordId: 0,
|
|
628
|
+
recordName: "",
|
|
629
|
+
dimensions: []
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
if (dimList !== NC_DIMENSION) {
|
|
633
|
+
throw new Error("NetCDF: wrong tag for list of dimensions");
|
|
634
|
+
}
|
|
635
|
+
const dimensionSize = buffer.readUint32();
|
|
636
|
+
const dimensions = new Array(dimensionSize);
|
|
637
|
+
let recordId;
|
|
638
|
+
let recordName;
|
|
639
|
+
for (let dim = 0; dim < dimensionSize; dim++) {
|
|
640
|
+
const name = readName(buffer);
|
|
641
|
+
const size = buffer.readUint32();
|
|
642
|
+
if (size === NC_UNLIMITED) {
|
|
643
|
+
recordId = dim;
|
|
644
|
+
recordName = name;
|
|
645
|
+
}
|
|
646
|
+
dimensions[dim] = {
|
|
647
|
+
name,
|
|
648
|
+
size
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
return {
|
|
652
|
+
dimensions,
|
|
653
|
+
recordId,
|
|
654
|
+
recordName
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
function readAttributesList(buffer) {
|
|
658
|
+
const gAttList = buffer.readUint32();
|
|
659
|
+
if (gAttList === ZERO) {
|
|
660
|
+
if (buffer.readUint32() !== ZERO) {
|
|
661
|
+
throw new Error("NetCDF: wrong empty tag for list of attributes");
|
|
662
|
+
}
|
|
663
|
+
return [];
|
|
664
|
+
}
|
|
665
|
+
if (gAttList !== NC_ATTRIBUTE) {
|
|
666
|
+
throw new Error("NetCDF: wrong tag for list of attributes");
|
|
667
|
+
}
|
|
668
|
+
const attributeSize = buffer.readUint32();
|
|
669
|
+
const attributes = new Array(attributeSize);
|
|
670
|
+
for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
|
|
671
|
+
const name = readName(buffer);
|
|
672
|
+
const type = buffer.readUint32();
|
|
673
|
+
if (type < 1 || type > 6) {
|
|
674
|
+
throw new Error(`NetCDF: non valid type ${type}`);
|
|
675
|
+
}
|
|
676
|
+
const size = buffer.readUint32();
|
|
677
|
+
const value = readType(buffer, type, size);
|
|
678
|
+
padding(buffer);
|
|
679
|
+
attributes[gAtt] = {
|
|
680
|
+
name,
|
|
681
|
+
type: num2str(type),
|
|
682
|
+
value
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
return attributes;
|
|
686
|
+
}
|
|
687
|
+
function readVariablesList(buffer, recordId, version) {
|
|
688
|
+
const varList = buffer.readUint32();
|
|
689
|
+
let recordStep = 0;
|
|
690
|
+
if (varList === ZERO) {
|
|
691
|
+
if (buffer.readUint32() !== ZERO) {
|
|
692
|
+
throw new Error("NetCDF: wrong empty tag for list of variables");
|
|
693
|
+
}
|
|
694
|
+
return {
|
|
695
|
+
recordStep,
|
|
696
|
+
variables: []
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
if (varList !== NC_VARIABLE) {
|
|
700
|
+
throw new Error("NetCDF: wrong tag for list of variables");
|
|
701
|
+
}
|
|
702
|
+
const variableSize = buffer.readUint32();
|
|
703
|
+
const variables = new Array(variableSize);
|
|
704
|
+
for (let v = 0; v < variableSize; v++) {
|
|
705
|
+
const name = readName(buffer);
|
|
706
|
+
const dimensionality = buffer.readUint32();
|
|
707
|
+
const dimensionsIds = new Array(dimensionality);
|
|
708
|
+
for (let dim = 0; dim < dimensionality; dim++) {
|
|
709
|
+
dimensionsIds[dim] = buffer.readUint32();
|
|
710
|
+
}
|
|
711
|
+
const attributes = readAttributesList(buffer);
|
|
712
|
+
const type = buffer.readUint32();
|
|
713
|
+
if (type < 1 && type > 6) {
|
|
714
|
+
throw new Error(`NetCDF: non valid type ${type}`);
|
|
715
|
+
}
|
|
716
|
+
const varSize = buffer.readUint32();
|
|
717
|
+
let offset = buffer.readUint32();
|
|
718
|
+
if (version === 2) {
|
|
719
|
+
if (offset > 0) {
|
|
720
|
+
throw new Error("NetCDF: offsets larger than 4GB not supported");
|
|
721
|
+
}
|
|
722
|
+
offset = buffer.readUint32();
|
|
723
|
+
}
|
|
724
|
+
let record = false;
|
|
725
|
+
if (typeof recordId !== "undefined" && dimensionsIds[0] === recordId) {
|
|
726
|
+
recordStep += varSize;
|
|
727
|
+
record = true;
|
|
728
|
+
}
|
|
729
|
+
variables[v] = {
|
|
730
|
+
name,
|
|
731
|
+
dimensions: dimensionsIds,
|
|
732
|
+
attributes,
|
|
733
|
+
type: num2str(type),
|
|
734
|
+
size: varSize,
|
|
735
|
+
offset,
|
|
736
|
+
record
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
return {
|
|
740
|
+
variables,
|
|
741
|
+
recordStep
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
function readName(buffer) {
|
|
745
|
+
const nameLength = buffer.readUint32();
|
|
746
|
+
const name = buffer.readChars(nameLength);
|
|
747
|
+
padding(buffer);
|
|
748
|
+
return name;
|
|
749
|
+
}
|
|
750
|
+
function padding(buffer) {
|
|
751
|
+
if (buffer.offset % 4 !== 0) {
|
|
752
|
+
buffer.skip(4 - buffer.offset % 4);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// src/netcdf/netcdfjs/read-data.ts
|
|
757
|
+
function readNonRecord(buffer, variable) {
|
|
758
|
+
const type = str2num(variable.type);
|
|
759
|
+
const size = variable.size / num2bytes(type);
|
|
760
|
+
const data = new Array(size);
|
|
761
|
+
for (let i = 0; i < size; i++) {
|
|
762
|
+
data[i] = readType(buffer, type, 1);
|
|
763
|
+
}
|
|
764
|
+
return data;
|
|
765
|
+
}
|
|
766
|
+
function readRecord(buffer, variable, recordDimension, dimensions) {
|
|
767
|
+
const type = str2num(variable.type);
|
|
768
|
+
const width = variable.dimensions.filter((dimensionId) => dimensionId !== recordDimension.id).reduce((elementCount, dimensionId) => elementCount * dimensions[dimensionId].size, 1);
|
|
769
|
+
const size = recordDimension.length;
|
|
770
|
+
const data = new Array(size);
|
|
771
|
+
const step = recordDimension.recordStep;
|
|
772
|
+
for (let i = 0; i < size; i++) {
|
|
773
|
+
const currentOffset = buffer.offset;
|
|
774
|
+
data[i] = readType(buffer, type, width);
|
|
775
|
+
buffer.seek(currentOffset + step);
|
|
776
|
+
}
|
|
777
|
+
return data;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
// src/netcdf/netcdfjs/netcdf-reader.ts
|
|
781
|
+
var NetCDFReader = class {
|
|
782
|
+
header;
|
|
783
|
+
buffer;
|
|
784
|
+
constructor(data) {
|
|
785
|
+
const buffer = new IOBuffer(data);
|
|
786
|
+
buffer.setBigEndian();
|
|
787
|
+
const magic = buffer.readChars(3);
|
|
788
|
+
if (magic !== "CDF") {
|
|
789
|
+
throw new Error(`NetCDF: file should start with 'CDF', found ${magic}`);
|
|
790
|
+
}
|
|
791
|
+
const version = buffer.readByte();
|
|
792
|
+
if (version > 2) {
|
|
793
|
+
throw new Error(`NetCDF: unsupported version ${version}`);
|
|
794
|
+
}
|
|
795
|
+
this.header = readNetCDFHeader(buffer, version);
|
|
796
|
+
this.buffer = buffer;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* @return {string} - Version for the NetCDF format
|
|
800
|
+
*/
|
|
801
|
+
get version() {
|
|
802
|
+
if (this.header.version === 1) {
|
|
803
|
+
return "classic format";
|
|
804
|
+
}
|
|
805
|
+
return "64-bit offset format";
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Get metadata for the record dimension
|
|
809
|
+
*/
|
|
810
|
+
get recordDimension() {
|
|
811
|
+
return this.header.recordDimension;
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Get list of dimensions (each with `name` and `size`)
|
|
815
|
+
*/
|
|
816
|
+
get dimensions() {
|
|
817
|
+
return this.header.dimensions;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Get list of global attributes with:
|
|
821
|
+
* * `name`: String with the name of the attribute
|
|
822
|
+
* * `type`: String with the type of the attribute
|
|
823
|
+
* * `value`: A number or string with the value of the attribute
|
|
824
|
+
*/
|
|
825
|
+
get attributes() {
|
|
826
|
+
return this.header.attributes;
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Get list of variables
|
|
830
|
+
*/
|
|
831
|
+
get variables() {
|
|
832
|
+
return this.header.variables;
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* Check if an attribute exists
|
|
836
|
+
* @param attributeName - Name of the attribute to find
|
|
837
|
+
* @return
|
|
838
|
+
*/
|
|
839
|
+
attributeExists(attributeName) {
|
|
840
|
+
const attribute = this.attributes.find((val) => val.name === attributeName);
|
|
841
|
+
return attribute !== void 0;
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Returns the value of an attribute
|
|
845
|
+
* @param attributeName
|
|
846
|
+
* @return Value of the attributeName or null
|
|
847
|
+
*/
|
|
848
|
+
getAttribute(attributeName) {
|
|
849
|
+
const attribute = this.attributes.find((val) => val.name === attributeName);
|
|
850
|
+
if (attribute) return attribute.value;
|
|
851
|
+
return null;
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Check if a dataVariable exists
|
|
855
|
+
* @param variableName - Name of the variable to find
|
|
856
|
+
* @return
|
|
857
|
+
*/
|
|
858
|
+
dataVariableExists(variableName) {
|
|
859
|
+
const variable = this.header.variables.find(function(val) {
|
|
860
|
+
return val.name === variableName;
|
|
861
|
+
});
|
|
862
|
+
return variable !== void 0;
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Returns the value of a variable as a string
|
|
866
|
+
* @param variableName
|
|
867
|
+
* @return Value of the variable as a string or null
|
|
868
|
+
*/
|
|
869
|
+
getDataVariableAsString(variableName) {
|
|
870
|
+
const variable = this.getDataVariable(variableName);
|
|
871
|
+
if (variable) return variable.join("");
|
|
872
|
+
return null;
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Retrieves the data for a given variable
|
|
876
|
+
* @param variableName - Name of the variable to search or variable object
|
|
877
|
+
* @return List with the variable values
|
|
878
|
+
*/
|
|
879
|
+
getDataVariable(variableName) {
|
|
880
|
+
let variable;
|
|
881
|
+
if (typeof variableName === "string") {
|
|
882
|
+
variable = this.header.variables.find(function(val) {
|
|
883
|
+
return val.name === variableName;
|
|
884
|
+
});
|
|
885
|
+
} else {
|
|
886
|
+
variable = variableName;
|
|
887
|
+
}
|
|
888
|
+
if (variable === void 0) {
|
|
889
|
+
let errorOutput;
|
|
890
|
+
if (typeof variableName === "string") {
|
|
891
|
+
errorOutput = variableName;
|
|
892
|
+
} else if (typeof variableName === "object") {
|
|
893
|
+
errorOutput = JSON.stringify(variableName);
|
|
894
|
+
}
|
|
895
|
+
throw new Error(`NetCDF: variable not found: ${errorOutput}`);
|
|
896
|
+
}
|
|
897
|
+
this.buffer.seek(variable.offset);
|
|
898
|
+
if (variable.record) {
|
|
899
|
+
return readRecord(this.buffer, variable, this.header.recordDimension, this.header.dimensions);
|
|
900
|
+
}
|
|
901
|
+
return readNonRecord(this.buffer, variable);
|
|
902
|
+
}
|
|
903
|
+
toString() {
|
|
904
|
+
const result = [];
|
|
905
|
+
result.push("DIMENSIONS");
|
|
906
|
+
for (const dimension of this.dimensions) {
|
|
907
|
+
result.push(` ${dimension.name.padEnd(30)} = size: ${dimension.size}`);
|
|
908
|
+
}
|
|
909
|
+
result.push("");
|
|
910
|
+
result.push("GLOBAL ATTRIBUTES");
|
|
911
|
+
for (const attribute of this.attributes) {
|
|
912
|
+
result.push(` ${attribute.name.padEnd(30)} = ${attribute.value}`);
|
|
913
|
+
}
|
|
914
|
+
const variables = JSON.parse(JSON.stringify(this.variables));
|
|
915
|
+
result.push("");
|
|
916
|
+
result.push("VARIABLES:");
|
|
917
|
+
for (const variable of variables) {
|
|
918
|
+
variable.value = this.getDataVariable(variable);
|
|
919
|
+
let stringify = JSON.stringify(variable.value);
|
|
920
|
+
if (stringify.length > 50) stringify = stringify.substring(0, 50);
|
|
921
|
+
if (!Number.isNaN(variable.value.length)) {
|
|
922
|
+
stringify += ` (length: ${variable.value.length})`;
|
|
923
|
+
}
|
|
924
|
+
result.push(` ${variable.name.padEnd(30)} = ${stringify}`);
|
|
925
|
+
}
|
|
926
|
+
return result.join("\n");
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
|
|
930
|
+
// src/netcdf/netcdf-format.ts
|
|
931
|
+
var NetCDFFormat = {
|
|
932
|
+
name: "NetCDF",
|
|
933
|
+
id: "netcdf",
|
|
934
|
+
module: "netcdf",
|
|
935
|
+
encoding: "binary",
|
|
936
|
+
format: "netcdf",
|
|
937
|
+
extensions: ["cdf", "nc"],
|
|
938
|
+
mimeTypes: ["application/x-netcdf"],
|
|
939
|
+
binary: true
|
|
940
|
+
};
|
|
941
|
+
|
|
942
|
+
// src/netcdf/netcdf-loader.ts
|
|
943
|
+
var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
|
|
944
|
+
async function preload() {
|
|
945
|
+
const { NetCDFLoaderWithParser: NetCDFLoaderWithParser2 } = await import("@loaders.gl/scientific/netcdf/netcdf-loader-with-parser");
|
|
946
|
+
return NetCDFLoaderWithParser2;
|
|
947
|
+
}
|
|
948
|
+
var NetCDFLoader = {
|
|
949
|
+
...NetCDFFormat,
|
|
950
|
+
dataType: null,
|
|
951
|
+
batchType: null,
|
|
952
|
+
name: "NetCDF",
|
|
953
|
+
id: "mvt",
|
|
954
|
+
module: "scientific",
|
|
955
|
+
version: VERSION,
|
|
956
|
+
extensions: ["cdf", "nc"],
|
|
957
|
+
mimeTypes: [
|
|
958
|
+
"application/netcdf",
|
|
959
|
+
"application/x-netcdf"
|
|
960
|
+
// 'application/octet-stream'
|
|
961
|
+
],
|
|
962
|
+
category: "image",
|
|
963
|
+
binary: true,
|
|
964
|
+
options: {
|
|
965
|
+
netcdf: {
|
|
966
|
+
loadVariables: false
|
|
967
|
+
}
|
|
968
|
+
},
|
|
969
|
+
preload
|
|
970
|
+
};
|
|
971
|
+
var NetCDFWorkerLoader = NetCDFLoader;
|
|
972
|
+
|
|
973
|
+
// src/netcdf/netcdf-loader-with-parser.ts
|
|
974
|
+
var { preload: _NetCDFWorkerLoaderPreload, ...NetCDFWorkerLoaderMetadataWithoutPreload } = NetCDFWorkerLoader;
|
|
975
|
+
var { preload: _NetCDFLoaderPreload, ...NetCDFLoaderMetadataWithoutPreload } = NetCDFLoader;
|
|
976
|
+
var NetCDFWorkerLoaderWithParser = {
|
|
977
|
+
...NetCDFWorkerLoaderMetadataWithoutPreload
|
|
978
|
+
};
|
|
979
|
+
var NetCDFLoaderWithParser = {
|
|
980
|
+
...NetCDFLoaderMetadataWithoutPreload,
|
|
981
|
+
parse: async (arrayBuffer, options) => parseNetCDF(arrayBuffer, options)
|
|
982
|
+
};
|
|
983
|
+
function parseNetCDF(arrayBuffer, options) {
|
|
984
|
+
const reader = new NetCDFReader(arrayBuffer);
|
|
985
|
+
const variables = {};
|
|
986
|
+
if (options?.netcdf?.loadData) {
|
|
987
|
+
for (const variable of reader.variables) {
|
|
988
|
+
variables[variable.name] = reader.getDataVariable(variable);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return {
|
|
992
|
+
loaderData: reader.header,
|
|
993
|
+
data: variables
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
// src/netcdf/netcdf-source-loader.ts
|
|
998
|
+
var import_loader_utils = require("@loaders.gl/core/loader-utils");
|
|
999
|
+
var VERSION2 = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
|
|
1000
|
+
var NetCDFSource = class _NetCDFSource extends import_loader_utils.DataSource {
|
|
1001
|
+
static HEADER_RANGE_SIZE = 64 * 1024;
|
|
1002
|
+
static MAX_HEADER_RANGE_SIZE = 4 * 1024 * 1024;
|
|
1003
|
+
headerPromise = null;
|
|
1004
|
+
/** Capabilities of the NetCDF variable and dimension-slice executor. */
|
|
1005
|
+
rasterQueryCapabilities = Object.freeze({
|
|
1006
|
+
bounds: "unsupported",
|
|
1007
|
+
level: "unsupported",
|
|
1008
|
+
variables: "pushdown",
|
|
1009
|
+
slices: "residual",
|
|
1010
|
+
streaming: false,
|
|
1011
|
+
cancellation: true
|
|
1012
|
+
});
|
|
1013
|
+
/** Creates a source over a NetCDF URL or Blob. */
|
|
1014
|
+
constructor(data, options = {}) {
|
|
1015
|
+
super(data, options);
|
|
1016
|
+
}
|
|
1017
|
+
/** Discovers variables and dimensions without decoding data arrays. */
|
|
1018
|
+
async getQueryMetadata(options = {}) {
|
|
1019
|
+
throwIfAborted(options.signal);
|
|
1020
|
+
const { header, byteLength } = await this.getHeader(options.signal);
|
|
1021
|
+
throwIfAborted(options.signal);
|
|
1022
|
+
const fields = header.variables.map((variable) => createVariableField(variable, header));
|
|
1023
|
+
const schema = { fields, metadata: createDimensionMetadata(header) };
|
|
1024
|
+
return (0, import_loader_utils.createScanQueryMetadata)({
|
|
1025
|
+
sourceType: "netcdf",
|
|
1026
|
+
queryType: "raster",
|
|
1027
|
+
execution: { status: "supported", method: "getRaster" },
|
|
1028
|
+
name: typeof this.data === "string" ? this.data.split("/").pop() : void 0,
|
|
1029
|
+
schema,
|
|
1030
|
+
capabilities: {
|
|
1031
|
+
bounds: "unsupported",
|
|
1032
|
+
levelOfDetail: "unsupported",
|
|
1033
|
+
raster: this.rasterQueryCapabilities
|
|
1034
|
+
},
|
|
1035
|
+
statistics: {
|
|
1036
|
+
byteLength,
|
|
1037
|
+
rowCount: header.recordDimension.length || void 0
|
|
1038
|
+
}
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Reads selected numeric variables and applies named dimension slices.
|
|
1043
|
+
*
|
|
1044
|
+
* Multiple variables must retain the same shape and numeric type so they can be returned as
|
|
1045
|
+
* ordinary raster bands. Leading dimensions are folded into height and the final dimension is
|
|
1046
|
+
* exposed as width; the original dimension names and shape remain in result metadata.
|
|
1047
|
+
*/
|
|
1048
|
+
async getRaster(options = {}) {
|
|
1049
|
+
(0, import_loader_utils.validateRasterQueryOptions)(options);
|
|
1050
|
+
validateNetCDFUnsupportedRasterOptions(options);
|
|
1051
|
+
throwIfAborted(options.signal);
|
|
1052
|
+
const reader = await this.loadReader(options.signal);
|
|
1053
|
+
throwIfAborted(options.signal);
|
|
1054
|
+
validateNetCDFSliceNames(reader.header, options.slices);
|
|
1055
|
+
const variables = selectNetCDFVariables(reader.header, options.variables);
|
|
1056
|
+
const results = variables.map((variable) => {
|
|
1057
|
+
throwIfAborted(options.signal);
|
|
1058
|
+
const dimensionNames = variable.dimensions.map(
|
|
1059
|
+
(dimensionId) => reader.header.dimensions[dimensionId]?.name || `dimension_${dimensionId}`
|
|
1060
|
+
);
|
|
1061
|
+
const dimensionSizes = variable.dimensions.map(
|
|
1062
|
+
(dimensionId) => getDimensionSize(reader.header, dimensionId)
|
|
1063
|
+
);
|
|
1064
|
+
const values = flattenNetCDFValues(reader.getDataVariable(variable));
|
|
1065
|
+
const selection = selectNetCDFValues(
|
|
1066
|
+
values,
|
|
1067
|
+
dimensionNames,
|
|
1068
|
+
dimensionSizes,
|
|
1069
|
+
options.slices || {},
|
|
1070
|
+
options.signal
|
|
1071
|
+
);
|
|
1072
|
+
const dtype = getNetCDFRasterDataType(variable.type);
|
|
1073
|
+
return {
|
|
1074
|
+
variable,
|
|
1075
|
+
dimensionNames: selection.dimensionNames,
|
|
1076
|
+
shape: selection.shape,
|
|
1077
|
+
data: createNetCDFTypedArray(selection.values, dtype),
|
|
1078
|
+
dtype
|
|
1079
|
+
};
|
|
1080
|
+
});
|
|
1081
|
+
const reference = results[0];
|
|
1082
|
+
for (const result of results.slice(1)) {
|
|
1083
|
+
if (result.dtype !== reference.dtype || !arraysEqual(result.shape, reference.shape)) {
|
|
1084
|
+
throw new Error(
|
|
1085
|
+
"NetCDF raster variables must retain the same shape and numeric type after slicing."
|
|
1086
|
+
);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
const width = reference.shape.at(-1) ?? 1;
|
|
1090
|
+
const height = reference.shape.length < 2 ? 1 : reference.shape.slice(0, -1).reduce((product, size) => product * size, 1);
|
|
1091
|
+
const noData = getNetCDFNoDataValue(reference.variable);
|
|
1092
|
+
return {
|
|
1093
|
+
data: results.length === 1 ? reference.data : results.map((result) => result.data),
|
|
1094
|
+
width,
|
|
1095
|
+
height,
|
|
1096
|
+
bandCount: results.length,
|
|
1097
|
+
dtype: reference.dtype,
|
|
1098
|
+
interleaved: false,
|
|
1099
|
+
noData,
|
|
1100
|
+
metadata: {
|
|
1101
|
+
variables: results.map((result) => result.variable.name),
|
|
1102
|
+
dimensions: reference.dimensionNames,
|
|
1103
|
+
shape: reference.shape,
|
|
1104
|
+
slices: { ...options.slices }
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
/** Loads a complete NetCDF reader for selected-variable execution. */
|
|
1109
|
+
async loadReader(signal) {
|
|
1110
|
+
let arrayBuffer;
|
|
1111
|
+
if (typeof this.data === "string") {
|
|
1112
|
+
const response = await this.fetch(this.url, { signal });
|
|
1113
|
+
if (!response.ok) throw new Error(`NetCDF request failed with status ${response.status}.`);
|
|
1114
|
+
arrayBuffer = await response.arrayBuffer();
|
|
1115
|
+
} else {
|
|
1116
|
+
arrayBuffer = await this.data.arrayBuffer();
|
|
1117
|
+
}
|
|
1118
|
+
throwIfAborted(signal);
|
|
1119
|
+
return new NetCDFReader(arrayBuffer);
|
|
1120
|
+
}
|
|
1121
|
+
/** Reads and caches the NetCDF header, preserving abort behavior for each caller. */
|
|
1122
|
+
async getHeader(signal) {
|
|
1123
|
+
throwIfAborted(signal);
|
|
1124
|
+
if (!this.headerPromise) {
|
|
1125
|
+
this.headerPromise = this.loadHeader().catch((error) => {
|
|
1126
|
+
this.headerPromise = null;
|
|
1127
|
+
throw error;
|
|
1128
|
+
});
|
|
1129
|
+
}
|
|
1130
|
+
const result = await this.headerPromise;
|
|
1131
|
+
throwIfAborted(signal);
|
|
1132
|
+
return result;
|
|
1133
|
+
}
|
|
1134
|
+
/** Loads enough leading bytes to parse a remote header, growing ranges when required. */
|
|
1135
|
+
async loadHeader() {
|
|
1136
|
+
if (typeof this.data !== "string") {
|
|
1137
|
+
const arrayBuffer2 = await this.data.arrayBuffer();
|
|
1138
|
+
return { header: new NetCDFReader(arrayBuffer2).header, byteLength: arrayBuffer2.byteLength };
|
|
1139
|
+
}
|
|
1140
|
+
let rangeSize = _NetCDFSource.HEADER_RANGE_SIZE;
|
|
1141
|
+
let response = null;
|
|
1142
|
+
let arrayBuffer = null;
|
|
1143
|
+
let lastError;
|
|
1144
|
+
while (rangeSize <= _NetCDFSource.MAX_HEADER_RANGE_SIZE) {
|
|
1145
|
+
response = await this.fetch(this.url, {
|
|
1146
|
+
headers: { Range: `bytes=0-${rangeSize - 1}` }
|
|
1147
|
+
});
|
|
1148
|
+
if (!response.ok) throw new Error(`NetCDF request failed with status ${response.status}.`);
|
|
1149
|
+
arrayBuffer = await response.arrayBuffer();
|
|
1150
|
+
const byteLength = getResponseByteLength(response, arrayBuffer.byteLength);
|
|
1151
|
+
try {
|
|
1152
|
+
return { header: new NetCDFReader(arrayBuffer).header, byteLength };
|
|
1153
|
+
} catch (error) {
|
|
1154
|
+
lastError = error;
|
|
1155
|
+
if (response.status !== 206 || arrayBuffer.byteLength < rangeSize) throw error;
|
|
1156
|
+
rangeSize *= 2;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
throw lastError || new Error("NetCDF header exceeds the maximum metadata range.");
|
|
1160
|
+
}
|
|
1161
|
+
};
|
|
1162
|
+
var NetCDFSourceLoaderWithParser = {
|
|
1163
|
+
dataType: null,
|
|
1164
|
+
batchType: null,
|
|
1165
|
+
...NetCDFFormat,
|
|
1166
|
+
module: "scientific",
|
|
1167
|
+
name: "NetCDFSourceLoader",
|
|
1168
|
+
version: VERSION2,
|
|
1169
|
+
type: "netcdf-source",
|
|
1170
|
+
fromUrl: true,
|
|
1171
|
+
fromBlob: true,
|
|
1172
|
+
options: {},
|
|
1173
|
+
defaultOptions: {},
|
|
1174
|
+
testURL: (url) => /\.(?:cdf|nc)(?:$|[?#])/i.test(url),
|
|
1175
|
+
createDataSource: (data, options) => new NetCDFSource(data, options)
|
|
1176
|
+
};
|
|
1177
|
+
function getNetCDFDataType(type) {
|
|
1178
|
+
switch (type.toLowerCase()) {
|
|
1179
|
+
case "byte":
|
|
1180
|
+
return "int8";
|
|
1181
|
+
case "ubyte":
|
|
1182
|
+
return "uint8";
|
|
1183
|
+
case "short":
|
|
1184
|
+
return "int16";
|
|
1185
|
+
case "ushort":
|
|
1186
|
+
return "uint16";
|
|
1187
|
+
case "int":
|
|
1188
|
+
return "int32";
|
|
1189
|
+
case "uint":
|
|
1190
|
+
return "uint32";
|
|
1191
|
+
case "int64":
|
|
1192
|
+
return "int64";
|
|
1193
|
+
case "uint64":
|
|
1194
|
+
return "uint64";
|
|
1195
|
+
case "float":
|
|
1196
|
+
return "float32";
|
|
1197
|
+
case "double":
|
|
1198
|
+
return "float64";
|
|
1199
|
+
case "char":
|
|
1200
|
+
case "string":
|
|
1201
|
+
return "utf8";
|
|
1202
|
+
default:
|
|
1203
|
+
return "binary";
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
function validateNetCDFUnsupportedRasterOptions(options) {
|
|
1207
|
+
if (options.bounds) throw new Error("NetCDF raster bounds are not supported.");
|
|
1208
|
+
if (options.level !== void 0) throw new Error("NetCDF raster levels are not supported.");
|
|
1209
|
+
if (options.width !== void 0 || options.height !== void 0) {
|
|
1210
|
+
throw new Error("NetCDF raster resampling is not supported.");
|
|
1211
|
+
}
|
|
1212
|
+
if (options.channels?.length) {
|
|
1213
|
+
throw new Error("NetCDF uses named variables instead of numeric channels.");
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
function selectNetCDFVariables(header, requestedVariableNames) {
|
|
1217
|
+
const defaultVariable = header.variables.find(
|
|
1218
|
+
(variable) => variable.dimensions.length > 0 && isNetCDFRasterDataType(variable.type)
|
|
1219
|
+
);
|
|
1220
|
+
const variableNames = requestedVariableNames?.length ? [...requestedVariableNames] : defaultVariable ? [defaultVariable.name] : [];
|
|
1221
|
+
if (variableNames.length === 0) {
|
|
1222
|
+
throw new Error("NetCDF contains no numeric variables that can be read as raster data.");
|
|
1223
|
+
}
|
|
1224
|
+
if (new Set(variableNames).size !== variableNames.length) {
|
|
1225
|
+
throw new Error("NetCDF raster variables must not contain duplicates.");
|
|
1226
|
+
}
|
|
1227
|
+
return variableNames.map((variableName) => {
|
|
1228
|
+
const variable = header.variables.find((candidate) => candidate.name === variableName);
|
|
1229
|
+
if (!variable) throw new Error(`NetCDF variable not found: ${variableName}`);
|
|
1230
|
+
getNetCDFRasterDataType(variable.type);
|
|
1231
|
+
return variable;
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
function validateNetCDFSliceNames(header, slices) {
|
|
1235
|
+
const dimensionNames = new Set(header.dimensions.map((dimension) => dimension.name));
|
|
1236
|
+
for (const dimensionName of Object.keys(slices || {})) {
|
|
1237
|
+
if (!dimensionNames.has(dimensionName)) {
|
|
1238
|
+
throw new Error(`NetCDF dimension not found: ${dimensionName}`);
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
function selectNetCDFValues(values, dimensionNames, dimensionSizes, slices, signal) {
|
|
1243
|
+
const selectors = dimensionNames.map((dimensionName, dimensionIndex) => {
|
|
1244
|
+
const size = dimensionSizes[dimensionIndex];
|
|
1245
|
+
const slice = slices[dimensionName];
|
|
1246
|
+
if (slice === void 0) return { start: 0, stop: size, remove: false };
|
|
1247
|
+
if (typeof slice !== "number") {
|
|
1248
|
+
const [start, stop] = slice;
|
|
1249
|
+
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(stop) || start < 0 || stop < start || stop > size) {
|
|
1250
|
+
throw new Error(
|
|
1251
|
+
`NetCDF slice ${dimensionName} must be a valid half-open range within 0..${size}.`
|
|
1252
|
+
);
|
|
1253
|
+
}
|
|
1254
|
+
return { start, stop, remove: false };
|
|
1255
|
+
}
|
|
1256
|
+
if (!Number.isSafeInteger(slice) || slice < 0 || slice >= size) {
|
|
1257
|
+
throw new Error(`NetCDF slice ${dimensionName} must be an index within 0..${size - 1}.`);
|
|
1258
|
+
}
|
|
1259
|
+
return { start: slice, stop: slice + 1, remove: true };
|
|
1260
|
+
});
|
|
1261
|
+
const strides = dimensionSizes.map(
|
|
1262
|
+
(_, dimensionIndex) => dimensionSizes.slice(dimensionIndex + 1).reduce((product, dimensionSize) => product * dimensionSize, 1)
|
|
1263
|
+
);
|
|
1264
|
+
const selectedValues = [];
|
|
1265
|
+
const visitDimension = (dimensionIndex, flatOffset) => {
|
|
1266
|
+
throwIfAborted(signal);
|
|
1267
|
+
if (dimensionIndex === selectors.length) {
|
|
1268
|
+
const value = values[flatOffset];
|
|
1269
|
+
if (typeof value !== "number") {
|
|
1270
|
+
throw new Error("NetCDF raster variables must contain numeric values.");
|
|
1271
|
+
}
|
|
1272
|
+
selectedValues.push(value);
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1275
|
+
const selector = selectors[dimensionIndex];
|
|
1276
|
+
for (let index = selector.start; index < selector.stop; index++) {
|
|
1277
|
+
visitDimension(dimensionIndex + 1, flatOffset + index * strides[dimensionIndex]);
|
|
1278
|
+
}
|
|
1279
|
+
};
|
|
1280
|
+
visitDimension(0, 0);
|
|
1281
|
+
return {
|
|
1282
|
+
values: selectedValues,
|
|
1283
|
+
dimensionNames: dimensionNames.filter((_, index) => !selectors[index].remove),
|
|
1284
|
+
shape: selectors.map((selector) => selector.stop - selector.start).filter((_, index) => !selectors[index].remove)
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
function flattenNetCDFValues(values) {
|
|
1288
|
+
if (Array.isArray(values)) return values.flatMap(flattenNetCDFValues);
|
|
1289
|
+
if (ArrayBuffer.isView(values)) {
|
|
1290
|
+
return Array.from(values);
|
|
1291
|
+
}
|
|
1292
|
+
return [values];
|
|
1293
|
+
}
|
|
1294
|
+
function isNetCDFRasterDataType(type) {
|
|
1295
|
+
try {
|
|
1296
|
+
getNetCDFRasterDataType(type);
|
|
1297
|
+
return true;
|
|
1298
|
+
} catch {
|
|
1299
|
+
return false;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
function getNetCDFRasterDataType(type) {
|
|
1303
|
+
switch (type.toLowerCase()) {
|
|
1304
|
+
case "byte":
|
|
1305
|
+
return "int8";
|
|
1306
|
+
case "ubyte":
|
|
1307
|
+
return "uint8";
|
|
1308
|
+
case "short":
|
|
1309
|
+
return "int16";
|
|
1310
|
+
case "ushort":
|
|
1311
|
+
return "uint16";
|
|
1312
|
+
case "int":
|
|
1313
|
+
return "int32";
|
|
1314
|
+
case "uint":
|
|
1315
|
+
return "uint32";
|
|
1316
|
+
case "float":
|
|
1317
|
+
return "float32";
|
|
1318
|
+
case "double":
|
|
1319
|
+
return "float64";
|
|
1320
|
+
default:
|
|
1321
|
+
throw new Error(`NetCDF variable type is not supported as raster data: ${type}`);
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
function createNetCDFTypedArray(values, dtype) {
|
|
1325
|
+
switch (dtype) {
|
|
1326
|
+
case "int8":
|
|
1327
|
+
return Int8Array.from(values);
|
|
1328
|
+
case "uint8":
|
|
1329
|
+
return Uint8Array.from(values);
|
|
1330
|
+
case "int16":
|
|
1331
|
+
return Int16Array.from(values);
|
|
1332
|
+
case "uint16":
|
|
1333
|
+
return Uint16Array.from(values);
|
|
1334
|
+
case "int32":
|
|
1335
|
+
return Int32Array.from(values);
|
|
1336
|
+
case "uint32":
|
|
1337
|
+
return Uint32Array.from(values);
|
|
1338
|
+
case "float32":
|
|
1339
|
+
return Float32Array.from(values);
|
|
1340
|
+
case "float64":
|
|
1341
|
+
return Float64Array.from(values);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
function getNetCDFNoDataValue(variable) {
|
|
1345
|
+
const attribute = variable.attributes.find(
|
|
1346
|
+
(candidate) => candidate.name === "_FillValue" || candidate.name === "missing_value"
|
|
1347
|
+
);
|
|
1348
|
+
const value = Number(attribute?.value);
|
|
1349
|
+
return attribute && Number.isFinite(value) ? value : null;
|
|
1350
|
+
}
|
|
1351
|
+
function arraysEqual(left, right) {
|
|
1352
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
1353
|
+
}
|
|
1354
|
+
function createVariableField(variable, header) {
|
|
1355
|
+
const dimensions = variable.dimensions.map((dimensionId) => header.dimensions[dimensionId]).filter(Boolean).map(
|
|
1356
|
+
(dimension, index) => `${dimension.name}[${getDimensionSize(header, variable.dimensions[index])}]`
|
|
1357
|
+
).join(",");
|
|
1358
|
+
const metadata = { dimensions };
|
|
1359
|
+
for (const attribute of variable.attributes) {
|
|
1360
|
+
metadata[attribute.name] = String(attribute.value);
|
|
1361
|
+
}
|
|
1362
|
+
return { name: variable.name, type: getNetCDFDataType(variable.type), nullable: true, metadata };
|
|
1363
|
+
}
|
|
1364
|
+
function createDimensionMetadata(header) {
|
|
1365
|
+
const metadata = {};
|
|
1366
|
+
for (const [dimensionId, dimension] of header.dimensions.entries()) {
|
|
1367
|
+
metadata[`dimension:${dimension.name}`] = String(getDimensionSize(header, dimensionId));
|
|
1368
|
+
}
|
|
1369
|
+
return metadata;
|
|
1370
|
+
}
|
|
1371
|
+
function getDimensionSize(header, dimensionId) {
|
|
1372
|
+
const dimension = header.dimensions[dimensionId];
|
|
1373
|
+
return dimensionId === header.recordDimension.id ? header.recordDimension.length : dimension?.size || 0;
|
|
1374
|
+
}
|
|
1375
|
+
function getResponseByteLength(response, fallback) {
|
|
1376
|
+
const contentRange = response.headers.get("content-range");
|
|
1377
|
+
const total = contentRange?.match(/\/([0-9]+)$/)?.[1];
|
|
1378
|
+
return Number(total || response.headers.get("content-length") || fallback);
|
|
1379
|
+
}
|
|
1380
|
+
function throwIfAborted(signal) {
|
|
1381
|
+
if (signal?.aborted) {
|
|
1382
|
+
throw new DOMException("The operation was aborted", "AbortError");
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1386
|
+
0 && (module.exports = {
|
|
1387
|
+
NetCDFFormat,
|
|
1388
|
+
NetCDFLoaderWithParser,
|
|
1389
|
+
NetCDFReader,
|
|
1390
|
+
NetCDFSource,
|
|
1391
|
+
NetCDFSourceLoader,
|
|
1392
|
+
NetCDFSourceLoaderWithParser,
|
|
1393
|
+
NetCDFWorkerLoaderWithParser
|
|
1394
|
+
});
|