NodeJS addon for reading and writing the files in the Network Common Data Form (NetCDF) version <= 4, built upon the C-library for netcdf.
netcdf4-js
is built with nodejs
>= 4.x
You will need libnetcdf
>= 4.x installed. Either build from
https://www.unidata.ucar.edu/downloads/netcdf/index.jsp or
use your package manager (e.g., the package is called libnetcdf-dev
in Ubuntu/Debian).
Then install using npm install netcdf4
.
Open files with
var netcdf4 = require("netcdf4");
var file = new netcdf4.File("test/testrh.nc", "r");
File modes are "r"
for "reading", "w"
for "writing", "c"
for
"creation", and "c!"
for "overwriting".
Then you can read variables using read
or readSlice
. The following example reads values at positions 5 to 15:
console.log(file.root.variables['var1'].readSlice(5, 10));
Properties marked (r/w) can be read and will be written to the file when set.
Properties:
root
: MainGroup
-object in file
Methods:
close()
: Close filesync()
: Sync (or "flush") file to disk
Properties:
id
: ID used by C-libraryname
: Namefullname
: Full name (path in file)variables
: Associative array of variables in groupdimensions
: Associative array of dimensions in groupunlimited
: Associative array of unlimited Dimensions in groupattribute
: Associative array of attributes of groupsubgroups
: Associative array of subgroups of group
Methods:
addVariable(name, type, dimensions)
: Add a new variable in group.type
is one of"byte", "char", "short", "int", "ubyte", "ushort", "uint", "float", "double"
.dimensions
is an array of ids of dimensions for the new variable. Returns new variable.addDimension(name, length)
: Add new dimension of lengthlength
(can be"unlimited"
for unlimited dimension). Returns new dimension.addSubgroup(name)
: Add subgroup. Returns new subgroup.addAttribute(name, value)
: Add and set new attribute. Returns new attribute.
Properties:
id
: ID used by C-libraryname
: Name (r/w)length
: Length or currently used length for unlimited dimensions
Properties:
id
: ID used by C-libraryname
: Name (r/w)value
: Value (r/w)
Methods:
delete()
: Delete attribute
Properties:
id
: ID used by C-libraryname
: Name (r/w)type
: Type of variableattributes
: Associative array of attributesdimensions
: Array of dimensions used by variableendianness
: Endianness:"little"
,"big"
, or"native"
(r/w)checksummode
: Checksum mode:"none"
, or"fletcher32"
(r/w)chunkmode
: Chunk mode:"contiguous"
, or"chunked"
(r/w)chunksizes
: Array of chunk sizes (one size per dimension) (r/w)fillmode
: Boolean switch for fill mode (r/w)fillvalue
: Fill value (r/w)compressionshuffle
: Boolean switch for shuffle (r/w)compressiondeflate
: Boolean switch for compression (r/w)compressionlevel
: Compression level (1-9) (r/w)
Methods:
read(pos....)
: Reads and returns a single value at positions given as forwrite
.readSlice(pos....)
: Reads and returns an array of values (cf. "Specify a Hyperslab") at positions and sizes given for each dimension,readSlice(pos1, size1, pos2, size2, ...)
e.g.readSlice(2, 3, 4, 2)
gives an array of the values at position 2 for 3 steps along the first dimension and position 4 for 2 step along the second one.write(pos..., value)
: Writevalue
at positions given, e.g.write(2, 3, "a")
writes"a"
at position 2 along the first dimension and position 3 along the second one.writeSlice(pos/size..., valuearray)
: Write values invaluearray
(must be a typed array) at positions and sizes given for each dimension, e.g.writeSlice(2, 3, 4, 2, new Int32Array([0, 1, 2, 3, 4, 5]))
writes the array at position 2 for 3 steps along the first dimension and position 4 for 2 step along the second one (cf. "Specify a Hyperslab").addAttribute(name, value)
: Adds and sets new attribute. Returns new attribute.