public DataGroup ToSubGroup(int startIndex, int endIndex) { DataGroup subGroup = new DataGroup(); foreach (DataSeries dataSeries in m_dataSeries) { subGroup.Add(dataSeries.ToSubSeries(startIndex, endIndex)); } return(subGroup); }
public static DataGroup Combine(params DataGroup[] dataGroups) { DataGroup combination = new DataGroup(); foreach (DataGroup dataGroup in dataGroups) { foreach (DataSeries dataSeries in dataGroup.DataSeries) { combination.Add(dataSeries); } } return(combination); }
public CycleDataGroup(DataGroup dataGroup) { string[] instantaneousSeriesTypes = { "Values", "Instantaneous" }; m_rmsIndex = -1; m_phaseIndex = -1; m_amplitudeIndex = -1; m_errorIndex = -1; m_dataGroup = new DataGroup(dataGroup.DataSeries); for (int i = 0; i < dataGroup.DataSeries.Count; i++) { if (!instantaneousSeriesTypes.Contains(dataGroup[i].SeriesInfo.SeriesType.Name)) { continue; } if (dataGroup[i].SeriesInfo.Channel.MeasurementCharacteristic.Name == "RMS") { m_rmsIndex = i; } else if (dataGroup[i].SeriesInfo.Channel.MeasurementCharacteristic.Name == "AngleFund") { m_phaseIndex = i; } else if (dataGroup[i].SeriesInfo.Channel.MeasurementCharacteristic.Name == "WaveAmplitude") { m_amplitudeIndex = i; } else if (dataGroup[i].SeriesInfo.Channel.MeasurementCharacteristic.Name == "WaveError") { m_errorIndex = i; } } if (new int[] { m_rmsIndex, m_phaseIndex, m_amplitudeIndex, m_errorIndex }.Any(i => i < 0)) { throw new InvalidOperationException("Cannot create CycleDataGroup from an incomplete set of channels"); } }
public VICycleDataGroup(DataGroup dataGroup) { m_vx1Index = -1; m_vx2Index = -1; m_vx3Index = -1; m_vy1Index = -1; m_vy2Index = -1; m_vy3Index = -1; m_i1Index = -1; m_i2Index = -1; m_i3Index = -1; m_irIndex = -1; m_cycleDataGroups = dataGroup.DataSeries .Select((dataSeries, index) => new { DataSeries = dataSeries, Index = index }) .GroupBy(obj => obj.Index / 4) .Where(grouping => grouping.Count() >= 4) .Select(grouping => grouping.Select(obj => obj.DataSeries)) .Select(grouping => new CycleDataGroup(new DataGroup(grouping))) .ToList(); MapIndexes(); }
public VIDataGroup(DataGroup dataGroup) { string[] instantaneousSeriesTypes = { "Values", "Instantaneous" }; // Initialize each of // the indexes to -1 m_vx1Index = -1; m_vx2Index = -1; m_vx3Index = -1; m_vy1Index = -1; m_vy2Index = -1; m_vy3Index = -1; m_i1Index = -1; m_i2Index = -1; m_i3Index = -1; m_irIndex = -1; // Initialize the data group m_dataGroup = new DataGroup(dataGroup.DataSeries); for (int i = 0; i < dataGroup.DataSeries.Count; i++) { // If the data group is not instantaneous, do not use it in the VIDataGroup if (dataGroup[i].SeriesInfo.Channel.MeasurementCharacteristic.Name != "Instantaneous") { continue; } // If the data group is not instantaneous, do not use it in the VIDataGroup if (!instantaneousSeriesTypes.Contains(dataGroup[i].SeriesInfo.SeriesType.Name)) { continue; } // Assign the proper indexes for the seven VIDataGroup // channels by checking the name of the channel if (dataGroup[i].SeriesInfo.Channel.Name == "VX1") { m_vx1Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "VX2") { m_vx2Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "VX3") { m_vx3Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "VY1") { m_vy1Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "VY2") { m_vy2Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "VY3") { m_vy3Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "I1") { m_i1Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "I2") { m_i2Index = i; } else if (dataGroup[i].SeriesInfo.Channel.Name == "I3") { m_i3Index = i; } } }
// Static Methods private static Series GetSeriesInfo(Meter meter, DataGroup dataGroup, string measurementTypeName, string phaseName) { int lineID = dataGroup.Line.ID; string measurementCharacteristicName = "Instantaneous"; string seriesTypeName = "Values"; char typeDesignation = (measurementTypeName == "Current") ? 'I' : measurementTypeName[0]; char phaseDesignation = (phaseName == "RES") ? 'R' : phaseName[0]; string channelName = string.Concat(typeDesignation, phaseDesignation); ChannelKey channelKey = new ChannelKey(lineID, 0, channelName, measurementTypeName, measurementCharacteristicName, phaseName); SeriesKey seriesKey = new SeriesKey(channelKey, seriesTypeName); Series dbSeries = meter.Channels .SelectMany(channel => channel.Series) .FirstOrDefault(series => seriesKey.Equals(new SeriesKey(series))); if ((object)dbSeries == null) { using (AdoDataConnection connection = meter.ConnectionFactory()) { Channel dbChannel = meter.Channels .FirstOrDefault(channel => channelKey.Equals(new ChannelKey(channel))); if ((object)dbChannel == null) { TableOperations <Channel> channelTable = new TableOperations <Channel>(connection); TableOperations <MeasurementType> measurementTypeTable = new TableOperations <MeasurementType>(connection); TableOperations <MeasurementCharacteristic> measurementCharacteristicTable = new TableOperations <MeasurementCharacteristic>(connection); TableOperations <Phase> phaseTable = new TableOperations <Phase>(connection); MeasurementType measurementType = measurementTypeTable.GetOrAdd(measurementTypeName); MeasurementCharacteristic measurementCharacteristic = measurementCharacteristicTable.GetOrAdd(measurementCharacteristicName); Phase phase = phaseTable.GetOrAdd(phaseName); dbChannel = new Channel() { MeterID = meter.ID, LineID = lineID, MeasurementTypeID = measurementType.ID, MeasurementCharacteristicID = measurementCharacteristic.ID, PhaseID = phase.ID, Name = channelKey.Name, SamplesPerHour = dataGroup.SamplesPerHour, Description = string.Concat(measurementCharacteristicName, " ", measurementTypeName, " ", phaseName), Enabled = true }; channelTable.AddNewRecord(dbChannel); dbChannel.ID = connection.ExecuteScalar <int>("SELECT @@IDENTITY"); meter.Channels = null; } TableOperations <Series> seriesTable = new TableOperations <Series>(connection); TableOperations <SeriesType> seriesTypeTable = new TableOperations <SeriesType>(connection); SeriesType seriesType = seriesTypeTable.GetOrAdd(seriesTypeName); dbSeries = new Series() { ChannelID = dbChannel.ID, SeriesTypeID = seriesType.ID, SourceIndexes = string.Empty }; seriesTable.AddNewRecord(dbSeries); dbSeries.ID = connection.ExecuteScalar <int>("SELECT @@IDENTITY"); dbChannel.Series = null; dbSeries = meter.Channels .SelectMany(channel => channel.Series) .First(series => seriesKey.Equals(new SeriesKey(series))); } } return(dbSeries); }
public static DataGroup ToCycleDataGroup(DataSeries dataSeries, double frequency) { DataGroup dataGroup = new DataGroup(); DataSeries rmsSeries = new DataSeries(); DataSeries phaseSeries = new DataSeries(); DataSeries peakSeries = new DataSeries(); DataSeries errorSeries = new DataSeries(); int samplesPerCycle; double[] yValues; double[] tValues; double sum; DateTime cycleTime; SineWave sineFit; if ((object)dataSeries == null) { return(null); } // Set series info to the source series info rmsSeries.SeriesInfo = dataSeries.SeriesInfo; phaseSeries.SeriesInfo = dataSeries.SeriesInfo; peakSeries.SeriesInfo = dataSeries.SeriesInfo; errorSeries.SeriesInfo = dataSeries.SeriesInfo; // Get samples per cycle of the data series based on the given frequency samplesPerCycle = CalculateSamplesPerCycle(dataSeries, frequency); // Initialize arrays of y-values and t-values for calculating cycle data yValues = new double[samplesPerCycle]; tValues = new double[samplesPerCycle]; for (int i = 0; i <= dataSeries.DataPoints.Count - samplesPerCycle; i += samplesPerCycle) { // Use the time of the first data point in the cycle as the time of the cycle cycleTime = dataSeries.DataPoints[i].Time; sum = 0.0D; // Copy values from the original data series into the y-value and t-value arrays for (int j = 0; j < samplesPerCycle; j++) { yValues[j] = dataSeries.DataPoints[i + j].Value; tValues[j] = (dataSeries.DataPoints[i + j].Time - cycleTime).TotalSeconds; sum += yValues[j] * yValues[j]; } // Use a curve fitting algorithm to estimate the sine wave over this cycle sineFit = WaveFit.SineFit(yValues, tValues, frequency); // Add data points to each of the cycle data series rmsSeries.DataPoints.Add(new DataPoint() { Time = cycleTime, Value = Math.Sqrt(sum / samplesPerCycle) }); phaseSeries.DataPoints.Add(new DataPoint() { Time = cycleTime, Value = sineFit.Phase }); peakSeries.DataPoints.Add(new DataPoint() { Time = cycleTime, Value = sineFit.Amplitude }); errorSeries.DataPoints.Add(new DataPoint() { Time = cycleTime, Value = tValues .Select(sineFit.CalculateY) .Zip(yValues, (estimate, value) => Math.Abs(estimate - value)) .Sum() }); } // Add a series to the data group for each series of cycle data dataGroup.Add(rmsSeries); dataGroup.Add(phaseSeries); dataGroup.Add(peakSeries); dataGroup.Add(errorSeries); return(dataGroup); }
public static Event GetEvent(this TableOperations <Event> eventTable, FileGroup fileGroup, DataGroup dataGroup) { int fileGroupID = fileGroup.ID; int lineID = dataGroup.Line.ID; DateTime startTime = dataGroup.StartTime; DateTime endTime = dataGroup.EndTime; int samples = dataGroup.Samples; IDbDataParameter startTimeParameter = new SqlParameter() { ParameterName = nameof(dataGroup.StartTime), DbType = DbType.DateTime2, Value = startTime }; IDbDataParameter endTimeParameter = new SqlParameter() { ParameterName = nameof(dataGroup.EndTime), DbType = DbType.DateTime2, Value = endTime }; RecordRestriction recordRestriction = new RecordRestriction("FileGroupID = {0}", fileGroupID) & new RecordRestriction("LineID = {0}", lineID) & new RecordRestriction("StartTime = {0}", startTimeParameter) & new RecordRestriction("EndTime = {0}", endTimeParameter) & new RecordRestriction("Samples = {0}", samples); return(eventTable.QueryRecord(recordRestriction)); }