Example #1
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);
        }
Example #2
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);
        }
Example #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);
        }
        public void ThrowsWhenGroupIsAddedTwice()
        {
            // Arrange
            var famosFile = new FamosFileHeader();
            var group     = new FamosFileGroup("Group 1");

            famosFile.Groups.Add(group);
            famosFile.Groups.Add(group);

            // Act
            Assert.Throws <FormatException>(() => famosFile.Validate());
        }
        public void ThrowsWhenRawBlockIsAddedTwice()
        {
            // Arrange
            var famosFile = new FamosFileHeader();
            var rawBlock  = new FamosFileRawBlock();

            famosFile.RawBlocks.Add(rawBlock);
            famosFile.RawBlocks.Add(rawBlock);

            // Act
            Assert.Throws <FormatException>(() => famosFile.Validate());
        }
        public void ThrowsWhenFieldIsAddedTwice()
        {
            // Arrange
            var famosFile = new FamosFileHeader();
            var field     = new FamosFileField();

            famosFile.Fields.Add(field);
            famosFile.Fields.Add(field);

            // Act
            Assert.Throws <FormatException>(() => famosFile.Validate());
        }
        public void ThrowsWhenCustomKeyIsAddedTwice()
        {
            // Arrange
            var famosFile = new FamosFileHeader();
            var customKey = new FamosFileCustomKey("Custom key 1", Encoding.ASCII.GetBytes("Value 1"));

            famosFile.CustomKeys.Add(customKey);
            famosFile.CustomKeys.Add(customKey);

            // Act
            Assert.Throws <FormatException>(() => famosFile.Validate());
        }
        public void ThrowsWhenChannelIsAddedTwice()
        {
            // Arrange
            var famosFile = new FamosFileHeader();
            var group     = new FamosFileGroup("Group 1");
            var channel   = new FamosFileChannel("Channel 1");

            group.Channels.Add(channel);
            famosFile.Channels.Add(channel);
            famosFile.Groups.Add(group);

            // Act
            Assert.Throws <FormatException>(() => famosFile.Validate());
        }
        public void ThrowsWhenTextIsAddedTwice()
        {
            // Arrange
            var famosFile = new FamosFileHeader();
            var group     = new FamosFileGroup("Group 1");
            var text      = new FamosFileText("Text 1", "Value 1");

            group.Texts.Add(text);
            famosFile.Texts.Add(text);
            famosFile.Groups.Add(group);

            // Act
            Assert.Throws <FormatException>(() => famosFile.Validate());
        }
        public void ThrowsWhenSingleValueIsAddedTwice()
        {
            // Arrange
            var famosFile   = new FamosFileHeader();
            var group       = new FamosFileGroup("Group 1");
            var singleValue = new FamosFileSingleValue <float>("Single Value 1", 1);

            group.SingleValues.Add(singleValue);
            famosFile.SingleValues.Add(singleValue);
            famosFile.Groups.Add(group);

            // Act
            Assert.Throws <FormatException>(() => famosFile.Validate());
        }
Example #11
0
        public static void WriteFileContent(FamosFileHeader famosFile, BinaryWriter writer)
        {
            var length     = 25;
            var components = famosFile.Fields.SelectMany(field => field.Components).ToList();

            // Generate some 'random' datasets and write them to file. One dataset per component.
            for (int i = 0; i < components.Count(); i++)
            {
                var component = components[i];

                switch (component.PackInfo.DataType)
                {
                case FamosFileDataType.Int16:
                    var shortData = Enumerable.Range(0, length).Select(value => (short)(value + i * 100)).ToArray();
                    famosFile.WriteSingle(writer, component, shortData);
                    break;

                case FamosFileDataType.Int32:
                case FamosFileDataType.UInt32:
                    var intData = Enumerable.Range(0, length).Select(value => value + i * 100).ToArray();
                    famosFile.WriteSingle(writer, component, intData);
                    break;

                case FamosFileDataType.Float32:
                    var floatData = Enumerable.Range(0, length).Select(value => (float)(value + i * 100 + 0.1)).ToArray();
                    famosFile.WriteSingle(writer, component, floatData);
                    break;

                default:
                    continue;
                }
            }

            // add real data to power curve components
            var powerComponent = famosFile.Fields[2].Components[0];

            famosFile.WriteSingle(writer, powerComponent, Program.PowerData);

            var powerCoeffComponent = famosFile.Fields[2].Components[1];

            famosFile.WriteSingle(writer, powerCoeffComponent, Program.PowerCoeffData);

            var windSpeedComponent = famosFile.Fields[2].Components[2];

            famosFile.WriteSingle(writer, windSpeedComponent, Program.WindSpeedData);
        }
