Table of Contents

Class GribFile

Namespace
EncDotNet.S100.Datasets.Grib
Assembly
EncDotNet.S100.Datasets.Grib.dll

A GRIB2 file opened for reading: its fields, indexed when it opens, and their values, decoded on demand.

public sealed class GribFile : IDisposable
Inheritance
GribFile
Implements
Inherited Members

Remarks

Opening reads Section 0 of each message and sections 1, 3, 4 and 5 of each field, and skips the bitmaps and data, so a 500 MB GFS file indexes in a few thousand small reads. ReadValues(GribField, CancellationToken) then reads one field's bitmap and data section and unpacks them. The file stays open until Dispose(), and reads are safe from several threads at once.

Supported: grid template 3.0 (regular latitude/longitude, any scanning mode except staggered), product templates 4.0 and 4.8, and data templates 5.0 (simple), 5.2 (complex), 5.3 (complex with spatial differencing of order 1 or 2), 5.40 (JPEG 2000) and 5.41 (PNG), with or without a bitmap. Fields using any other template are listed with UnsupportedReason set, and reading them throws UnsupportedGribException naming the template: notably 5.42 (CCSDS). A GRIB edition 1 message can't be indexed at all, so opening a file that holds one throws.

Properties

Fields

The fields of every message, in file order.

public IReadOnlyList<GribField> Fields { get; }

Property Value

IReadOnlyList<GribField>

Methods

CreateStepDecoder(GribField)

Returns a WeatherStepDecoder that decodes field with ReadValues(GribField, CancellationToken), for a WeatherTimeStep. The file must stay open while the decoder is in use.

public WeatherStepDecoder CreateStepDecoder(GribField field)

Parameters

field GribField

A field from Fields.

Returns

WeatherStepDecoder

The decoder.

Exceptions

ArgumentException

field isn't one of this file's fields.

Dispose()

Closes the file. Decoders made by CreateStepDecoder(GribField) stop working.

public void Dispose()

Open(IReadOnlyList<string>)

Opens and indexes several GRIB2 files as one, in the order given, as if they were appended (cat f000 f003 f006): one model run published one file per forecast hour (#1075). Every file stays open until the returned file is disposed.

public static GribFile Open(IReadOnlyList<string> paths)

Parameters

paths IReadOnlyList<string>

The files' paths; at least one.

Returns

GribFile

The opened file. Dispose it when done.

Exceptions

ArgumentException

paths is empty.

GribFormatException

A file isn't valid GRIB2.

UnsupportedGribException

A file holds a GRIB edition 1 message, or another edition.

Open(Stream, bool)

Opens and indexes GRIB2 data from a seekable stream.

public static GribFile Open(Stream stream, bool leaveOpen = false)

Parameters

stream Stream

A readable, seekable stream positioned anywhere; it's read from the start.

leaveOpen bool

Whether disposing the file leaves stream open.

Returns

GribFile

The opened file. Dispose it when done.

Exceptions

ArgumentException

stream can't read or seek.

GribFormatException

The data isn't valid GRIB2.

UnsupportedGribException

The data holds a GRIB edition 1 message, or another edition.

Open(ReadOnlyMemory<byte>)

Opens and indexes GRIB2 data already in memory.

public static GribFile Open(ReadOnlyMemory<byte> bytes)

Parameters

bytes ReadOnlyMemory<byte>

The data. It isn't copied, so don't change it while the file is in use.

Returns

GribFile

The opened file.

Exceptions

GribFormatException

The data isn't valid GRIB2.

UnsupportedGribException

The data holds a GRIB edition 1 message, or another edition.

Open(string)

Opens and indexes a GRIB2 file.

public static GribFile Open(string path)

Parameters

path string

The file's path.

Returns

GribFile

The opened file. Dispose it when done.

Exceptions

GribFormatException

The file isn't valid GRIB2.

UnsupportedGribException

The file holds a GRIB edition 1 message, or another edition.

ReadValues(GribField, CancellationToken)

Decodes a field's values.

public float[] ReadValues(GribField field, CancellationToken cancellationToken = default)

Parameters

field GribField

A field from Fields.

cancellationToken CancellationToken

Signals that the values are no longer needed.

Returns

float[]

One value per grid point, row by row: row × Ni + column, rows and columns running the way ToWeatherNativeGrid() says (the scanning mode's column-major and boustrophedon orders are undone). Points the bitmap or the complex packing's missing value management marks missing are NaN.

Exceptions

ArgumentException

field isn't one of this file's fields.

UnsupportedGribException

The field uses an unsupported template (UnsupportedReason).

GribFormatException

The field's bitmap or data section is malformed.

ObjectDisposedException

The file has been disposed.