netcdf

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 12 Imported by: ? 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 netcdf fetches and reads classic netCDF files, and writes them. It wraps the whole of github.com/scigolib/netcdf, and on it reads gridded fields that follow the CF conventions: a named quantity over (time, latitude, longitude).

Which file and which variable are asked for is the caller's; nothing here is about one reanalysis.

Its refusals wrap celestialdata.Refused. An error the reader under it gave stays reachable with errors.Is, so the two are told apart.

The fetch is one example file of Unidata's: surface fields for July 2002 from the ECMWF 40-year reanalysis.

Index

Constants

View Source
const (
	// FormatUnknown means the header matches no netCDF format.
	FormatUnknown = classic.FormatUnknown
	// FormatClassic is CDF-1, with 32-bit offsets.
	FormatClassic = classic.FormatClassic
	// FormatClassic64 is CDF-2, with 64-bit offsets.
	FormatClassic64 = classic.FormatClassic64
	// FormatClassic64Data is CDF-5, with 64-bit offsets and sizes.
	FormatClassic64Data = classic.FormatClassic64Data
	// FormatHDF5 is NetCDF-4, which this adapter does not read yet.
	FormatHDF5 = classic.FormatHDF5
)

The formats. This adapter reads and writes the classic three.

View Source
const (
	// TypeByte is a signed 8-bit integer.
	TypeByte = classic.TypeByte
	// TypeChar is a text character.
	TypeChar = classic.TypeChar
	// TypeShort is a signed 16-bit integer.
	TypeShort = classic.TypeShort
	// TypeInt is a signed 32-bit integer.
	TypeInt = classic.TypeInt
	// TypeFloat is an IEEE single.
	TypeFloat = classic.TypeFloat
	// TypeDouble is an IEEE double.
	TypeDouble = classic.TypeDouble
	// TypeUByte is an unsigned 8-bit integer (CDF-5).
	TypeUByte = classic.TypeUByte
	// TypeUShort is an unsigned 16-bit integer (CDF-5).
	TypeUShort = classic.TypeUShort
	// TypeUInt is an unsigned 32-bit integer (CDF-5).
	TypeUInt = classic.TypeUInt
	// TypeInt64 is a signed 64-bit integer (CDF-5).
	TypeInt64 = classic.TypeInt64
	// TypeUInt64 is an unsigned 64-bit integer (CDF-5).
	TypeUInt64 = classic.TypeUInt64
)

The eleven types. CDF-1 and CDF-2 know the first six; CDF-5 all.

Variables

View Source
var (
	// ErrNotNetCDF is input that is not a netCDF file.
	ErrNotNetCDF = classic.ErrNotNetCDF
	// ErrCorrupt is a file whose header or data breaks the format.
	ErrCorrupt = classic.ErrCorrupt
	// ErrOutOfRange is a selection outside a variable, or a destination
	// of the wrong length.
	ErrOutOfRange = classic.ErrOutOfRange
	// ErrUnsupportedFormat is a format the reader does not read yet.
	ErrUnsupportedFormat = classic.ErrUnsupportedFormat
	// ErrTypeMismatch is a destination of the wrong element type.
	ErrTypeMismatch = classic.ErrTypeMismatch
	// ErrClosed is the use of a file after Close.
	ErrClosed = classic.ErrClosed
	// ErrInvalidSchema is a layout that cannot be written.
	ErrInvalidSchema = classic.ErrInvalidSchema
)

The reader's errors, the same values, so that errors.Is finds them in what this package returns and says whose they are.

Functions

func FormatName

func FormatName(format Format) string

FormatName is a format's name as the netCDF documentation writes it.

func New

func New() (*celestialdata.Fixed, error)

New is the archive of the example file, asked by name only. It holds the one file, whatever the circle. Its sha256, in sources.yaml, is not checked by a fetch, which has nowhere to hold it.

func TypeName

func TypeName(typ Type) string

TypeName is a type's name as CDL writes it.

func TypeSize

func TypeSize(typ Type) int

TypeSize is the bytes one value of a type takes in a file.

func WriteGrid

func WriteGrid(path string, grid GridFile) error

WriteGrid writes a grid file at a path, through Layout and Writer. It checks the file first, and the written variables after, each against what was planned.

