netcdfjs

repository·main·Indexed 41 days ago

https://github.com/cheminfo/netcdfjs

A 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.

Tokens
2.4K
Snippets
9
Records
15
Agent score
49%

What's inside netcdfjs

  1. Read NetCDF v3 files with NetCDFReader

    main

    To 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');
  2. NetCDFReader API Reference

    main

    The NetCDFReader class provides access to the metadata and data contained within a NetCDF v3 file.

    Properties

    • 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.

    Methods

    • 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.
  3. Parse a NetCDF file header with header()

    main

    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:

    • A 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);
  4. Initialize NetCDFReader to read NetCDF v3.x files

    main

    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);
  5. Retrieve global attributes from a NetCDF file

    main

    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);
    }
  6. Import NetCDFJS parser and types

    main

    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';
  7. Explore NetCDF file metadata with NetCDFReader

    main

    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.
  8. Convert a NetCDFReader to a human-readable string with netcdfToString()

    main

    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:

    • DIMENSIONS: A list of dimension names and their sizes.
    • GLOBAL ATTRIBUTES: A list of all global attributes and their values.
    • VARIABLES: A list of variables, their values (truncated to the first 50 characters if they are long), and the length if the variable is an array.

    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);
  9. Convert NetCDF metadata to a human-readable string

    main

    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);
  10. Retrieve data variables from a NetCDF file

    main

    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');
    }
  11. Reference the Dimensions interface

    main

    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.