first commit
[bl_monitoring.git] / tools / data_processing / timePortioning_byData.py
blob27312f56fe603d0c5594ab9da5aaf0eab4d388e9
1 #!/usr/bin/env python
3 import os, sys, time
5 from tools.generic_utils.time_formats import *
6 from tools.generic_utils.time_intervals import *
7 #from tools.generic_utils.dataPortioning_byTime import *
10 #############################################
11 def get_okIntervals_indxs(param, param_data):
13 if len(param_data) == 0:
14 return [] # it is not really correct but we are not supposed to get here
16 #timeInterval_sec = periodsStr2periodsSec([timeInterval_str])[0]
18 curr_startInx = -1
19 curr_endInx = -1
20 ok_intervals = []
21 was_ok = False
23 for i in range(len(param_data)):
25 ok = param.is_ok(param_data[i][1])
26 if ok and not was_ok:
27 curr_startInx = i
28 was_ok = True
29 elif not ok and was_ok:
30 curr_endInx = i-1
31 was_ok = False
32 ok_intervals.append((curr_startInx, curr_endInx))
34 #if curr_startInx != -1 and curr_endInx == -1:
35 if curr_startInx > curr_endInx:
36 ok_intervals.append((curr_startInx, len(param_data)-1))
38 #print ok_intervals # debug
40 ## if len(ok_intervals) == 0:
41 ## return []
43 return ok_intervals
46 #################################################################################
47 def get_extraPeriods(param, param_data, timeInterval_str, minLength_sec = 0):
50 okIntervals_indxs = get_okIntervals_indxs(param, param_data)
52 if len(okIntervals_indxs) == 0:
53 return []
55 subIntervals_sec = []
57 timeInterval_sec = periodsStr2periodsSec([timeInterval_str])[0]
59 if okIntervals_indxs[0][0] == 0:
60 first_start = timeInterval_sec[0]
61 else: first_start = param_data[okIntervals_indxs[0][0]][0]
63 #print sec2str(first_start) # debug
65 if okIntervals_indxs[-1][1] == len(param_data)-1:
66 last_end = timeInterval_sec[1]
67 else: last_end = param_data[okIntervals_indxs[-1][1]][0]
69 #print sec2str(last_end) # debug
71 if len(okIntervals_indxs) == 1:
72 subIntervals_sec.append( (first_start, last_end) )
73 else:
74 subIntervals_sec.append((first_start, param_data[okIntervals_indxs[0][1]][0]))
75 for i in range(1, len(okIntervals_indxs)-1):
76 subIntervals_sec.append((param_data[okIntervals_indxs[i][0]][0], param_data[okIntervals_indxs[i][1]][0]))
77 subIntervals_sec.append((param_data[okIntervals_indxs[-1][0]][0], last_end))
81 return periodsSec2periodsStr( select_longIntervals(subIntervals_sec, minLength_sec) )
84 #################################################################################
85 def get_steadyPeriods(param, param_data, timeInterval_str, minLength_sec = 0):
87 #subIntervals_sec = periodsStr2periodsSec([timeInterval_str])
89 var_data = param.get_variation(param_data, periodsStr2periodsSec([timeInterval_str])[0])
91 okIntervals_indxs = get_okIntervals_indxs(param, var_data)
93 subIntervals_sec = []
94 param_vals = []
95 for i in okIntervals_indxs:
96 subIntervals_sec.append( (var_data[i[0]][0][0], var_data[i[1]][0][1]) )
97 param_vals.append(param_data[i[0]][1])
99 #return periodsSec2periodsStr( select_longIntervals(subIntervals_sec, minLength_sec) ), param_vals
100 return periodsSec2periodsStr(subIntervals_sec), param_vals
102 #################################################################################
103 def takeWholeOrEmpty_extendingTime(param, timeInterval_str, param_data):
105 if ( str2sec(timeInterval_str[1]) - str2sec(timeInterval_str[0]) <=
106 float(param.archiving_period)/1000 ):
108 extended_timeInterval_sec = ( max( str2sec(timeInterval_str[1])- float(param.archiving_period)/1000,
109 param_data[0][0]),
110 str2sec(timeInterval_str[1]) )
112 # take the corresponding portion of param_data
113 data_portion = get_dataPortion(param_data, extended_timeInterval_sec)
115 if len(data_portion) == 0:
116 print "No data for ", param.attr_fullName, " in the ", db, \
117 " for the period ", periodsSec2periodsStr([extended_timeInterval_sec])[0]
118 sys.exit(0)
120 if param.is_ok(data_portion[-1][1]):
121 return [timeInterval_str] # whole interval
122 else:
123 return [] # empty list
124 else:
125 print "No data for ", param.attr_fullName, " in the ", db, \
126 " for the period ", timeInterval_str
127 sys.exit(0)