Install netcdfjs via npm
mainInstall the netcdfjs package using npm to read and explore NetCDF v3 files in your project.
$ npm install netcdfjsrepository·main·Indexed 41 days ago
https://github.com/cheminfo/netcdfjsA JavaScript library for reading and exploring NetCDF v3 files. It provides the NetCDFReader class to access dimensions, global attributes, and variable data, as well as utilities to parse file headers and generate human-readable summaries of NetCDF metadata.
Install the netcdfjs package using npm to read and explore NetCDF v3 files in your project.
$ npm install netcdfjsTo read a NetCDF file, load the file data (e.g., using fs.readFileSync) and pass the buffer/data to a new NetCDFReader instance. You can then access variables using getDataVariable(name) or getDataVariableAsString(name).
import { readFileSync } from 'node:fs';
import { NetCDFReader } from 'netcdfjs';
// Load the file data
const data = readFileSync('madis-sao.nc');
// Initialize the reader
const reader = new NetCDFReader(data);
// Access variable values
const wmoId = reader.getDataVariable('wmoId');The NetCDFReader class provides access to the metadata and data contained within a NetCDF v3 file.
version: Returns 'classic format' or '64-bit offset format'.dimensions: A list of dimensions, each containing a name and a size.recordDimension: Metadata of the record dimension (includes length, id, name, and recordStep).globalAttributes: A list of global attributes, each with a name, type, and value.variables: A list of variables, each with a name, dimensions, attributes, type, size, and offset.getAttribute(name): Returns the value of a global attribute, or null if it does not exist.attributeExists(name): Returns true if a global attribute exists, otherwise false.getDataVariable(nameOrVariable): Returns the values of a variable. Throws an error if the variable does not exist.getDataVariableAsString(name): Returns the values of a variable joined into a single string, or null if the variable does not exist.dataVariableExists(name): Returns true if a variable exists, otherwise false.toString(): Returns a human-readable description of the dimensions, global attributes, and variables.Use the header function to parse the metadata from a NetCDF file buffer. This function returns a Header object containing the file version, dimensions, global attributes, and variables.
Parameters:
buffer: An IOBuffer containing the file data.version: The version of the NetCDF file.Returns:
Header object representing the file's structure.import { header } from 'netcdfjs';
// Assuming 'buffer' is an IOBuffer instance
const fileHeader = header(buffer, version);
console.log(fileHeader.dimensions);
console.log(fileHeader.variables);Use the NetCDFReader class to parse NetCDF v3.x files. The constructor accepts InputData, which can be an ArrayBuffer or any TypedArray (including Node.js Buffer). The reader automatically sets the byte order to Big Endian and validates the file signature (must start with CDF).
import { NetCDFReader } from 'netcdfjs';
// Assuming 'data' is an ArrayBuffer or TypedArray from a NetCDF file
const reader = new NetCDFReader(data);You can check for the existence of or retrieve specific global attributes using these methods:
attributeExists(attributeName: string): Returns true if the attribute exists.getAttribute(attributeName: string): Returns the value of the attribute, or null if not found.if (reader.attributeExists('description')) {
const desc = reader.getAttribute('description');
console.log(desc);
}The netcdfjs package exports the core parser functionality and several key types for interacting with NetCDF file structures. Use the main entrypoint to access the parser and the following types to type your NetCDF data structures:
Attribute: Represents metadata attributes.Header: Represents the file header structure.Variable: Represents a data variable within the NetCDF file.import { NetCDFParser } from 'netcdfjs'; // Assuming NetCDFParser is the primary export from parser.ts
import type { Attribute, Header, Variable } from 'netcdfjs';The NetCDFReader provides several getters to inspect the file's structure and metadata:
version: Returns 'classic format' (v1) or '64-bit offset format' (v2).dimensions: Returns an array of dimension objects, each containing name (string) and size (number).globalAttributes: Returns an array of global attribute objects, each containing name (string), type (string), and value (number or string).variables: Returns an array of variable objects defined in the header.recordDimension: Returns metadata for the record dimension, including length, id, name, and recordStep.Use the netcdfToString function to generate a human-readable text summary of a NetCDFReader instance. The resulting string provides a structured overview of the file's contents, including:
This is useful for debugging or logging the structure and metadata of a NetCDF file without inspecting the full data arrays.
import { netcdfToString } from 'netcdfjs';
// Assuming 'reader' is an instance of NetCDFReader
const description = netcdfToString(reader);
console.log(description);The NetCDFReader implements a toString() method that returns a human-readable description of the file's dimensions, global attributes, and variables.
const description = reader.toString();
console.log(description);To access the actual data stored in variables, use getDataVariable. This method supports both searching by name and passing a variable object directly.
dataVariableExists(variableName: string): Returns true if the variable exists.getDataVariable(variableName: string | Header['variables'][number]): Returns the variable values. It handles both record and non-record variables automatically. Throws an error if the variable is not found.getDataVariableAsString(variableName: string): A convenience method that returns the variable's data joined as a single string, or null if the variable doesn't exist.// By name
const temperature = reader.getDataVariable('temperature');
// As a string
const metadataString = reader.getDataVariableAsString('metadata_text');
// Check existence first
if (reader.dataVariableExists('pressure')) {
const pressure = reader.getDataVariable('pressure');
}The Dimensions interface describes the dimensions available in the NetCDF file.
dimensions: An array of objects, each containing:name: The string name of the dimension.size: The size of the dimension.recordId (optional): The ID of the dimension that has unlimited size (the record dimension).recordName (optional): The name of the dimension that has unlimited size.