/// <summary> /// The UnBucketize method converts all Data received into their respective Bucket average values. /// </summary> /// <param name="strName">Specifies the name to use when writing status information.</param> /// <param name="rgrgData">Specifies the data to unbucketize.</param> /// <param name="log">Specifies the output log.</param> /// <param name="evtCancel">Specifies the CancelEvent used to cancel the unbucketizing process.</param> /// <returns>On success, <i>true</i> is returned, otherwise <i>false</i>.</returns> public bool UnBucketize(string strName, List <double[]> rgrgData, Log log, CancelEvent evtCancel) { int nIdx = 0; int nItemCount = rgrgData.Count * rgrgData[0].Length; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < rgrgData.Count; i++) { for (int j = 0; j < rgrgData[i].Length; j++) { double dfVal = rgrgData[i][j]; double dfNewVal = Translate(dfVal); rgrgData[i][j] = dfNewVal; if (evtCancel != null && evtCancel.WaitOne(0)) { return(false); } if (sw.Elapsed.TotalMilliseconds > 1000) { double dfPct = (double)nIdx / (double)nItemCount; log.WriteLine(strName + " at " + dfPct.ToString("P") + "..."); sw.Restart(); } nIdx++; } } return(true); }
/// <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); }
/// <summary> /// Extract Tar data from a stream to a specified output directory. /// </summary> /// <param name="stream">Specifies the stream containing the Tar data to extract.</param> /// <param name="strOutputDir">Specifies the output directory.</param> /// <param name="evtCancel">Optionally, specifies the cancel event used to cancel the extraction (default = null).</param> /// <param name="log">Optionally, specifies the Log used to output status of the extraction (default = null).</param> /// <param name="nExpectedTotal">Optionally, specifies the expected total number of files (default = 0).</param> /// <param name="nIdx">Optionally, specifies an item index (default = 0).</param> /// <returns>Upon a successful extraction, the number of files extracted offset by the index is returned, or 0 on abort.</returns> public static int ExtractTar(Stream stream, string strOutputDir, CancelEvent evtCancel = null, Log log = null, int nExpectedTotal = 0, int nIdx = 0) { byte[] rgBuffer = new byte[500]; bool bDone = false; int nFileCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); try { while (!bDone) { stream.Read(rgBuffer, 0, 100); string strName = Encoding.ASCII.GetString(rgBuffer).Trim('\0'); if (string.IsNullOrWhiteSpace(strName)) { break; } stream.Seek(24, SeekOrigin.Current); stream.Read(rgBuffer, 0, 12); long lSize = Convert.ToInt64(Encoding.UTF8.GetString(rgBuffer, 0, 12).Trim('\0').Trim(), 8); stream.Seek(376L, SeekOrigin.Current); string strOutput = Path.Combine(strOutputDir, strName); string strPath = Path.GetDirectoryName(strOutput); if (!Directory.Exists(strPath)) { Directory.CreateDirectory(strPath); } if (!strName.EndsWith("/") && !strName.EndsWith("\\")) { if (sw.Elapsed.TotalMilliseconds > 1000) { if (log != null) { if (nExpectedTotal > 0) { log.Progress = (double)(nIdx + nFileCount) / nExpectedTotal; } log.WriteLine("Extracting " + nFileCount.ToString("N0") + " files - '" + strName + "'..."); } sw.Restart(); } using (FileStream fstrm = File.Open(strOutput, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] rgData = new byte[lSize]; stream.Read(rgData, 0, rgData.Length); fstrm.Write(rgData, 0, rgData.Length); nFileCount++; } } long lPos = stream.Position; long lOffset = 512 - (lPos % 512); if (lOffset == 512) { lOffset = 0; } stream.Seek(lOffset, SeekOrigin.Current); if (evtCancel != null) { if (evtCancel.WaitOne(0)) { return(0); } } } if (log != null) { log.WriteLine("Extracted a total of " + nFileCount.ToString("N0") + " files."); } } catch (Exception excpt) { if (log != null) { log.WriteError(excpt); } throw excpt; } return(nFileCount + nIdx); }