Class GribFile
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
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
Returns
- WeatherStepDecoder
The decoder.
Exceptions
- ArgumentException
fieldisn'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
pathsIReadOnlyList<string>The files' paths; at least one.
Returns
- GribFile
The opened file. Dispose it when done.
Exceptions
- ArgumentException
pathsis 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
streamStreamA readable, seekable stream positioned anywhere; it's read from the start.
leaveOpenboolWhether disposing the file leaves
streamopen.
Returns
- GribFile
The opened file. Dispose it when done.
Exceptions
- ArgumentException
streamcan'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
bytesReadOnlyMemory<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
pathstringThe 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
fieldGribFieldA field from Fields.
cancellationTokenCancellationTokenSignals 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
fieldisn'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.