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