cdf

package
v0.0.0-...-5be112a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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

func CheckName(name string) error

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

func FillBytes(t Type) ([]byte, error)

FillBytes returns the default fill value's external representation for t: the values NC_FILL_* names, big-endian, one value wide.

func ReadAtFull

func ReadAtFull(r io.ReaderAt, p []byte, off int64) error

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

type Attr struct {
	Name string
	Type Type
	Data []byte
}

Attr is one attribute. Data holds the values' external representation (big-endian, no padding), exactly Len values of Type.

func (Attr) Len

func (a Attr) Len() int

Len returns the number of values the attribute holds.

type Dim

type Dim struct {
	Name string
	Size int64 // 0 marks the record (unlimited) dimension
}

Dim is one entry of the dimensions list.

func (Dim) Unlimited

func (d Dim) Unlimited() bool

Unlimited reports whether the dimension is the record dimension.

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

func ReadHeader(r io.ReaderAt, size int64) (*Header, error)

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

func (h *Header) CheckWriteSelection(v *Var, start, count []int64) error

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

func (h *Header) EncodeSize() (int64, error)

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

func (h *Header) ExtendRecs(w io.WriterAt, to int64, fill func(*Var) []byte) error

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

func (h *Header) FillFixed(w io.WriterAt, fill func(*Var) []byte) error

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

func (h *Header) FillRecords(w io.WriterAt, from, to int64, fill func(*Var) []byte) error

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

func (h *Header) Layout() error

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

func (h *Header) Validate() error

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

func (h *Header) WalkVara(v *Var, start, count []int64, fn func(off, n int64) error) error

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

func (h *Header) WriteHeader(w io.Writer) error

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.

func (*Header) WriteNumRecs

func (h *Header) WriteNumRecs(w io.WriterAt) error

WriteNumRecs writes the header's record count at its fixed position, after the four-byte magic. Close calls it once the data is written.

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.

func (Type) Size

func (t Type) Size() int

Size returns the number of bytes one value of the type occupies in the file. It returns 0 for a type tag the formats do not define.

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.

const (
	Version1 Version = 1 // CDF-1, 32-bit offsets
	Version2 Version = 2 // CDF-2, 64-bit offsets
	Version5 Version = 5 // CDF-5, 64-bit offsets and 64-bit sizes
)

The three classic versions.

func (Version) String

func (v Version) String() string

String returns the format's name as the NetCDF documentation writes it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL