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
- Variables
- func FormatName(format Format) string
- func New() (*celestialdata.Fixed, error)
- func TypeName(typ Type) string
- func TypeSize(typ Type) int
- func WriteGrid(path string, grid GridFile) error
- type Attribute
- func (attribute *Attribute) Bytes() []byte
- func (attribute *Attribute) Length() int
- func (attribute *Attribute) Name() string
- func (attribute *Attribute) Number() (float64, bool)
- func (attribute *Attribute) Text() string
- func (attribute *Attribute) Type() Type
- func (attribute *Attribute) Value() any
- type Dimension
- type Field
- func (field *Field) All() ([]*Slice, error)
- func (field *Field) Latitudes() []float64
- func (field *Field) LongName() string
- func (field *Field) Longitudes() []float64
- func (field *Field) Name() string
- func (field *Field) Step(index int) (*Slice, error)
- func (field *Field) Steps() int
- func (field *Field) Times() ([]time.Time, error)
- func (field *Field) Units() string
- type File
- func (file *File) Attribute(name string) (*Attribute, error)
- func (file *File) Attributes() []*Attribute
- func (file *File) Close() error
- func (file *File) Dimension(name string) (*Dimension, error)
- func (file *File) Dimensions() []*Dimension
- func (file *File) Field(name string) (*Field, error)
- func (file *File) Fields() []*Field
- func (file *File) Format() Format
- func (file *File) Name() string
- func (file *File) NumRecs() int64
- func (file *File) Variable(name string) (*Variable, error)
- func (file *File) Variables() []*Variable
- type Format
- type GridFile
- type GridVariable
- type Layout
- func (layout *Layout) AddDimension(name string, length int64) (*LayoutDimension, error)
- func (layout *Layout) AddVariable(spec VariableSpec) (*LayoutVariable, error)
- func (layout *Layout) Create(path string) (*Writer, error)
- func (layout *Layout) Dimensions() []*LayoutDimension
- func (layout *Layout) Format() Format
- func (layout *Layout) Set(name string, values any) error
- func (layout *Layout) SetTyped(name string, typ Type, values any) error
- func (layout *Layout) Variables() []*LayoutVariable
- type LayoutDimension
- type LayoutVariable
- func (variable *LayoutVariable) Attributes() []*Attribute
- func (variable *LayoutVariable) Dimensions() []*LayoutDimension
- func (variable *LayoutVariable) Name() string
- func (variable *LayoutVariable) Set(name string, values any) error
- func (variable *LayoutVariable) SetTyped(name string, typ Type, values any) error
- func (variable *LayoutVariable) Type() Type
- type Packing
- type Slab
- type Slice
- func (slice *Slice) At(row, column int) (float64, bool)
- func (slice *Slice) Columns() int
- func (slice *Slice) Holds() bool
- func (slice *Slice) Index() int
- func (slice *Slice) Latitudes() []float64
- func (slice *Slice) Longitudes() []float64
- func (slice *Slice) Rows() int
- func (slice *Slice) Time() time.Time
- func (slice *Slice) ValueAt(lon, lat float64) (float64, bool)
- type Source
- type Type
- type Variable
- func (variable *Variable) Attribute(name string) (*Attribute, error)
- func (variable *Variable) Attributes() []*Attribute
- func (variable *Variable) Dimensions() []*Dimension
- func (variable *Variable) Length() int64
- func (variable *Variable) Name() string
- func (variable *Variable) Numbers() ([]float64, error)
- func (variable *Variable) NumbersSlab(slab Slab) ([]float64, error)
- func (variable *Variable) Record() bool
- func (variable *Variable) Shape() []int64
- func (variable *Variable) Slab(slab Slab) (any, error)
- func (variable *Variable) Type() Type
- func (variable *Variable) Values() (any, error)
- type VariableSpec
- type VariableWriter
- func (variable *VariableWriter) Name() string
- func (variable *VariableWriter) Record() bool
- func (variable *VariableWriter) Shape() []int64
- func (variable *VariableWriter) Type() Type
- func (variable *VariableWriter) Write(values any) error
- func (variable *VariableWriter) WriteSlab(values any, slab Slab) error
- type Writer
Constants ¶
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.
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 ¶
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 ¶
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 WriteGrid ¶
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) Number ¶
Number is the first value of a numeric attribute as a float64, and whether there is one. A text attribute has none.
type Dimension ¶
type Dimension struct {
// contains filtered or unexported fields
}
Dimension is one dimension of a file, shared by the variables that use it.
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 ¶
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) Longitudes ¶
Longitudes are the grid's longitudes in degrees, ascending, a copy.
func (*Field) Step ¶
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.
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 ¶
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 ¶
Read reads a file from its bytes. It refuses no bytes and a negative size. Its errors are those of Open.
func (*File) Attribute ¶
Attribute is the global attribute of a name, or a refusal that names the file and lists its attributes.
func (*File) Attributes ¶
Attributes are the file's global attributes, in the order written.
func (*File) Dimension ¶
Dimension is the dimension of a name, or a refusal that names the file and lists its dimensions.
func (*File) Dimensions ¶
Dimensions are the file's dimensions, in the order written.
func (*File) Field ¶
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) NumRecs ¶
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.
type 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 ¶
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 ¶
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 ¶
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 ¶
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) Set ¶
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 ¶
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 ¶
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 ¶
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) Longitudes ¶
Longitudes are the grid's longitudes in degrees, ascending, a copy.
func (*Slice) ValueAt ¶
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 ¶
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 Variable ¶
type Variable struct {
// contains filtered or unexported fields
}
Variable is one variable of a file.
func (*Variable) Attribute ¶
Attribute is the variable's attribute of a name, or a refusal that names the file and the variable.
func (*Variable) Attributes ¶
Attributes are the variable's attributes, in the order written.
func (*Variable) Dimensions ¶
Dimensions are the variable's dimensions, in order.
func (*Variable) Numbers ¶
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 ¶
NumbersSlab is a slab of the variable widened as Numbers does.
func (*Variable) Shape ¶
Shape is the length along each dimension. For a record variable the first is the file's record count.
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.
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 ¶
Close writes the record count and closes the file. A second Close is not an error.
func (*Writer) NumRecs ¶
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.