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
139 changes: 28 additions & 111 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@
}
],
"license": "MIT",
"dependencies": {
"@types/emscripten": "^1.41.6"
},
"devDependencies": {
"@biomejs/biome": "^2.6.4",
"@types/node": "^22.20.5",
"@vitest/coverage-v8": "^5.0.5",
"cpx2": "^8.0.0",
"rimraf": "^6.1.2",
"cpx2": "^9.0.0",
"rimraf": "^6.1.3",
"typescript": "^5.0.0",
"vitest": "^5.0.5"
},
Expand Down
32 changes: 10 additions & 22 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,16 @@ export const NC_CONSTANTS = {
// Shuffle filter
NC_NOSHUFFLE: 0,
NC_SHUFFLE: 1,
};
} as const;

// netcdf4-python modes: whether they create the file, and the nc_open/nc_create flags
export const FILE_MODES = {
r: { create: false, flags: NC_CONSTANTS.NC_NOWRITE },
"r+": { create: false, flags: NC_CONSTANTS.NC_WRITE },
a: { create: false, flags: NC_CONSTANTS.NC_WRITE },
w: { create: true, flags: NC_CONSTANTS.NC_CLOBBER },
"w-": { create: true, flags: NC_CONSTANTS.NC_NOCLOBBER },
} as const;

// Data type mapping from string names to NetCDF constants
export const DATA_TYPE_MAP: { [key: string]: number } = {
Expand Down Expand Up @@ -245,24 +254,3 @@ export const DATA_TYPE_SIZE: { [key: number]: number } = {
11: 8, // NC_UINT64
12: 0, // NC_STRING (variable length)
};

// Human-readable type names
export const TYPE_NAMES: { [key: number]: string } = {
0: "NAT",
1: "byte",
2: "char",
3: "short",
4: "int",
5: "float",
6: "double",
7: "ubyte",
8: "ushort",
9: "uint",
10: "int64",
11: "uint64",
12: "string",
13: "vlen",
14: "opaque",
15: "enum",
16: "compound",
};
17 changes: 17 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Error handling helpers

/** Turn a caught value, which can be anything that was thrown, into an Error. */
export function toError(err: unknown): Error {
if (err instanceof Error) {
return err;
}
if (
typeof err === "object" &&
err !== null &&
"message" in err &&
typeof err.message === "string"
) {
return new Error(err.message);
}
return new Error(String(err));
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import { NetCDF4 } from "./netcdf4.js";
import type {
DatasetOptions,
DatasetSource,
FileMode,
RemoteDatasetOptions,
} from "./types.js";

export async function Dataset(
source: DatasetSource,
mode: string = "r",
mode: FileMode = "r",
options: RemoteDatasetOptions = {},
): Promise<NetCDF4> {
// Type detection and routing
Expand All @@ -61,23 +62,23 @@ export async function Dataset(
// Legacy convenience functions for backward compatibility
export async function DatasetFromBlob(
blob: Blob,
mode: string = "r",
mode: FileMode = "r",
options: DatasetOptions = {},
): Promise<NetCDF4> {
return await Dataset(blob, mode, options);
}

export async function DatasetFromArrayBuffer(
buffer: ArrayBuffer,
mode: string = "r",
mode: FileMode = "r",
options: DatasetOptions = {},
): Promise<NetCDF4> {
return await Dataset(buffer, mode, options);
}

export async function DatasetFromMemory(
data: Uint8Array | ArrayBuffer,
mode: string = "r",
mode: FileMode = "r",
options: DatasetOptions = {},
filename?: string,
): Promise<NetCDF4> {
Expand Down
Loading
Loading