##// END OF EJS Templates
add multiSchain (@jchavez)
jespinoza -
r892:30b3788062ca
parent child
Show More
@@ -0,0 +1,82
1 import argparse
2
3 from schainpy.controller import Project, multiSchain
4
5 desc = "HF_EXAMPLE"
6
7 def fiber(cursor, skip, q, dt):
8
9 controllerObj = Project()
10
11 controllerObj.setup(id='191', name='test01', description=desc)
12
13 readUnitConfObj = controllerObj.addReadUnit(datatype='SpectraReader',
14 path='/data/workspace/data/julia/',
15 startDate=dt,
16 endDate=dt,
17 startTime="00:00:00",
18 endTime="23:59:59",
19 online=0,
20 #set=1426485881,
21 delay=10,
22 walk=1,
23 queue=q,
24 cursor=cursor,
25 skip=skip,
26 #timezone=-5*3600
27 )
28
29 # #opObj11 = readUnitConfObj.addOperation(name='printNumberOfBlock')
30 #
31 procUnitConfObj2 = controllerObj.addProcUnit(datatype='Spectra', inputId=readUnitConfObj.getId())
32 # opObj11 = procUnitConfObj2.addParameter(name='pairsList', value='(0,1)', format='pairslist')
33 #
34 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='ParametersProc', inputId=readUnitConfObj.getId())
35
36 # opObj11 = procUnitConfObj2.addOperation(name='SpectralMoments', optype='other')
37
38 #
39 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
40 # opObj11.addParameter(name='id', value='1000', format='int')
41 # opObj11.addParameter(name='wintitle', value='HF_Jicamarca_Spc', format='str')
42 # opObj11.addParameter(name='channelList', value='0', format='intlist')
43 # opObj11.addParameter(name='zmin', value='-120', format='float')
44 # opObj11.addParameter(name='zmax', value='-70', format='float')
45 # opObj11.addParameter(name='save', value='1', format='int')
46 # opObj11.addParameter(name='figpath', value=figpath, format='str')
47
48 # opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='other')
49 # opObj11.addParameter(name='id', value='2000', format='int')
50 # opObj11.addParameter(name='wintitle', value='HF_Jicamarca', format='str')
51 # opObj11.addParameter(name='showprofile', value='0', format='int')
52 # opObj11.addParameter(name='channelList', value='0', format='intlist')
53 # # opObj11.addParameter(name='xmin', value='0', format='float')
54 # opObj11.addParameter(name='xmin', value='0', format='float')
55 # opObj11.addParameter(name='xmax', value='24', format='float')
56 #
57 # opObj11.addParameter(name='zmin', value='-110', format='float')
58 # opObj11.addParameter(name='zmax', value='-70', format='float')
59 # opObj11.addParameter(name='save', value='0', format='int')
60 # opObj11.addParameter(name='figpath', value='/tmp/', format='str')
61
62 opObj12 = procUnitConfObj2.addOperation(name='PublishData', optype='other')
63 opObj12.addParameter(name='zeromq', value=1, format='int')
64
65 # print "Escribiendo el archivo XML"
66 # controllerObj.writeXml(filename)
67 # print "Leyendo el archivo XML"
68 # controllerObj.readXml(filename)
69
70 controllerObj.createObjects()
71 controllerObj.connectObjects()
72
73 # timeit.timeit('controllerObj.run()', number=2)
74
75 controllerObj.run()
76
77
78 if __name__ == '__main__':
79 parser = argparse.ArgumentParser(description='Set number of parallel processes')
80 parser.add_argument('--nProcess', default=2, type=int)
81 args = parser.parse_args()
82 multiSchain(fiber, nProcess=args.nProcess, startDate='2016/08/19', endDate='2016/08/20')
@@ -1,1261 +1,1314
1 '''
1 '''
2 Created on September , 2012
2 Created on September , 2012
3 @author:
3 @author:
4 '''
4 '''
5
5
6 import sys
6 import sys
7 import ast
7 import ast
8 import datetime
8 import datetime
9 import traceback
9 import traceback
10 from multiprocessing import Process, Queue, cpu_count
11
10 import schainpy
12 import schainpy
11 import schainpy.admin
13 import schainpy.admin
12
14
13 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
15 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
14 from xml.dom import minidom
16 from xml.dom import minidom
15
17
16 from schainpy.model import *
18 from schainpy.model import *
17 from time import sleep
19 from time import sleep
18
20
19 def prettify(elem):
21 def prettify(elem):
20 """Return a pretty-printed XML string for the Element.
22 """Return a pretty-printed XML string for the Element.
21 """
23 """
22 rough_string = tostring(elem, 'utf-8')
24 rough_string = tostring(elem, 'utf-8')
23 reparsed = minidom.parseString(rough_string)
25 reparsed = minidom.parseString(rough_string)
24 return reparsed.toprettyxml(indent=" ")
26 return reparsed.toprettyxml(indent=" ")
25
27
28 def multiSchain(child, nProcess=cpu_count(), startDate=None, endDate=None):
29 skip = 0
30 cursor = 0
31 nFiles = None
32 processes = []
33
34 dt1 = datetime.datetime.strptime(startDate, '%Y/%m/%d')
35 dt2 = datetime.datetime.strptime(endDate, '%Y/%m/%d')
36 days = (dt2 - dt1).days
37 print days
38 for day in range(days+1):
39 skip = 0
40 cursor = 0
41 q = Queue()
42 processes = []
43 dt = (dt1 + datetime.timedelta(day)).strftime('%Y/%m/%d')
44 firstProcess = Process(target=child, args=(cursor, skip, q, dt))
45 firstProcess.start()
46 nFiles = q.get()
47 firstProcess.terminate()
48 skip = int(math.ceil(nFiles/nProcess))
49 try:
50 while True:
51 processes.append(Process(target=child, args=(cursor, skip, q, dt)))
52 processes[cursor].start()
53 if nFiles < cursor*skip:
54 break
55 cursor += 1
56 except KeyboardInterrupt:
57 for process in processes:
58 process.terminate()
59 process.join()
60 for process in processes:
61 process.join()
62 #process.terminate()
63 sleep(3)
64
65 try:
66 while True:
67 pass
68 except KeyboardInterrupt:
69 for process in processes:
70 process.terminate()
71 process.join()
72
26 class ParameterConf():
73 class ParameterConf():
27
74
28 id = None
75 id = None
29 name = None
76 name = None
30 value = None
77 value = None
31 format = None
78 format = None
32
79
33 __formated_value = None
80 __formated_value = None
34
81
35 ELEMENTNAME = 'Parameter'
82 ELEMENTNAME = 'Parameter'
36
83
37 def __init__(self):
84 def __init__(self):
38
85
39 self.format = 'str'
86 self.format = 'str'
40
87
41 def getElementName(self):
88 def getElementName(self):
42
89
43 return self.ELEMENTNAME
90 return self.ELEMENTNAME
44
91
45 def getValue(self):
92 def getValue(self):
46
93
47 value = self.value
94 value = self.value
48 format = self.format
95 format = self.format
49
96
50 if self.__formated_value != None:
97 if self.__formated_value != None:
51
98
52 return self.__formated_value
99 return self.__formated_value
53
100
101 if format == 'obj':
102 return value
103
54 if format == 'str':
104 if format == 'str':
55 self.__formated_value = str(value)
105 self.__formated_value = str(value)
56 return self.__formated_value
106 return self.__formated_value
57
107
58 if value == '':
108 if value == '':
59 raise ValueError, "%s: This parameter value is empty" %self.name
109 raise ValueError, "%s: This parameter value is empty" %self.name
60
110
61 if format == 'list':
111 if format == 'list':
62 strList = value.split(',')
112 strList = value.split(',')
63
113
64 self.__formated_value = strList
114 self.__formated_value = strList
65
115
66 return self.__formated_value
116 return self.__formated_value
67
117
68 if format == 'intlist':
118 if format == 'intlist':
69 """
119 """
70 Example:
120 Example:
71 value = (0,1,2)
121 value = (0,1,2)
72 """
122 """
73
123
74 new_value = ast.literal_eval(value)
124 new_value = ast.literal_eval(value)
75
125
76 if type(new_value) not in (tuple, list):
126 if type(new_value) not in (tuple, list):
77 new_value = [int(new_value)]
127 new_value = [int(new_value)]
78
128
79 self.__formated_value = new_value
129 self.__formated_value = new_value
80
130
81 return self.__formated_value
131 return self.__formated_value
82
132
83 if format == 'floatlist':
133 if format == 'floatlist':
84 """
134 """
85 Example:
135 Example:
86 value = (0.5, 1.4, 2.7)
136 value = (0.5, 1.4, 2.7)
87 """
137 """
88
138
89 new_value = ast.literal_eval(value)
139 new_value = ast.literal_eval(value)
90
140
91 if type(new_value) not in (tuple, list):
141 if type(new_value) not in (tuple, list):
92 new_value = [float(new_value)]
142 new_value = [float(new_value)]
93
143
94 self.__formated_value = new_value
144 self.__formated_value = new_value
95
145
96 return self.__formated_value
146 return self.__formated_value
97
147
98 if format == 'date':
148 if format == 'date':
99 strList = value.split('/')
149 strList = value.split('/')
100 intList = [int(x) for x in strList]
150 intList = [int(x) for x in strList]
101 date = datetime.date(intList[0], intList[1], intList[2])
151 date = datetime.date(intList[0], intList[1], intList[2])
102
152
103 self.__formated_value = date
153 self.__formated_value = date
104
154
105 return self.__formated_value
155 return self.__formated_value
106
156
107 if format == 'time':
157 if format == 'time':
108 strList = value.split(':')
158 strList = value.split(':')
109 intList = [int(x) for x in strList]
159 intList = [int(x) for x in strList]
110 time = datetime.time(intList[0], intList[1], intList[2])
160 time = datetime.time(intList[0], intList[1], intList[2])
111
161
112 self.__formated_value = time
162 self.__formated_value = time
113
163
114 return self.__formated_value
164 return self.__formated_value
115
165
116 if format == 'pairslist':
166 if format == 'pairslist':
117 """
167 """
118 Example:
168 Example:
119 value = (0,1),(1,2)
169 value = (0,1),(1,2)
120 """
170 """
121
171
122 new_value = ast.literal_eval(value)
172 new_value = ast.literal_eval(value)
123
173
124 if type(new_value) not in (tuple, list):
174 if type(new_value) not in (tuple, list):
125 raise ValueError, "%s has to be a tuple or list of pairs" %value
175 raise ValueError, "%s has to be a tuple or list of pairs" %value
126
176
127 if type(new_value[0]) not in (tuple, list):
177 if type(new_value[0]) not in (tuple, list):
128 if len(new_value) != 2:
178 if len(new_value) != 2:
129 raise ValueError, "%s has to be a tuple or list of pairs" %value
179 raise ValueError, "%s has to be a tuple or list of pairs" %value
130 new_value = [new_value]
180 new_value = [new_value]
131
181
132 for thisPair in new_value:
182 for thisPair in new_value:
133 if len(thisPair) != 2:
183 if len(thisPair) != 2:
134 raise ValueError, "%s has to be a tuple or list of pairs" %value
184 raise ValueError, "%s has to be a tuple or list of pairs" %value
135
185
136 self.__formated_value = new_value
186 self.__formated_value = new_value
137
187
138 return self.__formated_value
188 return self.__formated_value
139
189
140 if format == 'multilist':
190 if format == 'multilist':
141 """
191 """
142 Example:
192 Example:
143 value = (0,1,2),(3,4,5)
193 value = (0,1,2),(3,4,5)
144 """
194 """
145 multiList = ast.literal_eval(value)
195 multiList = ast.literal_eval(value)
146
196
147 if type(multiList[0]) == int:
197 if type(multiList[0]) == int:
148 multiList = ast.literal_eval("(" + value + ")")
198 multiList = ast.literal_eval("(" + value + ")")
149
199
150 self.__formated_value = multiList
200 self.__formated_value = multiList
151
201
152 return self.__formated_value
202 return self.__formated_value
153
203
154 if format == 'bool':
204 if format == 'bool':
155 value = int(value)
205 value = int(value)
156
206
157 if format == 'int':
207 if format == 'int':
158 value = float(value)
208 value = float(value)
159
209
160 format_func = eval(format)
210 format_func = eval(format)
161
211
162 self.__formated_value = format_func(value)
212 self.__formated_value = format_func(value)
163
213
164 return self.__formated_value
214 return self.__formated_value
165
215
166 def updateId(self, new_id):
216 def updateId(self, new_id):
167
217
168 self.id = str(new_id)
218 self.id = str(new_id)
169
219
170 def setup(self, id, name, value, format='str'):
220 def setup(self, id, name, value, format='str'):
171
221
172 self.id = str(id)
222 self.id = str(id)
173 self.name = name
223 self.name = name
174 self.value = str(value)
224 if format == 'obj':
225 self.value = value
226 else:
227 self.value = str(value)
175 self.format = str.lower(format)
228 self.format = str.lower(format)
176
229
177 self.getValue()
230 self.getValue()
178
231
179 return 1
232 return 1
180
233
181 def update(self, name, value, format='str'):
234 def update(self, name, value, format='str'):
182
235
183 self.name = name
236 self.name = name
184 self.value = str(value)
237 self.value = str(value)
185 self.format = format
238 self.format = format
186
239
187 def makeXml(self, opElement):
240 def makeXml(self, opElement):
188
241
189 parmElement = SubElement(opElement, self.ELEMENTNAME)
242 parmElement = SubElement(opElement, self.ELEMENTNAME)
190 parmElement.set('id', str(self.id))
243 parmElement.set('id', str(self.id))
191 parmElement.set('name', self.name)
244 parmElement.set('name', self.name)
192 parmElement.set('value', self.value)
245 parmElement.set('value', self.value)
193 parmElement.set('format', self.format)
246 parmElement.set('format', self.format)
194
247
195 def readXml(self, parmElement):
248 def readXml(self, parmElement):
196
249
197 self.id = parmElement.get('id')
250 self.id = parmElement.get('id')
198 self.name = parmElement.get('name')
251 self.name = parmElement.get('name')
199 self.value = parmElement.get('value')
252 self.value = parmElement.get('value')
200 self.format = str.lower(parmElement.get('format'))
253 self.format = str.lower(parmElement.get('format'))
201
254
202 #Compatible with old signal chain version
255 #Compatible with old signal chain version
203 if self.format == 'int' and self.name == 'idfigure':
256 if self.format == 'int' and self.name == 'idfigure':
204 self.name = 'id'
257 self.name = 'id'
205
258
206 def printattr(self):
259 def printattr(self):
207
260
208 print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format)
261 print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format)
209
262
210 class OperationConf():
263 class OperationConf():
211
264
212 id = None
265 id = None
213 name = None
266 name = None
214 priority = None
267 priority = None
215 type = None
268 type = None
216
269
217 parmConfObjList = []
270 parmConfObjList = []
218
271
219 ELEMENTNAME = 'Operation'
272 ELEMENTNAME = 'Operation'
220
273
221 def __init__(self):
274 def __init__(self):
222
275
223 self.id = '0'
276 self.id = '0'
224 self.name = None
277 self.name = None
225 self.priority = None
278 self.priority = None
226 self.type = 'self'
279 self.type = 'self'
227
280
228
281
229 def __getNewId(self):
282 def __getNewId(self):
230
283
231 return int(self.id)*10 + len(self.parmConfObjList) + 1
284 return int(self.id)*10 + len(self.parmConfObjList) + 1
232
285
233 def updateId(self, new_id):
286 def updateId(self, new_id):
234
287
235 self.id = str(new_id)
288 self.id = str(new_id)
236
289
237 n = 1
290 n = 1
238 for parmObj in self.parmConfObjList:
291 for parmObj in self.parmConfObjList:
239
292
240 idParm = str(int(new_id)*10 + n)
293 idParm = str(int(new_id)*10 + n)
241 parmObj.updateId(idParm)
294 parmObj.updateId(idParm)
242
295
243 n += 1
296 n += 1
244
297
245 def getElementName(self):
298 def getElementName(self):
246
299
247 return self.ELEMENTNAME
300 return self.ELEMENTNAME
248
301
249 def getParameterObjList(self):
302 def getParameterObjList(self):
250
303
251 return self.parmConfObjList
304 return self.parmConfObjList
252
305
253 def getParameterObj(self, parameterName):
306 def getParameterObj(self, parameterName):
254
307
255 for parmConfObj in self.parmConfObjList:
308 for parmConfObj in self.parmConfObjList:
256
309
257 if parmConfObj.name != parameterName:
310 if parmConfObj.name != parameterName:
258 continue
311 continue
259
312
260 return parmConfObj
313 return parmConfObj
261
314
262 return None
315 return None
263
316
264 def getParameterObjfromValue(self, parameterValue):
317 def getParameterObjfromValue(self, parameterValue):
265
318
266 for parmConfObj in self.parmConfObjList:
319 for parmConfObj in self.parmConfObjList:
267
320
268 if parmConfObj.getValue() != parameterValue:
321 if parmConfObj.getValue() != parameterValue:
269 continue
322 continue
270
323
271 return parmConfObj.getValue()
324 return parmConfObj.getValue()
272
325
273 return None
326 return None
274
327
275 def getParameterValue(self, parameterName):
328 def getParameterValue(self, parameterName):
276
329
277 parameterObj = self.getParameterObj(parameterName)
330 parameterObj = self.getParameterObj(parameterName)
278
331
279 # if not parameterObj:
332 # if not parameterObj:
280 # return None
333 # return None
281
334
282 value = parameterObj.getValue()
335 value = parameterObj.getValue()
283
336
284 return value
337 return value
285
338
286
339
287 def getKwargs(self):
340 def getKwargs(self):
288
341
289 kwargs = {}
342 kwargs = {}
290
343
291 for parmConfObj in self.parmConfObjList:
344 for parmConfObj in self.parmConfObjList:
292 if self.name == 'run' and parmConfObj.name == 'datatype':
345 if self.name == 'run' and parmConfObj.name == 'datatype':
293 continue
346 continue
294
347
295 kwargs[parmConfObj.name] = parmConfObj.getValue()
348 kwargs[parmConfObj.name] = parmConfObj.getValue()
296
349
297 return kwargs
350 return kwargs
298
351
299 def setup(self, id, name, priority, type):
352 def setup(self, id, name, priority, type):
300
353
301 self.id = str(id)
354 self.id = str(id)
302 self.name = name
355 self.name = name
303 self.type = type
356 self.type = type
304 self.priority = priority
357 self.priority = priority
305
358
306 self.parmConfObjList = []
359 self.parmConfObjList = []
307
360
308 def removeParameters(self):
361 def removeParameters(self):
309
362
310 for obj in self.parmConfObjList:
363 for obj in self.parmConfObjList:
311 del obj
364 del obj
312
365
313 self.parmConfObjList = []
366 self.parmConfObjList = []
314
367
315 def addParameter(self, name, value, format='str'):
368 def addParameter(self, name, value, format='str'):
316
369
317 id = self.__getNewId()
370 id = self.__getNewId()
318
371
319 parmConfObj = ParameterConf()
372 parmConfObj = ParameterConf()
320 if not parmConfObj.setup(id, name, value, format):
373 if not parmConfObj.setup(id, name, value, format):
321 return None
374 return None
322
375
323 self.parmConfObjList.append(parmConfObj)
376 self.parmConfObjList.append(parmConfObj)
324
377
325 return parmConfObj
378 return parmConfObj
326
379
327 def changeParameter(self, name, value, format='str'):
380 def changeParameter(self, name, value, format='str'):
328
381
329 parmConfObj = self.getParameterObj(name)
382 parmConfObj = self.getParameterObj(name)
330 parmConfObj.update(name, value, format)
383 parmConfObj.update(name, value, format)
331
384
332 return parmConfObj
385 return parmConfObj
333
386
334 def makeXml(self, procUnitElement):
387 def makeXml(self, procUnitElement):
335
388
336 opElement = SubElement(procUnitElement, self.ELEMENTNAME)
389 opElement = SubElement(procUnitElement, self.ELEMENTNAME)
337 opElement.set('id', str(self.id))
390 opElement.set('id', str(self.id))
338 opElement.set('name', self.name)
391 opElement.set('name', self.name)
339 opElement.set('type', self.type)
392 opElement.set('type', self.type)
340 opElement.set('priority', str(self.priority))
393 opElement.set('priority', str(self.priority))
341
394
342 for parmConfObj in self.parmConfObjList:
395 for parmConfObj in self.parmConfObjList:
343 parmConfObj.makeXml(opElement)
396 parmConfObj.makeXml(opElement)
344
397
345 def readXml(self, opElement):
398 def readXml(self, opElement):
346
399
347 self.id = opElement.get('id')
400 self.id = opElement.get('id')
348 self.name = opElement.get('name')
401 self.name = opElement.get('name')
349 self.type = opElement.get('type')
402 self.type = opElement.get('type')
350 self.priority = opElement.get('priority')
403 self.priority = opElement.get('priority')
351
404
352 #Compatible with old signal chain version
405 #Compatible with old signal chain version
353 #Use of 'run' method instead 'init'
406 #Use of 'run' method instead 'init'
354 if self.type == 'self' and self.name == 'init':
407 if self.type == 'self' and self.name == 'init':
355 self.name = 'run'
408 self.name = 'run'
356
409
357 self.parmConfObjList = []
410 self.parmConfObjList = []
358
411
359 parmElementList = opElement.iter(ParameterConf().getElementName())
412 parmElementList = opElement.iter(ParameterConf().getElementName())
360
413
361 for parmElement in parmElementList:
414 for parmElement in parmElementList:
362 parmConfObj = ParameterConf()
415 parmConfObj = ParameterConf()
363 parmConfObj.readXml(parmElement)
416 parmConfObj.readXml(parmElement)
364
417
365 #Compatible with old signal chain version
418 #Compatible with old signal chain version
366 #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
419 #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
367 if self.type != 'self' and self.name == 'Plot':
420 if self.type != 'self' and self.name == 'Plot':
368 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
421 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
369 self.name = parmConfObj.value
422 self.name = parmConfObj.value
370 continue
423 continue
371
424
372 self.parmConfObjList.append(parmConfObj)
425 self.parmConfObjList.append(parmConfObj)
373
426
374 def printattr(self):
427 def printattr(self):
375
428
376 print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME,
429 print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME,
377 self.id,
430 self.id,
378 self.name,
431 self.name,
379 self.type,
432 self.type,
380 self.priority)
433 self.priority)
381
434
382 for parmConfObj in self.parmConfObjList:
435 for parmConfObj in self.parmConfObjList:
383 parmConfObj.printattr()
436 parmConfObj.printattr()
384
437
385 def createObject(self, plotter_queue=None):
438 def createObject(self, plotter_queue=None):
386
439
387 if self.type == 'self':
440 if self.type == 'self':
388 raise ValueError, "This operation type cannot be created"
441 raise ValueError, "This operation type cannot be created"
389
442
390 if self.type == 'plotter':
443 if self.type == 'plotter':
391 #Plotter(plotter_name)
444 #Plotter(plotter_name)
392 if not plotter_queue:
445 if not plotter_queue:
393 raise ValueError, "plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)"
446 raise ValueError, "plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)"
394
447
395 opObj = Plotter(self.name, plotter_queue)
448 opObj = Plotter(self.name, plotter_queue)
396
449
397 if self.type == 'external' or self.type == 'other':
450 if self.type == 'external' or self.type == 'other':
398 className = eval(self.name)
451 className = eval(self.name)
399 kwargs = self.getKwargs()
452 kwargs = self.getKwargs()
400 opObj = className(**kwargs)
453 opObj = className(**kwargs)
401
454
402 return opObj
455 return opObj
403
456
404
457
405 class ProcUnitConf():
458 class ProcUnitConf():
406
459
407 id = None
460 id = None
408 name = None
461 name = None
409 datatype = None
462 datatype = None
410 inputId = None
463 inputId = None
411 parentId = None
464 parentId = None
412
465
413 opConfObjList = []
466 opConfObjList = []
414
467
415 procUnitObj = None
468 procUnitObj = None
416 opObjList = []
469 opObjList = []
417
470
418 ELEMENTNAME = 'ProcUnit'
471 ELEMENTNAME = 'ProcUnit'
419
472
420 def __init__(self):
473 def __init__(self):
421
474
422 self.id = None
475 self.id = None
423 self.datatype = None
476 self.datatype = None
424 self.name = None
477 self.name = None
425 self.inputId = None
478 self.inputId = None
426
479
427 self.opConfObjList = []
480 self.opConfObjList = []
428
481
429 self.procUnitObj = None
482 self.procUnitObj = None
430 self.opObjDict = {}
483 self.opObjDict = {}
431
484
432 def __getPriority(self):
485 def __getPriority(self):
433
486
434 return len(self.opConfObjList)+1
487 return len(self.opConfObjList)+1
435
488
436 def __getNewId(self):
489 def __getNewId(self):
437
490
438 return int(self.id)*10 + len(self.opConfObjList) + 1
491 return int(self.id)*10 + len(self.opConfObjList) + 1
439
492
440 def getElementName(self):
493 def getElementName(self):
441
494
442 return self.ELEMENTNAME
495 return self.ELEMENTNAME
443
496
444 def getId(self):
497 def getId(self):
445
498
446 return self.id
499 return self.id
447
500
448 def updateId(self, new_id, parentId=parentId):
501 def updateId(self, new_id, parentId=parentId):
449
502
450
503
451 new_id = int(parentId)*10 + (int(self.id) % 10)
504 new_id = int(parentId)*10 + (int(self.id) % 10)
452 new_inputId = int(parentId)*10 + (int(self.inputId) % 10)
505 new_inputId = int(parentId)*10 + (int(self.inputId) % 10)
453
506
454 #If this proc unit has not inputs
507 #If this proc unit has not inputs
455 if self.inputId == '0':
508 if self.inputId == '0':
456 new_inputId = 0
509 new_inputId = 0
457
510
458 n = 1
511 n = 1
459 for opConfObj in self.opConfObjList:
512 for opConfObj in self.opConfObjList:
460
513
461 idOp = str(int(new_id)*10 + n)
514 idOp = str(int(new_id)*10 + n)
462 opConfObj.updateId(idOp)
515 opConfObj.updateId(idOp)
463
516
464 n += 1
517 n += 1
465
518
466 self.parentId = str(parentId)
519 self.parentId = str(parentId)
467 self.id = str(new_id)
520 self.id = str(new_id)
468 self.inputId = str(new_inputId)
521 self.inputId = str(new_inputId)
469
522
470
523
471 def getInputId(self):
524 def getInputId(self):
472
525
473 return self.inputId
526 return self.inputId
474
527
475 def getOperationObjList(self):
528 def getOperationObjList(self):
476
529
477 return self.opConfObjList
530 return self.opConfObjList
478
531
479 def getOperationObj(self, name=None):
532 def getOperationObj(self, name=None):
480
533
481 for opConfObj in self.opConfObjList:
534 for opConfObj in self.opConfObjList:
482
535
483 if opConfObj.name != name:
536 if opConfObj.name != name:
484 continue
537 continue
485
538
486 return opConfObj
539 return opConfObj
487
540
488 return None
541 return None
489
542
490 def getOpObjfromParamValue(self, value=None):
543 def getOpObjfromParamValue(self, value=None):
491
544
492 for opConfObj in self.opConfObjList:
545 for opConfObj in self.opConfObjList:
493 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
546 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
494 continue
547 continue
495 return opConfObj
548 return opConfObj
496 return None
549 return None
497
550
498 def getProcUnitObj(self):
551 def getProcUnitObj(self):
499
552
500 return self.procUnitObj
553 return self.procUnitObj
501
554
502 def setup(self, id, name, datatype, inputId, parentId=None):
555 def setup(self, id, name, datatype, inputId, parentId=None):
503
556
504 #Compatible with old signal chain version
557 #Compatible with old signal chain version
505 if datatype==None and name==None:
558 if datatype==None and name==None:
506 raise ValueError, "datatype or name should be defined"
559 raise ValueError, "datatype or name should be defined"
507
560
508 if name==None:
561 if name==None:
509 if 'Proc' in datatype:
562 if 'Proc' in datatype:
510 name = datatype
563 name = datatype
511 else:
564 else:
512 name = '%sProc' %(datatype)
565 name = '%sProc' %(datatype)
513
566
514 if datatype==None:
567 if datatype==None:
515 datatype = name.replace('Proc','')
568 datatype = name.replace('Proc','')
516
569
517 self.id = str(id)
570 self.id = str(id)
518 self.name = name
571 self.name = name
519 self.datatype = datatype
572 self.datatype = datatype
520 self.inputId = inputId
573 self.inputId = inputId
521 self.parentId = parentId
574 self.parentId = parentId
522
575
523 self.opConfObjList = []
576 self.opConfObjList = []
524
577
525 self.addOperation(name='run', optype='self')
578 self.addOperation(name='run', optype='self')
526
579
527 def removeOperations(self):
580 def removeOperations(self):
528
581
529 for obj in self.opConfObjList:
582 for obj in self.opConfObjList:
530 del obj
583 del obj
531
584
532 self.opConfObjList = []
585 self.opConfObjList = []
533 self.addOperation(name='run')
586 self.addOperation(name='run')
534
587
535 def addParameter(self, **kwargs):
588 def addParameter(self, **kwargs):
536 '''
589 '''
537 Add parameters to "run" operation
590 Add parameters to "run" operation
538 '''
591 '''
539 opObj = self.opConfObjList[0]
592 opObj = self.opConfObjList[0]
540
593
541 opObj.addParameter(**kwargs)
594 opObj.addParameter(**kwargs)
542
595
543 return opObj
596 return opObj
544
597
545 def addOperation(self, name, optype='self'):
598 def addOperation(self, name, optype='self'):
546
599
547 id = self.__getNewId()
600 id = self.__getNewId()
548 priority = self.__getPriority()
601 priority = self.__getPriority()
549
602
550 opConfObj = OperationConf()
603 opConfObj = OperationConf()
551 opConfObj.setup(id, name=name, priority=priority, type=optype)
604 opConfObj.setup(id, name=name, priority=priority, type=optype)
552
605
553 self.opConfObjList.append(opConfObj)
606 self.opConfObjList.append(opConfObj)
554
607
555 return opConfObj
608 return opConfObj
556
609
557 def makeXml(self, projectElement):
610 def makeXml(self, projectElement):
558
611
559 procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
612 procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
560 procUnitElement.set('id', str(self.id))
613 procUnitElement.set('id', str(self.id))
561 procUnitElement.set('name', self.name)
614 procUnitElement.set('name', self.name)
562 procUnitElement.set('datatype', self.datatype)
615 procUnitElement.set('datatype', self.datatype)
563 procUnitElement.set('inputId', str(self.inputId))
616 procUnitElement.set('inputId', str(self.inputId))
564
617
565 for opConfObj in self.opConfObjList:
618 for opConfObj in self.opConfObjList:
566 opConfObj.makeXml(procUnitElement)
619 opConfObj.makeXml(procUnitElement)
567
620
568 def readXml(self, upElement):
621 def readXml(self, upElement):
569
622
570 self.id = upElement.get('id')
623 self.id = upElement.get('id')
571 self.name = upElement.get('name')
624 self.name = upElement.get('name')
572 self.datatype = upElement.get('datatype')
625 self.datatype = upElement.get('datatype')
573 self.inputId = upElement.get('inputId')
626 self.inputId = upElement.get('inputId')
574
627
575 if self.ELEMENTNAME == "ReadUnit":
628 if self.ELEMENTNAME == "ReadUnit":
576 self.datatype = self.datatype.replace("Reader", "")
629 self.datatype = self.datatype.replace("Reader", "")
577
630
578 if self.ELEMENTNAME == "ProcUnit":
631 if self.ELEMENTNAME == "ProcUnit":
579 self.datatype = self.datatype.replace("Proc", "")
632 self.datatype = self.datatype.replace("Proc", "")
580
633
581 if self.inputId == 'None':
634 if self.inputId == 'None':
582 self.inputId = '0'
635 self.inputId = '0'
583
636
584 self.opConfObjList = []
637 self.opConfObjList = []
585
638
586 opElementList = upElement.iter(OperationConf().getElementName())
639 opElementList = upElement.iter(OperationConf().getElementName())
587
640
588 for opElement in opElementList:
641 for opElement in opElementList:
589 opConfObj = OperationConf()
642 opConfObj = OperationConf()
590 opConfObj.readXml(opElement)
643 opConfObj.readXml(opElement)
591 self.opConfObjList.append(opConfObj)
644 self.opConfObjList.append(opConfObj)
592
645
593 def printattr(self):
646 def printattr(self):
594
647
595 print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME,
648 print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME,
596 self.id,
649 self.id,
597 self.name,
650 self.name,
598 self.datatype,
651 self.datatype,
599 self.inputId)
652 self.inputId)
600
653
601 for opConfObj in self.opConfObjList:
654 for opConfObj in self.opConfObjList:
602 opConfObj.printattr()
655 opConfObj.printattr()
603
656
604
657
605 def getKwargs(self):
658 def getKwargs(self):
606
659
607 opObj = self.opConfObjList[0]
660 opObj = self.opConfObjList[0]
608 kwargs = opObj.getKwargs()
661 kwargs = opObj.getKwargs()
609
662
610 return kwargs
663 return kwargs
611
664
612 def createObjects(self, plotter_queue=None):
665 def createObjects(self, plotter_queue=None):
613
666
614 className = eval(self.name)
667 className = eval(self.name)
615 kwargs = self.getKwargs()
668 kwargs = self.getKwargs()
616 procUnitObj = className(**kwargs)
669 procUnitObj = className(**kwargs)
617
670
618 for opConfObj in self.opConfObjList:
671 for opConfObj in self.opConfObjList:
619
672
620 if opConfObj.type == 'self':
673 if opConfObj.type == 'self':
621 continue
674 continue
622
675
623 opObj = opConfObj.createObject(plotter_queue)
676 opObj = opConfObj.createObject(plotter_queue)
624
677
625 self.opObjDict[opConfObj.id] = opObj
678 self.opObjDict[opConfObj.id] = opObj
626 procUnitObj.addOperation(opObj, opConfObj.id)
679 procUnitObj.addOperation(opObj, opConfObj.id)
627
680
628 self.procUnitObj = procUnitObj
681 self.procUnitObj = procUnitObj
629
682
630 return procUnitObj
683 return procUnitObj
631
684
632 def run(self):
685 def run(self):
633
686
634 is_ok = False
687 is_ok = False
635
688
636 for opConfObj in self.opConfObjList:
689 for opConfObj in self.opConfObjList:
637
690
638 kwargs = {}
691 kwargs = {}
639 for parmConfObj in opConfObj.getParameterObjList():
692 for parmConfObj in opConfObj.getParameterObjList():
640 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
693 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
641 continue
694 continue
642
695
643 kwargs[parmConfObj.name] = parmConfObj.getValue()
696 kwargs[parmConfObj.name] = parmConfObj.getValue()
644
697
645 #ini = time.time()
698 #ini = time.time()
646
699
647 #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id)
700 #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id)
648 sts = self.procUnitObj.call(opType = opConfObj.type,
701 sts = self.procUnitObj.call(opType = opConfObj.type,
649 opName = opConfObj.name,
702 opName = opConfObj.name,
650 opId = opConfObj.id,
703 opId = opConfObj.id,
651 )
704 )
652
705
653 # total_time = time.time() - ini
706 # total_time = time.time() - ini
654 #
707 #
655 # if total_time > 0.002:
708 # if total_time > 0.002:
656 # print "%s::%s took %f seconds" %(self.name, opConfObj.name, total_time)
709 # print "%s::%s took %f seconds" %(self.name, opConfObj.name, total_time)
657
710
658 is_ok = is_ok or sts
711 is_ok = is_ok or sts
659
712
660 return is_ok
713 return is_ok
661
714
662 def close(self):
715 def close(self):
663
716
664 for opConfObj in self.opConfObjList:
717 for opConfObj in self.opConfObjList:
665 if opConfObj.type == 'self':
718 if opConfObj.type == 'self':
666 continue
719 continue
667
720
668 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
721 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
669 opObj.close()
722 opObj.close()
670
723
671 self.procUnitObj.close()
724 self.procUnitObj.close()
672
725
673 return
726 return
674
727
675 class ReadUnitConf(ProcUnitConf):
728 class ReadUnitConf(ProcUnitConf):
676
729
677 path = None
730 path = None
678 startDate = None
731 startDate = None
679 endDate = None
732 endDate = None
680 startTime = None
733 startTime = None
681 endTime = None
734 endTime = None
682
735
683 ELEMENTNAME = 'ReadUnit'
736 ELEMENTNAME = 'ReadUnit'
684
737
685 def __init__(self):
738 def __init__(self):
686
739
687 self.id = None
740 self.id = None
688 self.datatype = None
741 self.datatype = None
689 self.name = None
742 self.name = None
690 self.inputId = None
743 self.inputId = None
691
744
692 self.parentId = None
745 self.parentId = None
693
746
694 self.opConfObjList = []
747 self.opConfObjList = []
695 self.opObjList = []
748 self.opObjList = []
696
749
697 def getElementName(self):
750 def getElementName(self):
698
751
699 return self.ELEMENTNAME
752 return self.ELEMENTNAME
700
753
701 def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, **kwargs):
754 def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, queue=None, **kwargs):
702
755
703 #Compatible with old signal chain version
756 #Compatible with old signal chain version
704 if datatype==None and name==None:
757 if datatype==None and name==None:
705 raise ValueError, "datatype or name should be defined"
758 raise ValueError, "datatype or name should be defined"
706
759
707 if name==None:
760 if name==None:
708 if 'Reader' in datatype:
761 if 'Reader' in datatype:
709 name = datatype
762 name = datatype
710 else:
763 else:
711 name = '%sReader' %(datatype)
764 name = '%sReader' %(datatype)
712
765
713 if datatype==None:
766 if datatype==None:
714 datatype = name.replace('Reader','')
767 datatype = name.replace('Reader','')
715
768
716 self.id = id
769 self.id = id
717 self.name = name
770 self.name = name
718 self.datatype = datatype
771 self.datatype = datatype
719
772
720 self.path = os.path.abspath(path)
773 self.path = os.path.abspath(path)
721 self.startDate = startDate
774 self.startDate = startDate
722 self.endDate = endDate
775 self.endDate = endDate
723 self.startTime = startTime
776 self.startTime = startTime
724 self.endTime = endTime
777 self.endTime = endTime
725
778
726 self.inputId = '0'
779 self.inputId = '0'
727 self.parentId = parentId
780 self.parentId = parentId
728
781 self.queue = queue
729 self.addRunOperation(**kwargs)
782 self.addRunOperation(**kwargs)
730
783
731 def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs):
784 def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs):
732
785
733 #Compatible with old signal chain version
786 #Compatible with old signal chain version
734 if datatype==None and name==None:
787 if datatype==None and name==None:
735 raise ValueError, "datatype or name should be defined"
788 raise ValueError, "datatype or name should be defined"
736
789
737 if name==None:
790 if name==None:
738 if 'Reader' in datatype:
791 if 'Reader' in datatype:
739 name = datatype
792 name = datatype
740 else:
793 else:
741 name = '%sReader' %(datatype)
794 name = '%sReader' %(datatype)
742
795
743 if datatype==None:
796 if datatype==None:
744 datatype = name.replace('Reader','')
797 datatype = name.replace('Reader','')
745
798
746 self.datatype = datatype
799 self.datatype = datatype
747 self.name = name
800 self.name = name
748 self.path = path
801 self.path = path
749 self.startDate = startDate
802 self.startDate = startDate
750 self.endDate = endDate
803 self.endDate = endDate
751 self.startTime = startTime
804 self.startTime = startTime
752 self.endTime = endTime
805 self.endTime = endTime
753
806
754 self.inputId = '0'
807 self.inputId = '0'
755 self.parentId = parentId
808 self.parentId = parentId
756
809
757 self.updateRunOperation(**kwargs)
810 self.updateRunOperation(**kwargs)
758
811
759 def removeOperations(self):
812 def removeOperations(self):
760
813
761 for obj in self.opConfObjList:
814 for obj in self.opConfObjList:
762 del obj
815 del obj
763
816
764 self.opConfObjList = []
817 self.opConfObjList = []
765
818
766 def addRunOperation(self, **kwargs):
819 def addRunOperation(self, **kwargs):
767
820
768 opObj = self.addOperation(name = 'run', optype = 'self')
821 opObj = self.addOperation(name = 'run', optype = 'self')
769
822
770 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
823 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
771 opObj.addParameter(name='path' , value=self.path, format='str')
824 opObj.addParameter(name='path' , value=self.path, format='str')
772 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
825 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
773 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
826 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
774 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
827 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
775 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
828 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
776
829
777 for key, value in kwargs.items():
830 for key, value in kwargs.items():
778 opObj.addParameter(name=key, value=value, format=type(value).__name__)
831 opObj.addParameter(name=key, value=value, format=type(value).__name__)
779
832
780 return opObj
833 return opObj
781
834
782 def updateRunOperation(self, **kwargs):
835 def updateRunOperation(self, **kwargs):
783
836
784 opObj = self.getOperationObj(name = 'run')
837 opObj = self.getOperationObj(name = 'run')
785 opObj.removeParameters()
838 opObj.removeParameters()
786
839
787 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
840 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
788 opObj.addParameter(name='path' , value=self.path, format='str')
841 opObj.addParameter(name='path' , value=self.path, format='str')
789 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
842 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
790 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
843 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
791 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
844 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
792 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
845 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
793
846
794 for key, value in kwargs.items():
847 for key, value in kwargs.items():
795 opObj.addParameter(name=key, value=value, format=type(value).__name__)
848 opObj.addParameter(name=key, value=value, format=type(value).__name__)
796
849
797 return opObj
850 return opObj
798
851
799 # def makeXml(self, projectElement):
852 # def makeXml(self, projectElement):
800 #
853 #
801 # procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
854 # procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
802 # procUnitElement.set('id', str(self.id))
855 # procUnitElement.set('id', str(self.id))
803 # procUnitElement.set('name', self.name)
856 # procUnitElement.set('name', self.name)
804 # procUnitElement.set('datatype', self.datatype)
857 # procUnitElement.set('datatype', self.datatype)
805 # procUnitElement.set('inputId', str(self.inputId))
858 # procUnitElement.set('inputId', str(self.inputId))
806 #
859 #
807 # for opConfObj in self.opConfObjList:
860 # for opConfObj in self.opConfObjList:
808 # opConfObj.makeXml(procUnitElement)
861 # opConfObj.makeXml(procUnitElement)
809
862
810 def readXml(self, upElement):
863 def readXml(self, upElement):
811
864
812 self.id = upElement.get('id')
865 self.id = upElement.get('id')
813 self.name = upElement.get('name')
866 self.name = upElement.get('name')
814 self.datatype = upElement.get('datatype')
867 self.datatype = upElement.get('datatype')
815 self.inputId = upElement.get('inputId')
868 self.inputId = upElement.get('inputId')
816
869
817 if self.ELEMENTNAME == "ReadUnit":
870 if self.ELEMENTNAME == "ReadUnit":
818 self.datatype = self.datatype.replace("Reader", "")
871 self.datatype = self.datatype.replace("Reader", "")
819
872
820 if self.inputId == 'None':
873 if self.inputId == 'None':
821 self.inputId = '0'
874 self.inputId = '0'
822
875
823 self.opConfObjList = []
876 self.opConfObjList = []
824
877
825 opElementList = upElement.iter(OperationConf().getElementName())
878 opElementList = upElement.iter(OperationConf().getElementName())
826
879
827 for opElement in opElementList:
880 for opElement in opElementList:
828 opConfObj = OperationConf()
881 opConfObj = OperationConf()
829 opConfObj.readXml(opElement)
882 opConfObj.readXml(opElement)
830 self.opConfObjList.append(opConfObj)
883 self.opConfObjList.append(opConfObj)
831
884
832 if opConfObj.name == 'run':
885 if opConfObj.name == 'run':
833 self.path = opConfObj.getParameterValue('path')
886 self.path = opConfObj.getParameterValue('path')
834 self.startDate = opConfObj.getParameterValue('startDate')
887 self.startDate = opConfObj.getParameterValue('startDate')
835 self.endDate = opConfObj.getParameterValue('endDate')
888 self.endDate = opConfObj.getParameterValue('endDate')
836 self.startTime = opConfObj.getParameterValue('startTime')
889 self.startTime = opConfObj.getParameterValue('startTime')
837 self.endTime = opConfObj.getParameterValue('endTime')
890 self.endTime = opConfObj.getParameterValue('endTime')
838
891
839 class Project():
892 class Project():
840
893
841 id = None
894 id = None
842 name = None
895 name = None
843 description = None
896 description = None
844 filename = None
897 filename = None
845
898
846 procUnitConfObjDict = None
899 procUnitConfObjDict = None
847
900
848 ELEMENTNAME = 'Project'
901 ELEMENTNAME = 'Project'
849
902
850 plotterQueue = None
903 plotterQueue = None
851
904
852 def __init__(self, plotter_queue=None):
905 def __init__(self, plotter_queue=None):
853
906
854 self.id = None
907 self.id = None
855 self.name = None
908 self.name = None
856 self.description = None
909 self.description = None
857
910
858 self.plotterQueue = plotter_queue
911 self.plotterQueue = plotter_queue
859
912
860 self.procUnitConfObjDict = {}
913 self.procUnitConfObjDict = {}
861
914
862 def __getNewId(self):
915 def __getNewId(self):
863
916
864 idList = self.procUnitConfObjDict.keys()
917 idList = self.procUnitConfObjDict.keys()
865
918
866 id = int(self.id)*10
919 id = int(self.id)*10
867
920
868 while True:
921 while True:
869 id += 1
922 id += 1
870
923
871 if str(id) in idList:
924 if str(id) in idList:
872 continue
925 continue
873
926
874 break
927 break
875
928
876 return str(id)
929 return str(id)
877
930
878 def getElementName(self):
931 def getElementName(self):
879
932
880 return self.ELEMENTNAME
933 return self.ELEMENTNAME
881
934
882 def getId(self):
935 def getId(self):
883
936
884 return self.id
937 return self.id
885
938
886 def updateId(self, new_id):
939 def updateId(self, new_id):
887
940
888 self.id = str(new_id)
941 self.id = str(new_id)
889
942
890 keyList = self.procUnitConfObjDict.keys()
943 keyList = self.procUnitConfObjDict.keys()
891 keyList.sort()
944 keyList.sort()
892
945
893 n = 1
946 n = 1
894 newProcUnitConfObjDict = {}
947 newProcUnitConfObjDict = {}
895
948
896 for procKey in keyList:
949 for procKey in keyList:
897
950
898 procUnitConfObj = self.procUnitConfObjDict[procKey]
951 procUnitConfObj = self.procUnitConfObjDict[procKey]
899 idProcUnit = str(int(self.id)*10 + n)
952 idProcUnit = str(int(self.id)*10 + n)
900 procUnitConfObj.updateId(idProcUnit, parentId = self.id)
953 procUnitConfObj.updateId(idProcUnit, parentId = self.id)
901
954
902 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
955 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
903 n += 1
956 n += 1
904
957
905 self.procUnitConfObjDict = newProcUnitConfObjDict
958 self.procUnitConfObjDict = newProcUnitConfObjDict
906
959
907 def setup(self, id, name, description):
960 def setup(self, id, name, description):
908
961
909 self.id = str(id)
962 self.id = str(id)
910 self.name = name
963 self.name = name
911 self.description = description
964 self.description = description
912
965
913 def update(self, name, description):
966 def update(self, name, description):
914
967
915 self.name = name
968 self.name = name
916 self.description = description
969 self.description = description
917
970
918 def addReadUnit(self, id=None, datatype=None, name=None, **kwargs):
971 def addReadUnit(self, id=None, datatype=None, name=None, **kwargs):
919
972
920 if id is None:
973 if id is None:
921 idReadUnit = self.__getNewId()
974 idReadUnit = self.__getNewId()
922 else:
975 else:
923 idReadUnit = str(id)
976 idReadUnit = str(id)
924
977
925 readUnitConfObj = ReadUnitConf()
978 readUnitConfObj = ReadUnitConf()
926 readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs)
979 readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs)
927
980
928 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
981 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
929
982
930 return readUnitConfObj
983 return readUnitConfObj
931
984
932 def addProcUnit(self, inputId='0', datatype=None, name=None):
985 def addProcUnit(self, inputId='0', datatype=None, name=None):
933
986
934 idProcUnit = self.__getNewId()
987 idProcUnit = self.__getNewId()
935
988
936 procUnitConfObj = ProcUnitConf()
989 procUnitConfObj = ProcUnitConf()
937 procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id)
990 procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id)
938
991
939 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
992 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
940
993
941 return procUnitConfObj
994 return procUnitConfObj
942
995
943 def removeProcUnit(self, id):
996 def removeProcUnit(self, id):
944
997
945 if id in self.procUnitConfObjDict.keys():
998 if id in self.procUnitConfObjDict.keys():
946 self.procUnitConfObjDict.pop(id)
999 self.procUnitConfObjDict.pop(id)
947
1000
948 def getReadUnitId(self):
1001 def getReadUnitId(self):
949
1002
950 readUnitConfObj = self.getReadUnitObj()
1003 readUnitConfObj = self.getReadUnitObj()
951
1004
952 return readUnitConfObj.id
1005 return readUnitConfObj.id
953
1006
954 def getReadUnitObj(self):
1007 def getReadUnitObj(self):
955
1008
956 for obj in self.procUnitConfObjDict.values():
1009 for obj in self.procUnitConfObjDict.values():
957 if obj.getElementName() == "ReadUnit":
1010 if obj.getElementName() == "ReadUnit":
958 return obj
1011 return obj
959
1012
960 return None
1013 return None
961
1014
962 def getProcUnitObj(self, id=None, name=None):
1015 def getProcUnitObj(self, id=None, name=None):
963
1016
964 if id != None:
1017 if id != None:
965 return self.procUnitConfObjDict[id]
1018 return self.procUnitConfObjDict[id]
966
1019
967 if name != None:
1020 if name != None:
968 return self.getProcUnitObjByName(name)
1021 return self.getProcUnitObjByName(name)
969
1022
970 return None
1023 return None
971
1024
972 def getProcUnitObjByName(self, name):
1025 def getProcUnitObjByName(self, name):
973
1026
974 for obj in self.procUnitConfObjDict.values():
1027 for obj in self.procUnitConfObjDict.values():
975 if obj.name == name:
1028 if obj.name == name:
976 return obj
1029 return obj
977
1030
978 return None
1031 return None
979
1032
980 def procUnitItems(self):
1033 def procUnitItems(self):
981
1034
982 return self.procUnitConfObjDict.items()
1035 return self.procUnitConfObjDict.items()
983
1036
984 def makeXml(self):
1037 def makeXml(self):
985
1038
986 projectElement = Element('Project')
1039 projectElement = Element('Project')
987 projectElement.set('id', str(self.id))
1040 projectElement.set('id', str(self.id))
988 projectElement.set('name', self.name)
1041 projectElement.set('name', self.name)
989 projectElement.set('description', self.description)
1042 projectElement.set('description', self.description)
990
1043
991 for procUnitConfObj in self.procUnitConfObjDict.values():
1044 for procUnitConfObj in self.procUnitConfObjDict.values():
992 procUnitConfObj.makeXml(projectElement)
1045 procUnitConfObj.makeXml(projectElement)
993
1046
994 self.projectElement = projectElement
1047 self.projectElement = projectElement
995
1048
996 def writeXml(self, filename=None):
1049 def writeXml(self, filename=None):
997
1050
998 if filename == None:
1051 if filename == None:
999 if self.filename:
1052 if self.filename:
1000 filename = self.filename
1053 filename = self.filename
1001 else:
1054 else:
1002 filename = "schain.xml"
1055 filename = "schain.xml"
1003
1056
1004 if not filename:
1057 if not filename:
1005 print "filename has not been defined. Use setFilename(filename) for do it."
1058 print "filename has not been defined. Use setFilename(filename) for do it."
1006 return 0
1059 return 0
1007
1060
1008 abs_file = os.path.abspath(filename)
1061 abs_file = os.path.abspath(filename)
1009
1062
1010 if not os.access(os.path.dirname(abs_file), os.W_OK):
1063 if not os.access(os.path.dirname(abs_file), os.W_OK):
1011 print "No write permission on %s" %os.path.dirname(abs_file)
1064 print "No write permission on %s" %os.path.dirname(abs_file)
1012 return 0
1065 return 0
1013
1066
1014 if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)):
1067 if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)):
1015 print "File %s already exists and it could not be overwriten" %abs_file
1068 print "File %s already exists and it could not be overwriten" %abs_file
1016 return 0
1069 return 0
1017
1070
1018 self.makeXml()
1071 self.makeXml()
1019
1072
1020 ElementTree(self.projectElement).write(abs_file, method='xml')
1073 ElementTree(self.projectElement).write(abs_file, method='xml')
1021
1074
1022 self.filename = abs_file
1075 self.filename = abs_file
1023
1076
1024 return 1
1077 return 1
1025
1078
1026 def readXml(self, filename = None):
1079 def readXml(self, filename = None):
1027
1080
1028 if not filename:
1081 if not filename:
1029 print "filename is not defined"
1082 print "filename is not defined"
1030 return 0
1083 return 0
1031
1084
1032 abs_file = os.path.abspath(filename)
1085 abs_file = os.path.abspath(filename)
1033
1086
1034 if not os.path.isfile(abs_file):
1087 if not os.path.isfile(abs_file):
1035 print "%s file does not exist" %abs_file
1088 print "%s file does not exist" %abs_file
1036 return 0
1089 return 0
1037
1090
1038 self.projectElement = None
1091 self.projectElement = None
1039 self.procUnitConfObjDict = {}
1092 self.procUnitConfObjDict = {}
1040
1093
1041 try:
1094 try:
1042 self.projectElement = ElementTree().parse(abs_file)
1095 self.projectElement = ElementTree().parse(abs_file)
1043 except:
1096 except:
1044 print "Error reading %s, verify file format" %filename
1097 print "Error reading %s, verify file format" %filename
1045 return 0
1098 return 0
1046
1099
1047 self.project = self.projectElement.tag
1100 self.project = self.projectElement.tag
1048
1101
1049 self.id = self.projectElement.get('id')
1102 self.id = self.projectElement.get('id')
1050 self.name = self.projectElement.get('name')
1103 self.name = self.projectElement.get('name')
1051 self.description = self.projectElement.get('description')
1104 self.description = self.projectElement.get('description')
1052
1105
1053 readUnitElementList = self.projectElement.iter(ReadUnitConf().getElementName())
1106 readUnitElementList = self.projectElement.iter(ReadUnitConf().getElementName())
1054
1107
1055 for readUnitElement in readUnitElementList:
1108 for readUnitElement in readUnitElementList:
1056 readUnitConfObj = ReadUnitConf()
1109 readUnitConfObj = ReadUnitConf()
1057 readUnitConfObj.readXml(readUnitElement)
1110 readUnitConfObj.readXml(readUnitElement)
1058
1111
1059 if readUnitConfObj.parentId == None:
1112 if readUnitConfObj.parentId == None:
1060 readUnitConfObj.parentId = self.id
1113 readUnitConfObj.parentId = self.id
1061
1114
1062 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
1115 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
1063
1116
1064 procUnitElementList = self.projectElement.iter(ProcUnitConf().getElementName())
1117 procUnitElementList = self.projectElement.iter(ProcUnitConf().getElementName())
1065
1118
1066 for procUnitElement in procUnitElementList:
1119 for procUnitElement in procUnitElementList:
1067 procUnitConfObj = ProcUnitConf()
1120 procUnitConfObj = ProcUnitConf()
1068 procUnitConfObj.readXml(procUnitElement)
1121 procUnitConfObj.readXml(procUnitElement)
1069
1122
1070 if procUnitConfObj.parentId == None:
1123 if procUnitConfObj.parentId == None:
1071 procUnitConfObj.parentId = self.id
1124 procUnitConfObj.parentId = self.id
1072
1125
1073 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1126 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1074
1127
1075 self.filename = abs_file
1128 self.filename = abs_file
1076
1129
1077 return 1
1130 return 1
1078
1131
1079 def printattr(self):
1132 def printattr(self):
1080
1133
1081 print "Project[%s]: name = %s, description = %s" %(self.id,
1134 print "Project[%s]: name = %s, description = %s" %(self.id,
1082 self.name,
1135 self.name,
1083 self.description)
1136 self.description)
1084
1137
1085 for procUnitConfObj in self.procUnitConfObjDict.values():
1138 for procUnitConfObj in self.procUnitConfObjDict.values():
1086 procUnitConfObj.printattr()
1139 procUnitConfObj.printattr()
1087
1140
1088 def createObjects(self):
1141 def createObjects(self):
1089
1142
1090 for procUnitConfObj in self.procUnitConfObjDict.values():
1143 for procUnitConfObj in self.procUnitConfObjDict.values():
1091 procUnitConfObj.createObjects(self.plotterQueue)
1144 procUnitConfObj.createObjects(self.plotterQueue)
1092
1145
1093 def __connect(self, objIN, thisObj):
1146 def __connect(self, objIN, thisObj):
1094
1147
1095 thisObj.setInput(objIN.getOutputObj())
1148 thisObj.setInput(objIN.getOutputObj())
1096
1149
1097 def connectObjects(self):
1150 def connectObjects(self):
1098
1151
1099 for thisPUConfObj in self.procUnitConfObjDict.values():
1152 for thisPUConfObj in self.procUnitConfObjDict.values():
1100
1153
1101 inputId = thisPUConfObj.getInputId()
1154 inputId = thisPUConfObj.getInputId()
1102
1155
1103 if int(inputId) == 0:
1156 if int(inputId) == 0:
1104 continue
1157 continue
1105
1158
1106 #Get input object
1159 #Get input object
1107 puConfINObj = self.procUnitConfObjDict[inputId]
1160 puConfINObj = self.procUnitConfObjDict[inputId]
1108 puObjIN = puConfINObj.getProcUnitObj()
1161 puObjIN = puConfINObj.getProcUnitObj()
1109
1162
1110 #Get current object
1163 #Get current object
1111 thisPUObj = thisPUConfObj.getProcUnitObj()
1164 thisPUObj = thisPUConfObj.getProcUnitObj()
1112
1165
1113 self.__connect(puObjIN, thisPUObj)
1166 self.__connect(puObjIN, thisPUObj)
1114
1167
1115 def __handleError(self, procUnitConfObj, send_email=True):
1168 def __handleError(self, procUnitConfObj, send_email=True):
1116
1169
1117 import socket
1170 import socket
1118
1171
1119 err = traceback.format_exception(sys.exc_info()[0],
1172 err = traceback.format_exception(sys.exc_info()[0],
1120 sys.exc_info()[1],
1173 sys.exc_info()[1],
1121 sys.exc_info()[2])
1174 sys.exc_info()[2])
1122
1175
1123 print "***** Error occurred in %s *****" %(procUnitConfObj.name)
1176 print "***** Error occurred in %s *****" %(procUnitConfObj.name)
1124 print "***** %s" %err[-1]
1177 print "***** %s" %err[-1]
1125
1178
1126 message = "".join(err)
1179 message = "".join(err)
1127
1180
1128 sys.stderr.write(message)
1181 sys.stderr.write(message)
1129
1182
1130 if not send_email:
1183 if not send_email:
1131 return
1184 return
1132
1185
1133 subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name)
1186 subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name)
1134
1187
1135 subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name)
1188 subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name)
1136 subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname())
1189 subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname())
1137 subtitle += "Working directory: %s\n" %os.path.abspath("./")
1190 subtitle += "Working directory: %s\n" %os.path.abspath("./")
1138 subtitle += "Configuration file: %s\n" %self.filename
1191 subtitle += "Configuration file: %s\n" %self.filename
1139 subtitle += "Time: %s\n" %str(datetime.datetime.now())
1192 subtitle += "Time: %s\n" %str(datetime.datetime.now())
1140
1193
1141 readUnitConfObj = self.getReadUnitObj()
1194 readUnitConfObj = self.getReadUnitObj()
1142 if readUnitConfObj:
1195 if readUnitConfObj:
1143 subtitle += "\nInput parameters:\n"
1196 subtitle += "\nInput parameters:\n"
1144 subtitle += "[Data path = %s]\n" %readUnitConfObj.path
1197 subtitle += "[Data path = %s]\n" %readUnitConfObj.path
1145 subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype
1198 subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype
1146 subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate
1199 subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate
1147 subtitle += "[End date = %s]\n" %readUnitConfObj.endDate
1200 subtitle += "[End date = %s]\n" %readUnitConfObj.endDate
1148 subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime
1201 subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime
1149 subtitle += "[End time = %s]\n" %readUnitConfObj.endTime
1202 subtitle += "[End time = %s]\n" %readUnitConfObj.endTime
1150
1203
1151 adminObj = schainpy.admin.SchainNotify()
1204 adminObj = schainpy.admin.SchainNotify()
1152 adminObj.sendAlert(message=message,
1205 adminObj.sendAlert(message=message,
1153 subject=subject,
1206 subject=subject,
1154 subtitle=subtitle,
1207 subtitle=subtitle,
1155 filename=self.filename)
1208 filename=self.filename)
1156
1209
1157 def isPaused(self):
1210 def isPaused(self):
1158 return 0
1211 return 0
1159
1212
1160 def isStopped(self):
1213 def isStopped(self):
1161 return 0
1214 return 0
1162
1215
1163 def runController(self):
1216 def runController(self):
1164 """
1217 """
1165 returns 0 when this process has been stopped, 1 otherwise
1218 returns 0 when this process has been stopped, 1 otherwise
1166 """
1219 """
1167
1220
1168 if self.isPaused():
1221 if self.isPaused():
1169 print "Process suspended"
1222 print "Process suspended"
1170
1223
1171 while True:
1224 while True:
1172 sleep(0.1)
1225 sleep(0.1)
1173
1226
1174 if not self.isPaused():
1227 if not self.isPaused():
1175 break
1228 break
1176
1229
1177 if self.isStopped():
1230 if self.isStopped():
1178 break
1231 break
1179
1232
1180 print "Process reinitialized"
1233 print "Process reinitialized"
1181
1234
1182 if self.isStopped():
1235 if self.isStopped():
1183 print "Process stopped"
1236 print "Process stopped"
1184 return 0
1237 return 0
1185
1238
1186 return 1
1239 return 1
1187
1240
1188 def setFilename(self, filename):
1241 def setFilename(self, filename):
1189
1242
1190 self.filename = filename
1243 self.filename = filename
1191
1244
1192 def setPlotterQueue(self, plotter_queue):
1245 def setPlotterQueue(self, plotter_queue):
1193
1246
1194 raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class"
1247 raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class"
1195
1248
1196 def getPlotterQueue(self):
1249 def getPlotterQueue(self):
1197
1250
1198 raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class"
1251 raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class"
1199
1252
1200 def useExternalPlotter(self):
1253 def useExternalPlotter(self):
1201
1254
1202 raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class"
1255 raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class"
1203
1256
1204 def run(self):
1257 def run(self):
1205
1258
1206 print
1259 print
1207 print "*"*60
1260 print "*"*60
1208 print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__
1261 print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__
1209 print "*"*60
1262 print "*"*60
1210 print
1263 print
1211
1264
1212 keyList = self.procUnitConfObjDict.keys()
1265 keyList = self.procUnitConfObjDict.keys()
1213 keyList.sort()
1266 keyList.sort()
1214
1267
1215 while(True):
1268 while(True):
1216
1269
1217 is_ok = False
1270 is_ok = False
1218
1271
1219 for procKey in keyList:
1272 for procKey in keyList:
1220 # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
1273 # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
1221
1274
1222 procUnitConfObj = self.procUnitConfObjDict[procKey]
1275 procUnitConfObj = self.procUnitConfObjDict[procKey]
1223
1276
1224 try:
1277 try:
1225 sts = procUnitConfObj.run()
1278 sts = procUnitConfObj.run()
1226 is_ok = is_ok or sts
1279 is_ok = is_ok or sts
1227 except KeyboardInterrupt:
1280 except KeyboardInterrupt:
1228 is_ok = False
1281 is_ok = False
1229 break
1282 break
1230 except ValueError, e:
1283 except ValueError, e:
1231 sleep(0.5)
1284 sleep(0.5)
1232 self.__handleError(procUnitConfObj, send_email=True)
1285 self.__handleError(procUnitConfObj, send_email=True)
1233 is_ok = False
1286 is_ok = False
1234 break
1287 break
1235 except:
1288 except:
1236 sleep(0.5)
1289 sleep(0.5)
1237 self.__handleError(procUnitConfObj)
1290 self.__handleError(procUnitConfObj)
1238 is_ok = False
1291 is_ok = False
1239 break
1292 break
1240
1293
1241 #If every process unit finished so end process
1294 #If every process unit finished so end process
1242 if not(is_ok):
1295 if not(is_ok):
1243 # print "Every process unit have finished"
1296 # print "Every process unit have finished"
1244 break
1297 break
1245
1298
1246 if not self.runController():
1299 if not self.runController():
1247 break
1300 break
1248
1301
1249 #Closing every process
1302 #Closing every process
1250 for procKey in keyList:
1303 for procKey in keyList:
1251 procUnitConfObj = self.procUnitConfObjDict[procKey]
1304 procUnitConfObj = self.procUnitConfObjDict[procKey]
1252 procUnitConfObj.close()
1305 procUnitConfObj.close()
1253
1306
1254 print "Process finished"
1307 print "Process finished"
1255
1308
1256 def start(self):
1309 def start(self):
1257
1310
1258 self.writeXml()
1311 self.writeXml()
1259 self.createObjects()
1312 self.createObjects()
1260 self.connectObjects()
1313 self.connectObjects()
1261 self.run()
1314 self.run()
@@ -1,376 +1,377
1
1
2 import os
2 import os
3 import zmq
3 import zmq
4 import time
4 import time
5 import numpy
5 import numpy
6 import datetime
6 import datetime
7 import numpy as np
7 import numpy as np
8 import matplotlib.pyplot as plt
8 import matplotlib.pyplot as plt
9 from mpl_toolkits.axes_grid1 import make_axes_locatable
9 from mpl_toolkits.axes_grid1 import make_axes_locatable
10 from matplotlib.ticker import FuncFormatter, LinearLocator
10 from matplotlib.ticker import FuncFormatter, LinearLocator
11 from multiprocessing import Process
11 from multiprocessing import Process
12
12
13 from schainpy.model.proc.jroproc_base import Operation
13 from schainpy.model.proc.jroproc_base import Operation
14
14
15 #plt.ion()
15 #plt.ion()
16
16
17 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime('%H:%M'))
17 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime('%H:%M'))
18
18
19 d1970 = datetime.datetime(1970,1,1)
19 d1970 = datetime.datetime(1970,1,1)
20
20
21 class PlotData(Operation, Process):
21 class PlotData(Operation, Process):
22
22
23 CODE = 'Figure'
23 CODE = 'Figure'
24 colormap = 'jet'
24 colormap = 'jet'
25 __MAXNUMX = 80
25 __MAXNUMX = 80
26 __MAXNUMY = 80
26 __MAXNUMY = 80
27 __missing = 1E30
27 __missing = 1E30
28
28
29 def __init__(self, **kwargs):
29 def __init__(self, **kwargs):
30
30
31 Operation.__init__(self)
31 Operation.__init__(self)
32 Process.__init__(self)
32 Process.__init__(self)
33 self.mp = False
33 self.mp = False
34 self.dataOut = None
34 self.dataOut = None
35 self.isConfig = False
35 self.isConfig = False
36 self.figure = None
36 self.figure = None
37 self.axes = []
37 self.axes = []
38 self.localtime = kwargs.pop('localtime', True)
38 self.localtime = kwargs.pop('localtime', True)
39 self.show = kwargs.get('show', True)
39 self.show = kwargs.get('show', True)
40 self.save = kwargs.get('save', False)
40 self.save = kwargs.get('save', False)
41 self.colormap = kwargs.get('colormap', self.colormap)
41 self.colormap = kwargs.get('colormap', self.colormap)
42 self.showprofile = kwargs.get('showprofile', False)
42 self.showprofile = kwargs.get('showprofile', False)
43 self.title = kwargs.get('wintitle', '')
43 self.title = kwargs.get('wintitle', '')
44 self.xaxis = kwargs.get('xaxis', 'time')
44 self.xaxis = kwargs.get('xaxis', 'time')
45 self.zmin = kwargs.get('zmin', None)
45 self.zmin = kwargs.get('zmin', None)
46 self.zmax = kwargs.get('zmax', None)
46 self.zmax = kwargs.get('zmax', None)
47 self.xmin = kwargs.get('xmin', None)
47 self.xmin = kwargs.get('xmin', None)
48 self.xmax = kwargs.get('xmax', None)
48 self.xmax = kwargs.get('xmax', None)
49 self.xrange = kwargs.get('xrange', 24)
49 self.xrange = kwargs.get('xrange', 24)
50 self.ymin = kwargs.get('ymin', None)
50 self.ymin = kwargs.get('ymin', None)
51 self.ymax = kwargs.get('ymax', None)
51 self.ymax = kwargs.get('ymax', None)
52
52
53 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
53 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
54
54
55 if x_buffer.shape[0] < 2:
55 if x_buffer.shape[0] < 2:
56 return x_buffer, y_buffer, z_buffer
56 return x_buffer, y_buffer, z_buffer
57
57
58 deltas = x_buffer[1:] - x_buffer[0:-1]
58 deltas = x_buffer[1:] - x_buffer[0:-1]
59 x_median = np.median(deltas)
59 x_median = np.median(deltas)
60
60
61 index = np.where(deltas > 5*x_median)
61 index = np.where(deltas > 5*x_median)
62
62
63 if len(index[0]) != 0:
63 if len(index[0]) != 0:
64 z_buffer[::,index[0],::] = self.__missing
64 z_buffer[::,index[0],::] = self.__missing
65 z_buffer = np.ma.masked_inside(z_buffer,
65 z_buffer = np.ma.masked_inside(z_buffer,
66 0.99*self.__missing,
66 0.99*self.__missing,
67 1.01*self.__missing)
67 1.01*self.__missing)
68
68
69 return x_buffer, y_buffer, z_buffer
69 return x_buffer, y_buffer, z_buffer
70
70
71 def decimate(self):
71 def decimate(self):
72
72
73 dx = int(len(self.x)/self.__MAXNUMX) + 1
73 dx = int(len(self.x)/self.__MAXNUMX) + 1
74 dy = int(len(self.y)/self.__MAXNUMY) + 1
74 dy = int(len(self.y)/self.__MAXNUMY) + 1
75
75
76 x = self.x[::dx]
76 x = self.x[::dx]
77 y = self.y[::dy]
77 y = self.y[::dy]
78 z = self.z[::, ::dx, ::dy]
78 z = self.z[::, ::dx, ::dy]
79
79
80 return x, y, z
80 return x, y, z
81
81
82 def __plot(self):
82 def __plot(self):
83
83
84 print 'plotting...{}'.format(self.CODE)
84 print 'plotting...{}'.format(self.CODE)
85
85
86 self.plot()
86 self.plot()
87 self.figure.suptitle('{} {}'.format(self.title, self.CODE.upper()))
87 self.figure.suptitle('{} {} - Date:{}'.format(self.title, self.CODE.upper(),
88 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')))
88
89
89 if self.save:
90 if self.save:
90 figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,
91 figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,
91 datetime.datetime.utcfromtimestamp(self.times[-1]).strftime('%y%m%d_%H%M%S')))
92 datetime.datetime.utcfromtimestamp(self.times[-1]).strftime('%y%m%d_%H%M%S')))
92 print 'Saving figure: {}'.format(figname)
93 print 'Saving figure: {}'.format(figname)
93 self.figure.savefig(figname)
94 self.figure.savefig(figname)
94
95
95 self.figure.canvas.draw()
96 self.figure.canvas.draw()
96
97
97 def plot(self):
98 def plot(self):
98
99
99 print 'plotting...{}'.format(self.CODE.upper())
100 print 'plotting...{}'.format(self.CODE.upper())
100 return
101 return
101
102
102 def run(self):
103 def run(self):
103
104
104 print '[Starting] {}'.format(self.name)
105 print '[Starting] {}'.format(self.name)
105 context = zmq.Context()
106 context = zmq.Context()
106 receiver = context.socket(zmq.SUB)
107 receiver = context.socket(zmq.SUB)
107 receiver.setsockopt(zmq.SUBSCRIBE, '')
108 receiver.setsockopt(zmq.SUBSCRIBE, '')
108 receiver.setsockopt(zmq.CONFLATE, True)
109 receiver.setsockopt(zmq.CONFLATE, True)
109 receiver.connect("ipc:///tmp/zmq.plots")
110 receiver.connect("ipc:///tmp/zmq.plots")
110
111
111 while True:
112 while True:
112 try:
113 try:
113 #if True:
114 #if True:
114 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
115 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
115 self.dataOut = self.data['dataOut']
116 self.dataOut = self.data['dataOut']
116 self.times = self.data['times']
117 self.times = self.data['times']
117 self.times.sort()
118 self.times.sort()
118 self.min_time = self.times[0]
119 self.min_time = self.times[0]
119 self.max_time = self.times[-1]
120 self.max_time = self.times[-1]
120
121
121 if self.isConfig is False:
122 if self.isConfig is False:
122 self.setup()
123 self.setup()
123 self.isConfig = True
124 self.isConfig = True
124
125
125 self.__plot()
126 self.__plot()
126
127
127 if 'ENDED' in self.data:
128 if 'ENDED' in self.data:
128 #self.setup()
129 #self.setup()
129 #self.__plot()
130 #self.__plot()
130 pass
131 pass
131
132
132 except zmq.Again as e:
133 except zmq.Again as e:
133 print 'Waiting for data...'
134 print 'Waiting for data...'
134 plt.pause(5)
135 plt.pause(5)
135 #time.sleep(3)
136 #time.sleep(3)
136
137
137 def close(self):
138 def close(self):
138 if self.dataOut:
139 if self.dataOut:
139 self._plot()
140 self._plot()
140
141
141
142
142 class PlotSpectraData(PlotData):
143 class PlotSpectraData(PlotData):
143
144
144 CODE = 'spc'
145 CODE = 'spc'
145 colormap = 'jro'
146 colormap = 'jro'
146
147
147 def setup(self):
148 def setup(self):
148
149
149 ncolspan = 1
150 ncolspan = 1
150 colspan = 1
151 colspan = 1
151 self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9)
152 self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9)
152 self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9)
153 self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9)
153 self.width = 3.6*self.ncols
154 self.width = 3.6*self.ncols
154 self.height = 3.2*self.nrows
155 self.height = 3.2*self.nrows
155 if self.showprofile:
156 if self.showprofile:
156 ncolspan = 3
157 ncolspan = 3
157 colspan = 2
158 colspan = 2
158 self.width += 1.2*self.ncols
159 self.width += 1.2*self.ncols
159
160
160 self.ylabel = 'Range [Km]'
161 self.ylabel = 'Range [Km]'
161 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
162 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
162
163
163 if self.figure is None:
164 if self.figure is None:
164 self.figure = plt.figure(figsize=(self.width, self.height),
165 self.figure = plt.figure(figsize=(self.width, self.height),
165 edgecolor='k',
166 edgecolor='k',
166 facecolor='w')
167 facecolor='w')
167 else:
168 else:
168 self.figure.clf()
169 self.figure.clf()
169
170
170 n = 0
171 n = 0
171 for y in range(self.nrows):
172 for y in range(self.nrows):
172 for x in range(self.ncols):
173 for x in range(self.ncols):
173 if n>=self.dataOut.nChannels:
174 if n>=self.dataOut.nChannels:
174 break
175 break
175 ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan)
176 ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan)
176 if self.showprofile:
177 if self.showprofile:
177 ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1)
178 ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1)
178
179
179 ax.firsttime = True
180 ax.firsttime = True
180 self.axes.append(ax)
181 self.axes.append(ax)
181 n += 1
182 n += 1
182
183
183 self.figure.subplots_adjust(wspace=0.9, hspace=0.5)
184 self.figure.subplots_adjust(wspace=0.9, hspace=0.5)
184 self.figure.show()
185 self.figure.show()
185
186
186 def plot(self):
187 def plot(self):
187
188
188 if self.xaxis == "frequency":
189 if self.xaxis == "frequency":
189 x = self.dataOut.getFreqRange(1)/1000.
190 x = self.dataOut.getFreqRange(1)/1000.
190 xlabel = "Frequency (kHz)"
191 xlabel = "Frequency (kHz)"
191 elif self.xaxis == "time":
192 elif self.xaxis == "time":
192 x = self.dataOut.getAcfRange(1)
193 x = self.dataOut.getAcfRange(1)
193 xlabel = "Time (ms)"
194 xlabel = "Time (ms)"
194 else:
195 else:
195 x = self.dataOut.getVelRange(1)
196 x = self.dataOut.getVelRange(1)
196 xlabel = "Velocity (m/s)"
197 xlabel = "Velocity (m/s)"
197
198
198 y = self.dataOut.getHeiRange()
199 y = self.dataOut.getHeiRange()
199 z = self.data[self.CODE]
200 z = self.data[self.CODE]
200
201
201 for n, ax in enumerate(self.axes):
202 for n, ax in enumerate(self.axes):
202
203
203 if ax.firsttime:
204 if ax.firsttime:
204 self.xmax = self.xmax if self.xmax else np.nanmax(x)
205 self.xmax = self.xmax if self.xmax else np.nanmax(x)
205 self.xmin = self.xmin if self.xmin else -self.xmax
206 self.xmin = self.xmin if self.xmin else -self.xmax
206 self.ymin = self.ymin if self.ymin else np.nanmin(y)
207 self.ymin = self.ymin if self.ymin else np.nanmin(y)
207 self.ymax = self.ymax if self.ymax else np.nanmax(y)
208 self.ymax = self.ymax if self.ymax else np.nanmax(y)
208 self.zmin = self.zmin if self.zmin else np.nanmin(z)
209 self.zmin = self.zmin if self.zmin else np.nanmin(z)
209 self.zmax = self.zmax if self.zmax else np.nanmax(z)
210 self.zmax = self.zmax if self.zmax else np.nanmax(z)
210 ax.plot = ax.pcolormesh(x, y, z[n].T,
211 ax.plot = ax.pcolormesh(x, y, z[n].T,
211 vmin=self.zmin,
212 vmin=self.zmin,
212 vmax=self.zmax,
213 vmax=self.zmax,
213 cmap=plt.get_cmap(self.colormap)
214 cmap=plt.get_cmap(self.colormap)
214 )
215 )
215 divider = make_axes_locatable(ax)
216 divider = make_axes_locatable(ax)
216 cax = divider.new_horizontal(size='3%', pad=0.05)
217 cax = divider.new_horizontal(size='3%', pad=0.05)
217 self.figure.add_axes(cax)
218 self.figure.add_axes(cax)
218 plt.colorbar(ax.plot, cax)
219 plt.colorbar(ax.plot, cax)
219
220
220 ax.set_xlim(self.xmin, self.xmax)
221 ax.set_xlim(self.xmin, self.xmax)
221 ax.set_ylim(self.ymin, self.ymax)
222 ax.set_ylim(self.ymin, self.ymax)
222
223
223 ax.xaxis.set_major_locator(LinearLocator(5))
224 ax.xaxis.set_major_locator(LinearLocator(5))
224 #ax.yaxis.set_major_locator(LinearLocator(4))
225 #ax.yaxis.set_major_locator(LinearLocator(4))
225
226
226 ax.set_ylabel(self.ylabel)
227 ax.set_ylabel(self.ylabel)
227 ax.set_xlabel(xlabel)
228 ax.set_xlabel(xlabel)
228
229
229 ax.firsttime = False
230 ax.firsttime = False
230
231
231 if self.showprofile:
232 if self.showprofile:
232 ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
233 ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
233 ax.ax_profile.set_xlim(self.zmin, self.zmax)
234 ax.ax_profile.set_xlim(self.zmin, self.zmax)
234 ax.ax_profile.set_ylim(self.ymin, self.ymax)
235 ax.ax_profile.set_ylim(self.ymin, self.ymax)
235 ax.ax_profile.set_xlabel('dB')
236 ax.ax_profile.set_xlabel('dB')
236 ax.ax_profile.grid(b=True, axis='x')
237 ax.ax_profile.grid(b=True, axis='x')
238 ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
239 color="k", linestyle="dashed", lw=2)[0]
237 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
240 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
238 noise = 10*numpy.log10(self.data['rti'][self.max_time][n]/self.dataOut.normFactor)
239 ax.ax_profile.vlines(noise, self.ymin, self.ymax, colors="k", linestyle="dashed", lw=2)
240 else:
241 else:
241 ax.plot.set_array(z[n].T.ravel())
242 ax.plot.set_array(z[n].T.ravel())
242 ax.set_title('{} {}'.format(self.titles[n],
243 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
244 size=8)
245 if self.showprofile:
243 if self.showprofile:
246 ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y)
244 ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y)
245 ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
247
246
247 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
248 size=8)
248
249
249 class PlotRTIData(PlotData):
250 class PlotRTIData(PlotData):
250
251
251 CODE = 'rti'
252 CODE = 'rti'
252 colormap = 'jro'
253 colormap = 'jro'
253
254
254 def setup(self):
255 def setup(self):
255
256
256 self.ncols = 1
257 self.ncols = 1
257 self.nrows = self.dataOut.nChannels
258 self.nrows = self.dataOut.nChannels
258 self.width = 10
259 self.width = 10
259 self.height = 2.2*self.nrows
260 self.height = 2.2*self.nrows
260 self.ylabel = 'Range [Km]'
261 self.ylabel = 'Range [Km]'
261 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
262 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
262
263
263 if self.figure is None:
264 if self.figure is None:
264 self.figure = plt.figure(figsize=(self.width, self.height),
265 self.figure = plt.figure(figsize=(self.width, self.height),
265 edgecolor='k',
266 edgecolor='k',
266 facecolor='w')
267 facecolor='w')
267 else:
268 else:
268 self.figure.clf()
269 self.figure.clf()
269
270
270 for n in range(self.nrows):
271 for n in range(self.nrows):
271 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
272 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
272 ax.firsttime = True
273 ax.firsttime = True
273 self.axes.append(ax)
274 self.axes.append(ax)
274
275
275 self.figure.subplots_adjust(hspace=0.5)
276 self.figure.subplots_adjust(hspace=0.5)
276 self.figure.show()
277 self.figure.show()
277
278
278 def plot(self):
279 def plot(self):
279
280
280 self.x = np.array(self.times)
281 self.x = np.array(self.times)
281 self.y = self.dataOut.getHeiRange()
282 self.y = self.dataOut.getHeiRange()
282 self.z = []
283 self.z = []
283
284
284 for ch in range(self.nrows):
285 for ch in range(self.nrows):
285 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
286 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
286
287
287 self.z = np.array(self.z)
288 self.z = np.array(self.z)
288
289
289 for n, ax in enumerate(self.axes):
290 for n, ax in enumerate(self.axes):
290
291
291 x, y, z = self.fill_gaps(*self.decimate())
292 x, y, z = self.fill_gaps(*self.decimate())
292
293
293 if ax.firsttime:
294 if ax.firsttime:
294 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
295 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
295 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
296 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
296 self.zmin = self.zmin if self.zmin else np.nanmin(self.z)
297 self.zmin = self.zmin if self.zmin else np.nanmin(self.z)
297 zmax = self.zmax if self.zmax else np.nanmax(self.z)
298 zmax = self.zmax if self.zmax else np.nanmax(self.z)
298 plot = ax.pcolormesh(x, y, z[n].T,
299 plot = ax.pcolormesh(x, y, z[n].T,
299 vmin=self.zmin,
300 vmin=self.zmin,
300 vmax=self.zmax,
301 vmax=self.zmax,
301 cmap=plt.get_cmap(self.colormap)
302 cmap=plt.get_cmap(self.colormap)
302 )
303 )
303 divider = make_axes_locatable(ax)
304 divider = make_axes_locatable(ax)
304 cax = divider.new_horizontal(size='2%', pad=0.05)
305 cax = divider.new_horizontal(size='2%', pad=0.05)
305 self.figure.add_axes(cax)
306 self.figure.add_axes(cax)
306 plt.colorbar(plot, cax)
307 plt.colorbar(plot, cax)
307 ax.set_ylim(self.ymin, self.ymax)
308 ax.set_ylim(self.ymin, self.ymax)
308 if self.xaxis=='time':
309 if self.xaxis=='time':
309 ax.xaxis.set_major_formatter(FuncFormatter(func))
310 ax.xaxis.set_major_formatter(FuncFormatter(func))
310 ax.xaxis.set_major_locator(LinearLocator(6))
311 ax.xaxis.set_major_locator(LinearLocator(6))
311
312
312 ax.yaxis.set_major_locator(LinearLocator(4))
313 ax.yaxis.set_major_locator(LinearLocator(4))
313
314
314 ax.set_ylabel(self.ylabel)
315 ax.set_ylabel(self.ylabel)
315
316
316 if self.xmin is None:
317 if self.xmin is None:
317 print 'is none'
318 print 'is none'
318 xmin = self.min_time
319 xmin = self.min_time
319 else:
320 else:
320
321
321 xmin = (datetime.datetime.combine(self.dataOut.datatime.date(),
322 xmin = (datetime.datetime.combine(self.dataOut.datatime.date(),
322 datetime.time(self.xmin, 0, 0))-d1970).total_seconds()
323 datetime.time(self.xmin, 0, 0))-d1970).total_seconds()
323
324
324 xmax = xmin+self.xrange*60*60
325 xmax = xmin+self.xrange*60*60
325
326
326 ax.set_xlim(xmin, xmax)
327 ax.set_xlim(xmin, xmax)
327 ax.firsttime = False
328 ax.firsttime = False
328 else:
329 else:
329 ax.collections.remove(ax.collections[0])
330 ax.collections.remove(ax.collections[0])
330 plot = ax.pcolormesh(x, y, z[n].T,
331 plot = ax.pcolormesh(x, y, z[n].T,
331 vmin=self.zmin,
332 vmin=self.zmin,
332 vmax=self.zmax,
333 vmax=self.zmax,
333 cmap=plt.get_cmap(self.colormap)
334 cmap=plt.get_cmap(self.colormap)
334 )
335 )
335 ax.set_title('{} {}'.format(self.titles[n],
336 ax.set_title('{} {}'.format(self.titles[n],
336 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
337 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
337 size=8)
338 size=8)
338
339
339
340
340 class PlotCOHData(PlotRTIData):
341 class PlotCOHData(PlotRTIData):
341
342
342 CODE = 'coh'
343 CODE = 'coh'
343
344
344 def setup(self):
345 def setup(self):
345
346
346 self.ncols = 1
347 self.ncols = 1
347 self.nrows = self.dataOut.nPairs
348 self.nrows = self.dataOut.nPairs
348 self.width = 10
349 self.width = 10
349 self.height = 2.2*self.nrows
350 self.height = 2.2*self.nrows
350 self.ylabel = 'Range [Km]'
351 self.ylabel = 'Range [Km]'
351 self.titles = ['Channels {}'.format(x) for x in self.dataOut.pairsList]
352 self.titles = ['Channels {}'.format(x) for x in self.dataOut.pairsList]
352
353
353 if self.figure is None:
354 if self.figure is None:
354 self.figure = plt.figure(figsize=(self.width, self.height),
355 self.figure = plt.figure(figsize=(self.width, self.height),
355 edgecolor='k',
356 edgecolor='k',
356 facecolor='w')
357 facecolor='w')
357 else:
358 else:
358 self.figure.clf()
359 self.figure.clf()
359
360
360 for n in range(self.nrows):
361 for n in range(self.nrows):
361 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
362 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
362 ax.firsttime = True
363 ax.firsttime = True
363 self.axes.append(ax)
364 self.axes.append(ax)
364
365
365 self.figure.subplots_adjust(hspace=0.5)
366 self.figure.subplots_adjust(hspace=0.5)
366 self.figure.show()
367 self.figure.show()
367
368
368 class PlotSNRData(PlotRTIData):
369 class PlotSNRData(PlotRTIData):
369
370
370 CODE = 'coh'
371 CODE = 'coh'
371
372
372
373
373 class PlotPHASEData(PlotCOHData):
374 class PlotPHASEData(PlotCOHData):
374
375
375 CODE = 'phase'
376 CODE = 'phase'
376 colormap = 'seismic'
377 colormap = 'seismic'
This diff has been collapsed as it changes many lines, (966 lines changed) Show them Hide them
@@ -1,1723 +1,1743
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import sys
7 import sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import time, datetime
12 import time, datetime
13 #import h5py
13 #import h5py
14 import traceback
14 import traceback
15
15
16 try:
16 try:
17 from gevent import sleep
17 from gevent import sleep
18 except:
18 except:
19 from time import sleep
19 from time import sleep
20
20
21 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
21 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
22 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
22 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
23
23
24 LOCALTIME = True
24 LOCALTIME = True
25
25
26 def isNumber(cad):
26 def isNumber(cad):
27 """
27 """
28 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
28 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
29
29
30 Excepciones:
30 Excepciones:
31 Si un determinado string no puede ser convertido a numero
31 Si un determinado string no puede ser convertido a numero
32 Input:
32 Input:
33 str, string al cual se le analiza para determinar si convertible a un numero o no
33 str, string al cual se le analiza para determinar si convertible a un numero o no
34
34
35 Return:
35 Return:
36 True : si el string es uno numerico
36 True : si el string es uno numerico
37 False : no es un string numerico
37 False : no es un string numerico
38 """
38 """
39 try:
39 try:
40 float( cad )
40 float( cad )
41 return True
41 return True
42 except:
42 except:
43 return False
43 return False
44
44
45 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
45 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
46 """
46 """
47 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
47 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
48
48
49 Inputs:
49 Inputs:
50 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
50 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
51
51
52 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
52 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
53 segundos contados desde 01/01/1970.
53 segundos contados desde 01/01/1970.
54 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
54 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
55 segundos contados desde 01/01/1970.
55 segundos contados desde 01/01/1970.
56
56
57 Return:
57 Return:
58 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
58 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
59 fecha especificado, de lo contrario retorna False.
59 fecha especificado, de lo contrario retorna False.
60
60
61 Excepciones:
61 Excepciones:
62 Si el archivo no existe o no puede ser abierto
62 Si el archivo no existe o no puede ser abierto
63 Si la cabecera no puede ser leida.
63 Si la cabecera no puede ser leida.
64
64
65 """
65 """
66 basicHeaderObj = BasicHeader(LOCALTIME)
66 basicHeaderObj = BasicHeader(LOCALTIME)
67
67
68 try:
68 try:
69 fp = open(filename,'rb')
69 fp = open(filename,'rb')
70 except IOError:
70 except IOError:
71 print "The file %s can't be opened" %(filename)
71 print "The file %s can't be opened" %(filename)
72 return 0
72 return 0
73
73
74 sts = basicHeaderObj.read(fp)
74 sts = basicHeaderObj.read(fp)
75 fp.close()
75 fp.close()
76
76
77 if not(sts):
77 if not(sts):
78 print "Skipping the file %s because it has not a valid header" %(filename)
78 print "Skipping the file %s because it has not a valid header" %(filename)
79 return 0
79 return 0
80
80
81 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
81 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
82 return 0
82 return 0
83
83
84 return 1
84 return 1
85
85
86 def isTimeInRange(thisTime, startTime, endTime):
86 def isTimeInRange(thisTime, startTime, endTime):
87
87
88 if endTime >= startTime:
88 if endTime >= startTime:
89 if (thisTime < startTime) or (thisTime > endTime):
89 if (thisTime < startTime) or (thisTime > endTime):
90 return 0
90 return 0
91
91
92 return 1
92 return 1
93 else:
93 else:
94 if (thisTime < startTime) and (thisTime > endTime):
94 if (thisTime < startTime) and (thisTime > endTime):
95 return 0
95 return 0
96
96
97 return 1
97 return 1
98
98
99 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
99 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
100 """
100 """
101 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
101 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
102
102
103 Inputs:
103 Inputs:
104 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
104 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
105
105
106 startDate : fecha inicial del rango seleccionado en formato datetime.date
106 startDate : fecha inicial del rango seleccionado en formato datetime.date
107
107
108 endDate : fecha final del rango seleccionado en formato datetime.date
108 endDate : fecha final del rango seleccionado en formato datetime.date
109
109
110 startTime : tiempo inicial del rango seleccionado en formato datetime.time
110 startTime : tiempo inicial del rango seleccionado en formato datetime.time
111
111
112 endTime : tiempo final del rango seleccionado en formato datetime.time
112 endTime : tiempo final del rango seleccionado en formato datetime.time
113
113
114 Return:
114 Return:
115 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
115 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
116 fecha especificado, de lo contrario retorna False.
116 fecha especificado, de lo contrario retorna False.
117
117
118 Excepciones:
118 Excepciones:
119 Si el archivo no existe o no puede ser abierto
119 Si el archivo no existe o no puede ser abierto
120 Si la cabecera no puede ser leida.
120 Si la cabecera no puede ser leida.
121
121
122 """
122 """
123
123
124
124
125 try:
125 try:
126 fp = open(filename,'rb')
126 fp = open(filename,'rb')
127 except IOError:
127 except IOError:
128 print "The file %s can't be opened" %(filename)
128 print "The file %s can't be opened" %(filename)
129 return None
129 return None
130
130
131 firstBasicHeaderObj = BasicHeader(LOCALTIME)
131 firstBasicHeaderObj = BasicHeader(LOCALTIME)
132 systemHeaderObj = SystemHeader()
132 systemHeaderObj = SystemHeader()
133 radarControllerHeaderObj = RadarControllerHeader()
133 radarControllerHeaderObj = RadarControllerHeader()
134 processingHeaderObj = ProcessingHeader()
134 processingHeaderObj = ProcessingHeader()
135
135
136 lastBasicHeaderObj = BasicHeader(LOCALTIME)
136 lastBasicHeaderObj = BasicHeader(LOCALTIME)
137
137
138 sts = firstBasicHeaderObj.read(fp)
138 sts = firstBasicHeaderObj.read(fp)
139
139
140 if not(sts):
140 if not(sts):
141 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
141 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
142 return None
142 return None
143
143
144 if not systemHeaderObj.read(fp):
144 if not systemHeaderObj.read(fp):
145 return None
145 return None
146
146
147 if not radarControllerHeaderObj.read(fp):
147 if not radarControllerHeaderObj.read(fp):
148 return None
148 return None
149
149
150 if not processingHeaderObj.read(fp):
150 if not processingHeaderObj.read(fp):
151 return None
151 return None
152
152
153 filesize = os.path.getsize(filename)
153 filesize = os.path.getsize(filename)
154
154
155 offset = processingHeaderObj.blockSize + 24 #header size
155 offset = processingHeaderObj.blockSize + 24 #header size
156
156
157 if filesize <= offset:
157 if filesize <= offset:
158 print "[Reading] %s: This file has not enough data" %filename
158 print "[Reading] %s: This file has not enough data" %filename
159 return None
159 return None
160
160
161 fp.seek(-offset, 2)
161 fp.seek(-offset, 2)
162
162
163 sts = lastBasicHeaderObj.read(fp)
163 sts = lastBasicHeaderObj.read(fp)
164
164
165 fp.close()
165 fp.close()
166
166
167 thisDatetime = lastBasicHeaderObj.datatime
167 thisDatetime = lastBasicHeaderObj.datatime
168 thisTime_last_block = thisDatetime.time()
168 thisTime_last_block = thisDatetime.time()
169
169
170 thisDatetime = firstBasicHeaderObj.datatime
170 thisDatetime = firstBasicHeaderObj.datatime
171 thisDate = thisDatetime.date()
171 thisDate = thisDatetime.date()
172 thisTime_first_block = thisDatetime.time()
172 thisTime_first_block = thisDatetime.time()
173
173
174 #General case
174 #General case
175 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
175 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
176 #-----------o----------------------------o-----------
176 #-----------o----------------------------o-----------
177 # startTime endTime
177 # startTime endTime
178
178
179 if endTime >= startTime:
179 if endTime >= startTime:
180 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
180 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
181 return None
181 return None
182
182
183 return thisDatetime
183 return thisDatetime
184
184
185 #If endTime < startTime then endTime belongs to the next day
185 #If endTime < startTime then endTime belongs to the next day
186
186
187
187
188 #<<<<<<<<<<<o o>>>>>>>>>>>
188 #<<<<<<<<<<<o o>>>>>>>>>>>
189 #-----------o----------------------------o-----------
189 #-----------o----------------------------o-----------
190 # endTime startTime
190 # endTime startTime
191
191
192 if (thisDate == startDate) and (thisTime_last_block < startTime):
192 if (thisDate == startDate) and (thisTime_last_block < startTime):
193 return None
193 return None
194
194
195 if (thisDate == endDate) and (thisTime_first_block > endTime):
195 if (thisDate == endDate) and (thisTime_first_block > endTime):
196 return None
196 return None
197
197
198 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
198 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
199 return None
199 return None
200
200
201 return thisDatetime
201 return thisDatetime
202
202
203 def isFolderInDateRange(folder, startDate=None, endDate=None):
203 def isFolderInDateRange(folder, startDate=None, endDate=None):
204 """
204 """
205 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
205 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
206
206
207 Inputs:
207 Inputs:
208 folder : nombre completo del directorio.
208 folder : nombre completo del directorio.
209 Su formato deberia ser "/path_root/?YYYYDDD"
209 Su formato deberia ser "/path_root/?YYYYDDD"
210
210
211 siendo:
211 siendo:
212 YYYY : Anio (ejemplo 2015)
212 YYYY : Anio (ejemplo 2015)
213 DDD : Dia del anio (ejemplo 305)
213 DDD : Dia del anio (ejemplo 305)
214
214
215 startDate : fecha inicial del rango seleccionado en formato datetime.date
215 startDate : fecha inicial del rango seleccionado en formato datetime.date
216
216
217 endDate : fecha final del rango seleccionado en formato datetime.date
217 endDate : fecha final del rango seleccionado en formato datetime.date
218
218
219 Return:
219 Return:
220 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
220 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
221 fecha especificado, de lo contrario retorna False.
221 fecha especificado, de lo contrario retorna False.
222 Excepciones:
222 Excepciones:
223 Si el directorio no tiene el formato adecuado
223 Si el directorio no tiene el formato adecuado
224 """
224 """
225
225
226 basename = os.path.basename(folder)
226 basename = os.path.basename(folder)
227
227
228 if not isRadarFolder(basename):
228 if not isRadarFolder(basename):
229 print "The folder %s has not the rigth format" %folder
229 print "The folder %s has not the rigth format" %folder
230 return 0
230 return 0
231
231
232 if startDate and endDate:
232 if startDate and endDate:
233 thisDate = getDateFromRadarFolder(basename)
233 thisDate = getDateFromRadarFolder(basename)
234
234
235 if thisDate < startDate:
235 if thisDate < startDate:
236 return 0
236 return 0
237
237
238 if thisDate > endDate:
238 if thisDate > endDate:
239 return 0
239 return 0
240
240
241 return 1
241 return 1
242
242
243 def isFileInDateRange(filename, startDate=None, endDate=None):
243 def isFileInDateRange(filename, startDate=None, endDate=None):
244 """
244 """
245 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
245 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
246
246
247 Inputs:
247 Inputs:
248 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
248 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
249
249
250 Su formato deberia ser "?YYYYDDDsss"
250 Su formato deberia ser "?YYYYDDDsss"
251
251
252 siendo:
252 siendo:
253 YYYY : Anio (ejemplo 2015)
253 YYYY : Anio (ejemplo 2015)
254 DDD : Dia del anio (ejemplo 305)
254 DDD : Dia del anio (ejemplo 305)
255 sss : set
255 sss : set
256
256
257 startDate : fecha inicial del rango seleccionado en formato datetime.date
257 startDate : fecha inicial del rango seleccionado en formato datetime.date
258
258
259 endDate : fecha final del rango seleccionado en formato datetime.date
259 endDate : fecha final del rango seleccionado en formato datetime.date
260
260
261 Return:
261 Return:
262 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
262 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
263 fecha especificado, de lo contrario retorna False.
263 fecha especificado, de lo contrario retorna False.
264 Excepciones:
264 Excepciones:
265 Si el archivo no tiene el formato adecuado
265 Si el archivo no tiene el formato adecuado
266 """
266 """
267
267
268 basename = os.path.basename(filename)
268 basename = os.path.basename(filename)
269
269
270 if not isRadarFile(basename):
270 if not isRadarFile(basename):
271 print "The filename %s has not the rigth format" %filename
271 print "The filename %s has not the rigth format" %filename
272 return 0
272 return 0
273
273
274 if startDate and endDate:
274 if startDate and endDate:
275 thisDate = getDateFromRadarFile(basename)
275 thisDate = getDateFromRadarFile(basename)
276
276
277 if thisDate < startDate:
277 if thisDate < startDate:
278 return 0
278 return 0
279
279
280 if thisDate > endDate:
280 if thisDate > endDate:
281 return 0
281 return 0
282
282
283 return 1
283 return 1
284
284
285 def getFileFromSet(path, ext, set):
285 def getFileFromSet(path, ext, set):
286 validFilelist = []
286 validFilelist = []
287 fileList = os.listdir(path)
287 fileList = os.listdir(path)
288
288
289 # 0 1234 567 89A BCDE
289 # 0 1234 567 89A BCDE
290 # H YYYY DDD SSS .ext
290 # H YYYY DDD SSS .ext
291
291
292 for thisFile in fileList:
292 for thisFile in fileList:
293 try:
293 try:
294 year = int(thisFile[1:5])
294 year = int(thisFile[1:5])
295 doy = int(thisFile[5:8])
295 doy = int(thisFile[5:8])
296 except:
296 except:
297 continue
297 continue
298
298
299 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
299 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
300 continue
300 continue
301
301
302 validFilelist.append(thisFile)
302 validFilelist.append(thisFile)
303
303
304 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
304 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
305
305
306 if len(myfile)!= 0:
306 if len(myfile)!= 0:
307 return myfile[0]
307 return myfile[0]
308 else:
308 else:
309 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
309 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
310 print 'the filename %s does not exist'%filename
310 print 'the filename %s does not exist'%filename
311 print '...going to the last file: '
311 print '...going to the last file: '
312
312
313 if validFilelist:
313 if validFilelist:
314 validFilelist = sorted( validFilelist, key=str.lower )
314 validFilelist = sorted( validFilelist, key=str.lower )
315 return validFilelist[-1]
315 return validFilelist[-1]
316
316
317 return None
317 return None
318
318
319 def getlastFileFromPath(path, ext):
319 def getlastFileFromPath(path, ext):
320 """
320 """
321 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
321 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
322 al final de la depuracion devuelve el ultimo file de la lista que quedo.
322 al final de la depuracion devuelve el ultimo file de la lista que quedo.
323
323
324 Input:
324 Input:
325 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
325 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
326 ext : extension de los files contenidos en una carpeta
326 ext : extension de los files contenidos en una carpeta
327
327
328 Return:
328 Return:
329 El ultimo file de una determinada carpeta, no se considera el path.
329 El ultimo file de una determinada carpeta, no se considera el path.
330 """
330 """
331 validFilelist = []
331 validFilelist = []
332 fileList = os.listdir(path)
332 fileList = os.listdir(path)
333
333
334 # 0 1234 567 89A BCDE
334 # 0 1234 567 89A BCDE
335 # H YYYY DDD SSS .ext
335 # H YYYY DDD SSS .ext
336
336
337 for thisFile in fileList:
337 for thisFile in fileList:
338
338
339 year = thisFile[1:5]
339 year = thisFile[1:5]
340 if not isNumber(year):
340 if not isNumber(year):
341 continue
341 continue
342
342
343 doy = thisFile[5:8]
343 doy = thisFile[5:8]
344 if not isNumber(doy):
344 if not isNumber(doy):
345 continue
345 continue
346
346
347 year = int(year)
347 year = int(year)
348 doy = int(doy)
348 doy = int(doy)
349
349
350 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
350 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
351 continue
351 continue
352
352
353 validFilelist.append(thisFile)
353 validFilelist.append(thisFile)
354
354
355 if validFilelist:
355 if validFilelist:
356 validFilelist = sorted( validFilelist, key=str.lower )
356 validFilelist = sorted( validFilelist, key=str.lower )
357 return validFilelist[-1]
357 return validFilelist[-1]
358
358
359 return None
359 return None
360
360
361 def checkForRealPath(path, foldercounter, year, doy, set, ext):
361 def checkForRealPath(path, foldercounter, year, doy, set, ext):
362 """
362 """
363 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
363 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
364 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
364 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
365 el path exacto de un determinado file.
365 el path exacto de un determinado file.
366
366
367 Example :
367 Example :
368 nombre correcto del file es .../.../D2009307/P2009307367.ext
368 nombre correcto del file es .../.../D2009307/P2009307367.ext
369
369
370 Entonces la funcion prueba con las siguientes combinaciones
370 Entonces la funcion prueba con las siguientes combinaciones
371 .../.../y2009307367.ext
371 .../.../y2009307367.ext
372 .../.../Y2009307367.ext
372 .../.../Y2009307367.ext
373 .../.../x2009307/y2009307367.ext
373 .../.../x2009307/y2009307367.ext
374 .../.../x2009307/Y2009307367.ext
374 .../.../x2009307/Y2009307367.ext
375 .../.../X2009307/y2009307367.ext
375 .../.../X2009307/y2009307367.ext
376 .../.../X2009307/Y2009307367.ext
376 .../.../X2009307/Y2009307367.ext
377 siendo para este caso, la ultima combinacion de letras, identica al file buscado
377 siendo para este caso, la ultima combinacion de letras, identica al file buscado
378
378
379 Return:
379 Return:
380 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
380 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
381 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
381 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
382 para el filename
382 para el filename
383 """
383 """
384 fullfilename = None
384 fullfilename = None
385 find_flag = False
385 find_flag = False
386 filename = None
386 filename = None
387
387
388 prefixDirList = [None,'d','D']
388 prefixDirList = [None,'d','D']
389 if ext.lower() == ".r": #voltage
389 if ext.lower() == ".r": #voltage
390 prefixFileList = ['d','D']
390 prefixFileList = ['d','D']
391 elif ext.lower() == ".pdata": #spectra
391 elif ext.lower() == ".pdata": #spectra
392 prefixFileList = ['p','P']
392 prefixFileList = ['p','P']
393 else:
393 else:
394 return None, filename
394 return None, filename
395
395
396 #barrido por las combinaciones posibles
396 #barrido por las combinaciones posibles
397 for prefixDir in prefixDirList:
397 for prefixDir in prefixDirList:
398 thispath = path
398 thispath = path
399 if prefixDir != None:
399 if prefixDir != None:
400 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
400 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
401 if foldercounter == 0:
401 if foldercounter == 0:
402 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
402 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
403 else:
403 else:
404 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
404 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
405 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
405 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
406 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
406 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
407 fullfilename = os.path.join( thispath, filename ) #formo el path completo
407 fullfilename = os.path.join( thispath, filename ) #formo el path completo
408
408
409 if os.path.exists( fullfilename ): #verifico que exista
409 if os.path.exists( fullfilename ): #verifico que exista
410 find_flag = True
410 find_flag = True
411 break
411 break
412 if find_flag:
412 if find_flag:
413 break
413 break
414
414
415 if not(find_flag):
415 if not(find_flag):
416 return None, filename
416 return None, filename
417
417
418 return fullfilename, filename
418 return fullfilename, filename
419
419
420 def isRadarFolder(folder):
420 def isRadarFolder(folder):
421 try:
421 try:
422 year = int(folder[1:5])
422 year = int(folder[1:5])
423 doy = int(folder[5:8])
423 doy = int(folder[5:8])
424 except:
424 except:
425 return 0
425 return 0
426
426
427 return 1
427 return 1
428
428
429 def isRadarFile(file):
429 def isRadarFile(file):
430 try:
430 try:
431 year = int(file[1:5])
431 year = int(file[1:5])
432 doy = int(file[5:8])
432 doy = int(file[5:8])
433 set = int(file[8:11])
433 set = int(file[8:11])
434 except:
434 except:
435 return 0
435 return 0
436
436
437 return 1
437 return 1
438
438
439 def getDateFromRadarFile(file):
439 def getDateFromRadarFile(file):
440 try:
440 try:
441 year = int(file[1:5])
441 year = int(file[1:5])
442 doy = int(file[5:8])
442 doy = int(file[5:8])
443 set = int(file[8:11])
443 set = int(file[8:11])
444 except:
444 except:
445 return None
445 return None
446
446
447 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
447 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
448 return thisDate
448 return thisDate
449
449
450 def getDateFromRadarFolder(folder):
450 def getDateFromRadarFolder(folder):
451 try:
451 try:
452 year = int(folder[1:5])
452 year = int(folder[1:5])
453 doy = int(folder[5:8])
453 doy = int(folder[5:8])
454 except:
454 except:
455 return None
455 return None
456
456
457 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
457 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
458 return thisDate
458 return thisDate
459
459
460 class JRODataIO:
460 class JRODataIO:
461
461
462 c = 3E8
462 c = 3E8
463
463
464 isConfig = False
464 isConfig = False
465
465
466 basicHeaderObj = None
466 basicHeaderObj = None
467
467
468 systemHeaderObj = None
468 systemHeaderObj = None
469
469
470 radarControllerHeaderObj = None
470 radarControllerHeaderObj = None
471
471
472 processingHeaderObj = None
472 processingHeaderObj = None
473
473
474 dtype = None
474 dtype = None
475
475
476 pathList = []
476 pathList = []
477
477
478 filenameList = []
478 filenameList = []
479
479
480 filename = None
480 filename = None
481
481
482 ext = None
482 ext = None
483
483
484 flagIsNewFile = 1
484 flagIsNewFile = 1
485
485
486 flagDiscontinuousBlock = 0
486 flagDiscontinuousBlock = 0
487
487
488 flagIsNewBlock = 0
488 flagIsNewBlock = 0
489
489
490 fp = None
490 fp = None
491
491
492 firstHeaderSize = 0
492 firstHeaderSize = 0
493
493
494 basicHeaderSize = 24
494 basicHeaderSize = 24
495
495
496 versionFile = 1103
496 versionFile = 1103
497
497
498 fileSize = None
498 fileSize = None
499
499
500 # ippSeconds = None
500 # ippSeconds = None
501
501
502 fileSizeByHeader = None
502 fileSizeByHeader = None
503
503
504 fileIndex = None
504 fileIndex = None
505
505
506 profileIndex = None
506 profileIndex = None
507
507
508 blockIndex = None
508 blockIndex = None
509
509
510 nTotalBlocks = None
510 nTotalBlocks = None
511
511
512 maxTimeStep = 30
512 maxTimeStep = 30
513
513
514 lastUTTime = None
514 lastUTTime = None
515
515
516 datablock = None
516 datablock = None
517
517
518 dataOut = None
518 dataOut = None
519
519
520 blocksize = None
520 blocksize = None
521
521
522 getByBlock = False
522 getByBlock = False
523
523
524 def __init__(self):
524 def __init__(self):
525
525
526 raise NotImplementedError
526 raise NotImplementedError
527
527
528 def run(self):
528 def run(self):
529
529
530 raise NotImplementedError
530 raise NotImplementedError
531
531
532 def getDtypeWidth(self):
532 def getDtypeWidth(self):
533
533
534 dtype_index = get_dtype_index(self.dtype)
534 dtype_index = get_dtype_index(self.dtype)
535 dtype_width = get_dtype_width(dtype_index)
535 dtype_width = get_dtype_width(dtype_index)
536
536
537 return dtype_width
537 return dtype_width
538
538
539 class JRODataReader(JRODataIO):
539 class JRODataReader(JRODataIO):
540
540
541
541
542 online = 0
542 online = 0
543
543
544 realtime = 0
544 realtime = 0
545
545
546 nReadBlocks = 0
546 nReadBlocks = 0
547
547
548 delay = 10 #number of seconds waiting a new file
548 delay = 10 #number of seconds waiting a new file
549
549
550 nTries = 3 #quantity tries
550 nTries = 3 #quantity tries
551
551
552 nFiles = 3 #number of files for searching
552 nFiles = 3 #number of files for searching
553
553
554 path = None
554 path = None
555
555
556 foldercounter = 0
556 foldercounter = 0
557
557
558 flagNoMoreFiles = 0
558 flagNoMoreFiles = 0
559
559
560 datetimeList = []
560 datetimeList = []
561
561
562 __isFirstTimeOnline = 1
562 __isFirstTimeOnline = 1
563
563
564 __printInfo = True
564 __printInfo = True
565
565
566 profileIndex = None
566 profileIndex = None
567
567
568 nTxs = 1
568 nTxs = 1
569
569
570 txIndex = None
570 txIndex = None
571
571
572 #Added--------------------
572 #Added--------------------
573
573
574 selBlocksize = None
574 selBlocksize = None
575
575
576 selBlocktime = None
576 selBlocktime = None
577
577
578
578
579 def __init__(self):
579 def __init__(self):
580
580
581 """
581 """
582 This class is used to find data files
582 This class is used to find data files
583
583
584 Example:
584 Example:
585 reader = JRODataReader()
585 reader = JRODataReader()
586 fileList = reader.findDataFiles()
586 fileList = reader.findDataFiles()
587
587
588 """
588 """
589 pass
589 pass
590
590
591
591
592 def createObjByDefault(self):
592 def createObjByDefault(self):
593 """
593 """
594
594
595 """
595 """
596 raise NotImplementedError
596 raise NotImplementedError
597
597
598 def getBlockDimension(self):
598 def getBlockDimension(self):
599
599
600 raise NotImplementedError
600 raise NotImplementedError
601
601
602 def __searchFilesOffLine(self,
602 def __searchFilesOffLine(self,
603 path,
603 path,
604 startDate=None,
604 startDate=None,
605 endDate=None,
605 endDate=None,
606 startTime=datetime.time(0,0,0),
606 startTime=datetime.time(0,0,0),
607 endTime=datetime.time(23,59,59),
607 endTime=datetime.time(23,59,59),
608 set=None,
608 set=None,
609 expLabel='',
609 expLabel='',
610 ext='.r',
610 ext='.r',
611 queue=None,
612 cursor=None,
613 skip=None,
611 walk=True):
614 walk=True):
612
615
613 self.filenameList = []
616 self.filenameList = []
614 self.datetimeList = []
617 self.datetimeList = []
615
618
616 pathList = []
619 pathList = []
617
620
618 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
621 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
619
622
620 if dateList == []:
623 if dateList == []:
621 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
624 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
622 return None, None
625 return None, None
623
626
624 if len(dateList) > 1:
627 if len(dateList) > 1:
625 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
628 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
626 else:
629 else:
627 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
630 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
628
631
629 filenameList = []
632 filenameList = []
630 datetimeList = []
633 datetimeList = []
631
634
632 for thisPath in pathList:
635 for thisPath in pathList:
633 # thisPath = pathList[pathDict[file]]
636 # thisPath = pathList[pathDict[file]]
634
637
635 fileList = glob.glob1(thisPath, "*%s" %ext)
638 fileList = glob.glob1(thisPath, "*%s" %ext)
636 fileList.sort()
639 fileList.sort()
637
640
638 for file in fileList:
641 skippedFileList = []
639
642
643 if cursor is not None and skip is not None:
644 # if cursor*skip > len(fileList):
645 if skip == 0:
646 if queue is not None:
647 queue.put(len(fileList))
648 skippedFileList = []
649 else:
650 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
651
652 else:
653 skippedFileList = fileList
654
655 for file in skippedFileList:
656
640 filename = os.path.join(thisPath,file)
657 filename = os.path.join(thisPath,file)
641
658
642 if not isFileInDateRange(filename, startDate, endDate):
659 if not isFileInDateRange(filename, startDate, endDate):
643 continue
660 continue
644
661
645 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
662 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
646
663
647 if not(thisDatetime):
664 if not(thisDatetime):
648 continue
665 continue
649
666
650 filenameList.append(filename)
667 filenameList.append(filename)
651 datetimeList.append(thisDatetime)
668 datetimeList.append(thisDatetime)
652
669
653 if not(filenameList):
670 if not(filenameList):
654 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
671 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
655 return None, None
672 return None, None
656
673
657 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
674 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
658 print
675 print
659
676
660 for i in range(len(filenameList)):
677 for i in range(len(filenameList)):
661 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
678 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
662
679
663 self.filenameList = filenameList
680 self.filenameList = filenameList
664 self.datetimeList = datetimeList
681 self.datetimeList = datetimeList
665
682
666 return pathList, filenameList
683 return pathList, filenameList
667
684
668 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
685 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
669
686
670 """
687 """
671 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
688 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
672 devuelve el archivo encontrado ademas de otros datos.
689 devuelve el archivo encontrado ademas de otros datos.
673
690
674 Input:
691 Input:
675 path : carpeta donde estan contenidos los files que contiene data
692 path : carpeta donde estan contenidos los files que contiene data
676
693
677 expLabel : Nombre del subexperimento (subfolder)
694 expLabel : Nombre del subexperimento (subfolder)
678
695
679 ext : extension de los files
696 ext : extension de los files
680
697
681 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
698 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
682
699
683 Return:
700 Return:
684 directory : eL directorio donde esta el file encontrado
701 directory : eL directorio donde esta el file encontrado
685 filename : el ultimo file de una determinada carpeta
702 filename : el ultimo file de una determinada carpeta
686 year : el anho
703 year : el anho
687 doy : el numero de dia del anho
704 doy : el numero de dia del anho
688 set : el set del archivo
705 set : el set del archivo
689
706
690
707
691 """
708 """
692 if not os.path.isdir(path):
709 if not os.path.isdir(path):
693 return None, None, None, None, None, None
710 return None, None, None, None, None, None
694
711
695 dirList = []
712 dirList = []
696
713
697 if not walk:
714 if not walk:
698 fullpath = path
715 fullpath = path
699 foldercounter = 0
716 foldercounter = 0
700 else:
717 else:
701 #Filtra solo los directorios
718 #Filtra solo los directorios
702 for thisPath in os.listdir(path):
719 for thisPath in os.listdir(path):
703 if not os.path.isdir(os.path.join(path,thisPath)):
720 if not os.path.isdir(os.path.join(path,thisPath)):
704 continue
721 continue
705 if not isRadarFolder(thisPath):
722 if not isRadarFolder(thisPath):
706 continue
723 continue
707
724
708 dirList.append(thisPath)
725 dirList.append(thisPath)
709
726
710 if not(dirList):
727 if not(dirList):
711 return None, None, None, None, None, None
728 return None, None, None, None, None, None
712
729
713 dirList = sorted( dirList, key=str.lower )
730 dirList = sorted( dirList, key=str.lower )
714
731
715 doypath = dirList[-1]
732 doypath = dirList[-1]
716 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
733 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
717 fullpath = os.path.join(path, doypath, expLabel)
734 fullpath = os.path.join(path, doypath, expLabel)
718
735
719
736
720 print "[Reading] %s folder was found: " %(fullpath )
737 print "[Reading] %s folder was found: " %(fullpath )
721
738
722 if set == None:
739 if set == None:
723 filename = getlastFileFromPath(fullpath, ext)
740 filename = getlastFileFromPath(fullpath, ext)
724 else:
741 else:
725 filename = getFileFromSet(fullpath, ext, set)
742 filename = getFileFromSet(fullpath, ext, set)
726
743
727 if not(filename):
744 if not(filename):
728 return None, None, None, None, None, None
745 return None, None, None, None, None, None
729
746
730 print "[Reading] %s file was found" %(filename)
747 print "[Reading] %s file was found" %(filename)
731
748
732 if not(self.__verifyFile(os.path.join(fullpath, filename))):
749 if not(self.__verifyFile(os.path.join(fullpath, filename))):
733 return None, None, None, None, None, None
750 return None, None, None, None, None, None
734
751
735 year = int( filename[1:5] )
752 year = int( filename[1:5] )
736 doy = int( filename[5:8] )
753 doy = int( filename[5:8] )
737 set = int( filename[8:11] )
754 set = int( filename[8:11] )
738
755
739 return fullpath, foldercounter, filename, year, doy, set
756 return fullpath, foldercounter, filename, year, doy, set
740
757
741 def __setNextFileOffline(self):
758 def __setNextFileOffline(self):
742
759
743 idFile = self.fileIndex
760 idFile = self.fileIndex
744
761
745 while (True):
762 while (True):
746 idFile += 1
763 idFile += 1
747 if not(idFile < len(self.filenameList)):
764 if not(idFile < len(self.filenameList)):
748 self.flagNoMoreFiles = 1
765 self.flagNoMoreFiles = 1
749 # print "[Reading] No more Files"
766 # print "[Reading] No more Files"
750 return 0
767 return 0
751
768
752 filename = self.filenameList[idFile]
769 filename = self.filenameList[idFile]
753
770
754 if not(self.__verifyFile(filename)):
771 if not(self.__verifyFile(filename)):
755 continue
772 continue
756
773
757 fileSize = os.path.getsize(filename)
774 fileSize = os.path.getsize(filename)
758 fp = open(filename,'rb')
775 fp = open(filename,'rb')
759 break
776 break
760
777
761 self.flagIsNewFile = 1
778 self.flagIsNewFile = 1
762 self.fileIndex = idFile
779 self.fileIndex = idFile
763 self.filename = filename
780 self.filename = filename
764 self.fileSize = fileSize
781 self.fileSize = fileSize
765 self.fp = fp
782 self.fp = fp
766
783
767 # print "[Reading] Setting the file: %s"%self.filename
784 # print "[Reading] Setting the file: %s"%self.filename
768
785
769 return 1
786 return 1
770
787
771 def __setNextFileOnline(self):
788 def __setNextFileOnline(self):
772 """
789 """
773 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
790 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
774 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
791 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
775 siguientes.
792 siguientes.
776
793
777 Affected:
794 Affected:
778 self.flagIsNewFile
795 self.flagIsNewFile
779 self.filename
796 self.filename
780 self.fileSize
797 self.fileSize
781 self.fp
798 self.fp
782 self.set
799 self.set
783 self.flagNoMoreFiles
800 self.flagNoMoreFiles
784
801
785 Return:
802 Return:
786 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
803 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
787 1 : si el file fue abierto con exito y esta listo a ser leido
804 1 : si el file fue abierto con exito y esta listo a ser leido
788
805
789 Excepciones:
806 Excepciones:
790 Si un determinado file no puede ser abierto
807 Si un determinado file no puede ser abierto
791 """
808 """
792 nFiles = 0
809 nFiles = 0
793 fileOk_flag = False
810 fileOk_flag = False
794 firstTime_flag = True
811 firstTime_flag = True
795
812
796 self.set += 1
813 self.set += 1
797
814
798 if self.set > 999:
815 if self.set > 999:
799 self.set = 0
816 self.set = 0
800 self.foldercounter += 1
817 self.foldercounter += 1
801
818
802 #busca el 1er file disponible
819 #busca el 1er file disponible
803 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
820 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
804 if fullfilename:
821 if fullfilename:
805 if self.__verifyFile(fullfilename, False):
822 if self.__verifyFile(fullfilename, False):
806 fileOk_flag = True
823 fileOk_flag = True
807
824
808 #si no encuentra un file entonces espera y vuelve a buscar
825 #si no encuentra un file entonces espera y vuelve a buscar
809 if not(fileOk_flag):
826 if not(fileOk_flag):
810 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
827 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
811
828
812 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
829 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
813 tries = self.nTries
830 tries = self.nTries
814 else:
831 else:
815 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
832 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
816
833
817 for nTries in range( tries ):
834 for nTries in range( tries ):
818 if firstTime_flag:
835 if firstTime_flag:
819 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
836 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
820 sleep( self.delay )
837 sleep( self.delay )
821 else:
838 else:
822 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
839 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
823
840
824 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
841 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
825 if fullfilename:
842 if fullfilename:
826 if self.__verifyFile(fullfilename):
843 if self.__verifyFile(fullfilename):
827 fileOk_flag = True
844 fileOk_flag = True
828 break
845 break
829
846
830 if fileOk_flag:
847 if fileOk_flag:
831 break
848 break
832
849
833 firstTime_flag = False
850 firstTime_flag = False
834
851
835 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
852 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
836 self.set += 1
853 self.set += 1
837
854
838 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
855 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
839 self.set = 0
856 self.set = 0
840 self.doy += 1
857 self.doy += 1
841 self.foldercounter = 0
858 self.foldercounter = 0
842
859
843 if fileOk_flag:
860 if fileOk_flag:
844 self.fileSize = os.path.getsize( fullfilename )
861 self.fileSize = os.path.getsize( fullfilename )
845 self.filename = fullfilename
862 self.filename = fullfilename
846 self.flagIsNewFile = 1
863 self.flagIsNewFile = 1
847 if self.fp != None: self.fp.close()
864 if self.fp != None: self.fp.close()
848 self.fp = open(fullfilename, 'rb')
865 self.fp = open(fullfilename, 'rb')
849 self.flagNoMoreFiles = 0
866 self.flagNoMoreFiles = 0
850 # print '[Reading] Setting the file: %s' % fullfilename
867 # print '[Reading] Setting the file: %s' % fullfilename
851 else:
868 else:
852 self.fileSize = 0
869 self.fileSize = 0
853 self.filename = None
870 self.filename = None
854 self.flagIsNewFile = 0
871 self.flagIsNewFile = 0
855 self.fp = None
872 self.fp = None
856 self.flagNoMoreFiles = 1
873 self.flagNoMoreFiles = 1
857 # print '[Reading] No more files to read'
874 # print '[Reading] No more files to read'
858
875
859 return fileOk_flag
876 return fileOk_flag
860
877
861 def setNextFile(self):
878 def setNextFile(self):
862 if self.fp != None:
879 if self.fp != None:
863 self.fp.close()
880 self.fp.close()
864
881
865 if self.online:
882 if self.online:
866 newFile = self.__setNextFileOnline()
883 newFile = self.__setNextFileOnline()
867 else:
884 else:
868 newFile = self.__setNextFileOffline()
885 newFile = self.__setNextFileOffline()
869
886
870 if not(newFile):
887 if not(newFile):
871 print '[Reading] No more files to read'
888 print '[Reading] No more files to read'
872 return 0
889 return 0
873
890
874 print '[Reading] Setting the file: %s' % self.filename
891 print '[Reading] Setting the file: %s' % self.filename
875
892
876 self.__readFirstHeader()
893 self.__readFirstHeader()
877 self.nReadBlocks = 0
894 self.nReadBlocks = 0
878 return 1
895 return 1
879
896
880 def __waitNewBlock(self):
897 def __waitNewBlock(self):
881 """
898 """
882 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
899 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
883
900
884 Si el modo de lectura es OffLine siempre retorn 0
901 Si el modo de lectura es OffLine siempre retorn 0
885 """
902 """
886 if not self.online:
903 if not self.online:
887 return 0
904 return 0
888
905
889 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
906 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
890 return 0
907 return 0
891
908
892 currentPointer = self.fp.tell()
909 currentPointer = self.fp.tell()
893
910
894 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
911 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
895
912
896 for nTries in range( self.nTries ):
913 for nTries in range( self.nTries ):
897
914
898 self.fp.close()
915 self.fp.close()
899 self.fp = open( self.filename, 'rb' )
916 self.fp = open( self.filename, 'rb' )
900 self.fp.seek( currentPointer )
917 self.fp.seek( currentPointer )
901
918
902 self.fileSize = os.path.getsize( self.filename )
919 self.fileSize = os.path.getsize( self.filename )
903 currentSize = self.fileSize - currentPointer
920 currentSize = self.fileSize - currentPointer
904
921
905 if ( currentSize >= neededSize ):
922 if ( currentSize >= neededSize ):
906 self.basicHeaderObj.read(self.fp)
923 self.basicHeaderObj.read(self.fp)
907 return 1
924 return 1
908
925
909 if self.fileSize == self.fileSizeByHeader:
926 if self.fileSize == self.fileSizeByHeader:
910 # self.flagEoF = True
927 # self.flagEoF = True
911 return 0
928 return 0
912
929
913 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
930 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
914 sleep( self.delay )
931 sleep( self.delay )
915
932
916
933
917 return 0
934 return 0
918
935
919 def waitDataBlock(self,pointer_location):
936 def waitDataBlock(self,pointer_location):
920
937
921 currentPointer = pointer_location
938 currentPointer = pointer_location
922
939
923 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
940 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
924
941
925 for nTries in range( self.nTries ):
942 for nTries in range( self.nTries ):
926 self.fp.close()
943 self.fp.close()
927 self.fp = open( self.filename, 'rb' )
944 self.fp = open( self.filename, 'rb' )
928 self.fp.seek( currentPointer )
945 self.fp.seek( currentPointer )
929
946
930 self.fileSize = os.path.getsize( self.filename )
947 self.fileSize = os.path.getsize( self.filename )
931 currentSize = self.fileSize - currentPointer
948 currentSize = self.fileSize - currentPointer
932
949
933 if ( currentSize >= neededSize ):
950 if ( currentSize >= neededSize ):
934 return 1
951 return 1
935
952
936 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
953 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
937 sleep( self.delay )
954 sleep( self.delay )
938
955
939 return 0
956 return 0
940
957
941 def __jumpToLastBlock(self):
958 def __jumpToLastBlock(self):
942
959
943 if not(self.__isFirstTimeOnline):
960 if not(self.__isFirstTimeOnline):
944 return
961 return
945
962
946 csize = self.fileSize - self.fp.tell()
963 csize = self.fileSize - self.fp.tell()
947 blocksize = self.processingHeaderObj.blockSize
964 blocksize = self.processingHeaderObj.blockSize
948
965
949 #salta el primer bloque de datos
966 #salta el primer bloque de datos
950 if csize > self.processingHeaderObj.blockSize:
967 if csize > self.processingHeaderObj.blockSize:
951 self.fp.seek(self.fp.tell() + blocksize)
968 self.fp.seek(self.fp.tell() + blocksize)
952 else:
969 else:
953 return
970 return
954
971
955 csize = self.fileSize - self.fp.tell()
972 csize = self.fileSize - self.fp.tell()
956 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
973 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
957 while True:
974 while True:
958
975
959 if self.fp.tell()<self.fileSize:
976 if self.fp.tell()<self.fileSize:
960 self.fp.seek(self.fp.tell() + neededsize)
977 self.fp.seek(self.fp.tell() + neededsize)
961 else:
978 else:
962 self.fp.seek(self.fp.tell() - neededsize)
979 self.fp.seek(self.fp.tell() - neededsize)
963 break
980 break
964
981
965 # csize = self.fileSize - self.fp.tell()
982 # csize = self.fileSize - self.fp.tell()
966 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
983 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
967 # factor = int(csize/neededsize)
984 # factor = int(csize/neededsize)
968 # if factor > 0:
985 # if factor > 0:
969 # self.fp.seek(self.fp.tell() + factor*neededsize)
986 # self.fp.seek(self.fp.tell() + factor*neededsize)
970
987
971 self.flagIsNewFile = 0
988 self.flagIsNewFile = 0
972 self.__isFirstTimeOnline = 0
989 self.__isFirstTimeOnline = 0
973
990
974 def __setNewBlock(self):
991 def __setNewBlock(self):
975
992
976 if self.fp == None:
993 if self.fp == None:
977 return 0
994 return 0
978
995
979 # if self.online:
996 # if self.online:
980 # self.__jumpToLastBlock()
997 # self.__jumpToLastBlock()
981
998
982 if self.flagIsNewFile:
999 if self.flagIsNewFile:
983 self.lastUTTime = self.basicHeaderObj.utc
1000 self.lastUTTime = self.basicHeaderObj.utc
984 return 1
1001 return 1
985
1002
986 if self.realtime:
1003 if self.realtime:
987 self.flagDiscontinuousBlock = 1
1004 self.flagDiscontinuousBlock = 1
988 if not(self.setNextFile()):
1005 if not(self.setNextFile()):
989 return 0
1006 return 0
990 else:
1007 else:
991 return 1
1008 return 1
992
1009
993 currentSize = self.fileSize - self.fp.tell()
1010 currentSize = self.fileSize - self.fp.tell()
994 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1011 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
995
1012
996 if (currentSize >= neededSize):
1013 if (currentSize >= neededSize):
997 self.basicHeaderObj.read(self.fp)
1014 self.basicHeaderObj.read(self.fp)
998 self.lastUTTime = self.basicHeaderObj.utc
1015 self.lastUTTime = self.basicHeaderObj.utc
999 return 1
1016 return 1
1000
1017
1001 if self.__waitNewBlock():
1018 if self.__waitNewBlock():
1002 self.lastUTTime = self.basicHeaderObj.utc
1019 self.lastUTTime = self.basicHeaderObj.utc
1003 return 1
1020 return 1
1004
1021
1005 if not(self.setNextFile()):
1022 if not(self.setNextFile()):
1006 return 0
1023 return 0
1007
1024
1008 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1025 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1009 self.lastUTTime = self.basicHeaderObj.utc
1026 self.lastUTTime = self.basicHeaderObj.utc
1010
1027
1011 self.flagDiscontinuousBlock = 0
1028 self.flagDiscontinuousBlock = 0
1012
1029
1013 if deltaTime > self.maxTimeStep:
1030 if deltaTime > self.maxTimeStep:
1014 self.flagDiscontinuousBlock = 1
1031 self.flagDiscontinuousBlock = 1
1015
1032
1016 return 1
1033 return 1
1017
1034
1018 def readNextBlock(self):
1035 def readNextBlock(self):
1019
1036
1020 #Skip block out of startTime and endTime
1037 #Skip block out of startTime and endTime
1021 while True:
1038 while True:
1022 if not(self.__setNewBlock()):
1039 if not(self.__setNewBlock()):
1023 return 0
1040 return 0
1024
1041
1025 if not(self.readBlock()):
1042 if not(self.readBlock()):
1026 return 0
1043 return 0
1027
1044
1028 self.getBasicHeader()
1045 self.getBasicHeader()
1029
1046
1030 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1047 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1031
1048
1032 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1049 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1033 self.processingHeaderObj.dataBlocksPerFile,
1050 self.processingHeaderObj.dataBlocksPerFile,
1034 self.dataOut.datatime.ctime())
1051 self.dataOut.datatime.ctime())
1035 continue
1052 continue
1036
1053
1037 break
1054 break
1038
1055
1039 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1056 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1040 self.processingHeaderObj.dataBlocksPerFile,
1057 self.processingHeaderObj.dataBlocksPerFile,
1041 self.dataOut.datatime.ctime())
1058 self.dataOut.datatime.ctime())
1042 return 1
1059 return 1
1043
1060
1044 def __readFirstHeader(self):
1061 def __readFirstHeader(self):
1045
1062
1046 self.basicHeaderObj.read(self.fp)
1063 self.basicHeaderObj.read(self.fp)
1047 self.systemHeaderObj.read(self.fp)
1064 self.systemHeaderObj.read(self.fp)
1048 self.radarControllerHeaderObj.read(self.fp)
1065 self.radarControllerHeaderObj.read(self.fp)
1049 self.processingHeaderObj.read(self.fp)
1066 self.processingHeaderObj.read(self.fp)
1050
1067
1051 self.firstHeaderSize = self.basicHeaderObj.size
1068 self.firstHeaderSize = self.basicHeaderObj.size
1052
1069
1053 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1070 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1054 if datatype == 0:
1071 if datatype == 0:
1055 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1072 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1056 elif datatype == 1:
1073 elif datatype == 1:
1057 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1074 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1058 elif datatype == 2:
1075 elif datatype == 2:
1059 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1076 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1060 elif datatype == 3:
1077 elif datatype == 3:
1061 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1078 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1062 elif datatype == 4:
1079 elif datatype == 4:
1063 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1080 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1064 elif datatype == 5:
1081 elif datatype == 5:
1065 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1082 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1066 else:
1083 else:
1067 raise ValueError, 'Data type was not defined'
1084 raise ValueError, 'Data type was not defined'
1068
1085
1069 self.dtype = datatype_str
1086 self.dtype = datatype_str
1070 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1087 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1071 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1088 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1072 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1089 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1073 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1090 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1074 self.getBlockDimension()
1091 self.getBlockDimension()
1075
1092
1076 def __verifyFile(self, filename, msgFlag=True):
1093 def __verifyFile(self, filename, msgFlag=True):
1077
1094
1078 msg = None
1095 msg = None
1079
1096
1080 try:
1097 try:
1081 fp = open(filename, 'rb')
1098 fp = open(filename, 'rb')
1082 except IOError:
1099 except IOError:
1083
1100
1084 if msgFlag:
1101 if msgFlag:
1085 print "[Reading] File %s can't be opened" % (filename)
1102 print "[Reading] File %s can't be opened" % (filename)
1086
1103
1087 return False
1104 return False
1088
1105
1089 currentPosition = fp.tell()
1106 currentPosition = fp.tell()
1090 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1107 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1091
1108
1092 if neededSize == 0:
1109 if neededSize == 0:
1093 basicHeaderObj = BasicHeader(LOCALTIME)
1110 basicHeaderObj = BasicHeader(LOCALTIME)
1094 systemHeaderObj = SystemHeader()
1111 systemHeaderObj = SystemHeader()
1095 radarControllerHeaderObj = RadarControllerHeader()
1112 radarControllerHeaderObj = RadarControllerHeader()
1096 processingHeaderObj = ProcessingHeader()
1113 processingHeaderObj = ProcessingHeader()
1097
1114
1098 if not( basicHeaderObj.read(fp) ):
1115 if not( basicHeaderObj.read(fp) ):
1099 fp.close()
1116 fp.close()
1100 return False
1117 return False
1101
1118
1102 if not( systemHeaderObj.read(fp) ):
1119 if not( systemHeaderObj.read(fp) ):
1103 fp.close()
1120 fp.close()
1104 return False
1121 return False
1105
1122
1106 if not( radarControllerHeaderObj.read(fp) ):
1123 if not( radarControllerHeaderObj.read(fp) ):
1107 fp.close()
1124 fp.close()
1108 return False
1125 return False
1109
1126
1110 if not( processingHeaderObj.read(fp) ):
1127 if not( processingHeaderObj.read(fp) ):
1111 fp.close()
1128 fp.close()
1112 return False
1129 return False
1113
1130
1114 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1131 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1115 else:
1132 else:
1116 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1133 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1117
1134
1118 fp.close()
1135 fp.close()
1119
1136
1120 fileSize = os.path.getsize(filename)
1137 fileSize = os.path.getsize(filename)
1121 currentSize = fileSize - currentPosition
1138 currentSize = fileSize - currentPosition
1122
1139
1123 if currentSize < neededSize:
1140 if currentSize < neededSize:
1124 if msgFlag and (msg != None):
1141 if msgFlag and (msg != None):
1125 print msg
1142 print msg
1126 return False
1143 return False
1127
1144
1128 return True
1145 return True
1129
1146
1130 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1147 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1131
1148
1132 path_empty = True
1149 path_empty = True
1133
1150
1134 dateList = []
1151 dateList = []
1135 pathList = []
1152 pathList = []
1136
1153
1137 multi_path = path.split(',')
1154 multi_path = path.split(',')
1138
1155
1139 if not walk:
1156 if not walk:
1140
1157
1141 for single_path in multi_path:
1158 for single_path in multi_path:
1142
1159
1143 if not os.path.isdir(single_path):
1160 if not os.path.isdir(single_path):
1144 continue
1161 continue
1145
1162
1146 fileList = glob.glob1(single_path, "*"+ext)
1163 fileList = glob.glob1(single_path, "*"+ext)
1147
1164
1148 if not fileList:
1165 if not fileList:
1149 continue
1166 continue
1150
1167
1151 path_empty = False
1168 path_empty = False
1152
1169
1153 fileList.sort()
1170 fileList.sort()
1154
1171
1155 for thisFile in fileList:
1172 for thisFile in fileList:
1156
1173
1157 if not os.path.isfile(os.path.join(single_path, thisFile)):
1174 if not os.path.isfile(os.path.join(single_path, thisFile)):
1158 continue
1175 continue
1159
1176
1160 if not isRadarFile(thisFile):
1177 if not isRadarFile(thisFile):
1161 continue
1178 continue
1162
1179
1163 if not isFileInDateRange(thisFile, startDate, endDate):
1180 if not isFileInDateRange(thisFile, startDate, endDate):
1164 continue
1181 continue
1165
1182
1166 thisDate = getDateFromRadarFile(thisFile)
1183 thisDate = getDateFromRadarFile(thisFile)
1167
1184
1168 if thisDate in dateList:
1185 if thisDate in dateList:
1169 continue
1186 continue
1170
1187
1171 dateList.append(thisDate)
1188 dateList.append(thisDate)
1172 pathList.append(single_path)
1189 pathList.append(single_path)
1173
1190
1174 else:
1191 else:
1175 for single_path in multi_path:
1192 for single_path in multi_path:
1176
1193
1177 if not os.path.isdir(single_path):
1194 if not os.path.isdir(single_path):
1178 continue
1195 continue
1179
1196
1180 dirList = []
1197 dirList = []
1181
1198
1182 for thisPath in os.listdir(single_path):
1199 for thisPath in os.listdir(single_path):
1183
1200
1184 if not os.path.isdir(os.path.join(single_path,thisPath)):
1201 if not os.path.isdir(os.path.join(single_path,thisPath)):
1185 continue
1202 continue
1186
1203
1187 if not isRadarFolder(thisPath):
1204 if not isRadarFolder(thisPath):
1188 continue
1205 continue
1189
1206
1190 if not isFolderInDateRange(thisPath, startDate, endDate):
1207 if not isFolderInDateRange(thisPath, startDate, endDate):
1191 continue
1208 continue
1192
1209
1193 dirList.append(thisPath)
1210 dirList.append(thisPath)
1194
1211
1195 if not dirList:
1212 if not dirList:
1196 continue
1213 continue
1197
1214
1198 dirList.sort()
1215 dirList.sort()
1199
1216
1200 for thisDir in dirList:
1217 for thisDir in dirList:
1201
1218
1202 datapath = os.path.join(single_path, thisDir, expLabel)
1219 datapath = os.path.join(single_path, thisDir, expLabel)
1203 fileList = glob.glob1(datapath, "*"+ext)
1220 fileList = glob.glob1(datapath, "*"+ext)
1204
1221
1205 if not fileList:
1222 if not fileList:
1206 continue
1223 continue
1207
1224
1208 path_empty = False
1225 path_empty = False
1209
1226
1210 thisDate = getDateFromRadarFolder(thisDir)
1227 thisDate = getDateFromRadarFolder(thisDir)
1211
1228
1212 pathList.append(datapath)
1229 pathList.append(datapath)
1213 dateList.append(thisDate)
1230 dateList.append(thisDate)
1214
1231
1215 dateList.sort()
1232 dateList.sort()
1216
1233
1217 if walk:
1234 if walk:
1218 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1235 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1219 else:
1236 else:
1220 pattern_path = multi_path[0]
1237 pattern_path = multi_path[0]
1221
1238
1222 if path_empty:
1239 if path_empty:
1223 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1240 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1224 else:
1241 else:
1225 if not dateList:
1242 if not dateList:
1226 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1243 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1227
1244
1228 if include_path:
1245 if include_path:
1229 return dateList, pathList
1246 return dateList, pathList
1230
1247
1231 return dateList
1248 return dateList
1232
1249
1233 def setup(self,
1250 def setup(self,
1234 path=None,
1251 path=None,
1235 startDate=None,
1252 startDate=None,
1236 endDate=None,
1253 endDate=None,
1237 startTime=datetime.time(0,0,0),
1254 startTime=datetime.time(0,0,0),
1238 endTime=datetime.time(23,59,59),
1255 endTime=datetime.time(23,59,59),
1239 set=None,
1256 set=None,
1240 expLabel = "",
1257 expLabel = "",
1241 ext = None,
1258 ext = None,
1242 online = False,
1259 online = False,
1243 delay = 60,
1260 delay = 60,
1244 walk = True,
1261 walk = True,
1245 getblock = False,
1262 getblock = False,
1246 nTxs = 1,
1263 nTxs = 1,
1247 realtime=False,
1264 realtime=False,
1248 blocksize=None,
1265 blocksize=None,
1249 blocktime=None):
1266 blocktime=None,
1267 queue=None,
1268 skip=None,
1269 cursor=None):
1250
1270
1251 if path == None:
1271 if path == None:
1252 raise ValueError, "[Reading] The path is not valid"
1272 raise ValueError, "[Reading] The path is not valid"
1253
1273
1254 if ext == None:
1274 if ext == None:
1255 ext = self.ext
1275 ext = self.ext
1256
1276
1257 if online:
1277 if online:
1258 print "[Reading] Searching files in online mode..."
1278 print "[Reading] Searching files in online mode..."
1259
1279
1260 for nTries in range( self.nTries ):
1280 for nTries in range( self.nTries ):
1261 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1281 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1262
1282
1263 if fullpath:
1283 if fullpath:
1264 break
1284 break
1265
1285
1266 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1286 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1267 sleep( self.delay )
1287 sleep( self.delay )
1268
1288
1269 if not(fullpath):
1289 if not(fullpath):
1270 print "[Reading] There 'isn't any valid file in %s" % path
1290 print "[Reading] There 'isn't any valid file in %s" % path
1271 return
1291 return
1272
1292
1273 self.year = year
1293 self.year = year
1274 self.doy = doy
1294 self.doy = doy
1275 self.set = set - 1
1295 self.set = set - 1
1276 self.path = path
1296 self.path = path
1277 self.foldercounter = foldercounter
1297 self.foldercounter = foldercounter
1278 last_set = None
1298 last_set = None
1279
1299
1280 else:
1300 else:
1281 print "[Reading] Searching files in offline mode ..."
1301 print "[Reading] Searching files in offline mode ..."
1282 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1302 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1283 startTime=startTime, endTime=endTime,
1303 startTime=startTime, endTime=endTime,
1284 set=set, expLabel=expLabel, ext=ext,
1304 set=set, expLabel=expLabel, ext=ext,
1285 walk=walk)
1305 walk=walk, cursor=cursor,
1286
1306 skip=skip, queue=queue)
1307
1287 if not(pathList):
1308 if not(pathList):
1288 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1309 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1289 # datetime.datetime.combine(startDate,startTime).ctime(),
1310 # datetime.datetime.combine(startDate,startTime).ctime(),
1290 # datetime.datetime.combine(endDate,endTime).ctime())
1311 # datetime.datetime.combine(endDate,endTime).ctime())
1291
1312
1292 # sys.exit(-1)
1313 # sys.exit(-1)
1293
1314
1294 self.fileIndex = -1
1315 self.fileIndex = -1
1295 self.pathList = []
1316 self.pathList = []
1296 self.filenameList = []
1317 self.filenameList = []
1297 return
1318 return
1298
1319
1299 self.fileIndex = -1
1320 self.fileIndex = -1
1300 self.pathList = pathList
1321 self.pathList = pathList
1301 self.filenameList = filenameList
1322 self.filenameList = filenameList
1302 file_name = os.path.basename(filenameList[-1])
1323 file_name = os.path.basename(filenameList[-1])
1303 basename, ext = os.path.splitext(file_name)
1324 basename, ext = os.path.splitext(file_name)
1304 last_set = int(basename[-3:])
1325 last_set = int(basename[-3:])
1305
1326
1306 self.online = online
1327 self.online = online
1307 self.realtime = realtime
1328 self.realtime = realtime
1308 self.delay = delay
1329 self.delay = delay
1309 ext = ext.lower()
1330 ext = ext.lower()
1310 self.ext = ext
1331 self.ext = ext
1311 self.getByBlock = getblock
1332 self.getByBlock = getblock
1312 self.nTxs = nTxs
1333 self.nTxs = nTxs
1313 self.startTime = startTime
1334 self.startTime = startTime
1314 self.endTime = endTime
1335 self.endTime = endTime
1315
1336
1316 #Added-----------------
1337 #Added-----------------
1317 self.selBlocksize = blocksize
1338 self.selBlocksize = blocksize
1318 self.selBlocktime = blocktime
1339 self.selBlocktime = blocktime
1319
1340
1320
1341
1321 if not(self.setNextFile()):
1342 if not(self.setNextFile()):
1322 if (startDate!=None) and (endDate!=None):
1343 if (startDate!=None) and (endDate!=None):
1323 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1344 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1324 elif startDate != None:
1345 elif startDate != None:
1325 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1346 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1326 else:
1347 else:
1327 print "[Reading] No files"
1348 print "[Reading] No files"
1328
1349
1329 self.fileIndex = -1
1350 self.fileIndex = -1
1330 self.pathList = []
1351 self.pathList = []
1331 self.filenameList = []
1352 self.filenameList = []
1332 return
1353 return
1333
1354
1334 # self.getBasicHeader()
1355 # self.getBasicHeader()
1335
1356
1336 if last_set != None:
1357 if last_set != None:
1337 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1358 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1338 return
1359 return
1339
1360
1340 def getBasicHeader(self):
1361 def getBasicHeader(self):
1341
1362
1342 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1363 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1343
1364
1344 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1365 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1345
1366
1346 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1367 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1347
1368
1348 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1369 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1349
1370
1350 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1371 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1351
1372
1352 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1373 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1353
1374
1354 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1375 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1355
1376
1356 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1377 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1357
1378
1358
1379
1359 def getFirstHeader(self):
1380 def getFirstHeader(self):
1360
1381
1361 raise NotImplementedError
1382 raise NotImplementedError
1362
1383
1363 def getData(self):
1384 def getData(self):
1364
1385
1365 raise NotImplementedError
1386 raise NotImplementedError
1366
1387
1367 def hasNotDataInBuffer(self):
1388 def hasNotDataInBuffer(self):
1368
1389
1369 raise NotImplementedError
1390 raise NotImplementedError
1370
1391
1371 def readBlock(self):
1392 def readBlock(self):
1372
1393
1373 raise NotImplementedError
1394 raise NotImplementedError
1374
1395
1375 def isEndProcess(self):
1396 def isEndProcess(self):
1376
1397
1377 return self.flagNoMoreFiles
1398 return self.flagNoMoreFiles
1378
1399
1379 def printReadBlocks(self):
1400 def printReadBlocks(self):
1380
1401
1381 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1402 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1382
1403
1383 def printTotalBlocks(self):
1404 def printTotalBlocks(self):
1384
1405
1385 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1406 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1386
1407
1387 def printNumberOfBlock(self):
1408 def printNumberOfBlock(self):
1388
1409
1389 if self.flagIsNewBlock:
1410 if self.flagIsNewBlock:
1390 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1411 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1391 self.processingHeaderObj.dataBlocksPerFile,
1412 self.processingHeaderObj.dataBlocksPerFile,
1392 self.dataOut.datatime.ctime())
1413 self.dataOut.datatime.ctime())
1393
1414
1394 def printInfo(self):
1415 def printInfo(self):
1395
1416
1396 if self.__printInfo == False:
1417 if self.__printInfo == False:
1397 return
1418 return
1398
1419
1399 self.basicHeaderObj.printInfo()
1420 self.basicHeaderObj.printInfo()
1400 self.systemHeaderObj.printInfo()
1421 self.systemHeaderObj.printInfo()
1401 self.radarControllerHeaderObj.printInfo()
1422 self.radarControllerHeaderObj.printInfo()
1402 self.processingHeaderObj.printInfo()
1423 self.processingHeaderObj.printInfo()
1403
1424
1404 self.__printInfo = False
1425 self.__printInfo = False
1405
1426
1406
1427
1407 def run(self, **kwargs):
1428 def run(self, **kwargs):
1408
1429
1409 if not(self.isConfig):
1430 if not(self.isConfig):
1410
1431
1411 # self.dataOut = dataOut
1432 # self.dataOut = dataOut
1412 self.setup(**kwargs)
1433 self.setup(**kwargs)
1413 self.isConfig = True
1434 self.isConfig = True
1414
1435
1415 self.getData()
1436 self.getData()
1416
1437
1417 class JRODataWriter(JRODataIO):
1438 class JRODataWriter(JRODataIO):
1418
1439
1419 """
1440 """
1420 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1441 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1421 de los datos siempre se realiza por bloques.
1442 de los datos siempre se realiza por bloques.
1422 """
1443 """
1423
1444
1424 blockIndex = 0
1445 blockIndex = 0
1425
1446
1426 path = None
1447 path = None
1427
1448
1428 setFile = None
1449 setFile = None
1429
1450
1430 profilesPerBlock = None
1451 profilesPerBlock = None
1431
1452
1432 blocksPerFile = None
1453 blocksPerFile = None
1433
1454
1434 nWriteBlocks = 0
1455 nWriteBlocks = 0
1435
1456
1436 fileDate = None
1457 fileDate = None
1437
1458
1438 def __init__(self, dataOut=None):
1459 def __init__(self, dataOut=None):
1439 raise NotImplementedError
1460 raise NotImplementedError
1440
1461
1441
1462
1442 def hasAllDataInBuffer(self):
1463 def hasAllDataInBuffer(self):
1443 raise NotImplementedError
1464 raise NotImplementedError
1444
1465
1445
1466
1446 def setBlockDimension(self):
1467 def setBlockDimension(self):
1447 raise NotImplementedError
1468 raise NotImplementedError
1448
1469
1449
1470
1450 def writeBlock(self):
1471 def writeBlock(self):
1451 raise NotImplementedError
1472 raise NotImplementedError
1452
1473
1453
1474
1454 def putData(self):
1475 def putData(self):
1455 raise NotImplementedError
1476 raise NotImplementedError
1456
1477
1457
1478
1458 def getProcessFlags(self):
1479 def getProcessFlags(self):
1459
1480
1460 processFlags = 0
1481 processFlags = 0
1461
1482
1462 dtype_index = get_dtype_index(self.dtype)
1483 dtype_index = get_dtype_index(self.dtype)
1463 procflag_dtype = get_procflag_dtype(dtype_index)
1484 procflag_dtype = get_procflag_dtype(dtype_index)
1464
1485
1465 processFlags += procflag_dtype
1486 processFlags += procflag_dtype
1466
1487
1467 if self.dataOut.flagDecodeData:
1488 if self.dataOut.flagDecodeData:
1468 processFlags += PROCFLAG.DECODE_DATA
1489 processFlags += PROCFLAG.DECODE_DATA
1469
1490
1470 if self.dataOut.flagDeflipData:
1491 if self.dataOut.flagDeflipData:
1471 processFlags += PROCFLAG.DEFLIP_DATA
1492 processFlags += PROCFLAG.DEFLIP_DATA
1472
1493
1473 if self.dataOut.code is not None:
1494 if self.dataOut.code is not None:
1474 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1495 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1475
1496
1476 if self.dataOut.nCohInt > 1:
1497 if self.dataOut.nCohInt > 1:
1477 processFlags += PROCFLAG.COHERENT_INTEGRATION
1498 processFlags += PROCFLAG.COHERENT_INTEGRATION
1478
1499
1479 if self.dataOut.type == "Spectra":
1500 if self.dataOut.type == "Spectra":
1480 if self.dataOut.nIncohInt > 1:
1501 if self.dataOut.nIncohInt > 1:
1481 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1502 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1482
1503
1483 if self.dataOut.data_dc is not None:
1504 if self.dataOut.data_dc is not None:
1484 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1505 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1485
1506
1486 if self.dataOut.flagShiftFFT:
1507 if self.dataOut.flagShiftFFT:
1487 processFlags += PROCFLAG.SHIFT_FFT_DATA
1508 processFlags += PROCFLAG.SHIFT_FFT_DATA
1488
1509
1489 return processFlags
1510 return processFlags
1490
1511
1491 def setBasicHeader(self):
1512 def setBasicHeader(self):
1492
1513
1493 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1514 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1494 self.basicHeaderObj.version = self.versionFile
1515 self.basicHeaderObj.version = self.versionFile
1495 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1516 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1496
1517
1497 utc = numpy.floor(self.dataOut.utctime)
1518 utc = numpy.floor(self.dataOut.utctime)
1498 milisecond = (self.dataOut.utctime - utc)* 1000.0
1519 milisecond = (self.dataOut.utctime - utc)* 1000.0
1499
1520
1500 self.basicHeaderObj.utc = utc
1521 self.basicHeaderObj.utc = utc
1501 self.basicHeaderObj.miliSecond = milisecond
1522 self.basicHeaderObj.miliSecond = milisecond
1502 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1523 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1503 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1524 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1504 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1525 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1505
1526
1506 def setFirstHeader(self):
1527 def setFirstHeader(self):
1507 """
1528 """
1508 Obtiene una copia del First Header
1529 Obtiene una copia del First Header
1509
1530
1510 Affected:
1531 Affected:
1511
1532
1512 self.basicHeaderObj
1533 self.basicHeaderObj
1513 self.systemHeaderObj
1534 self.systemHeaderObj
1514 self.radarControllerHeaderObj
1535 self.radarControllerHeaderObj
1515 self.processingHeaderObj self.
1536 self.processingHeaderObj self.
1516
1537
1517 Return:
1538 Return:
1518 None
1539 None
1519 """
1540 """
1520
1541
1521 raise NotImplementedError
1542 raise NotImplementedError
1522
1543
1523 def __writeFirstHeader(self):
1544 def __writeFirstHeader(self):
1524 """
1545 """
1525 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1546 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1526
1547
1527 Affected:
1548 Affected:
1528 __dataType
1549 __dataType
1529
1550
1530 Return:
1551 Return:
1531 None
1552 None
1532 """
1553 """
1533
1554
1534 # CALCULAR PARAMETROS
1555 # CALCULAR PARAMETROS
1535
1556
1536 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1557 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1537 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1558 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1538
1559
1539 self.basicHeaderObj.write(self.fp)
1560 self.basicHeaderObj.write(self.fp)
1540 self.systemHeaderObj.write(self.fp)
1561 self.systemHeaderObj.write(self.fp)
1541 self.radarControllerHeaderObj.write(self.fp)
1562 self.radarControllerHeaderObj.write(self.fp)
1542 self.processingHeaderObj.write(self.fp)
1563 self.processingHeaderObj.write(self.fp)
1543
1564
1544 def __setNewBlock(self):
1565 def __setNewBlock(self):
1545 """
1566 """
1546 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1567 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1547
1568
1548 Return:
1569 Return:
1549 0 : si no pudo escribir nada
1570 0 : si no pudo escribir nada
1550 1 : Si escribio el Basic el First Header
1571 1 : Si escribio el Basic el First Header
1551 """
1572 """
1552 if self.fp == None:
1573 if self.fp == None:
1553 self.setNextFile()
1574 self.setNextFile()
1554
1575
1555 if self.flagIsNewFile:
1576 if self.flagIsNewFile:
1556 return 1
1577 return 1
1557
1578
1558 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1579 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1559 self.basicHeaderObj.write(self.fp)
1580 self.basicHeaderObj.write(self.fp)
1560 return 1
1581 return 1
1561
1582
1562 if not( self.setNextFile() ):
1583 if not( self.setNextFile() ):
1563 return 0
1584 return 0
1564
1585
1565 return 1
1586 return 1
1566
1587
1567
1588
1568 def writeNextBlock(self):
1589 def writeNextBlock(self):
1569 """
1590 """
1570 Selecciona el bloque siguiente de datos y los escribe en un file
1591 Selecciona el bloque siguiente de datos y los escribe en un file
1571
1592
1572 Return:
1593 Return:
1573 0 : Si no hizo pudo escribir el bloque de datos
1594 0 : Si no hizo pudo escribir el bloque de datos
1574 1 : Si no pudo escribir el bloque de datos
1595 1 : Si no pudo escribir el bloque de datos
1575 """
1596 """
1576 if not( self.__setNewBlock() ):
1597 if not( self.__setNewBlock() ):
1577 return 0
1598 return 0
1578
1599
1579 self.writeBlock()
1600 self.writeBlock()
1580
1601
1581 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1602 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1582 self.processingHeaderObj.dataBlocksPerFile)
1603 self.processingHeaderObj.dataBlocksPerFile)
1583
1604
1584 return 1
1605 return 1
1585
1606
1586 def setNextFile(self):
1607 def setNextFile(self):
1587 """
1608 """
1588 Determina el siguiente file que sera escrito
1609 Determina el siguiente file que sera escrito
1589
1610
1590 Affected:
1611 Affected:
1591 self.filename
1612 self.filename
1592 self.subfolder
1613 self.subfolder
1593 self.fp
1614 self.fp
1594 self.setFile
1615 self.setFile
1595 self.flagIsNewFile
1616 self.flagIsNewFile
1596
1617
1597 Return:
1618 Return:
1598 0 : Si el archivo no puede ser escrito
1619 0 : Si el archivo no puede ser escrito
1599 1 : Si el archivo esta listo para ser escrito
1620 1 : Si el archivo esta listo para ser escrito
1600 """
1621 """
1601 ext = self.ext
1622 ext = self.ext
1602 path = self.path
1623 path = self.path
1603
1624
1604 if self.fp != None:
1625 if self.fp != None:
1605 self.fp.close()
1626 self.fp.close()
1606
1627
1607 timeTuple = time.localtime( self.dataOut.utctime)
1628 timeTuple = time.localtime( self.dataOut.utctime)
1608 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1629 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1609
1630
1610 fullpath = os.path.join( path, subfolder )
1631 fullpath = os.path.join( path, subfolder )
1611 setFile = self.setFile
1632 setFile = self.setFile
1612
1633
1613 if not( os.path.exists(fullpath) ):
1634 if not( os.path.exists(fullpath) ):
1614 os.mkdir(fullpath)
1635 os.mkdir(fullpath)
1615 setFile = -1 #inicializo mi contador de seteo
1636 setFile = -1 #inicializo mi contador de seteo
1616 else:
1637 else:
1617 filesList = os.listdir( fullpath )
1638 filesList = os.listdir( fullpath )
1618 if len( filesList ) > 0:
1639 if len( filesList ) > 0:
1619 filesList = sorted( filesList, key=str.lower )
1640 filesList = sorted( filesList, key=str.lower )
1620 filen = filesList[-1]
1641 filen = filesList[-1]
1621 # el filename debera tener el siguiente formato
1642 # el filename debera tener el siguiente formato
1622 # 0 1234 567 89A BCDE (hex)
1643 # 0 1234 567 89A BCDE (hex)
1623 # x YYYY DDD SSS .ext
1644 # x YYYY DDD SSS .ext
1624 if isNumber( filen[8:11] ):
1645 if isNumber( filen[8:11] ):
1625 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1646 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1626 else:
1647 else:
1627 setFile = -1
1648 setFile = -1
1628 else:
1649 else:
1629 setFile = -1 #inicializo mi contador de seteo
1650 setFile = -1 #inicializo mi contador de seteo
1630
1651
1631 setFile += 1
1652 setFile += 1
1632
1653
1633 #If this is a new day it resets some values
1654 #If this is a new day it resets some values
1634 if self.dataOut.datatime.date() > self.fileDate:
1655 if self.dataOut.datatime.date() > self.fileDate:
1635 setFile = 0
1656 setFile = 0
1636 self.nTotalBlocks = 0
1657 self.nTotalBlocks = 0
1637
1658
1638 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1659 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1639
1660
1640 filename = os.path.join( path, subfolder, filen )
1661 filename = os.path.join( path, subfolder, filen )
1641
1662
1642 fp = open( filename,'wb' )
1663 fp = open( filename,'wb' )
1643
1664
1644 self.blockIndex = 0
1665 self.blockIndex = 0
1645
1666
1646 #guardando atributos
1667 #guardando atributos
1647 self.filename = filename
1668 self.filename = filename
1648 self.subfolder = subfolder
1669 self.subfolder = subfolder
1649 self.fp = fp
1670 self.fp = fp
1650 self.setFile = setFile
1671 self.setFile = setFile
1651 self.flagIsNewFile = 1
1672 self.flagIsNewFile = 1
1652 self.fileDate = self.dataOut.datatime.date()
1673 self.fileDate = self.dataOut.datatime.date()
1653
1674
1654 self.setFirstHeader()
1675 self.setFirstHeader()
1655
1676
1656 print '[Writing] Opening file: %s'%self.filename
1677 print '[Writing] Opening file: %s'%self.filename
1657
1678
1658 self.__writeFirstHeader()
1679 self.__writeFirstHeader()
1659
1680
1660 return 1
1681 return 1
1661
1682
1662 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1683 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1663 """
1684 """
1664 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1685 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1665
1686
1666 Inputs:
1687 Inputs:
1667 path : directory where data will be saved
1688 path : directory where data will be saved
1668 profilesPerBlock : number of profiles per block
1689 profilesPerBlock : number of profiles per block
1669 set : initial file set
1690 set : initial file set
1670 datatype : An integer number that defines data type:
1691 datatype : An integer number that defines data type:
1671 0 : int8 (1 byte)
1692 0 : int8 (1 byte)
1672 1 : int16 (2 bytes)
1693 1 : int16 (2 bytes)
1673 2 : int32 (4 bytes)
1694 2 : int32 (4 bytes)
1674 3 : int64 (8 bytes)
1695 3 : int64 (8 bytes)
1675 4 : float32 (4 bytes)
1696 4 : float32 (4 bytes)
1676 5 : double64 (8 bytes)
1697 5 : double64 (8 bytes)
1677
1698
1678 Return:
1699 Return:
1679 0 : Si no realizo un buen seteo
1700 0 : Si no realizo un buen seteo
1680 1 : Si realizo un buen seteo
1701 1 : Si realizo un buen seteo
1681 """
1702 """
1682
1703
1683 if ext == None:
1704 if ext == None:
1684 ext = self.ext
1705 ext = self.ext
1685
1706
1686 self.ext = ext.lower()
1707 self.ext = ext.lower()
1687
1708
1688 self.path = path
1709 self.path = path
1689
1710
1690 if set is None:
1711 if set is None:
1691 self.setFile = -1
1712 self.setFile = -1
1692 else:
1713 else:
1693 self.setFile = set - 1
1714 self.setFile = set - 1
1694
1715
1695 self.blocksPerFile = blocksPerFile
1716 self.blocksPerFile = blocksPerFile
1696
1717
1697 self.profilesPerBlock = profilesPerBlock
1718 self.profilesPerBlock = profilesPerBlock
1698
1719
1699 self.dataOut = dataOut
1720 self.dataOut = dataOut
1700 self.fileDate = self.dataOut.datatime.date()
1721 self.fileDate = self.dataOut.datatime.date()
1701 #By default
1722 #By default
1702 self.dtype = self.dataOut.dtype
1723 self.dtype = self.dataOut.dtype
1703
1724
1704 if datatype is not None:
1725 if datatype is not None:
1705 self.dtype = get_numpy_dtype(datatype)
1726 self.dtype = get_numpy_dtype(datatype)
1706
1727
1707 if not(self.setNextFile()):
1728 if not(self.setNextFile()):
1708 print "[Writing] There isn't a next file"
1729 print "[Writing] There isn't a next file"
1709 return 0
1730 return 0
1710
1731
1711 self.setBlockDimension()
1732 self.setBlockDimension()
1712
1733
1713 return 1
1734 return 1
1714
1735
1715 def run(self, dataOut, **kwargs):
1736 def run(self, dataOut, **kwargs):
1716
1737
1717 if not(self.isConfig):
1738 if not(self.isConfig):
1718
1739
1719 self.setup(dataOut, **kwargs)
1740 self.setup(dataOut, **kwargs)
1720 self.isConfig = True
1741 self.isConfig = True
1721
1722 self.putData()
1723
1742
1743 self.putData()
@@ -1,378 +1,378
1 '''
1 '''
2 @author: Juan C. Espinoza
2 @author: Juan C. Espinoza
3 '''
3 '''
4
4
5 import time
5 import time
6 import json
6 import json
7 import numpy
7 import numpy
8 import paho.mqtt.client as mqtt
8 import paho.mqtt.client as mqtt
9 import zmq
9 import zmq
10 import cPickle as pickle
10 import cPickle as pickle
11 import datetime
11 import datetime
12 from zmq.utils.monitor import recv_monitor_message
12 from zmq.utils.monitor import recv_monitor_message
13 from functools import wraps
13 from functools import wraps
14 from threading import Thread
14 from threading import Thread
15 from multiprocessing import Process
15 from multiprocessing import Process
16
16
17 from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit
17 from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit
18
18
19 MAXNUMX = 100
19 MAXNUMX = 100
20 MAXNUMY = 100
20 MAXNUMY = 100
21 throttle_value = 5
21 throttle_value = 5
22
22
23 class PrettyFloat(float):
23 class PrettyFloat(float):
24 def __repr__(self):
24 def __repr__(self):
25 return '%.2f' % self
25 return '%.2f' % self
26
26
27 def roundFloats(obj):
27 def roundFloats(obj):
28 if isinstance(obj, list):
28 if isinstance(obj, list):
29 return map(roundFloats, obj)
29 return map(roundFloats, obj)
30 elif isinstance(obj, float):
30 elif isinstance(obj, float):
31 return round(obj, 2)
31 return round(obj, 2)
32
32
33
33
34 class throttle(object):
34 class throttle(object):
35 """Decorator that prevents a function from being called more than once every
35 """Decorator that prevents a function from being called more than once every
36 time period.
36 time period.
37 To create a function that cannot be called more than once a minute, but
37 To create a function that cannot be called more than once a minute, but
38 will sleep until it can be called:
38 will sleep until it can be called:
39 @throttle(minutes=1)
39 @throttle(minutes=1)
40 def foo():
40 def foo():
41 pass
41 pass
42
42
43 for i in range(10):
43 for i in range(10):
44 foo()
44 foo()
45 print "This function has run %s times." % i
45 print "This function has run %s times." % i
46 """
46 """
47
47
48 def __init__(self, seconds=0, minutes=0, hours=0):
48 def __init__(self, seconds=0, minutes=0, hours=0):
49 self.throttle_period = datetime.timedelta(
49 self.throttle_period = datetime.timedelta(
50 seconds=seconds, minutes=minutes, hours=hours
50 seconds=seconds, minutes=minutes, hours=hours
51 )
51 )
52 self.time_of_last_call = datetime.datetime.min
52 self.time_of_last_call = datetime.datetime.min
53
53
54 def __call__(self, fn):
54 def __call__(self, fn):
55 @wraps(fn)
55 @wraps(fn)
56 def wrapper(*args, **kwargs):
56 def wrapper(*args, **kwargs):
57 now = datetime.datetime.now()
57 now = datetime.datetime.now()
58 time_since_last_call = now - self.time_of_last_call
58 time_since_last_call = now - self.time_of_last_call
59 time_left = self.throttle_period - time_since_last_call
59 time_left = self.throttle_period - time_since_last_call
60
60
61 if time_left > datetime.timedelta(seconds=0):
61 if time_left > datetime.timedelta(seconds=0):
62 return
62 return
63
63
64 self.time_of_last_call = datetime.datetime.now()
64 self.time_of_last_call = datetime.datetime.now()
65 return fn(*args, **kwargs)
65 return fn(*args, **kwargs)
66
66
67 return wrapper
67 return wrapper
68
68
69
69
70 class PublishData(Operation):
70 class PublishData(Operation):
71 """Clase publish."""
71 """Clase publish."""
72
72
73 def __init__(self, **kwargs):
73 def __init__(self, **kwargs):
74 """Inicio."""
74 """Inicio."""
75 Operation.__init__(self, **kwargs)
75 Operation.__init__(self, **kwargs)
76 self.isConfig = False
76 self.isConfig = False
77 self.client = None
77 self.client = None
78 self.zeromq = None
78 self.zeromq = None
79 self.mqtt = None
79 self.mqtt = None
80
80
81 def on_disconnect(self, client, userdata, rc):
81 def on_disconnect(self, client, userdata, rc):
82 if rc != 0:
82 if rc != 0:
83 print("Unexpected disconnection.")
83 print("Unexpected disconnection.")
84 self.connect()
84 self.connect()
85
85
86 def connect(self):
86 def connect(self):
87 print 'trying to connect'
87 print 'trying to connect'
88 try:
88 try:
89 self.client.connect(
89 self.client.connect(
90 host=self.host,
90 host=self.host,
91 port=self.port,
91 port=self.port,
92 keepalive=60*10,
92 keepalive=60*10,
93 bind_address='')
93 bind_address='')
94 print "connected"
94 print "connected"
95 self.client.loop_start()
95 self.client.loop_start()
96 # self.client.publish(
96 # self.client.publish(
97 # self.topic + 'SETUP',
97 # self.topic + 'SETUP',
98 # json.dumps(setup),
98 # json.dumps(setup),
99 # retain=True
99 # retain=True
100 # )
100 # )
101 except:
101 except:
102 print "MQTT Conection error."
102 print "MQTT Conection error."
103 self.client = False
103 self.client = False
104
104
105 def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, **kwargs):
105 def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, **kwargs):
106 self.counter = 0
106 self.counter = 0
107 self.topic = kwargs.get('topic', 'schain')
107 self.topic = kwargs.get('topic', 'schain')
108 self.delay = kwargs.get('delay', 0)
108 self.delay = kwargs.get('delay', 0)
109 self.plottype = kwargs.get('plottype', 'spectra')
109 self.plottype = kwargs.get('plottype', 'spectra')
110 self.host = kwargs.get('host', "10.10.10.82")
110 self.host = kwargs.get('host', "10.10.10.82")
111 self.port = kwargs.get('port', 3000)
111 self.port = kwargs.get('port', 3000)
112 self.clientId = clientId
112 self.clientId = clientId
113 self.cnt = 0
113 self.cnt = 0
114 self.zeromq = zeromq
114 self.zeromq = zeromq
115 self.mqtt = kwargs.get('plottype', 0)
115 self.mqtt = kwargs.get('plottype', 0)
116 self.client = None
116 self.client = None
117 setup = []
117 setup = []
118 if mqtt is 1:
118 if mqtt is 1:
119 print 'mqqt es 1'
119 print 'mqqt es 1'
120 self.client = mqtt.Client(
120 self.client = mqtt.Client(
121 client_id=self.clientId + self.topic + 'SCHAIN',
121 client_id=self.clientId + self.topic + 'SCHAIN',
122 clean_session=True)
122 clean_session=True)
123 self.client.on_disconnect = self.on_disconnect
123 self.client.on_disconnect = self.on_disconnect
124 self.connect()
124 self.connect()
125 for plot in self.plottype:
125 for plot in self.plottype:
126 setup.append({
126 setup.append({
127 'plot': plot,
127 'plot': plot,
128 'topic': self.topic + plot,
128 'topic': self.topic + plot,
129 'title': getattr(self, plot + '_' + 'title', False),
129 'title': getattr(self, plot + '_' + 'title', False),
130 'xlabel': getattr(self, plot + '_' + 'xlabel', False),
130 'xlabel': getattr(self, plot + '_' + 'xlabel', False),
131 'ylabel': getattr(self, plot + '_' + 'ylabel', False),
131 'ylabel': getattr(self, plot + '_' + 'ylabel', False),
132 'xrange': getattr(self, plot + '_' + 'xrange', False),
132 'xrange': getattr(self, plot + '_' + 'xrange', False),
133 'yrange': getattr(self, plot + '_' + 'yrange', False),
133 'yrange': getattr(self, plot + '_' + 'yrange', False),
134 'zrange': getattr(self, plot + '_' + 'zrange', False),
134 'zrange': getattr(self, plot + '_' + 'zrange', False),
135 })
135 })
136 if zeromq is 1:
136 if zeromq is 1:
137 context = zmq.Context()
137 context = zmq.Context()
138 self.zmq_socket = context.socket(zmq.PUSH)
138 self.zmq_socket = context.socket(zmq.PUSH)
139 server = kwargs.get('server', 'zmq.pipe')
139 server = kwargs.get('server', 'zmq.pipe')
140
140
141 if 'tcp://' in server:
141 if 'tcp://' in server:
142 address = server
142 address = server
143 else:
143 else:
144 address = 'ipc:///tmp/%s' % server
144 address = 'ipc:///tmp/%s' % server
145
145
146 self.zmq_socket.connect(address)
146 self.zmq_socket.connect(address)
147 time.sleep(1)
147 time.sleep(1)
148 print 'zeromq configured'
148 print 'zeromq configured'
149
149
150
150
151 def publish_data(self):
151 def publish_data(self):
152 self.dataOut.finished = False
152 self.dataOut.finished = False
153 if self.mqtt is 1:
153 if self.mqtt is 1:
154 yData = self.dataOut.heightList[:2].tolist()
154 yData = self.dataOut.heightList[:2].tolist()
155 if self.plottype == 'spectra':
155 if self.plottype == 'spectra':
156 data = getattr(self.dataOut, 'data_spc')
156 data = getattr(self.dataOut, 'data_spc')
157 z = data/self.dataOut.normFactor
157 z = data/self.dataOut.normFactor
158 zdB = 10*numpy.log10(z)
158 zdB = 10*numpy.log10(z)
159 xlen, ylen = zdB[0].shape
159 xlen, ylen = zdB[0].shape
160 dx = int(xlen/MAXNUMX) + 1
160 dx = int(xlen/MAXNUMX) + 1
161 dy = int(ylen/MAXNUMY) + 1
161 dy = int(ylen/MAXNUMY) + 1
162 Z = [0 for i in self.dataOut.channelList]
162 Z = [0 for i in self.dataOut.channelList]
163 for i in self.dataOut.channelList:
163 for i in self.dataOut.channelList:
164 Z[i] = zdB[i][::dx, ::dy].tolist()
164 Z[i] = zdB[i][::dx, ::dy].tolist()
165 payload = {
165 payload = {
166 'timestamp': self.dataOut.utctime,
166 'timestamp': self.dataOut.utctime,
167 'data': roundFloats(Z),
167 'data': roundFloats(Z),
168 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
168 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
169 'interval': self.dataOut.getTimeInterval(),
169 'interval': self.dataOut.getTimeInterval(),
170 'type': self.plottype,
170 'type': self.plottype,
171 'yData': yData
171 'yData': yData
172 }
172 }
173 # print payload
173 # print payload
174
174
175 elif self.plottype in ('rti', 'power'):
175 elif self.plottype in ('rti', 'power'):
176 data = getattr(self.dataOut, 'data_spc')
176 data = getattr(self.dataOut, 'data_spc')
177 z = data/self.dataOut.normFactor
177 z = data/self.dataOut.normFactor
178 avg = numpy.average(z, axis=1)
178 avg = numpy.average(z, axis=1)
179 avgdB = 10*numpy.log10(avg)
179 avgdB = 10*numpy.log10(avg)
180 xlen, ylen = z[0].shape
180 xlen, ylen = z[0].shape
181 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
181 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
182 AVG = [0 for i in self.dataOut.channelList]
182 AVG = [0 for i in self.dataOut.channelList]
183 for i in self.dataOut.channelList:
183 for i in self.dataOut.channelList:
184 AVG[i] = avgdB[i][::dy].tolist()
184 AVG[i] = avgdB[i][::dy].tolist()
185 payload = {
185 payload = {
186 'timestamp': self.dataOut.utctime,
186 'timestamp': self.dataOut.utctime,
187 'data': roundFloats(AVG),
187 'data': roundFloats(AVG),
188 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
188 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
189 'interval': self.dataOut.getTimeInterval(),
189 'interval': self.dataOut.getTimeInterval(),
190 'type': self.plottype,
190 'type': self.plottype,
191 'yData': yData
191 'yData': yData
192 }
192 }
193 elif self.plottype == 'noise':
193 elif self.plottype == 'noise':
194 noise = self.dataOut.getNoise()/self.dataOut.normFactor
194 noise = self.dataOut.getNoise()/self.dataOut.normFactor
195 noisedB = 10*numpy.log10(noise)
195 noisedB = 10*numpy.log10(noise)
196 payload = {
196 payload = {
197 'timestamp': self.dataOut.utctime,
197 'timestamp': self.dataOut.utctime,
198 'data': roundFloats(noisedB.reshape(-1, 1).tolist()),
198 'data': roundFloats(noisedB.reshape(-1, 1).tolist()),
199 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
199 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
200 'interval': self.dataOut.getTimeInterval(),
200 'interval': self.dataOut.getTimeInterval(),
201 'type': self.plottype,
201 'type': self.plottype,
202 'yData': yData
202 'yData': yData
203 }
203 }
204 elif self.plottype == 'snr':
204 elif self.plottype == 'snr':
205 data = getattr(self.dataOut, 'data_SNR')
205 data = getattr(self.dataOut, 'data_SNR')
206 avgdB = 10*numpy.log10(data)
206 avgdB = 10*numpy.log10(data)
207
207
208 ylen = data[0].size
208 ylen = data[0].size
209 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
209 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
210 AVG = [0 for i in self.dataOut.channelList]
210 AVG = [0 for i in self.dataOut.channelList]
211 for i in self.dataOut.channelList:
211 for i in self.dataOut.channelList:
212 AVG[i] = avgdB[i][::dy].tolist()
212 AVG[i] = avgdB[i][::dy].tolist()
213 payload = {
213 payload = {
214 'timestamp': self.dataOut.utctime,
214 'timestamp': self.dataOut.utctime,
215 'data': roundFloats(AVG),
215 'data': roundFloats(AVG),
216 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
216 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
217 'type': self.plottype,
217 'type': self.plottype,
218 'yData': yData
218 'yData': yData
219 }
219 }
220 else:
220 else:
221 print "Tipo de grafico invalido"
221 print "Tipo de grafico invalido"
222 payload = {
222 payload = {
223 'data': 'None',
223 'data': 'None',
224 'timestamp': 'None',
224 'timestamp': 'None',
225 'type': None
225 'type': None
226 }
226 }
227 # print 'Publishing data to {}'.format(self.host)
227 # print 'Publishing data to {}'.format(self.host)
228 self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0)
228 self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0)
229
229
230 if self.zeromq is 1:
230 if self.zeromq is 1:
231 print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime)
231 print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime)
232 self.zmq_socket.send_pyobj(self.dataOut)
232 self.zmq_socket.send_pyobj(self.dataOut)
233
233
234 def run(self, dataOut, **kwargs):
234 def run(self, dataOut, **kwargs):
235 self.dataOut = dataOut
235 self.dataOut = dataOut
236 if not self.isConfig:
236 if not self.isConfig:
237 self.setup(**kwargs)
237 self.setup(**kwargs)
238 self.isConfig = True
238 self.isConfig = True
239
239
240 self.publish_data()
240 self.publish_data()
241 time.sleep(self.delay)
241 time.sleep(self.delay)
242
242
243 def close(self):
243 def close(self):
244 if self.zeromq is 1:
244 if self.zeromq is 1:
245 self.dataOut.finished = True
245 self.dataOut.finished = True
246 self.zmq_socket.send_pyobj(self.dataOut)
246 self.zmq_socket.send_pyobj(self.dataOut)
247
247
248 if self.client:
248 if self.client:
249 self.client.loop_stop()
249 self.client.loop_stop()
250 self.client.disconnect()
250 self.client.disconnect()
251
251
252
252
253 class ReceiverData(ProcessingUnit, Process):
253 class ReceiverData(ProcessingUnit, Process):
254
254
255 def __init__(self, **kwargs):
255 def __init__(self, **kwargs):
256
256
257 ProcessingUnit.__init__(self, **kwargs)
257 ProcessingUnit.__init__(self, **kwargs)
258 Process.__init__(self)
258 Process.__init__(self)
259 self.mp = False
259 self.mp = False
260 self.isConfig = False
260 self.isConfig = False
261 self.plottypes =[]
261 self.plottypes =[]
262 self.connections = 0
262 self.connections = 0
263 server = kwargs.get('server', 'zmq.pipe')
263 server = kwargs.get('server', 'zmq.pipe')
264 if 'tcp://' in server:
264 if 'tcp://' in server:
265 address = server
265 address = server
266 else:
266 else:
267 address = 'ipc:///tmp/%s' % server
267 address = 'ipc:///tmp/%s' % server
268
268
269 self.address = address
269 self.address = address
270 self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')]
270 self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')]
271 self.realtime = kwargs.get('realtime', False)
271 self.realtime = kwargs.get('realtime', False)
272 global throttle_value
272 global throttle_value
273 throttle_value = kwargs.get('throttle', 10)
273 throttle_value = kwargs.get('throttle', 10)
274 self.setup()
274 self.setup()
275
275
276 def setup(self):
276 def setup(self):
277
277
278 self.data = {}
278 self.data = {}
279 self.data['times'] = []
279 self.data['times'] = []
280 for plottype in self.plottypes:
280 for plottype in self.plottypes:
281 self.data[plottype] = {}
281 self.data[plottype] = {}
282
282 self.data['noise'] = {}
283 self.isConfig = True
283 self.isConfig = True
284
284
285 def event_monitor(self, monitor):
285 def event_monitor(self, monitor):
286
286
287 events = {}
287 events = {}
288
288
289 for name in dir(zmq):
289 for name in dir(zmq):
290 if name.startswith('EVENT_'):
290 if name.startswith('EVENT_'):
291 value = getattr(zmq, name)
291 value = getattr(zmq, name)
292 events[value] = name
292 events[value] = name
293
293
294 while monitor.poll():
294 while monitor.poll():
295 evt = recv_monitor_message(monitor)
295 evt = recv_monitor_message(monitor)
296 if evt['event'] == 32:
296 if evt['event'] == 32:
297 self.connections += 1
297 self.connections += 1
298 if evt['event'] == 512:
298 if evt['event'] == 512:
299 pass
299 pass
300 if self.connections == 0 and self.started is True:
300 if self.connections == 0 and self.started is True:
301 self.ended = True
301 self.ended = True
302 # send('ENDED')
302 # send('ENDED')
303 evt.update({'description': events[evt['event']]})
303 evt.update({'description': events[evt['event']]})
304
304
305 if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
305 if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
306 break
306 break
307 monitor.close()
307 monitor.close()
308 print("event monitor thread done!")
308 print("event monitor thread done!")
309
309
310 @throttle(seconds=throttle_value)
310 @throttle(seconds=throttle_value)
311 def sendData(self, data):
311 def sendData(self, data):
312 self.send(data)
312 self.send(data)
313
313
314 def send(self, data):
314 def send(self, data):
315 print '[sending] data=%s size=%s' % (data.keys(), len(data['times']))
315 print '[sending] data=%s size=%s' % (data.keys(), len(data['times']))
316 self.sender.send_pyobj(data)
316 self.sender.send_pyobj(data)
317
317
318 def update(self):
318 def update(self):
319
319
320 t = self.dataOut.ltctime
320 t = self.dataOut.ltctime
321 self.data['times'].append(t)
321 self.data['times'].append(t)
322 self.data['dataOut'] = self.dataOut
322 self.data['dataOut'] = self.dataOut
323
323
324 for plottype in self.plottypes:
324 for plottype in self.plottypes:
325
325
326 if plottype == 'spc':
326 if plottype == 'spc':
327 z = self.dataOut.data_spc/self.dataOut.normFactor
327 z = self.dataOut.data_spc/self.dataOut.normFactor
328 zdB = 10*numpy.log10(z)
328 self.data[plottype] = 10*numpy.log10(z)
329 self.data[plottype] = zdB
329 self.data['noise'][t] = 10*numpy.log10(self.dataOut.getNoise()/self.dataOut.normFactor)
330 if plottype == 'rti':
330 if plottype == 'rti':
331 self.data[plottype][t] = self.dataOut.getPower()
331 self.data[plottype][t] = self.dataOut.getPower()
332 if plottype == 'snr':
332 if plottype == 'snr':
333 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR)
333 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR)
334 if plottype == 'dop':
334 if plottype == 'dop':
335 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP)
335 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP)
336 if plottype == 'coh':
336 if plottype == 'coh':
337 self.data[plottype][t] = self.dataOut.getCoherence()
337 self.data[plottype][t] = self.dataOut.getCoherence()
338 if plottype == 'phase':
338 if plottype == 'phase':
339 self.data[plottype][t] = self.dataOut.getCoherence(phase=True)
339 self.data[plottype][t] = self.dataOut.getCoherence(phase=True)
340
340
341 def run(self):
341 def run(self):
342
342
343 print '[Starting] {} from {}'.format(self.name, self.address)
343 print '[Starting] {} from {}'.format(self.name, self.address)
344
344
345 self.context = zmq.Context()
345 self.context = zmq.Context()
346 self.receiver = self.context.socket(zmq.PULL)
346 self.receiver = self.context.socket(zmq.PULL)
347 self.receiver.bind(self.address)
347 self.receiver.bind(self.address)
348 monitor = self.receiver.get_monitor_socket()
348 monitor = self.receiver.get_monitor_socket()
349 self.sender = self.context.socket(zmq.PUB)
349 self.sender = self.context.socket(zmq.PUB)
350
350
351 self.sender.bind("ipc:///tmp/zmq.plots")
351 self.sender.bind("ipc:///tmp/zmq.plots")
352
352
353 t = Thread(target=self.event_monitor, args=(monitor,))
353 t = Thread(target=self.event_monitor, args=(monitor,))
354 t.start()
354 t.start()
355
355
356 while True:
356 while True:
357 self.dataOut = self.receiver.recv_pyobj()
357 self.dataOut = self.receiver.recv_pyobj()
358 print '[Receiving] {} - {}'.format(self.dataOut.type,
358 print '[Receiving] {} - {}'.format(self.dataOut.type,
359 self.dataOut.datatime.ctime())
359 self.dataOut.datatime.ctime())
360
360
361 self.update()
361 self.update()
362
362
363 if self.dataOut.finished is True:
363 if self.dataOut.finished is True:
364 self.send(self.data)
364 self.send(self.data)
365 self.connections -= 1
365 self.connections -= 1
366 if self.connections==0 and self.started:
366 if self.connections==0 and self.started:
367 self.ended = True
367 self.ended = True
368 self.data['ENDED'] = True
368 self.data['ENDED'] = True
369 self.send(self.data)
369 self.send(self.data)
370 self.setup()
370 self.setup()
371 else:
371 else:
372 if self.realtime:
372 if self.realtime:
373 self.send(self.data)
373 self.send(self.data)
374 else:
374 else:
375 self.sendData(self.data)
375 self.sendData(self.data)
376 self.started = True
376 self.started = True
377
377
378 return
378 return
General Comments 0
You need to be logged in to leave comments. Login now