vol_read_npy: Read a NumPy array file

View source: R/npy.R

vol_read_npyR Documentation

Read a NumPy array file

Description

Reads a .npy file into an R array, without calling Python. The format is a short ASCII header followed by the raw buffer, so no package is needed to parse it.

Usage

vol_read_npy(path)

Arguments

path

Path to a .npy file.

Details

Read a NumPy array file

The reader covers the little-endian and big-endian floating-point types f4 and f8, the signed integer types i1, i2, i4 and i8, the unsigned integer types u1, u2 and u4, and the boolean type b1. An unsigned u4 file returns doubles, since its values may pass the largest R integer; any other type is refused. C order is the default that NumPy writes and is transposed on the way in, so dim() of the result matches the shape field of the header. A Fortran-ordered file is refused rather than read wrongly.

Values are carried across exactly. A f4 file widens to double with no loss, since every single-precision number is a double-precision number.

Value

An array of the shape the header states, of type double for a floating-point file, integer for an integer file and logical for a boolean file. A one-dimensional file returns a bare vector.

References

van der Walt, S., Colbert, S. C., & Varoquaux, G. (2011). The NumPy array: a structure for efficient numerical computation. Computing in Science & Engineering, 13(2), 22-30. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1109/MCSE.2011.37")}

See Also

vol_field() to wrap arrays as a field, vol_read_netcdf() for the other format the reader accepts, vol_payload() to build the browser payload from a field.

Examples

a <- vol_read_npy(system.file("extdata", "example.npy", package = "volumr"))
dim(a)
a

volumr documentation built on Dec. 2, 2026, 5:07 p.m.