public void CanEditAlreadyExistingFile() { // Arrange var filePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); var famosFileHeader = new FamosFileHeader(); var components = new List <FamosFileComponent>() { new FamosFileAnalogComponent("C1", FamosFileDataType.Float64, 5) }; famosFileHeader.Fields.Add(new FamosFileField(FamosFileFieldType.MultipleYToSingleEquidistantTime, components)); famosFileHeader.Channels.Add(components.First().Channels.First()); famosFileHeader.Save(filePath, writer => famosFileHeader.WriteSingle(writer, components.First(), new double[] { 0, 0, 0, 0, 0 })); // Act /* write data */ var expected = new double[] { 1, 2, 3, 4, 5 }; using (var famosFile = FamosFile.OpenEditable(filePath)) { var component = famosFile.Fields.First().Components.First(); famosFile.Edit(writer => famosFile.WriteSingle(writer, component, expected)); } /* read data */ using var famosFile2 = FamosFile.Open(filePath); var channelData = famosFile2.ReadSingle(famosFile2.Channels.First()); var actual = ((FamosFileComponentData <double>)channelData.ComponentsData.First()).Data.ToArray(); // Assert Assert.Equal(expected, actual); }
private void OpenFile(string dataFilePath, DateTime startDateTime, List <ChannelContextGroup> channelContextGroupSet) { if (File.Exists(dataFilePath)) { throw new Exception($"The file {dataFilePath} already exists. Extending an already existing file with additional channels is not supported."); } var famosFile = new FamosFileHeader(); // file var metadataGroup = new FamosFileGroup("Metadata"); metadataGroup.PropertyInfo = new FamosFilePropertyInfo(new List <FamosFileProperty>() { new FamosFileProperty("format_version", this.FormatVersion), new FamosFileProperty("system_name", this.DataWriterContext.SystemName), new FamosFileProperty("date_time", startDateTime), }); foreach (var customMetadataEntry in this.DataWriterContext.CustomMetadataEntrySet.Where(customMetadataEntry => customMetadataEntry.CustomMetadataEntryLevel == CustomMetadataEntryLevel.File)) { metadataGroup.PropertyInfo.Properties.Add(new FamosFileProperty(customMetadataEntry.Key, customMetadataEntry.Value)); } famosFile.Groups.Add(metadataGroup); // file -> project var projectGroup = new FamosFileGroup($"{this.DataWriterContext.ProjectDescription.PrimaryGroupName} / {this.DataWriterContext.ProjectDescription.SecondaryGroupName} / {this.DataWriterContext.ProjectDescription.ProjectName}"); projectGroup.PropertyInfo = new FamosFilePropertyInfo(new List <FamosFileProperty>() { new FamosFileProperty("project_version", this.DataWriterContext.ProjectDescription.Version) }); foreach (var customMetadataEntry in this.DataWriterContext.CustomMetadataEntrySet.Where(customMetadataEntry => customMetadataEntry.CustomMetadataEntryLevel == CustomMetadataEntryLevel.Project)) { projectGroup.PropertyInfo.Properties.Add(new FamosFileProperty(customMetadataEntry.Key, customMetadataEntry.Value)); } famosFile.Groups.Add(projectGroup); // for each context group foreach (var contextGroup in channelContextGroupSet) { var totalSeconds = (int)Math.Round(_settings.FilePeriod.TotalSeconds, MidpointRounding.AwayFromZero); var totalLength = (int)(totalSeconds * contextGroup.SampleRate.SamplesPerSecond); if (totalLength * (double)NexusUtilities.SizeOf(NexusDataType.FLOAT64) > 2 * Math.Pow(10, 9)) { throw new Exception(ErrorMessage.FamosWriter_DataSizeExceedsLimit); } // file -> project -> channels var field = new FamosFileField(FamosFileFieldType.MultipleYToSingleEquidistantTime); foreach (ChannelContext channelContext in contextGroup.ChannelContextSet) { var dx = contextGroup.SampleRate.Period.TotalSeconds; var channel = this.PrepareChannel(field, channelContext.ChannelDescription, (int)totalLength, startDateTime, dx); projectGroup.Channels.Add(channel); } famosFile.Fields.Add(field); _spdToFieldIndexMap[contextGroup.SampleRate.SamplesPerDay] = famosFile.Fields.Count - 1; } // famosFile.Save(dataFilePath, _ => { }); _famosFile = FamosFile.OpenEditable(dataFilePath); }