@loaders.gl/netcdf 4.0.0-beta.2 → 4.0.0-beta.3

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.
Files changed (46) hide show
  1. package/dist/dist.dev.js +943 -0
  2. package/dist/index.cjs +919 -0
  3. package/dist/index.js +3 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/{esm/iobuffer → iobuffer}/iobuffer.js +12 -13
  6. package/dist/iobuffer/iobuffer.js.map +1 -0
  7. package/dist/{esm/netcdf-loader.js → netcdf-loader.js} +2 -2
  8. package/dist/netcdf-loader.js.map +1 -0
  9. package/dist/{esm/netcdfjs → netcdfjs}/netcdf-reader.js +12 -13
  10. package/dist/netcdfjs/netcdf-reader.js.map +1 -0
  11. package/dist/netcdfjs/netcdf-types.js.map +1 -0
  12. package/dist/{esm/netcdfjs → netcdfjs}/read-data.js +1 -1
  13. package/dist/netcdfjs/read-data.js.map +1 -0
  14. package/dist/{esm/netcdfjs → netcdfjs}/read-header.js +3 -3
  15. package/dist/netcdfjs/read-header.js.map +1 -0
  16. package/dist/{esm/netcdfjs → netcdfjs}/read-type.js +1 -1
  17. package/dist/netcdfjs/read-type.js.map +1 -0
  18. package/package.json +15 -7
  19. package/dist/es5/index.js +0 -20
  20. package/dist/es5/index.js.map +0 -1
  21. package/dist/es5/iobuffer/iobuffer.js +0 -369
  22. package/dist/es5/iobuffer/iobuffer.js.map +0 -1
  23. package/dist/es5/netcdf-loader.js +0 -77
  24. package/dist/es5/netcdf-loader.js.map +0 -1
  25. package/dist/es5/netcdfjs/netcdf-reader.js +0 -173
  26. package/dist/es5/netcdfjs/netcdf-reader.js.map +0 -1
  27. package/dist/es5/netcdfjs/netcdf-types.js +0 -2
  28. package/dist/es5/netcdfjs/netcdf-types.js.map +0 -1
  29. package/dist/es5/netcdfjs/read-data.js +0 -31
  30. package/dist/es5/netcdfjs/read-data.js.map +0 -1
  31. package/dist/es5/netcdfjs/read-header.js +0 -168
  32. package/dist/es5/netcdfjs/read-header.js.map +0 -1
  33. package/dist/es5/netcdfjs/read-type.js +0 -108
  34. package/dist/es5/netcdfjs/read-type.js.map +0 -1
  35. package/dist/esm/index.js +0 -3
  36. package/dist/esm/index.js.map +0 -1
  37. package/dist/esm/iobuffer/iobuffer.js.map +0 -1
  38. package/dist/esm/netcdf-loader.js.map +0 -1
  39. package/dist/esm/netcdfjs/LICENSE +0 -24
  40. package/dist/esm/netcdfjs/netcdf-reader.js.map +0 -1
  41. package/dist/esm/netcdfjs/netcdf-types.js.map +0 -1
  42. package/dist/esm/netcdfjs/read-data.js.map +0 -1
  43. package/dist/esm/netcdfjs/read-header.js.map +0 -1
  44. package/dist/esm/netcdfjs/read-type.js.map +0 -1
  45. /package/dist/{es5/netcdfjs → netcdfjs}/LICENSE +0 -0
  46. /package/dist/{esm/netcdfjs → netcdfjs}/netcdf-types.js +0 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,919 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ NetCDFLoader: () => NetCDFLoader,
