##// END OF EJS Templates
Merge branch 'v3.0-devel' of http://jro-dev.igp.gob.pe/rhodecode/schain into v3.0-devel
Juan C. Espinoza -
r1175:7e36d2e90f1d merge
parent child
Show More
@@ -1,1342 +1,1333
1 '''
1 '''
2 Updated on January , 2018, for multiprocessing purposes
3 Author: Sergio Cortez
2 Created on September , 2012
4 Created on September , 2012
3 @author:
4 '''
5 '''
5
6 from platform import python_version
6 import sys
7 import sys
7 import ast
8 import ast
8 import datetime
9 import datetime
9 import traceback
10 import traceback
10 import math
11 import math
11 import time
12 import time
13 import zmq
12 from multiprocessing import Process, cpu_count
14 from multiprocessing import Process, cpu_count
13
15
14 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
16 from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring
15 from xml.dom import minidom
17 from xml.dom import minidom
16
18
17 import schainpy
19
18 from schainpy.admin import Alarm, SchainWarning
20 from schainpy.admin import Alarm, SchainWarning
19 from schainpy.model import *
21
22 ### Temporary imports!!!
23 # from schainpy.model import *
24 from schainpy.model.io import *
25 from schainpy.model.graphics import *
26 from schainpy.model.proc.jroproc_base import *
27 from schainpy.model.proc.bltrproc_parameters import *
28 from schainpy.model.proc.jroproc_spectra import *
29 from schainpy.model.proc.jroproc_voltage import *
30 from schainpy.model.proc.jroproc_parameters import *
31 from schainpy.model.utils.jroutils_publish import *
20 from schainpy.utils import log
32 from schainpy.utils import log
33 ###
21
34
22 DTYPES = {
35 DTYPES = {
23 'Voltage': '.r',
36 'Voltage': '.r',
24 'Spectra': '.pdata'
37 'Spectra': '.pdata'
25 }
38 }
26
39
27
40
28 def MPProject(project, n=cpu_count()):
41 def MPProject(project, n=cpu_count()):
29 '''
42 '''
30 Project wrapper to run schain in n processes
43 Project wrapper to run schain in n processes
31 '''
44 '''
32
45
33 rconf = project.getReadUnitObj()
46 rconf = project.getReadUnitObj()
34 op = rconf.getOperationObj('run')
47 op = rconf.getOperationObj('run')
35 dt1 = op.getParameterValue('startDate')
48 dt1 = op.getParameterValue('startDate')
36 dt2 = op.getParameterValue('endDate')
49 dt2 = op.getParameterValue('endDate')
37 tm1 = op.getParameterValue('startTime')
50 tm1 = op.getParameterValue('startTime')
38 tm2 = op.getParameterValue('endTime')
51 tm2 = op.getParameterValue('endTime')
39 days = (dt2 - dt1).days
52 days = (dt2 - dt1).days
40
53
41 for day in range(days + 1):
54 for day in range(days + 1):
42 skip = 0
55 skip = 0
43 cursor = 0
56 cursor = 0
44 processes = []
57 processes = []
45 dt = dt1 + datetime.timedelta(day)
58 dt = dt1 + datetime.timedelta(day)
46 dt_str = dt.strftime('%Y/%m/%d')
59 dt_str = dt.strftime('%Y/%m/%d')
47 reader = JRODataReader()
60 reader = JRODataReader()
48 paths, files = reader.searchFilesOffLine(path=rconf.path,
61 paths, files = reader.searchFilesOffLine(path=rconf.path,
49 startDate=dt,
62 startDate=dt,
50 endDate=dt,
63 endDate=dt,
51 startTime=tm1,
64 startTime=tm1,
52 endTime=tm2,
65 endTime=tm2,
53 ext=DTYPES[rconf.datatype])
66 ext=DTYPES[rconf.datatype])
54 nFiles = len(files)
67 nFiles = len(files)
55 if nFiles == 0:
68 if nFiles == 0:
56 continue
69 continue
57 skip = int(math.ceil(nFiles / n))
70 skip = int(math.ceil(nFiles / n))
58 while nFiles > cursor * skip:
71 while nFiles > cursor * skip:
59 rconf.update(startDate=dt_str, endDate=dt_str, cursor=cursor,
72 rconf.update(startDate=dt_str, endDate=dt_str, cursor=cursor,
60 skip=skip)
73 skip=skip)
61 p = project.clone()
74 p = project.clone()
62 p.start()
75 p.start()
63 processes.append(p)
76 processes.append(p)
64 cursor += 1
77 cursor += 1
65
78
66 def beforeExit(exctype, value, trace):
79 def beforeExit(exctype, value, trace):
67 for process in processes:
80 for process in processes:
68 process.terminate()
81 process.terminate()
69 process.join()
82 process.join()
70 print(traceback.print_tb(trace))
83 print(traceback.print_tb(trace))
71
84
72 sys.excepthook = beforeExit
85 sys.excepthook = beforeExit
73
86
74 for process in processes:
87 for process in processes:
75 process.join()
88 process.join()
76 process.terminate()
89 process.terminate()
77
90
78 time.sleep(3)
91 time.sleep(3)
79
92
80
81 class ParameterConf():
93 class ParameterConf():
82
94
83 id = None
95 id = None
84 name = None
96 name = None
85 value = None
97 value = None
86 format = None
98 format = None
87
99
88 __formated_value = None
100 __formated_value = None
89
101
90 ELEMENTNAME = 'Parameter'
102 ELEMENTNAME = 'Parameter'
91
103
92 def __init__(self):
104 def __init__(self):
93
105
94 self.format = 'str'
106 self.format = 'str'
95
107
96 def getElementName(self):
108 def getElementName(self):
97
109
98 return self.ELEMENTNAME
110 return self.ELEMENTNAME
99
111
100 def getValue(self):
112 def getValue(self):
101
113
102 value = self.value
114 value = self.value
103 format = self.format
115 format = self.format
104
116
105 if self.__formated_value != None:
117 if self.__formated_value != None:
106
118
107 return self.__formated_value
119 return self.__formated_value
108
120
109 if format == 'obj':
121 if format == 'obj':
110 return value
122 return value
111
123
112 if format == 'str':
124 if format == 'str':
113 self.__formated_value = str(value)
125 self.__formated_value = str(value)
114 return self.__formated_value
126 return self.__formated_value
115
127
116 if value == '':
128 if value == '':
117 raise ValueError('%s: This parameter value is empty' % self.name)
129 raise ValueError('%s: This parameter value is empty' % self.name)
118
130
119 if format == 'list':
131 if format == 'list':
120 strList = value.split(',')
132 strList = value.split(',')
121
133
122 self.__formated_value = strList
134 self.__formated_value = strList
123
135
124 return self.__formated_value
136 return self.__formated_value
125
137
126 if format == 'intlist':
138 if format == 'intlist':
127 '''
139 '''
128 Example:
140 Example:
129 value = (0,1,2)
141 value = (0,1,2)
130 '''
142 '''
131
143
132 new_value = ast.literal_eval(value)
144 new_value = ast.literal_eval(value)
133
145
134 if type(new_value) not in (tuple, list):
146 if type(new_value) not in (tuple, list):
135 new_value = [int(new_value)]
147 new_value = [int(new_value)]
136
148
137 self.__formated_value = new_value
149 self.__formated_value = new_value
138
150
139 return self.__formated_value
151 return self.__formated_value
140
152
141 if format == 'floatlist':
153 if format == 'floatlist':
142 '''
154 '''
143 Example:
155 Example:
144 value = (0.5, 1.4, 2.7)
156 value = (0.5, 1.4, 2.7)
145 '''
157 '''
146
158
147 new_value = ast.literal_eval(value)
159 new_value = ast.literal_eval(value)
148
160
149 if type(new_value) not in (tuple, list):
161 if type(new_value) not in (tuple, list):
150 new_value = [float(new_value)]
162 new_value = [float(new_value)]
151
163
152 self.__formated_value = new_value
164 self.__formated_value = new_value
153
165
154 return self.__formated_value
166 return self.__formated_value
155
167
156 if format == 'date':
168 if format == 'date':
157 strList = value.split('/')
169 strList = value.split('/')
158 intList = [int(x) for x in strList]
170 intList = [int(x) for x in strList]
159 date = datetime.date(intList[0], intList[1], intList[2])
171 date = datetime.date(intList[0], intList[1], intList[2])
160
172
161 self.__formated_value = date
173 self.__formated_value = date
162
174
163 return self.__formated_value
175 return self.__formated_value
164
176
165 if format == 'time':
177 if format == 'time':
166 strList = value.split(':')
178 strList = value.split(':')
167 intList = [int(x) for x in strList]
179 intList = [int(x) for x in strList]
168 time = datetime.time(intList[0], intList[1], intList[2])
180 time = datetime.time(intList[0], intList[1], intList[2])
169
181
170 self.__formated_value = time
182 self.__formated_value = time
171
183
172 return self.__formated_value
184 return self.__formated_value
173
185
174 if format == 'pairslist':
186 if format == 'pairslist':
175 '''
187 '''
176 Example:
188 Example:
177 value = (0,1),(1,2)
189 value = (0,1),(1,2)
178 '''
190 '''
179
191
180 new_value = ast.literal_eval(value)
192 new_value = ast.literal_eval(value)
181
193
182 if type(new_value) not in (tuple, list):
194 if type(new_value) not in (tuple, list):
183 raise ValueError('%s has to be a tuple or list of pairs' % value)
195 raise ValueError('%s has to be a tuple or list of pairs' % value)
184
196
185 if type(new_value[0]) not in (tuple, list):
197 if type(new_value[0]) not in (tuple, list):
186 if len(new_value) != 2:
198 if len(new_value) != 2:
187 raise ValueError('%s has to be a tuple or list of pairs' % value)
199 raise ValueError('%s has to be a tuple or list of pairs' % value)
188 new_value = [new_value]
200 new_value = [new_value]
189
201
190 for thisPair in new_value:
202 for thisPair in new_value:
191 if len(thisPair) != 2:
203 if len(thisPair) != 2:
192 raise ValueError('%s has to be a tuple or list of pairs' % value)
204 raise ValueError('%s has to be a tuple or list of pairs' % value)
193
205
194 self.__formated_value = new_value
206 self.__formated_value = new_value
195
207
196 return self.__formated_value
208 return self.__formated_value
197
209
198 if format == 'multilist':
210 if format == 'multilist':
199 '''
211 '''
200 Example:
212 Example:
201 value = (0,1,2),(3,4,5)
213 value = (0,1,2),(3,4,5)
202 '''
214 '''
203 multiList = ast.literal_eval(value)
215 multiList = ast.literal_eval(value)
204
216
205 if type(multiList[0]) == int:
217 if type(multiList[0]) == int:
206 multiList = ast.literal_eval('(' + value + ')')
218 multiList = ast.literal_eval('(' + value + ')')
207
219
208 self.__formated_value = multiList
220 self.__formated_value = multiList
209
221
210 return self.__formated_value
222 return self.__formated_value
211
223
212 if format == 'bool':
224 if format == 'bool':
213 value = int(value)
225 value = int(value)
214
226
215 if format == 'int':
227 if format == 'int':
216 value = float(value)
228 value = float(value)
217
229
218 format_func = eval(format)
230 format_func = eval(format)
219
231
220 self.__formated_value = format_func(value)
232 self.__formated_value = format_func(value)
221
233
222 return self.__formated_value
234 return self.__formated_value
223
235
224 def updateId(self, new_id):
236 def updateId(self, new_id):
225
237
226 self.id = str(new_id)
238 self.id = str(new_id)
227
239
228 def setup(self, id, name, value, format='str'):
240 def setup(self, id, name, value, format='str'):
229 self.id = str(id)
241 self.id = str(id)
230 self.name = name
242 self.name = name
231 if format == 'obj':
243 if format == 'obj':
232 self.value = value
244 self.value = value
233 else:
245 else:
234 self.value = str(value)
246 self.value = str(value)
235 self.format = str.lower(format)
247 self.format = str.lower(format)
236
248
237 self.getValue()
249 self.getValue()
238
250
239 return 1
251 return 1
240
252
241 def update(self, name, value, format='str'):
253 def update(self, name, value, format='str'):
242
254
243 self.name = name
255 self.name = name
244 self.value = str(value)
256 self.value = str(value)
245 self.format = format
257 self.format = format
246
258
247 def makeXml(self, opElement):
259 def makeXml(self, opElement):
248 if self.name not in ('queue',):
260 if self.name not in ('queue',):
249 parmElement = SubElement(opElement, self.ELEMENTNAME)
261 parmElement = SubElement(opElement, self.ELEMENTNAME)
250 parmElement.set('id', str(self.id))
262 parmElement.set('id', str(self.id))
251 parmElement.set('name', self.name)
263 parmElement.set('name', self.name)
252 parmElement.set('value', self.value)
264 parmElement.set('value', self.value)
253 parmElement.set('format', self.format)
265 parmElement.set('format', self.format)
254
266
255 def readXml(self, parmElement):
267 def readXml(self, parmElement):
256
268
257 self.id = parmElement.get('id')
269 self.id = parmElement.get('id')
258 self.name = parmElement.get('name')
270 self.name = parmElement.get('name')
259 self.value = parmElement.get('value')
271 self.value = parmElement.get('value')
260 self.format = str.lower(parmElement.get('format'))
272 self.format = str.lower(parmElement.get('format'))
261
273
262 # Compatible with old signal chain version
274 # Compatible with old signal chain version
263 if self.format == 'int' and self.name == 'idfigure':
275 if self.format == 'int' and self.name == 'idfigure':
264 self.name = 'id'
276 self.name = 'id'
265
277
266 def printattr(self):
278 def printattr(self):
267
279
268 print('Parameter[%s]: name = %s, value = %s, format = %s' % (self.id, self.name, self.value, self.format))
280 print('Parameter[%s]: name = %s, value = %s, format = %s' % (self.id, self.name, self.value, self.format))
269
281
270
271 class OperationConf():
282 class OperationConf():
272
283
273 id = None
284 id = None
274 name = None
285 name = None
275 priority = None
286 priority = None
276 type = None
287 type = None
277
288
278 parmConfObjList = []
289 parmConfObjList = []
279
290
280 ELEMENTNAME = 'Operation'
291 ELEMENTNAME = 'Operation'
281
292
282 def __init__(self):
293 def __init__(self):
283
294
284 self.id = '0'
295 self.id = '0'
285 self.name = None
296 self.name = None
286 self.priority = None
297 self.priority = None
287 self.type = 'self'
298 self.topic = None
288
299
289 def __getNewId(self):
300 def __getNewId(self):
290
301
291 return int(self.id) * 10 + len(self.parmConfObjList) + 1
302 return int(self.id) * 10 + len(self.parmConfObjList) + 1
292
303
304 def getId(self):
305 return self.id
306
293 def updateId(self, new_id):
307 def updateId(self, new_id):
294
308
295 self.id = str(new_id)
309 self.id = str(new_id)
296
310
297 n = 1
311 n = 1
298 for parmObj in self.parmConfObjList:
312 for parmObj in self.parmConfObjList:
299
313
300 idParm = str(int(new_id) * 10 + n)
314 idParm = str(int(new_id) * 10 + n)
301 parmObj.updateId(idParm)
315 parmObj.updateId(idParm)
302
316
303 n += 1
317 n += 1
304
318
305 def getElementName(self):
319 def getElementName(self):
306
320
307 return self.ELEMENTNAME
321 return self.ELEMENTNAME
308
322
309 def getParameterObjList(self):
323 def getParameterObjList(self):
310
324
311 return self.parmConfObjList
325 return self.parmConfObjList
312
326
313 def getParameterObj(self, parameterName):
327 def getParameterObj(self, parameterName):
314
328
315 for parmConfObj in self.parmConfObjList:
329 for parmConfObj in self.parmConfObjList:
316
330
317 if parmConfObj.name != parameterName:
331 if parmConfObj.name != parameterName:
318 continue
332 continue
319
333
320 return parmConfObj
334 return parmConfObj
321
335
322 return None
336 return None
323
337
324 def getParameterObjfromValue(self, parameterValue):
338 def getParameterObjfromValue(self, parameterValue):
325
339
326 for parmConfObj in self.parmConfObjList:
340 for parmConfObj in self.parmConfObjList:
327
341
328 if parmConfObj.getValue() != parameterValue:
342 if parmConfObj.getValue() != parameterValue:
329 continue
343 continue
330
344
331 return parmConfObj.getValue()
345 return parmConfObj.getValue()
332
346
333 return None
347 return None
334
348
335 def getParameterValue(self, parameterName):
349 def getParameterValue(self, parameterName):
336
350
337 parameterObj = self.getParameterObj(parameterName)
351 parameterObj = self.getParameterObj(parameterName)
338
352
339 # if not parameterObj:
353 # if not parameterObj:
340 # return None
354 # return None
341
355
342 value = parameterObj.getValue()
356 value = parameterObj.getValue()
343
357
344 return value
358 return value
345
359
346 def getKwargs(self):
360 def getKwargs(self):
347
361
348 kwargs = {}
362 kwargs = {}
349
363
350 for parmConfObj in self.parmConfObjList:
364 for parmConfObj in self.parmConfObjList:
351 if self.name == 'run' and parmConfObj.name == 'datatype':
365 if self.name == 'run' and parmConfObj.name == 'datatype':
352 continue
366 continue
353
367
354 kwargs[parmConfObj.name] = parmConfObj.getValue()
368 kwargs[parmConfObj.name] = parmConfObj.getValue()
355
369
356 return kwargs
370 return kwargs
357
371
358 def setup(self, id, name, priority, type):
372 def setup(self, id, name, priority, type):
359
373
360 self.id = str(id)
374 self.id = str(id)
361 self.name = name
375 self.name = name
362 self.type = type
376 self.type = type
363 self.priority = priority
377 self.priority = priority
364
365 self.parmConfObjList = []
378 self.parmConfObjList = []
366
379
367 def removeParameters(self):
380 def removeParameters(self):
368
381
369 for obj in self.parmConfObjList:
382 for obj in self.parmConfObjList:
370 del obj
383 del obj
371
384
372 self.parmConfObjList = []
385 self.parmConfObjList = []
373
386
374 def addParameter(self, name, value, format='str'):
387 def addParameter(self, name, value, format='str'):
375
388
376 if value is None:
389 if value is None:
377 return None
390 return None
378 id = self.__getNewId()
391 id = self.__getNewId()
379
392
380 parmConfObj = ParameterConf()
393 parmConfObj = ParameterConf()
381 if not parmConfObj.setup(id, name, value, format):
394 if not parmConfObj.setup(id, name, value, format):
382 return None
395 return None
383
396
384 self.parmConfObjList.append(parmConfObj)
397 self.parmConfObjList.append(parmConfObj)
385
398
386 return parmConfObj
399 return parmConfObj
387
400
388 def changeParameter(self, name, value, format='str'):
401 def changeParameter(self, name, value, format='str'):
389
402
390 parmConfObj = self.getParameterObj(name)
403 parmConfObj = self.getParameterObj(name)
391 parmConfObj.update(name, value, format)
404 parmConfObj.update(name, value, format)
392
405
393 return parmConfObj
406 return parmConfObj
394
407
395 def makeXml(self, procUnitElement):
408 def makeXml(self, procUnitElement):
396
409
397 opElement = SubElement(procUnitElement, self.ELEMENTNAME)
410 opElement = SubElement(procUnitElement, self.ELEMENTNAME)
398 opElement.set('id', str(self.id))
411 opElement.set('id', str(self.id))
399 opElement.set('name', self.name)
412 opElement.set('name', self.name)
400 opElement.set('type', self.type)
413 opElement.set('type', self.type)
401 opElement.set('priority', str(self.priority))
414 opElement.set('priority', str(self.priority))
402
415
403 for parmConfObj in self.parmConfObjList:
416 for parmConfObj in self.parmConfObjList:
404 parmConfObj.makeXml(opElement)
417 parmConfObj.makeXml(opElement)
405
418
406 def readXml(self, opElement):
419 def readXml(self, opElement):
407
420
408 self.id = opElement.get('id')
421 self.id = opElement.get('id')
409 self.name = opElement.get('name')
422 self.name = opElement.get('name')
410 self.type = opElement.get('type')
423 self.type = opElement.get('type')
411 self.priority = opElement.get('priority')
424 self.priority = opElement.get('priority')
412
425
413 # Compatible with old signal chain version
426 # Compatible with old signal chain version
414 # Use of 'run' method instead 'init'
427 # Use of 'run' method instead 'init'
415 if self.type == 'self' and self.name == 'init':
428 if self.type == 'self' and self.name == 'init':
416 self.name = 'run'
429 self.name = 'run'
417
430
418 self.parmConfObjList = []
431 self.parmConfObjList = []
419
432
420 parmElementList = opElement.iter(ParameterConf().getElementName())
433 parmElementList = opElement.iter(ParameterConf().getElementName())
421
434
422 for parmElement in parmElementList:
435 for parmElement in parmElementList:
423 parmConfObj = ParameterConf()
436 parmConfObj = ParameterConf()
424 parmConfObj.readXml(parmElement)
437 parmConfObj.readXml(parmElement)
425
438
426 # Compatible with old signal chain version
439 # Compatible with old signal chain version
427 # If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
440 # If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
428 if self.type != 'self' and self.name == 'Plot':
441 if self.type != 'self' and self.name == 'Plot':
429 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
442 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
430 self.name = parmConfObj.value
443 self.name = parmConfObj.value
431 continue
444 continue
432
445
433 self.parmConfObjList.append(parmConfObj)
446 self.parmConfObjList.append(parmConfObj)
434
447
435 def printattr(self):
448 def printattr(self):
436
449
437 print('%s[%s]: name = %s, type = %s, priority = %s' % (self.ELEMENTNAME,
450 print('%s[%s]: name = %s, type = %s, priority = %s' % (self.ELEMENTNAME,
438 self.id,
451 self.id,
439 self.name,
452 self.name,
440 self.type,
453 self.type,
441 self.priority))
454 self.priority))
442
455
443 for parmConfObj in self.parmConfObjList:
456 for parmConfObj in self.parmConfObjList:
444 parmConfObj.printattr()
457 parmConfObj.printattr()
445
458
446 def createObject(self, plotter_queue=None):
459 def createObject(self):
447
448 if self.type == 'self':
449 raise ValueError('This operation type cannot be created')
450
460
451 if self.type == 'plotter':
461 className = eval(self.name)
452 if not plotter_queue:
462 kwargs = self.getKwargs()
453 raise ValueError('plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)')
454
455 opObj = Plotter(self.name, plotter_queue)
456
463
457 if self.type == 'external' or self.type == 'other':
464 opObj = className(self.id, **kwargs)
458
465
459 className = eval(self.name)
466 opObj.start()
460 kwargs = self.getKwargs()
461
467
462 opObj = className(**kwargs)
468 print(' Operation created')
463
469
464 return opObj
470 return opObj
465
471
466
467 class ProcUnitConf():
472 class ProcUnitConf():
468
473
469 id = None
474 id = None
470 name = None
475 name = None
471 datatype = None
476 datatype = None
472 inputId = None
477 inputId = None
473 parentId = None
478 parentId = None
474
479
475 opConfObjList = []
480 opConfObjList = []
476
481
477 procUnitObj = None
482 procUnitObj = None
478 opObjList = []
483 opObjList = []
479
484
480 ELEMENTNAME = 'ProcUnit'
485 ELEMENTNAME = 'ProcUnit'
481
486
482 def __init__(self):
487 def __init__(self):
483
488
484 self.id = None
489 self.id = None
485 self.datatype = None
490 self.datatype = None
486 self.name = None
491 self.name = None
487 self.inputId = None
492 self.inputId = None
488
493
489 self.opConfObjList = []
494 self.opConfObjList = []
490
495
491 self.procUnitObj = None
496 self.procUnitObj = None
492 self.opObjDict = {}
497 self.opObjDict = {}
493
498
494 def __getPriority(self):
499 def __getPriority(self):
495
500
496 return len(self.opConfObjList) + 1
501 return len(self.opConfObjList) + 1
497
502
498 def __getNewId(self):
503 def __getNewId(self):
499
504
500 return int(self.id) * 10 + len(self.opConfObjList) + 1
505 return int(self.id) * 10 + len(self.opConfObjList) + 1
501
506
502 def getElementName(self):
507 def getElementName(self):
503
508
504 return self.ELEMENTNAME
509 return self.ELEMENTNAME
505
510
506 def getId(self):
511 def getId(self):
507
512
508 return self.id
513 return self.id
509
514
510 def updateId(self, new_id, parentId=parentId):
515 def updateId(self, new_id, parentId=parentId):
511
516 '''
512 new_id = int(parentId) * 10 + (int(self.id) % 10)
517 new_id = int(parentId) * 10 + (int(self.id) % 10)
513 new_inputId = int(parentId) * 10 + (int(self.inputId) % 10)
518 new_inputId = int(parentId) * 10 + (int(self.inputId) % 10)
514
519
515 # If this proc unit has not inputs
520 # If this proc unit has not inputs
516 if self.inputId == '0':
521 #if self.inputId == '0':
517 new_inputId = 0
522 #new_inputId = 0
518
523
519 n = 1
524 n = 1
520 for opConfObj in self.opConfObjList:
525 for opConfObj in self.opConfObjList:
521
526
522 idOp = str(int(new_id) * 10 + n)
527 idOp = str(int(new_id) * 10 + n)
523 opConfObj.updateId(idOp)
528 opConfObj.updateId(idOp)
524
529
525 n += 1
530 n += 1
526
531
527 self.parentId = str(parentId)
532 self.parentId = str(parentId)
528 self.id = str(new_id)
533 self.id = str(new_id)
529 self.inputId = str(new_inputId)
534 #self.inputId = str(new_inputId)
530
535 '''
536 n = 1
531 def getInputId(self):
537 def getInputId(self):
532
538
533 return self.inputId
539 return self.inputId
534
540
535 def getOperationObjList(self):
541 def getOperationObjList(self):
536
542
537 return self.opConfObjList
543 return self.opConfObjList
538
544
539 def getOperationObj(self, name=None):
545 def getOperationObj(self, name=None):
540
546
541 for opConfObj in self.opConfObjList:
547 for opConfObj in self.opConfObjList:
542
548
543 if opConfObj.name != name:
549 if opConfObj.name != name:
544 continue
550 continue
545
551
546 return opConfObj
552 return opConfObj
547
553
548 return None
554 return None
549
555
550 def getOpObjfromParamValue(self, value=None):
556 def getOpObjfromParamValue(self, value=None):
551
557
552 for opConfObj in self.opConfObjList:
558 for opConfObj in self.opConfObjList:
553 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
559 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
554 continue
560 continue
555 return opConfObj
561 return opConfObj
556 return None
562 return None
557
563
558 def getProcUnitObj(self):
564 def getProcUnitObj(self):
559
565
560 return self.procUnitObj
566 return self.procUnitObj
561
567
562 def setup(self, id, name, datatype, inputId, parentId=None):
568 def setup(self, id, name, datatype, inputId, parentId=None):
563
569 '''
570 id sera el topico a publicar
571 inputId sera el topico a subscribirse
572 '''
573
564 # Compatible with old signal chain version
574 # Compatible with old signal chain version
565 if datatype == None and name == None:
575 if datatype == None and name == None:
566 raise ValueError('datatype or name should be defined')
576 raise ValueError('datatype or name should be defined')
567
577
578 #Definir una condicion para inputId cuando sea 0
579
568 if name == None:
580 if name == None:
569 if 'Proc' in datatype:
581 if 'Proc' in datatype:
570 name = datatype
582 name = datatype
571 else:
583 else:
572 name = '%sProc' % (datatype)
584 name = '%sProc' % (datatype)
573
585
574 if datatype == None:
586 if datatype == None:
575 datatype = name.replace('Proc', '')
587 datatype = name.replace('Proc', '')
576
588
577 self.id = str(id)
589 self.id = str(id)
578 self.name = name
590 self.name = name
579 self.datatype = datatype
591 self.datatype = datatype
580 self.inputId = inputId
592 self.inputId = inputId
581 self.parentId = parentId
593 self.parentId = parentId
582
583 self.opConfObjList = []
594 self.opConfObjList = []
584
595
585 self.addOperation(name='run', optype='self')
596 self.addOperation(name='run', optype='self')
586
597
587 def removeOperations(self):
598 def removeOperations(self):
588
599
589 for obj in self.opConfObjList:
600 for obj in self.opConfObjList:
590 del obj
601 del obj
591
602
592 self.opConfObjList = []
603 self.opConfObjList = []
593 self.addOperation(name='run')
604 self.addOperation(name='run')
594
605
595 def addParameter(self, **kwargs):
606 def addParameter(self, **kwargs):
596 '''
607 '''
597 Add parameters to 'run' operation
608 Add parameters to 'run' operation
598 '''
609 '''
599 opObj = self.opConfObjList[0]
610 opObj = self.opConfObjList[0]
600
611
601 opObj.addParameter(**kwargs)
612 opObj.addParameter(**kwargs)
602
613
603 return opObj
614 return opObj
604
615
605 def addOperation(self, name, optype='self'):
616 def addOperation(self, name, optype = 'self'):
617 '''
618 Actualizacion - > proceso comunicacion
619 En el caso de optype='self', elminar. DEfinir comuncacion IPC -> Topic
620 definir el tipoc de socket o comunicacion ipc++
621
622 '''
606
623
607 id = self.__getNewId()
624 id = self.__getNewId()
608 priority = self.__getPriority()
625 priority = self.__getPriority() # Sin mucho sentido, pero puede usarse
609
626
610 opConfObj = OperationConf()
627 opConfObj = OperationConf()
611 opConfObj.setup(id, name=name, priority=priority, type=optype)
628 opConfObj.setup(id, name=name, priority=priority, type=optype)
612
629
613 self.opConfObjList.append(opConfObj)
630 self.opConfObjList.append(opConfObj)
614
631
615 return opConfObj
632 return opConfObj
616
633
617 def makeXml(self, projectElement):
634 def makeXml(self, projectElement):
618
635
619 procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
636 procUnitElement = SubElement(projectElement, self.ELEMENTNAME)
620 procUnitElement.set('id', str(self.id))
637 procUnitElement.set('id', str(self.id))
621 procUnitElement.set('name', self.name)
638 procUnitElement.set('name', self.name)
622 procUnitElement.set('datatype', self.datatype)
639 procUnitElement.set('datatype', self.datatype)
623 procUnitElement.set('inputId', str(self.inputId))
640 procUnitElement.set('inputId', str(self.inputId))
624
641
625 for opConfObj in self.opConfObjList:
642 for opConfObj in self.opConfObjList:
626 opConfObj.makeXml(procUnitElement)
643 opConfObj.makeXml(procUnitElement)
627
644
628 def readXml(self, upElement):
645 def readXml(self, upElement):
629
646
630 self.id = upElement.get('id')
647 self.id = upElement.get('id')
631 self.name = upElement.get('name')
648 self.name = upElement.get('name')
632 self.datatype = upElement.get('datatype')
649 self.datatype = upElement.get('datatype')
633 self.inputId = upElement.get('inputId')
650 self.inputId = upElement.get('inputId')
634
651
635 if self.ELEMENTNAME == 'ReadUnit':
652 if self.ELEMENTNAME == 'ReadUnit':
636 self.datatype = self.datatype.replace('Reader', '')
653 self.datatype = self.datatype.replace('Reader', '')
637
654
638 if self.ELEMENTNAME == 'ProcUnit':
655 if self.ELEMENTNAME == 'ProcUnit':
639 self.datatype = self.datatype.replace('Proc', '')
656 self.datatype = self.datatype.replace('Proc', '')
640
657
641 if self.inputId == 'None':
658 if self.inputId == 'None':
642 self.inputId = '0'
659 self.inputId = '0'
643
660
644 self.opConfObjList = []
661 self.opConfObjList = []
645
662
646 opElementList = upElement.iter(OperationConf().getElementName())
663 opElementList = upElement.iter(OperationConf().getElementName())
647
664
648 for opElement in opElementList:
665 for opElement in opElementList:
649 opConfObj = OperationConf()
666 opConfObj = OperationConf()
650 opConfObj.readXml(opElement)
667 opConfObj.readXml(opElement)
651 self.opConfObjList.append(opConfObj)
668 self.opConfObjList.append(opConfObj)
652
669
653 def printattr(self):
670 def printattr(self):
654
671
655 print('%s[%s]: name = %s, datatype = %s, inputId = %s' % (self.ELEMENTNAME,
672 print('%s[%s]: name = %s, datatype = %s, inputId = %s' % (self.ELEMENTNAME,
656 self.id,
673 self.id,
657 self.name,
674 self.name,
658 self.datatype,
675 self.datatype,
659 self.inputId))
676 self.inputId))
660
677
661 for opConfObj in self.opConfObjList:
678 for opConfObj in self.opConfObjList:
662 opConfObj.printattr()
679 opConfObj.printattr()
663
680
664 def getKwargs(self):
681 def getKwargs(self):
665
682
666 opObj = self.opConfObjList[0]
683 opObj = self.opConfObjList[0]
667 kwargs = opObj.getKwargs()
684 kwargs = opObj.getKwargs()
668
685
669 return kwargs
686 return kwargs
670
687
671 def createObjects(self, plotter_queue=None):
688 def createObjects(self, dictUnits):
689 '''
690 Instancia de unidades de procesamiento.
672
691
692 '''
673 className = eval(self.name)
693 className = eval(self.name)
674 kwargs = self.getKwargs()
694 kwargs = self.getKwargs()
675 procUnitObj = className(**kwargs)
695 procUnitObj = className(self.id, self.inputId, dictUnits, **kwargs) # necesitan saber su id y su entrada por fines de ipc
696
676
697
677 for opConfObj in self.opConfObjList:
698 for opConfObj in self.opConfObjList:
678
699
679 if opConfObj.type == 'self' and self.name == 'run':
700 if opConfObj.type == 'self' and self.name == 'run':
680 continue
701 continue
681 elif opConfObj.type == 'self':
702 elif opConfObj.type == 'self':
682 procUnitObj.addOperationKwargs(
703 procUnitObj.addOperationKwargs(
683 opConfObj.id, **opConfObj.getKwargs())
704 opConfObj.id, **opConfObj.getKwargs())
684 continue
705 continue
685
706 print("Creating operation process:", opConfObj.name, "for", self.name)
686 opObj = opConfObj.createObject(plotter_queue)
707 opObj = opConfObj.createObject()
687
708
688 self.opObjDict[opConfObj.id] = opObj
709
689
710 #self.opObjDict[opConfObj.id] = opObj.name
690 procUnitObj.addOperation(opObj, opConfObj.id)
711
712 procUnitObj.addOperation(opConfObj.name, opConfObj.id)
713
714 procUnitObj.start()
691
715
692 self.procUnitObj = procUnitObj
716 self.procUnitObj = procUnitObj
717
693
718
694 return procUnitObj
719 return procUnitObj
695
720
696 def run(self):
721 def run(self):
697
722
698 is_ok = False
723 is_ok = True
699
724 """
700 for opConfObj in self.opConfObjList:
725 for opConfObj in self.opConfObjList:
701
726
702 kwargs = {}
727 kwargs = {}
703 for parmConfObj in opConfObj.getParameterObjList():
728 for parmConfObj in opConfObj.getParameterObjList():
704 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
729 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
705 continue
730 continue
706
731
707 kwargs[parmConfObj.name] = parmConfObj.getValue()
732 kwargs[parmConfObj.name] = parmConfObj.getValue()
708
733
709 sts = self.procUnitObj.call(opType=opConfObj.type,
734 sts = self.procUnitObj.call(opType=opConfObj.type,
710 opName=opConfObj.name,
735 opName=opConfObj.name,
711 opId=opConfObj.id)
736 opId=opConfObj.id)
712
737
713 is_ok = is_ok or sts
738 is_ok = is_ok or sts
714
739
740 """
715 return is_ok
741 return is_ok
716
742
743
717 def close(self):
744 def close(self):
718
745
719 for opConfObj in self.opConfObjList:
746 for opConfObj in self.opConfObjList:
720 if opConfObj.type == 'self':
747 if opConfObj.type == 'self':
721 continue
748 continue
722
749
723 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
750 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
724 opObj.close()
751 opObj.close()
725
752
726 self.procUnitObj.close()
753 self.procUnitObj.close()
727
754
728 return
755 return
729
756
730
757
731 class ReadUnitConf(ProcUnitConf):
758 class ReadUnitConf(ProcUnitConf):
732
759
733 path = None
760 path = None
734 startDate = None
761 startDate = None
735 endDate = None
762 endDate = None
736 startTime = None
763 startTime = None
737 endTime = None
764 endTime = None
738
765
739 ELEMENTNAME = 'ReadUnit'
766 ELEMENTNAME = 'ReadUnit'
740
767
741 def __init__(self):
768 def __init__(self):
742
769
743 self.id = None
770 self.id = None
744 self.datatype = None
771 self.datatype = None
745 self.name = None
772 self.name = None
746 self.inputId = None
773 self.inputId = None
747
774
748 self.parentId = None
775 self.parentId = None
749
776
750 self.opConfObjList = []
777 self.opConfObjList = []
751 self.opObjList = []
778 self.opObjList = []
752
779
753 def getElementName(self):
780 def getElementName(self):
754
781
755 return self.ELEMENTNAME
782 return self.ELEMENTNAME
756
783
757 def setup(self, id, name, datatype, path='', startDate='', endDate='',
784 def setup(self, id, name, datatype, path='', startDate='', endDate='',
758 startTime='', endTime='', parentId=None, server=None, **kwargs):
785 startTime='', endTime='', parentId=None, server=None, **kwargs):
759
786
787
788 '''
789 *****el id del proceso sera el Topico
790
791 Adicion de {topic}, si no esta presente -> error
792 kwargs deben ser trasmitidos en la instanciacion
793
794 '''
795
760 # Compatible with old signal chain version
796 # Compatible with old signal chain version
761 if datatype == None and name == None:
797 if datatype == None and name == None:
762 raise ValueError('datatype or name should be defined')
798 raise ValueError('datatype or name should be defined')
763 if name == None:
799 if name == None:
764 if 'Reader' in datatype:
800 if 'Reader' in datatype:
765 name = datatype
801 name = datatype
766 datatype = name.replace('Reader','')
802 datatype = name.replace('Reader','')
767 else:
803 else:
768 name = '{}Reader'.format(datatype)
804 name = '{}Reader'.format(datatype)
769 if datatype == None:
805 if datatype == None:
770 if 'Reader' in name:
806 if 'Reader' in name:
771 datatype = name.replace('Reader','')
807 datatype = name.replace('Reader','')
772 else:
808 else:
773 datatype = name
809 datatype = name
774 name = '{}Reader'.format(name)
810 name = '{}Reader'.format(name)
775
811
776 self.id = id
812 self.id = id
777 self.name = name
813 self.name = name
778 self.datatype = datatype
814 self.datatype = datatype
779 if path != '':
815 if path != '':
780 self.path = os.path.abspath(path)
816 self.path = os.path.abspath(path)
781 self.startDate = startDate
817 self.startDate = startDate
782 self.endDate = endDate
818 self.endDate = endDate
783 self.startTime = startTime
819 self.startTime = startTime
784 self.endTime = endTime
820 self.endTime = endTime
785 self.inputId = '0'
821 self.inputId = '0'
786 self.parentId = parentId
822 self.parentId = parentId
787 self.server = server
823 self.server = server
788 self.addRunOperation(**kwargs)
824 self.addRunOperation(**kwargs)
789
825
790 def update(self, **kwargs):
826 def update(self, **kwargs):
791
827
792 if 'datatype' in kwargs:
828 if 'datatype' in kwargs:
793 datatype = kwargs.pop('datatype')
829 datatype = kwargs.pop('datatype')
794 if 'Reader' in datatype:
830 if 'Reader' in datatype:
795 self.name = datatype
831 self.name = datatype
796 else:
832 else:
797 self.name = '%sReader' % (datatype)
833 self.name = '%sReader' % (datatype)
798 self.datatype = self.name.replace('Reader', '')
834 self.datatype = self.name.replace('Reader', '')
799
835
800 attrs = ('path', 'startDate', 'endDate',
836 attrs = ('path', 'startDate', 'endDate',
801 'startTime', 'endTime', 'parentId')
837 'startTime', 'endTime', 'parentId')
802
838
803 for attr in attrs:
839 for attr in attrs:
804 if attr in kwargs:
840 if attr in kwargs:
805 setattr(self, attr, kwargs.pop(attr))
841 setattr(self, attr, kwargs.pop(attr))
806
842
807 self.inputId = '0'
843 self.inputId = '0'
808 self.updateRunOperation(**kwargs)
844 self.updateRunOperation(**kwargs)
809
845
810 def removeOperations(self):
846 def removeOperations(self):
811
847
812 for obj in self.opConfObjList:
848 for obj in self.opConfObjList:
813 del obj
849 del obj
814
850
815 self.opConfObjList = []
851 self.opConfObjList = []
816
852
817 def addRunOperation(self, **kwargs):
853 def addRunOperation(self, **kwargs):
818
854
819 opObj = self.addOperation(name='run', optype='self')
855 opObj = self.addOperation(name='run', optype='self')
820
856
821 if self.server is None:
857 if self.server is None:
822 opObj.addParameter(
858 opObj.addParameter(
823 name='datatype', value=self.datatype, format='str')
859 name='datatype', value=self.datatype, format='str')
824 opObj.addParameter(name='path', value=self.path, format='str')
860 opObj.addParameter(name='path', value=self.path, format='str')
825 opObj.addParameter(
861 opObj.addParameter(
826 name='startDate', value=self.startDate, format='date')
862 name='startDate', value=self.startDate, format='date')
827 opObj.addParameter(
863 opObj.addParameter(
828 name='endDate', value=self.endDate, format='date')
864 name='endDate', value=self.endDate, format='date')
829 opObj.addParameter(
865 opObj.addParameter(
830 name='startTime', value=self.startTime, format='time')
866 name='startTime', value=self.startTime, format='time')
831 opObj.addParameter(
867 opObj.addParameter(
832 name='endTime', value=self.endTime, format='time')
868 name='endTime', value=self.endTime, format='time')
833
869
834 for key, value in list(kwargs.items()):
870 for key, value in list(kwargs.items()):
835 opObj.addParameter(name=key, value=value,
871 opObj.addParameter(name=key, value=value,
836 format=type(value).__name__)
872 format=type(value).__name__)
837 else:
873 else:
838 opObj.addParameter(name='server', value=self.server, format='str')
874 opObj.addParameter(name='server', value=self.server, format='str')
839
875
840 return opObj
876 return opObj
841
877
842 def updateRunOperation(self, **kwargs):
878 def updateRunOperation(self, **kwargs):
843
879
844 opObj = self.getOperationObj(name='run')
880 opObj = self.getOperationObj(name='run')
845 opObj.removeParameters()
881 opObj.removeParameters()
846
882
847 opObj.addParameter(name='datatype', value=self.datatype, format='str')
883 opObj.addParameter(name='datatype', value=self.datatype, format='str')
848 opObj.addParameter(name='path', value=self.path, format='str')
884 opObj.addParameter(name='path', value=self.path, format='str')
849 opObj.addParameter(
885 opObj.addParameter(
850 name='startDate', value=self.startDate, format='date')
886 name='startDate', value=self.startDate, format='date')
851 opObj.addParameter(name='endDate', value=self.endDate, format='date')
887 opObj.addParameter(name='endDate', value=self.endDate, format='date')
852 opObj.addParameter(
888 opObj.addParameter(
853 name='startTime', value=self.startTime, format='time')
889 name='startTime', value=self.startTime, format='time')
854 opObj.addParameter(name='endTime', value=self.endTime, format='time')
890 opObj.addParameter(name='endTime', value=self.endTime, format='time')
855
891
856 for key, value in list(kwargs.items()):
892 for key, value in list(kwargs.items()):
857 opObj.addParameter(name=key, value=value,
893 opObj.addParameter(name=key, value=value,
858 format=type(value).__name__)
894 format=type(value).__name__)
859
895
860 return opObj
896 return opObj
861
897
862 def readXml(self, upElement):
898 def readXml(self, upElement):
863
899
864 self.id = upElement.get('id')
900 self.id = upElement.get('id')
865 self.name = upElement.get('name')
901 self.name = upElement.get('name')
866 self.datatype = upElement.get('datatype')
902 self.datatype = upElement.get('datatype')
867 self.inputId = upElement.get('inputId')
903 self.inputId = upElement.get('inputId')
868
904
869 if self.ELEMENTNAME == 'ReadUnit':
905 if self.ELEMENTNAME == 'ReadUnit':
870 self.datatype = self.datatype.replace('Reader', '')
906 self.datatype = self.datatype.replace('Reader', '')
871
907
872 if self.inputId == 'None':
908 if self.inputId == 'None':
873 self.inputId = '0'
909 self.inputId = '0'
874
910
875 self.opConfObjList = []
911 self.opConfObjList = []
876
912
877 opElementList = upElement.iter(OperationConf().getElementName())
913 opElementList = upElement.iter(OperationConf().getElementName())
878
914
879 for opElement in opElementList:
915 for opElement in opElementList:
880 opConfObj = OperationConf()
916 opConfObj = OperationConf()
881 opConfObj.readXml(opElement)
917 opConfObj.readXml(opElement)
882 self.opConfObjList.append(opConfObj)
918 self.opConfObjList.append(opConfObj)
883
919
884 if opConfObj.name == 'run':
920 if opConfObj.name == 'run':
885 self.path = opConfObj.getParameterValue('path')
921 self.path = opConfObj.getParameterValue('path')
886 self.startDate = opConfObj.getParameterValue('startDate')
922 self.startDate = opConfObj.getParameterValue('startDate')
887 self.endDate = opConfObj.getParameterValue('endDate')
923 self.endDate = opConfObj.getParameterValue('endDate')
888 self.startTime = opConfObj.getParameterValue('startTime')
924 self.startTime = opConfObj.getParameterValue('startTime')
889 self.endTime = opConfObj.getParameterValue('endTime')
925 self.endTime = opConfObj.getParameterValue('endTime')
890
926
891
927
892 class Project(Process):
928 class Project(Process):
893
929
894 id = None
930 id = None
895 # name = None
896 description = None
931 description = None
897 filename = None
932 filename = None
898
933
899 procUnitConfObjDict = None
934 procUnitConfObjDict = None
900
935
901 ELEMENTNAME = 'Project'
936 ELEMENTNAME = 'Project'
902
937
903 plotterQueue = None
938
904
939
905 def __init__(self, plotter_queue=None):
940 def __init__(self):
906
941
907 Process.__init__(self)
942 Process.__init__(self)
908 self.id = None
943 self.id = None
909 self.description = None
944 self.description = None
910 self.email = None
945 self.email = None
911 self.alarm = None
946 self.alarm = None
912 self.plotterQueue = plotter_queue
913 self.procUnitConfObjDict = {}
947 self.procUnitConfObjDict = {}
914
948
915 def __getNewId(self):
949 def __getNewId(self):
916
950
917 idList = list(self.procUnitConfObjDict.keys())
951 idList = list(self.procUnitConfObjDict.keys())
918
952
919 id = int(self.id) * 10
953 id = int(self.id) * 10
920
954
921 while True:
955 while True:
922 id += 1
956 id += 1
923
957
924 if str(id) in idList:
958 if str(id) in idList:
925 continue
959 continue
926
960
927 break
961 break
928
962
929 return str(id)
963 return str(id)
930
964
931 def getElementName(self):
965 def getElementName(self):
932
966
933 return self.ELEMENTNAME
967 return self.ELEMENTNAME
934
968
935 def getId(self):
969 def getId(self):
936
970
937 return self.id
971 return self.id
938
972
939 def updateId(self, new_id):
973 def updateId(self, new_id):
940
974
941 self.id = str(new_id)
975 self.id = str(new_id)
942
976
943 keyList = list(self.procUnitConfObjDict.keys())
977 keyList = list(self.procUnitConfObjDict.keys())
944 keyList.sort()
978 keyList.sort()
945
979
946 n = 1
980 n = 1
947 newProcUnitConfObjDict = {}
981 newProcUnitConfObjDict = {}
948
982
949 for procKey in keyList:
983 for procKey in keyList:
950
984
951 procUnitConfObj = self.procUnitConfObjDict[procKey]
985 procUnitConfObj = self.procUnitConfObjDict[procKey]
952 idProcUnit = str(int(self.id) * 10 + n)
986 idProcUnit = str(int(self.id) * 10 + n)
953 procUnitConfObj.updateId(idProcUnit, parentId=self.id)
987 procUnitConfObj.updateId(idProcUnit, parentId=self.id)
954 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
988 newProcUnitConfObjDict[idProcUnit] = procUnitConfObj
955 n += 1
989 n += 1
956
990
957 self.procUnitConfObjDict = newProcUnitConfObjDict
991 self.procUnitConfObjDict = newProcUnitConfObjDict
958
992
959 def setup(self, id, name='', description='', email=None, alarm=[]):
993 def setup(self, id, name='', description='', email=None, alarm=[]):
960
994
961 print()
995 print(' ')
962 print('*' * 60)
996 print('*' * 60)
963 print(' Starting SIGNAL CHAIN PROCESSING v%s ' % schainpy.__version__)
997 print('* Starting SIGNAL CHAIN PROCESSING (Multiprocessing) v%s *' % schainpy.__version__)
964 print('*' * 60)
998 print('*' * 60)
965 print()
999 print("* Python " + python_version() + " *")
1000 print('*' * 19)
1001 print(' ')
966 self.id = str(id)
1002 self.id = str(id)
967 self.description = description
1003 self.description = description
968 self.email = email
1004 self.email = email
969 self.alarm = alarm
1005 self.alarm = alarm
970
1006
971 def update(self, **kwargs):
1007 def update(self, **kwargs):
972
1008
973 for key, value in list(kwargs.items()):
1009 for key, value in list(kwargs.items()):
974 setattr(self, key, value)
1010 setattr(self, key, value)
975
1011
976 def clone(self):
1012 def clone(self):
977
1013
978 p = Project()
1014 p = Project()
979 p.procUnitConfObjDict = self.procUnitConfObjDict
1015 p.procUnitConfObjDict = self.procUnitConfObjDict
980 return p
1016 return p
981
1017
982 def addReadUnit(self, id=None, datatype=None, name=None, **kwargs):
1018 def addReadUnit(self, id=None, datatype=None, name=None, **kwargs):
983
1019
1020 '''
1021 Actualizacion:
1022 Se agrego un nuevo argumento: topic -relativo a la forma de comunicar los procesos simultaneos
1023
1024 * El id del proceso sera el topico al que se deben subscribir los procUnits para recibir la informacion(data)
1025
1026 '''
1027
984 if id is None:
1028 if id is None:
985 idReadUnit = self.__getNewId()
1029 idReadUnit = self.__getNewId()
986 else:
1030 else:
987 idReadUnit = str(id)
1031 idReadUnit = str(id)
988
1032
989 readUnitConfObj = ReadUnitConf()
1033 readUnitConfObj = ReadUnitConf()
990 readUnitConfObj.setup(idReadUnit, name, datatype,
1034 readUnitConfObj.setup(idReadUnit, name, datatype,
991 parentId=self.id, **kwargs)
1035 parentId=self.id, **kwargs)
992
1036
993 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
1037 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
994
1038
995 return readUnitConfObj
1039 return readUnitConfObj
996
1040
997 def addProcUnit(self, inputId='0', datatype=None, name=None):
1041 def addProcUnit(self, inputId='0', datatype=None, name=None):
998
1042
999 idProcUnit = self.__getNewId()
1043 '''
1044 Actualizacion:
1045 Se agrego dos nuevos argumentos: topic_read (lee data de otro procUnit) y topic_write(escribe o envia data a otro procUnit)
1046 Deberia reemplazar a "inputId"
1047
1048 ** A fin de mantener el inputID, este sera la representaacion del topicoal que deben subscribirse. El ID propio de la intancia
1049 (proceso) sera el topico de la publicacion, todo sera asignado de manera dinamica.
1050
1051 '''
1052
1053 idProcUnit = self.__getNewId() #Topico para subscripcion
1000
1054
1001 procUnitConfObj = ProcUnitConf()
1055 procUnitConfObj = ProcUnitConf()
1002 procUnitConfObj.setup(idProcUnit, name, datatype,
1056 procUnitConfObj.setup(idProcUnit, name, datatype, inputId, #topic_read, topic_write,
1003 inputId, parentId=self.id)
1057 parentId=self.id)
1004
1058
1005 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1059 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1006
1060
1007 return procUnitConfObj
1061 return procUnitConfObj
1008
1062
1009 def removeProcUnit(self, id):
1063 def removeProcUnit(self, id):
1010
1064
1011 if id in list(self.procUnitConfObjDict.keys()):
1065 if id in list(self.procUnitConfObjDict.keys()):
1012 self.procUnitConfObjDict.pop(id)
1066 self.procUnitConfObjDict.pop(id)
1013
1067
1014 def getReadUnitId(self):
1068 def getReadUnitId(self):
1015
1069
1016 readUnitConfObj = self.getReadUnitObj()
1070 readUnitConfObj = self.getReadUnitObj()
1017
1071
1018 return readUnitConfObj.id
1072 return readUnitConfObj.id
1019
1073
1020 def getReadUnitObj(self):
1074 def getReadUnitObj(self):
1021
1075
1022 for obj in list(self.procUnitConfObjDict.values()):
1076 for obj in list(self.procUnitConfObjDict.values()):
1023 if obj.getElementName() == 'ReadUnit':
1077 if obj.getElementName() == 'ReadUnit':
1024 return obj
1078 return obj
1025
1079
1026 return None
1080 return None
1027
1081
1028 def getProcUnitObj(self, id=None, name=None):
1082 def getProcUnitObj(self, id=None, name=None):
1029
1083
1030 if id != None:
1084 if id != None:
1031 return self.procUnitConfObjDict[id]
1085 return self.procUnitConfObjDict[id]
1032
1086
1033 if name != None:
1087 if name != None:
1034 return self.getProcUnitObjByName(name)
1088 return self.getProcUnitObjByName(name)
1035
1089
1036 return None
1090 return None
1037
1091
1038 def getProcUnitObjByName(self, name):
1092 def getProcUnitObjByName(self, name):
1039
1093
1040 for obj in list(self.procUnitConfObjDict.values()):
1094 for obj in list(self.procUnitConfObjDict.values()):
1041 if obj.name == name:
1095 if obj.name == name:
1042 return obj
1096 return obj
1043
1097
1044 return None
1098 return None
1045
1099
1046 def procUnitItems(self):
1100 def procUnitItems(self):
1047
1101
1048 return list(self.procUnitConfObjDict.items())
1102 return list(self.procUnitConfObjDict.items())
1049
1103
1050 def makeXml(self):
1104 def makeXml(self):
1051
1105
1052 projectElement = Element('Project')
1106 projectElement = Element('Project')
1053 projectElement.set('id', str(self.id))
1107 projectElement.set('id', str(self.id))
1054 projectElement.set('name', self.name)
1108 projectElement.set('name', self.name)
1055 projectElement.set('description', self.description)
1109 projectElement.set('description', self.description)
1056
1110
1057 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1111 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1058 procUnitConfObj.makeXml(projectElement)
1112 procUnitConfObj.makeXml(projectElement)
1059
1113
1060 self.projectElement = projectElement
1114 self.projectElement = projectElement
1061
1115
1062 def writeXml(self, filename=None):
1116 def writeXml(self, filename=None):
1063
1117
1064 if filename == None:
1118 if filename == None:
1065 if self.filename:
1119 if self.filename:
1066 filename = self.filename
1120 filename = self.filename
1067 else:
1121 else:
1068 filename = 'schain.xml'
1122 filename = 'schain.xml'
1069
1123
1070 if not filename:
1124 if not filename:
1071 print('filename has not been defined. Use setFilename(filename) for do it.')
1125 print('filename has not been defined. Use setFilename(filename) for do it.')
1072 return 0
1126 return 0
1073
1127
1074 abs_file = os.path.abspath(filename)
1128 abs_file = os.path.abspath(filename)
1075
1129
1076 if not os.access(os.path.dirname(abs_file), os.W_OK):
1130 if not os.access(os.path.dirname(abs_file), os.W_OK):
1077 print('No write permission on %s' % os.path.dirname(abs_file))
1131 print('No write permission on %s' % os.path.dirname(abs_file))
1078 return 0
1132 return 0
1079
1133
1080 if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)):
1134 if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)):
1081 print('File %s already exists and it could not be overwriten' % abs_file)
1135 print('File %s already exists and it could not be overwriten' % abs_file)
1082 return 0
1136 return 0
1083
1137
1084 self.makeXml()
1138 self.makeXml()
1085
1139
1086 ElementTree(self.projectElement).write(abs_file, method='xml')
1140 ElementTree(self.projectElement).write(abs_file, method='xml')
1087
1141
1088 self.filename = abs_file
1142 self.filename = abs_file
1089
1143
1090 return 1
1144 return 1
1091
1145
1092 def readXml(self, filename=None):
1146 def readXml(self, filename=None):
1093
1147
1094 if not filename:
1148 if not filename:
1095 print('filename is not defined')
1149 print('filename is not defined')
1096 return 0
1150 return 0
1097
1151
1098 abs_file = os.path.abspath(filename)
1152 abs_file = os.path.abspath(filename)
1099
1153
1100 if not os.path.isfile(abs_file):
1154 if not os.path.isfile(abs_file):
1101 print('%s file does not exist' % abs_file)
1155 print('%s file does not exist' % abs_file)
1102 return 0
1156 return 0
1103
1157
1104 self.projectElement = None
1158 self.projectElement = None
1105 self.procUnitConfObjDict = {}
1159 self.procUnitConfObjDict = {}
1106
1160
1107 try:
1161 try:
1108 self.projectElement = ElementTree().parse(abs_file)
1162 self.projectElement = ElementTree().parse(abs_file)
1109 except:
1163 except:
1110 print('Error reading %s, verify file format' % filename)
1164 print('Error reading %s, verify file format' % filename)
1111 return 0
1165 return 0
1112
1166
1113 self.project = self.projectElement.tag
1167 self.project = self.projectElement.tag
1114
1168
1115 self.id = self.projectElement.get('id')
1169 self.id = self.projectElement.get('id')
1116 self.name = self.projectElement.get('name')
1170 self.name = self.projectElement.get('name')
1117 self.description = self.projectElement.get('description')
1171 self.description = self.projectElement.get('description')
1118
1172
1119 readUnitElementList = self.projectElement.iter(
1173 readUnitElementList = self.projectElement.iter(
1120 ReadUnitConf().getElementName())
1174 ReadUnitConf().getElementName())
1121
1175
1122 for readUnitElement in readUnitElementList:
1176 for readUnitElement in readUnitElementList:
1123 readUnitConfObj = ReadUnitConf()
1177 readUnitConfObj = ReadUnitConf()
1124 readUnitConfObj.readXml(readUnitElement)
1178 readUnitConfObj.readXml(readUnitElement)
1125
1179
1126 if readUnitConfObj.parentId == None:
1180 if readUnitConfObj.parentId == None:
1127 readUnitConfObj.parentId = self.id
1181 readUnitConfObj.parentId = self.id
1128
1182
1129 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
1183 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
1130
1184
1131 procUnitElementList = self.projectElement.iter(
1185 procUnitElementList = self.projectElement.iter(
1132 ProcUnitConf().getElementName())
1186 ProcUnitConf().getElementName())
1133
1187
1134 for procUnitElement in procUnitElementList:
1188 for procUnitElement in procUnitElementList:
1135 procUnitConfObj = ProcUnitConf()
1189 procUnitConfObj = ProcUnitConf()
1136 procUnitConfObj.readXml(procUnitElement)
1190 procUnitConfObj.readXml(procUnitElement)
1137
1191
1138 if procUnitConfObj.parentId == None:
1192 if procUnitConfObj.parentId == None:
1139 procUnitConfObj.parentId = self.id
1193 procUnitConfObj.parentId = self.id
1140
1194
1141 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1195 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
1142
1196
1143 self.filename = abs_file
1197 self.filename = abs_file
1144
1198
1145 return 1
1199 return 1
1146
1200
1147 def printattr(self):
1201 def printattr(self):
1148
1202
1149 print('Project[%s]: name = %s, description = %s' % (self.id,
1203 print('Project[%s]: name = %s, description = %s' % (self.id,
1150 self.name,
1204 self.name,
1151 self.description))
1205 self.description))
1152
1206
1153 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1207 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1154 procUnitConfObj.printattr()
1208 procUnitConfObj.printattr()
1155
1209
1156 def createObjects(self):
1210 def createObjects(self):
1157
1211
1158 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1212 for procUnitConfObj in list(self.procUnitConfObjDict.values()):
1159 procUnitConfObj.createObjects(self.plotterQueue)
1213 print("Creating process:", procUnitConfObj.name)
1160
1214 procUnitConfObj.createObjects(self.procUnitConfObjDict)
1161 def __connect(self, objIN, thisObj):
1215
1162
1163 thisObj.setInput(objIN.getOutputObj())
1164
1165 def connectObjects(self):
1166
1167 for thisPUConfObj in list(self.procUnitConfObjDict.values()):
1168
1216
1169 inputId = thisPUConfObj.getInputId()
1217 print('All processes were created')
1170
1171 if int(inputId) == 0:
1172 continue
1173
1174 # Get input object
1175 puConfINObj = self.procUnitConfObjDict[inputId]
1176 puObjIN = puConfINObj.getProcUnitObj()
1177
1178 # Get current object
1179 thisPUObj = thisPUConfObj.getProcUnitObj()
1180
1181 self.__connect(puObjIN, thisPUObj)
1182
1218
1183 def __handleError(self, procUnitConfObj, modes=None, stdout=True):
1219 def __handleError(self, procUnitConfObj, modes=None, stdout=True):
1184
1220
1185 import socket
1221 import socket
1186
1222
1187 if modes is None:
1223 if modes is None:
1188 modes = self.alarm
1224 modes = self.alarm
1189
1225
1190 if not self.alarm:
1226 if not self.alarm:
1191 modes = []
1227 modes = []
1192
1228
1193 err = traceback.format_exception(sys.exc_info()[0],
1229 err = traceback.format_exception(sys.exc_info()[0],
1194 sys.exc_info()[1],
1230 sys.exc_info()[1],
1195 sys.exc_info()[2])
1231 sys.exc_info()[2])
1196
1232
1197 log.error('{}'.format(err[-1]), procUnitConfObj.name)
1233 log.error('{}'.format(err[-1]), procUnitConfObj.name)
1198
1234
1199 message = ''.join(err)
1235 message = ''.join(err)
1200
1236
1201 if stdout:
1237 if stdout:
1202 sys.stderr.write(message)
1238 sys.stderr.write(message)
1203
1239
1204 subject = 'SChain v%s: Error running %s\n' % (
1240 subject = 'SChain v%s: Error running %s\n' % (
1205 schainpy.__version__, procUnitConfObj.name)
1241 schainpy.__version__, procUnitConfObj.name)
1206
1242
1207 subtitle = '%s: %s\n' % (
1243 subtitle = '%s: %s\n' % (
1208 procUnitConfObj.getElementName(), procUnitConfObj.name)
1244 procUnitConfObj.getElementName(), procUnitConfObj.name)
1209 subtitle += 'Hostname: %s\n' % socket.gethostbyname(
1245 subtitle += 'Hostname: %s\n' % socket.gethostbyname(
1210 socket.gethostname())
1246 socket.gethostname())
1211 subtitle += 'Working directory: %s\n' % os.path.abspath('./')
1247 subtitle += 'Working directory: %s\n' % os.path.abspath('./')
1212 subtitle += 'Configuration file: %s\n' % self.filename
1248 subtitle += 'Configuration file: %s\n' % self.filename
1213 subtitle += 'Time: %s\n' % str(datetime.datetime.now())
1249 subtitle += 'Time: %s\n' % str(datetime.datetime.now())
1214
1250
1215 readUnitConfObj = self.getReadUnitObj()
1251 readUnitConfObj = self.getReadUnitObj()
1216 if readUnitConfObj:
1252 if readUnitConfObj:
1217 subtitle += '\nInput parameters:\n'
1253 subtitle += '\nInput parameters:\n'
1218 subtitle += '[Data path = %s]\n' % readUnitConfObj.path
1254 subtitle += '[Data path = %s]\n' % readUnitConfObj.path
1219 subtitle += '[Data type = %s]\n' % readUnitConfObj.datatype
1255 subtitle += '[Data type = %s]\n' % readUnitConfObj.datatype
1220 subtitle += '[Start date = %s]\n' % readUnitConfObj.startDate
1256 subtitle += '[Start date = %s]\n' % readUnitConfObj.startDate
1221 subtitle += '[End date = %s]\n' % readUnitConfObj.endDate
1257 subtitle += '[End date = %s]\n' % readUnitConfObj.endDate
1222 subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime
1258 subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime
1223 subtitle += '[End time = %s]\n' % readUnitConfObj.endTime
1259 subtitle += '[End time = %s]\n' % readUnitConfObj.endTime
1224
1260
1225 a = Alarm(
1261 a = Alarm(
1226 modes=modes,
1262 modes=modes,
1227 email=self.email,
1263 email=self.email,
1228 message=message,
1264 message=message,
1229 subject=subject,
1265 subject=subject,
1230 subtitle=subtitle,
1266 subtitle=subtitle,
1231 filename=self.filename
1267 filename=self.filename
1232 )
1268 )
1233
1269
1234 return a
1270 return a
1235
1271
1236 def isPaused(self):
1272 def isPaused(self):
1237 return 0
1273 return 0
1238
1274
1239 def isStopped(self):
1275 def isStopped(self):
1240 return 0
1276 return 0
1241
1277
1242 def runController(self):
1278 def runController(self):
1243 '''
1279 '''
1244 returns 0 when this process has been stopped, 1 otherwise
1280 returns 0 when this process has been stopped, 1 otherwise
1245 '''
1281 '''
1246
1282
1247 if self.isPaused():
1283 if self.isPaused():
1248 print('Process suspended')
1284 print('Process suspended')
1249
1285
1250 while True:
1286 while True:
1251 time.sleep(0.1)
1287 time.sleep(0.1)
1252
1288
1253 if not self.isPaused():
1289 if not self.isPaused():
1254 break
1290 break
1255
1291
1256 if self.isStopped():
1292 if self.isStopped():
1257 break
1293 break
1258
1294
1259 print('Process reinitialized')
1295 print('Process reinitialized')
1260
1296
1261 if self.isStopped():
1297 if self.isStopped():
1262 print('Process stopped')
1298 print('Process stopped')
1263 return 0
1299 return 0
1264
1300
1265 return 1
1301 return 1
1266
1302
1267 def setFilename(self, filename):
1303 def setFilename(self, filename):
1268
1304
1269 self.filename = filename
1305 self.filename = filename
1270
1306
1271 def setPlotterQueue(self, plotter_queue):
1307 def setProxyCom(self):
1272
1308
1273 raise NotImplementedError('Use schainpy.controller_api.ControllerThread instead Project class')
1309 ctx = zmq.Context()
1274
1310 if not os.path.exists('/tmp/socketTmp'): os.mkdir('/tmp/socketTmp')
1275 def getPlotterQueue(self):
1311 xsub = ctx.socket(zmq.XSUB)
1276
1312 xsub.bind('ipc:///tmp/socketTmp/a')
1277 raise NotImplementedError('Use schainpy.controller_api.ControllerThread instead Project class')
1313 xpub = ctx.socket(zmq.XPUB)
1278
1314 xpub.bind('ipc:///tmp/socketTmp/b')
1279 def useExternalPlotter(self):
1315
1316 print("Controller Ready: Processes and proxy created")
1317 zmq.proxy(xsub, xpub)
1280
1318
1281 raise NotImplementedError('Use schainpy.controller_api.ControllerThread instead Project class')
1319
1282
1320
1283 def run(self):
1321 def run(self):
1284
1322
1285 log.success('Starting {}'.format(self.name), tag='')
1323 log.success('Starting {}'.format(self.name), tag='')
1286 self.start_time = time.time()
1324 self.start_time = time.time()
1287 self.createObjects()
1325 self.createObjects()
1288 self.connectObjects()
1326 self.setProxyCom()
1289
1290 keyList = list(self.procUnitConfObjDict.keys())
1291 keyList.sort()
1292
1293 err = None
1294
1295 while(True):
1296
1297 is_ok = False
1298
1327
1299 for procKey in keyList:
1328 # Iniciar todos los procesos .start(), monitoreo de procesos. ELiminar lo de abajo
1300
1301 procUnitConfObj = self.procUnitConfObjDict[procKey]
1302
1303 try:
1304 sts = procUnitConfObj.run()
1305 is_ok = is_ok or sts
1306 except SchainWarning:
1307 err = self.__handleError(procUnitConfObj, modes=[2, 3], stdout=False)
1308 is_ok = False
1309 break
1310 except KeyboardInterrupt:
1311 is_ok = False
1312 break
1313 except ValueError as e:
1314 time.sleep(0.5)
1315 err = self.__handleError(procUnitConfObj)
1316 is_ok = False
1317 break
1318 except:
1319 time.sleep(0.5)
1320 err = self.__handleError(procUnitConfObj)
1321 is_ok = False
1322 break
1323
1324 # If every process unit finished so end process
1325 if not(is_ok):
1326 break
1327
1328 if not self.runController():
1329 break
1330
1329
1331 # Closing every process
1330 # Closing every process
1332 for procKey in keyList:
1333 procUnitConfObj = self.procUnitConfObjDict[procKey]
1334 procUnitConfObj.close()
1335
1336 if err is not None:
1337 err.start()
1338 # err.join()
1339
1340 log.success('{} finished (time: {}s)'.format(
1331 log.success('{} finished (time: {}s)'.format(
1341 self.name,
1332 self.name,
1342 time.time()-self.start_time)) No newline at end of file
1333 time.time()-self.start_time))
@@ -1,657 +1,658
1 import os
1 import os
2 import numpy
2 import numpy
3 import time, datetime
3 import time, datetime
4 from schainpy.model.graphics import mpldriver
4 from schainpy.model.graphics import mpldriver
5
5
6 from schainpy.model.proc.jroproc_base import Operation
6 from schainpy.model.proc.jroproc_base import MPDecorator, Operation
7
7
8
8 def isTimeInHourRange(datatime, xmin, xmax):
9 def isTimeInHourRange(datatime, xmin, xmax):
9
10
10 if xmin == None or xmax == None:
11 if xmin == None or xmax == None:
11 return 1
12 return 1
12 hour = datatime.hour + datatime.minute/60.0
13 hour = datatime.hour + datatime.minute/60.0
13
14
14 if xmin < (xmax % 24):
15 if xmin < (xmax % 24):
15
16
16 if hour >= xmin and hour <= xmax:
17 if hour >= xmin and hour <= xmax:
17 return 1
18 return 1
18 else:
19 else:
19 return 0
20 return 0
20
21
21 else:
22 else:
22
23
23 if hour >= xmin or hour <= (xmax % 24):
24 if hour >= xmin or hour <= (xmax % 24):
24 return 1
25 return 1
25 else:
26 else:
26 return 0
27 return 0
27
28
28 return 0
29 return 0
29
30
30 def isRealtime(utcdatatime):
31 def isRealtime(utcdatatime):
31
32
32 utcnow = time.mktime(time.localtime())
33 utcnow = time.mktime(time.localtime())
33 delta = abs(utcnow - utcdatatime) # abs
34 delta = abs(utcnow - utcdatatime) # abs
34 if delta >= 30.:
35 if delta >= 30.:
35 return False
36 return False
36 return True
37 return True
37
38
38 class Figure(Operation):
39 class Figure(Operation):
39
40
40 __driver = mpldriver
41 __driver = mpldriver
41 fig = None
42 fig = None
42
43
43 id = None
44 id = None
44 wintitle = None
45 wintitle = None
45 width = None
46 width = None
46 height = None
47 height = None
47 nplots = None
48 nplots = None
48 timerange = None
49 timerange = None
49
50
50 axesObjList = []
51 axesObjList = []
51
52
52 WIDTH = 300
53 WIDTH = 300
53 HEIGHT = 200
54 HEIGHT = 200
54 PREFIX = 'fig'
55 PREFIX = 'fig'
55
56
56 xmin = None
57 xmin = None
57 xmax = None
58 xmax = None
58
59
59 counter_imagwr = 0
60 counter_imagwr = 0
60
61
61 figfile = None
62 figfile = None
62
63
63 created = False
64 created = False
64 parameters = {}
65 parameters = {}
65 def __init__(self, **kwargs):
66 def __init__(self):#, **kwargs):
66
67
67 Operation.__init__(self, **kwargs)
68 Operation.__init__(self)#, **kwargs)
68
69
69 def __del__(self):
70 def __del__(self):
70
71
71 self.__driver.closeFigure()
72 self.__driver.closeFigure()
72
73
73 def getFilename(self, name, ext='.png'):
74 def getFilename(self, name, ext='.png'):
74
75
75 path = '%s%03d' %(self.PREFIX, self.id)
76 path = '%s%03d' %(self.PREFIX, self.id)
76 filename = '%s_%s%s' %(self.PREFIX, name, ext)
77 filename = '%s_%s%s' %(self.PREFIX, name, ext)
77 return os.path.join(path, filename)
78 return os.path.join(path, filename)
78
79
79 def getAxesObjList(self):
80 def getAxesObjList(self):
80
81
81 return self.axesObjList
82 return self.axesObjList
82
83
83 def getSubplots(self):
84 def getSubplots(self):
84
85
85 raise NotImplementedError
86 raise NotImplementedError
86
87
87 def getScreenDim(self, widthplot, heightplot):
88 def getScreenDim(self, widthplot, heightplot):
88
89
89 nrow, ncol = self.getSubplots()
90 nrow, ncol = self.getSubplots()
90
91
91 widthscreen = widthplot*ncol
92 widthscreen = widthplot*ncol
92 heightscreen = heightplot*nrow
93 heightscreen = heightplot*nrow
93
94
94 return widthscreen, heightscreen
95 return widthscreen, heightscreen
95
96
96 def getTimeLim(self, x, xmin=None, xmax=None, timerange=None):
97 def getTimeLim(self, x, xmin=None, xmax=None, timerange=None):
97
98
98 # if self.xmin != None and self.xmax != None:
99 # if self.xmin != None and self.xmax != None:
99 # if timerange == None:
100 # if timerange == None:
100 # timerange = self.xmax - self.xmin
101 # timerange = self.xmax - self.xmin
101 # xmin = self.xmin + timerange
102 # xmin = self.xmin + timerange
102 # xmax = self.xmax + timerange
103 # xmax = self.xmax + timerange
103 #
104 #
104 # return xmin, xmax
105 # return xmin, xmax
105
106
106 if timerange == None and (xmin==None or xmax==None):
107 if timerange == None and (xmin==None or xmax==None):
107 timerange = 14400 #seconds
108 timerange = 14400 #seconds
108
109
109 if timerange != None:
110 if timerange != None:
110 txmin = x[0] #- x[0] % min(timerange/10, 10*60)
111 txmin = x[0] #- x[0] % min(timerange/10, 10*60)
111 else:
112 else:
112 txmin = x[0] #- x[0] % 10*60
113 txmin = x[0] #- x[0] % 10*60
113
114
114 thisdatetime = datetime.datetime.utcfromtimestamp(txmin)
115 thisdatetime = datetime.datetime.utcfromtimestamp(txmin)
115 thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0))
116 thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0))
116
117
117 if timerange != None:
118 if timerange != None:
118 xmin = (thisdatetime - thisdate).seconds/(60*60.)
119 xmin = (thisdatetime - thisdate).seconds/(60*60.)
119 xmax = xmin + timerange/(60*60.)
120 xmax = xmin + timerange/(60*60.)
120
121
121 d1970 = datetime.datetime(1970,1,1)
122 d1970 = datetime.datetime(1970,1,1)
122
123
123 mindt = thisdate + datetime.timedelta(hours=xmin) #- datetime.timedelta(seconds=time.timezone)
124 mindt = thisdate + datetime.timedelta(hours=xmin) #- datetime.timedelta(seconds=time.timezone)
124 xmin_sec = (mindt - d1970).total_seconds() #time.mktime(mindt.timetuple()) - time.timezone
125 xmin_sec = (mindt - d1970).total_seconds() #time.mktime(mindt.timetuple()) - time.timezone
125
126
126 maxdt = thisdate + datetime.timedelta(hours=xmax) #- datetime.timedelta(seconds=time.timezone)
127 maxdt = thisdate + datetime.timedelta(hours=xmax) #- datetime.timedelta(seconds=time.timezone)
127 xmax_sec = (maxdt - d1970).total_seconds() #time.mktime(maxdt.timetuple()) - time.timezone
128 xmax_sec = (maxdt - d1970).total_seconds() #time.mktime(maxdt.timetuple()) - time.timezone
128
129
129 return xmin_sec, xmax_sec
130 return xmin_sec, xmax_sec
130
131
131 def init(self, id, nplots, wintitle):
132 def init(self, id, nplots, wintitle):
132
133
133 raise NotImplementedError("This method has been replaced by createFigure")
134 raise NotImplementedError("This method has been replaced by createFigure")
134
135
135 def createFigure(self, id, wintitle, widthplot=None, heightplot=None, show=True):
136 def createFigure(self, id, wintitle, widthplot=None, heightplot=None, show=True):
136
137
137 """
138 """
138 Crea la figura de acuerdo al driver y parametros seleccionados seleccionados.
139 Crea la figura de acuerdo al driver y parametros seleccionados seleccionados.
139 Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH
140 Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH
140 y self.HEIGHT y el numero de subplots (nrow, ncol)
141 y self.HEIGHT y el numero de subplots (nrow, ncol)
141
142
142 Input:
143 Input:
143 id : Los parametros necesarios son
144 id : Los parametros necesarios son
144 wintitle :
145 wintitle :
145
146
146 """
147 """
147
148
148 if widthplot == None:
149 if widthplot == None:
149 widthplot = self.WIDTH
150 widthplot = self.WIDTH
150
151
151 if heightplot == None:
152 if heightplot == None:
152 heightplot = self.HEIGHT
153 heightplot = self.HEIGHT
153
154
154 self.id = id
155 self.id = id
155
156
156 self.wintitle = wintitle
157 self.wintitle = wintitle
157
158
158 self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot)
159 self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot)
159
160
160 # if self.created:
161 # if self.created:
161 # self.__driver.closeFigure(self.fig)
162 # self.__driver.closeFigure(self.fig)
162
163
163 if not self.created:
164 if not self.created:
164 self.fig = self.__driver.createFigure(id=self.id,
165 self.fig = self.__driver.createFigure(id=self.id,
165 wintitle=self.wintitle,
166 wintitle=self.wintitle,
166 width=self.widthscreen,
167 width=self.widthscreen,
167 height=self.heightscreen,
168 height=self.heightscreen,
168 show=show)
169 show=show)
169 else:
170 else:
170 self.__driver.clearFigure(self.fig)
171 self.__driver.clearFigure(self.fig)
171
172
172 self.axesObjList = []
173 self.axesObjList = []
173 self.counter_imagwr = 0
174 self.counter_imagwr = 0
174
175
175 self.created = True
176 self.created = True
176
177
177 def setDriver(self, driver=mpldriver):
178 def setDriver(self, driver=mpldriver):
178
179
179 self.__driver = driver
180 self.__driver = driver
180
181
181 def setTitle(self, title):
182 def setTitle(self, title):
182
183
183 self.__driver.setTitle(self.fig, title)
184 self.__driver.setTitle(self.fig, title)
184
185
185 def setWinTitle(self, title):
186 def setWinTitle(self, title):
186
187
187 self.__driver.setWinTitle(self.fig, title=title)
188 self.__driver.setWinTitle(self.fig, title=title)
188
189
189 def setTextFromAxes(self, text):
190 def setTextFromAxes(self, text):
190
191
191 raise NotImplementedError("This method has been replaced with Axes.setText")
192 raise NotImplementedError("This method has been replaced with Axes.setText")
192
193
193 def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan):
194 def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan):
194
195
195 raise NotImplementedError("This method has been replaced with Axes.addAxes")
196 raise NotImplementedError("This method has been replaced with Axes.addAxes")
196
197
197 def addAxes(self, *args):
198 def addAxes(self, *args):
198 """
199 """
199
200
200 Input:
201 Input:
201 *args : Los parametros necesarios son
202 *args : Los parametros necesarios son
202 nrow, ncol, xpos, ypos, colspan, rowspan
203 nrow, ncol, xpos, ypos, colspan, rowspan
203 """
204 """
204
205
205 axesObj = Axes(self.fig, *args)
206 axesObj = Axes(self.fig, *args)
206 self.axesObjList.append(axesObj)
207 self.axesObjList.append(axesObj)
207
208
208 def saveFigure(self, figpath, figfile, *args):
209 def saveFigure(self, figpath, figfile, *args):
209
210
210 filename = os.path.join(figpath, figfile)
211 filename = os.path.join(figpath, figfile)
211
212
212 fullpath = os.path.split(filename)[0]
213 fullpath = os.path.split(filename)[0]
213
214
214 if not os.path.exists(fullpath):
215 if not os.path.exists(fullpath):
215 subpath = os.path.split(fullpath)[0]
216 subpath = os.path.split(fullpath)[0]
216
217
217 if not os.path.exists(subpath):
218 if not os.path.exists(subpath):
218 os.mkdir(subpath)
219 os.mkdir(subpath)
219
220
220 os.mkdir(fullpath)
221 os.mkdir(fullpath)
221
222
222 self.__driver.saveFigure(self.fig, filename, *args)
223 self.__driver.saveFigure(self.fig, filename, *args)
223
224
224 def save(self, figpath, figfile=None, save=True, ftp=False, wr_period=1, thisDatetime=None, update_figfile=True):
225 def save(self, figpath, figfile=None, save=True, ftp=False, wr_period=1, thisDatetime=None, update_figfile=True):
225
226
226 self.counter_imagwr += 1
227 self.counter_imagwr += 1
227 if self.counter_imagwr < wr_period:
228 if self.counter_imagwr < wr_period:
228 return
229 return
229
230
230 self.counter_imagwr = 0
231 self.counter_imagwr = 0
231
232
232 if save:
233 if save:
233
234
234 if not figfile:
235 if not figfile:
235
236
236 if not thisDatetime:
237 if not thisDatetime:
237 raise ValueError("Saving figure: figfile or thisDatetime should be defined")
238 raise ValueError("Saving figure: figfile or thisDatetime should be defined")
238 return
239 return
239
240
240 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
241 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
241 figfile = self.getFilename(name = str_datetime)
242 figfile = self.getFilename(name = str_datetime)
242
243
243 if self.figfile == None:
244 if self.figfile == None:
244 self.figfile = figfile
245 self.figfile = figfile
245
246
246 if update_figfile:
247 if update_figfile:
247 self.figfile = figfile
248 self.figfile = figfile
248
249
249 # store png plot to local folder
250 # store png plot to local folder
250 self.saveFigure(figpath, self.figfile)
251 self.saveFigure(figpath, self.figfile)
251
252
252
253
253 if not ftp:
254 if not ftp:
254 return
255 return
255
256
256 if not thisDatetime:
257 if not thisDatetime:
257 return
258 return
258
259
259 # store png plot to FTP server according to RT-Web format
260 # store png plot to FTP server according to RT-Web format
260 ftp_filename = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
261 ftp_filename = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
261 # ftp_filename = os.path.join(figpath, name)
262 # ftp_filename = os.path.join(figpath, name)
262 self.saveFigure(figpath, ftp_filename)
263 self.saveFigure(figpath, ftp_filename)
263
264
264 def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS):
265 def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS):
265 YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year
266 YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year
266 DOY_STR = '%3.3d'%thisDatetime.timetuple().tm_yday
267 DOY_STR = '%3.3d'%thisDatetime.timetuple().tm_yday
267 FTP_WEI = '%2.2d'%FTP_WEI
268 FTP_WEI = '%2.2d'%FTP_WEI
268 EXP_CODE = '%3.3d'%EXP_CODE
269 EXP_CODE = '%3.3d'%EXP_CODE
269 SUB_EXP_CODE = '%2.2d'%SUB_EXP_CODE
270 SUB_EXP_CODE = '%2.2d'%SUB_EXP_CODE
270 PLOT_CODE = '%2.2d'%PLOT_CODE
271 PLOT_CODE = '%2.2d'%PLOT_CODE
271 PLOT_POS = '%2.2d'%PLOT_POS
272 PLOT_POS = '%2.2d'%PLOT_POS
272 name = YEAR_STR + DOY_STR + FTP_WEI + EXP_CODE + SUB_EXP_CODE + PLOT_CODE + PLOT_POS
273 name = YEAR_STR + DOY_STR + FTP_WEI + EXP_CODE + SUB_EXP_CODE + PLOT_CODE + PLOT_POS
273 return name
274 return name
274
275
275 def draw(self):
276 def draw(self):
276
277
277 self.__driver.draw(self.fig)
278 self.__driver.draw(self.fig)
278
279
279 def run(self):
280 def run(self):
280
281
281 raise NotImplementedError
282 raise NotImplementedError
282
283
283 def close(self, show=False):
284 def close(self, show=False):
284
285
285 self.__driver.closeFigure(show=show, fig=self.fig)
286 self.__driver.closeFigure(show=show, fig=self.fig)
286
287
287 axesList = property(getAxesObjList)
288 axesList = property(getAxesObjList)
288
289
289
290
290 class Axes:
291 class Axes:
291
292
292 __driver = mpldriver
293 __driver = mpldriver
293 fig = None
294 fig = None
294 ax = None
295 ax = None
295 plot = None
296 plot = None
296 __missing = 1E30
297 __missing = 1E30
297 __firsttime = None
298 __firsttime = None
298
299
299 __showprofile = False
300 __showprofile = False
300
301
301 xmin = None
302 xmin = None
302 xmax = None
303 xmax = None
303 ymin = None
304 ymin = None
304 ymax = None
305 ymax = None
305 zmin = None
306 zmin = None
306 zmax = None
307 zmax = None
307
308
308 x_buffer = None
309 x_buffer = None
309 z_buffer = None
310 z_buffer = None
310
311
311 decimationx = None
312 decimationx = None
312 decimationy = None
313 decimationy = None
313
314
314 __MAXNUMX = 200
315 __MAXNUMX = 200
315 __MAXNUMY = 400
316 __MAXNUMY = 400
316
317
317 __MAXNUMTIME = 500
318 __MAXNUMTIME = 500
318
319
319 def __init__(self, *args):
320 def __init__(self, *args):
320
321
321 """
322 """
322
323
323 Input:
324 Input:
324 *args : Los parametros necesarios son
325 *args : Los parametros necesarios son
325 fig, nrow, ncol, xpos, ypos, colspan, rowspan
326 fig, nrow, ncol, xpos, ypos, colspan, rowspan
326 """
327 """
327
328
328 ax = self.__driver.createAxes(*args)
329 ax = self.__driver.createAxes(*args)
329 self.fig = args[0]
330 self.fig = args[0]
330 self.ax = ax
331 self.ax = ax
331 self.plot = None
332 self.plot = None
332
333
333 self.__firsttime = True
334 self.__firsttime = True
334 self.idlineList = []
335 self.idlineList = []
335
336
336 self.x_buffer = numpy.array([])
337 self.x_buffer = numpy.array([])
337 self.z_buffer = numpy.array([])
338 self.z_buffer = numpy.array([])
338
339
339 def setText(self, text):
340 def setText(self, text):
340
341
341 self.__driver.setAxesText(self.ax, text)
342 self.__driver.setAxesText(self.ax, text)
342
343
343 def setXAxisAsTime(self):
344 def setXAxisAsTime(self):
344 pass
345 pass
345
346
346 def pline(self, x, y,
347 def pline(self, x, y,
347 xmin=None, xmax=None,
348 xmin=None, xmax=None,
348 ymin=None, ymax=None,
349 ymin=None, ymax=None,
349 xlabel='', ylabel='',
350 xlabel='', ylabel='',
350 title='',
351 title='',
351 **kwargs):
352 **kwargs):
352
353
353 """
354 """
354
355
355 Input:
356 Input:
356 x :
357 x :
357 y :
358 y :
358 xmin :
359 xmin :
359 xmax :
360 xmax :
360 ymin :
361 ymin :
361 ymax :
362 ymax :
362 xlabel :
363 xlabel :
363 ylabel :
364 ylabel :
364 title :
365 title :
365 **kwargs : Los parametros aceptados son
366 **kwargs : Los parametros aceptados son
366
367
367 ticksize
368 ticksize
368 ytick_visible
369 ytick_visible
369 """
370 """
370
371
371 if self.__firsttime:
372 if self.__firsttime:
372
373
373 if xmin == None: xmin = numpy.nanmin(x)
374 if xmin == None: xmin = numpy.nanmin(x)
374 if xmax == None: xmax = numpy.nanmax(x)
375 if xmax == None: xmax = numpy.nanmax(x)
375 if ymin == None: ymin = numpy.nanmin(y)
376 if ymin == None: ymin = numpy.nanmin(y)
376 if ymax == None: ymax = numpy.nanmax(y)
377 if ymax == None: ymax = numpy.nanmax(y)
377
378
378 self.plot = self.__driver.createPline(self.ax, x, y,
379 self.plot = self.__driver.createPline(self.ax, x, y,
379 xmin, xmax,
380 xmin, xmax,
380 ymin, ymax,
381 ymin, ymax,
381 xlabel=xlabel,
382 xlabel=xlabel,
382 ylabel=ylabel,
383 ylabel=ylabel,
383 title=title,
384 title=title,
384 **kwargs)
385 **kwargs)
385
386
386 self.idlineList.append(0)
387 self.idlineList.append(0)
387 self.__firsttime = False
388 self.__firsttime = False
388 return
389 return
389
390
390 self.__driver.pline(self.plot, x, y, xlabel=xlabel,
391 self.__driver.pline(self.plot, x, y, xlabel=xlabel,
391 ylabel=ylabel,
392 ylabel=ylabel,
392 title=title)
393 title=title)
393
394
394 # self.__driver.pause()
395 # self.__driver.pause()
395
396
396 def addpline(self, x, y, idline, **kwargs):
397 def addpline(self, x, y, idline, **kwargs):
397 lines = self.ax.lines
398 lines = self.ax.lines
398
399
399 if idline in self.idlineList:
400 if idline in self.idlineList:
400 self.__driver.set_linedata(self.ax, x, y, idline)
401 self.__driver.set_linedata(self.ax, x, y, idline)
401
402
402 if idline not in(self.idlineList):
403 if idline not in(self.idlineList):
403 self.__driver.addpline(self.ax, x, y, **kwargs)
404 self.__driver.addpline(self.ax, x, y, **kwargs)
404 self.idlineList.append(idline)
405 self.idlineList.append(idline)
405
406
406 return
407 return
407
408
408 def pmultiline(self, x, y,
409 def pmultiline(self, x, y,
409 xmin=None, xmax=None,
410 xmin=None, xmax=None,
410 ymin=None, ymax=None,
411 ymin=None, ymax=None,
411 xlabel='', ylabel='',
412 xlabel='', ylabel='',
412 title='',
413 title='',
413 **kwargs):
414 **kwargs):
414
415
415 if self.__firsttime:
416 if self.__firsttime:
416
417
417 if xmin == None: xmin = numpy.nanmin(x)
418 if xmin == None: xmin = numpy.nanmin(x)
418 if xmax == None: xmax = numpy.nanmax(x)
419 if xmax == None: xmax = numpy.nanmax(x)
419 if ymin == None: ymin = numpy.nanmin(y)
420 if ymin == None: ymin = numpy.nanmin(y)
420 if ymax == None: ymax = numpy.nanmax(y)
421 if ymax == None: ymax = numpy.nanmax(y)
421
422
422 self.plot = self.__driver.createPmultiline(self.ax, x, y,
423 self.plot = self.__driver.createPmultiline(self.ax, x, y,
423 xmin, xmax,
424 xmin, xmax,
424 ymin, ymax,
425 ymin, ymax,
425 xlabel=xlabel,
426 xlabel=xlabel,
426 ylabel=ylabel,
427 ylabel=ylabel,
427 title=title,
428 title=title,
428 **kwargs)
429 **kwargs)
429 self.__firsttime = False
430 self.__firsttime = False
430 return
431 return
431
432
432 self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel,
433 self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel,
433 ylabel=ylabel,
434 ylabel=ylabel,
434 title=title)
435 title=title)
435
436
436 # self.__driver.pause()
437 # self.__driver.pause()
437
438
438 def pmultilineyaxis(self, x, y,
439 def pmultilineyaxis(self, x, y,
439 xmin=None, xmax=None,
440 xmin=None, xmax=None,
440 ymin=None, ymax=None,
441 ymin=None, ymax=None,
441 xlabel='', ylabel='',
442 xlabel='', ylabel='',
442 title='',
443 title='',
443 **kwargs):
444 **kwargs):
444
445
445 if self.__firsttime:
446 if self.__firsttime:
446
447
447 if xmin == None: xmin = numpy.nanmin(x)
448 if xmin == None: xmin = numpy.nanmin(x)
448 if xmax == None: xmax = numpy.nanmax(x)
449 if xmax == None: xmax = numpy.nanmax(x)
449 if ymin == None: ymin = numpy.nanmin(y)
450 if ymin == None: ymin = numpy.nanmin(y)
450 if ymax == None: ymax = numpy.nanmax(y)
451 if ymax == None: ymax = numpy.nanmax(y)
451
452
452 self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y,
453 self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y,
453 xmin, xmax,
454 xmin, xmax,
454 ymin, ymax,
455 ymin, ymax,
455 xlabel=xlabel,
456 xlabel=xlabel,
456 ylabel=ylabel,
457 ylabel=ylabel,
457 title=title,
458 title=title,
458 **kwargs)
459 **kwargs)
459 if self.xmin == None: self.xmin = xmin
460 if self.xmin == None: self.xmin = xmin
460 if self.xmax == None: self.xmax = xmax
461 if self.xmax == None: self.xmax = xmax
461 if self.ymin == None: self.ymin = ymin
462 if self.ymin == None: self.ymin = ymin
462 if self.ymax == None: self.ymax = ymax
463 if self.ymax == None: self.ymax = ymax
463
464
464 self.__firsttime = False
465 self.__firsttime = False
465 return
466 return
466
467
467 self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel,
468 self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel,
468 ylabel=ylabel,
469 ylabel=ylabel,
469 title=title)
470 title=title)
470
471
471 # self.__driver.pause()
472 # self.__driver.pause()
472
473
473 def pcolor(self, x, y, z,
474 def pcolor(self, x, y, z,
474 xmin=None, xmax=None,
475 xmin=None, xmax=None,
475 ymin=None, ymax=None,
476 ymin=None, ymax=None,
476 zmin=None, zmax=None,
477 zmin=None, zmax=None,
477 xlabel='', ylabel='',
478 xlabel='', ylabel='',
478 title='', colormap='jet',
479 title='', colormap='jet',
479 **kwargs):
480 **kwargs):
480
481
481 """
482 """
482 Input:
483 Input:
483 x :
484 x :
484 y :
485 y :
485 x :
486 x :
486 xmin :
487 xmin :
487 xmax :
488 xmax :
488 ymin :
489 ymin :
489 ymax :
490 ymax :
490 zmin :
491 zmin :
491 zmax :
492 zmax :
492 xlabel :
493 xlabel :
493 ylabel :
494 ylabel :
494 title :
495 title :
495 **kwargs : Los parametros aceptados son
496 **kwargs : Los parametros aceptados son
496 ticksize=9,
497 ticksize=9,
497 cblabel=''
498 cblabel=''
498 """
499 """
499
500
500 #Decimating data
501 #Decimating data
501 xlen = len(x)
502 xlen = len(x)
502 ylen = len(y)
503 ylen = len(y)
503
504
504 decimationx = int(xlen/self.__MAXNUMX) + 1
505 decimationx = int(xlen/self.__MAXNUMX) + 1
505 decimationy = int(ylen/self.__MAXNUMY) + 1
506 decimationy = int(ylen/self.__MAXNUMY) + 1
506
507
507
508
508 x_buffer = x#[::decimationx]
509 x_buffer = x#[::decimationx]
509 y_buffer = y#[::decimationy]
510 y_buffer = y#[::decimationy]
510 z_buffer = z#[::decimationx, ::decimationy]
511 z_buffer = z#[::decimationx, ::decimationy]
511 #===================================================
512 #===================================================
512
513
513 if self.__firsttime:
514 if self.__firsttime:
514
515
515 if xmin == None: xmin = numpy.nanmin(x)
516 if xmin == None: xmin = numpy.nanmin(x)
516 if xmax == None: xmax = numpy.nanmax(x)
517 if xmax == None: xmax = numpy.nanmax(x)
517 if ymin == None: ymin = numpy.nanmin(y)
518 if ymin == None: ymin = numpy.nanmin(y)
518 if ymax == None: ymax = numpy.nanmax(y)
519 if ymax == None: ymax = numpy.nanmax(y)
519 if zmin == None: zmin = numpy.nanmin(z)
520 if zmin == None: zmin = numpy.nanmin(z)
520 if zmax == None: zmax = numpy.nanmax(z)
521 if zmax == None: zmax = numpy.nanmax(z)
521
522
522
523
523 self.plot = self.__driver.createPcolor(self.ax, x_buffer,
524 self.plot = self.__driver.createPcolor(self.ax, x_buffer,
524 y_buffer,
525 y_buffer,
525 z_buffer,
526 z_buffer,
526 xmin, xmax,
527 xmin, xmax,
527 ymin, ymax,
528 ymin, ymax,
528 zmin, zmax,
529 zmin, zmax,
529 xlabel=xlabel,
530 xlabel=xlabel,
530 ylabel=ylabel,
531 ylabel=ylabel,
531 title=title,
532 title=title,
532 colormap=colormap,
533 colormap=colormap,
533 **kwargs)
534 **kwargs)
534
535
535 if self.xmin == None: self.xmin = xmin
536 if self.xmin == None: self.xmin = xmin
536 if self.xmax == None: self.xmax = xmax
537 if self.xmax == None: self.xmax = xmax
537 if self.ymin == None: self.ymin = ymin
538 if self.ymin == None: self.ymin = ymin
538 if self.ymax == None: self.ymax = ymax
539 if self.ymax == None: self.ymax = ymax
539 if self.zmin == None: self.zmin = zmin
540 if self.zmin == None: self.zmin = zmin
540 if self.zmax == None: self.zmax = zmax
541 if self.zmax == None: self.zmax = zmax
541
542
542 self.__firsttime = False
543 self.__firsttime = False
543 return
544 return
544
545
545 self.__driver.pcolor(self.plot,
546 self.__driver.pcolor(self.plot,
546 z_buffer,
547 z_buffer,
547 xlabel=xlabel,
548 xlabel=xlabel,
548 ylabel=ylabel,
549 ylabel=ylabel,
549 title=title)
550 title=title)
550
551
551 # self.__driver.pause()
552 # self.__driver.pause()
552
553
553 def pcolorbuffer(self, x, y, z,
554 def pcolorbuffer(self, x, y, z,
554 xmin=None, xmax=None,
555 xmin=None, xmax=None,
555 ymin=None, ymax=None,
556 ymin=None, ymax=None,
556 zmin=None, zmax=None,
557 zmin=None, zmax=None,
557 xlabel='', ylabel='',
558 xlabel='', ylabel='',
558 title='', rti = True, colormap='jet',
559 title='', rti = True, colormap='jet',
559 maxNumX = None, maxNumY = None,
560 maxNumX = None, maxNumY = None,
560 **kwargs):
561 **kwargs):
561
562
562 if maxNumX == None:
563 if maxNumX == None:
563 maxNumX = self.__MAXNUMTIME
564 maxNumX = self.__MAXNUMTIME
564
565
565 if maxNumY == None:
566 if maxNumY == None:
566 maxNumY = self.__MAXNUMY
567 maxNumY = self.__MAXNUMY
567
568
568 if self.__firsttime:
569 if self.__firsttime:
569 self.z_buffer = z
570 self.z_buffer = z
570 self.x_buffer = numpy.hstack((self.x_buffer, x))
571 self.x_buffer = numpy.hstack((self.x_buffer, x))
571
572
572 if xmin == None: xmin = numpy.nanmin(x)
573 if xmin == None: xmin = numpy.nanmin(x)
573 if xmax == None: xmax = numpy.nanmax(x)
574 if xmax == None: xmax = numpy.nanmax(x)
574 if ymin == None: ymin = numpy.nanmin(y)
575 if ymin == None: ymin = numpy.nanmin(y)
575 if ymax == None: ymax = numpy.nanmax(y)
576 if ymax == None: ymax = numpy.nanmax(y)
576 if zmin == None: zmin = numpy.nanmin(z)
577 if zmin == None: zmin = numpy.nanmin(z)
577 if zmax == None: zmax = numpy.nanmax(z)
578 if zmax == None: zmax = numpy.nanmax(z)
578
579
579 self.plot = self.__driver.createPcolor(self.ax, self.x_buffer, y, z,
580 self.plot = self.__driver.createPcolor(self.ax, self.x_buffer, y, z,
580 xmin, xmax,
581 xmin, xmax,
581 ymin, ymax,
582 ymin, ymax,
582 zmin, zmax,
583 zmin, zmax,
583 xlabel=xlabel,
584 xlabel=xlabel,
584 ylabel=ylabel,
585 ylabel=ylabel,
585 title=title,
586 title=title,
586 colormap=colormap,
587 colormap=colormap,
587 **kwargs)
588 **kwargs)
588
589
589 if self.xmin == None: self.xmin = xmin
590 if self.xmin == None: self.xmin = xmin
590 if self.xmax == None: self.xmax = xmax
591 if self.xmax == None: self.xmax = xmax
591 if self.ymin == None: self.ymin = ymin
592 if self.ymin == None: self.ymin = ymin
592 if self.ymax == None: self.ymax = ymax
593 if self.ymax == None: self.ymax = ymax
593 if self.zmin == None: self.zmin = zmin
594 if self.zmin == None: self.zmin = zmin
594 if self.zmax == None: self.zmax = zmax
595 if self.zmax == None: self.zmax = zmax
595
596
596 self.__firsttime = False
597 self.__firsttime = False
597 return
598 return
598
599
599 self.x_buffer = numpy.hstack((self.x_buffer[:-1], x[0], x[-1]))
600 self.x_buffer = numpy.hstack((self.x_buffer[:-1], x[0], x[-1]))
600 self.z_buffer = numpy.hstack((self.z_buffer, z))
601 self.z_buffer = numpy.hstack((self.z_buffer, z))
601 z_buffer = self.z_buffer.reshape(-1,len(y))
602 z_buffer = self.z_buffer.reshape(-1,len(y))
602
603
603 #Decimating data
604 #Decimating data
604 xlen = len(self.x_buffer)
605 xlen = len(self.x_buffer)
605 ylen = len(y)
606 ylen = len(y)
606
607
607 decimationx = int(xlen/maxNumX) + 1
608 decimationx = int(xlen/maxNumX) + 1
608 decimationy = int(ylen/maxNumY) + 1
609 decimationy = int(ylen/maxNumY) + 1
609
610
610 x_buffer = self.x_buffer#[::decimationx]
611 x_buffer = self.x_buffer#[::decimationx]
611 y_buffer = y#[::decimationy]
612 y_buffer = y#[::decimationy]
612 z_buffer = z_buffer#[::decimationx, ::decimationy]
613 z_buffer = z_buffer#[::decimationx, ::decimationy]
613 #===================================================
614 #===================================================
614
615
615 x_buffer, y_buffer, z_buffer = self.__fillGaps(x_buffer, y_buffer, z_buffer)
616 x_buffer, y_buffer, z_buffer = self.__fillGaps(x_buffer, y_buffer, z_buffer)
616
617
617 self.__driver.addpcolorbuffer(self.ax, x_buffer, y_buffer, z_buffer, self.zmin, self.zmax,
618 self.__driver.addpcolorbuffer(self.ax, x_buffer, y_buffer, z_buffer, self.zmin, self.zmax,
618 xlabel=xlabel,
619 xlabel=xlabel,
619 ylabel=ylabel,
620 ylabel=ylabel,
620 title=title,
621 title=title,
621 colormap=colormap)
622 colormap=colormap)
622
623
623 # self.__driver.pause()
624 # self.__driver.pause()
624
625
625 def polar(self, x, y,
626 def polar(self, x, y,
626 title='', xlabel='',ylabel='',**kwargs):
627 title='', xlabel='',ylabel='',**kwargs):
627
628
628 if self.__firsttime:
629 if self.__firsttime:
629 self.plot = self.__driver.createPolar(self.ax, x, y, title = title, xlabel = xlabel, ylabel = ylabel)
630 self.plot = self.__driver.createPolar(self.ax, x, y, title = title, xlabel = xlabel, ylabel = ylabel)
630 self.__firsttime = False
631 self.__firsttime = False
631 self.x_buffer = x
632 self.x_buffer = x
632 self.y_buffer = y
633 self.y_buffer = y
633 return
634 return
634
635
635 self.x_buffer = numpy.hstack((self.x_buffer,x))
636 self.x_buffer = numpy.hstack((self.x_buffer,x))
636 self.y_buffer = numpy.hstack((self.y_buffer,y))
637 self.y_buffer = numpy.hstack((self.y_buffer,y))
637 self.__driver.polar(self.plot, self.x_buffer, self.y_buffer, xlabel=xlabel,
638 self.__driver.polar(self.plot, self.x_buffer, self.y_buffer, xlabel=xlabel,
638 ylabel=ylabel,
639 ylabel=ylabel,
639 title=title)
640 title=title)
640
641
641 # self.__driver.pause()
642 # self.__driver.pause()
642
643
643 def __fillGaps(self, x_buffer, y_buffer, z_buffer):
644 def __fillGaps(self, x_buffer, y_buffer, z_buffer):
644
645
645 if x_buffer.shape[0] < 2:
646 if x_buffer.shape[0] < 2:
646 return x_buffer, y_buffer, z_buffer
647 return x_buffer, y_buffer, z_buffer
647
648
648 deltas = x_buffer[1:] - x_buffer[0:-1]
649 deltas = x_buffer[1:] - x_buffer[0:-1]
649 x_median = numpy.median(deltas)
650 x_median = numpy.median(deltas)
650
651
651 index = numpy.where(deltas > 5*x_median)
652 index = numpy.where(deltas > 5*x_median)
652
653
653 if len(index[0]) != 0:
654 if len(index[0]) != 0:
654 z_buffer[index[0],::] = self.__missing
655 z_buffer[index[0],::] = self.__missing
655 z_buffer = numpy.ma.masked_inside(z_buffer,0.99*self.__missing,1.01*self.__missing)
656 z_buffer = numpy.ma.masked_inside(z_buffer,0.99*self.__missing,1.01*self.__missing)
656
657
657 return x_buffer, y_buffer, z_buffer No newline at end of file
658 return x_buffer, y_buffer, z_buffer
@@ -1,1542 +1,1587
1 '''
1 '''
2 Created on Jul 9, 2014
2 Created on Jul 9, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import datetime
7 import datetime
8 import numpy
8 import numpy
9
9
10 from .figure import Figure, isRealtime, isTimeInHourRange
10 from .figure import Figure, isRealtime, isTimeInHourRange
11 from .plotting_codes import *
11 from .plotting_codes import *
12 from schainpy.model.proc.jroproc_base import MPDecorator
12
13
14 from schainpy.utils import log
13
15
16 @MPDecorator
14 class SpectraPlot(Figure):
17 class SpectraPlot(Figure):
15
18
16 isConfig = None
19 isConfig = None
17 __nsubplots = None
20 __nsubplots = None
18
21
19 WIDTHPROF = None
22 WIDTHPROF = None
20 HEIGHTPROF = None
23 HEIGHTPROF = None
21 PREFIX = 'spc'
24 PREFIX = 'spc'
22
25
23 def __init__(self, **kwargs):
26 def __init__(self):#, **kwargs):
24 Figure.__init__(self, **kwargs)
27 Figure.__init__(self)#, **kwargs)
25 self.isConfig = False
28 self.isConfig = False
26 self.__nsubplots = 1
29 self.__nsubplots = 1
27
28 self.WIDTH = 250
30 self.WIDTH = 250
29 self.HEIGHT = 250
31 self.HEIGHT = 250
30 self.WIDTHPROF = 120
32 self.WIDTHPROF = 120
31 self.HEIGHTPROF = 0
33 self.HEIGHTPROF = 0
32 self.counter_imagwr = 0
34 self.counter_imagwr = 0
33
35
34 self.PLOT_CODE = SPEC_CODE
36 self.PLOT_CODE = SPEC_CODE
35
37
36 self.FTP_WEI = None
38 self.FTP_WEI = None
37 self.EXP_CODE = None
39 self.EXP_CODE = None
38 self.SUB_EXP_CODE = None
40 self.SUB_EXP_CODE = None
39 self.PLOT_POS = None
41 self.PLOT_POS = None
40
42
41 self.__xfilter_ena = False
43 self.__xfilter_ena = False
42 self.__yfilter_ena = False
44 self.__yfilter_ena = False
43
45
44 def getSubplots(self):
46 def getSubplots(self):
45
47
46 ncol = int(numpy.sqrt(self.nplots)+0.9)
48 ncol = int(numpy.sqrt(self.nplots)+0.9)
47 nrow = int(self.nplots*1./ncol + 0.9)
49 nrow = int(self.nplots*1./ncol + 0.9)
48
50
49 return nrow, ncol
51 return nrow, ncol
50
52
51 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
53 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
52
54
53 self.__showprofile = showprofile
55 self.__showprofile = showprofile
54 self.nplots = nplots
56 self.nplots = nplots
55
57
56 ncolspan = 1
58 ncolspan = 1
57 colspan = 1
59 colspan = 1
58 if showprofile:
60 if showprofile:
59 ncolspan = 3
61 ncolspan = 3
60 colspan = 2
62 colspan = 2
61 self.__nsubplots = 2
63 self.__nsubplots = 2
62
64
63 self.createFigure(id = id,
65 self.createFigure(id = id,
64 wintitle = wintitle,
66 wintitle = wintitle,
65 widthplot = self.WIDTH + self.WIDTHPROF,
67 widthplot = self.WIDTH + self.WIDTHPROF,
66 heightplot = self.HEIGHT + self.HEIGHTPROF,
68 heightplot = self.HEIGHT + self.HEIGHTPROF,
67 show=show)
69 show=show)
68
70
69 nrow, ncol = self.getSubplots()
71 nrow, ncol = self.getSubplots()
70
72
71 counter = 0
73 counter = 0
72 for y in range(nrow):
74 for y in range(nrow):
73 for x in range(ncol):
75 for x in range(ncol):
74
76
75 if counter >= self.nplots:
77 if counter >= self.nplots:
76 break
78 break
77
79
78 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
80 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
79
81
80 if showprofile:
82 if showprofile:
81 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
83 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
82
84
83 counter += 1
85 counter += 1
84
86
85 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
87 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
86 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
88 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
87 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
89 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
88 server=None, folder=None, username=None, password=None,
90 server=None, folder=None, username=None, password=None,
89 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
91 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
90 xaxis="frequency", colormap='jet', normFactor=None):
92 xaxis="frequency", colormap='jet', normFactor=None):
91
93
92 """
94 """
93
95
94 Input:
96 Input:
95 dataOut :
97 dataOut :
96 id :
98 id :
97 wintitle :
99 wintitle :
98 channelList :
100 channelList :
99 showProfile :
101 showProfile :
100 xmin : None,
102 xmin : None,
101 xmax : None,
103 xmax : None,
102 ymin : None,
104 ymin : None,
103 ymax : None,
105 ymax : None,
104 zmin : None,
106 zmin : None,
105 zmax : None
107 zmax : None
106 """
108 """
109 if dataOut.flagNoData:
110 return dataOut
111
107 if realtime:
112 if realtime:
108 if not(isRealtime(utcdatatime = dataOut.utctime)):
113 if not(isRealtime(utcdatatime = dataOut.utctime)):
109 print('Skipping this plot function')
114 print('Skipping this plot function')
110 return
115 return
111
116
112 if channelList == None:
117 if channelList == None:
113 channelIndexList = dataOut.channelIndexList
118 channelIndexList = dataOut.channelIndexList
114 else:
119 else:
115 channelIndexList = []
120 channelIndexList = []
116 for channel in channelList:
121 for channel in channelList:
117 if channel not in dataOut.channelList:
122 if channel not in dataOut.channelList:
118 raise ValueError("Channel %d is not in dataOut.channelList" %channel)
123 raise ValueError("Channel %d is not in dataOut.channelList" %channel)
119 channelIndexList.append(dataOut.channelList.index(channel))
124 channelIndexList.append(dataOut.channelList.index(channel))
120
125
121 if normFactor is None:
126 if normFactor is None:
122 factor = dataOut.normFactor
127 factor = dataOut.normFactor
123 else:
128 else:
124 factor = normFactor
129 factor = normFactor
125 if xaxis == "frequency":
130 if xaxis == "frequency":
126 x = dataOut.getFreqRange(1)/1000.
131 x = dataOut.getFreqRange(1)/1000.
127 xlabel = "Frequency (kHz)"
132 xlabel = "Frequency (kHz)"
128
133
129 elif xaxis == "time":
134 elif xaxis == "time":
130 x = dataOut.getAcfRange(1)
135 x = dataOut.getAcfRange(1)
131 xlabel = "Time (ms)"
136 xlabel = "Time (ms)"
132
137
133 else:
138 else:
134 x = dataOut.getVelRange(1)
139 x = dataOut.getVelRange(1)
135 xlabel = "Velocity (m/s)"
140 xlabel = "Velocity (m/s)"
136
141
137 ylabel = "Range (Km)"
142 ylabel = "Range (Km)"
138
143
139 y = dataOut.getHeiRange()
144 y = dataOut.getHeiRange()
140
145
141 z = dataOut.data_spc/factor
146 z = dataOut.data_spc/factor
142 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
147 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
143 zdB = 10*numpy.log10(z)
148 zdB = 10*numpy.log10(z)
144
149
145 avg = numpy.average(z, axis=1)
150 avg = numpy.average(z, axis=1)
146 avgdB = 10*numpy.log10(avg)
151 avgdB = 10*numpy.log10(avg)
147
152
148 noise = dataOut.getNoise()/factor
153 noise = dataOut.getNoise()/factor
149 noisedB = 10*numpy.log10(noise)
154 noisedB = 10*numpy.log10(noise)
150
155
151 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
156 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
152 title = wintitle + " Spectra"
157 title = wintitle + " Spectra"
153 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
158 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
154 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
159 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
155
160
156 if not self.isConfig:
161 if not self.isConfig:
157
162
158 nplots = len(channelIndexList)
163 nplots = len(channelIndexList)
159
164
160 self.setup(id=id,
165 self.setup(id=id,
161 nplots=nplots,
166 nplots=nplots,
162 wintitle=wintitle,
167 wintitle=wintitle,
163 showprofile=showprofile,
168 showprofile=showprofile,
164 show=show)
169 show=show)
165
170
166 if xmin == None: xmin = numpy.nanmin(x)
171 if xmin == None: xmin = numpy.nanmin(x)
167 if xmax == None: xmax = numpy.nanmax(x)
172 if xmax == None: xmax = numpy.nanmax(x)
168 if ymin == None: ymin = numpy.nanmin(y)
173 if ymin == None: ymin = numpy.nanmin(y)
169 if ymax == None: ymax = numpy.nanmax(y)
174 if ymax == None: ymax = numpy.nanmax(y)
170 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
175 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
171 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
176 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
172
177
173 self.FTP_WEI = ftp_wei
178 self.FTP_WEI = ftp_wei
174 self.EXP_CODE = exp_code
179 self.EXP_CODE = exp_code
175 self.SUB_EXP_CODE = sub_exp_code
180 self.SUB_EXP_CODE = sub_exp_code
176 self.PLOT_POS = plot_pos
181 self.PLOT_POS = plot_pos
177
182
178 self.isConfig = True
183 self.isConfig = True
179
184
180 self.setWinTitle(title)
185 self.setWinTitle(title)
181
186
182 for i in range(self.nplots):
187 for i in range(self.nplots):
183 index = channelIndexList[i]
188 index = channelIndexList[i]
184 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
189 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
185 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
190 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
186 if len(dataOut.beam.codeList) != 0:
191 if len(dataOut.beam.codeList) != 0:
187 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
192 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
188
193
189 axes = self.axesList[i*self.__nsubplots]
194 axes = self.axesList[i*self.__nsubplots]
190 axes.pcolor(x, y, zdB[index,:,:],
195 axes.pcolor(x, y, zdB[index,:,:],
191 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
196 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
192 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
197 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
193 ticksize=9, cblabel='')
198 ticksize=9, cblabel='')
194
199
195 if self.__showprofile:
200 if self.__showprofile:
196 axes = self.axesList[i*self.__nsubplots +1]
201 axes = self.axesList[i*self.__nsubplots +1]
197 axes.pline(avgdB[index,:], y,
202 axes.pline(avgdB[index,:], y,
198 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
203 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
199 xlabel='dB', ylabel='', title='',
204 xlabel='dB', ylabel='', title='',
200 ytick_visible=False,
205 ytick_visible=False,
201 grid='x')
206 grid='x')
202
207
203 noiseline = numpy.repeat(noisedB[index], len(y))
208 noiseline = numpy.repeat(noisedB[index], len(y))
204 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
209 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
205
210
206 self.draw()
211 self.draw()
207
212
208 if figfile == None:
213 if figfile == None:
209 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
214 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
210 name = str_datetime
215 name = str_datetime
211 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
216 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
212 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
217 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
213 figfile = self.getFilename(name)
218 figfile = self.getFilename(name)
214
219
215 self.save(figpath=figpath,
220 self.save(figpath=figpath,
216 figfile=figfile,
221 figfile=figfile,
217 save=save,
222 save=save,
218 ftp=ftp,
223 ftp=ftp,
219 wr_period=wr_period,
224 wr_period=wr_period,
220 thisDatetime=thisDatetime)
225 thisDatetime=thisDatetime)
221
226
227 return dataOut
228 @MPDecorator
222 class CrossSpectraPlot(Figure):
229 class CrossSpectraPlot(Figure):
223
230
224 isConfig = None
231 isConfig = None
225 __nsubplots = None
232 __nsubplots = None
226
233
227 WIDTH = None
234 WIDTH = None
228 HEIGHT = None
235 HEIGHT = None
229 WIDTHPROF = None
236 WIDTHPROF = None
230 HEIGHTPROF = None
237 HEIGHTPROF = None
231 PREFIX = 'cspc'
238 PREFIX = 'cspc'
232
239
233 def __init__(self, **kwargs):
240 def __init__(self):#, **kwargs):
234 Figure.__init__(self, **kwargs)
241 Figure.__init__(self)#, **kwargs)
235 self.isConfig = False
242 self.isConfig = False
236 self.__nsubplots = 4
243 self.__nsubplots = 4
237 self.counter_imagwr = 0
244 self.counter_imagwr = 0
238 self.WIDTH = 250
245 self.WIDTH = 250
239 self.HEIGHT = 250
246 self.HEIGHT = 250
240 self.WIDTHPROF = 0
247 self.WIDTHPROF = 0
241 self.HEIGHTPROF = 0
248 self.HEIGHTPROF = 0
242
249
243 self.PLOT_CODE = CROSS_CODE
250 self.PLOT_CODE = CROSS_CODE
244 self.FTP_WEI = None
251 self.FTP_WEI = None
245 self.EXP_CODE = None
252 self.EXP_CODE = None
246 self.SUB_EXP_CODE = None
253 self.SUB_EXP_CODE = None
247 self.PLOT_POS = None
254 self.PLOT_POS = None
248
255
249 def getSubplots(self):
256 def getSubplots(self):
250
257
251 ncol = 4
258 ncol = 4
252 nrow = self.nplots
259 nrow = self.nplots
253
260
254 return nrow, ncol
261 return nrow, ncol
255
262
256 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
263 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
257
264
258 self.__showprofile = showprofile
265 self.__showprofile = showprofile
259 self.nplots = nplots
266 self.nplots = nplots
260
267
261 ncolspan = 1
268 ncolspan = 1
262 colspan = 1
269 colspan = 1
263
270
264 self.createFigure(id = id,
271 self.createFigure(id = id,
265 wintitle = wintitle,
272 wintitle = wintitle,
266 widthplot = self.WIDTH + self.WIDTHPROF,
273 widthplot = self.WIDTH + self.WIDTHPROF,
267 heightplot = self.HEIGHT + self.HEIGHTPROF,
274 heightplot = self.HEIGHT + self.HEIGHTPROF,
268 show=True)
275 show=True)
269
276
270 nrow, ncol = self.getSubplots()
277 nrow, ncol = self.getSubplots()
271
278
272 counter = 0
279 counter = 0
273 for y in range(nrow):
280 for y in range(nrow):
274 for x in range(ncol):
281 for x in range(ncol):
275 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
282 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
276
283
277 counter += 1
284 counter += 1
278
285
279 def run(self, dataOut, id, wintitle="", pairsList=None,
286 def run(self, dataOut, id, wintitle="", pairsList=None,
280 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
287 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
281 coh_min=None, coh_max=None, phase_min=None, phase_max=None,
288 coh_min=None, coh_max=None, phase_min=None, phase_max=None,
282 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
289 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
283 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
290 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
284 server=None, folder=None, username=None, password=None,
291 server=None, folder=None, username=None, password=None,
285 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None,
292 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None,
286 xaxis='frequency'):
293 xaxis='frequency'):
287
294
288 """
295 """
289
296
290 Input:
297 Input:
291 dataOut :
298 dataOut :
292 id :
299 id :
293 wintitle :
300 wintitle :
294 channelList :
301 channelList :
295 showProfile :
302 showProfile :
296 xmin : None,
303 xmin : None,
297 xmax : None,
304 xmax : None,
298 ymin : None,
305 ymin : None,
299 ymax : None,
306 ymax : None,
300 zmin : None,
307 zmin : None,
301 zmax : None
308 zmax : None
302 """
309 """
303
310
311 if dataOut.flagNoData:
312 return dataOut
313
304 if pairsList == None:
314 if pairsList == None:
305 pairsIndexList = dataOut.pairsIndexList
315 pairsIndexList = dataOut.pairsIndexList
306 else:
316 else:
307 pairsIndexList = []
317 pairsIndexList = []
308 for pair in pairsList:
318 for pair in pairsList:
309 if pair not in dataOut.pairsList:
319 if pair not in dataOut.pairsList:
310 raise ValueError("Pair %s is not in dataOut.pairsList" %str(pair))
320 raise ValueError("Pair %s is not in dataOut.pairsList" %str(pair))
311 pairsIndexList.append(dataOut.pairsList.index(pair))
321 pairsIndexList.append(dataOut.pairsList.index(pair))
312
322
313 if not pairsIndexList:
323 if not pairsIndexList:
314 return
324 return
315
325
316 if len(pairsIndexList) > 4:
326 if len(pairsIndexList) > 4:
317 pairsIndexList = pairsIndexList[0:4]
327 pairsIndexList = pairsIndexList[0:4]
318
328
319 if normFactor is None:
329 if normFactor is None:
320 factor = dataOut.normFactor
330 factor = dataOut.normFactor
321 else:
331 else:
322 factor = normFactor
332 factor = normFactor
323 x = dataOut.getVelRange(1)
333 x = dataOut.getVelRange(1)
324 y = dataOut.getHeiRange()
334 y = dataOut.getHeiRange()
325 z = dataOut.data_spc[:,:,:]/factor
335 z = dataOut.data_spc[:,:,:]/factor
326 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
336 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
327
337
328 noise = dataOut.noise/factor
338 noise = dataOut.noise/factor
329
339
330 zdB = 10*numpy.log10(z)
340 zdB = 10*numpy.log10(z)
331 noisedB = 10*numpy.log10(noise)
341 noisedB = 10*numpy.log10(noise)
332
342
333 if coh_min == None:
343 if coh_min == None:
334 coh_min = 0.0
344 coh_min = 0.0
335 if coh_max == None:
345 if coh_max == None:
336 coh_max = 1.0
346 coh_max = 1.0
337
347
338 if phase_min == None:
348 if phase_min == None:
339 phase_min = -180
349 phase_min = -180
340 if phase_max == None:
350 if phase_max == None:
341 phase_max = 180
351 phase_max = 180
342
352
343 #thisDatetime = dataOut.datatime
353 #thisDatetime = dataOut.datatime
344 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
354 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
345 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
355 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
346 # xlabel = "Velocity (m/s)"
356 # xlabel = "Velocity (m/s)"
347 ylabel = "Range (Km)"
357 ylabel = "Range (Km)"
348
358
349 if xaxis == "frequency":
359 if xaxis == "frequency":
350 x = dataOut.getFreqRange(1)/1000.
360 x = dataOut.getFreqRange(1)/1000.
351 xlabel = "Frequency (kHz)"
361 xlabel = "Frequency (kHz)"
352
362
353 elif xaxis == "time":
363 elif xaxis == "time":
354 x = dataOut.getAcfRange(1)
364 x = dataOut.getAcfRange(1)
355 xlabel = "Time (ms)"
365 xlabel = "Time (ms)"
356
366
357 else:
367 else:
358 x = dataOut.getVelRange(1)
368 x = dataOut.getVelRange(1)
359 xlabel = "Velocity (m/s)"
369 xlabel = "Velocity (m/s)"
360
370
361 if not self.isConfig:
371 if not self.isConfig:
362
372
363 nplots = len(pairsIndexList)
373 nplots = len(pairsIndexList)
364
374
365 self.setup(id=id,
375 self.setup(id=id,
366 nplots=nplots,
376 nplots=nplots,
367 wintitle=wintitle,
377 wintitle=wintitle,
368 showprofile=False,
378 showprofile=False,
369 show=show)
379 show=show)
370
380
371 avg = numpy.abs(numpy.average(z, axis=1))
381 avg = numpy.abs(numpy.average(z, axis=1))
372 avgdB = 10*numpy.log10(avg)
382 avgdB = 10*numpy.log10(avg)
373
383
374 if xmin == None: xmin = numpy.nanmin(x)
384 if xmin == None: xmin = numpy.nanmin(x)
375 if xmax == None: xmax = numpy.nanmax(x)
385 if xmax == None: xmax = numpy.nanmax(x)
376 if ymin == None: ymin = numpy.nanmin(y)
386 if ymin == None: ymin = numpy.nanmin(y)
377 if ymax == None: ymax = numpy.nanmax(y)
387 if ymax == None: ymax = numpy.nanmax(y)
378 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
388 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
379 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
389 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
380
390
381 self.FTP_WEI = ftp_wei
391 self.FTP_WEI = ftp_wei
382 self.EXP_CODE = exp_code
392 self.EXP_CODE = exp_code
383 self.SUB_EXP_CODE = sub_exp_code
393 self.SUB_EXP_CODE = sub_exp_code
384 self.PLOT_POS = plot_pos
394 self.PLOT_POS = plot_pos
385
395
386 self.isConfig = True
396 self.isConfig = True
387
397
388 self.setWinTitle(title)
398 self.setWinTitle(title)
389
399
390 for i in range(self.nplots):
400 for i in range(self.nplots):
391 pair = dataOut.pairsList[pairsIndexList[i]]
401 pair = dataOut.pairsList[pairsIndexList[i]]
392
402
393 chan_index0 = dataOut.channelList.index(pair[0])
403 chan_index0 = dataOut.channelList.index(pair[0])
394 chan_index1 = dataOut.channelList.index(pair[1])
404 chan_index1 = dataOut.channelList.index(pair[1])
395
405
396 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
406 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
397 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[chan_index0], str_datetime)
407 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[chan_index0], str_datetime)
398 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index0,:,:]/factor)
408 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index0,:,:]/factor)
399 axes0 = self.axesList[i*self.__nsubplots]
409 axes0 = self.axesList[i*self.__nsubplots]
400 axes0.pcolor(x, y, zdB,
410 axes0.pcolor(x, y, zdB,
401 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
411 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
402 xlabel=xlabel, ylabel=ylabel, title=title,
412 xlabel=xlabel, ylabel=ylabel, title=title,
403 ticksize=9, colormap=power_cmap, cblabel='')
413 ticksize=9, colormap=power_cmap, cblabel='')
404
414
405 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[chan_index1], str_datetime)
415 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[chan_index1], str_datetime)
406 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index1,:,:]/factor)
416 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index1,:,:]/factor)
407 axes0 = self.axesList[i*self.__nsubplots+1]
417 axes0 = self.axesList[i*self.__nsubplots+1]
408 axes0.pcolor(x, y, zdB,
418 axes0.pcolor(x, y, zdB,
409 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
419 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
410 xlabel=xlabel, ylabel=ylabel, title=title,
420 xlabel=xlabel, ylabel=ylabel, title=title,
411 ticksize=9, colormap=power_cmap, cblabel='')
421 ticksize=9, colormap=power_cmap, cblabel='')
412
422
413 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:])
423 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:])
414 coherence = numpy.abs(coherenceComplex)
424 coherence = numpy.abs(coherenceComplex)
415 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
425 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
416 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
426 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
417
427
418 title = "Coherence Ch%d * Ch%d" %(pair[0], pair[1])
428 title = "Coherence Ch%d * Ch%d" %(pair[0], pair[1])
419 axes0 = self.axesList[i*self.__nsubplots+2]
429 axes0 = self.axesList[i*self.__nsubplots+2]
420 axes0.pcolor(x, y, coherence,
430 axes0.pcolor(x, y, coherence,
421 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=coh_min, zmax=coh_max,
431 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=coh_min, zmax=coh_max,
422 xlabel=xlabel, ylabel=ylabel, title=title,
432 xlabel=xlabel, ylabel=ylabel, title=title,
423 ticksize=9, colormap=coherence_cmap, cblabel='')
433 ticksize=9, colormap=coherence_cmap, cblabel='')
424
434
425 title = "Phase Ch%d * Ch%d" %(pair[0], pair[1])
435 title = "Phase Ch%d * Ch%d" %(pair[0], pair[1])
426 axes0 = self.axesList[i*self.__nsubplots+3]
436 axes0 = self.axesList[i*self.__nsubplots+3]
427 axes0.pcolor(x, y, phase,
437 axes0.pcolor(x, y, phase,
428 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
438 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
429 xlabel=xlabel, ylabel=ylabel, title=title,
439 xlabel=xlabel, ylabel=ylabel, title=title,
430 ticksize=9, colormap=phase_cmap, cblabel='')
440 ticksize=9, colormap=phase_cmap, cblabel='')
431
441
432
442
433
443
434 self.draw()
444 self.draw()
435
445
436 self.save(figpath=figpath,
446 self.save(figpath=figpath,
437 figfile=figfile,
447 figfile=figfile,
438 save=save,
448 save=save,
439 ftp=ftp,
449 ftp=ftp,
440 wr_period=wr_period,
450 wr_period=wr_period,
441 thisDatetime=thisDatetime)
451 thisDatetime=thisDatetime)
442
452
453 return dataOut
443
454
455 @MPDecorator
444 class RTIPlot(Figure):
456 class RTIPlot(Figure):
445
457
446 __isConfig = None
458 __isConfig = None
447 __nsubplots = None
459 __nsubplots = None
448
460
449 WIDTHPROF = None
461 WIDTHPROF = None
450 HEIGHTPROF = None
462 HEIGHTPROF = None
451 PREFIX = 'rti'
463 PREFIX = 'rti'
452
464
453 def __init__(self, **kwargs):
465 def __init__(self):#, **kwargs):
454
466
455 Figure.__init__(self, **kwargs)
467 Figure.__init__(self)#, **kwargs)
456 self.timerange = None
468 self.timerange = None
457 self.isConfig = False
469 self.isConfig = False
458 self.__nsubplots = 1
470 self.__nsubplots = 1
459
471
460 self.WIDTH = 800
472 self.WIDTH = 800
461 self.HEIGHT = 180
473 self.HEIGHT = 180
462 self.WIDTHPROF = 120
474 self.WIDTHPROF = 120
463 self.HEIGHTPROF = 0
475 self.HEIGHTPROF = 0
464 self.counter_imagwr = 0
476 self.counter_imagwr = 0
465
477
466 self.PLOT_CODE = RTI_CODE
478 self.PLOT_CODE = RTI_CODE
467
479
468 self.FTP_WEI = None
480 self.FTP_WEI = None
469 self.EXP_CODE = None
481 self.EXP_CODE = None
470 self.SUB_EXP_CODE = None
482 self.SUB_EXP_CODE = None
471 self.PLOT_POS = None
483 self.PLOT_POS = None
472 self.tmin = None
484 self.tmin = None
473 self.tmax = None
485 self.tmax = None
474
486
475 self.xmin = None
487 self.xmin = None
476 self.xmax = None
488 self.xmax = None
477
489
478 self.figfile = None
490 self.figfile = None
479
491
480 def getSubplots(self):
492 def getSubplots(self):
481
493
482 ncol = 1
494 ncol = 1
483 nrow = self.nplots
495 nrow = self.nplots
484
496
485 return nrow, ncol
497 return nrow, ncol
486
498
487 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
499 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
488
500
489 self.__showprofile = showprofile
501 self.__showprofile = showprofile
490 self.nplots = nplots
502 self.nplots = nplots
491
503
492 ncolspan = 1
504 ncolspan = 1
493 colspan = 1
505 colspan = 1
494 if showprofile:
506 if showprofile:
495 ncolspan = 7
507 ncolspan = 7
496 colspan = 6
508 colspan = 6
497 self.__nsubplots = 2
509 self.__nsubplots = 2
498
510
499 self.createFigure(id = id,
511 self.createFigure(id = id,
500 wintitle = wintitle,
512 wintitle = wintitle,
501 widthplot = self.WIDTH + self.WIDTHPROF,
513 widthplot = self.WIDTH + self.WIDTHPROF,
502 heightplot = self.HEIGHT + self.HEIGHTPROF,
514 heightplot = self.HEIGHT + self.HEIGHTPROF,
503 show=show)
515 show=show)
504
516
505 nrow, ncol = self.getSubplots()
517 nrow, ncol = self.getSubplots()
506
518
507 counter = 0
519 counter = 0
508 for y in range(nrow):
520 for y in range(nrow):
509 for x in range(ncol):
521 for x in range(ncol):
510
522
511 if counter >= self.nplots:
523 if counter >= self.nplots:
512 break
524 break
513
525
514 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
526 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
515
527
516 if showprofile:
528 if showprofile:
517 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
529 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
518
530
519 counter += 1
531 counter += 1
520
532
521 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
533 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
522 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
534 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
523 timerange=None, colormap='jet',
535 timerange=None, colormap='jet',
524 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
536 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
525 server=None, folder=None, username=None, password=None,
537 server=None, folder=None, username=None, password=None,
526 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None, HEIGHT=None):
538 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None, HEIGHT=None):
527
539
528 """
540 """
529
541
530 Input:
542 Input:
531 dataOut :
543 dataOut :
532 id :
544 id :
533 wintitle :
545 wintitle :
534 channelList :
546 channelList :
535 showProfile :
547 showProfile :
536 xmin : None,
548 xmin : None,
537 xmax : None,
549 xmax : None,
538 ymin : None,
550 ymin : None,
539 ymax : None,
551 ymax : None,
540 zmin : None,
552 zmin : None,
541 zmax : None
553 zmax : None
542 """
554 """
555 if dataOut.flagNoData:
556 return dataOut
543
557
544 #colormap = kwargs.get('colormap', 'jet')
558 #colormap = kwargs.get('colormap', 'jet')
545 if HEIGHT is not None:
559 if HEIGHT is not None:
546 self.HEIGHT = HEIGHT
560 self.HEIGHT = HEIGHT
547
561
548 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
562 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
549 return
563 return
550
564
551 if channelList == None:
565 if channelList == None:
552 channelIndexList = dataOut.channelIndexList
566 channelIndexList = dataOut.channelIndexList
553 else:
567 else:
554 channelIndexList = []
568 channelIndexList = []
555 for channel in channelList:
569 for channel in channelList:
556 if channel not in dataOut.channelList:
570 if channel not in dataOut.channelList:
557 raise ValueError("Channel %d is not in dataOut.channelList")
571 raise ValueError("Channel %d is not in dataOut.channelList")
558 channelIndexList.append(dataOut.channelList.index(channel))
572 channelIndexList.append(dataOut.channelList.index(channel))
559
573
560 if normFactor is None:
574 if normFactor is None:
561 factor = dataOut.normFactor
575 factor = dataOut.normFactor
562 else:
576 else:
563 factor = normFactor
577 factor = normFactor
564
578
565 # factor = dataOut.normFactor
579 # factor = dataOut.normFactor
566 x = dataOut.getTimeRange()
580 x = dataOut.getTimeRange()
567 y = dataOut.getHeiRange()
581 y = dataOut.getHeiRange()
568
582
569 z = dataOut.data_spc/factor
583 z = dataOut.data_spc/factor
570 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
584 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
571 avg = numpy.average(z, axis=1)
585 avg = numpy.average(z, axis=1)
572 avgdB = 10.*numpy.log10(avg)
586 avgdB = 10.*numpy.log10(avg)
573 # avgdB = dataOut.getPower()
587 # avgdB = dataOut.getPower()
574
588
575
589
576 thisDatetime = dataOut.datatime
590 thisDatetime = dataOut.datatime
577 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
591 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
578 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
592 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
579 xlabel = ""
593 xlabel = ""
580 ylabel = "Range (Km)"
594 ylabel = "Range (Km)"
581
595
582 update_figfile = False
596 update_figfile = False
583
597
584 if self.xmax is not None and dataOut.ltctime >= self.xmax: #yong
598 if self.xmax is not None and dataOut.ltctime >= self.xmax: #yong
585 self.counter_imagwr = wr_period
599 self.counter_imagwr = wr_period
586 self.isConfig = False
600 self.isConfig = False
587 update_figfile = True
601 update_figfile = True
588
602
589 if not self.isConfig:
603 if not self.isConfig:
590
604
591 nplots = len(channelIndexList)
605 nplots = len(channelIndexList)
592
606
593 self.setup(id=id,
607 self.setup(id=id,
594 nplots=nplots,
608 nplots=nplots,
595 wintitle=wintitle,
609 wintitle=wintitle,
596 showprofile=showprofile,
610 showprofile=showprofile,
597 show=show)
611 show=show)
598
612
599 if timerange != None:
613 if timerange != None:
600 self.timerange = timerange
614 self.timerange = timerange
601
615
602 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
616 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
603
617
604 noise = dataOut.noise/factor
618 noise = dataOut.noise/factor
605 noisedB = 10*numpy.log10(noise)
619 noisedB = 10*numpy.log10(noise)
606
620
607 if ymin == None: ymin = numpy.nanmin(y)
621 if ymin == None: ymin = numpy.nanmin(y)
608 if ymax == None: ymax = numpy.nanmax(y)
622 if ymax == None: ymax = numpy.nanmax(y)
609 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
623 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
610 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
624 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
611
625
612 self.FTP_WEI = ftp_wei
626 self.FTP_WEI = ftp_wei
613 self.EXP_CODE = exp_code
627 self.EXP_CODE = exp_code
614 self.SUB_EXP_CODE = sub_exp_code
628 self.SUB_EXP_CODE = sub_exp_code
615 self.PLOT_POS = plot_pos
629 self.PLOT_POS = plot_pos
616
630
617 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
631 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
618 self.isConfig = True
632 self.isConfig = True
619 self.figfile = figfile
633 self.figfile = figfile
620 update_figfile = True
634 update_figfile = True
621
635
622 self.setWinTitle(title)
636 self.setWinTitle(title)
623
637
624 for i in range(self.nplots):
638 for i in range(self.nplots):
625 index = channelIndexList[i]
639 index = channelIndexList[i]
626 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
640 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
627 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
641 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
628 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
642 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
629 axes = self.axesList[i*self.__nsubplots]
643 axes = self.axesList[i*self.__nsubplots]
630 zdB = avgdB[index].reshape((1,-1))
644 zdB = avgdB[index].reshape((1,-1))
631 axes.pcolorbuffer(x, y, zdB,
645 axes.pcolorbuffer(x, y, zdB,
632 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
646 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
633 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
647 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
634 ticksize=9, cblabel='', cbsize="1%", colormap=colormap)
648 ticksize=9, cblabel='', cbsize="1%", colormap=colormap)
635
649
636 if self.__showprofile:
650 if self.__showprofile:
637 axes = self.axesList[i*self.__nsubplots +1]
651 axes = self.axesList[i*self.__nsubplots +1]
638 axes.pline(avgdB[index], y,
652 axes.pline(avgdB[index], y,
639 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
653 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
640 xlabel='dB', ylabel='', title='',
654 xlabel='dB', ylabel='', title='',
641 ytick_visible=False,
655 ytick_visible=False,
642 grid='x')
656 grid='x')
643
657
644 self.draw()
658 self.draw()
645
659
646 self.save(figpath=figpath,
660 self.save(figpath=figpath,
647 figfile=figfile,
661 figfile=figfile,
648 save=save,
662 save=save,
649 ftp=ftp,
663 ftp=ftp,
650 wr_period=wr_period,
664 wr_period=wr_period,
651 thisDatetime=thisDatetime,
665 thisDatetime=thisDatetime,
652 update_figfile=update_figfile)
666 update_figfile=update_figfile)
667 return dataOut
653
668
669 @MPDecorator
654 class CoherenceMap(Figure):
670 class CoherenceMap(Figure):
655 isConfig = None
671 isConfig = None
656 __nsubplots = None
672 __nsubplots = None
657
673
658 WIDTHPROF = None
674 WIDTHPROF = None
659 HEIGHTPROF = None
675 HEIGHTPROF = None
660 PREFIX = 'cmap'
676 PREFIX = 'cmap'
661
677
662 def __init__(self, **kwargs):
678 def __init__(self):#, **kwargs):
663 Figure.__init__(self, **kwargs)
679 Figure.__init__(self)#, **kwargs)
664 self.timerange = 2*60*60
680 self.timerange = 2*60*60
665 self.isConfig = False
681 self.isConfig = False
666 self.__nsubplots = 1
682 self.__nsubplots = 1
667
683
668 self.WIDTH = 800
684 self.WIDTH = 800
669 self.HEIGHT = 180
685 self.HEIGHT = 180
670 self.WIDTHPROF = 120
686 self.WIDTHPROF = 120
671 self.HEIGHTPROF = 0
687 self.HEIGHTPROF = 0
672 self.counter_imagwr = 0
688 self.counter_imagwr = 0
673
689
674 self.PLOT_CODE = COH_CODE
690 self.PLOT_CODE = COH_CODE
675
691
676 self.FTP_WEI = None
692 self.FTP_WEI = None
677 self.EXP_CODE = None
693 self.EXP_CODE = None
678 self.SUB_EXP_CODE = None
694 self.SUB_EXP_CODE = None
679 self.PLOT_POS = None
695 self.PLOT_POS = None
680 self.counter_imagwr = 0
696 self.counter_imagwr = 0
681
697
682 self.xmin = None
698 self.xmin = None
683 self.xmax = None
699 self.xmax = None
684
700
685 def getSubplots(self):
701 def getSubplots(self):
686 ncol = 1
702 ncol = 1
687 nrow = self.nplots*2
703 nrow = self.nplots*2
688
704
689 return nrow, ncol
705 return nrow, ncol
690
706
691 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
707 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
692 self.__showprofile = showprofile
708 self.__showprofile = showprofile
693 self.nplots = nplots
709 self.nplots = nplots
694
710
695 ncolspan = 1
711 ncolspan = 1
696 colspan = 1
712 colspan = 1
697 if showprofile:
713 if showprofile:
698 ncolspan = 7
714 ncolspan = 7
699 colspan = 6
715 colspan = 6
700 self.__nsubplots = 2
716 self.__nsubplots = 2
701
717
702 self.createFigure(id = id,
718 self.createFigure(id = id,
703 wintitle = wintitle,
719 wintitle = wintitle,
704 widthplot = self.WIDTH + self.WIDTHPROF,
720 widthplot = self.WIDTH + self.WIDTHPROF,
705 heightplot = self.HEIGHT + self.HEIGHTPROF,
721 heightplot = self.HEIGHT + self.HEIGHTPROF,
706 show=True)
722 show=True)
707
723
708 nrow, ncol = self.getSubplots()
724 nrow, ncol = self.getSubplots()
709
725
710 for y in range(nrow):
726 for y in range(nrow):
711 for x in range(ncol):
727 for x in range(ncol):
712
728
713 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
729 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
714
730
715 if showprofile:
731 if showprofile:
716 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
732 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
717
733
718 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
734 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
719 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
735 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
720 timerange=None, phase_min=None, phase_max=None,
736 timerange=None, phase_min=None, phase_max=None,
721 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
737 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
722 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
738 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
723 server=None, folder=None, username=None, password=None,
739 server=None, folder=None, username=None, password=None,
724 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
740 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
725
741
742
743 if dataOut.flagNoData:
744 return dataOut
745
726 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
746 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
727 return
747 return
728
748
729 if pairsList == None:
749 if pairsList == None:
730 pairsIndexList = dataOut.pairsIndexList
750 pairsIndexList = dataOut.pairsIndexList
731 else:
751 else:
732 pairsIndexList = []
752 pairsIndexList = []
733 for pair in pairsList:
753 for pair in pairsList:
734 if pair not in dataOut.pairsList:
754 if pair not in dataOut.pairsList:
735 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
755 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
736 pairsIndexList.append(dataOut.pairsList.index(pair))
756 pairsIndexList.append(dataOut.pairsList.index(pair))
737
757
738 if pairsIndexList == []:
758 if pairsIndexList == []:
739 return
759 return
740
760
741 if len(pairsIndexList) > 4:
761 if len(pairsIndexList) > 4:
742 pairsIndexList = pairsIndexList[0:4]
762 pairsIndexList = pairsIndexList[0:4]
743
763
744 if phase_min == None:
764 if phase_min == None:
745 phase_min = -180
765 phase_min = -180
746 if phase_max == None:
766 if phase_max == None:
747 phase_max = 180
767 phase_max = 180
748
768
749 x = dataOut.getTimeRange()
769 x = dataOut.getTimeRange()
750 y = dataOut.getHeiRange()
770 y = dataOut.getHeiRange()
751
771
752 thisDatetime = dataOut.datatime
772 thisDatetime = dataOut.datatime
753
773
754 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
774 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
755 xlabel = ""
775 xlabel = ""
756 ylabel = "Range (Km)"
776 ylabel = "Range (Km)"
757 update_figfile = False
777 update_figfile = False
758
778
759 if not self.isConfig:
779 if not self.isConfig:
760 nplots = len(pairsIndexList)
780 nplots = len(pairsIndexList)
761 self.setup(id=id,
781 self.setup(id=id,
762 nplots=nplots,
782 nplots=nplots,
763 wintitle=wintitle,
783 wintitle=wintitle,
764 showprofile=showprofile,
784 showprofile=showprofile,
765 show=show)
785 show=show)
766
786
767 if timerange != None:
787 if timerange != None:
768 self.timerange = timerange
788 self.timerange = timerange
769
789
770 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
790 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
771
791
772 if ymin == None: ymin = numpy.nanmin(y)
792 if ymin == None: ymin = numpy.nanmin(y)
773 if ymax == None: ymax = numpy.nanmax(y)
793 if ymax == None: ymax = numpy.nanmax(y)
774 if zmin == None: zmin = 0.
794 if zmin == None: zmin = 0.
775 if zmax == None: zmax = 1.
795 if zmax == None: zmax = 1.
776
796
777 self.FTP_WEI = ftp_wei
797 self.FTP_WEI = ftp_wei
778 self.EXP_CODE = exp_code
798 self.EXP_CODE = exp_code
779 self.SUB_EXP_CODE = sub_exp_code
799 self.SUB_EXP_CODE = sub_exp_code
780 self.PLOT_POS = plot_pos
800 self.PLOT_POS = plot_pos
781
801
782 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
802 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
783
803
784 self.isConfig = True
804 self.isConfig = True
785 update_figfile = True
805 update_figfile = True
786
806
787 self.setWinTitle(title)
807 self.setWinTitle(title)
788
808
789 for i in range(self.nplots):
809 for i in range(self.nplots):
790
810
791 pair = dataOut.pairsList[pairsIndexList[i]]
811 pair = dataOut.pairsList[pairsIndexList[i]]
792
812
793 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
813 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
794 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
814 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
795 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
815 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
796
816
797
817
798 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
818 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
799 coherence = numpy.abs(avgcoherenceComplex)
819 coherence = numpy.abs(avgcoherenceComplex)
800
820
801 z = coherence.reshape((1,-1))
821 z = coherence.reshape((1,-1))
802
822
803 counter = 0
823 counter = 0
804
824
805 title = "Coherence Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
825 title = "Coherence Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
806 axes = self.axesList[i*self.__nsubplots*2]
826 axes = self.axesList[i*self.__nsubplots*2]
807 axes.pcolorbuffer(x, y, z,
827 axes.pcolorbuffer(x, y, z,
808 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
828 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
809 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
829 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
810 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
830 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
811
831
812 if self.__showprofile:
832 if self.__showprofile:
813 counter += 1
833 counter += 1
814 axes = self.axesList[i*self.__nsubplots*2 + counter]
834 axes = self.axesList[i*self.__nsubplots*2 + counter]
815 axes.pline(coherence, y,
835 axes.pline(coherence, y,
816 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
836 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
817 xlabel='', ylabel='', title='', ticksize=7,
837 xlabel='', ylabel='', title='', ticksize=7,
818 ytick_visible=False, nxticks=5,
838 ytick_visible=False, nxticks=5,
819 grid='x')
839 grid='x')
820
840
821 counter += 1
841 counter += 1
822
842
823 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
843 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
824
844
825 z = phase.reshape((1,-1))
845 z = phase.reshape((1,-1))
826
846
827 title = "Phase Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
847 title = "Phase Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
828 axes = self.axesList[i*self.__nsubplots*2 + counter]
848 axes = self.axesList[i*self.__nsubplots*2 + counter]
829 axes.pcolorbuffer(x, y, z,
849 axes.pcolorbuffer(x, y, z,
830 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
850 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
831 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
851 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
832 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
852 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
833
853
834 if self.__showprofile:
854 if self.__showprofile:
835 counter += 1
855 counter += 1
836 axes = self.axesList[i*self.__nsubplots*2 + counter]
856 axes = self.axesList[i*self.__nsubplots*2 + counter]
837 axes.pline(phase, y,
857 axes.pline(phase, y,
838 xmin=phase_min, xmax=phase_max, ymin=ymin, ymax=ymax,
858 xmin=phase_min, xmax=phase_max, ymin=ymin, ymax=ymax,
839 xlabel='', ylabel='', title='', ticksize=7,
859 xlabel='', ylabel='', title='', ticksize=7,
840 ytick_visible=False, nxticks=4,
860 ytick_visible=False, nxticks=4,
841 grid='x')
861 grid='x')
842
862
843 self.draw()
863 self.draw()
844
864
845 if dataOut.ltctime >= self.xmax:
865 if dataOut.ltctime >= self.xmax:
846 self.counter_imagwr = wr_period
866 self.counter_imagwr = wr_period
847 self.isConfig = False
867 self.isConfig = False
848 update_figfile = True
868 update_figfile = True
849
869
850 self.save(figpath=figpath,
870 self.save(figpath=figpath,
851 figfile=figfile,
871 figfile=figfile,
852 save=save,
872 save=save,
853 ftp=ftp,
873 ftp=ftp,
854 wr_period=wr_period,
874 wr_period=wr_period,
855 thisDatetime=thisDatetime,
875 thisDatetime=thisDatetime,
856 update_figfile=update_figfile)
876 update_figfile=update_figfile)
857
877
878 return dataOut
879
880 @MPDecorator
858 class PowerProfilePlot(Figure):
881 class PowerProfilePlot(Figure):
859
882
860 isConfig = None
883 isConfig = None
861 __nsubplots = None
884 __nsubplots = None
862
885
863 WIDTHPROF = None
886 WIDTHPROF = None
864 HEIGHTPROF = None
887 HEIGHTPROF = None
865 PREFIX = 'spcprofile'
888 PREFIX = 'spcprofile'
866
889
867 def __init__(self, **kwargs):
890 def __init__(self):#, **kwargs):
868 Figure.__init__(self, **kwargs)
891 Figure.__init__(self)#, **kwargs)
869 self.isConfig = False
892 self.isConfig = False
870 self.__nsubplots = 1
893 self.__nsubplots = 1
871
894
872 self.PLOT_CODE = POWER_CODE
895 self.PLOT_CODE = POWER_CODE
873
896
874 self.WIDTH = 300
897 self.WIDTH = 300
875 self.HEIGHT = 500
898 self.HEIGHT = 500
876 self.counter_imagwr = 0
899 self.counter_imagwr = 0
877
900
878 def getSubplots(self):
901 def getSubplots(self):
879 ncol = 1
902 ncol = 1
880 nrow = 1
903 nrow = 1
881
904
882 return nrow, ncol
905 return nrow, ncol
883
906
884 def setup(self, id, nplots, wintitle, show):
907 def setup(self, id, nplots, wintitle, show):
885
908
886 self.nplots = nplots
909 self.nplots = nplots
887
910
888 ncolspan = 1
911 ncolspan = 1
889 colspan = 1
912 colspan = 1
890
913
891 self.createFigure(id = id,
914 self.createFigure(id = id,
892 wintitle = wintitle,
915 wintitle = wintitle,
893 widthplot = self.WIDTH,
916 widthplot = self.WIDTH,
894 heightplot = self.HEIGHT,
917 heightplot = self.HEIGHT,
895 show=show)
918 show=show)
896
919
897 nrow, ncol = self.getSubplots()
920 nrow, ncol = self.getSubplots()
898
921
899 counter = 0
922 counter = 0
900 for y in range(nrow):
923 for y in range(nrow):
901 for x in range(ncol):
924 for x in range(ncol):
902 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
925 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
903
926
904 def run(self, dataOut, id, wintitle="", channelList=None,
927 def run(self, dataOut, id, wintitle="", channelList=None,
905 xmin=None, xmax=None, ymin=None, ymax=None,
928 xmin=None, xmax=None, ymin=None, ymax=None,
906 save=False, figpath='./', figfile=None, show=True,
929 save=False, figpath='./', figfile=None, show=True,
907 ftp=False, wr_period=1, server=None,
930 ftp=False, wr_period=1, server=None,
908 folder=None, username=None, password=None):
931 folder=None, username=None, password=None):
909
932
933 if dataOut.flagNoData:
934 return dataOut
935
910
936
911 if channelList == None:
937 if channelList == None:
912 channelIndexList = dataOut.channelIndexList
938 channelIndexList = dataOut.channelIndexList
913 channelList = dataOut.channelList
939 channelList = dataOut.channelList
914 else:
940 else:
915 channelIndexList = []
941 channelIndexList = []
916 for channel in channelList:
942 for channel in channelList:
917 if channel not in dataOut.channelList:
943 if channel not in dataOut.channelList:
918 raise ValueError("Channel %d is not in dataOut.channelList")
944 raise ValueError("Channel %d is not in dataOut.channelList")
919 channelIndexList.append(dataOut.channelList.index(channel))
945 channelIndexList.append(dataOut.channelList.index(channel))
920
946
921 factor = dataOut.normFactor
947 factor = dataOut.normFactor
922
948
923 y = dataOut.getHeiRange()
949 y = dataOut.getHeiRange()
924
950
925 #for voltage
951 #for voltage
926 if dataOut.type == 'Voltage':
952 if dataOut.type == 'Voltage':
927 x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
953 x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
928 x = x.real
954 x = x.real
929 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
955 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
930
956
931 #for spectra
957 #for spectra
932 if dataOut.type == 'Spectra':
958 if dataOut.type == 'Spectra':
933 x = dataOut.data_spc[channelIndexList,:,:]/factor
959 x = dataOut.data_spc[channelIndexList,:,:]/factor
934 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
960 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
935 x = numpy.average(x, axis=1)
961 x = numpy.average(x, axis=1)
936
962
937
963
938 xdB = 10*numpy.log10(x)
964 xdB = 10*numpy.log10(x)
939
965
940 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
966 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
941 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
967 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
942 xlabel = "dB"
968 xlabel = "dB"
943 ylabel = "Range (Km)"
969 ylabel = "Range (Km)"
944
970
945 if not self.isConfig:
971 if not self.isConfig:
946
972
947 nplots = 1
973 nplots = 1
948
974
949 self.setup(id=id,
975 self.setup(id=id,
950 nplots=nplots,
976 nplots=nplots,
951 wintitle=wintitle,
977 wintitle=wintitle,
952 show=show)
978 show=show)
953
979
954 if ymin == None: ymin = numpy.nanmin(y)
980 if ymin == None: ymin = numpy.nanmin(y)
955 if ymax == None: ymax = numpy.nanmax(y)
981 if ymax == None: ymax = numpy.nanmax(y)
956 if xmin == None: xmin = numpy.nanmin(xdB)*0.9
982 if xmin == None: xmin = numpy.nanmin(xdB)*0.9
957 if xmax == None: xmax = numpy.nanmax(xdB)*1.1
983 if xmax == None: xmax = numpy.nanmax(xdB)*1.1
958
984
959 self.isConfig = True
985 self.isConfig = True
960
986
961 self.setWinTitle(title)
987 self.setWinTitle(title)
962
988
963 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
989 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
964 axes = self.axesList[0]
990 axes = self.axesList[0]
965
991
966 legendlabels = ["channel %d"%x for x in channelList]
992 legendlabels = ["channel %d"%x for x in channelList]
967 axes.pmultiline(xdB, y,
993 axes.pmultiline(xdB, y,
968 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
994 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
969 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
995 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
970 ytick_visible=True, nxticks=5,
996 ytick_visible=True, nxticks=5,
971 grid='x')
997 grid='x')
972
998
973 self.draw()
999 self.draw()
974
1000
975 self.save(figpath=figpath,
1001 self.save(figpath=figpath,
976 figfile=figfile,
1002 figfile=figfile,
977 save=save,
1003 save=save,
978 ftp=ftp,
1004 ftp=ftp,
979 wr_period=wr_period,
1005 wr_period=wr_period,
980 thisDatetime=thisDatetime)
1006 thisDatetime=thisDatetime)
1007
1008 return dataOut
981
1009
1010 @MPDecorator
982 class SpectraCutPlot(Figure):
1011 class SpectraCutPlot(Figure):
983
1012
984 isConfig = None
1013 isConfig = None
985 __nsubplots = None
1014 __nsubplots = None
986
1015
987 WIDTHPROF = None
1016 WIDTHPROF = None
988 HEIGHTPROF = None
1017 HEIGHTPROF = None
989 PREFIX = 'spc_cut'
1018 PREFIX = 'spc_cut'
990
1019
991 def __init__(self, **kwargs):
1020 def __init__(self):#, **kwargs):
992 Figure.__init__(self, **kwargs)
1021 Figure.__init__(self)#, **kwargs)
993 self.isConfig = False
1022 self.isConfig = False
994 self.__nsubplots = 1
1023 self.__nsubplots = 1
995
1024
996 self.PLOT_CODE = POWER_CODE
1025 self.PLOT_CODE = POWER_CODE
997
1026
998 self.WIDTH = 700
1027 self.WIDTH = 700
999 self.HEIGHT = 500
1028 self.HEIGHT = 500
1000 self.counter_imagwr = 0
1029 self.counter_imagwr = 0
1001
1030
1002 def getSubplots(self):
1031 def getSubplots(self):
1003 ncol = 1
1032 ncol = 1
1004 nrow = 1
1033 nrow = 1
1005
1034
1006 return nrow, ncol
1035 return nrow, ncol
1007
1036
1008 def setup(self, id, nplots, wintitle, show):
1037 def setup(self, id, nplots, wintitle, show):
1009
1038
1010 self.nplots = nplots
1039 self.nplots = nplots
1011
1040
1012 ncolspan = 1
1041 ncolspan = 1
1013 colspan = 1
1042 colspan = 1
1014
1043
1015 self.createFigure(id = id,
1044 self.createFigure(id = id,
1016 wintitle = wintitle,
1045 wintitle = wintitle,
1017 widthplot = self.WIDTH,
1046 widthplot = self.WIDTH,
1018 heightplot = self.HEIGHT,
1047 heightplot = self.HEIGHT,
1019 show=show)
1048 show=show)
1020
1049
1021 nrow, ncol = self.getSubplots()
1050 nrow, ncol = self.getSubplots()
1022
1051
1023 counter = 0
1052 counter = 0
1024 for y in range(nrow):
1053 for y in range(nrow):
1025 for x in range(ncol):
1054 for x in range(ncol):
1026 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1055 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1027
1056
1028 def run(self, dataOut, id, wintitle="", channelList=None,
1057 def run(self, dataOut, id, wintitle="", channelList=None,
1029 xmin=None, xmax=None, ymin=None, ymax=None,
1058 xmin=None, xmax=None, ymin=None, ymax=None,
1030 save=False, figpath='./', figfile=None, show=True,
1059 save=False, figpath='./', figfile=None, show=True,
1031 ftp=False, wr_period=1, server=None,
1060 ftp=False, wr_period=1, server=None,
1032 folder=None, username=None, password=None,
1061 folder=None, username=None, password=None,
1033 xaxis="frequency"):
1062 xaxis="frequency"):
1034
1063
1064 if dataOut.flagNoData:
1065 return dataOut
1035
1066
1036 if channelList == None:
1067 if channelList == None:
1037 channelIndexList = dataOut.channelIndexList
1068 channelIndexList = dataOut.channelIndexList
1038 channelList = dataOut.channelList
1069 channelList = dataOut.channelList
1039 else:
1070 else:
1040 channelIndexList = []
1071 channelIndexList = []
1041 for channel in channelList:
1072 for channel in channelList:
1042 if channel not in dataOut.channelList:
1073 if channel not in dataOut.channelList:
1043 raise ValueError("Channel %d is not in dataOut.channelList")
1074 raise ValueError("Channel %d is not in dataOut.channelList")
1044 channelIndexList.append(dataOut.channelList.index(channel))
1075 channelIndexList.append(dataOut.channelList.index(channel))
1045
1076
1046 factor = dataOut.normFactor
1077 factor = dataOut.normFactor
1047
1078
1048 y = dataOut.getHeiRange()
1079 y = dataOut.getHeiRange()
1049
1080
1050 z = dataOut.data_spc/factor
1081 z = dataOut.data_spc/factor
1051 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1082 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1052
1083
1053 hei_index = numpy.arange(25)*3 + 20
1084 hei_index = numpy.arange(25)*3 + 20
1054
1085
1055 if xaxis == "frequency":
1086 if xaxis == "frequency":
1056 x = dataOut.getFreqRange()/1000.
1087 x = dataOut.getFreqRange()/1000.
1057 zdB = 10*numpy.log10(z[0,:,hei_index])
1088 zdB = 10*numpy.log10(z[0,:,hei_index])
1058 xlabel = "Frequency (kHz)"
1089 xlabel = "Frequency (kHz)"
1059 ylabel = "Power (dB)"
1090 ylabel = "Power (dB)"
1060
1091
1061 elif xaxis == "time":
1092 elif xaxis == "time":
1062 x = dataOut.getAcfRange()
1093 x = dataOut.getAcfRange()
1063 zdB = z[0,:,hei_index]
1094 zdB = z[0,:,hei_index]
1064 xlabel = "Time (ms)"
1095 xlabel = "Time (ms)"
1065 ylabel = "ACF"
1096 ylabel = "ACF"
1066
1097
1067 else:
1098 else:
1068 x = dataOut.getVelRange()
1099 x = dataOut.getVelRange()
1069 zdB = 10*numpy.log10(z[0,:,hei_index])
1100 zdB = 10*numpy.log10(z[0,:,hei_index])
1070 xlabel = "Velocity (m/s)"
1101 xlabel = "Velocity (m/s)"
1071 ylabel = "Power (dB)"
1102 ylabel = "Power (dB)"
1072
1103
1073 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1104 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1074 title = wintitle + " Range Cuts %s" %(thisDatetime.strftime("%d-%b-%Y"))
1105 title = wintitle + " Range Cuts %s" %(thisDatetime.strftime("%d-%b-%Y"))
1075
1106
1076 if not self.isConfig:
1107 if not self.isConfig:
1077
1108
1078 nplots = 1
1109 nplots = 1
1079
1110
1080 self.setup(id=id,
1111 self.setup(id=id,
1081 nplots=nplots,
1112 nplots=nplots,
1082 wintitle=wintitle,
1113 wintitle=wintitle,
1083 show=show)
1114 show=show)
1084
1115
1085 if xmin == None: xmin = numpy.nanmin(x)*0.9
1116 if xmin == None: xmin = numpy.nanmin(x)*0.9
1086 if xmax == None: xmax = numpy.nanmax(x)*1.1
1117 if xmax == None: xmax = numpy.nanmax(x)*1.1
1087 if ymin == None: ymin = numpy.nanmin(zdB)
1118 if ymin == None: ymin = numpy.nanmin(zdB)
1088 if ymax == None: ymax = numpy.nanmax(zdB)
1119 if ymax == None: ymax = numpy.nanmax(zdB)
1089
1120
1090 self.isConfig = True
1121 self.isConfig = True
1091
1122
1092 self.setWinTitle(title)
1123 self.setWinTitle(title)
1093
1124
1094 title = "Spectra Cuts: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1125 title = "Spectra Cuts: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1095 axes = self.axesList[0]
1126 axes = self.axesList[0]
1096
1127
1097 legendlabels = ["Range = %dKm" %y[i] for i in hei_index]
1128 legendlabels = ["Range = %dKm" %y[i] for i in hei_index]
1098
1129
1099 axes.pmultilineyaxis( x, zdB,
1130 axes.pmultilineyaxis( x, zdB,
1100 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1131 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1101 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
1132 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
1102 ytick_visible=True, nxticks=5,
1133 ytick_visible=True, nxticks=5,
1103 grid='x')
1134 grid='x')
1104
1135
1105 self.draw()
1136 self.draw()
1106
1137
1107 self.save(figpath=figpath,
1138 self.save(figpath=figpath,
1108 figfile=figfile,
1139 figfile=figfile,
1109 save=save,
1140 save=save,
1110 ftp=ftp,
1141 ftp=ftp,
1111 wr_period=wr_period,
1142 wr_period=wr_period,
1112 thisDatetime=thisDatetime)
1143 thisDatetime=thisDatetime)
1113
1144
1145 return dataOut
1146
1147 @MPDecorator
1114 class Noise(Figure):
1148 class Noise(Figure):
1115
1149
1116 isConfig = None
1150 isConfig = None
1117 __nsubplots = None
1151 __nsubplots = None
1118
1152
1119 PREFIX = 'noise'
1153 PREFIX = 'noise'
1120
1154
1121
1155
1122 def __init__(self, **kwargs):
1156 def __init__(self):#, **kwargs):
1123 Figure.__init__(self, **kwargs)
1157 Figure.__init__(self)#, **kwargs)
1124 self.timerange = 24*60*60
1158 self.timerange = 24*60*60
1125 self.isConfig = False
1159 self.isConfig = False
1126 self.__nsubplots = 1
1160 self.__nsubplots = 1
1127 self.counter_imagwr = 0
1161 self.counter_imagwr = 0
1128 self.WIDTH = 800
1162 self.WIDTH = 800
1129 self.HEIGHT = 400
1163 self.HEIGHT = 400
1130 self.WIDTHPROF = 120
1164 self.WIDTHPROF = 120
1131 self.HEIGHTPROF = 0
1165 self.HEIGHTPROF = 0
1132 self.xdata = None
1166 self.xdata = None
1133 self.ydata = None
1167 self.ydata = None
1134
1168
1135 self.PLOT_CODE = NOISE_CODE
1169 self.PLOT_CODE = NOISE_CODE
1136
1170
1137 self.FTP_WEI = None
1171 self.FTP_WEI = None
1138 self.EXP_CODE = None
1172 self.EXP_CODE = None
1139 self.SUB_EXP_CODE = None
1173 self.SUB_EXP_CODE = None
1140 self.PLOT_POS = None
1174 self.PLOT_POS = None
1141 self.figfile = None
1175 self.figfile = None
1142
1176
1143 self.xmin = None
1177 self.xmin = None
1144 self.xmax = None
1178 self.xmax = None
1145
1179
1146 def getSubplots(self):
1180 def getSubplots(self):
1147
1181
1148 ncol = 1
1182 ncol = 1
1149 nrow = 1
1183 nrow = 1
1150
1184
1151 return nrow, ncol
1185 return nrow, ncol
1152
1186
1153 def openfile(self, filename):
1187 def openfile(self, filename):
1154 dirname = os.path.dirname(filename)
1188 dirname = os.path.dirname(filename)
1155
1189
1156 if not os.path.exists(dirname):
1190 if not os.path.exists(dirname):
1157 os.mkdir(dirname)
1191 os.mkdir(dirname)
1158
1192
1159 f = open(filename,'w+')
1193 f = open(filename,'w+')
1160 f.write('\n\n')
1194 f.write('\n\n')
1161 f.write('JICAMARCA RADIO OBSERVATORY - Noise \n')
1195 f.write('JICAMARCA RADIO OBSERVATORY - Noise \n')
1162 f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' )
1196 f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' )
1163 f.close()
1197 f.close()
1164
1198
1165 def save_data(self, filename_phase, data, data_datetime):
1199 def save_data(self, filename_phase, data, data_datetime):
1166
1200
1167 f=open(filename_phase,'a')
1201 f=open(filename_phase,'a')
1168
1202
1169 timetuple_data = data_datetime.timetuple()
1203 timetuple_data = data_datetime.timetuple()
1170 day = str(timetuple_data.tm_mday)
1204 day = str(timetuple_data.tm_mday)
1171 month = str(timetuple_data.tm_mon)
1205 month = str(timetuple_data.tm_mon)
1172 year = str(timetuple_data.tm_year)
1206 year = str(timetuple_data.tm_year)
1173 hour = str(timetuple_data.tm_hour)
1207 hour = str(timetuple_data.tm_hour)
1174 minute = str(timetuple_data.tm_min)
1208 minute = str(timetuple_data.tm_min)
1175 second = str(timetuple_data.tm_sec)
1209 second = str(timetuple_data.tm_sec)
1176
1210
1177 data_msg = ''
1211 data_msg = ''
1178 for i in range(len(data)):
1212 for i in range(len(data)):
1179 data_msg += str(data[i]) + ' '
1213 data_msg += str(data[i]) + ' '
1180
1214
1181 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n')
1215 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n')
1182 f.close()
1216 f.close()
1183
1217
1184
1218
1185 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1219 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1186
1220
1187 self.__showprofile = showprofile
1221 self.__showprofile = showprofile
1188 self.nplots = nplots
1222 self.nplots = nplots
1189
1223
1190 ncolspan = 7
1224 ncolspan = 7
1191 colspan = 6
1225 colspan = 6
1192 self.__nsubplots = 2
1226 self.__nsubplots = 2
1193
1227
1194 self.createFigure(id = id,
1228 self.createFigure(id = id,
1195 wintitle = wintitle,
1229 wintitle = wintitle,
1196 widthplot = self.WIDTH+self.WIDTHPROF,
1230 widthplot = self.WIDTH+self.WIDTHPROF,
1197 heightplot = self.HEIGHT+self.HEIGHTPROF,
1231 heightplot = self.HEIGHT+self.HEIGHTPROF,
1198 show=show)
1232 show=show)
1199
1233
1200 nrow, ncol = self.getSubplots()
1234 nrow, ncol = self.getSubplots()
1201
1235
1202 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1236 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1203
1237
1204
1238
1205 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1239 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1206 xmin=None, xmax=None, ymin=None, ymax=None,
1240 xmin=None, xmax=None, ymin=None, ymax=None,
1207 timerange=None,
1241 timerange=None,
1208 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1242 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1209 server=None, folder=None, username=None, password=None,
1243 server=None, folder=None, username=None, password=None,
1210 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1244 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1211
1245
1246 if dataOut.flagNoData:
1247 return dataOut
1248
1212 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1249 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1213 return
1250 return
1214
1251
1215 if channelList == None:
1252 if channelList == None:
1216 channelIndexList = dataOut.channelIndexList
1253 channelIndexList = dataOut.channelIndexList
1217 channelList = dataOut.channelList
1254 channelList = dataOut.channelList
1218 else:
1255 else:
1219 channelIndexList = []
1256 channelIndexList = []
1220 for channel in channelList:
1257 for channel in channelList:
1221 if channel not in dataOut.channelList:
1258 if channel not in dataOut.channelList:
1222 raise ValueError("Channel %d is not in dataOut.channelList")
1259 raise ValueError("Channel %d is not in dataOut.channelList")
1223 channelIndexList.append(dataOut.channelList.index(channel))
1260 channelIndexList.append(dataOut.channelList.index(channel))
1224
1261
1225 x = dataOut.getTimeRange()
1262 x = dataOut.getTimeRange()
1226 #y = dataOut.getHeiRange()
1263 #y = dataOut.getHeiRange()
1227 factor = dataOut.normFactor
1264 factor = dataOut.normFactor
1228 noise = dataOut.noise[channelIndexList]/factor
1265 noise = dataOut.noise[channelIndexList]/factor
1229 noisedB = 10*numpy.log10(noise)
1266 noisedB = 10*numpy.log10(noise)
1230
1267
1231 thisDatetime = dataOut.datatime
1268 thisDatetime = dataOut.datatime
1232
1269
1233 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1270 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1234 xlabel = ""
1271 xlabel = ""
1235 ylabel = "Intensity (dB)"
1272 ylabel = "Intensity (dB)"
1236 update_figfile = False
1273 update_figfile = False
1237
1274
1238 if not self.isConfig:
1275 if not self.isConfig:
1239
1276
1240 nplots = 1
1277 nplots = 1
1241
1278
1242 self.setup(id=id,
1279 self.setup(id=id,
1243 nplots=nplots,
1280 nplots=nplots,
1244 wintitle=wintitle,
1281 wintitle=wintitle,
1245 showprofile=showprofile,
1282 showprofile=showprofile,
1246 show=show)
1283 show=show)
1247
1284
1248 if timerange != None:
1285 if timerange != None:
1249 self.timerange = timerange
1286 self.timerange = timerange
1250
1287
1251 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1288 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1252
1289
1253 if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0
1290 if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0
1254 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1291 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1255
1292
1256 self.FTP_WEI = ftp_wei
1293 self.FTP_WEI = ftp_wei
1257 self.EXP_CODE = exp_code
1294 self.EXP_CODE = exp_code
1258 self.SUB_EXP_CODE = sub_exp_code
1295 self.SUB_EXP_CODE = sub_exp_code
1259 self.PLOT_POS = plot_pos
1296 self.PLOT_POS = plot_pos
1260
1297
1261
1298
1262 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1299 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1263 self.isConfig = True
1300 self.isConfig = True
1264 self.figfile = figfile
1301 self.figfile = figfile
1265 self.xdata = numpy.array([])
1302 self.xdata = numpy.array([])
1266 self.ydata = numpy.array([])
1303 self.ydata = numpy.array([])
1267
1304
1268 update_figfile = True
1305 update_figfile = True
1269
1306
1270 #open file beacon phase
1307 #open file beacon phase
1271 path = '%s%03d' %(self.PREFIX, self.id)
1308 path = '%s%03d' %(self.PREFIX, self.id)
1272 noise_file = os.path.join(path,'%s.txt'%self.name)
1309 noise_file = os.path.join(path,'%s.txt'%self.name)
1273 self.filename_noise = os.path.join(figpath,noise_file)
1310 self.filename_noise = os.path.join(figpath,noise_file)
1274
1311
1275 self.setWinTitle(title)
1312 self.setWinTitle(title)
1276
1313
1277 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1314 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1278
1315
1279 legendlabels = ["channel %d"%(idchannel) for idchannel in channelList]
1316 legendlabels = ["channel %d"%(idchannel) for idchannel in channelList]
1280 axes = self.axesList[0]
1317 axes = self.axesList[0]
1281
1318
1282 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1319 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1283
1320
1284 if len(self.ydata)==0:
1321 if len(self.ydata)==0:
1285 self.ydata = noisedB.reshape(-1,1)
1322 self.ydata = noisedB.reshape(-1,1)
1286 else:
1323 else:
1287 self.ydata = numpy.hstack((self.ydata, noisedB.reshape(-1,1)))
1324 self.ydata = numpy.hstack((self.ydata, noisedB.reshape(-1,1)))
1288
1325
1289
1326
1290 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1327 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1291 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1328 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1292 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1329 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1293 XAxisAsTime=True, grid='both'
1330 XAxisAsTime=True, grid='both'
1294 )
1331 )
1295
1332
1296 self.draw()
1333 self.draw()
1297
1334
1298 if dataOut.ltctime >= self.xmax:
1335 if dataOut.ltctime >= self.xmax:
1299 self.counter_imagwr = wr_period
1336 self.counter_imagwr = wr_period
1300 self.isConfig = False
1337 self.isConfig = False
1301 update_figfile = True
1338 update_figfile = True
1302
1339
1303 self.save(figpath=figpath,
1340 self.save(figpath=figpath,
1304 figfile=figfile,
1341 figfile=figfile,
1305 save=save,
1342 save=save,
1306 ftp=ftp,
1343 ftp=ftp,
1307 wr_period=wr_period,
1344 wr_period=wr_period,
1308 thisDatetime=thisDatetime,
1345 thisDatetime=thisDatetime,
1309 update_figfile=update_figfile)
1346 update_figfile=update_figfile)
1310
1347
1311 #store data beacon phase
1348 #store data beacon phase
1312 if save:
1349 if save:
1313 self.save_data(self.filename_noise, noisedB, thisDatetime)
1350 self.save_data(self.filename_noise, noisedB, thisDatetime)
1314
1351
1352 return dataOut
1353
1354 @MPDecorator
1315 class BeaconPhase(Figure):
1355 class BeaconPhase(Figure):
1316
1356
1317 __isConfig = None
1357 __isConfig = None
1318 __nsubplots = None
1358 __nsubplots = None
1319
1359
1320 PREFIX = 'beacon_phase'
1360 PREFIX = 'beacon_phase'
1321
1361
1322 def __init__(self, **kwargs):
1362 def __init__(self):#, **kwargs):
1323 Figure.__init__(self, **kwargs)
1363 Figure.__init__(self)#, **kwargs)
1324 self.timerange = 24*60*60
1364 self.timerange = 24*60*60
1325 self.isConfig = False
1365 self.isConfig = False
1326 self.__nsubplots = 1
1366 self.__nsubplots = 1
1327 self.counter_imagwr = 0
1367 self.counter_imagwr = 0
1328 self.WIDTH = 800
1368 self.WIDTH = 800
1329 self.HEIGHT = 400
1369 self.HEIGHT = 400
1330 self.WIDTHPROF = 120
1370 self.WIDTHPROF = 120
1331 self.HEIGHTPROF = 0
1371 self.HEIGHTPROF = 0
1332 self.xdata = None
1372 self.xdata = None
1333 self.ydata = None
1373 self.ydata = None
1334
1374
1335 self.PLOT_CODE = BEACON_CODE
1375 self.PLOT_CODE = BEACON_CODE
1336
1376
1337 self.FTP_WEI = None
1377 self.FTP_WEI = None
1338 self.EXP_CODE = None
1378 self.EXP_CODE = None
1339 self.SUB_EXP_CODE = None
1379 self.SUB_EXP_CODE = None
1340 self.PLOT_POS = None
1380 self.PLOT_POS = None
1341
1381
1342 self.filename_phase = None
1382 self.filename_phase = None
1343
1383
1344 self.figfile = None
1384 self.figfile = None
1345
1385
1346 self.xmin = None
1386 self.xmin = None
1347 self.xmax = None
1387 self.xmax = None
1348
1388
1349 def getSubplots(self):
1389 def getSubplots(self):
1350
1390
1351 ncol = 1
1391 ncol = 1
1352 nrow = 1
1392 nrow = 1
1353
1393
1354 return nrow, ncol
1394 return nrow, ncol
1355
1395
1356 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1396 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1357
1397
1358 self.__showprofile = showprofile
1398 self.__showprofile = showprofile
1359 self.nplots = nplots
1399 self.nplots = nplots
1360
1400
1361 ncolspan = 7
1401 ncolspan = 7
1362 colspan = 6
1402 colspan = 6
1363 self.__nsubplots = 2
1403 self.__nsubplots = 2
1364
1404
1365 self.createFigure(id = id,
1405 self.createFigure(id = id,
1366 wintitle = wintitle,
1406 wintitle = wintitle,
1367 widthplot = self.WIDTH+self.WIDTHPROF,
1407 widthplot = self.WIDTH+self.WIDTHPROF,
1368 heightplot = self.HEIGHT+self.HEIGHTPROF,
1408 heightplot = self.HEIGHT+self.HEIGHTPROF,
1369 show=show)
1409 show=show)
1370
1410
1371 nrow, ncol = self.getSubplots()
1411 nrow, ncol = self.getSubplots()
1372
1412
1373 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1413 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1374
1414
1375 def save_phase(self, filename_phase):
1415 def save_phase(self, filename_phase):
1376 f = open(filename_phase,'w+')
1416 f = open(filename_phase,'w+')
1377 f.write('\n\n')
1417 f.write('\n\n')
1378 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1418 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1379 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1419 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1380 f.close()
1420 f.close()
1381
1421
1382 def save_data(self, filename_phase, data, data_datetime):
1422 def save_data(self, filename_phase, data, data_datetime):
1383 f=open(filename_phase,'a')
1423 f=open(filename_phase,'a')
1384 timetuple_data = data_datetime.timetuple()
1424 timetuple_data = data_datetime.timetuple()
1385 day = str(timetuple_data.tm_mday)
1425 day = str(timetuple_data.tm_mday)
1386 month = str(timetuple_data.tm_mon)
1426 month = str(timetuple_data.tm_mon)
1387 year = str(timetuple_data.tm_year)
1427 year = str(timetuple_data.tm_year)
1388 hour = str(timetuple_data.tm_hour)
1428 hour = str(timetuple_data.tm_hour)
1389 minute = str(timetuple_data.tm_min)
1429 minute = str(timetuple_data.tm_min)
1390 second = str(timetuple_data.tm_sec)
1430 second = str(timetuple_data.tm_sec)
1391 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1431 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1392 f.close()
1432 f.close()
1393
1433
1394
1434
1395 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1435 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1396 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
1436 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
1397 timerange=None,
1437 timerange=None,
1398 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1438 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1399 server=None, folder=None, username=None, password=None,
1439 server=None, folder=None, username=None, password=None,
1400 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1440 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1401
1441
1442 if dataOut.flagNoData:
1443 return dataOut
1444
1402 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1445 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1403 return
1446 return
1404
1447
1405 if pairsList == None:
1448 if pairsList == None:
1406 pairsIndexList = dataOut.pairsIndexList[:10]
1449 pairsIndexList = dataOut.pairsIndexList[:10]
1407 else:
1450 else:
1408 pairsIndexList = []
1451 pairsIndexList = []
1409 for pair in pairsList:
1452 for pair in pairsList:
1410 if pair not in dataOut.pairsList:
1453 if pair not in dataOut.pairsList:
1411 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
1454 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
1412 pairsIndexList.append(dataOut.pairsList.index(pair))
1455 pairsIndexList.append(dataOut.pairsList.index(pair))
1413
1456
1414 if pairsIndexList == []:
1457 if pairsIndexList == []:
1415 return
1458 return
1416
1459
1417 # if len(pairsIndexList) > 4:
1460 # if len(pairsIndexList) > 4:
1418 # pairsIndexList = pairsIndexList[0:4]
1461 # pairsIndexList = pairsIndexList[0:4]
1419
1462
1420 hmin_index = None
1463 hmin_index = None
1421 hmax_index = None
1464 hmax_index = None
1422
1465
1423 if hmin != None and hmax != None:
1466 if hmin != None and hmax != None:
1424 indexes = numpy.arange(dataOut.nHeights)
1467 indexes = numpy.arange(dataOut.nHeights)
1425 hmin_list = indexes[dataOut.heightList >= hmin]
1468 hmin_list = indexes[dataOut.heightList >= hmin]
1426 hmax_list = indexes[dataOut.heightList <= hmax]
1469 hmax_list = indexes[dataOut.heightList <= hmax]
1427
1470
1428 if hmin_list.any():
1471 if hmin_list.any():
1429 hmin_index = hmin_list[0]
1472 hmin_index = hmin_list[0]
1430
1473
1431 if hmax_list.any():
1474 if hmax_list.any():
1432 hmax_index = hmax_list[-1]+1
1475 hmax_index = hmax_list[-1]+1
1433
1476
1434 x = dataOut.getTimeRange()
1477 x = dataOut.getTimeRange()
1435 #y = dataOut.getHeiRange()
1478 #y = dataOut.getHeiRange()
1436
1479
1437
1480
1438 thisDatetime = dataOut.datatime
1481 thisDatetime = dataOut.datatime
1439
1482
1440 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1483 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1441 xlabel = "Local Time"
1484 xlabel = "Local Time"
1442 ylabel = "Phase (degrees)"
1485 ylabel = "Phase (degrees)"
1443
1486
1444 update_figfile = False
1487 update_figfile = False
1445
1488
1446 nplots = len(pairsIndexList)
1489 nplots = len(pairsIndexList)
1447 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1490 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1448 phase_beacon = numpy.zeros(len(pairsIndexList))
1491 phase_beacon = numpy.zeros(len(pairsIndexList))
1449 for i in range(nplots):
1492 for i in range(nplots):
1450 pair = dataOut.pairsList[pairsIndexList[i]]
1493 pair = dataOut.pairsList[pairsIndexList[i]]
1451 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
1494 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
1452 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
1495 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
1453 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
1496 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
1454 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1497 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1455 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1498 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1456
1499
1457 #print "Phase %d%d" %(pair[0], pair[1])
1500 #print "Phase %d%d" %(pair[0], pair[1])
1458 #print phase[dataOut.beacon_heiIndexList]
1501 #print phase[dataOut.beacon_heiIndexList]
1459
1502
1460 if dataOut.beacon_heiIndexList:
1503 if dataOut.beacon_heiIndexList:
1461 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1504 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1462 else:
1505 else:
1463 phase_beacon[i] = numpy.average(phase)
1506 phase_beacon[i] = numpy.average(phase)
1464
1507
1465 if not self.isConfig:
1508 if not self.isConfig:
1466
1509
1467 nplots = len(pairsIndexList)
1510 nplots = len(pairsIndexList)
1468
1511
1469 self.setup(id=id,
1512 self.setup(id=id,
1470 nplots=nplots,
1513 nplots=nplots,
1471 wintitle=wintitle,
1514 wintitle=wintitle,
1472 showprofile=showprofile,
1515 showprofile=showprofile,
1473 show=show)
1516 show=show)
1474
1517
1475 if timerange != None:
1518 if timerange != None:
1476 self.timerange = timerange
1519 self.timerange = timerange
1477
1520
1478 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1521 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1479
1522
1480 if ymin == None: ymin = 0
1523 if ymin == None: ymin = 0
1481 if ymax == None: ymax = 360
1524 if ymax == None: ymax = 360
1482
1525
1483 self.FTP_WEI = ftp_wei
1526 self.FTP_WEI = ftp_wei
1484 self.EXP_CODE = exp_code
1527 self.EXP_CODE = exp_code
1485 self.SUB_EXP_CODE = sub_exp_code
1528 self.SUB_EXP_CODE = sub_exp_code
1486 self.PLOT_POS = plot_pos
1529 self.PLOT_POS = plot_pos
1487
1530
1488 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1531 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1489 self.isConfig = True
1532 self.isConfig = True
1490 self.figfile = figfile
1533 self.figfile = figfile
1491 self.xdata = numpy.array([])
1534 self.xdata = numpy.array([])
1492 self.ydata = numpy.array([])
1535 self.ydata = numpy.array([])
1493
1536
1494 update_figfile = True
1537 update_figfile = True
1495
1538
1496 #open file beacon phase
1539 #open file beacon phase
1497 path = '%s%03d' %(self.PREFIX, self.id)
1540 path = '%s%03d' %(self.PREFIX, self.id)
1498 beacon_file = os.path.join(path,'%s.txt'%self.name)
1541 beacon_file = os.path.join(path,'%s.txt'%self.name)
1499 self.filename_phase = os.path.join(figpath,beacon_file)
1542 self.filename_phase = os.path.join(figpath,beacon_file)
1500 #self.save_phase(self.filename_phase)
1543 #self.save_phase(self.filename_phase)
1501
1544
1502
1545
1503 #store data beacon phase
1546 #store data beacon phase
1504 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1547 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1505
1548
1506 self.setWinTitle(title)
1549 self.setWinTitle(title)
1507
1550
1508
1551
1509 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1552 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1510
1553
1511 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1554 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1512
1555
1513 axes = self.axesList[0]
1556 axes = self.axesList[0]
1514
1557
1515 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1558 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1516
1559
1517 if len(self.ydata)==0:
1560 if len(self.ydata)==0:
1518 self.ydata = phase_beacon.reshape(-1,1)
1561 self.ydata = phase_beacon.reshape(-1,1)
1519 else:
1562 else:
1520 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1563 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1521
1564
1522
1565
1523 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1566 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1524 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1567 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1525 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1568 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1526 XAxisAsTime=True, grid='both'
1569 XAxisAsTime=True, grid='both'
1527 )
1570 )
1528
1571
1529 self.draw()
1572 self.draw()
1530
1573
1531 if dataOut.ltctime >= self.xmax:
1574 if dataOut.ltctime >= self.xmax:
1532 self.counter_imagwr = wr_period
1575 self.counter_imagwr = wr_period
1533 self.isConfig = False
1576 self.isConfig = False
1534 update_figfile = True
1577 update_figfile = True
1535
1578
1536 self.save(figpath=figpath,
1579 self.save(figpath=figpath,
1537 figfile=figfile,
1580 figfile=figfile,
1538 save=save,
1581 save=save,
1539 ftp=ftp,
1582 ftp=ftp,
1540 wr_period=wr_period,
1583 wr_period=wr_period,
1541 thisDatetime=thisDatetime,
1584 thisDatetime=thisDatetime,
1542 update_figfile=update_figfile) No newline at end of file
1585 update_figfile=update_figfile)
1586
1587 return dataOut #Yong No newline at end of file
@@ -1,225 +1,232
1 '''
1 '''
2 Created on Jul 9, 2014
2 Created on Jul 9, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import datetime
7 import datetime
8 import numpy
8 import numpy
9
9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator #YONG
10 from schainpy.utils import log
10 from .figure import Figure
11 from .figure import Figure
11
12
13
14 @MPDecorator
12 class Scope(Figure):
15 class Scope(Figure):
13
16
14 isConfig = None
17 isConfig = None
15
18
16 def __init__(self, **kwargs):
19 def __init__(self):#, **kwargs): #YONG
17 Figure.__init__(self, **kwargs)
20 Figure.__init__(self)#, **kwargs)
18 self.isConfig = False
21 self.isConfig = False
19 self.WIDTH = 300
22 self.WIDTH = 300
20 self.HEIGHT = 200
23 self.HEIGHT = 200
21 self.counter_imagwr = 0
24 self.counter_imagwr = 0
22
25
23 def getSubplots(self):
26 def getSubplots(self):
24
27
25 nrow = self.nplots
28 nrow = self.nplots
26 ncol = 3
29 ncol = 3
27 return nrow, ncol
30 return nrow, ncol
28
31
29 def setup(self, id, nplots, wintitle, show):
32 def setup(self, id, nplots, wintitle, show):
30
33
31 self.nplots = nplots
34 self.nplots = nplots
32
35
33 self.createFigure(id=id,
36 self.createFigure(id=id,
34 wintitle=wintitle,
37 wintitle=wintitle,
35 show=show)
38 show=show)
36
39
37 nrow,ncol = self.getSubplots()
40 nrow,ncol = self.getSubplots()
38 colspan = 3
41 colspan = 3
39 rowspan = 1
42 rowspan = 1
40
43
41 for i in range(nplots):
44 for i in range(nplots):
42 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
45 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
43
46
44 def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
47 def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
45 yreal = y[channelIndexList,:].real
48 yreal = y[channelIndexList,:].real
46 yimag = y[channelIndexList,:].imag
49 yimag = y[channelIndexList,:].imag
47
50
48 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
51 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
49 xlabel = "Range (Km)"
52 xlabel = "Range (Km)"
50 ylabel = "Intensity - IQ"
53 ylabel = "Intensity - IQ"
51
54
52 if not self.isConfig:
55 if not self.isConfig:
53 nplots = len(channelIndexList)
56 nplots = len(channelIndexList)
54
57
55 self.setup(id=id,
58 self.setup(id=id,
56 nplots=nplots,
59 nplots=nplots,
57 wintitle='',
60 wintitle='',
58 show=show)
61 show=show)
59
62
60 if xmin == None: xmin = numpy.nanmin(x)
63 if xmin == None: xmin = numpy.nanmin(x)
61 if xmax == None: xmax = numpy.nanmax(x)
64 if xmax == None: xmax = numpy.nanmax(x)
62 if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag))
65 if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag))
63 if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag))
66 if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag))
64
67
65 self.isConfig = True
68 self.isConfig = True
66
69
67 self.setWinTitle(title)
70 self.setWinTitle(title)
68
71
69 for i in range(len(self.axesList)):
72 for i in range(len(self.axesList)):
70 title = "Channel %d" %(i)
73 title = "Channel %d" %(i)
71 axes = self.axesList[i]
74 axes = self.axesList[i]
72
75
73 axes.pline(x, yreal[i,:],
76 axes.pline(x, yreal[i,:],
74 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
77 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
75 xlabel=xlabel, ylabel=ylabel, title=title)
78 xlabel=xlabel, ylabel=ylabel, title=title)
76
79
77 axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2)
80 axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2)
78
81
79 def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
82 def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
80 y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:])
83 y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:])
81 yreal = y.real
84 yreal = y.real
82
85
83 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
86 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
84 xlabel = "Range (Km)"
87 xlabel = "Range (Km)"
85 ylabel = "Intensity"
88 ylabel = "Intensity"
86
89
87 if not self.isConfig:
90 if not self.isConfig:
88 nplots = len(channelIndexList)
91 nplots = len(channelIndexList)
89
92
90 self.setup(id=id,
93 self.setup(id=id,
91 nplots=nplots,
94 nplots=nplots,
92 wintitle='',
95 wintitle='',
93 show=show)
96 show=show)
94
97
95 if xmin == None: xmin = numpy.nanmin(x)
98 if xmin == None: xmin = numpy.nanmin(x)
96 if xmax == None: xmax = numpy.nanmax(x)
99 if xmax == None: xmax = numpy.nanmax(x)
97 if ymin == None: ymin = numpy.nanmin(yreal)
100 if ymin == None: ymin = numpy.nanmin(yreal)
98 if ymax == None: ymax = numpy.nanmax(yreal)
101 if ymax == None: ymax = numpy.nanmax(yreal)
99
102
100 self.isConfig = True
103 self.isConfig = True
101
104
102 self.setWinTitle(title)
105 self.setWinTitle(title)
103
106
104 for i in range(len(self.axesList)):
107 for i in range(len(self.axesList)):
105 title = "Channel %d" %(i)
108 title = "Channel %d" %(i)
106 axes = self.axesList[i]
109 axes = self.axesList[i]
107 ychannel = yreal[i,:]
110 ychannel = yreal[i,:]
108 axes.pline(x, ychannel,
111 axes.pline(x, ychannel,
109 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
112 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
110 xlabel=xlabel, ylabel=ylabel, title=title)
113 xlabel=xlabel, ylabel=ylabel, title=title)
111
114
112
115
113 def run(self, dataOut, id, wintitle="", channelList=None,
116 def run(self, dataOut, id, wintitle="", channelList=None,
114 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
117 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
115 figpath='./', figfile=None, show=True, wr_period=1,
118 figpath='./', figfile=None, show=True, wr_period=1,
116 ftp=False, server=None, folder=None, username=None, password=None, type='power', **kwargs):
119 ftp=False, server=None, folder=None, username=None, password=None, type='power', **kwargs):
117
120
118 """
121 """
119
122
120 Input:
123 Input:
121 dataOut :
124 dataOut :
122 id :
125 id :
123 wintitle :
126 wintitle :
124 channelList :
127 channelList :
125 xmin : None,
128 xmin : None,
126 xmax : None,
129 xmax : None,
127 ymin : None,
130 ymin : None,
128 ymax : None,
131 ymax : None,
129 """
132 """
133 if dataOut.flagNoData:
134 return dataOut
130
135
131 if channelList == None:
136 if channelList == None:
132 channelIndexList = dataOut.channelIndexList
137 channelIndexList = dataOut.channelIndexList
133 else:
138 else:
134 channelIndexList = []
139 channelIndexList = []
135 for channel in channelList:
140 for channel in channelList:
136 if channel not in dataOut.channelList:
141 if channel not in dataOut.channelList:
137 raise ValueError("Channel %d is not in dataOut.channelList")
142 raise ValueError("Channel %d is not in dataOut.channelList")
138 channelIndexList.append(dataOut.channelList.index(channel))
143 channelIndexList.append(dataOut.channelList.index(channel))
139
144
140 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
145 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
141
146
142 if dataOut.flagDataAsBlock:
147 if dataOut.flagDataAsBlock:
143
148
144 for i in range(dataOut.nProfiles):
149 for i in range(dataOut.nProfiles):
145
150
146 wintitle1 = wintitle + " [Profile = %d] " %i
151 wintitle1 = wintitle + " [Profile = %d] " %i
147
152
148 if type == "power":
153 if type == "power":
149 self.plot_power(dataOut.heightList,
154 self.plot_power(dataOut.heightList,
150 dataOut.data[:,i,:],
155 dataOut.data[:,i,:],
151 id,
156 id,
152 channelIndexList,
157 channelIndexList,
153 thisDatetime,
158 thisDatetime,
154 wintitle1,
159 wintitle1,
155 show,
160 show,
156 xmin,
161 xmin,
157 xmax,
162 xmax,
158 ymin,
163 ymin,
159 ymax)
164 ymax)
160
165
161 if type == "iq":
166 if type == "iq":
162 self.plot_iq(dataOut.heightList,
167 self.plot_iq(dataOut.heightList,
163 dataOut.data[:,i,:],
168 dataOut.data[:,i,:],
164 id,
169 id,
165 channelIndexList,
170 channelIndexList,
166 thisDatetime,
171 thisDatetime,
167 wintitle1,
172 wintitle1,
168 show,
173 show,
169 xmin,
174 xmin,
170 xmax,
175 xmax,
171 ymin,
176 ymin,
172 ymax)
177 ymax)
173
178
174 self.draw()
179 self.draw()
175
180
176 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
181 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
177 figfile = self.getFilename(name = str_datetime) + "_" + str(i)
182 figfile = self.getFilename(name = str_datetime) + "_" + str(i)
178
183
179 self.save(figpath=figpath,
184 self.save(figpath=figpath,
180 figfile=figfile,
185 figfile=figfile,
181 save=save,
186 save=save,
182 ftp=ftp,
187 ftp=ftp,
183 wr_period=wr_period,
188 wr_period=wr_period,
184 thisDatetime=thisDatetime)
189 thisDatetime=thisDatetime)
185
190
186 else:
191 else:
187 wintitle += " [Profile = %d] " %dataOut.profileIndex
192 wintitle += " [Profile = %d] " %dataOut.profileIndex
188
193
189 if type == "power":
194 if type == "power":
190 self.plot_power(dataOut.heightList,
195 self.plot_power(dataOut.heightList,
191 dataOut.data,
196 dataOut.data,
192 id,
197 id,
193 channelIndexList,
198 channelIndexList,
194 thisDatetime,
199 thisDatetime,
195 wintitle,
200 wintitle,
196 show,
201 show,
197 xmin,
202 xmin,
198 xmax,
203 xmax,
199 ymin,
204 ymin,
200 ymax)
205 ymax)
201
206
202 if type == "iq":
207 if type == "iq":
203 self.plot_iq(dataOut.heightList,
208 self.plot_iq(dataOut.heightList,
204 dataOut.data,
209 dataOut.data,
205 id,
210 id,
206 channelIndexList,
211 channelIndexList,
207 thisDatetime,
212 thisDatetime,
208 wintitle,
213 wintitle,
209 show,
214 show,
210 xmin,
215 xmin,
211 xmax,
216 xmax,
212 ymin,
217 ymin,
213 ymax)
218 ymax)
214
219
215 self.draw()
220 self.draw()
216
221
217 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") + "_" + str(dataOut.profileIndex)
222 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") + "_" + str(dataOut.profileIndex)
218 figfile = self.getFilename(name = str_datetime)
223 figfile = self.getFilename(name = str_datetime)
219
224
220 self.save(figpath=figpath,
225 self.save(figpath=figpath,
221 figfile=figfile,
226 figfile=figfile,
222 save=save,
227 save=save,
223 ftp=ftp,
228 ftp=ftp,
224 wr_period=wr_period,
229 wr_period=wr_period,
225 thisDatetime=thisDatetime) No newline at end of file
230 thisDatetime=thisDatetime)
231
232 return dataOut No newline at end of file
@@ -1,1826 +1,1826
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import sys
7 import sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import inspect
12 import inspect
13 import time
13 import time
14 import datetime
14 import datetime
15 import traceback
15 import traceback
16 import zmq
16 import zmq
17
17
18 try:
18 try:
19 from gevent import sleep
19 from gevent import sleep
20 except:
20 except:
21 from time import sleep
21 from time import sleep
22
22
23 import schainpy.admin
24 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
23 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
25 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
24 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
26 from schainpy.utils import log
25 from schainpy.utils import log
27 import schainpy.admin
26 import schainpy.admin
28
27
29 LOCALTIME = True
28 LOCALTIME = True
30
29
31
30
32 def isNumber(cad):
31 def isNumber(cad):
33 """
32 """
34 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
33 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
35
34
36 Excepciones:
35 Excepciones:
37 Si un determinado string no puede ser convertido a numero
36 Si un determinado string no puede ser convertido a numero
38 Input:
37 Input:
39 str, string al cual se le analiza para determinar si convertible a un numero o no
38 str, string al cual se le analiza para determinar si convertible a un numero o no
40
39
41 Return:
40 Return:
42 True : si el string es uno numerico
41 True : si el string es uno numerico
43 False : no es un string numerico
42 False : no es un string numerico
44 """
43 """
45 try:
44 try:
46 float(cad)
45 float(cad)
47 return True
46 return True
48 except:
47 except:
49 return False
48 return False
50
49
51
50
52 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
51 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
53 """
52 """
54 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
53 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
55
54
56 Inputs:
55 Inputs:
57 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
56 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
58
57
59 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
58 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
60 segundos contados desde 01/01/1970.
59 segundos contados desde 01/01/1970.
61 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
60 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
62 segundos contados desde 01/01/1970.
61 segundos contados desde 01/01/1970.
63
62
64 Return:
63 Return:
65 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
64 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
66 fecha especificado, de lo contrario retorna False.
65 fecha especificado, de lo contrario retorna False.
67
66
68 Excepciones:
67 Excepciones:
69 Si el archivo no existe o no puede ser abierto
68 Si el archivo no existe o no puede ser abierto
70 Si la cabecera no puede ser leida.
69 Si la cabecera no puede ser leida.
71
70
72 """
71 """
73 basicHeaderObj = BasicHeader(LOCALTIME)
72 basicHeaderObj = BasicHeader(LOCALTIME)
74
73
75 try:
74 try:
76 fp = open(filename, 'rb')
75 fp = open(filename, 'rb')
77 except IOError:
76 except IOError:
78 print("The file %s can't be opened" % (filename))
77 print("The file %s can't be opened" % (filename))
79 return 0
78 return 0
80
79
81 sts = basicHeaderObj.read(fp)
80 sts = basicHeaderObj.read(fp)
82 fp.close()
81 fp.close()
83
82
84 if not(sts):
83 if not(sts):
85 print("Skipping the file %s because it has not a valid header" % (filename))
84 print("Skipping the file %s because it has not a valid header" % (filename))
86 return 0
85 return 0
87
86
88 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
87 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
89 return 0
88 return 0
90
89
91 return 1
90 return 1
92
91
93
92
94 def isTimeInRange(thisTime, startTime, endTime):
93 def isTimeInRange(thisTime, startTime, endTime):
95 if endTime >= startTime:
94 if endTime >= startTime:
96 if (thisTime < startTime) or (thisTime > endTime):
95 if (thisTime < startTime) or (thisTime > endTime):
97 return 0
96 return 0
98 return 1
97 return 1
99 else:
98 else:
100 if (thisTime < startTime) and (thisTime > endTime):
99 if (thisTime < startTime) and (thisTime > endTime):
101 return 0
100 return 0
102 return 1
101 return 1
103
102
104
103
105 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
104 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
106 """
105 """
107 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
106 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
108
107
109 Inputs:
108 Inputs:
110 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
109 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
111
110
112 startDate : fecha inicial del rango seleccionado en formato datetime.date
111 startDate : fecha inicial del rango seleccionado en formato datetime.date
113
112
114 endDate : fecha final del rango seleccionado en formato datetime.date
113 endDate : fecha final del rango seleccionado en formato datetime.date
115
114
116 startTime : tiempo inicial del rango seleccionado en formato datetime.time
115 startTime : tiempo inicial del rango seleccionado en formato datetime.time
117
116
118 endTime : tiempo final del rango seleccionado en formato datetime.time
117 endTime : tiempo final del rango seleccionado en formato datetime.time
119
118
120 Return:
119 Return:
121 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
120 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
122 fecha especificado, de lo contrario retorna False.
121 fecha especificado, de lo contrario retorna False.
123
122
124 Excepciones:
123 Excepciones:
125 Si el archivo no existe o no puede ser abierto
124 Si el archivo no existe o no puede ser abierto
126 Si la cabecera no puede ser leida.
125 Si la cabecera no puede ser leida.
127
126
128 """
127 """
129
128
130 try:
129 try:
131 fp = open(filename, 'rb')
130 fp = open(filename, 'rb')
132 except IOError:
131 except IOError:
133 print("The file %s can't be opened" % (filename))
132 print("The file %s can't be opened" % (filename))
134 return None
133 return None
135
134
136 firstBasicHeaderObj = BasicHeader(LOCALTIME)
135 firstBasicHeaderObj = BasicHeader(LOCALTIME)
137 systemHeaderObj = SystemHeader()
136 systemHeaderObj = SystemHeader()
138 radarControllerHeaderObj = RadarControllerHeader()
137 radarControllerHeaderObj = RadarControllerHeader()
139 processingHeaderObj = ProcessingHeader()
138 processingHeaderObj = ProcessingHeader()
140
139
141 lastBasicHeaderObj = BasicHeader(LOCALTIME)
140 lastBasicHeaderObj = BasicHeader(LOCALTIME)
142
141
143 sts = firstBasicHeaderObj.read(fp)
142 sts = firstBasicHeaderObj.read(fp)
144
143
145 if not(sts):
144 if not(sts):
146 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
145 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
147 return None
146 return None
148
147
149 if not systemHeaderObj.read(fp):
148 if not systemHeaderObj.read(fp):
150 return None
149 return None
151
150
152 if not radarControllerHeaderObj.read(fp):
151 if not radarControllerHeaderObj.read(fp):
153 return None
152 return None
154
153
155 if not processingHeaderObj.read(fp):
154 if not processingHeaderObj.read(fp):
156 return None
155 return None
157
156
158 filesize = os.path.getsize(filename)
157 filesize = os.path.getsize(filename)
159
158
160 offset = processingHeaderObj.blockSize + 24 # header size
159 offset = processingHeaderObj.blockSize + 24 # header size
161
160
162 if filesize <= offset:
161 if filesize <= offset:
163 print("[Reading] %s: This file has not enough data" % filename)
162 print("[Reading] %s: This file has not enough data" % filename)
164 return None
163 return None
165
164
166 fp.seek(-offset, 2)
165 fp.seek(-offset, 2)
167
166
168 sts = lastBasicHeaderObj.read(fp)
167 sts = lastBasicHeaderObj.read(fp)
169
168
170 fp.close()
169 fp.close()
171
170
172 thisDatetime = lastBasicHeaderObj.datatime
171 thisDatetime = lastBasicHeaderObj.datatime
173 thisTime_last_block = thisDatetime.time()
172 thisTime_last_block = thisDatetime.time()
174
173
175 thisDatetime = firstBasicHeaderObj.datatime
174 thisDatetime = firstBasicHeaderObj.datatime
176 thisDate = thisDatetime.date()
175 thisDate = thisDatetime.date()
177 thisTime_first_block = thisDatetime.time()
176 thisTime_first_block = thisDatetime.time()
178
177
179 # General case
178 # General case
180 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
179 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
181 #-----------o----------------------------o-----------
180 #-----------o----------------------------o-----------
182 # startTime endTime
181 # startTime endTime
183
182
184 if endTime >= startTime:
183 if endTime >= startTime:
185 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
184 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
186 return None
185 return None
187
186
188 return thisDatetime
187 return thisDatetime
189
188
190 # If endTime < startTime then endTime belongs to the next day
189 # If endTime < startTime then endTime belongs to the next day
191
190
192 #<<<<<<<<<<<o o>>>>>>>>>>>
191 #<<<<<<<<<<<o o>>>>>>>>>>>
193 #-----------o----------------------------o-----------
192 #-----------o----------------------------o-----------
194 # endTime startTime
193 # endTime startTime
195
194
196 if (thisDate == startDate) and (thisTime_last_block < startTime):
195 if (thisDate == startDate) and (thisTime_last_block < startTime):
197 return None
196 return None
198
197
199 if (thisDate == endDate) and (thisTime_first_block > endTime):
198 if (thisDate == endDate) and (thisTime_first_block > endTime):
200 return None
199 return None
201
200
202 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
201 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
203 return None
202 return None
204
203
205 return thisDatetime
204 return thisDatetime
206
205
207
206
208 def isFolderInDateRange(folder, startDate=None, endDate=None):
207 def isFolderInDateRange(folder, startDate=None, endDate=None):
209 """
208 """
210 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
209 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
211
210
212 Inputs:
211 Inputs:
213 folder : nombre completo del directorio.
212 folder : nombre completo del directorio.
214 Su formato deberia ser "/path_root/?YYYYDDD"
213 Su formato deberia ser "/path_root/?YYYYDDD"
215
214
216 siendo:
215 siendo:
217 YYYY : Anio (ejemplo 2015)
216 YYYY : Anio (ejemplo 2015)
218 DDD : Dia del anio (ejemplo 305)
217 DDD : Dia del anio (ejemplo 305)
219
218
220 startDate : fecha inicial del rango seleccionado en formato datetime.date
219 startDate : fecha inicial del rango seleccionado en formato datetime.date
221
220
222 endDate : fecha final del rango seleccionado en formato datetime.date
221 endDate : fecha final del rango seleccionado en formato datetime.date
223
222
224 Return:
223 Return:
225 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
224 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
226 fecha especificado, de lo contrario retorna False.
225 fecha especificado, de lo contrario retorna False.
227 Excepciones:
226 Excepciones:
228 Si el directorio no tiene el formato adecuado
227 Si el directorio no tiene el formato adecuado
229 """
228 """
230
229
231 basename = os.path.basename(folder)
230 basename = os.path.basename(folder)
232
231
233 if not isRadarFolder(basename):
232 if not isRadarFolder(basename):
234 print("The folder %s has not the rigth format" % folder)
233 print("The folder %s has not the rigth format" % folder)
235 return 0
234 return 0
236
235
237 if startDate and endDate:
236 if startDate and endDate:
238 thisDate = getDateFromRadarFolder(basename)
237 thisDate = getDateFromRadarFolder(basename)
239
238
240 if thisDate < startDate:
239 if thisDate < startDate:
241 return 0
240 return 0
242
241
243 if thisDate > endDate:
242 if thisDate > endDate:
244 return 0
243 return 0
245
244
246 return 1
245 return 1
247
246
248
247
249 def isFileInDateRange(filename, startDate=None, endDate=None):
248 def isFileInDateRange(filename, startDate=None, endDate=None):
250 """
249 """
251 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
250 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
252
251
253 Inputs:
252 Inputs:
254 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
253 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
255
254
256 Su formato deberia ser "?YYYYDDDsss"
255 Su formato deberia ser "?YYYYDDDsss"
257
256
258 siendo:
257 siendo:
259 YYYY : Anio (ejemplo 2015)
258 YYYY : Anio (ejemplo 2015)
260 DDD : Dia del anio (ejemplo 305)
259 DDD : Dia del anio (ejemplo 305)
261 sss : set
260 sss : set
262
261
263 startDate : fecha inicial del rango seleccionado en formato datetime.date
262 startDate : fecha inicial del rango seleccionado en formato datetime.date
264
263
265 endDate : fecha final del rango seleccionado en formato datetime.date
264 endDate : fecha final del rango seleccionado en formato datetime.date
266
265
267 Return:
266 Return:
268 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
267 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
269 fecha especificado, de lo contrario retorna False.
268 fecha especificado, de lo contrario retorna False.
270 Excepciones:
269 Excepciones:
271 Si el archivo no tiene el formato adecuado
270 Si el archivo no tiene el formato adecuado
272 """
271 """
273
272
274 basename = os.path.basename(filename)
273 basename = os.path.basename(filename)
275
274
276 if not isRadarFile(basename):
275 if not isRadarFile(basename):
277 print("The filename %s has not the rigth format" % filename)
276 print("The filename %s has not the rigth format" % filename)
278 return 0
277 return 0
279
278
280 if startDate and endDate:
279 if startDate and endDate:
281 thisDate = getDateFromRadarFile(basename)
280 thisDate = getDateFromRadarFile(basename)
282
281
283 if thisDate < startDate:
282 if thisDate < startDate:
284 return 0
283 return 0
285
284
286 if thisDate > endDate:
285 if thisDate > endDate:
287 return 0
286 return 0
288
287
289 return 1
288 return 1
290
289
291
290
292 def getFileFromSet(path, ext, set):
291 def getFileFromSet(path, ext, set):
293 validFilelist = []
292 validFilelist = []
294 fileList = os.listdir(path)
293 fileList = os.listdir(path)
295
294
296 # 0 1234 567 89A BCDE
295 # 0 1234 567 89A BCDE
297 # H YYYY DDD SSS .ext
296 # H YYYY DDD SSS .ext
298
297
299 for thisFile in fileList:
298 for thisFile in fileList:
300 try:
299 try:
301 year = int(thisFile[1:5])
300 year = int(thisFile[1:5])
302 doy = int(thisFile[5:8])
301 doy = int(thisFile[5:8])
303 except:
302 except:
304 continue
303 continue
305
304
306 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
305 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
307 continue
306 continue
308
307
309 validFilelist.append(thisFile)
308 validFilelist.append(thisFile)
310
309
311 myfile = fnmatch.filter(
310 myfile = fnmatch.filter(
312 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
311 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
313
312
314 if len(myfile) != 0:
313 if len(myfile) != 0:
315 return myfile[0]
314 return myfile[0]
316 else:
315 else:
317 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
316 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
318 print('the filename %s does not exist' % filename)
317 print('the filename %s does not exist' % filename)
319 print('...going to the last file: ')
318 print('...going to the last file: ')
320
319
321 if validFilelist:
320 if validFilelist:
322 validFilelist = sorted(validFilelist, key=str.lower)
321 validFilelist = sorted(validFilelist, key=str.lower)
323 return validFilelist[-1]
322 return validFilelist[-1]
324
323
325 return None
324 return None
326
325
327
326
328 def getlastFileFromPath(path, ext):
327 def getlastFileFromPath(path, ext):
329 """
328 """
330 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
329 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
331 al final de la depuracion devuelve el ultimo file de la lista que quedo.
330 al final de la depuracion devuelve el ultimo file de la lista que quedo.
332
331
333 Input:
332 Input:
334 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
333 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
335 ext : extension de los files contenidos en una carpeta
334 ext : extension de los files contenidos en una carpeta
336
335
337 Return:
336 Return:
338 El ultimo file de una determinada carpeta, no se considera el path.
337 El ultimo file de una determinada carpeta, no se considera el path.
339 """
338 """
340 validFilelist = []
339 validFilelist = []
341 fileList = os.listdir(path)
340 fileList = os.listdir(path)
342
341
343 # 0 1234 567 89A BCDE
342 # 0 1234 567 89A BCDE
344 # H YYYY DDD SSS .ext
343 # H YYYY DDD SSS .ext
345
344
346 for thisFile in fileList:
345 for thisFile in fileList:
347
346
348 year = thisFile[1:5]
347 year = thisFile[1:5]
349 if not isNumber(year):
348 if not isNumber(year):
350 continue
349 continue
351
350
352 doy = thisFile[5:8]
351 doy = thisFile[5:8]
353 if not isNumber(doy):
352 if not isNumber(doy):
354 continue
353 continue
355
354
356 year = int(year)
355 year = int(year)
357 doy = int(doy)
356 doy = int(doy)
358
357
359 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
358 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
360 continue
359 continue
361
360
362 validFilelist.append(thisFile)
361 validFilelist.append(thisFile)
363
362
364 if validFilelist:
363 if validFilelist:
365 validFilelist = sorted(validFilelist, key=str.lower)
364 validFilelist = sorted(validFilelist, key=str.lower)
366 return validFilelist[-1]
365 return validFilelist[-1]
367
366
368 return None
367 return None
369
368
370
369
371 def checkForRealPath(path, foldercounter, year, doy, set, ext):
370 def checkForRealPath(path, foldercounter, year, doy, set, ext):
372 """
371 """
373 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
372 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
374 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
373 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
375 el path exacto de un determinado file.
374 el path exacto de un determinado file.
376
375
377 Example :
376 Example :
378 nombre correcto del file es .../.../D2009307/P2009307367.ext
377 nombre correcto del file es .../.../D2009307/P2009307367.ext
379
378
380 Entonces la funcion prueba con las siguientes combinaciones
379 Entonces la funcion prueba con las siguientes combinaciones
381 .../.../y2009307367.ext
380 .../.../y2009307367.ext
382 .../.../Y2009307367.ext
381 .../.../Y2009307367.ext
383 .../.../x2009307/y2009307367.ext
382 .../.../x2009307/y2009307367.ext
384 .../.../x2009307/Y2009307367.ext
383 .../.../x2009307/Y2009307367.ext
385 .../.../X2009307/y2009307367.ext
384 .../.../X2009307/y2009307367.ext
386 .../.../X2009307/Y2009307367.ext
385 .../.../X2009307/Y2009307367.ext
387 siendo para este caso, la ultima combinacion de letras, identica al file buscado
386 siendo para este caso, la ultima combinacion de letras, identica al file buscado
388
387
389 Return:
388 Return:
390 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
389 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
391 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
390 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
392 para el filename
391 para el filename
393 """
392 """
394 fullfilename = None
393 fullfilename = None
395 find_flag = False
394 find_flag = False
396 filename = None
395 filename = None
397
396
398 prefixDirList = [None, 'd', 'D']
397 prefixDirList = [None, 'd', 'D']
399 if ext.lower() == ".r": # voltage
398 if ext.lower() == ".r": # voltage
400 prefixFileList = ['d', 'D']
399 prefixFileList = ['d', 'D']
401 elif ext.lower() == ".pdata": # spectra
400 elif ext.lower() == ".pdata": # spectra
402 prefixFileList = ['p', 'P']
401 prefixFileList = ['p', 'P']
403 else:
402 else:
404 return None, filename
403 return None, filename
405
404
406 # barrido por las combinaciones posibles
405 # barrido por las combinaciones posibles
407 for prefixDir in prefixDirList:
406 for prefixDir in prefixDirList:
408 thispath = path
407 thispath = path
409 if prefixDir != None:
408 if prefixDir != None:
410 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
409 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
411 if foldercounter == 0:
410 if foldercounter == 0:
412 thispath = os.path.join(path, "%s%04d%03d" %
411 thispath = os.path.join(path, "%s%04d%03d" %
413 (prefixDir, year, doy))
412 (prefixDir, year, doy))
414 else:
413 else:
415 thispath = os.path.join(path, "%s%04d%03d_%02d" % (
414 thispath = os.path.join(path, "%s%04d%03d_%02d" % (
416 prefixDir, year, doy, foldercounter))
415 prefixDir, year, doy, foldercounter))
417 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
416 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
418 # formo el nombre del file xYYYYDDDSSS.ext
417 # formo el nombre del file xYYYYDDDSSS.ext
419 filename = "%s%04d%03d%03d%s" % (prefixFile, year, doy, set, ext)
418 filename = "%s%04d%03d%03d%s" % (prefixFile, year, doy, set, ext)
420 fullfilename = os.path.join(
419 fullfilename = os.path.join(
421 thispath, filename) # formo el path completo
420 thispath, filename) # formo el path completo
422
421
423 if os.path.exists(fullfilename): # verifico que exista
422 if os.path.exists(fullfilename): # verifico que exista
424 find_flag = True
423 find_flag = True
425 break
424 break
426 if find_flag:
425 if find_flag:
427 break
426 break
428
427
429 if not(find_flag):
428 if not(find_flag):
430 return None, filename
429 return None, filename
431
430
432 return fullfilename, filename
431 return fullfilename, filename
433
432
434
433
435 def isRadarFolder(folder):
434 def isRadarFolder(folder):
436 try:
435 try:
437 year = int(folder[1:5])
436 year = int(folder[1:5])
438 doy = int(folder[5:8])
437 doy = int(folder[5:8])
439 except:
438 except:
440 return 0
439 return 0
441
440
442 return 1
441 return 1
443
442
444
443
445 def isRadarFile(file):
444 def isRadarFile(file):
446 try:
445 try:
447 year = int(file[1:5])
446 year = int(file[1:5])
448 doy = int(file[5:8])
447 doy = int(file[5:8])
449 set = int(file[8:11])
448 set = int(file[8:11])
450 except:
449 except:
451 return 0
450 return 0
452
451
453 return 1
452 return 1
454
453
455
454
456 def getDateFromRadarFile(file):
455 def getDateFromRadarFile(file):
457 try:
456 try:
458 year = int(file[1:5])
457 year = int(file[1:5])
459 doy = int(file[5:8])
458 doy = int(file[5:8])
460 set = int(file[8:11])
459 set = int(file[8:11])
461 except:
460 except:
462 return None
461 return None
463
462
464 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
463 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
465 return thisDate
464 return thisDate
466
465
467
466
468 def getDateFromRadarFolder(folder):
467 def getDateFromRadarFolder(folder):
469 try:
468 try:
470 year = int(folder[1:5])
469 year = int(folder[1:5])
471 doy = int(folder[5:8])
470 doy = int(folder[5:8])
472 except:
471 except:
473 return None
472 return None
474
473
475 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
474 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
476 return thisDate
475 return thisDate
477
476
478
477
479 class JRODataIO:
478 class JRODataIO:
480
479
481 c = 3E8
480 c = 3E8
482
481
483 isConfig = False
482 isConfig = False
484
483
485 basicHeaderObj = None
484 basicHeaderObj = None
486
485
487 systemHeaderObj = None
486 systemHeaderObj = None
488
487
489 radarControllerHeaderObj = None
488 radarControllerHeaderObj = None
490
489
491 processingHeaderObj = None
490 processingHeaderObj = None
492
491
493 dtype = None
492 dtype = None
494
493
495 pathList = []
494 pathList = []
496
495
497 filenameList = []
496 filenameList = []
498
497
499 filename = None
498 filename = None
500
499
501 ext = None
500 ext = None
502
501
503 flagIsNewFile = 1
502 flagIsNewFile = 1
504
503
505 flagDiscontinuousBlock = 0
504 flagDiscontinuousBlock = 0
506
505
507 flagIsNewBlock = 0
506 flagIsNewBlock = 0
508
507
509 fp = None
508 fp = None
510
509
511 firstHeaderSize = 0
510 firstHeaderSize = 0
512
511
513 basicHeaderSize = 24
512 basicHeaderSize = 24
514
513
515 versionFile = 1103
514 versionFile = 1103
516
515
517 fileSize = None
516 fileSize = None
518
517
519 # ippSeconds = None
518 # ippSeconds = None
520
519
521 fileSizeByHeader = None
520 fileSizeByHeader = None
522
521
523 fileIndex = None
522 fileIndex = None
524
523
525 profileIndex = None
524 profileIndex = None
526
525
527 blockIndex = None
526 blockIndex = None
528
527
529 nTotalBlocks = None
528 nTotalBlocks = None
530
529
531 maxTimeStep = 30
530 maxTimeStep = 30
532
531
533 lastUTTime = None
532 lastUTTime = None
534
533
535 datablock = None
534 datablock = None
536
535
537 dataOut = None
536 dataOut = None
538
537
539 blocksize = None
538 blocksize = None
540
539
541 getByBlock = False
540 getByBlock = False
542
541
543 def __init__(self):
542 def __init__(self):
544
543
545 raise NotImplementedError
544 raise NotImplementedError
546
545
547 def run(self):
546 def run(self):
548
547
549 raise NotImplementedError
548 raise NotImplementedError
550
549
551 def getDtypeWidth(self):
550 def getDtypeWidth(self):
552
551
553 dtype_index = get_dtype_index(self.dtype)
552 dtype_index = get_dtype_index(self.dtype)
554 dtype_width = get_dtype_width(dtype_index)
553 dtype_width = get_dtype_width(dtype_index)
555
554
556 return dtype_width
555 return dtype_width
557
556
558 def getAllowedArgs(self):
557 def getAllowedArgs(self):
559 if hasattr(self, '__attrs__'):
558 if hasattr(self, '__attrs__'):
560 return self.__attrs__
559 return self.__attrs__
561 else:
560 else:
562 return inspect.getargspec(self.run).args
561 return inspect.getargspec(self.run).args
563
562
564
563
565 class JRODataReader(JRODataIO):
564 class JRODataReader(JRODataIO):
566
565
567 online = 0
566 online = 0
568
567
569 realtime = 0
568 realtime = 0
570
569
571 nReadBlocks = 0
570 nReadBlocks = 0
572
571
573 delay = 10 # number of seconds waiting a new file
572 delay = 10 # number of seconds waiting a new file
574
573
575 nTries = 3 # quantity tries
574 nTries = 3 # quantity tries
576
575
577 nFiles = 3 # number of files for searching
576 nFiles = 3 # number of files for searching
578
577
579 path = None
578 path = None
580
579
581 foldercounter = 0
580 foldercounter = 0
582
581
583 flagNoMoreFiles = 0
582 flagNoMoreFiles = 0
584
583
585 datetimeList = []
584 datetimeList = []
586
585
587 __isFirstTimeOnline = 1
586 __isFirstTimeOnline = 1
588
587
589 __printInfo = True
588 __printInfo = True
590
589
591 profileIndex = None
590 profileIndex = None
592
591
593 nTxs = 1
592 nTxs = 1
594
593
595 txIndex = None
594 txIndex = None
596
595
597 # Added--------------------
596 # Added--------------------
598
597
599 selBlocksize = None
598 selBlocksize = None
600
599
601 selBlocktime = None
600 selBlocktime = None
602
601
603 def __init__(self):
602 def __init__(self):
604 """
603 """
605 This class is used to find data files
604 This class is used to find data files
606
605
607 Example:
606 Example:
608 reader = JRODataReader()
607 reader = JRODataReader()
609 fileList = reader.findDataFiles()
608 fileList = reader.findDataFiles()
610
609
611 """
610 """
612 pass
611 pass
613
612
614 def createObjByDefault(self):
613 def createObjByDefault(self):
615 """
614 """
616
615
617 """
616 """
618 raise NotImplementedError
617 raise NotImplementedError
619
618
620 def getBlockDimension(self):
619 def getBlockDimension(self):
621
620
622 raise NotImplementedError
621 raise NotImplementedError
623
622
624 def searchFilesOffLine(self,
623 def searchFilesOffLine(self,
625 path,
624 path,
626 startDate=None,
625 startDate=None,
627 endDate=None,
626 endDate=None,
628 startTime=datetime.time(0, 0, 0),
627 startTime=datetime.time(0, 0, 0),
629 endTime=datetime.time(23, 59, 59),
628 endTime=datetime.time(23, 59, 59),
630 set=None,
629 set=None,
631 expLabel='',
630 expLabel='',
632 ext='.r',
631 ext='.r',
633 cursor=None,
632 cursor=None,
634 skip=None,
633 skip=None,
635 walk=True):
634 walk=True):
636
635
637 self.filenameList = []
636 self.filenameList = []
638 self.datetimeList = []
637 self.datetimeList = []
639
638
640 pathList = []
639 pathList = []
641
640
642 dateList, pathList = self.findDatafiles(
641 dateList, pathList = self.findDatafiles(
643 path, startDate, endDate, expLabel, ext, walk, include_path=True)
642 path, startDate, endDate, expLabel, ext, walk, include_path=True)
644
643
645 if dateList == []:
644 if dateList == []:
646 return [], []
645 return [], []
647
646
648 if len(dateList) > 1:
647 if len(dateList) > 1:
649 print("[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList)))
648 print("[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList)))
650 else:
649 else:
651 print("[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0]))
650 print("[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0]))
652
651
653 filenameList = []
652 filenameList = []
654 datetimeList = []
653 datetimeList = []
655
654
656 for thisPath in pathList:
655 for thisPath in pathList:
657
656
658 fileList = glob.glob1(thisPath, "*%s" % ext)
657 fileList = glob.glob1(thisPath, "*%s" % ext)
659 fileList.sort()
658 fileList.sort()
660
659
661 for file in fileList:
660 for file in fileList:
662
661
663 filename = os.path.join(thisPath, file)
662 filename = os.path.join(thisPath, file)
664
663
665 if not isFileInDateRange(filename, startDate, endDate):
664 if not isFileInDateRange(filename, startDate, endDate):
666 continue
665 continue
667
666
668 thisDatetime = isFileInTimeRange(
667 thisDatetime = isFileInTimeRange(
669 filename, startDate, endDate, startTime, endTime)
668 filename, startDate, endDate, startTime, endTime)
670
669
671 if not(thisDatetime):
670 if not(thisDatetime):
672 continue
671 continue
673
672
674 filenameList.append(filename)
673 filenameList.append(filename)
675 datetimeList.append(thisDatetime)
674 datetimeList.append(thisDatetime)
676
675
677 if cursor is not None and skip is not None:
676 if cursor is not None and skip is not None:
678 filenameList = filenameList[cursor * skip:cursor * skip + skip]
677 filenameList = filenameList[cursor * skip:cursor * skip + skip]
679 datetimeList = datetimeList[cursor * skip:cursor * skip + skip]
678 datetimeList = datetimeList[cursor * skip:cursor * skip + skip]
680
679
681 if not(filenameList):
680 if not(filenameList):
682 print("[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path))
681 print("[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path))
683 return [], []
682 return [], []
684
683
685 print("[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime))
684 print("[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime))
686
685
687 # for i in range(len(filenameList)):
686 # for i in range(len(filenameList)):
688 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
687 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
689
688
690 self.filenameList = filenameList
689 self.filenameList = filenameList
691 self.datetimeList = datetimeList
690 self.datetimeList = datetimeList
692
691
693 return pathList, filenameList
692 return pathList, filenameList
694
693
695 def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None):
694 def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None):
696 """
695 """
697 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
696 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
698 devuelve el archivo encontrado ademas de otros datos.
697 devuelve el archivo encontrado ademas de otros datos.
699
698
700 Input:
699 Input:
701 path : carpeta donde estan contenidos los files que contiene data
700 path : carpeta donde estan contenidos los files que contiene data
702
701
703 expLabel : Nombre del subexperimento (subfolder)
702 expLabel : Nombre del subexperimento (subfolder)
704
703
705 ext : extension de los files
704 ext : extension de los files
706
705
707 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
706 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
708
707
709 Return:
708 Return:
710 directory : eL directorio donde esta el file encontrado
709 directory : eL directorio donde esta el file encontrado
711 filename : el ultimo file de una determinada carpeta
710 filename : el ultimo file de una determinada carpeta
712 year : el anho
711 year : el anho
713 doy : el numero de dia del anho
712 doy : el numero de dia del anho
714 set : el set del archivo
713 set : el set del archivo
715
714
716
715
717 """
716 """
718 if not os.path.isdir(path):
717 if not os.path.isdir(path):
719 return None, None, None, None, None, None
718 return None, None, None, None, None, None
720
719
721 dirList = []
720 dirList = []
722
721
723 if not walk:
722 if not walk:
724 fullpath = path
723 fullpath = path
725 foldercounter = 0
724 foldercounter = 0
726 else:
725 else:
727 # Filtra solo los directorios
726 # Filtra solo los directorios
728 for thisPath in os.listdir(path):
727 for thisPath in os.listdir(path):
729 if not os.path.isdir(os.path.join(path, thisPath)):
728 if not os.path.isdir(os.path.join(path, thisPath)):
730 continue
729 continue
731 if not isRadarFolder(thisPath):
730 if not isRadarFolder(thisPath):
732 continue
731 continue
733
732
734 dirList.append(thisPath)
733 dirList.append(thisPath)
735
734
736 if not(dirList):
735 if not(dirList):
737 return None, None, None, None, None, None
736 return None, None, None, None, None, None
738
737
739 dirList = sorted(dirList, key=str.lower)
738 dirList = sorted(dirList, key=str.lower)
740
739
741 doypath = dirList[-1]
740 doypath = dirList[-1]
742 foldercounter = int(doypath.split('_')[1]) if len(
741 foldercounter = int(doypath.split('_')[1]) if len(
743 doypath.split('_')) > 1 else 0
742 doypath.split('_')) > 1 else 0
744 fullpath = os.path.join(path, doypath, expLabel)
743 fullpath = os.path.join(path, doypath, expLabel)
745
744
746 print("[Reading] %s folder was found: " % (fullpath))
745 print("[Reading] %s folder was found: " % (fullpath))
747
746
748 if set == None:
747 if set == None:
749 filename = getlastFileFromPath(fullpath, ext)
748 filename = getlastFileFromPath(fullpath, ext)
750 else:
749 else:
751 filename = getFileFromSet(fullpath, ext, set)
750 filename = getFileFromSet(fullpath, ext, set)
752
751
753 if not(filename):
752 if not(filename):
754 return None, None, None, None, None, None
753 return None, None, None, None, None, None
755
754
756 print("[Reading] %s file was found" % (filename))
755 print("[Reading] %s file was found" % (filename))
757
756
758 if not(self.__verifyFile(os.path.join(fullpath, filename))):
757 if not(self.__verifyFile(os.path.join(fullpath, filename))):
759 return None, None, None, None, None, None
758 return None, None, None, None, None, None
760
759
761 year = int(filename[1:5])
760 year = int(filename[1:5])
762 doy = int(filename[5:8])
761 doy = int(filename[5:8])
763 set = int(filename[8:11])
762 set = int(filename[8:11])
764
763
765 return fullpath, foldercounter, filename, year, doy, set
764 return fullpath, foldercounter, filename, year, doy, set
766
765
767 def __setNextFileOffline(self):
766 def __setNextFileOffline(self):
768
767
769 idFile = self.fileIndex
768 idFile = self.fileIndex
770
769
771 while (True):
770 while (True):
772 idFile += 1
771 idFile += 1
773 if not(idFile < len(self.filenameList)):
772 if not(idFile < len(self.filenameList)):
774 self.flagNoMoreFiles = 1
773 self.flagNoMoreFiles = 1
775 # print "[Reading] No more Files"
774 # print "[Reading] No more Files"
776 return 0
775 return 0
777
776
778 filename = self.filenameList[idFile]
777 filename = self.filenameList[idFile]
779
778
780 if not(self.__verifyFile(filename)):
779 if not(self.__verifyFile(filename)):
781 continue
780 continue
782
781
783 fileSize = os.path.getsize(filename)
782 fileSize = os.path.getsize(filename)
784 fp = open(filename, 'rb')
783 fp = open(filename, 'rb')
785 break
784 break
786
785
787 self.flagIsNewFile = 1
786 self.flagIsNewFile = 1
788 self.fileIndex = idFile
787 self.fileIndex = idFile
789 self.filename = filename
788 self.filename = filename
790 self.fileSize = fileSize
789 self.fileSize = fileSize
791 self.fp = fp
790 self.fp = fp
792
791
793 # print "[Reading] Setting the file: %s"%self.filename
792 # print "[Reading] Setting the file: %s"%self.filename
794
793
795 return 1
794 return 1
796
795
797 def __setNextFileOnline(self):
796 def __setNextFileOnline(self):
798 """
797 """
799 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
798 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
800 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
799 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
801 siguientes.
800 siguientes.
802
801
803 Affected:
802 Affected:
804 self.flagIsNewFile
803 self.flagIsNewFile
805 self.filename
804 self.filename
806 self.fileSize
805 self.fileSize
807 self.fp
806 self.fp
808 self.set
807 self.set
809 self.flagNoMoreFiles
808 self.flagNoMoreFiles
810
809
811 Return:
810 Return:
812 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
811 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
813 1 : si el file fue abierto con exito y esta listo a ser leido
812 1 : si el file fue abierto con exito y esta listo a ser leido
814
813
815 Excepciones:
814 Excepciones:
816 Si un determinado file no puede ser abierto
815 Si un determinado file no puede ser abierto
817 """
816 """
818 nFiles = 0
817 nFiles = 0
819 fileOk_flag = False
818 fileOk_flag = False
820 firstTime_flag = True
819 firstTime_flag = True
821
820
822 self.set += 1
821 self.set += 1
823
822
824 if self.set > 999:
823 if self.set > 999:
825 self.set = 0
824 self.set = 0
826 self.foldercounter += 1
825 self.foldercounter += 1
827
826
828 # busca el 1er file disponible
827 # busca el 1er file disponible
829 fullfilename, filename = checkForRealPath(
828 fullfilename, filename = checkForRealPath(
830 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
829 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
831 if fullfilename:
830 if fullfilename:
832 if self.__verifyFile(fullfilename, False):
831 if self.__verifyFile(fullfilename, False):
833 fileOk_flag = True
832 fileOk_flag = True
834
833
835 # si no encuentra un file entonces espera y vuelve a buscar
834 # si no encuentra un file entonces espera y vuelve a buscar
836 if not(fileOk_flag):
835 if not(fileOk_flag):
837 # busco en los siguientes self.nFiles+1 files posibles
836 # busco en los siguientes self.nFiles+1 files posibles
838 for nFiles in range(self.nFiles + 1):
837 for nFiles in range(self.nFiles + 1):
839
838
840 if firstTime_flag: # si es la 1era vez entonces hace el for self.nTries veces
839 if firstTime_flag: # si es la 1era vez entonces hace el for self.nTries veces
841 tries = self.nTries
840 tries = self.nTries
842 else:
841 else:
843 tries = 1 # si no es la 1era vez entonces solo lo hace una vez
842 tries = 1 # si no es la 1era vez entonces solo lo hace una vez
844
843
845 for nTries in range(tries):
844 for nTries in range(tries):
846 if firstTime_flag:
845 if firstTime_flag:
847 print("\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1))
846 print("\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1))
848 sleep(self.delay)
847 sleep(self.delay)
849 else:
848 else:
850 print("\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext))
849 print("\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext))
851
850
852 fullfilename, filename = checkForRealPath(
851 fullfilename, filename = checkForRealPath(
853 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
852 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
854 if fullfilename:
853 if fullfilename:
855 if self.__verifyFile(fullfilename):
854 if self.__verifyFile(fullfilename):
856 fileOk_flag = True
855 fileOk_flag = True
857 break
856 break
858
857
859 if fileOk_flag:
858 if fileOk_flag:
860 break
859 break
861
860
862 firstTime_flag = False
861 firstTime_flag = False
863
862
864 log.warning('Skipping the file {} due to this file doesn\'t exist'.format(filename))
863 log.warning('Skipping the file {} due to this file doesn\'t exist'.format(filename))
865 self.set += 1
864 self.set += 1
866
865
867 # si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
866 # si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
868 if nFiles == (self.nFiles - 1):
867 if nFiles == (self.nFiles - 1):
869 self.set = 0
868 self.set = 0
870 self.doy += 1
869 self.doy += 1
871 self.foldercounter = 0
870 self.foldercounter = 0
872
871
873 if fileOk_flag:
872 if fileOk_flag:
874 self.fileSize = os.path.getsize(fullfilename)
873 self.fileSize = os.path.getsize(fullfilename)
875 self.filename = fullfilename
874 self.filename = fullfilename
876 self.flagIsNewFile = 1
875 self.flagIsNewFile = 1
877 if self.fp != None:
876 if self.fp != None:
878 self.fp.close()
877 self.fp.close()
879 self.fp = open(fullfilename, 'rb')
878 self.fp = open(fullfilename, 'rb')
880 self.flagNoMoreFiles = 0
879 self.flagNoMoreFiles = 0
881 # print '[Reading] Setting the file: %s' % fullfilename
880 # print '[Reading] Setting the file: %s' % fullfilename
882 else:
881 else:
883 self.fileSize = 0
882 self.fileSize = 0
884 self.filename = None
883 self.filename = None
885 self.flagIsNewFile = 0
884 self.flagIsNewFile = 0
886 self.fp = None
885 self.fp = None
887 self.flagNoMoreFiles = 1
886 self.flagNoMoreFiles = 1
887 # print '[Reading] No more files to read'
888
888
889 return fileOk_flag
889 return fileOk_flag
890
890
891 def setNextFile(self):
891 def setNextFile(self):
892 if self.fp != None:
892 if self.fp != None:
893 self.fp.close()
893 self.fp.close()
894
894
895 if self.online:
895 if self.online:
896 newFile = self.__setNextFileOnline()
896 newFile = self.__setNextFileOnline()
897 else:
897 else:
898 newFile = self.__setNextFileOffline()
898 newFile = self.__setNextFileOffline()
899
899
900 if not(newFile):
900 if not(newFile):
901 raise schainpy.admin.SchainWarning('No more files to read')
901 raise(schainpy.admin.SchainWarning('No more files to read'))
902 return 0
902 return 0
903
903
904 if self.verbose:
904 if self.verbose:
905 print('[Reading] Setting the file: %s' % self.filename)
905 print('[Reading] Setting the file: %s' % self.filename)
906
906
907 self.__readFirstHeader()
907 self.__readFirstHeader()
908 self.nReadBlocks = 0
908 self.nReadBlocks = 0
909 return 1
909 return 1
910
910
911 def __waitNewBlock(self):
911 def __waitNewBlock(self):
912 """
912 """
913 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
913 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
914
914
915 Si el modo de lectura es OffLine siempre retorn 0
915 Si el modo de lectura es OffLine siempre retorn 0
916 """
916 """
917 if not self.online:
917 if not self.online:
918 return 0
918 return 0
919
919
920 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
920 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
921 return 0
921 return 0
922
922
923 currentPointer = self.fp.tell()
923 currentPointer = self.fp.tell()
924
924
925 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
925 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
926
926
927 for nTries in range(self.nTries):
927 for nTries in range(self.nTries):
928
928
929 self.fp.close()
929 self.fp.close()
930 self.fp = open(self.filename, 'rb')
930 self.fp = open(self.filename, 'rb')
931 self.fp.seek(currentPointer)
931 self.fp.seek(currentPointer)
932
932
933 self.fileSize = os.path.getsize(self.filename)
933 self.fileSize = os.path.getsize(self.filename)
934 currentSize = self.fileSize - currentPointer
934 currentSize = self.fileSize - currentPointer
935
935
936 if (currentSize >= neededSize):
936 if (currentSize >= neededSize):
937 self.basicHeaderObj.read(self.fp)
937 self.basicHeaderObj.read(self.fp)
938 return 1
938 return 1
939
939
940 if self.fileSize == self.fileSizeByHeader:
940 if self.fileSize == self.fileSizeByHeader:
941 # self.flagEoF = True
941 # self.flagEoF = True
942 return 0
942 return 0
943
943
944 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
944 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
945 sleep(self.delay)
945 sleep(self.delay)
946
946
947 return 0
947 return 0
948
948
949 def waitDataBlock(self, pointer_location):
949 def waitDataBlock(self, pointer_location):
950
950
951 currentPointer = pointer_location
951 currentPointer = pointer_location
952
952
953 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
953 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
954
954
955 for nTries in range(self.nTries):
955 for nTries in range(self.nTries):
956 self.fp.close()
956 self.fp.close()
957 self.fp = open(self.filename, 'rb')
957 self.fp = open(self.filename, 'rb')
958 self.fp.seek(currentPointer)
958 self.fp.seek(currentPointer)
959
959
960 self.fileSize = os.path.getsize(self.filename)
960 self.fileSize = os.path.getsize(self.filename)
961 currentSize = self.fileSize - currentPointer
961 currentSize = self.fileSize - currentPointer
962
962
963 if (currentSize >= neededSize):
963 if (currentSize >= neededSize):
964 return 1
964 return 1
965
965
966 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
966 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
967 sleep(self.delay)
967 sleep(self.delay)
968
968
969 return 0
969 return 0
970
970
971 def __jumpToLastBlock(self):
971 def __jumpToLastBlock(self):
972
972
973 if not(self.__isFirstTimeOnline):
973 if not(self.__isFirstTimeOnline):
974 return
974 return
975
975
976 csize = self.fileSize - self.fp.tell()
976 csize = self.fileSize - self.fp.tell()
977 blocksize = self.processingHeaderObj.blockSize
977 blocksize = self.processingHeaderObj.blockSize
978
978
979 # salta el primer bloque de datos
979 # salta el primer bloque de datos
980 if csize > self.processingHeaderObj.blockSize:
980 if csize > self.processingHeaderObj.blockSize:
981 self.fp.seek(self.fp.tell() + blocksize)
981 self.fp.seek(self.fp.tell() + blocksize)
982 else:
982 else:
983 return
983 return
984
984
985 csize = self.fileSize - self.fp.tell()
985 csize = self.fileSize - self.fp.tell()
986 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
986 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
987 while True:
987 while True:
988
988
989 if self.fp.tell() < self.fileSize:
989 if self.fp.tell() < self.fileSize:
990 self.fp.seek(self.fp.tell() + neededsize)
990 self.fp.seek(self.fp.tell() + neededsize)
991 else:
991 else:
992 self.fp.seek(self.fp.tell() - neededsize)
992 self.fp.seek(self.fp.tell() - neededsize)
993 break
993 break
994
994
995 # csize = self.fileSize - self.fp.tell()
995 # csize = self.fileSize - self.fp.tell()
996 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
996 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
997 # factor = int(csize/neededsize)
997 # factor = int(csize/neededsize)
998 # if factor > 0:
998 # if factor > 0:
999 # self.fp.seek(self.fp.tell() + factor*neededsize)
999 # self.fp.seek(self.fp.tell() + factor*neededsize)
1000
1000
1001 self.flagIsNewFile = 0
1001 self.flagIsNewFile = 0
1002 self.__isFirstTimeOnline = 0
1002 self.__isFirstTimeOnline = 0
1003
1003
1004 def __setNewBlock(self):
1004 def __setNewBlock(self):
1005 # if self.server is None:
1005 # if self.server is None:
1006 if self.fp == None:
1006 if self.fp == None:
1007 return 0
1007 return 0
1008
1008
1009 # if self.online:
1009 # if self.online:
1010 # self.__jumpToLastBlock()
1010 # self.__jumpToLastBlock()
1011
1011
1012 if self.flagIsNewFile:
1012 if self.flagIsNewFile:
1013 self.lastUTTime = self.basicHeaderObj.utc
1013 self.lastUTTime = self.basicHeaderObj.utc
1014 return 1
1014 return 1
1015
1015
1016 if self.realtime:
1016 if self.realtime:
1017 self.flagDiscontinuousBlock = 1
1017 self.flagDiscontinuousBlock = 1
1018 if not(self.setNextFile()):
1018 if not(self.setNextFile()):
1019 return 0
1019 return 0
1020 else:
1020 else:
1021 return 1
1021 return 1
1022 # if self.server is None:
1022 # if self.server is None:
1023 currentSize = self.fileSize - self.fp.tell()
1023 currentSize = self.fileSize - self.fp.tell()
1024 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1024 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1025 if (currentSize >= neededSize):
1025 if (currentSize >= neededSize):
1026 self.basicHeaderObj.read(self.fp)
1026 self.basicHeaderObj.read(self.fp)
1027 self.lastUTTime = self.basicHeaderObj.utc
1027 self.lastUTTime = self.basicHeaderObj.utc
1028 return 1
1028 return 1
1029 # else:
1029 # else:
1030 # self.basicHeaderObj.read(self.zHeader)
1030 # self.basicHeaderObj.read(self.zHeader)
1031 # self.lastUTTime = self.basicHeaderObj.utc
1031 # self.lastUTTime = self.basicHeaderObj.utc
1032 # return 1
1032 # return 1
1033 if self.__waitNewBlock():
1033 if self.__waitNewBlock():
1034 self.lastUTTime = self.basicHeaderObj.utc
1034 self.lastUTTime = self.basicHeaderObj.utc
1035 return 1
1035 return 1
1036 # if self.server is None:
1036 # if self.server is None:
1037 if not(self.setNextFile()):
1037 if not(self.setNextFile()):
1038 return 0
1038 return 0
1039
1039
1040 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
1040 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
1041 self.lastUTTime = self.basicHeaderObj.utc
1041 self.lastUTTime = self.basicHeaderObj.utc
1042
1042
1043 self.flagDiscontinuousBlock = 0
1043 self.flagDiscontinuousBlock = 0
1044
1044
1045 if deltaTime > self.maxTimeStep:
1045 if deltaTime > self.maxTimeStep:
1046 self.flagDiscontinuousBlock = 1
1046 self.flagDiscontinuousBlock = 1
1047
1047
1048 return 1
1048 return 1
1049
1049
1050 def readNextBlock(self):
1050 def readNextBlock(self):
1051
1051
1052 # Skip block out of startTime and endTime
1052 # Skip block out of startTime and endTime
1053 while True:
1053 while True:
1054 if not(self.__setNewBlock()):
1054 if not(self.__setNewBlock()):
1055 raise schainpy
1055 raise(schainpy.admin.SchainWarning('No more files'))
1056 return 0
1056 return 0
1057
1057
1058 if not(self.readBlock()):
1058 if not(self.readBlock()):
1059 return 0
1059 return 0
1060
1060
1061 self.getBasicHeader()
1061 self.getBasicHeader()
1062 if (self.dataOut.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or (self.dataOut.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
1062 if (self.dataOut.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or (self.dataOut.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
1063 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
1063 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
1064 self.processingHeaderObj.dataBlocksPerFile,
1064 self.processingHeaderObj.dataBlocksPerFile,
1065 self.dataOut.datatime.ctime()))
1065 self.dataOut.datatime.ctime()))
1066 continue
1066 continue
1067
1067
1068 break
1068 break
1069
1069
1070 if self.verbose:
1070 if self.verbose:
1071 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
1071 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
1072 self.processingHeaderObj.dataBlocksPerFile,
1072 self.processingHeaderObj.dataBlocksPerFile,
1073 self.dataOut.datatime.ctime()))
1073 self.dataOut.datatime.ctime()))
1074 return 1
1074 return 1
1075
1075
1076 def __readFirstHeader(self):
1076 def __readFirstHeader(self):
1077
1077
1078 self.basicHeaderObj.read(self.fp)
1078 self.basicHeaderObj.read(self.fp)
1079 self.systemHeaderObj.read(self.fp)
1079 self.systemHeaderObj.read(self.fp)
1080 self.radarControllerHeaderObj.read(self.fp)
1080 self.radarControllerHeaderObj.read(self.fp)
1081 self.processingHeaderObj.read(self.fp)
1081 self.processingHeaderObj.read(self.fp)
1082
1082
1083 self.firstHeaderSize = self.basicHeaderObj.size
1083 self.firstHeaderSize = self.basicHeaderObj.size
1084
1084
1085 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
1085 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
1086 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
1086 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
1087 if datatype == 0:
1087 if datatype == 0:
1088 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
1088 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
1089 elif datatype == 1:
1089 elif datatype == 1:
1090 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
1090 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
1091 elif datatype == 2:
1091 elif datatype == 2:
1092 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
1092 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
1093 elif datatype == 3:
1093 elif datatype == 3:
1094 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
1094 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
1095 elif datatype == 4:
1095 elif datatype == 4:
1096 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
1096 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
1097 elif datatype == 5:
1097 elif datatype == 5:
1098 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
1098 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
1099 else:
1099 else:
1100 raise ValueError('Data type was not defined')
1100 raise ValueError('Data type was not defined')
1101
1101
1102 self.dtype = datatype_str
1102 self.dtype = datatype_str
1103 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1103 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1104 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
1104 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
1105 self.firstHeaderSize + self.basicHeaderSize * \
1105 self.firstHeaderSize + self.basicHeaderSize * \
1106 (self.processingHeaderObj.dataBlocksPerFile - 1)
1106 (self.processingHeaderObj.dataBlocksPerFile - 1)
1107 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1107 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1108 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1108 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1109 self.getBlockDimension()
1109 self.getBlockDimension()
1110
1110
1111 def __verifyFile(self, filename, msgFlag=True):
1111 def __verifyFile(self, filename, msgFlag=True):
1112
1112
1113 msg = None
1113 msg = None
1114
1114
1115 try:
1115 try:
1116 fp = open(filename, 'rb')
1116 fp = open(filename, 'rb')
1117 except IOError:
1117 except IOError:
1118
1118
1119 if msgFlag:
1119 if msgFlag:
1120 print("[Reading] File %s can't be opened" % (filename))
1120 print("[Reading] File %s can't be opened" % (filename))
1121
1121
1122 return False
1122 return False
1123
1123
1124 currentPosition = fp.tell()
1124 currentPosition = fp.tell()
1125 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1125 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1126
1126
1127 if neededSize == 0:
1127 if neededSize == 0:
1128 basicHeaderObj = BasicHeader(LOCALTIME)
1128 basicHeaderObj = BasicHeader(LOCALTIME)
1129 systemHeaderObj = SystemHeader()
1129 systemHeaderObj = SystemHeader()
1130 radarControllerHeaderObj = RadarControllerHeader()
1130 radarControllerHeaderObj = RadarControllerHeader()
1131 processingHeaderObj = ProcessingHeader()
1131 processingHeaderObj = ProcessingHeader()
1132
1132
1133 if not(basicHeaderObj.read(fp)):
1133 if not(basicHeaderObj.read(fp)):
1134 fp.close()
1134 fp.close()
1135 return False
1135 return False
1136
1136
1137 if not(systemHeaderObj.read(fp)):
1137 if not(systemHeaderObj.read(fp)):
1138 fp.close()
1138 fp.close()
1139 return False
1139 return False
1140
1140
1141 if not(radarControllerHeaderObj.read(fp)):
1141 if not(radarControllerHeaderObj.read(fp)):
1142 fp.close()
1142 fp.close()
1143 return False
1143 return False
1144
1144
1145 if not(processingHeaderObj.read(fp)):
1145 if not(processingHeaderObj.read(fp)):
1146 fp.close()
1146 fp.close()
1147 return False
1147 return False
1148
1148
1149 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1149 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1150 else:
1150 else:
1151 msg = "[Reading] Skipping the file %s due to it hasn't enough data" % filename
1151 msg = "[Reading] Skipping the file %s due to it hasn't enough data" % filename
1152
1152
1153 fp.close()
1153 fp.close()
1154
1154
1155 fileSize = os.path.getsize(filename)
1155 fileSize = os.path.getsize(filename)
1156 currentSize = fileSize - currentPosition
1156 currentSize = fileSize - currentPosition
1157
1157
1158 if currentSize < neededSize:
1158 if currentSize < neededSize:
1159 if msgFlag and (msg != None):
1159 if msgFlag and (msg != None):
1160 print(msg)
1160 print(msg)
1161 return False
1161 return False
1162
1162
1163 return True
1163 return True
1164
1164
1165 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1165 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1166
1166
1167 path_empty = True
1167 path_empty = True
1168
1168
1169 dateList = []
1169 dateList = []
1170 pathList = []
1170 pathList = []
1171
1171
1172 multi_path = path.split(',')
1172 multi_path = path.split(',')
1173
1173
1174 if not walk:
1174 if not walk:
1175
1175
1176 for single_path in multi_path:
1176 for single_path in multi_path:
1177
1177
1178 if not os.path.isdir(single_path):
1178 if not os.path.isdir(single_path):
1179 continue
1179 continue
1180
1180
1181 fileList = glob.glob1(single_path, "*" + ext)
1181 fileList = glob.glob1(single_path, "*" + ext)
1182
1182
1183 if not fileList:
1183 if not fileList:
1184 continue
1184 continue
1185
1185
1186 path_empty = False
1186 path_empty = False
1187
1187
1188 fileList.sort()
1188 fileList.sort()
1189
1189
1190 for thisFile in fileList:
1190 for thisFile in fileList:
1191
1191
1192 if not os.path.isfile(os.path.join(single_path, thisFile)):
1192 if not os.path.isfile(os.path.join(single_path, thisFile)):
1193 continue
1193 continue
1194
1194
1195 if not isRadarFile(thisFile):
1195 if not isRadarFile(thisFile):
1196 continue
1196 continue
1197
1197
1198 if not isFileInDateRange(thisFile, startDate, endDate):
1198 if not isFileInDateRange(thisFile, startDate, endDate):
1199 continue
1199 continue
1200
1200
1201 thisDate = getDateFromRadarFile(thisFile)
1201 thisDate = getDateFromRadarFile(thisFile)
1202
1202
1203 if thisDate in dateList:
1203 if thisDate in dateList:
1204 continue
1204 continue
1205
1205
1206 dateList.append(thisDate)
1206 dateList.append(thisDate)
1207 pathList.append(single_path)
1207 pathList.append(single_path)
1208
1208
1209 else:
1209 else:
1210 for single_path in multi_path:
1210 for single_path in multi_path:
1211
1211
1212 if not os.path.isdir(single_path):
1212 if not os.path.isdir(single_path):
1213 continue
1213 continue
1214
1214
1215 dirList = []
1215 dirList = []
1216
1216
1217 for thisPath in os.listdir(single_path):
1217 for thisPath in os.listdir(single_path):
1218
1218
1219 if not os.path.isdir(os.path.join(single_path, thisPath)):
1219 if not os.path.isdir(os.path.join(single_path, thisPath)):
1220 continue
1220 continue
1221
1221
1222 if not isRadarFolder(thisPath):
1222 if not isRadarFolder(thisPath):
1223 continue
1223 continue
1224
1224
1225 if not isFolderInDateRange(thisPath, startDate, endDate):
1225 if not isFolderInDateRange(thisPath, startDate, endDate):
1226 continue
1226 continue
1227
1227
1228 dirList.append(thisPath)
1228 dirList.append(thisPath)
1229
1229
1230 if not dirList:
1230 if not dirList:
1231 continue
1231 continue
1232
1232
1233 dirList.sort()
1233 dirList.sort()
1234
1234
1235 for thisDir in dirList:
1235 for thisDir in dirList:
1236
1236
1237 datapath = os.path.join(single_path, thisDir, expLabel)
1237 datapath = os.path.join(single_path, thisDir, expLabel)
1238 fileList = glob.glob1(datapath, "*" + ext)
1238 fileList = glob.glob1(datapath, "*" + ext)
1239
1239
1240 if not fileList:
1240 if not fileList:
1241 continue
1241 continue
1242
1242
1243 path_empty = False
1243 path_empty = False
1244
1244
1245 thisDate = getDateFromRadarFolder(thisDir)
1245 thisDate = getDateFromRadarFolder(thisDir)
1246
1246
1247 pathList.append(datapath)
1247 pathList.append(datapath)
1248 dateList.append(thisDate)
1248 dateList.append(thisDate)
1249
1249
1250 dateList.sort()
1250 dateList.sort()
1251
1251
1252 if walk:
1252 if walk:
1253 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1253 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1254 else:
1254 else:
1255 pattern_path = multi_path[0]
1255 pattern_path = multi_path[0]
1256
1256
1257 if path_empty:
1257 if path_empty:
1258 print("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1258 print("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1259 else:
1259 else:
1260 if not dateList:
1260 if not dateList:
1261 print("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1261 print("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1262
1262
1263 if include_path:
1263 if include_path:
1264 return dateList, pathList
1264 return dateList, pathList
1265
1265
1266 return dateList
1266 return dateList
1267
1267
1268 def setup(self,
1268 def setup(self,
1269 path=None,
1269 path=None,
1270 startDate=None,
1270 startDate=None,
1271 endDate=None,
1271 endDate=None,
1272 startTime=datetime.time(0, 0, 0),
1272 startTime=datetime.time(0, 0, 0),
1273 endTime=datetime.time(23, 59, 59),
1273 endTime=datetime.time(23, 59, 59),
1274 set=None,
1274 set=None,
1275 expLabel="",
1275 expLabel="",
1276 ext=None,
1276 ext=None,
1277 online=False,
1277 online=False,
1278 delay=60,
1278 delay=60,
1279 walk=True,
1279 walk=True,
1280 getblock=False,
1280 getblock=False,
1281 nTxs=1,
1281 nTxs=1,
1282 realtime=False,
1282 realtime=False,
1283 blocksize=None,
1283 blocksize=None,
1284 blocktime=None,
1284 blocktime=None,
1285 skip=None,
1285 skip=None,
1286 cursor=None,
1286 cursor=None,
1287 warnings=True,
1287 warnings=True,
1288 verbose=True,
1288 verbose=True,
1289 server=None,
1289 server=None,
1290 format=None,
1290 format=None,
1291 oneDDict=None,
1291 oneDDict=None,
1292 twoDDict=None,
1292 twoDDict=None,
1293 ind2DList=None):
1293 ind2DList=None):
1294 if server is not None:
1294 if server is not None:
1295 if 'tcp://' in server:
1295 if 'tcp://' in server:
1296 address = server
1296 address = server
1297 else:
1297 else:
1298 address = 'ipc:///tmp/%s' % server
1298 address = 'ipc:///tmp/%s' % server
1299 self.server = address
1299 self.server = address
1300 self.context = zmq.Context()
1300 self.context = zmq.Context()
1301 self.receiver = self.context.socket(zmq.PULL)
1301 self.receiver = self.context.socket(zmq.PULL)
1302 self.receiver.connect(self.server)
1302 self.receiver.connect(self.server)
1303 time.sleep(0.5)
1303 time.sleep(0.5)
1304 print('[Starting] ReceiverData from {}'.format(self.server))
1304 print('[Starting] ReceiverData from {}'.format(self.server))
1305 else:
1305 else:
1306 self.server = None
1306 self.server = None
1307 if path == None:
1307 if path == None:
1308 raise ValueError("[Reading] The path is not valid")
1308 raise ValueError("[Reading] The path is not valid")
1309
1309
1310 if ext == None:
1310 if ext == None:
1311 ext = self.ext
1311 ext = self.ext
1312
1312
1313 if online:
1313 if online:
1314 print("[Reading] Searching files in online mode...")
1314 print("[Reading] Searching files in online mode...")
1315
1315
1316 for nTries in range(self.nTries):
1316 for nTries in range(self.nTries):
1317 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(
1317 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(
1318 path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1318 path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1319
1319
1320 if fullpath:
1320 if fullpath:
1321 break
1321 break
1322
1322
1323 print('[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (delay, path, nTries + 1))
1323 print('[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries + 1))
1324 sleep(delay)
1324 sleep(self.delay)
1325
1325
1326 if not(fullpath):
1326 if not(fullpath):
1327 raise schainpy.admin.SchainWarning('There isn\'t any valid file in {}'.format(path))
1327 raise(schainpy.admin.SchainWarning('There isn\'t any valid file in {}'.format(path)))
1328 return
1328 return
1329
1329
1330 self.year = year
1330 self.year = year
1331 self.doy = doy
1331 self.doy = doy
1332 self.set = set - 1
1332 self.set = set - 1
1333 self.path = path
1333 self.path = path
1334 self.foldercounter = foldercounter
1334 self.foldercounter = foldercounter
1335 last_set = None
1335 last_set = None
1336 else:
1336 else:
1337 print("[Reading] Searching files in offline mode ...")
1337 print("[Reading] Searching files in offline mode ...")
1338 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1338 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1339 startTime=startTime, endTime=endTime,
1339 startTime=startTime, endTime=endTime,
1340 set=set, expLabel=expLabel, ext=ext,
1340 set=set, expLabel=expLabel, ext=ext,
1341 walk=walk, cursor=cursor,
1341 walk=walk, cursor=cursor,
1342 skip=skip)
1342 skip=skip)
1343
1343
1344 if not(pathList):
1344 if not(pathList):
1345 self.fileIndex = -1
1345 self.fileIndex = -1
1346 self.pathList = []
1346 self.pathList = []
1347 self.filenameList = []
1347 self.filenameList = []
1348 return
1348 return
1349
1349
1350 self.fileIndex = -1
1350 self.fileIndex = -1
1351 self.pathList = pathList
1351 self.pathList = pathList
1352 self.filenameList = filenameList
1352 self.filenameList = filenameList
1353 file_name = os.path.basename(filenameList[-1])
1353 file_name = os.path.basename(filenameList[-1])
1354 basename, ext = os.path.splitext(file_name)
1354 basename, ext = os.path.splitext(file_name)
1355 last_set = int(basename[-3:])
1355 last_set = int(basename[-3:])
1356
1356
1357 self.online = online
1357 self.online = online
1358 self.realtime = realtime
1358 self.realtime = realtime
1359 self.delay = delay
1359 self.delay = delay
1360 ext = ext.lower()
1360 ext = ext.lower()
1361 self.ext = ext
1361 self.ext = ext
1362 self.getByBlock = getblock
1362 self.getByBlock = getblock
1363 self.nTxs = nTxs
1363 self.nTxs = nTxs
1364 self.startTime = startTime
1364 self.startTime = startTime
1365 self.endTime = endTime
1365 self.endTime = endTime
1366 self.endDate = endDate
1366 self.endDate = endDate
1367 self.startDate = startDate
1367 self.startDate = startDate
1368 # Added-----------------
1368 # Added-----------------
1369 self.selBlocksize = blocksize
1369 self.selBlocksize = blocksize
1370 self.selBlocktime = blocktime
1370 self.selBlocktime = blocktime
1371
1371
1372 # Verbose-----------
1372 # Verbose-----------
1373 self.verbose = verbose
1373 self.verbose = verbose
1374 self.warnings = warnings
1374 self.warnings = warnings
1375
1375
1376 if not(self.setNextFile()):
1376 if not(self.setNextFile()):
1377 if (startDate != None) and (endDate != None):
1377 if (startDate != None) and (endDate != None):
1378 print("[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime()))
1378 print("[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime()))
1379 elif startDate != None:
1379 elif startDate != None:
1380 print("[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime()))
1380 print("[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime()))
1381 else:
1381 else:
1382 print("[Reading] No files")
1382 print("[Reading] No files")
1383
1383
1384 self.fileIndex = -1
1384 self.fileIndex = -1
1385 self.pathList = []
1385 self.pathList = []
1386 self.filenameList = []
1386 self.filenameList = []
1387 return
1387 return
1388
1388
1389 # self.getBasicHeader()
1389 # self.getBasicHeader()
1390
1390
1391 if last_set != None:
1391 if last_set != None:
1392 self.dataOut.last_block = last_set * \
1392 self.dataOut.last_block = last_set * \
1393 self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1393 self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1394 return
1394 return
1395
1395
1396 def getBasicHeader(self):
1396 def getBasicHeader(self):
1397
1397
1398 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1398 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1399 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1399 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1400
1400
1401 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1401 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1402
1402
1403 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1403 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1404
1404
1405 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1405 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1406
1406
1407 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1407 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1408
1408
1409 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1409 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1410
1410
1411 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1411 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1412
1412
1413 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1413 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1414
1414
1415 def getFirstHeader(self):
1415 def getFirstHeader(self):
1416
1416
1417 raise NotImplementedError
1417 raise NotImplementedError
1418
1418
1419 def getData(self):
1419 def getData(self):
1420
1420
1421 raise NotImplementedError
1421 raise NotImplementedError
1422
1422
1423 def hasNotDataInBuffer(self):
1423 def hasNotDataInBuffer(self):
1424
1424
1425 raise NotImplementedError
1425 raise NotImplementedError
1426
1426
1427 def readBlock(self):
1427 def readBlock(self):
1428
1428
1429 raise NotImplementedError
1429 raise NotImplementedError
1430
1430
1431 def isEndProcess(self):
1431 def isEndProcess(self):
1432
1432
1433 return self.flagNoMoreFiles
1433 return self.flagNoMoreFiles
1434
1434
1435 def printReadBlocks(self):
1435 def printReadBlocks(self):
1436
1436
1437 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1437 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1438
1438
1439 def printTotalBlocks(self):
1439 def printTotalBlocks(self):
1440
1440
1441 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1441 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1442
1442
1443 def printNumberOfBlock(self):
1443 def printNumberOfBlock(self):
1444 'SPAM!'
1444 'SPAM!'
1445
1445
1446 # if self.flagIsNewBlock:
1446 # if self.flagIsNewBlock:
1447 # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1447 # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1448 # self.processingHeaderObj.dataBlocksPerFile,
1448 # self.processingHeaderObj.dataBlocksPerFile,
1449 # self.dataOut.datatime.ctime())
1449 # self.dataOut.datatime.ctime())
1450
1450
1451 def printInfo(self):
1451 def printInfo(self):
1452
1452
1453 if self.__printInfo == False:
1453 if self.__printInfo == False:
1454 return
1454 return
1455
1455
1456 self.basicHeaderObj.printInfo()
1456 self.basicHeaderObj.printInfo()
1457 self.systemHeaderObj.printInfo()
1457 self.systemHeaderObj.printInfo()
1458 self.radarControllerHeaderObj.printInfo()
1458 self.radarControllerHeaderObj.printInfo()
1459 self.processingHeaderObj.printInfo()
1459 self.processingHeaderObj.printInfo()
1460
1460
1461 self.__printInfo = False
1461 self.__printInfo = False
1462
1462
1463 def run(self,
1463 def run(self,
1464 path=None,
1464 path=None,
1465 startDate=None,
1465 startDate=None,
1466 endDate=None,
1466 endDate=None,
1467 startTime=datetime.time(0, 0, 0),
1467 startTime=datetime.time(0, 0, 0),
1468 endTime=datetime.time(23, 59, 59),
1468 endTime=datetime.time(23, 59, 59),
1469 set=None,
1469 set=None,
1470 expLabel="",
1470 expLabel="",
1471 ext=None,
1471 ext=None,
1472 online=False,
1472 online=False,
1473 delay=60,
1473 delay=60,
1474 walk=True,
1474 walk=True,
1475 getblock=False,
1475 getblock=False,
1476 nTxs=1,
1476 nTxs=1,
1477 realtime=False,
1477 realtime=False,
1478 blocksize=None,
1478 blocksize=None,
1479 blocktime=None,
1479 blocktime=None,
1480 skip=None,
1480 skip=None,
1481 cursor=None,
1481 cursor=None,
1482 warnings=True,
1482 warnings=True,
1483 server=None,
1483 server=None,
1484 verbose=True,
1484 verbose=True,
1485 format=None,
1485 format=None,
1486 oneDDict=None,
1486 oneDDict=None,
1487 twoDDict=None,
1487 twoDDict=None,
1488 ind2DList=None, **kwargs):
1488 ind2DList=None, **kwargs):
1489
1489
1490 if not(self.isConfig):
1490 if not(self.isConfig):
1491 self.setup(path=path,
1491 self.setup(path=path,
1492 startDate=startDate,
1492 startDate=startDate,
1493 endDate=endDate,
1493 endDate=endDate,
1494 startTime=startTime,
1494 startTime=startTime,
1495 endTime=endTime,
1495 endTime=endTime,
1496 set=set,
1496 set=set,
1497 expLabel=expLabel,
1497 expLabel=expLabel,
1498 ext=ext,
1498 ext=ext,
1499 online=online,
1499 online=online,
1500 delay=delay,
1500 delay=delay,
1501 walk=walk,
1501 walk=walk,
1502 getblock=getblock,
1502 getblock=getblock,
1503 nTxs=nTxs,
1503 nTxs=nTxs,
1504 realtime=realtime,
1504 realtime=realtime,
1505 blocksize=blocksize,
1505 blocksize=blocksize,
1506 blocktime=blocktime,
1506 blocktime=blocktime,
1507 skip=skip,
1507 skip=skip,
1508 cursor=cursor,
1508 cursor=cursor,
1509 warnings=warnings,
1509 warnings=warnings,
1510 server=server,
1510 server=server,
1511 verbose=verbose,
1511 verbose=verbose,
1512 format=format,
1512 format=format,
1513 oneDDict=oneDDict,
1513 oneDDict=oneDDict,
1514 twoDDict=twoDDict,
1514 twoDDict=twoDDict,
1515 ind2DList=ind2DList)
1515 ind2DList=ind2DList)
1516 self.isConfig = True
1516 self.isConfig = True
1517 if server is None:
1517 if server is None:
1518 self.getData()
1518 self.getData()
1519 else:
1519 else:
1520 self.getFromServer()
1520 self.getFromServer()
1521
1521
1522
1522
1523 class JRODataWriter(JRODataIO):
1523 class JRODataWriter(JRODataIO):
1524
1524
1525 """
1525 """
1526 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1526 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1527 de los datos siempre se realiza por bloques.
1527 de los datos siempre se realiza por bloques.
1528 """
1528 """
1529
1529
1530 blockIndex = 0
1530 blockIndex = 0
1531
1531
1532 path = None
1532 path = None
1533
1533
1534 setFile = None
1534 setFile = None
1535
1535
1536 profilesPerBlock = None
1536 profilesPerBlock = None
1537
1537
1538 blocksPerFile = None
1538 blocksPerFile = None
1539
1539
1540 nWriteBlocks = 0
1540 nWriteBlocks = 0
1541
1541
1542 fileDate = None
1542 fileDate = None
1543
1543
1544 def __init__(self, dataOut=None):
1544 def __init__(self, dataOut=None):
1545 raise NotImplementedError
1545 raise NotImplementedError
1546
1546
1547 def hasAllDataInBuffer(self):
1547 def hasAllDataInBuffer(self):
1548 raise NotImplementedError
1548 raise NotImplementedError
1549
1549
1550 def setBlockDimension(self):
1550 def setBlockDimension(self):
1551 raise NotImplementedError
1551 raise NotImplementedError
1552
1552
1553 def writeBlock(self):
1553 def writeBlock(self):
1554 raise NotImplementedError
1554 raise NotImplementedError
1555
1555
1556 def putData(self):
1556 def putData(self):
1557 raise NotImplementedError
1557 raise NotImplementedError
1558
1558
1559 def getProcessFlags(self):
1559 def getProcessFlags(self):
1560
1560
1561 processFlags = 0
1561 processFlags = 0
1562
1562
1563 dtype_index = get_dtype_index(self.dtype)
1563 dtype_index = get_dtype_index(self.dtype)
1564 procflag_dtype = get_procflag_dtype(dtype_index)
1564 procflag_dtype = get_procflag_dtype(dtype_index)
1565
1565
1566 processFlags += procflag_dtype
1566 processFlags += procflag_dtype
1567
1567
1568 if self.dataOut.flagDecodeData:
1568 if self.dataOut.flagDecodeData:
1569 processFlags += PROCFLAG.DECODE_DATA
1569 processFlags += PROCFLAG.DECODE_DATA
1570
1570
1571 if self.dataOut.flagDeflipData:
1571 if self.dataOut.flagDeflipData:
1572 processFlags += PROCFLAG.DEFLIP_DATA
1572 processFlags += PROCFLAG.DEFLIP_DATA
1573
1573
1574 if self.dataOut.code is not None:
1574 if self.dataOut.code is not None:
1575 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1575 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1576
1576
1577 if self.dataOut.nCohInt > 1:
1577 if self.dataOut.nCohInt > 1:
1578 processFlags += PROCFLAG.COHERENT_INTEGRATION
1578 processFlags += PROCFLAG.COHERENT_INTEGRATION
1579
1579
1580 if self.dataOut.type == "Spectra":
1580 if self.dataOut.type == "Spectra":
1581 if self.dataOut.nIncohInt > 1:
1581 if self.dataOut.nIncohInt > 1:
1582 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1582 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1583
1583
1584 if self.dataOut.data_dc is not None:
1584 if self.dataOut.data_dc is not None:
1585 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1585 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1586
1586
1587 if self.dataOut.flagShiftFFT:
1587 if self.dataOut.flagShiftFFT:
1588 processFlags += PROCFLAG.SHIFT_FFT_DATA
1588 processFlags += PROCFLAG.SHIFT_FFT_DATA
1589
1589
1590 return processFlags
1590 return processFlags
1591
1591
1592 def setBasicHeader(self):
1592 def setBasicHeader(self):
1593
1593
1594 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1594 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1595 self.basicHeaderObj.version = self.versionFile
1595 self.basicHeaderObj.version = self.versionFile
1596 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1596 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1597
1597
1598 utc = numpy.floor(self.dataOut.utctime)
1598 utc = numpy.floor(self.dataOut.utctime)
1599 milisecond = (self.dataOut.utctime - utc) * 1000.0
1599 milisecond = (self.dataOut.utctime - utc) * 1000.0
1600
1600
1601 self.basicHeaderObj.utc = utc
1601 self.basicHeaderObj.utc = utc
1602 self.basicHeaderObj.miliSecond = milisecond
1602 self.basicHeaderObj.miliSecond = milisecond
1603 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1603 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1604 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1604 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1605 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1605 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1606
1606
1607 def setFirstHeader(self):
1607 def setFirstHeader(self):
1608 """
1608 """
1609 Obtiene una copia del First Header
1609 Obtiene una copia del First Header
1610
1610
1611 Affected:
1611 Affected:
1612
1612
1613 self.basicHeaderObj
1613 self.basicHeaderObj
1614 self.systemHeaderObj
1614 self.systemHeaderObj
1615 self.radarControllerHeaderObj
1615 self.radarControllerHeaderObj
1616 self.processingHeaderObj self.
1616 self.processingHeaderObj self.
1617
1617
1618 Return:
1618 Return:
1619 None
1619 None
1620 """
1620 """
1621
1621
1622 raise NotImplementedError
1622 raise NotImplementedError
1623
1623
1624 def __writeFirstHeader(self):
1624 def __writeFirstHeader(self):
1625 """
1625 """
1626 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1626 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1627
1627
1628 Affected:
1628 Affected:
1629 __dataType
1629 __dataType
1630
1630
1631 Return:
1631 Return:
1632 None
1632 None
1633 """
1633 """
1634
1634
1635 # CALCULAR PARAMETROS
1635 # CALCULAR PARAMETROS
1636
1636
1637 sizeLongHeader = self.systemHeaderObj.size + \
1637 sizeLongHeader = self.systemHeaderObj.size + \
1638 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1638 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1639 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1639 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1640
1640
1641 self.basicHeaderObj.write(self.fp)
1641 self.basicHeaderObj.write(self.fp)
1642 self.systemHeaderObj.write(self.fp)
1642 self.systemHeaderObj.write(self.fp)
1643 self.radarControllerHeaderObj.write(self.fp)
1643 self.radarControllerHeaderObj.write(self.fp)
1644 self.processingHeaderObj.write(self.fp)
1644 self.processingHeaderObj.write(self.fp)
1645
1645
1646 def __setNewBlock(self):
1646 def __setNewBlock(self):
1647 """
1647 """
1648 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1648 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1649
1649
1650 Return:
1650 Return:
1651 0 : si no pudo escribir nada
1651 0 : si no pudo escribir nada
1652 1 : Si escribio el Basic el First Header
1652 1 : Si escribio el Basic el First Header
1653 """
1653 """
1654 if self.fp == None:
1654 if self.fp == None:
1655 self.setNextFile()
1655 self.setNextFile()
1656
1656
1657 if self.flagIsNewFile:
1657 if self.flagIsNewFile:
1658 return 1
1658 return 1
1659
1659
1660 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1660 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1661 self.basicHeaderObj.write(self.fp)
1661 self.basicHeaderObj.write(self.fp)
1662 return 1
1662 return 1
1663
1663
1664 if not(self.setNextFile()):
1664 if not(self.setNextFile()):
1665 return 0
1665 return 0
1666
1666
1667 return 1
1667 return 1
1668
1668
1669 def writeNextBlock(self):
1669 def writeNextBlock(self):
1670 """
1670 """
1671 Selecciona el bloque siguiente de datos y los escribe en un file
1671 Selecciona el bloque siguiente de datos y los escribe en un file
1672
1672
1673 Return:
1673 Return:
1674 0 : Si no hizo pudo escribir el bloque de datos
1674 0 : Si no hizo pudo escribir el bloque de datos
1675 1 : Si no pudo escribir el bloque de datos
1675 1 : Si no pudo escribir el bloque de datos
1676 """
1676 """
1677 if not(self.__setNewBlock()):
1677 if not(self.__setNewBlock()):
1678 return 0
1678 return 0
1679
1679
1680 self.writeBlock()
1680 self.writeBlock()
1681
1681
1682 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1682 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1683 self.processingHeaderObj.dataBlocksPerFile))
1683 self.processingHeaderObj.dataBlocksPerFile))
1684
1684
1685 return 1
1685 return 1
1686
1686
1687 def setNextFile(self):
1687 def setNextFile(self):
1688 """
1688 """
1689 Determina el siguiente file que sera escrito
1689 Determina el siguiente file que sera escrito
1690
1690
1691 Affected:
1691 Affected:
1692 self.filename
1692 self.filename
1693 self.subfolder
1693 self.subfolder
1694 self.fp
1694 self.fp
1695 self.setFile
1695 self.setFile
1696 self.flagIsNewFile
1696 self.flagIsNewFile
1697
1697
1698 Return:
1698 Return:
1699 0 : Si el archivo no puede ser escrito
1699 0 : Si el archivo no puede ser escrito
1700 1 : Si el archivo esta listo para ser escrito
1700 1 : Si el archivo esta listo para ser escrito
1701 """
1701 """
1702 ext = self.ext
1702 ext = self.ext
1703 path = self.path
1703 path = self.path
1704
1704
1705 if self.fp != None:
1705 if self.fp != None:
1706 self.fp.close()
1706 self.fp.close()
1707
1707
1708 timeTuple = time.localtime(self.dataOut.utctime)
1708 timeTuple = time.localtime(self.dataOut.utctime)
1709 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1709 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1710
1710
1711 fullpath = os.path.join(path, subfolder)
1711 fullpath = os.path.join(path, subfolder)
1712 setFile = self.setFile
1712 setFile = self.setFile
1713
1713
1714 if not(os.path.exists(fullpath)):
1714 if not(os.path.exists(fullpath)):
1715 os.mkdir(fullpath)
1715 os.mkdir(fullpath)
1716 setFile = -1 # inicializo mi contador de seteo
1716 setFile = -1 # inicializo mi contador de seteo
1717 else:
1717 else:
1718 filesList = os.listdir(fullpath)
1718 filesList = os.listdir(fullpath)
1719 if len(filesList) > 0:
1719 if len(filesList) > 0:
1720 filesList = sorted(filesList, key=str.lower)
1720 filesList = sorted(filesList, key=str.lower)
1721 filen = filesList[-1]
1721 filen = filesList[-1]
1722 # el filename debera tener el siguiente formato
1722 # el filename debera tener el siguiente formato
1723 # 0 1234 567 89A BCDE (hex)
1723 # 0 1234 567 89A BCDE (hex)
1724 # x YYYY DDD SSS .ext
1724 # x YYYY DDD SSS .ext
1725 if isNumber(filen[8:11]):
1725 if isNumber(filen[8:11]):
1726 # inicializo mi contador de seteo al seteo del ultimo file
1726 # inicializo mi contador de seteo al seteo del ultimo file
1727 setFile = int(filen[8:11])
1727 setFile = int(filen[8:11])
1728 else:
1728 else:
1729 setFile = -1
1729 setFile = -1
1730 else:
1730 else:
1731 setFile = -1 # inicializo mi contador de seteo
1731 setFile = -1 # inicializo mi contador de seteo
1732
1732
1733 setFile += 1
1733 setFile += 1
1734
1734
1735 # If this is a new day it resets some values
1735 # If this is a new day it resets some values
1736 if self.dataOut.datatime.date() > self.fileDate:
1736 if self.dataOut.datatime.date() > self.fileDate:
1737 setFile = 0
1737 setFile = 0
1738 self.nTotalBlocks = 0
1738 self.nTotalBlocks = 0
1739
1739
1740 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1740 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1741 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1741 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1742
1742
1743 filename = os.path.join(path, subfolder, filen)
1743 filename = os.path.join(path, subfolder, filen)
1744
1744
1745 fp = open(filename, 'wb')
1745 fp = open(filename, 'wb')
1746
1746
1747 self.blockIndex = 0
1747 self.blockIndex = 0
1748
1748
1749 # guardando atributos
1749 # guardando atributos
1750 self.filename = filename
1750 self.filename = filename
1751 self.subfolder = subfolder
1751 self.subfolder = subfolder
1752 self.fp = fp
1752 self.fp = fp
1753 self.setFile = setFile
1753 self.setFile = setFile
1754 self.flagIsNewFile = 1
1754 self.flagIsNewFile = 1
1755 self.fileDate = self.dataOut.datatime.date()
1755 self.fileDate = self.dataOut.datatime.date()
1756
1756
1757 self.setFirstHeader()
1757 self.setFirstHeader()
1758
1758
1759 print('[Writing] Opening file: %s' % self.filename)
1759 print('[Writing] Opening file: %s' % self.filename)
1760
1760
1761 self.__writeFirstHeader()
1761 self.__writeFirstHeader()
1762
1762
1763 return 1
1763 return 1
1764
1764
1765 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1765 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1766 """
1766 """
1767 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1767 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1768
1768
1769 Inputs:
1769 Inputs:
1770 path : directory where data will be saved
1770 path : directory where data will be saved
1771 profilesPerBlock : number of profiles per block
1771 profilesPerBlock : number of profiles per block
1772 set : initial file set
1772 set : initial file set
1773 datatype : An integer number that defines data type:
1773 datatype : An integer number that defines data type:
1774 0 : int8 (1 byte)
1774 0 : int8 (1 byte)
1775 1 : int16 (2 bytes)
1775 1 : int16 (2 bytes)
1776 2 : int32 (4 bytes)
1776 2 : int32 (4 bytes)
1777 3 : int64 (8 bytes)
1777 3 : int64 (8 bytes)
1778 4 : float32 (4 bytes)
1778 4 : float32 (4 bytes)
1779 5 : double64 (8 bytes)
1779 5 : double64 (8 bytes)
1780
1780
1781 Return:
1781 Return:
1782 0 : Si no realizo un buen seteo
1782 0 : Si no realizo un buen seteo
1783 1 : Si realizo un buen seteo
1783 1 : Si realizo un buen seteo
1784 """
1784 """
1785
1785
1786 if ext == None:
1786 if ext == None:
1787 ext = self.ext
1787 ext = self.ext
1788
1788
1789 self.ext = ext.lower()
1789 self.ext = ext.lower()
1790
1790
1791 self.path = path
1791 self.path = path
1792
1792
1793 if set is None:
1793 if set is None:
1794 self.setFile = -1
1794 self.setFile = -1
1795 else:
1795 else:
1796 self.setFile = set - 1
1796 self.setFile = set - 1
1797
1797
1798 self.blocksPerFile = blocksPerFile
1798 self.blocksPerFile = blocksPerFile
1799
1799
1800 self.profilesPerBlock = profilesPerBlock
1800 self.profilesPerBlock = profilesPerBlock
1801
1801
1802 self.dataOut = dataOut
1802 self.dataOut = dataOut
1803 self.fileDate = self.dataOut.datatime.date()
1803 self.fileDate = self.dataOut.datatime.date()
1804 # By default
1804 # By default
1805 self.dtype = self.dataOut.dtype
1805 self.dtype = self.dataOut.dtype
1806
1806
1807 if datatype is not None:
1807 if datatype is not None:
1808 self.dtype = get_numpy_dtype(datatype)
1808 self.dtype = get_numpy_dtype(datatype)
1809
1809
1810 if not(self.setNextFile()):
1810 if not(self.setNextFile()):
1811 print("[Writing] There isn't a next file")
1811 print("[Writing] There isn't a next file")
1812 return 0
1812 return 0
1813
1813
1814 self.setBlockDimension()
1814 self.setBlockDimension()
1815
1815
1816 return 1
1816 return 1
1817
1817
1818 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1818 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1819
1819
1820 if not(self.isConfig):
1820 if not(self.isConfig):
1821
1821
1822 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1822 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1823 set=set, ext=ext, datatype=datatype, **kwargs)
1823 set=set, ext=ext, datatype=datatype, **kwargs)
1824 self.isConfig = True
1824 self.isConfig = True
1825
1825
1826 self.putData() No newline at end of file
1826 self.putData()
@@ -1,679 +1,680
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import numpy
6 import numpy
7
7
8 from schainpy.model.io.jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
8 from schainpy.model.io.jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
10 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
10 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
11 from schainpy.model.data.jrodata import Spectra
11 from schainpy.model.data.jrodata import Spectra
12
12
13 @MPDecorator
13 class SpectraReader(JRODataReader, ProcessingUnit):
14 class SpectraReader(JRODataReader, ProcessingUnit):
14 """
15 """
15 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
16 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
16 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
17 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
17 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
18 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
18
19
19 paresCanalesIguales * alturas * perfiles (Self Spectra)
20 paresCanalesIguales * alturas * perfiles (Self Spectra)
20 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
21 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
21 canales * alturas (DC Channels)
22 canales * alturas (DC Channels)
22
23
23 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
24 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
24 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
25 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
25 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
26 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
26 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
27 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
27
28
28 Example:
29 Example:
29 dpath = "/home/myuser/data"
30 dpath = "/home/myuser/data"
30
31
31 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
32 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
32
33
33 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
34 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
34
35
35 readerObj = SpectraReader()
36 readerObj = SpectraReader()
36
37
37 readerObj.setup(dpath, startTime, endTime)
38 readerObj.setup(dpath, startTime, endTime)
38
39
39 while(True):
40 while(True):
40
41
41 readerObj.getData()
42 readerObj.getData()
42
43
43 print readerObj.data_spc
44 print readerObj.data_spc
44
45
45 print readerObj.data_cspc
46 print readerObj.data_cspc
46
47
47 print readerObj.data_dc
48 print readerObj.data_dc
48
49
49 if readerObj.flagNoMoreFiles:
50 if readerObj.flagNoMoreFiles:
50 break
51 break
51
52
52 """
53 """
53
54
54 pts2read_SelfSpectra = 0
55 pts2read_SelfSpectra = 0
55
56
56 pts2read_CrossSpectra = 0
57 pts2read_CrossSpectra = 0
57
58
58 pts2read_DCchannels = 0
59 pts2read_DCchannels = 0
59
60
60 ext = ".pdata"
61 ext = ".pdata"
61
62
62 optchar = "P"
63 optchar = "P"
63
64
64 dataOut = None
65 dataOut = None
65
66
66 nRdChannels = None
67 nRdChannels = None
67
68
68 nRdPairs = None
69 nRdPairs = None
69
70
70 rdPairList = []
71 rdPairList = []
71
72
72 def __init__(self, **kwargs):
73 def __init__(self):#, **kwargs):
73 """
74 """
74 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
75 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
75
76
76 Inputs:
77 Inputs:
77 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
78 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
78 almacenar un perfil de datos cada vez que se haga un requerimiento
79 almacenar un perfil de datos cada vez que se haga un requerimiento
79 (getData). El perfil sera obtenido a partir del buffer de datos,
80 (getData). El perfil sera obtenido a partir del buffer de datos,
80 si el buffer esta vacio se hara un nuevo proceso de lectura de un
81 si el buffer esta vacio se hara un nuevo proceso de lectura de un
81 bloque de datos.
82 bloque de datos.
82 Si este parametro no es pasado se creara uno internamente.
83 Si este parametro no es pasado se creara uno internamente.
83
84
84 Affected:
85 Affected:
85 self.dataOut
86 self.dataOut
86
87
87 Return : None
88 Return : None
88 """
89 """
89
90
90 #Eliminar de la base la herencia
91 #Eliminar de la base la herencia
91 ProcessingUnit.__init__(self, **kwargs)
92 ProcessingUnit.__init__(self)#, **kwargs)
92
93
93 # self.isConfig = False
94 # self.isConfig = False
94
95
95 self.pts2read_SelfSpectra = 0
96 self.pts2read_SelfSpectra = 0
96
97
97 self.pts2read_CrossSpectra = 0
98 self.pts2read_CrossSpectra = 0
98
99
99 self.pts2read_DCchannels = 0
100 self.pts2read_DCchannels = 0
100
101
101 self.datablock = None
102 self.datablock = None
102
103
103 self.utc = None
104 self.utc = None
104
105
105 self.ext = ".pdata"
106 self.ext = ".pdata"
106
107
107 self.optchar = "P"
108 self.optchar = "P"
108
109
109 self.basicHeaderObj = BasicHeader(LOCALTIME)
110 self.basicHeaderObj = BasicHeader(LOCALTIME)
110
111
111 self.systemHeaderObj = SystemHeader()
112 self.systemHeaderObj = SystemHeader()
112
113
113 self.radarControllerHeaderObj = RadarControllerHeader()
114 self.radarControllerHeaderObj = RadarControllerHeader()
114
115
115 self.processingHeaderObj = ProcessingHeader()
116 self.processingHeaderObj = ProcessingHeader()
116
117
117 self.online = 0
118 self.online = 0
118
119
119 self.fp = None
120 self.fp = None
120
121
121 self.idFile = None
122 self.idFile = None
122
123
123 self.dtype = None
124 self.dtype = None
124
125
125 self.fileSizeByHeader = None
126 self.fileSizeByHeader = None
126
127
127 self.filenameList = []
128 self.filenameList = []
128
129
129 self.filename = None
130 self.filename = None
130
131
131 self.fileSize = None
132 self.fileSize = None
132
133
133 self.firstHeaderSize = 0
134 self.firstHeaderSize = 0
134
135
135 self.basicHeaderSize = 24
136 self.basicHeaderSize = 24
136
137
137 self.pathList = []
138 self.pathList = []
138
139
139 self.lastUTTime = 0
140 self.lastUTTime = 0
140
141
141 self.maxTimeStep = 30
142 self.maxTimeStep = 30
142
143
143 self.flagNoMoreFiles = 0
144 self.flagNoMoreFiles = 0
144
145
145 self.set = 0
146 self.set = 0
146
147
147 self.path = None
148 self.path = None
148
149
149 self.delay = 60 #seconds
150 self.delay = 60 #seconds
150
151
151 self.nTries = 3 #quantity tries
152 self.nTries = 3 #quantity tries
152
153
153 self.nFiles = 3 #number of files for searching
154 self.nFiles = 3 #number of files for searching
154
155
155 self.nReadBlocks = 0
156 self.nReadBlocks = 0
156
157
157 self.flagIsNewFile = 1
158 self.flagIsNewFile = 1
158
159
159 self.__isFirstTimeOnline = 1
160 self.__isFirstTimeOnline = 1
160
161
161 # self.ippSeconds = 0
162 # self.ippSeconds = 0
162
163
163 self.flagDiscontinuousBlock = 0
164 self.flagDiscontinuousBlock = 0
164
165
165 self.flagIsNewBlock = 0
166 self.flagIsNewBlock = 0
166
167
167 self.nTotalBlocks = 0
168 self.nTotalBlocks = 0
168
169
169 self.blocksize = 0
170 self.blocksize = 0
170
171
171 self.dataOut = self.createObjByDefault()
172 self.dataOut = self.createObjByDefault()
172
173
173 self.profileIndex = 1 #Always
174 self.profileIndex = 1 #Always
174
175
175
176
176 def createObjByDefault(self):
177 def createObjByDefault(self):
177
178
178 dataObj = Spectra()
179 dataObj = Spectra()
179
180
180 return dataObj
181 return dataObj
181
182
182 def __hasNotDataInBuffer(self):
183 def __hasNotDataInBuffer(self):
183 return 1
184 return 1
184
185
185
186
186 def getBlockDimension(self):
187 def getBlockDimension(self):
187 """
188 """
188 Obtiene la cantidad de puntos a leer por cada bloque de datos
189 Obtiene la cantidad de puntos a leer por cada bloque de datos
189
190
190 Affected:
191 Affected:
191 self.nRdChannels
192 self.nRdChannels
192 self.nRdPairs
193 self.nRdPairs
193 self.pts2read_SelfSpectra
194 self.pts2read_SelfSpectra
194 self.pts2read_CrossSpectra
195 self.pts2read_CrossSpectra
195 self.pts2read_DCchannels
196 self.pts2read_DCchannels
196 self.blocksize
197 self.blocksize
197 self.dataOut.nChannels
198 self.dataOut.nChannels
198 self.dataOut.nPairs
199 self.dataOut.nPairs
199
200
200 Return:
201 Return:
201 None
202 None
202 """
203 """
203 self.nRdChannels = 0
204 self.nRdChannels = 0
204 self.nRdPairs = 0
205 self.nRdPairs = 0
205 self.rdPairList = []
206 self.rdPairList = []
206
207
207 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
208 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
208 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
209 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
209 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
210 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
210 else:
211 else:
211 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
212 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
212 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
213 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
213
214
214 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
215 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
215
216
216 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
217 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
217 self.blocksize = self.pts2read_SelfSpectra
218 self.blocksize = self.pts2read_SelfSpectra
218
219
219 if self.processingHeaderObj.flag_cspc:
220 if self.processingHeaderObj.flag_cspc:
220 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
221 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
221 self.blocksize += self.pts2read_CrossSpectra
222 self.blocksize += self.pts2read_CrossSpectra
222
223
223 if self.processingHeaderObj.flag_dc:
224 if self.processingHeaderObj.flag_dc:
224 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
225 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
225 self.blocksize += self.pts2read_DCchannels
226 self.blocksize += self.pts2read_DCchannels
226
227
227 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
228 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
228
229
229
230
230 def readBlock(self):
231 def readBlock(self):
231 """
232 """
232 Lee el bloque de datos desde la posicion actual del puntero del archivo
233 Lee el bloque de datos desde la posicion actual del puntero del archivo
233 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
234 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
234 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
235 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
235 es seteado a 0
236 es seteado a 0
236
237
237 Return: None
238 Return: None
238
239
239 Variables afectadas:
240 Variables afectadas:
240
241
241 self.flagIsNewFile
242 self.flagIsNewFile
242 self.flagIsNewBlock
243 self.flagIsNewBlock
243 self.nTotalBlocks
244 self.nTotalBlocks
244 self.data_spc
245 self.data_spc
245 self.data_cspc
246 self.data_cspc
246 self.data_dc
247 self.data_dc
247
248
248 Exceptions:
249 Exceptions:
249 Si un bloque leido no es un bloque valido
250 Si un bloque leido no es un bloque valido
250 """
251 """
251 blockOk_flag = False
252 blockOk_flag = False
252 fpointer = self.fp.tell()
253 fpointer = self.fp.tell()
253
254
254 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
255 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
255 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
256 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
256
257
257 if self.processingHeaderObj.flag_cspc:
258 if self.processingHeaderObj.flag_cspc:
258 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
259 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
259 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
260 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
260
261
261 if self.processingHeaderObj.flag_dc:
262 if self.processingHeaderObj.flag_dc:
262 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
263 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
263 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
264 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
264
265
265
266
266 if not self.processingHeaderObj.shif_fft:
267 if not self.processingHeaderObj.shif_fft:
267 #desplaza a la derecha en el eje 2 determinadas posiciones
268 #desplaza a la derecha en el eje 2 determinadas posiciones
268 shift = int(self.processingHeaderObj.profilesPerBlock/2)
269 shift = int(self.processingHeaderObj.profilesPerBlock/2)
269 spc = numpy.roll( spc, shift , axis=2 )
270 spc = numpy.roll( spc, shift , axis=2 )
270
271
271 if self.processingHeaderObj.flag_cspc:
272 if self.processingHeaderObj.flag_cspc:
272 #desplaza a la derecha en el eje 2 determinadas posiciones
273 #desplaza a la derecha en el eje 2 determinadas posiciones
273 cspc = numpy.roll( cspc, shift, axis=2 )
274 cspc = numpy.roll( cspc, shift, axis=2 )
274
275
275 #Dimensions : nChannels, nProfiles, nSamples
276 #Dimensions : nChannels, nProfiles, nSamples
276 spc = numpy.transpose( spc, (0,2,1) )
277 spc = numpy.transpose( spc, (0,2,1) )
277 self.data_spc = spc
278 self.data_spc = spc
278
279
279 if self.processingHeaderObj.flag_cspc:
280 if self.processingHeaderObj.flag_cspc:
280 cspc = numpy.transpose( cspc, (0,2,1) )
281 cspc = numpy.transpose( cspc, (0,2,1) )
281 self.data_cspc = cspc['real'] + cspc['imag']*1j
282 self.data_cspc = cspc['real'] + cspc['imag']*1j
282 else:
283 else:
283 self.data_cspc = None
284 self.data_cspc = None
284
285
285 if self.processingHeaderObj.flag_dc:
286 if self.processingHeaderObj.flag_dc:
286 self.data_dc = dc['real'] + dc['imag']*1j
287 self.data_dc = dc['real'] + dc['imag']*1j
287 else:
288 else:
288 self.data_dc = None
289 self.data_dc = None
289
290
290 self.flagIsNewFile = 0
291 self.flagIsNewFile = 0
291 self.flagIsNewBlock = 1
292 self.flagIsNewBlock = 1
292
293
293 self.nTotalBlocks += 1
294 self.nTotalBlocks += 1
294 self.nReadBlocks += 1
295 self.nReadBlocks += 1
295
296
296 return 1
297 return 1
297
298
298 def getFirstHeader(self):
299 def getFirstHeader(self):
299
300
300 self.getBasicHeader()
301 self.getBasicHeader()
301
302
302 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
303 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
303
304
304 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
305 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
305
306
306 # self.dataOut.ippSeconds = self.ippSeconds
307 # self.dataOut.ippSeconds = self.ippSeconds
307
308
308 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
309 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
309
310
310 self.dataOut.dtype = self.dtype
311 self.dataOut.dtype = self.dtype
311
312
312 # self.dataOut.nPairs = self.nPairs
313 # self.dataOut.nPairs = self.nPairs
313
314
314 self.dataOut.pairsList = self.rdPairList
315 self.dataOut.pairsList = self.rdPairList
315
316
316 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
317 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
317
318
318 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
319 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
319
320
320 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
321 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
321
322
322 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
323 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
323
324
324 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
325 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
325
326
326 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
327 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
327
328
328 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
329 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
329
330
330 self.dataOut.flagShiftFFT = True #Data is always shifted
331 self.dataOut.flagShiftFFT = True #Data is always shifted
331
332
332 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
333 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
333
334
334 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
335 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
335
336
336 def getData(self):
337 def getData(self):
337 """
338 """
338 First method to execute before "RUN" is called.
339 First method to execute before "RUN" is called.
339
340
340 Copia el buffer de lectura a la clase "Spectra",
341 Copia el buffer de lectura a la clase "Spectra",
341 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
342 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
342 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
343 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
343
344
344 Return:
345 Return:
345 0 : Si no hay mas archivos disponibles
346 0 : Si no hay mas archivos disponibles
346 1 : Si hizo una buena copia del buffer
347 1 : Si hizo una buena copia del buffer
347
348
348 Affected:
349 Affected:
349 self.dataOut
350 self.dataOut
350
351
351 self.flagDiscontinuousBlock
352 self.flagDiscontinuousBlock
352 self.flagIsNewBlock
353 self.flagIsNewBlock
353 """
354 """
354
355
355 if self.flagNoMoreFiles:
356 if self.flagNoMoreFiles:
356 self.dataOut.flagNoData = True
357 self.dataOut.flagNoData = True
357 print('Process finished')
358 print('Process finished')
358 return 0
359 return 0
359
360
360 self.flagDiscontinuousBlock = 0
361 self.flagDiscontinuousBlock = 0
361 self.flagIsNewBlock = 0
362 self.flagIsNewBlock = 0
362
363
363 if self.__hasNotDataInBuffer():
364 if self.__hasNotDataInBuffer():
364
365
365 if not( self.readNextBlock() ):
366 if not( self.readNextBlock() ):
366 self.dataOut.flagNoData = True
367 self.dataOut.flagNoData = True
367 return 0
368 return 0
368
369
369 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
370 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
370
371
371 if self.data_spc is None:
372 if self.data_spc is None:
372 self.dataOut.flagNoData = True
373 self.dataOut.flagNoData = True
373 return 0
374 return 0
374
375
375 self.getBasicHeader()
376 self.getBasicHeader()
376
377
377 self.getFirstHeader()
378 self.getFirstHeader()
378
379
379 self.dataOut.data_spc = self.data_spc
380 self.dataOut.data_spc = self.data_spc
380
381
381 self.dataOut.data_cspc = self.data_cspc
382 self.dataOut.data_cspc = self.data_cspc
382
383
383 self.dataOut.data_dc = self.data_dc
384 self.dataOut.data_dc = self.data_dc
384
385
385 self.dataOut.flagNoData = False
386 self.dataOut.flagNoData = False
386
387
387 self.dataOut.realtime = self.online
388 self.dataOut.realtime = self.online
388
389
389 return self.dataOut.data_spc
390 return self.dataOut.data_spc
390
391
391 class SpectraWriter(JRODataWriter, Operation):
392 class SpectraWriter(JRODataWriter, Operation):
392
393
393 """
394 """
394 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
395 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
395 de los datos siempre se realiza por bloques.
396 de los datos siempre se realiza por bloques.
396 """
397 """
397
398
398 ext = ".pdata"
399 ext = ".pdata"
399
400
400 optchar = "P"
401 optchar = "P"
401
402
402 shape_spc_Buffer = None
403 shape_spc_Buffer = None
403
404
404 shape_cspc_Buffer = None
405 shape_cspc_Buffer = None
405
406
406 shape_dc_Buffer = None
407 shape_dc_Buffer = None
407
408
408 data_spc = None
409 data_spc = None
409
410
410 data_cspc = None
411 data_cspc = None
411
412
412 data_dc = None
413 data_dc = None
413
414
414 # dataOut = None
415 # dataOut = None
415
416
416 def __init__(self, **kwargs):
417 def __init__(self, **kwargs):
417 """
418 """
418 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
419 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
419
420
420 Affected:
421 Affected:
421 self.dataOut
422 self.dataOut
422 self.basicHeaderObj
423 self.basicHeaderObj
423 self.systemHeaderObj
424 self.systemHeaderObj
424 self.radarControllerHeaderObj
425 self.radarControllerHeaderObj
425 self.processingHeaderObj
426 self.processingHeaderObj
426
427
427 Return: None
428 Return: None
428 """
429 """
429
430
430 Operation.__init__(self, **kwargs)
431 Operation.__init__(self, **kwargs)
431
432
432 self.isConfig = False
433 self.isConfig = False
433
434
434 self.nTotalBlocks = 0
435 self.nTotalBlocks = 0
435
436
436 self.data_spc = None
437 self.data_spc = None
437
438
438 self.data_cspc = None
439 self.data_cspc = None
439
440
440 self.data_dc = None
441 self.data_dc = None
441
442
442 self.fp = None
443 self.fp = None
443
444
444 self.flagIsNewFile = 1
445 self.flagIsNewFile = 1
445
446
446 self.nTotalBlocks = 0
447 self.nTotalBlocks = 0
447
448
448 self.flagIsNewBlock = 0
449 self.flagIsNewBlock = 0
449
450
450 self.setFile = None
451 self.setFile = None
451
452
452 self.dtype = None
453 self.dtype = None
453
454
454 self.path = None
455 self.path = None
455
456
456 self.noMoreFiles = 0
457 self.noMoreFiles = 0
457
458
458 self.filename = None
459 self.filename = None
459
460
460 self.basicHeaderObj = BasicHeader(LOCALTIME)
461 self.basicHeaderObj = BasicHeader(LOCALTIME)
461
462
462 self.systemHeaderObj = SystemHeader()
463 self.systemHeaderObj = SystemHeader()
463
464
464 self.radarControllerHeaderObj = RadarControllerHeader()
465 self.radarControllerHeaderObj = RadarControllerHeader()
465
466
466 self.processingHeaderObj = ProcessingHeader()
467 self.processingHeaderObj = ProcessingHeader()
467
468
468
469
469 def hasAllDataInBuffer(self):
470 def hasAllDataInBuffer(self):
470 return 1
471 return 1
471
472
472
473
473 def setBlockDimension(self):
474 def setBlockDimension(self):
474 """
475 """
475 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
476 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
476
477
477 Affected:
478 Affected:
478 self.shape_spc_Buffer
479 self.shape_spc_Buffer
479 self.shape_cspc_Buffer
480 self.shape_cspc_Buffer
480 self.shape_dc_Buffer
481 self.shape_dc_Buffer
481
482
482 Return: None
483 Return: None
483 """
484 """
484 self.shape_spc_Buffer = (self.dataOut.nChannels,
485 self.shape_spc_Buffer = (self.dataOut.nChannels,
485 self.processingHeaderObj.nHeights,
486 self.processingHeaderObj.nHeights,
486 self.processingHeaderObj.profilesPerBlock)
487 self.processingHeaderObj.profilesPerBlock)
487
488
488 self.shape_cspc_Buffer = (self.dataOut.nPairs,
489 self.shape_cspc_Buffer = (self.dataOut.nPairs,
489 self.processingHeaderObj.nHeights,
490 self.processingHeaderObj.nHeights,
490 self.processingHeaderObj.profilesPerBlock)
491 self.processingHeaderObj.profilesPerBlock)
491
492
492 self.shape_dc_Buffer = (self.dataOut.nChannels,
493 self.shape_dc_Buffer = (self.dataOut.nChannels,
493 self.processingHeaderObj.nHeights)
494 self.processingHeaderObj.nHeights)
494
495
495
496
496 def writeBlock(self):
497 def writeBlock(self):
497 """
498 """
498 Escribe el buffer en el file designado
499 Escribe el buffer en el file designado
499
500
500 Affected:
501 Affected:
501 self.data_spc
502 self.data_spc
502 self.data_cspc
503 self.data_cspc
503 self.data_dc
504 self.data_dc
504 self.flagIsNewFile
505 self.flagIsNewFile
505 self.flagIsNewBlock
506 self.flagIsNewBlock
506 self.nTotalBlocks
507 self.nTotalBlocks
507 self.nWriteBlocks
508 self.nWriteBlocks
508
509
509 Return: None
510 Return: None
510 """
511 """
511
512
512 spc = numpy.transpose( self.data_spc, (0,2,1) )
513 spc = numpy.transpose( self.data_spc, (0,2,1) )
513 if not self.processingHeaderObj.shif_fft:
514 if not self.processingHeaderObj.shif_fft:
514 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
515 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
515 data = spc.reshape((-1))
516 data = spc.reshape((-1))
516 data = data.astype(self.dtype[0])
517 data = data.astype(self.dtype[0])
517 data.tofile(self.fp)
518 data.tofile(self.fp)
518
519
519 if self.data_cspc is not None:
520 if self.data_cspc is not None:
520 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
521 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
521 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
522 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
522 if not self.processingHeaderObj.shif_fft:
523 if not self.processingHeaderObj.shif_fft:
523 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
524 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
524 data['real'] = cspc.real
525 data['real'] = cspc.real
525 data['imag'] = cspc.imag
526 data['imag'] = cspc.imag
526 data = data.reshape((-1))
527 data = data.reshape((-1))
527 data.tofile(self.fp)
528 data.tofile(self.fp)
528
529
529 if self.data_dc is not None:
530 if self.data_dc is not None:
530 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
531 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
531 dc = self.data_dc
532 dc = self.data_dc
532 data['real'] = dc.real
533 data['real'] = dc.real
533 data['imag'] = dc.imag
534 data['imag'] = dc.imag
534 data = data.reshape((-1))
535 data = data.reshape((-1))
535 data.tofile(self.fp)
536 data.tofile(self.fp)
536
537
537 # self.data_spc.fill(0)
538 # self.data_spc.fill(0)
538 #
539 #
539 # if self.data_dc is not None:
540 # if self.data_dc is not None:
540 # self.data_dc.fill(0)
541 # self.data_dc.fill(0)
541 #
542 #
542 # if self.data_cspc is not None:
543 # if self.data_cspc is not None:
543 # self.data_cspc.fill(0)
544 # self.data_cspc.fill(0)
544
545
545 self.flagIsNewFile = 0
546 self.flagIsNewFile = 0
546 self.flagIsNewBlock = 1
547 self.flagIsNewBlock = 1
547 self.nTotalBlocks += 1
548 self.nTotalBlocks += 1
548 self.nWriteBlocks += 1
549 self.nWriteBlocks += 1
549 self.blockIndex += 1
550 self.blockIndex += 1
550
551
551 # print "[Writing] Block = %d04" %self.blockIndex
552 # print "[Writing] Block = %d04" %self.blockIndex
552
553
553 def putData(self):
554 def putData(self):
554 """
555 """
555 Setea un bloque de datos y luego los escribe en un file
556 Setea un bloque de datos y luego los escribe en un file
556
557
557 Affected:
558 Affected:
558 self.data_spc
559 self.data_spc
559 self.data_cspc
560 self.data_cspc
560 self.data_dc
561 self.data_dc
561
562
562 Return:
563 Return:
563 0 : Si no hay data o no hay mas files que puedan escribirse
564 0 : Si no hay data o no hay mas files que puedan escribirse
564 1 : Si se escribio la data de un bloque en un file
565 1 : Si se escribio la data de un bloque en un file
565 """
566 """
566
567
567 if self.dataOut.flagNoData:
568 if self.dataOut.flagNoData:
568 return 0
569 return 0
569
570
570 self.flagIsNewBlock = 0
571 self.flagIsNewBlock = 0
571
572
572 if self.dataOut.flagDiscontinuousBlock:
573 if self.dataOut.flagDiscontinuousBlock:
573 self.data_spc.fill(0)
574 self.data_spc.fill(0)
574 if self.dataOut.data_cspc is not None:
575 if self.dataOut.data_cspc is not None:
575 self.data_cspc.fill(0)
576 self.data_cspc.fill(0)
576 if self.dataOut.data_dc is not None:
577 if self.dataOut.data_dc is not None:
577 self.data_dc.fill(0)
578 self.data_dc.fill(0)
578 self.setNextFile()
579 self.setNextFile()
579
580
580 if self.flagIsNewFile == 0:
581 if self.flagIsNewFile == 0:
581 self.setBasicHeader()
582 self.setBasicHeader()
582
583
583 self.data_spc = self.dataOut.data_spc.copy()
584 self.data_spc = self.dataOut.data_spc.copy()
584
585
585 if self.dataOut.data_cspc is not None:
586 if self.dataOut.data_cspc is not None:
586 self.data_cspc = self.dataOut.data_cspc.copy()
587 self.data_cspc = self.dataOut.data_cspc.copy()
587
588
588 if self.dataOut.data_dc is not None:
589 if self.dataOut.data_dc is not None:
589 self.data_dc = self.dataOut.data_dc.copy()
590 self.data_dc = self.dataOut.data_dc.copy()
590
591
591 # #self.processingHeaderObj.dataBlocksPerFile)
592 # #self.processingHeaderObj.dataBlocksPerFile)
592 if self.hasAllDataInBuffer():
593 if self.hasAllDataInBuffer():
593 # self.setFirstHeader()
594 # self.setFirstHeader()
594 self.writeNextBlock()
595 self.writeNextBlock()
595
596
596 return 1
597 return 1
597
598
598 def __getBlockSize(self):
599 def __getBlockSize(self):
599 '''
600 '''
600 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
601 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
601 '''
602 '''
602
603
603 dtype_width = self.getDtypeWidth()
604 dtype_width = self.getDtypeWidth()
604
605
605 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
606 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
606
607
607 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
608 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
608 blocksize = (pts2write_SelfSpectra*dtype_width)
609 blocksize = (pts2write_SelfSpectra*dtype_width)
609
610
610 if self.dataOut.data_cspc is not None:
611 if self.dataOut.data_cspc is not None:
611 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
612 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
612 blocksize += (pts2write_CrossSpectra*dtype_width*2)
613 blocksize += (pts2write_CrossSpectra*dtype_width*2)
613
614
614 if self.dataOut.data_dc is not None:
615 if self.dataOut.data_dc is not None:
615 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
616 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
616 blocksize += (pts2write_DCchannels*dtype_width*2)
617 blocksize += (pts2write_DCchannels*dtype_width*2)
617
618
618 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
619 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
619
620
620 return blocksize
621 return blocksize
621
622
622 def setFirstHeader(self):
623 def setFirstHeader(self):
623
624
624 """
625 """
625 Obtiene una copia del First Header
626 Obtiene una copia del First Header
626
627
627 Affected:
628 Affected:
628 self.systemHeaderObj
629 self.systemHeaderObj
629 self.radarControllerHeaderObj
630 self.radarControllerHeaderObj
630 self.dtype
631 self.dtype
631
632
632 Return:
633 Return:
633 None
634 None
634 """
635 """
635
636
636 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
637 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
637 self.systemHeaderObj.nChannels = self.dataOut.nChannels
638 self.systemHeaderObj.nChannels = self.dataOut.nChannels
638 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
639 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
639
640
640 self.processingHeaderObj.dtype = 1 # Spectra
641 self.processingHeaderObj.dtype = 1 # Spectra
641 self.processingHeaderObj.blockSize = self.__getBlockSize()
642 self.processingHeaderObj.blockSize = self.__getBlockSize()
642 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
643 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
643 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
644 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
644 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
645 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
645 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
646 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
646 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
647 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
647 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
648 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
648 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
649 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
649
650
650 if self.processingHeaderObj.totalSpectra > 0:
651 if self.processingHeaderObj.totalSpectra > 0:
651 channelList = []
652 channelList = []
652 for channel in range(self.dataOut.nChannels):
653 for channel in range(self.dataOut.nChannels):
653 channelList.append(channel)
654 channelList.append(channel)
654 channelList.append(channel)
655 channelList.append(channel)
655
656
656 pairsList = []
657 pairsList = []
657 if self.dataOut.nPairs > 0:
658 if self.dataOut.nPairs > 0:
658 for pair in self.dataOut.pairsList:
659 for pair in self.dataOut.pairsList:
659 pairsList.append(pair[0])
660 pairsList.append(pair[0])
660 pairsList.append(pair[1])
661 pairsList.append(pair[1])
661
662
662 spectraComb = channelList + pairsList
663 spectraComb = channelList + pairsList
663 spectraComb = numpy.array(spectraComb, dtype="u1")
664 spectraComb = numpy.array(spectraComb, dtype="u1")
664 self.processingHeaderObj.spectraComb = spectraComb
665 self.processingHeaderObj.spectraComb = spectraComb
665
666
666 if self.dataOut.code is not None:
667 if self.dataOut.code is not None:
667 self.processingHeaderObj.code = self.dataOut.code
668 self.processingHeaderObj.code = self.dataOut.code
668 self.processingHeaderObj.nCode = self.dataOut.nCode
669 self.processingHeaderObj.nCode = self.dataOut.nCode
669 self.processingHeaderObj.nBaud = self.dataOut.nBaud
670 self.processingHeaderObj.nBaud = self.dataOut.nBaud
670
671
671 if self.processingHeaderObj.nWindows != 0:
672 if self.processingHeaderObj.nWindows != 0:
672 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
673 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
673 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
674 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
674 self.processingHeaderObj.nHeights = self.dataOut.nHeights
675 self.processingHeaderObj.nHeights = self.dataOut.nHeights
675 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
676 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
676
677
677 self.processingHeaderObj.processFlags = self.getProcessFlags()
678 self.processingHeaderObj.processFlags = self.getProcessFlags()
678
679
679 self.setBasicHeader() No newline at end of file
680 self.setBasicHeader()
@@ -1,764 +1,765
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6
6
7 import numpy
7 import numpy
8
8
9 from .jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
9 from .jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
11 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
11 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
12 from schainpy.model.data.jrodata import Voltage
12 from schainpy.model.data.jrodata import Voltage
13 import zmq
13 import zmq
14 import tempfile
14 import tempfile
15 from io import StringIO
15 from io import StringIO
16 # from _sha import blocksize
16 # from _sha import blocksize
17
17
18
18 @MPDecorator
19 class VoltageReader(JRODataReader, ProcessingUnit):
19 class VoltageReader(JRODataReader, ProcessingUnit):
20 """
20 """
21 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
21 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
22 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
22 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
23 perfiles*alturas*canales) son almacenados en la variable "buffer".
23 perfiles*alturas*canales) son almacenados en la variable "buffer".
24
24
25 perfiles * alturas * canales
25 perfiles * alturas * canales
26
26
27 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
27 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
28 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
28 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
29 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
29 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
30 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
30 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
31
31
32 Example:
32 Example:
33
33
34 dpath = "/home/myuser/data"
34 dpath = "/home/myuser/data"
35
35
36 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
36 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
37
37
38 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
38 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
39
39
40 readerObj = VoltageReader()
40 readerObj = VoltageReader()
41
41
42 readerObj.setup(dpath, startTime, endTime)
42 readerObj.setup(dpath, startTime, endTime)
43
43
44 while(True):
44 while(True):
45
45
46 #to get one profile
46 #to get one profile
47 profile = readerObj.getData()
47 profile = readerObj.getData()
48
48
49 #print the profile
49 #print the profile
50 print profile
50 print profile
51
51
52 #If you want to see all datablock
52 #If you want to see all datablock
53 print readerObj.datablock
53 print readerObj.datablock
54
54
55 if readerObj.flagNoMoreFiles:
55 if readerObj.flagNoMoreFiles:
56 break
56 break
57
57
58 """
58 """
59
59
60 ext = ".r"
60 ext = ".r"
61
61
62 optchar = "D"
62 optchar = "D"
63 dataOut = None
63 dataOut = None
64
64
65 def __init__(self, **kwargs):
65 def __init__(self):#, **kwargs):
66 """
66 """
67 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
67 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
68
68
69 Input:
69 Input:
70 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
70 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
71 almacenar un perfil de datos cada vez que se haga un requerimiento
71 almacenar un perfil de datos cada vez que se haga un requerimiento
72 (getData). El perfil sera obtenido a partir del buffer de datos,
72 (getData). El perfil sera obtenido a partir del buffer de datos,
73 si el buffer esta vacio se hara un nuevo proceso de lectura de un
73 si el buffer esta vacio se hara un nuevo proceso de lectura de un
74 bloque de datos.
74 bloque de datos.
75 Si este parametro no es pasado se creara uno internamente.
75 Si este parametro no es pasado se creara uno internamente.
76
76
77 Variables afectadas:
77 Variables afectadas:
78 self.dataOut
78 self.dataOut
79
79
80 Return:
80 Return:
81 None
81 None
82 """
82 """
83
83
84 ProcessingUnit.__init__(self, **kwargs)
84 ProcessingUnit.__init__(self)#, **kwargs)
85
85
86 self.isConfig = False
86 self.isConfig = False
87
87
88 self.datablock = None
88 self.datablock = None
89
89
90 self.utc = 0
90 self.utc = 0
91
91
92 self.ext = ".r"
92 self.ext = ".r"
93
93
94 self.optchar = "D"
94 self.optchar = "D"
95
95
96 self.basicHeaderObj = BasicHeader(LOCALTIME)
96 self.basicHeaderObj = BasicHeader(LOCALTIME)
97
97
98 self.systemHeaderObj = SystemHeader()
98 self.systemHeaderObj = SystemHeader()
99
99
100 self.radarControllerHeaderObj = RadarControllerHeader()
100 self.radarControllerHeaderObj = RadarControllerHeader()
101
101
102 self.processingHeaderObj = ProcessingHeader()
102 self.processingHeaderObj = ProcessingHeader()
103
103
104 self.online = 0
104 self.online = 0
105
105
106 self.fp = None
106 self.fp = None
107
107
108 self.idFile = None
108 self.idFile = None
109
109
110 self.dtype = None
110 self.dtype = None
111
111
112 self.fileSizeByHeader = None
112 self.fileSizeByHeader = None
113
113
114 self.filenameList = []
114 self.filenameList = []
115
115
116 self.filename = None
116 self.filename = None
117
117
118 self.fileSize = None
118 self.fileSize = None
119
119
120 self.firstHeaderSize = 0
120 self.firstHeaderSize = 0
121
121
122 self.basicHeaderSize = 24
122 self.basicHeaderSize = 24
123
123
124 self.pathList = []
124 self.pathList = []
125
125
126 self.filenameList = []
126 self.filenameList = []
127
127
128 self.lastUTTime = 0
128 self.lastUTTime = 0
129
129
130 self.maxTimeStep = 30
130 self.maxTimeStep = 30
131
131
132 self.flagNoMoreFiles = 0
132 self.flagNoMoreFiles = 0
133
133
134 self.set = 0
134 self.set = 0
135
135
136 self.path = None
136 self.path = None
137
137
138 self.profileIndex = 2**32 - 1
138 self.profileIndex = 2**32 - 1
139
139
140 self.delay = 3 # seconds
140 self.delay = 3 # seconds
141
141
142 self.nTries = 3 # quantity tries
142 self.nTries = 3 # quantity tries
143
143
144 self.nFiles = 3 # number of files for searching
144 self.nFiles = 3 # number of files for searching
145
145
146 self.nReadBlocks = 0
146 self.nReadBlocks = 0
147
147
148 self.flagIsNewFile = 1
148 self.flagIsNewFile = 1
149
149
150 self.__isFirstTimeOnline = 1
150 self.__isFirstTimeOnline = 1
151
151
152 # self.ippSeconds = 0
152 # self.ippSeconds = 0
153
153
154 self.flagDiscontinuousBlock = 0
154 self.flagDiscontinuousBlock = 0
155
155
156 self.flagIsNewBlock = 0
156 self.flagIsNewBlock = 0
157
157
158 self.nTotalBlocks = 0
158 self.nTotalBlocks = 0
159
159
160 self.blocksize = 0
160 self.blocksize = 0
161
161
162 self.dataOut = self.createObjByDefault()
162 self.dataOut = self.createObjByDefault()
163
163
164 self.nTxs = 1
164 self.nTxs = 1
165
165
166 self.txIndex = 0
166 self.txIndex = 0
167
167
168 def createObjByDefault(self):
168 def createObjByDefault(self):
169
169
170 dataObj = Voltage()
170 dataObj = Voltage()
171
171
172 return dataObj
172 return dataObj
173
173
174 def __hasNotDataInBuffer(self):
174 def __hasNotDataInBuffer(self):
175
175
176 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
176 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
177 return 1
177 return 1
178
178
179 return 0
179 return 0
180
180
181 def getBlockDimension(self):
181 def getBlockDimension(self):
182 """
182 """
183 Obtiene la cantidad de puntos a leer por cada bloque de datos
183 Obtiene la cantidad de puntos a leer por cada bloque de datos
184
184
185 Affected:
185 Affected:
186 self.blocksize
186 self.blocksize
187
187
188 Return:
188 Return:
189 None
189 None
190 """
190 """
191 pts2read = self.processingHeaderObj.profilesPerBlock * \
191 pts2read = self.processingHeaderObj.profilesPerBlock * \
192 self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
192 self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
193 self.blocksize = pts2read
193 self.blocksize = pts2read
194
194
195 def readBlock(self):
195 def readBlock(self):
196 """
196 """
197 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
197 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
198 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
198 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
199 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
199 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
200 es seteado a 0
200 es seteado a 0
201
201
202 Inputs:
202 Inputs:
203 None
203 None
204
204
205 Return:
205 Return:
206 None
206 None
207
207
208 Affected:
208 Affected:
209 self.profileIndex
209 self.profileIndex
210 self.datablock
210 self.datablock
211 self.flagIsNewFile
211 self.flagIsNewFile
212 self.flagIsNewBlock
212 self.flagIsNewBlock
213 self.nTotalBlocks
213 self.nTotalBlocks
214
214
215 Exceptions:
215 Exceptions:
216 Si un bloque leido no es un bloque valido
216 Si un bloque leido no es un bloque valido
217 """
217 """
218
218
219 # if self.server is not None:
219 # if self.server is not None:
220 # self.zBlock = self.receiver.recv()
220 # self.zBlock = self.receiver.recv()
221 # self.zHeader = self.zBlock[:24]
221 # self.zHeader = self.zBlock[:24]
222 # self.zDataBlock = self.zBlock[24:]
222 # self.zDataBlock = self.zBlock[24:]
223 # junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')]))
223 # junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')]))
224 # self.processingHeaderObj.profilesPerBlock = 240
224 # self.processingHeaderObj.profilesPerBlock = 240
225 # self.processingHeaderObj.nHeights = 248
225 # self.processingHeaderObj.nHeights = 248
226 # self.systemHeaderObj.nChannels
226 # self.systemHeaderObj.nChannels
227 # else:
227 # else:
228 current_pointer_location = self.fp.tell()
228 current_pointer_location = self.fp.tell()
229 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
229 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
230
230
231 try:
231 try:
232 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
232 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
233 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
233 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
234 except:
234 except:
235 # print "The read block (%3d) has not enough data" %self.nReadBlocks
235 # print "The read block (%3d) has not enough data" %self.nReadBlocks
236
236
237 if self.waitDataBlock(pointer_location=current_pointer_location):
237 if self.waitDataBlock(pointer_location=current_pointer_location):
238 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
238 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
239 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
239 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
240 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
240 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
241 # return 0
241 # return 0
242
242
243 # Dimensions : nChannels, nProfiles, nSamples
243 # Dimensions : nChannels, nProfiles, nSamples
244
244
245 junk = numpy.transpose(junk, (2, 0, 1))
245 junk = numpy.transpose(junk, (2, 0, 1))
246 self.datablock = junk['real'] + junk['imag'] * 1j
246 self.datablock = junk['real'] + junk['imag'] * 1j
247
247
248 self.profileIndex = 0
248 self.profileIndex = 0
249
249
250 self.flagIsNewFile = 0
250 self.flagIsNewFile = 0
251 self.flagIsNewBlock = 1
251 self.flagIsNewBlock = 1
252
252
253 self.nTotalBlocks += 1
253 self.nTotalBlocks += 1
254 self.nReadBlocks += 1
254 self.nReadBlocks += 1
255
255
256 return 1
256 return 1
257
257
258 def getFirstHeader(self):
258 def getFirstHeader(self):
259
259
260 self.getBasicHeader()
260 self.getBasicHeader()
261
261
262 self.dataOut.processingHeaderObj = self.processingHeaderObj.copy()
262 self.dataOut.processingHeaderObj = self.processingHeaderObj.copy()
263
263
264 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
264 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
265
265
266 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
266 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
267
267
268 if self.nTxs > 1:
268 if self.nTxs > 1:
269 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
269 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
270 # Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
270 # Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
271
271
272 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
272 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
273 #
273 #
274 # if self.radarControllerHeaderObj.code is not None:
274 # if self.radarControllerHeaderObj.code is not None:
275 #
275 #
276 # self.dataOut.nCode = self.radarControllerHeaderObj.nCode
276 # self.dataOut.nCode = self.radarControllerHeaderObj.nCode
277 #
277 #
278 # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
278 # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
279 #
279 #
280 # self.dataOut.code = self.radarControllerHeaderObj.code
280 # self.dataOut.code = self.radarControllerHeaderObj.code
281
281
282 self.dataOut.dtype = self.dtype
282 self.dataOut.dtype = self.dtype
283
283
284 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
284 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
285
285
286 self.dataOut.heightList = numpy.arange(
286 self.dataOut.heightList = numpy.arange(
287 self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
287 self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
288
288
289 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
289 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
290
290
291 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
291 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
292
292
293 # asumo q la data no esta decodificada
293 # asumo q la data no esta decodificada
294 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode
294 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode
295
295
296 # asumo q la data no esta sin flip
296 # asumo q la data no esta sin flip
297 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip
297 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip
298
298
299 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
299 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
300
300
301 def reshapeData(self):
301 def reshapeData(self):
302
302
303 if self.nTxs < 0:
303 if self.nTxs < 0:
304 return
304 return
305
305
306 if self.nTxs == 1:
306 if self.nTxs == 1:
307 return
307 return
308
308
309 if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1. / self.nTxs) != 0:
309 if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1. / self.nTxs) != 0:
310 raise ValueError("1./nTxs (=%f), should be a multiple of nProfiles (=%d)" % (
310 raise ValueError("1./nTxs (=%f), should be a multiple of nProfiles (=%d)" % (
311 1. / self.nTxs, self.processingHeaderObj.profilesPerBlock))
311 1. / self.nTxs, self.processingHeaderObj.profilesPerBlock))
312
312
313 if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
313 if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
314 raise ValueError("nTxs (=%d), should be a multiple of nHeights (=%d)" % (
314 raise ValueError("nTxs (=%d), should be a multiple of nHeights (=%d)" % (
315 self.nTxs, self.processingHeaderObj.nHeights))
315 self.nTxs, self.processingHeaderObj.nHeights))
316
316
317 self.datablock = self.datablock.reshape(
317 self.datablock = self.datablock.reshape(
318 (self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock * self.nTxs, self.processingHeaderObj.nHeights / self.nTxs))
318 (self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock * self.nTxs, self.processingHeaderObj.nHeights / self.nTxs))
319
319
320 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock * self.nTxs
320 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock * self.nTxs
321 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights / self.nTxs) * \
321 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights / self.nTxs) * \
322 self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
322 self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
323 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
323 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
324
324
325 return
325 return
326
326
327 def readFirstHeaderFromServer(self):
327 def readFirstHeaderFromServer(self):
328
328
329 self.getFirstHeader()
329 self.getFirstHeader()
330
330
331 self.firstHeaderSize = self.basicHeaderObj.size
331 self.firstHeaderSize = self.basicHeaderObj.size
332
332
333 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
333 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
334 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
334 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
335 if datatype == 0:
335 if datatype == 0:
336 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
336 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
337 elif datatype == 1:
337 elif datatype == 1:
338 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
338 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
339 elif datatype == 2:
339 elif datatype == 2:
340 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
340 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
341 elif datatype == 3:
341 elif datatype == 3:
342 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
342 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
343 elif datatype == 4:
343 elif datatype == 4:
344 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
344 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
345 elif datatype == 5:
345 elif datatype == 5:
346 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
346 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
347 else:
347 else:
348 raise ValueError('Data type was not defined')
348 raise ValueError('Data type was not defined')
349
349
350 self.dtype = datatype_str
350 self.dtype = datatype_str
351 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
351 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
352 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
352 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
353 self.firstHeaderSize + self.basicHeaderSize * \
353 self.firstHeaderSize + self.basicHeaderSize * \
354 (self.processingHeaderObj.dataBlocksPerFile - 1)
354 (self.processingHeaderObj.dataBlocksPerFile - 1)
355 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
355 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
356 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
356 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
357 self.getBlockDimension()
357 self.getBlockDimension()
358
358
359 def getFromServer(self):
359 def getFromServer(self):
360 self.flagDiscontinuousBlock = 0
360 self.flagDiscontinuousBlock = 0
361 self.profileIndex = 0
361 self.profileIndex = 0
362 self.flagIsNewBlock = 1
362 self.flagIsNewBlock = 1
363 self.dataOut.flagNoData = False
363 self.dataOut.flagNoData = False
364 self.nTotalBlocks += 1
364 self.nTotalBlocks += 1
365 self.nReadBlocks += 1
365 self.nReadBlocks += 1
366 self.blockPointer = 0
366 self.blockPointer = 0
367
367
368 block = self.receiver.recv()
368 block = self.receiver.recv()
369
369
370 self.basicHeaderObj.read(block[self.blockPointer:])
370 self.basicHeaderObj.read(block[self.blockPointer:])
371 self.blockPointer += self.basicHeaderObj.length
371 self.blockPointer += self.basicHeaderObj.length
372 self.systemHeaderObj.read(block[self.blockPointer:])
372 self.systemHeaderObj.read(block[self.blockPointer:])
373 self.blockPointer += self.systemHeaderObj.length
373 self.blockPointer += self.systemHeaderObj.length
374 self.radarControllerHeaderObj.read(block[self.blockPointer:])
374 self.radarControllerHeaderObj.read(block[self.blockPointer:])
375 self.blockPointer += self.radarControllerHeaderObj.length
375 self.blockPointer += self.radarControllerHeaderObj.length
376 self.processingHeaderObj.read(block[self.blockPointer:])
376 self.processingHeaderObj.read(block[self.blockPointer:])
377 self.blockPointer += self.processingHeaderObj.length
377 self.blockPointer += self.processingHeaderObj.length
378 self.readFirstHeaderFromServer()
378 self.readFirstHeaderFromServer()
379
379
380 timestamp = self.basicHeaderObj.get_datatime()
380 timestamp = self.basicHeaderObj.get_datatime()
381 print('[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp))
381 print('[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp))
382 current_pointer_location = self.blockPointer
382 current_pointer_location = self.blockPointer
383 junk = numpy.fromstring(
383 junk = numpy.fromstring(
384 block[self.blockPointer:], self.dtype, self.blocksize)
384 block[self.blockPointer:], self.dtype, self.blocksize)
385
385
386 try:
386 try:
387 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
387 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
388 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
388 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
389 except:
389 except:
390 # print "The read block (%3d) has not enough data" %self.nReadBlocks
390 # print "The read block (%3d) has not enough data" %self.nReadBlocks
391 if self.waitDataBlock(pointer_location=current_pointer_location):
391 if self.waitDataBlock(pointer_location=current_pointer_location):
392 junk = numpy.fromstring(
392 junk = numpy.fromstring(
393 block[self.blockPointer:], self.dtype, self.blocksize)
393 block[self.blockPointer:], self.dtype, self.blocksize)
394 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
394 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
395 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
395 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
396 # return 0
396 # return 0
397
397
398 # Dimensions : nChannels, nProfiles, nSamples
398 # Dimensions : nChannels, nProfiles, nSamples
399
399
400 junk = numpy.transpose(junk, (2, 0, 1))
400 junk = numpy.transpose(junk, (2, 0, 1))
401 self.datablock = junk['real'] + junk['imag'] * 1j
401 self.datablock = junk['real'] + junk['imag'] * 1j
402 self.profileIndex = 0
402 self.profileIndex = 0
403 if self.selBlocksize == None:
403 if self.selBlocksize == None:
404 self.selBlocksize = self.dataOut.nProfiles
404 self.selBlocksize = self.dataOut.nProfiles
405 if self.selBlocktime != None:
405 if self.selBlocktime != None:
406 if self.dataOut.nCohInt is not None:
406 if self.dataOut.nCohInt is not None:
407 nCohInt = self.dataOut.nCohInt
407 nCohInt = self.dataOut.nCohInt
408 else:
408 else:
409 nCohInt = 1
409 nCohInt = 1
410 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
410 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
411 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
411 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
412 self.dataOut.data = self.datablock[:,
412 self.dataOut.data = self.datablock[:,
413 self.profileIndex:self.profileIndex + self.selBlocksize, :]
413 self.profileIndex:self.profileIndex + self.selBlocksize, :]
414 datasize = self.dataOut.data.shape[1]
414 datasize = self.dataOut.data.shape[1]
415 if datasize < self.selBlocksize:
415 if datasize < self.selBlocksize:
416 buffer = numpy.zeros(
416 buffer = numpy.zeros(
417 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
417 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
418 buffer[:, :datasize, :] = self.dataOut.data
418 buffer[:, :datasize, :] = self.dataOut.data
419 self.dataOut.data = buffer
419 self.dataOut.data = buffer
420 self.profileIndex = blockIndex
420 self.profileIndex = blockIndex
421
421
422 self.dataOut.flagDataAsBlock = True
422 self.dataOut.flagDataAsBlock = True
423 self.flagIsNewBlock = 1
423 self.flagIsNewBlock = 1
424 self.dataOut.realtime = self.online
424 self.dataOut.realtime = self.online
425
425
426 return self.dataOut.data
426 return self.dataOut.data
427
427
428 def getData(self):
428 def getData(self):
429 """
429 """
430 getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut
430 getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut
431 del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
431 del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
432 en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
432 en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
433 "readNextBlock"
433 "readNextBlock"
434
434
435 Ademas incrementa el contador del buffer "self.profileIndex" en 1.
435 Ademas incrementa el contador del buffer "self.profileIndex" en 1.
436
436
437 Return:
437 Return:
438
438
439 Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
439 Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
440 es igual al total de perfiles leidos desde el archivo.
440 es igual al total de perfiles leidos desde el archivo.
441
441
442 Si self.getByBlock == False:
442 Si self.getByBlock == False:
443
443
444 self.dataOut.data = buffer[:, thisProfile, :]
444 self.dataOut.data = buffer[:, thisProfile, :]
445
445
446 shape = [nChannels, nHeis]
446 shape = [nChannels, nHeis]
447
447
448 Si self.getByBlock == True:
448 Si self.getByBlock == True:
449
449
450 self.dataOut.data = buffer[:, :, :]
450 self.dataOut.data = buffer[:, :, :]
451
451
452 shape = [nChannels, nProfiles, nHeis]
452 shape = [nChannels, nProfiles, nHeis]
453
453
454 Variables afectadas:
454 Variables afectadas:
455 self.dataOut
455 self.dataOut
456 self.profileIndex
456 self.profileIndex
457
457
458 Affected:
458 Affected:
459 self.dataOut
459 self.dataOut
460 self.profileIndex
460 self.profileIndex
461 self.flagDiscontinuousBlock
461 self.flagDiscontinuousBlock
462 self.flagIsNewBlock
462 self.flagIsNewBlock
463 """
463 """
464 if self.flagNoMoreFiles:
464 if self.flagNoMoreFiles:
465 self.dataOut.flagNoData = True
465 self.dataOut.flagNoData = True
466 print('Process finished')
466 print('Process finished')
467 return 0
467 return 0
468 self.flagDiscontinuousBlock = 0
468 self.flagDiscontinuousBlock = 0
469 self.flagIsNewBlock = 0
469 self.flagIsNewBlock = 0
470 if self.__hasNotDataInBuffer():
470 if self.__hasNotDataInBuffer():
471 if not(self.readNextBlock()):
471 if not(self.readNextBlock()):
472 return 0
472 return 0
473
473
474 self.getFirstHeader()
474 self.getFirstHeader()
475
475
476 self.reshapeData()
476 self.reshapeData()
477 if self.datablock is None:
477 if self.datablock is None:
478 self.dataOut.flagNoData = True
478 self.dataOut.flagNoData = True
479 return 0
479 return 0
480
480
481 if not self.getByBlock:
481 if not self.getByBlock:
482
482
483 """
483 """
484 Return profile by profile
484 Return profile by profile
485
485
486 If nTxs > 1 then one profile is divided by nTxs and number of total
486 If nTxs > 1 then one profile is divided by nTxs and number of total
487 blocks is increased by nTxs (nProfiles *= nTxs)
487 blocks is increased by nTxs (nProfiles *= nTxs)
488 """
488 """
489 self.dataOut.flagDataAsBlock = False
489 self.dataOut.flagDataAsBlock = False
490 self.dataOut.data = self.datablock[:, self.profileIndex, :]
490 self.dataOut.data = self.datablock[:, self.profileIndex, :]
491 self.dataOut.profileIndex = self.profileIndex
491 self.dataOut.profileIndex = self.profileIndex
492
492
493 self.profileIndex += 1
493 self.profileIndex += 1
494
494
495 # elif self.selBlocksize==None or self.selBlocksize==self.dataOut.nProfiles:
495 # elif self.selBlocksize==None or self.selBlocksize==self.dataOut.nProfiles:
496 # """
496 # """
497 # Return all block
497 # Return all block
498 # """
498 # """
499 # self.dataOut.flagDataAsBlock = True
499 # self.dataOut.flagDataAsBlock = True
500 # self.dataOut.data = self.datablock
500 # self.dataOut.data = self.datablock
501 # self.dataOut.profileIndex = self.dataOut.nProfiles - 1
501 # self.dataOut.profileIndex = self.dataOut.nProfiles - 1
502 #
502 #
503 # self.profileIndex = self.dataOut.nProfiles
503 # self.profileIndex = self.dataOut.nProfiles
504
504
505 else:
505 else:
506 """
506 """
507 Return a block
507 Return a block
508 """
508 """
509 if self.selBlocksize == None:
509 if self.selBlocksize == None:
510 self.selBlocksize = self.dataOut.nProfiles
510 self.selBlocksize = self.dataOut.nProfiles
511 if self.selBlocktime != None:
511 if self.selBlocktime != None:
512 if self.dataOut.nCohInt is not None:
512 if self.dataOut.nCohInt is not None:
513 nCohInt = self.dataOut.nCohInt
513 nCohInt = self.dataOut.nCohInt
514 else:
514 else:
515 nCohInt = 1
515 nCohInt = 1
516 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
516 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
517 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
517 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
518
518
519 self.dataOut.data = self.datablock[:,
519 self.dataOut.data = self.datablock[:,
520 self.profileIndex:self.profileIndex + self.selBlocksize, :]
520 self.profileIndex:self.profileIndex + self.selBlocksize, :]
521 self.profileIndex += self.selBlocksize
521 self.profileIndex += self.selBlocksize
522 datasize = self.dataOut.data.shape[1]
522 datasize = self.dataOut.data.shape[1]
523
523
524 if datasize < self.selBlocksize:
524 if datasize < self.selBlocksize:
525 buffer = numpy.zeros(
525 buffer = numpy.zeros(
526 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
526 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
527 buffer[:, :datasize, :] = self.dataOut.data
527 buffer[:, :datasize, :] = self.dataOut.data
528
528
529 while datasize < self.selBlocksize: # Not enough profiles to fill the block
529 while datasize < self.selBlocksize: # Not enough profiles to fill the block
530 if not(self.readNextBlock()):
530 if not(self.readNextBlock()):
531 return 0
531 return 0
532 self.getFirstHeader()
532 self.getFirstHeader()
533 self.reshapeData()
533 self.reshapeData()
534 if self.datablock is None:
534 if self.datablock is None:
535 self.dataOut.flagNoData = True
535 self.dataOut.flagNoData = True
536 return 0
536 return 0
537 # stack data
537 # stack data
538 blockIndex = self.selBlocksize - datasize
538 blockIndex = self.selBlocksize - datasize
539 datablock1 = self.datablock[:, :blockIndex, :]
539 datablock1 = self.datablock[:, :blockIndex, :]
540
540
541 buffer[:, datasize:datasize +
541 buffer[:, datasize:datasize +
542 datablock1.shape[1], :] = datablock1
542 datablock1.shape[1], :] = datablock1
543 datasize += datablock1.shape[1]
543 datasize += datablock1.shape[1]
544
544
545 self.dataOut.data = buffer
545 self.dataOut.data = buffer
546 self.profileIndex = blockIndex
546 self.profileIndex = blockIndex
547
547
548 self.dataOut.flagDataAsBlock = True
548 self.dataOut.flagDataAsBlock = True
549 self.dataOut.nProfiles = self.dataOut.data.shape[1]
549 self.dataOut.nProfiles = self.dataOut.data.shape[1]
550
550
551 self.dataOut.flagNoData = False
551 self.dataOut.flagNoData = False
552
552
553 self.getBasicHeader()
553 self.getBasicHeader()
554
554
555 self.dataOut.realtime = self.online
555 self.dataOut.realtime = self.online
556
556
557 return self.dataOut.data
557 return self.dataOut.data
558
558
559
559
560 class VoltageWriter(JRODataWriter, Operation):
560 class VoltageWriter(JRODataWriter, Operation):
561 """
561 """
562 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
562 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
563 de los datos siempre se realiza por bloques.
563 de los datos siempre se realiza por bloques.
564 """
564 """
565
565
566 ext = ".r"
566 ext = ".r"
567
567
568 optchar = "D"
568 optchar = "D"
569
569
570 shapeBuffer = None
570 shapeBuffer = None
571
571
572 def __init__(self, **kwargs):
572 def __init__(self, **kwargs):
573 """
573 """
574 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
574 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
575
575
576 Affected:
576 Affected:
577 self.dataOut
577 self.dataOut
578
578
579 Return: None
579 Return: None
580 """
580 """
581 Operation.__init__(self, **kwargs)
581 Operation.__init__(self, **kwargs)
582
582
583 self.nTotalBlocks = 0
583 self.nTotalBlocks = 0
584
584
585 self.profileIndex = 0
585 self.profileIndex = 0
586
586
587 self.isConfig = False
587 self.isConfig = False
588
588
589 self.fp = None
589 self.fp = None
590
590
591 self.flagIsNewFile = 1
591 self.flagIsNewFile = 1
592
592
593 self.blockIndex = 0
593 self.blockIndex = 0
594
594
595 self.flagIsNewBlock = 0
595 self.flagIsNewBlock = 0
596
596
597 self.setFile = None
597 self.setFile = None
598
598
599 self.dtype = None
599 self.dtype = None
600
600
601 self.path = None
601 self.path = None
602
602
603 self.filename = None
603 self.filename = None
604
604
605 self.basicHeaderObj = BasicHeader(LOCALTIME)
605 self.basicHeaderObj = BasicHeader(LOCALTIME)
606
606
607 self.systemHeaderObj = SystemHeader()
607 self.systemHeaderObj = SystemHeader()
608
608
609 self.radarControllerHeaderObj = RadarControllerHeader()
609 self.radarControllerHeaderObj = RadarControllerHeader()
610
610
611 self.processingHeaderObj = ProcessingHeader()
611 self.processingHeaderObj = ProcessingHeader()
612
612
613 def hasAllDataInBuffer(self):
613 def hasAllDataInBuffer(self):
614 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
614 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
615 return 1
615 return 1
616 return 0
616 return 0
617
617
618 def setBlockDimension(self):
618 def setBlockDimension(self):
619 """
619 """
620 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
620 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
621
621
622 Affected:
622 Affected:
623 self.shape_spc_Buffer
623 self.shape_spc_Buffer
624 self.shape_cspc_Buffer
624 self.shape_cspc_Buffer
625 self.shape_dc_Buffer
625 self.shape_dc_Buffer
626
626
627 Return: None
627 Return: None
628 """
628 """
629 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
629 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
630 self.processingHeaderObj.nHeights,
630 self.processingHeaderObj.nHeights,
631 self.systemHeaderObj.nChannels)
631 self.systemHeaderObj.nChannels)
632
632
633 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
633 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
634 self.processingHeaderObj.profilesPerBlock,
634 self.processingHeaderObj.profilesPerBlock,
635 self.processingHeaderObj.nHeights),
635 self.processingHeaderObj.nHeights),
636 dtype=numpy.dtype('complex64'))
636 dtype=numpy.dtype('complex64'))
637
637
638 def writeBlock(self):
638 def writeBlock(self):
639 """
639 """
640 Escribe el buffer en el file designado
640 Escribe el buffer en el file designado
641
641
642 Affected:
642 Affected:
643 self.profileIndex
643 self.profileIndex
644 self.flagIsNewFile
644 self.flagIsNewFile
645 self.flagIsNewBlock
645 self.flagIsNewBlock
646 self.nTotalBlocks
646 self.nTotalBlocks
647 self.blockIndex
647 self.blockIndex
648
648
649 Return: None
649 Return: None
650 """
650 """
651 data = numpy.zeros(self.shapeBuffer, self.dtype)
651 data = numpy.zeros(self.shapeBuffer, self.dtype)
652
652
653 junk = numpy.transpose(self.datablock, (1, 2, 0))
653 junk = numpy.transpose(self.datablock, (1, 2, 0))
654
654
655 data['real'] = junk.real
655 data['real'] = junk.real
656 data['imag'] = junk.imag
656 data['imag'] = junk.imag
657
657
658 data = data.reshape((-1))
658 data = data.reshape((-1))
659
659
660 data.tofile(self.fp)
660 data.tofile(self.fp)
661
661
662 self.datablock.fill(0)
662 self.datablock.fill(0)
663
663
664 self.profileIndex = 0
664 self.profileIndex = 0
665 self.flagIsNewFile = 0
665 self.flagIsNewFile = 0
666 self.flagIsNewBlock = 1
666 self.flagIsNewBlock = 1
667
667
668 self.blockIndex += 1
668 self.blockIndex += 1
669 self.nTotalBlocks += 1
669 self.nTotalBlocks += 1
670
670
671 # print "[Writing] Block = %04d" %self.blockIndex
671 # print "[Writing] Block = %04d" %self.blockIndex
672
672
673 def putData(self):
673 def putData(self):
674 """
674 """
675 Setea un bloque de datos y luego los escribe en un file
675 Setea un bloque de datos y luego los escribe en un file
676
676
677 Affected:
677 Affected:
678 self.flagIsNewBlock
678 self.flagIsNewBlock
679 self.profileIndex
679 self.profileIndex
680
680
681 Return:
681 Return:
682 0 : Si no hay data o no hay mas files que puedan escribirse
682 0 : Si no hay data o no hay mas files que puedan escribirse
683 1 : Si se escribio la data de un bloque en un file
683 1 : Si se escribio la data de un bloque en un file
684 """
684 """
685 if self.dataOut.flagNoData:
685 if self.dataOut.flagNoData:
686 return 0
686 return 0
687
687
688 self.flagIsNewBlock = 0
688 self.flagIsNewBlock = 0
689
689
690 if self.dataOut.flagDiscontinuousBlock:
690 if self.dataOut.flagDiscontinuousBlock:
691 self.datablock.fill(0)
691 self.datablock.fill(0)
692 self.profileIndex = 0
692 self.profileIndex = 0
693 self.setNextFile()
693 self.setNextFile()
694
694
695 if self.profileIndex == 0:
695 if self.profileIndex == 0:
696 self.setBasicHeader()
696 self.setBasicHeader()
697
697
698 self.datablock[:, self.profileIndex, :] = self.dataOut.data
698 self.datablock[:, self.profileIndex, :] = self.dataOut.data
699
699
700 self.profileIndex += 1
700 self.profileIndex += 1
701
701
702 if self.hasAllDataInBuffer():
702 if self.hasAllDataInBuffer():
703 # if self.flagIsNewFile:
703 # if self.flagIsNewFile:
704 self.writeNextBlock()
704 self.writeNextBlock()
705 # self.setFirstHeader()
705 # self.setFirstHeader()
706
706
707 return 1
707 return 1
708
708
709 def __getBlockSize(self):
709 def __getBlockSize(self):
710 '''
710 '''
711 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
711 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
712 '''
712 '''
713
713
714 dtype_width = self.getDtypeWidth()
714 dtype_width = self.getDtypeWidth()
715
715
716 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels *
716 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels *
717 self.profilesPerBlock * dtype_width * 2)
717 self.profilesPerBlock * dtype_width * 2)
718
718
719 return blocksize
719 return blocksize
720
720
721 def setFirstHeader(self):
721 def setFirstHeader(self):
722 """
722 """
723 Obtiene una copia del First Header
723 Obtiene una copia del First Header
724
724
725 Affected:
725 Affected:
726 self.systemHeaderObj
726 self.systemHeaderObj
727 self.radarControllerHeaderObj
727 self.radarControllerHeaderObj
728 self.dtype
728 self.dtype
729
729
730 Return:
730 Return:
731 None
731 None
732 """
732 """
733
733
734 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
734 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
735 self.systemHeaderObj.nChannels = self.dataOut.nChannels
735 self.systemHeaderObj.nChannels = self.dataOut.nChannels
736 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
736 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
737
737
738 self.processingHeaderObj.dtype = 0 # Voltage
738 self.processingHeaderObj.dtype = 0 # Voltage
739 self.processingHeaderObj.blockSize = self.__getBlockSize()
739 self.processingHeaderObj.blockSize = self.__getBlockSize()
740 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
740 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
741 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
741 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
742 # podria ser 1 o self.dataOut.processingHeaderObj.nWindows
742 # podria ser 1 o self.dataOut.processingHeaderObj.nWindows
743 self.processingHeaderObj.nWindows = 1
743 self.processingHeaderObj.nWindows = 1
744 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
744 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
745 # Cuando la data de origen es de tipo Voltage
745 # Cuando la data de origen es de tipo Voltage
746 self.processingHeaderObj.nIncohInt = 1
746 self.processingHeaderObj.nIncohInt = 1
747 # Cuando la data de origen es de tipo Voltage
747 # Cuando la data de origen es de tipo Voltage
748 self.processingHeaderObj.totalSpectra = 0
748 self.processingHeaderObj.totalSpectra = 0
749
749
750 if self.dataOut.code is not None:
750 if self.dataOut.code is not None:
751 self.processingHeaderObj.code = self.dataOut.code
751 self.processingHeaderObj.code = self.dataOut.code
752 self.processingHeaderObj.nCode = self.dataOut.nCode
752 self.processingHeaderObj.nCode = self.dataOut.nCode
753 self.processingHeaderObj.nBaud = self.dataOut.nBaud
753 self.processingHeaderObj.nBaud = self.dataOut.nBaud
754
754
755 if self.processingHeaderObj.nWindows != 0:
755 if self.processingHeaderObj.nWindows != 0:
756 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
756 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
757 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - \
757 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - \
758 self.dataOut.heightList[0]
758 self.dataOut.heightList[0]
759 self.processingHeaderObj.nHeights = self.dataOut.nHeights
759 self.processingHeaderObj.nHeights = self.dataOut.nHeights
760 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
760 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
761
761
762 self.processingHeaderObj.processFlags = self.getProcessFlags()
762 self.processingHeaderObj.processFlags = self.getProcessFlags()
763
763
764 self.setBasicHeader() No newline at end of file
764 self.setBasicHeader()
765 No newline at end of file
This diff has been collapsed as it changes many lines, (606 lines changed) Show them Hide them
@@ -1,360 +1,538
1 '''
1 '''
2
2 Updated for multiprocessing
3 $Author: murco $
3 Author : Sergio Cortez
4 $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $
4 Jan 2018
5 Abstract:
6 Base class for processing units and operations. A decorator provides multiprocessing features and interconnect the processes created.
7 The argument (kwargs) sent from the controller is parsed and filtered via the decorator for each processing unit or operation instantiated.
8 The decorator handle also the methods inside the processing unit to be called from the main script (not as operations) (OPERATION -> type ='self').
9
10 Based on:
11 $Author: murco $
12 $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $
5 '''
13 '''
14 from platform import python_version
6 import inspect
15 import inspect
7 from fuzzywuzzy import process
16 import zmq
8
17 import time
9 def checkKwargs(method, kwargs):
18 import pickle
10 currentKwargs = kwargs
19 import os
11 choices = inspect.getargspec(method).args
20 from multiprocessing import Process
12 try:
13 choices.remove('self')
14 except Exception as e:
15 pass
16
21
17 try:
22 from schainpy.utils import log
18 choices.remove('dataOut')
19 except Exception as e:
20 pass
21
23
22 for kwarg in kwargs:
23 fuzz = process.extractOne(kwarg, choices)
24 if fuzz is None:
25 continue
26 if fuzz[1] < 100:
27 raise Exception('\x1b[0;32;40mDid you mean {} instead of {} in {}? \x1b[0m'.
28 format(fuzz[0], kwarg, method.__self__.__class__.__name__))
29
24
30 class ProcessingUnit(object):
25 class ProcessingUnit(object):
31
26
32 """
27 """
33 Esta es la clase base para el procesamiento de datos.
28 Update - Jan 2018 - MULTIPROCESSING
29 All the "call" methods present in the previous base were removed.
30 The majority of operations are independant processes, thus
31 the decorator is in charge of communicate the operation processes
32 with the proccessing unit via IPC.
34
33
35 Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser:
34 The constructor does not receive any argument. The remaining methods
36 - Metodos internos (callMethod)
35 are related with the operations to execute.
37 - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos
36
38 tienen que ser agreagados con el metodo "add".
39
37
40 """
38 """
41 # objeto de datos de entrada (Voltage, Spectra o Correlation)
39 # objeto de datos de entrada (Voltage, Spectra o Correlation)
42 dataIn = None
40 dataIn = None
43 dataInList = []
41 dataInList = []
44
42
45 # objeto de datos de entrada (Voltage, Spectra o Correlation)
43 # objeto de datos de entrada (Voltage, Spectra o Correlation)
44
45 id = None
46 inputId = None
47
46 dataOut = None
48 dataOut = None
47
49
50 dictProcs = None
51
48 operations2RunDict = None
52 operations2RunDict = None
49
53
50 isConfig = False
54 isConfig = False
51
55
52
56 def __init__(self):
53 def __init__(self, *args, **kwargs):
54
57
55 self.dataIn = None
58 self.dataIn = None
56 self.dataInList = []
57
58 self.dataOut = None
59 self.dataOut = None
59
60
60 self.operations2RunDict = {}
61 self.operationKwargs = {}
62
63 self.isConfig = False
61 self.isConfig = False
64
62
65 self.args = args
66 self.kwargs = kwargs
67
68 if not hasattr(self, 'name'):
69 self.name = self.__class__.__name__
70
71 checkKwargs(self.run, kwargs)
72
73 def getAllowedArgs(self):
63 def getAllowedArgs(self):
74 if hasattr(self, '__attrs__'):
64 if hasattr(self, '__attrs__'):
75 return self.__attrs__
65 return self.__attrs__
76 else:
66 else:
77 return inspect.getargspec(self.run).args
67 return inspect.getargspec(self.run).args
78
68
79 def addOperationKwargs(self, objId, **kwargs):
69 def addOperationKwargs(self, objId, **kwargs):
80 '''
70 '''
81 '''
71 '''
82
72
83 self.operationKwargs[objId] = kwargs
73 self.operationKwargs[objId] = kwargs
84
74
85
86 def addOperation(self, opObj, objId):
75 def addOperation(self, opObj, objId):
87
76
88 """
77 """
89 Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el
78 This method is used in the controller, and update the dictionary containing the operations to execute. The dict
90 identificador asociado a este objeto.
79 posses the id of the operation process (IPC purposes)
91
80
92 Input:
81 Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el
82 identificador asociado a este objeto.
93
83
94 object : objeto de la clase "Operation"
84 Input:
95
85
96 Return:
86 object : objeto de la clase "Operation"
87
88 Return:
97
89
98 objId : identificador del objeto, necesario para ejecutar la operacion
90 objId : identificador del objeto, necesario para comunicar con master(procUnit)
99 """
91 """
100
92
101 self.operations2RunDict[objId] = opObj
93 self.operations2RunDict[objId] = opObj
102
94
103 return objId
95 return objId
104
96
97
105 def getOperationObj(self, objId):
98 def getOperationObj(self, objId):
106
99
107 if objId not in list(self.operations2RunDict.keys()):
100 if objId not in list(self.operations2RunDict.keys()):
108 return None
101 return None
109
102
110 return self.operations2RunDict[objId]
103 return self.operations2RunDict[objId]
111
104
112 def operation(self, **kwargs):
105 def operation(self, **kwargs):
113
106
114 """
107 """
115 Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los
108 Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los
116 atributos del objeto dataOut
109 atributos del objeto dataOut
117
110
118 Input:
111 Input:
119
112
120 **kwargs : Diccionario de argumentos de la funcion a ejecutar
113 **kwargs : Diccionario de argumentos de la funcion a ejecutar
121 """
114 """
122
115
123 raise NotImplementedError
116 raise NotImplementedError
124
125 def callMethod(self, name, opId):
126
127 """
128 Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase.
129
130 Input:
131 name : nombre del metodo a ejecutar
132
133 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
134
135 """
136
117
137 #Checking the inputs
118 def setup(self):
138 if name == 'run':
139
140 if not self.checkInputs():
141 self.dataOut.flagNoData = True
142 return False
143 else:
144 #Si no es un metodo RUN la entrada es la misma dataOut (interna)
145 if self.dataOut is not None and self.dataOut.isEmpty():
146 return False
147
119
148 #Getting the pointer to method
120 raise NotImplementedError
149 methodToCall = getattr(self, name)
150
121
151 #Executing the self method
122 def run(self):
152
123
153 if hasattr(self, 'mp'):
124 raise NotImplementedError
154 if name=='run':
155 if self.mp is False:
156 self.mp = True
157 self.start()
158 else:
159 self.operationKwargs[opId]['parent'] = self.kwargs
160 methodToCall(**self.operationKwargs[opId])
161 else:
162 if name=='run':
163 methodToCall(**self.kwargs)
164 else:
165 methodToCall(**self.operationKwargs[opId])
166
125
167 if self.dataOut is None:
126 def close(self):
168 return False
127 #Close every thread, queue or any other object here is it is neccesary.
128 return
129
130 class Operation(object):
169
131
170 if self.dataOut.isEmpty():
132 """
171 return False
133 Update - Jan 2018 - MULTIPROCESSING
172
134
173 return True
135 Most of the methods remained the same. The decorator parse the arguments and executed the run() method for each process.
136 The constructor doe snot receive any argument, neither the baseclass.
174
137
175 def callObject(self, objId):
176
138
177 """
139 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
178 Ejecuta la operacion asociada al identificador del objeto "objId"
140 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
141 acumulacion dentro de esta clase
179
142
180 Input:
143 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
181
144
182 objId : identificador del objeto a ejecutar
145 """
146 id = None
147 __buffer = None
148 dest = None
149 isConfig = False
150 readyFlag = None
183
151
184 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
152 def __init__(self):
185
153
186 Return:
154 self.buffer = None
155 self.dest = None
156 self.isConfig = False
157 self.readyFlag = False
187
158
188 None
159 if not hasattr(self, 'name'):
189 """
160 self.name = self.__class__.__name__
161
162 def getAllowedArgs(self):
163 if hasattr(self, '__attrs__'):
164 return self.__attrs__
165 else:
166 return inspect.getargspec(self.run).args
190
167
191 if self.dataOut is not None and self.dataOut.isEmpty():
168 def setup(self):
192 return False
193
169
194 externalProcObj = self.operations2RunDict[objId]
170 self.isConfig = True
195
171
196 if hasattr(externalProcObj, 'mp'):
172 raise NotImplementedError
197 if externalProcObj.mp is False:
198 externalProcObj.kwargs['parent'] = self.kwargs
199 self.operationKwargs[objId] = externalProcObj.kwargs
200 externalProcObj.mp = True
201 externalProcObj.start()
202 else:
203 externalProcObj.run(self.dataOut, **externalProcObj.kwargs)
204 self.operationKwargs[objId] = externalProcObj.kwargs
205
173
206
174
207 return True
175 def run(self, dataIn, **kwargs):
208
176
209 def call(self, opType, opName=None, opId=None):
210 """
177 """
211 Return True si ejecuta la operacion interna nombrada "opName" o la operacion externa
178 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los
212 identificada con el id "opId"; con los argumentos "**kwargs".
179 atributos del objeto dataIn.
213
214 False si la operacion no se ha ejecutado.
215
180
216 Input:
181 Input:
217
182
218 opType : Puede ser "self" o "external"
183 dataIn : objeto del tipo JROData
219
184
220 Depende del tipo de operacion para llamar a:callMethod or callObject:
185 Return:
221
186
222 1. If opType = "self": Llama a un metodo propio de esta clase:
187 None
223
188
224 name_method = getattr(self, name)
189 Affected:
225 name_method(**kwargs)
190 __buffer : buffer de recepcion de datos.
226
191
192 """
193 if not self.isConfig:
194 self.setup(**kwargs)
227
195
228 2. If opType = "other" o"external": Llama al metodo "run()" de una instancia de la
196 raise NotImplementedError
229 clase "Operation" o de un derivado de ella:
230
197
231 instanceName = self.operationList[opId]
198 def close(self):
232 instanceName.run(**kwargs)
233
199
234 opName : Si la operacion es interna (opType = 'self'), entonces el "opName" sera
200 pass
235 usada para llamar a un metodo interno de la clase Processing
236
201
237 opId : Si la operacion es externa (opType = 'other' o 'external), entonces el
238 "opId" sera usada para llamar al metodo "run" de la clase Operation
239 registrada anteriormente con ese Id
240
202
241 Exception:
203 ######### Decorator #########
242 Este objeto de tipo Operation debe de haber sido agregado antes con el metodo:
243 "addOperation" e identificado con el valor "opId" = el id de la operacion.
244 De lo contrario retornara un error del tipo ValueError
245
204
246 """
247
205
248 if opType == 'self':
206 def MPDecorator(BaseClass):
207
208 """
209 "Multiprocessing class decorator"
249
210
250 if not opName:
211 This function add multiprocessing features to the base class. Also,
251 raise ValueError("opName parameter should be defined")
212 it handle the communication beetween processes (readers, procUnits and operations).
213 Receive the arguments at the moment of instantiation. According to that, discriminates if it
214 is a procUnit or an operation
215 """
216
217 class MPClass(BaseClass, Process):
218
219 "This is the overwritten class"
220 operations2RunDict = None
221 socket_l = None
222 socket_p = None
223 socketOP = None
224 socket_router = None
225 dictProcs = None
226 typeProc = None
227 def __init__(self, *args, **kwargs):
228 super(MPClass, self).__init__()
229 Process.__init__(self)
230
231
232 self.operationKwargs = {}
233 self.args = args
234
235
236 self.operations2RunDict = {}
237 self.kwargs = kwargs
238
239 # The number of arguments (args) determine the type of process
240
241 if len(self.args) is 3:
242 self.typeProc = "ProcUnit"
243 self.id = args[0] #topico de publicacion
244 self.inputId = args[1] #topico de subcripcion
245 self.dictProcs = args[2] #diccionario de procesos globales
246 else:
247 self.id = args[0]
248 self.typeProc = "Operation"
249
250 def addOperationKwargs(self, objId, **kwargs):
251
252 self.operationKwargs[objId] = kwargs
252
253
253 sts = self.callMethod(opName, opId)
254 def getAllowedArgs(self):
254
255
255 elif opType == 'other' or opType == 'external' or opType == 'plotter':
256 if hasattr(self, '__attrs__'):
257 return self.__attrs__
258 else:
259 return inspect.getargspec(self.run).args
260
261
262 def sockListening(self, topic):
263
264 """
265 This function create a socket to receive objects.
266 The 'topic' argument is related to the publisher process from which the self process is
267 listening (data).
268 In the case were the self process is listening to a Reader (proc Unit),
269 special conditions are introduced to maximize parallelism.
270 """
271
272 cont = zmq.Context()
273 zmq_socket = cont.socket(zmq.SUB)
274 if not os.path.exists('/tmp/socketTmp'):
275 os.mkdir('/tmp/socketTmp')
276
277 if 'Reader' in self.dictProcs[self.inputId].name:
278 zmq_socket.connect('ipc:///tmp/socketTmp/b')
279
280 else:
281 zmq_socket.connect('ipc:///tmp/socketTmp/%s' % self.inputId)
282
283 #log.error('RECEIVING FROM {} {}'.format(self.inputId, str(topic).encode()))
284 zmq_socket.setsockopt(zmq.SUBSCRIBE, str(topic).encode()) #yong
256
285
257 if not opId:
286 return zmq_socket
258 raise ValueError("opId parameter should be defined")
259
287
260 if opId not in list(self.operations2RunDict.keys()):
261 raise ValueError("Any operation with id=%s has been added" %str(opId))
262
288
263 sts = self.callObject(opId)
289 def listenProc(self, sock):
264
290
265 else:
291 """
266 raise ValueError("opType should be 'self', 'external' or 'plotter'; and not '%s'" %opType)
292 This function listen to a ipc addres until a message is recovered. To serialize the
293 data (object), pickle has been use.
294 The 'sock' argument is the socket previously connect to an ipc address and with a topic subscription.
295 """
296
297 a = sock.recv_multipart()
298 a = pickle.loads(a[1])
299 return a
267
300
268 return sts
301 def sockPublishing(self):
269
302
270 def setInput(self, dataIn):
303 """
304 This function create a socket for publishing purposes.
305 Depending on the process type from where is created, it binds or connect
306 to special IPC addresses.
307 """
308 time.sleep(4) #yong
309 context = zmq.Context()
310 zmq_socket = context.socket(zmq.PUB)
311 if not os.path.exists('/tmp/socketTmp'): os.mkdir('/tmp/socketTmp')
312 if 'Reader' in self.dictProcs[self.id].name:
313 zmq_socket.connect('ipc:///tmp/socketTmp/a')
314 else:
315 zmq_socket.bind('ipc:///tmp/socketTmp/%s' % self.id)
271
316
272 self.dataIn = dataIn
317 return zmq_socket
273 self.dataInList.append(dataIn)
274
318
275 def getOutputObj(self):
319 def publishProc(self, sock, data):
276
320
277 return self.dataOut
321 """
322 This function publish a python object (data) under a specific topic in a socket (sock).
323 Usually, the topic is the self id of the process.
324 """
278
325
279 def checkInputs(self):
326 sock.send_multipart([str(self.id).encode(), pickle.dumps(data)]) #yong
327
328 return True
280
329
281 for thisDataIn in self.dataInList:
330 def sockOp(self):
282
331
283 if thisDataIn.isEmpty():
332 """
284 return False
333 This function create a socket for communication purposes with operation processes.
334 """
285
335
286 return True
336 cont = zmq.Context()
337 zmq_socket = cont.socket(zmq.DEALER)
338
339 if python_version()[0] == '2':
340 zmq_socket.setsockopt(zmq.IDENTITY, self.id)
341 if python_version()[0] == '3':
342 zmq_socket.setsockopt_string(zmq.IDENTITY, self.id)
287
343
288 def setup(self):
289
344
290 raise NotImplementedError
345 return zmq_socket
291
346
292 def run(self):
293
347
294 raise NotImplementedError
348 def execOp(self, socket, opId, dataObj):
295
349
296 def close(self):
350 """
297 #Close every thread, queue or any other object here is it is neccesary.
351 This function 'execute' an operation main routine by establishing a
298 return
352 connection with it and sending a python object (dataOut).
353 """
354 if not os.path.exists('/tmp/socketTmp'): os.mkdir('/tmp/socketTmp')
355 socket.connect('ipc:///tmp/socketTmp/%s' %opId)
356
357
358 socket.send(pickle.dumps(dataObj)) #yong
359
360 argument = socket.recv_multipart()[0]
361
362 argument = pickle.loads(argument)
363
364 return argument
365
366 def sockIO(self):
299
367
300 class Operation(object):
368 """
369 Socket defined for an operation process. It is able to recover the object sent from another process as well as a
370 identifier of who sent it.
371 """
301
372
302 """
373 cont = zmq.Context()
303 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
374 if not os.path.exists('/tmp/socketTmp'): os.mkdir('/tmp/socketTmp')
304 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
375 socket = cont.socket(zmq.ROUTER)
305 acumulacion dentro de esta clase
376 socket.bind('ipc:///tmp/socketTmp/%s' % self.id)
306
377
307 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
378 return socket
308
379
309 """
380 def funIOrec(self, socket):
310
381
311 __buffer = None
382 """
312 isConfig = False
383 Operation method, recover the id of the process who sent a python object.
384 The 'socket' argument is the socket binded to a specific process ipc.
385 """
313
386
314 def __init__(self, **kwargs):
387 #id_proc = socket.recv()
388
389 #dataObj = socket.recv_pyobj()
390
391 dataObj = socket.recv_multipart()
392
393 dataObj[1] = pickle.loads(dataObj[1])
394 return dataObj[0], dataObj[1]
395
396 def funIOsen(self, socket, data, dest):
397
398 """
399 Operation method, send a python object to a specific destination.
400 The 'dest' argument is the id of a proccesinf unit.
401 """
402
403 socket.send_multipart([dest, pickle.dumps(data)]) #yong
315
404
316 self.__buffer = None
405 return True
317 self.isConfig = False
318 self.kwargs = kwargs
319 if not hasattr(self, 'name'):
320 self.name = self.__class__.__name__
321 checkKwargs(self.run, kwargs)
322
406
323 def getAllowedArgs(self):
324 if hasattr(self, '__attrs__'):
325 return self.__attrs__
326 else:
327 return inspect.getargspec(self.run).args
328
407
329 def setup(self):
408 def runReader(self):
330
409
331 self.isConfig = True
410 # time.sleep(3)
411 while True:
412
413 BaseClass.run(self, **self.kwargs)
332
414
333 raise NotImplementedError
334
415
335 def run(self, dataIn, **kwargs):
416 keyList = list(self.operations2RunDict.keys())
417 keyList.sort()
418
419 for key in keyList:
420 self.socketOP = self.sockOp()
421 self.dataOut = self.execOp(self.socketOP, key, self.dataOut)
336
422
337 """
423
338 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los
424 if self.flagNoMoreFiles: #Usar un objeto con flags para saber si termino el proc o hubo un error
339 atributos del objeto dataIn.
425 self.publishProc(self.socket_p, "Finish")
426 break
340
427
341 Input:
428 if self.dataOut.flagNoData:
342
429 continue
343 dataIn : objeto del tipo JROData
430
431 #print("Publishing data...")
432 self.publishProc(self.socket_p, self.dataOut)
433 # time.sleep(2)
434
435
436 print("%s done" %BaseClass.__name__)
437 return 0
438
439 def runProc(self):
344
440
345 Return:
441 # All the procUnits with kwargs that require a setup initialization must be defined here.
346
442
347 None
443 if self.setupReq:
444 BaseClass.setup(self, **self.kwargs)
348
445
349 Affected:
446 while True:
350 __buffer : buffer de recepcion de datos.
447 self.dataIn = self.listenProc(self.socket_l)
448 #print("%s received data" %BaseClass.__name__)
449
450 if self.dataIn == "Finish":
451 break
452
453 m_arg = list(self.kwargs.keys())
454 num_arg = list(range(1,int(BaseClass.run.__code__.co_argcount)))
455
456 run_arg = {}
457
458 for var in num_arg:
459 if BaseClass.run.__code__.co_varnames[var] in m_arg:
460 run_arg[BaseClass.run.__code__.co_varnames[var]] = self.kwargs[BaseClass.run.__code__.co_varnames[var]]
461
462 #BaseClass.run(self, **self.kwargs)
463 BaseClass.run(self, **run_arg)
464
465 ## Iterar sobre una serie de data que podrias aplicarse
466
467 for m_name in BaseClass.METHODS:
468
469 met_arg = {}
470
471 for arg in m_arg:
472 if arg in BaseClass.METHODS[m_name]:
473 for att in BaseClass.METHODS[m_name]:
474 met_arg[att] = self.kwargs[att]
475
476 method = getattr(BaseClass, m_name)
477 method(self, **met_arg)
478 break
479
480 if self.dataOut.flagNoData:
481 continue
482
483 keyList = list(self.operations2RunDict.keys())
484 keyList.sort()
485
486 for key in keyList:
487
488 self.socketOP = self.sockOp()
489 self.dataOut = self.execOp(self.socketOP, key, self.dataOut)
490
491
492 self.publishProc(self.socket_p, self.dataOut)
493
494
495 print("%s done" %BaseClass.__name__)
496
497 return 0
498
499 def runOp(self):
500
501 while True:
502
503 [self.dest ,self.buffer] = self.funIOrec(self.socket_router)
504
505 self.buffer = BaseClass.run(self, self.buffer, **self.kwargs)
506
507 self.funIOsen(self.socket_router, self.buffer, self.dest)
508
509 print("%s done" %BaseClass.__name__)
510 return 0
511
512
513 def run(self):
514
515 if self.typeProc is "ProcUnit":
516
517 self.socket_p = self.sockPublishing()
518
519 if 'Reader' not in self.dictProcs[self.id].name:
520 self.socket_l = self.sockListening(self.inputId)
521 self.runProc()
522
523 else:
524
525 self.runReader()
526
527 elif self.typeProc is "Operation":
528
529 self.socket_router = self.sockIO()
530
531 self.runOp()
351
532
352 """
533 else:
353 if not self.isConfig:
534 raise ValueError("Unknown type")
354 self.setup(**kwargs)
355
356 raise NotImplementedError
357
358 def close(self):
359
535
360 pass No newline at end of file
536 return 0
537
538 return MPClass No newline at end of file
@@ -1,953 +1,966
1 import itertools
1 import itertools
2
2
3 import numpy
3 import numpy
4
4
5 from .jroproc_base import ProcessingUnit, Operation
5 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation
6 from schainpy.model.data.jrodata import Spectra
6 from schainpy.model.data.jrodata import Spectra
7 from schainpy.model.data.jrodata import hildebrand_sekhon
7 from schainpy.model.data.jrodata import hildebrand_sekhon
8 from schainpy.utils import log #yong
8 from schainpy.utils import log
9
9
10 @MPDecorator
10 class SpectraProc(ProcessingUnit):
11 class SpectraProc(ProcessingUnit):
11
12
12 def __init__(self, **kwargs):
13 METHODS = {'selectHeights' : ['minHei', 'maxHei'],
14 'selectChannels' : 'channelList',
15 'selectChannelsByIndex': 'channelIndexList',
16 'getBeaconSignal' : ['tauindex', 'channelindex', 'hei_ref'],
17 'selectHeightsByIndex' : ['minIndex', 'maxIndex']
18 }
13
19
14 ProcessingUnit.__init__(self, **kwargs)
20 def __init__(self):#, **kwargs):
21
22 ProcessingUnit.__init__(self)#, **kwargs)
15
23
16 self.buffer = None
24 self.buffer = None
17 self.firstdatatime = None
25 self.firstdatatime = None
18 self.profIndex = 0
26 self.profIndex = 0
19 self.dataOut = Spectra()
27 self.dataOut = Spectra()
20 self.id_min = None
28 self.id_min = None
21 self.id_max = None
29 self.id_max = None
30 self.setupReq = False #Agregar a todas las unidades de proc
22
31
23 def __updateSpecFromVoltage(self):
32 def __updateSpecFromVoltage(self):
24
33
25 self.dataOut.timeZone = self.dataIn.timeZone
34 self.dataOut.timeZone = self.dataIn.timeZone
26 self.dataOut.dstFlag = self.dataIn.dstFlag
35 self.dataOut.dstFlag = self.dataIn.dstFlag
27 self.dataOut.errorCount = self.dataIn.errorCount
36 self.dataOut.errorCount = self.dataIn.errorCount
28 self.dataOut.useLocalTime = self.dataIn.useLocalTime
37 self.dataOut.useLocalTime = self.dataIn.useLocalTime
29 try:
38 try:
30 self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy()
39 self.dataOut.processingHeaderObj = self.dataIn.processingHeaderObj.copy()
31 except:
40 except:
32 pass
41 pass
33 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
42 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
34 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
43 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
35 self.dataOut.channelList = self.dataIn.channelList
44 self.dataOut.channelList = self.dataIn.channelList
36 self.dataOut.heightList = self.dataIn.heightList
45 self.dataOut.heightList = self.dataIn.heightList
37 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
46 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
38
47
39 self.dataOut.nBaud = self.dataIn.nBaud
48 self.dataOut.nBaud = self.dataIn.nBaud
40 self.dataOut.nCode = self.dataIn.nCode
49 self.dataOut.nCode = self.dataIn.nCode
41 self.dataOut.code = self.dataIn.code
50 self.dataOut.code = self.dataIn.code
42 self.dataOut.nProfiles = self.dataOut.nFFTPoints
51 self.dataOut.nProfiles = self.dataOut.nFFTPoints
43
52
44 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
53 self.dataOut.flagDiscontinuousBlock = self.dataIn.flagDiscontinuousBlock
45 self.dataOut.utctime = self.firstdatatime
54 self.dataOut.utctime = self.firstdatatime
46 # asumo q la data esta decodificada
55 # asumo q la data esta decodificada
47 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData
56 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData
48 # asumo q la data esta sin flip
57 # asumo q la data esta sin flip
49 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData
58 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData
50 self.dataOut.flagShiftFFT = False
59 self.dataOut.flagShiftFFT = False
51
60
52 self.dataOut.nCohInt = self.dataIn.nCohInt
61 self.dataOut.nCohInt = self.dataIn.nCohInt
53 self.dataOut.nIncohInt = 1
62 self.dataOut.nIncohInt = 1
54
63
55 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
64 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
56
65
57 self.dataOut.frequency = self.dataIn.frequency
66 self.dataOut.frequency = self.dataIn.frequency
58 self.dataOut.realtime = self.dataIn.realtime
67 self.dataOut.realtime = self.dataIn.realtime
59
68
60 self.dataOut.azimuth = self.dataIn.azimuth
69 self.dataOut.azimuth = self.dataIn.azimuth
61 self.dataOut.zenith = self.dataIn.zenith
70 self.dataOut.zenith = self.dataIn.zenith
62
71
63 self.dataOut.beam.codeList = self.dataIn.beam.codeList
72 self.dataOut.beam.codeList = self.dataIn.beam.codeList
64 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
73 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
65 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
74 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
66
75
67 def __getFft(self):
76 def __getFft(self):
68 """
77 """
69 Convierte valores de Voltaje a Spectra
78 Convierte valores de Voltaje a Spectra
70
79
71 Affected:
80 Affected:
72 self.dataOut.data_spc
81 self.dataOut.data_spc
73 self.dataOut.data_cspc
82 self.dataOut.data_cspc
74 self.dataOut.data_dc
83 self.dataOut.data_dc
75 self.dataOut.heightList
84 self.dataOut.heightList
76 self.profIndex
85 self.profIndex
77 self.buffer
86 self.buffer
78 self.dataOut.flagNoData
87 self.dataOut.flagNoData
79 """
88 """
80 fft_volt = numpy.fft.fft(
89 fft_volt = numpy.fft.fft(
81 self.buffer, n=self.dataOut.nFFTPoints, axis=1)
90 self.buffer, n=self.dataOut.nFFTPoints, axis=1)
82 fft_volt = fft_volt.astype(numpy.dtype('complex'))
91 fft_volt = fft_volt.astype(numpy.dtype('complex'))
83 dc = fft_volt[:, 0, :]
92 dc = fft_volt[:, 0, :]
84
93
85 # calculo de self-spectra
94 # calculo de self-spectra
86 fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,))
95 fft_volt = numpy.fft.fftshift(fft_volt, axes=(1,))
87 spc = fft_volt * numpy.conjugate(fft_volt)
96 spc = fft_volt * numpy.conjugate(fft_volt)
88 spc = spc.real
97 spc = spc.real
89
98
90 blocksize = 0
99 blocksize = 0
91 blocksize += dc.size
100 blocksize += dc.size
92 blocksize += spc.size
101 blocksize += spc.size
93
102
94 cspc = None
103 cspc = None
95 pairIndex = 0
104 pairIndex = 0
96 if self.dataOut.pairsList != None:
105 if self.dataOut.pairsList != None:
97 # calculo de cross-spectra
106 # calculo de cross-spectra
98 cspc = numpy.zeros(
107 cspc = numpy.zeros(
99 (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
108 (self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
100 for pair in self.dataOut.pairsList:
109 for pair in self.dataOut.pairsList:
101 if pair[0] not in self.dataOut.channelList:
110 if pair[0] not in self.dataOut.channelList:
102 raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (
111 raise ValueError("Error getting CrossSpectra: pair 0 of %s is not in channelList = %s" % (
103 str(pair), str(self.dataOut.channelList)))
112 str(pair), str(self.dataOut.channelList)))
104 if pair[1] not in self.dataOut.channelList:
113 if pair[1] not in self.dataOut.channelList:
105 raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (
114 raise ValueError("Error getting CrossSpectra: pair 1 of %s is not in channelList = %s" % (
106 str(pair), str(self.dataOut.channelList)))
115 str(pair), str(self.dataOut.channelList)))
107
116
108 cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \
117 cspc[pairIndex, :, :] = fft_volt[pair[0], :, :] * \
109 numpy.conjugate(fft_volt[pair[1], :, :])
118 numpy.conjugate(fft_volt[pair[1], :, :])
110 pairIndex += 1
119 pairIndex += 1
111 blocksize += cspc.size
120 blocksize += cspc.size
112
121
113 self.dataOut.data_spc = spc
122 self.dataOut.data_spc = spc
114 self.dataOut.data_cspc = cspc
123 self.dataOut.data_cspc = cspc
115 self.dataOut.data_dc = dc
124 self.dataOut.data_dc = dc
116 self.dataOut.blockSize = blocksize
125 self.dataOut.blockSize = blocksize
117 self.dataOut.flagShiftFFT = True
126 self.dataOut.flagShiftFFT = True
118
127
119 def run(self, nProfiles=None, nFFTPoints=None, pairsList=[], ippFactor=None, shift_fft=False):
128 def run(self, nProfiles=None, nFFTPoints=None, pairsList=[], ippFactor=None, shift_fft=False):
120
129
121 self.dataOut.flagNoData = True
130 self.dataOut.flagNoData = True
122
131
123 if self.dataIn.type == "Spectra":
132 if self.dataIn.type == "Spectra":
124 self.dataOut.copy(self.dataIn)
133 self.dataOut.copy(self.dataIn)
125 # if not pairsList:
134 # if not pairsList:
126 # pairsList = itertools.combinations(self.dataOut.channelList, 2)
135 # pairsList = itertools.combinations(self.dataOut.channelList, 2)
127 # if self.dataOut.data_cspc is not None:
136 # if self.dataOut.data_cspc is not None:
128 # self.__selectPairs(pairsList)
137 # self.__selectPairs(pairsList)
129 if shift_fft:
138 if shift_fft:
130 #desplaza a la derecha en el eje 2 determinadas posiciones
139 #desplaza a la derecha en el eje 2 determinadas posiciones
131 shift = int(self.dataOut.nFFTPoints/2)
140 shift = int(self.dataOut.nFFTPoints/2)
132 self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1)
141 self.dataOut.data_spc = numpy.roll(self.dataOut.data_spc, shift , axis=1)
133
142
134 if self.dataOut.data_cspc is not None:
143 if self.dataOut.data_cspc is not None:
135 #desplaza a la derecha en el eje 2 determinadas posiciones
144 #desplaza a la derecha en el eje 2 determinadas posiciones
136 self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1)
145 self.dataOut.data_cspc = numpy.roll(self.dataOut.data_cspc, shift, axis=1)
137
146
138 return True
147 return True
139
148
140 if self.dataIn.type == "Voltage":
149 if self.dataIn.type == "Voltage":
141
150
142 if nFFTPoints == None:
151 if nFFTPoints == None:
143 raise ValueError("This SpectraProc.run() need nFFTPoints input variable")
152 raise ValueError("This SpectraProc.run() need nFFTPoints input variable")
144
153
145 if nProfiles == None:
154 if nProfiles == None:
146 nProfiles = nFFTPoints
155 nProfiles = nFFTPoints
147
156
148 if ippFactor == None:
157 if ippFactor == None:
149 ippFactor = 1
158 ippFactor = 1
150
159
151 self.dataOut.ippFactor = ippFactor
160 self.dataOut.ippFactor = ippFactor
152
161
153 self.dataOut.nFFTPoints = nFFTPoints
162 self.dataOut.nFFTPoints = nFFTPoints
154 self.dataOut.pairsList = pairsList
163 self.dataOut.pairsList = pairsList
155
164
156 if self.buffer is None:
165 if self.buffer is None:
157 self.buffer = numpy.zeros((self.dataIn.nChannels,
166 self.buffer = numpy.zeros((self.dataIn.nChannels,
158 nProfiles,
167 nProfiles,
159 self.dataIn.nHeights),
168 self.dataIn.nHeights),
160 dtype='complex')
169 dtype='complex')
161
170
162 if self.dataIn.flagDataAsBlock:
171 if self.dataIn.flagDataAsBlock:
163 # data dimension: [nChannels, nProfiles, nSamples]
172 # data dimension: [nChannels, nProfiles, nSamples]
164 nVoltProfiles = self.dataIn.data.shape[1]
173 nVoltProfiles = self.dataIn.data.shape[1]
165 # nVoltProfiles = self.dataIn.nProfiles
174 # nVoltProfiles = self.dataIn.nProfiles
166
175
167 if nVoltProfiles == nProfiles:
176 if nVoltProfiles == nProfiles:
168 self.buffer = self.dataIn.data.copy()
177 self.buffer = self.dataIn.data.copy()
169 self.profIndex = nVoltProfiles
178 self.profIndex = nVoltProfiles
170
179
171 elif nVoltProfiles < nProfiles:
180 elif nVoltProfiles < nProfiles:
172
181
173 if self.profIndex == 0:
182 if self.profIndex == 0:
174 self.id_min = 0
183 self.id_min = 0
175 self.id_max = nVoltProfiles
184 self.id_max = nVoltProfiles
176
185
177 self.buffer[:, self.id_min:self.id_max,
186 self.buffer[:, self.id_min:self.id_max,
178 :] = self.dataIn.data
187 :] = self.dataIn.data
179 self.profIndex += nVoltProfiles
188 self.profIndex += nVoltProfiles
180 self.id_min += nVoltProfiles
189 self.id_min += nVoltProfiles
181 self.id_max += nVoltProfiles
190 self.id_max += nVoltProfiles
182 else:
191 else:
183 raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % (
192 raise ValueError("The type object %s has %d profiles, it should just has %d profiles" % (
184 self.dataIn.type, self.dataIn.data.shape[1], nProfiles))
193 self.dataIn.type, self.dataIn.data.shape[1], nProfiles))
185 self.dataOut.flagNoData = True
194 self.dataOut.flagNoData = True
186 return 0
195 return 0
187 else:
196 else:
188 self.buffer[:, self.profIndex, :] = self.dataIn.data.copy()
197 self.buffer[:, self.profIndex, :] = self.dataIn.data.copy()
189 self.profIndex += 1
198 self.profIndex += 1
190
199
191 if self.firstdatatime == None:
200 if self.firstdatatime == None:
192 self.firstdatatime = self.dataIn.utctime
201 self.firstdatatime = self.dataIn.utctime
193
202
194 if self.profIndex == nProfiles:
203 if self.profIndex == nProfiles:
195 self.__updateSpecFromVoltage()
204 self.__updateSpecFromVoltage()
196 self.__getFft()
205 self.__getFft()
197
206
198 self.dataOut.flagNoData = False
207 self.dataOut.flagNoData = False
199 self.firstdatatime = None
208 self.firstdatatime = None
200 self.profIndex = 0
209 self.profIndex = 0
201
210
202 return True
211 return True
203
212
204 raise ValueError("The type of input object '%s' is not valid" % (
213 raise ValueError("The type of input object '%s' is not valid" % (
205 self.dataIn.type))
214 self.dataIn.type))
206
215
207 def __selectPairs(self, pairsList):
216 def __selectPairs(self, pairsList):
208
217
209 if not pairsList:
218 if not pairsList:
210 return
219 return
211
220
212 pairs = []
221 pairs = []
213 pairsIndex = []
222 pairsIndex = []
214
223
215 for pair in pairsList:
224 for pair in pairsList:
216 if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList:
225 if pair[0] not in self.dataOut.channelList or pair[1] not in self.dataOut.channelList:
217 continue
226 continue
218 pairs.append(pair)
227 pairs.append(pair)
219 pairsIndex.append(pairs.index(pair))
228 pairsIndex.append(pairs.index(pair))
220
229
221 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex]
230 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndex]
222 self.dataOut.pairsList = pairs
231 self.dataOut.pairsList = pairs
223
232
224 return
233 return
225
234
226 def __selectPairsByChannel(self, channelList=None):
235 def __selectPairsByChannel(self, channelList=None):
227
236
228 if channelList == None:
237 if channelList == None:
229 return
238 return
230
239
231 pairsIndexListSelected = []
240 pairsIndexListSelected = []
232 for pairIndex in self.dataOut.pairsIndexList:
241 for pairIndex in self.dataOut.pairsIndexList:
233 # First pair
242 # First pair
234 if self.dataOut.pairsList[pairIndex][0] not in channelList:
243 if self.dataOut.pairsList[pairIndex][0] not in channelList:
235 continue
244 continue
236 # Second pair
245 # Second pair
237 if self.dataOut.pairsList[pairIndex][1] not in channelList:
246 if self.dataOut.pairsList[pairIndex][1] not in channelList:
238 continue
247 continue
239
248
240 pairsIndexListSelected.append(pairIndex)
249 pairsIndexListSelected.append(pairIndex)
241
250
242 if not pairsIndexListSelected:
251 if not pairsIndexListSelected:
243 self.dataOut.data_cspc = None
252 self.dataOut.data_cspc = None
244 self.dataOut.pairsList = []
253 self.dataOut.pairsList = []
245 return
254 return
246
255
247 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected]
256 self.dataOut.data_cspc = self.dataOut.data_cspc[pairsIndexListSelected]
248 self.dataOut.pairsList = [self.dataOut.pairsList[i]
257 self.dataOut.pairsList = [self.dataOut.pairsList[i]
249 for i in pairsIndexListSelected]
258 for i in pairsIndexListSelected]
250
259
251 return
260 return
252
261
253 def selectChannels(self, channelList):
262 def selectChannels(self, channelList):
254
263
255 channelIndexList = []
264 channelIndexList = []
256
265
257 for channel in channelList:
266 for channel in channelList:
258 if channel not in self.dataOut.channelList:
267 if channel not in self.dataOut.channelList:
259 raise ValueError("Error selecting channels, Channel %d is not valid.\nAvailable channels = %s" % (
268 raise ValueError("Error selecting channels, Channel %d is not valid.\nAvailable channels = %s" % (
260 channel, str(self.dataOut.channelList)))
269 channel, str(self.dataOut.channelList)))
261
270
262 index = self.dataOut.channelList.index(channel)
271 index = self.dataOut.channelList.index(channel)
263 channelIndexList.append(index)
272 channelIndexList.append(index)
264
273
265 self.selectChannelsByIndex(channelIndexList)
274 self.selectChannelsByIndex(channelIndexList)
266
275
267 def selectChannelsByIndex(self, channelIndexList):
276 def selectChannelsByIndex(self, channelIndexList):
268 """
277 """
269 Selecciona un bloque de datos en base a canales segun el channelIndexList
278 Selecciona un bloque de datos en base a canales segun el channelIndexList
270
279
271 Input:
280 Input:
272 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
281 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
273
282
274 Affected:
283 Affected:
275 self.dataOut.data_spc
284 self.dataOut.data_spc
276 self.dataOut.channelIndexList
285 self.dataOut.channelIndexList
277 self.dataOut.nChannels
286 self.dataOut.nChannels
278
287
279 Return:
288 Return:
280 None
289 None
281 """
290 """
282
291
283 for channelIndex in channelIndexList:
292 for channelIndex in channelIndexList:
284 if channelIndex not in self.dataOut.channelIndexList:
293 if channelIndex not in self.dataOut.channelIndexList:
285 raise ValueError("Error selecting channels: The value %d in channelIndexList is not valid.\nAvailable channel indexes = " % (
294 raise ValueError("Error selecting channels: The value %d in channelIndexList is not valid.\nAvailable channel indexes = " % (
286 channelIndex, self.dataOut.channelIndexList))
295 channelIndex, self.dataOut.channelIndexList))
287
296
288 # nChannels = len(channelIndexList)
297 # nChannels = len(channelIndexList)
289
298
290 data_spc = self.dataOut.data_spc[channelIndexList, :]
299 data_spc = self.dataOut.data_spc[channelIndexList, :]
291 data_dc = self.dataOut.data_dc[channelIndexList, :]
300 data_dc = self.dataOut.data_dc[channelIndexList, :]
292
301
293 self.dataOut.data_spc = data_spc
302 self.dataOut.data_spc = data_spc
294 self.dataOut.data_dc = data_dc
303 self.dataOut.data_dc = data_dc
295
304
296 self.dataOut.channelList = [
305 self.dataOut.channelList = [
297 self.dataOut.channelList[i] for i in channelIndexList]
306 self.dataOut.channelList[i] for i in channelIndexList]
298 # self.dataOut.nChannels = nChannels
307 # self.dataOut.nChannels = nChannels
299
308
300 self.__selectPairsByChannel(self.dataOut.channelList)
309 self.__selectPairsByChannel(self.dataOut.channelList)
301
310
302 return 1
311 return 1
303
312
304 def selectHeights(self, minHei, maxHei):
313 def selectHeights(self, minHei, maxHei):
305 """
314 """
306 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
315 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
307 minHei <= height <= maxHei
316 minHei <= height <= maxHei
308
317
309 Input:
318 Input:
310 minHei : valor minimo de altura a considerar
319 minHei : valor minimo de altura a considerar
311 maxHei : valor maximo de altura a considerar
320 maxHei : valor maximo de altura a considerar
312
321
313 Affected:
322 Affected:
314 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
323 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
315
324
316 Return:
325 Return:
317 1 si el metodo se ejecuto con exito caso contrario devuelve 0
326 1 si el metodo se ejecuto con exito caso contrario devuelve 0
318 """
327 """
319
328
320 if (minHei > maxHei):
329 if (minHei > maxHei):
321 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (
330 raise ValueError("Error selecting heights: Height range (%d,%d) is not valid" % (
322 minHei, maxHei))
331 minHei, maxHei))
323
332
324 if (minHei < self.dataOut.heightList[0]):
333 if (minHei < self.dataOut.heightList[0]):
325 minHei = self.dataOut.heightList[0]
334 minHei = self.dataOut.heightList[0]
326
335
327 if (maxHei > self.dataOut.heightList[-1]):
336 if (maxHei > self.dataOut.heightList[-1]):
328 maxHei = self.dataOut.heightList[-1]
337 maxHei = self.dataOut.heightList[-1]
329
338
330 minIndex = 0
339 minIndex = 0
331 maxIndex = 0
340 maxIndex = 0
332 heights = self.dataOut.heightList
341 heights = self.dataOut.heightList
333
342
334 inda = numpy.where(heights >= minHei)
343 inda = numpy.where(heights >= minHei)
335 indb = numpy.where(heights <= maxHei)
344 indb = numpy.where(heights <= maxHei)
336
345
337 try:
346 try:
338 minIndex = inda[0][0]
347 minIndex = inda[0][0]
339 except:
348 except:
340 minIndex = 0
349 minIndex = 0
341
350
342 try:
351 try:
343 maxIndex = indb[0][-1]
352 maxIndex = indb[0][-1]
344 except:
353 except:
345 maxIndex = len(heights)
354 maxIndex = len(heights)
346
355
347 self.selectHeightsByIndex(minIndex, maxIndex)
356 self.selectHeightsByIndex(minIndex, maxIndex)
348
357
349 return 1
358 return 1
350
359
351 def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None):
360 def getBeaconSignal(self, tauindex=0, channelindex=0, hei_ref=None):
352 newheis = numpy.where(
361 newheis = numpy.where(
353 self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex])
362 self.dataOut.heightList > self.dataOut.radarControllerHeaderObj.Taus[tauindex])
354
363
355 if hei_ref != None:
364 if hei_ref != None:
356 newheis = numpy.where(self.dataOut.heightList > hei_ref)
365 newheis = numpy.where(self.dataOut.heightList > hei_ref)
357
366
358 minIndex = min(newheis[0])
367 minIndex = min(newheis[0])
359 maxIndex = max(newheis[0])
368 maxIndex = max(newheis[0])
360 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
369 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
361 heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
370 heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
362
371
363 # determina indices
372 # determina indices
364 nheis = int(self.dataOut.radarControllerHeaderObj.txB /
373 nheis = int(self.dataOut.radarControllerHeaderObj.txB /
365 (self.dataOut.heightList[1] - self.dataOut.heightList[0]))
374 (self.dataOut.heightList[1] - self.dataOut.heightList[0]))
366 avg_dB = 10 * \
375 avg_dB = 10 * \
367 numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0))
376 numpy.log10(numpy.sum(data_spc[channelindex, :, :], axis=0))
368 beacon_dB = numpy.sort(avg_dB)[-nheis:]
377 beacon_dB = numpy.sort(avg_dB)[-nheis:]
369 beacon_heiIndexList = []
378 beacon_heiIndexList = []
370 for val in avg_dB.tolist():
379 for val in avg_dB.tolist():
371 if val >= beacon_dB[0]:
380 if val >= beacon_dB[0]:
372 beacon_heiIndexList.append(avg_dB.tolist().index(val))
381 beacon_heiIndexList.append(avg_dB.tolist().index(val))
373
382
374 #data_spc = data_spc[:,:,beacon_heiIndexList]
383 #data_spc = data_spc[:,:,beacon_heiIndexList]
375 data_cspc = None
384 data_cspc = None
376 if self.dataOut.data_cspc is not None:
385 if self.dataOut.data_cspc is not None:
377 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
386 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
378 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
387 #data_cspc = data_cspc[:,:,beacon_heiIndexList]
379
388
380 data_dc = None
389 data_dc = None
381 if self.dataOut.data_dc is not None:
390 if self.dataOut.data_dc is not None:
382 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
391 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
383 #data_dc = data_dc[:,beacon_heiIndexList]
392 #data_dc = data_dc[:,beacon_heiIndexList]
384
393
385 self.dataOut.data_spc = data_spc
394 self.dataOut.data_spc = data_spc
386 self.dataOut.data_cspc = data_cspc
395 self.dataOut.data_cspc = data_cspc
387 self.dataOut.data_dc = data_dc
396 self.dataOut.data_dc = data_dc
388 self.dataOut.heightList = heightList
397 self.dataOut.heightList = heightList
389 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
398 self.dataOut.beacon_heiIndexList = beacon_heiIndexList
390
399
391 return 1
400 return 1
392
401
393 def selectHeightsByIndex(self, minIndex, maxIndex):
402 def selectHeightsByIndex(self, minIndex, maxIndex):
394 """
403 """
395 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
404 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
396 minIndex <= index <= maxIndex
405 minIndex <= index <= maxIndex
397
406
398 Input:
407 Input:
399 minIndex : valor de indice minimo de altura a considerar
408 minIndex : valor de indice minimo de altura a considerar
400 maxIndex : valor de indice maximo de altura a considerar
409 maxIndex : valor de indice maximo de altura a considerar
401
410
402 Affected:
411 Affected:
403 self.dataOut.data_spc
412 self.dataOut.data_spc
404 self.dataOut.data_cspc
413 self.dataOut.data_cspc
405 self.dataOut.data_dc
414 self.dataOut.data_dc
406 self.dataOut.heightList
415 self.dataOut.heightList
407
416
408 Return:
417 Return:
409 1 si el metodo se ejecuto con exito caso contrario devuelve 0
418 1 si el metodo se ejecuto con exito caso contrario devuelve 0
410 """
419 """
411
420
412 if (minIndex < 0) or (minIndex > maxIndex):
421 if (minIndex < 0) or (minIndex > maxIndex):
413 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (
422 raise ValueError("Error selecting heights: Index range (%d,%d) is not valid" % (
414 minIndex, maxIndex))
423 minIndex, maxIndex))
415
424
416 if (maxIndex >= self.dataOut.nHeights):
425 if (maxIndex >= self.dataOut.nHeights):
417 maxIndex = self.dataOut.nHeights - 1
426 maxIndex = self.dataOut.nHeights - 1
418
427
419 # Spectra
428 # Spectra
420 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
429 data_spc = self.dataOut.data_spc[:, :, minIndex:maxIndex + 1]
421
430
422 data_cspc = None
431 data_cspc = None
423 if self.dataOut.data_cspc is not None:
432 if self.dataOut.data_cspc is not None:
424 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
433 data_cspc = self.dataOut.data_cspc[:, :, minIndex:maxIndex + 1]
425
434
426 data_dc = None
435 data_dc = None
427 if self.dataOut.data_dc is not None:
436 if self.dataOut.data_dc is not None:
428 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
437 data_dc = self.dataOut.data_dc[:, minIndex:maxIndex + 1]
429
438
430 self.dataOut.data_spc = data_spc
439 self.dataOut.data_spc = data_spc
431 self.dataOut.data_cspc = data_cspc
440 self.dataOut.data_cspc = data_cspc
432 self.dataOut.data_dc = data_dc
441 self.dataOut.data_dc = data_dc
433
442
434 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
443 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex + 1]
435
444
436 return 1
445 return 1
437
446
438 def removeDC(self, mode=2):
447 def removeDC(self, mode=2):
439 jspectra = self.dataOut.data_spc
448 jspectra = self.dataOut.data_spc
440 jcspectra = self.dataOut.data_cspc
449 jcspectra = self.dataOut.data_cspc
441
450
442 num_chan = jspectra.shape[0]
451 num_chan = jspectra.shape[0]
443 num_hei = jspectra.shape[2]
452 num_hei = jspectra.shape[2]
444
453
445 if jcspectra is not None:
454 if jcspectra is not None:
446 jcspectraExist = True
455 jcspectraExist = True
447 num_pairs = jcspectra.shape[0]
456 num_pairs = jcspectra.shape[0]
448 else:
457 else:
449 jcspectraExist = False
458 jcspectraExist = False
450
459
451 freq_dc = int(jspectra.shape[1] / 2)
460 freq_dc = int(jspectra.shape[1] / 2)
452 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
461 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
453 ind_vel = ind_vel.astype(int)
462 ind_vel = ind_vel.astype(int)
454
463
455 if ind_vel[0] < 0:
464 if ind_vel[0] < 0:
456 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
465 ind_vel[list(range(0, 1))] = ind_vel[list(range(0, 1))] + self.num_prof
457
466
458 if mode == 1:
467 if mode == 1:
459 jspectra[:, freq_dc, :] = (
468 jspectra[:, freq_dc, :] = (
460 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
469 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
461
470
462 if jcspectraExist:
471 if jcspectraExist:
463 jcspectra[:, freq_dc, :] = (
472 jcspectra[:, freq_dc, :] = (
464 jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2
473 jcspectra[:, ind_vel[1], :] + jcspectra[:, ind_vel[2], :]) / 2
465
474
466 if mode == 2:
475 if mode == 2:
467
476
468 vel = numpy.array([-2, -1, 1, 2])
477 vel = numpy.array([-2, -1, 1, 2])
469 xx = numpy.zeros([4, 4])
478 xx = numpy.zeros([4, 4])
470
479
471 for fil in range(4):
480 for fil in range(4):
472 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
481 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
473
482
474 xx_inv = numpy.linalg.inv(xx)
483 xx_inv = numpy.linalg.inv(xx)
475 xx_aux = xx_inv[0, :]
484 xx_aux = xx_inv[0, :]
476
485
477 for ich in range(num_chan):
486 for ich in range(num_chan):
478 yy = jspectra[ich, ind_vel, :]
487 yy = jspectra[ich, ind_vel, :]
479 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
488 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
480
489
481 junkid = jspectra[ich, freq_dc, :] <= 0
490 junkid = jspectra[ich, freq_dc, :] <= 0
482 cjunkid = sum(junkid)
491 cjunkid = sum(junkid)
483
492
484 if cjunkid.any():
493 if cjunkid.any():
485 jspectra[ich, freq_dc, junkid.nonzero()] = (
494 jspectra[ich, freq_dc, junkid.nonzero()] = (
486 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
495 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
487
496
488 if jcspectraExist:
497 if jcspectraExist:
489 for ip in range(num_pairs):
498 for ip in range(num_pairs):
490 yy = jcspectra[ip, ind_vel, :]
499 yy = jcspectra[ip, ind_vel, :]
491 jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy)
500 jcspectra[ip, freq_dc, :] = numpy.dot(xx_aux, yy)
492
501
493 self.dataOut.data_spc = jspectra
502 self.dataOut.data_spc = jspectra
494 self.dataOut.data_cspc = jcspectra
503 self.dataOut.data_cspc = jcspectra
495
504
496 return 1
505 return 1
497
506
498 def removeInterference(self, interf=2, hei_interf=None, nhei_interf=None, offhei_interf=None):
507 def removeInterference(self, interf=2, hei_interf=None, nhei_interf=None, offhei_interf=None):
499
508
500 jspectra = self.dataOut.data_spc
509 jspectra = self.dataOut.data_spc
501 jcspectra = self.dataOut.data_cspc
510 jcspectra = self.dataOut.data_cspc
502 jnoise = self.dataOut.getNoise()
511 jnoise = self.dataOut.getNoise()
503 num_incoh = self.dataOut.nIncohInt
512 num_incoh = self.dataOut.nIncohInt
504
513
505 num_channel = jspectra.shape[0]
514 num_channel = jspectra.shape[0]
506 num_prof = jspectra.shape[1]
515 num_prof = jspectra.shape[1]
507 num_hei = jspectra.shape[2]
516 num_hei = jspectra.shape[2]
508
517
509 # hei_interf
518 # hei_interf
510 if hei_interf is None:
519 if hei_interf is None:
511 count_hei = num_hei / 2 # Como es entero no importa
520 count_hei = num_hei / 2 # Como es entero no importa
512 hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei
521 hei_interf = numpy.asmatrix(list(range(count_hei))) + num_hei - count_hei
513 hei_interf = numpy.asarray(hei_interf)[0]
522 hei_interf = numpy.asarray(hei_interf)[0]
514 # nhei_interf
523 # nhei_interf
515 if (nhei_interf == None):
524 if (nhei_interf == None):
516 nhei_interf = 5
525 nhei_interf = 5
517 if (nhei_interf < 1):
526 if (nhei_interf < 1):
518 nhei_interf = 1
527 nhei_interf = 1
519 if (nhei_interf > count_hei):
528 if (nhei_interf > count_hei):
520 nhei_interf = count_hei
529 nhei_interf = count_hei
521 if (offhei_interf == None):
530 if (offhei_interf == None):
522 offhei_interf = 0
531 offhei_interf = 0
523
532
524 ind_hei = list(range(num_hei))
533 ind_hei = list(range(num_hei))
525 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
534 # mask_prof = numpy.asarray(range(num_prof - 2)) + 1
526 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
535 # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1
527 mask_prof = numpy.asarray(list(range(num_prof)))
536 mask_prof = numpy.asarray(list(range(num_prof)))
528 num_mask_prof = mask_prof.size
537 num_mask_prof = mask_prof.size
529 comp_mask_prof = [0, num_prof / 2]
538 comp_mask_prof = [0, num_prof / 2]
530
539
531 # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
540 # noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal
532 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
541 if (jnoise.size < num_channel or numpy.isnan(jnoise).any()):
533 jnoise = numpy.nan
542 jnoise = numpy.nan
534 noise_exist = jnoise[0] < numpy.Inf
543 noise_exist = jnoise[0] < numpy.Inf
535
544
536 # Subrutina de Remocion de la Interferencia
545 # Subrutina de Remocion de la Interferencia
537 for ich in range(num_channel):
546 for ich in range(num_channel):
538 # Se ordena los espectros segun su potencia (menor a mayor)
547 # Se ordena los espectros segun su potencia (menor a mayor)
539 power = jspectra[ich, mask_prof, :]
548 power = jspectra[ich, mask_prof, :]
540 power = power[:, hei_interf]
549 power = power[:, hei_interf]
541 power = power.sum(axis=0)
550 power = power.sum(axis=0)
542 psort = power.ravel().argsort()
551 psort = power.ravel().argsort()
543
552
544 # Se estima la interferencia promedio en los Espectros de Potencia empleando
553 # Se estima la interferencia promedio en los Espectros de Potencia empleando
545 junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(
554 junkspc_interf = jspectra[ich, :, hei_interf[psort[list(range(
546 offhei_interf, nhei_interf + offhei_interf))]]]
555 offhei_interf, nhei_interf + offhei_interf))]]]
547
556
548 if noise_exist:
557 if noise_exist:
549 # tmp_noise = jnoise[ich] / num_prof
558 # tmp_noise = jnoise[ich] / num_prof
550 tmp_noise = jnoise[ich]
559 tmp_noise = jnoise[ich]
551 junkspc_interf = junkspc_interf - tmp_noise
560 junkspc_interf = junkspc_interf - tmp_noise
552 #junkspc_interf[:,comp_mask_prof] = 0
561 #junkspc_interf[:,comp_mask_prof] = 0
553
562
554 jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf
563 jspc_interf = junkspc_interf.sum(axis=0) / nhei_interf
555 jspc_interf = jspc_interf.transpose()
564 jspc_interf = jspc_interf.transpose()
556 # Calculando el espectro de interferencia promedio
565 # Calculando el espectro de interferencia promedio
557 noiseid = numpy.where(
566 noiseid = numpy.where(
558 jspc_interf <= tmp_noise / numpy.sqrt(num_incoh))
567 jspc_interf <= tmp_noise / numpy.sqrt(num_incoh))
559 noiseid = noiseid[0]
568 noiseid = noiseid[0]
560 cnoiseid = noiseid.size
569 cnoiseid = noiseid.size
561 interfid = numpy.where(
570 interfid = numpy.where(
562 jspc_interf > tmp_noise / numpy.sqrt(num_incoh))
571 jspc_interf > tmp_noise / numpy.sqrt(num_incoh))
563 interfid = interfid[0]
572 interfid = interfid[0]
564 cinterfid = interfid.size
573 cinterfid = interfid.size
565
574
566 if (cnoiseid > 0):
575 if (cnoiseid > 0):
567 jspc_interf[noiseid] = 0
576 jspc_interf[noiseid] = 0
568
577
569 # Expandiendo los perfiles a limpiar
578 # Expandiendo los perfiles a limpiar
570 if (cinterfid > 0):
579 if (cinterfid > 0):
571 new_interfid = (
580 new_interfid = (
572 numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof
581 numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof) % num_prof
573 new_interfid = numpy.asarray(new_interfid)
582 new_interfid = numpy.asarray(new_interfid)
574 new_interfid = {x for x in new_interfid}
583 new_interfid = {x for x in new_interfid}
575 new_interfid = numpy.array(list(new_interfid))
584 new_interfid = numpy.array(list(new_interfid))
576 new_cinterfid = new_interfid.size
585 new_cinterfid = new_interfid.size
577 else:
586 else:
578 new_cinterfid = 0
587 new_cinterfid = 0
579
588
580 for ip in range(new_cinterfid):
589 for ip in range(new_cinterfid):
581 ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort()
590 ind = junkspc_interf[:, new_interfid[ip]].ravel().argsort()
582 jspc_interf[new_interfid[ip]
591 jspc_interf[new_interfid[ip]
583 ] = junkspc_interf[ind[nhei_interf / 2], new_interfid[ip]]
592 ] = junkspc_interf[ind[nhei_interf / 2], new_interfid[ip]]
584
593
585 jspectra[ich, :, ind_hei] = jspectra[ich, :,
594 jspectra[ich, :, ind_hei] = jspectra[ich, :,
586 ind_hei] - jspc_interf # Corregir indices
595 ind_hei] - jspc_interf # Corregir indices
587
596
588 # Removiendo la interferencia del punto de mayor interferencia
597 # Removiendo la interferencia del punto de mayor interferencia
589 ListAux = jspc_interf[mask_prof].tolist()
598 ListAux = jspc_interf[mask_prof].tolist()
590 maxid = ListAux.index(max(ListAux))
599 maxid = ListAux.index(max(ListAux))
591
600
592 if cinterfid > 0:
601 if cinterfid > 0:
593 for ip in range(cinterfid * (interf == 2) - 1):
602 for ip in range(cinterfid * (interf == 2) - 1):
594 ind = (jspectra[ich, interfid[ip], :] < tmp_noise *
603 ind = (jspectra[ich, interfid[ip], :] < tmp_noise *
595 (1 + 1 / numpy.sqrt(num_incoh))).nonzero()
604 (1 + 1 / numpy.sqrt(num_incoh))).nonzero()
596 cind = len(ind)
605 cind = len(ind)
597
606
598 if (cind > 0):
607 if (cind > 0):
599 jspectra[ich, interfid[ip], ind] = tmp_noise * \
608 jspectra[ich, interfid[ip], ind] = tmp_noise * \
600 (1 + (numpy.random.uniform(cind) - 0.5) /
609 (1 + (numpy.random.uniform(cind) - 0.5) /
601 numpy.sqrt(num_incoh))
610 numpy.sqrt(num_incoh))
602
611
603 ind = numpy.array([-2, -1, 1, 2])
612 ind = numpy.array([-2, -1, 1, 2])
604 xx = numpy.zeros([4, 4])
613 xx = numpy.zeros([4, 4])
605
614
606 for id1 in range(4):
615 for id1 in range(4):
607 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
616 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
608
617
609 xx_inv = numpy.linalg.inv(xx)
618 xx_inv = numpy.linalg.inv(xx)
610 xx = xx_inv[:, 0]
619 xx = xx_inv[:, 0]
611 ind = (ind + maxid + num_mask_prof) % num_mask_prof
620 ind = (ind + maxid + num_mask_prof) % num_mask_prof
612 yy = jspectra[ich, mask_prof[ind], :]
621 yy = jspectra[ich, mask_prof[ind], :]
613 jspectra[ich, mask_prof[maxid], :] = numpy.dot(
622 jspectra[ich, mask_prof[maxid], :] = numpy.dot(
614 yy.transpose(), xx)
623 yy.transpose(), xx)
615
624
616 indAux = (jspectra[ich, :, :] < tmp_noise *
625 indAux = (jspectra[ich, :, :] < tmp_noise *
617 (1 - 1 / numpy.sqrt(num_incoh))).nonzero()
626 (1 - 1 / numpy.sqrt(num_incoh))).nonzero()
618 jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \
627 jspectra[ich, indAux[0], indAux[1]] = tmp_noise * \
619 (1 - 1 / numpy.sqrt(num_incoh))
628 (1 - 1 / numpy.sqrt(num_incoh))
620
629
621 # Remocion de Interferencia en el Cross Spectra
630 # Remocion de Interferencia en el Cross Spectra
622 if jcspectra is None:
631 if jcspectra is None:
623 return jspectra, jcspectra
632 return jspectra, jcspectra
624 num_pairs = jcspectra.size / (num_prof * num_hei)
633 num_pairs = jcspectra.size / (num_prof * num_hei)
625 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
634 jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei)
626
635
627 for ip in range(num_pairs):
636 for ip in range(num_pairs):
628
637
629 #-------------------------------------------
638 #-------------------------------------------
630
639
631 cspower = numpy.abs(jcspectra[ip, mask_prof, :])
640 cspower = numpy.abs(jcspectra[ip, mask_prof, :])
632 cspower = cspower[:, hei_interf]
641 cspower = cspower[:, hei_interf]
633 cspower = cspower.sum(axis=0)
642 cspower = cspower.sum(axis=0)
634
643
635 cspsort = cspower.ravel().argsort()
644 cspsort = cspower.ravel().argsort()
636 junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(
645 junkcspc_interf = jcspectra[ip, :, hei_interf[cspsort[list(range(
637 offhei_interf, nhei_interf + offhei_interf))]]]
646 offhei_interf, nhei_interf + offhei_interf))]]]
638 junkcspc_interf = junkcspc_interf.transpose()
647 junkcspc_interf = junkcspc_interf.transpose()
639 jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf
648 jcspc_interf = junkcspc_interf.sum(axis=1) / nhei_interf
640
649
641 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
650 ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort()
642
651
643 median_real = numpy.median(numpy.real(
652 median_real = numpy.median(numpy.real(
644 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :]))
653 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :]))
645 median_imag = numpy.median(numpy.imag(
654 median_imag = numpy.median(numpy.imag(
646 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :]))
655 junkcspc_interf[mask_prof[ind[list(range(3 * num_prof / 4))]], :]))
647 junkcspc_interf[comp_mask_prof, :] = numpy.complex(
656 junkcspc_interf[comp_mask_prof, :] = numpy.complex(
648 median_real, median_imag)
657 median_real, median_imag)
649
658
650 for iprof in range(num_prof):
659 for iprof in range(num_prof):
651 ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort()
660 ind = numpy.abs(junkcspc_interf[iprof, :]).ravel().argsort()
652 jcspc_interf[iprof] = junkcspc_interf[iprof,
661 jcspc_interf[iprof] = junkcspc_interf[iprof,
653 ind[nhei_interf / 2]]
662 ind[nhei_interf / 2]]
654
663
655 # Removiendo la Interferencia
664 # Removiendo la Interferencia
656 jcspectra[ip, :, ind_hei] = jcspectra[ip,
665 jcspectra[ip, :, ind_hei] = jcspectra[ip,
657 :, ind_hei] - jcspc_interf
666 :, ind_hei] - jcspc_interf
658
667
659 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
668 ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist()
660 maxid = ListAux.index(max(ListAux))
669 maxid = ListAux.index(max(ListAux))
661
670
662 ind = numpy.array([-2, -1, 1, 2])
671 ind = numpy.array([-2, -1, 1, 2])
663 xx = numpy.zeros([4, 4])
672 xx = numpy.zeros([4, 4])
664
673
665 for id1 in range(4):
674 for id1 in range(4):
666 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
675 xx[:, id1] = ind[id1]**numpy.asarray(list(range(4)))
667
676
668 xx_inv = numpy.linalg.inv(xx)
677 xx_inv = numpy.linalg.inv(xx)
669 xx = xx_inv[:, 0]
678 xx = xx_inv[:, 0]
670
679
671 ind = (ind + maxid + num_mask_prof) % num_mask_prof
680 ind = (ind + maxid + num_mask_prof) % num_mask_prof
672 yy = jcspectra[ip, mask_prof[ind], :]
681 yy = jcspectra[ip, mask_prof[ind], :]
673 jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx)
682 jcspectra[ip, mask_prof[maxid], :] = numpy.dot(yy.transpose(), xx)
674
683
675 # Guardar Resultados
684 # Guardar Resultados
676 self.dataOut.data_spc = jspectra
685 self.dataOut.data_spc = jspectra
677 self.dataOut.data_cspc = jcspectra
686 self.dataOut.data_cspc = jcspectra
678
687
679 return 1
688 return 1
680
689
681 def setRadarFrequency(self, frequency=None):
690 def setRadarFrequency(self, frequency=None):
682
691
683 if frequency != None:
692 if frequency != None:
684 self.dataOut.frequency = frequency
693 self.dataOut.frequency = frequency
685
694
686 return 1
695 return 1
687
696
688 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
697 def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None):
689 # validacion de rango
698 # validacion de rango
690 if minHei == None:
699 if minHei == None:
691 minHei = self.dataOut.heightList[0]
700 minHei = self.dataOut.heightList[0]
692
701
693 if maxHei == None:
702 if maxHei == None:
694 maxHei = self.dataOut.heightList[-1]
703 maxHei = self.dataOut.heightList[-1]
695
704
696 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
705 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
697 print('minHei: %.2f is out of the heights range' % (minHei))
706 print('minHei: %.2f is out of the heights range' % (minHei))
698 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
707 print('minHei is setting to %.2f' % (self.dataOut.heightList[0]))
699 minHei = self.dataOut.heightList[0]
708 minHei = self.dataOut.heightList[0]
700
709
701 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
710 if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei):
702 print('maxHei: %.2f is out of the heights range' % (maxHei))
711 print('maxHei: %.2f is out of the heights range' % (maxHei))
703 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
712 print('maxHei is setting to %.2f' % (self.dataOut.heightList[-1]))
704 maxHei = self.dataOut.heightList[-1]
713 maxHei = self.dataOut.heightList[-1]
705
714
706 # validacion de velocidades
715 # validacion de velocidades
707 velrange = self.dataOut.getVelRange(1)
716 velrange = self.dataOut.getVelRange(1)
708
717
709 if minVel == None:
718 if minVel == None:
710 minVel = velrange[0]
719 minVel = velrange[0]
711
720
712 if maxVel == None:
721 if maxVel == None:
713 maxVel = velrange[-1]
722 maxVel = velrange[-1]
714
723
715 if (minVel < velrange[0]) or (minVel > maxVel):
724 if (minVel < velrange[0]) or (minVel > maxVel):
716 print('minVel: %.2f is out of the velocity range' % (minVel))
725 print('minVel: %.2f is out of the velocity range' % (minVel))
717 print('minVel is setting to %.2f' % (velrange[0]))
726 print('minVel is setting to %.2f' % (velrange[0]))
718 minVel = velrange[0]
727 minVel = velrange[0]
719
728
720 if (maxVel > velrange[-1]) or (maxVel < minVel):
729 if (maxVel > velrange[-1]) or (maxVel < minVel):
721 print('maxVel: %.2f is out of the velocity range' % (maxVel))
730 print('maxVel: %.2f is out of the velocity range' % (maxVel))
722 print('maxVel is setting to %.2f' % (velrange[-1]))
731 print('maxVel is setting to %.2f' % (velrange[-1]))
723 maxVel = velrange[-1]
732 maxVel = velrange[-1]
724
733
725 # seleccion de indices para rango
734 # seleccion de indices para rango
726 minIndex = 0
735 minIndex = 0
727 maxIndex = 0
736 maxIndex = 0
728 heights = self.dataOut.heightList
737 heights = self.dataOut.heightList
729
738
730 inda = numpy.where(heights >= minHei)
739 inda = numpy.where(heights >= minHei)
731 indb = numpy.where(heights <= maxHei)
740 indb = numpy.where(heights <= maxHei)
732
741
733 try:
742 try:
734 minIndex = inda[0][0]
743 minIndex = inda[0][0]
735 except:
744 except:
736 minIndex = 0
745 minIndex = 0
737
746
738 try:
747 try:
739 maxIndex = indb[0][-1]
748 maxIndex = indb[0][-1]
740 except:
749 except:
741 maxIndex = len(heights)
750 maxIndex = len(heights)
742
751
743 if (minIndex < 0) or (minIndex > maxIndex):
752 if (minIndex < 0) or (minIndex > maxIndex):
744 raise ValueError("some value in (%d,%d) is not valid" % (
753 raise ValueError("some value in (%d,%d) is not valid" % (
745 minIndex, maxIndex))
754 minIndex, maxIndex))
746
755
747 if (maxIndex >= self.dataOut.nHeights):
756 if (maxIndex >= self.dataOut.nHeights):
748 maxIndex = self.dataOut.nHeights - 1
757 maxIndex = self.dataOut.nHeights - 1
749
758
750 # seleccion de indices para velocidades
759 # seleccion de indices para velocidades
751 indminvel = numpy.where(velrange >= minVel)
760 indminvel = numpy.where(velrange >= minVel)
752 indmaxvel = numpy.where(velrange <= maxVel)
761 indmaxvel = numpy.where(velrange <= maxVel)
753 try:
762 try:
754 minIndexVel = indminvel[0][0]
763 minIndexVel = indminvel[0][0]
755 except:
764 except:
756 minIndexVel = 0
765 minIndexVel = 0
757
766
758 try:
767 try:
759 maxIndexVel = indmaxvel[0][-1]
768 maxIndexVel = indmaxvel[0][-1]
760 except:
769 except:
761 maxIndexVel = len(velrange)
770 maxIndexVel = len(velrange)
762
771
763 # seleccion del espectro
772 # seleccion del espectro
764 data_spc = self.dataOut.data_spc[:,
773 data_spc = self.dataOut.data_spc[:,
765 minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1]
774 minIndexVel:maxIndexVel + 1, minIndex:maxIndex + 1]
766 # estimacion de ruido
775 # estimacion de ruido
767 noise = numpy.zeros(self.dataOut.nChannels)
776 noise = numpy.zeros(self.dataOut.nChannels)
768
777
769 for channel in range(self.dataOut.nChannels):
778 for channel in range(self.dataOut.nChannels):
770 daux = data_spc[channel, :, :]
779 daux = data_spc[channel, :, :]
771 noise[channel] = hildebrand_sekhon(daux, self.dataOut.nIncohInt)
780 noise[channel] = hildebrand_sekhon(daux, self.dataOut.nIncohInt)
772
781
773 self.dataOut.noise_estimation = noise.copy()
782 self.dataOut.noise_estimation = noise.copy()
774
783
775 return 1
784 return 1
776
785
777
786 @MPDecorator
778 class IncohInt(Operation):
787 class IncohInt(Operation):
779
788
780 __profIndex = 0
789 __profIndex = 0
781 __withOverapping = False
790 __withOverapping = False
782
791
783 __byTime = False
792 __byTime = False
784 __initime = None
793 __initime = None
785 __lastdatatime = None
794 __lastdatatime = None
786 __integrationtime = None
795 __integrationtime = None
787
796
788 __buffer_spc = None
797 __buffer_spc = None
789 __buffer_cspc = None
798 __buffer_cspc = None
790 __buffer_dc = None
799 __buffer_dc = None
791
800
792 __dataReady = False
801 __dataReady = False
793
802
794 __timeInterval = None
803 __timeInterval = None
795
804
796 n = None
805 n = None
797
806
798 def __init__(self, **kwargs):
807 def __init__(self):#, **kwargs):
808
809 Operation.__init__(self)#, **kwargs)
810
799
811
800 Operation.__init__(self, **kwargs)
801 # self.isConfig = False
812 # self.isConfig = False
802
813
803 def setup(self, n=None, timeInterval=None, overlapping=False):
814 def setup(self, n=None, timeInterval=None, overlapping=False):
804 """
815 """
805 Set the parameters of the integration class.
816 Set the parameters of the integration class.
806
817
807 Inputs:
818 Inputs:
808
819
809 n : Number of coherent integrations
820 n : Number of coherent integrations
810 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
821 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
811 overlapping :
822 overlapping :
812
823
813 """
824 """
814
825
815 self.__initime = None
826 self.__initime = None
816 self.__lastdatatime = 0
827 self.__lastdatatime = 0
817
828
818 self.__buffer_spc = 0
829 self.__buffer_spc = 0
819 self.__buffer_cspc = 0
830 self.__buffer_cspc = 0
820 self.__buffer_dc = 0
831 self.__buffer_dc = 0
821
832
822 self.__profIndex = 0
833 self.__profIndex = 0
823 self.__dataReady = False
834 self.__dataReady = False
824 self.__byTime = False
835 self.__byTime = False
825
836
826 if n is None and timeInterval is None:
837 if n is None and timeInterval is None:
827 raise ValueError("n or timeInterval should be specified ...")
838 raise ValueError("n or timeInterval should be specified ...")
828
839
829 if n is not None:
840 if n is not None:
830 self.n = int(n)
841 self.n = int(n)
831 else:
842 else:
832 # if (type(timeInterval)!=integer) -> change this line
843 # if (type(timeInterval)!=integer) -> change this line
833 self.__integrationtime = int(timeInterval)
844 self.__integrationtime = int(timeInterval)
834 self.n = None
845 self.n = None
835 self.__byTime = True
846 self.__byTime = True
836
847
837 def putData(self, data_spc, data_cspc, data_dc):
848 def putData(self, data_spc, data_cspc, data_dc):
838 """
849 """
839 Add a profile to the __buffer_spc and increase in one the __profileIndex
850 Add a profile to the __buffer_spc and increase in one the __profileIndex
840
851
841 """
852 """
842
853
843 self.__buffer_spc += data_spc
854 self.__buffer_spc += data_spc
844
855
845 if data_cspc is None:
856 if data_cspc is None:
846 self.__buffer_cspc = None
857 self.__buffer_cspc = None
847 else:
858 else:
848 self.__buffer_cspc += data_cspc
859 self.__buffer_cspc += data_cspc
849
860
850 if data_dc is None:
861 if data_dc is None:
851 self.__buffer_dc = None
862 self.__buffer_dc = None
852 else:
863 else:
853 self.__buffer_dc += data_dc
864 self.__buffer_dc += data_dc
854
865
855 self.__profIndex += 1
866 self.__profIndex += 1
856
867
857 return
868 return
858
869
859 def pushData(self):
870 def pushData(self):
860 """
871 """
861 Return the sum of the last profiles and the profiles used in the sum.
872 Return the sum of the last profiles and the profiles used in the sum.
862
873
863 Affected:
874 Affected:
864
875
865 self.__profileIndex
876 self.__profileIndex
866
877
867 """
878 """
868
879
869 data_spc = self.__buffer_spc
880 data_spc = self.__buffer_spc
870 data_cspc = self.__buffer_cspc
881 data_cspc = self.__buffer_cspc
871 data_dc = self.__buffer_dc
882 data_dc = self.__buffer_dc
872 n = self.__profIndex
883 n = self.__profIndex
873
884
874 self.__buffer_spc = 0
885 self.__buffer_spc = 0
875 self.__buffer_cspc = 0
886 self.__buffer_cspc = 0
876 self.__buffer_dc = 0
887 self.__buffer_dc = 0
877 self.__profIndex = 0
888 self.__profIndex = 0
878
889
879 return data_spc, data_cspc, data_dc, n
890 return data_spc, data_cspc, data_dc, n
880
891
881 def byProfiles(self, *args):
892 def byProfiles(self, *args):
882
893
883 self.__dataReady = False
894 self.__dataReady = False
884 avgdata_spc = None
895 avgdata_spc = None
885 avgdata_cspc = None
896 avgdata_cspc = None
886 avgdata_dc = None
897 avgdata_dc = None
887
898
888 self.putData(*args)
899 self.putData(*args)
889
900
890 if self.__profIndex == self.n:
901 if self.__profIndex == self.n:
891
902
892 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
903 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
893 self.n = n
904 self.n = n
894 self.__dataReady = True
905 self.__dataReady = True
895
906
896 return avgdata_spc, avgdata_cspc, avgdata_dc
907 return avgdata_spc, avgdata_cspc, avgdata_dc
897
908
898 def byTime(self, datatime, *args):
909 def byTime(self, datatime, *args):
899
910
900 self.__dataReady = False
911 self.__dataReady = False
901 avgdata_spc = None
912 avgdata_spc = None
902 avgdata_cspc = None
913 avgdata_cspc = None
903 avgdata_dc = None
914 avgdata_dc = None
904
915
905 self.putData(*args)
916 self.putData(*args)
906
917
907 if (datatime - self.__initime) >= self.__integrationtime:
918 if (datatime - self.__initime) >= self.__integrationtime:
908 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
919 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
909 self.n = n
920 self.n = n
910 self.__dataReady = True
921 self.__dataReady = True
911
922
912 return avgdata_spc, avgdata_cspc, avgdata_dc
923 return avgdata_spc, avgdata_cspc, avgdata_dc
913
924
914 def integrate(self, datatime, *args):
925 def integrate(self, datatime, *args):
915
926
916 if self.__profIndex == 0:
927 if self.__profIndex == 0:
917 self.__initime = datatime
928 self.__initime = datatime
918
929
919 if self.__byTime:
930 if self.__byTime:
920 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
931 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(
921 datatime, *args)
932 datatime, *args)
922 else:
933 else:
923 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
934 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
924
935
925 if not self.__dataReady:
936 if not self.__dataReady:
926 return None, None, None, None
937 return None, None, None, None
927
938
928 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
939 return self.__initime, avgdata_spc, avgdata_cspc, avgdata_dc
929
940
930 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
941 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
931 if n == 1:
942 if n == 1:
932 return
943 return
933
944
934 dataOut.flagNoData = True
945 dataOut.flagNoData = True
935
946
936 if not self.isConfig:
947 if not self.isConfig:
937 self.setup(n, timeInterval, overlapping)
948 self.setup(n, timeInterval, overlapping)
938 self.isConfig = True
949 self.isConfig = True
939
950
940 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
951 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
941 dataOut.data_spc,
952 dataOut.data_spc,
942 dataOut.data_cspc,
953 dataOut.data_cspc,
943 dataOut.data_dc)
954 dataOut.data_dc)
944
955
945 if self.__dataReady:
956 if self.__dataReady:
946
957
947 dataOut.data_spc = avgdata_spc
958 dataOut.data_spc = avgdata_spc
948 dataOut.data_cspc = avgdata_cspc
959 dataOut.data_cspc = avgdata_cspc
949 dataOut.data_dc = avgdata_dc
960 dataOut.data_dc = avgdata_dc
950
961
951 dataOut.nIncohInt *= self.n
962 dataOut.nIncohInt *= self.n
952 dataOut.utctime = avgdatatime
963 dataOut.utctime = avgdatatime
953 dataOut.flagNoData = False No newline at end of file
964 dataOut.flagNoData = False
965
966 return dataOut No newline at end of file
@@ -1,1321 +1,1335
1 import sys
1 import sys
2 import numpy
2 import numpy
3 from scipy import interpolate
3 from scipy import interpolate
4 #TODO
4 #TODO
5 #from schainpy import cSchain
5 #from schainpy import cSchain
6 from .jroproc_base import ProcessingUnit, Operation
6 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator, Operation
7 from schainpy.model.data.jrodata import Voltage
7 from schainpy.model.data.jrodata import Voltage
8 from time import time
9 from schainpy.utils import log
8 from schainpy.utils import log
9 from time import time
10
10
11
11
12 @MPDecorator
12 class VoltageProc(ProcessingUnit):
13 class VoltageProc(ProcessingUnit):
14
15 METHODS = {} #yong
13
16
17 def __init__(self):#, **kwargs): #yong
14
18
15 def __init__(self, **kwargs):
19 ProcessingUnit.__init__(self)#, **kwargs)
16
17 ProcessingUnit.__init__(self, **kwargs)
18
20
19 # self.objectDict = {}
21 # self.objectDict = {}
20 self.dataOut = Voltage()
22 self.dataOut = Voltage()
21 self.flip = 1
23 self.flip = 1
24 self.setupReq = False #yong
22
25
23 def run(self):
26 def run(self):
27
24 if self.dataIn.type == 'AMISR':
28 if self.dataIn.type == 'AMISR':
25 self.__updateObjFromAmisrInput()
29 self.__updateObjFromAmisrInput()
26
30
27 if self.dataIn.type == 'Voltage':
31 if self.dataIn.type == 'Voltage':
28 self.dataOut.copy(self.dataIn)
32 self.dataOut.copy(self.dataIn)
29
33
30 # self.dataOut.copy(self.dataIn)
34 # self.dataOut.copy(self.dataIn)
31
35
32 def __updateObjFromAmisrInput(self):
36 def __updateObjFromAmisrInput(self):
33
37
34 self.dataOut.timeZone = self.dataIn.timeZone
38 self.dataOut.timeZone = self.dataIn.timeZone
35 self.dataOut.dstFlag = self.dataIn.dstFlag
39 self.dataOut.dstFlag = self.dataIn.dstFlag
36 self.dataOut.errorCount = self.dataIn.errorCount
40 self.dataOut.errorCount = self.dataIn.errorCount
37 self.dataOut.useLocalTime = self.dataIn.useLocalTime
41 self.dataOut.useLocalTime = self.dataIn.useLocalTime
38
42
39 self.dataOut.flagNoData = self.dataIn.flagNoData
43 self.dataOut.flagNoData = self.dataIn.flagNoData
40 self.dataOut.data = self.dataIn.data
44 self.dataOut.data = self.dataIn.data
41 self.dataOut.utctime = self.dataIn.utctime
45 self.dataOut.utctime = self.dataIn.utctime
42 self.dataOut.channelList = self.dataIn.channelList
46 self.dataOut.channelList = self.dataIn.channelList
43 # self.dataOut.timeInterval = self.dataIn.timeInterval
47 # self.dataOut.timeInterval = self.dataIn.timeInterval
44 self.dataOut.heightList = self.dataIn.heightList
48 self.dataOut.heightList = self.dataIn.heightList
45 self.dataOut.nProfiles = self.dataIn.nProfiles
49 self.dataOut.nProfiles = self.dataIn.nProfiles
46
50
47 self.dataOut.nCohInt = self.dataIn.nCohInt
51 self.dataOut.nCohInt = self.dataIn.nCohInt
48 self.dataOut.ippSeconds = self.dataIn.ippSeconds
52 self.dataOut.ippSeconds = self.dataIn.ippSeconds
49 self.dataOut.frequency = self.dataIn.frequency
53 self.dataOut.frequency = self.dataIn.frequency
50
54
51 self.dataOut.azimuth = self.dataIn.azimuth
55 self.dataOut.azimuth = self.dataIn.azimuth
52 self.dataOut.zenith = self.dataIn.zenith
56 self.dataOut.zenith = self.dataIn.zenith
53
57
54 self.dataOut.beam.codeList = self.dataIn.beam.codeList
58 self.dataOut.beam.codeList = self.dataIn.beam.codeList
55 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
59 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
56 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
60 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
57 #
61 #
58 # pass#
62 # pass#
59 #
63 #
60 # def init(self):
64 # def init(self):
61 #
65 #
62 #
66 #
63 # if self.dataIn.type == 'AMISR':
67 # if self.dataIn.type == 'AMISR':
64 # self.__updateObjFromAmisrInput()
68 # self.__updateObjFromAmisrInput()
65 #
69 #
66 # if self.dataIn.type == 'Voltage':
70 # if self.dataIn.type == 'Voltage':
67 # self.dataOut.copy(self.dataIn)
71 # self.dataOut.copy(self.dataIn)
68 # # No necesita copiar en cada init() los atributos de dataIn
72 # # No necesita copiar en cada init() los atributos de dataIn
69 # # la copia deberia hacerse por cada nuevo bloque de datos
73 # # la copia deberia hacerse por cada nuevo bloque de datos
70
74
71 def selectChannels(self, channelList):
75 def selectChannels(self, channelList):
72
76
73 channelIndexList = []
77 channelIndexList = []
74
78
75 for channel in channelList:
79 for channel in channelList:
76 if channel not in self.dataOut.channelList:
80 if channel not in self.dataOut.channelList:
77 raise ValueError("Channel %d is not in %s" %(channel, str(self.dataOut.channelList)))
81 raise ValueError("Channel %d is not in %s" %(channel, str(self.dataOut.channelList)))
78
82
79 index = self.dataOut.channelList.index(channel)
83 index = self.dataOut.channelList.index(channel)
80 channelIndexList.append(index)
84 channelIndexList.append(index)
81
85
82 self.selectChannelsByIndex(channelIndexList)
86 self.selectChannelsByIndex(channelIndexList)
83
87
84 def selectChannelsByIndex(self, channelIndexList):
88 def selectChannelsByIndex(self, channelIndexList):
85 """
89 """
86 Selecciona un bloque de datos en base a canales segun el channelIndexList
90 Selecciona un bloque de datos en base a canales segun el channelIndexList
87
91
88 Input:
92 Input:
89 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
93 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
90
94
91 Affected:
95 Affected:
92 self.dataOut.data
96 self.dataOut.data
93 self.dataOut.channelIndexList
97 self.dataOut.channelIndexList
94 self.dataOut.nChannels
98 self.dataOut.nChannels
95 self.dataOut.m_ProcessingHeader.totalSpectra
99 self.dataOut.m_ProcessingHeader.totalSpectra
96 self.dataOut.systemHeaderObj.numChannels
100 self.dataOut.systemHeaderObj.numChannels
97 self.dataOut.m_ProcessingHeader.blockSize
101 self.dataOut.m_ProcessingHeader.blockSize
98
102
99 Return:
103 Return:
100 None
104 None
101 """
105 """
102
106
103 for channelIndex in channelIndexList:
107 for channelIndex in channelIndexList:
104 if channelIndex not in self.dataOut.channelIndexList:
108 if channelIndex not in self.dataOut.channelIndexList:
105 print(channelIndexList)
109 print(channelIndexList)
106 raise ValueError("The value %d in channelIndexList is not valid" %channelIndex)
110 raise ValueError("The value %d in channelIndexList is not valid" %channelIndex)
107
111
108 if self.dataOut.flagDataAsBlock:
112 if self.dataOut.flagDataAsBlock:
109 """
113 """
110 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
114 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
111 """
115 """
112 data = self.dataOut.data[channelIndexList,:,:]
116 data = self.dataOut.data[channelIndexList,:,:]
113 else:
117 else:
114 data = self.dataOut.data[channelIndexList,:]
118 data = self.dataOut.data[channelIndexList,:]
115
119
116 self.dataOut.data = data
120 self.dataOut.data = data
117 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
121 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
118 # self.dataOut.nChannels = nChannels
122 # self.dataOut.nChannels = nChannels
119
123
120 return 1
124 return 1
121
125
122 def selectHeights(self, minHei=None, maxHei=None):
126 def selectHeights(self, minHei=None, maxHei=None):
123 """
127 """
124 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
128 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
125 minHei <= height <= maxHei
129 minHei <= height <= maxHei
126
130
127 Input:
131 Input:
128 minHei : valor minimo de altura a considerar
132 minHei : valor minimo de altura a considerar
129 maxHei : valor maximo de altura a considerar
133 maxHei : valor maximo de altura a considerar
130
134
131 Affected:
135 Affected:
132 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
136 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
133
137
134 Return:
138 Return:
135 1 si el metodo se ejecuto con exito caso contrario devuelve 0
139 1 si el metodo se ejecuto con exito caso contrario devuelve 0
136 """
140 """
137
141
138 if minHei == None:
142 if minHei == None:
139 minHei = self.dataOut.heightList[0]
143 minHei = self.dataOut.heightList[0]
140
144
141 if maxHei == None:
145 if maxHei == None:
142 maxHei = self.dataOut.heightList[-1]
146 maxHei = self.dataOut.heightList[-1]
143
147
144 if (minHei < self.dataOut.heightList[0]):
148 if (minHei < self.dataOut.heightList[0]):
145 minHei = self.dataOut.heightList[0]
149 minHei = self.dataOut.heightList[0]
146
150
147 if (maxHei > self.dataOut.heightList[-1]):
151 if (maxHei > self.dataOut.heightList[-1]):
148 maxHei = self.dataOut.heightList[-1]
152 maxHei = self.dataOut.heightList[-1]
149
153
150 minIndex = 0
154 minIndex = 0
151 maxIndex = 0
155 maxIndex = 0
152 heights = self.dataOut.heightList
156 heights = self.dataOut.heightList
153
157
154 inda = numpy.where(heights >= minHei)
158 inda = numpy.where(heights >= minHei)
155 indb = numpy.where(heights <= maxHei)
159 indb = numpy.where(heights <= maxHei)
156
160
157 try:
161 try:
158 minIndex = inda[0][0]
162 minIndex = inda[0][0]
159 except:
163 except:
160 minIndex = 0
164 minIndex = 0
161
165
162 try:
166 try:
163 maxIndex = indb[0][-1]
167 maxIndex = indb[0][-1]
164 except:
168 except:
165 maxIndex = len(heights)
169 maxIndex = len(heights)
166
170
167 self.selectHeightsByIndex(minIndex, maxIndex)
171 self.selectHeightsByIndex(minIndex, maxIndex)
168
172
169 return 1
173 return 1
170
174
171
175
172 def selectHeightsByIndex(self, minIndex, maxIndex):
176 def selectHeightsByIndex(self, minIndex, maxIndex):
173 """
177 """
174 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
178 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
175 minIndex <= index <= maxIndex
179 minIndex <= index <= maxIndex
176
180
177 Input:
181 Input:
178 minIndex : valor de indice minimo de altura a considerar
182 minIndex : valor de indice minimo de altura a considerar
179 maxIndex : valor de indice maximo de altura a considerar
183 maxIndex : valor de indice maximo de altura a considerar
180
184
181 Affected:
185 Affected:
182 self.dataOut.data
186 self.dataOut.data
183 self.dataOut.heightList
187 self.dataOut.heightList
184
188
185 Return:
189 Return:
186 1 si el metodo se ejecuto con exito caso contrario devuelve 0
190 1 si el metodo se ejecuto con exito caso contrario devuelve 0
187 """
191 """
188
192
189 if (minIndex < 0) or (minIndex > maxIndex):
193 if (minIndex < 0) or (minIndex > maxIndex):
190 raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex))
194 raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex))
191
195
192 if (maxIndex >= self.dataOut.nHeights):
196 if (maxIndex >= self.dataOut.nHeights):
193 maxIndex = self.dataOut.nHeights
197 maxIndex = self.dataOut.nHeights
194
198
195 #voltage
199 #voltage
196 if self.dataOut.flagDataAsBlock:
200 if self.dataOut.flagDataAsBlock:
197 """
201 """
198 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
202 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
199 """
203 """
200 data = self.dataOut.data[:,:, minIndex:maxIndex]
204 data = self.dataOut.data[:,:, minIndex:maxIndex]
201 else:
205 else:
202 data = self.dataOut.data[:, minIndex:maxIndex]
206 data = self.dataOut.data[:, minIndex:maxIndex]
203
207
204 # firstHeight = self.dataOut.heightList[minIndex]
208 # firstHeight = self.dataOut.heightList[minIndex]
205
209
206 self.dataOut.data = data
210 self.dataOut.data = data
207 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
211 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
208
212
209 if self.dataOut.nHeights <= 1:
213 if self.dataOut.nHeights <= 1:
210 raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights))
214 raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights))
211
215
212 return 1
216 return 1
213
217
214
218
215 def filterByHeights(self, window):
219 def filterByHeights(self, window):
216
220
217 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
221 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
218
222
219 if window == None:
223 if window == None:
220 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
224 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
221
225
222 newdelta = deltaHeight * window
226 newdelta = deltaHeight * window
223 r = self.dataOut.nHeights % window
227 r = self.dataOut.nHeights % window
224 newheights = (self.dataOut.nHeights-r)/window
228 newheights = (self.dataOut.nHeights-r)/window
225
229
226 if newheights <= 1:
230 if newheights <= 1:
227 raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window))
231 raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window))
228
232
229 if self.dataOut.flagDataAsBlock:
233 if self.dataOut.flagDataAsBlock:
230 """
234 """
231 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
235 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
232 """
236 """
233 buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r]
237 buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r]
234 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window)
238 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window)
235 buffer = numpy.sum(buffer,3)
239 buffer = numpy.sum(buffer,3)
236
240
237 else:
241 else:
238 buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r]
242 buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r]
239 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window)
243 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window)
240 buffer = numpy.sum(buffer,2)
244 buffer = numpy.sum(buffer,2)
241
245
242 self.dataOut.data = buffer
246 self.dataOut.data = buffer
243 self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta
247 self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta
244 self.dataOut.windowOfFilter = window
248 self.dataOut.windowOfFilter = window
245
249
246 def setH0(self, h0, deltaHeight = None):
250 def setH0(self, h0, deltaHeight = None):
247
251
248 if not deltaHeight:
252 if not deltaHeight:
249 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
253 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
250
254
251 nHeights = self.dataOut.nHeights
255 nHeights = self.dataOut.nHeights
252
256
253 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
257 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
254
258
255 self.dataOut.heightList = newHeiRange
259 self.dataOut.heightList = newHeiRange
256
260
257 def deFlip(self, channelList = []):
261 def deFlip(self, channelList = []):
258
262
259 data = self.dataOut.data.copy()
263 data = self.dataOut.data.copy()
260
264
261 if self.dataOut.flagDataAsBlock:
265 if self.dataOut.flagDataAsBlock:
262 flip = self.flip
266 flip = self.flip
263 profileList = list(range(self.dataOut.nProfiles))
267 profileList = list(range(self.dataOut.nProfiles))
264
268
265 if not channelList:
269 if not channelList:
266 for thisProfile in profileList:
270 for thisProfile in profileList:
267 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
271 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
268 flip *= -1.0
272 flip *= -1.0
269 else:
273 else:
270 for thisChannel in channelList:
274 for thisChannel in channelList:
271 if thisChannel not in self.dataOut.channelList:
275 if thisChannel not in self.dataOut.channelList:
272 continue
276 continue
273
277
274 for thisProfile in profileList:
278 for thisProfile in profileList:
275 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
279 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
276 flip *= -1.0
280 flip *= -1.0
277
281
278 self.flip = flip
282 self.flip = flip
279
283
280 else:
284 else:
281 if not channelList:
285 if not channelList:
282 data[:,:] = data[:,:]*self.flip
286 data[:,:] = data[:,:]*self.flip
283 else:
287 else:
284 for thisChannel in channelList:
288 for thisChannel in channelList:
285 if thisChannel not in self.dataOut.channelList:
289 if thisChannel not in self.dataOut.channelList:
286 continue
290 continue
287
291
288 data[thisChannel,:] = data[thisChannel,:]*self.flip
292 data[thisChannel,:] = data[thisChannel,:]*self.flip
289
293
290 self.flip *= -1.
294 self.flip *= -1.
291
295
292 self.dataOut.data = data
296 self.dataOut.data = data
293
297
294 def setRadarFrequency(self, frequency=None):
298 def setRadarFrequency(self, frequency=None):
295
299
296 if frequency != None:
300 if frequency != None:
297 self.dataOut.frequency = frequency
301 self.dataOut.frequency = frequency
298
302
299 return 1
303 return 1
300
304
301 def interpolateHeights(self, topLim, botLim):
305 def interpolateHeights(self, topLim, botLim):
302 #69 al 72 para julia
306 #69 al 72 para julia
303 #82-84 para meteoros
307 #82-84 para meteoros
304 if len(numpy.shape(self.dataOut.data))==2:
308 if len(numpy.shape(self.dataOut.data))==2:
305 sampInterp = (self.dataOut.data[:,botLim-1] + self.dataOut.data[:,topLim+1])/2
309 sampInterp = (self.dataOut.data[:,botLim-1] + self.dataOut.data[:,topLim+1])/2
306 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
310 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
307 #self.dataOut.data[:,botLim:limSup+1] = sampInterp
311 #self.dataOut.data[:,botLim:limSup+1] = sampInterp
308 self.dataOut.data[:,botLim:topLim+1] = sampInterp
312 self.dataOut.data[:,botLim:topLim+1] = sampInterp
309 else:
313 else:
310 nHeights = self.dataOut.data.shape[2]
314 nHeights = self.dataOut.data.shape[2]
311 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
315 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
312 y = self.dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))]
316 y = self.dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))]
313 f = interpolate.interp1d(x, y, axis = 2)
317 f = interpolate.interp1d(x, y, axis = 2)
314 xnew = numpy.arange(botLim,topLim+1)
318 xnew = numpy.arange(botLim,topLim+1)
315 ynew = f(xnew)
319 ynew = f(xnew)
316
320
317 self.dataOut.data[:,:,botLim:topLim+1] = ynew
321 self.dataOut.data[:,:,botLim:topLim+1] = ynew
318
322
319 # import collections
323 # import collections
320
324 @MPDecorator
321 class CohInt(Operation):
325 class CohInt(Operation):
322
326
323 isConfig = False
327 isConfig = False
324 __profIndex = 0
328 __profIndex = 0
325 __byTime = False
329 __byTime = False
326 __initime = None
330 __initime = None
327 __lastdatatime = None
331 __lastdatatime = None
328 __integrationtime = None
332 __integrationtime = None
329 __buffer = None
333 __buffer = None
330 __bufferStride = []
334 __bufferStride = []
331 __dataReady = False
335 __dataReady = False
332 __profIndexStride = 0
336 __profIndexStride = 0
333 __dataToPutStride = False
337 __dataToPutStride = False
334 n = None
338 n = None
335
339
336 def __init__(self, **kwargs):
340 def __init__(self):#, **kwargs):
337
341
338 Operation.__init__(self, **kwargs)
342 Operation.__init__(self)#, **kwargs)
339
343
340 # self.isConfig = False
344 # self.isConfig = False
341
345
342 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
346 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
343 """
347 """
344 Set the parameters of the integration class.
348 Set the parameters of the integration class.
345
349
346 Inputs:
350 Inputs:
347
351
348 n : Number of coherent integrations
352 n : Number of coherent integrations
349 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
353 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
350 overlapping :
354 overlapping :
351 """
355 """
352
356
353 self.__initime = None
357 self.__initime = None
354 self.__lastdatatime = 0
358 self.__lastdatatime = 0
355 self.__buffer = None
359 self.__buffer = None
356 self.__dataReady = False
360 self.__dataReady = False
357 self.byblock = byblock
361 self.byblock = byblock
358 self.stride = stride
362 self.stride = stride
359
363
360 if n == None and timeInterval == None:
364 if n == None and timeInterval == None:
361 raise ValueError("n or timeInterval should be specified ...")
365 raise ValueError("n or timeInterval should be specified ...")
362
366
363 if n != None:
367 if n != None:
364 self.n = n
368 self.n = n
365 self.__byTime = False
369 self.__byTime = False
366 else:
370 else:
367 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
371 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
368 self.n = 9999
372 self.n = 9999
369 self.__byTime = True
373 self.__byTime = True
370
374
371 if overlapping:
375 if overlapping:
372 self.__withOverlapping = True
376 self.__withOverlapping = True
373 self.__buffer = None
377 self.__buffer = None
374 else:
378 else:
375 self.__withOverlapping = False
379 self.__withOverlapping = False
376 self.__buffer = 0
380 self.__buffer = 0
377
381
378 self.__profIndex = 0
382 self.__profIndex = 0
379
383
380 def putData(self, data):
384 def putData(self, data):
381
385
382 """
386 """
383 Add a profile to the __buffer and increase in one the __profileIndex
387 Add a profile to the __buffer and increase in one the __profileIndex
384
388
385 """
389 """
386
390
387 if not self.__withOverlapping:
391 if not self.__withOverlapping:
388 self.__buffer += data.copy()
392 self.__buffer += data.copy()
389 self.__profIndex += 1
393 self.__profIndex += 1
390 return
394 return
391
395
392 #Overlapping data
396 #Overlapping data
393 nChannels, nHeis = data.shape
397 nChannels, nHeis = data.shape
394 data = numpy.reshape(data, (1, nChannels, nHeis))
398 data = numpy.reshape(data, (1, nChannels, nHeis))
395
399
396 #If the buffer is empty then it takes the data value
400 #If the buffer is empty then it takes the data value
397 if self.__buffer is None:
401 if self.__buffer is None:
398 self.__buffer = data
402 self.__buffer = data
399 self.__profIndex += 1
403 self.__profIndex += 1
400 return
404 return
401
405
402 #If the buffer length is lower than n then stakcing the data value
406 #If the buffer length is lower than n then stakcing the data value
403 if self.__profIndex < self.n:
407 if self.__profIndex < self.n:
404 self.__buffer = numpy.vstack((self.__buffer, data))
408 self.__buffer = numpy.vstack((self.__buffer, data))
405 self.__profIndex += 1
409 self.__profIndex += 1
406 return
410 return
407
411
408 #If the buffer length is equal to n then replacing the last buffer value with the data value
412 #If the buffer length is equal to n then replacing the last buffer value with the data value
409 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
413 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
410 self.__buffer[self.n-1] = data
414 self.__buffer[self.n-1] = data
411 self.__profIndex = self.n
415 self.__profIndex = self.n
412 return
416 return
413
417
414
418
415 def pushData(self):
419 def pushData(self):
416 """
420 """
417 Return the sum of the last profiles and the profiles used in the sum.
421 Return the sum of the last profiles and the profiles used in the sum.
418
422
419 Affected:
423 Affected:
420
424
421 self.__profileIndex
425 self.__profileIndex
422
426
423 """
427 """
424
428
425 if not self.__withOverlapping:
429 if not self.__withOverlapping:
426 data = self.__buffer
430 data = self.__buffer
427 n = self.__profIndex
431 n = self.__profIndex
428
432
429 self.__buffer = 0
433 self.__buffer = 0
430 self.__profIndex = 0
434 self.__profIndex = 0
431
435
432 return data, n
436 return data, n
433
437
434 #Integration with Overlapping
438 #Integration with Overlapping
435 data = numpy.sum(self.__buffer, axis=0)
439 data = numpy.sum(self.__buffer, axis=0)
436 # print data
440 # print data
437 # raise
441 # raise
438 n = self.__profIndex
442 n = self.__profIndex
439
443
440 return data, n
444 return data, n
441
445
442 def byProfiles(self, data):
446 def byProfiles(self, data):
443
447
444 self.__dataReady = False
448 self.__dataReady = False
445 avgdata = None
449 avgdata = None
446 # n = None
450 # n = None
447 # print data
451 # print data
448 # raise
452 # raise
449 self.putData(data)
453 self.putData(data)
450
454
451 if self.__profIndex == self.n:
455 if self.__profIndex == self.n:
452 avgdata, n = self.pushData()
456 avgdata, n = self.pushData()
453 self.__dataReady = True
457 self.__dataReady = True
454
458
455 return avgdata
459 return avgdata
456
460
457 def byTime(self, data, datatime):
461 def byTime(self, data, datatime):
458
462
459 self.__dataReady = False
463 self.__dataReady = False
460 avgdata = None
464 avgdata = None
461 n = None
465 n = None
462
466
463 self.putData(data)
467 self.putData(data)
464
468
465 if (datatime - self.__initime) >= self.__integrationtime:
469 if (datatime - self.__initime) >= self.__integrationtime:
466 avgdata, n = self.pushData()
470 avgdata, n = self.pushData()
467 self.n = n
471 self.n = n
468 self.__dataReady = True
472 self.__dataReady = True
469
473
470 return avgdata
474 return avgdata
471
475
472 def integrateByStride(self, data, datatime):
476 def integrateByStride(self, data, datatime):
473 # print data
477 # print data
474 if self.__profIndex == 0:
478 if self.__profIndex == 0:
475 self.__buffer = [[data.copy(), datatime]]
479 self.__buffer = [[data.copy(), datatime]]
476 else:
480 else:
477 self.__buffer.append([data.copy(),datatime])
481 self.__buffer.append([data.copy(),datatime])
478 self.__profIndex += 1
482 self.__profIndex += 1
479 self.__dataReady = False
483 self.__dataReady = False
480
484
481 if self.__profIndex == self.n * self.stride :
485 if self.__profIndex == self.n * self.stride :
482 self.__dataToPutStride = True
486 self.__dataToPutStride = True
483 self.__profIndexStride = 0
487 self.__profIndexStride = 0
484 self.__profIndex = 0
488 self.__profIndex = 0
485 self.__bufferStride = []
489 self.__bufferStride = []
486 for i in range(self.stride):
490 for i in range(self.stride):
487 current = self.__buffer[i::self.stride]
491 current = self.__buffer[i::self.stride]
488 data = numpy.sum([t[0] for t in current], axis=0)
492 data = numpy.sum([t[0] for t in current], axis=0)
489 avgdatatime = numpy.average([t[1] for t in current])
493 avgdatatime = numpy.average([t[1] for t in current])
490 # print data
494 # print data
491 self.__bufferStride.append((data, avgdatatime))
495 self.__bufferStride.append((data, avgdatatime))
492
496
493 if self.__dataToPutStride:
497 if self.__dataToPutStride:
494 self.__dataReady = True
498 self.__dataReady = True
495 self.__profIndexStride += 1
499 self.__profIndexStride += 1
496 if self.__profIndexStride == self.stride:
500 if self.__profIndexStride == self.stride:
497 self.__dataToPutStride = False
501 self.__dataToPutStride = False
498 # print self.__bufferStride[self.__profIndexStride - 1]
502 # print self.__bufferStride[self.__profIndexStride - 1]
499 # raise
503 # raise
500 return self.__bufferStride[self.__profIndexStride - 1]
504 return self.__bufferStride[self.__profIndexStride - 1]
501
505
502
506
503 return None, None
507 return None, None
504
508
505 def integrate(self, data, datatime=None):
509 def integrate(self, data, datatime=None):
506
510
507 if self.__initime == None:
511 if self.__initime == None:
508 self.__initime = datatime
512 self.__initime = datatime
509
513
510 if self.__byTime:
514 if self.__byTime:
511 avgdata = self.byTime(data, datatime)
515 avgdata = self.byTime(data, datatime)
512 else:
516 else:
513 avgdata = self.byProfiles(data)
517 avgdata = self.byProfiles(data)
514
518
515
519
516 self.__lastdatatime = datatime
520 self.__lastdatatime = datatime
517
521
518 if avgdata is None:
522 if avgdata is None:
519 return None, None
523 return None, None
520
524
521 avgdatatime = self.__initime
525 avgdatatime = self.__initime
522
526
523 deltatime = datatime - self.__lastdatatime
527 deltatime = datatime - self.__lastdatatime
524
528
525 if not self.__withOverlapping:
529 if not self.__withOverlapping:
526 self.__initime = datatime
530 self.__initime = datatime
527 else:
531 else:
528 self.__initime += deltatime
532 self.__initime += deltatime
529
533
530 return avgdata, avgdatatime
534 return avgdata, avgdatatime
531
535
532 def integrateByBlock(self, dataOut):
536 def integrateByBlock(self, dataOut):
533
537
534 times = int(dataOut.data.shape[1]/self.n)
538 times = int(dataOut.data.shape[1]/self.n)
535 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
539 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
536
540
537 id_min = 0
541 id_min = 0
538 id_max = self.n
542 id_max = self.n
539
543
540 for i in range(times):
544 for i in range(times):
541 junk = dataOut.data[:,id_min:id_max,:]
545 junk = dataOut.data[:,id_min:id_max,:]
542 avgdata[:,i,:] = junk.sum(axis=1)
546 avgdata[:,i,:] = junk.sum(axis=1)
543 id_min += self.n
547 id_min += self.n
544 id_max += self.n
548 id_max += self.n
545
549
546 timeInterval = dataOut.ippSeconds*self.n
550 timeInterval = dataOut.ippSeconds*self.n
547 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
551 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
548 self.__dataReady = True
552 self.__dataReady = True
549 return avgdata, avgdatatime
553 return avgdata, avgdatatime
550
554
551 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
555 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
556
552 if not self.isConfig:
557 if not self.isConfig:
553 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
558 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
554 self.isConfig = True
559 self.isConfig = True
555
560
556 if dataOut.flagDataAsBlock:
561 if dataOut.flagDataAsBlock:
557 """
562 """
558 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
563 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
559 """
564 """
560 avgdata, avgdatatime = self.integrateByBlock(dataOut)
565 avgdata, avgdatatime = self.integrateByBlock(dataOut)
561 dataOut.nProfiles /= self.n
566 dataOut.nProfiles /= self.n
562 else:
567 else:
563 if stride is None:
568 if stride is None:
564 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
569 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
565 else:
570 else:
566 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
571 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
567
572
568
573
569 # dataOut.timeInterval *= n
574 # dataOut.timeInterval *= n
570 dataOut.flagNoData = True
575 dataOut.flagNoData = True
571
576
572 if self.__dataReady:
577 if self.__dataReady:
573 dataOut.data = avgdata
578 dataOut.data = avgdata
574 dataOut.nCohInt *= self.n
579 dataOut.nCohInt *= self.n
575 dataOut.utctime = avgdatatime
580 dataOut.utctime = avgdatatime
576 # print avgdata, avgdatatime
581 # print avgdata, avgdatatime
577 # raise
582 # raise
578 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
583 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
579 dataOut.flagNoData = False
584 dataOut.flagNoData = False
580
585 return dataOut
586 @MPDecorator
581 class Decoder(Operation):
587 class Decoder(Operation):
582
588
583 isConfig = False
589 isConfig = False
584 __profIndex = 0
590 __profIndex = 0
585
591
586 code = None
592 code = None
587
593
588 nCode = None
594 nCode = None
589 nBaud = None
595 nBaud = None
590
596
591 def __init__(self, **kwargs):
597 def __init__(self):#, **kwargs):
592
598
593 Operation.__init__(self, **kwargs)
599 Operation.__init__(self)#, **kwargs)
594
600
595 self.times = None
601 self.times = None
596 self.osamp = None
602 self.osamp = None
597 # self.__setValues = False
603 # self.__setValues = False
598 self.isConfig = False
604 # self.isConfig = False
599
605 self.setupReq = False
600 def setup(self, code, osamp, dataOut):
606 def setup(self, code, osamp, dataOut):
601
607
602 self.__profIndex = 0
608 self.__profIndex = 0
603
609
604 self.code = code
610 self.code = code
605
611
606 self.nCode = len(code)
612 self.nCode = len(code)
607 self.nBaud = len(code[0])
613 self.nBaud = len(code[0])
608
614
609 if (osamp != None) and (osamp >1):
615 if (osamp != None) and (osamp >1):
610 self.osamp = osamp
616 self.osamp = osamp
611 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
617 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
612 self.nBaud = self.nBaud*self.osamp
618 self.nBaud = self.nBaud*self.osamp
613
619
614 self.__nChannels = dataOut.nChannels
620 self.__nChannels = dataOut.nChannels
615 self.__nProfiles = dataOut.nProfiles
621 self.__nProfiles = dataOut.nProfiles
616 self.__nHeis = dataOut.nHeights
622 self.__nHeis = dataOut.nHeights
617
623
618 if self.__nHeis < self.nBaud:
624 if self.__nHeis < self.nBaud:
619 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
625 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
620
626
621 #Frequency
627 #Frequency
622 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
628 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
623
629
624 __codeBuffer[:,0:self.nBaud] = self.code
630 __codeBuffer[:,0:self.nBaud] = self.code
625
631
626 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
632 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
627
633
628 if dataOut.flagDataAsBlock:
634 if dataOut.flagDataAsBlock:
629
635
630 self.ndatadec = self.__nHeis #- self.nBaud + 1
636 self.ndatadec = self.__nHeis #- self.nBaud + 1
631
637
632 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
638 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
633
639
634 else:
640 else:
635
641
636 #Time
642 #Time
637 self.ndatadec = self.__nHeis #- self.nBaud + 1
643 self.ndatadec = self.__nHeis #- self.nBaud + 1
638
644
639 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
645 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
640
646
641 def __convolutionInFreq(self, data):
647 def __convolutionInFreq(self, data):
642
648
643 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
649 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
644
650
645 fft_data = numpy.fft.fft(data, axis=1)
651 fft_data = numpy.fft.fft(data, axis=1)
646
652
647 conv = fft_data*fft_code
653 conv = fft_data*fft_code
648
654
649 data = numpy.fft.ifft(conv,axis=1)
655 data = numpy.fft.ifft(conv,axis=1)
650
656
651 return data
657 return data
652
658
653 def __convolutionInFreqOpt(self, data):
659 def __convolutionInFreqOpt(self, data):
654
660
655 raise NotImplementedError
661 raise NotImplementedError
656
662
657 def __convolutionInTime(self, data):
663 def __convolutionInTime(self, data):
658
664
659 code = self.code[self.__profIndex]
665 code = self.code[self.__profIndex]
660 for i in range(self.__nChannels):
666 for i in range(self.__nChannels):
661 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
667 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
662
668
663 return self.datadecTime
669 return self.datadecTime
664
670
665 def __convolutionByBlockInTime(self, data):
671 def __convolutionByBlockInTime(self, data):
666
672
667 repetitions = self.__nProfiles / self.nCode
673 repetitions = self.__nProfiles / self.nCode
668
674
669 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
675 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
670 junk = junk.flatten()
676 junk = junk.flatten()
671 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
677 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
672 profilesList = range(self.__nProfiles)
678 profilesList = range(self.__nProfiles)
673
679
674 for i in range(self.__nChannels):
680 for i in range(self.__nChannels):
675 for j in profilesList:
681 for j in profilesList:
676 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
682 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
677 return self.datadecTime
683 return self.datadecTime
678
684
679 def __convolutionByBlockInFreq(self, data):
685 def __convolutionByBlockInFreq(self, data):
680
686
681 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
687 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
682
688
683
689
684 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
690 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
685
691
686 fft_data = numpy.fft.fft(data, axis=2)
692 fft_data = numpy.fft.fft(data, axis=2)
687
693
688 conv = fft_data*fft_code
694 conv = fft_data*fft_code
689
695
690 data = numpy.fft.ifft(conv,axis=2)
696 data = numpy.fft.ifft(conv,axis=2)
691
697
692 return data
698 return data
693
699
694
700
695 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
701 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
696
702
697 if dataOut.flagDecodeData:
703 if dataOut.flagDecodeData:
698 print("This data is already decoded, recoding again ...")
704 print("This data is already decoded, recoding again ...")
699
705
700 if not self.isConfig:
706 if not self.isConfig:
701
707
702 if code is None:
708 if code is None:
703 if dataOut.code is None:
709 if dataOut.code is None:
704 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
710 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
705
711
706 code = dataOut.code
712 code = dataOut.code
707 else:
713 else:
708 code = numpy.array(code).reshape(nCode,nBaud)
714 code = numpy.array(code).reshape(nCode,nBaud)
709 self.setup(code, osamp, dataOut)
715 self.setup(code, osamp, dataOut)
710
716
711 self.isConfig = True
717 self.isConfig = True
712
718
713 if mode == 3:
719 if mode == 3:
714 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
720 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
715
721
716 if times != None:
722 if times != None:
717 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
723 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
718
724
719 if self.code is None:
725 if self.code is None:
720 print("Fail decoding: Code is not defined.")
726 print("Fail decoding: Code is not defined.")
721 return
727 return
722
728
723 self.__nProfiles = dataOut.nProfiles
729 self.__nProfiles = dataOut.nProfiles
724 datadec = None
730 datadec = None
725
731
726 if mode == 3:
732 if mode == 3:
727 mode = 0
733 mode = 0
728
734
729 if dataOut.flagDataAsBlock:
735 if dataOut.flagDataAsBlock:
730 """
736 """
731 Decoding when data have been read as block,
737 Decoding when data have been read as block,
732 """
738 """
733
739
734 if mode == 0:
740 if mode == 0:
735 datadec = self.__convolutionByBlockInTime(dataOut.data)
741 datadec = self.__convolutionByBlockInTime(dataOut.data)
736 if mode == 1:
742 if mode == 1:
737 datadec = self.__convolutionByBlockInFreq(dataOut.data)
743 datadec = self.__convolutionByBlockInFreq(dataOut.data)
738 else:
744 else:
739 """
745 """
740 Decoding when data have been read profile by profile
746 Decoding when data have been read profile by profile
741 """
747 """
742 if mode == 0:
748 if mode == 0:
743 datadec = self.__convolutionInTime(dataOut.data)
749 datadec = self.__convolutionInTime(dataOut.data)
744
750
745 if mode == 1:
751 if mode == 1:
746 datadec = self.__convolutionInFreq(dataOut.data)
752 datadec = self.__convolutionInFreq(dataOut.data)
747
753
748 if mode == 2:
754 if mode == 2:
749 datadec = self.__convolutionInFreqOpt(dataOut.data)
755 datadec = self.__convolutionInFreqOpt(dataOut.data)
750
756
751 if datadec is None:
757 if datadec is None:
752 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
758 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
753
759
754 dataOut.code = self.code
760 dataOut.code = self.code
755 dataOut.nCode = self.nCode
761 dataOut.nCode = self.nCode
756 dataOut.nBaud = self.nBaud
762 dataOut.nBaud = self.nBaud
757
763
758 dataOut.data = datadec
764 dataOut.data = datadec
759
765
760 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
766 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
761
767
762 dataOut.flagDecodeData = True #asumo q la data esta decodificada
768 dataOut.flagDecodeData = True #asumo q la data esta decodificada
763
769
764 if self.__profIndex == self.nCode-1:
770 if self.__profIndex == self.nCode-1:
765 self.__profIndex = 0
771 self.__profIndex = 0
766 return 1
772 return dataOut
767
773
768 self.__profIndex += 1
774 self.__profIndex += 1
769
775
770 return 1
776 return dataOut
771 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
777 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
772
778
773
779 @MPDecorator
774 class ProfileConcat(Operation):
780 class ProfileConcat(Operation):
775
781
776 isConfig = False
782 isConfig = False
777 buffer = None
783 buffer = None
778
784
779 def __init__(self, **kwargs):
785 def __init__(self):#, **kwargs):
780
786
781 Operation.__init__(self, **kwargs)
787 Operation.__init__(self)#, **kwargs)
782 self.profileIndex = 0
788 self.profileIndex = 0
783
789
784 def reset(self):
790 def reset(self):
785 self.buffer = numpy.zeros_like(self.buffer)
791 self.buffer = numpy.zeros_like(self.buffer)
786 self.start_index = 0
792 self.start_index = 0
787 self.times = 1
793 self.times = 1
788
794
789 def setup(self, data, m, n=1):
795 def setup(self, data, m, n=1):
790 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
796 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
791 self.nHeights = data.shape[1]#.nHeights
797 self.nHeights = data.shape[1]#.nHeights
792 self.start_index = 0
798 self.start_index = 0
793 self.times = 1
799 self.times = 1
794
800
795 def concat(self, data):
801 def concat(self, data):
796
802
797 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
803 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
798 self.start_index = self.start_index + self.nHeights
804 self.start_index = self.start_index + self.nHeights
799
805
800 def run(self, dataOut, m):
806 def run(self, dataOut, m):
801
807
802 dataOut.flagNoData = True
808 dataOut.flagNoData = True
803
809
804 if not self.isConfig:
810 if not self.isConfig:
805 self.setup(dataOut.data, m, 1)
811 self.setup(dataOut.data, m, 1)
806 self.isConfig = True
812 self.isConfig = True
807
813
808 if dataOut.flagDataAsBlock:
814 if dataOut.flagDataAsBlock:
809 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
815 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
810
816
811 else:
817 else:
812 self.concat(dataOut.data)
818 self.concat(dataOut.data)
813 self.times += 1
819 self.times += 1
814 if self.times > m:
820 if self.times > m:
815 dataOut.data = self.buffer
821 dataOut.data = self.buffer
816 self.reset()
822 self.reset()
817 dataOut.flagNoData = False
823 dataOut.flagNoData = False
818 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
824 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
819 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
825 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
820 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
826 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
821 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
827 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
822 dataOut.ippSeconds *= m
828 dataOut.ippSeconds *= m
823
829 return dataOut
830 @MPDecorator
824 class ProfileSelector(Operation):
831 class ProfileSelector(Operation):
825
832
826 profileIndex = None
833 profileIndex = None
827 # Tamanho total de los perfiles
834 # Tamanho total de los perfiles
828 nProfiles = None
835 nProfiles = None
829
836
830 def __init__(self, **kwargs):
837 def __init__(self):#, **kwargs):
831
838
832 Operation.__init__(self, **kwargs)
839 Operation.__init__(self)#, **kwargs)
833 self.profileIndex = 0
840 self.profileIndex = 0
834
841
835 def incProfileIndex(self):
842 def incProfileIndex(self):
836
843
837 self.profileIndex += 1
844 self.profileIndex += 1
838
845
839 if self.profileIndex >= self.nProfiles:
846 if self.profileIndex >= self.nProfiles:
840 self.profileIndex = 0
847 self.profileIndex = 0
841
848
842 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
849 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
843
850
844 if profileIndex < minIndex:
851 if profileIndex < minIndex:
845 return False
852 return False
846
853
847 if profileIndex > maxIndex:
854 if profileIndex > maxIndex:
848 return False
855 return False
849
856
850 return True
857 return True
851
858
852 def isThisProfileInList(self, profileIndex, profileList):
859 def isThisProfileInList(self, profileIndex, profileList):
853
860
854 if profileIndex not in profileList:
861 if profileIndex not in profileList:
855 return False
862 return False
856
863
857 return True
864 return True
858
865
859 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
866 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
860
867
861 """
868 """
862 ProfileSelector:
869 ProfileSelector:
863
870
864 Inputs:
871 Inputs:
865 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
872 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
866
873
867 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
874 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
868
875
869 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
876 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
870
877
871 """
878 """
872
879
873 if rangeList is not None:
880 if rangeList is not None:
874 if type(rangeList[0]) not in (tuple, list):
881 if type(rangeList[0]) not in (tuple, list):
875 rangeList = [rangeList]
882 rangeList = [rangeList]
876
883
877 dataOut.flagNoData = True
884 dataOut.flagNoData = True
878
885
879 if dataOut.flagDataAsBlock:
886 if dataOut.flagDataAsBlock:
880 """
887 """
881 data dimension = [nChannels, nProfiles, nHeis]
888 data dimension = [nChannels, nProfiles, nHeis]
882 """
889 """
883 if profileList != None:
890 if profileList != None:
884 dataOut.data = dataOut.data[:,profileList,:]
891 dataOut.data = dataOut.data[:,profileList,:]
885
892
886 if profileRangeList != None:
893 if profileRangeList != None:
887 minIndex = profileRangeList[0]
894 minIndex = profileRangeList[0]
888 maxIndex = profileRangeList[1]
895 maxIndex = profileRangeList[1]
889 profileList = list(range(minIndex, maxIndex+1))
896 profileList = list(range(minIndex, maxIndex+1))
890
897
891 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
898 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
892
899
893 if rangeList != None:
900 if rangeList != None:
894
901
895 profileList = []
902 profileList = []
896
903
897 for thisRange in rangeList:
904 for thisRange in rangeList:
898 minIndex = thisRange[0]
905 minIndex = thisRange[0]
899 maxIndex = thisRange[1]
906 maxIndex = thisRange[1]
900
907
901 profileList.extend(list(range(minIndex, maxIndex+1)))
908 profileList.extend(list(range(minIndex, maxIndex+1)))
902
909
903 dataOut.data = dataOut.data[:,profileList,:]
910 dataOut.data = dataOut.data[:,profileList,:]
904
911
905 dataOut.nProfiles = len(profileList)
912 dataOut.nProfiles = len(profileList)
906 dataOut.profileIndex = dataOut.nProfiles - 1
913 dataOut.profileIndex = dataOut.nProfiles - 1
907 dataOut.flagNoData = False
914 dataOut.flagNoData = False
908
915
909 return True
916 return True
910
917
911 """
918 """
912 data dimension = [nChannels, nHeis]
919 data dimension = [nChannels, nHeis]
913 """
920 """
914
921
915 if profileList != None:
922 if profileList != None:
916
923
917 if self.isThisProfileInList(dataOut.profileIndex, profileList):
924 if self.isThisProfileInList(dataOut.profileIndex, profileList):
918
925
919 self.nProfiles = len(profileList)
926 self.nProfiles = len(profileList)
920 dataOut.nProfiles = self.nProfiles
927 dataOut.nProfiles = self.nProfiles
921 dataOut.profileIndex = self.profileIndex
928 dataOut.profileIndex = self.profileIndex
922 dataOut.flagNoData = False
929 dataOut.flagNoData = False
923
930
924 self.incProfileIndex()
931 self.incProfileIndex()
925 return True
932 return True
926
933
927 if profileRangeList != None:
934 if profileRangeList != None:
928
935
929 minIndex = profileRangeList[0]
936 minIndex = profileRangeList[0]
930 maxIndex = profileRangeList[1]
937 maxIndex = profileRangeList[1]
931
938
932 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
939 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
933
940
934 self.nProfiles = maxIndex - minIndex + 1
941 self.nProfiles = maxIndex - minIndex + 1
935 dataOut.nProfiles = self.nProfiles
942 dataOut.nProfiles = self.nProfiles
936 dataOut.profileIndex = self.profileIndex
943 dataOut.profileIndex = self.profileIndex
937 dataOut.flagNoData = False
944 dataOut.flagNoData = False
938
945
939 self.incProfileIndex()
946 self.incProfileIndex()
940 return True
947 return True
941
948
942 if rangeList != None:
949 if rangeList != None:
943
950
944 nProfiles = 0
951 nProfiles = 0
945
952
946 for thisRange in rangeList:
953 for thisRange in rangeList:
947 minIndex = thisRange[0]
954 minIndex = thisRange[0]
948 maxIndex = thisRange[1]
955 maxIndex = thisRange[1]
949
956
950 nProfiles += maxIndex - minIndex + 1
957 nProfiles += maxIndex - minIndex + 1
951
958
952 for thisRange in rangeList:
959 for thisRange in rangeList:
953
960
954 minIndex = thisRange[0]
961 minIndex = thisRange[0]
955 maxIndex = thisRange[1]
962 maxIndex = thisRange[1]
956
963
957 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
964 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
958
965
959 self.nProfiles = nProfiles
966 self.nProfiles = nProfiles
960 dataOut.nProfiles = self.nProfiles
967 dataOut.nProfiles = self.nProfiles
961 dataOut.profileIndex = self.profileIndex
968 dataOut.profileIndex = self.profileIndex
962 dataOut.flagNoData = False
969 dataOut.flagNoData = False
963
970
964 self.incProfileIndex()
971 self.incProfileIndex()
965
972
966 break
973 break
967
974
968 return True
975 return True
969
976
970
977
971 if beam != None: #beam is only for AMISR data
978 if beam != None: #beam is only for AMISR data
972 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
979 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
973 dataOut.flagNoData = False
980 dataOut.flagNoData = False
974 dataOut.profileIndex = self.profileIndex
981 dataOut.profileIndex = self.profileIndex
975
982
976 self.incProfileIndex()
983 self.incProfileIndex()
977
984
978 return True
985 return True
979
986
980 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
987 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
981
988
982 return False
989 #return False
983
990 return dataOut
991 @MPDecorator
984 class Reshaper(Operation):
992 class Reshaper(Operation):
985
993
986 def __init__(self, **kwargs):
994 def __init__(self):#, **kwargs):
987
995
988 Operation.__init__(self, **kwargs)
996 Operation.__init__(self)#, **kwargs)
989
997
990 self.__buffer = None
998 self.__buffer = None
991 self.__nitems = 0
999 self.__nitems = 0
992
1000
993 def __appendProfile(self, dataOut, nTxs):
1001 def __appendProfile(self, dataOut, nTxs):
994
1002
995 if self.__buffer is None:
1003 if self.__buffer is None:
996 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
1004 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
997 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
1005 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
998
1006
999 ini = dataOut.nHeights * self.__nitems
1007 ini = dataOut.nHeights * self.__nitems
1000 end = ini + dataOut.nHeights
1008 end = ini + dataOut.nHeights
1001
1009
1002 self.__buffer[:, ini:end] = dataOut.data
1010 self.__buffer[:, ini:end] = dataOut.data
1003
1011
1004 self.__nitems += 1
1012 self.__nitems += 1
1005
1013
1006 return int(self.__nitems*nTxs)
1014 return int(self.__nitems*nTxs)
1007
1015
1008 def __getBuffer(self):
1016 def __getBuffer(self):
1009
1017
1010 if self.__nitems == int(1./self.__nTxs):
1018 if self.__nitems == int(1./self.__nTxs):
1011
1019
1012 self.__nitems = 0
1020 self.__nitems = 0
1013
1021
1014 return self.__buffer.copy()
1022 return self.__buffer.copy()
1015
1023
1016 return None
1024 return None
1017
1025
1018 def __checkInputs(self, dataOut, shape, nTxs):
1026 def __checkInputs(self, dataOut, shape, nTxs):
1019
1027
1020 if shape is None and nTxs is None:
1028 if shape is None and nTxs is None:
1021 raise ValueError("Reshaper: shape of factor should be defined")
1029 raise ValueError("Reshaper: shape of factor should be defined")
1022
1030
1023 if nTxs:
1031 if nTxs:
1024 if nTxs < 0:
1032 if nTxs < 0:
1025 raise ValueError("nTxs should be greater than 0")
1033 raise ValueError("nTxs should be greater than 0")
1026
1034
1027 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
1035 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
1028 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
1036 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
1029
1037
1030 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
1038 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
1031
1039
1032 return shape, nTxs
1040 return shape, nTxs
1033
1041
1034 if len(shape) != 2 and len(shape) != 3:
1042 if len(shape) != 2 and len(shape) != 3:
1035 raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights))
1043 raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights))
1036
1044
1037 if len(shape) == 2:
1045 if len(shape) == 2:
1038 shape_tuple = [dataOut.nChannels]
1046 shape_tuple = [dataOut.nChannels]
1039 shape_tuple.extend(shape)
1047 shape_tuple.extend(shape)
1040 else:
1048 else:
1041 shape_tuple = list(shape)
1049 shape_tuple = list(shape)
1042
1050
1043 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1051 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1044
1052
1045 return shape_tuple, nTxs
1053 return shape_tuple, nTxs
1046
1054
1047 def run(self, dataOut, shape=None, nTxs=None):
1055 def run(self, dataOut, shape=None, nTxs=None):
1048
1056
1049 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1057 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1050
1058
1051 dataOut.flagNoData = True
1059 dataOut.flagNoData = True
1052 profileIndex = None
1060 profileIndex = None
1053
1061
1054 if dataOut.flagDataAsBlock:
1062 if dataOut.flagDataAsBlock:
1055
1063
1056 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1064 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1057 dataOut.flagNoData = False
1065 dataOut.flagNoData = False
1058
1066
1059 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1067 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1060
1068
1061 else:
1069 else:
1062
1070
1063 if self.__nTxs < 1:
1071 if self.__nTxs < 1:
1064
1072
1065 self.__appendProfile(dataOut, self.__nTxs)
1073 self.__appendProfile(dataOut, self.__nTxs)
1066 new_data = self.__getBuffer()
1074 new_data = self.__getBuffer()
1067
1075
1068 if new_data is not None:
1076 if new_data is not None:
1069 dataOut.data = new_data
1077 dataOut.data = new_data
1070 dataOut.flagNoData = False
1078 dataOut.flagNoData = False
1071
1079
1072 profileIndex = dataOut.profileIndex*nTxs
1080 profileIndex = dataOut.profileIndex*nTxs
1073
1081
1074 else:
1082 else:
1075 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
1083 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
1076
1084
1077 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1085 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1078
1086
1079 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1087 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1080
1088
1081 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1089 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1082
1090
1083 dataOut.profileIndex = profileIndex
1091 dataOut.profileIndex = profileIndex
1084
1092
1085 dataOut.ippSeconds /= self.__nTxs
1093 dataOut.ippSeconds /= self.__nTxs
1086
1094
1095 return dataOut
1096 @MPDecorator
1087 class SplitProfiles(Operation):
1097 class SplitProfiles(Operation):
1088
1098
1089 def __init__(self, **kwargs):
1099 def __init__(self):#, **kwargs):
1090
1100
1091 Operation.__init__(self, **kwargs)
1101 Operation.__init__(self)#, **kwargs)
1092
1102
1093 def run(self, dataOut, n):
1103 def run(self, dataOut, n):
1094
1104
1095 dataOut.flagNoData = True
1105 dataOut.flagNoData = True
1096 profileIndex = None
1106 profileIndex = None
1097
1107
1098 if dataOut.flagDataAsBlock:
1108 if dataOut.flagDataAsBlock:
1099
1109
1100 #nchannels, nprofiles, nsamples
1110 #nchannels, nprofiles, nsamples
1101 shape = dataOut.data.shape
1111 shape = dataOut.data.shape
1102
1112
1103 if shape[2] % n != 0:
1113 if shape[2] % n != 0:
1104 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
1114 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
1105
1115
1106 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
1116 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
1117
1107 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1118 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1108 dataOut.flagNoData = False
1119 dataOut.flagNoData = False
1109
1120
1110 profileIndex = int(dataOut.nProfiles/n) - 1
1121 profileIndex = int(dataOut.nProfiles/n) - 1
1111
1122
1112 else:
1123 else:
1113
1124
1114 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
1125 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
1115
1126
1116 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1127 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1117
1128
1118 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1129 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1119
1130
1120 dataOut.nProfiles = int(dataOut.nProfiles*n)
1131 dataOut.nProfiles = int(dataOut.nProfiles*n)
1121
1132
1122 dataOut.profileIndex = profileIndex
1133 dataOut.profileIndex = profileIndex
1123
1134
1124 dataOut.ippSeconds /= n
1135 dataOut.ippSeconds /= n
1125
1136
1137 return dataOut
1138 @MPDecorator
1126 class CombineProfiles(Operation):
1139 class CombineProfiles(Operation):
1127 def __init__(self, **kwargs):
1140 def __init__(self):#, **kwargs):
1128
1141
1129 Operation.__init__(self, **kwargs)
1142 Operation.__init__(self)#, **kwargs)
1130
1143
1131 self.__remData = None
1144 self.__remData = None
1132 self.__profileIndex = 0
1145 self.__profileIndex = 0
1133
1146
1134 def run(self, dataOut, n):
1147 def run(self, dataOut, n):
1135
1148
1136 dataOut.flagNoData = True
1149 dataOut.flagNoData = True
1137 profileIndex = None
1150 profileIndex = None
1138
1151
1139 if dataOut.flagDataAsBlock:
1152 if dataOut.flagDataAsBlock:
1140
1153
1141 #nchannels, nprofiles, nsamples
1154 #nchannels, nprofiles, nsamples
1142 shape = dataOut.data.shape
1155 shape = dataOut.data.shape
1143 new_shape = shape[0], shape[1]/n, shape[2]*n
1156 new_shape = shape[0], shape[1]/n, shape[2]*n
1144
1157
1145 if shape[1] % n != 0:
1158 if shape[1] % n != 0:
1146 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
1159 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
1147
1160
1148 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1161 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1149 dataOut.flagNoData = False
1162 dataOut.flagNoData = False
1150
1163
1151 profileIndex = int(dataOut.nProfiles*n) - 1
1164 profileIndex = int(dataOut.nProfiles*n) - 1
1152
1165
1153 else:
1166 else:
1154
1167
1155 #nchannels, nsamples
1168 #nchannels, nsamples
1156 if self.__remData is None:
1169 if self.__remData is None:
1157 newData = dataOut.data
1170 newData = dataOut.data
1158 else:
1171 else:
1159 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1172 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1160
1173
1161 self.__profileIndex += 1
1174 self.__profileIndex += 1
1162
1175
1163 if self.__profileIndex < n:
1176 if self.__profileIndex < n:
1164 self.__remData = newData
1177 self.__remData = newData
1165 #continue
1178 #continue
1166 return
1179 return
1167
1180
1168 self.__profileIndex = 0
1181 self.__profileIndex = 0
1169 self.__remData = None
1182 self.__remData = None
1170
1183
1171 dataOut.data = newData
1184 dataOut.data = newData
1172 dataOut.flagNoData = False
1185 dataOut.flagNoData = False
1173
1186
1174 profileIndex = dataOut.profileIndex/n
1187 profileIndex = dataOut.profileIndex/n
1175
1188
1176
1189
1177 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1190 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1178
1191
1179 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1192 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1180
1193
1181 dataOut.nProfiles = int(dataOut.nProfiles/n)
1194 dataOut.nProfiles = int(dataOut.nProfiles/n)
1182
1195
1183 dataOut.profileIndex = profileIndex
1196 dataOut.profileIndex = profileIndex
1184
1197
1185 dataOut.ippSeconds *= n
1198 dataOut.ippSeconds *= n
1186
1199
1200 return dataOut
1187 # import collections
1201 # import collections
1188 # from scipy.stats import mode
1202 # from scipy.stats import mode
1189 #
1203 #
1190 # class Synchronize(Operation):
1204 # class Synchronize(Operation):
1191 #
1205 #
1192 # isConfig = False
1206 # isConfig = False
1193 # __profIndex = 0
1207 # __profIndex = 0
1194 #
1208 #
1195 # def __init__(self, **kwargs):
1209 # def __init__(self, **kwargs):
1196 #
1210 #
1197 # Operation.__init__(self, **kwargs)
1211 # Operation.__init__(self, **kwargs)
1198 # # self.isConfig = False
1212 # # self.isConfig = False
1199 # self.__powBuffer = None
1213 # self.__powBuffer = None
1200 # self.__startIndex = 0
1214 # self.__startIndex = 0
1201 # self.__pulseFound = False
1215 # self.__pulseFound = False
1202 #
1216 #
1203 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1217 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1204 #
1218 #
1205 # #Read data
1219 # #Read data
1206 #
1220 #
1207 # powerdB = dataOut.getPower(channel = channel)
1221 # powerdB = dataOut.getPower(channel = channel)
1208 # noisedB = dataOut.getNoise(channel = channel)[0]
1222 # noisedB = dataOut.getNoise(channel = channel)[0]
1209 #
1223 #
1210 # self.__powBuffer.extend(powerdB.flatten())
1224 # self.__powBuffer.extend(powerdB.flatten())
1211 #
1225 #
1212 # dataArray = numpy.array(self.__powBuffer)
1226 # dataArray = numpy.array(self.__powBuffer)
1213 #
1227 #
1214 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1228 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1215 #
1229 #
1216 # maxValue = numpy.nanmax(filteredPower)
1230 # maxValue = numpy.nanmax(filteredPower)
1217 #
1231 #
1218 # if maxValue < noisedB + 10:
1232 # if maxValue < noisedB + 10:
1219 # #No se encuentra ningun pulso de transmision
1233 # #No se encuentra ningun pulso de transmision
1220 # return None
1234 # return None
1221 #
1235 #
1222 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1236 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1223 #
1237 #
1224 # if len(maxValuesIndex) < 2:
1238 # if len(maxValuesIndex) < 2:
1225 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1239 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1226 # return None
1240 # return None
1227 #
1241 #
1228 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1242 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1229 #
1243 #
1230 # #Seleccionar solo valores con un espaciamiento de nSamples
1244 # #Seleccionar solo valores con un espaciamiento de nSamples
1231 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1245 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1232 #
1246 #
1233 # if len(pulseIndex) < 2:
1247 # if len(pulseIndex) < 2:
1234 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1248 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1235 # return None
1249 # return None
1236 #
1250 #
1237 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1251 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1238 #
1252 #
1239 # #remover senales que se distancien menos de 10 unidades o muestras
1253 # #remover senales que se distancien menos de 10 unidades o muestras
1240 # #(No deberian existir IPP menor a 10 unidades)
1254 # #(No deberian existir IPP menor a 10 unidades)
1241 #
1255 #
1242 # realIndex = numpy.where(spacing > 10 )[0]
1256 # realIndex = numpy.where(spacing > 10 )[0]
1243 #
1257 #
1244 # if len(realIndex) < 2:
1258 # if len(realIndex) < 2:
1245 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1259 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1246 # return None
1260 # return None
1247 #
1261 #
1248 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1262 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1249 # realPulseIndex = pulseIndex[realIndex]
1263 # realPulseIndex = pulseIndex[realIndex]
1250 #
1264 #
1251 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1265 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1252 #
1266 #
1253 # print "IPP = %d samples" %period
1267 # print "IPP = %d samples" %period
1254 #
1268 #
1255 # self.__newNSamples = dataOut.nHeights #int(period)
1269 # self.__newNSamples = dataOut.nHeights #int(period)
1256 # self.__startIndex = int(realPulseIndex[0])
1270 # self.__startIndex = int(realPulseIndex[0])
1257 #
1271 #
1258 # return 1
1272 # return 1
1259 #
1273 #
1260 #
1274 #
1261 # def setup(self, nSamples, nChannels, buffer_size = 4):
1275 # def setup(self, nSamples, nChannels, buffer_size = 4):
1262 #
1276 #
1263 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1277 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1264 # maxlen = buffer_size*nSamples)
1278 # maxlen = buffer_size*nSamples)
1265 #
1279 #
1266 # bufferList = []
1280 # bufferList = []
1267 #
1281 #
1268 # for i in range(nChannels):
1282 # for i in range(nChannels):
1269 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1283 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1270 # maxlen = buffer_size*nSamples)
1284 # maxlen = buffer_size*nSamples)
1271 #
1285 #
1272 # bufferList.append(bufferByChannel)
1286 # bufferList.append(bufferByChannel)
1273 #
1287 #
1274 # self.__nSamples = nSamples
1288 # self.__nSamples = nSamples
1275 # self.__nChannels = nChannels
1289 # self.__nChannels = nChannels
1276 # self.__bufferList = bufferList
1290 # self.__bufferList = bufferList
1277 #
1291 #
1278 # def run(self, dataOut, channel = 0):
1292 # def run(self, dataOut, channel = 0):
1279 #
1293 #
1280 # if not self.isConfig:
1294 # if not self.isConfig:
1281 # nSamples = dataOut.nHeights
1295 # nSamples = dataOut.nHeights
1282 # nChannels = dataOut.nChannels
1296 # nChannels = dataOut.nChannels
1283 # self.setup(nSamples, nChannels)
1297 # self.setup(nSamples, nChannels)
1284 # self.isConfig = True
1298 # self.isConfig = True
1285 #
1299 #
1286 # #Append new data to internal buffer
1300 # #Append new data to internal buffer
1287 # for thisChannel in range(self.__nChannels):
1301 # for thisChannel in range(self.__nChannels):
1288 # bufferByChannel = self.__bufferList[thisChannel]
1302 # bufferByChannel = self.__bufferList[thisChannel]
1289 # bufferByChannel.extend(dataOut.data[thisChannel])
1303 # bufferByChannel.extend(dataOut.data[thisChannel])
1290 #
1304 #
1291 # if self.__pulseFound:
1305 # if self.__pulseFound:
1292 # self.__startIndex -= self.__nSamples
1306 # self.__startIndex -= self.__nSamples
1293 #
1307 #
1294 # #Finding Tx Pulse
1308 # #Finding Tx Pulse
1295 # if not self.__pulseFound:
1309 # if not self.__pulseFound:
1296 # indexFound = self.__findTxPulse(dataOut, channel)
1310 # indexFound = self.__findTxPulse(dataOut, channel)
1297 #
1311 #
1298 # if indexFound == None:
1312 # if indexFound == None:
1299 # dataOut.flagNoData = True
1313 # dataOut.flagNoData = True
1300 # return
1314 # return
1301 #
1315 #
1302 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1316 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1303 # self.__pulseFound = True
1317 # self.__pulseFound = True
1304 # self.__startIndex = indexFound
1318 # self.__startIndex = indexFound
1305 #
1319 #
1306 # #If pulse was found ...
1320 # #If pulse was found ...
1307 # for thisChannel in range(self.__nChannels):
1321 # for thisChannel in range(self.__nChannels):
1308 # bufferByChannel = self.__bufferList[thisChannel]
1322 # bufferByChannel = self.__bufferList[thisChannel]
1309 # #print self.__startIndex
1323 # #print self.__startIndex
1310 # x = numpy.array(bufferByChannel)
1324 # x = numpy.array(bufferByChannel)
1311 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1325 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1312 #
1326 #
1313 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1327 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1314 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1328 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1315 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1329 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1316 #
1330 #
1317 # dataOut.data = self.__arrayBuffer
1331 # dataOut.data = self.__arrayBuffer
1318 #
1332 #
1319 # self.__startIndex += self.__newNSamples
1333 # self.__startIndex += self.__newNSamples
1320 #
1334 #
1321 # return No newline at end of file
1335 # return
General Comments 0
You need to be logged in to leave comments. Login now