Example #12
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));
            }
        }
Example #13
0
        public static void PrepareHeader(FamosFileHeader famosFile)
        {
            var encoding = Encoding.GetEncoding(1252);

            // add language info
            famosFile.LanguageInfo = new FamosFileLanguageInfo()
            {
                CodePage = encoding.CodePage
            };

            // add data origin info
            famosFile.OriginInfo = new FamosFileOriginInfo("ImcFamosFile", FamosFileOrigin.Calculated);

            // add custom key
            var customKey = new FamosFileCustomKey("FileID", encoding.GetBytes(Guid.NewGuid().ToString()));

            famosFile.CustomKeys.Add(customKey);

            // data fields
            var length = 25; /* number of samples per channel or component, respectively. */

            /* data field with equidistant time */
            var calibrationInfo1 = new FamosFileCalibration()
            {
                ApplyTransformation = true,
                Factor = 10,
                Offset = 7,
                Unit   = "°C"
            };

            famosFile.Fields.Add(new FamosFileField(FamosFileFieldType.MultipleYToSingleEquidistantTime, new List <FamosFileComponent>()
            {
                /* generator data */
                new FamosFileAnalogComponent("GEN_TEMP_1", FamosFileDataType.Float32, length, calibrationInfo1),
                new FamosFileAnalogComponent("GEN_TEMP_2", FamosFileDataType.Float32, length, calibrationInfo1),
                new FamosFileAnalogComponent("GEN_TEMP_3", FamosFileDataType.Int32, length, calibrationInfo1),

                new FamosFileAnalogComponent("GEN_TEMP_4", FamosFileDataType.Float32, length, calibrationInfo1),

                /* no group */
                new FamosFileAnalogComponent("ENV_TEMP_1", FamosFileDataType.Int16, length, calibrationInfo1)
            })
            {
                TriggerTime  = new FamosFileTriggerTime(DateTime.Now, FamosFileTimeMode.Normal),
                XAxisScaling = new FamosFileXAxisScaling(deltaX: 0.01M)
                {
                    X0 = 985.0M, Unit = "Seconds"
                }
            });

            famosFile.Fields[0].Components[0].Channels[0].PropertyInfo = new FamosFilePropertyInfo(new List <FamosFileProperty>()
            {
                new FamosFileProperty("Sensor Location", "Below generator.")
            });

            /* data field with monotonous increasing time */
            famosFile.Fields.Add(new FamosFileField(FamosFileFieldType.MultipleYToSingleMonotonousTime, new List <FamosFileComponent>()
            {
                /* hydraulic data */
                new FamosFileAnalogComponent("HYD_TEMP_1", FamosFileDataType.Float32, length, calibrationInfo1),
                new FamosFileAnalogComponent("HYD_TEMP_2", FamosFileDataType.Float32, length, calibrationInfo1),

                /* time-axis */
                new FamosFileAnalogComponent(FamosFileDataType.UInt32, length, FamosFileComponentType.Secondary),
            })
            {
                XAxisScaling = new FamosFileXAxisScaling(deltaX: 100M)
                {
                    X0   = 0M,
                    Unit = "Milliseconds"
                }
            });

            /* data field for characteristic curves */
            var calibrationInfo2 = new FamosFileCalibration()
            {
                Unit = "kW"
            };
            var calibrationInfo3 = new FamosFileCalibration()
            {
                Unit = "-"
            };
            var calibrationInfo4 = new FamosFileCalibration()
            {
                Unit = "m/s"
            };

            famosFile.Fields.Add(new FamosFileField(FamosFileFieldType.MultipleYToSingleXOrViceVersa, new List <FamosFileComponent>()
            {
                /* power */
                new FamosFileAnalogComponent("POWER", FamosFileDataType.Float64, length, calibrationInfo2),

                /* power coefficient */
                new FamosFileAnalogComponent("POWER_COEFF", FamosFileDataType.Float64, length, calibrationInfo3),

                /* wind speed */
                new FamosFileAnalogComponent(FamosFileDataType.Float64, length, FamosFileComponentType.Secondary, calibrationInfo4),
            }));

            /* data field with complex values */
            var calibrationInfo5 = new FamosFileCalibration()
            {
                Unit = "A"
            };

            famosFile.Fields.Add(new FamosFileField(FamosFileFieldType.ComplexRealImaginary, new List <FamosFileComponent>()
            {
                /* converter data (real part) */
                new FamosFileAnalogComponent("CONV_CURRENT_L1", FamosFileDataType.Float32, length, FamosFileComponentType.Primary, calibrationInfo5),

                /* converter data (imaginary part) */
                new FamosFileAnalogComponent(FamosFileDataType.Float32, length, FamosFileComponentType.Secondary, calibrationInfo5),
            })
            {
                XAxisScaling = new FamosFileXAxisScaling(deltaX: 1 / 25000M)
                {
                    X0   = 0M,
                    Unit = "Seconds"
                },
                ZAxisScaling = new FamosFileZAxisScaling(deltaZ: 5M)
                {
                    Z0          = 0M,
                    SegmentSize = 2,
                    Unit        = "Meters"
                }
            });

            // add events to data field (not supported yet)

            //field1.EventInfos.Add(new FamosFileEventInfo(new List<FamosFileEvent>()
            //{
            //    new FamosFileEvent()
            //    {
            //        AmplificationFactor0 = 0, AmplificationFactor1 = 1,
            //        AmplitudeOffset0 = 2, AmplitudeOffset1 = 3,
            //        deltaX = 4,
            //        Index = 1,
            //        Length = 6,
            //        Offset = 7,
            //        Time = 8,
            //        x0 = 9
            //    },
            //    new FamosFileEvent()
            //    {
            //        AmplificationFactor0 = 10, AmplificationFactor1 = 11,
            //        AmplitudeOffset0 = 12, AmplitudeOffset1 = 13,
            //        deltaX = 14,
            //        Index = 2,
            //        Length = 16,
            //        Offset = 17,
            //        Time = 18,
            //        x0 = 19
            //    }
            //}));

            // property info (for hydraulic group)
            var propertyInfo2 = new FamosFilePropertyInfo(new List <FamosFileProperty>()
            {
                new FamosFileProperty("Weight", 3752.23),
                new FamosFileProperty("Start-up date", new DateTime(2019, 12, 06, 11, 41, 30, 210))
            });

            // define groups
            famosFile.Groups.AddRange(new List <FamosFileGroup>()
            {
                /* generator */
                new FamosFileGroup("Generator")
                {
                    Comment = "This group contains channels related to the generator."
                },

                /* hydraulic */
                new FamosFileGroup("Hydraulic")
                {
                    Comment      = "This group contains channels related to the hydraulic unit.",
                    PropertyInfo = propertyInfo2
                },

                /* power curve */
                new FamosFileGroup("Power curve"),

                /* converter */
                new FamosFileGroup("Converter")
                {
                    Comment = "This group contains channels related to the converter."
                }
            });

            // get group references
            var generatorGroup  = famosFile.Groups[0];
            var hydraulicGroup  = famosFile.Groups[1];
            var powerCurveGroup = famosFile.Groups[2];
            var converterGroup  = famosFile.Groups[3];

            // add elements to the generator group
            generatorGroup.SingleValues.Add(new FamosFileSingleValue <double>("GEN_TEMP_1_AVG", 40.25)
            {
                Comment = "Generator temperature 1.",
                Unit    = "°C",
                Time    = DateTime.Now,
            });

            generatorGroup.Texts.Add(new FamosFileText("Description", "In electricity generation, a generator is a device that converts motive power (mechanical energy) into electrical power for use in an external circuit. Sources of mechanical energy include steam turbines, gas turbines, water turbines, internal combustion engines, wind turbines and even hand cranks. - Wikipedia (2019)")
            {
                Comment      = "Maybe its useful.",
                PropertyInfo = new FamosFilePropertyInfo(new List <FamosFileProperty>()
                {
                    new FamosFileProperty("Length", 318)
                })
            });

            generatorGroup.Channels.AddRange(famosFile.Fields[0].GetChannels().Take(4));

            // add elements to the hydraulic group
            hydraulicGroup.Channels.AddRange(famosFile.Fields[1].GetChannels());

            // add elements to the power curve group
            powerCurveGroup.Texts.Add(new FamosFileText("Type", "E-126"));
            powerCurveGroup.Texts.Add(new FamosFileText("Source", "Enercon Product Sheet (2015)"));

            powerCurveGroup.Channels.AddRange(famosFile.Fields[2].GetChannels());

            // add elements to the converter group
            converterGroup.Channels.AddRange(famosFile.Fields[3].GetChannels());

            // add other elements to top level (no group)
            famosFile.Texts.Add(new FamosFileText("Random list of texts.", new List <string>()
            {
                "Text 1.", "Text 2?", "Text 3!"
            }));
            famosFile.Channels.Add(famosFile.Fields[0].GetChannels().Last());
        }
Example #14
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);
        }