예제 #1
0
        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);
        }
예제 #2
0
        static void Main(string[] args)
        {
            var afamosFile = FamosFile.Open(@"Q:\RAW\DB_ACHTERMEER_V66\calibrated\2014-11\2014-11-19\2014-11-19_00-00-00.dat");

            var famosFile = new FamosFileHeader();

            Program.PrepareHeader(famosFile);

            // OPTION 1: Save file normally (one buffer per component).

            /* Save file. */
            famosFile.Save("continuous.dat", FileMode.Create, writer => Program.WriteFileContent(famosFile, writer));

            /* Remove -automatically- added raw block (only required to get correct initial conditions for OPTION 2). */
            famosFile.RawBlocks.Clear();

            // OPTION 2: Save file interlaced (a single buffer for all components, i.e. write data row-wise like in an Excel document).

            /* A raw block must be added manually when option 'autoAlign: false'. is set. */
            famosFile.RawBlocks.Add(new FamosFileRawBlock());

            /* Align buffers (lengths and offsets) to get an interlaced layout.*/
            famosFile.AlignBuffers(famosFile.RawBlocks.First(), FamosFileAlignmentMode.Interlaced);

            /* Save file with option 'autoAlign: false'. */
            famosFile.Save("interlaced.dat", FileMode.Create, writer => Program.WriteFileContent(famosFile, writer), autoAlign: false);
        }
예제 #3
0
        public void CanReadWriteSampleData(string type)
        {
            // Arrange
            var stream         = new MemoryStream();
            var famosFileWrite = new FamosFileHeader();

            ImcFamosFileSample.Program.PrepareHeader(famosFileWrite);

            if (type == "interlaced")
            {
                famosFileWrite.RawBlocks.Add(new FamosFileRawBlock());
                famosFileWrite.AlignBuffers(famosFileWrite.RawBlocks.First(), FamosFileAlignmentMode.Interlaced);
                famosFileWrite.Save(stream, writer => ImcFamosFileSample.Program.WriteFileContent(famosFileWrite, writer), autoAlign: false);
            }
            else
            {
                famosFileWrite.Save(stream, writer => ImcFamosFileSample.Program.WriteFileContent(famosFileWrite, writer));
            }

            // Act
            using var famosFileRead = FamosFile.Open(stream);

            var allData           = famosFileRead.ReadAll();
            var singleData        = famosFileRead.ReadSingle(famosFileRead.Fields[2].Components[0].Channels.First());
            var singleDataPartial = famosFileRead.ReadSingle(famosFileRead.Fields[2].Components[0].Channels.First(), 10, 8);

            // Assert

            // all data
            var expectedPower1 = ImcFamosFileSample.Program.PowerData;
            var actualPower1   = ((FamosFileComponentData <double>)allData[7].ComponentsData[0]).Data.ToArray();

            Assert.Equal(expectedPower1, actualPower1);

            var expectedPowerCoeff = ImcFamosFileSample.Program.PowerCoeffData;
            var actualPowerCoeff   = ((FamosFileComponentData <double>)allData[8].ComponentsData[0]).Data.ToArray();

            Assert.Equal(expectedPowerCoeff, actualPowerCoeff);

            var expectedWindSpeed = ImcFamosFileSample.Program.WindSpeedData;
            var actualWindSpeed   = ((FamosFileComponentData <double>)allData[7].ComponentsData[1]).Data.ToArray();

            Assert.Equal(expectedWindSpeed, actualWindSpeed);

            // singleData
            var expectedPower2 = ImcFamosFileSample.Program.PowerData;
            var actualPower2   = ((FamosFileComponentData <double>)singleData.ComponentsData[0]).Data.ToArray();

            Assert.Equal(expectedPower2, actualPower1);

            // singleDataPartial
            var expectedPower3 = ImcFamosFileSample.Program.PowerData.Skip(10).Take(8);
            var actualPower3   = ((FamosFileComponentData <double>)singleDataPartial.ComponentsData[0]).Data.ToArray();

            Assert.Equal(expectedPower3, actualPower3);
        }
예제 #4
0
        public void ThrowsWhenReadingCorruptFile()
        {
            // Arrange
            var filePath = $"./ImcTestData/BusTrip_corrupt.dat";

            // Act
            Action action = () => FamosFile.Open(filePath);

            // Assert
            Assert.Throws <FormatException>(action);
        }
예제 #5
0
        public void CanReadTestImcTestData(string fileName)
        {
            // Arrange
            var filePath = $"./ImcTestData/{fileName}";

            // Act
            using (var famosFile = FamosFile.Open(filePath))
            {
                //
            }

            // Assert
        }
예제 #6
0
        public void DistributesPropertiesForEachComponent()
        {
            // Arrange
            var filePath        = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            var famosFileHeader = new FamosFileHeader();

            var components = new List <FamosFileComponent>()
            {
                new FamosFileAnalogComponent("A", FamosFileDataType.Float64, 5),
                new FamosFileAnalogComponent("B", FamosFileDataType.UInt16, 15),
                new FamosFileAnalogComponent("C", FamosFileDataType.Int32, 7),
            };

            components[1].XAxisScaling = new FamosFileXAxisScaling(1);
            components[2].XAxisScaling = components[1].XAxisScaling;

            famosFileHeader.Fields.Add(new FamosFileField(FamosFileFieldType.MultipleYToSingleEquidistantTime, components));
            famosFileHeader.Channels.AddRange(components.SelectMany(component => component.Channels));

            // Act
            famosFileHeader.Save(filePath, writer => { });

            using var famosFile = FamosFile.Open(filePath);

            // Assert
            DoAssert(famosFileHeader.Fields[0]);
            DoAssert(famosFile.Fields[0]);

            void DoAssert(FamosFileField field)
            {
                Assert.True(field.XAxisScaling == null);
                Assert.True(field.Components[0].XAxisScaling == null);
                Assert.True(field.Components[1].XAxisScaling != null);
                Assert.True(field.Components[2].XAxisScaling != null);
                Assert.True(!(field.Components[1].XAxisScaling == field.Components[2].XAxisScaling));
            }
        }
예제 #7
0
        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);
        }