/// <summary> /// Converts a byte stream into a BucketCollection. /// </summary> /// <param name="rg">Specifies the byte stream.</param> /// <returns>The new BucketCollection is returned.</returns> public static BucketCollection FromByteStream(byte[] rg) { using (MemoryStream ms = new MemoryStream()) using (BinaryReader br = new BinaryReader(ms)) { bool bIsReal = br.ReadBoolean(); BucketCollection col = new BucketCollection(bIsReal); int nCount = br.ReadInt32(); for (int i = 0; i < nCount; i++) { Bucket b = Bucket.Load(br); col.m_rgBuckets.Add(b); } return(col); } }
/// <summary> /// The Bucketize method adds all values within a SimpleDatum to a new BucketCollection. /// </summary> /// <param name="strName">Specifies the name to use when writing status information.</param> /// <param name="nBucketCount">Specifies the number of Buckets to use.</param> /// <param name="sd">Specifies the SimpleDatum containing the data to add.</param> /// <param name="log">Specifies the output log.</param> /// <param name="evtCancel">Specifies the CancelEvent used to cancel the bucketizing process.</param> /// <param name="dfMin">Optionally, specifies a overall minimum to use in the BucketCollection, when missing this is calculated from the SimpleDatum.</param> /// <param name="dfMax">Optionally, specifies a overall maximum to use in the BucketCollection, when missing this is calculated from the SimpleDatum.</param> /// <returns></returns> public static BucketCollection Bucketize(string strName, int nBucketCount, SimpleDatum sd, Log log, CancelEvent evtCancel, double?dfMin = null, double?dfMax = null) { int nIdx = 0; int nChannels = sd.Channels; int nCount = sd.ItemCount / nChannels; int nItemCount = sd.ItemCount; int nOffset = 0; Stopwatch sw = new Stopwatch(); sw.Start(); // Calculate the min/max values if not already specified. if (!dfMin.HasValue || !dfMax.HasValue) { dfMin = double.MaxValue; dfMax = -double.MaxValue; for (int i = 0; i < nChannels; i++) { for (int j = 0; j < nCount; j++) { double dfVal = sd.GetDataAtD(nOffset + j); dfMin = Math.Min(dfMin.Value, dfVal); dfMax = Math.Max(dfMax.Value, dfVal); nIdx++; if (sw.Elapsed.TotalMilliseconds > 1000) { if (evtCancel != null && evtCancel.WaitOne(0)) { return(null); } double dfPct = (double)nIdx / (double)nItemCount; log.WriteLine("Calculating min/max at " + dfPct.ToString("P") + "..."); sw.Restart(); } } nOffset += nCount; } } BucketCollection col = new BucketCollection(dfMin.Value, dfMax.Value, nBucketCount); nIdx = 0; nOffset = 0; for (int i = 0; i < nChannels; i++) { for (int j = 0; j < nCount; j++) { double dfVal = sd.GetDataAtD(nOffset + j); col.Add(dfVal); nIdx++; if (sw.Elapsed.TotalMilliseconds > 1000) { if (evtCancel != null && evtCancel.WaitOne(0)) { return(null); } double dfPct = (double)nIdx / (double)nItemCount; log.WriteLine(strName + " at " + dfPct.ToString("P") + "..."); sw.Restart(); } } nOffset += nCount; } return(col); }