Documentation
¶
Overview ¶
Package cdf implements the byte-level reading of the classic netCDF file formats: CDF-1 (version byte 1, 32-bit offsets), CDF-2 (version 2, 64-bit offsets) and CDF-5 (version 5, 64-bit offsets and 64-bit sizes).
The formats are defined by the NetCDF Classic Format Specification; the reference implementation is netCDF-C (libsrc/v1hpg.c, var.c, nc3internal.c). Every value in the header and in the data is stored big-endian, and this package decodes with an explicit binary.BigEndian / manual shifts only.
The package parses a header into Header, computes how a hyperslab of a variable maps to file offsets, and encodes a header back, with the layout and fill rules of the reference writer. It does no conversion of data types; the caller decodes and encodes the bytes it asks for.
Security: the input is untrusted. Every count, length and offset that comes from the file is validated against the bytes that remain before memory is allocated or a read is issued, and every arithmetic step is overflow-checked.
Index ¶
- Variables
- func CheckName(name string) error
- func FillBytes(t Type) ([]byte, error)
- func ReadAtFull(r io.ReaderAt, p []byte, off int64) error
- type Attr
- type Dim
- type Header
- func (h *Header) CheckWriteSelection(v *Var, start, count []int64) error
- func (h *Header) EncodeSize() (int64, error)
- func (h *Header) ExtendRecs(w io.WriterAt, to int64, fill func(*Var) []byte) error
- func (h *Header) FillFixed(w io.WriterAt, fill func(*Var) []byte) error
- func (h *Header) FillRecords(w io.WriterAt, from, to int64, fill func(*Var) []byte) error
- func (h *Header) Layout() error
- func (h *Header) Validate() error
- func (h *Header) WalkVara(v *Var, start, count []int64, fn func(off, n int64) error) error
- func (h *Header) WriteHeader(w io.Writer) error
- func (h *Header) WriteNumRecs(w io.WriterAt) error
- type Type
- type Var
- type Version
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotNetCDF reports input that does not start with a classic header. ErrNotNetCDF = errors.New("not a classic netCDF file") // ErrCorrupt reports a header or data section that violates the format. ErrCorrupt = errors.New("corrupt netCDF file") // ErrOutOfRange reports a selection outside the variable's shape. ErrOutOfRange = errors.New("selection out of range") )
Format violations and input classification.
Functions ¶
func CheckName ¶
CheckName validates a name by netCDF-C's NC_check_name rules: non-empty, valid UTF-8, no '/', the first character is [A-Za-z0-9_] or a multibyte UTF-8 character, later characters are printable ASCII or UTF-8, no trailing space, at most NC_MAX_NAME (256) bytes.
Names are stored as given, in whatever UTF-8 form the caller wrote. Unlike netCDF-C this package does not NFC-normalise them (libsrc/dim.c, var.c): a decomposed name round-trips unchanged instead of being composed, which needs a Unicode normalisation table the package does not carry.
func FillBytes ¶
FillBytes returns the default fill value's external representation for t: the values NC_FILL_* names, big-endian, one value wide.
func ReadAtFull ¶
ReadAtFull reads len(p) bytes at off. io.ReaderAt permits a complete read to report io.EOF when it ends exactly at the end of the input; that is a success. A short read is io.ErrUnexpectedEOF, a truncated input; any other error (a timeout, a closed reader) is returned unchanged, so a caller can tell corruption from an I/O failure.
Types ¶
type Attr ¶
Attr is one attribute. Data holds the values' external representation (big-endian, no padding), exactly Len values of Type.
type Header ¶
type Header struct {
Version Version
NumRecs int64 // resolved record count (STREAMING is computed from the file)
Dims []Dim
Attrs []Attr // global attributes
Vars []Var
HeaderSize int64 // bytes the header occupies, the first data offset
RecSize int64 // bytes one record occupies; 0 without record variables
}
Header is a parsed classic header with every variable's geometry computed.
func ReadHeader ¶
ReadHeader parses the header of the classic file r whose total length is size, and returns it with every variable's geometry computed and validated.
Input that does not start with a classic magic yields ErrNotNetCDF; input that starts like one but violates the format yields an error wrapping ErrCorrupt.
func (*Header) CheckWriteSelection ¶
CheckWriteSelection validates a hyperslab for writing: the inner dimensions against the variable's shape, the record dimension only for overflow, since a write may extend the record section. The caller extends first and then walks with WalkVara, which revalidates against the new record count.
func (*Header) EncodeSize ¶
EncodeSize returns the number of bytes WriteHeader writes for h.
The header's length does not depend on the variables' starting offsets: every field has a width fixed by the format version, so a header can be sized before any begin is assigned (NC_begins relies on the same).
func (*Header) ExtendRecs ¶
ExtendRecs fills and extends the record section to to records, as a write beyond the current count does in netCDF-C. It reports the format's record count limit and any offset overflow before writing.
func (*Header) FillFixed ¶
FillFixed fills every fixed-size variable's padded data region with the bytes fill returns for it, as netCDF-C's fillerup does on creation.
func (*Header) FillRecords ¶
FillRecords fills the record slabs of records [from, to): the padded slab for each record variable, or the unpadded slab in the single-variable case (NCfillrecord and NCfillspecialrecord).
func (*Header) Layout ¶
Layout computes every variable's geometry and starting offset exactly as netCDF-C's NC_begins with its default alignment (1, no headroom): the fixed variables follow the header in definition order, each padded to four bytes, then the record variables. The header's NumRecs is left untouched.
func (*Header) Validate ¶
Validate checks a header built for writing against every rule the format imposes: names, types, list sizes, the unlimited dimension and _FillValue. ReadHeader applies the same rules to input; a header that passes Validate and Layout is one the reader accepts back.
func (*Header) WalkVara ¶
WalkVara calls fn once per contiguous run of values in the requested hyperslab, in row-major order, with the run's file offset and its length in values. start and count must have one entry per dimension of v; the record dimension of a record variable is addressed by record number.
A selection outside the variable's shape, including a negative start or count, yields ErrOutOfRange. WalkVara does not check the runs against the file length: the caller knows the file and must bounds-check before reading.
func (*Header) WriteHeader ¶
WriteHeader writes the classic header in the format's grammar, exactly as ReadHeader parses it. The variables' Begin values and the header's NumRecs are written as they are; Layout assigns the begins of a new file.
type Type ¶
type Type uint8
Type is a netCDF external data type tag.
const ( TypeByte Type = 1 // NC_BYTE TypeChar Type = 2 // NC_CHAR TypeShort Type = 3 // NC_SHORT TypeInt Type = 4 // NC_INT TypeFloat Type = 5 // NC_FLOAT TypeDouble Type = 6 // NC_DOUBLE TypeUByte Type = 7 // NC_UBYTE, CDF-5 TypeUShort Type = 8 // NC_USHORT, CDF-5 TypeUInt Type = 9 // NC_UINT, CDF-5 TypeInt64 Type = 10 // NC_INT64, CDF-5 TypeUInt64 Type = 11 // NC_UINT64, CDF-5 )
The external type tags, in the values the format stores in the header.
type Var ¶
type Var struct {
Name string
DimIDs []int64 // indexes into Header.Dims
Attrs []Attr
Type Type
Begin int64 // file offset of the variable's data
// Computed from DimIDs and the dimensions:
Shape []int64 // dimension sizes; Shape[0] == 0 for a record variable
Strides []int64 // element stride per dimension within one slab
ElemSize int // bytes per value
Record bool // the first dimension is the record dimension
DataSize int64 // bytes of values in one slab (one record, or all data)
SlabSize int64 // bytes one record occupies in the record section
}
Var is one entry of the variables list, with the geometry computed and validated by ReadHeader.
type Version ¶
type Version uint8
Version is the classic format version, as stored in the fourth magic byte.