/// <summary> /// Initializes a new instance of the <see cref="GribFile" /> class. File read rights are shared between processes. /// </summary> /// <param name="fileName">Name of the file.</param> /// <exception cref="System.IO.IOException">Could not open file. See inner exception for more detail.</exception> /// <exception cref="System.IO.FileLoadException">The file is empty.</exception> public GribFile(string fileName) { FileInfo fi = new FileInfo(fileName); // need a better check if (!GribFile.FileIsValid(fileName)) { throw new FileLoadException("This file is empty or invalid."); } _pFileHandleProxy = GribApiNative.CreateFileHandleProxy(fileName); if (_pFileHandleProxy == IntPtr.Zero) { throw new FileLoadException("Could not open file. See inner exception for more detail.", new Win32Exception(Marshal.GetLastWin32Error())); } _fileHandleProxy = (FileHandleProxy)Marshal.PtrToStructure(_pFileHandleProxy, typeof(FileHandleProxy)); FileName = fileName; Reference = new HandleRef(this, _fileHandleProxy.File); // set the message count here; the result seems to be connected to the message iterator so // that after you begin iterating messages, the count decreases until it reaches 1. int count = 0; GribApiProxy.GribCountInFile(Context, this, out count); MessageCount = count; }
/// <summary> /// Creates a GribMessage instance from a <seealso cref="Grib.Api.GribFile"/>. /// </summary> /// <param name="file">The file.</param> /// <param name="index">The index.</param> /// <returns></returns> public static GribMessage Create(GribFile file, int index) { GribMessage msg = null; int err = 0; // grib_api moves to the next message in a stream for each new handle GribHandle handle = GribApiProxy.GribHandleNewFromFile(file.Context, file, out err); if (err != 0) { throw GribApiException.Create(err); } if (handle != null) { msg = new GribMessage(handle, file.Context, index); } return(msg); }
public GribImporter(string inputFile) { _inputFile = inputFile; CorrectFilePath(ref _inputFile); bool ok = false; while (!ok) { try { GribEnvironment.Init(); ok = true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } using (var gf = new Grib.Api.GribFile(_inputFile)) _messages = gf.ToArray(); }
/// <summary> /// Writes all messages in the file to the specified path. /// </summary> /// <param name="path">The path.</param> /// <param name="file">The file.</param> /// <param name="mode">The mode.</param> public static void Write(string path, GribFile file, FileMode mode = FileMode.Create) { Write(path, file as IEnumerable <GribMessage>, mode); }