Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bits"></param>
        /// <param name="index"></param>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public static GribMessage Create(byte[] bits, int index, GribContext ctx)
        {
            var buff = Marshal.AllocHGlobal(bits.Length);

            Marshal.Copy(bits, 0, buff, bits.Length);
            int   err = 0;
            SizeT sz  = (SizeT)bits.Length;

            GribMessage msg    = null;
            GribHandle  handle = null;

            lock (_fileLock)
            {
                // grib_api moves to the next message in a stream for each new handle
                handle = GribApiProxy.GribHandleNewFromMultiMessage(ctx, out buff, ref sz, out err);
            }

            if (err != 0)
            {
                Marshal.AllocHGlobal(buff);
                throw GribApiException.Create(err);
            }

            if (handle != null)
            {
                msg = new GribMessage(handle, ctx, buff, index);
            }

            return(msg);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a GribMessage instance from a buffer.
        /// </summary>
        /// <param name="bits"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static GribMessage Create(byte[] bits, int index = 0)
        {
            GCHandle h       = GCHandle.Alloc(bits, GCHandleType.Pinned);
            IntPtr   pHandle = h.AddrOfPinnedObject();

            int        err    = 0;
            SizeT      sz     = (SizeT)bits.Length;
            GribHandle handle = GribApiProxy.GribHandleNewFromMultiMessage(GribContext.Default, out pHandle, ref sz, out err);

            if (err != 0)
            {
                h.Free();
                throw GribApiException.Create(err);
            }

            GribMessage msg = null;

            if (handle != null)
            {
                msg = new GribMessage(handle, GribContext.Default, h, index);
            }

            return(msg);
        }