It refuses no coordinates, no variables, a step of the wrong size, a Record with no Times, and a packed value that does not fit.

Types

type Attribute

type Attribute struct {
	// contains filtered or unexported fields
}

Attribute is one attribute, global or on a variable. Its values are kept as the file stores them; Value, Text and Bytes give them.

func (*Attribute) Bytes

func (attribute *Attribute) Bytes() []byte

Bytes is the values as the file stores them, big-endian, a copy.

func (*Attribute) Length

func (attribute *Attribute) Length() int

Length is how many values the attribute holds.

func (*Attribute) Name

func (attribute *Attribute) Name() string

Name is the attribute's name.

func (*Attribute) Number

func (attribute *Attribute) Number() (float64, bool)

Number is the first value of a numeric attribute as a float64, and whether there is one. A text attribute has none.

func (*Attribute) Text

func (attribute *Attribute) Text() string

Text is a text attribute's value, and nothing for any other type.

func (*Attribute) Type

func (attribute *Attribute) Type() Type

Type is the attribute's netCDF type.

func (*Attribute) Value

func (attribute *Attribute) Value() any

Value is the values as a slice of their Go type: []int8 for a byte, []byte for a char or an unsigned byte, and []int16, []uint16, []int32, []uint32, []float32, []float64, []int64 or []uint64 for the rest.

type Dimension

type Dimension struct {
	// contains filtered or unexported fields
}

Dimension is one dimension of a file, shared by the variables that use it.

func (*Dimension) Length

func (dimension *Dimension) Length() int64

Length is the dimension's length. For the unlimited dimension it is the file's record count.

func (*Dimension) Name

func (dimension *Dimension) Name() string

Name is the dimension's name.

func (*Dimension) Unlimited

func (dimension *Dimension) Unlimited() bool

Unlimited is whether this is the record dimension.

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field is a gridded quantity in a file: one variable over (time, latitude, longitude), or over (latitude, longitude) alone, which has one step and no time. Its latitudes ascend here whichever way the file stores them.

func (*Field) All

func (field *Field) All() ([]*Slice, error)

All is every time step, in one read of the whole variable. It is how a field should be read when more than one step is wanted.

func (*Field) Latitudes

func (field *Field) Latitudes() []float64

Latitudes are the grid's latitudes in degrees, ascending, a copy.

func (*Field) LongName

func (field *Field) LongName() string

LongName is the variable's long_name, or nothing.

func (*Field) Longitudes

func (field *Field) Longitudes() []float64

Longitudes are the grid's longitudes in degrees, ascending, a copy.

func (*Field) Name

func (field *Field) Name() string

Name is the variable's name.

func (*Field) Step

func (field *Field) Step(index int) (*Slice, error)

Step is one time step of the field, read from the file by itself. It refuses a step that does not exist, naming how many there are, and reads the time units as Times does.

func (*Field) Steps

func (field *Field) Steps() int

Steps is how many time steps the field has; one with no time.

func (*Field) Times

func (field *Field) Times() ([]time.Time, error)

Times are the time of each step. A field with no time has one step and the zero time. The error says the units text it could not read, and names the file and the time variable.

func (*Field) Units

func (field *Field) Units() string

Units is the variable's units after unpacking, or nothing.

type File

type File struct {
	// contains filtered or unexported fields
}

File is an open classic netCDF file. It may be read from several goroutines at once; Close must not run beside reads.

func Open

func Open(path string) (*File, error)

Open opens the file at a path. It refuses an empty path. A NetCDF-4 (HDF5) file is refused in a sentence that says so, and its error wraps ErrUnsupportedFormat.

func Read

func Read(source Source) (*File, error)

Read reads a file from its bytes. It refuses no bytes and a negative size. Its errors are those of Open.

func (*File) Attribute

func (file *File) Attribute(name string) (*Attribute, error)

Attribute is the global attribute of a name, or a refusal that names the file and lists its attributes.

func (*File) Attributes

func (file *File) Attributes() []*Attribute

Attributes are the file's global attributes, in the order written.

func (*File) Close

func (file *File) Close() error

Close releases the file. A file read from bytes leaves its bytes open.

func (*File) Dimension

func (file *File) Dimension(name string) (*Dimension, error)

Dimension is the dimension of a name, or a refusal that names the file and lists its dimensions.

func (*File) Dimensions

