Пример #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);
        }
Пример #2
0
        internal static void DeleteGribBox(HandleRef box)
        {
            int ret = _DeleteGribBox(box);

            if (ret != 0)
            {
                throw GribApiException.Create(ret);
            }
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GribBox"/> class.
        /// </summary>
        /// <param name="msgHandle">The MSG handle.</param>
        /// <param name="nw">The nw.</param>
        /// <param name="se">The se.</param>
        public GribBox(GribHandle msgHandle, GeoCoordinate nw, GeoCoordinate se)
        {
            int err;
            var box = GribApiProxy.GribBoxNew(msgHandle, out err);

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

            var pts = GribApiProxy.GribBoxGetPoints(box, nw.Latitude, nw.Longitude, se.Latitude, se.Longitude, out err);

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

            _points = new GribPoints(SWIGTYPE_p_grib_points.getCPtr(pts).Handle, false);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
0
        protected GribMessage GetNextMessage(int index)
        {
            // the starting position of the stream
            var streamStartPos = this.Stream.Position;
            // if there are headers in the file, the message may not align with the beginning of the stream
            // or the end of the previous message.
            long msgOffset = 0;

            byte[] buffer64 = new byte[8];
            byte[] buffer32 = new byte[4];
            byte[] buffer24 = new byte[3];

            /*
             * GRIB Message Octets Content (0 indexed)
             * 0-3    'GRIB' (Coded CCITT-ITA No. 5) (ASCII);
             * 4-6	    GRIB1: Total length of GRIB message (including Section 0)
             * 7       Edition number
             * 8-15    GRIB2: Total length of GRIB message(including Sections 0 & 5);
             */

            bool foundStart = false;

            while (this.Stream.Position + 1 < this.Stream.Length && !foundStart)
            {
                msgOffset = this.Stream.Position;

                if (this.Stream.ReadByte() == GRIB_MSG_START[0])
                {
                    this.Stream.Seek(-1, SeekOrigin.Current);
                    this.Stream.Read(buffer32, 0, 4);

                    if (buffer32.SequenceEqual(GRIB_MSG_START))
                    {
                        foundStart = true;
                    }
                    else
                    {
                        this.Stream.Seek(msgOffset + 1, SeekOrigin.Begin);
                    }
                }
            }

            if (!foundStart)
            {
                return(null);
            }

            // at this point, the stream should be pointing to the "B" octet in the "GRIB" sequence
            // if this is an edition 1, the next 3 bytes are the length of the message
            this.Stream.Read(buffer24, 0, 3);

            // get the edition number and read the message length per ed spec
            var ed = this.Stream.ReadByte();

            if (ed == 0)
            {
                throw GribApiException.Create(-12);
            }
            else if (ed == 1)
            {
                buffer32.Initialize();
                Array.Copy(buffer24, 0, buffer64, 5, 3);
            }
            else if (ed == 2)
            {
                this.Stream.Read(buffer64, 0, 8);
            }

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(buffer64);
            }

            long msgLen = BitConverter.ToInt64(buffer64, 0);
            long end    = msgLen + streamStartPos;
            // get the length of the message, including any headers
            long totalLen = (msgOffset - streamStartPos) + msgLen;

            this.Stream.Seek(streamStartPos, SeekOrigin.Begin);
            byte[] msgBytes = new byte[totalLen];
            this.Stream.Read(msgBytes, 0, (int)totalLen);

            return(GribMessage.Create(msgBytes));
        }