/// <summary> /// Sets the value of this scalar as a 32-bit unsigned integer. /// </summary> /// <param name="value">The new value as a 32-bit unsigned integer.</param> public void SetUInt4(uint value) { LittleEndian.CopyBytes(value, m_value, 0); }
public byte[] ToData() { var timeSeries = m_dataSeries[0].DataPoints .Select(dataPoint => new { Time = dataPoint.Time.Ticks, Compressed = false }) .ToList(); for (int i = 1; i < timeSeries.Count; i++) { long previousTimestamp = m_dataSeries[0][i - 1].Time.Ticks; long timestamp = timeSeries[i].Time; long diff = timestamp - previousTimestamp; if (diff >= 0 && diff <= ushort.MaxValue) { timeSeries[i] = new { Time = diff, Compressed = true } } ; } int timeSeriesByteLength = timeSeries.Sum(obj => obj.Compressed ? sizeof(ushort) : sizeof(int) + sizeof(long)); int dataSeriesByteLength = sizeof(int) + (2 * sizeof(double)) + (m_samples * sizeof(ushort)); int totalByteLength = sizeof(int) + timeSeriesByteLength + (dataSeriesByteLength * m_dataSeries.Count); byte[] data = new byte[totalByteLength]; int offset = 0; offset += LittleEndian.CopyBytes(m_samples, data, offset); List <int> uncompressedIndexes = timeSeries .Select((obj, Index) => new { obj.Compressed, Index }) .Where(obj => !obj.Compressed) .Select(obj => obj.Index) .ToList(); for (int i = 0; i < uncompressedIndexes.Count; i++) { int index = uncompressedIndexes[i]; int nextIndex = (i + 1 < uncompressedIndexes.Count) ? uncompressedIndexes[i + 1] : timeSeries.Count; offset += LittleEndian.CopyBytes(nextIndex - index, data, offset); offset += LittleEndian.CopyBytes(timeSeries[index].Time, data, offset); for (int j = index + 1; j < nextIndex; j++) { offset += LittleEndian.CopyBytes((ushort)timeSeries[j].Time, data, offset); } } foreach (DataSeries dataSeries in m_dataSeries) { if (dataSeries.Calculated) { continue; } const ushort NaNValue = ushort.MaxValue; const ushort MaxCompressedValue = ushort.MaxValue - 1; int seriesID = dataSeries.SeriesInfo?.ID ?? 0; double range = dataSeries.Maximum - dataSeries.Minimum; double decompressionOffset = dataSeries.Minimum; double decompressionScale = range / MaxCompressedValue; double compressionScale = (decompressionScale != 0.0D) ? 1.0D / decompressionScale : 0.0D; offset += LittleEndian.CopyBytes(seriesID, data, offset); offset += LittleEndian.CopyBytes(decompressionOffset, data, offset); offset += LittleEndian.CopyBytes(decompressionScale, data, offset); foreach (DataPoint dataPoint in dataSeries.DataPoints) { ushort compressedValue = (ushort)Math.Round((dataPoint.Value - decompressionOffset) * compressionScale); if (compressedValue == NaNValue) { compressedValue--; } if (double.IsNaN(dataPoint.Value)) { compressedValue = NaNValue; } offset += LittleEndian.CopyBytes(compressedValue, data, offset); } } byte[] returnArray = GZipStream.CompressBuffer(data); returnArray[0] = 0x44; returnArray[1] = 0x33; return(returnArray); }
/// <summary> /// Sets the value of this scalar as a 16-bit signed integer. /// </summary> /// <param name="value">The new value as a 16-bit signed integer.</param> public void SetInt2(short value) { LittleEndian.CopyBytes(value, m_value, 0); }
/// <summary> /// Appends checksum onto <paramref name="buffer"/> starting at <paramref name="startIndex"/>. /// </summary> /// <param name="buffer">Buffer image on which to append checksum.</param> /// <param name="startIndex">Index into <paramref name="buffer"/> where checksum should be appended.</param> /// <remarks> /// We override default implementation since BPA PDCstream implements check sum for frames in little-endian. /// </remarks> protected override void AppendChecksum(byte[] buffer, int startIndex) { // Oddly enough, check sum for frames in BPA PDC stream is little-endian LittleEndian.CopyBytes(CalculateChecksum(buffer, 0, startIndex), buffer, startIndex); }