Time Weighted Calculation Modes

The ihAverage and ihTotal and ihStandardDeviation modes use time-weighted calculations of interpolated and raw samples. The following example illustrates this concept using the ihAverage mode.

Import the following data:
[Tags]
Tagname,DataType,HiEngineeringUnits,LoEngineeringUnits
TAG2,SingleFloat,60,0

[Data]
Tagname,TimeStamp,Value,DataQuality
TAG2,29-Mar-2002 14:00:00.000,30.0,Good
TAG2,29-Mar-2002 14:01:00.000,40.0,Good
TAG2,29-Mar-2002 14:01:10.000,50.0,Good
TAG2,29-Mar-2002 14:01:15.000,20.0,Bad
TAG2,29-Mar-2002 14:01:45.000,25.0,Good
Attributes for each data sample are Tagname, TimeStamp, Value, and DataQuality. The data can also be organized by duration:
Value Duration
30 60 seconds
40 10 seconds
50 5 seconds
20 30 seconds
25 15 seconds
We want to analyze the data over the following interval. The time begins at the timestamp for the first sample and ends 15 seconds after the timestamp of the last sample.
3/29/2002 14:00:00 - start time
3/29/2002 14:02:00 - end time
Calculating a raw average over these two minutes produces the following:
(40 + 50 + 25) / 3 = 38.33
You can also calculate it with the following query:
select timestamp, value, quality from ihrawdata where 
samplingmode=calculated and calculationmode=RawAverage and timestamp >= '29-Mar-2002 14:00' 
and timestamp <= '29-Mar-2002 14:02' and tagname  = tag2 and numberofsamples = 1
Time Stamp Value Quality
29-Mar-200214:02:00.000 38.33 100.00

The value of 30 is not used because the RawAverage mode uses only samples whose timestamps are greater than the interval's start time. A value whose timestamp is 14:00 would be associated with the previous interval.

In a time-weighted average, the values are multiplied by the durations. The two-minute time weighted average is:
((30 * 59.999) + (40 * 10) + (50 * 5) + (25*15)) / (60 + 10 + 5 + 15) = 31.38
You can calculate this with the following query:
select timestamp, value, quality from ihrawdata where 
samplingmode=calculated and calculationmode=Average and timestamp >= '29-Mar-2002 14:00' 
and timestamp <= '29-Mar-2002 14:02' and tagname  = tag2 and numberofsamples = 1

This more closely describes the real situation, the value was 30 during most of the queried interval. The value of 30 is assigned to a timestamp of 14:00:00.001, which is the first possible timestamp greater than the interval start time (up to a resolution of milliseconds).

Note: The bad quality sample (whose value was 20) is ignored when calculating the time-weighted average. The quality was bad for 30 seconds out of the 2 minutes, so the percent good quality is 75. When performing time-weighted calculations, percent good represents the percentage of time within the interval that had data with good quality: (90 seconds of good data quality / 120 seconds of the total interval duration ) = 75%

Computing an Average Without A Raw Sample At Start Time

There is rarely a raw sample available at the interval start time. However, the archiver needs to know the value at the start of an interval before it can perform time-weighted calculations. The archiver uses interpolation to get values it needs for which no raw samples are available.

For example, if we set the start time for the query to 14:05, then the archiver will interpolate a value at the timestamp of 14:05.

The RawAverage would then be calculated as follows:
select timestamp, value, quality from ihrawdata where samplingmode=calculated and 
calculationmode=RawAverage and timestamp >= '29-Mar-2002 14:00:05' and 
timestamp <= '29-Mar-2002 14:02' and tagname  = tag2 and numberofsamples = 1
Time Stamp Value Quality
29-Mar-200214:02:00.000 38.33 100.00
Similarly, the time-weighted average would be calculated as follows:
select timestamp, value, quality from ihrawdata where samplingmode=calculated 
and calculationmode=Average and timestamp >= '29-Mar-2002 14:00:05' and 
timestamp <= '29-Mar-2002 14:02' and tagname  = tag2 and numberofsamples = 1
Time Stamp Value Quality
29-Mar-2002 14:02:00.000 32.01 73.91

Average and Step Values

The average of the raw samples is the interval, but there is special logic for time weighting and for computing the value at the start of the interval.

Averages are computed differently depending on the value of the Tag.StepValue property. If StepValuee=FALSE then the average works as it always did in 2.0 and 3.0. A value at the start of the interval is determined via interpolation.

If StepValue=TRUE then lab sampling, not interpolation, is used to determine the value at interval start time. This would more accurately reflect a value that steps or a value that uses collector compression and did not change for a long period of time.

ihTotal Mode

The ihTotal mode retrieves the time weighted rate total for each calculation interval.

A rate total is considered for totalizing a continuous measurement. A factor is applied to the totalized value to convert into the appropriate engineering units. Since this is a rate total, a base rate of Units/Day is assumed. If the actual units of the continuous measurement is Units/Minute, multiply the results by 1440 Minutes / Day to convert the totalized number into the appropriate engineering units.

The formula for total is total = average & (interval in milliseconds / 1000) / 86400. The 86400 is number of seconds in a day. This formula takes the average, which is assumed to be already in units per day, and divides it into "units per interval".

Collecting a Rate from a Data Source

Assume an average of 240 barrels per day.

If your interval is one day, then the "units per interval" is units per DAY. Since the average was already assumed to be in units per day, you just get back the average.

240 = 240 * (86400000/1000) / 86400

240 = 240 * 1

If your interval is 1 hour, you should get back 1/24 of the average.

total= 240 * (3600000/1000) / 86400

total = 240 * 0.0417

total = 10

Ten is 1/24 of 240 and tells you 10 units were produced that hour.