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