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