first commit
[bl_monitoring.git] / tools / archiving_extraction / extract_shift_v0.py
blob28ad2f37b898d71c4c26c8c4214f8cca648f38ca
1 #!/usr/bin/env python
3 import os, sys, time
5 from PyTango import *
7 from build_bl import beam_path
8 from bl_metastruct import *
10 from extraction import *
11 from data_portioning import *
14 ###############################################
15 startShift_hour = '08:00:00'
16 endShift_hour = '08:00:00'
18 extraPeriod_minLength_sec = 60*5 # = 5 min
19 EXTRAPERIOD_MAXLENGTH_SEC = 60*60*3 # = 3 hours
21 STEADYPERIODS_SEPARATORS = ['mono1/energy']
22 ###############################################
26 ############################
27 if __name__ == '__main__':
30 if len(sys.argv) < 3:
31 print "Correct use: " + sys.argv[0] + " startDate_str ('Y-m-d') dest_dir [hdb/tdb]"
32 sys.exit(0)
34 startDate_str = sys.argv[1]
35 dest_dir = sys.argv[2]
38 db = "TDB"
39 if len(sys.argv) == 4 and (sys.argv[3] == "hdb" or sys.argv[3] == "HDB"):
40 extractor = DeviceProxy("archiving/hdb/hdbextractor.1")
41 db = "HDB"
42 else:
43 extractor = DeviceProxy("archiving/tdb/tdbextractor.1")
44 #print db #debug
47 extractor.set_timeout_millis(100000)
48 # ----------------------------------------------------------------------------------------------
50 startShift_str = startDate_str +' '+ startShift_hour
51 startShift_sec = str2sec(startShift_str)
53 startShift_hour_sec = hour2sec(startShift_hour)
54 endShift_hour_sec = hour2sec(endShift_hour)
56 if startShift_hour_sec >= endShift_hour_sec:
57 startShift_date_sec = time.mktime(time.strptime(startDate_str, "%Y-%m-%d"))
58 endShift_date_sec = startShift_date_sec + hour2sec("24:00:00")
60 endShift_sec = endShift_date_sec + endShift_hour_sec
61 endShift_str = sec2str(endShift_sec)
63 ## print startShift_str # debug
64 ## print endShift_str # debug
65 # ----------------------------------------------------------------------------------------------
67 # INIT ---
68 extractionPeriods_sec = [(startShift_sec, endShift_sec)]
69 extractionPeriods_str = periodsSec2periodsStr(extractionPeriods_sec)
70 steadyPeriods_str = extractionPeriods_str
72 #beam_monitors = []
73 ############################# Main loop #########################
75 for elem in beam_path:
76 if isinstance(elem, beam_monitor)and not isinstance(elem, extractable_beamMonitor):
77 monitor = elem # just a notation for better readability
78 print '\nMonitor: ', monitor.nickname
79 #beam_monitors.append(elem)
81 monitor.define_blSteadiness(beam_path)
83 # ------------------------------------------
84 for param in monitor.beamAccess_params:
85 print '\nBeam access: ', param.name # debug
87 new_extractionPeriods_str = []
88 for time_interval in extractionPeriods_str:
89 #print timeInterval_str # debug
91 param_data = extractAttr_relativeTime( param.attr_fullName, startShift_sec,
92 time_interval, extractor, dest_dir )
93 #print len(param_data) # debug
94 if len(param_data) == 0:
95 subPeriods_str = takeWholeOrEmpty_extendingExtraction(param, time_interval, extractor, dest_dir)
96 else:
97 subPeriods_str = get_extraPeriods(param, param_data, time_interval, extraPeriod_minLength_sec)
99 new_extractionPeriods_str += subPeriods_str
100 #print new_extractionPeriods_str # debug
102 extractionPeriods_str = new_extractionPeriods_str
103 print extractionPeriods_str # debug
105 steadyPeriods_str = extractionPeriods_str
106 #print steadyPeriods_str # debug
108 # ------------------------------------------
109 ## steadinessParam_names = []
110 ## for param in monitor.blSteadiness_params:
111 ## steadinessParam_names.append(param.name)
112 ## print steadinessParam_names # debug
114 for param in monitor.blSteadiness_params:
115 if param.name in STEADYPERIODS_SEPARATORS:
116 print '\nSteadiness: ', param.name # debug
118 new_steadyPeriods_str = []
120 for time_interval in extractionPeriods_str:
122 param_data = extractAttr_relativeTime( param.attr_fullName, startShift_sec,
123 time_interval, extractor, dest_dir )
124 if len(param_data) == 0:
125 subPeriods_str = [time_interval] # as no value means it did not change => steadiness
126 # ........... but the extension of the time_interval may be needed anyway ? .......
127 else:
128 subPeriods_str = get_steadyPeriods(param, param_data, time_interval,
129 extraPeriod_minLength_sec)
130 new_steadyPeriods_str += subPeriods_str
131 #print new_steadyPeriods_str # debug
133 steadyPeriods_str = new_steadyPeriods_str
134 print steadyPeriods_str
136 # ------------------------------------------
137 for param in monitor.blSteadiness_params:
138 if not param.name in STEADYPERIODS_SEPARATORS:
140 for steady_period in steadyPeriods_str:
141 ## shortSteadyPeriods_str = split_longInterval(steady_period, EXTRAPERIOD_MAXLENGTH_SEC)
143 ## for short_steadyPeriod in shortSteadyPeriods_str:
145 param_data = extractAttr_relativeTime( param.attr_fullName, startShift_sec,
146 steady_period, extractor, dest_dir )
148 # ------------------------------------------
149 for param in monitor.params:
150 print '\n', param.name # debug
152 for steady_period in steadyPeriods_str:
153 shortSteadyPeriods_str = split_longInterval(steady_period, EXTRAPERIOD_MAXLENGTH_SEC)
155 for short_steadyPeriod in shortSteadyPeriods_str:
157 param_data = extractAttr_relativeTime( param.attr_fullName, startShift_sec,
158 short_steadyPeriod, extractor, dest_dir )
159 # -------------------------------------------------------
161 sys.exit(0) ############ DBUG ##############
167 # TEST 1 -------------------------------------------
168 attr_list = reading_txtac.attrs_fromDevList(archi_filename)
169 #print attr_list # DEBUG
170 date_string = startTime_str[:10]
171 startHour_string = startTime_str[11:].replace(':', '.')
172 endHour_string = endTime_str[11:].replace(':', '.')
174 baseOutputName = dest_dir + os.sep + date_string + '_' + startHour_string + '-' + endHour_string + '_'
176 extractData_fromTimeToTime( attr_list, startTime_str, endTime_str, extractor, baseOutputName)
179 # TEST 2 -------------------------------------------
180 #extractData_archivedPeriods(archi_filename, startTime_str, endTime_str, extractor, dest_dir)