From 602d5bb6cd3363993e9448236e93f1a2093852c0 Mon Sep 17 00:00:00 2001
From: Giulio Gaio <giulio.gaio@elettra.eu>
Date: Fri, 21 Jan 2022 22:21:54 +0100
Subject: [PATCH] Fixed circular buffer shift direction

---
 src/CalcStats.py | 94 +++++++++++++++---------------------------------
 1 file changed, 29 insertions(+), 65 deletions(-)

diff --git a/src/CalcStats.py b/src/CalcStats.py
index 70d320f..60635dc 100755
--- a/src/CalcStats.py
+++ b/src/CalcStats.py
@@ -467,52 +467,35 @@ class DeviceObj:
 			# History for Mean,Median,Std,Skew,Kurt,Min,Max,MinMax
 			# mean			
 			attribute = self.prefix()+'{:03d}_Mean_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = np.roll(val, 1);val[-1] = self.mean; selfseq_dev.write_attribute(attribute,val)
-			else:
-				val = np.append(val,self.mean); selfseq_dev.write_attribute(attribute,val)
+			val = np.roll(val, -1);val[-1] = self.mean; selfseq_dev.write_attribute(attribute,val)
+			
 			# median											 				
 			attribute = self.prefix()+'{:03d}_Median_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = np.roll(val, 1);val[-1] = self.median; selfseq_dev.write_attribute(attribute,val)
-			else:
-				val = np.append(val,self.median); selfseq_dev.write_attribute(attribute,val)
+			val = np.roll(val, -1);val[-1] = self.median; selfseq_dev.write_attribute(attribute,val)
+
 			# std				
 			attribute = self.prefix()+'{:03d}_Std_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = np.roll(val, 1);val[-1] = self.std; selfseq_dev.write_attribute(attribute,val)
-			else:
-				val = np.append(val,self.std); selfseq_dev.write_attribute(attribute,val)		
+			val = np.roll(val, -1);val[-1] = self.std; selfseq_dev.write_attribute(attribute,val)
+	
 			# skew						
 			attribute = self.prefix()+'{:03d}_Skew_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = np.roll(val, 1);val[-1] = self.skew; selfseq_dev.write_attribute(attribute,val)
-			else:
-				val = np.append(val,self.skew); selfseq_dev.write_attribute(attribute,val)	
+			val = np.roll(val, -1);val[-1] = self.skew; selfseq_dev.write_attribute(attribute,val)
+	
 			# kurt							
 			attribute = self.prefix()+'{:03d}_Kurt_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = np.roll(val, 1);val[-1] = self.kurt; selfseq_dev.write_attribute(attribute,val)
-			else:
-				val = np.append(val,self.kurt); selfseq_dev.write_attribute(attribute,val)	
+			val = np.roll(val, -1);val[-1] = self.kurt; selfseq_dev.write_attribute(attribute,val)
+
 			# min				
 			attribute = self.prefix()+'{:03d}_Min_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = np.roll(val, 1);val[-1] = self.min; selfseq_dev.write_attribute(attribute,val)
-			else:
-				val = np.append(val,self.min); selfseq_dev.write_attribute(attribute,val)	
+			val = np.roll(val, -1);val[-1] = self.min; selfseq_dev.write_attribute(attribute,val)
+	
 			# max								
 			attribute = self.prefix()+'{:03d}_Max_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = val = np.roll(val, 1);val[-1] = self.max; selfseq_dev.write_attribute(attribute,val)
-			else:
-				np.append(val,self.max); selfseq_dev.write_attribute(attribute,val)	
+			val = val = np.roll(val, -1);val[-1] = self.max; selfseq_dev.write_attribute(attribute,val)
+	
 			# minmax					
 			attribute = self.prefix()+'{:03d}_MinMax_History'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-			if val.size >= HISTORY_SIZE: 
