@earthyscience/netcdf4-wasm 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +29 -22
- package/README.md +12 -10
- package/dist/constants.d.ts +165 -146
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +48 -60
- package/dist/constants.js.map +1 -1
- package/dist/dimension.js +1 -1
- package/dist/errors.d.ts +3 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/group.d.ts +4 -5
- package/dist/group.d.ts.map +1 -1
- package/dist/group.js +11 -70
- package/dist/group.js.map +1 -1
- package/dist/index.d.ts +15 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -14
- package/dist/index.js.map +1 -1
- package/dist/netcdf-getters.d.ts +5 -5
- package/dist/netcdf-getters.d.ts.map +1 -1
- package/dist/netcdf-getters.js +78 -50
- package/dist/netcdf-getters.js.map +1 -1
- package/dist/netcdf-worker.js +15 -105
- package/dist/netcdf-worker.js.map +1 -1
- package/dist/netcdf4-wasm.js +2 -2
- package/dist/netcdf4-wasm.wasm +0 -0
- package/dist/netcdf4.d.ts +19 -20
- package/dist/netcdf4.d.ts.map +1 -1
- package/dist/netcdf4.js +201 -518
- package/dist/netcdf4.js.map +1 -1
- package/dist/operations.d.ts +67 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/operations.js +57 -0
- package/dist/operations.js.map +1 -0
- package/dist/remote-read.d.ts +4 -4
- package/dist/remote-read.d.ts.map +1 -1
- package/dist/remote-read.js +22 -18
- package/dist/remote-read.js.map +1 -1
- package/dist/slice.d.ts.map +1 -1
- package/dist/slice.js.map +1 -1
- package/dist/types.d.ts +27 -414
- package/dist/types.d.ts.map +1 -1
- package/dist/variable.d.ts +1 -1
- package/dist/variable.d.ts.map +1 -1
- package/dist/variable.js +60 -78
- package/dist/variable.js.map +1 -1
- package/dist/wasm-module.d.ts +468 -2
- package/dist/wasm-module.d.ts.map +1 -1
- package/dist/wasm-module.js +331 -251
- package/dist/wasm-module.js.map +1 -1
- package/package.json +17 -14
- package/dist/test-setup.d.ts +0 -13
- package/dist/test-setup.d.ts.map +0 -1
- package/dist/test-setup.js +0 -78
- package/dist/test-setup.js.map +0 -1
package/dist/netcdf4.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
// Main NetCDF4 class implementation
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
2
|
+
import { FILE_MODES, NC_CONSTANTS } from "./constants.js";
|
|
3
|
+
import { toError } from "./errors.js";
|
|
4
|
+
import { Group } from "./group.js";
|
|
5
|
+
import { runOperation, } from "./operations.js";
|
|
6
|
+
import { WasmModuleLoader } from "./wasm-module.js";
|
|
7
|
+
function isFileMode(mode) {
|
|
8
|
+
return Object.hasOwn(FILE_MODES, mode);
|
|
9
|
+
}
|
|
6
10
|
export class NetCDF4 extends Group {
|
|
7
11
|
filename;
|
|
8
12
|
mode;
|
|
@@ -19,8 +23,9 @@ export class NetCDF4 extends Group {
|
|
|
19
23
|
workerError;
|
|
20
24
|
pendingRequests = new Map();
|
|
21
25
|
removeAbortListener;
|
|
22
|
-
constructor(filename, mode =
|
|
23
|
-
super(
|
|
26
|
+
constructor(filename, mode = "r", options = {}) {
|
|
27
|
+
// this can't be passed before super(), so it is set on the next line
|
|
28
|
+
super(null, "", -1);
|
|
24
29
|
this.filename = filename;
|
|
25
30
|
this.mode = mode;
|
|
26
31
|
this.options = options;
|
|
@@ -30,40 +35,27 @@ export class NetCDF4 extends Group {
|
|
|
30
35
|
async initialize() {
|
|
31
36
|
if (this.initialized)
|
|
32
37
|
return;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
await this.setupWorker();
|
|
37
|
-
this.initialized = true;
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
this.module = await WasmModuleLoader.loadModule(this.options);
|
|
41
|
-
if (this.memorySource) {
|
|
42
|
-
await this.mountMemoryData();
|
|
43
|
-
}
|
|
44
|
-
}
|
|
38
|
+
if (this.workerSource) {
|
|
39
|
+
// This now handles the WORKERFS mounting
|
|
40
|
+
await this.setupWorker();
|
|
45
41
|
this.initialized = true;
|
|
46
|
-
// Automatically open the file if a filename was provided
|
|
47
|
-
if (this.filename && !this.workerSource) {
|
|
48
|
-
await this.open();
|
|
49
|
-
}
|
|
50
42
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this.
|
|
55
|
-
if (this.filename)
|
|
56
|
-
await this.open();
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
throw error;
|
|
43
|
+
else {
|
|
44
|
+
this.module = await WasmModuleLoader.loadModule(this.options);
|
|
45
|
+
if (this.memorySource) {
|
|
46
|
+
await this.mountMemoryData();
|
|
60
47
|
}
|
|
61
48
|
}
|
|
49
|
+
this.initialized = true;
|
|
50
|
+
// Automatically open the file if a filename was provided
|
|
51
|
+
if (this.filename && !this.workerSource) {
|
|
52
|
+
await this.open();
|
|
53
|
+
}
|
|
62
54
|
}
|
|
63
55
|
// Python-like factory method
|
|
64
|
-
static async Dataset(filename, mode =
|
|
56
|
+
static async Dataset(filename, mode = "r", options = {}) {
|
|
65
57
|
if (filename instanceof URL || /^https?:\/\//i.test(filename)) {
|
|
66
|
-
if (mode !==
|
|
58
|
+
if (mode !== "r") {
|
|
67
59
|
throw new Error(`Remote files can only be opened read-only (mode 'r'), got '${mode}'`);
|
|
68
60
|
}
|
|
69
61
|
return NetCDF4.fromUrl(filename, options);
|
|
@@ -73,29 +65,30 @@ export class NetCDF4 extends Group {
|
|
|
73
65
|
return dataset;
|
|
74
66
|
}
|
|
75
67
|
// Create dataset from Blob
|
|
76
|
-
static async fromBlob(blob, mode =
|
|
68
|
+
static async fromBlob(blob, mode = "r", options = {}) {
|
|
77
69
|
const arrayBuffer = await blob.arrayBuffer();
|
|
78
70
|
return NetCDF4.fromArrayBuffer(arrayBuffer, mode, options);
|
|
79
71
|
}
|
|
80
72
|
// Create dataset from ArrayBuffer
|
|
81
|
-
static async fromArrayBuffer(buffer, mode =
|
|
73
|
+
static async fromArrayBuffer(buffer, mode = "r", options = {}) {
|
|
82
74
|
const data = new Uint8Array(buffer);
|
|
83
75
|
return NetCDF4.fromMemory(data, mode, options);
|
|
84
76
|
}
|
|
85
77
|
// Create dataset from memory data (Uint8Array or ArrayBuffer)
|
|
86
|
-
static async fromMemory(data, mode =
|
|
78
|
+
static async fromMemory(data, mode = "r", options = {}, filename) {
|
|
87
79
|
if (!data) {
|
|
88
|
-
throw new Error(
|
|
80
|
+
throw new Error("Data cannot be null or undefined");
|
|
89
81
|
}
|
|
90
82
|
if (!(data instanceof ArrayBuffer) && !(data instanceof Uint8Array)) {
|
|
91
|
-
throw new Error(
|
|
83
|
+
throw new Error("Data must be ArrayBuffer or Uint8Array");
|
|
92
84
|
}
|
|
93
85
|
const uint8Data = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
|
94
|
-
const virtualFilename = filename ||
|
|
86
|
+
const virtualFilename = filename ||
|
|
87
|
+
`/tmp/netcdf_${Date.now()}_${Math.random().toString(36).substr(2, 9)}.nc`;
|
|
95
88
|
const dataset = new NetCDF4(virtualFilename, mode, options);
|
|
96
89
|
dataset.memorySource = {
|
|
97
90
|
data: uint8Data,
|
|
98
|
-
filename: virtualFilename
|
|
91
|
+
filename: virtualFilename,
|
|
99
92
|
};
|
|
100
93
|
await dataset.initialize();
|
|
101
94
|
return dataset;
|
|
@@ -103,20 +96,26 @@ export class NetCDF4 extends Group {
|
|
|
103
96
|
// New factory for Blob/File (local, no full preload)
|
|
104
97
|
static async fromBlobLazy(blob, options = {}) {
|
|
105
98
|
// IMPORTANT: Keep this path consistent with the mount logic in the worker
|
|
106
|
-
const mountPoint =
|
|
99
|
+
const mountPoint = "/working";
|
|
107
100
|
const baseName = `netcdf_lazy_${Date.now()}.nc`;
|
|
108
101
|
const fullPath = `${mountPoint}/${baseName}`;
|
|
109
|
-
const dataset = new NetCDF4(fullPath,
|
|
102
|
+
const dataset = new NetCDF4(fullPath, "r", options);
|
|
110
103
|
// Store the raw blob. The worker will mount it via WORKERFS
|
|
111
104
|
dataset.workerSource = { blob, filename: fullPath };
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
105
|
+
try {
|
|
106
|
+
await dataset.initialize();
|
|
107
|
+
// After worker is set up, open the file
|
|
108
|
+
await dataset.open();
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
dataset.terminateWorker(toError(error));
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
115
114
|
return dataset;
|
|
116
115
|
}
|
|
117
116
|
// Remote file over HTTP Range requests (read-only, only the blocks netCDF-C touches are fetched)
|
|
118
117
|
static async fromUrl(url, options = {}) {
|
|
119
|
-
const { blockSize, cacheSize, headers, withCredentials, maxFullDownload, signal } = options;
|
|
118
|
+
const { blockSize, cacheSize, headers, withCredentials, maxFullDownload, signal, } = options;
|
|
120
119
|
// The worker resolves relative URLs against its own script, so resolve here
|
|
121
120
|
let resolved;
|
|
122
121
|
try {
|
|
@@ -125,33 +124,39 @@ export class NetCDF4 extends Group {
|
|
|
125
124
|
catch {
|
|
126
125
|
throw new Error(`Invalid URL: '${url}'`);
|
|
127
126
|
}
|
|
128
|
-
if (resolved.protocol !==
|
|
127
|
+
if (resolved.protocol !== "http:" && resolved.protocol !== "https:") {
|
|
129
128
|
throw new Error(`Remote files must be served over HTTP(S), got '${resolved.href}'`);
|
|
130
129
|
}
|
|
131
|
-
if (typeof Worker ===
|
|
132
|
-
throw new Error(
|
|
130
|
+
if (typeof Worker === "undefined") {
|
|
131
|
+
throw new Error("Remote files require Web Worker support");
|
|
133
132
|
}
|
|
134
133
|
signal?.throwIfAborted();
|
|
135
|
-
const baseName = resolved.pathname.split(
|
|
134
|
+
const baseName = resolved.pathname.split("/").pop() || "remote.nc";
|
|
136
135
|
const fullPath = `/remote/${baseName}`;
|
|
137
|
-
const dataset = new NetCDF4(fullPath,
|
|
136
|
+
const dataset = new NetCDF4(fullPath, "r", options);
|
|
138
137
|
dataset.workerSource = {
|
|
139
138
|
url: resolved.href,
|
|
140
|
-
remote: {
|
|
139
|
+
remote: {
|
|
140
|
+
blockSize,
|
|
141
|
+
cacheSize,
|
|
142
|
+
headers,
|
|
143
|
+
withCredentials,
|
|
144
|
+
maxFullDownload,
|
|
145
|
+
},
|
|
141
146
|
filename: fullPath,
|
|
142
147
|
};
|
|
143
148
|
if (signal) {
|
|
144
149
|
// Reads are synchronous inside the worker, so terminating it is the only way to cancel one
|
|
145
150
|
const abort = () => dataset.terminateWorker(signal.reason);
|
|
146
|
-
signal.addEventListener(
|
|
147
|
-
dataset.removeAbortListener = () => signal.removeEventListener(
|
|
151
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
152
|
+
dataset.removeAbortListener = () => signal.removeEventListener("abort", abort);
|
|
148
153
|
}
|
|
149
154
|
try {
|
|
150
155
|
await dataset.initialize();
|
|
151
156
|
await dataset.open();
|
|
152
157
|
}
|
|
153
158
|
catch (error) {
|
|
154
|
-
dataset.terminateWorker(
|
|
159
|
+
dataset.terminateWorker(toError(error));
|
|
155
160
|
throw error;
|
|
156
161
|
}
|
|
157
162
|
return dataset;
|
|
@@ -159,44 +164,30 @@ export class NetCDF4 extends Group {
|
|
|
159
164
|
async open() {
|
|
160
165
|
if (this._isOpen)
|
|
161
166
|
return;
|
|
162
|
-
if (!this.filename || this.filename.trim() ===
|
|
163
|
-
throw new Error(
|
|
167
|
+
if (!this.filename || this.filename.trim() === "") {
|
|
168
|
+
throw new Error("No filename specified");
|
|
164
169
|
}
|
|
165
170
|
// Check for valid modes early, before any WASM operations
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
168
|
-
throw new Error(`Unsupported mode: ${
|
|
171
|
+
const mode = this.mode;
|
|
172
|
+
if (!isFileMode(mode)) {
|
|
173
|
+
throw new Error(`Unsupported mode: ${mode}`);
|
|
169
174
|
}
|
|
170
|
-
|
|
171
|
-
if (this.worker) {
|
|
172
|
-
// Wait for worker to be ready first
|
|
173
|
-
await this.workerReady;
|
|
174
|
-
const modeValue = this.mode === 'r' ? NC_CONSTANTS.NC_NOWRITE : NC_CONSTANTS.NC_WRITE;
|
|
175
|
-
this.ncid = await this.callWorker('open', { path: this.filename, modeValue });
|
|
176
|
-
this.groupId = this.ncid;
|
|
177
|
-
this._isOpen = true;
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
if (this.mode === 'w' || this.mode === 'w-') {
|
|
175
|
+
if (!this.worker && (mode === "w" || mode === "w-")) {
|
|
181
176
|
// Create new file
|
|
182
177
|
let createMode = NC_CONSTANTS.NC_CLOBBER;
|
|
183
|
-
if (this.options.format ===
|
|
178
|
+
if (this.options.format === "NETCDF4") {
|
|
184
179
|
createMode |= NC_CONSTANTS.NC_NETCDF4;
|
|
185
180
|
}
|
|
186
|
-
|
|
187
|
-
this.
|
|
188
|
-
this.groupId = result;
|
|
181
|
+
this.ncid = await this.createFile(this.filename, createMode);
|
|
182
|
+
this.groupId = this.ncid;
|
|
189
183
|
}
|
|
190
|
-
else
|
|
191
|
-
//
|
|
192
|
-
|
|
193
|
-
|
|
184
|
+
else {
|
|
185
|
+
// Worker datasets open the file inside the worker, where write
|
|
186
|
+
// modes open the existing file for writing like "a"
|
|
187
|
+
await this.workerReady;
|
|
188
|
+
this.ncid = await this.openFile(this.filename, mode === "w" || mode === "w-" ? "a" : mode);
|
|
194
189
|
this.groupId = this.ncid;
|
|
195
|
-
|
|
196
|
-
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
|
|
197
|
-
this.loadMockDimensions();
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
190
|
+
if (!this.worker) {
|
|
200
191
|
await this.load();
|
|
201
192
|
}
|
|
202
193
|
}
|
|
@@ -204,13 +195,13 @@ export class NetCDF4 extends Group {
|
|
|
204
195
|
}
|
|
205
196
|
// Property access similar to Python API
|
|
206
197
|
get file_format() {
|
|
207
|
-
return this.options.format ||
|
|
198
|
+
return this.options.format || "NETCDF4";
|
|
208
199
|
}
|
|
209
200
|
get disk_format() {
|
|
210
201
|
return this.file_format;
|
|
211
202
|
}
|
|
212
203
|
get filepath() {
|
|
213
|
-
return this.filename ||
|
|
204
|
+
return this.filename || "";
|
|
214
205
|
}
|
|
215
206
|
get isopen() {
|
|
216
207
|
return this._isOpen;
|
|
@@ -221,7 +212,7 @@ export class NetCDF4 extends Group {
|
|
|
221
212
|
}
|
|
222
213
|
getModule() {
|
|
223
214
|
if (!this.module) {
|
|
224
|
-
throw new Error(
|
|
215
|
+
throw new Error("NetCDF4 module not initialized. Call initialize() first.");
|
|
225
216
|
}
|
|
226
217
|
return this.module;
|
|
227
218
|
}
|
|
@@ -237,14 +228,14 @@ export class NetCDF4 extends Group {
|
|
|
237
228
|
finally {
|
|
238
229
|
// Nothing can reopen a worker's file, so free the worker (and any remote block cache)
|
|
239
230
|
if (this.worker)
|
|
240
|
-
this.terminateWorker(new Error(
|
|
231
|
+
this.terminateWorker(new Error("Dataset is closed"));
|
|
241
232
|
}
|
|
242
233
|
}
|
|
243
234
|
// Sync method (flush to disk)
|
|
244
235
|
async sync() {
|
|
245
236
|
if (this._isOpen) {
|
|
246
237
|
// TODO: Implement nc_sync when available
|
|
247
|
-
console.warn(
|
|
238
|
+
console.warn("sync() not yet implemented");
|
|
248
239
|
}
|
|
249
240
|
}
|
|
250
241
|
// Context manager support (Python-like)
|
|
@@ -258,16 +249,8 @@ export class NetCDF4 extends Group {
|
|
|
258
249
|
await this.close();
|
|
259
250
|
}
|
|
260
251
|
// Low-level NetCDF operations (used by Group methods)
|
|
261
|
-
async openFile(path, mode =
|
|
262
|
-
|
|
263
|
-
const modeValue = mode === 'r' ? NC_CONSTANTS.NC_NOWRITE :
|
|
264
|
-
mode === 'w' ? NC_CONSTANTS.NC_WRITE :
|
|
265
|
-
NC_CONSTANTS.NC_WRITE;
|
|
266
|
-
const result = module.nc_open(path, modeValue);
|
|
267
|
-
if (result.result !== NC_CONSTANTS.NC_NOERR) {
|
|
268
|
-
throw new Error(`Failed to open NetCDF file: ${path} (error: ${result.result})`);
|
|
269
|
-
}
|
|
270
|
-
return result.ncid;
|
|
252
|
+
async openFile(path, mode = "r") {
|
|
253
|
+
return this.run("open", { path, modeValue: FILE_MODES[mode].flags });
|
|
271
254
|
}
|
|
272
255
|
async createFile(path, mode = NC_CONSTANTS.NC_CLOBBER) {
|
|
273
256
|
const module = this.getModule();
|
|
@@ -278,26 +261,22 @@ export class NetCDF4 extends Group {
|
|
|
278
261
|
return result.ncid;
|
|
279
262
|
}
|
|
280
263
|
async closeFile(ncid) {
|
|
281
|
-
|
|
282
|
-
await this.callWorker('close');
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
285
|
-
const module = this.module;
|
|
286
|
-
if (!module)
|
|
287
|
-
throw new Error("Failed to load module. Ensure module is initialized before calling methods");
|
|
288
|
-
const result = module.nc_close(ncid);
|
|
289
|
-
if (result !== NC_CONSTANTS.NC_NOERR) {
|
|
290
|
-
throw new Error(`Failed to close NetCDF file with ID: ${ncid} (error: ${result})`);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
264
|
+
await this.run("close", { ncid });
|
|
293
265
|
}
|
|
294
266
|
requestId = 0;
|
|
295
|
-
|
|
267
|
+
// Run an operation from operations.ts: in the worker if there is one,
|
|
268
|
+
// otherwise on this thread. ncid defaults to the dataset's own.
|
|
269
|
+
async run(name, args) {
|
|
270
|
+
const fullArgs = { ncid: this.ncid, ...args };
|
|
296
271
|
if (this.workerError)
|
|
297
272
|
throw this.workerError;
|
|
298
|
-
if (
|
|
299
|
-
|
|
300
|
-
|
|
273
|
+
if (this.worker) {
|
|
274
|
+
return this.requestWorker({
|
|
275
|
+
type: name,
|
|
276
|
+
args: fullArgs,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
return runOperation(this.getModule(), name, fullArgs);
|
|
301
280
|
}
|
|
302
281
|
// Post a message to the worker and wait for the reply carrying the same id.
|
|
303
282
|
// With startup set, a worker 'error' event also rejects, since a worker that
|
|
@@ -314,20 +293,22 @@ export class NetCDF4 extends Group {
|
|
|
314
293
|
this.pendingRequests.delete(id);
|
|
315
294
|
if (error)
|
|
316
295
|
reject(error);
|
|
296
|
+
// The worker's reply can't be checked; it is trusted to match T
|
|
317
297
|
else
|
|
318
298
|
resolve(result);
|
|
319
299
|
};
|
|
320
|
-
worker.addEventListener(
|
|
300
|
+
worker.addEventListener("message", (e) => {
|
|
321
301
|
if (e.data.id !== id)
|
|
322
302
|
return;
|
|
323
303
|
if (e.data.success)
|
|
324
304
|
finish(undefined, e.data.result);
|
|
325
305
|
else
|
|
326
|
-
finish(new Error(e.data.error ||
|
|
306
|
+
finish(new Error(e.data.error ||
|
|
307
|
+
`Worker error in ${message.type}`));
|
|
327
308
|
}, { signal: listeners.signal });
|
|
328
309
|
if (startup) {
|
|
329
|
-
worker.addEventListener(
|
|
330
|
-
finish(new Error(`Worker failed to start: ${e.message ||
|
|
310
|
+
worker.addEventListener("error", (e) => {
|
|
311
|
+
finish(new Error(`Worker failed to start: ${e.message || "unknown error"}`));
|
|
331
312
|
}, { signal: listeners.signal });
|
|
332
313
|
}
|
|
333
314
|
this.pendingRequests.set(id, finish);
|
|
@@ -346,130 +327,51 @@ export class NetCDF4 extends Group {
|
|
|
346
327
|
this.ncid = -1;
|
|
347
328
|
}
|
|
348
329
|
async getGlobalAttributes(groupPath) {
|
|
349
|
-
|
|
350
|
-
return this.callWorker('getGlobalAttributes', { groupPath });
|
|
351
|
-
}
|
|
352
|
-
else {
|
|
353
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
354
|
-
return NCGet.getGlobalAttributes(this.module, this.ncid, groupPath);
|
|
355
|
-
}
|
|
330
|
+
return this.run("getGlobalAttributes", { groupPath });
|
|
356
331
|
}
|
|
357
332
|
async getFullMetadata(groupPath) {
|
|
358
|
-
|
|
359
|
-
return this.callWorker('getFullMetadata', { groupPath });
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
362
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
363
|
-
return NCGet.getFullMetadata(this.module, this.ncid, groupPath);
|
|
364
|
-
}
|
|
333
|
+
return this.run("getFullMetadata", { groupPath });
|
|
365
334
|
}
|
|
366
335
|
async getAttributeValues(varid, attname) {
|
|
367
|
-
|
|
368
|
-
return this.callWorker('getAttributeValues', { varid, attname });
|
|
369
|
-
}
|
|
370
|
-
else {
|
|
371
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
372
|
-
return NCGet.getAttributeValues(this.module, this.ncid, varid, attname);
|
|
373
|
-
}
|
|
336
|
+
return this.run("getAttributeValues", { varid, attname });
|
|
374
337
|
}
|
|
375
338
|
async getDimCount(ncid = this.ncid) {
|
|
376
|
-
|
|
377
|
-
return this.callWorker('getDimCount', { ncid });
|
|
378
|
-
}
|
|
379
|
-
else {
|
|
380
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
381
|
-
return NCGet.getDimCount(this.module, ncid);
|
|
382
|
-
}
|
|
339
|
+
return this.run("getDimCount", { ncid });
|
|
383
340
|
}
|
|
384
341
|
async getGroupVariables(groupPath) {
|
|
385
|
-
|
|
386
|
-
return this.callWorker('getGroupVariables', { groupPath });
|
|
387
|
-
}
|
|
388
|
-
else {
|
|
389
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
390
|
-
return NCGet.getGroupVariables(this.module, this.ncid, groupPath);
|
|
391
|
-
}
|
|
342
|
+
return this.run("getGroupVariables", { groupPath });
|
|
392
343
|
}
|
|
393
344
|
async getVarIDs(ncid = this.ncid) {
|
|
394
|
-
|
|
395
|
-
return this.callWorker('getVarIDs', { ncid });
|
|
396
|
-
}
|
|
397
|
-
else {
|
|
398
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
399
|
-
return NCGet.getVarIDs(this.module, ncid);
|
|
400
|
-
}
|
|
345
|
+
return this.run("getVarIDs", { ncid });
|
|
401
346
|
}
|
|
402
347
|
async getDimIDs(ncid = this.ncid) {
|
|
403
|
-
|
|
404
|
-
return this.callWorker('getDimIDs', { ncid });
|
|
405
|
-
}
|
|
406
|
-
else {
|
|
407
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
408
|
-
return NCGet.getDimIDs(this.module, ncid);
|
|
409
|
-
}
|
|
348
|
+
return this.run("getDimIDs", { ncid });
|
|
410
349
|
}
|
|
411
350
|
async getDim(dimid, ncid = this.ncid) {
|
|
412
|
-
|
|
413
|
-
return this.callWorker('getDim', { dimid, ncid });
|
|
414
|
-
}
|
|
415
|
-
else {
|
|
416
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
417
|
-
return NCGet.getDim(this.module, ncid, dimid);
|
|
418
|
-
}
|
|
351
|
+
return this.run("getDim", { dimid, ncid });
|
|
419
352
|
}
|
|
420
353
|
async getDims(groupPath) {
|
|
421
|
-
|
|
422
|
-
return this.callWorker('getDims', { groupPath });
|
|
423
|
-
}
|
|
424
|
-
else {
|
|
425
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
426
|
-
return NCGet.getDims(this.module, this.ncid, groupPath);
|
|
427
|
-
}
|
|
354
|
+
return this.run("getDims", { groupPath });
|
|
428
355
|
}
|
|
429
356
|
async getVarCount(ncid = this.ncid) {
|
|
430
|
-
|
|
431
|
-
return this.callWorker('getVarCount', { ncid });
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
435
|
-
return NCGet.getVarCount(this.module, ncid);
|
|
436
|
-
}
|
|
357
|
+
return this.run("getVarCount", { ncid });
|
|
437
358
|
}
|
|
438
359
|
async getAttributeName(varid, attId) {
|
|
439
|
-
|
|
440
|
-
return this.callWorker('getAttributeName', { varid, attId });
|
|
441
|
-
}
|
|
442
|
-
else {
|
|
443
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
444
|
-
return NCGet.getAttributeName(this.module, this.ncid, varid, attId);
|
|
445
|
-
}
|
|
360
|
+
return this.run("getAttributeName", { varid, attId });
|
|
446
361
|
}
|
|
447
362
|
async getVariableInfo(variable, groupPath) {
|
|
448
|
-
|
|
449
|
-
return this.callWorker('getVariableInfo', { variable, groupPath });
|
|
450
|
-
}
|
|
451
|
-
else {
|
|
452
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
453
|
-
return NCGet.getVariableInfo(this.module, this.ncid, variable, groupPath);
|
|
454
|
-
}
|
|
363
|
+
return this.run("getVariableInfo", { variable, groupPath });
|
|
455
364
|
}
|
|
456
365
|
async getVariableArray(variable, groupPath) {
|
|
457
|
-
|
|
458
|
-
return this.callWorker('getVariableArray', { variable, groupPath });
|
|
459
|
-
}
|
|
460
|
-
else {
|
|
461
|
-
// Main thread path is already synchronous (or could be wrapped in Promise.resolve)
|
|
462
|
-
return NCGet.getVariableArray(this.module, this.ncid, variable, groupPath);
|
|
463
|
-
}
|
|
366
|
+
return this.run("getVariableArray", { variable, groupPath });
|
|
464
367
|
}
|
|
465
368
|
async getSlicedVariableArray(variable, start, count, groupPath) {
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
}
|
|
369
|
+
return this.run("getSlicedVariableArray", {
|
|
370
|
+
variable,
|
|
371
|
+
start,
|
|
372
|
+
count,
|
|
373
|
+
groupPath,
|
|
374
|
+
});
|
|
473
375
|
}
|
|
474
376
|
/**
|
|
475
377
|
* slicing and indexing convenience method.
|
|
@@ -495,55 +397,28 @@ export class NetCDF4 extends Group {
|
|
|
495
397
|
*
|
|
496
398
|
*/
|
|
497
399
|
async get(variable, selection, groupPath, options) {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
return NCGet.getVariableArrayWithSelection(this.module, this.ncid, variable, selection, groupPath, options);
|
|
505
|
-
}
|
|
400
|
+
return this.run("getVariableArrayWithSelection", {
|
|
401
|
+
variable,
|
|
402
|
+
selection,
|
|
403
|
+
groupPath,
|
|
404
|
+
options,
|
|
405
|
+
});
|
|
506
406
|
}
|
|
507
407
|
// Group functions
|
|
508
408
|
async getGroups(ncid = this.ncid) {
|
|
509
|
-
|
|
510
|
-
return this.callWorker('getGroups', { ncid });
|
|
511
|
-
}
|
|
512
|
-
else {
|
|
513
|
-
return NCGet.getGroups(this.module, ncid);
|
|
514
|
-
}
|
|
409
|
+
return this.run("getGroups", { ncid });
|
|
515
410
|
}
|
|
516
411
|
async getGroupsRecursive(ncid = this.ncid) {
|
|
517
|
-
|
|
518
|
-
return this.callWorker('getGroupsRecursive', { ncid });
|
|
519
|
-
}
|
|
520
|
-
else {
|
|
521
|
-
return NCGet.getGroupsRecursive(this.module, ncid);
|
|
522
|
-
}
|
|
412
|
+
return this.run("getGroupsRecursive", { ncid });
|
|
523
413
|
}
|
|
524
414
|
async getGroupNCID(groupPath) {
|
|
525
|
-
|
|
526
|
-
return this.callWorker('getGroupNCID', { groupPath });
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
return NCGet.getGroupNCID(this.module, this.ncid, groupPath);
|
|
530
|
-
}
|
|
415
|
+
return this.run("getGroupNCID", { groupPath });
|
|
531
416
|
}
|
|
532
417
|
async getGroupName(ncid = this.ncid) {
|
|
533
|
-
|
|
534
|
-
return this.callWorker('getGroupName', { ncid });
|
|
535
|
-
}
|
|
536
|
-
else {
|
|
537
|
-
return NCGet.getGroupName(this.module, ncid);
|
|
538
|
-
}
|
|
418
|
+
return this.run("getGroupName", { ncid });
|
|
539
419
|
}
|
|
540
420
|
async getGroupPath(ncid = this.ncid) {
|
|
541
|
-
|
|
542
|
-
return this.callWorker('getGroupPath', { ncid });
|
|
543
|
-
}
|
|
544
|
-
else {
|
|
545
|
-
return NCGet.getGroupPath(this.module, ncid);
|
|
546
|
-
}
|
|
421
|
+
return this.run("getGroupPath", { ncid });
|
|
547
422
|
}
|
|
548
423
|
async defineDimension(ncid, name, size) {
|
|
549
424
|
const module = this.getModule();
|
|
@@ -586,217 +461,40 @@ export class NetCDF4 extends Group {
|
|
|
586
461
|
}
|
|
587
462
|
return result.data;
|
|
588
463
|
}
|
|
589
|
-
// Create a mock module for testing
|
|
590
|
-
createMockModule() {
|
|
591
|
-
// Global mock file storage to simulate persistence across instances
|
|
592
|
-
if (!global.__netcdf4_mock_files) {
|
|
593
|
-
global.__netcdf4_mock_files = {};
|
|
594
|
-
}
|
|
595
|
-
const mockFiles = global.__netcdf4_mock_files;
|
|
596
|
-
const getFile = () => this.filename ? mockFiles[this.filename] : undefined;
|
|
597
|
-
const getVariableNames = () => Object.keys(getFile()?.variables ?? {});
|
|
598
|
-
// Match the existing mock's 1-based variable IDs.
|
|
599
|
-
const getVariable = (varid) => getFile()?.variables[getVariableNames()[varid - 1]];
|
|
600
|
-
const getAttributes = (varid) => varid === NC_CONSTANTS.NC_GLOBAL
|
|
601
|
-
? getFile()?.attributes : getVariable(varid)?.attributes;
|
|
602
|
-
return {
|
|
603
|
-
nc_open: (path, mode) => {
|
|
604
|
-
// Mock implementation that simulates invalid filenames and unsupported modes
|
|
605
|
-
if (!path || path.trim() === '' || path.includes('unsupported') || !['r', 'w', 'a'].some(m => this.mode.includes(m))) {
|
|
606
|
-
return { result: -1, ncid: -1 };
|
|
607
|
-
}
|
|
608
|
-
// For reading mode, file should exist in mock storage, otherwise create a minimal entry
|
|
609
|
-
if (this.mode === 'r' && !mockFiles[path]) {
|
|
610
|
-
// For test purposes, allow reading non-existent files but initialize them empty
|
|
611
|
-
mockFiles[path] = {
|
|
612
|
-
attributes: {},
|
|
613
|
-
dimensions: {},
|
|
614
|
-
variables: {}
|
|
615
|
-
};
|
|
616
|
-
}
|
|
617
|
-
return { result: NC_CONSTANTS.NC_NOERR, ncid: 1 };
|
|
618
|
-
},
|
|
619
|
-
nc_close: (ncid) => {
|
|
620
|
-
// In a real implementation, this would flush data to the file
|
|
621
|
-
// For our mock, we'll keep the data in memory
|
|
622
|
-
return NC_CONSTANTS.NC_NOERR;
|
|
623
|
-
},
|
|
624
|
-
nc_create: (path, mode) => {
|
|
625
|
-
if (path.includes('unsupported') || ['x', 'invalid'].some(m => this.mode.includes(m))) {
|
|
626
|
-
return { result: -1, ncid: -1 };
|
|
627
|
-
}
|
|
628
|
-
// Initialize mock file storage
|
|
629
|
-
mockFiles[path] = {
|
|
630
|
-
attributes: {},
|
|
631
|
-
dimensions: {},
|
|
632
|
-
variables: {}
|
|
633
|
-
};
|
|
634
|
-
return { result: NC_CONSTANTS.NC_NOERR, ncid: 1 };
|
|
635
|
-
},
|
|
636
|
-
nc_def_dim: (ncid, name, len) => {
|
|
637
|
-
// Store dimension in mock file
|
|
638
|
-
if (this.filename && mockFiles[this.filename]) {
|
|
639
|
-
mockFiles[this.filename].dimensions[name] = {
|
|
640
|
-
size: len,
|
|
641
|
-
unlimited: len === NC_CONSTANTS.NC_UNLIMITED
|
|
642
|
-
};
|
|
643
|
-
}
|
|
644
|
-
return { result: NC_CONSTANTS.NC_NOERR, dimid: 1 };
|
|
645
|
-
},
|
|
646
|
-
nc_def_var: (ncid, name, xtype, ndims, dimids) => {
|
|
647
|
-
// Initialize variable storage
|
|
648
|
-
if (this.filename && mockFiles[this.filename]) {
|
|
649
|
-
mockFiles[this.filename].variables[name] = {
|
|
650
|
-
type: xtype,
|
|
651
|
-
dimids: [...dimids],
|
|
652
|
-
data: new Float64Array(0),
|
|
653
|
-
attributes: {}
|
|
654
|
-
};
|
|
655
|
-
// Return varid based on current variable count (1-based)
|
|
656
|
-
const varCount = Object.keys(mockFiles[this.filename].variables).length;
|
|
657
|
-
return { result: NC_CONSTANTS.NC_NOERR, varid: varCount };
|
|
658
|
-
}
|
|
659
|
-
return { result: NC_CONSTANTS.NC_NOERR, varid: 1 };
|
|
660
|
-
},
|
|
661
|
-
nc_inq_varids: (ncid) => ({
|
|
662
|
-
result: NC_CONSTANTS.NC_NOERR,
|
|
663
|
-
varids: Int32Array.from(getVariableNames(), (_, i) => i + 1)
|
|
664
|
-
}),
|
|
665
|
-
nc_inq_varid: (ncid, name) => {
|
|
666
|
-
const index = getVariableNames().indexOf(name);
|
|
667
|
-
return index < 0 ? { result: -1 } : { result: NC_CONSTANTS.NC_NOERR, varid: index + 1 };
|
|
668
|
-
},
|
|
669
|
-
nc_inq_var: (ncid, varid) => {
|
|
670
|
-
const variable = getVariable(varid);
|
|
671
|
-
if (!variable)
|
|
672
|
-
return { result: -1 };
|
|
673
|
-
return {
|
|
674
|
-
result: NC_CONSTANTS.NC_NOERR,
|
|
675
|
-
name: getVariableNames()[varid - 1],
|
|
676
|
-
type: variable.type,
|
|
677
|
-
ndims: variable.dimids.length,
|
|
678
|
-
dimids: Int32Array.from(variable.dimids),
|
|
679
|
-
natts: Object.keys(variable.attributes).length
|
|
680
|
-
};
|
|
681
|
-
},
|
|
682
|
-
nc_inq_dim: (ncid, dimid) => {
|
|
683
|
-
const dimensions = getFile()?.dimensions ?? {};
|
|
684
|
-
const name = Object.keys(dimensions)[dimid];
|
|
685
|
-
return name === undefined ? { result: -1 } : {
|
|
686
|
-
result: NC_CONSTANTS.NC_NOERR, name, len: dimensions[name].size
|
|
687
|
-
};
|
|
688
|
-
},
|
|
689
|
-
nc_inq_grp_parent: (ncid) => ({ result: -1 }),
|
|
690
|
-
nc_inq_attname: (ncid, varid, attnum) => {
|
|
691
|
-
const name = Object.keys(getAttributes(varid) ?? {})[attnum];
|
|
692
|
-
return name === undefined ? { result: -1 } : { result: NC_CONSTANTS.NC_NOERR, name };
|
|
693
|
-
},
|
|
694
|
-
nc_inq_att: (ncid, varid, name) => {
|
|
695
|
-
const value = getAttributes(varid)?.[name];
|
|
696
|
-
if (value === undefined)
|
|
697
|
-
return { result: -1 };
|
|
698
|
-
return {
|
|
699
|
-
result: NC_CONSTANTS.NC_NOERR,
|
|
700
|
-
type: typeof value === 'string' ? NC_CONSTANTS.NC_CHAR : NC_CONSTANTS.NC_DOUBLE,
|
|
701
|
-
len: typeof value === 'string' || Array.isArray(value) ? value.length : 1
|
|
702
|
-
};
|
|
703
|
-
},
|
|
704
|
-
nc_get_att_text: (ncid, varid, name, length) => {
|
|
705
|
-
const data = getAttributes(varid)?.[name];
|
|
706
|
-
return typeof data === 'string' ? { result: NC_CONSTANTS.NC_NOERR, data } : { result: -1 };
|
|
707
|
-
},
|
|
708
|
-
nc_get_att_double: (ncid, varid, name, length) => {
|
|
709
|
-
const value = getAttributes(varid)?.[name];
|
|
710
|
-
return value === undefined ? { result: -1 } : {
|
|
711
|
-
result: NC_CONSTANTS.NC_NOERR, data: Array.isArray(value) ? value : [value]
|
|
712
|
-
};
|
|
713
|
-
},
|
|
714
|
-
nc_inq_var_chunking: (ncid, varid) => getVariable(varid)
|
|
715
|
-
? { result: NC_CONSTANTS.NC_NOERR, chunking: NC_CONSTANTS.NC_CONTIGUOUS }
|
|
716
|
-
: { result: -1 },
|
|
717
|
-
nc_put_var_double: (ncid, varid, data) => {
|
|
718
|
-
// Store data in mock file - try to map varid to variable name
|
|
719
|
-
if (this.filename && mockFiles[this.filename]) {
|
|
720
|
-
const variables = mockFiles[this.filename].variables;
|
|
721
|
-
const varNames = Object.keys(variables);
|
|
722
|
-
// Map varid to variable name (1-based indexing)
|
|
723
|
-
if (varNames.length > 0 && varid >= 1 && varid <= varNames.length) {
|
|
724
|
-
const varName = varNames[varid - 1]; // Convert to 0-based
|
|
725
|
-
variables[varName].data = new Float64Array(data);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
return NC_CONSTANTS.NC_NOERR;
|
|
729
|
-
},
|
|
730
|
-
nc_get_var_double: (ncid, varid, size) => {
|
|
731
|
-
// Try to get actual stored data first
|
|
732
|
-
if (this.filename && mockFiles[this.filename]) {
|
|
733
|
-
const variables = mockFiles[this.filename].variables;
|
|
734
|
-
const varNames = Object.keys(variables);
|
|
735
|
-
// Map varid to variable name (1-based indexing)
|
|
736
|
-
if (varNames.length > 0 && varid >= 1 && varid <= varNames.length) {
|
|
737
|
-
const varName = varNames[varid - 1]; // Convert to 0-based
|
|
738
|
-
const storedData = variables[varName].data;
|
|
739
|
-
if (storedData && storedData.length > 0) {
|
|
740
|
-
// Return the stored data, resized to requested size if needed
|
|
741
|
-
if (size <= 0) {
|
|
742
|
-
return { result: NC_CONSTANTS.NC_NOERR, data: new Float64Array(0) };
|
|
743
|
-
}
|
|
744
|
-
const result = new Float64Array(size);
|
|
745
|
-
for (let i = 0; i < size && i < storedData.length; i++) {
|
|
746
|
-
result[i] = storedData[i];
|
|
747
|
-
}
|
|
748
|
-
return { result: NC_CONSTANTS.NC_NOERR, data: result };
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
// Fallback to test pattern if no data stored
|
|
753
|
-
if (size <= 0) {
|
|
754
|
-
return { result: NC_CONSTANTS.NC_NOERR, data: new Float64Array(0) };
|
|
755
|
-
}
|
|
756
|
-
const data = new Float64Array(size);
|
|
757
|
-
for (let i = 0; i < size; i++) {
|
|
758
|
-
data[i] = i * 0.1; // Simple test pattern
|
|
759
|
-
}
|
|
760
|
-
return { result: NC_CONSTANTS.NC_NOERR, data };
|
|
761
|
-
},
|
|
762
|
-
nc_enddef: (ncid) => NC_CONSTANTS.NC_NOERR,
|
|
763
|
-
};
|
|
764
|
-
}
|
|
765
464
|
async setupWorker() {
|
|
766
465
|
if (!this.workerSource)
|
|
767
|
-
throw new Error(
|
|
466
|
+
throw new Error("No worker source");
|
|
768
467
|
// 1. Instantiate the worker if it doesn't exist
|
|
769
468
|
if (!this.worker) {
|
|
770
469
|
// Option A: If using Vite/Webpack 5
|
|
771
|
-
this.worker = new Worker(new URL(
|
|
470
|
+
this.worker = new Worker(new URL("./netcdf-worker.js", import.meta.url), {
|
|
471
|
+
type: "module",
|
|
472
|
+
});
|
|
772
473
|
}
|
|
773
474
|
// 2. Send the initialization message
|
|
774
|
-
this.workerReady = this.requestWorker({ type:
|
|
475
|
+
this.workerReady = this.requestWorker({ type: "init", ...this.workerSource }, { startup: true });
|
|
775
476
|
return this.workerReady;
|
|
776
477
|
}
|
|
777
478
|
// Mount memory data in the WASM virtual file system
|
|
778
479
|
async mountMemoryData() {
|
|
779
|
-
if (!this.memorySource || !this.module) {
|
|
780
|
-
return;
|
|
781
|
-
}
|
|
782
|
-
// Skip mounting in test mode (mock module doesn't have FS)
|
|
783
|
-
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
|
|
480
|
+
if (!this.memorySource?.filename || !this.module) {
|
|
784
481
|
return;
|
|
785
482
|
}
|
|
786
483
|
try {
|
|
787
484
|
const module = this.getModule();
|
|
788
485
|
if (!module.FS) {
|
|
789
|
-
throw new Error(
|
|
486
|
+
throw new Error("Emscripten FS not available");
|
|
790
487
|
}
|
|
791
488
|
// Ensure the /tmp directory exists
|
|
792
489
|
try {
|
|
793
|
-
module.FS.mkdir(
|
|
490
|
+
module.FS.mkdir("/tmp");
|
|
794
491
|
}
|
|
795
|
-
catch
|
|
492
|
+
catch {
|
|
796
493
|
// Directory might already exist, ignore error
|
|
797
494
|
}
|
|
798
495
|
// Write the memory data to a virtual file
|
|
799
|
-
|
|
496
|
+
const { data, filename } = this.memorySource;
|
|
497
|
+
module.FS.writeFile(filename, data instanceof ArrayBuffer ? new Uint8Array(data) : data);
|
|
800
498
|
}
|
|
801
499
|
catch (error) {
|
|
802
500
|
throw new Error(`Failed to mount memory data: ${error}`);
|
|
@@ -805,17 +503,12 @@ export class NetCDF4 extends Group {
|
|
|
805
503
|
// Get data from memory or file as ArrayBuffer (for writing back to Blob)
|
|
806
504
|
async toArrayBuffer() {
|
|
807
505
|
if (!this.module) {
|
|
808
|
-
throw new Error(
|
|
809
|
-
}
|
|
810
|
-
// Skip in test mode
|
|
811
|
-
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
|
|
812
|
-
// Return empty buffer in test mode
|
|
813
|
-
return new ArrayBuffer(0);
|
|
506
|
+
throw new Error("NetCDF4 module not initialized");
|
|
814
507
|
}
|
|
815
508
|
try {
|
|
816
509
|
const module = this.getModule();
|
|
817
510
|
if (!module.FS || !this.filename) {
|
|
818
|
-
throw new Error(
|
|
511
|
+
throw new Error("Cannot read file data");
|
|
819
512
|
}
|
|
820
513
|
// Read the file from the virtual file system
|
|
821
514
|
const data = module.FS.readFile(this.filename);
|
|
@@ -826,39 +519,29 @@ export class NetCDF4 extends Group {
|
|
|
826
519
|
}
|
|
827
520
|
}
|
|
828
521
|
// Convert to Blob
|
|
829
|
-
async toBlob(type =
|
|
522
|
+
async toBlob(type = "application/x-netcdf") {
|
|
830
523
|
const buffer = await this.toArrayBuffer();
|
|
831
524
|
return new Blob([buffer], { type });
|
|
832
525
|
}
|
|
833
526
|
toString() {
|
|
834
|
-
const status = this._isOpen ?
|
|
835
|
-
const source = this.memorySource ?
|
|
527
|
+
const status = this._isOpen ? "open" : "closed";
|
|
528
|
+
const source = this.memorySource ? "(in-memory)" : "";
|
|
836
529
|
return `<netCDF4.Dataset '${this.filename}'${source}: mode = '${this.mode}', file format = '${this.file_format}', ${status}>`;
|
|
837
530
|
}
|
|
838
531
|
/**
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
532
|
+
* Get complete hierarchy of groups, variables, dimensions, and attributes
|
|
533
|
+
* This is the unified method for exploring the entire file structure
|
|
534
|
+
* @param groupPath - Optional path to start from a specific group
|
|
535
|
+
*/
|
|
843
536
|
async getCompleteHierarchy(groupPath) {
|
|
844
|
-
|
|
845
|
-
return this.callWorker('getCompleteHierarchy', { groupPath });
|
|
846
|
-
}
|
|
847
|
-
else {
|
|
848
|
-
return NCGet.getCompleteHierarchy(this.module, this.ncid, groupPath);
|
|
849
|
-
}
|
|
537
|
+
return this.run("getCompleteHierarchy", { groupPath });
|
|
850
538
|
}
|
|
851
539
|
/**
|
|
852
540
|
* Get all variables recursively from all groups
|
|
853
541
|
* Returns a flat dictionary with full path keys like "/group1/var1"
|
|
854
542
|
*/
|
|
855
543
|
async getVariables() {
|
|
856
|
-
|
|
857
|
-
return this.callWorker('getVariables');
|
|
858
|
-
}
|
|
859
|
-
else {
|
|
860
|
-
return NCGet.getVariables(this.module, this.ncid);
|
|
861
|
-
}
|
|
544
|
+
return this.run("getVariables", {});
|
|
862
545
|
}
|
|
863
546
|
}
|
|
864
547
|
/**
|
|
@@ -880,12 +563,12 @@ export class DataTree {
|
|
|
880
563
|
// --------------------------------------------------
|
|
881
564
|
// Core navigation
|
|
882
565
|
// --------------------------------------------------
|
|
883
|
-
getGroup(groupPath =
|
|
566
|
+
getGroup(groupPath = "/") {
|
|
884
567
|
if (!this.tree)
|
|
885
568
|
return null;
|
|
886
|
-
if (groupPath ===
|
|
569
|
+
if (groupPath === "/" || !groupPath)
|
|
887
570
|
return this.tree;
|
|
888
|
-
const parts = groupPath.split(
|
|
571
|
+
const parts = groupPath.split("/").filter(Boolean);
|
|
889
572
|
let current = this.tree;
|
|
890
573
|
for (const part of parts) {
|
|
891
574
|
if (!current.groups || !current.groups[part])
|
|
@@ -895,12 +578,12 @@ export class DataTree {
|
|
|
895
578
|
return current;
|
|
896
579
|
}
|
|
897
580
|
getGroupName(groupPath) {
|
|
898
|
-
if (!groupPath || groupPath ===
|
|
899
|
-
return
|
|
900
|
-
const parts = groupPath.split(
|
|
581
|
+
if (!groupPath || groupPath === "/")
|
|
582
|
+
return "root";
|
|
583
|
+
const parts = groupPath.split("/").filter(Boolean);
|
|
901
584
|
return parts[parts.length - 1];
|
|
902
585
|
}
|
|
903
|
-
hasSubgroups(groupPath =
|
|
586
|
+
hasSubgroups(groupPath = "/") {
|
|
904
587
|
const group = this.getGroup(groupPath);
|
|
905
588
|
return group ? Object.keys(group.groups || {}).length > 0 : false;
|
|
906
589
|
}
|
|
@@ -908,13 +591,13 @@ export class DataTree {
|
|
|
908
591
|
// Groups (for dropdowns)
|
|
909
592
|
// --------------------------------------------------
|
|
910
593
|
/** immediate children only */
|
|
911
|
-
listGroups(groupPath =
|
|
594
|
+
listGroups(groupPath = "/") {
|
|
912
595
|
const group = this.getGroup(groupPath);
|
|
913
596
|
if (!group || !group.groups)
|
|
914
597
|
return [];
|
|
915
598
|
return Object.entries(group.groups).map(([name, g]) => ({
|
|
916
599
|
name,
|
|
917
|
-
path: g.path || `${groupPath ===
|
|
600
|
+
path: g.path || `${groupPath === "/" ? "" : groupPath}/${name}`,
|
|
918
601
|
}));
|
|
919
602
|
}
|
|
920
603
|
/** every group recursively */
|
|
@@ -947,21 +630,21 @@ export class DataTree {
|
|
|
947
630
|
const allGroups = this.listAllGroups();
|
|
948
631
|
// Create root node
|
|
949
632
|
const root = {
|
|
950
|
-
name:
|
|
951
|
-
path:
|
|
633
|
+
name: "/",
|
|
634
|
+
path: "/",
|
|
952
635
|
children: [],
|
|
953
|
-
hasVariables: this.hasVariables(
|
|
954
|
-
hasAttributes: this.hasAttributes(
|
|
955
|
-
variableCount: this.getVariableCount(
|
|
956
|
-
attributeCount: this.getAttributeCount(
|
|
636
|
+
hasVariables: this.hasVariables("/"),
|
|
637
|
+
hasAttributes: this.hasAttributes("/"),
|
|
638
|
+
variableCount: this.getVariableCount("/"),
|
|
639
|
+
attributeCount: this.getAttributeCount("/"),
|
|
957
640
|
};
|
|
958
641
|
// Map to store all nodes by path for quick lookup
|
|
959
642
|
const nodeMap = new Map();
|
|
960
|
-
nodeMap.set(
|
|
643
|
+
nodeMap.set("/", root);
|
|
961
644
|
// Sort groups by path depth to ensure parents are created before children
|
|
962
645
|
const sortedGroups = allGroups.sort((a, b) => {
|
|
963
|
-
const depthA = a.path.split(
|
|
964
|
-
const depthB = b.path.split(
|
|
646
|
+
const depthA = a.path.split("/").filter(Boolean).length;
|
|
647
|
+
const depthB = b.path.split("/").filter(Boolean).length;
|
|
965
648
|
return depthA - depthB;
|
|
966
649
|
});
|
|
967
650
|
// Build the tree
|
|
@@ -973,14 +656,14 @@ export class DataTree {
|
|
|
973
656
|
hasVariables: this.hasVariables(path),
|
|
974
657
|
hasAttributes: this.hasAttributes(path),
|
|
975
658
|
variableCount: this.getVariableCount(path),
|
|
976
|
-
attributeCount: this.getAttributeCount(path)
|
|
659
|
+
attributeCount: this.getAttributeCount(path),
|
|
977
660
|
};
|
|
978
661
|
nodeMap.set(path, node);
|
|
979
662
|
// Find parent path
|
|
980
|
-
const pathParts = path.split(
|
|
663
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
981
664
|
const parentPath = pathParts.length === 1
|
|
982
|
-
?
|
|
983
|
-
:
|
|
665
|
+
? "/"
|
|
666
|
+
: "/" + pathParts.slice(0, -1).join("/");
|
|
984
667
|
const parent = nodeMap.get(parentPath);
|
|
985
668
|
if (parent) {
|
|
986
669
|
parent.children.push(node);
|
|
@@ -995,12 +678,12 @@ export class DataTree {
|
|
|
995
678
|
*/
|
|
996
679
|
getGroupNode(path) {
|
|
997
680
|
const tree = this.buildGroupTree();
|
|
998
|
-
if (path ===
|
|
681
|
+
if (path === "/")
|
|
999
682
|
return tree;
|
|
1000
|
-
const parts = path.split(
|
|
683
|
+
const parts = path.split("/").filter(Boolean);
|
|
1001
684
|
let current = tree;
|
|
1002
685
|
for (const part of parts) {
|
|
1003
|
-
const child = current.children.find(c => c.name === part);
|
|
686
|
+
const child = current.children.find((c) => c.name === part);
|
|
1004
687
|
if (!child)
|
|
1005
688
|
return null;
|
|
1006
689
|
current = child;
|
|
@@ -1012,19 +695,19 @@ export class DataTree {
|
|
|
1012
695
|
* Returns array of {name, path} from root to target
|
|
1013
696
|
*/
|
|
1014
697
|
getBreadcrumbs(groupPath) {
|
|
1015
|
-
if (groupPath ===
|
|
1016
|
-
return [{ name:
|
|
698
|
+
if (groupPath === "/") {
|
|
699
|
+
return [{ name: "root", path: "/" }];
|
|
1017
700
|
}
|
|
1018
|
-
const parts = groupPath.split(
|
|
701
|
+
const parts = groupPath.split("/").filter(Boolean);
|
|
1019
702
|
const breadcrumbs = [
|
|
1020
|
-
{ name:
|
|
703
|
+
{ name: "root", path: "/" },
|
|
1021
704
|
];
|
|
1022
|
-
let currentPath =
|
|
705
|
+
let currentPath = "";
|
|
1023
706
|
for (const part of parts) {
|
|
1024
|
-
currentPath +=
|
|
707
|
+
currentPath += "/" + part;
|
|
1025
708
|
breadcrumbs.push({
|
|
1026
709
|
name: part,
|
|
1027
|
-
path: currentPath
|
|
710
|
+
path: currentPath,
|
|
1028
711
|
});
|
|
1029
712
|
}
|
|
1030
713
|
return breadcrumbs;
|
|
@@ -1034,28 +717,28 @@ export class DataTree {
|
|
|
1034
717
|
*/
|
|
1035
718
|
searchGroups(query) {
|
|
1036
719
|
const lowerQuery = query.toLowerCase();
|
|
1037
|
-
return this.listAllGroups().filter(g => g.name.toLowerCase().includes(lowerQuery) ||
|
|
720
|
+
return this.listAllGroups().filter((g) => g.name.toLowerCase().includes(lowerQuery) ||
|
|
1038
721
|
g.path.toLowerCase().includes(lowerQuery));
|
|
1039
722
|
}
|
|
1040
723
|
// --------------------------------------------------
|
|
1041
724
|
// Variables
|
|
1042
725
|
// --------------------------------------------------
|
|
1043
726
|
/** variables inside a group */
|
|
1044
|
-
getAllVariables(groupPath =
|
|
727
|
+
getAllVariables(groupPath = "/") {
|
|
1045
728
|
const group = this.getGroup(groupPath);
|
|
1046
729
|
return group?.variables || {};
|
|
1047
730
|
}
|
|
1048
731
|
/** check if group has variables */
|
|
1049
|
-
hasVariables(groupPath =
|
|
732
|
+
hasVariables(groupPath = "/") {
|
|
1050
733
|
const vars = this.getAllVariables(groupPath);
|
|
1051
734
|
return Object.keys(vars).length > 0;
|
|
1052
735
|
}
|
|
1053
736
|
/** count variables in a group */
|
|
1054
|
-
getVariableCount(groupPath =
|
|
737
|
+
getVariableCount(groupPath = "/") {
|
|
1055
738
|
return Object.keys(this.getAllVariables(groupPath)).length;
|
|
1056
739
|
}
|
|
1057
740
|
/** get variable names as array */
|
|
1058
|
-
getVariableNames(groupPath =
|
|
741
|
+
getVariableNames(groupPath = "/") {
|
|
1059
742
|
return Object.keys(this.getAllVariables(groupPath));
|
|
1060
743
|
}
|
|
1061
744
|
/**
|
|
@@ -1071,7 +754,7 @@ export class DataTree {
|
|
|
1071
754
|
results.push({
|
|
1072
755
|
name: varName,
|
|
1073
756
|
groupPath,
|
|
1074
|
-
path: `${groupPath ===
|
|
757
|
+
path: `${groupPath === "/" ? "" : groupPath}/${varName}`,
|
|
1075
758
|
});
|
|
1076
759
|
}
|
|
1077
760
|
}
|
|
@@ -1081,41 +764,41 @@ export class DataTree {
|
|
|
1081
764
|
searchInGroup(path);
|
|
1082
765
|
}
|
|
1083
766
|
};
|
|
1084
|
-
searchInGroup(
|
|
767
|
+
searchInGroup("/");
|
|
1085
768
|
return results;
|
|
1086
769
|
}
|
|
1087
770
|
// --------------------------------------------------
|
|
1088
771
|
// Attributes
|
|
1089
772
|
// --------------------------------------------------
|
|
1090
773
|
/** attributes from the tree (fast) */
|
|
1091
|
-
getAttributes(groupPath =
|
|
774
|
+
getAttributes(groupPath = "/") {
|
|
1092
775
|
const group = this.getGroup(groupPath);
|
|
1093
776
|
return group?.attributes || {};
|
|
1094
777
|
}
|
|
1095
778
|
/** check if group has attributes */
|
|
1096
|
-
hasAttributes(groupPath =
|
|
779
|
+
hasAttributes(groupPath = "/") {
|
|
1097
780
|
const attrs = this.getAttributes(groupPath);
|
|
1098
781
|
return Object.keys(attrs).length > 0;
|
|
1099
782
|
}
|
|
1100
783
|
/** count attributes in a group */
|
|
1101
|
-
getAttributeCount(groupPath =
|
|
784
|
+
getAttributeCount(groupPath = "/") {
|
|
1102
785
|
return Object.keys(this.getAttributes(groupPath)).length;
|
|
1103
786
|
}
|
|
1104
787
|
// --------------------------------------------------
|
|
1105
788
|
// Dimensions
|
|
1106
789
|
// --------------------------------------------------
|
|
1107
790
|
/** get dimensions for a group */
|
|
1108
|
-
getDimensions(groupPath =
|
|
791
|
+
getDimensions(groupPath = "/") {
|
|
1109
792
|
const group = this.getGroup(groupPath);
|
|
1110
793
|
return group?.dimensions || {};
|
|
1111
794
|
}
|
|
1112
795
|
/** check if group has dimensions */
|
|
1113
|
-
hasDimensions(groupPath =
|
|
796
|
+
hasDimensions(groupPath = "/") {
|
|
1114
797
|
const dims = this.getDimensions(groupPath);
|
|
1115
798
|
return Object.keys(dims).length > 0;
|
|
1116
799
|
}
|
|
1117
800
|
/** count dimensions in a group */
|
|
1118
|
-
getDimensionCount(groupPath =
|
|
801
|
+
getDimensionCount(groupPath = "/") {
|
|
1119
802
|
return Object.keys(this.getDimensions(groupPath)).length;
|
|
1120
803
|
}
|
|
1121
804
|
// --------------------------------------------------
|
|
@@ -1124,7 +807,7 @@ export class DataTree {
|
|
|
1124
807
|
/**
|
|
1125
808
|
* Get summary statistics for a group
|
|
1126
809
|
*/
|
|
1127
|
-
getGroupSummary(groupPath =
|
|
810
|
+
getGroupSummary(groupPath = "/") {
|
|
1128
811
|
const group = this.getGroup(groupPath);
|
|
1129
812
|
if (!group)
|
|
1130
813
|
return null;
|
|
@@ -1135,7 +818,7 @@ export class DataTree {
|
|
|
1135
818
|
attributeCount: this.getAttributeCount(groupPath),
|
|
1136
819
|
dimensionCount: this.getDimensionCount(groupPath),
|
|
1137
820
|
subgroupCount: this.listGroups(groupPath).length,
|
|
1138
|
-
hasSubgroups: this.hasSubgroups(groupPath)
|
|
821
|
+
hasSubgroups: this.hasSubgroups(groupPath),
|
|
1139
822
|
};
|
|
1140
823
|
}
|
|
1141
824
|
/**
|
|
@@ -1156,13 +839,13 @@ export class DataTree {
|
|
|
1156
839
|
countInGroup(path, depth + 1);
|
|
1157
840
|
}
|
|
1158
841
|
};
|
|
1159
|
-
countInGroup(
|
|
842
|
+
countInGroup("/", 0);
|
|
1160
843
|
return {
|
|
1161
844
|
totalGroups: this.listAllGroups().length + 1, // +1 for root
|
|
1162
845
|
totalVariables,
|
|
1163
846
|
totalAttributes,
|
|
1164
847
|
totalDimensions,
|
|
1165
|
-
maxDepth
|
|
848
|
+
maxDepth,
|
|
1166
849
|
};
|
|
1167
850
|
}
|
|
1168
851
|
// --------------------------------------------------
|