Beispiel #1
0
        public static void MakeSampleFilesFromCsv(string csvPathName, string columnMappingsPathName, bool hasHeader)
        {
            List <RawMeasurements> measurementSamples = RawMeasurements.FromCsv(csvPathName, columnMappingsPathName, true);

            foreach (RawMeasurements sample in measurementSamples)
            {
                // Create an XmlSerializer with the type of Network
                XmlSerializer serializer = new XmlSerializer(typeof(RawMeasurements));

                // Open a connection to the file and path.
                TextWriter writer = new StreamWriter($@"C:\Users\kevin\OneDrive\Documents\VT Data\Models\sample_{measurementSamples.IndexOf(sample)}.xml");

                // Serialize this instance of NetworkMeasurements
                serializer.Serialize(writer, sample);

                Console.WriteLine($"Writing sample file {measurementSamples.IndexOf(sample)}.");

                // Close the connection
                writer.Close();
            }
        }
        /// <summary>
        /// Deserializes the collection of raw measurement key-value pairs from the *.xml file.
        /// </summary>
        /// <param name="pathName">The path name to the file to be deserialized.</param>
        /// <returns>A <see cref="SynchrophasorAnalytics.Testing.RawMeasurements"/> object.</returns>
        public static RawMeasurements DeserializeFromXml(string pathName)
        {
            try
            {
                RawMeasurements snapshot = null;

                XmlSerializer deserializer = new XmlSerializer(typeof(RawMeasurements));

                StreamReader reader = new StreamReader(pathName);

                snapshot = (RawMeasurements)deserializer.Deserialize(reader);

                reader.Close();

                return(snapshot);
            }
            catch (Exception exception)
            {
                throw new Exception("Failed to Deserialize the Raw Measurements from the Snapshot File: " + exception.ToString());
            }
        }
Beispiel #3
0
        public static List <RawMeasurements> FromCsv(string csvPathName, string columnMappingsPathName, bool hasHeader)
        {
            String columnMappingFile = null;

            try
            {   // Open the text file using a stream reader.
                using (StreamReader sr = new StreamReader(columnMappingsPathName))
                {
                    // Read the stream to a string, and write the string to the console.
                    columnMappingFile = sr.ReadToEnd();
                    Console.WriteLine(columnMappingFile);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            string[] columnMappingPairs = columnMappingFile.Split(';');

            Dictionary <string, int> columnMetaData = new Dictionary <string, int>();

            foreach (string mapping in columnMappingPairs)
            {
                string[] metaData = mapping.Split('=');
                if (metaData.Length == 2)
                {
                    int    columnIndex = Convert.ToInt32(metaData[0]);
                    string columnKey   = metaData[1];
                    columnMetaData.Add(columnKey, columnIndex);
                }
            }

            int lineCount = 0;

            List <RawMeasurements> measurementSamples = new List <RawMeasurements>();

            using (var reader = new StreamReader(File.OpenRead(csvPathName)))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (!hasHeader || lineCount != 0)
                    {
                        var values = line.Split(',');
                        List <RawMeasurementsMeasurement> measurements = new List <RawMeasurementsMeasurement>();
                        foreach (KeyValuePair <string, int> column in columnMetaData)
                        {
                            measurements.Add(new RawMeasurementsMeasurement()
                            {
                                Key   = column.Key,
                                Value = values[column.Value]
                            });
                        }
                        RawMeasurements measurementSample = new RawMeasurements();
                        measurementSample.Items = measurements.ToArray();
                        measurementSamples.Add(measurementSample);
                    }

                    lineCount++;
                }
            }

            foreach (RawMeasurements sample in measurementSamples)
            {
                sample.identifier = measurementSamples.IndexOf(sample).ToString();
            }
            return(measurementSamples);
        }