24
+ NetCDFReader: () => NetCDFReader
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/iobuffer/iobuffer.ts
29
+ var DEFAULT_BYTE_LENGTH = 1024 * 8;
30
+ var IOBuffer = class {
31
+ /**
32
+ * @param data - The data to construct the IOBuffer with.
33
+ * If data is a number, it will be the new buffer's length<br>
34
+ * If data is `undefined`, the buffer will be initialized with a default length of 8Kb<br>
35
+ * If data is an ArrayBuffer, SharedArrayBuffer, an ArrayBufferView (Typed Array), an IOBuffer instance,
36
+ * or a Node.js Buffer, a view will be created over the underlying ArrayBuffer.
37
+ * @param options
38
+ */
39
+ constructor(data = DEFAULT_BYTE_LENGTH, options = {}) {
40
+ this.textDecoder = new TextDecoder();
41
+ this.textEncoder = new TextEncoder();
42
+ let dataIsGiven = false;
43
+ if (typeof data === "number") {
44
+ data = new ArrayBuffer(data);
45
+ } else {
46
+ dataIsGiven = true;
47
+ this.lastWrittenByte = data.byteLength;
48
+ }
49
+ const offset = options.offset ? options.offset >>> 0 : 0;
50
+ const byteLength = data.byteLength - offset;
51
+ let dvOffset = offset;
52
+ if (ArrayBuffer.isView(data) || data instanceof IOBuffer) {
53
+ if (data.byteLength !== data.buffer.byteLength) {
54
+ dvOffset = data.byteOffset + offset;
55
+ }
56
+ data = data.buffer;
57
+ }
58
+ if (dataIsGiven) {
59
+ this.lastWrittenByte = byteLength;
60
+ } else {
61
+ this.lastWrittenByte = 0;
62
+ }
63
+ this.buffer = data;
64
+ this.length = byteLength;
65
+ this.byteLength = byteLength;
66
+ this.byteOffset = dvOffset;
67
+ this.offset = 0;
68
+ this.littleEndian = true;
69
+ this._data = new DataView(this.buffer, dvOffset, byteLength);
70
+ this._mark = 0;
71
+ this._marks = [];
72
+ }
73
+ /**
74
+ * Checks if the memory allocated to the buffer is sufficient to store more
75
+ * bytes after the offset.
76
+ * @param byteLength - The needed memory in bytes.
77
+ * @returns `true` if there is sufficient space and `false` otherwise.
78
+ */
79
+ available(byteLength = 1) {
80
+ return this.offset + byteLength <= this.length;
81
+ }
82
+ /**
83
+ * Check if little-endian mode is used for reading and writing multi-byte
84
+ * values.
85
+ * @returns `true` if little-endian mode is used, `false` otherwise.
86
+ */
87
+ isLittleEndian() {
88
+ return this.littleEndian;
89
+ }
90
+ /**
91
+ * Set little-endian mode for reading and writing multi-byte values.
92
+ */
93
+ setLittleEndian() {
94
+ this.littleEndian = true;
95
+ return this;
96
+ }
97
+ /**
98
+ * Check if big-endian mode is used for reading and writing multi-byte values.
99
+ * @returns `true` if big-endian mode is used, `false` otherwise.
100
+ */
101
+ isBigEndian() {
102
+ return !this.littleEndian;
103
+ }
104
+ /**
105
+ * Switches to big-endian mode for reading and writing multi-byte values.
106
+ */
107
+ setBigEndian() {
108
+ this.littleEndian = false;
109
+ return this;
110
+ }
111
+ /**
112
+ * Move the pointer n bytes forward.
113
+ * @param n - Number of bytes to skip.
114
+ */
115
+ skip(n = 1) {
116
+ this.offset += n;
117
+ return this;
118
+ }
119
+ /**
120
+ * Move the pointer to the given offset.
121
+ * @param offset
122
+ */
123
+ seek(offset) {
124
+ this.offset = offset;
125
+ return this;
126
+ }
127
+ /**
128
+ * Store the current pointer offset.
129
+ * @see {@link IOBuffer#reset}
130
+ */
131
+ mark() {
132
+ this._mark = this.offset;
133
+ return this;
134
+ }
135
+ /**
136
+ * Move the pointer back to the last pointer offset set by mark.
137
+ * @see {@link IOBuffer#mark}
138
+ */
139
+ reset() {
140
+ this.offset = this._mark;
141
+ return this;
142
+ }
143
+ /**
144
+ * Push the current pointer offset to the mark stack.
145
+ * @see {@link IOBuffer#popMark}
146
+ */
147
+ pushMark() {
148
+ this._marks.push(this.offset);
149
+ return this;
150
+ }
151
+ /**
152
+ * Pop the last pointer offset from the mark stack, and set the current
153
+ * pointer offset to the popped value.
154
+ * @see {@link IOBuffer#pushMark}
155
+ */
156
+ popMark() {
157
+ const offset = this._marks.pop();
158
+ if (offset === void 0) {
159
+ throw new Error("Mark stack empty");
160
+ }
161
+ this.seek(offset);
162
+ return this;
163
+ }
164
+ /**
165
+ * Move the pointer offset back to 0.
166
+ */
167
+ rewind() {
168
+ this.offset = 0;
169
+ return this;
170
+ }
171
+ /**
172
+ * Make sure the buffer has sufficient memory to write a given byteLength at
173
+ * the current pointer offset.
174
+ * If the buffer's memory is insufficient, this method will create a new
175
+ * buffer (a copy) with a length that is twice (byteLength + current offset).
176
+ * @param byteLength
177
+ */
178
+ ensureAvailable(byteLength = 1) {
179
+ if (!this.available(byteLength)) {
180
+ const lengthNeeded = this.offset + byteLength;
181
+ const newLength = lengthNeeded * 2;
182
+ const newArray = new Uint8Array(newLength);
183
+ newArray.set(new Uint8Array(this.buffer));
184
+ this.buffer = newArray.buffer;
185
+ this.length = this.byteLength = newLength;
186
+ this._data = new DataView(this.buffer);
187
+ }
188
+ return this;
189
+ }
190
+ /**
191
+ * Read a byte and return false if the byte's value is 0, or true otherwise.
192
+ * Moves pointer forward by one byte.
193
+ */
194
+ readBoolean() {
195
+ return this.readUint8() !== 0;
196
+ }
197
+ /**
198
+ * Read a signed 8-bit integer and move pointer forward by 1 byte.
199
+ */
200
+ readInt8() {
201
+ return this._data.getInt8(this.offset++);
202
+ }
203
+ /**
204
+ * Read an unsigned 8-bit integer and move pointer forward by 1 byte.
205
+ */
206
+ readUint8() {
207
+ return this._data.getUint8(this.offset++);
208
+ }
209
+ /**
210
+ * Alias for {@link IOBuffer#readUint8}.
211
+ */
212
+ readByte() {
213
+ return this.readUint8();
214
+ }
215
+ /**
216
+ * Read `n` bytes and move pointer forward by `n` bytes.
217
+ */
218
+ readBytes(n = 1) {
219
+ const bytes = new Uint8Array(n);
220
+ for (let i = 0; i < n; i++) {
221
+ bytes[i] = this.readByte();
222
+ }
223
+ return bytes;
224
+ }
225
+ /**
226
+ * Read a 16-bit signed integer and move pointer forward by 2 bytes.
227
+ */
228
+ readInt16() {
229
+ const value = this._data.getInt16(this.offset, this.littleEndian);
230
+ this.offset += 2;
231
+ return value;
232
+ }
233
+ /**
234
+ * Read a 16-bit unsigned integer and move pointer forward by 2 bytes.
235
+ */
236
+ readUint16() {
237
+ const value = this._data.getUint16(this.offset, this.littleEndian);
238
+ this.offset += 2;
239
+ return value;
240
+ }
241
+ /**
242
+ * Read a 32-bit signed integer and move pointer forward by 4 bytes.
243
+ */
244
+ readInt32() {
245
+ const value = this._data.getInt32(this.offset, this.littleEndian);
246
+ this.offset += 4;
247
+ return value;
248
+ }
249
+ /**
250
+ * Read a 32-bit unsigned integer and move pointer forward by 4 bytes.
251
+ */
252
+ readUint32() {
253
+ const value = this._data.getUint32(this.offset, this.littleEndian);
254
+ this.offset += 4;
255
+ return value;
256
+ }
257
+ /**
258
+ * Read a 32-bit floating number and move pointer forward by 4 bytes.
259
+ */
260
+ readFloat32() {
261
+ const value = this._data.getFloat32(this.offset, this.littleEndian);
262
+ this.offset += 4;
263
+ return value;
264
+ }
265
+ /**
266
+ * Read a 64-bit floating number and move pointer forward by 8 bytes.
267
+ */
268
+ readFloat64() {
269
+ const value = this._data.getFloat64(this.offset, this.littleEndian);
270
+ this.offset += 8;
271
+ return value;
272
+ }
273
+ /**
274
+ * Read a 1-byte ASCII character and move pointer forward by 1 byte.
275
+ */
276
+ readChar() {
277
+ return String.fromCharCode(this.readInt8());
278
+ }
279
+ /**
280
+ * Read `n` 1-byte ASCII characters and move pointer forward by `n` bytes.
281
+ */
282
+ readChars(n = 1) {
283
+ let result = "";
284
+ for (let i = 0; i < n; i++) {
285
+ result += this.readChar();
286
+ }
287
+ return result;
288
+ }
289
+ /**
290
+ * Read the next `n` bytes, return a UTF-8 decoded string and move pointer
291
+ * forward by `n` bytes.
292
+ */
293
+ readUtf8(n = 1) {
294
+ return this.textDecoder.decode(this.readBytes(n));
295
+ }
296
+ /**
297
+ * Write 0xff if the passed value is truthy, 0x00 otherwise and move pointer
298
+ * forward by 1 byte.
299
+ */
300
+ writeBoolean(value) {
301
+ this.writeUint8(value ? 255 : 0);
302
+ return this;
303
+ }
304
+ /**
305
+ * Write `value` as an 8-bit signed integer and move pointer forward by 1 byte.
306
+ */
307
+ writeInt8(value) {
308
+ this.ensureAvailable(1);
309
+ this._data.setInt8(this.offset++, value);
310
+ this._updateLastWrittenByte();
311
+ return this;
312
+ }
313
+ /**
314
+ * Write `value` as an 8-bit unsigned integer and move pointer forward by 1
315
+ * byte.
316
+ */
317
+ writeUint8(value) {
318
+ this.ensureAvailable(1);
319
+ this._data.setUint8(this.offset++, value);
320
+ this._updateLastWrittenByte();
321
+ return this;
322
+ }
323
+ /**
324
+ * An alias for {@link IOBuffer#writeUint8}.
325
+ */
326
+ writeByte(value) {
327
+ return this.writeUint8(value);
328
+ }
329
+ /**
330
+ * Write all elements of `bytes` as uint8 values and move pointer forward by
331
+ * `bytes.length` bytes.
332
+ */
333
+ writeBytes(bytes) {
334
+ this.ensureAvailable(bytes.length);
335
+ for (let i = 0; i < bytes.length; i++) {
336
+ this._data.setUint8(this.offset++, bytes[i]);
337
+ }
338
+ this._updateLastWrittenByte();
339
+ return this;
340
+ }
341
+ /**
342
+ * Write `value` as a 16-bit signed integer and move pointer forward by 2
343
+ * bytes.
344
+ */
345
+ writeInt16(value) {
346
+ this.ensureAvailable(2);
347
+ this._data.setInt16(this.offset, value, this.littleEndian);
348
+ this.offset += 2;
349
+ this._updateLastWrittenByte();
350
+ return this;
351
+ }
352
+ /**
353
+ * Write `value` as a 16-bit unsigned integer and move pointer forward by 2
354
+ * bytes.
355
+ */
356
+ writeUint16(value) {
357
+ this.ensureAvailable(2);
358
+ this._data.setUint16(this.offset, value, this.littleEndian);
359
+ this.offset += 2;
360
+ this._updateLastWrittenByte();
361
+ return this;
362
+ }
363
+ /**
364
+ * Write `value` as a 32-bit signed integer and move pointer forward by 4
365
+ * bytes.
366
+ */
367
+ writeInt32(value) {
368
+ this.ensureAvailable(4);
369
+ this._data.setInt32(this.offset, value, this.littleEndian);
370
+ this.offset += 4;
371
+ this._updateLastWrittenByte();
372
+ return this;
373
+ }
374
+ /**
375
+ * Write `value` as a 32-bit unsigned integer and move pointer forward by 4
376
+ * bytes.
377
+ */
378
+ writeUint32(value) {
379
+ this.ensureAvailable(4);
380
+ this._data.setUint32(this.offset, value, this.littleEndian);
381
+ this.offset += 4;
382
+ this._updateLastWrittenByte();
383
+ return this;
384
+ }
385
+ /**
386
+ * Write `value` as a 32-bit floating number and move pointer forward by 4
387
+ * bytes.
388
+ */
389
+ writeFloat32(value) {
390
+ this.ensureAvailable(4);
391
+ this._data.setFloat32(this.offset, value, this.littleEndian);
392
+ this.offset += 4;
393
+ this._updateLastWrittenByte();
394
+ return this;
395
+ }
396
+ /**
397
+ * Write `value` as a 64-bit floating number and move pointer forward by 8
398
+ * bytes.
399
+ */
400
+ writeFloat64(value) {
401
+ this.ensureAvailable(8);
402
+ this._data.setFloat64(this.offset, value, this.littleEndian);
403
+ this.offset += 8;
404
+ this._updateLastWrittenByte();
405
+ return this;
406
+ }
407
+ /**
408
+ * Write the charCode of `str`'s first character as an 8-bit unsigned integer
409
+ * and move pointer forward by 1 byte.
410
+ */
411
+ writeChar(str) {
412
+ return this.writeUint8(str.charCodeAt(0));
413
+ }
414
+ /**
415
+ * Write the charCodes of all `str`'s characters as 8-bit unsigned integers
416
+ * and move pointer forward by `str.length` bytes.
417
+ */
418
+ writeChars(str) {
419
+ for (let i = 0; i < str.length; i++) {
420
+ this.writeUint8(str.charCodeAt(i));
421
+ }
422
+ return this;
423
+ }
424
+ /**
425
+ * UTF-8 encode and write `str` to the current pointer offset and move pointer
426
+ * forward according to the encoded length.
427
+ */
428
+ writeUtf8(str) {
429
+ const bytes = this.textEncoder.encode(str);
430
+ return this.writeBytes(bytes);
431
+ }
432
+ /**
433
+ * Export a Uint8Array view of the internal buffer.
434
+ * The view starts at the byte offset and its length
435
+ * is calculated to stop at the last written byte or the original length.
436
+ */
437
+ toArray() {
438
+ return new Uint8Array(this.buffer, this.byteOffset, this.lastWrittenByte);
439
+ }
440
+ /**
441
+ * Update the last written byte offset
442
+ * @private
443
+ */
444
+ _updateLastWrittenByte() {
445
+ if (this.offset > this.lastWrittenByte) {
446
+ this.lastWrittenByte = this.offset;
447
+ }
448
+ }
449
+ };
450
+
451
+ // src/netcdfjs/read-type.ts
452
+ var TYPES = {
453
+ BYTE: 1,
454
+ CHAR: 2,
455
+ SHORT: 3,
456
+ INT: 4,
457
+ FLOAT: 5,
458
+ DOUBLE: 6
459
+ };
460
+ function readType(buffer, type, size) {
461
+ switch (type) {
462
+ case TYPES.BYTE:
463
+ return buffer.readBytes(size);
464
+ case TYPES.CHAR:
465
+ return trimNull(buffer.readChars(size));
466
+ case TYPES.SHORT:
467
+ return readNumber(size, buffer.readInt16.bind(buffer));
468
+ case TYPES.INT:
469
+ return readNumber(size, buffer.readInt32.bind(buffer));
470
+ case TYPES.FLOAT:
471
+ return readNumber(size, buffer.readFloat32.bind(buffer));
472
+ case TYPES.DOUBLE:
473
+ return readNumber(size, buffer.readFloat64.bind(buffer));
474
+ default:
475
+ throw new Error(`NetCDF: non valid type ${type}`);
476
+ }
477
+ }
478
+ function num2str(type) {
479
+ switch (Number(type)) {
480
+ case TYPES.BYTE:
481
+ return "byte";
482
+ case TYPES.CHAR:
483
+ return "char";
484
+ case TYPES.SHORT:
485
+ return "short";
486
+ case TYPES.INT:
487
+ return "int";
488
+ case TYPES.FLOAT:
489
+ return "float";
490
+ case TYPES.DOUBLE:
491
+ return "double";
492
+ default:
493
+ return "undefined";
494
+ }
495
+ }
496
+ function num2bytes(type) {
497
+ switch (Number(type)) {
498
+ case TYPES.BYTE:
499
+ return 1;
500
+ case TYPES.CHAR:
501
+ return 1;
502
+ case TYPES.SHORT:
503
+ return 2;
504
+ case TYPES.INT:
505
+ return 4;
506
+ case TYPES.FLOAT:
507
+ return 4;
508
+ case TYPES.DOUBLE:
509
+ return 8;
510
+ default:
511
+ return -1;
512
+ }
513
+ }
514
+ function str2num(type) {
515
+ switch (String(type)) {
516
+ case "byte":
517
+ return TYPES.BYTE;
518
+ case "char":
519
+ return TYPES.CHAR;
520
+ case "short":
521
+ return TYPES.SHORT;
522
+ case "int":
523
+ return TYPES.INT;
524
+ case "float":
525
+ return TYPES.FLOAT;
526
+ case "double":
527
+ return TYPES.DOUBLE;
528
+ default:
529
+ return -1;
530
+ }
531
+ }
532
+ function readNumber(size, bufferReader) {
533
+ if (size !== 1) {
534
+ const numbers = new Array(size);
535
+ for (let i = 0; i < size; i++) {
536
+ numbers[i] = bufferReader();
537
+ }
538
+ return numbers;
539
+ }
540
+ return bufferReader();
541
+ }
542
+ function trimNull(value) {
543
+ if (value.charCodeAt(value.length - 1) === 0) {
544
+ return value.substring(0, value.length - 1);
545
+ }
546
+ return value;
547
+ }
548
+
549
+ // src/netcdfjs/read-header.ts
550
+ var ZERO = 0;
551
+ var NC_DIMENSION = 10;
552
+ var NC_VARIABLE = 11;
553
+ var NC_ATTRIBUTE = 12;
554
+ var NC_UNLIMITED = 0;
555
+ function readNetCDFHeader(buffer, version) {
556
+ const recordDimensionLength = buffer.readUint32();
557
+ const dimList = readDimensionsList(buffer);
558
+ const attributes = readAttributesList(buffer);
559
+ const variableList = readVariablesList(buffer, dimList.recordId, version);
560
+ const header = {
561
+ version,
562
+ recordDimension: {
563
+ length: recordDimensionLength,
564
+ id: dimList.recordId,
565
+ // id of the unlimited dimension
566
+ name: dimList.recordName,
567
+ // name of the unlimited dimension
568
+ recordStep: variableList.recordStep
569
+ },
570
+ dimensions: dimList.dimensions,
571
+ variables: variableList.variables,
572
+ attributes
573
+ };
574
+ return header;
575
+ }
576
+ function readDimensionsList(buffer) {
577
+ const dimList = buffer.readUint32();
578
+ if (dimList === ZERO) {
579
+ if (buffer.readUint32() !== ZERO) {
580
+ throw new Error("NetCDF: wrong empty tag for list of dimensions");
581
+ }
582
+ return {
583
+ recordId: 0,
584
+ recordName: "",
585
+ dimensions: []
586
+ };
587
+ }
588
+ if (dimList !== NC_DIMENSION) {
589
+ throw new Error("NetCDF: wrong tag for list of dimensions");
590
+ }
591
+ const dimensionSize = buffer.readUint32();
592
+ const dimensions = new Array(dimensionSize);
593
+ let recordId;
594
+ let recordName;
595
+ for (let dim = 0; dim < dimensionSize; dim++) {
596
+ const name = readName(buffer);
597
+ const size = buffer.readUint32();
598
+ if (size === NC_UNLIMITED) {
599
+ recordId = dim;
600
+ recordName = name;
601
+ }
602
+ dimensions[dim] = {
603
+ name,
604
+ size
605
+ };
606
+ }
607
+ return {
608
+ dimensions,
609
+ recordId,
610
+ recordName
611
+ };
612
+ }
613
+ function readAttributesList(buffer) {
614
+ const gAttList = buffer.readUint32();
615
+ if (gAttList === ZERO) {
616
+ if (buffer.readUint32() !== ZERO) {
617
+ throw new Error("NetCDF: wrong empty tag for list of attributes");
618
+ }
619
+ return [];
620
+ }
621
+ if (gAttList !== NC_ATTRIBUTE) {
622
+ throw new Error("NetCDF: wrong tag for list of attributes");
623
+ }
624
+ const attributeSize = buffer.readUint32();
625
+ const attributes = new Array(attributeSize);
626
+ for (let gAtt = 0; gAtt < attributeSize; gAtt++) {
627
+ const name = readName(buffer);
628
+ const type = buffer.readUint32();
629
+ if (type < 1 || type > 6) {
630
+ throw new Error(`NetCDF: non valid type ${type}`);
631
+ }
632
+ const size = buffer.readUint32();
633
+ const value = readType(buffer, type, size);
634
+ padding(buffer);
635
+ attributes[gAtt] = {
636
+ name,
637
+ type: num2str(type),
638
+ value
639
+ };
640
+ }
641
+ return attributes;
642
+ }
643
+ function readVariablesList(buffer, recordId, version) {
644
+ const varList = buffer.readUint32();
645
+ let recordStep = 0;
646
+ if (varList === ZERO) {
647
+ if (buffer.readUint32() !== ZERO) {
648
+ throw new Error("NetCDF: wrong empty tag for list of variables");
649
+ }
650
+ return {
651
+ recordStep,
652
+ variables: []
653
+ };
654
+ }
655
+ if (varList !== NC_VARIABLE) {
656
+ throw new Error("NetCDF: wrong tag for list of variables");
657
+ }
658
+ const variableSize = buffer.readUint32();
659
+ const variables = new Array(variableSize);
660
+ for (let v = 0; v < variableSize; v++) {
661
+ const name = readName(buffer);
662
+ const dimensionality = buffer.readUint32();
663
+ const dimensionsIds = new Array(dimensionality);
664
+ for (let dim = 0; dim < dimensionality; dim++) {
665
+ dimensionsIds[dim] = buffer.readUint32();
666
+ }
667
+ const attributes = readAttributesList(buffer);
668
+ const type = buffer.readUint32();
669
+ if (type < 1 && type > 6) {
670
+ throw new Error(`NetCDF: non valid type ${type}`);
671
+ }
672
+ const varSize = buffer.readUint32();
673
+ let offset = buffer.readUint32();
674
+ if (version === 2) {
675
+ if (offset > 0) {
676
+ throw new Error("NetCDF: offsets larger than 4GB not supported");
677
+ }
678
+ offset = buffer.readUint32();
679
+ }
680
+ let record = false;
681
+ if (typeof recordId !== "undefined" && dimensionsIds[0] === recordId) {
682
+ recordStep += varSize;
683
+ record = true;
684
+ }
685
+ variables[v] = {
686
+ name,
687
+ dimensions: dimensionsIds,
688
+ attributes,
689
+ type: num2str(type),
690
+ size: varSize,
691
+ offset,
692
+ record
693
+ };
694
+ }
695
+ return {
696
+ variables,
697
+ recordStep
698
+ };
699
+ }
700
+ function readName(buffer) {
701
+ const nameLength = buffer.readUint32();
702
+ const name = buffer.readChars(nameLength);
703
+ padding(buffer);
704
+ return name;
705
+ }
706
+ function padding(buffer) {
707
+ if (buffer.offset % 4 !== 0) {
708
+ buffer.skip(4 - buffer.offset % 4);
709
+ }
710
+ }
711
+
712
+ // src/netcdfjs/read-data.ts
713
+ function readNonRecord(buffer, variable) {
714
+ const type = str2num(variable.type);
715
+ const size = variable.size / num2bytes(type);
716
+ const data = new Array(size);
717
+ for (let i = 0; i < size; i++) {
718
+ data[i] = readType(buffer, type, 1);
719
+ }
720
+ return data;
721
+ }
722
+ function readRecord(buffer, variable, recordDimension) {
723
+ const type = str2num(variable.type);
724
+ const width = variable.size ? variable.size / num2bytes(type) : 1;
725
+ const size = recordDimension.length;
726
+ const data = new Array(size);
727
+ const step = recordDimension.recordStep;
728
+ for (let i = 0; i < size; i++) {
729
+ const currentOffset = buffer.offset;
730
+ data[i] = readType(buffer, type, width);
731
+ buffer.seek(currentOffset + step);
732
+ }
733
+ return data;
734
+ }
735
+
736
+ // src/netcdfjs/netcdf-reader.ts
737
+ var NetCDFReader = class {
738
+ constructor(data) {
739
+ const buffer = new IOBuffer(data);
740
+ buffer.setBigEndian();
741
+ const magic = buffer.readChars(3);
742
+ if (magic !== "CDF") {
743
+ throw new Error(`NetCDF: file should start with 'CDF', found ${magic}`);
744
+ }
745
+ const version = buffer.readByte();
746
+ if (version > 2) {
747
+ throw new Error(`NetCDF: unsupported version ${version}`);
748
+ }
749
+ this.header = readNetCDFHeader(buffer, version);
750
+ this.buffer = buffer;
751
+ }
752
+ /**
753
+ * @return {string} - Version for the NetCDF format
754
+ */
755
+ get version() {
756
+ if (this.header.version === 1) {
757
+ return "classic format";
758
+ }
759
+ return "64-bit offset format";
760
+ }
761
+ /**
762
+ * Get metadata for the record dimension
763
+ */
764
+ get recordDimension() {
765
+ return this.header.recordDimension;
766
+ }
767
+ /**
768
+ * Get list of dimensions (each with `name` and `size`)
769
+ */
770
+ get dimensions() {
771
+ return this.header.dimensions;
772
+ }
773
+ /**
774
+ * Get list of global attributes with:
775
+ * * `name`: String with the name of the attribute
776
+ * * `type`: String with the type of the attribute
777
+ * * `value`: A number or string with the value of the attribute
778
+ */
779
+ get attributes() {
780
+ return this.header.attributes;
781
+ }
782
+ /**
783
+ * Get list of variables
784
+ */
785
+ get variables() {
786
+ return this.header.variables;
787
+ }
788
+ /**
789
+ * Check if an attribute exists
790
+ * @param attributeName - Name of the attribute to find
791
+ * @return
792
+ */
793
+ attributeExists(attributeName) {
794
+ const attribute = this.attributes.find((val) => val.name === attributeName);
795
+ return attribute !== void 0;
796
+ }
797
+ /**
798
+ * Returns the value of an attribute
799
+ * @param attributeName
800
+ * @return Value of the attributeName or null
801
+ */
802
+ getAttribute(attributeName) {
803
+ const attribute = this.attributes.find((val) => val.name === attributeName);
804
+ if (attribute)
805
+ return attribute.value;
806
+ return null;
807
+ }
808
+ /**
809
+ * Check if a dataVariable exists
810
+ * @param variableName - Name of the variable to find
811
+ * @return
812
+ */
813
+ dataVariableExists(variableName) {
814
+ const variable = this.header.variables.find(function(val) {
815
+ return val.name === variableName;
816
+ });
817
+ return variable !== void 0;
818
+ }
819
+ /**
820
+ * Returns the value of a variable as a string
821
+ * @param variableName
822
+ * @return Value of the variable as a string or null
823
+ */
824
+ getDataVariableAsString(variableName) {
825
+ const variable = this.getDataVariable(variableName);
826
+ if (variable)
827
+ return variable.join("");
828
+ return null;
829
+ }
830
+ /**
831
+ * Retrieves the data for a given variable
832
+ * @param variableName - Name of the variable to search or variable object
833
+ * @return List with the variable values
834
+ */
835
+ getDataVariable(variableName) {
836
+ let variable;
837
+ if (typeof variableName === "string") {
838
+ variable = this.header.variables.find(function(val) {
839
+ return val.name === variableName;
840
+ });
841
+ } else {
842
+ variable = variableName;
843
+ }
844
+ if (variable === void 0) {
845
+ throw new Error(`NetCDF: variable not found: ${variableName}`);
846
+ }
847
+ this.buffer.seek(variable.offset);
848
+ if (variable.record) {
849
+ return readRecord(this.buffer, variable, this.header.recordDimension);
850
+ }
851
+ return readNonRecord(this.buffer, variable);
852
+ }
853
+ toString() {
854
+ const result = [];
855
+ result.push("DIMENSIONS");
856
+ for (const dimension of this.dimensions) {
857
+ result.push(` ${dimension.name.padEnd(30)} = size: ${dimension.size}`);
858
+ }
859
+ result.push("");
860
+ result.push("GLOBAL ATTRIBUTES");
861
+ for (const attribute of this.attributes) {
862
+ result.push(` ${attribute.name.padEnd(30)} = ${attribute.value}`);
863
+ }
864
+ const variables = JSON.parse(JSON.stringify(this.variables));
865
+ result.push("");
866
+ result.push("VARIABLES:");
867
+ for (const variable of variables) {
868
+ variable.value = this.getDataVariable(variable);
869
+ let stringify = JSON.stringify(variable.value);
870
+ if (stringify.length > 50)
871
+ stringify = stringify.substring(0, 50);
872
+ if (!isNaN(variable.value.length)) {
873
+ stringify += ` (length: ${variable.value.length})`;
874
+ }
875
+ result.push(` ${variable.name.padEnd(30)} = ${stringify}`);
876
+ }
877
+ return result.join("\n");
878
+ }
879
+ };
880
+
881
+ // src/netcdf-loader.ts
882
+ var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
883
+ var NetCDFWorkerLoader = {
884
+ name: "NetCDF",
885
+ id: "mvt",
886
+ module: "mvt",
887
+ version: VERSION,
888
+ extensions: ["cdf", "nc"],
889
+ mimeTypes: [
890
+ "application/netcdf",
891
+ "application/x-netcdf"
892
+ // 'application/octet-stream'
893
+ ],
894
+ category: "image",
895
+ options: {
896
+ netcdf: {
897
+ loadVariables: false
898
+ }
899
+ }
900
+ };
901
+ var NetCDFLoader = {
902
+ ...NetCDFWorkerLoader,
903
+ parse: async (arrayBuffer, options) => parseNetCDF(arrayBuffer, options),
904
+ binary: true
905
+ };
906
+ function parseNetCDF(arrayBuffer, options) {
907
+ var _a;
908
+ const reader = new NetCDFReader(arrayBuffer);
909
+ const variables = {};
910
+ if ((_a = options == null ? void 0 : options.netcdf) == null ? void 0 : _a.loadData) {
911
+ for (const variable of reader.variables) {
912
+ variables[variable.name] = reader.getDataVariable(variable);
913
+ }
914
+ }
915
+ return {
916
+ loaderData: reader.header,
917
+ data: variables
918
+ };
919
+ }