##// END OF EJS Templates
Merge branch 'schain_mp' of http://jro-dev.igp.gob.pe/rhodecode/schain into schain_mp
José Chávez -
r940:3b577291ba9a merge
parent child
Show More
@@ -1,1225 +1,1228
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: JROData.py 173 2012-11-20 15:06:21Z murco $
4 $Id: JROData.py 173 2012-11-20 15:06:21Z murco $
5 '''
5 '''
6
6
7 import copy
7 import copy
8 import numpy
8 import numpy
9 import datetime
9 import datetime
10
10
11 from jroheaderIO import SystemHeader, RadarControllerHeader
11 from jroheaderIO import SystemHeader, RadarControllerHeader
12 from schainpy import cSchain
12 from schainpy import cSchain
13
13
14
14
15 def getNumpyDtype(dataTypeCode):
15 def getNumpyDtype(dataTypeCode):
16
16
17 if dataTypeCode == 0:
17 if dataTypeCode == 0:
18 numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')])
18 numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')])
19 elif dataTypeCode == 1:
19 elif dataTypeCode == 1:
20 numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')])
20 numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')])
21 elif dataTypeCode == 2:
21 elif dataTypeCode == 2:
22 numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')])
22 numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')])
23 elif dataTypeCode == 3:
23 elif dataTypeCode == 3:
24 numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')])
24 numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')])
25 elif dataTypeCode == 4:
25 elif dataTypeCode == 4:
26 numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')])
26 numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')])
27 elif dataTypeCode == 5:
27 elif dataTypeCode == 5:
28 numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')])
28 numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')])
29 else:
29 else:
30 raise ValueError, 'dataTypeCode was not defined'
30 raise ValueError, 'dataTypeCode was not defined'
31
31
32 return numpyDtype
32 return numpyDtype
33
33
34 def getDataTypeCode(numpyDtype):
34 def getDataTypeCode(numpyDtype):
35
35
36 if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]):
36 if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]):
37 datatype = 0
37 datatype = 0
38 elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]):
38 elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]):
39 datatype = 1
39 datatype = 1
40 elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]):
40 elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]):
41 datatype = 2
41 datatype = 2
42 elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]):
42 elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]):
43 datatype = 3
43 datatype = 3
44 elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]):
44 elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]):
45 datatype = 4
45 datatype = 4
46 elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]):
46 elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]):
47 datatype = 5
47 datatype = 5
48 else:
48 else:
49 datatype = None
49 datatype = None
50
50
51 return datatype
51 return datatype
52
52
53 def hildebrand_sekhon(data, navg):
53 def hildebrand_sekhon(data, navg):
54 """
54 """
55 This method is for the objective determination of the noise level in Doppler spectra. This
55 This method is for the objective determination of the noise level in Doppler spectra. This
56 implementation technique is based on the fact that the standard deviation of the spectral
56 implementation technique is based on the fact that the standard deviation of the spectral
57 densities is equal to the mean spectral density for white Gaussian noise
57 densities is equal to the mean spectral density for white Gaussian noise
58
58
59 Inputs:
59 Inputs:
60 Data : heights
60 Data : heights
61 navg : numbers of averages
61 navg : numbers of averages
62
62
63 Return:
63 Return:
64 -1 : any error
64 -1 : any error
65 anoise : noise's level
65 anoise : noise's level
66 """
66 """
67
67
68 sortdata = numpy.sort(data, axis=None)
68 sortdata = numpy.sort(data, axis=None)
69 # lenOfData = len(sortdata)
69 # lenOfData = len(sortdata)
70 # nums_min = lenOfData*0.2
70 # nums_min = lenOfData*0.2
71 #
71 #
72 # if nums_min <= 5:
72 # if nums_min <= 5:
73 # nums_min = 5
73 # nums_min = 5
74 #
74 #
75 # sump = 0.
75 # sump = 0.
76 #
76 #
77 # sumq = 0.
77 # sumq = 0.
78 #
78 #
79 # j = 0
79 # j = 0
80 #
80 #
81 # cont = 1
81 # cont = 1
82 #
82 #
83 # while((cont==1)and(j<lenOfData)):
83 # while((cont==1)and(j<lenOfData)):
84 #
84 #
85 # sump += sortdata[j]
85 # sump += sortdata[j]
86 #
86 #
87 # sumq += sortdata[j]**2
87 # sumq += sortdata[j]**2
88 #
88 #
89 # if j > nums_min:
89 # if j > nums_min:
90 # rtest = float(j)/(j-1) + 1.0/navg
90 # rtest = float(j)/(j-1) + 1.0/navg
91 # if ((sumq*j) > (rtest*sump**2)):
91 # if ((sumq*j) > (rtest*sump**2)):
92 # j = j - 1
92 # j = j - 1
93 # sump = sump - sortdata[j]
93 # sump = sump - sortdata[j]
94 # sumq = sumq - sortdata[j]**2
94 # sumq = sumq - sortdata[j]**2
95 # cont = 0
95 # cont = 0
96 #
96 #
97 # j += 1
97 # j += 1
98 #
98 #
99 # lnoise = sump /j
99 # lnoise = sump /j
100 #
100 #
101 # return lnoise
101 # return lnoise
102
102
103 return cSchain.hildebrand_sekhon(sortdata, navg)
103 return cSchain.hildebrand_sekhon(sortdata, navg)
104
104
105
105
106 class Beam:
106 class Beam:
107
107
108 def __init__(self):
108 def __init__(self):
109 self.codeList = []
109 self.codeList = []
110 self.azimuthList = []
110 self.azimuthList = []
111 self.zenithList = []
111 self.zenithList = []
112
112
113 class GenericData(object):
113 class GenericData(object):
114
114
115 flagNoData = True
115 flagNoData = True
116
116
117 def __init__(self):
117 def __init__(self):
118
118
119 raise NotImplementedError
119 raise NotImplementedError
120
120
121 def copy(self, inputObj=None):
121 def copy(self, inputObj=None):
122
122
123 if inputObj == None:
123 if inputObj == None:
124 return copy.deepcopy(self)
124 return copy.deepcopy(self)
125
125
126 for key in inputObj.__dict__.keys():
126 for key in inputObj.__dict__.keys():
127
127
128 attribute = inputObj.__dict__[key]
128 attribute = inputObj.__dict__[key]
129
129
130 #If this attribute is a tuple or list
130 #If this attribute is a tuple or list
131 if type(inputObj.__dict__[key]) in (tuple, list):
131 if type(inputObj.__dict__[key]) in (tuple, list):
132 self.__dict__[key] = attribute[:]
132 self.__dict__[key] = attribute[:]
133 continue
133 continue
134
134
135 #If this attribute is another object or instance
135 #If this attribute is another object or instance
136 if hasattr(attribute, '__dict__'):
136 if hasattr(attribute, '__dict__'):
137 self.__dict__[key] = attribute.copy()
137 self.__dict__[key] = attribute.copy()
138 continue
138 continue
139
139
140 self.__dict__[key] = inputObj.__dict__[key]
140 self.__dict__[key] = inputObj.__dict__[key]
141
141
142 def deepcopy(self):
142 def deepcopy(self):
143
143
144 return copy.deepcopy(self)
144 return copy.deepcopy(self)
145
145
146 def isEmpty(self):
146 def isEmpty(self):
147
147
148 return self.flagNoData
148 return self.flagNoData
149
149
150 class JROData(GenericData):
150 class JROData(GenericData):
151
151
152 # m_BasicHeader = BasicHeader()
152 # m_BasicHeader = BasicHeader()
153 # m_ProcessingHeader = ProcessingHeader()
153 # m_ProcessingHeader = ProcessingHeader()
154
154
155 systemHeaderObj = SystemHeader()
155 systemHeaderObj = SystemHeader()
156
156
157 radarControllerHeaderObj = RadarControllerHeader()
157 radarControllerHeaderObj = RadarControllerHeader()
158
158
159 # data = None
159 # data = None
160
160
161 type = None
161 type = None
162
162
163 datatype = None #dtype but in string
163 datatype = None #dtype but in string
164
164
165 # dtype = None
165 # dtype = None
166
166
167 # nChannels = None
167 # nChannels = None
168
168
169 # nHeights = None
169 # nHeights = None
170
170
171 nProfiles = None
171 nProfiles = None
172
172
173 heightList = None
173 heightList = None
174
174
175 channelList = None
175 channelList = None
176
176
177 flagDiscontinuousBlock = False
177 flagDiscontinuousBlock = False
178
178
179 useLocalTime = False
179 useLocalTime = False
180
180
181 utctime = None
181 utctime = None
182
182
183 timeZone = None
183 timeZone = None
184
184
185 dstFlag = None
185 dstFlag = None
186
186
187 errorCount = None
187 errorCount = None
188
188
189 blocksize = None
189 blocksize = None
190
190
191 # nCode = None
191 # nCode = None
192 #
192 #
193 # nBaud = None
193 # nBaud = None
194 #
194 #
195 # code = None
195 # code = None
196
196
197 flagDecodeData = False #asumo q la data no esta decodificada
197 flagDecodeData = False #asumo q la data no esta decodificada
198
198
199 flagDeflipData = False #asumo q la data no esta sin flip
199 flagDeflipData = False #asumo q la data no esta sin flip
200
200
201 flagShiftFFT = False
201 flagShiftFFT = False
202
202
203 # ippSeconds = None
203 # ippSeconds = None
204
204
205 # timeInterval = None
205 # timeInterval = None
206
206
207 nCohInt = None
207 nCohInt = None
208
208
209 # noise = None
209 # noise = None
210
210
211 windowOfFilter = 1
211 windowOfFilter = 1
212
212
213 #Speed of ligth
213 #Speed of ligth
214 C = 3e8
214 C = 3e8
215
215
216 frequency = 49.92e6
216 frequency = 49.92e6
217
217
218 realtime = False
218 realtime = False
219
219
220 beacon_heiIndexList = None
220 beacon_heiIndexList = None
221
221
222 last_block = None
222 last_block = None
223
223
224 blocknow = None
224 blocknow = None
225
225
226 azimuth = None
226 azimuth = None
227
227
228 zenith = None
228 zenith = None
229
229
230 beam = Beam()
230 beam = Beam()
231
231
232 profileIndex = None
232 profileIndex = None
233
233
234 def __init__(self):
234 def __init__(self):
235
235
236 raise NotImplementedError
236 raise NotImplementedError
237
237
238 def getNoise(self):
238 def getNoise(self):
239
239
240 raise NotImplementedError
240 raise NotImplementedError
241
241
242 def getNChannels(self):
242 def getNChannels(self):
243
243
244 return len(self.channelList)
244 return len(self.channelList)
245
245
246 def getChannelIndexList(self):
246 def getChannelIndexList(self):
247
247
248 return range(self.nChannels)
248 return range(self.nChannels)
249
249
250 def getNHeights(self):
250 def getNHeights(self):
251
251
252 return len(self.heightList)
252 return len(self.heightList)
253
253
254 def getHeiRange(self, extrapoints=0):
254 def getHeiRange(self, extrapoints=0):
255
255
256 heis = self.heightList
256 heis = self.heightList
257 # deltah = self.heightList[1] - self.heightList[0]
257 # deltah = self.heightList[1] - self.heightList[0]
258 #
258 #
259 # heis.append(self.heightList[-1])
259 # heis.append(self.heightList[-1])
260
260
261 return heis
261 return heis
262
262
263 def getDeltaH(self):
263 def getDeltaH(self):
264
264
265 delta = self.heightList[1] - self.heightList[0]
265 delta = self.heightList[1] - self.heightList[0]
266
266
267 return delta
267 return delta
268
268
269 def getltctime(self):
269 def getltctime(self):
270
270
271 if self.useLocalTime:
271 if self.useLocalTime:
272 return self.utctime - self.timeZone*60
272 return self.utctime - self.timeZone*60
273
273
274 return self.utctime
274 return self.utctime
275
275
276 def getDatatime(self):
276 def getDatatime(self):
277
277
278 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
278 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
279 return datatimeValue
279 return datatimeValue
280
280
281 def getTimeRange(self):
281 def getTimeRange(self):
282
282
283 datatime = []
283 datatime = []
284
284
285 datatime.append(self.ltctime)
285 datatime.append(self.ltctime)
286 datatime.append(self.ltctime + self.timeInterval+1)
286 datatime.append(self.ltctime + self.timeInterval+1)
287
287
288 datatime = numpy.array(datatime)
288 datatime = numpy.array(datatime)
289
289
290 return datatime
290 return datatime
291
291
292 def getFmaxTimeResponse(self):
292 def getFmaxTimeResponse(self):
293
293
294 period = (10**-6)*self.getDeltaH()/(0.15)
294 period = (10**-6)*self.getDeltaH()/(0.15)
295
295
296 PRF = 1./(period * self.nCohInt)
296 PRF = 1./(period * self.nCohInt)
297
297
298 fmax = PRF
298 fmax = PRF
299
299
300 return fmax
300 return fmax
301
301
302 def getFmax(self):
302 def getFmax(self):
303
303
304 PRF = 1./(self.ippSeconds * self.nCohInt)
304 PRF = 1./(self.ippSeconds * self.nCohInt)
305
305
306 fmax = PRF
306 fmax = PRF
307
307
308 return fmax
308 return fmax
309
309
310 def getVmax(self):
310 def getVmax(self):
311
311
312 _lambda = self.C/self.frequency
312 _lambda = self.C/self.frequency
313
313
314 vmax = self.getFmax() * _lambda/2
314 vmax = self.getFmax() * _lambda/2
315
315
316 return vmax
316 return vmax
317
317
318 def get_ippSeconds(self):
318 def get_ippSeconds(self):
319 '''
319 '''
320 '''
320 '''
321 return self.radarControllerHeaderObj.ippSeconds
321 return self.radarControllerHeaderObj.ippSeconds
322
322
323 def set_ippSeconds(self, ippSeconds):
323 def set_ippSeconds(self, ippSeconds):
324 '''
324 '''
325 '''
325 '''
326
326
327 self.radarControllerHeaderObj.ippSeconds = ippSeconds
327 self.radarControllerHeaderObj.ippSeconds = ippSeconds
328
328
329 return
329 return
330
330
331 def get_dtype(self):
331 def get_dtype(self):
332 '''
332 '''
333 '''
333 '''
334 return getNumpyDtype(self.datatype)
334 return getNumpyDtype(self.datatype)
335
335
336 def set_dtype(self, numpyDtype):
336 def set_dtype(self, numpyDtype):
337 '''
337 '''
338 '''
338 '''
339
339
340 self.datatype = getDataTypeCode(numpyDtype)
340 self.datatype = getDataTypeCode(numpyDtype)
341
341
342 def get_code(self):
342 def get_code(self):
343 '''
343 '''
344 '''
344 '''
345 return self.radarControllerHeaderObj.code
345 return self.radarControllerHeaderObj.code
346
346
347 def set_code(self, code):
347 def set_code(self, code):
348 '''
348 '''
349 '''
349 '''
350 self.radarControllerHeaderObj.code = code
350 self.radarControllerHeaderObj.code = code
351
351
352 return
352 return
353
353
354 def get_ncode(self):
354 def get_ncode(self):
355 '''
355 '''
356 '''
356 '''
357 return self.radarControllerHeaderObj.nCode
357 return self.radarControllerHeaderObj.nCode
358
358
359 def set_ncode(self, nCode):
359 def set_ncode(self, nCode):
360 '''
360 '''
361 '''
361 '''
362 self.radarControllerHeaderObj.nCode = nCode
362 self.radarControllerHeaderObj.nCode = nCode
363
363
364 return
364 return
365
365
366 def get_nbaud(self):
366 def get_nbaud(self):
367 '''
367 '''
368 '''
368 '''
369 return self.radarControllerHeaderObj.nBaud
369 return self.radarControllerHeaderObj.nBaud
370
370
371 def set_nbaud(self, nBaud):
371 def set_nbaud(self, nBaud):
372 '''
372 '''
373 '''
373 '''
374 self.radarControllerHeaderObj.nBaud = nBaud
374 self.radarControllerHeaderObj.nBaud = nBaud
375
375
376 return
376 return
377
377
378 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
378 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
379 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
379 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
380 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
380 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
381 #noise = property(getNoise, "I'm the 'nHeights' property.")
381 #noise = property(getNoise, "I'm the 'nHeights' property.")
382 datatime = property(getDatatime, "I'm the 'datatime' property")
382 datatime = property(getDatatime, "I'm the 'datatime' property")
383 ltctime = property(getltctime, "I'm the 'ltctime' property")
383 ltctime = property(getltctime, "I'm the 'ltctime' property")
384 ippSeconds = property(get_ippSeconds, set_ippSeconds)
384 ippSeconds = property(get_ippSeconds, set_ippSeconds)
385 dtype = property(get_dtype, set_dtype)
385 dtype = property(get_dtype, set_dtype)
386 # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
386 # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
387 code = property(get_code, set_code)
387 code = property(get_code, set_code)
388 nCode = property(get_ncode, set_ncode)
388 nCode = property(get_ncode, set_ncode)
389 nBaud = property(get_nbaud, set_nbaud)
389 nBaud = property(get_nbaud, set_nbaud)
390
390
391 class Voltage(JROData):
391 class Voltage(JROData):
392
392
393 #data es un numpy array de 2 dmensiones (canales, alturas)
393 #data es un numpy array de 2 dmensiones (canales, alturas)
394 data = None
394 data = None
395
395
396 def __init__(self):
396 def __init__(self):
397 '''
397 '''
398 Constructor
398 Constructor
399 '''
399 '''
400
400
401 self.useLocalTime = True
401 self.useLocalTime = True
402
402
403 self.radarControllerHeaderObj = RadarControllerHeader()
403 self.radarControllerHeaderObj = RadarControllerHeader()
404
404
405 self.systemHeaderObj = SystemHeader()
405 self.systemHeaderObj = SystemHeader()
406
406
407 self.type = "Voltage"
407 self.type = "Voltage"
408
408
409 self.data = None
409 self.data = None
410
410
411 # self.dtype = None
411 # self.dtype = None
412
412
413 # self.nChannels = 0
413 # self.nChannels = 0
414
414
415 # self.nHeights = 0
415 # self.nHeights = 0
416
416
417 self.nProfiles = None
417 self.nProfiles = None
418
418
419 self.heightList = None
419 self.heightList = None
420
420
421 self.channelList = None
421 self.channelList = None
422
422
423 # self.channelIndexList = None
423 # self.channelIndexList = None
424
424
425 self.flagNoData = True
425 self.flagNoData = True
426
426
427 self.flagDiscontinuousBlock = False
427 self.flagDiscontinuousBlock = False
428
428
429 self.utctime = None
429 self.utctime = None
430
430
431 self.timeZone = None
431 self.timeZone = None
432
432
433 self.dstFlag = None
433 self.dstFlag = None
434
434
435 self.errorCount = None
435 self.errorCount = None
436
436
437 self.nCohInt = None
437 self.nCohInt = None
438
438
439 self.blocksize = None
439 self.blocksize = None
440
440
441 self.flagDecodeData = False #asumo q la data no esta decodificada
441 self.flagDecodeData = False #asumo q la data no esta decodificada
442
442
443 self.flagDeflipData = False #asumo q la data no esta sin flip
443 self.flagDeflipData = False #asumo q la data no esta sin flip
444
444
445 self.flagShiftFFT = False
445 self.flagShiftFFT = False
446
446
447 self.flagDataAsBlock = False #Asumo que la data es leida perfil a perfil
447 self.flagDataAsBlock = False #Asumo que la data es leida perfil a perfil
448
448
449 self.profileIndex = 0
449 self.profileIndex = 0
450
450
451 def getNoisebyHildebrand(self, channel = None):
451 def getNoisebyHildebrand(self, channel = None):
452 """
452 """
453 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
453 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
454
454
455 Return:
455 Return:
456 noiselevel
456 noiselevel
457 """
457 """
458
458
459 if channel != None:
459 if channel != None:
460 data = self.data[channel]
460 data = self.data[channel]
461 nChannels = 1
461 nChannels = 1
462 else:
462 else:
463 data = self.data
463 data = self.data
464 nChannels = self.nChannels
464 nChannels = self.nChannels
465
465
466 noise = numpy.zeros(nChannels)
466 noise = numpy.zeros(nChannels)
467 power = data * numpy.conjugate(data)
467 power = data * numpy.conjugate(data)
468
468
469 for thisChannel in range(nChannels):
469 for thisChannel in range(nChannels):
470 if nChannels == 1:
470 if nChannels == 1:
471 daux = power[:].real
471 daux = power[:].real
472 else:
472 else:
473 daux = power[thisChannel,:].real
473 daux = power[thisChannel,:].real
474 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
474 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
475
475
476 return noise
476 return noise
477
477
478 def getNoise(self, type = 1, channel = None):
478 def getNoise(self, type = 1, channel = None):
479
479
480 if type == 1:
480 if type == 1:
481 noise = self.getNoisebyHildebrand(channel)
481 noise = self.getNoisebyHildebrand(channel)
482
482
483 return noise
483 return noise
484
484
485 def getPower(self, channel = None):
485 def getPower(self, channel = None):
486
486
487 if channel != None:
487 if channel != None:
488 data = self.data[channel]
488 data = self.data[channel]
489 else:
489 else:
490 data = self.data
490 data = self.data
491
491
492 power = data * numpy.conjugate(data)
492 power = data * numpy.conjugate(data)
493 powerdB = 10*numpy.log10(power.real)
493 powerdB = 10*numpy.log10(power.real)
494 powerdB = numpy.squeeze(powerdB)
494 powerdB = numpy.squeeze(powerdB)
495
495
496 return powerdB
496 return powerdB
497
497
498 def getTimeInterval(self):
498 def getTimeInterval(self):
499
499
500 timeInterval = self.ippSeconds * self.nCohInt
500 timeInterval = self.ippSeconds * self.nCohInt
501
501
502 return timeInterval
502 return timeInterval
503
503
504 noise = property(getNoise, "I'm the 'nHeights' property.")
504 noise = property(getNoise, "I'm the 'nHeights' property.")
505 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
505 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
506
506
507 class Spectra(JROData):
507 class Spectra(JROData):
508
508
509 #data spc es un numpy array de 2 dmensiones (canales, perfiles, alturas)
509 #data spc es un numpy array de 2 dmensiones (canales, perfiles, alturas)
510 data_spc = None
510 data_spc = None
511
511
512 #data cspc es un numpy array de 2 dmensiones (canales, pares, alturas)
512 #data cspc es un numpy array de 2 dmensiones (canales, pares, alturas)
513 data_cspc = None
513 data_cspc = None
514
514
515 #data dc es un numpy array de 2 dmensiones (canales, alturas)
515 #data dc es un numpy array de 2 dmensiones (canales, alturas)
516 data_dc = None
516 data_dc = None
517
517
518 #data power
518 #data power
519 data_pwr = None
519 data_pwr = None
520
520
521 nFFTPoints = None
521 nFFTPoints = None
522
522
523 # nPairs = None
523 # nPairs = None
524
524
525 pairsList = None
525 pairsList = None
526
526
527 nIncohInt = None
527 nIncohInt = None
528
528
529 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
529 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
530
530
531 nCohInt = None #se requiere para determinar el valor de timeInterval
531 nCohInt = None #se requiere para determinar el valor de timeInterval
532
532
533 ippFactor = None
533 ippFactor = None
534
534
535 profileIndex = 0
535 profileIndex = 0
536
536
537 plotting = "spectra"
537 plotting = "spectra"
538
538
539 def __init__(self):
539 def __init__(self):
540 '''
540 '''
541 Constructor
541 Constructor
542 '''
542 '''
543
543
544 self.useLocalTime = True
544 self.useLocalTime = True
545
545
546 self.radarControllerHeaderObj = RadarControllerHeader()
546 self.radarControllerHeaderObj = RadarControllerHeader()
547
547
548 self.systemHeaderObj = SystemHeader()
548 self.systemHeaderObj = SystemHeader()
549
549
550 self.type = "Spectra"
550 self.type = "Spectra"
551
551
552 # self.data = None
552 # self.data = None
553
553
554 # self.dtype = None
554 # self.dtype = None
555
555
556 # self.nChannels = 0
556 # self.nChannels = 0
557
557
558 # self.nHeights = 0
558 # self.nHeights = 0
559
559
560 self.nProfiles = None
560 self.nProfiles = None
561
561
562 self.heightList = None
562 self.heightList = None
563
563
564 self.channelList = None
564 self.channelList = None
565
565
566 # self.channelIndexList = None
566 # self.channelIndexList = None
567
567
568 self.pairsList = None
568 self.pairsList = None
569
569
570 self.flagNoData = True
570 self.flagNoData = True
571
571
572 self.flagDiscontinuousBlock = False
572 self.flagDiscontinuousBlock = False
573
573
574 self.utctime = None
574 self.utctime = None
575
575
576 self.nCohInt = None
576 self.nCohInt = None
577
577
578 self.nIncohInt = None
578 self.nIncohInt = None
579
579
580 self.blocksize = None
580 self.blocksize = None
581
581
582 self.nFFTPoints = None
582 self.nFFTPoints = None
583
583
584 self.wavelength = None
584 self.wavelength = None
585
585
586 self.flagDecodeData = False #asumo q la data no esta decodificada
586 self.flagDecodeData = False #asumo q la data no esta decodificada
587
587
588 self.flagDeflipData = False #asumo q la data no esta sin flip
588 self.flagDeflipData = False #asumo q la data no esta sin flip
589
589
590 self.flagShiftFFT = False
590 self.flagShiftFFT = False
591
591
592 self.ippFactor = 1
592 self.ippFactor = 1
593
593
594 #self.noise = None
594 #self.noise = None
595
595
596 self.beacon_heiIndexList = []
596 self.beacon_heiIndexList = []
597
597
598 self.noise_estimation = None
598 self.noise_estimation = None
599
599
600
600
601 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
601 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
602 """
602 """
603 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
603 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
604
604
605 Return:
605 Return:
606 noiselevel
606 noiselevel
607 """
607 """
608
608
609 noise = numpy.zeros(self.nChannels)
609 noise = numpy.zeros(self.nChannels)
610
610
611 for channel in range(self.nChannels):
611 for channel in range(self.nChannels):
612 daux = self.data_spc[channel,xmin_index:xmax_index,ymin_index:ymax_index]
612 daux = self.data_spc[channel,xmin_index:xmax_index,ymin_index:ymax_index]
613 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
613 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
614
614
615 return noise
615 return noise
616
616
617 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
617 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
618
618
619 if self.noise_estimation is not None:
619 if self.noise_estimation is not None:
620 return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py
620 return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py
621 else:
621 else:
622 noise = self.getNoisebyHildebrand(xmin_index, xmax_index, ymin_index, ymax_index)
622 noise = self.getNoisebyHildebrand(xmin_index, xmax_index, ymin_index, ymax_index)
623 return noise
623 return noise
624
624
625 def getFreqRangeTimeResponse(self, extrapoints=0):
625 def getFreqRangeTimeResponse(self, extrapoints=0):
626
626
627 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints*self.ippFactor)
627 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints*self.ippFactor)
628 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
628 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
629
629
630 return freqrange
630 return freqrange
631
631
632 def getAcfRange(self, extrapoints=0):
632 def getAcfRange(self, extrapoints=0):
633
633
634 deltafreq = 10./(self.getFmax() / (self.nFFTPoints*self.ippFactor))
634 deltafreq = 10./(self.getFmax() / (self.nFFTPoints*self.ippFactor))
635 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
635 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
636
636
637 return freqrange
637 return freqrange
638
638
639 def getFreqRange(self, extrapoints=0):
639 def getFreqRange(self, extrapoints=0):
640
640
641 deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor)
641 deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor)
642 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
642 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
643
643
644 return freqrange
644 return freqrange
645
645
646 def getVelRange(self, extrapoints=0):
646 def getVelRange(self, extrapoints=0):
647
647
648 deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor)
648 deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor)
649 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) #- deltav/2
649 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) #- deltav/2
650
650
651 return velrange
651 return velrange
652
652
653 def getNPairs(self):
653 def getNPairs(self):
654
654
655 return len(self.pairsList)
655 return len(self.pairsList)
656
656
657 def getPairsIndexList(self):
657 def getPairsIndexList(self):
658
658
659 return range(self.nPairs)
659 return range(self.nPairs)
660
660
661 def getNormFactor(self):
661 def getNormFactor(self):
662
662
663 pwcode = 1
663 pwcode = 1
664
664
665 if self.flagDecodeData:
665 if self.flagDecodeData:
666 pwcode = numpy.sum(self.code[0]**2)
666 pwcode = numpy.sum(self.code[0]**2)
667 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
667 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
668 normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
668 normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
669
669
670 return normFactor
670 return normFactor
671
671
672 def getFlagCspc(self):
672 def getFlagCspc(self):
673
673
674 if self.data_cspc is None:
674 if self.data_cspc is None:
675 return True
675 return True
676
676
677 return False
677 return False
678
678
679 def getFlagDc(self):
679 def getFlagDc(self):
680
680
681 if self.data_dc is None:
681 if self.data_dc is None:
682 return True
682 return True
683
683
684 return False
684 return False
685
685
686 def getTimeInterval(self):
686 def getTimeInterval(self):
687
687
688 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles
688 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles
689
689
690 return timeInterval
690 return timeInterval
691
691
692 def getPower(self):
692 def getPower(self):
693
693
694 factor = self.normFactor
694 factor = self.normFactor
695 z = self.data_spc/factor
695 z = self.data_spc/factor
696 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
696 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
697 avg = numpy.average(z, axis=1)
697 avg = numpy.average(z, axis=1)
698
698
699 return 10*numpy.log10(avg)
699 return 10*numpy.log10(avg)
700
700
701 def getCoherence(self, pairsList=None, phase=False):
701 def getCoherence(self, pairsList=None, phase=False):
702
702
703 z = []
703 z = []
704 if pairsList is None:
704 if pairsList is None:
705 pairsIndexList = self.pairsIndexList
705 pairsIndexList = self.pairsIndexList
706 else:
706 else:
707 pairsIndexList = []
707 pairsIndexList = []
708 for pair in pairsList:
708 for pair in pairsList:
709 if pair not in self.pairsList:
709 if pair not in self.pairsList:
710 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
710 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
711 pairsIndexList.append(self.pairsList.index(pair))
711 pairsIndexList.append(self.pairsList.index(pair))
712 for i in range(len(pairsIndexList)):
712 for i in range(len(pairsIndexList)):
713 pair = self.pairsList[pairsIndexList[i]]
713 pair = self.pairsList[pairsIndexList[i]]
714 ccf = numpy.average(self.data_cspc[pairsIndexList[i], :, :], axis=0)
714 ccf = numpy.average(self.data_cspc[pairsIndexList[i], :, :], axis=0)
715 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
715 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
716 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
716 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
717 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
717 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
718 if phase:
718 if phase:
719 data = numpy.arctan2(avgcoherenceComplex.imag,
719 data = numpy.arctan2(avgcoherenceComplex.imag,
720 avgcoherenceComplex.real)*180/numpy.pi
720 avgcoherenceComplex.real)*180/numpy.pi
721 else:
721 else:
722 data = numpy.abs(avgcoherenceComplex)
722 data = numpy.abs(avgcoherenceComplex)
723
723
724 z.append(data)
724 z.append(data)
725
725
726 return numpy.array(z)
726 return numpy.array(z)
727
727
728 def setValue(self, value):
728 def setValue(self, value):
729
729
730 print "This property should not be initialized"
730 print "This property should not be initialized"
731
731
732 return
732 return
733
733
734 nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.")
734 nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.")
735 pairsIndexList = property(getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.")
735 pairsIndexList = property(getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.")
736 normFactor = property(getNormFactor, setValue, "I'm the 'getNormFactor' property.")
736 normFactor = property(getNormFactor, setValue, "I'm the 'getNormFactor' property.")
737 flag_cspc = property(getFlagCspc, setValue)
737 flag_cspc = property(getFlagCspc, setValue)
738 flag_dc = property(getFlagDc, setValue)
738 flag_dc = property(getFlagDc, setValue)
739 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
739 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
740 timeInterval = property(getTimeInterval, setValue, "I'm the 'timeInterval' property")
740 timeInterval = property(getTimeInterval, setValue, "I'm the 'timeInterval' property")
741
741
742 class SpectraHeis(Spectra):
742 class SpectraHeis(Spectra):
743
743
744 data_spc = None
744 data_spc = None
745
745
746 data_cspc = None
746 data_cspc = None
747
747
748 data_dc = None
748 data_dc = None
749
749
750 nFFTPoints = None
750 nFFTPoints = None
751
751
752 # nPairs = None
752 # nPairs = None
753
753
754 pairsList = None
754 pairsList = None
755
755
756 nCohInt = None
756 nCohInt = None
757
757
758 nIncohInt = None
758 nIncohInt = None
759
759
760 def __init__(self):
760 def __init__(self):
761
761
762 self.radarControllerHeaderObj = RadarControllerHeader()
762 self.radarControllerHeaderObj = RadarControllerHeader()
763
763
764 self.systemHeaderObj = SystemHeader()
764 self.systemHeaderObj = SystemHeader()
765
765
766 self.type = "SpectraHeis"
766 self.type = "SpectraHeis"
767
767
768 # self.dtype = None
768 # self.dtype = None
769
769
770 # self.nChannels = 0
770 # self.nChannels = 0
771
771
772 # self.nHeights = 0
772 # self.nHeights = 0
773
773
774 self.nProfiles = None
774 self.nProfiles = None
775
775
776 self.heightList = None
776 self.heightList = None
777
777
778 self.channelList = None
778 self.channelList = None
779
779
780 # self.channelIndexList = None
780 # self.channelIndexList = None
781
781
782 self.flagNoData = True
782 self.flagNoData = True
783
783
784 self.flagDiscontinuousBlock = False
784 self.flagDiscontinuousBlock = False
785
785
786 # self.nPairs = 0
786 # self.nPairs = 0
787
787
788 self.utctime = None
788 self.utctime = None
789
789
790 self.blocksize = None
790 self.blocksize = None
791
791
792 self.profileIndex = 0
792 self.profileIndex = 0
793
793
794 self.nCohInt = 1
794 self.nCohInt = 1
795
795
796 self.nIncohInt = 1
796 self.nIncohInt = 1
797
797
798 def getNormFactor(self):
798 def getNormFactor(self):
799 pwcode = 1
799 pwcode = 1
800 if self.flagDecodeData:
800 if self.flagDecodeData:
801 pwcode = numpy.sum(self.code[0]**2)
801 pwcode = numpy.sum(self.code[0]**2)
802
802
803 normFactor = self.nIncohInt*self.nCohInt*pwcode
803 normFactor = self.nIncohInt*self.nCohInt*pwcode
804
804
805 return normFactor
805 return normFactor
806
806
807 def getTimeInterval(self):
807 def getTimeInterval(self):
808
808
809 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
809 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
810
810
811 return timeInterval
811 return timeInterval
812
812
813 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
813 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
814 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
814 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
815
815
816 class Fits(JROData):
816 class Fits(JROData):
817
817
818 heightList = None
818 heightList = None
819
819
820 channelList = None
820 channelList = None
821
821
822 flagNoData = True
822 flagNoData = True
823
823
824 flagDiscontinuousBlock = False
824 flagDiscontinuousBlock = False
825
825
826 useLocalTime = False
826 useLocalTime = False
827
827
828 utctime = None
828 utctime = None
829
829
830 timeZone = None
830 timeZone = None
831
831
832 # ippSeconds = None
832 # ippSeconds = None
833
833
834 # timeInterval = None
834 # timeInterval = None
835
835
836 nCohInt = None
836 nCohInt = None
837
837
838 nIncohInt = None
838 nIncohInt = None
839
839
840 noise = None
840 noise = None
841
841
842 windowOfFilter = 1
842 windowOfFilter = 1
843
843
844 #Speed of ligth
844 #Speed of ligth
845 C = 3e8
845 C = 3e8
846
846
847 frequency = 49.92e6
847 frequency = 49.92e6
848
848
849 realtime = False
849 realtime = False
850
850
851
851
852 def __init__(self):
852 def __init__(self):
853
853
854 self.type = "Fits"
854 self.type = "Fits"
855
855
856 self.nProfiles = None
856 self.nProfiles = None
857
857
858 self.heightList = None
858 self.heightList = None
859
859
860 self.channelList = None
860 self.channelList = None
861
861
862 # self.channelIndexList = None
862 # self.channelIndexList = None
863
863
864 self.flagNoData = True
864 self.flagNoData = True
865
865
866 self.utctime = None
866 self.utctime = None
867
867
868 self.nCohInt = 1
868 self.nCohInt = 1
869
869
870 self.nIncohInt = 1
870 self.nIncohInt = 1
871
871
872 self.useLocalTime = True
872 self.useLocalTime = True
873
873
874 self.profileIndex = 0
874 self.profileIndex = 0
875
875
876 # self.utctime = None
876 # self.utctime = None
877 # self.timeZone = None
877 # self.timeZone = None
878 # self.ltctime = None
878 # self.ltctime = None
879 # self.timeInterval = None
879 # self.timeInterval = None
880 # self.header = None
880 # self.header = None
881 # self.data_header = None
881 # self.data_header = None
882 # self.data = None
882 # self.data = None
883 # self.datatime = None
883 # self.datatime = None
884 # self.flagNoData = False
884 # self.flagNoData = False
885 # self.expName = ''
885 # self.expName = ''
886 # self.nChannels = None
886 # self.nChannels = None
887 # self.nSamples = None
887 # self.nSamples = None
888 # self.dataBlocksPerFile = None
888 # self.dataBlocksPerFile = None
889 # self.comments = ''
889 # self.comments = ''
890 #
890 #
891
891
892
892
893 def getltctime(self):
893 def getltctime(self):
894
894
895 if self.useLocalTime:
895 if self.useLocalTime:
896 return self.utctime - self.timeZone*60
896 return self.utctime - self.timeZone*60
897
897
898 return self.utctime
898 return self.utctime
899
899
900 def getDatatime(self):
900 def getDatatime(self):
901
901
902 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
902 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
903 return datatime
903 return datatime
904
904
905 def getTimeRange(self):
905 def getTimeRange(self):
906
906
907 datatime = []
907 datatime = []
908
908
909 datatime.append(self.ltctime)
909 datatime.append(self.ltctime)
910 datatime.append(self.ltctime + self.timeInterval)
910 datatime.append(self.ltctime + self.timeInterval)
911
911
912 datatime = numpy.array(datatime)
912 datatime = numpy.array(datatime)
913
913
914 return datatime
914 return datatime
915
915
916 def getHeiRange(self):
916 def getHeiRange(self):
917
917
918 heis = self.heightList
918 heis = self.heightList
919
919
920 return heis
920 return heis
921
921
922 def getNHeights(self):
922 def getNHeights(self):
923
923
924 return len(self.heightList)
924 return len(self.heightList)
925
925
926 def getNChannels(self):
926 def getNChannels(self):
927
927
928 return len(self.channelList)
928 return len(self.channelList)
929
929
930 def getChannelIndexList(self):
930 def getChannelIndexList(self):
931
931
932 return range(self.nChannels)
932 return range(self.nChannels)
933
933
934 def getNoise(self, type = 1):
934 def getNoise(self, type = 1):
935
935
936 #noise = numpy.zeros(self.nChannels)
936 #noise = numpy.zeros(self.nChannels)
937
937
938 if type == 1:
938 if type == 1:
939 noise = self.getNoisebyHildebrand()
939 noise = self.getNoisebyHildebrand()
940
940
941 if type == 2:
941 if type == 2:
942 noise = self.getNoisebySort()
942 noise = self.getNoisebySort()
943
943
944 if type == 3:
944 if type == 3:
945 noise = self.getNoisebyWindow()
945 noise = self.getNoisebyWindow()
946
946
947 return noise
947 return noise
948
948
949 def getTimeInterval(self):
949 def getTimeInterval(self):
950
950
951 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
951 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
952
952
953 return timeInterval
953 return timeInterval
954
954
955 datatime = property(getDatatime, "I'm the 'datatime' property")
955 datatime = property(getDatatime, "I'm the 'datatime' property")
956 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
956 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
957 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
957 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
958 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
958 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
959 noise = property(getNoise, "I'm the 'nHeights' property.")
959 noise = property(getNoise, "I'm the 'nHeights' property.")
960
960
961 ltctime = property(getltctime, "I'm the 'ltctime' property")
961 ltctime = property(getltctime, "I'm the 'ltctime' property")
962 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
962 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
963
963
964
964
965 class Correlation(JROData):
965 class Correlation(JROData):
966
966
967 noise = None
967 noise = None
968
968
969 SNR = None
969 SNR = None
970
970
971 #--------------------------------------------------
971 #--------------------------------------------------
972
972
973 mode = None
973 mode = None
974
974
975 split = False
975 split = False
976
976
977 data_cf = None
977 data_cf = None
978
978
979 lags = None
979 lags = None
980
980
981 lagRange = None
981 lagRange = None
982
982
983 pairsList = None
983 pairsList = None
984
984
985 normFactor = None
985 normFactor = None
986
986
987 #--------------------------------------------------
987 #--------------------------------------------------
988
988
989 # calculateVelocity = None
989 # calculateVelocity = None
990
990
991 nLags = None
991 nLags = None
992
992
993 nPairs = None
993 nPairs = None
994
994
995 nAvg = None
995 nAvg = None
996
996
997
997
998 def __init__(self):
998 def __init__(self):
999 '''
999 '''
1000 Constructor
1000 Constructor
1001 '''
1001 '''
1002 self.radarControllerHeaderObj = RadarControllerHeader()
1002 self.radarControllerHeaderObj = RadarControllerHeader()
1003
1003
1004 self.systemHeaderObj = SystemHeader()
1004 self.systemHeaderObj = SystemHeader()
1005
1005
1006 self.type = "Correlation"
1006 self.type = "Correlation"
1007
1007
1008 self.data = None
1008 self.data = None
1009
1009
1010 self.dtype = None
1010 self.dtype = None
1011
1011
1012 self.nProfiles = None
1012 self.nProfiles = None
1013
1013
1014 self.heightList = None
1014 self.heightList = None
1015
1015
1016 self.channelList = None
1016 self.channelList = None
1017
1017
1018 self.flagNoData = True
1018 self.flagNoData = True
1019
1019
1020 self.flagDiscontinuousBlock = False
1020 self.flagDiscontinuousBlock = False
1021
1021
1022 self.utctime = None
1022 self.utctime = None
1023
1023
1024 self.timeZone = None
1024 self.timeZone = None
1025
1025
1026 self.dstFlag = None
1026 self.dstFlag = None
1027
1027
1028 self.errorCount = None
1028 self.errorCount = None
1029
1029
1030 self.blocksize = None
1030 self.blocksize = None
1031
1031
1032 self.flagDecodeData = False #asumo q la data no esta decodificada
1032 self.flagDecodeData = False #asumo q la data no esta decodificada
1033
1033
1034 self.flagDeflipData = False #asumo q la data no esta sin flip
1034 self.flagDeflipData = False #asumo q la data no esta sin flip
1035
1035
1036 self.pairsList = None
1036 self.pairsList = None
1037
1037
1038 self.nPoints = None
1038 self.nPoints = None
1039
1039
1040 def getPairsList(self):
1040 def getPairsList(self):
1041
1041
1042 return self.pairsList
1042 return self.pairsList
1043
1043
1044 def getNoise(self, mode = 2):
1044 def getNoise(self, mode = 2):
1045
1045
1046 indR = numpy.where(self.lagR == 0)[0][0]
1046 indR = numpy.where(self.lagR == 0)[0][0]
1047 indT = numpy.where(self.lagT == 0)[0][0]
1047 indT = numpy.where(self.lagT == 0)[0][0]
1048
1048
1049 jspectra0 = self.data_corr[:,:,indR,:]
1049 jspectra0 = self.data_corr[:,:,indR,:]
1050 jspectra = copy.copy(jspectra0)
1050 jspectra = copy.copy(jspectra0)
1051
1051
1052 num_chan = jspectra.shape[0]
1052 num_chan = jspectra.shape[0]
1053 num_hei = jspectra.shape[2]
1053 num_hei = jspectra.shape[2]
1054
1054
1055 freq_dc = jspectra.shape[1]/2
1055 freq_dc = jspectra.shape[1]/2
1056 ind_vel = numpy.array([-2,-1,1,2]) + freq_dc
1056 ind_vel = numpy.array([-2,-1,1,2]) + freq_dc
1057
1057
1058 if ind_vel[0]<0:
1058 if ind_vel[0]<0:
1059 ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof
1059 ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof
1060
1060
1061 if mode == 1:
1061 if mode == 1:
1062 jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION
1062 jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION
1063
1063
1064 if mode == 2:
1064 if mode == 2:
1065
1065
1066 vel = numpy.array([-2,-1,1,2])
1066 vel = numpy.array([-2,-1,1,2])
1067 xx = numpy.zeros([4,4])
1067 xx = numpy.zeros([4,4])
1068
1068
1069 for fil in range(4):
1069 for fil in range(4):
1070 xx[fil,:] = vel[fil]**numpy.asarray(range(4))
1070 xx[fil,:] = vel[fil]**numpy.asarray(range(4))
1071
1071
1072 xx_inv = numpy.linalg.inv(xx)
1072 xx_inv = numpy.linalg.inv(xx)
1073 xx_aux = xx_inv[0,:]
1073 xx_aux = xx_inv[0,:]
1074
1074
1075 for ich in range(num_chan):
1075 for ich in range(num_chan):
1076 yy = jspectra[ich,ind_vel,:]
1076 yy = jspectra[ich,ind_vel,:]
1077 jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy)
1077 jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy)
1078
1078
1079 junkid = jspectra[ich,freq_dc,:]<=0
1079 junkid = jspectra[ich,freq_dc,:]<=0
1080 cjunkid = sum(junkid)
1080 cjunkid = sum(junkid)
1081
1081
1082 if cjunkid.any():
1082 if cjunkid.any():
1083 jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2
1083 jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2
1084
1084
1085 noise = jspectra0[:,freq_dc,:] - jspectra[:,freq_dc,:]
1085 noise = jspectra0[:,freq_dc,:] - jspectra[:,freq_dc,:]
1086
1086
1087 return noise
1087 return noise
1088
1088
1089 def getTimeInterval(self):
1089 def getTimeInterval(self):
1090
1090
1091 timeInterval = self.ippSeconds * self.nCohInt * self.nProfiles
1091 timeInterval = self.ippSeconds * self.nCohInt * self.nProfiles
1092
1092
1093 return timeInterval
1093 return timeInterval
1094
1094
1095 def splitFunctions(self):
1095 def splitFunctions(self):
1096
1096
1097 pairsList = self.pairsList
1097 pairsList = self.pairsList
1098 ccf_pairs = []
1098 ccf_pairs = []
1099 acf_pairs = []
1099 acf_pairs = []
1100 ccf_ind = []
1100 ccf_ind = []
1101 acf_ind = []
1101 acf_ind = []
1102 for l in range(len(pairsList)):
1102 for l in range(len(pairsList)):
1103 chan0 = pairsList[l][0]
1103 chan0 = pairsList[l][0]
1104 chan1 = pairsList[l][1]
1104 chan1 = pairsList[l][1]
1105
1105
1106 #Obteniendo pares de Autocorrelacion
1106 #Obteniendo pares de Autocorrelacion
1107 if chan0 == chan1:
1107 if chan0 == chan1:
1108 acf_pairs.append(chan0)
1108 acf_pairs.append(chan0)
1109 acf_ind.append(l)
1109 acf_ind.append(l)
1110 else:
1110 else:
1111 ccf_pairs.append(pairsList[l])
1111 ccf_pairs.append(pairsList[l])
1112 ccf_ind.append(l)
1112 ccf_ind.append(l)
1113
1113
1114 data_acf = self.data_cf[acf_ind]
1114 data_acf = self.data_cf[acf_ind]
1115 data_ccf = self.data_cf[ccf_ind]
1115 data_ccf = self.data_cf[ccf_ind]
1116
1116
1117 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
1117 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
1118
1118
1119 def getNormFactor(self):
1119 def getNormFactor(self):
1120 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
1120 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
1121 acf_pairs = numpy.array(acf_pairs)
1121 acf_pairs = numpy.array(acf_pairs)
1122 normFactor = numpy.zeros((self.nPairs,self.nHeights))
1122 normFactor = numpy.zeros((self.nPairs,self.nHeights))
1123
1123
1124 for p in range(self.nPairs):
1124 for p in range(self.nPairs):
1125 pair = self.pairsList[p]
1125 pair = self.pairsList[p]
1126
1126
1127 ch0 = pair[0]
1127 ch0 = pair[0]
1128 ch1 = pair[1]
1128 ch1 = pair[1]
1129
1129
1130 ch0_max = numpy.max(data_acf[acf_pairs==ch0,:,:], axis=1)
1130 ch0_max = numpy.max(data_acf[acf_pairs==ch0,:,:], axis=1)
1131 ch1_max = numpy.max(data_acf[acf_pairs==ch1,:,:], axis=1)
1131 ch1_max = numpy.max(data_acf[acf_pairs==ch1,:,:], axis=1)
1132 normFactor[p,:] = numpy.sqrt(ch0_max*ch1_max)
1132 normFactor[p,:] = numpy.sqrt(ch0_max*ch1_max)
1133
1133
1134 return normFactor
1134 return normFactor
1135
1135
1136 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
1136 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
1137 normFactor = property(getNormFactor, "I'm the 'normFactor property'")
1137 normFactor = property(getNormFactor, "I'm the 'normFactor property'")
1138
1138
1139 class Parameters(Spectra):
1139 class Parameters(Spectra):
1140
1140
1141 experimentInfo = None #Information about the experiment
1141 experimentInfo = None #Information about the experiment
1142
1142
1143 #Information from previous data
1143 #Information from previous data
1144
1144
1145 inputUnit = None #Type of data to be processed
1145 inputUnit = None #Type of data to be processed
1146
1146
1147 operation = None #Type of operation to parametrize
1147 operation = None #Type of operation to parametrize
1148
1148
1149 #normFactor = None #Normalization Factor
1149 #normFactor = None #Normalization Factor
1150
1150
1151 groupList = None #List of Pairs, Groups, etc
1151 groupList = None #List of Pairs, Groups, etc
1152
1152
1153 #Parameters
1153 #Parameters
1154
1154
1155 data_param = None #Parameters obtained
1155 data_param = None #Parameters obtained
1156
1156
1157 data_pre = None #Data Pre Parametrization
1157 data_pre = None #Data Pre Parametrization
1158
1158
1159 data_SNR = None #Signal to Noise Ratio
1159 data_SNR = None #Signal to Noise Ratio
1160
1160
1161 # heightRange = None #Heights
1161 # heightRange = None #Heights
1162
1162
1163 abscissaList = None #Abscissa, can be velocities, lags or time
1163 abscissaList = None #Abscissa, can be velocities, lags or time
1164
1164
1165 # noise = None #Noise Potency
1165 # noise = None #Noise Potency
1166
1166
1167 utctimeInit = None #Initial UTC time
1167 utctimeInit = None #Initial UTC time
1168
1168
1169 paramInterval = None #Time interval to calculate Parameters in seconds
1169 paramInterval = None #Time interval to calculate Parameters in seconds
1170
1170
1171 useLocalTime = True
1171 useLocalTime = True
1172
1172
1173 #Fitting
1173 #Fitting
1174
1174
1175 data_error = None #Error of the estimation
1175 data_error = None #Error of the estimation
1176
1176
1177 constants = None
1177 constants = None
1178
1178
1179 library = None
1179 library = None
1180
1180
1181 #Output signal
1181 #Output signal
1182
1182
1183 outputInterval = None #Time interval to calculate output signal in seconds
1183 outputInterval = None #Time interval to calculate output signal in seconds
1184
1184
1185 data_output = None #Out signal
1185 data_output = None #Out signal
1186
1186
1187 nAvg = None
1187 nAvg = None
1188
1188
1189 noise_estimation = None
1189 noise_estimation = None
1190
1190
1191
1191
1192 def __init__(self):
1192 def __init__(self):
1193 '''
1193 '''
1194 Constructor
1194 Constructor
1195 '''
1195 '''
1196 self.radarControllerHeaderObj = RadarControllerHeader()
1196 self.radarControllerHeaderObj = RadarControllerHeader()
1197
1197
1198 self.systemHeaderObj = SystemHeader()
1198 self.systemHeaderObj = SystemHeader()
1199
1199
1200 self.type = "Parameters"
1200 self.type = "Parameters"
1201
1201
1202 def getTimeRange1(self, interval):
1202 def getTimeRange1(self, interval):
1203
1203
1204 datatime = []
1204 datatime = []
1205
1205
1206 if self.useLocalTime:
1206 if self.useLocalTime:
1207 time1 = self.utctimeInit - self.timeZone*60
1207 time1 = self.utctimeInit - self.timeZone*60
1208 else:
1208 else:
1209 time1 = self.utctimeInit
1209 time1 = self.utctimeInit
1210
1210
1211 datatime.append(time1)
1211 datatime.append(time1)
1212 datatime.append(time1 + interval)
1212 datatime.append(time1 + interval)
1213 datatime = numpy.array(datatime)
1213 datatime = numpy.array(datatime)
1214
1214
1215 return datatime
1215 return datatime
1216
1216
1217 def getTimeInterval(self):
1217 def getTimeInterval(self):
1218
1218
1219 return self.timeInterval1
1219 if hasattr(self, 'timeInterval1'):
1220 return self.timeInterval1
1221 else:
1222 return self.paramInterval
1220
1223
1221 def getNoise(self):
1224 def getNoise(self):
1222
1225
1223 return self.spc_noise
1226 return self.spc_noise
1224
1227
1225 timeInterval = property(getTimeInterval)
1228 timeInterval = property(getTimeInterval)
@@ -1,709 +1,775
1
1
2 import os
2 import os
3 import zmq
3 import zmq
4 import time
4 import time
5 import numpy
5 import numpy
6 import datetime
6 import datetime
7 import numpy as np
7 import numpy as np
8 import matplotlib
8 import matplotlib
9 matplotlib.use('TkAgg')
9 matplotlib.use('TkAgg')
10 import matplotlib.pyplot as plt
10 import matplotlib.pyplot as plt
11 from mpl_toolkits.axes_grid1 import make_axes_locatable
11 from mpl_toolkits.axes_grid1 import make_axes_locatable
12 from matplotlib.ticker import FuncFormatter, LinearLocator
12 from matplotlib.ticker import FuncFormatter, LinearLocator
13 from multiprocessing import Process
13 from multiprocessing import Process
14
14
15 from schainpy.model.proc.jroproc_base import Operation
15 from schainpy.model.proc.jroproc_base import Operation
16
16
17 plt.ion()
17 plt.ion()
18
18
19 func = lambda x, pos: ('%s') %(datetime.datetime.fromtimestamp(x).strftime('%H:%M'))
19 func = lambda x, pos: ('%s') %(datetime.datetime.fromtimestamp(x).strftime('%H:%M'))
20
20
21 d1970 = datetime.datetime(1970,1,1)
21 d1970 = datetime.datetime(1970,1,1)
22
22
23 class PlotData(Operation, Process):
23 class PlotData(Operation, Process):
24
24
25 CODE = 'Figure'
25 CODE = 'Figure'
26 colormap = 'jro'
26 colormap = 'jro'
27 CONFLATE = False
27 CONFLATE = False
28 __MAXNUMX = 80
28 __MAXNUMX = 80
29 __missing = 1E30
29 __missing = 1E30
30
30
31 def __init__(self, **kwargs):
31 def __init__(self, **kwargs):
32
32
33 Operation.__init__(self, plot=True, **kwargs)
33 Operation.__init__(self, plot=True, **kwargs)
34 Process.__init__(self)
34 Process.__init__(self)
35 self.kwargs['code'] = self.CODE
35 self.kwargs['code'] = self.CODE
36 self.mp = False
36 self.mp = False
37 self.dataOut = None
37 self.dataOut = None
38 self.isConfig = False
38 self.isConfig = False
39 self.figure = None
39 self.figure = None
40 self.axes = []
40 self.axes = []
41 self.localtime = kwargs.pop('localtime', True)
41 self.localtime = kwargs.pop('localtime', True)
42 self.show = kwargs.get('show', True)
42 self.show = kwargs.get('show', True)
43 self.save = kwargs.get('save', False)
43 self.save = kwargs.get('save', False)
44 self.colormap = kwargs.get('colormap', self.colormap)
44 self.colormap = kwargs.get('colormap', self.colormap)
45 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
45 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
46 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
46 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
47 self.showprofile = kwargs.get('showprofile', True)
47 self.showprofile = kwargs.get('showprofile', True)
48 self.title = kwargs.get('wintitle', '')
48 self.title = kwargs.get('wintitle', '')
49 self.xaxis = kwargs.get('xaxis', 'frequency')
49 self.xaxis = kwargs.get('xaxis', 'frequency')
50 self.zmin = kwargs.get('zmin', None)
50 self.zmin = kwargs.get('zmin', None)
51 self.zmax = kwargs.get('zmax', None)
51 self.zmax = kwargs.get('zmax', None)
52 self.xmin = kwargs.get('xmin', None)
52 self.xmin = kwargs.get('xmin', None)
53 self.xmax = kwargs.get('xmax', None)
53 self.xmax = kwargs.get('xmax', None)
54 self.xrange = kwargs.get('xrange', 24)
54 self.xrange = kwargs.get('xrange', 24)
55 self.ymin = kwargs.get('ymin', None)
55 self.ymin = kwargs.get('ymin', None)
56 self.ymax = kwargs.get('ymax', None)
56 self.ymax = kwargs.get('ymax', None)
57 self.__MAXNUMY = kwargs.get('decimation', 80)
57 self.__MAXNUMY = kwargs.get('decimation', 80)
58 self.throttle_value = 5
58 self.throttle_value = 5
59 self.times = []
59 self.times = []
60 #self.interactive = self.kwargs['parent']
60 #self.interactive = self.kwargs['parent']
61
61
62
62
63 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
63 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
64
64
65 if x_buffer.shape[0] < 2:
65 if x_buffer.shape[0] < 2:
66 return x_buffer, y_buffer, z_buffer
66 return x_buffer, y_buffer, z_buffer
67
67
68 deltas = x_buffer[1:] - x_buffer[0:-1]
68 deltas = x_buffer[1:] - x_buffer[0:-1]
69 x_median = np.median(deltas)
69 x_median = np.median(deltas)
70
70
71 index = np.where(deltas > 5*x_median)
71 index = np.where(deltas > 5*x_median)
72
72
73 if len(index[0]) != 0:
73 if len(index[0]) != 0:
74 z_buffer[::, index[0], ::] = self.__missing
74 z_buffer[::, index[0], ::] = self.__missing
75 z_buffer = np.ma.masked_inside(z_buffer,
75 z_buffer = np.ma.masked_inside(z_buffer,
76 0.99*self.__missing,
76 0.99*self.__missing,
77 1.01*self.__missing)
77 1.01*self.__missing)
78
78
79 return x_buffer, y_buffer, z_buffer
79 return x_buffer, y_buffer, z_buffer
80
80
81 def decimate(self):
81 def decimate(self):
82
82
83 # dx = int(len(self.x)/self.__MAXNUMX) + 1
83 # dx = int(len(self.x)/self.__MAXNUMX) + 1
84 dy = int(len(self.y)/self.__MAXNUMY) + 1
84 dy = int(len(self.y)/self.__MAXNUMY) + 1
85
85
86 # x = self.x[::dx]
86 # x = self.x[::dx]
87 x = self.x
87 x = self.x
88 y = self.y[::dy]
88 y = self.y[::dy]
89 z = self.z[::, ::, ::dy]
89 z = self.z[::, ::, ::dy]
90
90
91 return x, y, z
91 return x, y, z
92
92
93 def __plot(self):
93 def __plot(self):
94
94
95 print 'plotting...{}'.format(self.CODE)
95 print 'plotting...{}'.format(self.CODE)
96
96
97 if self.show:
97 if self.show:
98 print 'showing'
99 self.figure.show()
98 self.figure.show()
100
99
101 self.plot()
100 self.plot()
102 plt.tight_layout()
101 plt.tight_layout()
103 self.figure.canvas.manager.set_window_title('{} {} - Date:{}'.format(self.title, self.CODE.upper(),
102 self.figure.canvas.manager.set_window_title('{} {} - {}'.format(self.title, self.CODE.upper(),
104 datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')))
103 datetime.datetime.fromtimestamp(self.max_time).strftime('%Y/%m/%d')))
105
104
106 if self.save:
105 if self.save:
107 figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,
106 figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,
108 datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S')))
107 datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S')))
109 print 'Saving figure: {}'.format(figname)
108 print 'Saving figure: {}'.format(figname)
110 self.figure.savefig(figname)
109 self.figure.savefig(figname)
111
110
112 self.figure.canvas.draw()
111 self.figure.canvas.draw()
113
112
114 def plot(self):
113 def plot(self):
115
114
116 print 'plotting...{}'.format(self.CODE.upper())
115 print 'plotting...{}'.format(self.CODE.upper())
117 return
116 return
118
117
119 def run(self):
118 def run(self):
120
119
121 print '[Starting] {}'.format(self.name)
120 print '[Starting] {}'.format(self.name)
121
122 context = zmq.Context()
122 context = zmq.Context()
123 receiver = context.socket(zmq.SUB)
123 receiver = context.socket(zmq.SUB)
124 receiver.setsockopt(zmq.SUBSCRIBE, '')
124 receiver.setsockopt(zmq.SUBSCRIBE, '')
125 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
125 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
126 receiver.connect("ipc:///tmp/zmq.plots")
126
127 if 'server' in self.kwargs['parent']:
128 receiver.connect('ipc:///tmp/{}.plots'.format(self.kwargs['parent']['server']))
129 else:
130 receiver.connect("ipc:///tmp/zmq.plots")
131
127 seconds_passed = 0
132 seconds_passed = 0
133
128 while True:
134 while True:
129 try:
135 try:
130 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)#flags=zmq.NOBLOCK
136 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)#flags=zmq.NOBLOCK
131 self.started = self.data['STARTED']
137 self.started = self.data['STARTED']
132 self.dataOut = self.data['dataOut']
138 self.dataOut = self.data['dataOut']
133
139
134 if (len(self.times) < len(self.data['times']) and not self.started and self.data['ENDED']):
140 if (len(self.times) < len(self.data['times']) and not self.started and self.data['ENDED']):
135 continue
141 continue
136
142
137 self.times = self.data['times']
143 self.times = self.data['times']
138 self.times.sort()
144 self.times.sort()
139 self.throttle_value = self.data['throttle']
145 self.throttle_value = self.data['throttle']
140 self.min_time = self.times[0]
146 self.min_time = self.times[0]
141 self.max_time = self.times[-1]
147 self.max_time = self.times[-1]
142
148
143 if self.isConfig is False:
149 if self.isConfig is False:
144 print 'setting up'
150 print 'setting up'
145 self.setup()
151 self.setup()
146 self.isConfig = True
152 self.isConfig = True
147 self.__plot()
153 self.__plot()
148
154
149 if self.data['ENDED'] is True:
155 if self.data['ENDED'] is True:
150 print '********GRAPHIC ENDED********'
156 print '********GRAPHIC ENDED********'
151 self.ended = True
157 self.ended = True
152 self.isConfig = False
158 self.isConfig = False
153 self.__plot()
159 self.__plot()
154 elif seconds_passed >= self.data['throttle']:
160 elif seconds_passed >= self.data['throttle']:
155 print 'passed', seconds_passed
161 print 'passed', seconds_passed
156 self.__plot()
162 self.__plot()
157 seconds_passed = 0
163 seconds_passed = 0
158
164
159 except zmq.Again as e:
165 except zmq.Again as e:
160 print 'Waiting for data...'
166 print 'Waiting for data...'
161 plt.pause(2)
167 plt.pause(2)
162 seconds_passed += 2
168 seconds_passed += 2
163
169
164 def close(self):
170 def close(self):
165 if self.dataOut:
171 if self.dataOut:
166 self.__plot()
172 self.__plot()
167
173
168
174
169 class PlotSpectraData(PlotData):
175 class PlotSpectraData(PlotData):
170
176
171 CODE = 'spc'
177 CODE = 'spc'
172 colormap = 'jro'
178 colormap = 'jro'
173 CONFLATE = False
179 CONFLATE = False
174
180
175 def setup(self):
181 def setup(self):
176
182
177 ncolspan = 1
183 ncolspan = 1
178 colspan = 1
184 colspan = 1
179 self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9)
185 self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9)
180 self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9)
186 self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9)
181 self.width = 3.6*self.ncols
187 self.width = 3.6*self.ncols
182 self.height = 3.2*self.nrows
188 self.height = 3.2*self.nrows
183 if self.showprofile:
189 if self.showprofile:
184 ncolspan = 3
190 ncolspan = 3
185 colspan = 2
191 colspan = 2
186 self.width += 1.2*self.ncols
192 self.width += 1.2*self.ncols
187
193
188 self.ylabel = 'Range [Km]'
194 self.ylabel = 'Range [Km]'
189 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
195 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
190
196
191 if self.figure is None:
197 if self.figure is None:
192 self.figure = plt.figure(figsize=(self.width, self.height),
198 self.figure = plt.figure(figsize=(self.width, self.height),
193 edgecolor='k',
199 edgecolor='k',
194 facecolor='w')
200 facecolor='w')
195 else:
201 else:
196 self.figure.clf()
202 self.figure.clf()
197
203
198 n = 0
204 n = 0
199 for y in range(self.nrows):
205 for y in range(self.nrows):
200 for x in range(self.ncols):
206 for x in range(self.ncols):
201 if n >= self.dataOut.nChannels:
207 if n >= self.dataOut.nChannels:
202 break
208 break
203 ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan)
209 ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan)
204 if self.showprofile:
210 if self.showprofile:
205 ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1)
211 ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1)
206
212
207 ax.firsttime = True
213 ax.firsttime = True
208 self.axes.append(ax)
214 self.axes.append(ax)
209 n += 1
215 n += 1
210
216
211 def plot(self):
217 def plot(self):
212
218
213 if self.xaxis == "frequency":
219 if self.xaxis == "frequency":
214 x = self.dataOut.getFreqRange(1)/1000.
220 x = self.dataOut.getFreqRange(1)/1000.
215 xlabel = "Frequency (kHz)"
221 xlabel = "Frequency (kHz)"
216 elif self.xaxis == "time":
222 elif self.xaxis == "time":
217 x = self.dataOut.getAcfRange(1)
223 x = self.dataOut.getAcfRange(1)
218 xlabel = "Time (ms)"
224 xlabel = "Time (ms)"
219 else:
225 else:
220 x = self.dataOut.getVelRange(1)
226 x = self.dataOut.getVelRange(1)
221 xlabel = "Velocity (m/s)"
227 xlabel = "Velocity (m/s)"
222
228
223 y = self.dataOut.getHeiRange()
229 y = self.dataOut.getHeiRange()
224 z = self.data[self.CODE]
230 z = self.data[self.CODE]
225
231
226 for n, ax in enumerate(self.axes):
232 for n, ax in enumerate(self.axes):
227
233
228 if ax.firsttime:
234 if ax.firsttime:
229 self.xmax = self.xmax if self.xmax else np.nanmax(x)
235 self.xmax = self.xmax if self.xmax else np.nanmax(x)
230 self.xmin = self.xmin if self.xmin else -self.xmax
236 self.xmin = self.xmin if self.xmin else -self.xmax
231 self.ymin = self.ymin if self.ymin else np.nanmin(y)
237 self.ymin = self.ymin if self.ymin else np.nanmin(y)
232 self.ymax = self.ymax if self.ymax else np.nanmax(y)
238 self.ymax = self.ymax if self.ymax else np.nanmax(y)
233 self.zmin = self.zmin if self.zmin else np.nanmin(z)
239 self.zmin = self.zmin if self.zmin else np.nanmin(z)
234 self.zmax = self.zmax if self.zmax else np.nanmax(z)
240 self.zmax = self.zmax if self.zmax else np.nanmax(z)
235 ax.plot = ax.pcolormesh(x, y, z[n].T,
241 ax.plot = ax.pcolormesh(x, y, z[n].T,
236 vmin=self.zmin,
242 vmin=self.zmin,
237 vmax=self.zmax,
243 vmax=self.zmax,
238 cmap=plt.get_cmap(self.colormap)
244 cmap=plt.get_cmap(self.colormap)
239 )
245 )
240 divider = make_axes_locatable(ax)
246 divider = make_axes_locatable(ax)
241 cax = divider.new_horizontal(size='3%', pad=0.05)
247 cax = divider.new_horizontal(size='3%', pad=0.05)
242 self.figure.add_axes(cax)
248 self.figure.add_axes(cax)
243 plt.colorbar(ax.plot, cax)
249 plt.colorbar(ax.plot, cax)
244
250
245 ax.set_xlim(self.xmin, self.xmax)
251 ax.set_xlim(self.xmin, self.xmax)
246 ax.set_ylim(self.ymin, self.ymax)
252 ax.set_ylim(self.ymin, self.ymax)
247
253
248 ax.set_ylabel(self.ylabel)
254 ax.set_ylabel(self.ylabel)
249 ax.set_xlabel(xlabel)
255 ax.set_xlabel(xlabel)
250
256
251 ax.firsttime = False
257 ax.firsttime = False
252
258
253 if self.showprofile:
259 if self.showprofile:
254 ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
260 ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
255 ax.ax_profile.set_xlim(self.zmin, self.zmax)
261 ax.ax_profile.set_xlim(self.zmin, self.zmax)
256 ax.ax_profile.set_ylim(self.ymin, self.ymax)
262 ax.ax_profile.set_ylim(self.ymin, self.ymax)
257 ax.ax_profile.set_xlabel('dB')
263 ax.ax_profile.set_xlabel('dB')
258 ax.ax_profile.grid(b=True, axis='x')
264 ax.ax_profile.grid(b=True, axis='x')
259 ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
265 ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
260 color="k", linestyle="dashed", lw=2)[0]
266 color="k", linestyle="dashed", lw=2)[0]
261 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
267 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
262 else:
268 else:
263 ax.plot.set_array(z[n].T.ravel())
269 ax.plot.set_array(z[n].T.ravel())
264 if self.showprofile:
270 if self.showprofile:
265 ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y)
271 ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y)
266 ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
272 ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
267
273
268 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
274 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
269 size=8)
275 size=8)
270 self.saveTime = self.max_time
276 self.saveTime = self.max_time
271
277
272
278
273 class PlotCrossSpectraData(PlotData):
279 class PlotCrossSpectraData(PlotData):
274
280
275 CODE = 'cspc'
281 CODE = 'cspc'
276 zmin_coh = None
282 zmin_coh = None
277 zmax_coh = None
283 zmax_coh = None
278 zmin_phase = None
284 zmin_phase = None
279 zmax_phase = None
285 zmax_phase = None
280 CONFLATE = False
286 CONFLATE = False
281
287
282 def setup(self):
288 def setup(self):
283
289
284 ncolspan = 1
290 ncolspan = 1
285 colspan = 1
291 colspan = 1
286 self.ncols = 2
292 self.ncols = 2
287 self.nrows = self.dataOut.nPairs
293 self.nrows = self.dataOut.nPairs
288 self.width = 3.6*self.ncols
294 self.width = 3.6*self.ncols
289 self.height = 3.2*self.nrows
295 self.height = 3.2*self.nrows
290
296
291 self.ylabel = 'Range [Km]'
297 self.ylabel = 'Range [Km]'
292 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
298 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
293
299
294 if self.figure is None:
300 if self.figure is None:
295 self.figure = plt.figure(figsize=(self.width, self.height),
301 self.figure = plt.figure(figsize=(self.width, self.height),
296 edgecolor='k',
302 edgecolor='k',
297 facecolor='w')
303 facecolor='w')
298 else:
304 else:
299 self.figure.clf()
305 self.figure.clf()
300
306
301 for y in range(self.nrows):
307 for y in range(self.nrows):
302 for x in range(self.ncols):
308 for x in range(self.ncols):
303 ax = plt.subplot2grid((self.nrows, self.ncols), (y, x), 1, 1)
309 ax = plt.subplot2grid((self.nrows, self.ncols), (y, x), 1, 1)
304 ax.firsttime = True
310 ax.firsttime = True
305 self.axes.append(ax)
311 self.axes.append(ax)
306
312
307 def plot(self):
313 def plot(self):
308
314
309 if self.xaxis == "frequency":
315 if self.xaxis == "frequency":
310 x = self.dataOut.getFreqRange(1)/1000.
316 x = self.dataOut.getFreqRange(1)/1000.
311 xlabel = "Frequency (kHz)"
317 xlabel = "Frequency (kHz)"
312 elif self.xaxis == "time":
318 elif self.xaxis == "time":
313 x = self.dataOut.getAcfRange(1)
319 x = self.dataOut.getAcfRange(1)
314 xlabel = "Time (ms)"
320 xlabel = "Time (ms)"
315 else:
321 else:
316 x = self.dataOut.getVelRange(1)
322 x = self.dataOut.getVelRange(1)
317 xlabel = "Velocity (m/s)"
323 xlabel = "Velocity (m/s)"
318
324
319 y = self.dataOut.getHeiRange()
325 y = self.dataOut.getHeiRange()
320 z_coh = self.data['cspc_coh']
326 z_coh = self.data['cspc_coh']
321 z_phase = self.data['cspc_phase']
327 z_phase = self.data['cspc_phase']
322
328
323 for n in range(self.nrows):
329 for n in range(self.nrows):
324 ax = self.axes[2*n]
330 ax = self.axes[2*n]
325 ax1 = self.axes[2*n+1]
331 ax1 = self.axes[2*n+1]
326 if ax.firsttime:
332 if ax.firsttime:
327 self.xmax = self.xmax if self.xmax else np.nanmax(x)
333 self.xmax = self.xmax if self.xmax else np.nanmax(x)
328 self.xmin = self.xmin if self.xmin else -self.xmax
334 self.xmin = self.xmin if self.xmin else -self.xmax
329 self.ymin = self.ymin if self.ymin else np.nanmin(y)
335 self.ymin = self.ymin if self.ymin else np.nanmin(y)
330 self.ymax = self.ymax if self.ymax else np.nanmax(y)
336 self.ymax = self.ymax if self.ymax else np.nanmax(y)
331 self.zmin_coh = self.zmin_coh if self.zmin_coh else 0.0
337 self.zmin_coh = self.zmin_coh if self.zmin_coh else 0.0
332 self.zmax_coh = self.zmax_coh if self.zmax_coh else 1.0
338 self.zmax_coh = self.zmax_coh if self.zmax_coh else 1.0
333 self.zmin_phase = self.zmin_phase if self.zmin_phase else -180
339 self.zmin_phase = self.zmin_phase if self.zmin_phase else -180
334 self.zmax_phase = self.zmax_phase if self.zmax_phase else 180
340 self.zmax_phase = self.zmax_phase if self.zmax_phase else 180
335
341
336 ax.plot = ax.pcolormesh(x, y, z_coh[n].T,
342 ax.plot = ax.pcolormesh(x, y, z_coh[n].T,
337 vmin=self.zmin_coh,
343 vmin=self.zmin_coh,
338 vmax=self.zmax_coh,
344 vmax=self.zmax_coh,
339 cmap=plt.get_cmap(self.colormap_coh)
345 cmap=plt.get_cmap(self.colormap_coh)
340 )
346 )
341 divider = make_axes_locatable(ax)
347 divider = make_axes_locatable(ax)
342 cax = divider.new_horizontal(size='3%', pad=0.05)
348 cax = divider.new_horizontal(size='3%', pad=0.05)
343 self.figure.add_axes(cax)
349 self.figure.add_axes(cax)
344 plt.colorbar(ax.plot, cax)
350 plt.colorbar(ax.plot, cax)
345
351
346 ax.set_xlim(self.xmin, self.xmax)
352 ax.set_xlim(self.xmin, self.xmax)
347 ax.set_ylim(self.ymin, self.ymax)
353 ax.set_ylim(self.ymin, self.ymax)
348
354
349 ax.set_ylabel(self.ylabel)
355 ax.set_ylabel(self.ylabel)
350 ax.set_xlabel(xlabel)
356 ax.set_xlabel(xlabel)
351 ax.firsttime = False
357 ax.firsttime = False
352
358
353 ax1.plot = ax1.pcolormesh(x, y, z_phase[n].T,
359 ax1.plot = ax1.pcolormesh(x, y, z_phase[n].T,
354 vmin=self.zmin_phase,
360 vmin=self.zmin_phase,
355 vmax=self.zmax_phase,
361 vmax=self.zmax_phase,
356 cmap=plt.get_cmap(self.colormap_phase)
362 cmap=plt.get_cmap(self.colormap_phase)
357 )
363 )
358 divider = make_axes_locatable(ax1)
364 divider = make_axes_locatable(ax1)
359 cax = divider.new_horizontal(size='3%', pad=0.05)
365 cax = divider.new_horizontal(size='3%', pad=0.05)
360 self.figure.add_axes(cax)
366 self.figure.add_axes(cax)
361 plt.colorbar(ax1.plot, cax)
367 plt.colorbar(ax1.plot, cax)
362
368
363 ax1.set_xlim(self.xmin, self.xmax)
369 ax1.set_xlim(self.xmin, self.xmax)
364 ax1.set_ylim(self.ymin, self.ymax)
370 ax1.set_ylim(self.ymin, self.ymax)
365
371
366 ax1.set_ylabel(self.ylabel)
372 ax1.set_ylabel(self.ylabel)
367 ax1.set_xlabel(xlabel)
373 ax1.set_xlabel(xlabel)
368 ax1.firsttime = False
374 ax1.firsttime = False
369 else:
375 else:
370 ax.plot.set_array(z_coh[n].T.ravel())
376 ax.plot.set_array(z_coh[n].T.ravel())
371 ax1.plot.set_array(z_phase[n].T.ravel())
377 ax1.plot.set_array(z_phase[n].T.ravel())
372
378
373 ax.set_title('Coherence Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8)
379 ax.set_title('Coherence Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8)
374 ax1.set_title('Phase Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8)
380 ax1.set_title('Phase Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8)
375 self.saveTime = self.max_time
381 self.saveTime = self.max_time
376
382
377
383
378 class PlotSpectraMeanData(PlotSpectraData):
384 class PlotSpectraMeanData(PlotSpectraData):
379
385
380 CODE = 'spc_mean'
386 CODE = 'spc_mean'
381 colormap = 'jet'
387 colormap = 'jet'
382
388
383 def plot(self):
389 def plot(self):
384
390
385 if self.xaxis == "frequency":
391 if self.xaxis == "frequency":
386 x = self.dataOut.getFreqRange(1)/1000.
392 x = self.dataOut.getFreqRange(1)/1000.
387 xlabel = "Frequency (kHz)"
393 xlabel = "Frequency (kHz)"
388 elif self.xaxis == "time":
394 elif self.xaxis == "time":
389 x = self.dataOut.getAcfRange(1)
395 x = self.dataOut.getAcfRange(1)
390 xlabel = "Time (ms)"
396 xlabel = "Time (ms)"
391 else:
397 else:
392 x = self.dataOut.getVelRange(1)
398 x = self.dataOut.getVelRange(1)
393 xlabel = "Velocity (m/s)"
399 xlabel = "Velocity (m/s)"
394
400
395 y = self.dataOut.getHeiRange()
401 y = self.dataOut.getHeiRange()
396 z = self.data['spc']
402 z = self.data['spc']
397 mean = self.data['mean'][self.max_time]
403 mean = self.data['mean'][self.max_time]
398
404
399 for n, ax in enumerate(self.axes):
405 for n, ax in enumerate(self.axes):
400
406
401 if ax.firsttime:
407 if ax.firsttime:
402 self.xmax = self.xmax if self.xmax else np.nanmax(x)
408 self.xmax = self.xmax if self.xmax else np.nanmax(x)
403 self.xmin = self.xmin if self.xmin else -self.xmax
409 self.xmin = self.xmin if self.xmin else -self.xmax
404 self.ymin = self.ymin if self.ymin else np.nanmin(y)
410 self.ymin = self.ymin if self.ymin else np.nanmin(y)
405 self.ymax = self.ymax if self.ymax else np.nanmax(y)
411 self.ymax = self.ymax if self.ymax else np.nanmax(y)
406 self.zmin = self.zmin if self.zmin else np.nanmin(z)
412 self.zmin = self.zmin if self.zmin else np.nanmin(z)
407 self.zmax = self.zmax if self.zmax else np.nanmax(z)
413 self.zmax = self.zmax if self.zmax else np.nanmax(z)
408 ax.plt = ax.pcolormesh(x, y, z[n].T,
414 ax.plt = ax.pcolormesh(x, y, z[n].T,
409 vmin=self.zmin,
415 vmin=self.zmin,
410 vmax=self.zmax,
416 vmax=self.zmax,
411 cmap=plt.get_cmap(self.colormap)
417 cmap=plt.get_cmap(self.colormap)
412 )
418 )
413 ax.plt_dop = ax.plot(mean[n], y,
419 ax.plt_dop = ax.plot(mean[n], y,
414 color='k')[0]
420 color='k')[0]
415
421
416 divider = make_axes_locatable(ax)
422 divider = make_axes_locatable(ax)
417 cax = divider.new_horizontal(size='3%', pad=0.05)
423 cax = divider.new_horizontal(size='3%', pad=0.05)
418 self.figure.add_axes(cax)
424 self.figure.add_axes(cax)
419 plt.colorbar(ax.plt, cax)
425 plt.colorbar(ax.plt, cax)
420
426
421 ax.set_xlim(self.xmin, self.xmax)
427 ax.set_xlim(self.xmin, self.xmax)
422 ax.set_ylim(self.ymin, self.ymax)
428 ax.set_ylim(self.ymin, self.ymax)
423
429
424 ax.set_ylabel(self.ylabel)
430 ax.set_ylabel(self.ylabel)
425 ax.set_xlabel(xlabel)
431 ax.set_xlabel(xlabel)
426
432
427 ax.firsttime = False
433 ax.firsttime = False
428
434
429 if self.showprofile:
435 if self.showprofile:
430 ax.plt_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
436 ax.plt_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
431 ax.ax_profile.set_xlim(self.zmin, self.zmax)
437 ax.ax_profile.set_xlim(self.zmin, self.zmax)
432 ax.ax_profile.set_ylim(self.ymin, self.ymax)
438 ax.ax_profile.set_ylim(self.ymin, self.ymax)
433 ax.ax_profile.set_xlabel('dB')
439 ax.ax_profile.set_xlabel('dB')
434 ax.ax_profile.grid(b=True, axis='x')
440 ax.ax_profile.grid(b=True, axis='x')
435 ax.plt_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
441 ax.plt_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
436 color="k", linestyle="dashed", lw=2)[0]
442 color="k", linestyle="dashed", lw=2)[0]
437 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
443 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
438 else:
444 else:
439 ax.plt.set_array(z[n].T.ravel())
445 ax.plt.set_array(z[n].T.ravel())
440 ax.plt_dop.set_data(mean[n], y)
446 ax.plt_dop.set_data(mean[n], y)
441 if self.showprofile:
447 if self.showprofile:
442 ax.plt_profile.set_data(self.data['rti'][self.max_time][n], y)
448 ax.plt_profile.set_data(self.data['rti'][self.max_time][n], y)
443 ax.plt_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
449 ax.plt_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
444
450
445 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
451 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
446 size=8)
452 size=8)
447 self.saveTime = self.max_time
453 self.saveTime = self.max_time
448
454
449
455
450 class PlotRTIData(PlotData):
456 class PlotRTIData(PlotData):
451
457
452 CODE = 'rti'
458 CODE = 'rti'
453 colormap = 'jro'
459 colormap = 'jro'
454
460
455 def setup(self):
461 def setup(self):
456 self.ncols = 1
462 self.ncols = 1
457 self.nrows = self.dataOut.nChannels
463 self.nrows = self.dataOut.nChannels
458 self.width = 10
464 self.width = 10
459 self.height = 2.2*self.nrows if self.nrows<6 else 12
465 self.height = 2.2*self.nrows if self.nrows<6 else 12
460 if self.nrows==1:
466 if self.nrows==1:
461 self.height += 1
467 self.height += 1
462 self.ylabel = 'Range [Km]'
468 self.ylabel = 'Range [Km]'
463 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
469 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
464
470
465 if self.figure is None:
471 if self.figure is None:
466 self.figure = plt.figure(figsize=(self.width, self.height),
472 self.figure = plt.figure(figsize=(self.width, self.height),
467 edgecolor='k',
473 edgecolor='k',
468 facecolor='w')
474 facecolor='w')
469 else:
475 else:
470 self.figure.clf()
476 self.figure.clf()
471 self.axes = []
477 self.axes = []
472
478
473 for n in range(self.nrows):
479 for n in range(self.nrows):
474 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
480 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
475 ax.firsttime = True
481 ax.firsttime = True
476 self.axes.append(ax)
482 self.axes.append(ax)
477
483
478 def plot(self):
484 def plot(self):
479
485
480 self.x = np.array(self.times)
486 self.x = np.array(self.times)
481 self.y = self.dataOut.getHeiRange()
487 self.y = self.dataOut.getHeiRange()
482 self.z = []
488 self.z = []
483
489
484 for ch in range(self.nrows):
490 for ch in range(self.nrows):
485 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
491 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
486
492
487 self.z = np.array(self.z)
493 self.z = np.array(self.z)
488 for n, ax in enumerate(self.axes):
494 for n, ax in enumerate(self.axes):
489 x, y, z = self.fill_gaps(*self.decimate())
495 x, y, z = self.fill_gaps(*self.decimate())
490 xmin = self.min_time
496 xmin = self.min_time
491 xmax = xmin+self.xrange*60*60
497 xmax = xmin+self.xrange*60*60
492 self.zmin = self.zmin if self.zmin else np.min(self.z)
498 self.zmin = self.zmin if self.zmin else np.min(self.z)
493 self.zmax = self.zmax if self.zmax else np.max(self.z)
499 self.zmax = self.zmax if self.zmax else np.max(self.z)
494 if ax.firsttime:
500 if ax.firsttime:
495 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
501 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
496 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
502 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
497 plot = ax.pcolormesh(x, y, z[n].T,
503 plot = ax.pcolormesh(x, y, z[n].T,
498 vmin=self.zmin,
504 vmin=self.zmin,
499 vmax=self.zmax,
505 vmax=self.zmax,
500 cmap=plt.get_cmap(self.colormap)
506 cmap=plt.get_cmap(self.colormap)
501 )
507 )
502 divider = make_axes_locatable(ax)
508 divider = make_axes_locatable(ax)
503 cax = divider.new_horizontal(size='2%', pad=0.05)
509 cax = divider.new_horizontal(size='2%', pad=0.05)
504 self.figure.add_axes(cax)
510 self.figure.add_axes(cax)
505 plt.colorbar(plot, cax)
511 plt.colorbar(plot, cax)
506 ax.set_ylim(self.ymin, self.ymax)
512 ax.set_ylim(self.ymin, self.ymax)
507
513
508 ax.xaxis.set_major_formatter(FuncFormatter(func))
514 ax.xaxis.set_major_formatter(FuncFormatter(func))
509 ax.xaxis.set_major_locator(LinearLocator(6))
515 ax.xaxis.set_major_locator(LinearLocator(6))
510
516
511 ax.set_ylabel(self.ylabel)
517 ax.set_ylabel(self.ylabel)
512
518
513 # if self.xmin is None:
519 # if self.xmin is None:
514 # xmin = self.min_time
520 # xmin = self.min_time
515 # else:
521 # else:
516 # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(),
522 # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(),
517 # datetime.time(self.xmin, 0, 0))-d1970).total_seconds()
523 # datetime.time(self.xmin, 0, 0))-d1970).total_seconds()
518
524
519 ax.set_xlim(xmin, xmax)
525 ax.set_xlim(xmin, xmax)
520 ax.firsttime = False
526 ax.firsttime = False
521 else:
527 else:
522 ax.collections.remove(ax.collections[0])
528 ax.collections.remove(ax.collections[0])
523 ax.set_xlim(xmin, xmax)
529 ax.set_xlim(xmin, xmax)
524 plot = ax.pcolormesh(x, y, z[n].T,
530 plot = ax.pcolormesh(x, y, z[n].T,
525 vmin=self.zmin,
531 vmin=self.zmin,
526 vmax=self.zmax,
532 vmax=self.zmax,
527 cmap=plt.get_cmap(self.colormap)
533 cmap=plt.get_cmap(self.colormap)
528 )
534 )
529 ax.set_title('{} {}'.format(self.titles[n],
535 ax.set_title('{} {}'.format(self.titles[n],
530 datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
536 datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
531 size=8)
537 size=8)
532
538
533 self.saveTime = self.min_time
539 self.saveTime = self.min_time
534
540
535
541
536 class PlotCOHData(PlotRTIData):
542 class PlotCOHData(PlotRTIData):
537
543
538 CODE = 'coh'
544 CODE = 'coh'
539
545
540 def setup(self):
546 def setup(self):
541
547
542 self.ncols = 1
548 self.ncols = 1
543 self.nrows = self.dataOut.nPairs
549 self.nrows = self.dataOut.nPairs
544 self.width = 10
550 self.width = 10
545 self.height = 2.2*self.nrows if self.nrows<6 else 12
551 self.height = 2.2*self.nrows if self.nrows<6 else 12
546 if self.nrows==1:
552 if self.nrows==1:
547 self.height += 1
553 self.height += 1
548 self.ylabel = 'Range [Km]'
554 self.ylabel = 'Range [Km]'
549 self.titles = ['{} Ch{} * Ch{}'.format(self.CODE.upper(), x[0], x[1]) for x in self.dataOut.pairsList]
555 self.titles = ['{} Ch{} * Ch{}'.format(self.CODE.upper(), x[0], x[1]) for x in self.dataOut.pairsList]
550
556
551 if self.figure is None:
557 if self.figure is None:
552 self.figure = plt.figure(figsize=(self.width, self.height),
558 self.figure = plt.figure(figsize=(self.width, self.height),
553 edgecolor='k',
559 edgecolor='k',
554 facecolor='w')
560 facecolor='w')
555 else:
561 else:
556 self.figure.clf()
562 self.figure.clf()
557 self.axes = []
563 self.axes = []
558
564
559 for n in range(self.nrows):
565 for n in range(self.nrows):
560 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
566 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
561 ax.firsttime = True
567 ax.firsttime = True
562 self.axes.append(ax)
568 self.axes.append(ax)
563
569
564
570
565 class PlotNoiseData(PlotData):
571 class PlotNoiseData(PlotData):
566 CODE = 'noise'
572 CODE = 'noise'
567
573
568 def setup(self):
574 def setup(self):
569
575
570 self.ncols = 1
576 self.ncols = 1
571 self.nrows = 1
577 self.nrows = 1
572 self.width = 10
578 self.width = 10
573 self.height = 3.2
579 self.height = 3.2
574 self.ylabel = 'Intensity [dB]'
580 self.ylabel = 'Intensity [dB]'
575 self.titles = ['Noise']
581 self.titles = ['Noise']
576
582
577 if self.figure is None:
583 if self.figure is None:
578 self.figure = plt.figure(figsize=(self.width, self.height),
584 self.figure = plt.figure(figsize=(self.width, self.height),
579 edgecolor='k',
585 edgecolor='k',
580 facecolor='w')
586 facecolor='w')
581 else:
587 else:
582 self.figure.clf()
588 self.figure.clf()
583 self.axes = []
589 self.axes = []
584
590
585 self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1)
591 self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1)
586 self.ax.firsttime = True
592 self.ax.firsttime = True
587
593
588 def plot(self):
594 def plot(self):
589
595
590 x = self.times
596 x = self.times
591 xmin = self.min_time
597 xmin = self.min_time
592 xmax = xmin+self.xrange*60*60
598 xmax = xmin+self.xrange*60*60
593 if self.ax.firsttime:
599 if self.ax.firsttime:
594 for ch in self.dataOut.channelList:
600 for ch in self.dataOut.channelList:
595 y = [self.data[self.CODE][t][ch] for t in self.times]
601 y = [self.data[self.CODE][t][ch] for t in self.times]
596 self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch))
602 self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch))
597 self.ax.firsttime = False
603 self.ax.firsttime = False
598 self.ax.xaxis.set_major_formatter(FuncFormatter(func))
604 self.ax.xaxis.set_major_formatter(FuncFormatter(func))
599 self.ax.xaxis.set_major_locator(LinearLocator(6))
605 self.ax.xaxis.set_major_locator(LinearLocator(6))
600 self.ax.set_ylabel(self.ylabel)
606 self.ax.set_ylabel(self.ylabel)
601 plt.legend()
607 plt.legend()
602 else:
608 else:
603 for ch in self.dataOut.channelList:
609 for ch in self.dataOut.channelList:
604 y = [self.data[self.CODE][t][ch] for t in self.times]
610 y = [self.data[self.CODE][t][ch] for t in self.times]
605 self.ax.lines[ch].set_data(x, y)
611 self.ax.lines[ch].set_data(x, y)
606
612
607 self.ax.set_xlim(xmin, xmax)
613 self.ax.set_xlim(xmin, xmax)
608 self.ax.set_ylim(min(y)-5, max(y)+5)
614 self.ax.set_ylim(min(y)-5, max(y)+5)
609 self.saveTime = self.min_time
615 self.saveTime = self.min_time
610
616
611
617
612 class PlotWindProfilerData(PlotRTIData):
618 class PlotWindProfilerData(PlotRTIData):
619
613 CODE = 'wind'
620 CODE = 'wind'
614 colormap = 'seismic'
621 colormap = 'seismic'
615
622
616 def setup(self):
623 def setup(self):
617 self.ncols = 1
624 self.ncols = 1
618 self.nrows = self.dataOut.data_output.shape[0]
625 self.nrows = self.dataOut.data_output.shape[0]
619 self.width = 10
626 self.width = 10
620 self.height = 2.2*self.nrows
627 self.height = 2.2*self.nrows
621 self.ylabel = 'Height [Km]'
628 self.ylabel = 'Height [Km]'
622 self.titles = ['Zonal' ,'Meridional', 'Vertical']
629 self.titles = ['Zonal Wind' ,'Meridional Wind', 'Vertical Wind']
623 self.clabels = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)']
630 self.clabels = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)']
624 self.windFactor = [1, 1, 100]
631 self.windFactor = [1, 1, 100]
625
632
626 if self.figure is None:
633 if self.figure is None:
627 self.figure = plt.figure(figsize=(self.width, self.height),
634 self.figure = plt.figure(figsize=(self.width, self.height),
628 edgecolor='k',
635 edgecolor='k',
629 facecolor='w')
636 facecolor='w')
630 else:
637 else:
631 self.figure.clf()
638 self.figure.clf()
632 self.axes = []
639 self.axes = []
633
640
634 for n in range(self.nrows):
641 for n in range(self.nrows):
635 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
642 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
636 ax.firsttime = True
643 ax.firsttime = True
637 self.axes.append(ax)
644 self.axes.append(ax)
638
645
639 def plot(self):
646 def plot(self):
640
647
641 self.x = np.array(self.times)
648 self.x = np.array(self.times)
642 self.y = self.dataOut.heightList
649 self.y = self.dataOut.heightList
643 self.z = []
650 self.z = []
644
651
645 for ch in range(self.nrows):
652 for ch in range(self.nrows):
646 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
653 self.z.append([self.data['output'][t][ch] for t in self.times])
647
654
648 self.z = np.array(self.z)
655 self.z = np.array(self.z)
649 self.z = numpy.ma.masked_invalid(self.z)
656 self.z = numpy.ma.masked_invalid(self.z)
650
657
651 cmap=plt.get_cmap(self.colormap)
658 cmap=plt.get_cmap(self.colormap)
652 cmap.set_bad('white', 1.)
659 cmap.set_bad('black', 1.)
653
660
654 for n, ax in enumerate(self.axes):
661 for n, ax in enumerate(self.axes):
655 x, y, z = self.fill_gaps(*self.decimate())
662 x, y, z = self.fill_gaps(*self.decimate())
656 xmin = self.min_time
663 xmin = self.min_time
657 xmax = xmin+self.xrange*60*60
664 xmax = xmin+self.xrange*60*60
658 if ax.firsttime:
665 if ax.firsttime:
659 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
666 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
660 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
667 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
661 self.zmax = self.zmax if self.zmax else numpy.nanmax(abs(self.z[:-1, :]))
668 self.zmax = self.zmax if self.zmax else numpy.nanmax(abs(self.z[:-1, :]))
662 self.zmin = self.zmin if self.zmin else -self.zmax
669 self.zmin = self.zmin if self.zmin else -self.zmax
663
670
664 plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n],
671 plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n],
665 vmin=self.zmin,
672 vmin=self.zmin,
666 vmax=self.zmax,
673 vmax=self.zmax,
667 cmap=cmap
674 cmap=cmap
668 )
675 )
669 divider = make_axes_locatable(ax)
676 divider = make_axes_locatable(ax)
670 cax = divider.new_horizontal(size='2%', pad=0.05)
677 cax = divider.new_horizontal(size='2%', pad=0.05)
671 cax.set_ylabel(self.clabels[n])
672 self.figure.add_axes(cax)
678 self.figure.add_axes(cax)
673 plt.colorbar(plot, cax)
679 cb = plt.colorbar(plot, cax)
680 cb.set_label(self.clabels[n])
674 ax.set_ylim(self.ymin, self.ymax)
681 ax.set_ylim(self.ymin, self.ymax)
675
682
676 ax.xaxis.set_major_formatter(FuncFormatter(func))
683 ax.xaxis.set_major_formatter(FuncFormatter(func))
677 ax.xaxis.set_major_locator(LinearLocator(6))
684 ax.xaxis.set_major_locator(LinearLocator(6))
678
685
679 ax.set_ylabel(self.ylabel)
686 ax.set_ylabel(self.ylabel)
680
687
681 ax.set_xlim(xmin, xmax)
688 ax.set_xlim(xmin, xmax)
682 ax.firsttime = False
689 ax.firsttime = False
683 else:
690 else:
684 ax.collections.remove(ax.collections[0])
691 ax.collections.remove(ax.collections[0])
685 ax.set_xlim(xmin, xmax)
692 ax.set_xlim(xmin, xmax)
686 plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n],
693 plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n],
687 vmin=self.zmin,
694 vmin=self.zmin,
688 vmax=self.zmax,
695 vmax=self.zmax,
689 cmap=plt.get_cmap(self.colormap)
696 cmap=plt.get_cmap(self.colormap)
690 )
697 )
691 ax.set_title('{} {}'.format(self.titles[n],
698 ax.set_title('{} {}'.format(self.titles[n],
692 datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
699 datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
693 size=8)
700 size=8)
694
701
695 self.saveTime = self.min_time
702 self.saveTime = self.min_time
696
703
697
704
698 class PlotSNRData(PlotRTIData):
705 class PlotSNRData(PlotRTIData):
699 CODE = 'snr'
706 CODE = 'snr'
700 colormap = 'jet'
707 colormap = 'jet'
701
708
702 class PlotDOPData(PlotRTIData):
709 class PlotDOPData(PlotRTIData):
703 CODE = 'dop'
710 CODE = 'dop'
704 colormap = 'jet'
711 colormap = 'jet'
705
712
706
713
707 class PlotPHASEData(PlotCOHData):
714 class PlotPHASEData(PlotCOHData):
708 CODE = 'phase'
715 CODE = 'phase'
709 colormap = 'seismic'
716 colormap = 'seismic'
717
718
719 class PlotSkyMapData(PlotData):
720
721 CODE = 'met'
722
723 def setup(self):
724
725 self.ncols = 1
726 self.nrows = 1
727 self.width = 7.2
728 self.height = 7.2
729
730 self.xlabel = 'Zonal Zenith Angle (deg)'
731 self.ylabel = 'Meridional Zenith Angle (deg)'
732
733 if self.figure is None:
734 self.figure = plt.figure(figsize=(self.width, self.height),
735 edgecolor='k',
736 facecolor='w')
737 else:
738 self.figure.clf()
739
740 self.ax = plt.subplot2grid((self.nrows, self.ncols), (0, 0), 1, 1, polar=True)
741 self.ax.firsttime = True
742
743
744 def plot(self):
745
746 arrayParameters = np.concatenate([self.data['param'][t] for t in self.times])
747 error = arrayParameters[:,-1]
748 indValid = numpy.where(error == 0)[0]
749 finalMeteor = arrayParameters[indValid,:]
750 finalAzimuth = finalMeteor[:,3]
751 finalZenith = finalMeteor[:,4]
752
753 x = finalAzimuth*numpy.pi/180
754 y = finalZenith
755
756 if self.ax.firsttime:
757 self.ax.plot = self.ax.plot(x, y, 'bo', markersize=5)[0]
758 self.ax.set_ylim(0,90)
759 self.ax.set_yticks(numpy.arange(0,90,20))
760 self.ax.set_xlabel(self.xlabel)
761 self.ax.set_ylabel(self.ylabel)
762 self.ax.yaxis.labelpad = 40
763 self.ax.firsttime = False
764 else:
765 self.ax.plot.set_data(x, y)
766
767
768 dt1 = datetime.datetime.fromtimestamp(self.min_time).strftime('%y/%m/%d %H:%M:%S')
769 dt2 = datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')
770 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
771 dt2,
772 len(x))
773 self.ax.set_title(title, size=8)
774
775 self.saveTime = self.max_time
@@ -1,346 +1,349
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $
4 $Id: jroproc_base.py 1 2012-11-12 18:56:07Z murco $
5 '''
5 '''
6 import inspect
6 import inspect
7 from fuzzywuzzy import process
7 from fuzzywuzzy import process
8
8
9 def checkKwargs(method, kwargs):
9 def checkKwargs(method, kwargs):
10 currentKwargs = kwargs
10 currentKwargs = kwargs
11 choices = inspect.getargspec(method).args
11 choices = inspect.getargspec(method).args
12 try:
12 try:
13 choices.remove('self')
13 choices.remove('self')
14 except Exception as e:
14 except Exception as e:
15 pass
15 pass
16
16
17 try:
17 try:
18 choices.remove('dataOut')
18 choices.remove('dataOut')
19 except Exception as e:
19 except Exception as e:
20 pass
20 pass
21
21
22 for kwarg in kwargs:
22 for kwarg in kwargs:
23 fuzz = process.extractOne(kwarg, choices)
23 fuzz = process.extractOne(kwarg, choices)
24 if fuzz is None:
24 if fuzz is None:
25 continue
25 continue
26 if fuzz[1] < 100:
26 if fuzz[1] < 100:
27 raise Exception('\x1b[0;32;40mDid you mean {} instead of {} in {}? \x1b[0m'.
27 raise Exception('\x1b[0;32;40mDid you mean {} instead of {} in {}? \x1b[0m'.
28 format(fuzz[0], kwarg, method.__self__.__class__.__name__))
28 format(fuzz[0], kwarg, method.__self__.__class__.__name__))
29
29
30 class ProcessingUnit(object):
30 class ProcessingUnit(object):
31
31
32 """
32 """
33 Esta es la clase base para el procesamiento de datos.
33 Esta es la clase base para el procesamiento de datos.
34
34
35 Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser:
35 Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser:
36 - Metodos internos (callMethod)
36 - Metodos internos (callMethod)
37 - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos
37 - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos
38 tienen que ser agreagados con el metodo "add".
38 tienen que ser agreagados con el metodo "add".
39
39
40 """
40 """
41 # objeto de datos de entrada (Voltage, Spectra o Correlation)
41 # objeto de datos de entrada (Voltage, Spectra o Correlation)
42 dataIn = None
42 dataIn = None
43 dataInList = []
43 dataInList = []
44
44
45 # objeto de datos de entrada (Voltage, Spectra o Correlation)
45 # objeto de datos de entrada (Voltage, Spectra o Correlation)
46 dataOut = None
46 dataOut = None
47
47
48 operations2RunDict = None
48 operations2RunDict = None
49
49
50 isConfig = False
50 isConfig = False
51
51
52
52
53 def __init__(self, *args, **kwargs):
53 def __init__(self, *args, **kwargs):
54
54
55 self.dataIn = None
55 self.dataIn = None
56 self.dataInList = []
56 self.dataInList = []
57
57
58 self.dataOut = None
58 self.dataOut = None
59
59
60 self.operations2RunDict = {}
60 self.operations2RunDict = {}
61 self.operationKwargs = {}
61 self.operationKwargs = {}
62
62
63 self.isConfig = False
63 self.isConfig = False
64
64
65 self.args = args
65 self.args = args
66 self.kwargs = kwargs
66 self.kwargs = kwargs
67 checkKwargs(self.run, kwargs)
67 checkKwargs(self.run, kwargs)
68
68
69 def getAllowedArgs(self):
69 def getAllowedArgs(self):
70 return inspect.getargspec(self.run).args
70 return inspect.getargspec(self.run).args
71
71
72 def addOperationKwargs(self, objId, **kwargs):
72 def addOperationKwargs(self, objId, **kwargs):
73 '''
73 '''
74 '''
74 '''
75
75
76 self.operationKwargs[objId] = kwargs
76 self.operationKwargs[objId] = kwargs
77
77
78
78
79 def addOperation(self, opObj, objId):
79 def addOperation(self, opObj, objId):
80
80
81 """
81 """
82 Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el
82 Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el
83 identificador asociado a este objeto.
83 identificador asociado a este objeto.
84
84
85 Input:
85 Input:
86
86
87 object : objeto de la clase "Operation"
87 object : objeto de la clase "Operation"
88
88
89 Return:
89 Return:
90
90
91 objId : identificador del objeto, necesario para ejecutar la operacion
91 objId : identificador del objeto, necesario para ejecutar la operacion
92 """
92 """
93
93
94 self.operations2RunDict[objId] = opObj
94 self.operations2RunDict[objId] = opObj
95
95
96 return objId
96 return objId
97
97
98 def getOperationObj(self, objId):
98 def getOperationObj(self, objId):
99
99
100 if objId not in self.operations2RunDict.keys():
100 if objId not in self.operations2RunDict.keys():
101 return None
101 return None
102
102
103 return self.operations2RunDict[objId]
103 return self.operations2RunDict[objId]
104
104
105 def operation(self, **kwargs):
105 def operation(self, **kwargs):
106
106
107 """
107 """
108 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
109 atributos del objeto dataOut
109 atributos del objeto dataOut
110
110
111 Input:
111 Input:
112
112
113 **kwargs : Diccionario de argumentos de la funcion a ejecutar
113 **kwargs : Diccionario de argumentos de la funcion a ejecutar
114 """
114 """
115
115
116 raise NotImplementedError
116 raise NotImplementedError
117
117
118 def callMethod(self, name, opId):
118 def callMethod(self, name, opId):
119
119
120 """
120 """
121 Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase.
121 Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase.
122
122
123 Input:
123 Input:
124 name : nombre del metodo a ejecutar
124 name : nombre del metodo a ejecutar
125
125
126 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
126 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
127
127
128 """
128 """
129
129
130 #Checking the inputs
130 #Checking the inputs
131 if name == 'run':
131 if name == 'run':
132
132
133 if not self.checkInputs():
133 if not self.checkInputs():
134 self.dataOut.flagNoData = True
134 self.dataOut.flagNoData = True
135 return False
135 return False
136 else:
136 else:
137 #Si no es un metodo RUN la entrada es la misma dataOut (interna)
137 #Si no es un metodo RUN la entrada es la misma dataOut (interna)
138 if self.dataOut is not None and self.dataOut.isEmpty():
138 if self.dataOut is not None and self.dataOut.isEmpty():
139 return False
139 return False
140
140
141 #Getting the pointer to method
141 #Getting the pointer to method
142 methodToCall = getattr(self, name)
142 methodToCall = getattr(self, name)
143
143
144 #Executing the self method
144 #Executing the self method
145
145
146 if hasattr(self, 'mp'):
146 if hasattr(self, 'mp'):
147 if name=='run':
147 if name=='run':
148 if self.mp is False:
148 if self.mp is False:
149 self.mp = True
149 self.mp = True
150 self.start()
150 self.start()
151 else:
151 else:
152 self.operationKwargs[opId]['parent'] = self.kwargs
152 methodToCall(**self.operationKwargs[opId])
153 methodToCall(**self.operationKwargs[opId])
153 else:
154 else:
154 if name=='run':
155 if name=='run':
155 methodToCall(**self.kwargs)
156 methodToCall(**self.kwargs)
156 else:
157 else:
157 methodToCall(**self.operationKwargs[opId])
158 methodToCall(**self.operationKwargs[opId])
158
159
159 if self.dataOut is None:
160 if self.dataOut is None:
160 return False
161 return False
161
162
162 if self.dataOut.isEmpty():
163 if self.dataOut.isEmpty():
163 return False
164 return False
164
165
165 return True
166 return True
166
167
167 def callObject(self, objId):
168 def callObject(self, objId):
168
169
169 """
170 """
170 Ejecuta la operacion asociada al identificador del objeto "objId"
171 Ejecuta la operacion asociada al identificador del objeto "objId"
171
172
172 Input:
173 Input:
173
174
174 objId : identificador del objeto a ejecutar
175 objId : identificador del objeto a ejecutar
175
176
176 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
177 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
177
178
178 Return:
179 Return:
179
180
180 None
181 None
181 """
182 """
182
183
183 if self.dataOut is not None and self.dataOut.isEmpty():
184 if self.dataOut is not None and self.dataOut.isEmpty():
184 return False
185 return False
185
186
186 externalProcObj = self.operations2RunDict[objId]
187 externalProcObj = self.operations2RunDict[objId]
187
188
188 if hasattr(externalProcObj, 'mp'):
189 if hasattr(externalProcObj, 'mp'):
189 if externalProcObj.mp is False:
190 if externalProcObj.mp is False:
191 externalProcObj.kwargs['parent'] = self.kwargs
190 self.operationKwargs[objId] = externalProcObj.kwargs
192 self.operationKwargs[objId] = externalProcObj.kwargs
191 externalProcObj.mp = True
193 externalProcObj.mp = True
192 externalProcObj.start()
194 externalProcObj.start()
193 else:
195 else:
194 externalProcObj.run(self.dataOut, **externalProcObj.kwargs)
196 externalProcObj.run(self.dataOut, **externalProcObj.kwargs)
195 self.operationKwargs[objId] = externalProcObj.kwargs
197 self.operationKwargs[objId] = externalProcObj.kwargs
196
198
199
197 return True
200 return True
198
201
199 def call(self, opType, opName=None, opId=None):
202 def call(self, opType, opName=None, opId=None):
200
203
201 """
204 """
202 Return True si ejecuta la operacion interna nombrada "opName" o la operacion externa
205 Return True si ejecuta la operacion interna nombrada "opName" o la operacion externa
203 identificada con el id "opId"; con los argumentos "**kwargs".
206 identificada con el id "opId"; con los argumentos "**kwargs".
204
207
205 False si la operacion no se ha ejecutado.
208 False si la operacion no se ha ejecutado.
206
209
207 Input:
210 Input:
208
211
209 opType : Puede ser "self" o "external"
212 opType : Puede ser "self" o "external"
210
213
211 Depende del tipo de operacion para llamar a:callMethod or callObject:
214 Depende del tipo de operacion para llamar a:callMethod or callObject:
212
215
213 1. If opType = "self": Llama a un metodo propio de esta clase:
216 1. If opType = "self": Llama a un metodo propio de esta clase:
214
217
215 name_method = getattr(self, name)
218 name_method = getattr(self, name)
216 name_method(**kwargs)
219 name_method(**kwargs)
217
220
218
221
219 2. If opType = "other" o"external": Llama al metodo "run()" de una instancia de la
222 2. If opType = "other" o"external": Llama al metodo "run()" de una instancia de la
220 clase "Operation" o de un derivado de ella:
223 clase "Operation" o de un derivado de ella:
221
224
222 instanceName = self.operationList[opId]
225 instanceName = self.operationList[opId]
223 instanceName.run(**kwargs)
226 instanceName.run(**kwargs)
224
227
225 opName : Si la operacion es interna (opType = 'self'), entonces el "opName" sera
228 opName : Si la operacion es interna (opType = 'self'), entonces el "opName" sera
226 usada para llamar a un metodo interno de la clase Processing
229 usada para llamar a un metodo interno de la clase Processing
227
230
228 opId : Si la operacion es externa (opType = 'other' o 'external), entonces el
231 opId : Si la operacion es externa (opType = 'other' o 'external), entonces el
229 "opId" sera usada para llamar al metodo "run" de la clase Operation
232 "opId" sera usada para llamar al metodo "run" de la clase Operation
230 registrada anteriormente con ese Id
233 registrada anteriormente con ese Id
231
234
232 Exception:
235 Exception:
233 Este objeto de tipo Operation debe de haber sido agregado antes con el metodo:
236 Este objeto de tipo Operation debe de haber sido agregado antes con el metodo:
234 "addOperation" e identificado con el valor "opId" = el id de la operacion.
237 "addOperation" e identificado con el valor "opId" = el id de la operacion.
235 De lo contrario retornara un error del tipo ValueError
238 De lo contrario retornara un error del tipo ValueError
236
239
237 """
240 """
238
241
239 if opType == 'self':
242 if opType == 'self':
240
243
241 if not opName:
244 if not opName:
242 raise ValueError, "opName parameter should be defined"
245 raise ValueError, "opName parameter should be defined"
243
246
244 sts = self.callMethod(opName, opId)
247 sts = self.callMethod(opName, opId)
245
248
246 elif opType == 'other' or opType == 'external' or opType == 'plotter':
249 elif opType == 'other' or opType == 'external' or opType == 'plotter':
247
250
248 if not opId:
251 if not opId:
249 raise ValueError, "opId parameter should be defined"
252 raise ValueError, "opId parameter should be defined"
250
253
251 if opId not in self.operations2RunDict.keys():
254 if opId not in self.operations2RunDict.keys():
252 raise ValueError, "Any operation with id=%s has been added" %str(opId)
255 raise ValueError, "Any operation with id=%s has been added" %str(opId)
253
256
254 sts = self.callObject(opId)
257 sts = self.callObject(opId)
255
258
256 else:
259 else:
257 raise ValueError, "opType should be 'self', 'external' or 'plotter'; and not '%s'" %opType
260 raise ValueError, "opType should be 'self', 'external' or 'plotter'; and not '%s'" %opType
258
261
259 return sts
262 return sts
260
263
261 def setInput(self, dataIn):
264 def setInput(self, dataIn):
262
265
263 self.dataIn = dataIn
266 self.dataIn = dataIn
264 self.dataInList.append(dataIn)
267 self.dataInList.append(dataIn)
265
268
266 def getOutputObj(self):
269 def getOutputObj(self):
267
270
268 return self.dataOut
271 return self.dataOut
269
272
270 def checkInputs(self):
273 def checkInputs(self):
271
274
272 for thisDataIn in self.dataInList:
275 for thisDataIn in self.dataInList:
273
276
274 if thisDataIn.isEmpty():
277 if thisDataIn.isEmpty():
275 return False
278 return False
276
279
277 return True
280 return True
278
281
279 def setup(self):
282 def setup(self):
280
283
281 raise NotImplementedError
284 raise NotImplementedError
282
285
283 def run(self):
286 def run(self):
284
287
285 raise NotImplementedError
288 raise NotImplementedError
286
289
287 def close(self):
290 def close(self):
288 #Close every thread, queue or any other object here is it is neccesary.
291 #Close every thread, queue or any other object here is it is neccesary.
289 return
292 return
290
293
291 class Operation(object):
294 class Operation(object):
292
295
293 """
296 """
294 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
297 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
295 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
298 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
296 acumulacion dentro de esta clase
299 acumulacion dentro de esta clase
297
300
298 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
301 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
299
302
300 """
303 """
301
304
302 __buffer = None
305 __buffer = None
303 isConfig = False
306 isConfig = False
304
307
305 def __init__(self, **kwargs):
308 def __init__(self, **kwargs):
306
309
307 self.__buffer = None
310 self.__buffer = None
308 self.isConfig = False
311 self.isConfig = False
309 self.kwargs = kwargs
312 self.kwargs = kwargs
310 checkKwargs(self.run, kwargs)
313 checkKwargs(self.run, kwargs)
311
314
312 def getAllowedArgs(self):
315 def getAllowedArgs(self):
313 return inspect.getargspec(self.run).args
316 return inspect.getargspec(self.run).args
314
317
315 def setup(self):
318 def setup(self):
316
319
317 self.isConfig = True
320 self.isConfig = True
318
321
319 raise NotImplementedError
322 raise NotImplementedError
320
323
321 def run(self, dataIn, **kwargs):
324 def run(self, dataIn, **kwargs):
322
325
323 """
326 """
324 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los
327 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los
325 atributos del objeto dataIn.
328 atributos del objeto dataIn.
326
329
327 Input:
330 Input:
328
331
329 dataIn : objeto del tipo JROData
332 dataIn : objeto del tipo JROData
330
333
331 Return:
334 Return:
332
335
333 None
336 None
334
337
335 Affected:
338 Affected:
336 __buffer : buffer de recepcion de datos.
339 __buffer : buffer de recepcion de datos.
337
340
338 """
341 """
339 if not self.isConfig:
342 if not self.isConfig:
340 self.setup(**kwargs)
343 self.setup(**kwargs)
341
344
342 raise NotImplementedError
345 raise NotImplementedError
343
346
344 def close(self):
347 def close(self):
345
348
346 pass
349 pass
@@ -1,456 +1,464
1 '''
1 '''
2 @author: Juan C. Espinoza
2 @author: Juan C. Espinoza
3 '''
3 '''
4
4
5 import time
5 import time
6 import json
6 import json
7 import numpy
7 import numpy
8 import paho.mqtt.client as mqtt
8 import paho.mqtt.client as mqtt
9 import zmq
9 import zmq
10 from profilehooks import profile
10 from profilehooks import profile
11 import datetime
11 import datetime
12 from zmq.utils.monitor import recv_monitor_message
12 from zmq.utils.monitor import recv_monitor_message
13 from functools import wraps
13 from functools import wraps
14 from threading import Thread
14 from threading import Thread
15 from multiprocessing import Process
15 from multiprocessing import Process
16
16
17 from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit
17 from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit
18
18
19 MAXNUMX = 100
19 MAXNUMX = 100
20 MAXNUMY = 100
20 MAXNUMY = 100
21
21
22 class PrettyFloat(float):
22 class PrettyFloat(float):
23 def __repr__(self):
23 def __repr__(self):
24 return '%.2f' % self
24 return '%.2f' % self
25
25
26 def roundFloats(obj):
26 def roundFloats(obj):
27 if isinstance(obj, list):
27 if isinstance(obj, list):
28 return map(roundFloats, obj)
28 return map(roundFloats, obj)
29 elif isinstance(obj, float):
29 elif isinstance(obj, float):
30 return round(obj, 2)
30 return round(obj, 2)
31
31
32 def decimate(z, MAXNUMY):
32 def decimate(z, MAXNUMY):
33 # dx = int(len(self.x)/self.__MAXNUMX) + 1
33 # dx = int(len(self.x)/self.__MAXNUMX) + 1
34
34
35 dy = int(len(z[0])/MAXNUMY) + 1
35 dy = int(len(z[0])/MAXNUMY) + 1
36
36
37 return z[::, ::dy]
37 return z[::, ::dy]
38
38
39 class throttle(object):
39 class throttle(object):
40 """Decorator that prevents a function from being called more than once every
40 """Decorator that prevents a function from being called more than once every
41 time period.
41 time period.
42 To create a function that cannot be called more than once a minute, but
42 To create a function that cannot be called more than once a minute, but
43 will sleep until it can be called:
43 will sleep until it can be called:
44 @throttle(minutes=1)
44 @throttle(minutes=1)
45 def foo():
45 def foo():
46 pass
46 pass
47
47
48 for i in range(10):
48 for i in range(10):
49 foo()
49 foo()
50 print "This function has run %s times." % i
50 print "This function has run %s times." % i
51 """
51 """
52
52
53 def __init__(self, seconds=0, minutes=0, hours=0):
53 def __init__(self, seconds=0, minutes=0, hours=0):
54 self.throttle_period = datetime.timedelta(
54 self.throttle_period = datetime.timedelta(
55 seconds=seconds, minutes=minutes, hours=hours
55 seconds=seconds, minutes=minutes, hours=hours
56 )
56 )
57
57
58 self.time_of_last_call = datetime.datetime.min
58 self.time_of_last_call = datetime.datetime.min
59
59
60 def __call__(self, fn):
60 def __call__(self, fn):
61 @wraps(fn)
61 @wraps(fn)
62 def wrapper(*args, **kwargs):
62 def wrapper(*args, **kwargs):
63 now = datetime.datetime.now()
63 now = datetime.datetime.now()
64 time_since_last_call = now - self.time_of_last_call
64 time_since_last_call = now - self.time_of_last_call
65 time_left = self.throttle_period - time_since_last_call
65 time_left = self.throttle_period - time_since_last_call
66
66
67 if time_left > datetime.timedelta(seconds=0):
67 if time_left > datetime.timedelta(seconds=0):
68 return
68 return
69
69
70 self.time_of_last_call = datetime.datetime.now()
70 self.time_of_last_call = datetime.datetime.now()
71 return fn(*args, **kwargs)
71 return fn(*args, **kwargs)
72
72
73 return wrapper
73 return wrapper
74
74
75
75
76 class PublishData(Operation):
76 class PublishData(Operation):
77 """Clase publish."""
77 """Clase publish."""
78
78
79 def __init__(self, **kwargs):
79 def __init__(self, **kwargs):
80 """Inicio."""
80 """Inicio."""
81 Operation.__init__(self, **kwargs)
81 Operation.__init__(self, **kwargs)
82 self.isConfig = False
82 self.isConfig = False
83 self.client = None
83 self.client = None
84 self.zeromq = None
84 self.zeromq = None
85 self.mqtt = None
85 self.mqtt = None
86
86
87 def on_disconnect(self, client, userdata, rc):
87 def on_disconnect(self, client, userdata, rc):
88 if rc != 0:
88 if rc != 0:
89 print("Unexpected disconnection.")
89 print("Unexpected disconnection.")
90 self.connect()
90 self.connect()
91
91
92 def connect(self):
92 def connect(self):
93 print 'trying to connect'
93 print 'trying to connect'
94 try:
94 try:
95 self.client.connect(
95 self.client.connect(
96 host=self.host,
96 host=self.host,
97 port=self.port,
97 port=self.port,
98 keepalive=60*10,
98 keepalive=60*10,
99 bind_address='')
99 bind_address='')
100 self.client.loop_start()
100 self.client.loop_start()
101 # self.client.publish(
101 # self.client.publish(
102 # self.topic + 'SETUP',
102 # self.topic + 'SETUP',
103 # json.dumps(setup),
103 # json.dumps(setup),
104 # retain=True
104 # retain=True
105 # )
105 # )
106 except:
106 except:
107 print "MQTT Conection error."
107 print "MQTT Conection error."
108 self.client = False
108 self.client = False
109
109
110 def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, verbose=True, **kwargs):
110 def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, verbose=True, **kwargs):
111 self.counter = 0
111 self.counter = 0
112 self.topic = kwargs.get('topic', 'schain')
112 self.topic = kwargs.get('topic', 'schain')
113 self.delay = kwargs.get('delay', 0)
113 self.delay = kwargs.get('delay', 0)
114 self.plottype = kwargs.get('plottype', 'spectra')
114 self.plottype = kwargs.get('plottype', 'spectra')
115 self.host = kwargs.get('host', "10.10.10.82")
115 self.host = kwargs.get('host', "10.10.10.82")
116 self.port = kwargs.get('port', 3000)
116 self.port = kwargs.get('port', 3000)
117 self.clientId = clientId
117 self.clientId = clientId
118 self.cnt = 0
118 self.cnt = 0
119 self.zeromq = zeromq
119 self.zeromq = zeromq
120 self.mqtt = kwargs.get('plottype', 0)
120 self.mqtt = kwargs.get('plottype', 0)
121 self.client = None
121 self.client = None
122 self.verbose = verbose
122 self.verbose = verbose
123 self.dataOut.firstdata = True
123 self.dataOut.firstdata = True
124 setup = []
124 setup = []
125 if mqtt is 1:
125 if mqtt is 1:
126 self.client = mqtt.Client(
126 self.client = mqtt.Client(
127 client_id=self.clientId + self.topic + 'SCHAIN',
127 client_id=self.clientId + self.topic + 'SCHAIN',
128 clean_session=True)
128 clean_session=True)
129 self.client.on_disconnect = self.on_disconnect
129 self.client.on_disconnect = self.on_disconnect
130 self.connect()
130 self.connect()
131 for plot in self.plottype:
131 for plot in self.plottype:
132 setup.append({
132 setup.append({
133 'plot': plot,
133 'plot': plot,
134 'topic': self.topic + plot,
134 'topic': self.topic + plot,
135 'title': getattr(self, plot + '_' + 'title', False),
135 'title': getattr(self, plot + '_' + 'title', False),
136 'xlabel': getattr(self, plot + '_' + 'xlabel', False),
136 'xlabel': getattr(self, plot + '_' + 'xlabel', False),
137 'ylabel': getattr(self, plot + '_' + 'ylabel', False),
137 'ylabel': getattr(self, plot + '_' + 'ylabel', False),
138 'xrange': getattr(self, plot + '_' + 'xrange', False),
138 'xrange': getattr(self, plot + '_' + 'xrange', False),
139 'yrange': getattr(self, plot + '_' + 'yrange', False),
139 'yrange': getattr(self, plot + '_' + 'yrange', False),
140 'zrange': getattr(self, plot + '_' + 'zrange', False),
140 'zrange': getattr(self, plot + '_' + 'zrange', False),
141 })
141 })
142 if zeromq is 1:
142 if zeromq is 1:
143 context = zmq.Context()
143 context = zmq.Context()
144 self.zmq_socket = context.socket(zmq.PUSH)
144 self.zmq_socket = context.socket(zmq.PUSH)
145 server = kwargs.get('server', 'zmq.pipe')
145 server = kwargs.get('server', 'zmq.pipe')
146
146
147 if 'tcp://' in server:
147 if 'tcp://' in server:
148 address = server
148 address = server
149 else:
149 else:
150 address = 'ipc:///tmp/%s' % server
150 address = 'ipc:///tmp/%s' % server
151
151
152 self.zmq_socket.connect(address)
152 self.zmq_socket.connect(address)
153 time.sleep(1)
153 time.sleep(1)
154
154
155
155
156 def publish_data(self):
156 def publish_data(self):
157 self.dataOut.finished = False
157 self.dataOut.finished = False
158 if self.mqtt is 1:
158 if self.mqtt is 1:
159 yData = self.dataOut.heightList[:2].tolist()
159 yData = self.dataOut.heightList[:2].tolist()
160 if self.plottype == 'spectra':
160 if self.plottype == 'spectra':
161 data = getattr(self.dataOut, 'data_spc')
161 data = getattr(self.dataOut, 'data_spc')
162 z = data/self.dataOut.normFactor
162 z = data/self.dataOut.normFactor
163 zdB = 10*numpy.log10(z)
163 zdB = 10*numpy.log10(z)
164 xlen, ylen = zdB[0].shape
164 xlen, ylen = zdB[0].shape
165 dx = int(xlen/MAXNUMX) + 1
165 dx = int(xlen/MAXNUMX) + 1
166 dy = int(ylen/MAXNUMY) + 1
166 dy = int(ylen/MAXNUMY) + 1
167 Z = [0 for i in self.dataOut.channelList]
167 Z = [0 for i in self.dataOut.channelList]
168 for i in self.dataOut.channelList:
168 for i in self.dataOut.channelList:
169 Z[i] = zdB[i][::dx, ::dy].tolist()
169 Z[i] = zdB[i][::dx, ::dy].tolist()
170 payload = {
170 payload = {
171 'timestamp': self.dataOut.utctime,
171 'timestamp': self.dataOut.utctime,
172 'data': roundFloats(Z),
172 'data': roundFloats(Z),
173 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
173 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
174 'interval': self.dataOut.getTimeInterval(),
174 'interval': self.dataOut.getTimeInterval(),
175 'type': self.plottype,
175 'type': self.plottype,
176 'yData': yData
176 'yData': yData
177 }
177 }
178 # print payload
178 # print payload
179
179
180 elif self.plottype in ('rti', 'power'):
180 elif self.plottype in ('rti', 'power'):
181 data = getattr(self.dataOut, 'data_spc')
181 data = getattr(self.dataOut, 'data_spc')
182 z = data/self.dataOut.normFactor
182 z = data/self.dataOut.normFactor
183 avg = numpy.average(z, axis=1)
183 avg = numpy.average(z, axis=1)
184 avgdB = 10*numpy.log10(avg)
184 avgdB = 10*numpy.log10(avg)
185 xlen, ylen = z[0].shape
185 xlen, ylen = z[0].shape
186 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
186 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
187 AVG = [0 for i in self.dataOut.channelList]
187 AVG = [0 for i in self.dataOut.channelList]
188 for i in self.dataOut.channelList:
188 for i in self.dataOut.channelList:
189 AVG[i] = avgdB[i][::dy].tolist()
189 AVG[i] = avgdB[i][::dy].tolist()
190 payload = {
190 payload = {
191 'timestamp': self.dataOut.utctime,
191 'timestamp': self.dataOut.utctime,
192 'data': roundFloats(AVG),
192 'data': roundFloats(AVG),
193 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
193 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
194 'interval': self.dataOut.getTimeInterval(),
194 'interval': self.dataOut.getTimeInterval(),
195 'type': self.plottype,
195 'type': self.plottype,
196 'yData': yData
196 'yData': yData
197 }
197 }
198 elif self.plottype == 'noise':
198 elif self.plottype == 'noise':
199 noise = self.dataOut.getNoise()/self.dataOut.normFactor
199 noise = self.dataOut.getNoise()/self.dataOut.normFactor
200 noisedB = 10*numpy.log10(noise)
200 noisedB = 10*numpy.log10(noise)
201 payload = {
201 payload = {
202 'timestamp': self.dataOut.utctime,
202 'timestamp': self.dataOut.utctime,
203 'data': roundFloats(noisedB.reshape(-1, 1).tolist()),
203 'data': roundFloats(noisedB.reshape(-1, 1).tolist()),
204 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
204 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
205 'interval': self.dataOut.getTimeInterval(),
205 'interval': self.dataOut.getTimeInterval(),
206 'type': self.plottype,
206 'type': self.plottype,
207 'yData': yData
207 'yData': yData
208 }
208 }
209 elif self.plottype == 'snr':
209 elif self.plottype == 'snr':
210 data = getattr(self.dataOut, 'data_SNR')
210 data = getattr(self.dataOut, 'data_SNR')
211 avgdB = 10*numpy.log10(data)
211 avgdB = 10*numpy.log10(data)
212
212
213 ylen = data[0].size
213 ylen = data[0].size
214 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
214 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
215 AVG = [0 for i in self.dataOut.channelList]
215 AVG = [0 for i in self.dataOut.channelList]
216 for i in self.dataOut.channelList:
216 for i in self.dataOut.channelList:
217 AVG[i] = avgdB[i][::dy].tolist()
217 AVG[i] = avgdB[i][::dy].tolist()
218 payload = {
218 payload = {
219 'timestamp': self.dataOut.utctime,
219 'timestamp': self.dataOut.utctime,
220 'data': roundFloats(AVG),
220 'data': roundFloats(AVG),
221 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
221 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
222 'type': self.plottype,
222 'type': self.plottype,
223 'yData': yData
223 'yData': yData
224 }
224 }
225 else:
225 else:
226 print "Tipo de grafico invalido"
226 print "Tipo de grafico invalido"
227 payload = {
227 payload = {
228 'data': 'None',
228 'data': 'None',
229 'timestamp': 'None',
229 'timestamp': 'None',
230 'type': None
230 'type': None
231 }
231 }
232 # print 'Publishing data to {}'.format(self.host)
232 # print 'Publishing data to {}'.format(self.host)
233 self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0)
233 self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0)
234
234
235 if self.zeromq is 1:
235 if self.zeromq is 1:
236 if self.verbose:
236 if self.verbose:
237 print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime)
237 print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime)
238 self.zmq_socket.send_pyobj(self.dataOut)
238 self.zmq_socket.send_pyobj(self.dataOut)
239 self.dataOut.firstdata = False
239 self.dataOut.firstdata = False
240
240
241
241
242 def run(self, dataOut, **kwargs):
242 def run(self, dataOut, **kwargs):
243 self.dataOut = dataOut
243 self.dataOut = dataOut
244 if not self.isConfig:
244 if not self.isConfig:
245 self.setup(**kwargs)
245 self.setup(**kwargs)
246 self.isConfig = True
246 self.isConfig = True
247
247
248 self.publish_data()
248 self.publish_data()
249 time.sleep(self.delay)
249 time.sleep(self.delay)
250
250
251 def close(self):
251 def close(self):
252 if self.zeromq is 1:
252 if self.zeromq is 1:
253 self.dataOut.finished = True
253 self.dataOut.finished = True
254 self.zmq_socket.send_pyobj(self.dataOut)
254 self.zmq_socket.send_pyobj(self.dataOut)
255 self.zmq_socket.close()
255 self.zmq_socket.close()
256 if self.client:
256 if self.client:
257 self.client.loop_stop()
257 self.client.loop_stop()
258 self.client.disconnect()
258 self.client.disconnect()
259
259
260 class ReceiverData(ProcessingUnit, Process):
260 class ReceiverData(ProcessingUnit, Process):
261
261
262 throttle_value = 5
262 throttle_value = 5
263
263
264 def __init__(self, **kwargs):
264 def __init__(self, **kwargs):
265
265
266 ProcessingUnit.__init__(self, **kwargs)
266 ProcessingUnit.__init__(self, **kwargs)
267 Process.__init__(self)
267 Process.__init__(self)
268 self.mp = False
268 self.mp = False
269 self.isConfig = False
269 self.isConfig = False
270 self.isWebConfig = False
270 self.isWebConfig = False
271 self.plottypes =[]
271 self.plottypes =[]
272 self.connections = 0
272 self.connections = 0
273 server = kwargs.get('server', 'zmq.pipe')
273 server = kwargs.get('server', 'zmq.pipe')
274 plot_server = kwargs.get('plot_server', 'zmq.web')
274 plot_server = kwargs.get('plot_server', 'zmq.web')
275 if 'tcp://' in server:
275 if 'tcp://' in server:
276 address = server
276 address = server
277 else:
277 else:
278 address = 'ipc:///tmp/%s' % server
278 address = 'ipc:///tmp/%s' % server
279
279
280 if 'tcp://' in plot_server:
280 if 'tcp://' in plot_server:
281 plot_address = plot_server
281 plot_address = plot_server
282 else:
282 else:
283 plot_address = 'ipc:///tmp/%s' % plot_server
283 plot_address = 'ipc:///tmp/%s' % plot_server
284
284
285 self.address = address
285 self.address = address
286 self.plot_address = plot_address
286 self.plot_address = plot_address
287 self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')]
287 self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')]
288 self.realtime = kwargs.get('realtime', False)
288 self.realtime = kwargs.get('realtime', False)
289 self.throttle_value = kwargs.get('throttle', 5)
289 self.throttle_value = kwargs.get('throttle', 5)
290 self.sendData = self.initThrottle(self.throttle_value)
290 self.sendData = self.initThrottle(self.throttle_value)
291 self.setup()
291 self.setup()
292
292
293 def setup(self):
293 def setup(self):
294
294
295 self.data = {}
295 self.data = {}
296 self.data['times'] = []
296 self.data['times'] = []
297 for plottype in self.plottypes:
297 for plottype in self.plottypes:
298 self.data[plottype] = {}
298 self.data[plottype] = {}
299 self.data['noise'] = {}
299 self.data['noise'] = {}
300 self.data['throttle'] = self.throttle_value
300 self.data['throttle'] = self.throttle_value
301 self.data['ENDED'] = False
301 self.data['ENDED'] = False
302 self.isConfig = True
302 self.isConfig = True
303 self.data_web = {}
303 self.data_web = {}
304
304
305 def event_monitor(self, monitor):
305 def event_monitor(self, monitor):
306
306
307 events = {}
307 events = {}
308
308
309 for name in dir(zmq):
309 for name in dir(zmq):
310 if name.startswith('EVENT_'):
310 if name.startswith('EVENT_'):
311 value = getattr(zmq, name)
311 value = getattr(zmq, name)
312 events[value] = name
312 events[value] = name
313
313
314 while monitor.poll():
314 while monitor.poll():
315 evt = recv_monitor_message(monitor)
315 evt = recv_monitor_message(monitor)
316 if evt['event'] == 32:
316 if evt['event'] == 32:
317 self.connections += 1
317 self.connections += 1
318 if evt['event'] == 512:
318 if evt['event'] == 512:
319 pass
319 pass
320 if self.connections == 0 and self.started is True:
320 if self.connections == 0 and self.started is True:
321 self.ended = True
321 self.ended = True
322
322
323 evt.update({'description': events[evt['event']]})
323 evt.update({'description': events[evt['event']]})
324
324
325 if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
325 if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
326 break
326 break
327 monitor.close()
327 monitor.close()
328 print("event monitor thread done!")
328 print("event monitor thread done!")
329
329
330 def initThrottle(self, throttle_value):
330 def initThrottle(self, throttle_value):
331
331
332 @throttle(seconds=throttle_value)
332 @throttle(seconds=throttle_value)
333 def sendDataThrottled(fn_sender, data):
333 def sendDataThrottled(fn_sender, data):
334 fn_sender(data)
334 fn_sender(data)
335
335
336 return sendDataThrottled
336 return sendDataThrottled
337
337
338
338
339 def send(self, data):
339 def send(self, data):
340 # print '[sending] data=%s size=%s' % (data.keys(), len(data['times']))
340 # print '[sending] data=%s size=%s' % (data.keys(), len(data['times']))
341 self.sender.send_pyobj(data)
341 self.sender.send_pyobj(data)
342
342
343
343
344 def update(self):
344 def update(self):
345 t = self.dataOut.utctime
345 t = self.dataOut.utctime
346
346
347 if t in self.data['times']:
347 if t in self.data['times']:
348 return
348 return
349
349
350 self.data['times'].append(t)
350 self.data['times'].append(t)
351 self.data['dataOut'] = self.dataOut
351 self.data['dataOut'] = self.dataOut
352
352
353 for plottype in self.plottypes:
353 for plottype in self.plottypes:
354 if plottype == 'spc':
354 if plottype == 'spc':
355 z = self.dataOut.data_spc/self.dataOut.normFactor
355 z = self.dataOut.data_spc/self.dataOut.normFactor
356 self.data[plottype] = 10*numpy.log10(z)
356 self.data[plottype] = 10*numpy.log10(z)
357 self.data['noise'][t] = 10*numpy.log10(self.dataOut.getNoise()/self.dataOut.normFactor)
357 self.data['noise'][t] = 10*numpy.log10(self.dataOut.getNoise()/self.dataOut.normFactor)
358 if plottype == 'cspc':
358 if plottype == 'cspc':
359 jcoherence = self.dataOut.data_cspc/numpy.sqrt(self.dataOut.data_spc*self.dataOut.data_spc)
359 jcoherence = self.dataOut.data_cspc/numpy.sqrt(self.dataOut.data_spc*self.dataOut.data_spc)
360 self.data['cspc_coh'] = numpy.abs(jcoherence)
360 self.data['cspc_coh'] = numpy.abs(jcoherence)
361 self.data['cspc_phase'] = numpy.arctan2(jcoherence.imag, jcoherence.real)*180/numpy.pi
361 self.data['cspc_phase'] = numpy.arctan2(jcoherence.imag, jcoherence.real)*180/numpy.pi
362 if plottype == 'rti':
362 if plottype == 'rti':
363 self.data[plottype][t] = self.dataOut.getPower()
363 self.data[plottype][t] = self.dataOut.getPower()
364 if plottype == 'snr':
364 if plottype == 'snr':
365 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR)
365 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR)
366 if plottype == 'dop':
366 if plottype == 'dop':
367 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP)
367 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP)
368 if plottype == 'mean':
368 if plottype == 'mean':
369 self.data[plottype][t] = self.dataOut.data_MEAN
369 self.data[plottype][t] = self.dataOut.data_MEAN
370 if plottype == 'std':
370 if plottype == 'std':
371 self.data[plottype][t] = self.dataOut.data_STD
371 self.data[plottype][t] = self.dataOut.data_STD
372 if plottype == 'coh':
372 if plottype == 'coh':
373 self.data[plottype][t] = self.dataOut.getCoherence()
373 self.data[plottype][t] = self.dataOut.getCoherence()
374 if plottype == 'phase':
374 if plottype == 'phase':
375 self.data[plottype][t] = self.dataOut.getCoherence(phase=True)
375 self.data[plottype][t] = self.dataOut.getCoherence(phase=True)
376 if plottype == 'wind':
376 if plottype == 'output':
377 self.data[plottype][t] = self.dataOut.data_output
377 self.data[plottype][t] = self.dataOut.data_output
378 if plottype == 'param':
379 self.data[plottype][t] = self.dataOut.data_param
378 if self.realtime:
380 if self.realtime:
379 self.data_web['timestamp'] = t
381 self.data_web['timestamp'] = t
380 if plottype == 'spc':
382 if plottype == 'spc':
381 self.data_web[plottype] = roundFloats(decimate(self.data[plottype]).tolist())
383 self.data_web[plottype] = roundFloats(decimate(self.data[plottype]).tolist())
382 elif plottype == 'cspc':
384 elif plottype == 'cspc':
383 self.data_web['cspc_coh'] = roundFloats(decimate(self.data['cspc_coh']).tolist())
385 self.data_web['cspc_coh'] = roundFloats(decimate(self.data['cspc_coh']).tolist())
384 self.data_web['cspc_phase'] = roundFloats(decimate(self.data['cspc_phase']).tolist())
386 self.data_web['cspc_phase'] = roundFloats(decimate(self.data['cspc_phase']).tolist())
385 elif plottype == 'noise':
387 elif plottype == 'noise':
386 self.data_web['noise'] = roundFloats(self.data['noise'][t].tolist())
388 self.data_web['noise'] = roundFloats(self.data['noise'][t].tolist())
387 else:
389 else:
388 self.data_web[plottype] = roundFloats(decimate(self.data[plottype][t]).tolist())
390 self.data_web[plottype] = roundFloats(decimate(self.data[plottype][t]).tolist())
389 self.data_web['interval'] = self.dataOut.getTimeInterval()
391 self.data_web['interval'] = self.dataOut.getTimeInterval()
390 self.data_web['type'] = plottype
392 self.data_web['type'] = plottype
391
393
392 def run(self):
394 def run(self):
393
395
394 print '[Starting] {} from {}'.format(self.name, self.address)
396 print '[Starting] {} from {}'.format(self.name, self.address)
395
397
396 self.context = zmq.Context()
398 self.context = zmq.Context()
397 self.receiver = self.context.socket(zmq.PULL)
399 self.receiver = self.context.socket(zmq.PULL)
398 self.receiver.bind(self.address)
400 self.receiver.bind(self.address)
399 monitor = self.receiver.get_monitor_socket()
401 monitor = self.receiver.get_monitor_socket()
400 self.sender = self.context.socket(zmq.PUB)
402 self.sender = self.context.socket(zmq.PUB)
401 if self.realtime:
403 if self.realtime:
402 self.sender_web = self.context.socket(zmq.PUB)
404 self.sender_web = self.context.socket(zmq.PUB)
403 self.sender_web.connect(self.plot_address)
405 self.sender_web.connect(self.plot_address)
404 time.sleep(1)
406 time.sleep(1)
405 self.sender.bind("ipc:///tmp/zmq.plots")
407
408 if 'server' in self.kwargs:
409 self.sender.bind("ipc:///tmp/{}.plots".format(self.kwargs['server']))
410 else:
411 self.sender.bind("ipc:///tmp/zmq.plots")
412
406 time.sleep(3)
413 time.sleep(3)
414
407 t = Thread(target=self.event_monitor, args=(monitor,))
415 t = Thread(target=self.event_monitor, args=(monitor,))
408 t.start()
416 t.start()
409
417
410 while True:
418 while True:
411 self.dataOut = self.receiver.recv_pyobj()
419 self.dataOut = self.receiver.recv_pyobj()
412 # print '[Receiving] {} - {}'.format(self.dataOut.type,
420 # print '[Receiving] {} - {}'.format(self.dataOut.type,
413 # self.dataOut.datatime.ctime())
421 # self.dataOut.datatime.ctime())
414
422
415 self.update()
423 self.update()
416
424
417 if self.dataOut.firstdata is True:
425 if self.dataOut.firstdata is True:
418 self.data['STARTED'] = True
426 self.data['STARTED'] = True
419
427
420 if self.dataOut.finished is True:
428 if self.dataOut.finished is True:
421 self.send(self.data)
429 self.send(self.data)
422 self.connections -= 1
430 self.connections -= 1
423 if self.connections == 0 and self.started:
431 if self.connections == 0 and self.started:
424 self.ended = True
432 self.ended = True
425 self.data['ENDED'] = True
433 self.data['ENDED'] = True
426 self.send(self.data)
434 self.send(self.data)
427 self.setup()
435 self.setup()
428 self.started = False
436 self.started = False
429 else:
437 else:
430 if self.realtime:
438 if self.realtime:
431 self.send(self.data)
439 self.send(self.data)
432 self.sender_web.send_string(json.dumps(self.data_web))
440 self.sender_web.send_string(json.dumps(self.data_web))
433 else:
441 else:
434 self.sendData(self.send, self.data)
442 self.sendData(self.send, self.data)
435 self.started = True
443 self.started = True
436
444
437 self.data['STARTED'] = False
445 self.data['STARTED'] = False
438 return
446 return
439
447
440 def sendToWeb(self):
448 def sendToWeb(self):
441
449
442 if not self.isWebConfig:
450 if not self.isWebConfig:
443 context = zmq.Context()
451 context = zmq.Context()
444 sender_web_config = context.socket(zmq.PUB)
452 sender_web_config = context.socket(zmq.PUB)
445 if 'tcp://' in self.plot_address:
453 if 'tcp://' in self.plot_address:
446 dum, address, port = self.plot_address.split(':')
454 dum, address, port = self.plot_address.split(':')
447 conf_address = '{}:{}:{}'.format(dum, address, int(port)+1)
455 conf_address = '{}:{}:{}'.format(dum, address, int(port)+1)
448 else:
456 else:
449 conf_address = self.plot_address + '.config'
457 conf_address = self.plot_address + '.config'
450 sender_web_config.bind(conf_address)
458 sender_web_config.bind(conf_address)
451 time.sleep(1)
459 time.sleep(1)
452 for kwargs in self.operationKwargs.values():
460 for kwargs in self.operationKwargs.values():
453 if 'plot' in kwargs:
461 if 'plot' in kwargs:
454 print '[Sending] Config data to web for {}'.format(kwargs['code'].upper())
462 print '[Sending] Config data to web for {}'.format(kwargs['code'].upper())
455 sender_web_config.send_string(json.dumps(kwargs))
463 sender_web_config.send_string(json.dumps(kwargs))
456 self.isWebConfig = True
464 self.isWebConfig = True
General Comments 0
You need to be logged in to leave comments. Login now