// Some rough calculations have shown that sample rate calculation is extremely accurate, // so long as the frequency of the system is close to the frequency passed into this method // (within a few hundreths of a Hz). However, in the interest of correctness, this method // attempts to use the sample rate provided by the data loader--if one has been provided-- // rather than blindly using the calculated sample rate. Given the accuracy of the // calculation, this may not be necessary. private static void ValidateSampleRates(double frequency, MeasurementDataSet measurementDataSet) { int[] sampleRates = new int[] { measurementDataSet.AN.SampleRate, measurementDataSet.BN.SampleRate, measurementDataSet.CN.SampleRate }; int[] distinctSampleRates; if (sampleRates.Any(rate => rate == 0)) { distinctSampleRates = sampleRates.Where(rate => rate != 0).Distinct().ToArray(); if (distinctSampleRates.Length != 1) { measurementDataSet.CalculateSampleRates(frequency); } else { measurementDataSet.AN.SampleRate = distinctSampleRates[0]; measurementDataSet.BN.SampleRate = distinctSampleRates[0]; measurementDataSet.CN.SampleRate = distinctSampleRates[0]; } } }
private void ProcessFile(string fileName) { DateTime timeStarted; DateTime timeProcessed; string rootFileName; ICollection <Device> devices; FaultLocationDataSet faultDataSet; Device disturbanceRecorder; ICollection <DisturbanceFile> disturbanceFiles; ICollection <Tuple <Line, FaultLocationDataSet> > lineDataSets; List <double> largestCurrentDistances; List <double> boundedDistances; timeStarted = DateTime.UtcNow; m_currentLogger = null; rootFileName = null; try { rootFileName = FilePath.GetFileNameWithoutExtension(fileName); if (m_debugLevel >= 1) { m_currentLogger = Logger.Open(string.Format("{0}{1}.log", m_debugFolder, rootFileName)); } OnStatusMessage(string.Format("Processing {0}...", fileName)); // Make sure device definitions exist, and attempt to load them if they don't devices = m_devices; if ((object)devices == null) { devices = CreateDevices(m_deviceDefinitionsFile); foreach (IFaultResultsWriter resultsWriter in m_resultsWriters.Adapters) { if (resultsWriter.Enabled) { TryWriteConfiguration(resultsWriter, devices); } } m_devices = devices; } // Get the definition for the device that captured this event disturbanceRecorder = GetDevice(devices, rootFileName); disturbanceFiles = CreateDisturbanceFiles(rootFileName); lineDataSets = new Collection <Tuple <Line, FaultLocationDataSet> >(); if ((object)disturbanceRecorder == null) { throw new InvalidOperationException(string.Format("No device record found for \"{0}\" in configuration.", fileName)); } foreach (Line line in disturbanceRecorder.Lines) { try { // Provide status information about which line is being processed OnStatusMessage("Detecting faults on line {0}...", line.Name); // Get the fault data set for this line faultDataSet = GetFaultDataSet(fileName, line); // Get the maximum rated current from the line definition faultDataSet.RatedCurrent = line.Rating50F; // Export data to CSV for validation if (m_debugLevel >= 1) { MeasurementDataSet.ExportToCSV(FilePath.GetAbsolutePath(string.Format("{0}{1}.{2}_measurementData.csv", m_debugFolder, rootFileName, line.ID)), faultDataSet.Voltages, faultDataSet.Currents); CycleDataSet.ExportToCSV(FilePath.GetAbsolutePath(string.Format("{0}{1}.{2}_cycleData.csv", m_debugFolder, rootFileName, line.ID)), faultDataSet.Cycles); } // Run fault trigger, type, and location algorithms if (ExecuteFaultTriggerAlgorithm(line.FaultAlgorithmsSet.FaultTriggerAlgorithm, faultDataSet, line.FaultAlgorithmsSet.FaultTriggerParameters)) { faultDataSet.FaultType = ExecuteFaultTypeAlgorithm(line.FaultAlgorithmsSet.FaultTypeAlgorithm, faultDataSet, line.FaultAlgorithmsSet.FaultTypeParameters); faultDataSet.FaultDistances = line.FaultAlgorithmsSet.FaultLocationAlgorithms.ToDictionary(algorithm => algorithm.Method.Name, algorithm => ExecuteFaultLocationAlgorithm(algorithm, faultDataSet, null)); // Get the set of distance calculations for the cycle with the largest current largestCurrentDistances = faultDataSet.FaultDistances.Values .Select(distances => distances[faultDataSet.Cycles.GetLargestCurrentIndex()]) .OrderBy(dist => dist) .ToList(); // Filter the set down to the distance values that are reasonable boundedDistances = largestCurrentDistances .Where(dist => dist >= 0.0D && dist <= faultDataSet.LineDistance) .ToList(); // Get the representative fault distance as the median of the bounded distances, // falling back on the median of the unbounded distances if there are no bounded ones faultDataSet.FaultDistance = (boundedDistances.Count > 0) ? boundedDistances[boundedDistances.Count / 2] : largestCurrentDistances[largestCurrentDistances.Count / 2]; OnStatusMessage("Distance to fault: {0} {1}", faultDataSet.FaultDistance, m_lengthUnits); // Add the line-specific parameters to the faultResultsWriter lineDataSets.Add(Tuple.Create(line, faultDataSet)); } else { OnStatusMessage("No fault detected."); } } catch (Exception ex) { string message = string.Format("Error detecting faults on line {0}: {1}", line.Name, ex.Message); OnProcessException(new Exception(message, ex)); } } try { // Write results to the output source if (lineDataSets.Count > 0) { timeProcessed = DateTime.UtcNow; foreach (DisturbanceFile disturbanceFile in disturbanceFiles) { disturbanceFile.FLETimeStarted = timeStarted; disturbanceFile.FLETimeProcessed = timeProcessed; } foreach (IFaultResultsWriter resultsWriter in m_resultsWriters.Adapters) { if (resultsWriter.Enabled) { TryWriteResults(resultsWriter, disturbanceRecorder, disturbanceFiles, lineDataSets); } } } } catch (Exception ex) { string errorMessage = string.Format("Unable to write results for file \"{0}\" due to exception: {1}", fileName, ex.Message); OnProcessException(new InvalidOperationException(errorMessage, ex)); } } catch (Exception ex) { string errorMessage = string.Format("Unable to process file \"{0}\" due to exception: {1}", fileName, ex.Message); OnProcessException(new InvalidOperationException(errorMessage, ex)); } try { if ((object)m_currentLogger != null) { m_currentLogger.Close(); m_currentLogger = null; } } catch (Exception ex) { OnProcessException(ex); } try { if ((object)rootFileName != null) { // Get a list of processed files based on file prefix List <string> processedFiles = FilePath.GetFileList(string.Format("{0}{1}.*", m_dropFolder, rootFileName)) .Concat(FilePath.GetFileList(string.Format("{0}{1}_*", m_dropFolder, rootFileName))) .ToList(); // Delete processed files from the drop folder foreach (string file in processedFiles) { File.Delete(file); } } } catch (Exception ex) { OnProcessException(ex); } OnStatusMessage(""); }
public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedTableSchema(global::System.Xml.Schema.XmlSchemaSet xs) { global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType(); global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence(); MeasurementDataSet ds = new MeasurementDataSet(); global::System.Xml.Schema.XmlSchemaAny any1 = new global::System.Xml.Schema.XmlSchemaAny(); any1.Namespace = "http://www.w3.org/2001/XMLSchema"; any1.MinOccurs = new decimal(0); any1.MaxOccurs = decimal.MaxValue; any1.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax; sequence.Items.Add(any1); global::System.Xml.Schema.XmlSchemaAny any2 = new global::System.Xml.Schema.XmlSchemaAny(); any2.Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1"; any2.MinOccurs = new decimal(1); any2.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax; sequence.Items.Add(any2); global::System.Xml.Schema.XmlSchemaAttribute attribute1 = new global::System.Xml.Schema.XmlSchemaAttribute(); attribute1.Name = "namespace"; attribute1.FixedValue = ds.Namespace; type.Attributes.Add(attribute1); global::System.Xml.Schema.XmlSchemaAttribute attribute2 = new global::System.Xml.Schema.XmlSchemaAttribute(); attribute2.Name = "tableTypeName"; attribute2.FixedValue = "MeasurementDataTable"; type.Attributes.Add(attribute2); type.Particle = sequence; global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable(); if (xs.Contains(dsSchema.TargetNamespace)) { global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream(); global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream(); try { global::System.Xml.Schema.XmlSchema schema = null; dsSchema.Write(s1); for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext(); ) { schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current)); s2.SetLength(0); schema.Write(s2); if ((s1.Length == s2.Length)) { s1.Position = 0; s2.Position = 0; for (; ((s1.Position != s1.Length) && (s1.ReadByte() == s2.ReadByte())); ) { ; } if ((s1.Position == s1.Length)) { return type; } } } } finally { if ((s1 != null)) { s1.Close(); } if ((s2 != null)) { s2.Close(); } } } xs.Add(dsSchema); return type; }
public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedDataSetSchema(global::System.Xml.Schema.XmlSchemaSet xs) { MeasurementDataSet ds = new MeasurementDataSet(); global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType(); global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence(); global::System.Xml.Schema.XmlSchemaAny any = new global::System.Xml.Schema.XmlSchemaAny(); any.Namespace = ds.Namespace; sequence.Items.Add(any); type.Particle = sequence; global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable(); if (xs.Contains(dsSchema.TargetNamespace)) { global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream(); global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream(); try { global::System.Xml.Schema.XmlSchema schema = null; dsSchema.Write(s1); for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext(); ) { schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current)); s2.SetLength(0); schema.Write(s2); if ((s1.Length == s2.Length)) { s1.Position = 0; s2.Position = 0; for (; ((s1.Position != s1.Length) && (s1.ReadByte() == s2.ReadByte())); ) { ; } if ((s1.Position == s1.Length)) { return type; } } } } finally { if ((s1 != null)) { s1.Close(); } if ((s2 != null)) { s2.Close(); } } } xs.Add(dsSchema); return type; }