public static byte[] Encode(SpectrumCollection collection) { var spc = _ToSpcFile(collection); var ms = new MemoryStream(); var logOffset = Constants.HEADER_LENGTH; using (var bw = new BinaryWriter(ms)) { var bb = spc.SubFiles.SelectMany(file => _EncodeSubFile(spc.Header, file)).ToArray(); var fileType = spc.Header.FileType; if (fileType.HasFlag(FileType.XYFile) && !fileType.HasFlag(FileType.OwnXArray)) { bb = spc.SubFiles[0].XValues.SelectMany(d => BitConverter.GetBytes((float)d)).Concat(bb).ToArray(); } logOffset += bb.Length; if (spc.LogBlock != LogBlock.Empty) { spc.Header.ByteOffsetToLogBlock = logOffset; } bw.Write(_EncodeHeader(spc.Header)); bw.Write(bb); if (spc.LogBlock != LogBlock.Empty) { bw.Write(LogBlockWriter.Encode(spc.LogBlock)); } return(ms.ToArray()); } }
private static SpcFile _ToSpcFile(SpectrumCollection collection) { var spectra = collection.Spectra; var fileType = (spectra.Length == 1 && spectra[0].IsXEvenSpaced) ? FileType.SingleFileEvenlySpacedXValues : (FileType.Multifile | FileType.XYFile | FileType.OwnXArray); if (spectra.Length > 1 && spectra[0].IsXEvenSpaced && _HaveSameXAxes(spectra)) { fileType = FileType.Multifile; } if (spectra.Length > 1 && !spectra[0].IsXEvenSpaced && _HaveSameXAxes(spectra)) { fileType = FileType.Multifile | FileType.XYFile; } return(new SpcFile { Header = new Header { NumberOfPointsInFile = spectra[0].Y.Length, NumberOfSubfiles = spectra.Length, FileType = fileType, Memo = collection.Memo, ExponentForYValues = 0x80, XUnitsTypeCode = collection.XUnit.Code, YUnitsTypeCode = collection.YUnit.Code, FirstXCoordinate = spectra[0].X[0], LastXCoordinate = spectra[0].X[spectra[0].Length - 1] }, SubFiles = spectra.Select((spectrum, i) => new SubFile { Header = new SubHeader { NumberOfPoints = spectrum.Length, IndexNumber = (short)i }, XValues = spectrum.X, YValues = spectrum.Y }).ToArray(), LogBlock = collection.MetaData == null ? LogBlock.Empty : new LogBlock(collection.MetaData) }); }
public static void ToFile(SpectrumCollection collection, string file) { var bytes = Encode(collection); File.WriteAllBytes(file, bytes); }
public static void ToStream(SpectrumCollection collection, Stream stream) { var bytes = Encode(collection); stream.Write(bytes, 0, bytes.Length); }