func (file *File) Dimensions() []*Dimension

Dimensions are the file's dimensions, in the order written.

func (*File) Field

func (file *File) Field(name string) (*Field, error)

Field is the grid of a variable's name. Each refusal is a sentence that names the file and the variable.

It refuses a name the file does not have, a variable that is not a grid, a grid with no coordinates or with no latitudes or longitudes, longitudes that do not ascend and latitudes that do neither.

func (*File) Fields

func (file *File) Fields() []*Field

Fields are the file's variables that are grids, in the order written.

func (*File) Format

func (file *File) Format() Format

Format is the file's format, one of the classic three.

func (*File) Name

func (file *File) Name() string

Name is the file's name as errors say it: its path, or its Source's name.

func (*File) NumRecs

func (file *File) NumRecs() int64

NumRecs is the length of the file's unlimited dimension, or none for a file that has no such dimension.

It counts the dimension, not the variables: a file that declares the dimension and has no record variable may still give a count above none, where Writer.NumRecs counts the records of record variables.

func (*File) Variable

func (file *File) Variable(name string) (*Variable, error)

Variable is the variable of a name, or a refusal that names the file and lists its variables.

func (*File) Variables

func (file *File) Variables() []*Variable

Variables are the file's variables, in the order written.

type Format

type Format = classic.Format

Format is a netCDF file format, as the reader under this package names it: the three classic ones, NetCDF-4 (HDF5) and unknown.

func DetectFormat

func DetectFormat(header []byte) Format

DetectFormat is the format a file's first bytes say. Eight bytes are enough; fewer are not an error, and give FormatUnknown unless they are a whole classic header.

type GridFile

type GridFile struct {
	Format                Format
	Latitudes, Longitudes []float64
	TimeUnits             string
	Times                 []float64
	Record                bool
	Variables             []GridVariable
}

GridFile is a file of gridded fields to write. Times are counted in TimeUnits ("hours since 1900-01-01 00:00:0.0"); with no Times the variables have no time dimension and one step each.

With Record the time dimension is the unlimited one, and the variables are written a step at a time, which extends it. A Format of FormatUnknown is CDF-1.

type GridVariable

type GridVariable struct {
	Name, LongName, Units string
	Packing               *Packing
	Steps                 [][]float64
}

GridVariable is one gridded quantity to write. Each of Steps is one time step, rows by columns, the rows in the order of the file's latitudes; NaN is no data. With a Packing the values are stored as shorts, and with none as doubles.

type Layout

type Layout struct {
	// contains filtered or unexported fields
}

Layout is the design of a file to be written: its dimensions, its variables and their attributes, and its global attributes. Create writes the file from it.

func NewLayout

func NewLayout(format Format) (*Layout, error)

NewLayout is an empty layout for a file of a format. It refuses a format that is not one of the classic three.

func (*Layout) AddDimension

func (layout *Layout) AddDimension(name string,
	length int64) (*LayoutDimension, error)

AddDimension adds a dimension. It refuses an empty name and a negative length; the reader's own checks of the name come with Create.

func (*Layout) AddVariable

func (layout *Layout) AddVariable(spec VariableSpec) (*LayoutVariable,
	error)

AddVariable adds a variable. It refuses an empty name and a dimension that another layout made.

func (*Layout) Create

func (layout *Layout) Create(path string) (*Writer, error)

Create writes the file at a path from the layout and gives the writer for its values. The layout is checked here by the reader's writer: a fault in it is an error that wraps ErrInvalidSchema. The file's fixed variables are filled with their fill value at once.

func (*Layout) Dimensions

func (layout *Layout) Dimensions() []*LayoutDimension

Dimensions are the layout's dimensions, in the order added.

func (*Layout) Format

func (layout *Layout) Format() Format

Format is the format the layout will be written in.

func (*Layout) Set

func (layout *Layout) Set(name string, values any) error

Set sets a global attribute, its type taken from the Go value: a string or []byte is text, []int8 a byte, []int16 a short, []int32 an int, []float32 a float, []float64 a double, []uint16, []uint32, []int64 and []uint64 their own. A name set twice takes the last.

func (*Layout) SetTyped

func (layout *Layout) SetTyped(name string, typ Type, values any) error

SetTyped sets a global attribute of a type that the Go value does not say, such as an unsigned byte.

