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