10 Replies Latest reply on Dec 18, 2012 2:33 PM by PierreMarcil

    PIData.Summary with astAverage and no value in datetime range

    PierreMarcil

      Hello,

       

      I figure this function trigger my try/catch since it might try to divide by 0 since there no data in the datetime range.  Using astCount instead go trough same condition without fail and just return 0.

       

      My quesiton is there any other proper way to handle this than create a sub try/catch or test with astCount then do astAverage if count > 0?  When i first write up my code, I though it would be handle after all using .IsGood() function.

       
      var avg = pot.Data.Summary(startTime, endTime, ArchiveSummaryTypeConstants.astAverage);
      
      // => trigger try/catch
      
      //idea #1
      
      var result = (double)0;
      
      try {
      
      var avg = pot.Data.Summary(startTime, endTime, ArchiveSummaryTypeConstants.astAverage);
      
      if(avg.IsGood())
      result = avg.Value
      
      }
      catch 
      (COMException ex)
      {
      result = null;
      }
      
      //idea #2 :
      
      var cnt = pot.Data.Summary(startTime, endTime, ArchiveSummaryTypeConstants.astCount);
      
      if(cnt.value > 0)
      
      var avg = pot.Data.Summary(startTime, endTime, ArchiveSummaryTypeConstants.astAverage);
      

       

        • Re: PIData.Summary with astAverage and no value in datetime range
          Marcos Vainer Loeff

          Hello Pierre,

           

          If you don´t define the CalculationBasisConstants parameter, it will use cbTimeWeighted by default.

           

          In this case, from the PI SDK Programming Reference :

           

          "cbTimeWeighted Weight the values in the calculation by the time over which they apply. Interpolation is based on the step attribute of the point. Interpolated events are generated at the boundaries if necessary."

           

          In other words, even if there are no values on the timerange chosen, it should return a value through interpolation.

           

          This won´t be true if you are using cbEventWeighted option for CalculationBasis. Therefore, you will have to use similar functions like yours (both ideas should work fine) in order to deal with the situation where there are no compressed values on a chosen time range.

           

          Hope this helps!

            • Re: PIData.Summary with astAverage and no value in datetime range
              PierreMarcil

              Yeah I did though about using interpolate value, still wont work in this case, since when I get error, it's when I use my generic fonction with a new tag which still in "created point" state without any value before or after my range of datetime.  Appreciate answer Marcos

               

              I still a bit disapointed this case can't be handle with IsGood() Method, so I ll walk around and move ahead.

                • Re: PIData.Summary with astAverage and no value in datetime range
                  Marcos Vainer Loeff

                  Hello,

                   

                  The method IsGood() used on your code is executed after the application throws an exception. Therefore, it is not very useful in this case.

                   

                  However, you can use the following code below:

                   
                      class Application
                      {
                  
                          public bool NewTagDetection(PIPoint myTag)
                          {
                              PIValue snapshotval = myTag.Data.Snapshot;
                              if (snapshotval.IsGood() == false)
                              {
                                  if (((DigitalState)snapshotval.Value).Name == "Pt Created")
                                      return true;
                              }
                              return false;
                          }
                  
                          public void Run()
                          {
                  
                              PISDK.PISDK sdkroot = new PISDK.PISDK();
                              Server myPIServer = sdkroot.Servers.DefaultServer;
                              PIPoint sinusoid = myPIServer.PIPoints["sinusoid"];
                  
                              bool NewTag = this.NewTagDetection(sinusoid);
                              Console.WriteLine("OldTag?" + NewTag);
                              if (NewTag==false)
                              {
                                  PIValue avg = sinusoid.Data.Summary("*-10m", "*", ArchiveSummaryTypeConstants.astAverage, CalculationBasisConstants.cbTimeWeighted);
                                  Console.WriteLine("Avg: " + avg.Value);
                              }
                              
                          }
                  

                   

                   

                  The idea is to detect if the snapshot value is a good or bad value. If it is a bad value, it should be a system digital state. The snapshot of new tags are always "Pt Created". Your application should only execute the Summary method if its snapshot is not "Pt Created".

                   

                  Is the example above useful to you? If not, could you explain please the reason?

                   

                  Regards,