func (*Layout) Variables

func (layout *Layout) Variables() []*LayoutVariable

Variables are the layout's variables, in the order added.

type LayoutDimension

type LayoutDimension struct {
	// contains filtered or unexported fields
}

LayoutDimension is a dimension of a layout. A length of zero is the unlimited (record) dimension, of which a layout may have one.

func (*LayoutDimension) Length

func (dimension *LayoutDimension) Length() int64

Length is the dimension's length; the unlimited one is none.

func (*LayoutDimension) Name

func (dimension *LayoutDimension) Name() string

Name is the dimension's name.

func (*LayoutDimension) Unlimited

func (dimension *LayoutDimension) Unlimited() bool

Unlimited is whether this is the record dimension.

type LayoutVariable

type LayoutVariable struct {
	// contains filtered or unexported fields
}

LayoutVariable is a variable of a layout, with its attributes.

func (*LayoutVariable) Attributes

func (variable *LayoutVariable) Attributes() []*Attribute

Attributes are the variable's attributes, in the order set.

func (*LayoutVariable) Dimensions

func (variable *LayoutVariable) Dimensions() []*LayoutDimension

Dimensions are the variable's dimensions, in order.

func (*LayoutVariable) Name

func (variable *LayoutVariable) Name() string

Name is the variable's name.

func (*LayoutVariable) Set

func (variable *LayoutVariable) Set(name string, values any) error

Set sets an attribute of the variable as Layout.Set does.

func (*LayoutVariable) SetTyped

func (variable *LayoutVariable) SetTyped(name string, typ Type,
	values any) error

SetTyped sets an attribute of the variable of a stated type.

func (*LayoutVariable) Type

func (variable *LayoutVariable) Type() Type

Type is the variable's type.

type Packing

type Packing struct {
	Scale, Offset float64
	Fill          int16
}

Packing says how real values are stored as 16-bit integers: stored = (value - Offset) / Scale, rounded. Fill is the stored value for no data, written as _FillValue and missing_value.

type Slab

type Slab struct {
	Start, Count []int64
}

Slab is a hyperslab: for each dimension of a variable, the index of the first value and how many are taken. The record dimension is addressed by record number.

type Slice

type Slice struct {
	// contains filtered or unexported fields
}

Slice is one time step of a field, unpacked to real values. Its latitudes and longitudes ascend. No data is kept inside, never handed out: At and ValueAt say whether there is a value.

func (*Slice) At

func (slice *Slice) At(row, column int) (float64, bool)

At is the value at a row and a column of the grid, rows counted from the first latitude, and whether there is one. A place outside the grid has none.

func (*Slice) Columns

func (slice *Slice) Columns() int

Columns is how many longitudes the grid has.

func (*Slice) Holds

func (slice *Slice) Holds() bool

Holds is whether the step has any data at all.

func (*Slice) Index

func (slice *Slice) Index() int

Index is the step's place in the field.

func (*Slice) Latitudes

func (slice *Slice) Latitudes() []float64

Latitudes are the grid's latitudes in degrees, ascending, a copy.

func (*Slice) Longitudes

func (slice *Slice) Longitudes() []float64

Longitudes are the grid's longitudes in degrees, ascending, a copy.

func (*Slice) Rows

func (slice *Slice) Rows() int

Rows is how many latitudes the grid has.

func (*Slice) Time

func (slice *Slice) Time() time.Time

Time is the step's time in UTC, the zero time for a field with none.

func (*Slice) ValueAt

func (slice *Slice) ValueAt(lon, lat float64) (float64, bool)

ValueAt is the field at a longitude and latitude in degrees, by bilinear interpolation, and whether there is a value. Longitude wraps, so the seam is a cell like any. Latitude clamps at the first and last row, which is the pole for a grid that reaches it.

No data in any corner the place has a share of is no data; a corner it has no share of, because it is on a grid line, is not asked.

A place that is not a number has no value.

type Source

type Source struct {
	Name string
	At   io.ReaderAt
	Size int64
}

Source is a file's bytes: an io.ReaderAt and the length, and a name for the errors to say. It is what a file read from memory, a part of a larger file or a test fixture is given.

type Type

type Type = classic.Type

Type is a netCDF data type, as the reader under this package names it.

func AllTypes

func AllTypes() []Type

