Jerome Lefebvre

Looping over arrays

Blog Post created by Jerome Lefebvre on Aug 26, 2019

Looping arrays

the technique

AF Analysis has supported arrays for a while, but doesn't support looping them with a typical for loop statement. A lot of use cases for loops are converted by the map, filter and aggregate methods such as sum.

In this post, I want to explore an other way to loop over arrays.

The idea is quite simple, say you have an array of 5 element and would like to loop over it over one minute. We can schedule an analysis to run every 12 seconds (60/5 = 12). We can then use the number of seconds method to pick an element of an array.

Here is the AF structure, in which I have my array: Array

Here is the analysis that picks a different element of the array each time it runs:


The analysis will have to be modified if you want to loop over something different than 1 minute.

Building up an XY-plot

Here is an example of using such technique. I was recently asked how to monitor pressure across a pipeline. The pipeline had manholes every few kilometers, at each manholes inlet or outlet (sometime both) pressure were collected. In between manholes, we assume the pressure just drops linearly. Visualizing this in PI Vision can be a bit tricky as the x-axis is not time, but distance along the pipeline. An X=Y plot is much closer, but the raw data is made up of 1-2 tag per manhole. Thus, there is the need to combine the data before displaying it.


The sensors array is just a list of tag names. AF2

Now, I built up an analysis similar to the one above, but looping over both arrays.


Now that with this done we can display everything in PI Vision: XY


We are forced to pick a timestamp for this display, I couldn't find a good choice for this graph, thus I picked midnight of each day. Thus, the red line will show the latest values for the particular display. If somebody can think of a better output time, please let me know!