-				val = np.roll(val, 1);val[-1] = self.minmax; selfseq_dev.write_attribute(attribute,val)
-			else:
-				val = np.append(val,self.minmax); selfseq_dev.write_attribute(attribute,val)	
+			val = np.roll(val, -1);val[-1] = self.minmax; selfseq_dev.write_attribute(attribute,val)
 
 			if self.tag == SENSOR_TAG:
 				for target in target_dev:
@@ -532,29 +515,19 @@ class DeviceObj:
 							# History for Corr, CorrFiltAbs, CorrLow, CorrHigh
 							# corr
 							attribute = self.prefix()+'s{:03d}_t{:03d}_Corr'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.corr[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.corr[target.index]); selfseq_dev.write_attribute(attribute,val)	
+							val = np.roll(val, -1);val[-1] = self.corr[target.index]; selfseq_dev.write_attribute(attribute,val)
+	
 							# corrfiltabs							
 							attribute = self.prefix()+'s{:03d}_t{:03d}_CorrFiltAbs'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.lp_filter_corr[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.lp_filter_corr[target.index]); selfseq_dev.write_attribute(attribute,val)	
+							val = np.roll(val, -1);val[-1] = self.lp_filter_corr[target.index]; selfseq_dev.write_attribute(attribute,val)
+	
 							# corrlow								
 							attribute = self.prefix()+'s{:03d}_t{:03d}_CorrLow'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.corr_low[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.corr_low[target.index]); selfseq_dev.write_attribute(attribute,val)
+							val = np.roll(val, -1);val[-1] = self.corr_low[target.index]; selfseq_dev.write_attribute(attribute,val)
+
 							# corrhigh																	
 							attribute = self.prefix()+'s{:03d}_t{:03d}_CorrHigh'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.corr_high[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.corr_high[target.index]); selfseq_dev.write_attribute(attribute,val)								
-							
+							val = np.roll(val, -1);val[-1] = self.corr_high[target.index]; selfseq_dev.write_attribute(attribute,val)								
 
 			elif self.tag == TARGET_TAG:
 				for target in target_dev:
@@ -574,28 +547,19 @@ class DeviceObj:
 							# History for Corr, CorrFiltAbs, CorrLow, CorrHigh
 							# corr
 							attribute = self.prefix()+'t{:03d}_t{:03d}_Corr'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.corr[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.corr[target.index]); selfseq_dev.write_attribute(attribute,val)	
+							val = np.roll(val, -1);val[-1] = self.corr[target.index]; selfseq_dev.write_attribute(attribute,val)
+	
 							# corrfiltabs							
 							attribute = self.prefix()+'t{:03d}_t{:03d}_CorrFiltAbs'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.lp_filter_corr[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.lp_filter_corr[target.index]); selfseq_dev.write_attribute(attribute,val)	
+							val = np.roll(val, -1);val[-1] = self.lp_filter_corr[target.index]; selfseq_dev.write_attribute(attribute,val)
+	
 							# corrlow								
 							attribute = self.prefix()+'t{:03d}_t{:03d}_CorrLow'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.corr_low[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.corr_low[target.index]); selfseq_dev.write_attribute(attribute,val)
+							val = np.roll(val, -1);val[-1] = self.corr_low[target.index]; selfseq_dev.write_attribute(attribute,val)
+
 							# corrhigh																	
 							attribute = self.prefix()+'t{:03d}_t{:03d}_CorrHigh'.format(self.index+1); val = selfseq_dev.read_attribute(attribute); val = val.value
-							if val.size >= HISTORY_SIZE: 
-								val = np.roll(val, 1);val[-1] = self.corr_high[target.index]; selfseq_dev.write_attribute(attribute,val)
-							else:
-								val = np.append(val,self.corr_high[target.index]); selfseq_dev.write_attribute(attribute,val)							
+							val = np.roll(val, -1);val[-1] = self.corr_high[target.index]; selfseq_dev.write_attribute(attribute,val)						
 							
 
 			attribute=self.prefix()+'{:03d}_BunchNumberEnd'.format(self.index+1);selfseq_dev.write_attribute(attribute,self.bn_end)
-- 
GitLab