AllTypes is the eleven types, in the order of their tags.

type Variable

type Variable struct {
	// contains filtered or unexported fields
}

Variable is one variable of a file.

func (*Variable) Attribute

func (variable *Variable) Attribute(name string) (*Attribute, error)

Attribute is the variable's attribute of a name, or a refusal that names the file and the variable.

func (*Variable) Attributes

func (variable *Variable) Attributes() []*Attribute

Attributes are the variable's attributes, in the order written.

func (*Variable) Dimensions

func (variable *Variable) Dimensions() []*Dimension

Dimensions are the variable's dimensions, in order.

func (*Variable) Length

func (variable *Variable) Length() int64

Length is how many values the variable holds.

func (*Variable) Name

func (variable *Variable) Name() string

Name is the variable's name.

func (*Variable) Numbers

func (variable *Variable) Numbers() ([]float64, error)

Numbers is the whole variable widened to float64, as stored: no scale, no offset, no fill. A text variable is refused. A 64-bit integer beyond 2 to the 53rd loses its low bits.

func (*Variable) NumbersSlab

func (variable *Variable) NumbersSlab(slab Slab) ([]float64, error)

NumbersSlab is a slab of the variable widened as Numbers does.

func (*Variable) Record

func (variable *Variable) Record() bool

Record is whether the variable's first dimension is the unlimited one.

func (*Variable) Shape

func (variable *Variable) Shape() []int64

Shape is the length along each dimension. For a record variable the first is the file's record count.

func (*Variable) Slab

func (variable *Variable) Slab(slab Slab) (any, error)

Slab is a hyperslab of the variable, as a slice of the Go type it is stored as. Start and Count hold one value for each dimension.

func (*Variable) Type

func (variable *Variable) Type() Type

Type is the variable's netCDF type.

func (*Variable) Values

func (variable *Variable) Values() (any, error)

Values is the whole variable, read once, as a slice of the Go type it is stored as: see Attribute.Value for which. A variable that claims more values than the file has bytes is refused before anything is allocated.

type VariableSpec

type VariableSpec struct {
	Name       string
	Type       Type
	Dimensions []*LayoutDimension
}

VariableSpec is what AddVariable is given: a name, a type, and the dimensions, from this layout, that the variable runs over.

type VariableWriter

type VariableWriter struct {
	// contains filtered or unexported fields
}

VariableWriter writes one variable's values.

func (*VariableWriter) Name

func (variable *VariableWriter) Name() string

Name is the variable's name.

func (*VariableWriter) Record

func (variable *VariableWriter) Record() bool

Record is whether the variable's first dimension is the unlimited one.

func (*VariableWriter) Shape

func (variable *VariableWriter) Shape() []int64

Shape is the variable's shape so far; for a record variable the first is the record count.

func (*VariableWriter) Type

func (variable *VariableWriter) Type() Type

Type is the variable's type.

func (*VariableWriter) Write

func (variable *VariableWriter) Write(values any) error

Write writes the whole variable, from a slice of the Go type it is stored as (see Attribute.Value). A fixed variable's values must fill its shape. A record variable's are written from record 0, and extend the record dimension by as many records as they cover.

func (*VariableWriter) WriteSlab

func (variable *VariableWriter) WriteSlab(values any, slab Slab) error

WriteSlab writes a hyperslab. A record variable written past its record count extends the file, and the records between are filled.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer writes the values of a file made from a Layout. Close it, which writes the record count into the header.

func (*Writer) Close

func (writer *Writer) Close() error

Close writes the record count and closes the file. A second Close is not an error.

func (*Writer) Format

func (writer *Writer) Format() Format

Format is the format the file is written in.

func (*Writer) NumRecs

func (writer *Writer) NumRecs() int64

NumRecs is how many records the file's record variables hold so far, taken from the first of them, since the writer keeps them level. It is none when the file has no record variable, even if it declares an unlimited dimension; File.NumRecs counts that dimension instead.

func (*Writer) Variable

func (writer *Writer) Variable(name string) (*VariableWriter, error)

Variable is the writer of the variable of a name, or a refusal that names the file and lists its variables.

func (*Writer) Variables

func (writer *Writer) Variables() []*VariableWriter

Variables are the writers of the file's variables, in the order of the layout.

Jump to

Keyboard shortcuts

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