// Shamelessly copy pasted from the XDAWaveFormDataParser // Thanks other Stephen private void ParsePQDIF() { try { List <ObservationRecord> observationRecords; using (OpenFileDialog dialog = new OpenFileDialog()) { // Parse PQDif File Data using (LogicalParser logicalParser = new LogicalParser(m_currentFilePath)) { observationRecords = new List <ObservationRecord>(); logicalParser.Open(); while (logicalParser.HasNextObservationRecord()) { observationRecords.Add(logicalParser.NextObservationRecord()); } } // Convert to common channel format m_channels = observationRecords .SelectMany(observation => observation.ChannelInstances) .Where(channel => channel.Definition.QuantityTypeID == QuantityType.WaveForm) .Select(MakeParsedChannel) .ToList(); } } catch (Exception e) { e.Source = m_currentFileRootName; ExceptionList.Add(e); } }
private void PQDIFButton_Click(object sender, EventArgs e) { List <ObservationRecord> observationRecords; using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "PQDIF Files|*.pqd|All Files|*.*"; dialog.Title = "Browse PQDIF Files"; if (dialog.ShowDialog() == DialogResult.Cancel) { return; } if (!File.Exists(dialog.FileName)) { return; } // Parse PQDif File Data using (LogicalParser logicalParser = new LogicalParser(dialog.FileName)) { observationRecords = new List <ObservationRecord>(); logicalParser.Open(); while (logicalParser.HasNextObservationRecord()) { observationRecords.Add(logicalParser.NextObservationRecord()); } } // Convert to common channel format m_channels = observationRecords .SelectMany(observation => observation.ChannelInstances) .Where(channel => channel.Definition.QuantityTypeID == QuantityType.WaveForm) .Select(MakeParsedChannel) .ToList(); // Clear the list box and data chart ChannelListBox.Items.Clear(); DataChart.Series.Clear(); // Populate the list box with channel names ChannelListBox.Items.AddRange(m_channels .Select((channel, index) => string.Format("[{0}] {1}", index, channel.Name)) .Cast <object>() .ToArray()); // Select the first channel in the list ChannelListBox.SelectedIndex = 0; // Change the title text of the window to show what file the user has open m_fileName = dialog.SafeFileName; Text = string.Format("PQDIF - [{0}]", dialog.SafeFileName); } }
/// <summary> /// Determines whether the file can be parsed at this time. /// </summary> /// <param name="filePath">The path to the file to be parsed.</param> /// <param name="fileCreationTime">The time the file was created.</param> /// <returns>True if the file can be parsed; false otherwise.</returns> public bool CanParse(string filePath, DateTime fileCreationTime) { try { m_parser = new LogicalParser(filePath); m_parser.Open(); return(true); } catch (IOException) { return(false); } }
/// <summary> /// Populate known voltage and current data from PQDIF file. /// </summary> /// <param name="faultDataSet">Fault data set to be populated.</param> /// <param name="settings">Source parameters.</param> public static void PopulateDataSet(FaultLocationDataSet faultDataSet, Dictionary <string, string> settings) { string fileName; List <ObservationRecord> observationRecords; ObservationRecord faultObservation; ChannelInstance[] anChannels; ChannelInstance[] bnChannels; ChannelInstance[] cnChannels; if (!settings.TryGetValue("fileName", out fileName) || !File.Exists(fileName)) { throw new ArgumentException("Parameters must define a valid \"fileName\" setting."); } // Parse PQDif File Data using (LogicalParser logicalParser = new LogicalParser(fileName)) { observationRecords = new List <ObservationRecord>(); logicalParser.Open(); while (logicalParser.HasNextObservationRecord()) { observationRecords.Add(logicalParser.NextObservationRecord()); } } // Get the first observation record that contains six wave forms. // Assume that this observation record will has both voltage and // current wave forms for each of the three phases faultObservation = observationRecords.FirstOrDefault(observation => observation.ChannelInstances.Count(channel => channel.Definition.QuantityTypeID == QuantityType.WaveForm) >= 6); if ((object)faultObservation != null) { // Get the voltage and current wave forms for each of the six phase types IEnumerable <ChannelInstance> waveForms = faultObservation.ChannelInstances.Where(channel => channel.Definition.QuantityTypeID == QuantityType.WaveForm).ToList(); anChannels = waveForms.Where(channel => channel.Definition.Phase == Phase.AN).ToArray(); bnChannels = waveForms.Where(channel => channel.Definition.Phase == Phase.BN).ToArray(); cnChannels = waveForms.Where(channel => channel.Definition.Phase == Phase.CN).ToArray(); // Attempt to fill in fault data for each of the three phase types FillFaultData(faultDataSet.Voltages.AN, faultDataSet.Currents.AN, anChannels); FillFaultData(faultDataSet.Voltages.BN, faultDataSet.Currents.BN, bnChannels); FillFaultData(faultDataSet.Voltages.CN, faultDataSet.Currents.CN, cnChannels); // Set the frequency in the data set to the nominal frequency of the system faultDataSet.Frequency = faultObservation.Settings.NominalFrequency; } }
private void Parse(string filename) { List <ObservationRecord> observationRecords; List <DataSourceRecord> dataSourceRecords; using (LogicalParser logicalParser = new LogicalParser(filename)) { observationRecords = new List <ObservationRecord>(); logicalParser.Open(); while (logicalParser.HasNextObservationRecord()) { observationRecords.Add(logicalParser.NextObservationRecord()); } dataSourceRecords = logicalParser.DataSourceRecords; } this.m_previousProgress = this.m_previousProgress + 50; this.mProgress.Report(this.m_previousProgress); if (observationRecords.Count == 0) { return; } if (dataSourceRecords.Count != 1) { return; } //create Meter Definition //For now assume a single meter Meter meter = new Meter(); meter.DeviceName = dataSourceRecords[0].DataSourceName; meter.Owner = dataSourceRecords[0].DataSourceOwner; meter.DeviceAlias = GSF.PQDIF.Logical.Equipment.ToString(dataSourceRecords[0].EquipmentID); meter.DeviceLocation = dataSourceRecords[0].DataSourceLocation; if (dataSourceRecords[0].Latitude < uint.MaxValue) { meter.Latitude = dataSourceRecords[0].Latitude; } if (dataSourceRecords[0].Longitude < uint.MaxValue) { meter.Longitude = dataSourceRecords[0].Longitude; } meter.AccountName = GSF.PQDIF.Logical.Vendor.ToString(dataSourceRecords[0].VendorID); using (TransactionScope scope = new TransactionScope()) { AdoDataConnection connection = new AdoDataConnection(connectionstring, dataprovider); GSF.Data.Model.TableOperations <Meter> meterTable = new GSF.Data.Model.TableOperations <Meter>(connection); meterTable.AddNewRecord(meter); meter.ID = ModelID.GetID <Meter>(connection); //create Channel Definitions List <PQio.Model.Channel> channels = dataSourceRecords[0].ChannelDefinitions.Select(channel => ParseChannel(meter, channel, connection)).ToList(); List <PQio.Model.Event> events = new List <Event>(); //create Event Definitions foreach (ObservationRecord record in observationRecords) { //Create Event Event evt = ParseObservationRecord(record, connection); //create DataSeries objects foreach (ChannelInstance channelInstance in record.ChannelInstances) { ParseSeries(channelInstance, channels[(int)channelInstance.ChannelDefinitionIndex], evt, connection); } events.Add(evt); } // Remove Channels whithout data channels = channels.FindAll(item => RemoveEmptyChannel(item, connection)).ToList(); events = events.FindAll(item => RemoveEmptyEvents(item, connection)).ToList(); // Remove Channels whithout data channels = channels.FindAll(item => RemoveEmptyChannel(item, connection)).ToList(); // If only one set of data it's easy to keep only single line int nVa = channels.Count(channel => channel.MeasurementType.ToLower() == MeasurementType.VoltageA); int nVb = channels.Count(channel => channel.MeasurementType.ToLower() == MeasurementType.VoltageB); int nVc = channels.Count(channel => channel.MeasurementType.ToLower() == MeasurementType.VoltageC); int nIa = channels.Count(channel => channel.MeasurementType.ToLower() == MeasurementType.CurrentA); int nIb = channels.Count(channel => channel.MeasurementType.ToLower() == MeasurementType.CurrentB); int nIc = channels.Count(channel => channel.MeasurementType.ToLower() == MeasurementType.CurrentC); if (nVa == 1 && nVb == 1 && nVc == 1) { //Create new asset Asset asset = new Asset() { AssetKey = String.Format("Asset 1 ({0})", meter.AccountName) }; GSF.Data.Model.TableOperations <Asset> assetTable = new GSF.Data.Model.TableOperations <Asset>(connection); assetTable.AddNewRecord(asset); asset.ID = ModelID.GetID <Asset>(connection); GSF.Data.Model.TableOperations <Channel> channelTable = new GSF.Data.Model.TableOperations <Channel>(connection); Channel Va = channels.Find(item => item.MeasurementType.ToLower() == MeasurementType.VoltageA); Channel Vb = channels.Find(item => item.MeasurementType.ToLower() == MeasurementType.VoltageB); Channel Vc = channels.Find(item => item.MeasurementType.ToLower() == MeasurementType.VoltageC); Va.AssetID = asset.ID; Vb.AssetID = asset.ID; Vc.AssetID = asset.ID; channelTable.UpdateRecord(Va); channelTable.UpdateRecord(Vb); channelTable.UpdateRecord(Vc); if (nIa == 1 && nIb == 1 && nIc == 1) { Channel Ia = channels.Find(item => item.MeasurementType.ToLower() == MeasurementType.CurrentA); Channel Ib = channels.Find(item => item.MeasurementType.ToLower() == MeasurementType.CurrentB); Channel Ic = channels.Find(item => item.MeasurementType.ToLower() == MeasurementType.CurrentC); Ia.AssetID = asset.ID; Ib.AssetID = asset.ID; Ic.AssetID = asset.ID; channelTable.UpdateRecord(Ia); channelTable.UpdateRecord(Ib); channelTable.UpdateRecord(Ic); } } scope.Complete(); } this.m_previousProgress = this.m_previousProgress + 50; this.mProgress.Report(this.m_previousProgress); }