GribApi.NET
What it is
GribApi.NET is a C# wrapper around the European Centre for Medium Range Weather Forecasting's powerful grib_api, a C library for reading, writing, and converting GRIB1 and GRIB2 files.
GRIB is a format commonly used in meteorology to store weather data. GribApi.NET makes it easy to encode and decode these data by providing access to both GRIB editions through a set of GRIB API keys. GribApi.NET and grib_api are licensed under the friendly Apache License 2.0.
Special thanks to John L'Heureux, Meteorological Data Analyst at aWhere, Inc., for his contributions as scientific advisor.
Features
- Read and write GRIB 1 and 2 messages
- Easy to understand API
- Supports x86, x64, and "All CPU"
- Thread safe
- JPEG and PNG compression support
- Multi-field support
GRIB Tools
You can also use grib_api via CLI. Checkout the GRIB tools package on Chocolatey. At the very least, you'll find it very helpful for debugging.
Docs
The documentation is very much a WIP, but you'll find grib_api's wiki, helpful.
Usage
Make sure you have the MSVC 2015 redistributables installed. If you don't, you can use chocolatey
,
C:\> choco install vcredist2015
Install GribApi.NET using Nuget. From Package Manager Console run
PM> Install-Package Grib.Api
Troubleshooting
In general, GribApi.NET should "just work". In rare cases, GribApi.NET may have difficulty locating the Grib.Api
directory, which contains a number of dependencies.
To solve this problem, set the environment variable GRIB_API_DIR_ROOT
before calling GribApi.NET for the first time. The value should be the directory containing the Grib.Api
directory. E.g., for C:\Some\Path\Grib.Api
, set:
Environment.SetEnvironmentVariable("GRIB_API_DIR_ROOT", "C:\\Some\\Path", EnvironmentVariableTarget.Process);
Examples
Getting grid information from a GRIB message:
using (GribFile file = new GribFile("mygrib.grb"))
{
GribMessage msg = file.First();
Console.WriteLine("Grid Type: " + msg.GridType);
double latInDegrees = msg["latitudeOfFirstGridPoint"].AsDouble();
// GribApi.NET normalizes the coordinate values to degrees. This follows the best practice advised by ECMWF.
// values are also accessible as strings
Console.WriteLine("latitudeOfFirstGridPointInDegrees = " + msg["latitudeOfFirstGridPoint"].AsString());
}
Iterating multiple messages:
using (GribFile file = new GribFile("mygrib.grb"))
{
foreach (GribMessage msg in file)
{
// do something
}
}
Iterating lat/lon/value:
GribMessage msg = gribFile.First();
// the values in GeoSpatialValues are calculated by grid type
foreach (GeoSpatialValue val in msg.GeoSpatialValues)
{
if (val.IsMissing) { continue; }
Console.WriteLine("Lat: {0} Lon: {1} Val: {2}", val.Latitude, val.Longitude, val.Value);
}
Cherry-picking messages by parameter name
using(GribFile file = new GribFile(@".\TestData\Pacific.wind.7days.grb"))
{
var vComp = file.Where(m => m.Name.Contains("V-component of wind m s**-1")).First();
foreach (var val in vComp.GeoSpatialValues)
{
Console.WriteLine("Lat: {0} Lon: {1} Val: {2}", val.Latitude, val.Longitude, val.Value);
}
}
Key dump:
using (GribFile file = new GribFile("mygrib.grb"))
{
GribMessage msg = file.First();
foreach (var msg in file)
{
foreach (var key in msg)
{
Console.WriteLine("Key: {0}, Value: {1}", key.Name, key.Value.AsString());
}
}
}
Getting raw data:
GribMessage msg = gribFile.First();
double[] rawValues;
// a copy of the raw values stored in the message
msg.Values(out rawValues);
Editing a single message and saving to a new file:
string outPath = "out.grb";
string readPath = "some.grb";
using (GribFile readFile = new GribFile(readPath))
{
Console.WriteLine("Writing 1 message from {0} to {1}", readPath, outPath);
var msg = readFile.First();
msg["latitudeOfFirstGridPoint"].AsDouble(42);
GribFile.Write(outPath, msg);
}
Appending multiple messages to an existing file:
using (GribFile readFile = new GribFile(readPath))
{
Console.WriteLine("Appending {0} messages from {1} to {2}", readFile.MessageCount, readPath, outPath);
GribFile.Write(outPath, readFile as IEnumerable<GribMessage>, FileMode.Append);
// or, more simply:
// GribFile.Write(outPath, readFile, FileMode.Append);
}
For more examples, checkout the tests.
Building
The current build is only designed for Windows and Visual Studio. I am eager to get it converted to CMake and make it cross-platform. Even a consistent build using make under msys2 would be great. I'd love some help doing this. :)
First, install the Nuget packages (this assumes you have nuget on PATH):
nuget install NUnit -Version 2.6.4 -O src\GribApi.NET\packages\
Install NUnit 2.6.4. Then run:
build\build_gribapi.cmd [build|rebuild] [VS version, 11|12|14] [Debug|Release] [nuget package version]
E.g., to build with Visual Studio 2013 (VS version 12):
build\build_gribapi.cmd build 12 Debug
Running SWIG
Most of the interop interfaces are generated using SWIG
and included in the repository. You shouldn't need to create them. However, if you want to generate the interfaces yourself, you'll need SWIG
installed and available on PATH. Then run build/swig_gen.cmd
.
Running Tests
- Install NUnit and expose it on PATH.
- Run
build/run_tests <architecture> <configuration> [optional "1" to break the tests on start]
, e.g.
build/run_tests x64 Debug
or
build/run_tests x86 Debug 1