2 Replies Latest reply on May 28, 2015 2:55 PM by jorgewong

    Comparing Archive Values to Find Closest to a Discrete Time

    jorgewong

      I'm looking to write a C# function that would take the following inputs: tag name, start date range, end date range and a discrete date within that range.  The purpose is to find the closest archived value if one exists to the discrete date within the given range.  Getting the values for a range is the easy part but doing the comparison between the archives values if they exist gets pretty tricky.  Has anybody done something similar or know of a function within the SDK to possibly use to get what I need?  I'm currently looking at AFTime to set a discrete date first and then loop through a comparison against that from the archives values to try to get a difference.

       

      Thanks!

        • Re: Comparing Archive Values to Find Closest to a Discrete Time
          gachen

          Hi Jorge,

           

          Instead of iterating through an array of an unknown length of values, I think it would be more efficient to just call AFData.RecordedValue() (or PIPoint.RecordedValue()) twice. This method takes the following relevant parameters:

           

          time - The time at which the value is desired. Here, you would put in your discrete time.

           

          mode - The mode is used to determine the value to return when a value does not exist at the exact time specified. The value is taken from the AFRetrievalMode enumeration. This is where the two calls will differ. One call you would use AtOrBefore, and the next you would use AtOrAfter.

           

          Then the rest of the logic would be in your code to validate that the returned value's timestamp falls within your overall time range, as well as to determine which returned value from the two calls is closest to your discrete timestamp. Additional logic could be added after the first call to RecordedValue() if an exact match is found so that the second call does not need to be made unnecessarily.

          1 of 1 people found this helpful
          • Re: Comparing Archive Values to Find Closest to a Discrete Time
            jorgewong

            Hi ,

             

            That's brilliant!  Thanks for the tip.  Will give that a try and let you know how it goes.  Will post my code as well