Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ netcdf4-wasm/
│ ├── netcdf-worker.ts # Web Worker implementation
│ ├── netcdf4-wasm.d.ts # TypeScript declarations
│ ├── netcdf4.ts # Main NetCDF4 class
│ ├── operations.ts # Operations shared by the main thread and the worker
│ ├── types.ts # Type definitions
│ ├── variable.ts # Variable class
│ └── wasm-module.ts # WASM module loader
Expand Down
168 changes: 4 additions & 164 deletions src/netcdf-worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NC_CONSTANTS } from "./constants.js";
import * as NCGet from "./netcdf-getters.js";
import { isOperationName, runOperation } from "./operations.js";
import {
checkRangeSupport,
createRemoteFileNode,
Expand Down Expand Up @@ -94,169 +93,10 @@ self.onmessage = async (e: MessageEvent) => {
// Only report read errors raised by this request
if (remoteFile) remoteFile.lastError = undefined;

let result;

switch (type) {
case "open": {
const openResult = mod.nc_open(data.path, data.modeValue);
if (openResult.result !== NC_CONSTANTS.NC_NOERR) {
throw new Error("nc_open failed: " + openResult.result);
}
result = openResult.ncid as number;
break;
}

case "close": {
const closeResult = mod.nc_close(data.ncid);
if (closeResult !== NC_CONSTANTS.NC_NOERR) {
throw new Error("Failed to close file");
}
result = closeResult;
break;
}

// ---- Globals ----
case "getGlobalAttributes":
result = NCGet.getGlobalAttributes(
mod,
data.ncid,
data.groupPath,
);
break;

case "getFullMetadata":
result = NCGet.getFullMetadata(mod, data.ncid, data.groupPath);
break;

// ---- Dims ----
case "getDimCount":
result = NCGet.getDimCount(mod, data.ncid);
break;

case "getDimIDs":
result = Array.from(NCGet.getDimIDs(mod, data.ncid));
break;

case "getDim":
result = NCGet.getDim(mod, data.ncid, data.dimid);
break;

case "getDims":
result = NCGet.getDims(mod, data.ncid, data.groupPath);
break;

// ---- Vars ----
case "getGroupVariables":
// console.log('Worker: getGroupVariables', data.ncid);
result = NCGet.getGroupVariables(
mod,
data.ncid,
data.groupPath,
);
break;

case "getVarIDs":
result = Array.from(NCGet.getVarIDs(mod, data.ncid));
break;

case "getVarCount":
result = NCGet.getVarCount(mod, data.ncid);
break;

case "getAttributeName":
result = NCGet.getAttributeName(
mod,
data.ncid,
data.varid,
data.attId,
);
break;

case "getVariableInfo":
result = NCGet.getVariableInfo(
mod,
data.ncid,
data.variable,
data.groupPath,
);
break;

case "getAttributeValues":
result = NCGet.getAttributeValues(
mod,
data.ncid,
data.varid,
data.attname,
);
break;

case "getVariableArrayWithSelection":
result = NCGet.getVariableArrayWithSelection(
mod,
data.ncid,
data.variable,
data.selection,
data.groupPath,
data.options,
);
break;

// ---- Arrays ----
case "getVariableArray":
result = NCGet.getVariableArray(
mod,
data.ncid,
data.variable,
data.groupPath,
);
break;

case "getSlicedVariableArray":
result = NCGet.getSlicedVariableArray(
mod,
data.ncid,
data.variable,
data.start,
data.count,
data.groupPath,
);
break;

// ---- Groups ----
case "getGroups":
result = NCGet.getGroups(mod, data.ncid);
break;

case "getGroupsRecursive":
result = NCGet.getGroupsRecursive(mod, data.ncid);
break;

case "getGroupNCID":
result = NCGet.getGroupNCID(mod, data.ncid, data.groupPath);
break;

case "getGroupName":
result = NCGet.getGroupName(mod, data.ncid);
break;

case "getGroupPath":
result = NCGet.getGroupPath(mod, data.ncid);
break;
case "getCompleteHierarchy":
result = NCGet.getCompleteHierarchy(
mod,
data.ncid,
data.groupPath,
);
break;

case "getVariables":
result = NCGet.getVariables(mod, data.ncid);
break;

default:
throw new Error(`Unknown message type: ${type}`);
if (!isOperationName(type)) {
throw new Error(`Unknown message type: ${type}`);
}
const result = runOperation(mod, type, data.args);

self.postMessage({ id, success: true, result });
} catch (err: any) {
Expand Down
Loading
Loading