1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
This diff has been collapsed as it changes many lines, (782 lines changed) Show them Hide them | |||||
@@ -0,0 +1,782 | |||||
|
1 | ''' | |||
|
2 | ||||
|
3 | $Author: murco $ | |||
|
4 | $Id: JROData.py 173 2012-11-20 15:06:21Z murco $ | |||
|
5 | ''' | |||
|
6 | ||||
|
7 | import copy | |||
|
8 | import numpy | |||
|
9 | import datetime | |||
|
10 | ||||
|
11 | from jroheaderIO import SystemHeader, RadarControllerHeader | |||
|
12 | ||||
|
13 | def getNumpyDtype(dataTypeCode): | |||
|
14 | ||||
|
15 | if dataTypeCode == 0: | |||
|
16 | numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')]) | |||
|
17 | elif dataTypeCode == 1: | |||
|
18 | numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')]) | |||
|
19 | elif dataTypeCode == 2: | |||
|
20 | numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')]) | |||
|
21 | elif dataTypeCode == 3: | |||
|
22 | numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')]) | |||
|
23 | elif dataTypeCode == 4: | |||
|
24 | numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
25 | elif dataTypeCode == 5: | |||
|
26 | numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')]) | |||
|
27 | else: | |||
|
28 | raise ValueError, 'dataTypeCode was not defined' | |||
|
29 | ||||
|
30 | return numpyDtype | |||
|
31 | ||||
|
32 | def getDataTypeCode(numpyDtype): | |||
|
33 | ||||
|
34 | if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]): | |||
|
35 | datatype = 0 | |||
|
36 | elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]): | |||
|
37 | datatype = 1 | |||
|
38 | elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]): | |||
|
39 | datatype = 2 | |||
|
40 | elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]): | |||
|
41 | datatype = 3 | |||
|
42 | elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]): | |||
|
43 | datatype = 4 | |||
|
44 | elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]): | |||
|
45 | datatype = 5 | |||
|
46 | else: | |||
|
47 | datatype = None | |||
|
48 | ||||
|
49 | return datatype | |||
|
50 | ||||
|
51 | def hildebrand_sekhon(data, navg): | |||
|
52 | ||||
|
53 | data = data.copy() | |||
|
54 | ||||
|
55 | sortdata = numpy.sort(data,axis=None) | |||
|
56 | lenOfData = len(sortdata) | |||
|
57 | nums_min = lenOfData/10 | |||
|
58 | ||||
|
59 | if (lenOfData/10) > 2: | |||
|
60 | nums_min = lenOfData/10 | |||
|
61 | else: | |||
|
62 | nums_min = 2 | |||
|
63 | ||||
|
64 | sump = 0. | |||
|
65 | ||||
|
66 | sumq = 0. | |||
|
67 | ||||
|
68 | j = 0 | |||
|
69 | ||||
|
70 | cont = 1 | |||
|
71 | ||||
|
72 | while((cont==1)and(j<lenOfData)): | |||
|
73 | ||||
|
74 | sump += sortdata[j] | |||
|
75 | ||||
|
76 | sumq += sortdata[j]**2 | |||
|
77 | ||||
|
78 | j += 1 | |||
|
79 | ||||
|
80 | if j > nums_min: | |||
|
81 | rtest = float(j)/(j-1) + 1.0/navg | |||
|
82 | if ((sumq*j) > (rtest*sump**2)): | |||
|
83 | j = j - 1 | |||
|
84 | sump = sump - sortdata[j] | |||
|
85 | sumq = sumq - sortdata[j]**2 | |||
|
86 | cont = 0 | |||
|
87 | ||||
|
88 | lnoise = sump /j | |||
|
89 | stdv = numpy.sqrt((sumq - lnoise**2)/(j - 1)) | |||
|
90 | return lnoise | |||
|
91 | ||||
|
92 | class GenericData(object): | |||
|
93 | ||||
|
94 | flagNoData = True | |||
|
95 | ||||
|
96 | def __init__(self): | |||
|
97 | ||||
|
98 | raise ValueError, "This class has not been implemented" | |||
|
99 | ||||
|
100 | def copy(self, inputObj=None): | |||
|
101 | ||||
|
102 | if inputObj == None: | |||
|
103 | return copy.deepcopy(self) | |||
|
104 | ||||
|
105 | for key in inputObj.__dict__.keys(): | |||
|
106 | self.__dict__[key] = inputObj.__dict__[key] | |||
|
107 | ||||
|
108 | def deepcopy(self): | |||
|
109 | ||||
|
110 | return copy.deepcopy(self) | |||
|
111 | ||||
|
112 | def isEmpty(self): | |||
|
113 | ||||
|
114 | return self.flagNoData | |||
|
115 | ||||
|
116 | class JROData(GenericData): | |||
|
117 | ||||
|
118 | # m_BasicHeader = BasicHeader() | |||
|
119 | # m_ProcessingHeader = ProcessingHeader() | |||
|
120 | ||||
|
121 | systemHeaderObj = SystemHeader() | |||
|
122 | ||||
|
123 | radarControllerHeaderObj = RadarControllerHeader() | |||
|
124 | ||||
|
125 | # data = None | |||
|
126 | ||||
|
127 | type = None | |||
|
128 | ||||
|
129 | datatype = None #dtype but in string | |||
|
130 | ||||
|
131 | # dtype = None | |||
|
132 | ||||
|
133 | # nChannels = None | |||
|
134 | ||||
|
135 | # nHeights = None | |||
|
136 | ||||
|
137 | nProfiles = None | |||
|
138 | ||||
|
139 | heightList = None | |||
|
140 | ||||
|
141 | channelList = None | |||
|
142 | ||||
|
143 | flagTimeBlock = False | |||
|
144 | ||||
|
145 | useLocalTime = False | |||
|
146 | ||||
|
147 | utctime = None | |||
|
148 | ||||
|
149 | timeZone = None | |||
|
150 | ||||
|
151 | dstFlag = None | |||
|
152 | ||||
|
153 | errorCount = None | |||
|
154 | ||||
|
155 | blocksize = None | |||
|
156 | ||||
|
157 | nCode = None | |||
|
158 | ||||
|
159 | nBaud = None | |||
|
160 | ||||
|
161 | code = None | |||
|
162 | ||||
|
163 | flagDecodeData = False #asumo q la data no esta decodificada | |||
|
164 | ||||
|
165 | flagDeflipData = False #asumo q la data no esta sin flip | |||
|
166 | ||||
|
167 | flagShiftFFT = False | |||
|
168 | ||||
|
169 | # ippSeconds = None | |||
|
170 | ||||
|
171 | timeInterval = None | |||
|
172 | ||||
|
173 | nCohInt = None | |||
|
174 | ||||
|
175 | noise = None | |||
|
176 | ||||
|
177 | windowOfFilter = 1 | |||
|
178 | ||||
|
179 | #Speed of ligth | |||
|
180 | C = 3e8 | |||
|
181 | ||||
|
182 | frequency = 49.92e6 | |||
|
183 | ||||
|
184 | realtime = False | |||
|
185 | ||||
|
186 | beacon_heiIndexList = None | |||
|
187 | ||||
|
188 | last_block = None | |||
|
189 | ||||
|
190 | blocknow = None | |||
|
191 | ||||
|
192 | def __init__(self): | |||
|
193 | ||||
|
194 | raise ValueError, "This class has not been implemented" | |||
|
195 | ||||
|
196 | def getNoise(self): | |||
|
197 | ||||
|
198 | raise ValueError, "Not implemented" | |||
|
199 | ||||
|
200 | def getNChannels(self): | |||
|
201 | ||||
|
202 | return len(self.channelList) | |||
|
203 | ||||
|
204 | def getChannelIndexList(self): | |||
|
205 | ||||
|
206 | return range(self.nChannels) | |||
|
207 | ||||
|
208 | def getNHeights(self): | |||
|
209 | ||||
|
210 | return len(self.heightList) | |||
|
211 | ||||
|
212 | def getHeiRange(self, extrapoints=0): | |||
|
213 | ||||
|
214 | heis = self.heightList | |||
|
215 | # deltah = self.heightList[1] - self.heightList[0] | |||
|
216 | # | |||
|
217 | # heis.append(self.heightList[-1]) | |||
|
218 | ||||
|
219 | return heis | |||
|
220 | ||||
|
221 | def getltctime(self): | |||
|
222 | ||||
|
223 | if self.useLocalTime: | |||
|
224 | return self.utctime - self.timeZone*60 | |||
|
225 | ||||
|
226 | return self.utctime | |||
|
227 | ||||
|
228 | def getDatatime(self): | |||
|
229 | ||||
|
230 | datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime) | |||
|
231 | return datatimeValue | |||
|
232 | ||||
|
233 | def getTimeRange(self): | |||
|
234 | ||||
|
235 | datatime = [] | |||
|
236 | ||||
|
237 | datatime.append(self.ltctime) | |||
|
238 | datatime.append(self.ltctime + self.timeInterval) | |||
|
239 | ||||
|
240 | datatime = numpy.array(datatime) | |||
|
241 | ||||
|
242 | return datatime | |||
|
243 | ||||
|
244 | def getFmax(self): | |||
|
245 | ||||
|
246 | PRF = 1./(self.ippSeconds * self.nCohInt) | |||
|
247 | ||||
|
248 | fmax = PRF/2. | |||
|
249 | ||||
|
250 | return fmax | |||
|
251 | ||||
|
252 | def getVmax(self): | |||
|
253 | ||||
|
254 | _lambda = self.C/self.frequency | |||
|
255 | ||||
|
256 | vmax = self.getFmax() * _lambda | |||
|
257 | ||||
|
258 | return vmax | |||
|
259 | ||||
|
260 | def get_ippSeconds(self): | |||
|
261 | ''' | |||
|
262 | ''' | |||
|
263 | return self.radarControllerHeaderObj.ippSeconds | |||
|
264 | ||||
|
265 | def set_ippSeconds(self, ippSeconds): | |||
|
266 | ''' | |||
|
267 | ''' | |||
|
268 | ||||
|
269 | self.radarControllerHeaderObj.ippSeconds = ippSeconds | |||
|
270 | ||||
|
271 | return | |||
|
272 | ||||
|
273 | def get_dtype(self): | |||
|
274 | ''' | |||
|
275 | ''' | |||
|
276 | return getNumpyDtype(self.datatype) | |||
|
277 | ||||
|
278 | def set_dtype(self, numpyDtype): | |||
|
279 | ''' | |||
|
280 | ''' | |||
|
281 | ||||
|
282 | self.datatype = getDataTypeCode(numpyDtype) | |||
|
283 | ||||
|
284 | nChannels = property(getNChannels, "I'm the 'nChannel' property.") | |||
|
285 | channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") | |||
|
286 | nHeights = property(getNHeights, "I'm the 'nHeights' property.") | |||
|
287 | #noise = property(getNoise, "I'm the 'nHeights' property.") | |||
|
288 | datatime = property(getDatatime, "I'm the 'datatime' property") | |||
|
289 | ltctime = property(getltctime, "I'm the 'ltctime' property") | |||
|
290 | ippSeconds = property(get_ippSeconds, set_ippSeconds) | |||
|
291 | dtype = property(get_dtype, set_dtype) | |||
|
292 | ||||
|
293 | class Voltage(JROData): | |||
|
294 | ||||
|
295 | #data es un numpy array de 2 dmensiones (canales, alturas) | |||
|
296 | data = None | |||
|
297 | ||||
|
298 | def __init__(self): | |||
|
299 | ''' | |||
|
300 | Constructor | |||
|
301 | ''' | |||
|
302 | ||||
|
303 | self.radarControllerHeaderObj = RadarControllerHeader() | |||
|
304 | ||||
|
305 | self.systemHeaderObj = SystemHeader() | |||
|
306 | ||||
|
307 | self.type = "Voltage" | |||
|
308 | ||||
|
309 | self.data = None | |||
|
310 | ||||
|
311 | # self.dtype = None | |||
|
312 | ||||
|
313 | # self.nChannels = 0 | |||
|
314 | ||||
|
315 | # self.nHeights = 0 | |||
|
316 | ||||
|
317 | self.nProfiles = None | |||
|
318 | ||||
|
319 | self.heightList = None | |||
|
320 | ||||
|
321 | self.channelList = None | |||
|
322 | ||||
|
323 | # self.channelIndexList = None | |||
|
324 | ||||
|
325 | self.flagNoData = True | |||
|
326 | ||||
|
327 | self.flagTimeBlock = False | |||
|
328 | ||||
|
329 | self.utctime = None | |||
|
330 | ||||
|
331 | self.timeZone = None | |||
|
332 | ||||
|
333 | self.dstFlag = None | |||
|
334 | ||||
|
335 | self.errorCount = None | |||
|
336 | ||||
|
337 | self.nCohInt = None | |||
|
338 | ||||
|
339 | self.blocksize = None | |||
|
340 | ||||
|
341 | self.flagDecodeData = False #asumo q la data no esta decodificada | |||
|
342 | ||||
|
343 | self.flagDeflipData = False #asumo q la data no esta sin flip | |||
|
344 | ||||
|
345 | self.flagShiftFFT = False | |||
|
346 | ||||
|
347 | ||||
|
348 | def getNoisebyHildebrand(self): | |||
|
349 | """ | |||
|
350 | Determino el nivel de ruido usando el metodo Hildebrand-Sekhon | |||
|
351 | ||||
|
352 | Return: | |||
|
353 | noiselevel | |||
|
354 | """ | |||
|
355 | ||||
|
356 | for channel in range(self.nChannels): | |||
|
357 | daux = self.data_spc[channel,:,:] | |||
|
358 | self.noise[channel] = hildebrand_sekhon(daux, self.nCohInt) | |||
|
359 | ||||
|
360 | return self.noise | |||
|
361 | ||||
|
362 | def getNoise(self, type = 1): | |||
|
363 | ||||
|
364 | self.noise = numpy.zeros(self.nChannels) | |||
|
365 | ||||
|
366 | if type == 1: | |||
|
367 | noise = self.getNoisebyHildebrand() | |||
|
368 | ||||
|
369 | return 10*numpy.log10(noise) | |||
|
370 | ||||
|
371 | noise = property(getNoise, "I'm the 'nHeights' property.") | |||
|
372 | ||||
|
373 | class Spectra(JROData): | |||
|
374 | ||||
|
375 | #data es un numpy array de 2 dmensiones (canales, perfiles, alturas) | |||
|
376 | data_spc = None | |||
|
377 | ||||
|
378 | #data es un numpy array de 2 dmensiones (canales, pares, alturas) | |||
|
379 | data_cspc = None | |||
|
380 | ||||
|
381 | #data es un numpy array de 2 dmensiones (canales, alturas) | |||
|
382 | data_dc = None | |||
|
383 | ||||
|
384 | nFFTPoints = None | |||
|
385 | ||||
|
386 | # nPairs = None | |||
|
387 | ||||
|
388 | pairsList = None | |||
|
389 | ||||
|
390 | nIncohInt = None | |||
|
391 | ||||
|
392 | wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia | |||
|
393 | ||||
|
394 | nCohInt = None #se requiere para determinar el valor de timeInterval | |||
|
395 | ||||
|
396 | ippFactor = None | |||
|
397 | ||||
|
398 | def __init__(self): | |||
|
399 | ''' | |||
|
400 | Constructor | |||
|
401 | ''' | |||
|
402 | ||||
|
403 | self.radarControllerHeaderObj = RadarControllerHeader() | |||
|
404 | ||||
|
405 | self.systemHeaderObj = SystemHeader() | |||
|
406 | ||||
|
407 | self.type = "Spectra" | |||
|
408 | ||||
|
409 | # self.data = None | |||
|
410 | ||||
|
411 | # self.dtype = None | |||
|
412 | ||||
|
413 | # self.nChannels = 0 | |||
|
414 | ||||
|
415 | # self.nHeights = 0 | |||
|
416 | ||||
|
417 | self.nProfiles = None | |||
|
418 | ||||
|
419 | self.heightList = None | |||
|
420 | ||||
|
421 | self.channelList = None | |||
|
422 | ||||
|
423 | # self.channelIndexList = None | |||
|
424 | ||||
|
425 | self.pairsList = None | |||
|
426 | ||||
|
427 | self.flagNoData = True | |||
|
428 | ||||
|
429 | self.flagTimeBlock = False | |||
|
430 | ||||
|
431 | self.utctime = None | |||
|
432 | ||||
|
433 | self.nCohInt = None | |||
|
434 | ||||
|
435 | self.nIncohInt = None | |||
|
436 | ||||
|
437 | self.blocksize = None | |||
|
438 | ||||
|
439 | self.nFFTPoints = None | |||
|
440 | ||||
|
441 | self.wavelength = None | |||
|
442 | ||||
|
443 | self.flagDecodeData = False #asumo q la data no esta decodificada | |||
|
444 | ||||
|
445 | self.flagDeflipData = False #asumo q la data no esta sin flip | |||
|
446 | ||||
|
447 | self.flagShiftFFT = False | |||
|
448 | ||||
|
449 | self.ippFactor = 1 | |||
|
450 | ||||
|
451 | #self.noise = None | |||
|
452 | ||||
|
453 | self.beacon_heiIndexList = [] | |||
|
454 | ||||
|
455 | self.noise_estimation = None | |||
|
456 | ||||
|
457 | ||||
|
458 | def getNoisebyHildebrand(self): | |||
|
459 | """ | |||
|
460 | Determino el nivel de ruido usando el metodo Hildebrand-Sekhon | |||
|
461 | ||||
|
462 | Return: | |||
|
463 | noiselevel | |||
|
464 | """ | |||
|
465 | ||||
|
466 | noise = numpy.zeros(self.nChannels) | |||
|
467 | for channel in range(self.nChannels): | |||
|
468 | daux = self.data_spc[channel,:,:] | |||
|
469 | noise[channel] = hildebrand_sekhon(daux, self.nIncohInt) | |||
|
470 | ||||
|
471 | return noise | |||
|
472 | ||||
|
473 | def getNoise(self): | |||
|
474 | if self.noise_estimation != None: | |||
|
475 | return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py | |||
|
476 | else: | |||
|
477 | noise = self.getNoisebyHildebrand() | |||
|
478 | return noise | |||
|
479 | ||||
|
480 | ||||
|
481 | def getFreqRange(self, extrapoints=0): | |||
|
482 | ||||
|
483 | deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor) | |||
|
484 | freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2 | |||
|
485 | ||||
|
486 | return freqrange | |||
|
487 | ||||
|
488 | def getVelRange(self, extrapoints=0): | |||
|
489 | ||||
|
490 | deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor) | |||
|
491 | velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltav/2 | |||
|
492 | ||||
|
493 | return velrange | |||
|
494 | ||||
|
495 | def getNPairs(self): | |||
|
496 | ||||
|
497 | return len(self.pairsList) | |||
|
498 | ||||
|
499 | def getPairsIndexList(self): | |||
|
500 | ||||
|
501 | return range(self.nPairs) | |||
|
502 | ||||
|
503 | def getNormFactor(self): | |||
|
504 | pwcode = 1 | |||
|
505 | if self.flagDecodeData: | |||
|
506 | pwcode = numpy.sum(self.code[0]**2) | |||
|
507 | #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter | |||
|
508 | normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter | |||
|
509 | ||||
|
510 | return normFactor | |||
|
511 | ||||
|
512 | def getFlagCspc(self): | |||
|
513 | ||||
|
514 | if self.data_cspc == None: | |||
|
515 | return True | |||
|
516 | ||||
|
517 | return False | |||
|
518 | ||||
|
519 | def getFlagDc(self): | |||
|
520 | ||||
|
521 | if self.data_dc == None: | |||
|
522 | return True | |||
|
523 | ||||
|
524 | return False | |||
|
525 | ||||
|
526 | nPairs = property(getNPairs, "I'm the 'nPairs' property.") | |||
|
527 | pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.") | |||
|
528 | normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.") | |||
|
529 | flag_cspc = property(getFlagCspc) | |||
|
530 | flag_dc = property(getFlagDc) | |||
|
531 | noise = property(getNoise, "I'm the 'nHeights' property.") | |||
|
532 | ||||
|
533 | class SpectraHeis(Spectra): | |||
|
534 | ||||
|
535 | data_spc = None | |||
|
536 | ||||
|
537 | data_cspc = None | |||
|
538 | ||||
|
539 | data_dc = None | |||
|
540 | ||||
|
541 | nFFTPoints = None | |||
|
542 | ||||
|
543 | # nPairs = None | |||
|
544 | ||||
|
545 | pairsList = None | |||
|
546 | ||||
|
547 | nIncohInt = None | |||
|
548 | ||||
|
549 | def __init__(self): | |||
|
550 | ||||
|
551 | self.radarControllerHeaderObj = RadarControllerHeader() | |||
|
552 | ||||
|
553 | self.systemHeaderObj = SystemHeader() | |||
|
554 | ||||
|
555 | self.type = "SpectraHeis" | |||
|
556 | ||||
|
557 | # self.dtype = None | |||
|
558 | ||||
|
559 | # self.nChannels = 0 | |||
|
560 | ||||
|
561 | # self.nHeights = 0 | |||
|
562 | ||||
|
563 | self.nProfiles = None | |||
|
564 | ||||
|
565 | self.heightList = None | |||
|
566 | ||||
|
567 | self.channelList = None | |||
|
568 | ||||
|
569 | # self.channelIndexList = None | |||
|
570 | ||||
|
571 | self.flagNoData = True | |||
|
572 | ||||
|
573 | self.flagTimeBlock = False | |||
|
574 | ||||
|
575 | # self.nPairs = 0 | |||
|
576 | ||||
|
577 | self.utctime = None | |||
|
578 | ||||
|
579 | self.blocksize = None | |||
|
580 | ||||
|
581 | class Fits: | |||
|
582 | ||||
|
583 | heightList = None | |||
|
584 | ||||
|
585 | channelList = None | |||
|
586 | ||||
|
587 | flagNoData = True | |||
|
588 | ||||
|
589 | flagTimeBlock = False | |||
|
590 | ||||
|
591 | useLocalTime = False | |||
|
592 | ||||
|
593 | utctime = None | |||
|
594 | ||||
|
595 | timeZone = None | |||
|
596 | ||||
|
597 | # ippSeconds = None | |||
|
598 | ||||
|
599 | timeInterval = None | |||
|
600 | ||||
|
601 | nCohInt = None | |||
|
602 | ||||
|
603 | nIncohInt = None | |||
|
604 | ||||
|
605 | noise = None | |||
|
606 | ||||
|
607 | windowOfFilter = 1 | |||
|
608 | ||||
|
609 | #Speed of ligth | |||
|
610 | C = 3e8 | |||
|
611 | ||||
|
612 | frequency = 49.92e6 | |||
|
613 | ||||
|
614 | realtime = False | |||
|
615 | ||||
|
616 | ||||
|
617 | def __init__(self): | |||
|
618 | ||||
|
619 | self.type = "Fits" | |||
|
620 | ||||
|
621 | self.nProfiles = None | |||
|
622 | ||||
|
623 | self.heightList = None | |||
|
624 | ||||
|
625 | self.channelList = None | |||
|
626 | ||||
|
627 | # self.channelIndexList = None | |||
|
628 | ||||
|
629 | self.flagNoData = True | |||
|
630 | ||||
|
631 | self.utctime = None | |||
|
632 | ||||
|
633 | self.nCohInt = None | |||
|
634 | ||||
|
635 | self.nIncohInt = None | |||
|
636 | ||||
|
637 | self.useLocalTime = True | |||
|
638 | ||||
|
639 | # self.utctime = None | |||
|
640 | # self.timeZone = None | |||
|
641 | # self.ltctime = None | |||
|
642 | # self.timeInterval = None | |||
|
643 | # self.header = None | |||
|
644 | # self.data_header = None | |||
|
645 | # self.data = None | |||
|
646 | # self.datatime = None | |||
|
647 | # self.flagNoData = False | |||
|
648 | # self.expName = '' | |||
|
649 | # self.nChannels = None | |||
|
650 | # self.nSamples = None | |||
|
651 | # self.dataBlocksPerFile = None | |||
|
652 | # self.comments = '' | |||
|
653 | # | |||
|
654 | ||||
|
655 | ||||
|
656 | def getltctime(self): | |||
|
657 | ||||
|
658 | if self.useLocalTime: | |||
|
659 | return self.utctime - self.timeZone*60 | |||
|
660 | ||||
|
661 | return self.utctime | |||
|
662 | ||||
|
663 | def getDatatime(self): | |||
|
664 | ||||
|
665 | datatime = datetime.datetime.utcfromtimestamp(self.ltctime) | |||
|
666 | return datatime | |||
|
667 | ||||
|
668 | def getTimeRange(self): | |||
|
669 | ||||
|
670 | datatime = [] | |||
|
671 | ||||
|
672 | datatime.append(self.ltctime) | |||
|
673 | datatime.append(self.ltctime + self.timeInterval) | |||
|
674 | ||||
|
675 | datatime = numpy.array(datatime) | |||
|
676 | ||||
|
677 | return datatime | |||
|
678 | ||||
|
679 | def getHeiRange(self): | |||
|
680 | ||||
|
681 | heis = self.heightList | |||
|
682 | ||||
|
683 | return heis | |||
|
684 | ||||
|
685 | def isEmpty(self): | |||
|
686 | ||||
|
687 | return self.flagNoData | |||
|
688 | ||||
|
689 | def getNHeights(self): | |||
|
690 | ||||
|
691 | return len(self.heightList) | |||
|
692 | ||||
|
693 | def getNChannels(self): | |||
|
694 | ||||
|
695 | return len(self.channelList) | |||
|
696 | ||||
|
697 | def getChannelIndexList(self): | |||
|
698 | ||||
|
699 | return range(self.nChannels) | |||
|
700 | ||||
|
701 | def getNoise(self, type = 1): | |||
|
702 | ||||
|
703 | self.noise = numpy.zeros(self.nChannels) | |||
|
704 | ||||
|
705 | if type == 1: | |||
|
706 | noise = self.getNoisebyHildebrand() | |||
|
707 | ||||
|
708 | if type == 2: | |||
|
709 | noise = self.getNoisebySort() | |||
|
710 | ||||
|
711 | if type == 3: | |||
|
712 | noise = self.getNoisebyWindow() | |||
|
713 | ||||
|
714 | return noise | |||
|
715 | ||||
|
716 | datatime = property(getDatatime, "I'm the 'datatime' property") | |||
|
717 | nHeights = property(getNHeights, "I'm the 'nHeights' property.") | |||
|
718 | nChannels = property(getNChannels, "I'm the 'nChannel' property.") | |||
|
719 | channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") | |||
|
720 | noise = property(getNoise, "I'm the 'nHeights' property.") | |||
|
721 | datatime = property(getDatatime, "I'm the 'datatime' property") | |||
|
722 | ltctime = property(getltctime, "I'm the 'ltctime' property") | |||
|
723 | ||||
|
724 | ltctime = property(getltctime, "I'm the 'ltctime' property") | |||
|
725 | ||||
|
726 | class AMISR: | |||
|
727 | def __init__(self): | |||
|
728 | self.flagNoData = True | |||
|
729 | self.data = None | |||
|
730 | self.utctime = None | |||
|
731 | self.type = "AMISR" | |||
|
732 | ||||
|
733 | #propiedades para compatibilidad con Voltages | |||
|
734 | self.timeZone = 300#timezone like jroheader, difference in minutes between UTC and localtime | |||
|
735 | self.dstFlag = 0#self.dataIn.dstFlag | |||
|
736 | self.errorCount = 0#self.dataIn.errorCount | |||
|
737 | self.useLocalTime = True#self.dataIn.useLocalTime | |||
|
738 | ||||
|
739 | self.radarControllerHeaderObj = None#self.dataIn.radarControllerHeaderObj.copy() | |||
|
740 | self.systemHeaderObj = None#self.dataIn.systemHeaderObj.copy() | |||
|
741 | self.channelList = [0]#self.dataIn.channelList esto solo aplica para el caso de AMISR | |||
|
742 | self.dtype = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
743 | ||||
|
744 | self.flagTimeBlock = None#self.dataIn.flagTimeBlock | |||
|
745 | #self.utctime = #self.firstdatatime | |||
|
746 | self.flagDecodeData = None#self.dataIn.flagDecodeData #asumo q la data esta decodificada | |||
|
747 | self.flagDeflipData = None#self.dataIn.flagDeflipData #asumo q la data esta sin flip | |||
|
748 | ||||
|
749 | self.nCohInt = 1#self.dataIn.nCohInt | |||
|
750 | self.nIncohInt = 1 | |||
|
751 | self.ippSeconds = None#self.dataIn.ippSeconds, segun el filename/Setup/Tufile | |||
|
752 | self.windowOfFilter = None#self.dataIn.windowOfFilter | |||
|
753 | ||||
|
754 | self.timeInterval = None#self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt | |||
|
755 | self.frequency = None#self.dataIn.frequency | |||
|
756 | self.realtime = 0#self.dataIn.realtime | |||
|
757 | ||||
|
758 | #actualizar en la lectura de datos | |||
|
759 | self.heightList = None#self.dataIn.heightList | |||
|
760 | self.nProfiles = None#Number of samples or nFFTPoints | |||
|
761 | self.nRecords = None | |||
|
762 | self.nBeams = None | |||
|
763 | self.nBaud = None#self.dataIn.nBaud | |||
|
764 | self.nCode = None#self.dataIn.nCode | |||
|
765 | self.code = None#self.dataIn.code | |||
|
766 | ||||
|
767 | #consideracion para los Beams | |||
|
768 | self.beamCodeDict = None | |||
|
769 | self.beamRangeDict = None | |||
|
770 | ||||
|
771 | def copy(self, inputObj=None): | |||
|
772 | ||||
|
773 | if inputObj == None: | |||
|
774 | return copy.deepcopy(self) | |||
|
775 | ||||
|
776 | for key in inputObj.__dict__.keys(): | |||
|
777 | self.__dict__[key] = inputObj.__dict__[key] | |||
|
778 | ||||
|
779 | ||||
|
780 | def isEmpty(self): | |||
|
781 | ||||
|
782 | return self.flagNoData No newline at end of file |
This diff has been collapsed as it changes many lines, (604 lines changed) Show them Hide them | |||||
@@ -0,0 +1,604 | |||||
|
1 | ''' | |||
|
2 | ||||
|
3 | $Author: murco $ | |||
|
4 | $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $ | |||
|
5 | ''' | |||
|
6 | import numpy | |||
|
7 | import copy | |||
|
8 | import datetime | |||
|
9 | ||||
|
10 | BASIC_STRUCTURE = numpy.dtype([ | |||
|
11 | ('nSize','<u4'), | |||
|
12 | ('nVersion','<u2'), | |||
|
13 | ('nDataBlockId','<u4'), | |||
|
14 | ('nUtime','<u4'), | |||
|
15 | ('nMilsec','<u2'), | |||
|
16 | ('nTimezone','<i2'), | |||
|
17 | ('nDstflag','<i2'), | |||
|
18 | ('nErrorCount','<u4') | |||
|
19 | ]) | |||
|
20 | ||||
|
21 | SYSTEM_STRUCTURE = numpy.dtype([ | |||
|
22 | ('nSize','<u4'), | |||
|
23 | ('nNumSamples','<u4'), | |||
|
24 | ('nNumProfiles','<u4'), | |||
|
25 | ('nNumChannels','<u4'), | |||
|
26 | ('nADCResolution','<u4'), | |||
|
27 | ('nPCDIOBusWidth','<u4'), | |||
|
28 | ]) | |||
|
29 | ||||
|
30 | RADAR_STRUCTURE = numpy.dtype([ | |||
|
31 | ('nSize','<u4'), | |||
|
32 | ('nExpType','<u4'), | |||
|
33 | ('nNTx','<u4'), | |||
|
34 | ('fIpp','<f4'), | |||
|
35 | ('fTxA','<f4'), | |||
|
36 | ('fTxB','<f4'), | |||
|
37 | ('nNumWindows','<u4'), | |||
|
38 | ('nNumTaus','<u4'), | |||
|
39 | ('nCodeType','<u4'), | |||
|
40 | ('nLine6Function','<u4'), | |||
|
41 | ('nLine5Function','<u4'), | |||
|
42 | ('fClock','<f4'), | |||
|
43 | ('nPrePulseBefore','<u4'), | |||
|
44 | ('nPrePulseAfter','<u4'), | |||
|
45 | ('sRangeIPP','<a20'), | |||
|
46 | ('sRangeTxA','<a20'), | |||
|
47 | ('sRangeTxB','<a20'), | |||
|
48 | ]) | |||
|
49 | ||||
|
50 | SAMPLING_STRUCTURE = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')]) | |||
|
51 | ||||
|
52 | ||||
|
53 | PROCESSING_STRUCTURE = numpy.dtype([ | |||
|
54 | ('nSize','<u4'), | |||
|
55 | ('nDataType','<u4'), | |||
|
56 | ('nSizeOfDataBlock','<u4'), | |||
|
57 | ('nProfilesperBlock','<u4'), | |||
|
58 | ('nDataBlocksperFile','<u4'), | |||
|
59 | ('nNumWindows','<u4'), | |||
|
60 | ('nProcessFlags','<u4'), | |||
|
61 | ('nCoherentIntegrations','<u4'), | |||
|
62 | ('nIncoherentIntegrations','<u4'), | |||
|
63 | ('nTotalSpectra','<u4') | |||
|
64 | ]) | |||
|
65 | ||||
|
66 | class Header(object): | |||
|
67 | ||||
|
68 | def __init__(self): | |||
|
69 | raise | |||
|
70 | ||||
|
71 | def copy(self): | |||
|
72 | return copy.deepcopy(self) | |||
|
73 | ||||
|
74 | def read(self): | |||
|
75 | ||||
|
76 | raise ValueError | |||
|
77 | ||||
|
78 | def write(self): | |||
|
79 | ||||
|
80 | raise ValueError | |||
|
81 | ||||
|
82 | def printInfo(self): | |||
|
83 | ||||
|
84 | print "#"*100 | |||
|
85 | print self.__class__.__name__.upper() | |||
|
86 | print "#"*100 | |||
|
87 | for key in self.__dict__.keys(): | |||
|
88 | print "%s = %s" %(key, self.__dict__[key]) | |||
|
89 | ||||
|
90 | class BasicHeader(Header): | |||
|
91 | ||||
|
92 | size = None | |||
|
93 | version = None | |||
|
94 | dataBlock = None | |||
|
95 | utc = None | |||
|
96 | ltc = None | |||
|
97 | miliSecond = None | |||
|
98 | timeZone = None | |||
|
99 | dstFlag = None | |||
|
100 | errorCount = None | |||
|
101 | datatime = None | |||
|
102 | ||||
|
103 | __LOCALTIME = None | |||
|
104 | ||||
|
105 | def __init__(self, useLocalTime=True): | |||
|
106 | ||||
|
107 | self.size = 24 | |||
|
108 | self.version = 0 | |||
|
109 | self.dataBlock = 0 | |||
|
110 | self.utc = 0 | |||
|
111 | self.miliSecond = 0 | |||
|
112 | self.timeZone = 0 | |||
|
113 | self.dstFlag = 0 | |||
|
114 | self.errorCount = 0 | |||
|
115 | ||||
|
116 | self.useLocalTime = useLocalTime | |||
|
117 | ||||
|
118 | def read(self, fp): | |||
|
119 | try: | |||
|
120 | ||||
|
121 | header = numpy.fromfile(fp, BASIC_STRUCTURE,1) | |||
|
122 | ||||
|
123 | self.size = int(header['nSize'][0]) | |||
|
124 | self.version = int(header['nVersion'][0]) | |||
|
125 | self.dataBlock = int(header['nDataBlockId'][0]) | |||
|
126 | self.utc = int(header['nUtime'][0]) | |||
|
127 | self.miliSecond = int(header['nMilsec'][0]) | |||
|
128 | self.timeZone = int(header['nTimezone'][0]) | |||
|
129 | self.dstFlag = int(header['nDstflag'][0]) | |||
|
130 | self.errorCount = int(header['nErrorCount'][0]) | |||
|
131 | ||||
|
132 | except Exception, e: | |||
|
133 | print "BasicHeader: " | |||
|
134 | print e | |||
|
135 | return 0 | |||
|
136 | ||||
|
137 | return 1 | |||
|
138 | ||||
|
139 | def write(self, fp): | |||
|
140 | ||||
|
141 | headerTuple = (self.size,self.version,self.dataBlock,self.utc,self.miliSecond,self.timeZone,self.dstFlag,self.errorCount) | |||
|
142 | header = numpy.array(headerTuple, BASIC_STRUCTURE) | |||
|
143 | header.tofile(fp) | |||
|
144 | ||||
|
145 | return 1 | |||
|
146 | ||||
|
147 | def get_ltc(self): | |||
|
148 | ||||
|
149 | return self.utc - self.timeZone*60 | |||
|
150 | ||||
|
151 | def set_ltc(self, value): | |||
|
152 | ||||
|
153 | self.utc = value + self.timeZone*60 | |||
|
154 | ||||
|
155 | def get_datatime(self): | |||
|
156 | ||||
|
157 | return datetime.datetime.utcfromtimestamp(self.ltc) | |||
|
158 | ||||
|
159 | ltc = property(get_ltc, set_ltc) | |||
|
160 | datatime = property(get_datatime) | |||
|
161 | ||||
|
162 | class SystemHeader(Header): | |||
|
163 | ||||
|
164 | size = None | |||
|
165 | nSamples = None | |||
|
166 | nProfiles = None | |||
|
167 | nChannels = None | |||
|
168 | adcResolution = None | |||
|
169 | pciDioBusWidth = None | |||
|
170 | ||||
|
171 | def __init__(self): | |||
|
172 | self.size = 24 | |||
|
173 | self.nSamples = 0 | |||
|
174 | self.nProfiles = 0 | |||
|
175 | self.nChannels = 0 | |||
|
176 | self.adcResolution = 0 | |||
|
177 | self.pciDioBusWidth = 0 | |||
|
178 | ||||
|
179 | def read(self, fp): | |||
|
180 | try: | |||
|
181 | header = numpy.fromfile(fp,SYSTEM_STRUCTURE,1) | |||
|
182 | self.size = header['nSize'][0] | |||
|
183 | self.nSamples = header['nNumSamples'][0] | |||
|
184 | self.nProfiles = header['nNumProfiles'][0] | |||
|
185 | self.nChannels = header['nNumChannels'][0] | |||
|
186 | self.adcResolution = header['nADCResolution'][0] | |||
|
187 | self.pciDioBusWidth = header['nPCDIOBusWidth'][0] | |||
|
188 | ||||
|
189 | except Exception, e: | |||
|
190 | print "SystemHeader: " + e | |||
|
191 | return 0 | |||
|
192 | ||||
|
193 | return 1 | |||
|
194 | ||||
|
195 | def write(self, fp): | |||
|
196 | ||||
|
197 | headerTuple = (self.size,self.nSamples,self.nProfiles,self.nChannels,self.adcResolution,self.pciDioBusWidth) | |||
|
198 | header = numpy.array(headerTuple,SYSTEM_STRUCTURE) | |||
|
199 | header.tofile(fp) | |||
|
200 | ||||
|
201 | return 1 | |||
|
202 | ||||
|
203 | class RadarControllerHeader(Header): | |||
|
204 | ||||
|
205 | size = None | |||
|
206 | expType = None | |||
|
207 | nTx = None | |||
|
208 | ipp = None | |||
|
209 | txA = None | |||
|
210 | txB = None | |||
|
211 | nWindows = None | |||
|
212 | numTaus = None | |||
|
213 | codeType = None | |||
|
214 | line6Function = None | |||
|
215 | line5Function = None | |||
|
216 | fClock = None | |||
|
217 | prePulseBefore = None | |||
|
218 | prePulserAfter = None | |||
|
219 | rangeIpp = None | |||
|
220 | rangeTxA = None | |||
|
221 | rangeTxB = None | |||
|
222 | ||||
|
223 | __C = 3e8 | |||
|
224 | ||||
|
225 | def __init__(self): | |||
|
226 | self.size = 116 | |||
|
227 | self.expType = 0 | |||
|
228 | self.nTx = 0 | |||
|
229 | self.ipp = 0 | |||
|
230 | self.txA = 0 | |||
|
231 | self.txB = 0 | |||
|
232 | self.nWindows = 0 | |||
|
233 | self.numTaus = 0 | |||
|
234 | self.codeType = 0 | |||
|
235 | self.line6Function = 0 | |||
|
236 | self.line5Function = 0 | |||
|
237 | self.fClock = 0 | |||
|
238 | self.prePulseBefore = 0 | |||
|
239 | self.prePulserAfter = 0 | |||
|
240 | self.rangeIpp = 0 | |||
|
241 | self.rangeTxA = 0 | |||
|
242 | self.rangeTxB = 0 | |||
|
243 | ||||
|
244 | self.samplingWindow = None | |||
|
245 | self.nHeights = None | |||
|
246 | self.firstHeight = None | |||
|
247 | self.deltaHeight = None | |||
|
248 | self.samplesWin = None | |||
|
249 | ||||
|
250 | self.nCode = None | |||
|
251 | self.nBaud = None | |||
|
252 | self.code = None | |||
|
253 | self.flip1 = None | |||
|
254 | self.flip2 = None | |||
|
255 | ||||
|
256 | # self.dynamic = numpy.array([],numpy.dtype('byte')) | |||
|
257 | ||||
|
258 | ||||
|
259 | def read(self, fp): | |||
|
260 | try: | |||
|
261 | startFp = fp.tell() | |||
|
262 | header = numpy.fromfile(fp,RADAR_STRUCTURE,1) | |||
|
263 | ||||
|
264 | self.size = int(header['nSize'][0]) | |||
|
265 | self.expType = int(header['nExpType'][0]) | |||
|
266 | self.nTx = int(header['nNTx'][0]) | |||
|
267 | self.ipp = float(header['fIpp'][0]) | |||
|
268 | self.txA = float(header['fTxA'][0]) | |||
|
269 | self.txB = float(header['fTxB'][0]) | |||
|
270 | self.nWindows = int(header['nNumWindows'][0]) | |||
|
271 | self.numTaus = int(header['nNumTaus'][0]) | |||
|
272 | self.codeType = int(header['nCodeType'][0]) | |||
|
273 | self.line6Function = int(header['nLine6Function'][0]) | |||
|
274 | self.line5Function = int(header['nLine5Function'][0]) | |||
|
275 | self.fClock = float(header['fClock'][0]) | |||
|
276 | self.prePulseBefore = int(header['nPrePulseBefore'][0]) | |||
|
277 | self.prePulserAfter = int(header['nPrePulseAfter'][0]) | |||
|
278 | self.rangeIpp = header['sRangeIPP'][0] | |||
|
279 | self.rangeTxA = header['sRangeTxA'][0] | |||
|
280 | self.rangeTxB = header['sRangeTxB'][0] | |||
|
281 | # jump Dynamic Radar Controller Header | |||
|
282 | # jumpFp = self.size - 116 | |||
|
283 | # self.dynamic = numpy.fromfile(fp,numpy.dtype('byte'),jumpFp) | |||
|
284 | #pointer backward to dynamic header and read | |||
|
285 | # backFp = fp.tell() - jumpFp | |||
|
286 | # fp.seek(backFp) | |||
|
287 | ||||
|
288 | self.samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows) | |||
|
289 | ||||
|
290 | self.nHeights = int(numpy.sum(self.samplingWindow['nsa'])) | |||
|
291 | self.firstHeight = self.samplingWindow['h0'] | |||
|
292 | self.deltaHeight = self.samplingWindow['dh'] | |||
|
293 | self.samplesWin = self.samplingWindow['nsa'] | |||
|
294 | ||||
|
295 | self.Taus = numpy.fromfile(fp,'<f4',self.numTaus) | |||
|
296 | ||||
|
297 | if self.codeType != 0: | |||
|
298 | self.nCode = int(numpy.fromfile(fp,'<u4',1)) | |||
|
299 | self.nBaud = int(numpy.fromfile(fp,'<u4',1)) | |||
|
300 | self.code = numpy.empty([self.nCode,self.nBaud],dtype='u1') | |||
|
301 | ||||
|
302 | for ic in range(self.nCode): | |||
|
303 | temp = numpy.fromfile(fp,'u4',int(numpy.ceil(self.nBaud/32.))) | |||
|
304 | for ib in range(self.nBaud-1,-1,-1): | |||
|
305 | self.code[ic,ib] = temp[ib/32]%2 | |||
|
306 | temp[ib/32] = temp[ib/32]/2 | |||
|
307 | self.code = 2.0*self.code - 1.0 | |||
|
308 | ||||
|
309 | if self.line5Function == RCfunction.FLIP: | |||
|
310 | self.flip1 = numpy.fromfile(fp,'<u4',1) | |||
|
311 | ||||
|
312 | if self.line6Function == RCfunction.FLIP: | |||
|
313 | self.flip2 = numpy.fromfile(fp,'<u4',1) | |||
|
314 | ||||
|
315 | endFp = self.size + startFp | |||
|
316 | # jumpFp = endFp - fp.tell() | |||
|
317 | # if jumpFp > 0: | |||
|
318 | # | |||
|
319 | fp.seek(endFp) | |||
|
320 | ||||
|
321 | except Exception, e: | |||
|
322 | print "RadarControllerHeader: " + e | |||
|
323 | return 0 | |||
|
324 | ||||
|
325 | return 1 | |||
|
326 | ||||
|
327 | def write(self, fp): | |||
|
328 | headerTuple = (self.size, | |||
|
329 | self.expType, | |||
|
330 | self.nTx, | |||
|
331 | self.ipp, | |||
|
332 | self.txA, | |||
|
333 | self.txB, | |||
|
334 | self.nWindows, | |||
|
335 | self.numTaus, | |||
|
336 | self.codeType, | |||
|
337 | self.line6Function, | |||
|
338 | self.line5Function, | |||
|
339 | self.fClock, | |||
|
340 | self.prePulseBefore, | |||
|
341 | self.prePulserAfter, | |||
|
342 | self.rangeIpp, | |||
|
343 | self.rangeTxA, | |||
|
344 | self.rangeTxB) | |||
|
345 | ||||
|
346 | header = numpy.array(headerTuple,RADAR_STRUCTURE) | |||
|
347 | header.tofile(fp) | |||
|
348 | ||||
|
349 | #dynamic = self.dynamic | |||
|
350 | #dynamic.tofile(fp) | |||
|
351 | ||||
|
352 | samplingWindow = self.samplingWindow | |||
|
353 | samplingWindow.tofile(fp) | |||
|
354 | ||||
|
355 | if self.numTaus > 0: | |||
|
356 | self.Taus.tofile(fp) | |||
|
357 | ||||
|
358 | nCode = numpy.array(self.nCode, '<u4') | |||
|
359 | nCode.tofile(fp) | |||
|
360 | nBaud = numpy.array(self.nBaud, '<u4') | |||
|
361 | nBaud.tofile(fp) | |||
|
362 | code1 = (self.code + 1.0)/2. | |||
|
363 | ||||
|
364 | for ic in range(self.nCode): | |||
|
365 | tempx = numpy.zeros(numpy.ceil(self.nBaud/32.)) | |||
|
366 | start = 0 | |||
|
367 | end = 32 | |||
|
368 | for i in range(len(tempx)): | |||
|
369 | code_selected = code1[ic,start:end] | |||
|
370 | for j in range(len(code_selected)-1,-1,-1): | |||
|
371 | if code_selected[j] == 1: | |||
|
372 | tempx[i] = tempx[i] + 2**(len(code_selected)-1-j) | |||
|
373 | start = start + 32 | |||
|
374 | end = end + 32 | |||
|
375 | ||||
|
376 | tempx = tempx.astype('u4') | |||
|
377 | tempx.tofile(fp) | |||
|
378 | ||||
|
379 | if self.line5Function == RCfunction.FLIP: | |||
|
380 | self.flip1.tofile(fp) | |||
|
381 | ||||
|
382 | if self.line6Function == RCfunction.FLIP: | |||
|
383 | self.flip2.tofile(fp) | |||
|
384 | ||||
|
385 | return 1 | |||
|
386 | ||||
|
387 | def get_ippSeconds(self): | |||
|
388 | ''' | |||
|
389 | ''' | |||
|
390 | ippSeconds = 2.0 * 1000 * self.ipp / self.__C | |||
|
391 | ||||
|
392 | return ippSeconds | |||
|
393 | ||||
|
394 | def set_ippSeconds(self, ippSeconds): | |||
|
395 | ''' | |||
|
396 | ''' | |||
|
397 | ||||
|
398 | self.ipp = ippSeconds * self.__C / (2.0*1000) | |||
|
399 | ||||
|
400 | return | |||
|
401 | ||||
|
402 | ippSeconds = property(get_ippSeconds, set_ippSeconds) | |||
|
403 | ||||
|
404 | class ProcessingHeader(Header): | |||
|
405 | ||||
|
406 | size = None | |||
|
407 | dtype = None | |||
|
408 | blockSize = None | |||
|
409 | profilesPerBlock = None | |||
|
410 | dataBlocksPerFile = None | |||
|
411 | nWindows = None | |||
|
412 | processFlags = None | |||
|
413 | nCohInt = None | |||
|
414 | nIncohInt = None | |||
|
415 | totalSpectra = None | |||
|
416 | ||||
|
417 | flag_dc = None | |||
|
418 | flag_cspc = None | |||
|
419 | ||||
|
420 | def __init__(self): | |||
|
421 | self.size = 0 | |||
|
422 | self.dtype = 0 | |||
|
423 | self.blockSize = 0 | |||
|
424 | self.profilesPerBlock = 0 | |||
|
425 | self.dataBlocksPerFile = 0 | |||
|
426 | self.nWindows = 0 | |||
|
427 | self.processFlags = 0 | |||
|
428 | self.nCohInt = 0 | |||
|
429 | self.nIncohInt = 0 | |||
|
430 | self.totalSpectra = 0 | |||
|
431 | ||||
|
432 | self.samplingWindow = 0 | |||
|
433 | ||||
|
434 | self.nHeights = 0 | |||
|
435 | self.firstHeight = 0 | |||
|
436 | self.deltaHeight = 0 | |||
|
437 | self.samplesWin = 0 | |||
|
438 | self.spectraComb = 0 | |||
|
439 | # self.nCode = None | |||
|
440 | # self.code = None | |||
|
441 | # self.nBaud = None | |||
|
442 | self.shif_fft = False | |||
|
443 | self.flag_dc = False | |||
|
444 | self.flag_cspc = False | |||
|
445 | ||||
|
446 | def read(self, fp): | |||
|
447 | # try: | |||
|
448 | header = numpy.fromfile(fp,PROCESSING_STRUCTURE,1) | |||
|
449 | self.size = int(header['nSize'][0]) | |||
|
450 | self.dtype = int(header['nDataType'][0]) | |||
|
451 | self.blockSize = int(header['nSizeOfDataBlock'][0]) | |||
|
452 | self.profilesPerBlock = int(header['nProfilesperBlock'][0]) | |||
|
453 | self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0]) | |||
|
454 | self.nWindows = int(header['nNumWindows'][0]) | |||
|
455 | self.processFlags = header['nProcessFlags'] | |||
|
456 | self.nCohInt = int(header['nCoherentIntegrations'][0]) | |||
|
457 | self.nIncohInt = int(header['nIncoherentIntegrations'][0]) | |||
|
458 | self.totalSpectra = int(header['nTotalSpectra'][0]) | |||
|
459 | ||||
|
460 | self.samplingWindow = numpy.fromfile(fp,SAMPLING_STRUCTURE,self.nWindows) | |||
|
461 | ||||
|
462 | self.nHeights = int(numpy.sum(self.samplingWindow['nsa'])) | |||
|
463 | self.firstHeight = float(self.samplingWindow['h0'][0]) | |||
|
464 | self.deltaHeight = float(self.samplingWindow['dh'][0]) | |||
|
465 | self.samplesWin = self.samplingWindow['nsa'][0] | |||
|
466 | self.spectraComb = numpy.fromfile(fp,'u1',2*self.totalSpectra) | |||
|
467 | ||||
|
468 | # if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE): | |||
|
469 | # self.nCode = int(numpy.fromfile(fp,'<u4',1)) | |||
|
470 | # self.nBaud = int(numpy.fromfile(fp,'<u4',1)) | |||
|
471 | # self.code = numpy.fromfile(fp,'<f4',self.nCode*self.nBaud).reshape(self.nCode,self.nBaud) | |||
|
472 | ||||
|
473 | if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA): | |||
|
474 | self.shif_fft = True | |||
|
475 | else: | |||
|
476 | self.shif_fft = False | |||
|
477 | ||||
|
478 | if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC): | |||
|
479 | self.flag_dc = True | |||
|
480 | ||||
|
481 | nChannels = 0 | |||
|
482 | nPairs = 0 | |||
|
483 | pairList = [] | |||
|
484 | ||||
|
485 | for i in range( 0, self.totalSpectra*2, 2 ): | |||
|
486 | if self.spectraComb[i] == self.spectraComb[i+1]: | |||
|
487 | nChannels = nChannels + 1 #par de canales iguales | |||
|
488 | else: | |||
|
489 | nPairs = nPairs + 1 #par de canales diferentes | |||
|
490 | pairList.append( (self.spectraComb[i], self.spectraComb[i+1]) ) | |||
|
491 | ||||
|
492 | self.flag_cspc = False | |||
|
493 | if nPairs > 0: | |||
|
494 | self.flag_cspc = True | |||
|
495 | ||||
|
496 | # except Exception, e: | |||
|
497 | # print "Error ProcessingHeader: " | |||
|
498 | # return 0 | |||
|
499 | ||||
|
500 | return 1 | |||
|
501 | ||||
|
502 | def write(self, fp): | |||
|
503 | headerTuple = (self.size, | |||
|
504 | self.dtype, | |||
|
505 | self.blockSize, | |||
|
506 | self.profilesPerBlock, | |||
|
507 | self.dataBlocksPerFile, | |||
|
508 | self.nWindows, | |||
|
509 | self.processFlags, | |||
|
510 | self.nCohInt, | |||
|
511 | self.nIncohInt, | |||
|
512 | self.totalSpectra) | |||
|
513 | ||||
|
514 | header = numpy.array(headerTuple,PROCESSING_STRUCTURE) | |||
|
515 | header.tofile(fp) | |||
|
516 | ||||
|
517 | if self.nWindows != 0: | |||
|
518 | sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin) | |||
|
519 | samplingWindow = numpy.array(sampleWindowTuple,SAMPLING_STRUCTURE) | |||
|
520 | samplingWindow.tofile(fp) | |||
|
521 | ||||
|
522 | ||||
|
523 | if self.totalSpectra != 0: | |||
|
524 | spectraComb = numpy.array([],numpy.dtype('u1')) | |||
|
525 | spectraComb = self.spectraComb | |||
|
526 | spectraComb.tofile(fp) | |||
|
527 | ||||
|
528 | # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE: | |||
|
529 | # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba | |||
|
530 | # nCode.tofile(fp) | |||
|
531 | # | |||
|
532 | # nBaud = numpy.array([self.nBaud], numpy.dtype('u4')) | |||
|
533 | # nBaud.tofile(fp) | |||
|
534 | # | |||
|
535 | # code = self.code.reshape(self.nCode*self.nBaud) | |||
|
536 | # code = code.astype(numpy.dtype('<f4')) | |||
|
537 | # code.tofile(fp) | |||
|
538 | ||||
|
539 | return 1 | |||
|
540 | ||||
|
541 | class RCfunction: | |||
|
542 | NONE=0 | |||
|
543 | FLIP=1 | |||
|
544 | CODE=2 | |||
|
545 | SAMPLING=3 | |||
|
546 | LIN6DIV256=4 | |||
|
547 | SYNCHRO=5 | |||
|
548 | ||||
|
549 | class nCodeType: | |||
|
550 | NONE=0 | |||
|
551 | USERDEFINE=1 | |||
|
552 | BARKER2=2 | |||
|
553 | BARKER3=3 | |||
|
554 | BARKER4=4 | |||
|
555 | BARKER5=5 | |||
|
556 | BARKER7=6 | |||
|
557 | BARKER11=7 | |||
|
558 | BARKER13=8 | |||
|
559 | AC128=9 | |||
|
560 | COMPLEMENTARYCODE2=10 | |||
|
561 | COMPLEMENTARYCODE4=11 | |||
|
562 | COMPLEMENTARYCODE8=12 | |||
|
563 | COMPLEMENTARYCODE16=13 | |||
|
564 | COMPLEMENTARYCODE32=14 | |||
|
565 | COMPLEMENTARYCODE64=15 | |||
|
566 | COMPLEMENTARYCODE128=16 | |||
|
567 | CODE_BINARY28=17 | |||
|
568 | ||||
|
569 | class PROCFLAG: | |||
|
570 | COHERENT_INTEGRATION = numpy.uint32(0x00000001) | |||
|
571 | DECODE_DATA = numpy.uint32(0x00000002) | |||
|
572 | SPECTRA_CALC = numpy.uint32(0x00000004) | |||
|
573 | INCOHERENT_INTEGRATION = numpy.uint32(0x00000008) | |||
|
574 | POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010) | |||
|
575 | SHIFT_FFT_DATA = numpy.uint32(0x00000020) | |||
|
576 | ||||
|
577 | DATATYPE_CHAR = numpy.uint32(0x00000040) | |||
|
578 | DATATYPE_SHORT = numpy.uint32(0x00000080) | |||
|
579 | DATATYPE_LONG = numpy.uint32(0x00000100) | |||
|
580 | DATATYPE_INT64 = numpy.uint32(0x00000200) | |||
|
581 | DATATYPE_FLOAT = numpy.uint32(0x00000400) | |||
|
582 | DATATYPE_DOUBLE = numpy.uint32(0x00000800) | |||
|
583 | ||||
|
584 | DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000) | |||
|
585 | DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000) | |||
|
586 | DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000) | |||
|
587 | ||||
|
588 | SAVE_CHANNELS_DC = numpy.uint32(0x00008000) | |||
|
589 | DEFLIP_DATA = numpy.uint32(0x00010000) | |||
|
590 | DEFINE_PROCESS_CODE = numpy.uint32(0x00020000) | |||
|
591 | ||||
|
592 | ACQ_SYS_NATALIA = numpy.uint32(0x00040000) | |||
|
593 | ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000) | |||
|
594 | ACQ_SYS_ADRXD = numpy.uint32(0x000C0000) | |||
|
595 | ACQ_SYS_JULIA = numpy.uint32(0x00100000) | |||
|
596 | ACQ_SYS_XXXXXX = numpy.uint32(0x00140000) | |||
|
597 | ||||
|
598 | EXP_NAME_ESP = numpy.uint32(0x00200000) | |||
|
599 | CHANNEL_NAMES_ESP = numpy.uint32(0x00400000) | |||
|
600 | ||||
|
601 | OPERATION_MASK = numpy.uint32(0x0000003F) | |||
|
602 | DATATYPE_MASK = numpy.uint32(0x00000FC0) | |||
|
603 | DATAARRANGE_MASK = numpy.uint32(0x00007000) | |||
|
604 | ACQ_SYS_MASK = numpy.uint32(0x001C0000) No newline at end of file |
@@ -0,0 +1,3 | |||||
|
1 | from jroplot_voltage import * | |||
|
2 | from jroplot_spectra import * | |||
|
3 | from jroplot_heispectra import * |
@@ -0,0 +1,318 | |||||
|
1 | ''' | |||
|
2 | ||||
|
3 | @author: Daniel Suarez | |||
|
4 | ''' | |||
|
5 | ||||
|
6 | import os | |||
|
7 | import datetime | |||
|
8 | import numpy | |||
|
9 | ||||
|
10 | from figure import Figure, isRealtime | |||
|
11 | ||||
|
12 | class SpectraHeisScope(Figure): | |||
|
13 | ||||
|
14 | ||||
|
15 | isConfig = None | |||
|
16 | __nsubplots = None | |||
|
17 | ||||
|
18 | WIDTHPROF = None | |||
|
19 | HEIGHTPROF = None | |||
|
20 | PREFIX = 'spc' | |||
|
21 | ||||
|
22 | def __init__(self): | |||
|
23 | ||||
|
24 | self.isConfig = False | |||
|
25 | self.__nsubplots = 1 | |||
|
26 | ||||
|
27 | self.WIDTH = 230 | |||
|
28 | self.HEIGHT = 250 | |||
|
29 | self.WIDTHPROF = 120 | |||
|
30 | self.HEIGHTPROF = 0 | |||
|
31 | self.counter_imagwr = 0 | |||
|
32 | ||||
|
33 | def getSubplots(self): | |||
|
34 | ||||
|
35 | ncol = int(numpy.sqrt(self.nplots)+0.9) | |||
|
36 | nrow = int(self.nplots*1./ncol + 0.9) | |||
|
37 | ||||
|
38 | return nrow, ncol | |||
|
39 | ||||
|
40 | def setup(self, id, nplots, wintitle, show): | |||
|
41 | ||||
|
42 | showprofile = False | |||
|
43 | self.__showprofile = showprofile | |||
|
44 | self.nplots = nplots | |||
|
45 | ||||
|
46 | ncolspan = 1 | |||
|
47 | colspan = 1 | |||
|
48 | if showprofile: | |||
|
49 | ncolspan = 3 | |||
|
50 | colspan = 2 | |||
|
51 | self.__nsubplots = 2 | |||
|
52 | ||||
|
53 | self.createFigure(id = id, | |||
|
54 | wintitle = wintitle, | |||
|
55 | widthplot = self.WIDTH + self.WIDTHPROF, | |||
|
56 | heightplot = self.HEIGHT + self.HEIGHTPROF, | |||
|
57 | show = show) | |||
|
58 | ||||
|
59 | nrow, ncol = self.getSubplots() | |||
|
60 | ||||
|
61 | counter = 0 | |||
|
62 | for y in range(nrow): | |||
|
63 | for x in range(ncol): | |||
|
64 | ||||
|
65 | if counter >= self.nplots: | |||
|
66 | break | |||
|
67 | ||||
|
68 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) | |||
|
69 | ||||
|
70 | if showprofile: | |||
|
71 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) | |||
|
72 | ||||
|
73 | counter += 1 | |||
|
74 | ||||
|
75 | ||||
|
76 | def run(self, dataOut, id, wintitle="", channelList=None, | |||
|
77 | xmin=None, xmax=None, ymin=None, ymax=None, save=False, | |||
|
78 | figpath='./', figfile=None, ftp=False, wr_period=1, show=True, | |||
|
79 | server=None, folder=None, username=None, password=None): | |||
|
80 | ||||
|
81 | """ | |||
|
82 | ||||
|
83 | Input: | |||
|
84 | dataOut : | |||
|
85 | id : | |||
|
86 | wintitle : | |||
|
87 | channelList : | |||
|
88 | xmin : None, | |||
|
89 | xmax : None, | |||
|
90 | ymin : None, | |||
|
91 | ymax : None, | |||
|
92 | """ | |||
|
93 | ||||
|
94 | if dataOut.realtime: | |||
|
95 | if not(isRealtime(utcdatatime = dataOut.utctime)): | |||
|
96 | print 'Skipping this plot function' | |||
|
97 | return | |||
|
98 | ||||
|
99 | if channelList == None: | |||
|
100 | channelIndexList = dataOut.channelIndexList | |||
|
101 | else: | |||
|
102 | channelIndexList = [] | |||
|
103 | for channel in channelList: | |||
|
104 | if channel not in dataOut.channelList: | |||
|
105 | raise ValueError, "Channel %d is not in dataOut.channelList" | |||
|
106 | channelIndexList.append(dataOut.channelList.index(channel)) | |||
|
107 | ||||
|
108 | # x = dataOut.heightList | |||
|
109 | c = 3E8 | |||
|
110 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | |||
|
111 | #deberia cambiar para el caso de 1Mhz y 100KHz | |||
|
112 | x = numpy.arange(-1*dataOut.nHeights/2.,dataOut.nHeights/2.)*(c/(2*deltaHeight*dataOut.nHeights*1000)) | |||
|
113 | #para 1Mhz descomentar la siguiente linea | |||
|
114 | #x= x/(10000.0) | |||
|
115 | # y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) | |||
|
116 | # y = y.real | |||
|
117 | datadB = 10.*numpy.log10(dataOut.data_spc) | |||
|
118 | y = datadB | |||
|
119 | ||||
|
120 | #thisDatetime = dataOut.datatime | |||
|
121 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
122 | title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
123 | xlabel = "" | |||
|
124 | #para 1Mhz descomentar la siguiente linea | |||
|
125 | #xlabel = "Frequency x 10000" | |||
|
126 | ylabel = "Intensity (dB)" | |||
|
127 | ||||
|
128 | if not self.isConfig: | |||
|
129 | nplots = len(channelIndexList) | |||
|
130 | ||||
|
131 | self.setup(id=id, | |||
|
132 | nplots=nplots, | |||
|
133 | wintitle=wintitle, | |||
|
134 | show=show) | |||
|
135 | ||||
|
136 | if xmin == None: xmin = numpy.nanmin(x) | |||
|
137 | if xmax == None: xmax = numpy.nanmax(x) | |||
|
138 | if ymin == None: ymin = numpy.nanmin(y) | |||
|
139 | if ymax == None: ymax = numpy.nanmax(y) | |||
|
140 | ||||
|
141 | self.isConfig = True | |||
|
142 | ||||
|
143 | self.setWinTitle(title) | |||
|
144 | ||||
|
145 | for i in range(len(self.axesList)): | |||
|
146 | ychannel = y[i,:] | |||
|
147 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) | |||
|
148 | title = "Channel %d: %4.2fdB: %s" %(i, numpy.max(ychannel), str_datetime) | |||
|
149 | axes = self.axesList[i] | |||
|
150 | axes.pline(x, ychannel, | |||
|
151 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, | |||
|
152 | xlabel=xlabel, ylabel=ylabel, title=title, grid='both') | |||
|
153 | ||||
|
154 | ||||
|
155 | self.draw() | |||
|
156 | ||||
|
157 | if save: | |||
|
158 | date = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
159 | if figfile == None: | |||
|
160 | figfile = self.getFilename(name = date) | |||
|
161 | ||||
|
162 | self.saveFigure(figpath, figfile) | |||
|
163 | ||||
|
164 | self.counter_imagwr += 1 | |||
|
165 | if (ftp and (self.counter_imagwr==wr_period)): | |||
|
166 | ftp_filename = os.path.join(figpath,figfile) | |||
|
167 | self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
168 | self.counter_imagwr = 0 | |||
|
169 | ||||
|
170 | class RTIfromSpectraHeis(Figure): | |||
|
171 | ||||
|
172 | isConfig = None | |||
|
173 | __nsubplots = None | |||
|
174 | ||||
|
175 | PREFIX = 'rtinoise' | |||
|
176 | ||||
|
177 | def __init__(self): | |||
|
178 | ||||
|
179 | self.timerange = 24*60*60 | |||
|
180 | self.isConfig = False | |||
|
181 | self.__nsubplots = 1 | |||
|
182 | ||||
|
183 | self.WIDTH = 820 | |||
|
184 | self.HEIGHT = 200 | |||
|
185 | self.WIDTHPROF = 120 | |||
|
186 | self.HEIGHTPROF = 0 | |||
|
187 | self.counter_imagwr = 0 | |||
|
188 | self.xdata = None | |||
|
189 | self.ydata = None | |||
|
190 | ||||
|
191 | def getSubplots(self): | |||
|
192 | ||||
|
193 | ncol = 1 | |||
|
194 | nrow = 1 | |||
|
195 | ||||
|
196 | return nrow, ncol | |||
|
197 | ||||
|
198 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): | |||
|
199 | ||||
|
200 | self.__showprofile = showprofile | |||
|
201 | self.nplots = nplots | |||
|
202 | ||||
|
203 | ncolspan = 7 | |||
|
204 | colspan = 6 | |||
|
205 | self.__nsubplots = 2 | |||
|
206 | ||||
|
207 | self.createFigure(id = id, | |||
|
208 | wintitle = wintitle, | |||
|
209 | widthplot = self.WIDTH+self.WIDTHPROF, | |||
|
210 | heightplot = self.HEIGHT+self.HEIGHTPROF, | |||
|
211 | show = show) | |||
|
212 | ||||
|
213 | nrow, ncol = self.getSubplots() | |||
|
214 | ||||
|
215 | self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) | |||
|
216 | ||||
|
217 | ||||
|
218 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', | |||
|
219 | xmin=None, xmax=None, ymin=None, ymax=None, | |||
|
220 | timerange=None, | |||
|
221 | save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True, | |||
|
222 | server=None, folder=None, username=None, password=None): | |||
|
223 | ||||
|
224 | if channelList == None: | |||
|
225 | channelIndexList = dataOut.channelIndexList | |||
|
226 | channelList = dataOut.channelList | |||
|
227 | else: | |||
|
228 | channelIndexList = [] | |||
|
229 | for channel in channelList: | |||
|
230 | if channel not in dataOut.channelList: | |||
|
231 | raise ValueError, "Channel %d is not in dataOut.channelList" | |||
|
232 | channelIndexList.append(dataOut.channelList.index(channel)) | |||
|
233 | ||||
|
234 | if timerange != None: | |||
|
235 | self.timerange = timerange | |||
|
236 | ||||
|
237 | tmin = None | |||
|
238 | tmax = None | |||
|
239 | x = dataOut.getTimeRange() | |||
|
240 | y = dataOut.getHeiRange() | |||
|
241 | ||||
|
242 | ||||
|
243 | data = dataOut.data_spc | |||
|
244 | data = numpy.average(data,axis=1) | |||
|
245 | datadB = 10*numpy.log10(data) | |||
|
246 | ||||
|
247 | # factor = dataOut.normFactor | |||
|
248 | # noise = dataOut.getNoise()/factor | |||
|
249 | # noisedB = 10*numpy.log10(noise) | |||
|
250 | ||||
|
251 | #thisDatetime = dataOut.datatime | |||
|
252 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
253 | title = wintitle + " RTI: %s" %(thisDatetime.strftime("%d-%b-%Y")) | |||
|
254 | xlabel = "Local Time" | |||
|
255 | ylabel = "Intensity (dB)" | |||
|
256 | ||||
|
257 | if not self.isConfig: | |||
|
258 | ||||
|
259 | nplots = 1 | |||
|
260 | ||||
|
261 | self.setup(id=id, | |||
|
262 | nplots=nplots, | |||
|
263 | wintitle=wintitle, | |||
|
264 | showprofile=showprofile, | |||
|
265 | show=show) | |||
|
266 | ||||
|
267 | tmin, tmax = self.getTimeLim(x, xmin, xmax) | |||
|
268 | if ymin == None: ymin = numpy.nanmin(datadB) | |||
|
269 | if ymax == None: ymax = numpy.nanmax(datadB) | |||
|
270 | ||||
|
271 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
272 | self.isConfig = True | |||
|
273 | ||||
|
274 | self.xdata = numpy.array([]) | |||
|
275 | self.ydata = numpy.array([]) | |||
|
276 | ||||
|
277 | self.setWinTitle(title) | |||
|
278 | ||||
|
279 | ||||
|
280 | # title = "RTI %s" %(thisDatetime.strftime("%d-%b-%Y")) | |||
|
281 | title = "RTI - %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
282 | ||||
|
283 | legendlabels = ["channel %d"%idchannel for idchannel in channelList] | |||
|
284 | axes = self.axesList[0] | |||
|
285 | ||||
|
286 | self.xdata = numpy.hstack((self.xdata, x[0:1])) | |||
|
287 | ||||
|
288 | if len(self.ydata)==0: | |||
|
289 | self.ydata = datadB[channelIndexList].reshape(-1,1) | |||
|
290 | else: | |||
|
291 | self.ydata = numpy.hstack((self.ydata, datadB[channelIndexList].reshape(-1,1))) | |||
|
292 | ||||
|
293 | ||||
|
294 | axes.pmultilineyaxis(x=self.xdata, y=self.ydata, | |||
|
295 | xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, | |||
|
296 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='.', markersize=8, linestyle="solid", grid='both', | |||
|
297 | XAxisAsTime=True | |||
|
298 | ) | |||
|
299 | ||||
|
300 | self.draw() | |||
|
301 | ||||
|
302 | if save: | |||
|
303 | ||||
|
304 | if figfile == None: | |||
|
305 | figfile = self.getFilename(name = self.name) | |||
|
306 | ||||
|
307 | self.saveFigure(figpath, figfile) | |||
|
308 | ||||
|
309 | self.counter_imagwr += 1 | |||
|
310 | if (ftp and (self.counter_imagwr==wr_period)): | |||
|
311 | ftp_filename = os.path.join(figpath,figfile) | |||
|
312 | self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
313 | self.counter_imagwr = 0 | |||
|
314 | ||||
|
315 | if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: | |||
|
316 | self.isConfig = False | |||
|
317 | del self.xdata | |||
|
318 | del self.ydata |
This diff has been collapsed as it changes many lines, (1196 lines changed) Show them Hide them | |||||
@@ -0,0 +1,1196 | |||||
|
1 | ''' | |||
|
2 | @author: Daniel Suarez | |||
|
3 | ''' | |||
|
4 | import os | |||
|
5 | import datetime | |||
|
6 | import numpy | |||
|
7 | ||||
|
8 | from figure import Figure, isRealtime | |||
|
9 | ||||
|
10 | class SpectraPlot(Figure): | |||
|
11 | ||||
|
12 | isConfig = None | |||
|
13 | __nsubplots = None | |||
|
14 | ||||
|
15 | WIDTHPROF = None | |||
|
16 | HEIGHTPROF = None | |||
|
17 | PREFIX = 'spc' | |||
|
18 | ||||
|
19 | def __init__(self): | |||
|
20 | ||||
|
21 | self.isConfig = False | |||
|
22 | self.__nsubplots = 1 | |||
|
23 | ||||
|
24 | self.WIDTH = 280 | |||
|
25 | self.HEIGHT = 250 | |||
|
26 | self.WIDTHPROF = 120 | |||
|
27 | self.HEIGHTPROF = 0 | |||
|
28 | self.counter_imagwr = 0 | |||
|
29 | ||||
|
30 | self.PLOT_CODE = 1 | |||
|
31 | self.FTP_WEI = None | |||
|
32 | self.EXP_CODE = None | |||
|
33 | self.SUB_EXP_CODE = None | |||
|
34 | self.PLOT_POS = None | |||
|
35 | ||||
|
36 | def getSubplots(self): | |||
|
37 | ||||
|
38 | ncol = int(numpy.sqrt(self.nplots)+0.9) | |||
|
39 | nrow = int(self.nplots*1./ncol + 0.9) | |||
|
40 | ||||
|
41 | return nrow, ncol | |||
|
42 | ||||
|
43 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): | |||
|
44 | ||||
|
45 | self.__showprofile = showprofile | |||
|
46 | self.nplots = nplots | |||
|
47 | ||||
|
48 | ncolspan = 1 | |||
|
49 | colspan = 1 | |||
|
50 | if showprofile: | |||
|
51 | ncolspan = 3 | |||
|
52 | colspan = 2 | |||
|
53 | self.__nsubplots = 2 | |||
|
54 | ||||
|
55 | self.createFigure(id = id, | |||
|
56 | wintitle = wintitle, | |||
|
57 | widthplot = self.WIDTH + self.WIDTHPROF, | |||
|
58 | heightplot = self.HEIGHT + self.HEIGHTPROF, | |||
|
59 | show=show) | |||
|
60 | ||||
|
61 | nrow, ncol = self.getSubplots() | |||
|
62 | ||||
|
63 | counter = 0 | |||
|
64 | for y in range(nrow): | |||
|
65 | for x in range(ncol): | |||
|
66 | ||||
|
67 | if counter >= self.nplots: | |||
|
68 | break | |||
|
69 | ||||
|
70 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) | |||
|
71 | ||||
|
72 | if showprofile: | |||
|
73 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) | |||
|
74 | ||||
|
75 | counter += 1 | |||
|
76 | ||||
|
77 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True, | |||
|
78 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, | |||
|
79 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, | |||
|
80 | server=None, folder=None, username=None, password=None, | |||
|
81 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False): | |||
|
82 | ||||
|
83 | """ | |||
|
84 | ||||
|
85 | Input: | |||
|
86 | dataOut : | |||
|
87 | id : | |||
|
88 | wintitle : | |||
|
89 | channelList : | |||
|
90 | showProfile : | |||
|
91 | xmin : None, | |||
|
92 | xmax : None, | |||
|
93 | ymin : None, | |||
|
94 | ymax : None, | |||
|
95 | zmin : None, | |||
|
96 | zmax : None | |||
|
97 | """ | |||
|
98 | ||||
|
99 | if dataOut.flagNoData: | |||
|
100 | return None | |||
|
101 | ||||
|
102 | if realtime: | |||
|
103 | if not(isRealtime(utcdatatime = dataOut.utctime)): | |||
|
104 | print 'Skipping this plot function' | |||
|
105 | return | |||
|
106 | ||||
|
107 | if channelList == None: | |||
|
108 | channelIndexList = dataOut.channelIndexList | |||
|
109 | else: | |||
|
110 | channelIndexList = [] | |||
|
111 | for channel in channelList: | |||
|
112 | if channel not in dataOut.channelList: | |||
|
113 | raise ValueError, "Channel %d is not in dataOut.channelList" | |||
|
114 | channelIndexList.append(dataOut.channelList.index(channel)) | |||
|
115 | ||||
|
116 | factor = dataOut.normFactor | |||
|
117 | ||||
|
118 | x = dataOut.getVelRange(1) | |||
|
119 | y = dataOut.getHeiRange() | |||
|
120 | ||||
|
121 | z = dataOut.data_spc[channelIndexList,:,:]/factor | |||
|
122 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) | |||
|
123 | avg = numpy.average(z, axis=1) | |||
|
124 | avg = numpy.nanmean(z, axis=1) | |||
|
125 | noise = dataOut.noise/factor | |||
|
126 | ||||
|
127 | zdB = 10*numpy.log10(z) | |||
|
128 | avgdB = 10*numpy.log10(avg) | |||
|
129 | noisedB = 10*numpy.log10(noise) | |||
|
130 | ||||
|
131 | #thisDatetime = dataOut.datatime | |||
|
132 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
133 | title = wintitle + " Spectra" | |||
|
134 | xlabel = "Velocity (m/s)" | |||
|
135 | ylabel = "Range (Km)" | |||
|
136 | ||||
|
137 | if not self.isConfig: | |||
|
138 | ||||
|
139 | nplots = len(channelIndexList) | |||
|
140 | ||||
|
141 | self.setup(id=id, | |||
|
142 | nplots=nplots, | |||
|
143 | wintitle=wintitle, | |||
|
144 | showprofile=showprofile, | |||
|
145 | show=show) | |||
|
146 | ||||
|
147 | if xmin == None: xmin = numpy.nanmin(x) | |||
|
148 | if xmax == None: xmax = numpy.nanmax(x) | |||
|
149 | if ymin == None: ymin = numpy.nanmin(y) | |||
|
150 | if ymax == None: ymax = numpy.nanmax(y) | |||
|
151 | if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 | |||
|
152 | if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 | |||
|
153 | ||||
|
154 | self.FTP_WEI = ftp_wei | |||
|
155 | self.EXP_CODE = exp_code | |||
|
156 | self.SUB_EXP_CODE = sub_exp_code | |||
|
157 | self.PLOT_POS = plot_pos | |||
|
158 | ||||
|
159 | self.isConfig = True | |||
|
160 | ||||
|
161 | self.setWinTitle(title) | |||
|
162 | ||||
|
163 | for i in range(self.nplots): | |||
|
164 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) | |||
|
165 | title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i]+1, noisedB[i], str_datetime) | |||
|
166 | axes = self.axesList[i*self.__nsubplots] | |||
|
167 | axes.pcolor(x, y, zdB[i,:,:], | |||
|
168 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, | |||
|
169 | xlabel=xlabel, ylabel=ylabel, title=title, | |||
|
170 | ticksize=9, cblabel='') | |||
|
171 | ||||
|
172 | if self.__showprofile: | |||
|
173 | axes = self.axesList[i*self.__nsubplots +1] | |||
|
174 | axes.pline(avgdB[i], y, | |||
|
175 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, | |||
|
176 | xlabel='dB', ylabel='', title='', | |||
|
177 | ytick_visible=False, | |||
|
178 | grid='x') | |||
|
179 | ||||
|
180 | noiseline = numpy.repeat(noisedB[i], len(y)) | |||
|
181 | axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2) | |||
|
182 | ||||
|
183 | self.draw() | |||
|
184 | ||||
|
185 | if save: | |||
|
186 | ||||
|
187 | self.counter_imagwr += 1 | |||
|
188 | if (self.counter_imagwr==wr_period): | |||
|
189 | if figfile == None: | |||
|
190 | str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
191 | figfile = self.getFilename(name = str_datetime) | |||
|
192 | ||||
|
193 | self.saveFigure(figpath, figfile) | |||
|
194 | ||||
|
195 | if ftp: | |||
|
196 | #provisionalmente envia archivos en el formato de la web en tiempo real | |||
|
197 | name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) | |||
|
198 | path = '%s%03d' %(self.PREFIX, self.id) | |||
|
199 | ftp_file = os.path.join(path,'ftp','%s.png'%name) | |||
|
200 | self.saveFigure(figpath, ftp_file) | |||
|
201 | ftp_filename = os.path.join(figpath,ftp_file) | |||
|
202 | self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
203 | self.counter_imagwr = 0 | |||
|
204 | ||||
|
205 | ||||
|
206 | self.counter_imagwr = 0 | |||
|
207 | ||||
|
208 | class CrossSpectraPlot(Figure): | |||
|
209 | ||||
|
210 | isConfig = None | |||
|
211 | __nsubplots = None | |||
|
212 | ||||
|
213 | WIDTH = None | |||
|
214 | HEIGHT = None | |||
|
215 | WIDTHPROF = None | |||
|
216 | HEIGHTPROF = None | |||
|
217 | PREFIX = 'cspc' | |||
|
218 | ||||
|
219 | def __init__(self): | |||
|
220 | ||||
|
221 | self.isConfig = False | |||
|
222 | self.__nsubplots = 4 | |||
|
223 | self.counter_imagwr = 0 | |||
|
224 | self.WIDTH = 250 | |||
|
225 | self.HEIGHT = 250 | |||
|
226 | self.WIDTHPROF = 0 | |||
|
227 | self.HEIGHTPROF = 0 | |||
|
228 | ||||
|
229 | self.PLOT_CODE = 1 | |||
|
230 | self.FTP_WEI = None | |||
|
231 | self.EXP_CODE = None | |||
|
232 | self.SUB_EXP_CODE = None | |||
|
233 | self.PLOT_POS = None | |||
|
234 | ||||
|
235 | def getSubplots(self): | |||
|
236 | ||||
|
237 | ncol = 4 | |||
|
238 | nrow = self.nplots | |||
|
239 | ||||
|
240 | return nrow, ncol | |||
|
241 | ||||
|
242 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): | |||
|
243 | ||||
|
244 | self.__showprofile = showprofile | |||
|
245 | self.nplots = nplots | |||
|
246 | ||||
|
247 | ncolspan = 1 | |||
|
248 | colspan = 1 | |||
|
249 | ||||
|
250 | self.createFigure(id = id, | |||
|
251 | wintitle = wintitle, | |||
|
252 | widthplot = self.WIDTH + self.WIDTHPROF, | |||
|
253 | heightplot = self.HEIGHT + self.HEIGHTPROF, | |||
|
254 | show=True) | |||
|
255 | ||||
|
256 | nrow, ncol = self.getSubplots() | |||
|
257 | ||||
|
258 | counter = 0 | |||
|
259 | for y in range(nrow): | |||
|
260 | for x in range(ncol): | |||
|
261 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) | |||
|
262 | ||||
|
263 | counter += 1 | |||
|
264 | ||||
|
265 | def run(self, dataOut, id, wintitle="", pairsList=None, | |||
|
266 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, | |||
|
267 | save=False, figpath='./', figfile=None, ftp=False, wr_period=1, | |||
|
268 | power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True, | |||
|
269 | server=None, folder=None, username=None, password=None, | |||
|
270 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): | |||
|
271 | ||||
|
272 | """ | |||
|
273 | ||||
|
274 | Input: | |||
|
275 | dataOut : | |||
|
276 | id : | |||
|
277 | wintitle : | |||
|
278 | channelList : | |||
|
279 | showProfile : | |||
|
280 | xmin : None, | |||
|
281 | xmax : None, | |||
|
282 | ymin : None, | |||
|
283 | ymax : None, | |||
|
284 | zmin : None, | |||
|
285 | zmax : None | |||
|
286 | """ | |||
|
287 | ||||
|
288 | if pairsList == None: | |||
|
289 | pairsIndexList = dataOut.pairsIndexList | |||
|
290 | else: | |||
|
291 | pairsIndexList = [] | |||
|
292 | for pair in pairsList: | |||
|
293 | if pair not in dataOut.pairsList: | |||
|
294 | raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) | |||
|
295 | pairsIndexList.append(dataOut.pairsList.index(pair)) | |||
|
296 | ||||
|
297 | if pairsIndexList == []: | |||
|
298 | return | |||
|
299 | ||||
|
300 | if len(pairsIndexList) > 4: | |||
|
301 | pairsIndexList = pairsIndexList[0:4] | |||
|
302 | factor = dataOut.normFactor | |||
|
303 | x = dataOut.getVelRange(1) | |||
|
304 | y = dataOut.getHeiRange() | |||
|
305 | z = dataOut.data_spc[:,:,:]/factor | |||
|
306 | # z = numpy.where(numpy.isfinite(z), z, numpy.NAN) | |||
|
307 | avg = numpy.abs(numpy.average(z, axis=1)) | |||
|
308 | noise = dataOut.noise()/factor | |||
|
309 | ||||
|
310 | zdB = 10*numpy.log10(z) | |||
|
311 | avgdB = 10*numpy.log10(avg) | |||
|
312 | noisedB = 10*numpy.log10(noise) | |||
|
313 | ||||
|
314 | ||||
|
315 | #thisDatetime = dataOut.datatime | |||
|
316 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
317 | title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
318 | xlabel = "Velocity (m/s)" | |||
|
319 | ylabel = "Range (Km)" | |||
|
320 | ||||
|
321 | if not self.isConfig: | |||
|
322 | ||||
|
323 | nplots = len(pairsIndexList) | |||
|
324 | ||||
|
325 | self.setup(id=id, | |||
|
326 | nplots=nplots, | |||
|
327 | wintitle=wintitle, | |||
|
328 | showprofile=False, | |||
|
329 | show=show) | |||
|
330 | ||||
|
331 | if xmin == None: xmin = numpy.nanmin(x) | |||
|
332 | if xmax == None: xmax = numpy.nanmax(x) | |||
|
333 | if ymin == None: ymin = numpy.nanmin(y) | |||
|
334 | if ymax == None: ymax = numpy.nanmax(y) | |||
|
335 | if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 | |||
|
336 | if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 | |||
|
337 | ||||
|
338 | self.FTP_WEI = ftp_wei | |||
|
339 | self.EXP_CODE = exp_code | |||
|
340 | self.SUB_EXP_CODE = sub_exp_code | |||
|
341 | self.PLOT_POS = plot_pos | |||
|
342 | ||||
|
343 | self.isConfig = True | |||
|
344 | ||||
|
345 | self.setWinTitle(title) | |||
|
346 | ||||
|
347 | for i in range(self.nplots): | |||
|
348 | pair = dataOut.pairsList[pairsIndexList[i]] | |||
|
349 | str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S")) | |||
|
350 | title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[pair[0]], str_datetime) | |||
|
351 | zdB = 10.*numpy.log10(dataOut.data_spc[pair[0],:,:]/factor) | |||
|
352 | axes0 = self.axesList[i*self.__nsubplots] | |||
|
353 | axes0.pcolor(x, y, zdB, | |||
|
354 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, | |||
|
355 | xlabel=xlabel, ylabel=ylabel, title=title, | |||
|
356 | ticksize=9, colormap=power_cmap, cblabel='') | |||
|
357 | ||||
|
358 | title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[pair[1]], str_datetime) | |||
|
359 | zdB = 10.*numpy.log10(dataOut.data_spc[pair[1],:,:]/factor) | |||
|
360 | axes0 = self.axesList[i*self.__nsubplots+1] | |||
|
361 | axes0.pcolor(x, y, zdB, | |||
|
362 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, | |||
|
363 | xlabel=xlabel, ylabel=ylabel, title=title, | |||
|
364 | ticksize=9, colormap=power_cmap, cblabel='') | |||
|
365 | ||||
|
366 | coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:]) | |||
|
367 | coherence = numpy.abs(coherenceComplex) | |||
|
368 | # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi | |||
|
369 | phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi | |||
|
370 | ||||
|
371 | title = "Coherence %d%d" %(pair[0], pair[1]) | |||
|
372 | axes0 = self.axesList[i*self.__nsubplots+2] | |||
|
373 | axes0.pcolor(x, y, coherence, | |||
|
374 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=1, | |||
|
375 | xlabel=xlabel, ylabel=ylabel, title=title, | |||
|
376 | ticksize=9, colormap=coherence_cmap, cblabel='') | |||
|
377 | ||||
|
378 | title = "Phase %d%d" %(pair[0], pair[1]) | |||
|
379 | axes0 = self.axesList[i*self.__nsubplots+3] | |||
|
380 | axes0.pcolor(x, y, phase, | |||
|
381 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180, | |||
|
382 | xlabel=xlabel, ylabel=ylabel, title=title, | |||
|
383 | ticksize=9, colormap=phase_cmap, cblabel='') | |||
|
384 | ||||
|
385 | ||||
|
386 | ||||
|
387 | self.draw() | |||
|
388 | ||||
|
389 | if save: | |||
|
390 | ||||
|
391 | self.counter_imagwr += 1 | |||
|
392 | if (self.counter_imagwr==wr_period): | |||
|
393 | if figfile == None: | |||
|
394 | str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
395 | figfile = self.getFilename(name = str_datetime) | |||
|
396 | ||||
|
397 | self.saveFigure(figpath, figfile) | |||
|
398 | ||||
|
399 | if ftp: | |||
|
400 | #provisionalmente envia archivos en el formato de la web en tiempo real | |||
|
401 | name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) | |||
|
402 | path = '%s%03d' %(self.PREFIX, self.id) | |||
|
403 | ftp_file = os.path.join(path,'ftp','%s.png'%name) | |||
|
404 | self.saveFigure(figpath, ftp_file) | |||
|
405 | ftp_filename = os.path.join(figpath,ftp_file) | |||
|
406 | ||||
|
407 | try: | |||
|
408 | self.sendByFTP(ftp_filename, server, folder, username, password) | |||
|
409 | except: | |||
|
410 | self.counter_imagwr = 0 | |||
|
411 | print ValueError, 'Error FTP' | |||
|
412 | ||||
|
413 | self.counter_imagwr = 0 | |||
|
414 | ||||
|
415 | class RTIPlot(Figure): | |||
|
416 | ||||
|
417 | isConfig = None | |||
|
418 | __nsubplots = None | |||
|
419 | ||||
|
420 | WIDTHPROF = None | |||
|
421 | HEIGHTPROF = None | |||
|
422 | PREFIX = 'rti' | |||
|
423 | ||||
|
424 | def __init__(self): | |||
|
425 | ||||
|
426 | self.timerange = 2*60*60 | |||
|
427 | self.isConfig = False | |||
|
428 | self.__nsubplots = 1 | |||
|
429 | ||||
|
430 | self.WIDTH = 800 | |||
|
431 | self.HEIGHT = 150 | |||
|
432 | self.WIDTHPROF = 120 | |||
|
433 | self.HEIGHTPROF = 0 | |||
|
434 | self.counter_imagwr = 0 | |||
|
435 | ||||
|
436 | self.PLOT_CODE = 0 | |||
|
437 | self.FTP_WEI = None | |||
|
438 | self.EXP_CODE = None | |||
|
439 | self.SUB_EXP_CODE = None | |||
|
440 | self.PLOT_POS = None | |||
|
441 | self.tmin = None | |||
|
442 | self.tmax = None | |||
|
443 | ||||
|
444 | self.xmin = None | |||
|
445 | self.xmax = None | |||
|
446 | ||||
|
447 | def getSubplots(self): | |||
|
448 | ||||
|
449 | ncol = 1 | |||
|
450 | nrow = self.nplots | |||
|
451 | ||||
|
452 | return nrow, ncol | |||
|
453 | ||||
|
454 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): | |||
|
455 | ||||
|
456 | self.__showprofile = showprofile | |||
|
457 | self.nplots = nplots | |||
|
458 | ||||
|
459 | ncolspan = 1 | |||
|
460 | colspan = 1 | |||
|
461 | if showprofile: | |||
|
462 | ncolspan = 7 | |||
|
463 | colspan = 6 | |||
|
464 | self.__nsubplots = 2 | |||
|
465 | ||||
|
466 | self.createFigure(id = id, | |||
|
467 | wintitle = wintitle, | |||
|
468 | widthplot = self.WIDTH + self.WIDTHPROF, | |||
|
469 | heightplot = self.HEIGHT + self.HEIGHTPROF, | |||
|
470 | show=show) | |||
|
471 | ||||
|
472 | nrow, ncol = self.getSubplots() | |||
|
473 | ||||
|
474 | counter = 0 | |||
|
475 | for y in range(nrow): | |||
|
476 | for x in range(ncol): | |||
|
477 | ||||
|
478 | if counter >= self.nplots: | |||
|
479 | break | |||
|
480 | ||||
|
481 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) | |||
|
482 | ||||
|
483 | if showprofile: | |||
|
484 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) | |||
|
485 | ||||
|
486 | counter += 1 | |||
|
487 | ||||
|
488 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', | |||
|
489 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, | |||
|
490 | timerange=None, | |||
|
491 | save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True, | |||
|
492 | server=None, folder=None, username=None, password=None, | |||
|
493 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): | |||
|
494 | ||||
|
495 | """ | |||
|
496 | ||||
|
497 | Input: | |||
|
498 | dataOut : | |||
|
499 | id : | |||
|
500 | wintitle : | |||
|
501 | channelList : | |||
|
502 | showProfile : | |||
|
503 | xmin : None, | |||
|
504 | xmax : None, | |||
|
505 | ymin : None, | |||
|
506 | ymax : None, | |||
|
507 | zmin : None, | |||
|
508 | zmax : None | |||
|
509 | """ | |||
|
510 | ||||
|
511 | if channelList == None: | |||
|
512 | channelIndexList = dataOut.channelIndexList | |||
|
513 | else: | |||
|
514 | channelIndexList = [] | |||
|
515 | for channel in channelList: | |||
|
516 | if channel not in dataOut.channelList: | |||
|
517 | raise ValueError, "Channel %d is not in dataOut.channelList" | |||
|
518 | channelIndexList.append(dataOut.channelList.index(channel)) | |||
|
519 | ||||
|
520 | if timerange != None: | |||
|
521 | self.timerange = timerange | |||
|
522 | ||||
|
523 | #tmin = None | |||
|
524 | #tmax = None | |||
|
525 | factor = dataOut.normFactor | |||
|
526 | x = dataOut.getTimeRange() | |||
|
527 | y = dataOut.getHeiRange() | |||
|
528 | ||||
|
529 | z = dataOut.data_spc[channelIndexList,:,:]/factor | |||
|
530 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) | |||
|
531 | avg = numpy.average(z, axis=1) | |||
|
532 | ||||
|
533 | avgdB = 10.*numpy.log10(avg) | |||
|
534 | ||||
|
535 | ||||
|
536 | # thisDatetime = dataOut.datatime | |||
|
537 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
538 | title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) | |||
|
539 | xlabel = "" | |||
|
540 | ylabel = "Range (Km)" | |||
|
541 | ||||
|
542 | if not self.isConfig: | |||
|
543 | ||||
|
544 | nplots = len(channelIndexList) | |||
|
545 | ||||
|
546 | self.setup(id=id, | |||
|
547 | nplots=nplots, | |||
|
548 | wintitle=wintitle, | |||
|
549 | showprofile=showprofile, | |||
|
550 | show=show) | |||
|
551 | ||||
|
552 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) | |||
|
553 | ||||
|
554 | # if timerange != None: | |||
|
555 | # self.timerange = timerange | |||
|
556 | # self.xmin, self.tmax = self.getTimeLim(x, xmin, xmax, timerange) | |||
|
557 | ||||
|
558 | ||||
|
559 | ||||
|
560 | if ymin == None: ymin = numpy.nanmin(y) | |||
|
561 | if ymax == None: ymax = numpy.nanmax(y) | |||
|
562 | if zmin == None: zmin = numpy.nanmin(avgdB)*0.9 | |||
|
563 | if zmax == None: zmax = numpy.nanmax(avgdB)*0.9 | |||
|
564 | ||||
|
565 | self.FTP_WEI = ftp_wei | |||
|
566 | self.EXP_CODE = exp_code | |||
|
567 | self.SUB_EXP_CODE = sub_exp_code | |||
|
568 | self.PLOT_POS = plot_pos | |||
|
569 | ||||
|
570 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
571 | self.isConfig = True | |||
|
572 | ||||
|
573 | ||||
|
574 | self.setWinTitle(title) | |||
|
575 | ||||
|
576 | if ((self.xmax - x[1]) < (x[1]-x[0])): | |||
|
577 | x[1] = self.xmax | |||
|
578 | ||||
|
579 | for i in range(self.nplots): | |||
|
580 | title = "Channel %d: %s" %(dataOut.channelList[i]+1, thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) | |||
|
581 | axes = self.axesList[i*self.__nsubplots] | |||
|
582 | zdB = avgdB[i].reshape((1,-1)) | |||
|
583 | axes.pcolorbuffer(x, y, zdB, | |||
|
584 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, | |||
|
585 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, | |||
|
586 | ticksize=9, cblabel='', cbsize="1%") | |||
|
587 | ||||
|
588 | if self.__showprofile: | |||
|
589 | axes = self.axesList[i*self.__nsubplots +1] | |||
|
590 | axes.pline(avgdB[i], y, | |||
|
591 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, | |||
|
592 | xlabel='dB', ylabel='', title='', | |||
|
593 | ytick_visible=False, | |||
|
594 | grid='x') | |||
|
595 | ||||
|
596 | self.draw() | |||
|
597 | ||||
|
598 | # if lastone: | |||
|
599 | # if dataOut.blocknow >= dataOut.last_block: | |||
|
600 | # if figfile == None: | |||
|
601 | # figfile = self.getFilename(name = self.name) | |||
|
602 | # self.saveFigure(figpath, figfile) | |||
|
603 | # | |||
|
604 | # if (save and not(lastone)): | |||
|
605 | # | |||
|
606 | # self.counter_imagwr += 1 | |||
|
607 | # if (self.counter_imagwr==wr_period): | |||
|
608 | # if figfile == None: | |||
|
609 | # figfile = self.getFilename(name = self.name) | |||
|
610 | # self.saveFigure(figpath, figfile) | |||
|
611 | # | |||
|
612 | # if ftp: | |||
|
613 | # #provisionalmente envia archivos en el formato de la web en tiempo real | |||
|
614 | # name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) | |||
|
615 | # path = '%s%03d' %(self.PREFIX, self.id) | |||
|
616 | # ftp_file = os.path.join(path,'ftp','%s.png'%name) | |||
|
617 | # self.saveFigure(figpath, ftp_file) | |||
|
618 | # ftp_filename = os.path.join(figpath,ftp_file) | |||
|
619 | # self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
620 | # self.counter_imagwr = 0 | |||
|
621 | # | |||
|
622 | # self.counter_imagwr = 0 | |||
|
623 | ||||
|
624 | #if ((dataOut.utctime-time.timezone) >= self.axesList[0].xmax): | |||
|
625 | self.saveFigure(figpath, figfile) | |||
|
626 | if x[1] >= self.axesList[0].xmax: | |||
|
627 | self.saveFigure(figpath, figfile) | |||
|
628 | self.isConfig = False | |||
|
629 | ||||
|
630 | # if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: | |||
|
631 | # | |||
|
632 | # self.isConfig = False | |||
|
633 | ||||
|
634 | # if lastone: | |||
|
635 | # if figfile == None: | |||
|
636 | # figfile = self.getFilename(name = self.name) | |||
|
637 | # self.saveFigure(figpath, figfile) | |||
|
638 | # | |||
|
639 | # if ftp: | |||
|
640 | # #provisionalmente envia archivos en el formato de la web en tiempo real | |||
|
641 | # name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) | |||
|
642 | # path = '%s%03d' %(self.PREFIX, self.id) | |||
|
643 | # ftp_file = os.path.join(path,'ftp','%s.png'%name) | |||
|
644 | # self.saveFigure(figpath, ftp_file) | |||
|
645 | # ftp_filename = os.path.join(figpath,ftp_file) | |||
|
646 | # self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
647 | ||||
|
648 | class CoherenceMap(Figure): | |||
|
649 | isConfig = None | |||
|
650 | __nsubplots = None | |||
|
651 | ||||
|
652 | WIDTHPROF = None | |||
|
653 | HEIGHTPROF = None | |||
|
654 | PREFIX = 'cmap' | |||
|
655 | ||||
|
656 | def __init__(self): | |||
|
657 | self.timerange = 2*60*60 | |||
|
658 | self.isConfig = False | |||
|
659 | self.__nsubplots = 1 | |||
|
660 | ||||
|
661 | self.WIDTH = 800 | |||
|
662 | self.HEIGHT = 150 | |||
|
663 | self.WIDTHPROF = 120 | |||
|
664 | self.HEIGHTPROF = 0 | |||
|
665 | self.counter_imagwr = 0 | |||
|
666 | ||||
|
667 | self.PLOT_CODE = 3 | |||
|
668 | self.FTP_WEI = None | |||
|
669 | self.EXP_CODE = None | |||
|
670 | self.SUB_EXP_CODE = None | |||
|
671 | self.PLOT_POS = None | |||
|
672 | self.counter_imagwr = 0 | |||
|
673 | ||||
|
674 | self.xmin = None | |||
|
675 | self.xmax = None | |||
|
676 | ||||
|
677 | def getSubplots(self): | |||
|
678 | ncol = 1 | |||
|
679 | nrow = self.nplots*2 | |||
|
680 | ||||
|
681 | return nrow, ncol | |||
|
682 | ||||
|
683 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): | |||
|
684 | self.__showprofile = showprofile | |||
|
685 | self.nplots = nplots | |||
|
686 | ||||
|
687 | ncolspan = 1 | |||
|
688 | colspan = 1 | |||
|
689 | if showprofile: | |||
|
690 | ncolspan = 7 | |||
|
691 | colspan = 6 | |||
|
692 | self.__nsubplots = 2 | |||
|
693 | ||||
|
694 | self.createFigure(id = id, | |||
|
695 | wintitle = wintitle, | |||
|
696 | widthplot = self.WIDTH + self.WIDTHPROF, | |||
|
697 | heightplot = self.HEIGHT + self.HEIGHTPROF, | |||
|
698 | show=True) | |||
|
699 | ||||
|
700 | nrow, ncol = self.getSubplots() | |||
|
701 | ||||
|
702 | for y in range(nrow): | |||
|
703 | for x in range(ncol): | |||
|
704 | ||||
|
705 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) | |||
|
706 | ||||
|
707 | if showprofile: | |||
|
708 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1) | |||
|
709 | ||||
|
710 | def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True', | |||
|
711 | xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, | |||
|
712 | timerange=None, | |||
|
713 | save=False, figpath='./', figfile=None, ftp=False, wr_period=1, | |||
|
714 | coherence_cmap='jet', phase_cmap='RdBu_r', show=True, | |||
|
715 | server=None, folder=None, username=None, password=None, | |||
|
716 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): | |||
|
717 | ||||
|
718 | if pairsList == None: | |||
|
719 | pairsIndexList = dataOut.pairsIndexList | |||
|
720 | else: | |||
|
721 | pairsIndexList = [] | |||
|
722 | for pair in pairsList: | |||
|
723 | if pair not in dataOut.pairsList: | |||
|
724 | raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) | |||
|
725 | pairsIndexList.append(dataOut.pairsList.index(pair)) | |||
|
726 | ||||
|
727 | if timerange != None: | |||
|
728 | self.timerange = timerange | |||
|
729 | ||||
|
730 | if pairsIndexList == []: | |||
|
731 | return | |||
|
732 | ||||
|
733 | if len(pairsIndexList) > 4: | |||
|
734 | pairsIndexList = pairsIndexList[0:4] | |||
|
735 | ||||
|
736 | # tmin = None | |||
|
737 | # tmax = None | |||
|
738 | x = dataOut.getTimeRange() | |||
|
739 | y = dataOut.getHeiRange() | |||
|
740 | ||||
|
741 | #thisDatetime = dataOut.datatime | |||
|
742 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
743 | title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y")) | |||
|
744 | xlabel = "" | |||
|
745 | ylabel = "Range (Km)" | |||
|
746 | ||||
|
747 | if not self.isConfig: | |||
|
748 | nplots = len(pairsIndexList) | |||
|
749 | self.setup(id=id, | |||
|
750 | nplots=nplots, | |||
|
751 | wintitle=wintitle, | |||
|
752 | showprofile=showprofile, | |||
|
753 | show=show) | |||
|
754 | ||||
|
755 | #tmin, tmax = self.getTimeLim(x, xmin, xmax) | |||
|
756 | ||||
|
757 | self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange) | |||
|
758 | ||||
|
759 | if ymin == None: ymin = numpy.nanmin(y) | |||
|
760 | if ymax == None: ymax = numpy.nanmax(y) | |||
|
761 | if zmin == None: zmin = 0. | |||
|
762 | if zmax == None: zmax = 1. | |||
|
763 | ||||
|
764 | self.FTP_WEI = ftp_wei | |||
|
765 | self.EXP_CODE = exp_code | |||
|
766 | self.SUB_EXP_CODE = sub_exp_code | |||
|
767 | self.PLOT_POS = plot_pos | |||
|
768 | ||||
|
769 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
770 | ||||
|
771 | self.isConfig = True | |||
|
772 | ||||
|
773 | self.setWinTitle(title) | |||
|
774 | ||||
|
775 | if ((self.xmax - x[1]) < (x[1]-x[0])): | |||
|
776 | x[1] = self.xmax | |||
|
777 | ||||
|
778 | for i in range(self.nplots): | |||
|
779 | ||||
|
780 | pair = dataOut.pairsList[pairsIndexList[i]] | |||
|
781 | # coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:]) | |||
|
782 | # avgcoherenceComplex = numpy.average(coherenceComplex, axis=0) | |||
|
783 | # coherence = numpy.abs(avgcoherenceComplex) | |||
|
784 | ||||
|
785 | ## coherence = numpy.abs(coherenceComplex) | |||
|
786 | ## avg = numpy.average(coherence, axis=0) | |||
|
787 | ||||
|
788 | ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0) | |||
|
789 | powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0) | |||
|
790 | powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0) | |||
|
791 | ||||
|
792 | ||||
|
793 | avgcoherenceComplex = ccf/numpy.sqrt(powa*powb) | |||
|
794 | coherence = numpy.abs(avgcoherenceComplex) | |||
|
795 | ||||
|
796 | z = coherence.reshape((1,-1)) | |||
|
797 | ||||
|
798 | counter = 0 | |||
|
799 | ||||
|
800 | title = "Coherence %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
801 | axes = self.axesList[i*self.__nsubplots*2] | |||
|
802 | axes.pcolorbuffer(x, y, z, | |||
|
803 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax, | |||
|
804 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, | |||
|
805 | ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%") | |||
|
806 | ||||
|
807 | if self.__showprofile: | |||
|
808 | counter += 1 | |||
|
809 | axes = self.axesList[i*self.__nsubplots*2 + counter] | |||
|
810 | axes.pline(coherence, y, | |||
|
811 | xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax, | |||
|
812 | xlabel='', ylabel='', title='', ticksize=7, | |||
|
813 | ytick_visible=False, nxticks=5, | |||
|
814 | grid='x') | |||
|
815 | ||||
|
816 | counter += 1 | |||
|
817 | # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi | |||
|
818 | phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi | |||
|
819 | # avg = numpy.average(phase, axis=0) | |||
|
820 | z = phase.reshape((1,-1)) | |||
|
821 | ||||
|
822 | title = "Phase %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
823 | axes = self.axesList[i*self.__nsubplots*2 + counter] | |||
|
824 | axes.pcolorbuffer(x, y, z, | |||
|
825 | xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180, | |||
|
826 | xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True, | |||
|
827 | ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%") | |||
|
828 | ||||
|
829 | if self.__showprofile: | |||
|
830 | counter += 1 | |||
|
831 | axes = self.axesList[i*self.__nsubplots*2 + counter] | |||
|
832 | axes.pline(phase, y, | |||
|
833 | xmin=-180, xmax=180, ymin=ymin, ymax=ymax, | |||
|
834 | xlabel='', ylabel='', title='', ticksize=7, | |||
|
835 | ytick_visible=False, nxticks=4, | |||
|
836 | grid='x') | |||
|
837 | ||||
|
838 | self.draw() | |||
|
839 | ||||
|
840 | if x[1] >= self.axesList[0].xmax: | |||
|
841 | self.saveFigure(figpath, figfile) | |||
|
842 | self.isConfig = False | |||
|
843 | ||||
|
844 | # if save: | |||
|
845 | # | |||
|
846 | # self.counter_imagwr += 1 | |||
|
847 | # if (self.counter_imagwr==wr_period): | |||
|
848 | # if figfile == None: | |||
|
849 | # figfile = self.getFilename(name = self.name) | |||
|
850 | # self.saveFigure(figpath, figfile) | |||
|
851 | # | |||
|
852 | # if ftp: | |||
|
853 | # #provisionalmente envia archivos en el formato de la web en tiempo real | |||
|
854 | # name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) | |||
|
855 | # path = '%s%03d' %(self.PREFIX, self.id) | |||
|
856 | # ftp_file = os.path.join(path,'ftp','%s.png'%name) | |||
|
857 | # self.saveFigure(figpath, ftp_file) | |||
|
858 | # ftp_filename = os.path.join(figpath,ftp_file) | |||
|
859 | # self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
860 | # self.counter_imagwr = 0 | |||
|
861 | # | |||
|
862 | # self.counter_imagwr = 0 | |||
|
863 | # | |||
|
864 | # | |||
|
865 | # if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: | |||
|
866 | # self.isConfig = False | |||
|
867 | ||||
|
868 | class PowerProfile(Figure): | |||
|
869 | isConfig = None | |||
|
870 | __nsubplots = None | |||
|
871 | ||||
|
872 | WIDTHPROF = None | |||
|
873 | HEIGHTPROF = None | |||
|
874 | PREFIX = 'spcprofile' | |||
|
875 | ||||
|
876 | def __init__(self): | |||
|
877 | self.isConfig = False | |||
|
878 | self.__nsubplots = 1 | |||
|
879 | ||||
|
880 | self.WIDTH = 300 | |||
|
881 | self.HEIGHT = 500 | |||
|
882 | self.counter_imagwr = 0 | |||
|
883 | ||||
|
884 | def getSubplots(self): | |||
|
885 | ncol = 1 | |||
|
886 | nrow = 1 | |||
|
887 | ||||
|
888 | return nrow, ncol | |||
|
889 | ||||
|
890 | def setup(self, id, nplots, wintitle, show): | |||
|
891 | ||||
|
892 | self.nplots = nplots | |||
|
893 | ||||
|
894 | ncolspan = 1 | |||
|
895 | colspan = 1 | |||
|
896 | ||||
|
897 | self.createFigure(id = id, | |||
|
898 | wintitle = wintitle, | |||
|
899 | widthplot = self.WIDTH, | |||
|
900 | heightplot = self.HEIGHT, | |||
|
901 | show=show) | |||
|
902 | ||||
|
903 | nrow, ncol = self.getSubplots() | |||
|
904 | ||||
|
905 | counter = 0 | |||
|
906 | for y in range(nrow): | |||
|
907 | for x in range(ncol): | |||
|
908 | self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1) | |||
|
909 | ||||
|
910 | def run(self, dataOut, id, wintitle="", channelList=None, | |||
|
911 | xmin=None, xmax=None, ymin=None, ymax=None, | |||
|
912 | save=False, figpath='./', figfile=None, show=True, wr_period=1, | |||
|
913 | server=None, folder=None, username=None, password=None,): | |||
|
914 | ||||
|
915 | if dataOut.flagNoData: | |||
|
916 | return None | |||
|
917 | ||||
|
918 | if channelList == None: | |||
|
919 | channelIndexList = dataOut.channelIndexList | |||
|
920 | channelList = dataOut.channelList | |||
|
921 | else: | |||
|
922 | channelIndexList = [] | |||
|
923 | for channel in channelList: | |||
|
924 | if channel not in dataOut.channelList: | |||
|
925 | raise ValueError, "Channel %d is not in dataOut.channelList" | |||
|
926 | channelIndexList.append(dataOut.channelList.index(channel)) | |||
|
927 | ||||
|
928 | try: | |||
|
929 | factor = dataOut.normFactor | |||
|
930 | except: | |||
|
931 | factor = 1 | |||
|
932 | ||||
|
933 | y = dataOut.getHeiRange() | |||
|
934 | ||||
|
935 | #for voltage | |||
|
936 | if dataOut.type == 'Voltage': | |||
|
937 | x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) | |||
|
938 | x = x.real | |||
|
939 | x = numpy.where(numpy.isfinite(x), x, numpy.NAN) | |||
|
940 | ||||
|
941 | #for spectra | |||
|
942 | if dataOut.type == 'Spectra': | |||
|
943 | x = dataOut.data_spc[channelIndexList,:,:]/factor | |||
|
944 | x = numpy.where(numpy.isfinite(x), x, numpy.NAN) | |||
|
945 | x = numpy.average(x, axis=1) | |||
|
946 | ||||
|
947 | ||||
|
948 | xdB = 10*numpy.log10(x) | |||
|
949 | ||||
|
950 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
951 | title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y")) | |||
|
952 | xlabel = "dB" | |||
|
953 | ylabel = "Range (Km)" | |||
|
954 | ||||
|
955 | if not self.isConfig: | |||
|
956 | ||||
|
957 | nplots = 1 | |||
|
958 | ||||
|
959 | self.setup(id=id, | |||
|
960 | nplots=nplots, | |||
|
961 | wintitle=wintitle, | |||
|
962 | show=show) | |||
|
963 | ||||
|
964 | if ymin == None: ymin = numpy.nanmin(y) | |||
|
965 | if ymax == None: ymax = numpy.nanmax(y) | |||
|
966 | if xmin == None: xmin = numpy.nanmin(xdB)*0.9 | |||
|
967 | if xmax == None: xmax = numpy.nanmax(xdB)*0.9 | |||
|
968 | ||||
|
969 | self.__isConfig = True | |||
|
970 | ||||
|
971 | self.setWinTitle(title) | |||
|
972 | ||||
|
973 | title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
974 | axes = self.axesList[0] | |||
|
975 | ||||
|
976 | legendlabels = ["channel %d"%x for x in channelList] | |||
|
977 | axes.pmultiline(xdB, y, | |||
|
978 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, | |||
|
979 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, | |||
|
980 | ytick_visible=True, nxticks=5, | |||
|
981 | grid='x') | |||
|
982 | ||||
|
983 | self.draw() | |||
|
984 | ||||
|
985 | if save: | |||
|
986 | date = thisDatetime.strftime("%Y%m%d") | |||
|
987 | if figfile == None: | |||
|
988 | figfile = self.getFilename(name = date) | |||
|
989 | ||||
|
990 | self.saveFigure(figpath, figfile) | |||
|
991 | ||||
|
992 | self.counter_imagwr += 1 | |||
|
993 | if (ftp and (self.counter_imagwr==wr_period)): | |||
|
994 | ftp_filename = os.path.join(figpath,figfile) | |||
|
995 | self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
996 | self.counter_imagwr = 0 | |||
|
997 | ||||
|
998 | class Noise(Figure): | |||
|
999 | ||||
|
1000 | isConfig = None | |||
|
1001 | __nsubplots = None | |||
|
1002 | ||||
|
1003 | PREFIX = 'noise' | |||
|
1004 | ||||
|
1005 | def __init__(self): | |||
|
1006 | ||||
|
1007 | self.timerange = 24*60*60 | |||
|
1008 | self.isConfig = False | |||
|
1009 | self.__nsubplots = 1 | |||
|
1010 | self.counter_imagwr = 0 | |||
|
1011 | self.WIDTH = 600 | |||
|
1012 | self.HEIGHT = 300 | |||
|
1013 | self.WIDTHPROF = 120 | |||
|
1014 | self.HEIGHTPROF = 0 | |||
|
1015 | self.xdata = None | |||
|
1016 | self.ydata = None | |||
|
1017 | ||||
|
1018 | self.PLOT_CODE = 77 | |||
|
1019 | self.FTP_WEI = None | |||
|
1020 | self.EXP_CODE = None | |||
|
1021 | self.SUB_EXP_CODE = None | |||
|
1022 | self.PLOT_POS = None | |||
|
1023 | ||||
|
1024 | def getSubplots(self): | |||
|
1025 | ||||
|
1026 | ncol = 1 | |||
|
1027 | nrow = 1 | |||
|
1028 | ||||
|
1029 | return nrow, ncol | |||
|
1030 | ||||
|
1031 | def openfile(self, filename): | |||
|
1032 | f = open(filename,'w+') | |||
|
1033 | f.write('\n\n') | |||
|
1034 | f.write('JICAMARCA RADIO OBSERVATORY - Noise \n') | |||
|
1035 | f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' ) | |||
|
1036 | f.close() | |||
|
1037 | ||||
|
1038 | def save_data(self, filename_phase, data, data_datetime): | |||
|
1039 | f=open(filename_phase,'a') | |||
|
1040 | timetuple_data = data_datetime.timetuple() | |||
|
1041 | day = str(timetuple_data.tm_mday) | |||
|
1042 | month = str(timetuple_data.tm_mon) | |||
|
1043 | year = str(timetuple_data.tm_year) | |||
|
1044 | hour = str(timetuple_data.tm_hour) | |||
|
1045 | minute = str(timetuple_data.tm_min) | |||
|
1046 | second = str(timetuple_data.tm_sec) | |||
|
1047 | f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n') | |||
|
1048 | f.close() | |||
|
1049 | ||||
|
1050 | ||||
|
1051 | def setup(self, id, nplots, wintitle, showprofile=True, show=True): | |||
|
1052 | ||||
|
1053 | self.__showprofile = showprofile | |||
|
1054 | self.nplots = nplots | |||
|
1055 | ||||
|
1056 | ncolspan = 7 | |||
|
1057 | colspan = 6 | |||
|
1058 | self.__nsubplots = 2 | |||
|
1059 | ||||
|
1060 | self.createFigure(id = id, | |||
|
1061 | wintitle = wintitle, | |||
|
1062 | widthplot = self.WIDTH+self.WIDTHPROF, | |||
|
1063 | heightplot = self.HEIGHT+self.HEIGHTPROF, | |||
|
1064 | show=show) | |||
|
1065 | ||||
|
1066 | nrow, ncol = self.getSubplots() | |||
|
1067 | ||||
|
1068 | self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1) | |||
|
1069 | ||||
|
1070 | ||||
|
1071 | def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True', | |||
|
1072 | xmin=None, xmax=None, ymin=None, ymax=None, | |||
|
1073 | timerange=None, | |||
|
1074 | save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1, | |||
|
1075 | server=None, folder=None, username=None, password=None, | |||
|
1076 | ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0): | |||
|
1077 | ||||
|
1078 | if channelList == None: | |||
|
1079 | channelIndexList = dataOut.channelIndexList | |||
|
1080 | channelList = dataOut.channelList | |||
|
1081 | else: | |||
|
1082 | channelIndexList = [] | |||
|
1083 | for channel in channelList: | |||
|
1084 | if channel not in dataOut.channelList: | |||
|
1085 | raise ValueError, "Channel %d is not in dataOut.channelList" | |||
|
1086 | channelIndexList.append(dataOut.channelList.index(channel)) | |||
|
1087 | ||||
|
1088 | if timerange != None: | |||
|
1089 | self.timerange = timerange | |||
|
1090 | ||||
|
1091 | tmin = None | |||
|
1092 | tmax = None | |||
|
1093 | x = dataOut.getTimeRange() | |||
|
1094 | y = dataOut.getHeiRange() | |||
|
1095 | factor = dataOut.normFactor | |||
|
1096 | noise = dataOut.noise()/factor | |||
|
1097 | noisedB = 10*numpy.log10(noise) | |||
|
1098 | ||||
|
1099 | #thisDatetime = dataOut.datatime | |||
|
1100 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
1101 | title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y")) | |||
|
1102 | xlabel = "" | |||
|
1103 | ylabel = "Intensity (dB)" | |||
|
1104 | ||||
|
1105 | if not self.isConfig: | |||
|
1106 | ||||
|
1107 | nplots = 1 | |||
|
1108 | ||||
|
1109 | self.setup(id=id, | |||
|
1110 | nplots=nplots, | |||
|
1111 | wintitle=wintitle, | |||
|
1112 | showprofile=showprofile, | |||
|
1113 | show=show) | |||
|
1114 | ||||
|
1115 | tmin, tmax = self.getTimeLim(x, xmin, xmax) | |||
|
1116 | if ymin == None: ymin = numpy.nanmin(noisedB) - 10.0 | |||
|
1117 | if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0 | |||
|
1118 | ||||
|
1119 | self.FTP_WEI = ftp_wei | |||
|
1120 | self.EXP_CODE = exp_code | |||
|
1121 | self.SUB_EXP_CODE = sub_exp_code | |||
|
1122 | self.PLOT_POS = plot_pos | |||
|
1123 | ||||
|
1124 | ||||
|
1125 | self.name = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
1126 | self.isConfig = True | |||
|
1127 | ||||
|
1128 | self.xdata = numpy.array([]) | |||
|
1129 | self.ydata = numpy.array([]) | |||
|
1130 | ||||
|
1131 | #open file beacon phase | |||
|
1132 | path = '%s%03d' %(self.PREFIX, self.id) | |||
|
1133 | noise_file = os.path.join(path,'%s.txt'%self.name) | |||
|
1134 | self.filename_noise = os.path.join(figpath,noise_file) | |||
|
1135 | self.openfile(self.filename_noise) | |||
|
1136 | ||||
|
1137 | ||||
|
1138 | #store data beacon phase | |||
|
1139 | self.save_data(self.filename_noise, noisedB, thisDatetime) | |||
|
1140 | ||||
|
1141 | ||||
|
1142 | self.setWinTitle(title) | |||
|
1143 | ||||
|
1144 | ||||
|
1145 | title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S")) | |||
|
1146 | ||||
|
1147 | legendlabels = ["channel %d"%(idchannel+1) for idchannel in channelList] | |||
|
1148 | axes = self.axesList[0] | |||
|
1149 | ||||
|
1150 | self.xdata = numpy.hstack((self.xdata, x[0:1])) | |||
|
1151 | ||||
|
1152 | if len(self.ydata)==0: | |||
|
1153 | self.ydata = noisedB[channelIndexList].reshape(-1,1) | |||
|
1154 | else: | |||
|
1155 | self.ydata = numpy.hstack((self.ydata, noisedB[channelIndexList].reshape(-1,1))) | |||
|
1156 | ||||
|
1157 | ||||
|
1158 | axes.pmultilineyaxis(x=self.xdata, y=self.ydata, | |||
|
1159 | xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, | |||
|
1160 | xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid", | |||
|
1161 | XAxisAsTime=True, grid='both' | |||
|
1162 | ) | |||
|
1163 | ||||
|
1164 | self.draw() | |||
|
1165 | ||||
|
1166 | # if save: | |||
|
1167 | # | |||
|
1168 | # if figfile == None: | |||
|
1169 | # figfile = self.getFilename(name = self.name) | |||
|
1170 | # | |||
|
1171 | # self.saveFigure(figpath, figfile) | |||
|
1172 | ||||
|
1173 | if save: | |||
|
1174 | ||||
|
1175 | self.counter_imagwr += 1 | |||
|
1176 | if (self.counter_imagwr==wr_period): | |||
|
1177 | if figfile == None: | |||
|
1178 | figfile = self.getFilename(name = self.name) | |||
|
1179 | self.saveFigure(figpath, figfile) | |||
|
1180 | ||||
|
1181 | if ftp: | |||
|
1182 | #provisionalmente envia archivos en el formato de la web en tiempo real | |||
|
1183 | name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS) | |||
|
1184 | path = '%s%03d' %(self.PREFIX, self.id) | |||
|
1185 | ftp_file = os.path.join(path,'ftp','%s.png'%name) | |||
|
1186 | self.saveFigure(figpath, ftp_file) | |||
|
1187 | ftp_filename = os.path.join(figpath,ftp_file) | |||
|
1188 | self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
1189 | self.counter_imagwr = 0 | |||
|
1190 | ||||
|
1191 | self.counter_imagwr = 0 | |||
|
1192 | ||||
|
1193 | if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax: | |||
|
1194 | self.isConfig = False | |||
|
1195 | del self.xdata | |||
|
1196 | del self.ydata |
@@ -0,0 +1,186 | |||||
|
1 | ''' | |||
|
2 | @author: Daniel Suarez | |||
|
3 | ''' | |||
|
4 | ||||
|
5 | import datetime | |||
|
6 | import numpy | |||
|
7 | ||||
|
8 | from figure import Figure | |||
|
9 | ||||
|
10 | class Scope(Figure): | |||
|
11 | ||||
|
12 | isConfig = None | |||
|
13 | ||||
|
14 | def __init__(self): | |||
|
15 | ||||
|
16 | self.isConfig = False | |||
|
17 | self.WIDTH = 300 | |||
|
18 | self.HEIGHT = 200 | |||
|
19 | self.counter_imagwr = 0 | |||
|
20 | ||||
|
21 | def getSubplots(self): | |||
|
22 | ||||
|
23 | nrow = self.nplots | |||
|
24 | ncol = 3 | |||
|
25 | return nrow, ncol | |||
|
26 | ||||
|
27 | def setup(self, id, nplots, wintitle, show): | |||
|
28 | ||||
|
29 | self.nplots = nplots | |||
|
30 | ||||
|
31 | self.createFigure(id=id, | |||
|
32 | wintitle=wintitle, | |||
|
33 | show=show) | |||
|
34 | ||||
|
35 | nrow,ncol = self.getSubplots() | |||
|
36 | colspan = 3 | |||
|
37 | rowspan = 1 | |||
|
38 | ||||
|
39 | for i in range(nplots): | |||
|
40 | self.addAxes(nrow, ncol, i, 0, colspan, rowspan) | |||
|
41 | ||||
|
42 | def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax): | |||
|
43 | yreal = y[channelIndexList,:].real | |||
|
44 | yimag = y[channelIndexList,:].imag | |||
|
45 | ||||
|
46 | title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
47 | xlabel = "Range (Km)" | |||
|
48 | ylabel = "Intensity - IQ" | |||
|
49 | ||||
|
50 | if not self.isConfig: | |||
|
51 | nplots = len(channelIndexList) | |||
|
52 | ||||
|
53 | self.setup(id=id, | |||
|
54 | nplots=nplots, | |||
|
55 | wintitle='', | |||
|
56 | show=show) | |||
|
57 | ||||
|
58 | if xmin == None: xmin = numpy.nanmin(x) | |||
|
59 | if xmax == None: xmax = numpy.nanmax(x) | |||
|
60 | if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag)) | |||
|
61 | if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag)) | |||
|
62 | ||||
|
63 | self.isConfig = True | |||
|
64 | ||||
|
65 | self.setWinTitle(title) | |||
|
66 | ||||
|
67 | for i in range(len(self.axesList)): | |||
|
68 | title = "Channel %d" %(i) | |||
|
69 | axes = self.axesList[i] | |||
|
70 | ||||
|
71 | axes.pline(x, yreal[i,:], | |||
|
72 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, | |||
|
73 | xlabel=xlabel, ylabel=ylabel, title=title) | |||
|
74 | ||||
|
75 | axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2) | |||
|
76 | ||||
|
77 | def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax): | |||
|
78 | y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:]) | |||
|
79 | yreal = y.real | |||
|
80 | ||||
|
81 | title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S")) | |||
|
82 | xlabel = "Range (Km)" | |||
|
83 | ylabel = "Intensity" | |||
|
84 | ||||
|
85 | if not self.isConfig: | |||
|
86 | nplots = len(channelIndexList) | |||
|
87 | ||||
|
88 | self.setup(id=id, | |||
|
89 | nplots=nplots, | |||
|
90 | wintitle='', | |||
|
91 | show=show) | |||
|
92 | ||||
|
93 | if xmin == None: xmin = numpy.nanmin(x) | |||
|
94 | if xmax == None: xmax = numpy.nanmax(x) | |||
|
95 | if ymin == None: ymin = numpy.nanmin(yreal) | |||
|
96 | if ymax == None: ymax = numpy.nanmax(yreal) | |||
|
97 | ||||
|
98 | self.isConfig = True | |||
|
99 | ||||
|
100 | self.setWinTitle(title) | |||
|
101 | ||||
|
102 | for i in range(len(self.axesList)): | |||
|
103 | title = "Channel %d" %(i) | |||
|
104 | axes = self.axesList[i] | |||
|
105 | ychannel = yreal[i,:] | |||
|
106 | axes.pline(x, ychannel, | |||
|
107 | xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, | |||
|
108 | xlabel=xlabel, ylabel=ylabel, title=title) | |||
|
109 | ||||
|
110 | ||||
|
111 | def run(self, dataOut, id, wintitle="", channelList=None, | |||
|
112 | xmin=None, xmax=None, ymin=None, ymax=None, save=False, | |||
|
113 | figpath='./', figfile=None, show=True, wr_period=1, | |||
|
114 | server=None, folder=None, username=None, password=None, type='power'): | |||
|
115 | ||||
|
116 | """ | |||
|
117 | ||||
|
118 | Input: | |||
|
119 | dataOut : | |||
|
120 | id : | |||
|
121 | wintitle : | |||
|
122 | channelList : | |||
|
123 | xmin : None, | |||
|
124 | xmax : None, | |||
|
125 | ymin : None, | |||
|
126 | ymax : None, | |||
|
127 | """ | |||
|
128 | if dataOut.flagNoData: | |||
|
129 | return None | |||
|
130 | ||||
|
131 | if channelList == None: | |||
|
132 | channelIndexList = dataOut.channelIndexList | |||
|
133 | else: | |||
|
134 | channelIndexList = [] | |||
|
135 | for channel in channelList: | |||
|
136 | if channel not in dataOut.channelList: | |||
|
137 | raise ValueError, "Channel %d is not in dataOut.channelList" | |||
|
138 | channelIndexList.append(dataOut.channelList.index(channel)) | |||
|
139 | ||||
|
140 | x = dataOut.heightList | |||
|
141 | y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:]) | |||
|
142 | y = y.real | |||
|
143 | ||||
|
144 | thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1]) | |||
|
145 | ||||
|
146 | if type == "power": | |||
|
147 | self.plot_power(dataOut.heightList, | |||
|
148 | dataOut.data, | |||
|
149 | id, | |||
|
150 | channelIndexList, | |||
|
151 | thisDatetime, | |||
|
152 | wintitle, | |||
|
153 | show, | |||
|
154 | xmin, | |||
|
155 | xmax, | |||
|
156 | ymin, | |||
|
157 | ymax) | |||
|
158 | ||||
|
159 | if type == "iq": | |||
|
160 | self.plot_iq(dataOut.heightList, | |||
|
161 | dataOut.data, | |||
|
162 | id, | |||
|
163 | channelIndexList, | |||
|
164 | thisDatetime, | |||
|
165 | wintitle, | |||
|
166 | show, | |||
|
167 | xmin, | |||
|
168 | xmax, | |||
|
169 | ymin, | |||
|
170 | ymax) | |||
|
171 | ||||
|
172 | ||||
|
173 | self.draw() | |||
|
174 | ||||
|
175 | if save: | |||
|
176 | date = thisDatetime.strftime("%Y%m%d_%H%M%S") | |||
|
177 | if figfile == None: | |||
|
178 | figfile = self.getFilename(name = date) | |||
|
179 | ||||
|
180 | self.saveFigure(figpath, figfile) | |||
|
181 | ||||
|
182 | self.counter_imagwr += 1 | |||
|
183 | if (ftp and (self.counter_imagwr==wr_period)): | |||
|
184 | ftp_filename = os.path.join(figpath,figfile) | |||
|
185 | self.sendByFTP_Thread(ftp_filename, server, folder, username, password) | |||
|
186 | self.counter_imagwr = 0 |
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
This diff has been collapsed as it changes many lines, (1334 lines changed) Show them Hide them | |||||
@@ -0,0 +1,1334 | |||||
|
1 | ''' | |||
|
2 | ||||
|
3 | ''' | |||
|
4 | import os | |||
|
5 | import sys | |||
|
6 | import glob | |||
|
7 | import time | |||
|
8 | import numpy | |||
|
9 | import fnmatch | |||
|
10 | import time, datetime | |||
|
11 | import h5py | |||
|
12 | import traceback | |||
|
13 | ||||
|
14 | #try: | |||
|
15 | # import pyfits | |||
|
16 | #except: | |||
|
17 | # print "pyfits module has not been imported, it should be installed to save files in fits format" | |||
|
18 | ||||
|
19 | #from jrodata import * | |||
|
20 | #from jroheaderIO import * | |||
|
21 | #from jroprocessing import * | |||
|
22 | ||||
|
23 | #import re | |||
|
24 | #from xml.etree.ElementTree import Element, SubElement, ElementTree | |||
|
25 | ||||
|
26 | ||||
|
27 | LOCALTIME = True #-18000 | |||
|
28 | ||||
|
29 | from model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader | |||
|
30 | ||||
|
31 | def isNumber(str): | |||
|
32 | """ | |||
|
33 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. | |||
|
34 | ||||
|
35 | Excepciones: | |||
|
36 | Si un determinado string no puede ser convertido a numero | |||
|
37 | Input: | |||
|
38 | str, string al cual se le analiza para determinar si convertible a un numero o no | |||
|
39 | ||||
|
40 | Return: | |||
|
41 | True : si el string es uno numerico | |||
|
42 | False : no es un string numerico | |||
|
43 | """ | |||
|
44 | try: | |||
|
45 | float( str ) | |||
|
46 | return True | |||
|
47 | except: | |||
|
48 | return False | |||
|
49 | ||||
|
50 | def isThisFileinRange(filename, startUTSeconds, endUTSeconds): | |||
|
51 | """ | |||
|
52 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. | |||
|
53 | ||||
|
54 | Inputs: | |||
|
55 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) | |||
|
56 | ||||
|
57 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en | |||
|
58 | segundos contados desde 01/01/1970. | |||
|
59 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en | |||
|
60 | segundos contados desde 01/01/1970. | |||
|
61 | ||||
|
62 | Return: | |||
|
63 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |||
|
64 | fecha especificado, de lo contrario retorna False. | |||
|
65 | ||||
|
66 | Excepciones: | |||
|
67 | Si el archivo no existe o no puede ser abierto | |||
|
68 | Si la cabecera no puede ser leida. | |||
|
69 | ||||
|
70 | """ | |||
|
71 | basicHeaderObj = BasicHeader(LOCALTIME) | |||
|
72 | ||||
|
73 | try: | |||
|
74 | fp = open(filename,'rb') | |||
|
75 | except IOError: | |||
|
76 | traceback.print_exc() | |||
|
77 | raise IOError, "The file %s can't be opened" %(filename) | |||
|
78 | ||||
|
79 | sts = basicHeaderObj.read(fp) | |||
|
80 | fp.close() | |||
|
81 | ||||
|
82 | if not(sts): | |||
|
83 | print "Skipping the file %s because it has not a valid header" %(filename) | |||
|
84 | return 0 | |||
|
85 | ||||
|
86 | if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)): | |||
|
87 | return 0 | |||
|
88 | ||||
|
89 | return 1 | |||
|
90 | ||||
|
91 | def isFileinThisTime(filename, startTime, endTime): | |||
|
92 | """ | |||
|
93 | Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado. | |||
|
94 | ||||
|
95 | Inputs: | |||
|
96 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) | |||
|
97 | ||||
|
98 | startTime : tiempo inicial del rango seleccionado en formato datetime.time | |||
|
99 | ||||
|
100 | endTime : tiempo final del rango seleccionado en formato datetime.time | |||
|
101 | ||||
|
102 | Return: | |||
|
103 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de | |||
|
104 | fecha especificado, de lo contrario retorna False. | |||
|
105 | ||||
|
106 | Excepciones: | |||
|
107 | Si el archivo no existe o no puede ser abierto | |||
|
108 | Si la cabecera no puede ser leida. | |||
|
109 | ||||
|
110 | """ | |||
|
111 | ||||
|
112 | ||||
|
113 | try: | |||
|
114 | fp = open(filename,'rb') | |||
|
115 | except IOError: | |||
|
116 | traceback.print_exc() | |||
|
117 | raise IOError, "The file %s can't be opened" %(filename) | |||
|
118 | ||||
|
119 | basicHeaderObj = BasicHeader(LOCALTIME) | |||
|
120 | sts = basicHeaderObj.read(fp) | |||
|
121 | fp.close() | |||
|
122 | ||||
|
123 | thisDatetime = basicHeaderObj.datatime | |||
|
124 | thisTime = thisDatetime.time() | |||
|
125 | ||||
|
126 | if not(sts): | |||
|
127 | print "Skipping the file %s because it has not a valid header" %(filename) | |||
|
128 | return None | |||
|
129 | ||||
|
130 | if not ((startTime <= thisTime) and (endTime > thisTime)): | |||
|
131 | return None | |||
|
132 | ||||
|
133 | return thisDatetime | |||
|
134 | ||||
|
135 | def getFileFromSet(path, ext, set): | |||
|
136 | validFilelist = [] | |||
|
137 | fileList = os.listdir(path) | |||
|
138 | ||||
|
139 | # 0 1234 567 89A BCDE | |||
|
140 | # H YYYY DDD SSS .ext | |||
|
141 | ||||
|
142 | for thisFile in fileList: | |||
|
143 | try: | |||
|
144 | year = int(thisFile[1:5]) | |||
|
145 | doy = int(thisFile[5:8]) | |||
|
146 | except: | |||
|
147 | continue | |||
|
148 | ||||
|
149 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): | |||
|
150 | continue | |||
|
151 | ||||
|
152 | validFilelist.append(thisFile) | |||
|
153 | ||||
|
154 | myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set)) | |||
|
155 | ||||
|
156 | if len(myfile)!= 0: | |||
|
157 | return myfile[0] | |||
|
158 | else: | |||
|
159 | filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower()) | |||
|
160 | print 'the filename %s does not exist'%filename | |||
|
161 | print '...going to the last file: ' | |||
|
162 | ||||
|
163 | if validFilelist: | |||
|
164 | validFilelist = sorted( validFilelist, key=str.lower ) | |||
|
165 | return validFilelist[-1] | |||
|
166 | ||||
|
167 | return None | |||
|
168 | ||||
|
169 | def getlastFileFromPath(path, ext): | |||
|
170 | """ | |||
|
171 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" | |||
|
172 | al final de la depuracion devuelve el ultimo file de la lista que quedo. | |||
|
173 | ||||
|
174 | Input: | |||
|
175 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta | |||
|
176 | ext : extension de los files contenidos en una carpeta | |||
|
177 | ||||
|
178 | Return: | |||
|
179 | El ultimo file de una determinada carpeta, no se considera el path. | |||
|
180 | """ | |||
|
181 | validFilelist = [] | |||
|
182 | fileList = os.listdir(path) | |||
|
183 | ||||
|
184 | # 0 1234 567 89A BCDE | |||
|
185 | # H YYYY DDD SSS .ext | |||
|
186 | ||||
|
187 | for thisFile in fileList: | |||
|
188 | ||||
|
189 | year = thisFile[1:5] | |||
|
190 | if not isNumber(year): | |||
|
191 | continue | |||
|
192 | ||||
|
193 | doy = thisFile[5:8] | |||
|
194 | if not isNumber(doy): | |||
|
195 | continue | |||
|
196 | ||||
|
197 | year = int(year) | |||
|
198 | doy = int(doy) | |||
|
199 | ||||
|
200 | if (os.path.splitext(thisFile)[-1].lower() != ext.lower()): | |||
|
201 | continue | |||
|
202 | ||||
|
203 | validFilelist.append(thisFile) | |||
|
204 | ||||
|
205 | if validFilelist: | |||
|
206 | validFilelist = sorted( validFilelist, key=str.lower ) | |||
|
207 | return validFilelist[-1] | |||
|
208 | ||||
|
209 | return None | |||
|
210 | ||||
|
211 | def checkForRealPath(path, foldercounter, year, doy, set, ext): | |||
|
212 | """ | |||
|
213 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, | |||
|
214 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar | |||
|
215 | el path exacto de un determinado file. | |||
|
216 | ||||
|
217 | Example : | |||
|
218 | nombre correcto del file es .../.../D2009307/P2009307367.ext | |||
|
219 | ||||
|
220 | Entonces la funcion prueba con las siguientes combinaciones | |||
|
221 | .../.../y2009307367.ext | |||
|
222 | .../.../Y2009307367.ext | |||
|
223 | .../.../x2009307/y2009307367.ext | |||
|
224 | .../.../x2009307/Y2009307367.ext | |||
|
225 | .../.../X2009307/y2009307367.ext | |||
|
226 | .../.../X2009307/Y2009307367.ext | |||
|
227 | siendo para este caso, la ultima combinacion de letras, identica al file buscado | |||
|
228 | ||||
|
229 | Return: | |||
|
230 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file | |||
|
231 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas | |||
|
232 | para el filename | |||
|
233 | """ | |||
|
234 | fullfilename = None | |||
|
235 | find_flag = False | |||
|
236 | filename = None | |||
|
237 | ||||
|
238 | prefixDirList = [None,'d','D'] | |||
|
239 | if ext.lower() == ".r": #voltage | |||
|
240 | prefixFileList = ['d','D'] | |||
|
241 | elif ext.lower() == ".pdata": #spectra | |||
|
242 | prefixFileList = ['p','P'] | |||
|
243 | else: | |||
|
244 | return None, filename | |||
|
245 | ||||
|
246 | #barrido por las combinaciones posibles | |||
|
247 | for prefixDir in prefixDirList: | |||
|
248 | thispath = path | |||
|
249 | if prefixDir != None: | |||
|
250 | #formo el nombre del directorio xYYYYDDD (x=d o x=D) | |||
|
251 | if foldercounter == 0: | |||
|
252 | thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy )) | |||
|
253 | else: | |||
|
254 | thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter)) | |||
|
255 | for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D" | |||
|
256 | filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext | |||
|
257 | fullfilename = os.path.join( thispath, filename ) #formo el path completo | |||
|
258 | ||||
|
259 | if os.path.exists( fullfilename ): #verifico que exista | |||
|
260 | find_flag = True | |||
|
261 | break | |||
|
262 | if find_flag: | |||
|
263 | break | |||
|
264 | ||||
|
265 | if not(find_flag): | |||
|
266 | return None, filename | |||
|
267 | ||||
|
268 | return fullfilename, filename | |||
|
269 | ||||
|
270 | def isDoyFolder(folder): | |||
|
271 | try: | |||
|
272 | year = int(folder[1:5]) | |||
|
273 | except: | |||
|
274 | return 0 | |||
|
275 | ||||
|
276 | try: | |||
|
277 | doy = int(folder[5:8]) | |||
|
278 | except: | |||
|
279 | return 0 | |||
|
280 | ||||
|
281 | return 1 | |||
|
282 | ||||
|
283 | class JRODataIO: | |||
|
284 | ||||
|
285 | c = 3E8 | |||
|
286 | ||||
|
287 | isConfig = False | |||
|
288 | ||||
|
289 | basicHeaderObj = None | |||
|
290 | ||||
|
291 | systemHeaderObj = None | |||
|
292 | ||||
|
293 | radarControllerHeaderObj = None | |||
|
294 | ||||
|
295 | processingHeaderObj = None | |||
|
296 | ||||
|
297 | online = 0 | |||
|
298 | ||||
|
299 | dtype = None | |||
|
300 | ||||
|
301 | pathList = [] | |||
|
302 | ||||
|
303 | filenameList = [] | |||
|
304 | ||||
|
305 | filename = None | |||
|
306 | ||||
|
307 | ext = None | |||
|
308 | ||||
|
309 | flagIsNewFile = 1 | |||
|
310 | ||||
|
311 | flagTimeBlock = 0 | |||
|
312 | ||||
|
313 | flagIsNewBlock = 0 | |||
|
314 | ||||
|
315 | fp = None | |||
|
316 | ||||
|
317 | firstHeaderSize = 0 | |||
|
318 | ||||
|
319 | basicHeaderSize = 24 | |||
|
320 | ||||
|
321 | versionFile = 1103 | |||
|
322 | ||||
|
323 | fileSize = None | |||
|
324 | ||||
|
325 | # ippSeconds = None | |||
|
326 | ||||
|
327 | fileSizeByHeader = None | |||
|
328 | ||||
|
329 | fileIndex = None | |||
|
330 | ||||
|
331 | profileIndex = None | |||
|
332 | ||||
|
333 | blockIndex = None | |||
|
334 | ||||
|
335 | nTotalBlocks = None | |||
|
336 | ||||
|
337 | maxTimeStep = 30 | |||
|
338 | ||||
|
339 | lastUTTime = None | |||
|
340 | ||||
|
341 | datablock = None | |||
|
342 | ||||
|
343 | dataOut = None | |||
|
344 | ||||
|
345 | blocksize = None | |||
|
346 | ||||
|
347 | def __init__(self): | |||
|
348 | ||||
|
349 | raise ValueError, "Not implemented" | |||
|
350 | ||||
|
351 | def run(self): | |||
|
352 | ||||
|
353 | raise ValueError, "Not implemented" | |||
|
354 | ||||
|
355 | class JRODataReader(JRODataIO): | |||
|
356 | ||||
|
357 | nReadBlocks = 0 | |||
|
358 | ||||
|
359 | delay = 10 #number of seconds waiting a new file | |||
|
360 | ||||
|
361 | nTries = 3 #quantity tries | |||
|
362 | ||||
|
363 | nFiles = 3 #number of files for searching | |||
|
364 | ||||
|
365 | path = None | |||
|
366 | ||||
|
367 | foldercounter = 0 | |||
|
368 | ||||
|
369 | flagNoMoreFiles = 0 | |||
|
370 | ||||
|
371 | datetimeList = [] | |||
|
372 | ||||
|
373 | __isFirstTimeOnline = 1 | |||
|
374 | ||||
|
375 | __printInfo = True | |||
|
376 | ||||
|
377 | profileIndex = None | |||
|
378 | ||||
|
379 | def __init__(self): | |||
|
380 | ||||
|
381 | """ | |||
|
382 | ||||
|
383 | """ | |||
|
384 | ||||
|
385 | raise ValueError, "This method has not been implemented" | |||
|
386 | ||||
|
387 | ||||
|
388 | def createObjByDefault(self): | |||
|
389 | """ | |||
|
390 | ||||
|
391 | """ | |||
|
392 | raise ValueError, "This method has not been implemented" | |||
|
393 | ||||
|
394 | def getBlockDimension(self): | |||
|
395 | ||||
|
396 | raise ValueError, "No implemented" | |||
|
397 | ||||
|
398 | def __searchFilesOffLine(self, | |||
|
399 | path, | |||
|
400 | startDate, | |||
|
401 | endDate, | |||
|
402 | startTime=datetime.time(0,0,0), | |||
|
403 | endTime=datetime.time(23,59,59), | |||
|
404 | set=None, | |||
|
405 | expLabel='', | |||
|
406 | ext='.r', | |||
|
407 | walk=True): | |||
|
408 | ||||
|
409 | pathList = [] | |||
|
410 | ||||
|
411 | if not walk: | |||
|
412 | #pathList.append(path) | |||
|
413 | multi_path = path.split(',') | |||
|
414 | for single_path in multi_path: | |||
|
415 | pathList.append(single_path) | |||
|
416 | ||||
|
417 | else: | |||
|
418 | #dirList = [] | |||
|
419 | multi_path = path.split(',') | |||
|
420 | for single_path in multi_path: | |||
|
421 | dirList = [] | |||
|
422 | for thisPath in os.listdir(single_path): | |||
|
423 | if not os.path.isdir(os.path.join(single_path,thisPath)): | |||
|
424 | continue | |||
|
425 | if not isDoyFolder(thisPath): | |||
|
426 | continue | |||
|
427 | ||||
|
428 | dirList.append(thisPath) | |||
|
429 | ||||
|
430 | if not(dirList): | |||
|
431 | return None, None | |||
|
432 | ||||
|
433 | thisDate = startDate | |||
|
434 | ||||
|
435 | while(thisDate <= endDate): | |||
|
436 | year = thisDate.timetuple().tm_year | |||
|
437 | doy = thisDate.timetuple().tm_yday | |||
|
438 | ||||
|
439 | matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*') | |||
|
440 | if len(matchlist) == 0: | |||
|
441 | thisDate += datetime.timedelta(1) | |||
|
442 | continue | |||
|
443 | for match in matchlist: | |||
|
444 | pathList.append(os.path.join(single_path,match,expLabel)) | |||
|
445 | ||||
|
446 | thisDate += datetime.timedelta(1) | |||
|
447 | ||||
|
448 | if pathList == []: | |||
|
449 | print "Any folder was found for the date range: %s-%s" %(startDate, endDate) | |||
|
450 | return None, None | |||
|
451 | ||||
|
452 | print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate) | |||
|
453 | ||||
|
454 | filenameList = [] | |||
|
455 | datetimeList = [] | |||
|
456 | pathDict = {} | |||
|
457 | filenameList_to_sort = [] | |||
|
458 | ||||
|
459 | for i in range(len(pathList)): | |||
|
460 | ||||
|
461 | thisPath = pathList[i] | |||
|
462 | ||||
|
463 | fileList = glob.glob1(thisPath, "*%s" %ext) | |||
|
464 | fileList.sort() | |||
|
465 | pathDict.setdefault(fileList[0]) | |||
|
466 | pathDict[fileList[0]] = i | |||
|
467 | filenameList_to_sort.append(fileList[0]) | |||
|
468 | ||||
|
469 | filenameList_to_sort.sort() | |||
|
470 | ||||
|
471 | for file in filenameList_to_sort: | |||
|
472 | thisPath = pathList[pathDict[file]] | |||
|
473 | ||||
|
474 | fileList = glob.glob1(thisPath, "*%s" %ext) | |||
|
475 | fileList.sort() | |||
|
476 | ||||
|
477 | for file in fileList: | |||
|
478 | ||||
|
479 | filename = os.path.join(thisPath,file) | |||
|
480 | thisDatetime = isFileinThisTime(filename, startTime, endTime) | |||
|
481 | ||||
|
482 | if not(thisDatetime): | |||
|
483 | continue | |||
|
484 | ||||
|
485 | filenameList.append(filename) | |||
|
486 | datetimeList.append(thisDatetime) | |||
|
487 | ||||
|
488 | if not(filenameList): | |||
|
489 | print "Any file was found for the time range %s - %s" %(startTime, endTime) | |||
|
490 | return None, None | |||
|
491 | ||||
|
492 | print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime) | |||
|
493 | ||||
|
494 | ||||
|
495 | for i in range(len(filenameList)): | |||
|
496 | print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) | |||
|
497 | ||||
|
498 | self.filenameList = filenameList | |||
|
499 | self.datetimeList = datetimeList | |||
|
500 | ||||
|
501 | return pathList, filenameList | |||
|
502 | ||||
|
503 | def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None): | |||
|
504 | ||||
|
505 | """ | |||
|
506 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y | |||
|
507 | devuelve el archivo encontrado ademas de otros datos. | |||
|
508 | ||||
|
509 | Input: | |||
|
510 | path : carpeta donde estan contenidos los files que contiene data | |||
|
511 | ||||
|
512 | expLabel : Nombre del subexperimento (subfolder) | |||
|
513 | ||||
|
514 | ext : extension de los files | |||
|
515 | ||||
|
516 | walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath) | |||
|
517 | ||||
|
518 | Return: | |||
|
519 | directory : eL directorio donde esta el file encontrado | |||
|
520 | filename : el ultimo file de una determinada carpeta | |||
|
521 | year : el anho | |||
|
522 | doy : el numero de dia del anho | |||
|
523 | set : el set del archivo | |||
|
524 | ||||
|
525 | ||||
|
526 | """ | |||
|
527 | dirList = [] | |||
|
528 | ||||
|
529 | if not walk: | |||
|
530 | fullpath = path | |||
|
531 | foldercounter = 0 | |||
|
532 | else: | |||
|
533 | #Filtra solo los directorios | |||
|
534 | for thisPath in os.listdir(path): | |||
|
535 | if not os.path.isdir(os.path.join(path,thisPath)): | |||
|
536 | continue | |||
|
537 | if not isDoyFolder(thisPath): | |||
|
538 | continue | |||
|
539 | ||||
|
540 | dirList.append(thisPath) | |||
|
541 | ||||
|
542 | if not(dirList): | |||
|
543 | return None, None, None, None, None, None | |||
|
544 | ||||
|
545 | dirList = sorted( dirList, key=str.lower ) | |||
|
546 | ||||
|
547 | doypath = dirList[-1] | |||
|
548 | foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0 | |||
|
549 | fullpath = os.path.join(path, doypath, expLabel) | |||
|
550 | ||||
|
551 | ||||
|
552 | print "%s folder was found: " %(fullpath ) | |||
|
553 | ||||
|
554 | if set == None: | |||
|
555 | filename = getlastFileFromPath(fullpath, ext) | |||
|
556 | else: | |||
|
557 | filename = getFileFromSet(fullpath, ext, set) | |||
|
558 | ||||
|
559 | if not(filename): | |||
|
560 | return None, None, None, None, None, None | |||
|
561 | ||||
|
562 | print "%s file was found" %(filename) | |||
|
563 | ||||
|
564 | if not(self.__verifyFile(os.path.join(fullpath, filename))): | |||
|
565 | return None, None, None, None, None, None | |||
|
566 | ||||
|
567 | year = int( filename[1:5] ) | |||
|
568 | doy = int( filename[5:8] ) | |||
|
569 | set = int( filename[8:11] ) | |||
|
570 | ||||
|
571 | return fullpath, foldercounter, filename, year, doy, set | |||
|
572 | ||||
|
573 | def __setNextFileOffline(self): | |||
|
574 | ||||
|
575 | idFile = self.fileIndex | |||
|
576 | ||||
|
577 | while (True): | |||
|
578 | idFile += 1 | |||
|
579 | if not(idFile < len(self.filenameList)): | |||
|
580 | self.flagNoMoreFiles = 1 | |||
|
581 | print "No more Files" | |||
|
582 | return 0 | |||
|
583 | ||||
|
584 | filename = self.filenameList[idFile] | |||
|
585 | ||||
|
586 | if not(self.__verifyFile(filename)): | |||
|
587 | continue | |||
|
588 | ||||
|
589 | fileSize = os.path.getsize(filename) | |||
|
590 | fp = open(filename,'rb') | |||
|
591 | break | |||
|
592 | ||||
|
593 | self.flagIsNewFile = 1 | |||
|
594 | self.fileIndex = idFile | |||
|
595 | self.filename = filename | |||
|
596 | self.fileSize = fileSize | |||
|
597 | self.fp = fp | |||
|
598 | ||||
|
599 | print "Setting the file: %s"%self.filename | |||
|
600 | ||||
|
601 | return 1 | |||
|
602 | ||||
|
603 | def __setNextFileOnline(self): | |||
|
604 | """ | |||
|
605 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si | |||
|
606 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files | |||
|
607 | siguientes. | |||
|
608 | ||||
|
609 | Affected: | |||
|
610 | self.flagIsNewFile | |||
|
611 | self.filename | |||
|
612 | self.fileSize | |||
|
613 | self.fp | |||
|
614 | self.set | |||
|
615 | self.flagNoMoreFiles | |||
|
616 | ||||
|
617 | Return: | |||
|
618 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado | |||
|
619 | 1 : si el file fue abierto con exito y esta listo a ser leido | |||
|
620 | ||||
|
621 | Excepciones: | |||
|
622 | Si un determinado file no puede ser abierto | |||
|
623 | """ | |||
|
624 | nFiles = 0 | |||
|
625 | fileOk_flag = False | |||
|
626 | firstTime_flag = True | |||
|
627 | ||||
|
628 | self.set += 1 | |||
|
629 | ||||
|
630 | if self.set > 999: | |||
|
631 | self.set = 0 | |||
|
632 | self.foldercounter += 1 | |||
|
633 | ||||
|
634 | #busca el 1er file disponible | |||
|
635 | fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) | |||
|
636 | if fullfilename: | |||
|
637 | if self.__verifyFile(fullfilename, False): | |||
|
638 | fileOk_flag = True | |||
|
639 | ||||
|
640 | #si no encuentra un file entonces espera y vuelve a buscar | |||
|
641 | if not(fileOk_flag): | |||
|
642 | for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles | |||
|
643 | ||||
|
644 | if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces | |||
|
645 | tries = self.nTries | |||
|
646 | else: | |||
|
647 | tries = 1 #si no es la 1era vez entonces solo lo hace una vez | |||
|
648 | ||||
|
649 | for nTries in range( tries ): | |||
|
650 | if firstTime_flag: | |||
|
651 | print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 ) | |||
|
652 | time.sleep( self.delay ) | |||
|
653 | else: | |||
|
654 | print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext) | |||
|
655 | ||||
|
656 | fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext ) | |||
|
657 | if fullfilename: | |||
|
658 | if self.__verifyFile(fullfilename): | |||
|
659 | fileOk_flag = True | |||
|
660 | break | |||
|
661 | ||||
|
662 | if fileOk_flag: | |||
|
663 | break | |||
|
664 | ||||
|
665 | firstTime_flag = False | |||
|
666 | ||||
|
667 | print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename | |||
|
668 | self.set += 1 | |||
|
669 | ||||
|
670 | if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta | |||
|
671 | self.set = 0 | |||
|
672 | self.doy += 1 | |||
|
673 | self.foldercounter = 0 | |||
|
674 | ||||
|
675 | if fileOk_flag: | |||
|
676 | self.fileSize = os.path.getsize( fullfilename ) | |||
|
677 | self.filename = fullfilename | |||
|
678 | self.flagIsNewFile = 1 | |||
|
679 | if self.fp != None: self.fp.close() | |||
|
680 | self.fp = open(fullfilename, 'rb') | |||
|
681 | self.flagNoMoreFiles = 0 | |||
|
682 | print 'Setting the file: %s' % fullfilename | |||
|
683 | else: | |||
|
684 | self.fileSize = 0 | |||
|
685 | self.filename = None | |||
|
686 | self.flagIsNewFile = 0 | |||
|
687 | self.fp = None | |||
|
688 | self.flagNoMoreFiles = 1 | |||
|
689 | print 'No more Files' | |||
|
690 | ||||
|
691 | return fileOk_flag | |||
|
692 | ||||
|
693 | def setNextFile(self): | |||
|
694 | if self.fp != None: | |||
|
695 | self.fp.close() | |||
|
696 | ||||
|
697 | if self.online: | |||
|
698 | newFile = self.__setNextFileOnline() | |||
|
699 | else: | |||
|
700 | newFile = self.__setNextFileOffline() | |||
|
701 | ||||
|
702 | if not(newFile): | |||
|
703 | return 0 | |||
|
704 | ||||
|
705 | self.__readFirstHeader() | |||
|
706 | self.nReadBlocks = 0 | |||
|
707 | return 1 | |||
|
708 | ||||
|
709 | def __waitNewBlock(self): | |||
|
710 | """ | |||
|
711 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. | |||
|
712 | ||||
|
713 | Si el modo de lectura es OffLine siempre retorn 0 | |||
|
714 | """ | |||
|
715 | if not self.online: | |||
|
716 | return 0 | |||
|
717 | ||||
|
718 | if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile): | |||
|
719 | return 0 | |||
|
720 | ||||
|
721 | currentPointer = self.fp.tell() | |||
|
722 | ||||
|
723 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |||
|
724 | ||||
|
725 | for nTries in range( self.nTries ): | |||
|
726 | ||||
|
727 | self.fp.close() | |||
|
728 | self.fp = open( self.filename, 'rb' ) | |||
|
729 | self.fp.seek( currentPointer ) | |||
|
730 | ||||
|
731 | self.fileSize = os.path.getsize( self.filename ) | |||
|
732 | currentSize = self.fileSize - currentPointer | |||
|
733 | ||||
|
734 | if ( currentSize >= neededSize ): | |||
|
735 | self.basicHeaderObj.read(self.fp) | |||
|
736 | return 1 | |||
|
737 | ||||
|
738 | if self.fileSize == self.fileSizeByHeader: | |||
|
739 | # self.flagEoF = True | |||
|
740 | return 0 | |||
|
741 | ||||
|
742 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) | |||
|
743 | time.sleep( self.delay ) | |||
|
744 | ||||
|
745 | ||||
|
746 | return 0 | |||
|
747 | ||||
|
748 | def waitDataBlock(self,pointer_location): | |||
|
749 | ||||
|
750 | currentPointer = pointer_location | |||
|
751 | ||||
|
752 | neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize | |||
|
753 | ||||
|
754 | for nTries in range( self.nTries ): | |||
|
755 | self.fp.close() | |||
|
756 | self.fp = open( self.filename, 'rb' ) | |||
|
757 | self.fp.seek( currentPointer ) | |||
|
758 | ||||
|
759 | self.fileSize = os.path.getsize( self.filename ) | |||
|
760 | currentSize = self.fileSize - currentPointer | |||
|
761 | ||||
|
762 | if ( currentSize >= neededSize ): | |||
|
763 | return 1 | |||
|
764 | ||||
|
765 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) | |||
|
766 | time.sleep( self.delay ) | |||
|
767 | ||||
|
768 | return 0 | |||
|
769 | ||||
|
770 | def __jumpToLastBlock(self): | |||
|
771 | ||||
|
772 | if not(self.__isFirstTimeOnline): | |||
|
773 | return | |||
|
774 | ||||
|
775 | csize = self.fileSize - self.fp.tell() | |||
|
776 | blocksize = self.processingHeaderObj.blockSize | |||
|
777 | ||||
|
778 | #salta el primer bloque de datos | |||
|
779 | if csize > self.processingHeaderObj.blockSize: | |||
|
780 | self.fp.seek(self.fp.tell() + blocksize) | |||
|
781 | else: | |||
|
782 | return | |||
|
783 | ||||
|
784 | csize = self.fileSize - self.fp.tell() | |||
|
785 | neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |||
|
786 | while True: | |||
|
787 | ||||
|
788 | if self.fp.tell()<self.fileSize: | |||
|
789 | self.fp.seek(self.fp.tell() + neededsize) | |||
|
790 | else: | |||
|
791 | self.fp.seek(self.fp.tell() - neededsize) | |||
|
792 | break | |||
|
793 | ||||
|
794 | # csize = self.fileSize - self.fp.tell() | |||
|
795 | # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |||
|
796 | # factor = int(csize/neededsize) | |||
|
797 | # if factor > 0: | |||
|
798 | # self.fp.seek(self.fp.tell() + factor*neededsize) | |||
|
799 | ||||
|
800 | self.flagIsNewFile = 0 | |||
|
801 | self.__isFirstTimeOnline = 0 | |||
|
802 | ||||
|
803 | def __setNewBlock(self): | |||
|
804 | ||||
|
805 | if self.fp == None: | |||
|
806 | return 0 | |||
|
807 | ||||
|
808 | if self.online: | |||
|
809 | self.__jumpToLastBlock() | |||
|
810 | ||||
|
811 | if self.flagIsNewFile: | |||
|
812 | return 1 | |||
|
813 | ||||
|
814 | self.lastUTTime = self.basicHeaderObj.utc | |||
|
815 | currentSize = self.fileSize - self.fp.tell() | |||
|
816 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |||
|
817 | ||||
|
818 | if (currentSize >= neededSize): | |||
|
819 | self.basicHeaderObj.read(self.fp) | |||
|
820 | return 1 | |||
|
821 | ||||
|
822 | if self.__waitNewBlock(): | |||
|
823 | return 1 | |||
|
824 | ||||
|
825 | if not(self.setNextFile()): | |||
|
826 | return 0 | |||
|
827 | ||||
|
828 | deltaTime = self.basicHeaderObj.utc - self.lastUTTime # | |||
|
829 | ||||
|
830 | self.flagTimeBlock = 0 | |||
|
831 | ||||
|
832 | if deltaTime > self.maxTimeStep: | |||
|
833 | self.flagTimeBlock = 1 | |||
|
834 | ||||
|
835 | return 1 | |||
|
836 | ||||
|
837 | def readNextBlock(self): | |||
|
838 | if not(self.__setNewBlock()): | |||
|
839 | return 0 | |||
|
840 | ||||
|
841 | if not(self.readBlock()): | |||
|
842 | return 0 | |||
|
843 | ||||
|
844 | return 1 | |||
|
845 | ||||
|
846 | def __readFirstHeader(self): | |||
|
847 | ||||
|
848 | self.basicHeaderObj.read(self.fp) | |||
|
849 | self.systemHeaderObj.read(self.fp) | |||
|
850 | self.radarControllerHeaderObj.read(self.fp) | |||
|
851 | self.processingHeaderObj.read(self.fp) | |||
|
852 | ||||
|
853 | self.firstHeaderSize = self.basicHeaderObj.size | |||
|
854 | ||||
|
855 | datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) | |||
|
856 | if datatype == 0: | |||
|
857 | datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')]) | |||
|
858 | elif datatype == 1: | |||
|
859 | datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')]) | |||
|
860 | elif datatype == 2: | |||
|
861 | datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')]) | |||
|
862 | elif datatype == 3: | |||
|
863 | datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')]) | |||
|
864 | elif datatype == 4: | |||
|
865 | datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
866 | elif datatype == 5: | |||
|
867 | datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')]) | |||
|
868 | else: | |||
|
869 | raise ValueError, 'Data type was not defined' | |||
|
870 | ||||
|
871 | self.dtype = datatype_str | |||
|
872 | #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c | |||
|
873 | self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1) | |||
|
874 | # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels) | |||
|
875 | # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels) | |||
|
876 | self.getBlockDimension() | |||
|
877 | ||||
|
878 | def __verifyFile(self, filename, msgFlag=True): | |||
|
879 | msg = None | |||
|
880 | try: | |||
|
881 | fp = open(filename, 'rb') | |||
|
882 | currentPosition = fp.tell() | |||
|
883 | except IOError: | |||
|
884 | traceback.print_exc() | |||
|
885 | if msgFlag: | |||
|
886 | print "The file %s can't be opened" % (filename) | |||
|
887 | return False | |||
|
888 | ||||
|
889 | neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize | |||
|
890 | ||||
|
891 | if neededSize == 0: | |||
|
892 | basicHeaderObj = BasicHeader(LOCALTIME) | |||
|
893 | systemHeaderObj = SystemHeader() | |||
|
894 | radarControllerHeaderObj = RadarControllerHeader() | |||
|
895 | processingHeaderObj = ProcessingHeader() | |||
|
896 | ||||
|
897 | try: | |||
|
898 | if not( basicHeaderObj.read(fp) ): raise IOError | |||
|
899 | if not( systemHeaderObj.read(fp) ): raise IOError | |||
|
900 | if not( radarControllerHeaderObj.read(fp) ): raise IOError | |||
|
901 | if not( processingHeaderObj.read(fp) ): raise IOError | |||
|
902 | # data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) | |||
|
903 | ||||
|
904 | neededSize = processingHeaderObj.blockSize + basicHeaderObj.size | |||
|
905 | ||||
|
906 | except IOError: | |||
|
907 | traceback.print_exc() | |||
|
908 | if msgFlag: | |||
|
909 | print "\tThe file %s is empty or it hasn't enough data" % filename | |||
|
910 | ||||
|
911 | fp.close() | |||
|
912 | return False | |||
|
913 | else: | |||
|
914 | msg = "\tSkipping the file %s due to it hasn't enough data" %filename | |||
|
915 | ||||
|
916 | fp.close() | |||
|
917 | fileSize = os.path.getsize(filename) | |||
|
918 | currentSize = fileSize - currentPosition | |||
|
919 | if currentSize < neededSize: | |||
|
920 | if msgFlag and (msg != None): | |||
|
921 | print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename | |||
|
922 | return False | |||
|
923 | ||||
|
924 | return True | |||
|
925 | ||||
|
926 | def setup(self, | |||
|
927 | path=None, | |||
|
928 | startDate=None, | |||
|
929 | endDate=None, | |||
|
930 | startTime=datetime.time(0,0,0), | |||
|
931 | endTime=datetime.time(23,59,59), | |||
|
932 | set=None, | |||
|
933 | expLabel = "", | |||
|
934 | ext = None, | |||
|
935 | online = False, | |||
|
936 | delay = 60, | |||
|
937 | walk = True): | |||
|
938 | ||||
|
939 | if path == None: | |||
|
940 | raise ValueError, "The path is not valid" | |||
|
941 | ||||
|
942 | if ext == None: | |||
|
943 | ext = self.ext | |||
|
944 | ||||
|
945 | if online: | |||
|
946 | print "Searching files in online mode..." | |||
|
947 | ||||
|
948 | for nTries in range( self.nTries ): | |||
|
949 | fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set) | |||
|
950 | ||||
|
951 | if fullpath: | |||
|
952 | break | |||
|
953 | ||||
|
954 | print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) | |||
|
955 | time.sleep( self.delay ) | |||
|
956 | ||||
|
957 | if not(fullpath): | |||
|
958 | print "There 'isn't valied files in %s" % path | |||
|
959 | return None | |||
|
960 | ||||
|
961 | self.year = year | |||
|
962 | self.doy = doy | |||
|
963 | self.set = set - 1 | |||
|
964 | self.path = path | |||
|
965 | self.foldercounter = foldercounter | |||
|
966 | last_set = None | |||
|
967 | ||||
|
968 | else: | |||
|
969 | print "Searching files in offline mode ..." | |||
|
970 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |||
|
971 | startTime=startTime, endTime=endTime, | |||
|
972 | set=set, expLabel=expLabel, ext=ext, | |||
|
973 | walk=walk) | |||
|
974 | ||||
|
975 | if not(pathList): | |||
|
976 | print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, | |||
|
977 | datetime.datetime.combine(startDate,startTime).ctime(), | |||
|
978 | datetime.datetime.combine(endDate,endTime).ctime()) | |||
|
979 | ||||
|
980 | sys.exit(-1) | |||
|
981 | ||||
|
982 | ||||
|
983 | self.fileIndex = -1 | |||
|
984 | self.pathList = pathList | |||
|
985 | self.filenameList = filenameList | |||
|
986 | file_name = os.path.basename(filenameList[-1]) | |||
|
987 | basename, ext = os.path.splitext(file_name) | |||
|
988 | last_set = int(basename[-3:]) | |||
|
989 | ||||
|
990 | self.online = online | |||
|
991 | self.delay = delay | |||
|
992 | ext = ext.lower() | |||
|
993 | self.ext = ext | |||
|
994 | ||||
|
995 | if not(self.setNextFile()): | |||
|
996 | if (startDate!=None) and (endDate!=None): | |||
|
997 | print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) | |||
|
998 | elif startDate != None: | |||
|
999 | print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) | |||
|
1000 | else: | |||
|
1001 | print "No files" | |||
|
1002 | ||||
|
1003 | sys.exit(-1) | |||
|
1004 | ||||
|
1005 | # self.updateDataHeader() | |||
|
1006 | if last_set != None: | |||
|
1007 | self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock | |||
|
1008 | return | |||
|
1009 | ||||
|
1010 | def getBasicHeader(self): | |||
|
1011 | ||||
|
1012 | self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds | |||
|
1013 | ||||
|
1014 | self.dataOut.flagTimeBlock = self.flagTimeBlock | |||
|
1015 | ||||
|
1016 | self.dataOut.timeZone = self.basicHeaderObj.timeZone | |||
|
1017 | ||||
|
1018 | self.dataOut.dstFlag = self.basicHeaderObj.dstFlag | |||
|
1019 | ||||
|
1020 | self.dataOut.errorCount = self.basicHeaderObj.errorCount | |||
|
1021 | ||||
|
1022 | self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime | |||
|
1023 | ||||
|
1024 | def getFirstHeader(self): | |||
|
1025 | ||||
|
1026 | raise ValueError, "This method has not been implemented" | |||
|
1027 | ||||
|
1028 | def getData(self): | |||
|
1029 | ||||
|
1030 | raise ValueError, "This method has not been implemented" | |||
|
1031 | ||||
|
1032 | def hasNotDataInBuffer(self): | |||
|
1033 | ||||
|
1034 | raise ValueError, "This method has not been implemented" | |||
|
1035 | ||||
|
1036 | def readBlock(self): | |||
|
1037 | ||||
|
1038 | raise ValueError, "This method has not been implemented" | |||
|
1039 | ||||
|
1040 | def isEndProcess(self): | |||
|
1041 | ||||
|
1042 | return self.flagNoMoreFiles | |||
|
1043 | ||||
|
1044 | def printReadBlocks(self): | |||
|
1045 | ||||
|
1046 | print "Number of read blocks per file %04d" %self.nReadBlocks | |||
|
1047 | ||||
|
1048 | def printTotalBlocks(self): | |||
|
1049 | ||||
|
1050 | print "Number of read blocks %04d" %self.nTotalBlocks | |||
|
1051 | ||||
|
1052 | def printNumberOfBlock(self): | |||
|
1053 | ||||
|
1054 | if self.flagIsNewBlock: | |||
|
1055 | print "Block No. %04d, Total blocks %04d -> %s" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks, self.dataOut.datatime.ctime()) | |||
|
1056 | self.dataOut.blocknow = self.basicHeaderObj.dataBlock | |||
|
1057 | ||||
|
1058 | def printInfo(self): | |||
|
1059 | ||||
|
1060 | if self.__printInfo == False: | |||
|
1061 | return | |||
|
1062 | ||||
|
1063 | self.basicHeaderObj.printInfo() | |||
|
1064 | self.systemHeaderObj.printInfo() | |||
|
1065 | self.radarControllerHeaderObj.printInfo() | |||
|
1066 | self.processingHeaderObj.printInfo() | |||
|
1067 | ||||
|
1068 | self.__printInfo = False | |||
|
1069 | ||||
|
1070 | ||||
|
1071 | def run(self, **kwargs): | |||
|
1072 | ||||
|
1073 | if not(self.isConfig): | |||
|
1074 | ||||
|
1075 | # self.dataOut = dataOut | |||
|
1076 | self.setup(**kwargs) | |||
|
1077 | self.isConfig = True | |||
|
1078 | ||||
|
1079 | self.getData() | |||
|
1080 | ||||
|
1081 | class JRODataWriter(JRODataIO): | |||
|
1082 | ||||
|
1083 | """ | |||
|
1084 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura | |||
|
1085 | de los datos siempre se realiza por bloques. | |||
|
1086 | """ | |||
|
1087 | ||||
|
1088 | blockIndex = 0 | |||
|
1089 | ||||
|
1090 | path = None | |||
|
1091 | ||||
|
1092 | setFile = None | |||
|
1093 | ||||
|
1094 | profilesPerBlock = None | |||
|
1095 | ||||
|
1096 | blocksPerFile = None | |||
|
1097 | ||||
|
1098 | nWriteBlocks = 0 | |||
|
1099 | ||||
|
1100 | def __init__(self, dataOut=None): | |||
|
1101 | raise ValueError, "Not implemented" | |||
|
1102 | ||||
|
1103 | ||||
|
1104 | def hasAllDataInBuffer(self): | |||
|
1105 | raise ValueError, "Not implemented" | |||
|
1106 | ||||
|
1107 | ||||
|
1108 | def setBlockDimension(self): | |||
|
1109 | raise ValueError, "Not implemented" | |||
|
1110 | ||||
|
1111 | ||||
|
1112 | def writeBlock(self): | |||
|
1113 | raise ValueError, "No implemented" | |||
|
1114 | ||||
|
1115 | ||||
|
1116 | def putData(self): | |||
|
1117 | raise ValueError, "No implemented" | |||
|
1118 | ||||
|
1119 | ||||
|
1120 | def setBasicHeader(self): | |||
|
1121 | ||||
|
1122 | self.basicHeaderObj.size = self.basicHeaderSize #bytes | |||
|
1123 | self.basicHeaderObj.version = self.versionFile | |||
|
1124 | self.basicHeaderObj.dataBlock = self.nTotalBlocks | |||
|
1125 | ||||
|
1126 | utc = numpy.floor(self.dataOut.utctime) | |||
|
1127 | milisecond = (self.dataOut.utctime - utc)* 1000.0 | |||
|
1128 | ||||
|
1129 | self.basicHeaderObj.utc = utc | |||
|
1130 | self.basicHeaderObj.miliSecond = milisecond | |||
|
1131 | self.basicHeaderObj.timeZone = self.dataOut.timeZone | |||
|
1132 | self.basicHeaderObj.dstFlag = self.dataOut.dstFlag | |||
|
1133 | self.basicHeaderObj.errorCount = self.dataOut.errorCount | |||
|
1134 | ||||
|
1135 | def setFirstHeader(self): | |||
|
1136 | """ | |||
|
1137 | Obtiene una copia del First Header | |||
|
1138 | ||||
|
1139 | Affected: | |||
|
1140 | ||||
|
1141 | self.basicHeaderObj | |||
|
1142 | self.systemHeaderObj | |||
|
1143 | self.radarControllerHeaderObj | |||
|
1144 | self.processingHeaderObj self. | |||
|
1145 | ||||
|
1146 | Return: | |||
|
1147 | None | |||
|
1148 | """ | |||
|
1149 | ||||
|
1150 | raise ValueError, "No implemented" | |||
|
1151 | ||||
|
1152 | def __writeFirstHeader(self): | |||
|
1153 | """ | |||
|
1154 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) | |||
|
1155 | ||||
|
1156 | Affected: | |||
|
1157 | __dataType | |||
|
1158 | ||||
|
1159 | Return: | |||
|
1160 | None | |||
|
1161 | """ | |||
|
1162 | ||||
|
1163 | # CALCULAR PARAMETROS | |||
|
1164 | ||||
|
1165 | sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size | |||
|
1166 | self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader | |||
|
1167 | ||||
|
1168 | self.basicHeaderObj.write(self.fp) | |||
|
1169 | self.systemHeaderObj.write(self.fp) | |||
|
1170 | self.radarControllerHeaderObj.write(self.fp) | |||
|
1171 | self.processingHeaderObj.write(self.fp) | |||
|
1172 | ||||
|
1173 | self.dtype = self.dataOut.dtype | |||
|
1174 | ||||
|
1175 | def __setNewBlock(self): | |||
|
1176 | """ | |||
|
1177 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header | |||
|
1178 | ||||
|
1179 | Return: | |||
|
1180 | 0 : si no pudo escribir nada | |||
|
1181 | 1 : Si escribio el Basic el First Header | |||
|
1182 | """ | |||
|
1183 | if self.fp == None: | |||
|
1184 | self.setNextFile() | |||
|
1185 | ||||
|
1186 | if self.flagIsNewFile: | |||
|
1187 | return 1 | |||
|
1188 | ||||
|
1189 | if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile: | |||
|
1190 | self.basicHeaderObj.write(self.fp) | |||
|
1191 | return 1 | |||
|
1192 | ||||
|
1193 | if not( self.setNextFile() ): | |||
|
1194 | return 0 | |||
|
1195 | ||||
|
1196 | return 1 | |||
|
1197 | ||||
|
1198 | ||||
|
1199 | def writeNextBlock(self): | |||
|
1200 | """ | |||
|
1201 | Selecciona el bloque siguiente de datos y los escribe en un file | |||
|
1202 | ||||
|
1203 | Return: | |||
|
1204 | 0 : Si no hizo pudo escribir el bloque de datos | |||
|
1205 | 1 : Si no pudo escribir el bloque de datos | |||
|
1206 | """ | |||
|
1207 | if not( self.__setNewBlock() ): | |||
|
1208 | return 0 | |||
|
1209 | ||||
|
1210 | self.writeBlock() | |||
|
1211 | ||||
|
1212 | return 1 | |||
|
1213 | ||||
|
1214 | def setNextFile(self): | |||
|
1215 | """ | |||
|
1216 | Determina el siguiente file que sera escrito | |||
|
1217 | ||||
|
1218 | Affected: | |||
|
1219 | self.filename | |||
|
1220 | self.subfolder | |||
|
1221 | self.fp | |||
|
1222 | self.setFile | |||
|
1223 | self.flagIsNewFile | |||
|
1224 | ||||
|
1225 | Return: | |||
|
1226 | 0 : Si el archivo no puede ser escrito | |||
|
1227 | 1 : Si el archivo esta listo para ser escrito | |||
|
1228 | """ | |||
|
1229 | ext = self.ext | |||
|
1230 | path = self.path | |||
|
1231 | ||||
|
1232 | if self.fp != None: | |||
|
1233 | self.fp.close() | |||
|
1234 | ||||
|
1235 | timeTuple = time.localtime( self.dataOut.utctime) | |||
|
1236 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) | |||
|
1237 | ||||
|
1238 | fullpath = os.path.join( path, subfolder ) | |||
|
1239 | if not( os.path.exists(fullpath) ): | |||
|
1240 | os.mkdir(fullpath) | |||
|
1241 | self.setFile = -1 #inicializo mi contador de seteo | |||
|
1242 | else: | |||
|
1243 | filesList = os.listdir( fullpath ) | |||
|
1244 | if len( filesList ) > 0: | |||
|
1245 | filesList = sorted( filesList, key=str.lower ) | |||
|
1246 | filen = filesList[-1] | |||
|
1247 | # el filename debera tener el siguiente formato | |||
|
1248 | # 0 1234 567 89A BCDE (hex) | |||
|
1249 | # x YYYY DDD SSS .ext | |||
|
1250 | if isNumber( filen[8:11] ): | |||
|
1251 | self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file | |||
|
1252 | else: | |||
|
1253 | self.setFile = -1 | |||
|
1254 | else: | |||
|
1255 | self.setFile = -1 #inicializo mi contador de seteo | |||
|
1256 | ||||
|
1257 | setFile = self.setFile | |||
|
1258 | setFile += 1 | |||
|
1259 | ||||
|
1260 | file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, | |||
|
1261 | timeTuple.tm_year, | |||
|
1262 | timeTuple.tm_yday, | |||
|
1263 | setFile, | |||
|
1264 | ext ) | |||
|
1265 | ||||
|
1266 | filename = os.path.join( path, subfolder, file ) | |||
|
1267 | ||||
|
1268 | fp = open( filename,'wb' ) | |||
|
1269 | ||||
|
1270 | self.blockIndex = 0 | |||
|
1271 | ||||
|
1272 | #guardando atributos | |||
|
1273 | self.filename = filename | |||
|
1274 | self.subfolder = subfolder | |||
|
1275 | self.fp = fp | |||
|
1276 | self.setFile = setFile | |||
|
1277 | self.flagIsNewFile = 1 | |||
|
1278 | ||||
|
1279 | self.setFirstHeader() | |||
|
1280 | ||||
|
1281 | print 'Writing the file: %s'%self.filename | |||
|
1282 | ||||
|
1283 | self.__writeFirstHeader() | |||
|
1284 | ||||
|
1285 | return 1 | |||
|
1286 | ||||
|
1287 | def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=0, ext=None): | |||
|
1288 | """ | |||
|
1289 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header | |||
|
1290 | ||||
|
1291 | Inputs: | |||
|
1292 | path : el path destino en el cual se escribiran los files a crear | |||
|
1293 | format : formato en el cual sera salvado un file | |||
|
1294 | set : el setebo del file | |||
|
1295 | ||||
|
1296 | Return: | |||
|
1297 | 0 : Si no realizo un buen seteo | |||
|
1298 | 1 : Si realizo un buen seteo | |||
|
1299 | """ | |||
|
1300 | ||||
|
1301 | if ext == None: | |||
|
1302 | ext = self.ext | |||
|
1303 | ||||
|
1304 | ext = ext.lower() | |||
|
1305 | ||||
|
1306 | self.ext = ext | |||
|
1307 | ||||
|
1308 | self.path = path | |||
|
1309 | ||||
|
1310 | self.setFile = set - 1 | |||
|
1311 | ||||
|
1312 | self.blocksPerFile = blocksPerFile | |||
|
1313 | ||||
|
1314 | self.profilesPerBlock = profilesPerBlock | |||
|
1315 | ||||
|
1316 | self.dataOut = dataOut | |||
|
1317 | ||||
|
1318 | if not(self.setNextFile()): | |||
|
1319 | print "There isn't a next file" | |||
|
1320 | return 0 | |||
|
1321 | ||||
|
1322 | self.setBlockDimension() | |||
|
1323 | ||||
|
1324 | return 1 | |||
|
1325 | ||||
|
1326 | def run(self, dataOut, **kwargs): | |||
|
1327 | ||||
|
1328 | if not(self.isConfig): | |||
|
1329 | ||||
|
1330 | self.setup(dataOut, **kwargs) | |||
|
1331 | self.isConfig = True | |||
|
1332 | ||||
|
1333 | self.putData() | |||
|
1334 |
@@ -0,0 +1,100 | |||||
|
1 | ''' | |||
|
2 | Created on Jul 3, 2014 | |||
|
3 | ||||
|
4 | @author: roj-idl71 | |||
|
5 | ''' | |||
|
6 | ||||
|
7 | import os | |||
|
8 | ||||
|
9 | from schainpy.model.data.jrodata import Voltage | |||
|
10 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation | |||
|
11 | ||||
|
12 | class Reader(ProcessingUnit): | |||
|
13 | ''' | |||
|
14 | classdocs | |||
|
15 | ''' | |||
|
16 | ||||
|
17 | def __init__(self): | |||
|
18 | ''' | |||
|
19 | Constructor | |||
|
20 | ''' | |||
|
21 | ||||
|
22 | ProcessingUnit.__init__(self) | |||
|
23 | ||||
|
24 | # self.dataIn = None | |||
|
25 | # | |||
|
26 | # self.isConfig = False | |||
|
27 | ||||
|
28 | #Is really necessary create the output object in the initializer | |||
|
29 | self.dataOut = Voltage() | |||
|
30 | ||||
|
31 | def setup(self, path = None, | |||
|
32 | startDate = None, | |||
|
33 | endDate = None, | |||
|
34 | startTime = None, | |||
|
35 | endTime = None, | |||
|
36 | set = None, | |||
|
37 | expLabel = "", | |||
|
38 | ext = None, | |||
|
39 | online = False, | |||
|
40 | delay = 60, | |||
|
41 | walk = True): | |||
|
42 | ''' | |||
|
43 | In this method we should set all initial parameters. | |||
|
44 | ||||
|
45 | ''' | |||
|
46 | ||||
|
47 | self.isConfig = True | |||
|
48 | ||||
|
49 | def run(self, **kwargs): | |||
|
50 | ''' | |||
|
51 | This method will be called many times so here you should put all your code | |||
|
52 | ''' | |||
|
53 | ||||
|
54 | if not self.isConfig: | |||
|
55 | self.setup(**kwargs) | |||
|
56 | ||||
|
57 | class Writer(Operation): | |||
|
58 | ''' | |||
|
59 | classdocs | |||
|
60 | ''' | |||
|
61 | ||||
|
62 | def __init__(self): | |||
|
63 | ''' | |||
|
64 | Constructor | |||
|
65 | ''' | |||
|
66 | self.dataOut = None | |||
|
67 | ||||
|
68 | self.isConfig = False | |||
|
69 | ||||
|
70 | def setup(self, dataIn, path, blocksPerFile, set=0, ext=None): | |||
|
71 | ''' | |||
|
72 | In this method we should set all initial parameters. | |||
|
73 | ||||
|
74 | Input: | |||
|
75 | dataIn : Input data will also be outputa data | |||
|
76 | ||||
|
77 | ''' | |||
|
78 | self.dataOut = dataIn | |||
|
79 | ||||
|
80 | ||||
|
81 | ||||
|
82 | ||||
|
83 | ||||
|
84 | self.isConfig = True | |||
|
85 | ||||
|
86 | return | |||
|
87 | ||||
|
88 | def run(self, dataIn, **kwargs): | |||
|
89 | ''' | |||
|
90 | This method will be called many times so here you should put all your code | |||
|
91 | ||||
|
92 | Inputs: | |||
|
93 | ||||
|
94 | dataIn : object with the data | |||
|
95 | ||||
|
96 | ''' | |||
|
97 | ||||
|
98 | if not self.isConfig: | |||
|
99 | self.setup(dataIn, **kwargs) | |||
|
100 | No newline at end of file |
This diff has been collapsed as it changes many lines, (763 lines changed) Show them Hide them | |||||
@@ -0,0 +1,763 | |||||
|
1 | ''' | |||
|
2 | ||||
|
3 | ''' | |||
|
4 | ||||
|
5 | import os, sys | |||
|
6 | import time, datetime | |||
|
7 | import numpy | |||
|
8 | import fnmatch | |||
|
9 | import glob | |||
|
10 | ||||
|
11 | try: | |||
|
12 | import pyfits | |||
|
13 | except: | |||
|
14 | """ | |||
|
15 | """ | |||
|
16 | ||||
|
17 | from xml.etree.ElementTree import ElementTree | |||
|
18 | ||||
|
19 | from jroIO_base import isDoyFolder, isNumber | |||
|
20 | from model.proc.jroproc_base import Operation, ProcessingUnit | |||
|
21 | ||||
|
22 | class ParameterConf: | |||
|
23 | ELEMENTNAME = 'Parameter' | |||
|
24 | def __init__(self): | |||
|
25 | self.name = '' | |||
|
26 | self.value = '' | |||
|
27 | ||||
|
28 | def readXml(self, parmElement): | |||
|
29 | self.name = parmElement.get('name') | |||
|
30 | self.value = parmElement.get('value') | |||
|
31 | ||||
|
32 | def getElementName(self): | |||
|
33 | return self.ELEMENTNAME | |||
|
34 | ||||
|
35 | class Metadata: | |||
|
36 | ||||
|
37 | def __init__(self, filename): | |||
|
38 | self.parmConfObjList = [] | |||
|
39 | self.readXml(filename) | |||
|
40 | ||||
|
41 | def readXml(self, filename): | |||
|
42 | self.projectElement = None | |||
|
43 | self.procUnitConfObjDict = {} | |||
|
44 | self.projectElement = ElementTree().parse(filename) | |||
|
45 | self.project = self.projectElement.tag | |||
|
46 | ||||
|
47 | parmElementList = self.projectElement.getiterator(ParameterConf().getElementName()) | |||
|
48 | ||||
|
49 | for parmElement in parmElementList: | |||
|
50 | parmConfObj = ParameterConf() | |||
|
51 | parmConfObj.readXml(parmElement) | |||
|
52 | self.parmConfObjList.append(parmConfObj) | |||
|
53 | ||||
|
54 | class FitsWriter(Operation): | |||
|
55 | ||||
|
56 | def __init__(self): | |||
|
57 | self.isConfig = False | |||
|
58 | self.dataBlocksPerFile = None | |||
|
59 | self.blockIndex = 0 | |||
|
60 | self.flagIsNewFile = 1 | |||
|
61 | self.fitsObj = None | |||
|
62 | self.optchar = 'P' | |||
|
63 | self.ext = '.fits' | |||
|
64 | self.setFile = 0 | |||
|
65 | ||||
|
66 | def setFitsHeader(self, dataOut, metadatafile): | |||
|
67 | ||||
|
68 | header_data = pyfits.PrimaryHDU() | |||
|
69 | ||||
|
70 | metadata4fits = Metadata(metadatafile) | |||
|
71 | for parameter in metadata4fits.parmConfObjList: | |||
|
72 | parm_name = parameter.name | |||
|
73 | parm_value = parameter.value | |||
|
74 | ||||
|
75 | # if parm_value == 'fromdatadatetime': | |||
|
76 | # value = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple()) | |||
|
77 | # elif parm_value == 'fromdataheights': | |||
|
78 | # value = dataOut.nHeights | |||
|
79 | # elif parm_value == 'fromdatachannel': | |||
|
80 | # value = dataOut.nChannels | |||
|
81 | # elif parm_value == 'fromdatasamples': | |||
|
82 | # value = dataOut.nFFTPoints | |||
|
83 | # else: | |||
|
84 | # value = parm_value | |||
|
85 | ||||
|
86 | header_data.header[parm_name] = parm_value | |||
|
87 | ||||
|
88 | ||||
|
89 | header_data.header['DATETIME'] = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple()) | |||
|
90 | header_data.header['CHANNELLIST'] = str(dataOut.channelList) | |||
|
91 | header_data.header['NCHANNELS'] = dataOut.nChannels | |||
|
92 | #header_data.header['HEIGHTS'] = dataOut.heightList | |||
|
93 | header_data.header['NHEIGHTS'] = dataOut.nHeights | |||
|
94 | ||||
|
95 | header_data.header['IPPSECONDS'] = dataOut.ippSeconds | |||
|
96 | header_data.header['NCOHINT'] = dataOut.nCohInt | |||
|
97 | header_data.header['NINCOHINT'] = dataOut.nIncohInt | |||
|
98 | header_data.header['TIMEZONE'] = dataOut.timeZone | |||
|
99 | header_data.header['NBLOCK'] = self.blockIndex | |||
|
100 | ||||
|
101 | header_data.writeto(self.filename) | |||
|
102 | ||||
|
103 | self.addExtension(dataOut.heightList,'HEIGHTLIST') | |||
|
104 | ||||
|
105 | ||||
|
106 | def setup(self, dataOut, path, dataBlocksPerFile, metadatafile): | |||
|
107 | ||||
|
108 | self.path = path | |||
|
109 | self.dataOut = dataOut | |||
|
110 | self.metadatafile = metadatafile | |||
|
111 | self.dataBlocksPerFile = dataBlocksPerFile | |||
|
112 | ||||
|
113 | def open(self): | |||
|
114 | self.fitsObj = pyfits.open(self.filename, mode='update') | |||
|
115 | ||||
|
116 | ||||
|
117 | def addExtension(self, data, tagname): | |||
|
118 | self.open() | |||
|
119 | extension = pyfits.ImageHDU(data=data, name=tagname) | |||
|
120 | #extension.header['TAG'] = tagname | |||
|
121 | self.fitsObj.append(extension) | |||
|
122 | self.write() | |||
|
123 | ||||
|
124 | def addData(self, data): | |||
|
125 | self.open() | |||
|
126 | extension = pyfits.ImageHDU(data=data, name=self.fitsObj[0].header['DATATYPE']) | |||
|
127 | extension.header['UTCTIME'] = self.dataOut.utctime | |||
|
128 | self.fitsObj.append(extension) | |||
|
129 | self.blockIndex += 1 | |||
|
130 | self.fitsObj[0].header['NBLOCK'] = self.blockIndex | |||
|
131 | ||||
|
132 | self.write() | |||
|
133 | ||||
|
134 | def write(self): | |||
|
135 | ||||
|
136 | self.fitsObj.flush(verbose=True) | |||
|
137 | self.fitsObj.close() | |||
|
138 | ||||
|
139 | ||||
|
140 | def setNextFile(self): | |||
|
141 | ||||
|
142 | ext = self.ext | |||
|
143 | path = self.path | |||
|
144 | ||||
|
145 | timeTuple = time.localtime( self.dataOut.utctime) | |||
|
146 | subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) | |||
|
147 | ||||
|
148 | fullpath = os.path.join( path, subfolder ) | |||
|
149 | if not( os.path.exists(fullpath) ): | |||
|
150 | os.mkdir(fullpath) | |||
|
151 | self.setFile = -1 #inicializo mi contador de seteo | |||
|
152 | else: | |||
|
153 | filesList = os.listdir( fullpath ) | |||
|
154 | if len( filesList ) > 0: | |||
|
155 | filesList = sorted( filesList, key=str.lower ) | |||
|
156 | filen = filesList[-1] | |||
|
157 | ||||
|
158 | if isNumber( filen[8:11] ): | |||
|
159 | self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file | |||
|
160 | else: | |||
|
161 | self.setFile = -1 | |||
|
162 | else: | |||
|
163 | self.setFile = -1 #inicializo mi contador de seteo | |||
|
164 | ||||
|
165 | setFile = self.setFile | |||
|
166 | setFile += 1 | |||
|
167 | ||||
|
168 | file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, | |||
|
169 | timeTuple.tm_year, | |||
|
170 | timeTuple.tm_yday, | |||
|
171 | setFile, | |||
|
172 | ext ) | |||
|
173 | ||||
|
174 | filename = os.path.join( path, subfolder, file ) | |||
|
175 | ||||
|
176 | self.blockIndex = 0 | |||
|
177 | self.filename = filename | |||
|
178 | self.setFile = setFile | |||
|
179 | self.flagIsNewFile = 1 | |||
|
180 | ||||
|
181 | print 'Writing the file: %s'%self.filename | |||
|
182 | ||||
|
183 | self.setFitsHeader(self.dataOut, self.metadatafile) | |||
|
184 | ||||
|
185 | return 1 | |||
|
186 | ||||
|
187 | def writeBlock(self): | |||
|
188 | self.addData(self.dataOut.data_spc) | |||
|
189 | self.flagIsNewFile = 0 | |||
|
190 | ||||
|
191 | ||||
|
192 | def __setNewBlock(self): | |||
|
193 | ||||
|
194 | if self.flagIsNewFile: | |||
|
195 | return 1 | |||
|
196 | ||||
|
197 | if self.blockIndex < self.dataBlocksPerFile: | |||
|
198 | return 1 | |||
|
199 | ||||
|
200 | if not( self.setNextFile() ): | |||
|
201 | return 0 | |||
|
202 | ||||
|
203 | return 1 | |||
|
204 | ||||
|
205 | def writeNextBlock(self): | |||
|
206 | if not( self.__setNewBlock() ): | |||
|
207 | return 0 | |||
|
208 | self.writeBlock() | |||
|
209 | return 1 | |||
|
210 | ||||
|
211 | def putData(self): | |||
|
212 | if self.flagIsNewFile: | |||
|
213 | self.setNextFile() | |||
|
214 | self.writeNextBlock() | |||
|
215 | ||||
|
216 | def run(self, dataOut, **kwargs): | |||
|
217 | if not(self.isConfig): | |||
|
218 | self.setup(dataOut, **kwargs) | |||
|
219 | self.isConfig = True | |||
|
220 | self.putData() | |||
|
221 | ||||
|
222 | ||||
|
223 | class FitsReader(ProcessingUnit): | |||
|
224 | ||||
|
225 | # __TIMEZONE = time.timezone | |||
|
226 | ||||
|
227 | expName = None | |||
|
228 | datetimestr = None | |||
|
229 | utc = None | |||
|
230 | nChannels = None | |||
|
231 | nSamples = None | |||
|
232 | dataBlocksPerFile = None | |||
|
233 | comments = None | |||
|
234 | lastUTTime = None | |||
|
235 | header_dict = None | |||
|
236 | data = None | |||
|
237 | data_header_dict = None | |||
|
238 | ||||
|
239 | def __init__(self): | |||
|
240 | self.isConfig = False | |||
|
241 | self.ext = '.fits' | |||
|
242 | self.setFile = 0 | |||
|
243 | self.flagNoMoreFiles = 0 | |||
|
244 | self.flagIsNewFile = 1 | |||
|
245 | self.flagTimeBlock = None | |||
|
246 | self.fileIndex = None | |||
|
247 | self.filename = None | |||
|
248 | self.fileSize = None | |||
|
249 | self.fitsObj = None | |||
|
250 | self.timeZone = None | |||
|
251 | self.nReadBlocks = 0 | |||
|
252 | self.nTotalBlocks = 0 | |||
|
253 | self.dataOut = self.createObjByDefault() | |||
|
254 | self.maxTimeStep = 10# deberia ser definido por el usuario usando el metodo setup() | |||
|
255 | self.blockIndex = 1 | |||
|
256 | ||||
|
257 | def createObjByDefault(self): | |||
|
258 | ||||
|
259 | dataObj = Fits() | |||
|
260 | ||||
|
261 | return dataObj | |||
|
262 | ||||
|
263 | def isFileinThisTime(self, filename, startTime, endTime, useLocalTime=False): | |||
|
264 | try: | |||
|
265 | fitsObj = pyfits.open(filename,'readonly') | |||
|
266 | except: | |||
|
267 | raise IOError, "The file %s can't be opened" %(filename) | |||
|
268 | ||||
|
269 | header = fitsObj[0].header | |||
|
270 | struct_time = time.strptime(header['DATETIME'], "%b %d %Y %H:%M:%S") | |||
|
271 | utc = time.mktime(struct_time) - time.timezone #TIMEZONE debe ser un parametro del header FITS | |||
|
272 | ||||
|
273 | ltc = utc | |||
|
274 | if useLocalTime: | |||
|
275 | ltc -= time.timezone | |||
|
276 | thisDatetime = datetime.datetime.utcfromtimestamp(ltc) | |||
|
277 | thisTime = thisDatetime.time() | |||
|
278 | ||||
|
279 | if not ((startTime <= thisTime) and (endTime > thisTime)): | |||
|
280 | return None | |||
|
281 | ||||
|
282 | return thisDatetime | |||
|
283 | ||||
|
284 | def __setNextFileOnline(self): | |||
|
285 | raise ValueError, "No implemented" | |||
|
286 | ||||
|
287 | def __setNextFileOffline(self): | |||
|
288 | idFile = self.fileIndex | |||
|
289 | ||||
|
290 | while (True): | |||
|
291 | idFile += 1 | |||
|
292 | if not(idFile < len(self.filenameList)): | |||
|
293 | self.flagNoMoreFiles = 1 | |||
|
294 | print "No more Files" | |||
|
295 | return 0 | |||
|
296 | ||||
|
297 | filename = self.filenameList[idFile] | |||
|
298 | ||||
|
299 | # if not(self.__verifyFile(filename)): | |||
|
300 | # continue | |||
|
301 | ||||
|
302 | fileSize = os.path.getsize(filename) | |||
|
303 | fitsObj = pyfits.open(filename,'readonly') | |||
|
304 | break | |||
|
305 | ||||
|
306 | self.flagIsNewFile = 1 | |||
|
307 | self.fileIndex = idFile | |||
|
308 | self.filename = filename | |||
|
309 | self.fileSize = fileSize | |||
|
310 | self.fitsObj = fitsObj | |||
|
311 | self.blockIndex = 0 | |||
|
312 | print "Setting the file: %s"%self.filename | |||
|
313 | ||||
|
314 | return 1 | |||
|
315 | ||||
|
316 | def readHeader(self): | |||
|
317 | headerObj = self.fitsObj[0] | |||
|
318 | ||||
|
319 | self.header_dict = headerObj.header | |||
|
320 | if 'EXPNAME' in headerObj.header.keys(): | |||
|
321 | self.expName = headerObj.header['EXPNAME'] | |||
|
322 | ||||
|
323 | if 'DATATYPE' in headerObj.header.keys(): | |||
|
324 | self.dataType = headerObj.header['DATATYPE'] | |||
|
325 | ||||
|
326 | self.datetimestr = headerObj.header['DATETIME'] | |||
|
327 | channelList = headerObj.header['CHANNELLIST'] | |||
|
328 | channelList = channelList.split('[') | |||
|
329 | channelList = channelList[1].split(']') | |||
|
330 | channelList = channelList[0].split(',') | |||
|
331 | channelList = [int(ch) for ch in channelList] | |||
|
332 | self.channelList = channelList | |||
|
333 | self.nChannels = headerObj.header['NCHANNELS'] | |||
|
334 | self.nHeights = headerObj.header['NHEIGHTS'] | |||
|
335 | self.ippSeconds = headerObj.header['IPPSECONDS'] | |||
|
336 | self.nCohInt = headerObj.header['NCOHINT'] | |||
|
337 | self.nIncohInt = headerObj.header['NINCOHINT'] | |||
|
338 | self.dataBlocksPerFile = headerObj.header['NBLOCK'] | |||
|
339 | self.timeZone = headerObj.header['TIMEZONE'] | |||
|
340 | ||||
|
341 | self.timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt | |||
|
342 | ||||
|
343 | if 'COMMENT' in headerObj.header.keys(): | |||
|
344 | self.comments = headerObj.header['COMMENT'] | |||
|
345 | ||||
|
346 | self.readHeightList() | |||
|
347 | ||||
|
348 | def readHeightList(self): | |||
|
349 | self.blockIndex = self.blockIndex + 1 | |||
|
350 | obj = self.fitsObj[self.blockIndex] | |||
|
351 | self.heightList = obj.data | |||
|
352 | self.blockIndex = self.blockIndex + 1 | |||
|
353 | ||||
|
354 | def readExtension(self): | |||
|
355 | obj = self.fitsObj[self.blockIndex] | |||
|
356 | self.heightList = obj.data | |||
|
357 | self.blockIndex = self.blockIndex + 1 | |||
|
358 | ||||
|
359 | def setNextFile(self): | |||
|
360 | ||||
|
361 | if self.online: | |||
|
362 | newFile = self.__setNextFileOnline() | |||
|
363 | else: | |||
|
364 | newFile = self.__setNextFileOffline() | |||
|
365 | ||||
|
366 | if not(newFile): | |||
|
367 | return 0 | |||
|
368 | ||||
|
369 | self.readHeader() | |||
|
370 | ||||
|
371 | self.nReadBlocks = 0 | |||
|
372 | # self.blockIndex = 1 | |||
|
373 | return 1 | |||
|
374 | ||||
|
375 | def __searchFilesOffLine(self, | |||
|
376 | path, | |||
|
377 | startDate, | |||
|
378 | endDate, | |||
|
379 | startTime=datetime.time(0,0,0), | |||
|
380 | endTime=datetime.time(23,59,59), | |||
|
381 | set=None, | |||
|
382 | expLabel='', | |||
|
383 | ext='.fits', | |||
|
384 | walk=True): | |||
|
385 | ||||
|
386 | pathList = [] | |||
|
387 | ||||
|
388 | if not walk: | |||
|
389 | pathList.append(path) | |||
|
390 | ||||
|
391 | else: | |||
|
392 | dirList = [] | |||
|
393 | for thisPath in os.listdir(path): | |||
|
394 | if not os.path.isdir(os.path.join(path,thisPath)): | |||
|
395 | continue | |||
|
396 | if not isDoyFolder(thisPath): | |||
|
397 | continue | |||
|
398 | ||||
|
399 | dirList.append(thisPath) | |||
|
400 | ||||
|
401 | if not(dirList): | |||
|
402 | return None, None | |||
|
403 | ||||
|
404 | thisDate = startDate | |||
|
405 | ||||
|
406 | while(thisDate <= endDate): | |||
|
407 | year = thisDate.timetuple().tm_year | |||
|
408 | doy = thisDate.timetuple().tm_yday | |||
|
409 | ||||
|
410 | matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*') | |||
|
411 | if len(matchlist) == 0: | |||
|
412 | thisDate += datetime.timedelta(1) | |||
|
413 | continue | |||
|
414 | for match in matchlist: | |||
|
415 | pathList.append(os.path.join(path,match,expLabel)) | |||
|
416 | ||||
|
417 | thisDate += datetime.timedelta(1) | |||
|
418 | ||||
|
419 | if pathList == []: | |||
|
420 | print "Any folder was found for the date range: %s-%s" %(startDate, endDate) | |||
|
421 | return None, None | |||
|
422 | ||||
|
423 | print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate) | |||
|
424 | ||||
|
425 | filenameList = [] | |||
|
426 | datetimeList = [] | |||
|
427 | ||||
|
428 | for i in range(len(pathList)): | |||
|
429 | ||||
|
430 | thisPath = pathList[i] | |||
|
431 | ||||
|
432 | fileList = glob.glob1(thisPath, "*%s" %ext) | |||
|
433 | fileList.sort() | |||
|
434 | ||||
|
435 | for file in fileList: | |||
|
436 | ||||
|
437 | filename = os.path.join(thisPath,file) | |||
|
438 | thisDatetime = self.isFileinThisTime(filename, startTime, endTime) | |||
|
439 | ||||
|
440 | if not(thisDatetime): | |||
|
441 | continue | |||
|
442 | ||||
|
443 | filenameList.append(filename) | |||
|
444 | datetimeList.append(thisDatetime) | |||
|
445 | ||||
|
446 | if not(filenameList): | |||
|
447 | print "Any file was found for the time range %s - %s" %(startTime, endTime) | |||
|
448 | return None, None | |||
|
449 | ||||
|
450 | print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime) | |||
|
451 | ||||
|
452 | ||||
|
453 | for i in range(len(filenameList)): | |||
|
454 | print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime()) | |||
|
455 | ||||
|
456 | self.filenameList = filenameList | |||
|
457 | self.datetimeList = datetimeList | |||
|
458 | ||||
|
459 | return pathList, filenameList | |||
|
460 | ||||
|
461 | def setup(self, path=None, | |||
|
462 | startDate=None, | |||
|
463 | endDate=None, | |||
|
464 | startTime=datetime.time(0,0,0), | |||
|
465 | endTime=datetime.time(23,59,59), | |||
|
466 | set=0, | |||
|
467 | expLabel = "", | |||
|
468 | ext = None, | |||
|
469 | online = False, | |||
|
470 | delay = 60, | |||
|
471 | walk = True): | |||
|
472 | ||||
|
473 | if path == None: | |||
|
474 | raise ValueError, "The path is not valid" | |||
|
475 | ||||
|
476 | if ext == None: | |||
|
477 | ext = self.ext | |||
|
478 | ||||
|
479 | if not(online): | |||
|
480 | print "Searching files in offline mode ..." | |||
|
481 | pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate, | |||
|
482 | startTime=startTime, endTime=endTime, | |||
|
483 | set=set, expLabel=expLabel, ext=ext, | |||
|
484 | walk=walk) | |||
|
485 | ||||
|
486 | if not(pathList): | |||
|
487 | print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, | |||
|
488 | datetime.datetime.combine(startDate,startTime).ctime(), | |||
|
489 | datetime.datetime.combine(endDate,endTime).ctime()) | |||
|
490 | ||||
|
491 | sys.exit(-1) | |||
|
492 | ||||
|
493 | self.fileIndex = -1 | |||
|
494 | self.pathList = pathList | |||
|
495 | self.filenameList = filenameList | |||
|
496 | ||||
|
497 | self.online = online | |||
|
498 | self.delay = delay | |||
|
499 | ext = ext.lower() | |||
|
500 | self.ext = ext | |||
|
501 | ||||
|
502 | if not(self.setNextFile()): | |||
|
503 | if (startDate!=None) and (endDate!=None): | |||
|
504 | print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) | |||
|
505 | elif startDate != None: | |||
|
506 | print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) | |||
|
507 | else: | |||
|
508 | print "No files" | |||
|
509 | ||||
|
510 | sys.exit(-1) | |||
|
511 | ||||
|
512 | ||||
|
513 | ||||
|
514 | def readBlock(self): | |||
|
515 | dataObj = self.fitsObj[self.blockIndex] | |||
|
516 | ||||
|
517 | self.data = dataObj.data | |||
|
518 | self.data_header_dict = dataObj.header | |||
|
519 | self.utc = self.data_header_dict['UTCTIME'] | |||
|
520 | ||||
|
521 | self.flagIsNewFile = 0 | |||
|
522 | self.blockIndex += 1 | |||
|
523 | self.nTotalBlocks += 1 | |||
|
524 | self.nReadBlocks += 1 | |||
|
525 | ||||
|
526 | return 1 | |||
|
527 | ||||
|
528 | def __jumpToLastBlock(self): | |||
|
529 | raise ValueError, "No implemented" | |||
|
530 | ||||
|
531 | def __waitNewBlock(self): | |||
|
532 | """ | |||
|
533 | Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma. | |||
|
534 | ||||
|
535 | Si el modo de lectura es OffLine siempre retorn 0 | |||
|
536 | """ | |||
|
537 | if not self.online: | |||
|
538 | return 0 | |||
|
539 | ||||
|
540 | if (self.nReadBlocks >= self.dataBlocksPerFile): | |||
|
541 | return 0 | |||
|
542 | ||||
|
543 | currentPointer = self.fp.tell() | |||
|
544 | ||||
|
545 | neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize | |||
|
546 | ||||
|
547 | for nTries in range( self.nTries ): | |||
|
548 | ||||
|
549 | self.fp.close() | |||
|
550 | self.fp = open( self.filename, 'rb' ) | |||
|
551 | self.fp.seek( currentPointer ) | |||
|
552 | ||||
|
553 | self.fileSize = os.path.getsize( self.filename ) | |||
|
554 | currentSize = self.fileSize - currentPointer | |||
|
555 | ||||
|
556 | if ( currentSize >= neededSize ): | |||
|
557 | self.__rdBasicHeader() | |||
|
558 | return 1 | |||
|
559 | ||||
|
560 | print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1) | |||
|
561 | time.sleep( self.delay ) | |||
|
562 | ||||
|
563 | ||||
|
564 | return 0 | |||
|
565 | ||||
|
566 | def __setNewBlock(self): | |||
|
567 | ||||
|
568 | if self.online: | |||
|
569 | self.__jumpToLastBlock() | |||
|
570 | ||||
|
571 | if self.flagIsNewFile: | |||
|
572 | return 1 | |||
|
573 | ||||
|
574 | self.lastUTTime = self.utc | |||
|
575 | ||||
|
576 | if self.online: | |||
|
577 | if self.__waitNewBlock(): | |||
|
578 | return 1 | |||
|
579 | ||||
|
580 | if self.nReadBlocks < self.dataBlocksPerFile: | |||
|
581 | return 1 | |||
|
582 | ||||
|
583 | if not(self.setNextFile()): | |||
|
584 | return 0 | |||
|
585 | ||||
|
586 | deltaTime = self.utc - self.lastUTTime | |||
|
587 | ||||
|
588 | self.flagTimeBlock = 0 | |||
|
589 | ||||
|
590 | if deltaTime > self.maxTimeStep: | |||
|
591 | self.flagTimeBlock = 1 | |||
|
592 | ||||
|
593 | return 1 | |||
|
594 | ||||
|
595 | ||||
|
596 | def readNextBlock(self): | |||
|
597 | if not(self.__setNewBlock()): | |||
|
598 | return 0 | |||
|
599 | ||||
|
600 | if not(self.readBlock()): | |||
|
601 | return 0 | |||
|
602 | ||||
|
603 | return 1 | |||
|
604 | ||||
|
605 | ||||
|
606 | def getData(self): | |||
|
607 | ||||
|
608 | if self.flagNoMoreFiles: | |||
|
609 | self.dataOut.flagNoData = True | |||
|
610 | print 'Process finished' | |||
|
611 | return 0 | |||
|
612 | ||||
|
613 | self.flagTimeBlock = 0 | |||
|
614 | self.flagIsNewBlock = 0 | |||
|
615 | ||||
|
616 | if not(self.readNextBlock()): | |||
|
617 | return 0 | |||
|
618 | ||||
|
619 | if self.data == None: | |||
|
620 | self.dataOut.flagNoData = True | |||
|
621 | return 0 | |||
|
622 | ||||
|
623 | self.dataOut.data = self.data | |||
|
624 | self.dataOut.data_header = self.data_header_dict | |||
|
625 | self.dataOut.utctime = self.utc | |||
|
626 | ||||
|
627 | self.dataOut.header = self.header_dict | |||
|
628 | self.dataOut.expName = self.expName | |||
|
629 | self.dataOut.nChannels = self.nChannels | |||
|
630 | self.dataOut.timeZone = self.timeZone | |||
|
631 | self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile | |||
|
632 | self.dataOut.comments = self.comments | |||
|
633 | self.dataOut.timeInterval = self.timeInterval | |||
|
634 | self.dataOut.channelList = self.channelList | |||
|
635 | self.dataOut.heightList = self.heightList | |||
|
636 | self.dataOut.flagNoData = False | |||
|
637 | ||||
|
638 | return self.dataOut.data | |||
|
639 | ||||
|
640 | def run(self, **kwargs): | |||
|
641 | ||||
|
642 | if not(self.isConfig): | |||
|
643 | self.setup(**kwargs) | |||
|
644 | self.isConfig = True | |||
|
645 | ||||
|
646 | self.getData() | |||
|
647 | ||||
|
648 | class SpectraHeisWriter(Operation): | |||
|
649 | # set = None | |||
|
650 | setFile = None | |||
|
651 | idblock = None | |||
|
652 | doypath = None | |||
|
653 | subfolder = None | |||
|
654 | ||||
|
655 | def __init__(self): | |||
|
656 | self.wrObj = FITS() | |||
|
657 | # self.dataOut = dataOut | |||
|
658 | self.nTotalBlocks=0 | |||
|
659 | # self.set = None | |||
|
660 | self.setFile = None | |||
|
661 | self.idblock = 0 | |||
|
662 | self.wrpath = None | |||
|
663 | self.doypath = None | |||
|
664 | self.subfolder = None | |||
|
665 | self.isConfig = False | |||
|
666 | ||||
|
667 | def isNumber(str): | |||
|
668 | """ | |||
|
669 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. | |||
|
670 | ||||
|
671 | Excepciones: | |||
|
672 | Si un determinado string no puede ser convertido a numero | |||
|
673 | Input: | |||
|
674 | str, string al cual se le analiza para determinar si convertible a un numero o no | |||
|
675 | ||||
|
676 | Return: | |||
|
677 | True : si el string es uno numerico | |||
|
678 | False : no es un string numerico | |||
|
679 | """ | |||
|
680 | try: | |||
|
681 | float( str ) | |||
|
682 | return True | |||
|
683 | except: | |||
|
684 | return False | |||
|
685 | ||||
|
686 | def setup(self, dataOut, wrpath): | |||
|
687 | ||||
|
688 | if not(os.path.exists(wrpath)): | |||
|
689 | os.mkdir(wrpath) | |||
|
690 | ||||
|
691 | self.wrpath = wrpath | |||
|
692 | # self.setFile = 0 | |||
|
693 | self.dataOut = dataOut | |||
|
694 | ||||
|
695 | def putData(self): | |||
|
696 | name= time.localtime( self.dataOut.utctime) | |||
|
697 | ext=".fits" | |||
|
698 | ||||
|
699 | if self.doypath == None: | |||
|
700 | self.subfolder = 'F%4.4d%3.3d_%d' % (name.tm_year,name.tm_yday,time.mktime(datetime.datetime.now().timetuple())) | |||
|
701 | self.doypath = os.path.join( self.wrpath, self.subfolder ) | |||
|
702 | os.mkdir(self.doypath) | |||
|
703 | ||||
|
704 | if self.setFile == None: | |||
|
705 | # self.set = self.dataOut.set | |||
|
706 | self.setFile = 0 | |||
|
707 | # if self.set != self.dataOut.set: | |||
|
708 | ## self.set = self.dataOut.set | |||
|
709 | # self.setFile = 0 | |||
|
710 | ||||
|
711 | #make the filename | |||
|
712 | file = 'D%4.4d%3.3d_%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext) | |||
|
713 | ||||
|
714 | filename = os.path.join(self.wrpath,self.subfolder, file) | |||
|
715 | ||||
|
716 | idblock = numpy.array([self.idblock],dtype="int64") | |||
|
717 | header=self.wrObj.cFImage(idblock=idblock, | |||
|
718 | year=time.gmtime(self.dataOut.utctime).tm_year, | |||
|
719 | month=time.gmtime(self.dataOut.utctime).tm_mon, | |||
|
720 | day=time.gmtime(self.dataOut.utctime).tm_mday, | |||
|
721 | hour=time.gmtime(self.dataOut.utctime).tm_hour, | |||
|
722 | minute=time.gmtime(self.dataOut.utctime).tm_min, | |||
|
723 | second=time.gmtime(self.dataOut.utctime).tm_sec) | |||
|
724 | ||||
|
725 | c=3E8 | |||
|
726 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] | |||
|
727 | freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)*(c/(2*deltaHeight*1000)) | |||
|
728 | ||||
|
729 | colList = [] | |||
|
730 | ||||
|
731 | colFreq=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq) | |||
|
732 | ||||
|
733 | colList.append(colFreq) | |||
|
734 | ||||
|
735 | nchannel=self.dataOut.nChannels | |||
|
736 | ||||
|
737 | for i in range(nchannel): | |||
|
738 | col = self.wrObj.writeData(name="PCh"+str(i+1), | |||
|
739 | format=str(self.dataOut.nFFTPoints)+'E', | |||
|
740 | data=10*numpy.log10(self.dataOut.data_spc[i,:])) | |||
|
741 | ||||
|
742 | colList.append(col) | |||
|
743 | ||||
|
744 | data=self.wrObj.Ctable(colList=colList) | |||
|
745 | ||||
|
746 | self.wrObj.CFile(header,data) | |||
|
747 | ||||
|
748 | self.wrObj.wFile(filename) | |||
|
749 | ||||
|
750 | #update the setFile | |||
|
751 | self.setFile += 1 | |||
|
752 | self.idblock += 1 | |||
|
753 | ||||
|
754 | return 1 | |||
|
755 | ||||
|
756 | def run(self, dataOut, **kwargs): | |||
|
757 | ||||
|
758 | if not(self.isConfig): | |||
|
759 | ||||
|
760 | self.setup(dataOut, **kwargs) | |||
|
761 | self.isConfig = True | |||
|
762 | ||||
|
763 | self.putData() No newline at end of file |
This diff has been collapsed as it changes many lines, (761 lines changed) Show them Hide them | |||||
@@ -0,0 +1,761 | |||||
|
1 | ''' | |||
|
2 | ''' | |||
|
3 | ||||
|
4 | import numpy | |||
|
5 | ||||
|
6 | from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter | |||
|
7 | from model.proc.jroproc_base import ProcessingUnit, Operation | |||
|
8 | from model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader | |||
|
9 | from model.data.jrodata import Spectra | |||
|
10 | ||||
|
11 | class SpectraReader(JRODataReader, ProcessingUnit): | |||
|
12 | """ | |||
|
13 | Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura | |||
|
14 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones) | |||
|
15 | son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel. | |||
|
16 | ||||
|
17 | paresCanalesIguales * alturas * perfiles (Self Spectra) | |||
|
18 | paresCanalesDiferentes * alturas * perfiles (Cross Spectra) | |||
|
19 | canales * alturas (DC Channels) | |||
|
20 | ||||
|
21 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, | |||
|
22 | RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la | |||
|
23 | cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de | |||
|
24 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". | |||
|
25 | ||||
|
26 | Example: | |||
|
27 | dpath = "/home/myuser/data" | |||
|
28 | ||||
|
29 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) | |||
|
30 | ||||
|
31 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) | |||
|
32 | ||||
|
33 | readerObj = SpectraReader() | |||
|
34 | ||||
|
35 | readerObj.setup(dpath, startTime, endTime) | |||
|
36 | ||||
|
37 | while(True): | |||
|
38 | ||||
|
39 | readerObj.getData() | |||
|
40 | ||||
|
41 | print readerObj.data_spc | |||
|
42 | ||||
|
43 | print readerObj.data_cspc | |||
|
44 | ||||
|
45 | print readerObj.data_dc | |||
|
46 | ||||
|
47 | if readerObj.flagNoMoreFiles: | |||
|
48 | break | |||
|
49 | ||||
|
50 | """ | |||
|
51 | ||||
|
52 | pts2read_SelfSpectra = 0 | |||
|
53 | ||||
|
54 | pts2read_CrossSpectra = 0 | |||
|
55 | ||||
|
56 | pts2read_DCchannels = 0 | |||
|
57 | ||||
|
58 | ext = ".pdata" | |||
|
59 | ||||
|
60 | optchar = "P" | |||
|
61 | ||||
|
62 | dataOut = None | |||
|
63 | ||||
|
64 | nRdChannels = None | |||
|
65 | ||||
|
66 | nRdPairs = None | |||
|
67 | ||||
|
68 | rdPairList = [] | |||
|
69 | ||||
|
70 | def __init__(self): | |||
|
71 | """ | |||
|
72 | Inicializador de la clase SpectraReader para la lectura de datos de espectros. | |||
|
73 | ||||
|
74 | Inputs: | |||
|
75 | dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para | |||
|
76 | almacenar un perfil de datos cada vez que se haga un requerimiento | |||
|
77 | (getData). El perfil sera obtenido a partir del buffer de datos, | |||
|
78 | si el buffer esta vacio se hara un nuevo proceso de lectura de un | |||
|
79 | bloque de datos. | |||
|
80 | Si este parametro no es pasado se creara uno internamente. | |||
|
81 | ||||
|
82 | Affected: | |||
|
83 | self.dataOut | |||
|
84 | ||||
|
85 | Return : None | |||
|
86 | """ | |||
|
87 | ||||
|
88 | #Eliminar de la base la herencia | |||
|
89 | ProcessingUnit.__init__(self) | |||
|
90 | ||||
|
91 | # self.isConfig = False | |||
|
92 | ||||
|
93 | self.pts2read_SelfSpectra = 0 | |||
|
94 | ||||
|
95 | self.pts2read_CrossSpectra = 0 | |||
|
96 | ||||
|
97 | self.pts2read_DCchannels = 0 | |||
|
98 | ||||
|
99 | self.datablock = None | |||
|
100 | ||||
|
101 | self.utc = None | |||
|
102 | ||||
|
103 | self.ext = ".pdata" | |||
|
104 | ||||
|
105 | self.optchar = "P" | |||
|
106 | ||||
|
107 | self.basicHeaderObj = BasicHeader(LOCALTIME) | |||
|
108 | ||||
|
109 | self.systemHeaderObj = SystemHeader() | |||
|
110 | ||||
|
111 | self.radarControllerHeaderObj = RadarControllerHeader() | |||
|
112 | ||||
|
113 | self.processingHeaderObj = ProcessingHeader() | |||
|
114 | ||||
|
115 | self.online = 0 | |||
|
116 | ||||
|
117 | self.fp = None | |||
|
118 | ||||
|
119 | self.idFile = None | |||
|
120 | ||||
|
121 | self.dtype = None | |||
|
122 | ||||
|
123 | self.fileSizeByHeader = None | |||
|
124 | ||||
|
125 | self.filenameList = [] | |||
|
126 | ||||
|
127 | self.filename = None | |||
|
128 | ||||
|
129 | self.fileSize = None | |||
|
130 | ||||
|
131 | self.firstHeaderSize = 0 | |||
|
132 | ||||
|
133 | self.basicHeaderSize = 24 | |||
|
134 | ||||
|
135 | self.pathList = [] | |||
|
136 | ||||
|
137 | self.lastUTTime = 0 | |||
|
138 | ||||
|
139 | self.maxTimeStep = 30 | |||
|
140 | ||||
|
141 | self.flagNoMoreFiles = 0 | |||
|
142 | ||||
|
143 | self.set = 0 | |||
|
144 | ||||
|
145 | self.path = None | |||
|
146 | ||||
|
147 | self.delay = 60 #seconds | |||
|
148 | ||||
|
149 | self.nTries = 3 #quantity tries | |||
|
150 | ||||
|
151 | self.nFiles = 3 #number of files for searching | |||
|
152 | ||||
|
153 | self.nReadBlocks = 0 | |||
|
154 | ||||
|
155 | self.flagIsNewFile = 1 | |||
|
156 | ||||
|
157 | self.__isFirstTimeOnline = 1 | |||
|
158 | ||||
|
159 | # self.ippSeconds = 0 | |||
|
160 | ||||
|
161 | self.flagTimeBlock = 0 | |||
|
162 | ||||
|
163 | self.flagIsNewBlock = 0 | |||
|
164 | ||||
|
165 | self.nTotalBlocks = 0 | |||
|
166 | ||||
|
167 | self.blocksize = 0 | |||
|
168 | ||||
|
169 | self.dataOut = self.createObjByDefault() | |||
|
170 | ||||
|
171 | self.profileIndex = 1 #Always | |||
|
172 | ||||
|
173 | ||||
|
174 | def createObjByDefault(self): | |||
|
175 | ||||
|
176 | dataObj = Spectra() | |||
|
177 | ||||
|
178 | return dataObj | |||
|
179 | ||||
|
180 | def __hasNotDataInBuffer(self): | |||
|
181 | return 1 | |||
|
182 | ||||
|
183 | ||||
|
184 | def getBlockDimension(self): | |||
|
185 | """ | |||
|
186 | Obtiene la cantidad de puntos a leer por cada bloque de datos | |||
|
187 | ||||
|
188 | Affected: | |||
|
189 | self.nRdChannels | |||
|
190 | self.nRdPairs | |||
|
191 | self.pts2read_SelfSpectra | |||
|
192 | self.pts2read_CrossSpectra | |||
|
193 | self.pts2read_DCchannels | |||
|
194 | self.blocksize | |||
|
195 | self.dataOut.nChannels | |||
|
196 | self.dataOut.nPairs | |||
|
197 | ||||
|
198 | Return: | |||
|
199 | None | |||
|
200 | """ | |||
|
201 | self.nRdChannels = 0 | |||
|
202 | self.nRdPairs = 0 | |||
|
203 | self.rdPairList = [] | |||
|
204 | ||||
|
205 | for i in range(0, self.processingHeaderObj.totalSpectra*2, 2): | |||
|
206 | if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]: | |||
|
207 | self.nRdChannels = self.nRdChannels + 1 #par de canales iguales | |||
|
208 | else: | |||
|
209 | self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes | |||
|
210 | self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1])) | |||
|
211 | ||||
|
212 | pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock | |||
|
213 | ||||
|
214 | self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read) | |||
|
215 | self.blocksize = self.pts2read_SelfSpectra | |||
|
216 | ||||
|
217 | if self.processingHeaderObj.flag_cspc: | |||
|
218 | self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read) | |||
|
219 | self.blocksize += self.pts2read_CrossSpectra | |||
|
220 | ||||
|
221 | if self.processingHeaderObj.flag_dc: | |||
|
222 | self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights) | |||
|
223 | self.blocksize += self.pts2read_DCchannels | |||
|
224 | ||||
|
225 | # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels | |||
|
226 | ||||
|
227 | ||||
|
228 | def readBlock(self): | |||
|
229 | """ | |||
|
230 | Lee el bloque de datos desde la posicion actual del puntero del archivo | |||
|
231 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos | |||
|
232 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer | |||
|
233 | es seteado a 0 | |||
|
234 | ||||
|
235 | Return: None | |||
|
236 | ||||
|
237 | Variables afectadas: | |||
|
238 | ||||
|
239 | self.flagIsNewFile | |||
|
240 | self.flagIsNewBlock | |||
|
241 | self.nTotalBlocks | |||
|
242 | self.data_spc | |||
|
243 | self.data_cspc | |||
|
244 | self.data_dc | |||
|
245 | ||||
|
246 | Exceptions: | |||
|
247 | Si un bloque leido no es un bloque valido | |||
|
248 | """ | |||
|
249 | blockOk_flag = False | |||
|
250 | fpointer = self.fp.tell() | |||
|
251 | ||||
|
252 | spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra ) | |||
|
253 | spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D | |||
|
254 | ||||
|
255 | if self.processingHeaderObj.flag_cspc: | |||
|
256 | cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra ) | |||
|
257 | cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D | |||
|
258 | ||||
|
259 | if self.processingHeaderObj.flag_dc: | |||
|
260 | dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) ) | |||
|
261 | dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D | |||
|
262 | ||||
|
263 | ||||
|
264 | if not(self.processingHeaderObj.shif_fft): | |||
|
265 | #desplaza a la derecha en el eje 2 determinadas posiciones | |||
|
266 | shift = int(self.processingHeaderObj.profilesPerBlock/2) | |||
|
267 | spc = numpy.roll( spc, shift , axis=2 ) | |||
|
268 | ||||
|
269 | if self.processingHeaderObj.flag_cspc: | |||
|
270 | #desplaza a la derecha en el eje 2 determinadas posiciones | |||
|
271 | cspc = numpy.roll( cspc, shift, axis=2 ) | |||
|
272 | ||||
|
273 | # self.processingHeaderObj.shif_fft = True | |||
|
274 | ||||
|
275 | spc = numpy.transpose( spc, (0,2,1) ) | |||
|
276 | self.data_spc = spc | |||
|
277 | ||||
|
278 | if self.processingHeaderObj.flag_cspc: | |||
|
279 | cspc = numpy.transpose( cspc, (0,2,1) ) | |||
|
280 | self.data_cspc = cspc['real'] + cspc['imag']*1j | |||
|
281 | else: | |||
|
282 | self.data_cspc = None | |||
|
283 | ||||
|
284 | if self.processingHeaderObj.flag_dc: | |||
|
285 | self.data_dc = dc['real'] + dc['imag']*1j | |||
|
286 | else: | |||
|
287 | self.data_dc = None | |||
|
288 | ||||
|
289 | self.flagIsNewFile = 0 | |||
|
290 | self.flagIsNewBlock = 1 | |||
|
291 | ||||
|
292 | self.nTotalBlocks += 1 | |||
|
293 | self.nReadBlocks += 1 | |||
|
294 | ||||
|
295 | return 1 | |||
|
296 | ||||
|
297 | def getFirstHeader(self): | |||
|
298 | ||||
|
299 | self.dataOut.systemHeaderObj = self.systemHeaderObj.copy() | |||
|
300 | ||||
|
301 | self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() | |||
|
302 | ||||
|
303 | # self.dataOut.ippSeconds = self.ippSeconds | |||
|
304 | ||||
|
305 | self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock | |||
|
306 | ||||
|
307 | self.dataOut.dtype = self.dtype | |||
|
308 | ||||
|
309 | # self.dataOut.nPairs = self.nPairs | |||
|
310 | ||||
|
311 | self.dataOut.pairsList = self.rdPairList | |||
|
312 | ||||
|
313 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock | |||
|
314 | ||||
|
315 | self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock | |||
|
316 | ||||
|
317 | self.dataOut.nCohInt = self.processingHeaderObj.nCohInt | |||
|
318 | ||||
|
319 | self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt | |||
|
320 | ||||
|
321 | xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight | |||
|
322 | ||||
|
323 | self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight) | |||
|
324 | ||||
|
325 | self.dataOut.channelList = range(self.systemHeaderObj.nChannels) | |||
|
326 | ||||
|
327 | self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft | |||
|
328 | ||||
|
329 | self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada | |||
|
330 | ||||
|
331 | self.dataOut.flagDeflipData = True #asumo q la data no esta sin flip | |||
|
332 | ||||
|
333 | if self.radarControllerHeaderObj.code != None: | |||
|
334 | ||||
|
335 | self.dataOut.nCode = self.radarControllerHeaderObj.nCode | |||
|
336 | ||||
|
337 | self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud | |||
|
338 | ||||
|
339 | self.dataOut.code = self.radarControllerHeaderObj.code | |||
|
340 | ||||
|
341 | self.dataOut.flagDecodeData = True | |||
|
342 | ||||
|
343 | def getData(self): | |||
|
344 | """ | |||
|
345 | First method to execute before "RUN" is called. | |||
|
346 | ||||
|
347 | Copia el buffer de lectura a la clase "Spectra", | |||
|
348 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de | |||
|
349 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" | |||
|
350 | ||||
|
351 | Return: | |||
|
352 | 0 : Si no hay mas archivos disponibles | |||
|
353 | 1 : Si hizo una buena copia del buffer | |||
|
354 | ||||
|
355 | Affected: | |||
|
356 | self.dataOut | |||
|
357 | ||||
|
358 | self.flagTimeBlock | |||
|
359 | self.flagIsNewBlock | |||
|
360 | """ | |||
|
361 | ||||
|
362 | if self.flagNoMoreFiles: | |||
|
363 | self.dataOut.flagNoData = True | |||
|
364 | print 'Process finished' | |||
|
365 | return 0 | |||
|
366 | ||||
|
367 | self.flagTimeBlock = 0 | |||
|
368 | self.flagIsNewBlock = 0 | |||
|
369 | ||||
|
370 | if self.__hasNotDataInBuffer(): | |||
|
371 | ||||
|
372 | if not( self.readNextBlock() ): | |||
|
373 | self.dataOut.flagNoData = True | |||
|
374 | return 0 | |||
|
375 | ||||
|
376 | #data es un numpy array de 3 dmensiones (perfiles, alturas y canales) | |||
|
377 | ||||
|
378 | if self.data_dc == None: | |||
|
379 | self.dataOut.flagNoData = True | |||
|
380 | return 0 | |||
|
381 | ||||
|
382 | self.getBasicHeader() | |||
|
383 | ||||
|
384 | self.getFirstHeader() | |||
|
385 | ||||
|
386 | self.dataOut.data_spc = self.data_spc | |||
|
387 | ||||
|
388 | self.dataOut.data_cspc = self.data_cspc | |||
|
389 | ||||
|
390 | self.dataOut.data_dc = self.data_dc | |||
|
391 | ||||
|
392 | self.dataOut.flagNoData = False | |||
|
393 | ||||
|
394 | self.dataOut.realtime = self.online | |||
|
395 | ||||
|
396 | return self.dataOut.data_spc | |||
|
397 | ||||
|
398 | class SpectraWriter(JRODataWriter, Operation): | |||
|
399 | ||||
|
400 | """ | |||
|
401 | Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura | |||
|
402 | de los datos siempre se realiza por bloques. | |||
|
403 | """ | |||
|
404 | ||||
|
405 | ext = ".pdata" | |||
|
406 | ||||
|
407 | optchar = "P" | |||
|
408 | ||||
|
409 | shape_spc_Buffer = None | |||
|
410 | ||||
|
411 | shape_cspc_Buffer = None | |||
|
412 | ||||
|
413 | shape_dc_Buffer = None | |||
|
414 | ||||
|
415 | data_spc = None | |||
|
416 | ||||
|
417 | data_cspc = None | |||
|
418 | ||||
|
419 | data_dc = None | |||
|
420 | ||||
|
421 | # dataOut = None | |||
|
422 | ||||
|
423 | def __init__(self): | |||
|
424 | """ | |||
|
425 | Inicializador de la clase SpectraWriter para la escritura de datos de espectros. | |||
|
426 | ||||
|
427 | Affected: | |||
|
428 | self.dataOut | |||
|
429 | self.basicHeaderObj | |||
|
430 | self.systemHeaderObj | |||
|
431 | self.radarControllerHeaderObj | |||
|
432 | self.processingHeaderObj | |||
|
433 | ||||
|
434 | Return: None | |||
|
435 | """ | |||
|
436 | ||||
|
437 | Operation.__init__(self) | |||
|
438 | ||||
|
439 | self.isConfig = False | |||
|
440 | ||||
|
441 | self.nTotalBlocks = 0 | |||
|
442 | ||||
|
443 | self.data_spc = None | |||
|
444 | ||||
|
445 | self.data_cspc = None | |||
|
446 | ||||
|
447 | self.data_dc = None | |||
|
448 | ||||
|
449 | self.fp = None | |||
|
450 | ||||
|
451 | self.flagIsNewFile = 1 | |||
|
452 | ||||
|
453 | self.nTotalBlocks = 0 | |||
|
454 | ||||
|
455 | self.flagIsNewBlock = 0 | |||
|
456 | ||||
|
457 | self.setFile = None | |||
|
458 | ||||
|
459 | self.dtype = None | |||
|
460 | ||||
|
461 | self.path = None | |||
|
462 | ||||
|
463 | self.noMoreFiles = 0 | |||
|
464 | ||||
|
465 | self.filename = None | |||
|
466 | ||||
|
467 | self.basicHeaderObj = BasicHeader(LOCALTIME) | |||
|
468 | ||||
|
469 | self.systemHeaderObj = SystemHeader() | |||
|
470 | ||||
|
471 | self.radarControllerHeaderObj = RadarControllerHeader() | |||
|
472 | ||||
|
473 | self.processingHeaderObj = ProcessingHeader() | |||
|
474 | ||||
|
475 | ||||
|
476 | def hasAllDataInBuffer(self): | |||
|
477 | return 1 | |||
|
478 | ||||
|
479 | ||||
|
480 | def setBlockDimension(self): | |||
|
481 | """ | |||
|
482 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque | |||
|
483 | ||||
|
484 | Affected: | |||
|
485 | self.shape_spc_Buffer | |||
|
486 | self.shape_cspc_Buffer | |||
|
487 | self.shape_dc_Buffer | |||
|
488 | ||||
|
489 | Return: None | |||
|
490 | """ | |||
|
491 | self.shape_spc_Buffer = (self.dataOut.nChannels, | |||
|
492 | self.processingHeaderObj.nHeights, | |||
|
493 | self.processingHeaderObj.profilesPerBlock) | |||
|
494 | ||||
|
495 | self.shape_cspc_Buffer = (self.dataOut.nPairs, | |||
|
496 | self.processingHeaderObj.nHeights, | |||
|
497 | self.processingHeaderObj.profilesPerBlock) | |||
|
498 | ||||
|
499 | self.shape_dc_Buffer = (self.dataOut.nChannels, | |||
|
500 | self.processingHeaderObj.nHeights) | |||
|
501 | ||||
|
502 | ||||
|
503 | def writeBlock(self): | |||
|
504 | """ | |||
|
505 | Escribe el buffer en el file designado | |||
|
506 | ||||
|
507 | Affected: | |||
|
508 | self.data_spc | |||
|
509 | self.data_cspc | |||
|
510 | self.data_dc | |||
|
511 | self.flagIsNewFile | |||
|
512 | self.flagIsNewBlock | |||
|
513 | self.nTotalBlocks | |||
|
514 | self.nWriteBlocks | |||
|
515 | ||||
|
516 | Return: None | |||
|
517 | """ | |||
|
518 | ||||
|
519 | spc = numpy.transpose( self.data_spc, (0,2,1) ) | |||
|
520 | if not( self.processingHeaderObj.shif_fft ): | |||
|
521 | spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones | |||
|
522 | data = spc.reshape((-1)) | |||
|
523 | data = data.astype(self.dtype[0]) | |||
|
524 | data.tofile(self.fp) | |||
|
525 | ||||
|
526 | if self.data_cspc != None: | |||
|
527 | data = numpy.zeros( self.shape_cspc_Buffer, self.dtype ) | |||
|
528 | cspc = numpy.transpose( self.data_cspc, (0,2,1) ) | |||
|
529 | if not( self.processingHeaderObj.shif_fft ): | |||
|
530 | cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones | |||
|
531 | data['real'] = cspc.real | |||
|
532 | data['imag'] = cspc.imag | |||
|
533 | data = data.reshape((-1)) | |||
|
534 | data.tofile(self.fp) | |||
|
535 | ||||
|
536 | if self.data_dc != None: | |||
|
537 | data = numpy.zeros( self.shape_dc_Buffer, self.dtype ) | |||
|
538 | dc = self.data_dc | |||
|
539 | data['real'] = dc.real | |||
|
540 | data['imag'] = dc.imag | |||
|
541 | data = data.reshape((-1)) | |||
|
542 | data.tofile(self.fp) | |||
|
543 | ||||
|
544 | self.data_spc.fill(0) | |||
|
545 | ||||
|
546 | if self.data_dc != None: | |||
|
547 | self.data_dc.fill(0) | |||
|
548 | ||||
|
549 | if self.data_cspc != None: | |||
|
550 | self.data_cspc.fill(0) | |||
|
551 | ||||
|
552 | self.flagIsNewFile = 0 | |||
|
553 | self.flagIsNewBlock = 1 | |||
|
554 | self.nTotalBlocks += 1 | |||
|
555 | self.nWriteBlocks += 1 | |||
|
556 | self.blockIndex += 1 | |||
|
557 | ||||
|
558 | ||||
|
559 | def putData(self): | |||
|
560 | """ | |||
|
561 | Setea un bloque de datos y luego los escribe en un file | |||
|
562 | ||||
|
563 | Affected: | |||
|
564 | self.data_spc | |||
|
565 | self.data_cspc | |||
|
566 | self.data_dc | |||
|
567 | ||||
|
568 | Return: | |||
|
569 | 0 : Si no hay data o no hay mas files que puedan escribirse | |||
|
570 | 1 : Si se escribio la data de un bloque en un file | |||
|
571 | """ | |||
|
572 | ||||
|
573 | if self.dataOut.flagNoData: | |||
|
574 | return 0 | |||
|
575 | ||||
|
576 | self.flagIsNewBlock = 0 | |||
|
577 | ||||
|
578 | if self.dataOut.flagTimeBlock: | |||
|
579 | self.data_spc.fill(0) | |||
|
580 | self.data_cspc.fill(0) | |||
|
581 | self.data_dc.fill(0) | |||
|
582 | self.setNextFile() | |||
|
583 | ||||
|
584 | if self.flagIsNewFile == 0: | |||
|
585 | self.setBasicHeader() | |||
|
586 | ||||
|
587 | self.data_spc = self.dataOut.data_spc.copy() | |||
|
588 | if self.dataOut.data_cspc != None: | |||
|
589 | self.data_cspc = self.dataOut.data_cspc.copy() | |||
|
590 | self.data_dc = self.dataOut.data_dc.copy() | |||
|
591 | ||||
|
592 | # #self.processingHeaderObj.dataBlocksPerFile) | |||
|
593 | if self.hasAllDataInBuffer(): | |||
|
594 | # self.setFirstHeader() | |||
|
595 | self.writeNextBlock() | |||
|
596 | ||||
|
597 | return 1 | |||
|
598 | ||||
|
599 | ||||
|
600 | def __getProcessFlags(self): | |||
|
601 | ||||
|
602 | processFlags = 0 | |||
|
603 | ||||
|
604 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) | |||
|
605 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) | |||
|
606 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) | |||
|
607 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) | |||
|
608 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
609 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) | |||
|
610 | ||||
|
611 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] | |||
|
612 | ||||
|
613 | ||||
|
614 | ||||
|
615 | datatypeValueList = [PROCFLAG.DATATYPE_CHAR, | |||
|
616 | PROCFLAG.DATATYPE_SHORT, | |||
|
617 | PROCFLAG.DATATYPE_LONG, | |||
|
618 | PROCFLAG.DATATYPE_INT64, | |||
|
619 | PROCFLAG.DATATYPE_FLOAT, | |||
|
620 | PROCFLAG.DATATYPE_DOUBLE] | |||
|
621 | ||||
|
622 | ||||
|
623 | for index in range(len(dtypeList)): | |||
|
624 | if self.dataOut.dtype == dtypeList[index]: | |||
|
625 | dtypeValue = datatypeValueList[index] | |||
|
626 | break | |||
|
627 | ||||
|
628 | processFlags += dtypeValue | |||
|
629 | ||||
|
630 | if self.dataOut.flagDecodeData: | |||
|
631 | processFlags += PROCFLAG.DECODE_DATA | |||
|
632 | ||||
|
633 | if self.dataOut.flagDeflipData: | |||
|
634 | processFlags += PROCFLAG.DEFLIP_DATA | |||
|
635 | ||||
|
636 | if self.dataOut.code != None: | |||
|
637 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE | |||
|
638 | ||||
|
639 | if self.dataOut.nIncohInt > 1: | |||
|
640 | processFlags += PROCFLAG.INCOHERENT_INTEGRATION | |||
|
641 | ||||
|
642 | if self.dataOut.data_dc != None: | |||
|
643 | processFlags += PROCFLAG.SAVE_CHANNELS_DC | |||
|
644 | ||||
|
645 | return processFlags | |||
|
646 | ||||
|
647 | ||||
|
648 | def __getBlockSize(self): | |||
|
649 | ''' | |||
|
650 | Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra | |||
|
651 | ''' | |||
|
652 | ||||
|
653 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) | |||
|
654 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) | |||
|
655 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) | |||
|
656 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) | |||
|
657 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
658 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) | |||
|
659 | ||||
|
660 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] | |||
|
661 | datatypeValueList = [1,2,4,8,4,8] | |||
|
662 | for index in range(len(dtypeList)): | |||
|
663 | if self.dataOut.dtype == dtypeList[index]: | |||
|
664 | datatypeValue = datatypeValueList[index] | |||
|
665 | break | |||
|
666 | ||||
|
667 | ||||
|
668 | pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints | |||
|
669 | ||||
|
670 | pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write) | |||
|
671 | blocksize = (pts2write_SelfSpectra*datatypeValue) | |||
|
672 | ||||
|
673 | if self.dataOut.data_cspc != None: | |||
|
674 | pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write) | |||
|
675 | blocksize += (pts2write_CrossSpectra*datatypeValue*2) | |||
|
676 | ||||
|
677 | if self.dataOut.data_dc != None: | |||
|
678 | pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights) | |||
|
679 | blocksize += (pts2write_DCchannels*datatypeValue*2) | |||
|
680 | ||||
|
681 | blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO | |||
|
682 | ||||
|
683 | return blocksize | |||
|
684 | ||||
|
685 | def setFirstHeader(self): | |||
|
686 | ||||
|
687 | """ | |||
|
688 | Obtiene una copia del First Header | |||
|
689 | ||||
|
690 | Affected: | |||
|
691 | self.systemHeaderObj | |||
|
692 | self.radarControllerHeaderObj | |||
|
693 | self.dtype | |||
|
694 | ||||
|
695 | Return: | |||
|
696 | None | |||
|
697 | """ | |||
|
698 | ||||
|
699 | self.systemHeaderObj = self.dataOut.systemHeaderObj.copy() | |||
|
700 | self.systemHeaderObj.nChannels = self.dataOut.nChannels | |||
|
701 | self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy() | |||
|
702 | old_code_size = self.dataOut.radarControllerHeaderObj.code_size | |||
|
703 | new_code_size = int(numpy.ceil(self.dataOut.nBaud/32.))*self.dataOut.nCode*4 | |||
|
704 | self.radarControllerHeaderObj.size = self.radarControllerHeaderObj.size - old_code_size + new_code_size | |||
|
705 | ||||
|
706 | self.setBasicHeader() | |||
|
707 | ||||
|
708 | processingHeaderSize = 40 # bytes | |||
|
709 | self.processingHeaderObj.dtype = 1 # Spectra | |||
|
710 | self.processingHeaderObj.blockSize = self.__getBlockSize() | |||
|
711 | self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints | |||
|
712 | self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile | |||
|
713 | self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows | |||
|
714 | self.processingHeaderObj.processFlags = self.__getProcessFlags() | |||
|
715 | self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval | |||
|
716 | self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt | |||
|
717 | self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels | |||
|
718 | self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT | |||
|
719 | ||||
|
720 | if self.processingHeaderObj.totalSpectra > 0: | |||
|
721 | channelList = [] | |||
|
722 | for channel in range(self.dataOut.nChannels): | |||
|
723 | channelList.append(channel) | |||
|
724 | channelList.append(channel) | |||
|
725 | ||||
|
726 | pairsList = [] | |||
|
727 | if self.dataOut.nPairs > 0: | |||
|
728 | for pair in self.dataOut.pairsList: | |||
|
729 | pairsList.append(pair[0]) | |||
|
730 | pairsList.append(pair[1]) | |||
|
731 | ||||
|
732 | spectraComb = channelList + pairsList | |||
|
733 | spectraComb = numpy.array(spectraComb,dtype="u1") | |||
|
734 | self.processingHeaderObj.spectraComb = spectraComb | |||
|
735 | sizeOfSpcComb = len(spectraComb) | |||
|
736 | processingHeaderSize += sizeOfSpcComb | |||
|
737 | ||||
|
738 | # The processing header should not have information about code | |||
|
739 | # if self.dataOut.code != None: | |||
|
740 | # self.processingHeaderObj.code = self.dataOut.code | |||
|
741 | # self.processingHeaderObj.nCode = self.dataOut.nCode | |||
|
742 | # self.processingHeaderObj.nBaud = self.dataOut.nBaud | |||
|
743 | # nCodeSize = 4 # bytes | |||
|
744 | # nBaudSize = 4 # bytes | |||
|
745 | # codeSize = 4 # bytes | |||
|
746 | # sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud) | |||
|
747 | # processingHeaderSize += sizeOfCode | |||
|
748 | ||||
|
749 | if self.processingHeaderObj.nWindows != 0: | |||
|
750 | self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] | |||
|
751 | self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] | |||
|
752 | self.processingHeaderObj.nHeights = self.dataOut.nHeights | |||
|
753 | self.processingHeaderObj.samplesWin = self.dataOut.nHeights | |||
|
754 | sizeOfFirstHeight = 4 | |||
|
755 | sizeOfdeltaHeight = 4 | |||
|
756 | sizeOfnHeights = 4 | |||
|
757 | sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows | |||
|
758 | processingHeaderSize += sizeOfWindows | |||
|
759 | ||||
|
760 | self.processingHeaderObj.size = processingHeaderSize | |||
|
761 |
This diff has been collapsed as it changes many lines, (588 lines changed) Show them Hide them | |||||
@@ -0,0 +1,588 | |||||
|
1 | ''' | |||
|
2 | ||||
|
3 | ''' | |||
|
4 | import numpy | |||
|
5 | ||||
|
6 | from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter | |||
|
7 | from model.proc.jroproc_base import ProcessingUnit, Operation | |||
|
8 | from model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader | |||
|
9 | from model.data.jrodata import Voltage | |||
|
10 | ||||
|
11 | class VoltageReader(JRODataReader, ProcessingUnit): | |||
|
12 | """ | |||
|
13 | Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura | |||
|
14 | de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones: | |||
|
15 | perfiles*alturas*canales) son almacenados en la variable "buffer". | |||
|
16 | ||||
|
17 | perfiles * alturas * canales | |||
|
18 | ||||
|
19 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, | |||
|
20 | RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la | |||
|
21 | cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de | |||
|
22 | datos desde el "buffer" cada vez que se ejecute el metodo "getData". | |||
|
23 | ||||
|
24 | Example: | |||
|
25 | ||||
|
26 | dpath = "/home/myuser/data" | |||
|
27 | ||||
|
28 | startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0) | |||
|
29 | ||||
|
30 | endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0) | |||
|
31 | ||||
|
32 | readerObj = VoltageReader() | |||
|
33 | ||||
|
34 | readerObj.setup(dpath, startTime, endTime) | |||
|
35 | ||||
|
36 | while(True): | |||
|
37 | ||||
|
38 | #to get one profile | |||
|
39 | profile = readerObj.getData() | |||
|
40 | ||||
|
41 | #print the profile | |||
|
42 | print profile | |||
|
43 | ||||
|
44 | #If you want to see all datablock | |||
|
45 | print readerObj.datablock | |||
|
46 | ||||
|
47 | if readerObj.flagNoMoreFiles: | |||
|
48 | break | |||
|
49 | ||||
|
50 | """ | |||
|
51 | ||||
|
52 | ext = ".r" | |||
|
53 | ||||
|
54 | optchar = "D" | |||
|
55 | dataOut = None | |||
|
56 | ||||
|
57 | ||||
|
58 | def __init__(self): | |||
|
59 | """ | |||
|
60 | Inicializador de la clase VoltageReader para la lectura de datos de voltage. | |||
|
61 | ||||
|
62 | Input: | |||
|
63 | dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para | |||
|
64 | almacenar un perfil de datos cada vez que se haga un requerimiento | |||
|
65 | (getData). El perfil sera obtenido a partir del buffer de datos, | |||
|
66 | si el buffer esta vacio se hara un nuevo proceso de lectura de un | |||
|
67 | bloque de datos. | |||
|
68 | Si este parametro no es pasado se creara uno internamente. | |||
|
69 | ||||
|
70 | Variables afectadas: | |||
|
71 | self.dataOut | |||
|
72 | ||||
|
73 | Return: | |||
|
74 | None | |||
|
75 | """ | |||
|
76 | ||||
|
77 | ProcessingUnit.__init__(self) | |||
|
78 | ||||
|
79 | self.isConfig = False | |||
|
80 | ||||
|
81 | self.datablock = None | |||
|
82 | ||||
|
83 | self.utc = 0 | |||
|
84 | ||||
|
85 | self.ext = ".r" | |||
|
86 | ||||
|
87 | self.optchar = "D" | |||
|
88 | ||||
|
89 | self.basicHeaderObj = BasicHeader(LOCALTIME) | |||
|
90 | ||||
|
91 | self.systemHeaderObj = SystemHeader() | |||
|
92 | ||||
|
93 | self.radarControllerHeaderObj = RadarControllerHeader() | |||
|
94 | ||||
|
95 | self.processingHeaderObj = ProcessingHeader() | |||
|
96 | ||||
|
97 | self.online = 0 | |||
|
98 | ||||
|
99 | self.fp = None | |||
|
100 | ||||
|
101 | self.idFile = None | |||
|
102 | ||||
|
103 | self.dtype = None | |||
|
104 | ||||
|
105 | self.fileSizeByHeader = None | |||
|
106 | ||||
|
107 | self.filenameList = [] | |||
|
108 | ||||
|
109 | self.filename = None | |||
|
110 | ||||
|
111 | self.fileSize = None | |||
|
112 | ||||
|
113 | self.firstHeaderSize = 0 | |||
|
114 | ||||
|
115 | self.basicHeaderSize = 24 | |||
|
116 | ||||
|
117 | self.pathList = [] | |||
|
118 | ||||
|
119 | self.filenameList = [] | |||
|
120 | ||||
|
121 | self.lastUTTime = 0 | |||
|
122 | ||||
|
123 | self.maxTimeStep = 30 | |||
|
124 | ||||
|
125 | self.flagNoMoreFiles = 0 | |||
|
126 | ||||
|
127 | self.set = 0 | |||
|
128 | ||||
|
129 | self.path = None | |||
|
130 | ||||
|
131 | self.profileIndex = 2**32-1 | |||
|
132 | ||||
|
133 | self.delay = 3 #seconds | |||
|
134 | ||||
|
135 | self.nTries = 3 #quantity tries | |||
|
136 | ||||
|
137 | self.nFiles = 3 #number of files for searching | |||
|
138 | ||||
|
139 | self.nReadBlocks = 0 | |||
|
140 | ||||
|
141 | self.flagIsNewFile = 1 | |||
|
142 | ||||
|
143 | self.__isFirstTimeOnline = 1 | |||
|
144 | ||||
|
145 | # self.ippSeconds = 0 | |||
|
146 | ||||
|
147 | self.flagTimeBlock = 0 | |||
|
148 | ||||
|
149 | self.flagIsNewBlock = 0 | |||
|
150 | ||||
|
151 | self.nTotalBlocks = 0 | |||
|
152 | ||||
|
153 | self.blocksize = 0 | |||
|
154 | ||||
|
155 | self.dataOut = self.createObjByDefault() | |||
|
156 | ||||
|
157 | def createObjByDefault(self): | |||
|
158 | ||||
|
159 | dataObj = Voltage() | |||
|
160 | ||||
|
161 | return dataObj | |||
|
162 | ||||
|
163 | def __hasNotDataInBuffer(self): | |||
|
164 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: | |||
|
165 | return 1 | |||
|
166 | return 0 | |||
|
167 | ||||
|
168 | ||||
|
169 | def getBlockDimension(self): | |||
|
170 | """ | |||
|
171 | Obtiene la cantidad de puntos a leer por cada bloque de datos | |||
|
172 | ||||
|
173 | Affected: | |||
|
174 | self.blocksize | |||
|
175 | ||||
|
176 | Return: | |||
|
177 | None | |||
|
178 | """ | |||
|
179 | pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels | |||
|
180 | self.blocksize = pts2read | |||
|
181 | ||||
|
182 | ||||
|
183 | def readBlock(self): | |||
|
184 | """ | |||
|
185 | readBlock lee el bloque de datos desde la posicion actual del puntero del archivo | |||
|
186 | (self.fp) y actualiza todos los parametros relacionados al bloque de datos | |||
|
187 | (metadata + data). La data leida es almacenada en el buffer y el contador del buffer | |||
|
188 | es seteado a 0 | |||
|
189 | ||||
|
190 | Inputs: | |||
|
191 | None | |||
|
192 | ||||
|
193 | Return: | |||
|
194 | None | |||
|
195 | ||||
|
196 | Affected: | |||
|
197 | self.profileIndex | |||
|
198 | self.datablock | |||
|
199 | self.flagIsNewFile | |||
|
200 | self.flagIsNewBlock | |||
|
201 | self.nTotalBlocks | |||
|
202 | ||||
|
203 | Exceptions: | |||
|
204 | Si un bloque leido no es un bloque valido | |||
|
205 | """ | |||
|
206 | current_pointer_location = self.fp.tell() | |||
|
207 | junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) | |||
|
208 | ||||
|
209 | try: | |||
|
210 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) | |||
|
211 | except: | |||
|
212 | #print "The read block (%3d) has not enough data" %self.nReadBlocks | |||
|
213 | ||||
|
214 | if self.waitDataBlock(pointer_location=current_pointer_location): | |||
|
215 | junk = numpy.fromfile( self.fp, self.dtype, self.blocksize ) | |||
|
216 | junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) ) | |||
|
217 | # return 0 | |||
|
218 | ||||
|
219 | junk = numpy.transpose(junk, (2,0,1)) | |||
|
220 | self.datablock = junk['real'] + junk['imag']*1j | |||
|
221 | ||||
|
222 | self.profileIndex = 0 | |||
|
223 | ||||
|
224 | self.flagIsNewFile = 0 | |||
|
225 | self.flagIsNewBlock = 1 | |||
|
226 | ||||
|
227 | self.nTotalBlocks += 1 | |||
|
228 | self.nReadBlocks += 1 | |||
|
229 | ||||
|
230 | return 1 | |||
|
231 | ||||
|
232 | def getFirstHeader(self): | |||
|
233 | ||||
|
234 | self.dataOut.systemHeaderObj = self.systemHeaderObj.copy() | |||
|
235 | ||||
|
236 | self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy() | |||
|
237 | ||||
|
238 | # self.dataOut.ippSeconds = self.ippSeconds | |||
|
239 | ||||
|
240 | self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt | |||
|
241 | ||||
|
242 | if self.radarControllerHeaderObj.code != None: | |||
|
243 | ||||
|
244 | self.dataOut.nCode = self.radarControllerHeaderObj.nCode | |||
|
245 | ||||
|
246 | self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud | |||
|
247 | ||||
|
248 | self.dataOut.code = self.radarControllerHeaderObj.code | |||
|
249 | ||||
|
250 | self.dataOut.dtype = self.dtype | |||
|
251 | ||||
|
252 | self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock | |||
|
253 | ||||
|
254 | xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight | |||
|
255 | ||||
|
256 | self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight) | |||
|
257 | ||||
|
258 | self.dataOut.channelList = range(self.systemHeaderObj.nChannels) | |||
|
259 | ||||
|
260 | self.dataOut.nCohInt = self.processingHeaderObj.nCohInt | |||
|
261 | ||||
|
262 | self.dataOut.flagShiftFFT = False | |||
|
263 | ||||
|
264 | self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada | |||
|
265 | ||||
|
266 | self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip | |||
|
267 | ||||
|
268 | self.dataOut.flagShiftFFT = False | |||
|
269 | ||||
|
270 | def getData(self): | |||
|
271 | """ | |||
|
272 | getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage" | |||
|
273 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de | |||
|
274 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" | |||
|
275 | ||||
|
276 | Ademas incrementa el contador del buffer en 1. | |||
|
277 | ||||
|
278 | Return: | |||
|
279 | data : retorna un perfil de voltages (alturas * canales) copiados desde el | |||
|
280 | buffer. Si no hay mas archivos a leer retorna None. | |||
|
281 | ||||
|
282 | Variables afectadas: | |||
|
283 | self.dataOut | |||
|
284 | self.profileIndex | |||
|
285 | ||||
|
286 | Affected: | |||
|
287 | self.dataOut | |||
|
288 | self.profileIndex | |||
|
289 | self.flagTimeBlock | |||
|
290 | self.flagIsNewBlock | |||
|
291 | """ | |||
|
292 | ||||
|
293 | if self.flagNoMoreFiles: | |||
|
294 | self.dataOut.flagNoData = True | |||
|
295 | print 'Process finished' | |||
|
296 | return 0 | |||
|
297 | ||||
|
298 | self.flagTimeBlock = 0 | |||
|
299 | self.flagIsNewBlock = 0 | |||
|
300 | ||||
|
301 | if self.__hasNotDataInBuffer(): | |||
|
302 | ||||
|
303 | if not( self.readNextBlock() ): | |||
|
304 | return 0 | |||
|
305 | ||||
|
306 | self.getFirstHeader() | |||
|
307 | ||||
|
308 | if self.datablock == None: | |||
|
309 | self.dataOut.flagNoData = True | |||
|
310 | return 0 | |||
|
311 | ||||
|
312 | self.dataOut.data = self.datablock[:,self.profileIndex,:] | |||
|
313 | ||||
|
314 | self.dataOut.flagNoData = False | |||
|
315 | ||||
|
316 | self.getBasicHeader() | |||
|
317 | ||||
|
318 | self.profileIndex += 1 | |||
|
319 | ||||
|
320 | self.dataOut.realtime = self.online | |||
|
321 | ||||
|
322 | return self.dataOut.data | |||
|
323 | ||||
|
324 | class VoltageWriter(JRODataWriter, Operation): | |||
|
325 | """ | |||
|
326 | Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura | |||
|
327 | de los datos siempre se realiza por bloques. | |||
|
328 | """ | |||
|
329 | ||||
|
330 | ext = ".r" | |||
|
331 | ||||
|
332 | optchar = "D" | |||
|
333 | ||||
|
334 | shapeBuffer = None | |||
|
335 | ||||
|
336 | ||||
|
337 | def __init__(self): | |||
|
338 | """ | |||
|
339 | Inicializador de la clase VoltageWriter para la escritura de datos de espectros. | |||
|
340 | ||||
|
341 | Affected: | |||
|
342 | self.dataOut | |||
|
343 | ||||
|
344 | Return: None | |||
|
345 | """ | |||
|
346 | Operation.__init__(self) | |||
|
347 | ||||
|
348 | self.nTotalBlocks = 0 | |||
|
349 | ||||
|
350 | self.profileIndex = 0 | |||
|
351 | ||||
|
352 | self.isConfig = False | |||
|
353 | ||||
|
354 | self.fp = None | |||
|
355 | ||||
|
356 | self.flagIsNewFile = 1 | |||
|
357 | ||||
|
358 | self.nTotalBlocks = 0 | |||
|
359 | ||||
|
360 | self.flagIsNewBlock = 0 | |||
|
361 | ||||
|
362 | self.setFile = None | |||
|
363 | ||||
|
364 | self.dtype = None | |||
|
365 | ||||
|
366 | self.path = None | |||
|
367 | ||||
|
368 | self.filename = None | |||
|
369 | ||||
|
370 | self.basicHeaderObj = BasicHeader(LOCALTIME) | |||
|
371 | ||||
|
372 | self.systemHeaderObj = SystemHeader() | |||
|
373 | ||||
|
374 | self.radarControllerHeaderObj = RadarControllerHeader() | |||
|
375 | ||||
|
376 | self.processingHeaderObj = ProcessingHeader() | |||
|
377 | ||||
|
378 | def hasAllDataInBuffer(self): | |||
|
379 | if self.profileIndex >= self.processingHeaderObj.profilesPerBlock: | |||
|
380 | return 1 | |||
|
381 | return 0 | |||
|
382 | ||||
|
383 | ||||
|
384 | def setBlockDimension(self): | |||
|
385 | """ | |||
|
386 | Obtiene las formas dimensionales del los subbloques de datos que componen un bloque | |||
|
387 | ||||
|
388 | Affected: | |||
|
389 | self.shape_spc_Buffer | |||
|
390 | self.shape_cspc_Buffer | |||
|
391 | self.shape_dc_Buffer | |||
|
392 | ||||
|
393 | Return: None | |||
|
394 | """ | |||
|
395 | self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock, | |||
|
396 | self.processingHeaderObj.nHeights, | |||
|
397 | self.systemHeaderObj.nChannels) | |||
|
398 | ||||
|
399 | self.datablock = numpy.zeros((self.systemHeaderObj.nChannels, | |||
|
400 | self.processingHeaderObj.profilesPerBlock, | |||
|
401 | self.processingHeaderObj.nHeights), | |||
|
402 | dtype=numpy.dtype('complex64')) | |||
|
403 | ||||
|
404 | ||||
|
405 | def writeBlock(self): | |||
|
406 | """ | |||
|
407 | Escribe el buffer en el file designado | |||
|
408 | ||||
|
409 | Affected: | |||
|
410 | self.profileIndex | |||
|
411 | self.flagIsNewFile | |||
|
412 | self.flagIsNewBlock | |||
|
413 | self.nTotalBlocks | |||
|
414 | self.blockIndex | |||
|
415 | ||||
|
416 | Return: None | |||
|
417 | """ | |||
|
418 | data = numpy.zeros( self.shapeBuffer, self.dtype ) | |||
|
419 | ||||
|
420 | junk = numpy.transpose(self.datablock, (1,2,0)) | |||
|
421 | ||||
|
422 | data['real'] = junk.real | |||
|
423 | data['imag'] = junk.imag | |||
|
424 | ||||
|
425 | data = data.reshape( (-1) ) | |||
|
426 | ||||
|
427 | data.tofile( self.fp ) | |||
|
428 | ||||
|
429 | self.datablock.fill(0) | |||
|
430 | ||||
|
431 | self.profileIndex = 0 | |||
|
432 | self.flagIsNewFile = 0 | |||
|
433 | self.flagIsNewBlock = 1 | |||
|
434 | ||||
|
435 | self.blockIndex += 1 | |||
|
436 | self.nTotalBlocks += 1 | |||
|
437 | ||||
|
438 | def putData(self): | |||
|
439 | """ | |||
|
440 | Setea un bloque de datos y luego los escribe en un file | |||
|
441 | ||||
|
442 | Affected: | |||
|
443 | self.flagIsNewBlock | |||
|
444 | self.profileIndex | |||
|
445 | ||||
|
446 | Return: | |||
|
447 | 0 : Si no hay data o no hay mas files que puedan escribirse | |||
|
448 | 1 : Si se escribio la data de un bloque en un file | |||
|
449 | """ | |||
|
450 | if self.dataOut.flagNoData: | |||
|
451 | return 0 | |||
|
452 | ||||
|
453 | self.flagIsNewBlock = 0 | |||
|
454 | ||||
|
455 | if self.dataOut.flagTimeBlock: | |||
|
456 | ||||
|
457 | self.datablock.fill(0) | |||
|
458 | self.profileIndex = 0 | |||
|
459 | self.setNextFile() | |||
|
460 | ||||
|
461 | if self.profileIndex == 0: | |||
|
462 | self.setBasicHeader() | |||
|
463 | ||||
|
464 | self.datablock[:,self.profileIndex,:] = self.dataOut.data | |||
|
465 | ||||
|
466 | self.profileIndex += 1 | |||
|
467 | ||||
|
468 | if self.hasAllDataInBuffer(): | |||
|
469 | #if self.flagIsNewFile: | |||
|
470 | self.writeNextBlock() | |||
|
471 | # self.setFirstHeader() | |||
|
472 | ||||
|
473 | return 1 | |||
|
474 | ||||
|
475 | def __getProcessFlags(self): | |||
|
476 | ||||
|
477 | processFlags = 0 | |||
|
478 | ||||
|
479 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) | |||
|
480 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) | |||
|
481 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) | |||
|
482 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) | |||
|
483 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
484 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) | |||
|
485 | ||||
|
486 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] | |||
|
487 | ||||
|
488 | ||||
|
489 | ||||
|
490 | datatypeValueList = [PROCFLAG.DATATYPE_CHAR, | |||
|
491 | PROCFLAG.DATATYPE_SHORT, | |||
|
492 | PROCFLAG.DATATYPE_LONG, | |||
|
493 | PROCFLAG.DATATYPE_INT64, | |||
|
494 | PROCFLAG.DATATYPE_FLOAT, | |||
|
495 | PROCFLAG.DATATYPE_DOUBLE] | |||
|
496 | ||||
|
497 | ||||
|
498 | for index in range(len(dtypeList)): | |||
|
499 | if self.dataOut.dtype == dtypeList[index]: | |||
|
500 | dtypeValue = datatypeValueList[index] | |||
|
501 | break | |||
|
502 | ||||
|
503 | processFlags += dtypeValue | |||
|
504 | ||||
|
505 | if self.dataOut.flagDecodeData: | |||
|
506 | processFlags += PROCFLAG.DECODE_DATA | |||
|
507 | ||||
|
508 | if self.dataOut.flagDeflipData: | |||
|
509 | processFlags += PROCFLAG.DEFLIP_DATA | |||
|
510 | ||||
|
511 | if self.dataOut.code != None: | |||
|
512 | processFlags += PROCFLAG.DEFINE_PROCESS_CODE | |||
|
513 | ||||
|
514 | if self.dataOut.nCohInt > 1: | |||
|
515 | processFlags += PROCFLAG.COHERENT_INTEGRATION | |||
|
516 | ||||
|
517 | return processFlags | |||
|
518 | ||||
|
519 | ||||
|
520 | def __getBlockSize(self): | |||
|
521 | ''' | |||
|
522 | Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage | |||
|
523 | ''' | |||
|
524 | ||||
|
525 | dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')]) | |||
|
526 | dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')]) | |||
|
527 | dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')]) | |||
|
528 | dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')]) | |||
|
529 | dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
530 | dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')]) | |||
|
531 | ||||
|
532 | dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5] | |||
|
533 | datatypeValueList = [1,2,4,8,4,8] | |||
|
534 | for index in range(len(dtypeList)): | |||
|
535 | if self.dataOut.dtype == dtypeList[index]: | |||
|
536 | datatypeValue = datatypeValueList[index] | |||
|
537 | break | |||
|
538 | ||||
|
539 | blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * datatypeValue * 2) | |||
|
540 | ||||
|
541 | return blocksize | |||
|
542 | ||||
|
543 | def setFirstHeader(self): | |||
|
544 | ||||
|
545 | """ | |||
|
546 | Obtiene una copia del First Header | |||
|
547 | ||||
|
548 | Affected: | |||
|
549 | self.systemHeaderObj | |||
|
550 | self.radarControllerHeaderObj | |||
|
551 | self.dtype | |||
|
552 | ||||
|
553 | Return: | |||
|
554 | None | |||
|
555 | """ | |||
|
556 | ||||
|
557 | self.systemHeaderObj = self.dataOut.systemHeaderObj.copy() | |||
|
558 | self.systemHeaderObj.nChannels = self.dataOut.nChannels | |||
|
559 | self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy() | |||
|
560 | ||||
|
561 | self.setBasicHeader() | |||
|
562 | ||||
|
563 | processingHeaderSize = 40 # bytes | |||
|
564 | self.processingHeaderObj.dtype = 0 # Voltage | |||
|
565 | self.processingHeaderObj.blockSize = self.__getBlockSize() | |||
|
566 | self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock | |||
|
567 | self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile | |||
|
568 | self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows | |||
|
569 | self.processingHeaderObj.processFlags = self.__getProcessFlags() | |||
|
570 | self.processingHeaderObj.nCohInt = self.dataOut.nCohInt | |||
|
571 | self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage | |||
|
572 | self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage | |||
|
573 | ||||
|
574 | # if self.dataOut.code != None: | |||
|
575 | # self.processingHeaderObj.code = self.dataOut.code | |||
|
576 | # self.processingHeaderObj.nCode = self.dataOut.nCode | |||
|
577 | # self.processingHeaderObj.nBaud = self.dataOut.nBaud | |||
|
578 | # codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud) | |||
|
579 | # processingHeaderSize += codesize | |||
|
580 | ||||
|
581 | if self.processingHeaderObj.nWindows != 0: | |||
|
582 | self.processingHeaderObj.firstHeight = self.dataOut.heightList[0] | |||
|
583 | self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] | |||
|
584 | self.processingHeaderObj.nHeights = self.dataOut.nHeights | |||
|
585 | self.processingHeaderObj.samplesWin = self.dataOut.nHeights | |||
|
586 | processingHeaderSize += 12 | |||
|
587 | ||||
|
588 | self.processingHeaderObj.size = processingHeaderSize No newline at end of file |
@@ -0,0 +1,3 | |||||
|
1 | from jroIO_voltage import * | |||
|
2 | from jroIO_spectra import * | |||
|
3 | from jroIO_heispectra import * |
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
@@ -0,0 +1,253 | |||||
|
1 | ''' | |||
|
2 | ''' | |||
|
3 | ||||
|
4 | class ProcessingUnit: | |||
|
5 | ||||
|
6 | """ | |||
|
7 | Esta es la clase base para el procesamiento de datos. | |||
|
8 | ||||
|
9 | Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser: | |||
|
10 | - Metodos internos (callMethod) | |||
|
11 | - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos | |||
|
12 | tienen que ser agreagados con el metodo "add". | |||
|
13 | ||||
|
14 | """ | |||
|
15 | # objeto de datos de entrada (Voltage, Spectra o Correlation) | |||
|
16 | dataIn = None | |||
|
17 | dataInList = [] | |||
|
18 | ||||
|
19 | # objeto de datos de entrada (Voltage, Spectra o Correlation) | |||
|
20 | dataOut = None | |||
|
21 | ||||
|
22 | operations2RunDict = None | |||
|
23 | ||||
|
24 | isConfig = False | |||
|
25 | ||||
|
26 | ||||
|
27 | def __init__(self): | |||
|
28 | ||||
|
29 | self.dataIn = None | |||
|
30 | self.dataInList = [] | |||
|
31 | ||||
|
32 | self.dataOut = {} | |||
|
33 | ||||
|
34 | self.operations2RunDict = {} | |||
|
35 | ||||
|
36 | self.isConfig = False | |||
|
37 | ||||
|
38 | def addOperation(self, opObj, objId): | |||
|
39 | ||||
|
40 | """ | |||
|
41 | Agrega un objeto del tipo "Operation" (opObj) a la lista de objetos "self.objectList" y retorna el | |||
|
42 | identificador asociado a este objeto. | |||
|
43 | ||||
|
44 | Input: | |||
|
45 | ||||
|
46 | object : objeto de la clase "Operation" | |||
|
47 | ||||
|
48 | Return: | |||
|
49 | ||||
|
50 | objId : identificador del objeto, necesario para ejecutar la operacion | |||
|
51 | """ | |||
|
52 | ||||
|
53 | self.operations2RunDict[objId] = opObj | |||
|
54 | ||||
|
55 | return objId | |||
|
56 | ||||
|
57 | def operation(self, **kwargs): | |||
|
58 | ||||
|
59 | """ | |||
|
60 | Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los | |||
|
61 | atributos del objeto dataOut | |||
|
62 | ||||
|
63 | Input: | |||
|
64 | ||||
|
65 | **kwargs : Diccionario de argumentos de la funcion a ejecutar | |||
|
66 | """ | |||
|
67 | ||||
|
68 | raise ValueError, "ImplementedError" | |||
|
69 | ||||
|
70 | def callMethod(self, name, **kwargs): | |||
|
71 | ||||
|
72 | """ | |||
|
73 | Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase. | |||
|
74 | ||||
|
75 | Input: | |||
|
76 | name : nombre del metodo a ejecutar | |||
|
77 | ||||
|
78 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. | |||
|
79 | ||||
|
80 | """ | |||
|
81 | if name == 'run': | |||
|
82 | ||||
|
83 | if not self.checkInputs(): | |||
|
84 | self.dataOut.flagNoData = True | |||
|
85 | return False | |||
|
86 | else: | |||
|
87 | #Si no es un metodo RUN la entrada es la misma dataOut (interna) | |||
|
88 | if self.dataOut.isEmpty(): | |||
|
89 | return False | |||
|
90 | ||||
|
91 | #Getting the pointer to method | |||
|
92 | methodToCall = getattr(self, name) | |||
|
93 | ||||
|
94 | #Executing the self method | |||
|
95 | methodToCall(**kwargs) | |||
|
96 | ||||
|
97 | #Checkin the outputs | |||
|
98 | ||||
|
99 | # if name == 'run': | |||
|
100 | # pass | |||
|
101 | # else: | |||
|
102 | # pass | |||
|
103 | # | |||
|
104 | # if name != 'run': | |||
|
105 | # return True | |||
|
106 | ||||
|
107 | if self.dataOut.isEmpty(): | |||
|
108 | return False | |||
|
109 | ||||
|
110 | return True | |||
|
111 | ||||
|
112 | def callObject(self, objId, **kwargs): | |||
|
113 | ||||
|
114 | """ | |||
|
115 | Ejecuta la operacion asociada al identificador del objeto "objId" | |||
|
116 | ||||
|
117 | Input: | |||
|
118 | ||||
|
119 | objId : identificador del objeto a ejecutar | |||
|
120 | ||||
|
121 | **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. | |||
|
122 | ||||
|
123 | Return: | |||
|
124 | ||||
|
125 | None | |||
|
126 | """ | |||
|
127 | ||||
|
128 | if self.dataOut.isEmpty(): | |||
|
129 | return False | |||
|
130 | ||||
|
131 | externalProcObj = self.operations2RunDict[objId] | |||
|
132 | ||||
|
133 | externalProcObj.run(self.dataOut, **kwargs) | |||
|
134 | ||||
|
135 | return True | |||
|
136 | ||||
|
137 | def call(self, opType, opName=None, opId=None, **kwargs): | |||
|
138 | ||||
|
139 | """ | |||
|
140 | Return True si ejecuta la operacion "operationConf.name" con los | |||
|
141 | argumentos "**kwargs". False si la operacion no se ha ejecutado. | |||
|
142 | La operacion puede ser de dos tipos: | |||
|
143 | ||||
|
144 | 1. Un metodo propio de esta clase: | |||
|
145 | ||||
|
146 | operation.type = "self" | |||
|
147 | ||||
|
148 | 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella: | |||
|
149 | operation.type = "other". | |||
|
150 | ||||
|
151 | Este objeto de tipo Operation debe de haber sido agregado antes con el metodo: | |||
|
152 | "addOperation" e identificado con el operation.id | |||
|
153 | ||||
|
154 | ||||
|
155 | con el id de la operacion. | |||
|
156 | ||||
|
157 | Input: | |||
|
158 | ||||
|
159 | Operation : Objeto del tipo operacion con los atributos: name, type y id. | |||
|
160 | ||||
|
161 | """ | |||
|
162 | ||||
|
163 | if opType == 'self': | |||
|
164 | ||||
|
165 | if not opName: | |||
|
166 | raise IOError, "opName parameter should be defined" | |||
|
167 | ||||
|
168 | sts = self.callMethod(opName, **kwargs) | |||
|
169 | ||||
|
170 | if opType == 'other' or opType == 'external': | |||
|
171 | ||||
|
172 | if not opId: | |||
|
173 | raise IOError, "opId parameter should be defined" | |||
|
174 | ||||
|
175 | if opId not in self.operations2RunDict.keys(): | |||
|
176 | raise IOError, "This id operation have not been registered" | |||
|
177 | ||||
|
178 | sts = self.callObject(opId, **kwargs) | |||
|
179 | ||||
|
180 | return sts | |||
|
181 | ||||
|
182 | def setInput(self, dataIn): | |||
|
183 | ||||
|
184 | self.dataIn = dataIn | |||
|
185 | self.dataInList.append(dataIn) | |||
|
186 | ||||
|
187 | def getOutputObj(self): | |||
|
188 | ||||
|
189 | return self.dataOut | |||
|
190 | ||||
|
191 | def checkInputs(self): | |||
|
192 | ||||
|
193 | for thisDataIn in self.dataInList: | |||
|
194 | ||||
|
195 | if thisDataIn.isEmpty(): | |||
|
196 | return False | |||
|
197 | ||||
|
198 | return True | |||
|
199 | ||||
|
200 | def setup(self): | |||
|
201 | ||||
|
202 | raise ValueError, "Not implemented" | |||
|
203 | ||||
|
204 | def run(self): | |||
|
205 | ||||
|
206 | raise ValueError, "Not implemented" | |||
|
207 | ||||
|
208 | class Operation(): | |||
|
209 | ||||
|
210 | """ | |||
|
211 | Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit | |||
|
212 | y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de | |||
|
213 | acumulacion dentro de esta clase | |||
|
214 | ||||
|
215 | Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer) | |||
|
216 | ||||
|
217 | """ | |||
|
218 | ||||
|
219 | __buffer = None | |||
|
220 | isConfig = False | |||
|
221 | ||||
|
222 | def __init__(self): | |||
|
223 | ||||
|
224 | self.__buffer = None | |||
|
225 | self.isConfig = False | |||
|
226 | ||||
|
227 | def setup(self): | |||
|
228 | ||||
|
229 | self.isConfig = True | |||
|
230 | ||||
|
231 | raise ValueError, "Not implemented" | |||
|
232 | ||||
|
233 | def run(self, dataIn, **kwargs): | |||
|
234 | ||||
|
235 | """ | |||
|
236 | Realiza las operaciones necesarias sobre la dataIn.data y actualiza los atributos del objeto dataIn. | |||
|
237 | ||||
|
238 | Input: | |||
|
239 | ||||
|
240 | dataIn : objeto del tipo JROData | |||
|
241 | ||||
|
242 | Return: | |||
|
243 | ||||
|
244 | None | |||
|
245 | ||||
|
246 | Affected: | |||
|
247 | __buffer : buffer de recepcion de datos. | |||
|
248 | ||||
|
249 | """ | |||
|
250 | if not self.isConfig: | |||
|
251 | self.setup(**kwargs) | |||
|
252 | ||||
|
253 | raise ValueError, "ImplementedError" No newline at end of file |
@@ -0,0 +1,339 | |||||
|
1 | import numpy | |||
|
2 | ||||
|
3 | from jroproc_base import ProcessingUnit, Operation | |||
|
4 | from model.data.jrodata import SpectraHeis | |||
|
5 | ||||
|
6 | class SpectraHeisProc(ProcessingUnit): | |||
|
7 | ||||
|
8 | def __init__(self): | |||
|
9 | ||||
|
10 | ProcessingUnit.__init__(self) | |||
|
11 | ||||
|
12 | # self.buffer = None | |||
|
13 | # self.firstdatatime = None | |||
|
14 | # self.profIndex = 0 | |||
|
15 | self.dataOut = SpectraHeis() | |||
|
16 | ||||
|
17 | def __updateObjFromInput(self): | |||
|
18 | ||||
|
19 | self.dataOut.timeZone = self.dataIn.timeZone | |||
|
20 | self.dataOut.dstFlag = self.dataIn.dstFlag | |||
|
21 | self.dataOut.errorCount = self.dataIn.errorCount | |||
|
22 | self.dataOut.useLocalTime = self.dataIn.useLocalTime | |||
|
23 | ||||
|
24 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()# | |||
|
25 | self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()# | |||
|
26 | self.dataOut.channelList = self.dataIn.channelList | |||
|
27 | self.dataOut.heightList = self.dataIn.heightList | |||
|
28 | # self.dataOut.dtype = self.dataIn.dtype | |||
|
29 | self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
30 | # self.dataOut.nHeights = self.dataIn.nHeights | |||
|
31 | # self.dataOut.nChannels = self.dataIn.nChannels | |||
|
32 | self.dataOut.nBaud = self.dataIn.nBaud | |||
|
33 | self.dataOut.nCode = self.dataIn.nCode | |||
|
34 | self.dataOut.code = self.dataIn.code | |||
|
35 | # self.dataOut.nProfiles = 1 | |||
|
36 | # self.dataOut.nProfiles = self.dataOut.nFFTPoints | |||
|
37 | self.dataOut.nFFTPoints = self.dataIn.nHeights | |||
|
38 | # self.dataOut.channelIndexList = self.dataIn.channelIndexList | |||
|
39 | # self.dataOut.flagNoData = self.dataIn.flagNoData | |||
|
40 | self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock | |||
|
41 | self.dataOut.utctime = self.dataIn.utctime | |||
|
42 | # self.dataOut.utctime = self.firstdatatime | |||
|
43 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada | |||
|
44 | self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip | |||
|
45 | # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT | |||
|
46 | self.dataOut.nCohInt = self.dataIn.nCohInt | |||
|
47 | self.dataOut.nIncohInt = 1 | |||
|
48 | # self.dataOut.ippSeconds= self.dataIn.ippSeconds | |||
|
49 | self.dataOut.windowOfFilter = self.dataIn.windowOfFilter | |||
|
50 | ||||
|
51 | self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nIncohInt | |||
|
52 | # self.dataOut.set=self.dataIn.set | |||
|
53 | # self.dataOut.deltaHeight=self.dataIn.deltaHeight | |||
|
54 | ||||
|
55 | ||||
|
56 | def __updateObjFromFits(self): | |||
|
57 | self.dataOut.utctime = self.dataIn.utctime | |||
|
58 | self.dataOut.channelIndexList = self.dataIn.channelIndexList | |||
|
59 | ||||
|
60 | self.dataOut.channelList = self.dataIn.channelList | |||
|
61 | self.dataOut.heightList = self.dataIn.heightList | |||
|
62 | self.dataOut.data_spc = self.dataIn.data | |||
|
63 | self.dataOut.timeInterval = self.dataIn.timeInterval | |||
|
64 | self.dataOut.timeZone = self.dataIn.timeZone | |||
|
65 | self.dataOut.useLocalTime = True | |||
|
66 | # self.dataOut. | |||
|
67 | # self.dataOut. | |||
|
68 | ||||
|
69 | def __getFft(self): | |||
|
70 | ||||
|
71 | fft_volt = numpy.fft.fft(self.dataIn.data, axis=1) | |||
|
72 | fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,)) | |||
|
73 | spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))/(self.dataOut.nFFTPoints) | |||
|
74 | self.dataOut.data_spc = spc | |||
|
75 | ||||
|
76 | def run(self): | |||
|
77 | ||||
|
78 | self.dataOut.flagNoData = True | |||
|
79 | ||||
|
80 | if self.dataIn.type == "Fits": | |||
|
81 | self.__updateObjFromFits() | |||
|
82 | self.dataOut.flagNoData = False | |||
|
83 | return | |||
|
84 | ||||
|
85 | if self.dataIn.type == "SpectraHeis": | |||
|
86 | self.dataOut.copy(self.dataIn) | |||
|
87 | return | |||
|
88 | ||||
|
89 | if self.dataIn.type == "Voltage": | |||
|
90 | self.__updateObjFromInput() | |||
|
91 | self.__getFft() | |||
|
92 | self.dataOut.flagNoData = False | |||
|
93 | ||||
|
94 | return | |||
|
95 | ||||
|
96 | raise ValueError, "The type object %s is not valid"%(self.dataIn.type) | |||
|
97 | ||||
|
98 | ||||
|
99 | def selectChannels(self, channelList): | |||
|
100 | ||||
|
101 | channelIndexList = [] | |||
|
102 | ||||
|
103 | for channel in channelList: | |||
|
104 | index = self.dataOut.channelList.index(channel) | |||
|
105 | channelIndexList.append(index) | |||
|
106 | ||||
|
107 | self.selectChannelsByIndex(channelIndexList) | |||
|
108 | ||||
|
109 | def selectChannelsByIndex(self, channelIndexList): | |||
|
110 | """ | |||
|
111 | Selecciona un bloque de datos en base a canales segun el channelIndexList | |||
|
112 | ||||
|
113 | Input: | |||
|
114 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] | |||
|
115 | ||||
|
116 | Affected: | |||
|
117 | self.dataOut.data | |||
|
118 | self.dataOut.channelIndexList | |||
|
119 | self.dataOut.nChannels | |||
|
120 | self.dataOut.m_ProcessingHeader.totalSpectra | |||
|
121 | self.dataOut.systemHeaderObj.numChannels | |||
|
122 | self.dataOut.m_ProcessingHeader.blockSize | |||
|
123 | ||||
|
124 | Return: | |||
|
125 | None | |||
|
126 | """ | |||
|
127 | ||||
|
128 | for channelIndex in channelIndexList: | |||
|
129 | if channelIndex not in self.dataOut.channelIndexList: | |||
|
130 | print channelIndexList | |||
|
131 | raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex | |||
|
132 | ||||
|
133 | # nChannels = len(channelIndexList) | |||
|
134 | ||||
|
135 | data_spc = self.dataOut.data_spc[channelIndexList,:] | |||
|
136 | ||||
|
137 | self.dataOut.data_spc = data_spc | |||
|
138 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] | |||
|
139 | ||||
|
140 | return 1 | |||
|
141 | ||||
|
142 | class IncohInt4SpectraHeis(Operation): | |||
|
143 | ||||
|
144 | isConfig = False | |||
|
145 | ||||
|
146 | __profIndex = 0 | |||
|
147 | __withOverapping = False | |||
|
148 | ||||
|
149 | __byTime = False | |||
|
150 | __initime = None | |||
|
151 | __lastdatatime = None | |||
|
152 | __integrationtime = None | |||
|
153 | ||||
|
154 | __buffer = None | |||
|
155 | ||||
|
156 | __dataReady = False | |||
|
157 | ||||
|
158 | n = None | |||
|
159 | ||||
|
160 | ||||
|
161 | def __init__(self): | |||
|
162 | ||||
|
163 | Operation.__init__(self) | |||
|
164 | # self.isConfig = False | |||
|
165 | ||||
|
166 | def setup(self, n=None, timeInterval=None, overlapping=False): | |||
|
167 | """ | |||
|
168 | Set the parameters of the integration class. | |||
|
169 | ||||
|
170 | Inputs: | |||
|
171 | ||||
|
172 | n : Number of coherent integrations | |||
|
173 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work | |||
|
174 | overlapping : | |||
|
175 | ||||
|
176 | """ | |||
|
177 | ||||
|
178 | self.__initime = None | |||
|
179 | self.__lastdatatime = 0 | |||
|
180 | self.__buffer = None | |||
|
181 | self.__dataReady = False | |||
|
182 | ||||
|
183 | ||||
|
184 | if n == None and timeInterval == None: | |||
|
185 | raise ValueError, "n or timeInterval should be specified ..." | |||
|
186 | ||||
|
187 | if n != None: | |||
|
188 | self.n = n | |||
|
189 | self.__byTime = False | |||
|
190 | else: | |||
|
191 | self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line | |||
|
192 | self.n = 9999 | |||
|
193 | self.__byTime = True | |||
|
194 | ||||
|
195 | if overlapping: | |||
|
196 | self.__withOverapping = True | |||
|
197 | self.__buffer = None | |||
|
198 | else: | |||
|
199 | self.__withOverapping = False | |||
|
200 | self.__buffer = 0 | |||
|
201 | ||||
|
202 | self.__profIndex = 0 | |||
|
203 | ||||
|
204 | def putData(self, data): | |||
|
205 | ||||
|
206 | """ | |||
|
207 | Add a profile to the __buffer and increase in one the __profileIndex | |||
|
208 | ||||
|
209 | """ | |||
|
210 | ||||
|
211 | if not self.__withOverapping: | |||
|
212 | self.__buffer += data.copy() | |||
|
213 | self.__profIndex += 1 | |||
|
214 | return | |||
|
215 | ||||
|
216 | #Overlapping data | |||
|
217 | nChannels, nHeis = data.shape | |||
|
218 | data = numpy.reshape(data, (1, nChannels, nHeis)) | |||
|
219 | ||||
|
220 | #If the buffer is empty then it takes the data value | |||
|
221 | if self.__buffer == None: | |||
|
222 | self.__buffer = data | |||
|
223 | self.__profIndex += 1 | |||
|
224 | return | |||
|
225 | ||||
|
226 | #If the buffer length is lower than n then stakcing the data value | |||
|
227 | if self.__profIndex < self.n: | |||
|
228 | self.__buffer = numpy.vstack((self.__buffer, data)) | |||
|
229 | self.__profIndex += 1 | |||
|
230 | return | |||
|
231 | ||||
|
232 | #If the buffer length is equal to n then replacing the last buffer value with the data value | |||
|
233 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) | |||
|
234 | self.__buffer[self.n-1] = data | |||
|
235 | self.__profIndex = self.n | |||
|
236 | return | |||
|
237 | ||||
|
238 | ||||
|
239 | def pushData(self): | |||
|
240 | """ | |||
|
241 | Return the sum of the last profiles and the profiles used in the sum. | |||
|
242 | ||||
|
243 | Affected: | |||
|
244 | ||||
|
245 | self.__profileIndex | |||
|
246 | ||||
|
247 | """ | |||
|
248 | ||||
|
249 | if not self.__withOverapping: | |||
|
250 | data = self.__buffer | |||
|
251 | n = self.__profIndex | |||
|
252 | ||||
|
253 | self.__buffer = 0 | |||
|
254 | self.__profIndex = 0 | |||
|
255 | ||||
|
256 | return data, n | |||
|
257 | ||||
|
258 | #Integration with Overlapping | |||
|
259 | data = numpy.sum(self.__buffer, axis=0) | |||
|
260 | n = self.__profIndex | |||
|
261 | ||||
|
262 | return data, n | |||
|
263 | ||||
|
264 | def byProfiles(self, data): | |||
|
265 | ||||
|
266 | self.__dataReady = False | |||
|
267 | avgdata = None | |||
|
268 | # n = None | |||
|
269 | ||||
|
270 | self.putData(data) | |||
|
271 | ||||
|
272 | if self.__profIndex == self.n: | |||
|
273 | ||||
|
274 | avgdata, n = self.pushData() | |||
|
275 | self.__dataReady = True | |||
|
276 | ||||
|
277 | return avgdata | |||
|
278 | ||||
|
279 | def byTime(self, data, datatime): | |||
|
280 | ||||
|
281 | self.__dataReady = False | |||
|
282 | avgdata = None | |||
|
283 | n = None | |||
|
284 | ||||
|
285 | self.putData(data) | |||
|
286 | ||||
|
287 | if (datatime - self.__initime) >= self.__integrationtime: | |||
|
288 | avgdata, n = self.pushData() | |||
|
289 | self.n = n | |||
|
290 | self.__dataReady = True | |||
|
291 | ||||
|
292 | return avgdata | |||
|
293 | ||||
|
294 | def integrate(self, data, datatime=None): | |||
|
295 | ||||
|
296 | if self.__initime == None: | |||
|
297 | self.__initime = datatime | |||
|
298 | ||||
|
299 | if self.__byTime: | |||
|
300 | avgdata = self.byTime(data, datatime) | |||
|
301 | else: | |||
|
302 | avgdata = self.byProfiles(data) | |||
|
303 | ||||
|
304 | ||||
|
305 | self.__lastdatatime = datatime | |||
|
306 | ||||
|
307 | if avgdata == None: | |||
|
308 | return None, None | |||
|
309 | ||||
|
310 | avgdatatime = self.__initime | |||
|
311 | ||||
|
312 | deltatime = datatime -self.__lastdatatime | |||
|
313 | ||||
|
314 | if not self.__withOverapping: | |||
|
315 | self.__initime = datatime | |||
|
316 | else: | |||
|
317 | self.__initime += deltatime | |||
|
318 | ||||
|
319 | return avgdata, avgdatatime | |||
|
320 | ||||
|
321 | def run(self, dataOut, **kwargs): | |||
|
322 | ||||
|
323 | if not self.isConfig: | |||
|
324 | self.setup(**kwargs) | |||
|
325 | self.isConfig = True | |||
|
326 | ||||
|
327 | avgdata, avgdatatime = self.integrate(dataOut.data_spc, dataOut.utctime) | |||
|
328 | ||||
|
329 | # dataOut.timeInterval *= n | |||
|
330 | dataOut.flagNoData = True | |||
|
331 | ||||
|
332 | if self.__dataReady: | |||
|
333 | dataOut.data_spc = avgdata | |||
|
334 | dataOut.nIncohInt *= self.n | |||
|
335 | # dataOut.nCohInt *= self.n | |||
|
336 | dataOut.utctime = avgdatatime | |||
|
337 | dataOut.timeInterval = dataOut.ippSeconds * dataOut.nIncohInt | |||
|
338 | # dataOut.timeInterval = self.__timeInterval*self.n | |||
|
339 | dataOut.flagNoData = False No newline at end of file |
This diff has been collapsed as it changes many lines, (1024 lines changed) Show them Hide them | |||||
@@ -0,0 +1,1024 | |||||
|
1 | import numpy | |||
|
2 | ||||
|
3 | from jroproc_base import ProcessingUnit, Operation | |||
|
4 | from model.data.jrodata import Spectra | |||
|
5 | ||||
|
6 | class SpectraProc(ProcessingUnit): | |||
|
7 | ||||
|
8 | def __init__(self): | |||
|
9 | ||||
|
10 | ProcessingUnit.__init__(self) | |||
|
11 | ||||
|
12 | self.buffer = None | |||
|
13 | self.firstdatatime = None | |||
|
14 | self.profIndex = 0 | |||
|
15 | self.dataOut = Spectra() | |||
|
16 | ||||
|
17 | def __updateObjFromInput(self): | |||
|
18 | ||||
|
19 | self.dataOut.timeZone = self.dataIn.timeZone | |||
|
20 | self.dataOut.dstFlag = self.dataIn.dstFlag | |||
|
21 | self.dataOut.errorCount = self.dataIn.errorCount | |||
|
22 | self.dataOut.useLocalTime = self.dataIn.useLocalTime | |||
|
23 | ||||
|
24 | self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy() | |||
|
25 | self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy() | |||
|
26 | self.dataOut.channelList = self.dataIn.channelList | |||
|
27 | self.dataOut.heightList = self.dataIn.heightList | |||
|
28 | self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')]) | |||
|
29 | # self.dataOut.nHeights = self.dataIn.nHeights | |||
|
30 | # self.dataOut.nChannels = self.dataIn.nChannels | |||
|
31 | self.dataOut.nBaud = self.dataIn.nBaud | |||
|
32 | self.dataOut.nCode = self.dataIn.nCode | |||
|
33 | self.dataOut.code = self.dataIn.code | |||
|
34 | self.dataOut.nProfiles = self.dataOut.nFFTPoints | |||
|
35 | # self.dataOut.channelIndexList = self.dataIn.channelIndexList | |||
|
36 | self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock | |||
|
37 | self.dataOut.utctime = self.firstdatatime | |||
|
38 | self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada | |||
|
39 | self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip | |||
|
40 | # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT | |||
|
41 | self.dataOut.nCohInt = self.dataIn.nCohInt | |||
|
42 | self.dataOut.nIncohInt = 1 | |||
|
43 | # self.dataOut.ippSeconds = self.dataIn.ippSeconds | |||
|
44 | self.dataOut.windowOfFilter = self.dataIn.windowOfFilter | |||
|
45 | ||||
|
46 | self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt | |||
|
47 | self.dataOut.frequency = self.dataIn.frequency | |||
|
48 | self.dataOut.realtime = self.dataIn.realtime | |||
|
49 | ||||
|
50 | def __getFft(self): | |||
|
51 | """ | |||
|
52 | Convierte valores de Voltaje a Spectra | |||
|
53 | ||||
|
54 | Affected: | |||
|
55 | self.dataOut.data_spc | |||
|
56 | self.dataOut.data_cspc | |||
|
57 | self.dataOut.data_dc | |||
|
58 | self.dataOut.heightList | |||
|
59 | self.profIndex | |||
|
60 | self.buffer | |||
|
61 | self.dataOut.flagNoData | |||
|
62 | """ | |||
|
63 | fft_volt = numpy.fft.fft(self.buffer,n=self.dataOut.nFFTPoints,axis=1) | |||
|
64 | fft_volt = fft_volt.astype(numpy.dtype('complex')) | |||
|
65 | dc = fft_volt[:,0,:] | |||
|
66 | ||||
|
67 | #calculo de self-spectra | |||
|
68 | fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,)) | |||
|
69 | spc = fft_volt * numpy.conjugate(fft_volt) | |||
|
70 | spc = spc.real | |||
|
71 | ||||
|
72 | blocksize = 0 | |||
|
73 | blocksize += dc.size | |||
|
74 | blocksize += spc.size | |||
|
75 | ||||
|
76 | cspc = None | |||
|
77 | pairIndex = 0 | |||
|
78 | if self.dataOut.pairsList != None: | |||
|
79 | #calculo de cross-spectra | |||
|
80 | cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex') | |||
|
81 | for pair in self.dataOut.pairsList: | |||
|
82 | cspc[pairIndex,:,:] = fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:]) | |||
|
83 | pairIndex += 1 | |||
|
84 | blocksize += cspc.size | |||
|
85 | ||||
|
86 | self.dataOut.data_spc = spc | |||
|
87 | self.dataOut.data_cspc = cspc | |||
|
88 | self.dataOut.data_dc = dc | |||
|
89 | self.dataOut.blockSize = blocksize | |||
|
90 | self.dataOut.flagShiftFFT = False | |||
|
91 | ||||
|
92 | def run(self, nProfiles=None, nFFTPoints=None, pairsList=[], ippFactor=None): | |||
|
93 | ||||
|
94 | self.dataOut.flagNoData = True | |||
|
95 | ||||
|
96 | if self.dataIn.type == "Spectra": | |||
|
97 | self.dataOut.copy(self.dataIn) | |||
|
98 | return True | |||
|
99 | ||||
|
100 | if self.dataIn.type == "Voltage": | |||
|
101 | ||||
|
102 | if nFFTPoints == None: | |||
|
103 | raise ValueError, "This SpectraProc.run() need nFFTPoints input variable" | |||
|
104 | ||||
|
105 | nProfiles = nFFTPoints | |||
|
106 | ||||
|
107 | # if pairsList == None: | |||
|
108 | # nPairs = 0 | |||
|
109 | # else: | |||
|
110 | # nPairs = len(pairsList) | |||
|
111 | ||||
|
112 | if ippFactor == None: | |||
|
113 | ippFactor = 1 | |||
|
114 | self.dataOut.ippFactor = ippFactor | |||
|
115 | ||||
|
116 | self.dataOut.nFFTPoints = nFFTPoints | |||
|
117 | self.dataOut.pairsList = pairsList | |||
|
118 | # self.dataOut.nPairs = nPairs | |||
|
119 | ||||
|
120 | if self.buffer == None: | |||
|
121 | self.buffer = numpy.zeros((self.dataIn.nChannels, | |||
|
122 | nProfiles, | |||
|
123 | self.dataIn.nHeights), | |||
|
124 | dtype='complex') | |||
|
125 | ||||
|
126 | ||||
|
127 | self.buffer[:,self.profIndex,:] = self.dataIn.data.copy() | |||
|
128 | self.profIndex += 1 | |||
|
129 | ||||
|
130 | if self.firstdatatime == None: | |||
|
131 | self.firstdatatime = self.dataIn.utctime | |||
|
132 | ||||
|
133 | if self.profIndex == nProfiles: | |||
|
134 | self.__updateObjFromInput() | |||
|
135 | self.__getFft() | |||
|
136 | ||||
|
137 | self.dataOut.flagNoData = False | |||
|
138 | ||||
|
139 | self.buffer = None | |||
|
140 | self.firstdatatime = None | |||
|
141 | self.profIndex = 0 | |||
|
142 | ||||
|
143 | return True | |||
|
144 | ||||
|
145 | raise ValueError, "The type object %s is not valid"%(self.dataIn.type) | |||
|
146 | ||||
|
147 | def selectChannels(self, channelList): | |||
|
148 | ||||
|
149 | channelIndexList = [] | |||
|
150 | ||||
|
151 | for channel in channelList: | |||
|
152 | index = self.dataOut.channelList.index(channel) | |||
|
153 | channelIndexList.append(index) | |||
|
154 | ||||
|
155 | self.selectChannelsByIndex(channelIndexList) | |||
|
156 | ||||
|
157 | def selectChannelsByIndex(self, channelIndexList): | |||
|
158 | """ | |||
|
159 | Selecciona un bloque de datos en base a canales segun el channelIndexList | |||
|
160 | ||||
|
161 | Input: | |||
|
162 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] | |||
|
163 | ||||
|
164 | Affected: | |||
|
165 | self.dataOut.data_spc | |||
|
166 | self.dataOut.channelIndexList | |||
|
167 | self.dataOut.nChannels | |||
|
168 | ||||
|
169 | Return: | |||
|
170 | None | |||
|
171 | """ | |||
|
172 | ||||
|
173 | for channelIndex in channelIndexList: | |||
|
174 | if channelIndex not in self.dataOut.channelIndexList: | |||
|
175 | print channelIndexList | |||
|
176 | raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex | |||
|
177 | ||||
|
178 | # nChannels = len(channelIndexList) | |||
|
179 | ||||
|
180 | data_spc = self.dataOut.data_spc[channelIndexList,:] | |||
|
181 | ||||
|
182 | self.dataOut.data_spc = data_spc | |||
|
183 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] | |||
|
184 | # self.dataOut.nChannels = nChannels | |||
|
185 | ||||
|
186 | return 1 | |||
|
187 | ||||
|
188 | def selectHeights(self, minHei, maxHei): | |||
|
189 | """ | |||
|
190 | Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango | |||
|
191 | minHei <= height <= maxHei | |||
|
192 | ||||
|
193 | Input: | |||
|
194 | minHei : valor minimo de altura a considerar | |||
|
195 | maxHei : valor maximo de altura a considerar | |||
|
196 | ||||
|
197 | Affected: | |||
|
198 | Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex | |||
|
199 | ||||
|
200 | Return: | |||
|
201 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |||
|
202 | """ | |||
|
203 | if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): | |||
|
204 | raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |||
|
205 | ||||
|
206 | if (maxHei > self.dataOut.heightList[-1]): | |||
|
207 | maxHei = self.dataOut.heightList[-1] | |||
|
208 | # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |||
|
209 | ||||
|
210 | minIndex = 0 | |||
|
211 | maxIndex = 0 | |||
|
212 | heights = self.dataOut.heightList | |||
|
213 | ||||
|
214 | inda = numpy.where(heights >= minHei) | |||
|
215 | indb = numpy.where(heights <= maxHei) | |||
|
216 | ||||
|
217 | try: | |||
|
218 | minIndex = inda[0][0] | |||
|
219 | except: | |||
|
220 | minIndex = 0 | |||
|
221 | ||||
|
222 | try: | |||
|
223 | maxIndex = indb[0][-1] | |||
|
224 | except: | |||
|
225 | maxIndex = len(heights) | |||
|
226 | ||||
|
227 | self.selectHeightsByIndex(minIndex, maxIndex) | |||
|
228 | ||||
|
229 | return 1 | |||
|
230 | ||||
|
231 | def getBeaconSignal(self, tauindex = 0, channelindex = 0, hei_ref=None): | |||
|
232 | newheis = numpy.where(self.dataOut.heightList>self.dataOut.radarControllerHeaderObj.Taus[tauindex]) | |||
|
233 | ||||
|
234 | if hei_ref != None: | |||
|
235 | newheis = numpy.where(self.dataOut.heightList>hei_ref) | |||
|
236 | ||||
|
237 | minIndex = min(newheis[0]) | |||
|
238 | maxIndex = max(newheis[0]) | |||
|
239 | data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1] | |||
|
240 | heightList = self.dataOut.heightList[minIndex:maxIndex+1] | |||
|
241 | ||||
|
242 | # determina indices | |||
|
243 | nheis = int(self.dataOut.radarControllerHeaderObj.txB/(self.dataOut.heightList[1]-self.dataOut.heightList[0])) | |||
|
244 | avg_dB = 10*numpy.log10(numpy.sum(data_spc[channelindex,:,:],axis=0)) | |||
|
245 | beacon_dB = numpy.sort(avg_dB)[-nheis:] | |||
|
246 | beacon_heiIndexList = [] | |||
|
247 | for val in avg_dB.tolist(): | |||
|
248 | if val >= beacon_dB[0]: | |||
|
249 | beacon_heiIndexList.append(avg_dB.tolist().index(val)) | |||
|
250 | ||||
|
251 | #data_spc = data_spc[:,:,beacon_heiIndexList] | |||
|
252 | data_cspc = None | |||
|
253 | if self.dataOut.data_cspc != None: | |||
|
254 | data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1] | |||
|
255 | #data_cspc = data_cspc[:,:,beacon_heiIndexList] | |||
|
256 | ||||
|
257 | data_dc = None | |||
|
258 | if self.dataOut.data_dc != None: | |||
|
259 | data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1] | |||
|
260 | #data_dc = data_dc[:,beacon_heiIndexList] | |||
|
261 | ||||
|
262 | self.dataOut.data_spc = data_spc | |||
|
263 | self.dataOut.data_cspc = data_cspc | |||
|
264 | self.dataOut.data_dc = data_dc | |||
|
265 | self.dataOut.heightList = heightList | |||
|
266 | self.dataOut.beacon_heiIndexList = beacon_heiIndexList | |||
|
267 | ||||
|
268 | return 1 | |||
|
269 | ||||
|
270 | ||||
|
271 | def selectHeightsByIndex(self, minIndex, maxIndex): | |||
|
272 | """ | |||
|
273 | Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango | |||
|
274 | minIndex <= index <= maxIndex | |||
|
275 | ||||
|
276 | Input: | |||
|
277 | minIndex : valor de indice minimo de altura a considerar | |||
|
278 | maxIndex : valor de indice maximo de altura a considerar | |||
|
279 | ||||
|
280 | Affected: | |||
|
281 | self.dataOut.data_spc | |||
|
282 | self.dataOut.data_cspc | |||
|
283 | self.dataOut.data_dc | |||
|
284 | self.dataOut.heightList | |||
|
285 | ||||
|
286 | Return: | |||
|
287 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |||
|
288 | """ | |||
|
289 | ||||
|
290 | if (minIndex < 0) or (minIndex > maxIndex): | |||
|
291 | raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |||
|
292 | ||||
|
293 | if (maxIndex >= self.dataOut.nHeights): | |||
|
294 | maxIndex = self.dataOut.nHeights-1 | |||
|
295 | # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |||
|
296 | ||||
|
297 | # nHeights = maxIndex - minIndex + 1 | |||
|
298 | ||||
|
299 | #Spectra | |||
|
300 | data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1] | |||
|
301 | ||||
|
302 | data_cspc = None | |||
|
303 | if self.dataOut.data_cspc != None: | |||
|
304 | data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1] | |||
|
305 | ||||
|
306 | data_dc = None | |||
|
307 | if self.dataOut.data_dc != None: | |||
|
308 | data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1] | |||
|
309 | ||||
|
310 | self.dataOut.data_spc = data_spc | |||
|
311 | self.dataOut.data_cspc = data_cspc | |||
|
312 | self.dataOut.data_dc = data_dc | |||
|
313 | ||||
|
314 | self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1] | |||
|
315 | ||||
|
316 | return 1 | |||
|
317 | ||||
|
318 | def removeDC(self, mode = 2): | |||
|
319 | jspectra = self.dataOut.data_spc | |||
|
320 | jcspectra = self.dataOut.data_cspc | |||
|
321 | ||||
|
322 | ||||
|
323 | num_chan = jspectra.shape[0] | |||
|
324 | num_hei = jspectra.shape[2] | |||
|
325 | ||||
|
326 | if jcspectra != None: | |||
|
327 | jcspectraExist = True | |||
|
328 | num_pairs = jcspectra.shape[0] | |||
|
329 | else: jcspectraExist = False | |||
|
330 | ||||
|
331 | freq_dc = jspectra.shape[1]/2 | |||
|
332 | ind_vel = numpy.array([-2,-1,1,2]) + freq_dc | |||
|
333 | ||||
|
334 | if ind_vel[0]<0: | |||
|
335 | ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof | |||
|
336 | ||||
|
337 | if mode == 1: | |||
|
338 | jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION | |||
|
339 | ||||
|
340 | if jcspectraExist: | |||
|
341 | jcspectra[:,freq_dc,:] = (jcspectra[:,ind_vel[1],:] + jcspectra[:,ind_vel[2],:])/2 | |||
|
342 | ||||
|
343 | if mode == 2: | |||
|
344 | ||||
|
345 | vel = numpy.array([-2,-1,1,2]) | |||
|
346 | xx = numpy.zeros([4,4]) | |||
|
347 | ||||
|
348 | for fil in range(4): | |||
|
349 | xx[fil,:] = vel[fil]**numpy.asarray(range(4)) | |||
|
350 | ||||
|
351 | xx_inv = numpy.linalg.inv(xx) | |||
|
352 | xx_aux = xx_inv[0,:] | |||
|
353 | ||||
|
354 | for ich in range(num_chan): | |||
|
355 | yy = jspectra[ich,ind_vel,:] | |||
|
356 | jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy) | |||
|
357 | ||||
|
358 | junkid = jspectra[ich,freq_dc,:]<=0 | |||
|
359 | cjunkid = sum(junkid) | |||
|
360 | ||||
|
361 | if cjunkid.any(): | |||
|
362 | jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2 | |||
|
363 | ||||
|
364 | if jcspectraExist: | |||
|
365 | for ip in range(num_pairs): | |||
|
366 | yy = jcspectra[ip,ind_vel,:] | |||
|
367 | jcspectra[ip,freq_dc,:] = numpy.dot(xx_aux,yy) | |||
|
368 | ||||
|
369 | ||||
|
370 | self.dataOut.data_spc = jspectra | |||
|
371 | self.dataOut.data_cspc = jcspectra | |||
|
372 | ||||
|
373 | return 1 | |||
|
374 | ||||
|
375 | def removeInterference(self, interf = 2,hei_interf = None, nhei_interf = None, offhei_interf = None): | |||
|
376 | ||||
|
377 | jspectra = self.dataOut.data_spc | |||
|
378 | jcspectra = self.dataOut.data_cspc | |||
|
379 | jnoise = self.dataOut.getNoise() | |||
|
380 | num_incoh = self.dataOut.nIncohInt | |||
|
381 | ||||
|
382 | num_channel = jspectra.shape[0] | |||
|
383 | num_prof = jspectra.shape[1] | |||
|
384 | num_hei = jspectra.shape[2] | |||
|
385 | ||||
|
386 | #hei_interf | |||
|
387 | if hei_interf == None: | |||
|
388 | count_hei = num_hei/2 #Como es entero no importa | |||
|
389 | hei_interf = numpy.asmatrix(range(count_hei)) + num_hei - count_hei | |||
|
390 | hei_interf = numpy.asarray(hei_interf)[0] | |||
|
391 | #nhei_interf | |||
|
392 | if (nhei_interf == None): | |||
|
393 | nhei_interf = 5 | |||
|
394 | if (nhei_interf < 1): | |||
|
395 | nhei_interf = 1 | |||
|
396 | if (nhei_interf > count_hei): | |||
|
397 | nhei_interf = count_hei | |||
|
398 | if (offhei_interf == None): | |||
|
399 | offhei_interf = 0 | |||
|
400 | ||||
|
401 | ind_hei = range(num_hei) | |||
|
402 | # mask_prof = numpy.asarray(range(num_prof - 2)) + 1 | |||
|
403 | # mask_prof[range(num_prof/2 - 1,len(mask_prof))] += 1 | |||
|
404 | mask_prof = numpy.asarray(range(num_prof)) | |||
|
405 | num_mask_prof = mask_prof.size | |||
|
406 | comp_mask_prof = [0, num_prof/2] | |||
|
407 | ||||
|
408 | ||||
|
409 | #noise_exist: Determina si la variable jnoise ha sido definida y contiene la informacion del ruido de cada canal | |||
|
410 | if (jnoise.size < num_channel or numpy.isnan(jnoise).any()): | |||
|
411 | jnoise = numpy.nan | |||
|
412 | noise_exist = jnoise[0] < numpy.Inf | |||
|
413 | ||||
|
414 | #Subrutina de Remocion de la Interferencia | |||
|
415 | for ich in range(num_channel): | |||
|
416 | #Se ordena los espectros segun su potencia (menor a mayor) | |||
|
417 | power = jspectra[ich,mask_prof,:] | |||
|
418 | power = power[:,hei_interf] | |||
|
419 | power = power.sum(axis = 0) | |||
|
420 | psort = power.ravel().argsort() | |||
|
421 | ||||
|
422 | #Se estima la interferencia promedio en los Espectros de Potencia empleando | |||
|
423 | junkspc_interf = jspectra[ich,:,hei_interf[psort[range(offhei_interf, nhei_interf + offhei_interf)]]] | |||
|
424 | ||||
|
425 | if noise_exist: | |||
|
426 | # tmp_noise = jnoise[ich] / num_prof | |||
|
427 | tmp_noise = jnoise[ich] | |||
|
428 | junkspc_interf = junkspc_interf - tmp_noise | |||
|
429 | #junkspc_interf[:,comp_mask_prof] = 0 | |||
|
430 | ||||
|
431 | jspc_interf = junkspc_interf.sum(axis = 0) / nhei_interf | |||
|
432 | jspc_interf = jspc_interf.transpose() | |||
|
433 | #Calculando el espectro de interferencia promedio | |||
|
434 | noiseid = numpy.where(jspc_interf <= tmp_noise/ math.sqrt(num_incoh)) | |||
|
435 | noiseid = noiseid[0] | |||
|
436 | cnoiseid = noiseid.size | |||
|
437 | interfid = numpy.where(jspc_interf > tmp_noise/ math.sqrt(num_incoh)) | |||
|
438 | interfid = interfid[0] | |||
|
439 | cinterfid = interfid.size | |||
|
440 | ||||
|
441 | if (cnoiseid > 0): jspc_interf[noiseid] = 0 | |||
|
442 | ||||
|
443 | #Expandiendo los perfiles a limpiar | |||
|
444 | if (cinterfid > 0): | |||
|
445 | new_interfid = (numpy.r_[interfid - 1, interfid, interfid + 1] + num_prof)%num_prof | |||
|
446 | new_interfid = numpy.asarray(new_interfid) | |||
|
447 | new_interfid = {x for x in new_interfid} | |||
|
448 | new_interfid = numpy.array(list(new_interfid)) | |||
|
449 | new_cinterfid = new_interfid.size | |||
|
450 | else: new_cinterfid = 0 | |||
|
451 | ||||
|
452 | for ip in range(new_cinterfid): | |||
|
453 | ind = junkspc_interf[:,new_interfid[ip]].ravel().argsort() | |||
|
454 | jspc_interf[new_interfid[ip]] = junkspc_interf[ind[nhei_interf/2],new_interfid[ip]] | |||
|
455 | ||||
|
456 | ||||
|
457 | jspectra[ich,:,ind_hei] = jspectra[ich,:,ind_hei] - jspc_interf #Corregir indices | |||
|
458 | ||||
|
459 | #Removiendo la interferencia del punto de mayor interferencia | |||
|
460 | ListAux = jspc_interf[mask_prof].tolist() | |||
|
461 | maxid = ListAux.index(max(ListAux)) | |||
|
462 | ||||
|
463 | ||||
|
464 | if cinterfid > 0: | |||
|
465 | for ip in range(cinterfid*(interf == 2) - 1): | |||
|
466 | ind = (jspectra[ich,interfid[ip],:] < tmp_noise*(1 + 1/math.sqrt(num_incoh))).nonzero() | |||
|
467 | cind = len(ind) | |||
|
468 | ||||
|
469 | if (cind > 0): | |||
|
470 | jspectra[ich,interfid[ip],ind] = tmp_noise*(1 + (numpy.random.uniform(cind) - 0.5)/math.sqrt(num_incoh)) | |||
|
471 | ||||
|
472 | ind = numpy.array([-2,-1,1,2]) | |||
|
473 | xx = numpy.zeros([4,4]) | |||
|
474 | ||||
|
475 | for id1 in range(4): | |||
|
476 | xx[:,id1] = ind[id1]**numpy.asarray(range(4)) | |||
|
477 | ||||
|
478 | xx_inv = numpy.linalg.inv(xx) | |||
|
479 | xx = xx_inv[:,0] | |||
|
480 | ind = (ind + maxid + num_mask_prof)%num_mask_prof | |||
|
481 | yy = jspectra[ich,mask_prof[ind],:] | |||
|
482 | jspectra[ich,mask_prof[maxid],:] = numpy.dot(yy.transpose(),xx) | |||
|
483 | ||||
|
484 | ||||
|
485 | indAux = (jspectra[ich,:,:] < tmp_noise*(1-1/math.sqrt(num_incoh))).nonzero() | |||
|
486 | jspectra[ich,indAux[0],indAux[1]] = tmp_noise * (1 - 1/math.sqrt(num_incoh)) | |||
|
487 | ||||
|
488 | #Remocion de Interferencia en el Cross Spectra | |||
|
489 | if jcspectra == None: return jspectra, jcspectra | |||
|
490 | num_pairs = jcspectra.size/(num_prof*num_hei) | |||
|
491 | jcspectra = jcspectra.reshape(num_pairs, num_prof, num_hei) | |||
|
492 | ||||
|
493 | for ip in range(num_pairs): | |||
|
494 | ||||
|
495 | #------------------------------------------- | |||
|
496 | ||||
|
497 | cspower = numpy.abs(jcspectra[ip,mask_prof,:]) | |||
|
498 | cspower = cspower[:,hei_interf] | |||
|
499 | cspower = cspower.sum(axis = 0) | |||
|
500 | ||||
|
501 | cspsort = cspower.ravel().argsort() | |||
|
502 | junkcspc_interf = jcspectra[ip,:,hei_interf[cspsort[range(offhei_interf, nhei_interf + offhei_interf)]]] | |||
|
503 | junkcspc_interf = junkcspc_interf.transpose() | |||
|
504 | jcspc_interf = junkcspc_interf.sum(axis = 1)/nhei_interf | |||
|
505 | ||||
|
506 | ind = numpy.abs(jcspc_interf[mask_prof]).ravel().argsort() | |||
|
507 | ||||
|
508 | median_real = numpy.median(numpy.real(junkcspc_interf[mask_prof[ind[range(3*num_prof/4)]],:])) | |||
|
509 | median_imag = numpy.median(numpy.imag(junkcspc_interf[mask_prof[ind[range(3*num_prof/4)]],:])) | |||
|
510 | junkcspc_interf[comp_mask_prof,:] = numpy.complex(median_real, median_imag) | |||
|
511 | ||||
|
512 | for iprof in range(num_prof): | |||
|
513 | ind = numpy.abs(junkcspc_interf[iprof,:]).ravel().argsort() | |||
|
514 | jcspc_interf[iprof] = junkcspc_interf[iprof, ind[nhei_interf/2]] | |||
|
515 | ||||
|
516 | #Removiendo la Interferencia | |||
|
517 | jcspectra[ip,:,ind_hei] = jcspectra[ip,:,ind_hei] - jcspc_interf | |||
|
518 | ||||
|
519 | ListAux = numpy.abs(jcspc_interf[mask_prof]).tolist() | |||
|
520 | maxid = ListAux.index(max(ListAux)) | |||
|
521 | ||||
|
522 | ind = numpy.array([-2,-1,1,2]) | |||
|
523 | xx = numpy.zeros([4,4]) | |||
|
524 | ||||
|
525 | for id1 in range(4): | |||
|
526 | xx[:,id1] = ind[id1]**numpy.asarray(range(4)) | |||
|
527 | ||||
|
528 | xx_inv = numpy.linalg.inv(xx) | |||
|
529 | xx = xx_inv[:,0] | |||
|
530 | ||||
|
531 | ind = (ind + maxid + num_mask_prof)%num_mask_prof | |||
|
532 | yy = jcspectra[ip,mask_prof[ind],:] | |||
|
533 | jcspectra[ip,mask_prof[maxid],:] = numpy.dot(yy.transpose(),xx) | |||
|
534 | ||||
|
535 | #Guardar Resultados | |||
|
536 | self.dataOut.data_spc = jspectra | |||
|
537 | self.dataOut.data_cspc = jcspectra | |||
|
538 | ||||
|
539 | return 1 | |||
|
540 | ||||
|
541 | def setRadarFrequency(self, frequency=None): | |||
|
542 | if frequency != None: | |||
|
543 | self.dataOut.frequency = frequency | |||
|
544 | ||||
|
545 | return 1 | |||
|
546 | ||||
|
547 | def getNoise(self, minHei=None, maxHei=None, minVel=None, maxVel=None): | |||
|
548 | #validacion de rango | |||
|
549 | if minHei == None: | |||
|
550 | minHei = self.dataOut.heightList[0] | |||
|
551 | ||||
|
552 | if maxHei == None: | |||
|
553 | maxHei = self.dataOut.heightList[-1] | |||
|
554 | ||||
|
555 | if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): | |||
|
556 | print 'minHei: %.2f is out of the heights range'%(minHei) | |||
|
557 | print 'minHei is setting to %.2f'%(self.dataOut.heightList[0]) | |||
|
558 | minHei = self.dataOut.heightList[0] | |||
|
559 | ||||
|
560 | if (maxHei > self.dataOut.heightList[-1]) or (maxHei < minHei): | |||
|
561 | print 'maxHei: %.2f is out of the heights range'%(maxHei) | |||
|
562 | print 'maxHei is setting to %.2f'%(self.dataOut.heightList[-1]) | |||
|
563 | maxHei = self.dataOut.heightList[-1] | |||
|
564 | ||||
|
565 | # validacion de velocidades | |||
|
566 | velrange = self.dataOut.getVelRange(1) | |||
|
567 | ||||
|
568 | if minVel == None: | |||
|
569 | minVel = velrange[0] | |||
|
570 | ||||
|
571 | if maxVel == None: | |||
|
572 | maxVel = velrange[-1] | |||
|
573 | ||||
|
574 | if (minVel < velrange[0]) or (minVel > maxVel): | |||
|
575 | print 'minVel: %.2f is out of the velocity range'%(minVel) | |||
|
576 | print 'minVel is setting to %.2f'%(velrange[0]) | |||
|
577 | minVel = velrange[0] | |||
|
578 | ||||
|
579 | if (maxVel > velrange[-1]) or (maxVel < minVel): | |||
|
580 | print 'maxVel: %.2f is out of the velocity range'%(maxVel) | |||
|
581 | print 'maxVel is setting to %.2f'%(velrange[-1]) | |||
|
582 | maxVel = velrange[-1] | |||
|
583 | ||||
|
584 | # seleccion de indices para rango | |||
|
585 | minIndex = 0 | |||
|
586 | maxIndex = 0 | |||
|
587 | heights = self.dataOut.heightList | |||
|
588 | ||||
|
589 | inda = numpy.where(heights >= minHei) | |||
|
590 | indb = numpy.where(heights <= maxHei) | |||
|
591 | ||||
|
592 | try: | |||
|
593 | minIndex = inda[0][0] | |||
|
594 | except: | |||
|
595 | minIndex = 0 | |||
|
596 | ||||
|
597 | try: | |||
|
598 | maxIndex = indb[0][-1] | |||
|
599 | except: | |||
|
600 | maxIndex = len(heights) | |||
|
601 | ||||
|
602 | if (minIndex < 0) or (minIndex > maxIndex): | |||
|
603 | raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |||
|
604 | ||||
|
605 | if (maxIndex >= self.dataOut.nHeights): | |||
|
606 | maxIndex = self.dataOut.nHeights-1 | |||
|
607 | ||||
|
608 | # seleccion de indices para velocidades | |||
|
609 | indminvel = numpy.where(velrange >= minVel) | |||
|
610 | indmaxvel = numpy.where(velrange <= maxVel) | |||
|
611 | try: | |||
|
612 | minIndexVel = indminvel[0][0] | |||
|
613 | except: | |||
|
614 | minIndexVel = 0 | |||
|
615 | ||||
|
616 | try: | |||
|
617 | maxIndexVel = indmaxvel[0][-1] | |||
|
618 | except: | |||
|
619 | maxIndexVel = len(velrange) | |||
|
620 | ||||
|
621 | #seleccion del espectro | |||
|
622 | data_spc = self.dataOut.data_spc[:,minIndexVel:maxIndexVel+1,minIndex:maxIndex+1] | |||
|
623 | #estimacion de ruido | |||
|
624 | noise = numpy.zeros(self.dataOut.nChannels) | |||
|
625 | ||||
|
626 | for channel in range(self.dataOut.nChannels): | |||
|
627 | daux = data_spc[channel,:,:] | |||
|
628 | noise[channel] = hildebrand_sekhon(daux, self.dataOut.nIncohInt) | |||
|
629 | ||||
|
630 | self.dataOut.noise_estimation = noise.copy() | |||
|
631 | ||||
|
632 | return 1 | |||
|
633 | ||||
|
634 | class IncohInt(Operation): | |||
|
635 | ||||
|
636 | ||||
|
637 | __profIndex = 0 | |||
|
638 | __withOverapping = False | |||
|
639 | ||||
|
640 | __byTime = False | |||
|
641 | __initime = None | |||
|
642 | __lastdatatime = None | |||
|
643 | __integrationtime = None | |||
|
644 | ||||
|
645 | __buffer_spc = None | |||
|
646 | __buffer_cspc = None | |||
|
647 | __buffer_dc = None | |||
|
648 | ||||
|
649 | __dataReady = False | |||
|
650 | ||||
|
651 | __timeInterval = None | |||
|
652 | ||||
|
653 | n = None | |||
|
654 | ||||
|
655 | ||||
|
656 | ||||
|
657 | def __init__(self): | |||
|
658 | ||||
|
659 | Operation.__init__(self) | |||
|
660 | # self.isConfig = False | |||
|
661 | ||||
|
662 | def setup(self, n=None, timeInterval=None, overlapping=False): | |||
|
663 | """ | |||
|
664 | Set the parameters of the integration class. | |||
|
665 | ||||
|
666 | Inputs: | |||
|
667 | ||||
|
668 | n : Number of coherent integrations | |||
|
669 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work | |||
|
670 | overlapping : | |||
|
671 | ||||
|
672 | """ | |||
|
673 | ||||
|
674 | self.__initime = None | |||
|
675 | self.__lastdatatime = 0 | |||
|
676 | self.__buffer_spc = None | |||
|
677 | self.__buffer_cspc = None | |||
|
678 | self.__buffer_dc = None | |||
|
679 | self.__dataReady = False | |||
|
680 | ||||
|
681 | ||||
|
682 | if n == None and timeInterval == None: | |||
|
683 | raise ValueError, "n or timeInterval should be specified ..." | |||
|
684 | ||||
|
685 | if n != None: | |||
|
686 | self.n = n | |||
|
687 | self.__byTime = False | |||
|
688 | else: | |||
|
689 | self.__integrationtime = timeInterval #if (type(timeInterval)!=integer) -> change this line | |||
|
690 | self.n = 9999 | |||
|
691 | self.__byTime = True | |||
|
692 | ||||
|
693 | if overlapping: | |||
|
694 | self.__withOverapping = True | |||
|
695 | else: | |||
|
696 | self.__withOverapping = False | |||
|
697 | self.__buffer_spc = 0 | |||
|
698 | self.__buffer_cspc = 0 | |||
|
699 | self.__buffer_dc = 0 | |||
|
700 | ||||
|
701 | self.__profIndex = 0 | |||
|
702 | ||||
|
703 | def putData(self, data_spc, data_cspc, data_dc): | |||
|
704 | ||||
|
705 | """ | |||
|
706 | Add a profile to the __buffer_spc and increase in one the __profileIndex | |||
|
707 | ||||
|
708 | """ | |||
|
709 | ||||
|
710 | if not self.__withOverapping: | |||
|
711 | self.__buffer_spc += data_spc | |||
|
712 | ||||
|
713 | if data_cspc == None: | |||
|
714 | self.__buffer_cspc = None | |||
|
715 | else: | |||
|
716 | self.__buffer_cspc += data_cspc | |||
|
717 | ||||
|
718 | if data_dc == None: | |||
|
719 | self.__buffer_dc = None | |||
|
720 | else: | |||
|
721 | self.__buffer_dc += data_dc | |||
|
722 | ||||
|
723 | self.__profIndex += 1 | |||
|
724 | return | |||
|
725 | ||||
|
726 | #Overlapping data | |||
|
727 | nChannels, nFFTPoints, nHeis = data_spc.shape | |||
|
728 | data_spc = numpy.reshape(data_spc, (1, nChannels, nFFTPoints, nHeis)) | |||
|
729 | if data_cspc != None: | |||
|
730 | data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis)) | |||
|
731 | if data_dc != None: | |||
|
732 | data_dc = numpy.reshape(data_dc, (1, -1, nHeis)) | |||
|
733 | ||||
|
734 | #If the buffer is empty then it takes the data value | |||
|
735 | if self.__buffer_spc == None: | |||
|
736 | self.__buffer_spc = data_spc | |||
|
737 | ||||
|
738 | if data_cspc == None: | |||
|
739 | self.__buffer_cspc = None | |||
|
740 | else: | |||
|
741 | self.__buffer_cspc += data_cspc | |||
|
742 | ||||
|
743 | if data_dc == None: | |||
|
744 | self.__buffer_dc = None | |||
|
745 | else: | |||
|
746 | self.__buffer_dc += data_dc | |||
|
747 | ||||
|
748 | self.__profIndex += 1 | |||
|
749 | return | |||
|
750 | ||||
|
751 | #If the buffer length is lower than n then stakcing the data value | |||
|
752 | if self.__profIndex < self.n: | |||
|
753 | self.__buffer_spc = numpy.vstack((self.__buffer_spc, data_spc)) | |||
|
754 | ||||
|
755 | if data_cspc != None: | |||
|
756 | self.__buffer_cspc = numpy.vstack((self.__buffer_cspc, data_cspc)) | |||
|
757 | ||||
|
758 | if data_dc != None: | |||
|
759 | self.__buffer_dc = numpy.vstack((self.__buffer_dc, data_dc)) | |||
|
760 | ||||
|
761 | self.__profIndex += 1 | |||
|
762 | return | |||
|
763 | ||||
|
764 | #If the buffer length is equal to n then replacing the last buffer value with the data value | |||
|
765 | self.__buffer_spc = numpy.roll(self.__buffer_spc, -1, axis=0) | |||
|
766 | self.__buffer_spc[self.n-1] = data_spc | |||
|
767 | ||||
|
768 | if data_cspc != None: | |||
|
769 | self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0) | |||
|
770 | self.__buffer_cspc[self.n-1] = data_cspc | |||
|
771 | ||||
|
772 | if data_dc != None: | |||
|
773 | self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0) | |||
|
774 | self.__buffer_dc[self.n-1] = data_dc | |||
|
775 | ||||
|
776 | self.__profIndex = self.n | |||
|
777 | return | |||
|
778 | ||||
|
779 | ||||
|
780 | def pushData(self): | |||
|
781 | """ | |||
|
782 | Return the sum of the last profiles and the profiles used in the sum. | |||
|
783 | ||||
|
784 | Affected: | |||
|
785 | ||||
|
786 | self.__profileIndex | |||
|
787 | ||||
|
788 | """ | |||
|
789 | data_spc = None | |||
|
790 | data_cspc = None | |||
|
791 | data_dc = None | |||
|
792 | ||||
|
793 | if not self.__withOverapping: | |||
|
794 | data_spc = self.__buffer_spc | |||
|
795 | data_cspc = self.__buffer_cspc | |||
|
796 | data_dc = self.__buffer_dc | |||
|
797 | ||||
|
798 | n = self.__profIndex | |||
|
799 | ||||
|
800 | self.__buffer_spc = 0 | |||
|
801 | self.__buffer_cspc = 0 | |||
|
802 | self.__buffer_dc = 0 | |||
|
803 | self.__profIndex = 0 | |||
|
804 | ||||
|
805 | return data_spc, data_cspc, data_dc, n | |||
|
806 | ||||
|
807 | #Integration with Overlapping | |||
|
808 | data_spc = numpy.sum(self.__buffer_spc, axis=0) | |||
|
809 | ||||
|
810 | if self.__buffer_cspc != None: | |||
|
811 | data_cspc = numpy.sum(self.__buffer_cspc, axis=0) | |||
|
812 | ||||
|
813 | if self.__buffer_dc != None: | |||
|
814 | data_dc = numpy.sum(self.__buffer_dc, axis=0) | |||
|
815 | ||||
|
816 | n = self.__profIndex | |||
|
817 | ||||
|
818 | return data_spc, data_cspc, data_dc, n | |||
|
819 | ||||
|
820 | def byProfiles(self, *args): | |||
|
821 | ||||
|
822 | self.__dataReady = False | |||
|
823 | avgdata_spc = None | |||
|
824 | avgdata_cspc = None | |||
|
825 | avgdata_dc = None | |||
|
826 | # n = None | |||
|
827 | ||||
|
828 | self.putData(*args) | |||
|
829 | ||||
|
830 | if self.__profIndex == self.n: | |||
|
831 | ||||
|
832 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |||
|
833 | self.__dataReady = True | |||
|
834 | ||||
|
835 | return avgdata_spc, avgdata_cspc, avgdata_dc | |||
|
836 | ||||
|
837 | def byTime(self, datatime, *args): | |||
|
838 | ||||
|
839 | self.__dataReady = False | |||
|
840 | avgdata_spc = None | |||
|
841 | avgdata_cspc = None | |||
|
842 | avgdata_dc = None | |||
|
843 | n = None | |||
|
844 | ||||
|
845 | self.putData(*args) | |||
|
846 | ||||
|
847 | if (datatime - self.__initime) >= self.__integrationtime: | |||
|
848 | avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData() | |||
|
849 | self.n = n | |||
|
850 | self.__dataReady = True | |||
|
851 | ||||
|
852 | return avgdata_spc, avgdata_cspc, avgdata_dc | |||
|
853 | ||||
|
854 | def integrate(self, datatime, *args): | |||
|
855 | ||||
|
856 | if self.__initime == None: | |||
|
857 | self.__initime = datatime | |||
|
858 | ||||
|
859 | if self.__byTime: | |||
|
860 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args) | |||
|
861 | else: | |||
|
862 | avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args) | |||
|
863 | ||||
|
864 | self.__lastdatatime = datatime | |||
|
865 | ||||
|
866 | if avgdata_spc == None: | |||
|
867 | return None, None, None, None | |||
|
868 | ||||
|
869 | avgdatatime = self.__initime | |||
|
870 | try: | |||
|
871 | self.__timeInterval = (self.__lastdatatime - self.__initime)/(self.n - 1) | |||
|
872 | except: | |||
|
873 | self.__timeInterval = self.__lastdatatime - self.__initime | |||
|
874 | ||||
|
875 | deltatime = datatime -self.__lastdatatime | |||
|
876 | ||||
|
877 | if not self.__withOverapping: | |||
|
878 | self.__initime = datatime | |||
|
879 | else: | |||
|
880 | self.__initime += deltatime | |||
|
881 | ||||
|
882 | return avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc | |||
|
883 | ||||
|
884 | def run(self, dataOut, n=None, timeInterval=None, overlapping=False): | |||
|
885 | ||||
|
886 | if n==1: | |||
|
887 | dataOut.flagNoData = False | |||
|
888 | return | |||
|
889 | ||||
|
890 | if not self.isConfig: | |||
|
891 | self.setup(n, timeInterval, overlapping) | |||
|
892 | self.isConfig = True | |||
|
893 | ||||
|
894 | avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime, | |||
|
895 | dataOut.data_spc, | |||
|
896 | dataOut.data_cspc, | |||
|
897 | dataOut.data_dc) | |||
|
898 | ||||
|
899 | # dataOut.timeInterval *= n | |||
|
900 | dataOut.flagNoData = True | |||
|
901 | ||||
|
902 | if self.__dataReady: | |||
|
903 | ||||
|
904 | dataOut.data_spc = avgdata_spc | |||
|
905 | dataOut.data_cspc = avgdata_cspc | |||
|
906 | dataOut.data_dc = avgdata_dc | |||
|
907 | ||||
|
908 | dataOut.nIncohInt *= self.n | |||
|
909 | dataOut.utctime = avgdatatime | |||
|
910 | #dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt * dataOut.nIncohInt * dataOut.nFFTPoints | |||
|
911 | dataOut.timeInterval = self.__timeInterval*self.n | |||
|
912 | dataOut.flagNoData = False | |||
|
913 | ||||
|
914 | class ProfileConcat(Operation): | |||
|
915 | ||||
|
916 | isConfig = False | |||
|
917 | buffer = None | |||
|
918 | ||||
|
919 | def __init__(self): | |||
|
920 | ||||
|
921 | Operation.__init__(self) | |||
|
922 | self.profileIndex = 0 | |||
|
923 | ||||
|
924 | def reset(self): | |||
|
925 | self.buffer = numpy.zeros_like(self.buffer) | |||
|
926 | self.start_index = 0 | |||
|
927 | self.times = 1 | |||
|
928 | ||||
|
929 | def setup(self, data, m, n=1): | |||
|
930 | self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0])) | |||
|
931 | self.profiles = data.shape[1] | |||
|
932 | self.start_index = 0 | |||
|
933 | self.times = 1 | |||
|
934 | ||||
|
935 | def concat(self, data): | |||
|
936 | ||||
|
937 | self.buffer[:,self.start_index:self.profiles*self.times] = data.copy() | |||
|
938 | self.start_index = self.start_index + self.profiles | |||
|
939 | ||||
|
940 | def run(self, dataOut, m): | |||
|
941 | ||||
|
942 | dataOut.flagNoData = True | |||
|
943 | ||||
|
944 | if not self.isConfig: | |||
|
945 | self.setup(dataOut.data, m, 1) | |||
|
946 | self.isConfig = True | |||
|
947 | ||||
|
948 | self.concat(dataOut.data) | |||
|
949 | self.times += 1 | |||
|
950 | if self.times > m: | |||
|
951 | dataOut.data = self.buffer | |||
|
952 | self.reset() | |||
|
953 | dataOut.flagNoData = False | |||
|
954 | # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas | |||
|
955 | deltaHeight = dataOut.heightList[1] - dataOut.heightList[0] | |||
|
956 | xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * 5 | |||
|
957 | dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight) | |||
|
958 | ||||
|
959 | class ProfileSelector(Operation): | |||
|
960 | ||||
|
961 | profileIndex = None | |||
|
962 | # Tamanho total de los perfiles | |||
|
963 | nProfiles = None | |||
|
964 | ||||
|
965 | def __init__(self): | |||
|
966 | ||||
|
967 | Operation.__init__(self) | |||
|
968 | self.profileIndex = 0 | |||
|
969 | ||||
|
970 | def incIndex(self): | |||
|
971 | self.profileIndex += 1 | |||
|
972 | ||||
|
973 | if self.profileIndex >= self.nProfiles: | |||
|
974 | self.profileIndex = 0 | |||
|
975 | ||||
|
976 | def isProfileInRange(self, minIndex, maxIndex): | |||
|
977 | ||||
|
978 | if self.profileIndex < minIndex: | |||
|
979 | return False | |||
|
980 | ||||
|
981 | if self.profileIndex > maxIndex: | |||
|
982 | return False | |||
|
983 | ||||
|
984 | return True | |||
|
985 | ||||
|
986 | def isProfileInList(self, profileList): | |||
|
987 | ||||
|
988 | if self.profileIndex not in profileList: | |||
|
989 | return False | |||
|
990 | ||||
|
991 | return True | |||
|
992 | ||||
|
993 | def run(self, dataOut, profileList=None, profileRangeList=None, beam=None): | |||
|
994 | ||||
|
995 | dataOut.flagNoData = True | |||
|
996 | self.nProfiles = dataOut.nProfiles | |||
|
997 | ||||
|
998 | if profileList != None: | |||
|
999 | if self.isProfileInList(profileList): | |||
|
1000 | dataOut.flagNoData = False | |||
|
1001 | ||||
|
1002 | self.incIndex() | |||
|
1003 | return 1 | |||
|
1004 | ||||
|
1005 | ||||
|
1006 | elif profileRangeList != None: | |||
|
1007 | minIndex = profileRangeList[0] | |||
|
1008 | maxIndex = profileRangeList[1] | |||
|
1009 | if self.isProfileInRange(minIndex, maxIndex): | |||
|
1010 | dataOut.flagNoData = False | |||
|
1011 | ||||
|
1012 | self.incIndex() | |||
|
1013 | return 1 | |||
|
1014 | elif beam != None: | |||
|
1015 | if self.isProfileInList(dataOut.beamRangeDict[beam]): | |||
|
1016 | dataOut.flagNoData = False | |||
|
1017 | ||||
|
1018 | self.incIndex() | |||
|
1019 | return 1 | |||
|
1020 | ||||
|
1021 | else: | |||
|
1022 | raise ValueError, "ProfileSelector needs profileList or profileRangeList" | |||
|
1023 | ||||
|
1024 | return 0 |
This diff has been collapsed as it changes many lines, (518 lines changed) Show them Hide them | |||||
@@ -0,0 +1,518 | |||||
|
1 | import numpy | |||
|
2 | ||||
|
3 | from jroproc_base import ProcessingUnit, Operation | |||
|
4 | from model.data.jrodata import Voltage | |||
|
5 | ||||
|
6 | class VoltageProc(ProcessingUnit): | |||
|
7 | ||||
|
8 | ||||
|
9 | def __init__(self): | |||
|
10 | ||||
|
11 | ProcessingUnit.__init__(self) | |||
|
12 | ||||
|
13 | # self.objectDict = {} | |||
|
14 | self.dataOut = Voltage() | |||
|
15 | self.flip = 1 | |||
|
16 | ||||
|
17 | def run(self): | |||
|
18 | self.dataOut.copy(self.dataIn) | |||
|
19 | ||||
|
20 | # def __updateObjFromAmisrInput(self): | |||
|
21 | # | |||
|
22 | # self.dataOut.timeZone = self.dataIn.timeZone | |||
|
23 | # self.dataOut.dstFlag = self.dataIn.dstFlag | |||
|
24 | # self.dataOut.errorCount = self.dataIn.errorCount | |||
|
25 | # self.dataOut.useLocalTime = self.dataIn.useLocalTime | |||
|
26 | # | |||
|
27 | # self.dataOut.flagNoData = self.dataIn.flagNoData | |||
|
28 | # self.dataOut.data = self.dataIn.data | |||
|
29 | # self.dataOut.utctime = self.dataIn.utctime | |||
|
30 | # self.dataOut.channelList = self.dataIn.channelList | |||
|
31 | # self.dataOut.timeInterval = self.dataIn.timeInterval | |||
|
32 | # self.dataOut.heightList = self.dataIn.heightList | |||
|
33 | # self.dataOut.nProfiles = self.dataIn.nProfiles | |||
|
34 | # | |||
|
35 | # self.dataOut.nCohInt = self.dataIn.nCohInt | |||
|
36 | # self.dataOut.ippSeconds = self.dataIn.ippSeconds | |||
|
37 | # self.dataOut.frequency = self.dataIn.frequency | |||
|
38 | # | |||
|
39 | # pass# | |||
|
40 | # | |||
|
41 | # def init(self): | |||
|
42 | # | |||
|
43 | # | |||
|
44 | # if self.dataIn.type == 'AMISR': | |||
|
45 | # self.__updateObjFromAmisrInput() | |||
|
46 | # | |||
|
47 | # if self.dataIn.type == 'Voltage': | |||
|
48 | # self.dataOut.copy(self.dataIn) | |||
|
49 | # # No necesita copiar en cada init() los atributos de dataIn | |||
|
50 | # # la copia deberia hacerse por cada nuevo bloque de datos | |||
|
51 | ||||
|
52 | def selectChannels(self, channelList): | |||
|
53 | ||||
|
54 | channelIndexList = [] | |||
|
55 | ||||
|
56 | for channel in channelList: | |||
|
57 | index = self.dataOut.channelList.index(channel) | |||
|
58 | channelIndexList.append(index) | |||
|
59 | ||||
|
60 | self.selectChannelsByIndex(channelIndexList) | |||
|
61 | ||||
|
62 | def selectChannelsByIndex(self, channelIndexList): | |||
|
63 | """ | |||
|
64 | Selecciona un bloque de datos en base a canales segun el channelIndexList | |||
|
65 | ||||
|
66 | Input: | |||
|
67 | channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7] | |||
|
68 | ||||
|
69 | Affected: | |||
|
70 | self.dataOut.data | |||
|
71 | self.dataOut.channelIndexList | |||
|
72 | self.dataOut.nChannels | |||
|
73 | self.dataOut.m_ProcessingHeader.totalSpectra | |||
|
74 | self.dataOut.systemHeaderObj.numChannels | |||
|
75 | self.dataOut.m_ProcessingHeader.blockSize | |||
|
76 | ||||
|
77 | Return: | |||
|
78 | None | |||
|
79 | """ | |||
|
80 | ||||
|
81 | for channelIndex in channelIndexList: | |||
|
82 | if channelIndex not in self.dataOut.channelIndexList: | |||
|
83 | print channelIndexList | |||
|
84 | raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex | |||
|
85 | ||||
|
86 | # nChannels = len(channelIndexList) | |||
|
87 | ||||
|
88 | data = self.dataOut.data[channelIndexList,:] | |||
|
89 | ||||
|
90 | self.dataOut.data = data | |||
|
91 | self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList] | |||
|
92 | # self.dataOut.nChannels = nChannels | |||
|
93 | ||||
|
94 | return 1 | |||
|
95 | ||||
|
96 | def selectHeights(self, minHei=None, maxHei=None): | |||
|
97 | """ | |||
|
98 | Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango | |||
|
99 | minHei <= height <= maxHei | |||
|
100 | ||||
|
101 | Input: | |||
|
102 | minHei : valor minimo de altura a considerar | |||
|
103 | maxHei : valor maximo de altura a considerar | |||
|
104 | ||||
|
105 | Affected: | |||
|
106 | Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex | |||
|
107 | ||||
|
108 | Return: | |||
|
109 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |||
|
110 | """ | |||
|
111 | ||||
|
112 | if minHei == None: | |||
|
113 | minHei = self.dataOut.heightList[0] | |||
|
114 | ||||
|
115 | if maxHei == None: | |||
|
116 | maxHei = self.dataOut.heightList[-1] | |||
|
117 | ||||
|
118 | if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei): | |||
|
119 | raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |||
|
120 | ||||
|
121 | ||||
|
122 | if (maxHei > self.dataOut.heightList[-1]): | |||
|
123 | maxHei = self.dataOut.heightList[-1] | |||
|
124 | # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei) | |||
|
125 | ||||
|
126 | minIndex = 0 | |||
|
127 | maxIndex = 0 | |||
|
128 | heights = self.dataOut.heightList | |||
|
129 | ||||
|
130 | inda = numpy.where(heights >= minHei) | |||
|
131 | indb = numpy.where(heights <= maxHei) | |||
|
132 | ||||
|
133 | try: | |||
|
134 | minIndex = inda[0][0] | |||
|
135 | except: | |||
|
136 | minIndex = 0 | |||
|
137 | ||||
|
138 | try: | |||
|
139 | maxIndex = indb[0][-1] | |||
|
140 | except: | |||
|
141 | maxIndex = len(heights) | |||
|
142 | ||||
|
143 | self.selectHeightsByIndex(minIndex, maxIndex) | |||
|
144 | ||||
|
145 | return 1 | |||
|
146 | ||||
|
147 | ||||
|
148 | def selectHeightsByIndex(self, minIndex, maxIndex): | |||
|
149 | """ | |||
|
150 | Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango | |||
|
151 | minIndex <= index <= maxIndex | |||
|
152 | ||||
|
153 | Input: | |||
|
154 | minIndex : valor de indice minimo de altura a considerar | |||
|
155 | maxIndex : valor de indice maximo de altura a considerar | |||
|
156 | ||||
|
157 | Affected: | |||
|
158 | self.dataOut.data | |||
|
159 | self.dataOut.heightList | |||
|
160 | ||||
|
161 | Return: | |||
|
162 | 1 si el metodo se ejecuto con exito caso contrario devuelve 0 | |||
|
163 | """ | |||
|
164 | ||||
|
165 | if (minIndex < 0) or (minIndex > maxIndex): | |||
|
166 | raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |||
|
167 | ||||
|
168 | if (maxIndex >= self.dataOut.nHeights): | |||
|
169 | maxIndex = self.dataOut.nHeights-1 | |||
|
170 | # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex) | |||
|
171 | ||||
|
172 | # nHeights = maxIndex - minIndex + 1 | |||
|
173 | ||||
|
174 | #voltage | |||
|
175 | data = self.dataOut.data[:,minIndex:maxIndex+1] | |||
|
176 | ||||
|
177 | # firstHeight = self.dataOut.heightList[minIndex] | |||
|
178 | ||||
|
179 | self.dataOut.data = data | |||
|
180 | self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1] | |||
|
181 | ||||
|
182 | return 1 | |||
|
183 | ||||
|
184 | ||||
|
185 | def filterByHeights(self, window): | |||
|
186 | deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0] | |||
|
187 | ||||
|
188 | if window == None: | |||
|
189 | window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight | |||
|
190 | ||||
|
191 | newdelta = deltaHeight * window | |||
|
192 | r = self.dataOut.data.shape[1] % window | |||
|
193 | buffer = self.dataOut.data[:,0:self.dataOut.data.shape[1]-r] | |||
|
194 | buffer = buffer.reshape(self.dataOut.data.shape[0],self.dataOut.data.shape[1]/window,window) | |||
|
195 | buffer = numpy.sum(buffer,2) | |||
|
196 | self.dataOut.data = buffer | |||
|
197 | self.dataOut.heightList = numpy.arange(self.dataOut.heightList[0],newdelta*(self.dataOut.nHeights-r)/window,newdelta) | |||
|
198 | self.dataOut.windowOfFilter = window | |||
|
199 | ||||
|
200 | def deFlip(self): | |||
|
201 | self.dataOut.data *= self.flip | |||
|
202 | self.flip *= -1. | |||
|
203 | ||||
|
204 | def setRadarFrequency(self, frequency=None): | |||
|
205 | if frequency != None: | |||
|
206 | self.dataOut.frequency = frequency | |||
|
207 | ||||
|
208 | return 1 | |||
|
209 | ||||
|
210 | class CohInt(Operation): | |||
|
211 | ||||
|
212 | isConfig = False | |||
|
213 | ||||
|
214 | __profIndex = 0 | |||
|
215 | __withOverapping = False | |||
|
216 | ||||
|
217 | __byTime = False | |||
|
218 | __initime = None | |||
|
219 | __lastdatatime = None | |||
|
220 | __integrationtime = None | |||
|
221 | ||||
|
222 | __buffer = None | |||
|
223 | ||||
|
224 | __dataReady = False | |||
|
225 | ||||
|
226 | n = None | |||
|
227 | ||||
|
228 | ||||
|
229 | def __init__(self): | |||
|
230 | ||||
|
231 | Operation.__init__(self) | |||
|
232 | ||||
|
233 | # self.isConfig = False | |||
|
234 | ||||
|
235 | def setup(self, n=None, timeInterval=None, overlapping=False): | |||
|
236 | """ | |||
|
237 | Set the parameters of the integration class. | |||
|
238 | ||||
|
239 | Inputs: | |||
|
240 | ||||
|
241 | n : Number of coherent integrations | |||
|
242 | timeInterval : Time of integration. If the parameter "n" is selected this one does not work | |||
|
243 | overlapping : | |||
|
244 | ||||
|
245 | """ | |||
|
246 | ||||
|
247 | self.__initime = None | |||
|
248 | self.__lastdatatime = 0 | |||
|
249 | self.__buffer = None | |||
|
250 | self.__dataReady = False | |||
|
251 | ||||
|
252 | ||||
|
253 | if n == None and timeInterval == None: | |||
|
254 | raise ValueError, "n or timeInterval should be specified ..." | |||
|
255 | ||||
|
256 | if n != None: | |||
|
257 | self.n = n | |||
|
258 | self.__byTime = False | |||
|
259 | else: | |||
|
260 | self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line | |||
|
261 | self.n = 9999 | |||
|
262 | self.__byTime = True | |||
|
263 | ||||
|
264 | if overlapping: | |||
|
265 | self.__withOverapping = True | |||
|
266 | self.__buffer = None | |||
|
267 | else: | |||
|
268 | self.__withOverapping = False | |||
|
269 | self.__buffer = 0 | |||
|
270 | ||||
|
271 | self.__profIndex = 0 | |||
|
272 | ||||
|
273 | def putData(self, data): | |||
|
274 | ||||
|
275 | """ | |||
|
276 | Add a profile to the __buffer and increase in one the __profileIndex | |||
|
277 | ||||
|
278 | """ | |||
|
279 | ||||
|
280 | if not self.__withOverapping: | |||
|
281 | self.__buffer += data.copy() | |||
|
282 | self.__profIndex += 1 | |||
|
283 | return | |||
|
284 | ||||
|
285 | #Overlapping data | |||
|
286 | nChannels, nHeis = data.shape | |||
|
287 | data = numpy.reshape(data, (1, nChannels, nHeis)) | |||
|
288 | ||||
|
289 | #If the buffer is empty then it takes the data value | |||
|
290 | if self.__buffer == None: | |||
|
291 | self.__buffer = data | |||
|
292 | self.__profIndex += 1 | |||
|
293 | return | |||
|
294 | ||||
|
295 | #If the buffer length is lower than n then stakcing the data value | |||
|
296 | if self.__profIndex < self.n: | |||
|
297 | self.__buffer = numpy.vstack((self.__buffer, data)) | |||
|
298 | self.__profIndex += 1 | |||
|
299 | return | |||
|
300 | ||||
|
301 | #If the buffer length is equal to n then replacing the last buffer value with the data value | |||
|
302 | self.__buffer = numpy.roll(self.__buffer, -1, axis=0) | |||
|
303 | self.__buffer[self.n-1] = data | |||
|
304 | self.__profIndex = self.n | |||
|
305 | return | |||
|
306 | ||||
|
307 | ||||
|
308 | def pushData(self): | |||
|
309 | """ | |||
|
310 | Return the sum of the last profiles and the profiles used in the sum. | |||
|
311 | ||||
|
312 | Affected: | |||
|
313 | ||||
|
314 | self.__profileIndex | |||
|
315 | ||||
|
316 | """ | |||
|
317 | ||||
|
318 | if not self.__withOverapping: | |||
|
319 | data = self.__buffer | |||
|
320 | n = self.__profIndex | |||
|
321 | ||||
|
322 | self.__buffer = 0 | |||
|
323 | self.__profIndex = 0 | |||
|
324 | ||||
|
325 | return data, n | |||
|
326 | ||||
|
327 | #Integration with Overlapping | |||
|
328 | data = numpy.sum(self.__buffer, axis=0) | |||
|
329 | n = self.__profIndex | |||
|
330 | ||||
|
331 | return data, n | |||
|
332 | ||||
|
333 | def byProfiles(self, data): | |||
|
334 | ||||
|
335 | self.__dataReady = False | |||
|
336 | avgdata = None | |||
|
337 | # n = None | |||
|
338 | ||||
|
339 | self.putData(data) | |||
|
340 | ||||
|
341 | if self.__profIndex == self.n: | |||
|
342 | ||||
|
343 | avgdata, n = self.pushData() | |||
|
344 | self.__dataReady = True | |||
|
345 | ||||
|
346 | return avgdata | |||
|
347 | ||||
|
348 | def byTime(self, data, datatime): | |||
|
349 | ||||
|
350 | self.__dataReady = False | |||
|
351 | avgdata = None | |||
|
352 | n = None | |||
|
353 | ||||
|
354 | self.putData(data) | |||
|
355 | ||||
|
356 | if (datatime - self.__initime) >= self.__integrationtime: | |||
|
357 | avgdata, n = self.pushData() | |||
|
358 | self.n = n | |||
|
359 | self.__dataReady = True | |||
|
360 | ||||
|
361 | return avgdata | |||
|
362 | ||||
|
363 | def integrate(self, data, datatime=None): | |||
|
364 | ||||
|
365 | if self.__initime == None: | |||
|
366 | self.__initime = datatime | |||
|
367 | ||||
|
368 | if self.__byTime: | |||
|
369 | avgdata = self.byTime(data, datatime) | |||
|
370 | else: | |||
|
371 | avgdata = self.byProfiles(data) | |||
|
372 | ||||
|
373 | ||||
|
374 | self.__lastdatatime = datatime | |||
|
375 | ||||
|
376 | if avgdata == None: | |||
|
377 | return None, None | |||
|
378 | ||||
|
379 | avgdatatime = self.__initime | |||
|
380 | ||||
|
381 | deltatime = datatime -self.__lastdatatime | |||
|
382 | ||||
|
383 | if not self.__withOverapping: | |||
|
384 | self.__initime = datatime | |||
|
385 | else: | |||
|
386 | self.__initime += deltatime | |||
|
387 | ||||
|
388 | return avgdata, avgdatatime | |||
|
389 | ||||
|
390 | def run(self, dataOut, **kwargs): | |||
|
391 | ||||
|
392 | if not self.isConfig: | |||
|
393 | self.setup(**kwargs) | |||
|
394 | self.isConfig = True | |||
|
395 | ||||
|
396 | avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime) | |||
|
397 | ||||
|
398 | # dataOut.timeInterval *= n | |||
|
399 | dataOut.flagNoData = True | |||
|
400 | ||||
|
401 | if self.__dataReady: | |||
|
402 | dataOut.data = avgdata | |||
|
403 | dataOut.nCohInt *= self.n | |||
|
404 | dataOut.utctime = avgdatatime | |||
|
405 | dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt | |||
|
406 | dataOut.flagNoData = False | |||
|
407 | ||||
|
408 | class Decoder(Operation): | |||
|
409 | ||||
|
410 | isConfig = False | |||
|
411 | __profIndex = 0 | |||
|
412 | ||||
|
413 | code = None | |||
|
414 | ||||
|
415 | nCode = None | |||
|
416 | nBaud = None | |||
|
417 | ||||
|
418 | def __init__(self): | |||
|
419 | ||||
|
420 | Operation.__init__(self) | |||
|
421 | # self.isConfig = False | |||
|
422 | ||||
|
423 | def setup(self, code, shape): | |||
|
424 | ||||
|
425 | self.__profIndex = 0 | |||
|
426 | ||||
|
427 | self.code = code | |||
|
428 | ||||
|
429 | self.nCode = len(code) | |||
|
430 | self.nBaud = len(code[0]) | |||
|
431 | ||||
|
432 | self.__nChannels, self.__nHeis = shape | |||
|
433 | ||||
|
434 | __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex) | |||
|
435 | ||||
|
436 | __codeBuffer[:,0:self.nBaud] = self.code | |||
|
437 | ||||
|
438 | self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1)) | |||
|
439 | ||||
|
440 | self.ndatadec = self.__nHeis - self.nBaud + 1 | |||
|
441 | ||||
|
442 | self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex) | |||
|
443 | ||||
|
444 | def convolutionInFreq(self, data): | |||
|
445 | ||||
|
446 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) | |||
|
447 | ||||
|
448 | fft_data = numpy.fft.fft(data, axis=1) | |||
|
449 | ||||
|
450 | conv = fft_data*fft_code | |||
|
451 | ||||
|
452 | data = numpy.fft.ifft(conv,axis=1) | |||
|
453 | ||||
|
454 | datadec = data[:,:-self.nBaud+1] | |||
|
455 | ||||
|
456 | return datadec | |||
|
457 | ||||
|
458 | def convolutionInFreqOpt(self, data): | |||
|
459 | ||||
|
460 | fft_code = self.fft_code[self.__profIndex].reshape(1,-1) | |||
|
461 | ||||
|
462 | data = cfunctions.decoder(fft_code, data) | |||
|
463 | ||||
|
464 | datadec = data[:,:-self.nBaud+1] | |||
|
465 | ||||
|
466 | return datadec | |||
|
467 | ||||
|
468 | def convolutionInTime(self, data): | |||
|
469 | ||||
|
470 | code = self.code[self.__profIndex] | |||
|
471 | ||||
|
472 | for i in range(self.__nChannels): | |||
|
473 | self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='valid') | |||
|
474 | ||||
|
475 | return self.datadecTime | |||
|
476 | ||||
|
477 | def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0): | |||
|
478 | ||||
|
479 | if code == None: | |||
|
480 | code = dataOut.code | |||
|
481 | else: | |||
|
482 | code = numpy.array(code).reshape(nCode,nBaud) | |||
|
483 | dataOut.code = code | |||
|
484 | dataOut.nCode = nCode | |||
|
485 | dataOut.nBaud = nBaud | |||
|
486 | dataOut.radarControllerHeaderObj.code = code | |||
|
487 | dataOut.radarControllerHeaderObj.nCode = nCode | |||
|
488 | dataOut.radarControllerHeaderObj.nBaud = nBaud | |||
|
489 | ||||
|
490 | ||||
|
491 | if not self.isConfig: | |||
|
492 | ||||
|
493 | self.setup(code, dataOut.data.shape) | |||
|
494 | self.isConfig = True | |||
|
495 | ||||
|
496 | if mode == 0: | |||
|
497 | datadec = self.convolutionInTime(dataOut.data) | |||
|
498 | ||||
|
499 | if mode == 1: | |||
|
500 | datadec = self.convolutionInFreq(dataOut.data) | |||
|
501 | ||||
|
502 | if mode == 2: | |||
|
503 | datadec = self.convolutionInFreqOpt(dataOut.data) | |||
|
504 | ||||
|
505 | dataOut.data = datadec | |||
|
506 | ||||
|
507 | dataOut.heightList = dataOut.heightList[0:self.ndatadec] | |||
|
508 | ||||
|
509 | dataOut.flagDecodeData = True #asumo q la data no esta decodificada | |||
|
510 | ||||
|
511 | if self.__profIndex == self.nCode-1: | |||
|
512 | self.__profIndex = 0 | |||
|
513 | return 1 | |||
|
514 | ||||
|
515 | self.__profIndex += 1 | |||
|
516 | ||||
|
517 | return 1 | |||
|
518 | # dataOut.flagDeflipData = True #asumo q la data no esta sin flip |
@@ -0,0 +1,3 | |||||
|
1 | from jroproc_voltage import * | |||
|
2 | from jroproc_spectra import * | |||
|
3 | from jroproc_heispectra import * No newline at end of file |
@@ -1,744 +1,746 | |||||
1 | ''' |
|
1 | ''' | |
2 | Created on September , 2012 |
|
2 | Created on September , 2012 | |
3 | @author: |
|
3 | @author: | |
4 | ''' |
|
4 | ''' | |
5 | from xml.etree.ElementTree import Element, SubElement, ElementTree |
|
5 | from xml.etree.ElementTree import Element, SubElement, ElementTree | |
6 | from xml.etree import ElementTree as ET |
|
6 | from xml.etree import ElementTree as ET | |
7 | from xml.dom import minidom |
|
7 | from xml.dom import minidom | |
8 |
|
8 | |||
9 | import sys |
|
|||
10 | import datetime |
|
9 | import datetime | |
11 |
from model |
|
10 | from model import * | |
12 | from model.jroprocessing import * |
|
|||
13 | from model.jroplot import * |
|
|||
14 |
|
11 | |||
15 | def prettify(elem): |
|
12 | def prettify(elem): | |
16 | """Return a pretty-printed XML string for the Element. |
|
13 | """Return a pretty-printed XML string for the Element. | |
17 | """ |
|
14 | """ | |
18 | rough_string = ET.tostring(elem, 'utf-8') |
|
15 | rough_string = ET.tostring(elem, 'utf-8') | |
19 | reparsed = minidom.parseString(rough_string) |
|
16 | reparsed = minidom.parseString(rough_string) | |
20 | return reparsed.toprettyxml(indent=" ") |
|
17 | return reparsed.toprettyxml(indent=" ") | |
21 |
|
18 | |||
22 | class ParameterConf(): |
|
19 | class ParameterConf(): | |
23 |
|
20 | |||
24 | id = None |
|
21 | id = None | |
25 | name = None |
|
22 | name = None | |
26 | value = None |
|
23 | value = None | |
27 | format = None |
|
24 | format = None | |
28 |
|
25 | |||
29 | __value = None |
|
26 | __value = None | |
30 |
|
27 | |||
31 | ELEMENTNAME = 'Parameter' |
|
28 | ELEMENTNAME = 'Parameter' | |
32 |
|
29 | |||
33 | def __init__(self): |
|
30 | def __init__(self): | |
34 |
|
31 | |||
35 | self.format = 'str' |
|
32 | self.format = 'str' | |
36 |
|
33 | |||
37 | def getElementName(self): |
|
34 | def getElementName(self): | |
38 |
|
35 | |||
39 | return self.ELEMENTNAME |
|
36 | return self.ELEMENTNAME | |
40 |
|
37 | |||
41 | def getValue(self): |
|
38 | def getValue(self): | |
42 |
|
39 | |||
43 | if self.__value != None: |
|
40 | if self.__value != None: | |
44 |
|
41 | |||
45 | return self.__value |
|
42 | return self.__value | |
46 |
|
43 | |||
47 | value = self.value |
|
44 | value = self.value | |
48 |
|
45 | |||
49 | if self.format == 'list': |
|
46 | if self.format == 'list': | |
50 | strList = value.split(',') |
|
47 | strList = value.split(',') | |
51 | return strList |
|
48 | return strList | |
52 |
|
49 | |||
53 | if self.format == 'intlist': |
|
50 | if self.format == 'intlist': | |
54 | strList = value.split(',') |
|
51 | strList = value.split(',') | |
55 | intList = [int(x) for x in strList] |
|
52 | intList = [int(x) for x in strList] | |
56 | return intList |
|
53 | return intList | |
57 |
|
54 | |||
58 | if self.format == 'floatlist': |
|
55 | if self.format == 'floatlist': | |
59 | strList = value.split(',') |
|
56 | strList = value.split(',') | |
60 | floatList = [float(x) for x in strList] |
|
57 | floatList = [float(x) for x in strList] | |
61 | return floatList |
|
58 | return floatList | |
62 |
|
59 | |||
63 | if self.format == 'date': |
|
60 | if self.format == 'date': | |
64 | strList = value.split('/') |
|
61 | strList = value.split('/') | |
65 | intList = [int(x) for x in strList] |
|
62 | intList = [int(x) for x in strList] | |
66 | date = datetime.date(intList[0], intList[1], intList[2]) |
|
63 | date = datetime.date(intList[0], intList[1], intList[2]) | |
67 | return date |
|
64 | return date | |
68 |
|
65 | |||
69 | if self.format == 'time': |
|
66 | if self.format == 'time': | |
70 | strList = value.split(':') |
|
67 | strList = value.split(':') | |
71 | intList = [int(x) for x in strList] |
|
68 | intList = [int(x) for x in strList] | |
72 | time = datetime.time(intList[0], intList[1], intList[2]) |
|
69 | time = datetime.time(intList[0], intList[1], intList[2]) | |
73 | return time |
|
70 | return time | |
74 |
|
71 | |||
75 | if self.format == 'bool': |
|
72 | if self.format == 'bool': | |
76 | value = int(value) |
|
73 | value = int(value) | |
77 |
|
74 | |||
78 | if self.format == 'pairsList': |
|
75 | if self.format == 'pairsList': | |
79 | """ |
|
76 | """ | |
80 | Example: |
|
77 | Example: | |
81 | value = (0,1),(1,2) |
|
78 | value = (0,1),(1,2) | |
82 | """ |
|
79 | """ | |
83 |
|
80 | |||
84 | value = value.replace('(', '') |
|
81 | value = value.replace('(', '') | |
85 | value = value.replace(')', '') |
|
82 | value = value.replace(')', '') | |
86 |
|
83 | |||
87 | strList = value.split(',') |
|
84 | strList = value.split(',') | |
88 | intList = [int(item) for item in strList] |
|
85 | intList = [int(item) for item in strList] | |
89 | pairList = [] |
|
86 | pairList = [] | |
90 | for i in range(len(intList)/2): |
|
87 | for i in range(len(intList)/2): | |
91 | pairList.append((intList[i*2], intList[i*2 + 1])) |
|
88 | pairList.append((intList[i*2], intList[i*2 + 1])) | |
92 |
|
89 | |||
93 | return pairList |
|
90 | return pairList | |
94 |
|
91 | |||
95 | func = eval(self.format) |
|
92 | func = eval(self.format) | |
96 |
|
93 | |||
97 | self.__value = func(value) |
|
94 | self.__value = func(value) | |
98 |
|
95 | |||
99 | return self.__value |
|
96 | return self.__value | |
100 |
|
97 | |||
101 | def setup(self, id, name, value, format='str'): |
|
98 | def setup(self, id, name, value, format='str'): | |
102 |
|
99 | |||
103 | self.id = id |
|
100 | self.id = id | |
104 | self.name = name |
|
101 | self.name = name | |
105 | self.value = str(value) |
|
102 | self.value = str(value) | |
106 | self.format = format |
|
103 | self.format = format | |
107 |
|
104 | |||
108 | def makeXml(self, opElement): |
|
105 | def makeXml(self, opElement): | |
109 |
|
106 | |||
110 | parmElement = SubElement(opElement, self.ELEMENTNAME) |
|
107 | parmElement = SubElement(opElement, self.ELEMENTNAME) | |
111 | parmElement.set('id', str(self.id)) |
|
108 | parmElement.set('id', str(self.id)) | |
112 | parmElement.set('name', self.name) |
|
109 | parmElement.set('name', self.name) | |
113 | parmElement.set('value', self.value) |
|
110 | parmElement.set('value', self.value) | |
114 | parmElement.set('format', self.format) |
|
111 | parmElement.set('format', self.format) | |
115 |
|
112 | |||
116 | def readXml(self, parmElement): |
|
113 | def readXml(self, parmElement): | |
117 |
|
114 | |||
118 | self.id = parmElement.get('id') |
|
115 | self.id = parmElement.get('id') | |
119 | self.name = parmElement.get('name') |
|
116 | self.name = parmElement.get('name') | |
120 | self.value = parmElement.get('value') |
|
117 | self.value = parmElement.get('value') | |
121 | self.format = parmElement.get('format') |
|
118 | self.format = parmElement.get('format') | |
122 |
|
119 | |||
123 | def printattr(self): |
|
120 | def printattr(self): | |
124 |
|
121 | |||
125 | print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format) |
|
122 | print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format) | |
126 |
|
123 | |||
127 | class OperationConf(): |
|
124 | class OperationConf(): | |
128 |
|
125 | |||
129 | id = None |
|
126 | id = None | |
130 | name = None |
|
127 | name = None | |
131 | priority = None |
|
128 | priority = None | |
132 | type = None |
|
129 | type = None | |
133 |
|
130 | |||
134 | parmConfObjList = [] |
|
131 | parmConfObjList = [] | |
135 |
|
132 | |||
136 | ELEMENTNAME = 'Operation' |
|
133 | ELEMENTNAME = 'Operation' | |
137 |
|
134 | |||
138 | def __init__(self): |
|
135 | def __init__(self): | |
139 |
|
136 | |||
140 | id = 0 |
|
137 | id = 0 | |
141 | name = None |
|
138 | name = None | |
142 | priority = None |
|
139 | priority = None | |
143 | type = 'self' |
|
140 | type = 'self' | |
144 |
|
141 | |||
145 |
|
142 | |||
146 | def __getNewId(self): |
|
143 | def __getNewId(self): | |
147 |
|
144 | |||
148 | return int(self.id)*10 + len(self.parmConfObjList) + 1 |
|
145 | return int(self.id)*10 + len(self.parmConfObjList) + 1 | |
149 |
|
146 | |||
150 | def getElementName(self): |
|
147 | def getElementName(self): | |
151 |
|
148 | |||
152 | return self.ELEMENTNAME |
|
149 | return self.ELEMENTNAME | |
153 |
|
150 | |||
154 | def getParameterObjList(self): |
|
151 | def getParameterObjList(self): | |
155 |
|
152 | |||
156 | return self.parmConfObjList |
|
153 | return self.parmConfObjList | |
157 |
|
154 | |||
158 | def setup(self, id, name, priority, type): |
|
155 | def setup(self, id, name, priority, type): | |
159 |
|
156 | |||
160 | self.id = id |
|
157 | self.id = id | |
161 | self.name = name |
|
158 | self.name = name | |
162 | self.type = type |
|
159 | self.type = type | |
163 | self.priority = priority |
|
160 | self.priority = priority | |
164 |
|
161 | |||
165 | self.parmConfObjList = [] |
|
162 | self.parmConfObjList = [] | |
166 |
|
163 | |||
167 | def addParameter(self, name, value, format='str'): |
|
164 | def addParameter(self, name, value, format='str'): | |
168 |
|
165 | |||
169 | id = self.__getNewId() |
|
166 | id = self.__getNewId() | |
170 |
|
167 | |||
171 | parmConfObj = ParameterConf() |
|
168 | parmConfObj = ParameterConf() | |
172 | parmConfObj.setup(id, name, value, format) |
|
169 | parmConfObj.setup(id, name, value, format) | |
173 |
|
170 | |||
174 | self.parmConfObjList.append(parmConfObj) |
|
171 | self.parmConfObjList.append(parmConfObj) | |
175 |
|
172 | |||
176 | return parmConfObj |
|
173 | return parmConfObj | |
177 |
|
174 | |||
178 | def makeXml(self, upElement): |
|
175 | def makeXml(self, upElement): | |
179 |
|
176 | |||
180 | opElement = SubElement(upElement, self.ELEMENTNAME) |
|
177 | opElement = SubElement(upElement, self.ELEMENTNAME) | |
181 | opElement.set('id', str(self.id)) |
|
178 | opElement.set('id', str(self.id)) | |
182 | opElement.set('name', self.name) |
|
179 | opElement.set('name', self.name) | |
183 | opElement.set('type', self.type) |
|
180 | opElement.set('type', self.type) | |
184 | opElement.set('priority', str(self.priority)) |
|
181 | opElement.set('priority', str(self.priority)) | |
185 |
|
182 | |||
186 | for parmConfObj in self.parmConfObjList: |
|
183 | for parmConfObj in self.parmConfObjList: | |
187 | parmConfObj.makeXml(opElement) |
|
184 | parmConfObj.makeXml(opElement) | |
188 |
|
185 | |||
189 | def readXml(self, opElement): |
|
186 | def readXml(self, opElement): | |
190 |
|
187 | |||
191 | self.id = opElement.get('id') |
|
188 | self.id = opElement.get('id') | |
192 | self.name = opElement.get('name') |
|
189 | self.name = opElement.get('name') | |
193 | self.type = opElement.get('type') |
|
190 | self.type = opElement.get('type') | |
194 | self.priority = opElement.get('priority') |
|
191 | self.priority = opElement.get('priority') | |
195 |
|
192 | |||
196 | self.parmConfObjList = [] |
|
193 | self.parmConfObjList = [] | |
197 |
|
194 | |||
198 | parmElementList = opElement.getiterator(ParameterConf().getElementName()) |
|
195 | parmElementList = opElement.getiterator(ParameterConf().getElementName()) | |
199 |
|
196 | |||
200 | for parmElement in parmElementList: |
|
197 | for parmElement in parmElementList: | |
201 | parmConfObj = ParameterConf() |
|
198 | parmConfObj = ParameterConf() | |
202 | parmConfObj.readXml(parmElement) |
|
199 | parmConfObj.readXml(parmElement) | |
203 | self.parmConfObjList.append(parmConfObj) |
|
200 | self.parmConfObjList.append(parmConfObj) | |
204 |
|
201 | |||
205 | def printattr(self): |
|
202 | def printattr(self): | |
206 |
|
203 | |||
207 | print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, |
|
204 | print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, | |
208 | self.id, |
|
205 | self.id, | |
209 | self.name, |
|
206 | self.name, | |
210 | self.type, |
|
207 | self.type, | |
211 | self.priority) |
|
208 | self.priority) | |
212 |
|
209 | |||
213 | for parmConfObj in self.parmConfObjList: |
|
210 | for parmConfObj in self.parmConfObjList: | |
214 | parmConfObj.printattr() |
|
211 | parmConfObj.printattr() | |
215 |
|
212 | |||
216 | def createObject(self): |
|
213 | def createObject(self): | |
217 |
|
214 | |||
218 | if self.type == 'self': |
|
215 | if self.type == 'self': | |
219 | raise ValueError, "This operation type cannot be created" |
|
216 | raise ValueError, "This operation type cannot be created" | |
220 |
|
217 | |||
221 | if self.type == 'other': |
|
218 | if self.type == 'external' or self.type == 'other': | |
222 | className = eval(self.name) |
|
219 | className = eval(self.name) | |
223 | opObj = className() |
|
220 | opObj = className() | |
224 |
|
221 | |||
225 | return opObj |
|
222 | return opObj | |
226 |
|
223 | |||
227 | class ProcUnitConf(): |
|
224 | class ProcUnitConf(): | |
228 |
|
225 | |||
229 | id = None |
|
226 | id = None | |
230 | name = None |
|
227 | name = None | |
231 | datatype = None |
|
228 | datatype = None | |
232 | inputId = None |
|
229 | inputId = None | |
233 |
|
230 | |||
234 | opConfObjList = [] |
|
231 | opConfObjList = [] | |
235 |
|
232 | |||
236 | procUnitObj = None |
|
233 | procUnitObj = None | |
237 | opObjList = [] |
|
234 | opObjList = [] | |
238 |
|
235 | |||
239 | ELEMENTNAME = 'ProcUnit' |
|
236 | ELEMENTNAME = 'ProcUnit' | |
240 |
|
237 | |||
241 | def __init__(self): |
|
238 | def __init__(self): | |
242 |
|
239 | |||
243 | self.id = None |
|
240 | self.id = None | |
244 | self.datatype = None |
|
241 | self.datatype = None | |
245 | self.name = None |
|
242 | self.name = None | |
246 | self.inputId = None |
|
243 | self.inputId = None | |
247 |
|
244 | |||
248 | self.opConfObjList = [] |
|
245 | self.opConfObjList = [] | |
249 |
|
246 | |||
250 | self.procUnitObj = None |
|
247 | self.procUnitObj = None | |
251 | self.opObjDict = {} |
|
248 | self.opObjDict = {} | |
252 |
|
249 | |||
253 | def __getPriority(self): |
|
250 | def __getPriority(self): | |
254 |
|
251 | |||
255 | return len(self.opConfObjList)+1 |
|
252 | return len(self.opConfObjList)+1 | |
256 |
|
253 | |||
257 | def __getNewId(self): |
|
254 | def __getNewId(self): | |
258 |
|
255 | |||
259 | return int(self.id)*10 + len(self.opConfObjList) + 1 |
|
256 | return int(self.id)*10 + len(self.opConfObjList) + 1 | |
260 |
|
257 | |||
261 | def getElementName(self): |
|
258 | def getElementName(self): | |
262 |
|
259 | |||
263 | return self.ELEMENTNAME |
|
260 | return self.ELEMENTNAME | |
264 |
|
261 | |||
265 | def getId(self): |
|
262 | def getId(self): | |
266 |
|
263 | |||
267 | return str(self.id) |
|
264 | return str(self.id) | |
268 |
|
265 | |||
269 | def getInputId(self): |
|
266 | def getInputId(self): | |
270 |
|
267 | |||
271 | return str(self.inputId) |
|
268 | return str(self.inputId) | |
272 |
|
269 | |||
273 | def getOperationObjList(self): |
|
270 | def getOperationObjList(self): | |
274 |
|
271 | |||
275 | return self.opConfObjList |
|
272 | return self.opConfObjList | |
276 |
|
273 | |||
277 | def getProcUnitObj(self): |
|
274 | def getProcUnitObj(self): | |
278 |
|
275 | |||
279 | return self.procUnitObj |
|
276 | return self.procUnitObj | |
280 |
|
277 | |||
281 | def setup(self, id, name, datatype, inputId): |
|
278 | def setup(self, id, name, datatype, inputId): | |
282 |
|
279 | |||
283 | self.id = id |
|
280 | self.id = id | |
284 | self.name = name |
|
281 | self.name = name | |
285 | self.datatype = datatype |
|
282 | self.datatype = datatype | |
286 | self.inputId = inputId |
|
283 | self.inputId = inputId | |
287 |
|
284 | |||
288 | self.opConfObjList = [] |
|
285 | self.opConfObjList = [] | |
289 |
|
286 | |||
290 |
self.addOperation(name=' |
|
287 | self.addOperation(name='run', optype='self') | |
291 |
|
288 | |||
292 | def addParameter(self, **kwargs): |
|
289 | def addParameter(self, **kwargs): | |
293 |
|
290 | |||
294 | opObj = self.opConfObjList[0] |
|
291 | opObj = self.opConfObjList[0] | |
295 |
|
292 | |||
296 | opObj.addParameter(**kwargs) |
|
293 | opObj.addParameter(**kwargs) | |
297 |
|
294 | |||
298 | return opObj |
|
295 | return opObj | |
299 |
|
296 | |||
300 | def addOperation(self, name, optype='self'): |
|
297 | def addOperation(self, name, optype='self'): | |
301 |
|
298 | |||
302 | id = self.__getNewId() |
|
299 | id = self.__getNewId() | |
303 | priority = self.__getPriority() |
|
300 | priority = self.__getPriority() | |
304 |
|
301 | |||
305 | opConfObj = OperationConf() |
|
302 | opConfObj = OperationConf() | |
306 | opConfObj.setup(id, name=name, priority=priority, type=optype) |
|
303 | opConfObj.setup(id, name=name, priority=priority, type=optype) | |
307 |
|
304 | |||
308 | self.opConfObjList.append(opConfObj) |
|
305 | self.opConfObjList.append(opConfObj) | |
309 |
|
306 | |||
310 | return opConfObj |
|
307 | return opConfObj | |
311 |
|
308 | |||
312 | def makeXml(self, procUnitElement): |
|
309 | def makeXml(self, procUnitElement): | |
313 |
|
310 | |||
314 | upElement = SubElement(procUnitElement, self.ELEMENTNAME) |
|
311 | upElement = SubElement(procUnitElement, self.ELEMENTNAME) | |
315 | upElement.set('id', str(self.id)) |
|
312 | upElement.set('id', str(self.id)) | |
316 | upElement.set('name', self.name) |
|
313 | upElement.set('name', self.name) | |
317 | upElement.set('datatype', self.datatype) |
|
314 | upElement.set('datatype', self.datatype) | |
318 | upElement.set('inputId', str(self.inputId)) |
|
315 | upElement.set('inputId', str(self.inputId)) | |
319 |
|
316 | |||
320 | for opConfObj in self.opConfObjList: |
|
317 | for opConfObj in self.opConfObjList: | |
321 | opConfObj.makeXml(upElement) |
|
318 | opConfObj.makeXml(upElement) | |
322 |
|
319 | |||
323 | def readXml(self, upElement): |
|
320 | def readXml(self, upElement): | |
324 |
|
321 | |||
325 | self.id = upElement.get('id') |
|
322 | self.id = upElement.get('id') | |
326 | self.name = upElement.get('name') |
|
323 | self.name = upElement.get('name') | |
327 | self.datatype = upElement.get('datatype') |
|
324 | self.datatype = upElement.get('datatype') | |
328 | self.inputId = upElement.get('inputId') |
|
325 | self.inputId = upElement.get('inputId') | |
329 |
|
326 | |||
330 | self.opConfObjList = [] |
|
327 | self.opConfObjList = [] | |
331 |
|
328 | |||
332 | opElementList = upElement.getiterator(OperationConf().getElementName()) |
|
329 | opElementList = upElement.getiterator(OperationConf().getElementName()) | |
333 |
|
330 | |||
334 | for opElement in opElementList: |
|
331 | for opElement in opElementList: | |
335 | opConfObj = OperationConf() |
|
332 | opConfObj = OperationConf() | |
336 | opConfObj.readXml(opElement) |
|
333 | opConfObj.readXml(opElement) | |
337 | self.opConfObjList.append(opConfObj) |
|
334 | self.opConfObjList.append(opConfObj) | |
338 |
|
335 | |||
339 | def printattr(self): |
|
336 | def printattr(self): | |
340 |
|
337 | |||
341 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, |
|
338 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, | |
342 | self.id, |
|
339 | self.id, | |
343 | self.name, |
|
340 | self.name, | |
344 | self.datatype, |
|
341 | self.datatype, | |
345 | self.inputId) |
|
342 | self.inputId) | |
346 |
|
343 | |||
347 | for opConfObj in self.opConfObjList: |
|
344 | for opConfObj in self.opConfObjList: | |
348 | opConfObj.printattr() |
|
345 | opConfObj.printattr() | |
349 |
|
346 | |||
350 | def createObjects(self): |
|
347 | def createObjects(self): | |
351 |
|
348 | |||
352 | className = eval(self.name) |
|
349 | className = eval(self.name) | |
353 | procUnitObj = className() |
|
350 | procUnitObj = className() | |
354 |
|
351 | |||
355 | for opConfObj in self.opConfObjList: |
|
352 | for opConfObj in self.opConfObjList: | |
356 |
|
353 | |||
357 | if opConfObj.type == 'self': |
|
354 | if opConfObj.type == 'self': | |
358 | continue |
|
355 | continue | |
359 |
|
356 | |||
360 | opObj = opConfObj.createObject() |
|
357 | opObj = opConfObj.createObject() | |
361 |
|
358 | |||
362 | self.opObjDict[opConfObj.id] = opObj |
|
359 | self.opObjDict[opConfObj.id] = opObj | |
363 | procUnitObj.addOperation(opObj, opConfObj.id) |
|
360 | procUnitObj.addOperation(opObj, opConfObj.id) | |
364 |
|
361 | |||
365 | self.procUnitObj = procUnitObj |
|
362 | self.procUnitObj = procUnitObj | |
366 |
|
363 | |||
367 | return procUnitObj |
|
364 | return procUnitObj | |
368 |
|
365 | |||
369 | def run(self): |
|
366 | def run(self): | |
370 |
|
367 | |||
371 | finalSts = False |
|
368 | finalSts = False | |
372 |
|
369 | |||
373 | for opConfObj in self.opConfObjList: |
|
370 | for opConfObj in self.opConfObjList: | |
374 |
|
371 | |||
375 | kwargs = {} |
|
372 | kwargs = {} | |
376 | for parmConfObj in opConfObj.getParameterObjList(): |
|
373 | for parmConfObj in opConfObj.getParameterObjList(): | |
377 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
374 | kwargs[parmConfObj.name] = parmConfObj.getValue() | |
378 |
|
375 | |||
379 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) |
|
376 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) | |
380 |
sts = self.procUnitObj.call(opConfObj, |
|
377 | sts = self.procUnitObj.call(opType = opConfObj.type, | |
|
378 | opName = opConfObj.name, | |||
|
379 | opId = opConfObj.id, | |||
|
380 | **kwargs) | |||
381 | finalSts = finalSts or sts |
|
381 | finalSts = finalSts or sts | |
382 |
|
382 | |||
383 | return finalSts |
|
383 | return finalSts | |
384 |
|
384 | |||
385 | class ReadUnitConf(ProcUnitConf): |
|
385 | class ReadUnitConf(ProcUnitConf): | |
386 |
|
386 | |||
387 | path = None |
|
387 | path = None | |
388 | startDate = None |
|
388 | startDate = None | |
389 | endDate = None |
|
389 | endDate = None | |
390 | startTime = None |
|
390 | startTime = None | |
391 | endTime = None |
|
391 | endTime = None | |
392 |
|
392 | |||
393 | ELEMENTNAME = 'ReadUnit' |
|
393 | ELEMENTNAME = 'ReadUnit' | |
394 |
|
394 | |||
395 | def __init__(self): |
|
395 | def __init__(self): | |
396 |
|
396 | |||
397 | self.id = None |
|
397 | self.id = None | |
398 | self.datatype = None |
|
398 | self.datatype = None | |
399 | self.name = None |
|
399 | self.name = None | |
400 | self.inputId = 0 |
|
400 | self.inputId = 0 | |
401 |
|
401 | |||
402 | self.opConfObjList = [] |
|
402 | self.opConfObjList = [] | |
403 | self.opObjList = [] |
|
403 | self.opObjList = [] | |
404 |
|
404 | |||
405 | def getElementName(self): |
|
405 | def getElementName(self): | |
406 |
|
406 | |||
407 | return self.ELEMENTNAME |
|
407 | return self.ELEMENTNAME | |
408 |
|
408 | |||
409 | def setup(self, id, name, datatype, path, startDate, endDate, startTime, endTime, **kwargs): |
|
409 | def setup(self, id, name, datatype, path="", startDate="", endDate="", startTime="", endTime="", **kwargs): | |
410 |
|
410 | |||
411 | self.id = id |
|
411 | self.id = id | |
412 | self.name = name |
|
412 | self.name = name | |
413 | self.datatype = datatype |
|
413 | self.datatype = datatype | |
414 |
|
414 | |||
415 | self.path = path |
|
415 | self.path = path | |
416 | self.startDate = startDate |
|
416 | self.startDate = startDate | |
417 | self.endDate = endDate |
|
417 | self.endDate = endDate | |
418 | self.startTime = startTime |
|
418 | self.startTime = startTime | |
419 | self.endTime = endTime |
|
419 | self.endTime = endTime | |
420 |
|
420 | |||
421 | self.addRunOperation(**kwargs) |
|
421 | self.addRunOperation(**kwargs) | |
422 |
|
422 | |||
423 | def addRunOperation(self, **kwargs): |
|
423 | def addRunOperation(self, **kwargs): | |
424 |
|
424 | |||
425 | opObj = self.addOperation(name = 'run', optype = 'self') |
|
425 | opObj = self.addOperation(name = 'run', optype = 'self') | |
426 |
|
426 | |||
427 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
427 | opObj.addParameter(name='path' , value=self.path, format='str') | |
428 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
428 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') | |
429 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
429 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') | |
430 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
430 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') | |
431 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
431 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') | |
432 |
|
432 | |||
433 | for key, value in kwargs.items(): |
|
433 | for key, value in kwargs.items(): | |
434 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
434 | opObj.addParameter(name=key, value=value, format=type(value).__name__) | |
435 |
|
435 | |||
436 | return opObj |
|
436 | return opObj | |
437 |
|
437 | |||
438 |
|
438 | |||
439 | class Project(): |
|
439 | class Project(): | |
440 |
|
440 | |||
441 | id = None |
|
441 | id = None | |
442 | name = None |
|
442 | name = None | |
443 | description = None |
|
443 | description = None | |
444 | # readUnitConfObjList = None |
|
444 | # readUnitConfObjList = None | |
445 | procUnitConfObjDict = None |
|
445 | procUnitConfObjDict = None | |
446 |
|
446 | |||
447 | ELEMENTNAME = 'Project' |
|
447 | ELEMENTNAME = 'Project' | |
448 |
|
448 | |||
449 | def __init__(self): |
|
449 | def __init__(self): | |
450 |
|
450 | |||
451 | self.id = None |
|
451 | self.id = None | |
452 | self.name = None |
|
452 | self.name = None | |
453 | self.description = None |
|
453 | self.description = None | |
454 |
|
454 | |||
455 | # self.readUnitConfObjList = [] |
|
455 | # self.readUnitConfObjList = [] | |
456 | self.procUnitConfObjDict = {} |
|
456 | self.procUnitConfObjDict = {} | |
457 |
|
457 | |||
458 | def __getNewId(self): |
|
458 | def __getNewId(self): | |
459 |
|
459 | |||
460 | id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1 |
|
460 | id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1 | |
461 |
|
461 | |||
462 | return str(id) |
|
462 | return str(id) | |
463 |
|
463 | |||
464 | def getElementName(self): |
|
464 | def getElementName(self): | |
465 |
|
465 | |||
466 | return self.ELEMENTNAME |
|
466 | return self.ELEMENTNAME | |
467 |
|
467 | |||
468 | def setup(self, id, name, description): |
|
468 | def setup(self, id, name, description): | |
469 |
|
469 | |||
470 | self.id = id |
|
470 | self.id = id | |
471 | self.name = name |
|
471 | self.name = name | |
472 | self.description = description |
|
472 | self.description = description | |
473 |
|
473 | |||
474 |
def addReadUnit(self, datatype, |
|
474 | def addReadUnit(self, datatype, **kwargs): | |
475 |
|
475 | |||
476 | id = self.__getNewId() |
|
476 | id = self.__getNewId() | |
477 |
name = '%s |
|
477 | name = '%s' %(datatype) | |
478 |
|
478 | |||
479 | readUnitConfObj = ReadUnitConf() |
|
479 | readUnitConfObj = ReadUnitConf() | |
480 |
readUnitConfObj.setup(id, name, datatype, |
|
480 | readUnitConfObj.setup(id, name, datatype, **kwargs) | |
481 |
|
481 | |||
482 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
482 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj | |
483 |
|
483 | |||
484 | return readUnitConfObj |
|
484 | return readUnitConfObj | |
485 |
|
485 | |||
486 | def addProcUnit(self, datatype, inputId): |
|
486 | def addProcUnit(self, datatype, inputId): | |
487 |
|
487 | |||
488 | id = self.__getNewId() |
|
488 | id = self.__getNewId() | |
489 |
name = '%s |
|
489 | name = '%s' %(datatype) | |
490 |
|
490 | |||
491 | procUnitConfObj = ProcUnitConf() |
|
491 | procUnitConfObj = ProcUnitConf() | |
492 | procUnitConfObj.setup(id, name, datatype, inputId) |
|
492 | procUnitConfObj.setup(id, name, datatype, inputId) | |
493 |
|
493 | |||
494 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
494 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj | |
495 |
|
495 | |||
496 | return procUnitConfObj |
|
496 | return procUnitConfObj | |
497 |
|
497 | |||
498 | def makeXml(self): |
|
498 | def makeXml(self): | |
499 |
|
499 | |||
500 | projectElement = Element('Project') |
|
500 | projectElement = Element('Project') | |
501 | projectElement.set('id', str(self.id)) |
|
501 | projectElement.set('id', str(self.id)) | |
502 | projectElement.set('name', self.name) |
|
502 | projectElement.set('name', self.name) | |
503 | projectElement.set('description', self.description) |
|
503 | projectElement.set('description', self.description) | |
504 |
|
504 | |||
505 | # for readUnitConfObj in self.readUnitConfObjList: |
|
505 | # for readUnitConfObj in self.readUnitConfObjList: | |
506 | # readUnitConfObj.makeXml(projectElement) |
|
506 | # readUnitConfObj.makeXml(projectElement) | |
507 |
|
507 | |||
508 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
508 | for procUnitConfObj in self.procUnitConfObjDict.values(): | |
509 | procUnitConfObj.makeXml(projectElement) |
|
509 | procUnitConfObj.makeXml(projectElement) | |
510 |
|
510 | |||
511 | self.projectElement = projectElement |
|
511 | self.projectElement = projectElement | |
512 |
|
512 | |||
513 | def writeXml(self, filename): |
|
513 | def writeXml(self, filename): | |
514 |
|
514 | |||
515 | self.makeXml() |
|
515 | self.makeXml() | |
516 |
|
516 | |||
517 | print prettify(self.projectElement) |
|
517 | print prettify(self.projectElement) | |
518 |
|
518 | |||
519 | ElementTree(self.projectElement).write(filename, method='xml') |
|
519 | ElementTree(self.projectElement).write(filename, method='xml') | |
520 |
|
520 | |||
521 | def readXml(self, filename): |
|
521 | def readXml(self, filename): | |
522 |
|
522 | |||
523 | #tree = ET.parse(filename) |
|
523 | #tree = ET.parse(filename) | |
524 | self.projectElement = None |
|
524 | self.projectElement = None | |
525 | # self.readUnitConfObjList = [] |
|
525 | # self.readUnitConfObjList = [] | |
526 | self.procUnitConfObjDict = {} |
|
526 | self.procUnitConfObjDict = {} | |
527 |
|
527 | |||
528 | self.projectElement = ElementTree().parse(filename) |
|
528 | self.projectElement = ElementTree().parse(filename) | |
529 |
|
529 | |||
530 | self.project = self.projectElement.tag |
|
530 | self.project = self.projectElement.tag | |
531 |
|
531 | |||
532 | self.id = self.projectElement.get('id') |
|
532 | self.id = self.projectElement.get('id') | |
533 | self.name = self.projectElement.get('name') |
|
533 | self.name = self.projectElement.get('name') | |
534 | self.description = self.projectElement.get('description') |
|
534 | self.description = self.projectElement.get('description') | |
535 |
|
535 | |||
536 | readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName()) |
|
536 | readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName()) | |
537 |
|
537 | |||
538 | for readUnitElement in readUnitElementList: |
|
538 | for readUnitElement in readUnitElementList: | |
539 | readUnitConfObj = ReadUnitConf() |
|
539 | readUnitConfObj = ReadUnitConf() | |
540 | readUnitConfObj.readXml(readUnitElement) |
|
540 | readUnitConfObj.readXml(readUnitElement) | |
541 |
|
541 | |||
542 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
542 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj | |
543 |
|
543 | |||
544 | procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName()) |
|
544 | procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName()) | |
545 |
|
545 | |||
546 | for procUnitElement in procUnitElementList: |
|
546 | for procUnitElement in procUnitElementList: | |
547 | procUnitConfObj = ProcUnitConf() |
|
547 | procUnitConfObj = ProcUnitConf() | |
548 | procUnitConfObj.readXml(procUnitElement) |
|
548 | procUnitConfObj.readXml(procUnitElement) | |
549 |
|
549 | |||
550 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
550 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj | |
551 |
|
551 | |||
552 | def printattr(self): |
|
552 | def printattr(self): | |
553 |
|
553 | |||
554 | print "Project[%s]: name = %s, description = %s" %(self.id, |
|
554 | print "Project[%s]: name = %s, description = %s" %(self.id, | |
555 | self.name, |
|
555 | self.name, | |
556 | self.description) |
|
556 | self.description) | |
557 |
|
557 | |||
558 | # for readUnitConfObj in self.readUnitConfObjList: |
|
558 | # for readUnitConfObj in self.readUnitConfObjList: | |
559 | # readUnitConfObj.printattr() |
|
559 | # readUnitConfObj.printattr() | |
560 |
|
560 | |||
561 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
561 | for procUnitConfObj in self.procUnitConfObjDict.values(): | |
562 | procUnitConfObj.printattr() |
|
562 | procUnitConfObj.printattr() | |
563 |
|
563 | |||
564 | def createObjects(self): |
|
564 | def createObjects(self): | |
565 |
|
565 | |||
566 | # for readUnitConfObj in self.readUnitConfObjList: |
|
566 | # for readUnitConfObj in self.readUnitConfObjList: | |
567 | # readUnitConfObj.createObjects() |
|
567 | # readUnitConfObj.createObjects() | |
568 |
|
568 | |||
569 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
569 | for procUnitConfObj in self.procUnitConfObjDict.values(): | |
570 | procUnitConfObj.createObjects() |
|
570 | procUnitConfObj.createObjects() | |
571 |
|
571 | |||
572 |
def __connect(self, objIN, |
|
572 | def __connect(self, objIN, thisObj): | |
573 |
|
573 | |||
574 |
|
|
574 | thisObj.setInput(objIN.getOutputObj()) | |
575 |
|
575 | |||
576 | def connectObjects(self): |
|
576 | def connectObjects(self): | |
577 |
|
577 | |||
578 |
for |
|
578 | for thisPUConfObj in self.procUnitConfObjDict.values(): | |
579 |
|
579 | |||
580 |
inputId = |
|
580 | inputId = thisPUConfObj.getInputId() | |
581 |
|
581 | |||
582 | if int(inputId) == 0: |
|
582 | if int(inputId) == 0: | |
583 | continue |
|
583 | continue | |
584 |
|
584 | |||
|
585 | #Get input object | |||
585 | puConfINObj = self.procUnitConfObjDict[inputId] |
|
586 | puConfINObj = self.procUnitConfObjDict[inputId] | |
|
587 | puObjIN = puConfINObj.getProcUnitObj() | |||
586 |
|
588 | |||
587 | puObj = puConfObj.getProcUnitObj() |
|
589 | #Get current object | |
588 |
|
|
590 | thisPUObj = thisPUConfObj.getProcUnitObj() | |
589 |
|
591 | |||
590 |
self.__connect(pu |
|
592 | self.__connect(puObjIN, thisPUObj) | |
591 |
|
593 | |||
592 | def run(self): |
|
594 | def run(self): | |
593 |
|
595 | |||
594 | # for readUnitConfObj in self.readUnitConfObjList: |
|
596 | # for readUnitConfObj in self.readUnitConfObjList: | |
595 | # readUnitConfObj.run() |
|
597 | # readUnitConfObj.run() | |
596 |
|
598 | |||
597 | while(True): |
|
599 | while(True): | |
598 |
|
600 | |||
599 | finalSts = False |
|
601 | finalSts = False | |
600 |
|
602 | |||
601 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
603 | for procUnitConfObj in self.procUnitConfObjDict.values(): | |
602 | #print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) |
|
604 | #print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) | |
603 | sts = procUnitConfObj.run() |
|
605 | sts = procUnitConfObj.run() | |
604 | finalSts = finalSts or sts |
|
606 | finalSts = finalSts or sts | |
605 |
|
607 | |||
606 | #If every process unit finished so end process |
|
608 | #If every process unit finished so end process | |
607 | if not(finalSts): |
|
609 | if not(finalSts): | |
608 | print "Every process units have finished" |
|
610 | print "Every process units have finished" | |
609 | break |
|
611 | break | |
610 |
|
612 | |||
611 | if __name__ == '__main__': |
|
613 | if __name__ == '__main__': | |
612 |
|
614 | |||
613 | desc = "Segundo Test" |
|
615 | desc = "Segundo Test" | |
614 | filename = "schain.xml" |
|
616 | filename = "schain.xml" | |
615 |
|
617 | |||
616 | controllerObj = Project() |
|
618 | controllerObj = Project() | |
617 |
|
619 | |||
618 | controllerObj.setup(id = '191', name='test01', description=desc) |
|
620 | controllerObj.setup(id = '191', name='test01', description=desc) | |
619 |
|
621 | |||
620 | readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage', |
|
622 | readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage', | |
621 | path='data/rawdata/', |
|
623 | path='data/rawdata/', | |
622 | startDate='2011/01/01', |
|
624 | startDate='2011/01/01', | |
623 | endDate='2012/12/31', |
|
625 | endDate='2012/12/31', | |
624 | startTime='00:00:00', |
|
626 | startTime='00:00:00', | |
625 | endTime='23:59:59', |
|
627 | endTime='23:59:59', | |
626 | online=1, |
|
628 | online=1, | |
627 | walk=1) |
|
629 | walk=1) | |
628 |
|
630 | |||
629 | # opObj00 = readUnitConfObj.addOperation(name='printInfo') |
|
631 | # opObj00 = readUnitConfObj.addOperation(name='printInfo') | |
630 |
|
632 | |||
631 | procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId()) |
|
633 | procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId()) | |
632 |
|
634 | |||
633 | opObj10 = procUnitConfObj0.addOperation(name='selectChannels') |
|
635 | opObj10 = procUnitConfObj0.addOperation(name='selectChannels') | |
634 | opObj10.addParameter(name='channelList', value='3,4,5', format='intlist') |
|
636 | opObj10.addParameter(name='channelList', value='3,4,5', format='intlist') | |
635 |
|
637 | |||
636 | opObj10 = procUnitConfObj0.addOperation(name='selectHeights') |
|
638 | opObj10 = procUnitConfObj0.addOperation(name='selectHeights') | |
637 | opObj10.addParameter(name='minHei', value='90', format='float') |
|
639 | opObj10.addParameter(name='minHei', value='90', format='float') | |
638 | opObj10.addParameter(name='maxHei', value='180', format='float') |
|
640 | opObj10.addParameter(name='maxHei', value='180', format='float') | |
639 |
|
641 | |||
640 |
opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype=' |
|
642 | opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='external') | |
641 | opObj12.addParameter(name='n', value='10', format='int') |
|
643 | opObj12.addParameter(name='n', value='10', format='int') | |
642 |
|
644 | |||
643 | procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId()) |
|
645 | procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId()) | |
644 | procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int') |
|
646 | procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int') | |
645 | # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='') |
|
647 | # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='') | |
646 |
|
648 | |||
647 |
|
649 | |||
648 |
opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype=' |
|
650 | opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external') | |
649 | opObj11.addParameter(name='idfigure', value='1', format='int') |
|
651 | opObj11.addParameter(name='idfigure', value='1', format='int') | |
650 | opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str') |
|
652 | opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str') | |
651 | opObj11.addParameter(name='zmin', value='40', format='int') |
|
653 | opObj11.addParameter(name='zmin', value='40', format='int') | |
652 | opObj11.addParameter(name='zmax', value='90', format='int') |
|
654 | opObj11.addParameter(name='zmax', value='90', format='int') | |
653 | opObj11.addParameter(name='showprofile', value='1', format='int') |
|
655 | opObj11.addParameter(name='showprofile', value='1', format='int') | |
654 |
|
656 | |||
655 |
# opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype=' |
|
657 | # opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype='external') | |
656 | # opObj11.addParameter(name='idfigure', value='2', format='int') |
|
658 | # opObj11.addParameter(name='idfigure', value='2', format='int') | |
657 | # opObj11.addParameter(name='wintitle', value='CrossSpectraPlot', format='str') |
|
659 | # opObj11.addParameter(name='wintitle', value='CrossSpectraPlot', format='str') | |
658 | # opObj11.addParameter(name='zmin', value='40', format='int') |
|
660 | # opObj11.addParameter(name='zmin', value='40', format='int') | |
659 | # opObj11.addParameter(name='zmax', value='90', format='int') |
|
661 | # opObj11.addParameter(name='zmax', value='90', format='int') | |
660 |
|
662 | |||
661 |
|
663 | |||
662 | # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Voltage', inputId=procUnitConfObj0.getId()) |
|
664 | # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Voltage', inputId=procUnitConfObj0.getId()) | |
663 | # |
|
665 | # | |
664 |
# opObj12 = procUnitConfObj2.addOperation(name='CohInt', optype=' |
|
666 | # opObj12 = procUnitConfObj2.addOperation(name='CohInt', optype='external') | |
665 | # opObj12.addParameter(name='n', value='2', format='int') |
|
667 | # opObj12.addParameter(name='n', value='2', format='int') | |
666 | # opObj12.addParameter(name='overlapping', value='1', format='int') |
|
668 | # opObj12.addParameter(name='overlapping', value='1', format='int') | |
667 | # |
|
669 | # | |
668 | # procUnitConfObj3 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj2.getId()) |
|
670 | # procUnitConfObj3 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj2.getId()) | |
669 | # procUnitConfObj3.addParameter(name='nFFTPoints', value='32', format='int') |
|
671 | # procUnitConfObj3.addParameter(name='nFFTPoints', value='32', format='int') | |
670 | # |
|
672 | # | |
671 |
# opObj11 = procUnitConfObj3.addOperation(name='SpectraPlot', optype=' |
|
673 | # opObj11 = procUnitConfObj3.addOperation(name='SpectraPlot', optype='external') | |
672 | # opObj11.addParameter(name='idfigure', value='2', format='int') |
|
674 | # opObj11.addParameter(name='idfigure', value='2', format='int') | |
673 | # opObj11.addParameter(name='wintitle', value='SpectraPlot1', format='str') |
|
675 | # opObj11.addParameter(name='wintitle', value='SpectraPlot1', format='str') | |
674 | # opObj11.addParameter(name='zmin', value='40', format='int') |
|
676 | # opObj11.addParameter(name='zmin', value='40', format='int') | |
675 | # opObj11.addParameter(name='zmax', value='90', format='int') |
|
677 | # opObj11.addParameter(name='zmax', value='90', format='int') | |
676 | # opObj11.addParameter(name='showprofile', value='1', format='int') |
|
678 | # opObj11.addParameter(name='showprofile', value='1', format='int') | |
677 |
|
679 | |||
678 |
# opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype=' |
|
680 | # opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='external') | |
679 | # opObj11.addParameter(name='idfigure', value='10', format='int') |
|
681 | # opObj11.addParameter(name='idfigure', value='10', format='int') | |
680 | # opObj11.addParameter(name='wintitle', value='RTI', format='str') |
|
682 | # opObj11.addParameter(name='wintitle', value='RTI', format='str') | |
681 | ## opObj11.addParameter(name='xmin', value='21', format='float') |
|
683 | ## opObj11.addParameter(name='xmin', value='21', format='float') | |
682 | ## opObj11.addParameter(name='xmax', value='22', format='float') |
|
684 | ## opObj11.addParameter(name='xmax', value='22', format='float') | |
683 | # opObj11.addParameter(name='zmin', value='40', format='int') |
|
685 | # opObj11.addParameter(name='zmin', value='40', format='int') | |
684 | # opObj11.addParameter(name='zmax', value='90', format='int') |
|
686 | # opObj11.addParameter(name='zmax', value='90', format='int') | |
685 | # opObj11.addParameter(name='showprofile', value='1', format='int') |
|
687 | # opObj11.addParameter(name='showprofile', value='1', format='int') | |
686 | # opObj11.addParameter(name='timerange', value=str(60), format='int') |
|
688 | # opObj11.addParameter(name='timerange', value=str(60), format='int') | |
687 |
|
689 | |||
688 | # opObj10 = procUnitConfObj1.addOperation(name='selectChannels') |
|
690 | # opObj10 = procUnitConfObj1.addOperation(name='selectChannels') | |
689 | # opObj10.addParameter(name='channelList', value='0,2,4,6', format='intlist') |
|
691 | # opObj10.addParameter(name='channelList', value='0,2,4,6', format='intlist') | |
690 | # |
|
692 | # | |
691 |
# opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype=' |
|
693 | # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='external') | |
692 | # opObj12.addParameter(name='n', value='2', format='int') |
|
694 | # opObj12.addParameter(name='n', value='2', format='int') | |
693 | # |
|
695 | # | |
694 |
# opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype=' |
|
696 | # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external') | |
695 | # opObj11.addParameter(name='idfigure', value='2', format='int') |
|
697 | # opObj11.addParameter(name='idfigure', value='2', format='int') | |
696 | # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str') |
|
698 | # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str') | |
697 | # opObj11.addParameter(name='zmin', value='70', format='int') |
|
699 | # opObj11.addParameter(name='zmin', value='70', format='int') | |
698 | # opObj11.addParameter(name='zmax', value='90', format='int') |
|
700 | # opObj11.addParameter(name='zmax', value='90', format='int') | |
699 | # |
|
701 | # | |
700 | # opObj10 = procUnitConfObj1.addOperation(name='selectChannels') |
|
702 | # opObj10 = procUnitConfObj1.addOperation(name='selectChannels') | |
701 | # opObj10.addParameter(name='channelList', value='2,6', format='intlist') |
|
703 | # opObj10.addParameter(name='channelList', value='2,6', format='intlist') | |
702 | # |
|
704 | # | |
703 |
# opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype=' |
|
705 | # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='external') | |
704 | # opObj12.addParameter(name='n', value='2', format='int') |
|
706 | # opObj12.addParameter(name='n', value='2', format='int') | |
705 | # |
|
707 | # | |
706 |
# opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype=' |
|
708 | # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external') | |
707 | # opObj11.addParameter(name='idfigure', value='3', format='int') |
|
709 | # opObj11.addParameter(name='idfigure', value='3', format='int') | |
708 | # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str') |
|
710 | # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str') | |
709 | # opObj11.addParameter(name='zmin', value='70', format='int') |
|
711 | # opObj11.addParameter(name='zmin', value='70', format='int') | |
710 | # opObj11.addParameter(name='zmax', value='90', format='int') |
|
712 | # opObj11.addParameter(name='zmax', value='90', format='int') | |
711 |
|
713 | |||
712 |
|
714 | |||
713 | # opObj12 = procUnitConfObj1.addOperation(name='decoder') |
|
715 | # opObj12 = procUnitConfObj1.addOperation(name='decoder') | |
714 | # opObj12.addParameter(name='ncode', value='2', format='int') |
|
716 | # opObj12.addParameter(name='ncode', value='2', format='int') | |
715 | # opObj12.addParameter(name='nbauds', value='8', format='int') |
|
717 | # opObj12.addParameter(name='nbauds', value='8', format='int') | |
716 | # opObj12.addParameter(name='code0', value='001110011', format='int') |
|
718 | # opObj12.addParameter(name='code0', value='001110011', format='int') | |
717 | # opObj12.addParameter(name='code1', value='001110011', format='int') |
|
719 | # opObj12.addParameter(name='code1', value='001110011', format='int') | |
718 |
|
720 | |||
719 |
|
721 | |||
720 |
|
722 | |||
721 | # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj1.getId()) |
|
723 | # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj1.getId()) | |
722 | # |
|
724 | # | |
723 |
# opObj21 = procUnitConfObj2.addOperation(name='IncohInt', optype=' |
|
725 | # opObj21 = procUnitConfObj2.addOperation(name='IncohInt', optype='external') | |
724 | # opObj21.addParameter(name='n', value='2', format='int') |
|
726 | # opObj21.addParameter(name='n', value='2', format='int') | |
725 | # |
|
727 | # | |
726 |
# opObj11 = procUnitConfObj2.addOperation(name='SpectraPlot', optype=' |
|
728 | # opObj11 = procUnitConfObj2.addOperation(name='SpectraPlot', optype='external') | |
727 | # opObj11.addParameter(name='idfigure', value='4', format='int') |
|
729 | # opObj11.addParameter(name='idfigure', value='4', format='int') | |
728 | # opObj11.addParameter(name='wintitle', value='SpectraPlot OBJ 2', format='str') |
|
730 | # opObj11.addParameter(name='wintitle', value='SpectraPlot OBJ 2', format='str') | |
729 | # opObj11.addParameter(name='zmin', value='70', format='int') |
|
731 | # opObj11.addParameter(name='zmin', value='70', format='int') | |
730 | # opObj11.addParameter(name='zmax', value='90', format='int') |
|
732 | # opObj11.addParameter(name='zmax', value='90', format='int') | |
731 |
|
733 | |||
732 | print "Escribiendo el archivo XML" |
|
734 | print "Escribiendo el archivo XML" | |
733 |
|
735 | |||
734 | controllerObj.writeXml(filename) |
|
736 | controllerObj.writeXml(filename) | |
735 |
|
737 | |||
736 | print "Leyendo el archivo XML" |
|
738 | print "Leyendo el archivo XML" | |
737 | controllerObj.readXml(filename) |
|
739 | controllerObj.readXml(filename) | |
738 | #controllerObj.printattr() |
|
740 | #controllerObj.printattr() | |
739 |
|
741 | |||
740 | controllerObj.createObjects() |
|
742 | controllerObj.createObjects() | |
741 | controllerObj.connectObjects() |
|
743 | controllerObj.connectObjects() | |
742 | controllerObj.run() |
|
744 | controllerObj.run() | |
743 |
|
745 | |||
744 | No newline at end of file |
|
746 |
@@ -1,3 +1,4 | |||||
1 | import jrodata |
|
1 | from model.data.jrodata import * | |
2 | import jrodataIO |
|
2 | from model.io.jrodataIO import * | |
3 | import jroprocessing |
|
3 | from model.proc.jroprocessing import * | |
|
4 | from model.graphics.jroplot import * |
@@ -1,629 +1,641 | |||||
1 | import os |
|
1 | import os | |
2 | import numpy |
|
2 | import numpy | |
3 | import time, datetime |
|
3 | import time, datetime | |
4 | import mpldriver |
|
4 | import mpldriver | |
5 | from customftp import * |
|
5 | from customftp import * | |
6 |
|
6 | |||
7 | import Queue |
|
7 | import Queue | |
8 | import threading |
|
8 | import threading | |
9 |
|
9 | |||
|
10 | def isRealtime(utcdatatime): | |||
|
11 | utcnow = time.mktime(time.localtime()) | |||
|
12 | delta = abs(utcnow - utcdatatime) # abs | |||
|
13 | if delta >= 30.: | |||
|
14 | return False | |||
|
15 | return True | |||
|
16 | ||||
|
17 | ||||
10 | class FTP_Thread (threading.Thread): |
|
18 | class FTP_Thread (threading.Thread): | |
11 | def __init__(self): |
|
19 | def __init__(self): | |
12 | threading.Thread.__init__(self) |
|
20 | threading.Thread.__init__(self) | |
13 | self.exitFlag = 0 |
|
21 | self.exitFlag = 0 | |
14 | self.queueLock = threading.Lock() |
|
22 | self.queueLock = threading.Lock() | |
15 | self.workQueue = Queue.Queue() |
|
23 | self.workQueue = Queue.Queue() | |
16 |
|
24 | |||
17 | def run(self): |
|
25 | def run(self): | |
18 | self.send_data() |
|
26 | self.send_data() | |
19 |
|
27 | |||
20 | def fin(self): |
|
28 | def fin(self): | |
21 | self.exitFlag = 1 |
|
29 | self.exitFlag = 1 | |
22 |
|
30 | |||
23 | def put_data(self, data): |
|
31 | def put_data(self, data): | |
24 | # Fill the queue |
|
32 | # Fill the queue | |
25 | self.queueLock.acquire() |
|
33 | self.queueLock.acquire() | |
26 | self.workQueue.put(data) |
|
34 | self.workQueue.put(data) | |
27 | self.queueLock.release() |
|
35 | self.queueLock.release() | |
28 |
|
36 | |||
29 | def send_data(self): |
|
37 | def send_data(self): | |
30 | while not self.exitFlag: |
|
38 | while not self.exitFlag: | |
31 | if self.workQueue.qsize(): |
|
39 | if self.workQueue.qsize(): | |
32 |
|
40 | |||
33 | data = self.workQueue.get(True) |
|
41 | data = self.workQueue.get(True) | |
34 |
|
42 | |||
35 | try: |
|
43 | try: | |
36 | ftpObj = Ftp(host=data['server'], |
|
44 | ftpObj = Ftp(host=data['server'], | |
37 | username=data['username'], |
|
45 | username=data['username'], | |
38 | passw=data['password'], |
|
46 | passw=data['password'], | |
39 | remotefolder=data['folder']) |
|
47 | remotefolder=data['folder']) | |
40 |
|
48 | |||
41 | ftpObj.upload(data['figfilename']) |
|
49 | ftpObj.upload(data['figfilename']) | |
42 | ftpObj.close() |
|
50 | ftpObj.close() | |
43 | except: |
|
51 | except: | |
44 | print ValueError, 'Error FTP' |
|
52 | print ValueError, 'Error FTP' | |
45 | print "don't worry still running the program" |
|
53 | print "don't worry still running the program" | |
46 |
|
54 | |||
47 |
|
55 | |||
48 | class Figure: |
|
56 | class Figure: | |
49 |
|
57 | |||
50 | __driver = mpldriver |
|
58 | __driver = mpldriver | |
51 | __isConfigThread = False |
|
59 | __isConfigThread = False | |
52 | fig = None |
|
60 | fig = None | |
53 |
|
61 | |||
54 | id = None |
|
62 | id = None | |
55 | wintitle = None |
|
63 | wintitle = None | |
56 | width = None |
|
64 | width = None | |
57 | height = None |
|
65 | height = None | |
58 | nplots = None |
|
66 | nplots = None | |
59 | timerange = None |
|
67 | timerange = None | |
60 |
|
68 | |||
61 | axesObjList = [] |
|
69 | axesObjList = [] | |
62 |
|
70 | |||
63 | WIDTH = None |
|
71 | WIDTH = None | |
64 | HEIGHT = None |
|
72 | HEIGHT = None | |
65 | PREFIX = 'fig' |
|
73 | PREFIX = 'fig' | |
66 |
|
74 | |||
67 | xmin = None |
|
75 | xmin = None | |
68 | xmax = None |
|
76 | xmax = None | |
69 |
|
77 | |||
70 | def __init__(self): |
|
78 | def __init__(self): | |
71 |
|
79 | |||
72 | raise ValueError, "This method is not implemented" |
|
80 | raise ValueError, "This method is not implemented" | |
73 |
|
81 | |||
74 | def __del__(self): |
|
82 | def __del__(self): | |
75 |
|
83 | |||
76 | self.__driver.closeFigure() |
|
84 | self.__driver.closeFigure() | |
77 |
|
85 | |||
78 | def getFilename(self, name, ext='.png'): |
|
86 | def getFilename(self, name, ext='.png'): | |
79 |
|
87 | |||
80 | path = '%s%03d' %(self.PREFIX, self.id) |
|
88 | path = '%s%03d' %(self.PREFIX, self.id) | |
81 | filename = '%s_%s%s' %(self.PREFIX, name, ext) |
|
89 | filename = '%s_%s%s' %(self.PREFIX, name, ext) | |
82 | return os.path.join(path, filename) |
|
90 | return os.path.join(path, filename) | |
83 |
|
91 | |||
84 | def getAxesObjList(self): |
|
92 | def getAxesObjList(self): | |
85 |
|
93 | |||
86 | return self.axesObjList |
|
94 | return self.axesObjList | |
87 |
|
95 | |||
88 | def getSubplots(self): |
|
96 | def getSubplots(self): | |
89 |
|
97 | |||
90 | raise ValueError, "Abstract method: This method should be defined" |
|
98 | raise ValueError, "Abstract method: This method should be defined" | |
91 |
|
99 | |||
92 | def getScreenDim(self, widthplot, heightplot): |
|
100 | def getScreenDim(self, widthplot, heightplot): | |
93 |
|
101 | |||
94 | nrow, ncol = self.getSubplots() |
|
102 | nrow, ncol = self.getSubplots() | |
95 |
|
103 | |||
96 | widthscreen = widthplot*ncol |
|
104 | widthscreen = widthplot*ncol | |
97 | heightscreen = heightplot*nrow |
|
105 | heightscreen = heightplot*nrow | |
98 |
|
106 | |||
99 | return widthscreen, heightscreen |
|
107 | return widthscreen, heightscreen | |
100 |
|
108 | |||
101 | def getTimeLim(self, x, xmin=None, xmax=None, timerange=None): |
|
109 | def getTimeLim(self, x, xmin=None, xmax=None, timerange=None): | |
102 |
|
110 | |||
103 | if self.xmin != None and self.xmax != None: |
|
111 | if self.xmin != None and self.xmax != None: | |
104 | if timerange == None: |
|
112 | if timerange == None: | |
105 | timerange = self.xmax - self.xmin |
|
113 | timerange = self.xmax - self.xmin | |
106 | xmin = self.xmin + timerange |
|
114 | xmin = self.xmin + timerange | |
107 | xmax = self.xmax + timerange |
|
115 | xmax = self.xmax + timerange | |
108 |
|
116 | |||
109 | return xmin, xmax |
|
117 | return xmin, xmax | |
110 |
|
118 | |||
111 |
|
119 | |||
112 | if timerange != None and self.xmin == None and self.xmax == None: |
|
120 | if timerange != None and self.xmin == None and self.xmax == None: | |
113 | txmin = x[0] - x[0]%timerange |
|
121 | txmin = x[0] - x[0]%timerange | |
114 | thisdatetime = datetime.datetime.utcfromtimestamp(txmin) |
|
122 | thisdatetime = datetime.datetime.utcfromtimestamp(txmin) | |
115 | thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) |
|
123 | thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) | |
116 | xmin = (thisdatetime - thisdate).seconds/(60*60.) |
|
124 | xmin = (thisdatetime - thisdate).seconds/(60*60.) | |
117 | xmax = xmin + timerange/(60*60.) |
|
125 | xmax = xmin + timerange/(60*60.) | |
118 |
|
126 | |||
119 |
|
127 | |||
120 | if timerange == None: |
|
128 | if timerange == None: | |
121 | txmin = numpy.min(x) |
|
129 | txmin = numpy.min(x) | |
122 | thisdatetime = datetime.datetime.utcfromtimestamp(txmin) |
|
130 | thisdatetime = datetime.datetime.utcfromtimestamp(txmin) | |
123 | thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) |
|
131 | thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) | |
124 |
|
132 | |||
125 | mindt = thisdate + datetime.timedelta(hours=xmin) - datetime.timedelta(seconds=time.timezone) |
|
133 | mindt = thisdate + datetime.timedelta(hours=xmin) - datetime.timedelta(seconds=time.timezone) | |
126 | xmin_sec = time.mktime(mindt.timetuple()) |
|
134 | xmin_sec = time.mktime(mindt.timetuple()) | |
127 |
|
135 | |||
128 | maxdt = thisdate + datetime.timedelta(hours=xmax) - datetime.timedelta(seconds=time.timezone) |
|
136 | maxdt = thisdate + datetime.timedelta(hours=xmax) - datetime.timedelta(seconds=time.timezone) | |
129 | xmax_sec = time.mktime(maxdt.timetuple()) |
|
137 | xmax_sec = time.mktime(maxdt.timetuple()) | |
130 |
|
138 | |||
131 | return xmin_sec, xmax_sec |
|
139 | return xmin_sec, xmax_sec | |
132 |
|
140 | |||
133 |
|
141 | |||
134 |
|
142 | |||
135 |
|
143 | |||
136 |
|
144 | |||
137 | # if timerange != None: |
|
145 | # if timerange != None: | |
138 | # txmin = x[0] - x[0]%timerange |
|
146 | # txmin = x[0] - x[0]%timerange | |
139 | # else: |
|
147 | # else: | |
140 | # txmin = numpy.min(x) |
|
148 | # txmin = numpy.min(x) | |
141 | # |
|
149 | # | |
142 | # thisdatetime = datetime.datetime.utcfromtimestamp(txmin) |
|
150 | # thisdatetime = datetime.datetime.utcfromtimestamp(txmin) | |
143 | # thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) |
|
151 | # thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) | |
144 | # |
|
152 | # | |
145 | # #################################################### |
|
153 | # #################################################### | |
146 | # #If the x is out of xrange |
|
154 | # #If the x is out of xrange | |
147 | # if xmax != None: |
|
155 | # if xmax != None: | |
148 | # if xmax < (thisdatetime - thisdate).seconds/(60*60.): |
|
156 | # if xmax < (thisdatetime - thisdate).seconds/(60*60.): | |
149 | # xmin = None |
|
157 | # xmin = None | |
150 | # xmax = None |
|
158 | # xmax = None | |
151 | # |
|
159 | # | |
152 | # if xmin == None: |
|
160 | # if xmin == None: | |
153 | # td = thisdatetime - thisdate |
|
161 | # td = thisdatetime - thisdate | |
154 | # xmin = td.seconds/(60*60.) |
|
162 | # xmin = td.seconds/(60*60.) | |
155 | # |
|
163 | # | |
156 | # if xmax == None: |
|
164 | # if xmax == None: | |
157 | # xmax = xmin + self.timerange/(60*60.) |
|
165 | # xmax = xmin + self.timerange/(60*60.) | |
158 | # |
|
166 | # | |
159 | # mindt = thisdate + datetime.timedelta(hours=xmin) - datetime.timedelta(seconds=time.timezone) |
|
167 | # mindt = thisdate + datetime.timedelta(hours=xmin) - datetime.timedelta(seconds=time.timezone) | |
160 | # tmin = time.mktime(mindt.timetuple()) |
|
168 | # tmin = time.mktime(mindt.timetuple()) | |
161 | # |
|
169 | # | |
162 | # maxdt = thisdate + datetime.timedelta(hours=xmax) - datetime.timedelta(seconds=time.timezone) |
|
170 | # maxdt = thisdate + datetime.timedelta(hours=xmax) - datetime.timedelta(seconds=time.timezone) | |
163 | # tmax = time.mktime(maxdt.timetuple()) |
|
171 | # tmax = time.mktime(maxdt.timetuple()) | |
164 | # |
|
172 | # | |
165 | # #self.timerange = tmax - tmin |
|
173 | # #self.timerange = tmax - tmin | |
166 | # |
|
174 | # | |
167 | # return tmin, tmax |
|
175 | # return tmin, tmax | |
168 |
|
176 | |||
169 | def init(self, id, nplots, wintitle): |
|
177 | def init(self, id, nplots, wintitle): | |
170 |
|
178 | |||
171 | raise ValueError, "This method has been replaced with createFigure" |
|
179 | raise ValueError, "This method has been replaced with createFigure" | |
172 |
|
180 | |||
173 | def createFigure(self, id, wintitle, widthplot=None, heightplot=None, show=True): |
|
181 | def createFigure(self, id, wintitle, widthplot=None, heightplot=None, show=True): | |
174 |
|
182 | |||
175 | """ |
|
183 | """ | |
176 | Crea la figura de acuerdo al driver y parametros seleccionados seleccionados. |
|
184 | Crea la figura de acuerdo al driver y parametros seleccionados seleccionados. | |
177 | Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH |
|
185 | Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH | |
178 | y self.HEIGHT y el numero de subplots (nrow, ncol) |
|
186 | y self.HEIGHT y el numero de subplots (nrow, ncol) | |
179 |
|
187 | |||
180 | Input: |
|
188 | Input: | |
181 | id : Los parametros necesarios son |
|
189 | id : Los parametros necesarios son | |
182 | wintitle : |
|
190 | wintitle : | |
183 |
|
191 | |||
184 | """ |
|
192 | """ | |
185 |
|
193 | |||
186 | if widthplot == None: |
|
194 | if widthplot == None: | |
187 | widthplot = self.WIDTH |
|
195 | widthplot = self.WIDTH | |
188 |
|
196 | |||
189 | if heightplot == None: |
|
197 | if heightplot == None: | |
190 | heightplot = self.HEIGHT |
|
198 | heightplot = self.HEIGHT | |
191 |
|
199 | |||
192 | self.id = id |
|
200 | self.id = id | |
193 |
|
201 | |||
194 | self.wintitle = wintitle |
|
202 | self.wintitle = wintitle | |
195 |
|
203 | |||
196 | self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot) |
|
204 | self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot) | |
197 |
|
205 | |||
198 | self.fig = self.__driver.createFigure(id=self.id, |
|
206 | self.fig = self.__driver.createFigure(id=self.id, | |
199 | wintitle=self.wintitle, |
|
207 | wintitle=self.wintitle, | |
200 | width=self.widthscreen, |
|
208 | width=self.widthscreen, | |
201 | height=self.heightscreen, |
|
209 | height=self.heightscreen, | |
202 | show=show) |
|
210 | show=show) | |
203 |
|
211 | |||
204 | self.axesObjList = [] |
|
212 | self.axesObjList = [] | |
205 |
|
213 | |||
206 |
|
214 | |||
207 | def setDriver(self, driver=mpldriver): |
|
215 | def setDriver(self, driver=mpldriver): | |
208 |
|
216 | |||
209 | self.__driver = driver |
|
217 | self.__driver = driver | |
210 |
|
218 | |||
211 | def setTitle(self, title): |
|
219 | def setTitle(self, title): | |
212 |
|
220 | |||
213 | self.__driver.setTitle(self.fig, title) |
|
221 | self.__driver.setTitle(self.fig, title) | |
214 |
|
222 | |||
215 | def setWinTitle(self, title): |
|
223 | def setWinTitle(self, title): | |
216 |
|
224 | |||
217 | self.__driver.setWinTitle(self.fig, title=title) |
|
225 | self.__driver.setWinTitle(self.fig, title=title) | |
218 |
|
226 | |||
219 | def setTextFromAxes(self, text): |
|
227 | def setTextFromAxes(self, text): | |
220 |
|
228 | |||
221 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo setText de la clase Axes" |
|
229 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo setText de la clase Axes" | |
222 |
|
230 | |||
223 | def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
231 | def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan): | |
224 |
|
232 | |||
225 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo addAxes" |
|
233 | raise ValueError, "Este metodo ha sido reemplazaado con el metodo addAxes" | |
226 |
|
234 | |||
227 | def addAxes(self, *args): |
|
235 | def addAxes(self, *args): | |
228 | """ |
|
236 | """ | |
229 |
|
237 | |||
230 | Input: |
|
238 | Input: | |
231 | *args : Los parametros necesarios son |
|
239 | *args : Los parametros necesarios son | |
232 | nrow, ncol, xpos, ypos, colspan, rowspan |
|
240 | nrow, ncol, xpos, ypos, colspan, rowspan | |
233 | """ |
|
241 | """ | |
234 |
|
242 | |||
235 | axesObj = Axes(self.fig, *args) |
|
243 | axesObj = Axes(self.fig, *args) | |
236 | self.axesObjList.append(axesObj) |
|
244 | self.axesObjList.append(axesObj) | |
237 |
|
245 | |||
238 | def saveFigure(self, figpath, figfile, *args): |
|
246 | def saveFigure(self, figpath, figfile, *args): | |
239 |
|
247 | |||
240 | filename = os.path.join(figpath, figfile) |
|
248 | filename = os.path.join(figpath, figfile) | |
241 |
|
249 | |||
242 | fullpath = os.path.split(filename)[0] |
|
250 | fullpath = os.path.split(filename)[0] | |
243 |
|
251 | |||
244 | if not os.path.exists(fullpath): |
|
252 | if not os.path.exists(fullpath): | |
245 | subpath = os.path.split(fullpath)[0] |
|
253 | subpath = os.path.split(fullpath)[0] | |
246 |
|
254 | |||
247 | if not os.path.exists(subpath): |
|
255 | if not os.path.exists(subpath): | |
248 | os.mkdir(subpath) |
|
256 | os.mkdir(subpath) | |
249 |
|
257 | |||
250 | os.mkdir(fullpath) |
|
258 | os.mkdir(fullpath) | |
251 |
|
259 | |||
252 | self.__driver.saveFigure(self.fig, filename, *args) |
|
260 | self.__driver.saveFigure(self.fig, filename, *args) | |
253 |
|
261 | |||
254 | def sendByFTP(self, figfilename, server, folder, username, password): |
|
262 | def sendByFTP(self, figfilename, server, folder, username, password): | |
255 | ftpObj = Ftp(host=server, username=username, passw=password, remotefolder=folder) |
|
263 | ftpObj = Ftp(host=server, username=username, passw=password, remotefolder=folder) | |
256 | ftpObj.upload(figfilename) |
|
264 | ftpObj.upload(figfilename) | |
257 | ftpObj.close() |
|
265 | ftpObj.close() | |
258 |
|
266 | |||
259 | def sendByFTP_Thread(self, figfilename, server, folder, username, password): |
|
267 | def sendByFTP_Thread(self, figfilename, server, folder, username, password): | |
260 | data = {'figfilename':figfilename,'server':server,'folder':folder,'username':username,'password':password} |
|
268 | data = {'figfilename':figfilename,'server':server,'folder':folder,'username':username,'password':password} | |
261 |
|
269 | |||
262 | if not(self.__isConfigThread): |
|
270 | if not(self.__isConfigThread): | |
263 |
|
271 | |||
264 | self.thread = FTP_Thread() |
|
272 | self.thread = FTP_Thread() | |
265 | self.thread.start() |
|
273 | self.thread.start() | |
266 | self.__isConfigThread = True |
|
274 | self.__isConfigThread = True | |
267 |
|
275 | |||
268 | self.thread.put_data(data) |
|
276 | self.thread.put_data(data) | |
269 | #print 'thread.isAlive()', self.thread.isAlive() |
|
277 | #print 'thread.isAlive()', self.thread.isAlive() | |
270 |
|
278 | |||
271 |
|
279 | |||
272 | def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS): |
|
280 | def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS): | |
273 | YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year |
|
281 | YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year | |
274 | DOY_STR = '%3.3d'%thisDatetime.timetuple().tm_yday |
|
282 | DOY_STR = '%3.3d'%thisDatetime.timetuple().tm_yday | |
275 | FTP_WEI = '%2.2d'%FTP_WEI |
|
283 | FTP_WEI = '%2.2d'%FTP_WEI | |
276 | EXP_CODE = '%3.3d'%EXP_CODE |
|
284 | EXP_CODE = '%3.3d'%EXP_CODE | |
277 | SUB_EXP_CODE = '%2.2d'%SUB_EXP_CODE |
|
285 | SUB_EXP_CODE = '%2.2d'%SUB_EXP_CODE | |
278 | PLOT_CODE = '%2.2d'%PLOT_CODE |
|
286 | PLOT_CODE = '%2.2d'%PLOT_CODE | |
279 | PLOT_POS = '%2.2d'%PLOT_POS |
|
287 | PLOT_POS = '%2.2d'%PLOT_POS | |
280 | name = YEAR_STR + DOY_STR + FTP_WEI + EXP_CODE + SUB_EXP_CODE + PLOT_CODE + PLOT_POS |
|
288 | name = YEAR_STR + DOY_STR + FTP_WEI + EXP_CODE + SUB_EXP_CODE + PLOT_CODE + PLOT_POS | |
281 | return name |
|
289 | return name | |
282 |
|
290 | |||
283 | def draw(self): |
|
291 | def draw(self): | |
284 |
|
292 | |||
285 | self.__driver.draw(self.fig) |
|
293 | self.__driver.draw(self.fig) | |
286 |
|
294 | |||
287 | def run(self): |
|
295 | def run(self): | |
288 |
|
296 | |||
289 | raise ValueError, "This method is not implemented" |
|
297 | raise ValueError, "This method is not implemented" | |
290 |
|
298 | |||
|
299 | def close(self): | |||
|
300 | ||||
|
301 | self.__driver.show(True) | |||
|
302 | ||||
291 | axesList = property(getAxesObjList) |
|
303 | axesList = property(getAxesObjList) | |
292 |
|
304 | |||
293 |
|
305 | |||
294 | class Axes: |
|
306 | class Axes: | |
295 |
|
307 | |||
296 | __driver = mpldriver |
|
308 | __driver = mpldriver | |
297 | fig = None |
|
309 | fig = None | |
298 | ax = None |
|
310 | ax = None | |
299 | plot = None |
|
311 | plot = None | |
300 | __missing = 1E30 |
|
312 | __missing = 1E30 | |
301 | __firsttime = None |
|
313 | __firsttime = None | |
302 |
|
314 | |||
303 | __showprofile = False |
|
315 | __showprofile = False | |
304 |
|
316 | |||
305 | xmin = None |
|
317 | xmin = None | |
306 | xmax = None |
|
318 | xmax = None | |
307 | ymin = None |
|
319 | ymin = None | |
308 | ymax = None |
|
320 | ymax = None | |
309 | zmin = None |
|
321 | zmin = None | |
310 | zmax = None |
|
322 | zmax = None | |
311 |
|
323 | |||
312 | x_buffer = None |
|
324 | x_buffer = None | |
313 | z_buffer = None |
|
325 | z_buffer = None | |
314 |
|
326 | |||
315 | decimationx = None |
|
327 | decimationx = None | |
316 | decimationy = None |
|
328 | decimationy = None | |
317 |
|
329 | |||
318 | __MAXNUMX = 300 |
|
330 | __MAXNUMX = 300 | |
319 | __MAXNUMY = 150 |
|
331 | __MAXNUMY = 150 | |
320 |
|
332 | |||
321 | def __init__(self, *args): |
|
333 | def __init__(self, *args): | |
322 |
|
334 | |||
323 | """ |
|
335 | """ | |
324 |
|
336 | |||
325 | Input: |
|
337 | Input: | |
326 | *args : Los parametros necesarios son |
|
338 | *args : Los parametros necesarios son | |
327 | fig, nrow, ncol, xpos, ypos, colspan, rowspan |
|
339 | fig, nrow, ncol, xpos, ypos, colspan, rowspan | |
328 | """ |
|
340 | """ | |
329 |
|
341 | |||
330 | ax = self.__driver.createAxes(*args) |
|
342 | ax = self.__driver.createAxes(*args) | |
331 | self.fig = args[0] |
|
343 | self.fig = args[0] | |
332 | self.ax = ax |
|
344 | self.ax = ax | |
333 | self.plot = None |
|
345 | self.plot = None | |
334 |
|
346 | |||
335 | self.__firsttime = True |
|
347 | self.__firsttime = True | |
336 | self.idlineList = [] |
|
348 | self.idlineList = [] | |
337 |
|
349 | |||
338 | self.x_buffer = numpy.array([]) |
|
350 | self.x_buffer = numpy.array([]) | |
339 | self.z_buffer = numpy.array([]) |
|
351 | self.z_buffer = numpy.array([]) | |
340 |
|
352 | |||
341 | def setText(self, text): |
|
353 | def setText(self, text): | |
342 |
|
354 | |||
343 | self.__driver.setAxesText(self.ax, text) |
|
355 | self.__driver.setAxesText(self.ax, text) | |
344 |
|
356 | |||
345 | def setXAxisAsTime(self): |
|
357 | def setXAxisAsTime(self): | |
346 | pass |
|
358 | pass | |
347 |
|
359 | |||
348 | def pline(self, x, y, |
|
360 | def pline(self, x, y, | |
349 | xmin=None, xmax=None, |
|
361 | xmin=None, xmax=None, | |
350 | ymin=None, ymax=None, |
|
362 | ymin=None, ymax=None, | |
351 | xlabel='', ylabel='', |
|
363 | xlabel='', ylabel='', | |
352 | title='', |
|
364 | title='', | |
353 | **kwargs): |
|
365 | **kwargs): | |
354 |
|
366 | |||
355 | """ |
|
367 | """ | |
356 |
|
368 | |||
357 | Input: |
|
369 | Input: | |
358 | x : |
|
370 | x : | |
359 | y : |
|
371 | y : | |
360 | xmin : |
|
372 | xmin : | |
361 | xmax : |
|
373 | xmax : | |
362 | ymin : |
|
374 | ymin : | |
363 | ymax : |
|
375 | ymax : | |
364 | xlabel : |
|
376 | xlabel : | |
365 | ylabel : |
|
377 | ylabel : | |
366 | title : |
|
378 | title : | |
367 | **kwargs : Los parametros aceptados son |
|
379 | **kwargs : Los parametros aceptados son | |
368 |
|
380 | |||
369 | ticksize |
|
381 | ticksize | |
370 | ytick_visible |
|
382 | ytick_visible | |
371 | """ |
|
383 | """ | |
372 |
|
384 | |||
373 | if self.__firsttime: |
|
385 | if self.__firsttime: | |
374 |
|
386 | |||
375 | if xmin == None: xmin = numpy.nanmin(x) |
|
387 | if xmin == None: xmin = numpy.nanmin(x) | |
376 | if xmax == None: xmax = numpy.nanmax(x) |
|
388 | if xmax == None: xmax = numpy.nanmax(x) | |
377 | if ymin == None: ymin = numpy.nanmin(y) |
|
389 | if ymin == None: ymin = numpy.nanmin(y) | |
378 | if ymax == None: ymax = numpy.nanmax(y) |
|
390 | if ymax == None: ymax = numpy.nanmax(y) | |
379 |
|
391 | |||
380 | self.plot = self.__driver.createPline(self.ax, x, y, |
|
392 | self.plot = self.__driver.createPline(self.ax, x, y, | |
381 | xmin, xmax, |
|
393 | xmin, xmax, | |
382 | ymin, ymax, |
|
394 | ymin, ymax, | |
383 | xlabel=xlabel, |
|
395 | xlabel=xlabel, | |
384 | ylabel=ylabel, |
|
396 | ylabel=ylabel, | |
385 | title=title, |
|
397 | title=title, | |
386 | **kwargs) |
|
398 | **kwargs) | |
387 |
|
399 | |||
388 | self.idlineList.append(0) |
|
400 | self.idlineList.append(0) | |
389 | self.__firsttime = False |
|
401 | self.__firsttime = False | |
390 | return |
|
402 | return | |
391 |
|
403 | |||
392 | self.__driver.pline(self.plot, x, y, xlabel=xlabel, |
|
404 | self.__driver.pline(self.plot, x, y, xlabel=xlabel, | |
393 | ylabel=ylabel, |
|
405 | ylabel=ylabel, | |
394 | title=title) |
|
406 | title=title) | |
395 |
|
407 | |||
396 | def addpline(self, x, y, idline, **kwargs): |
|
408 | def addpline(self, x, y, idline, **kwargs): | |
397 | lines = self.ax.lines |
|
409 | lines = self.ax.lines | |
398 |
|
410 | |||
399 | if idline in self.idlineList: |
|
411 | if idline in self.idlineList: | |
400 | self.__driver.set_linedata(self.ax, x, y, idline) |
|
412 | self.__driver.set_linedata(self.ax, x, y, idline) | |
401 |
|
413 | |||
402 | if idline not in(self.idlineList): |
|
414 | if idline not in(self.idlineList): | |
403 | self.__driver.addpline(self.ax, x, y, **kwargs) |
|
415 | self.__driver.addpline(self.ax, x, y, **kwargs) | |
404 | self.idlineList.append(idline) |
|
416 | self.idlineList.append(idline) | |
405 |
|
417 | |||
406 | return |
|
418 | return | |
407 |
|
419 | |||
408 | def pmultiline(self, x, y, |
|
420 | def pmultiline(self, x, y, | |
409 | xmin=None, xmax=None, |
|
421 | xmin=None, xmax=None, | |
410 | ymin=None, ymax=None, |
|
422 | ymin=None, ymax=None, | |
411 | xlabel='', ylabel='', |
|
423 | xlabel='', ylabel='', | |
412 | title='', |
|
424 | title='', | |
413 | **kwargs): |
|
425 | **kwargs): | |
414 |
|
426 | |||
415 | if self.__firsttime: |
|
427 | if self.__firsttime: | |
416 |
|
428 | |||
417 | if xmin == None: xmin = numpy.nanmin(x) |
|
429 | if xmin == None: xmin = numpy.nanmin(x) | |
418 | if xmax == None: xmax = numpy.nanmax(x) |
|
430 | if xmax == None: xmax = numpy.nanmax(x) | |
419 | if ymin == None: ymin = numpy.nanmin(y) |
|
431 | if ymin == None: ymin = numpy.nanmin(y) | |
420 | if ymax == None: ymax = numpy.nanmax(y) |
|
432 | if ymax == None: ymax = numpy.nanmax(y) | |
421 |
|
433 | |||
422 | self.plot = self.__driver.createPmultiline(self.ax, x, y, |
|
434 | self.plot = self.__driver.createPmultiline(self.ax, x, y, | |
423 | xmin, xmax, |
|
435 | xmin, xmax, | |
424 | ymin, ymax, |
|
436 | ymin, ymax, | |
425 | xlabel=xlabel, |
|
437 | xlabel=xlabel, | |
426 | ylabel=ylabel, |
|
438 | ylabel=ylabel, | |
427 | title=title, |
|
439 | title=title, | |
428 | **kwargs) |
|
440 | **kwargs) | |
429 | self.__firsttime = False |
|
441 | self.__firsttime = False | |
430 | return |
|
442 | return | |
431 |
|
443 | |||
432 | self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel, |
|
444 | self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel, | |
433 | ylabel=ylabel, |
|
445 | ylabel=ylabel, | |
434 | title=title) |
|
446 | title=title) | |
435 |
|
447 | |||
436 | def pmultilineyaxis(self, x, y, |
|
448 | def pmultilineyaxis(self, x, y, | |
437 | xmin=None, xmax=None, |
|
449 | xmin=None, xmax=None, | |
438 | ymin=None, ymax=None, |
|
450 | ymin=None, ymax=None, | |
439 | xlabel='', ylabel='', |
|
451 | xlabel='', ylabel='', | |
440 | title='', |
|
452 | title='', | |
441 | **kwargs): |
|
453 | **kwargs): | |
442 |
|
454 | |||
443 | if self.__firsttime: |
|
455 | if self.__firsttime: | |
444 |
|
456 | |||
445 | if xmin == None: xmin = numpy.nanmin(x) |
|
457 | if xmin == None: xmin = numpy.nanmin(x) | |
446 | if xmax == None: xmax = numpy.nanmax(x) |
|
458 | if xmax == None: xmax = numpy.nanmax(x) | |
447 | if ymin == None: ymin = numpy.nanmin(y) |
|
459 | if ymin == None: ymin = numpy.nanmin(y) | |
448 | if ymax == None: ymax = numpy.nanmax(y) |
|
460 | if ymax == None: ymax = numpy.nanmax(y) | |
449 |
|
461 | |||
450 | self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y, |
|
462 | self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y, | |
451 | xmin, xmax, |
|
463 | xmin, xmax, | |
452 | ymin, ymax, |
|
464 | ymin, ymax, | |
453 | xlabel=xlabel, |
|
465 | xlabel=xlabel, | |
454 | ylabel=ylabel, |
|
466 | ylabel=ylabel, | |
455 | title=title, |
|
467 | title=title, | |
456 | **kwargs) |
|
468 | **kwargs) | |
457 | if self.xmin == None: self.xmin = xmin |
|
469 | if self.xmin == None: self.xmin = xmin | |
458 | if self.xmax == None: self.xmax = xmax |
|
470 | if self.xmax == None: self.xmax = xmax | |
459 | if self.ymin == None: self.ymin = ymin |
|
471 | if self.ymin == None: self.ymin = ymin | |
460 | if self.ymax == None: self.ymax = ymax |
|
472 | if self.ymax == None: self.ymax = ymax | |
461 |
|
473 | |||
462 | self.__firsttime = False |
|
474 | self.__firsttime = False | |
463 | return |
|
475 | return | |
464 |
|
476 | |||
465 | self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel, |
|
477 | self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel, | |
466 | ylabel=ylabel, |
|
478 | ylabel=ylabel, | |
467 | title=title) |
|
479 | title=title) | |
468 |
|
480 | |||
469 | def pcolor(self, x, y, z, |
|
481 | def pcolor(self, x, y, z, | |
470 | xmin=None, xmax=None, |
|
482 | xmin=None, xmax=None, | |
471 | ymin=None, ymax=None, |
|
483 | ymin=None, ymax=None, | |
472 | zmin=None, zmax=None, |
|
484 | zmin=None, zmax=None, | |
473 | xlabel='', ylabel='', |
|
485 | xlabel='', ylabel='', | |
474 | title='', rti = False, colormap='jet', |
|
486 | title='', rti = False, colormap='jet', | |
475 | **kwargs): |
|
487 | **kwargs): | |
476 |
|
488 | |||
477 | """ |
|
489 | """ | |
478 | Input: |
|
490 | Input: | |
479 | x : |
|
491 | x : | |
480 | y : |
|
492 | y : | |
481 | x : |
|
493 | x : | |
482 | xmin : |
|
494 | xmin : | |
483 | xmax : |
|
495 | xmax : | |
484 | ymin : |
|
496 | ymin : | |
485 | ymax : |
|
497 | ymax : | |
486 | zmin : |
|
498 | zmin : | |
487 | zmax : |
|
499 | zmax : | |
488 | xlabel : |
|
500 | xlabel : | |
489 | ylabel : |
|
501 | ylabel : | |
490 | title : |
|
502 | title : | |
491 | **kwargs : Los parametros aceptados son |
|
503 | **kwargs : Los parametros aceptados son | |
492 | ticksize=9, |
|
504 | ticksize=9, | |
493 | cblabel='' |
|
505 | cblabel='' | |
494 | rti = True or False |
|
506 | rti = True or False | |
495 | """ |
|
507 | """ | |
496 |
|
508 | |||
497 | if self.__firsttime: |
|
509 | if self.__firsttime: | |
498 |
|
510 | |||
499 | if xmin == None: xmin = numpy.nanmin(x) |
|
511 | if xmin == None: xmin = numpy.nanmin(x) | |
500 | if xmax == None: xmax = numpy.nanmax(x) |
|
512 | if xmax == None: xmax = numpy.nanmax(x) | |
501 | if ymin == None: ymin = numpy.nanmin(y) |
|
513 | if ymin == None: ymin = numpy.nanmin(y) | |
502 | if ymax == None: ymax = numpy.nanmax(y) |
|
514 | if ymax == None: ymax = numpy.nanmax(y) | |
503 | if zmin == None: zmin = numpy.nanmin(z) |
|
515 | if zmin == None: zmin = numpy.nanmin(z) | |
504 | if zmax == None: zmax = numpy.nanmax(z) |
|
516 | if zmax == None: zmax = numpy.nanmax(z) | |
505 |
|
517 | |||
506 |
|
518 | |||
507 | self.plot = self.__driver.createPcolor(self.ax, x, y, z, |
|
519 | self.plot = self.__driver.createPcolor(self.ax, x, y, z, | |
508 | xmin, xmax, |
|
520 | xmin, xmax, | |
509 | ymin, ymax, |
|
521 | ymin, ymax, | |
510 | zmin, zmax, |
|
522 | zmin, zmax, | |
511 | xlabel=xlabel, |
|
523 | xlabel=xlabel, | |
512 | ylabel=ylabel, |
|
524 | ylabel=ylabel, | |
513 | title=title, |
|
525 | title=title, | |
514 | colormap=colormap, |
|
526 | colormap=colormap, | |
515 | **kwargs) |
|
527 | **kwargs) | |
516 |
|
528 | |||
517 | if self.xmin == None: self.xmin = xmin |
|
529 | if self.xmin == None: self.xmin = xmin | |
518 | if self.xmax == None: self.xmax = xmax |
|
530 | if self.xmax == None: self.xmax = xmax | |
519 | if self.ymin == None: self.ymin = ymin |
|
531 | if self.ymin == None: self.ymin = ymin | |
520 | if self.ymax == None: self.ymax = ymax |
|
532 | if self.ymax == None: self.ymax = ymax | |
521 | if self.zmin == None: self.zmin = zmin |
|
533 | if self.zmin == None: self.zmin = zmin | |
522 | if self.zmax == None: self.zmax = zmax |
|
534 | if self.zmax == None: self.zmax = zmax | |
523 |
|
535 | |||
524 | self.__firsttime = False |
|
536 | self.__firsttime = False | |
525 | return |
|
537 | return | |
526 |
|
538 | |||
527 | if rti: |
|
539 | if rti: | |
528 | self.__driver.addpcolor(self.ax, x, y, z, self.zmin, self.zmax, |
|
540 | self.__driver.addpcolor(self.ax, x, y, z, self.zmin, self.zmax, | |
529 | xlabel=xlabel, |
|
541 | xlabel=xlabel, | |
530 | ylabel=ylabel, |
|
542 | ylabel=ylabel, | |
531 | title=title, |
|
543 | title=title, | |
532 | colormap=colormap) |
|
544 | colormap=colormap) | |
533 | return |
|
545 | return | |
534 |
|
546 | |||
535 | self.__driver.pcolor(self.plot, z, |
|
547 | self.__driver.pcolor(self.plot, z, | |
536 | xlabel=xlabel, |
|
548 | xlabel=xlabel, | |
537 | ylabel=ylabel, |
|
549 | ylabel=ylabel, | |
538 | title=title) |
|
550 | title=title) | |
539 |
|
551 | |||
540 | def pcolorbuffer(self, x, y, z, |
|
552 | def pcolorbuffer(self, x, y, z, | |
541 | xmin=None, xmax=None, |
|
553 | xmin=None, xmax=None, | |
542 | ymin=None, ymax=None, |
|
554 | ymin=None, ymax=None, | |
543 | zmin=None, zmax=None, |
|
555 | zmin=None, zmax=None, | |
544 | xlabel='', ylabel='', |
|
556 | xlabel='', ylabel='', | |
545 | title='', rti = True, colormap='jet', |
|
557 | title='', rti = True, colormap='jet', | |
546 | maxNumX = None, maxNumY = None, |
|
558 | maxNumX = None, maxNumY = None, | |
547 | **kwargs): |
|
559 | **kwargs): | |
548 |
|
560 | |||
549 | if maxNumX == None: |
|
561 | if maxNumX == None: | |
550 | maxNumX = self.__MAXNUMX |
|
562 | maxNumX = self.__MAXNUMX | |
551 |
|
563 | |||
552 | if maxNumY == None: |
|
564 | if maxNumY == None: | |
553 | maxNumY = self.__MAXNUMY |
|
565 | maxNumY = self.__MAXNUMY | |
554 |
|
566 | |||
555 | if self.__firsttime: |
|
567 | if self.__firsttime: | |
556 | self.z_buffer = z |
|
568 | self.z_buffer = z | |
557 | self.x_buffer = numpy.hstack((self.x_buffer, x)) |
|
569 | self.x_buffer = numpy.hstack((self.x_buffer, x)) | |
558 |
|
570 | |||
559 | if xmin == None: xmin = numpy.nanmin(x) |
|
571 | if xmin == None: xmin = numpy.nanmin(x) | |
560 | if xmax == None: xmax = numpy.nanmax(x) |
|
572 | if xmax == None: xmax = numpy.nanmax(x) | |
561 | if ymin == None: ymin = numpy.nanmin(y) |
|
573 | if ymin == None: ymin = numpy.nanmin(y) | |
562 | if ymax == None: ymax = numpy.nanmax(y) |
|
574 | if ymax == None: ymax = numpy.nanmax(y) | |
563 | if zmin == None: zmin = numpy.nanmin(z) |
|
575 | if zmin == None: zmin = numpy.nanmin(z) | |
564 | if zmax == None: zmax = numpy.nanmax(z) |
|
576 | if zmax == None: zmax = numpy.nanmax(z) | |
565 |
|
577 | |||
566 |
|
578 | |||
567 | self.plot = self.__driver.createPcolor(self.ax, self.x_buffer, y, z, |
|
579 | self.plot = self.__driver.createPcolor(self.ax, self.x_buffer, y, z, | |
568 | xmin, xmax, |
|
580 | xmin, xmax, | |
569 | ymin, ymax, |
|
581 | ymin, ymax, | |
570 | zmin, zmax, |
|
582 | zmin, zmax, | |
571 | xlabel=xlabel, |
|
583 | xlabel=xlabel, | |
572 | ylabel=ylabel, |
|
584 | ylabel=ylabel, | |
573 | title=title, |
|
585 | title=title, | |
574 | colormap=colormap, |
|
586 | colormap=colormap, | |
575 | **kwargs) |
|
587 | **kwargs) | |
576 |
|
588 | |||
577 | if self.xmin == None: self.xmin = xmin |
|
589 | if self.xmin == None: self.xmin = xmin | |
578 | if self.xmax == None: self.xmax = xmax |
|
590 | if self.xmax == None: self.xmax = xmax | |
579 | if self.ymin == None: self.ymin = ymin |
|
591 | if self.ymin == None: self.ymin = ymin | |
580 | if self.ymax == None: self.ymax = ymax |
|
592 | if self.ymax == None: self.ymax = ymax | |
581 | if self.zmin == None: self.zmin = zmin |
|
593 | if self.zmin == None: self.zmin = zmin | |
582 | if self.zmax == None: self.zmax = zmax |
|
594 | if self.zmax == None: self.zmax = zmax | |
583 |
|
595 | |||
584 | self.__firsttime = False |
|
596 | self.__firsttime = False | |
585 | return |
|
597 | return | |
586 |
|
598 | |||
587 | self.x_buffer = numpy.hstack((self.x_buffer, x[-1])) |
|
599 | self.x_buffer = numpy.hstack((self.x_buffer, x[-1])) | |
588 | self.z_buffer = numpy.hstack((self.z_buffer, z)) |
|
600 | self.z_buffer = numpy.hstack((self.z_buffer, z)) | |
589 |
|
601 | |||
590 | if self.decimationx == None: |
|
602 | if self.decimationx == None: | |
591 | deltax = float(self.xmax - self.xmin)/maxNumX |
|
603 | deltax = float(self.xmax - self.xmin)/maxNumX | |
592 | deltay = float(self.ymax - self.ymin)/maxNumY |
|
604 | deltay = float(self.ymax - self.ymin)/maxNumY | |
593 |
|
605 | |||
594 | resolutionx = self.x_buffer[2]-self.x_buffer[0] |
|
606 | resolutionx = self.x_buffer[2]-self.x_buffer[0] | |
595 | resolutiony = y[1]-y[0] |
|
607 | resolutiony = y[1]-y[0] | |
596 |
|
608 | |||
597 | self.decimationx = numpy.ceil(deltax / resolutionx) |
|
609 | self.decimationx = numpy.ceil(deltax / resolutionx) | |
598 | self.decimationy = numpy.ceil(deltay / resolutiony) |
|
610 | self.decimationy = numpy.ceil(deltay / resolutiony) | |
599 |
|
611 | |||
600 | z_buffer = self.z_buffer.reshape(-1,len(y)) |
|
612 | z_buffer = self.z_buffer.reshape(-1,len(y)) | |
601 |
|
613 | |||
602 | x_buffer = self.x_buffer[::self.decimationx] |
|
614 | x_buffer = self.x_buffer[::self.decimationx] | |
603 | y_buffer = y[::self.decimationy] |
|
615 | y_buffer = y[::self.decimationy] | |
604 | z_buffer = z_buffer[::self.decimationx, ::self.decimationy] |
|
616 | z_buffer = z_buffer[::self.decimationx, ::self.decimationy] | |
605 | #=================================================== |
|
617 | #=================================================== | |
606 |
|
618 | |||
607 | x_buffer, y_buffer, z_buffer = self.__fillGaps(x_buffer, y_buffer, z_buffer) |
|
619 | x_buffer, y_buffer, z_buffer = self.__fillGaps(x_buffer, y_buffer, z_buffer) | |
608 |
|
620 | |||
609 | self.__driver.addpcolorbuffer(self.ax, x_buffer, y_buffer, z_buffer, self.zmin, self.zmax, |
|
621 | self.__driver.addpcolorbuffer(self.ax, x_buffer, y_buffer, z_buffer, self.zmin, self.zmax, | |
610 | xlabel=xlabel, |
|
622 | xlabel=xlabel, | |
611 | ylabel=ylabel, |
|
623 | ylabel=ylabel, | |
612 | title=title, |
|
624 | title=title, | |
613 | colormap=colormap) |
|
625 | colormap=colormap) | |
614 | def __fillGaps(self, x_buffer, y_buffer, z_buffer): |
|
626 | def __fillGaps(self, x_buffer, y_buffer, z_buffer): | |
615 |
|
627 | |||
616 | deltas = x_buffer[1:] - x_buffer[0:-1] |
|
628 | deltas = x_buffer[1:] - x_buffer[0:-1] | |
617 | x_median = numpy.median(deltas) |
|
629 | x_median = numpy.median(deltas) | |
618 |
|
630 | |||
619 | index = numpy.where(deltas >= 2*x_median) |
|
631 | index = numpy.where(deltas >= 2*x_median) | |
620 |
|
632 | |||
621 | if len(index[0]) != 0: |
|
633 | if len(index[0]) != 0: | |
622 | z_buffer[index[0],::] = self.__missing |
|
634 | z_buffer[index[0],::] = self.__missing | |
623 | z_buffer = numpy.ma.masked_inside(z_buffer,0.99*self.__missing,1.01*self.__missing) |
|
635 | z_buffer = numpy.ma.masked_inside(z_buffer,0.99*self.__missing,1.01*self.__missing) | |
624 |
|
636 | |||
625 | return x_buffer, y_buffer, z_buffer |
|
637 | return x_buffer, y_buffer, z_buffer | |
626 |
|
638 | |||
627 |
|
639 | |||
628 |
|
640 | |||
629 | No newline at end of file |
|
641 |
@@ -1,383 +1,383 | |||||
1 | import numpy |
|
1 | import numpy | |
2 | import datetime |
|
2 | import datetime | |
3 | import sys |
|
3 | import sys | |
4 | import matplotlib |
|
4 | import matplotlib | |
5 |
|
5 | |||
6 | if 'linux' in sys.platform: |
|
6 | if 'linux' in sys.platform: | |
7 | matplotlib.use("TKAgg") |
|
7 | matplotlib.use("TKAgg") | |
8 |
|
8 | |||
9 | if 'darwin' in sys.platform: |
|
9 | if 'darwin' in sys.platform: | |
10 |
matplotlib.use(" |
|
10 | matplotlib.use("TKAgg") | |
11 |
|
11 | |||
12 | import matplotlib.pyplot |
|
12 | import matplotlib.pyplot | |
13 |
|
13 | |||
14 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
14 | from mpl_toolkits.axes_grid1 import make_axes_locatable | |
15 | from matplotlib.ticker import * |
|
15 | from matplotlib.ticker import * | |
16 |
|
16 | |||
17 | ########################################### |
|
17 | ########################################### | |
18 | #Actualizacion de las funciones del driver |
|
18 | #Actualizacion de las funciones del driver | |
19 | ########################################### |
|
19 | ########################################### | |
20 |
|
20 | |||
21 | def createFigure(id, wintitle, width, height, facecolor="w", show=True): |
|
21 | def createFigure(id, wintitle, width, height, facecolor="w", show=True): | |
22 |
|
22 | |||
23 | matplotlib.pyplot.ioff() |
|
23 | matplotlib.pyplot.ioff() | |
24 | fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor) |
|
24 | fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor) | |
25 | fig.canvas.manager.set_window_title(wintitle) |
|
25 | fig.canvas.manager.set_window_title(wintitle) | |
26 | fig.canvas.manager.resize(width, height) |
|
26 | fig.canvas.manager.resize(width, height) | |
27 | matplotlib.pyplot.ion() |
|
27 | matplotlib.pyplot.ion() | |
28 | if show: |
|
28 | if show: | |
29 | matplotlib.pyplot.show() |
|
29 | matplotlib.pyplot.show() | |
30 |
|
30 | |||
31 | return fig |
|
31 | return fig | |
32 |
|
32 | |||
33 | def closeFigure(show=True): |
|
33 | def closeFigure(show=True): | |
34 |
|
34 | |||
35 | matplotlib.pyplot.ioff() |
|
35 | matplotlib.pyplot.ioff() | |
36 | if show: |
|
36 | if show: | |
37 | matplotlib.pyplot.show() |
|
37 | matplotlib.pyplot.show() | |
38 |
|
38 | |||
39 | return |
|
39 | return | |
40 |
|
40 | |||
41 | def saveFigure(fig, filename): |
|
41 | def saveFigure(fig, filename): | |
42 |
|
42 | |||
43 | matplotlib.pyplot.ioff() |
|
43 | matplotlib.pyplot.ioff() | |
44 | fig.savefig(filename) |
|
44 | fig.savefig(filename) | |
45 | matplotlib.pyplot.ion() |
|
45 | matplotlib.pyplot.ion() | |
46 |
|
46 | |||
47 | def setWinTitle(fig, title): |
|
47 | def setWinTitle(fig, title): | |
48 |
|
48 | |||
49 | fig.canvas.manager.set_window_title(title) |
|
49 | fig.canvas.manager.set_window_title(title) | |
50 |
|
50 | |||
51 | def setTitle(fig, title): |
|
51 | def setTitle(fig, title): | |
52 |
|
52 | |||
53 | fig.suptitle(title) |
|
53 | fig.suptitle(title) | |
54 |
|
54 | |||
55 | def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan): |
|
55 | def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan): | |
56 |
|
56 | |||
57 | matplotlib.pyplot.ioff() |
|
57 | matplotlib.pyplot.ioff() | |
58 | matplotlib.pyplot.figure(fig.number) |
|
58 | matplotlib.pyplot.figure(fig.number) | |
59 | axes = matplotlib.pyplot.subplot2grid((nrow, ncol), |
|
59 | axes = matplotlib.pyplot.subplot2grid((nrow, ncol), | |
60 | (xpos, ypos), |
|
60 | (xpos, ypos), | |
61 | colspan=colspan, |
|
61 | colspan=colspan, | |
62 | rowspan=rowspan) |
|
62 | rowspan=rowspan) | |
63 |
|
63 | |||
64 | matplotlib.pyplot.ion() |
|
64 | matplotlib.pyplot.ion() | |
65 | return axes |
|
65 | return axes | |
66 |
|
66 | |||
67 | def setAxesText(ax, text): |
|
67 | def setAxesText(ax, text): | |
68 |
|
68 | |||
69 | ax.annotate(text, |
|
69 | ax.annotate(text, | |
70 | xy = (.1, .99), |
|
70 | xy = (.1, .99), | |
71 | xycoords = 'figure fraction', |
|
71 | xycoords = 'figure fraction', | |
72 | horizontalalignment = 'left', |
|
72 | horizontalalignment = 'left', | |
73 | verticalalignment = 'top', |
|
73 | verticalalignment = 'top', | |
74 | fontsize = 10) |
|
74 | fontsize = 10) | |
75 |
|
75 | |||
76 | def printLabels(ax, xlabel, ylabel, title): |
|
76 | def printLabels(ax, xlabel, ylabel, title): | |
77 |
|
77 | |||
78 | ax.set_xlabel(xlabel, size=11) |
|
78 | ax.set_xlabel(xlabel, size=11) | |
79 | ax.set_ylabel(ylabel, size=11) |
|
79 | ax.set_ylabel(ylabel, size=11) | |
80 | ax.set_title(title, size=12) |
|
80 | ax.set_title(title, size=12) | |
81 |
|
81 | |||
82 | def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', |
|
82 | def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', | |
83 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
83 | ticksize=9, xtick_visible=True, ytick_visible=True, | |
84 | nxticks=4, nyticks=10, |
|
84 | nxticks=4, nyticks=10, | |
85 | grid=None,color='blue'): |
|
85 | grid=None,color='blue'): | |
86 |
|
86 | |||
87 | """ |
|
87 | """ | |
88 |
|
88 | |||
89 | Input: |
|
89 | Input: | |
90 | grid : None, 'both', 'x', 'y' |
|
90 | grid : None, 'both', 'x', 'y' | |
91 | """ |
|
91 | """ | |
92 |
|
92 | |||
93 | matplotlib.pyplot.ioff() |
|
93 | matplotlib.pyplot.ioff() | |
94 |
|
94 | |||
95 | ax.set_xlim([xmin,xmax]) |
|
95 | ax.set_xlim([xmin,xmax]) | |
96 | ax.set_ylim([ymin,ymax]) |
|
96 | ax.set_ylim([ymin,ymax]) | |
97 |
|
97 | |||
98 | printLabels(ax, xlabel, ylabel, title) |
|
98 | printLabels(ax, xlabel, ylabel, title) | |
99 |
|
99 | |||
100 | ###################################################### |
|
100 | ###################################################### | |
101 | if (xmax-xmin)<=1: |
|
101 | if (xmax-xmin)<=1: | |
102 | xtickspos = numpy.linspace(xmin,xmax,nxticks) |
|
102 | xtickspos = numpy.linspace(xmin,xmax,nxticks) | |
103 | xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos]) |
|
103 | xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos]) | |
104 | ax.set_xticks(xtickspos) |
|
104 | ax.set_xticks(xtickspos) | |
105 | else: |
|
105 | else: | |
106 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) |
|
106 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) | |
107 | # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin) |
|
107 | # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin) | |
108 | ax.set_xticks(xtickspos) |
|
108 | ax.set_xticks(xtickspos) | |
109 |
|
109 | |||
110 | for tick in ax.get_xticklabels(): |
|
110 | for tick in ax.get_xticklabels(): | |
111 | tick.set_visible(xtick_visible) |
|
111 | tick.set_visible(xtick_visible) | |
112 |
|
112 | |||
113 | for tick in ax.xaxis.get_major_ticks(): |
|
113 | for tick in ax.xaxis.get_major_ticks(): | |
114 | tick.label.set_fontsize(ticksize) |
|
114 | tick.label.set_fontsize(ticksize) | |
115 |
|
115 | |||
116 | ###################################################### |
|
116 | ###################################################### | |
117 | for tick in ax.get_yticklabels(): |
|
117 | for tick in ax.get_yticklabels(): | |
118 | tick.set_visible(ytick_visible) |
|
118 | tick.set_visible(ytick_visible) | |
119 |
|
119 | |||
120 | for tick in ax.yaxis.get_major_ticks(): |
|
120 | for tick in ax.yaxis.get_major_ticks(): | |
121 | tick.label.set_fontsize(ticksize) |
|
121 | tick.label.set_fontsize(ticksize) | |
122 |
|
122 | |||
123 | ax.plot(x, y, color=color) |
|
123 | ax.plot(x, y, color=color) | |
124 | iplot = ax.lines[-1] |
|
124 | iplot = ax.lines[-1] | |
125 |
|
125 | |||
126 | ###################################################### |
|
126 | ###################################################### | |
127 | if '0.' in matplotlib.__version__[0:2]: |
|
127 | if '0.' in matplotlib.__version__[0:2]: | |
128 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
128 | print "The matplotlib version has to be updated to 1.1 or newer" | |
129 | return iplot |
|
129 | return iplot | |
130 |
|
130 | |||
131 | if '1.0.' in matplotlib.__version__[0:4]: |
|
131 | if '1.0.' in matplotlib.__version__[0:4]: | |
132 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
132 | print "The matplotlib version has to be updated to 1.1 or newer" | |
133 | return iplot |
|
133 | return iplot | |
134 |
|
134 | |||
135 | if grid != None: |
|
135 | if grid != None: | |
136 | ax.grid(b=True, which='major', axis=grid) |
|
136 | ax.grid(b=True, which='major', axis=grid) | |
137 |
|
137 | |||
138 | matplotlib.pyplot.tight_layout() |
|
138 | matplotlib.pyplot.tight_layout() | |
139 |
|
139 | |||
140 | matplotlib.pyplot.ion() |
|
140 | matplotlib.pyplot.ion() | |
141 |
|
141 | |||
142 | return iplot |
|
142 | return iplot | |
143 |
|
143 | |||
144 | def set_linedata(ax, x, y, idline): |
|
144 | def set_linedata(ax, x, y, idline): | |
145 |
|
145 | |||
146 | ax.lines[idline].set_data(x,y) |
|
146 | ax.lines[idline].set_data(x,y) | |
147 |
|
147 | |||
148 | def pline(iplot, x, y, xlabel='', ylabel='', title=''): |
|
148 | def pline(iplot, x, y, xlabel='', ylabel='', title=''): | |
149 |
|
149 | |||
150 | ax = iplot.get_axes() |
|
150 | ax = iplot.get_axes() | |
151 |
|
151 | |||
152 | printLabels(ax, xlabel, ylabel, title) |
|
152 | printLabels(ax, xlabel, ylabel, title) | |
153 |
|
153 | |||
154 | set_linedata(ax, x, y, idline=0) |
|
154 | set_linedata(ax, x, y, idline=0) | |
155 |
|
155 | |||
156 | def addpline(ax, x, y, color, linestyle, lw): |
|
156 | def addpline(ax, x, y, color, linestyle, lw): | |
157 |
|
157 | |||
158 | ax.plot(x,y,color=color,linestyle=linestyle,lw=lw) |
|
158 | ax.plot(x,y,color=color,linestyle=linestyle,lw=lw) | |
159 |
|
159 | |||
160 |
|
160 | |||
161 | def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, |
|
161 | def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, | |
162 | xlabel='', ylabel='', title='', ticksize = 9, |
|
162 | xlabel='', ylabel='', title='', ticksize = 9, | |
163 | colormap='jet',cblabel='', cbsize="5%", |
|
163 | colormap='jet',cblabel='', cbsize="5%", | |
164 | XAxisAsTime=False): |
|
164 | XAxisAsTime=False): | |
165 |
|
165 | |||
166 | matplotlib.pyplot.ioff() |
|
166 | matplotlib.pyplot.ioff() | |
167 |
|
167 | |||
168 | divider = make_axes_locatable(ax) |
|
168 | divider = make_axes_locatable(ax) | |
169 | ax_cb = divider.new_horizontal(size=cbsize, pad=0.05) |
|
169 | ax_cb = divider.new_horizontal(size=cbsize, pad=0.05) | |
170 | fig = ax.get_figure() |
|
170 | fig = ax.get_figure() | |
171 | fig.add_axes(ax_cb) |
|
171 | fig.add_axes(ax_cb) | |
172 |
|
172 | |||
173 | ax.set_xlim([xmin,xmax]) |
|
173 | ax.set_xlim([xmin,xmax]) | |
174 | ax.set_ylim([ymin,ymax]) |
|
174 | ax.set_ylim([ymin,ymax]) | |
175 |
|
175 | |||
176 | printLabels(ax, xlabel, ylabel, title) |
|
176 | printLabels(ax, xlabel, ylabel, title) | |
177 |
|
177 | |||
178 | imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) |
|
178 | imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) | |
179 | cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) |
|
179 | cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb) | |
180 | cb.set_label(cblabel) |
|
180 | cb.set_label(cblabel) | |
181 |
|
181 | |||
182 | # for tl in ax_cb.get_yticklabels(): |
|
182 | # for tl in ax_cb.get_yticklabels(): | |
183 | # tl.set_visible(True) |
|
183 | # tl.set_visible(True) | |
184 |
|
184 | |||
185 | for tick in ax.yaxis.get_major_ticks(): |
|
185 | for tick in ax.yaxis.get_major_ticks(): | |
186 | tick.label.set_fontsize(ticksize) |
|
186 | tick.label.set_fontsize(ticksize) | |
187 |
|
187 | |||
188 | for tick in ax.xaxis.get_major_ticks(): |
|
188 | for tick in ax.xaxis.get_major_ticks(): | |
189 | tick.label.set_fontsize(ticksize) |
|
189 | tick.label.set_fontsize(ticksize) | |
190 |
|
190 | |||
191 | for tick in cb.ax.get_yticklabels(): |
|
191 | for tick in cb.ax.get_yticklabels(): | |
192 | tick.set_fontsize(ticksize) |
|
192 | tick.set_fontsize(ticksize) | |
193 |
|
193 | |||
194 | ax_cb.yaxis.tick_right() |
|
194 | ax_cb.yaxis.tick_right() | |
195 |
|
195 | |||
196 | if '0.' in matplotlib.__version__[0:2]: |
|
196 | if '0.' in matplotlib.__version__[0:2]: | |
197 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
197 | print "The matplotlib version has to be updated to 1.1 or newer" | |
198 | return imesh |
|
198 | return imesh | |
199 |
|
199 | |||
200 | if '1.0.' in matplotlib.__version__[0:4]: |
|
200 | if '1.0.' in matplotlib.__version__[0:4]: | |
201 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
201 | print "The matplotlib version has to be updated to 1.1 or newer" | |
202 | return imesh |
|
202 | return imesh | |
203 |
|
203 | |||
204 | matplotlib.pyplot.tight_layout() |
|
204 | matplotlib.pyplot.tight_layout() | |
205 |
|
205 | |||
206 | if XAxisAsTime: |
|
206 | if XAxisAsTime: | |
207 |
|
207 | |||
208 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) |
|
208 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) | |
209 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
209 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |
210 | ax.xaxis.set_major_locator(LinearLocator(7)) |
|
210 | ax.xaxis.set_major_locator(LinearLocator(7)) | |
211 |
|
211 | |||
212 | matplotlib.pyplot.ion() |
|
212 | matplotlib.pyplot.ion() | |
213 | return imesh |
|
213 | return imesh | |
214 |
|
214 | |||
215 | def pcolor(imesh, z, xlabel='', ylabel='', title=''): |
|
215 | def pcolor(imesh, z, xlabel='', ylabel='', title=''): | |
216 |
|
216 | |||
217 | z = z.T |
|
217 | z = z.T | |
218 |
|
218 | |||
219 | ax = imesh.get_axes() |
|
219 | ax = imesh.get_axes() | |
220 |
|
220 | |||
221 | printLabels(ax, xlabel, ylabel, title) |
|
221 | printLabels(ax, xlabel, ylabel, title) | |
222 |
|
222 | |||
223 | imesh.set_array(z.ravel()) |
|
223 | imesh.set_array(z.ravel()) | |
224 |
|
224 | |||
225 | def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): |
|
225 | def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): | |
226 |
|
226 | |||
227 | printLabels(ax, xlabel, ylabel, title) |
|
227 | printLabels(ax, xlabel, ylabel, title) | |
228 |
|
228 | |||
229 | ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) |
|
229 | ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) | |
230 |
|
230 | |||
231 | def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): |
|
231 | def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'): | |
232 |
|
232 | |||
233 | printLabels(ax, xlabel, ylabel, title) |
|
233 | printLabels(ax, xlabel, ylabel, title) | |
234 |
|
234 | |||
235 | ax.collections.remove(ax.collections[0]) |
|
235 | ax.collections.remove(ax.collections[0]) | |
236 |
|
236 | |||
237 | ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) |
|
237 | ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap)) | |
238 |
|
238 | |||
239 | def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, |
|
239 | def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, | |
240 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
240 | ticksize=9, xtick_visible=True, ytick_visible=True, | |
241 | nxticks=4, nyticks=10, |
|
241 | nxticks=4, nyticks=10, | |
242 | grid=None): |
|
242 | grid=None): | |
243 |
|
243 | |||
244 | """ |
|
244 | """ | |
245 |
|
245 | |||
246 | Input: |
|
246 | Input: | |
247 | grid : None, 'both', 'x', 'y' |
|
247 | grid : None, 'both', 'x', 'y' | |
248 | """ |
|
248 | """ | |
249 |
|
249 | |||
250 | matplotlib.pyplot.ioff() |
|
250 | matplotlib.pyplot.ioff() | |
251 |
|
251 | |||
252 | lines = ax.plot(x.T, y) |
|
252 | lines = ax.plot(x.T, y) | |
253 | leg = ax.legend(lines, legendlabels, loc='upper right') |
|
253 | leg = ax.legend(lines, legendlabels, loc='upper right') | |
254 | leg.get_frame().set_alpha(0.5) |
|
254 | leg.get_frame().set_alpha(0.5) | |
255 | ax.set_xlim([xmin,xmax]) |
|
255 | ax.set_xlim([xmin,xmax]) | |
256 | ax.set_ylim([ymin,ymax]) |
|
256 | ax.set_ylim([ymin,ymax]) | |
257 | printLabels(ax, xlabel, ylabel, title) |
|
257 | printLabels(ax, xlabel, ylabel, title) | |
258 |
|
258 | |||
259 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) |
|
259 | xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) | |
260 | ax.set_xticks(xtickspos) |
|
260 | ax.set_xticks(xtickspos) | |
261 |
|
261 | |||
262 | for tick in ax.get_xticklabels(): |
|
262 | for tick in ax.get_xticklabels(): | |
263 | tick.set_visible(xtick_visible) |
|
263 | tick.set_visible(xtick_visible) | |
264 |
|
264 | |||
265 | for tick in ax.xaxis.get_major_ticks(): |
|
265 | for tick in ax.xaxis.get_major_ticks(): | |
266 | tick.label.set_fontsize(ticksize) |
|
266 | tick.label.set_fontsize(ticksize) | |
267 |
|
267 | |||
268 | for tick in ax.get_yticklabels(): |
|
268 | for tick in ax.get_yticklabels(): | |
269 | tick.set_visible(ytick_visible) |
|
269 | tick.set_visible(ytick_visible) | |
270 |
|
270 | |||
271 | for tick in ax.yaxis.get_major_ticks(): |
|
271 | for tick in ax.yaxis.get_major_ticks(): | |
272 | tick.label.set_fontsize(ticksize) |
|
272 | tick.label.set_fontsize(ticksize) | |
273 |
|
273 | |||
274 | iplot = ax.lines[-1] |
|
274 | iplot = ax.lines[-1] | |
275 |
|
275 | |||
276 | if '0.' in matplotlib.__version__[0:2]: |
|
276 | if '0.' in matplotlib.__version__[0:2]: | |
277 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
277 | print "The matplotlib version has to be updated to 1.1 or newer" | |
278 | return iplot |
|
278 | return iplot | |
279 |
|
279 | |||
280 | if '1.0.' in matplotlib.__version__[0:4]: |
|
280 | if '1.0.' in matplotlib.__version__[0:4]: | |
281 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
281 | print "The matplotlib version has to be updated to 1.1 or newer" | |
282 | return iplot |
|
282 | return iplot | |
283 |
|
283 | |||
284 | if grid != None: |
|
284 | if grid != None: | |
285 | ax.grid(b=True, which='major', axis=grid) |
|
285 | ax.grid(b=True, which='major', axis=grid) | |
286 |
|
286 | |||
287 | matplotlib.pyplot.tight_layout() |
|
287 | matplotlib.pyplot.tight_layout() | |
288 |
|
288 | |||
289 | matplotlib.pyplot.ion() |
|
289 | matplotlib.pyplot.ion() | |
290 |
|
290 | |||
291 | return iplot |
|
291 | return iplot | |
292 |
|
292 | |||
293 |
|
293 | |||
294 | def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''): |
|
294 | def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''): | |
295 |
|
295 | |||
296 | ax = iplot.get_axes() |
|
296 | ax = iplot.get_axes() | |
297 |
|
297 | |||
298 | printLabels(ax, xlabel, ylabel, title) |
|
298 | printLabels(ax, xlabel, ylabel, title) | |
299 |
|
299 | |||
300 | for i in range(len(ax.lines)): |
|
300 | for i in range(len(ax.lines)): | |
301 | line = ax.lines[i] |
|
301 | line = ax.lines[i] | |
302 | line.set_data(x[i,:],y) |
|
302 | line.set_data(x[i,:],y) | |
303 |
|
303 | |||
304 | def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, |
|
304 | def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None, | |
305 | ticksize=9, xtick_visible=True, ytick_visible=True, |
|
305 | ticksize=9, xtick_visible=True, ytick_visible=True, | |
306 | nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None", |
|
306 | nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None", | |
307 | grid=None, XAxisAsTime=False): |
|
307 | grid=None, XAxisAsTime=False): | |
308 |
|
308 | |||
309 | """ |
|
309 | """ | |
310 |
|
310 | |||
311 | Input: |
|
311 | Input: | |
312 | grid : None, 'both', 'x', 'y' |
|
312 | grid : None, 'both', 'x', 'y' | |
313 | """ |
|
313 | """ | |
314 |
|
314 | |||
315 | matplotlib.pyplot.ioff() |
|
315 | matplotlib.pyplot.ioff() | |
316 |
|
316 | |||
317 | # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle) |
|
317 | # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle) | |
318 | lines = ax.plot(x, y.T, linestyle='None', marker='.', markersize=markersize) |
|
318 | lines = ax.plot(x, y.T, linestyle='None', marker='.', markersize=markersize) | |
319 | leg = ax.legend(lines, legendlabels, loc='upper left', bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \ |
|
319 | leg = ax.legend(lines, legendlabels, loc='upper left', bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \ | |
320 | handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.) |
|
320 | handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.) | |
321 |
|
321 | |||
322 | for label in leg.get_texts(): label.set_fontsize(9) |
|
322 | for label in leg.get_texts(): label.set_fontsize(9) | |
323 |
|
323 | |||
324 | ax.set_xlim([xmin,xmax]) |
|
324 | ax.set_xlim([xmin,xmax]) | |
325 | ax.set_ylim([ymin,ymax]) |
|
325 | ax.set_ylim([ymin,ymax]) | |
326 | printLabels(ax, xlabel, ylabel, title) |
|
326 | printLabels(ax, xlabel, ylabel, title) | |
327 |
|
327 | |||
328 | # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) |
|
328 | # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin) | |
329 | # ax.set_xticks(xtickspos) |
|
329 | # ax.set_xticks(xtickspos) | |
330 |
|
330 | |||
331 | for tick in ax.get_xticklabels(): |
|
331 | for tick in ax.get_xticklabels(): | |
332 | tick.set_visible(xtick_visible) |
|
332 | tick.set_visible(xtick_visible) | |
333 |
|
333 | |||
334 | for tick in ax.xaxis.get_major_ticks(): |
|
334 | for tick in ax.xaxis.get_major_ticks(): | |
335 | tick.label.set_fontsize(ticksize) |
|
335 | tick.label.set_fontsize(ticksize) | |
336 |
|
336 | |||
337 | for tick in ax.get_yticklabels(): |
|
337 | for tick in ax.get_yticklabels(): | |
338 | tick.set_visible(ytick_visible) |
|
338 | tick.set_visible(ytick_visible) | |
339 |
|
339 | |||
340 | for tick in ax.yaxis.get_major_ticks(): |
|
340 | for tick in ax.yaxis.get_major_ticks(): | |
341 | tick.label.set_fontsize(ticksize) |
|
341 | tick.label.set_fontsize(ticksize) | |
342 |
|
342 | |||
343 | iplot = ax.lines[-1] |
|
343 | iplot = ax.lines[-1] | |
344 |
|
344 | |||
345 | if '0.' in matplotlib.__version__[0:2]: |
|
345 | if '0.' in matplotlib.__version__[0:2]: | |
346 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
346 | print "The matplotlib version has to be updated to 1.1 or newer" | |
347 | return iplot |
|
347 | return iplot | |
348 |
|
348 | |||
349 | if '1.0.' in matplotlib.__version__[0:4]: |
|
349 | if '1.0.' in matplotlib.__version__[0:4]: | |
350 | print "The matplotlib version has to be updated to 1.1 or newer" |
|
350 | print "The matplotlib version has to be updated to 1.1 or newer" | |
351 | return iplot |
|
351 | return iplot | |
352 |
|
352 | |||
353 | if grid != None: |
|
353 | if grid != None: | |
354 | ax.grid(b=True, which='major', axis=grid) |
|
354 | ax.grid(b=True, which='major', axis=grid) | |
355 |
|
355 | |||
356 | matplotlib.pyplot.tight_layout() |
|
356 | matplotlib.pyplot.tight_layout() | |
357 |
|
357 | |||
358 | if XAxisAsTime: |
|
358 | if XAxisAsTime: | |
359 |
|
359 | |||
360 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) |
|
360 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S")) | |
361 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
361 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |
362 | ax.xaxis.set_major_locator(LinearLocator(7)) |
|
362 | ax.xaxis.set_major_locator(LinearLocator(7)) | |
363 |
|
363 | |||
364 | matplotlib.pyplot.ion() |
|
364 | matplotlib.pyplot.ion() | |
365 |
|
365 | |||
366 | return iplot |
|
366 | return iplot | |
367 |
|
367 | |||
368 | def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''): |
|
368 | def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''): | |
369 |
|
369 | |||
370 | ax = iplot.get_axes() |
|
370 | ax = iplot.get_axes() | |
371 |
|
371 | |||
372 | printLabels(ax, xlabel, ylabel, title) |
|
372 | printLabels(ax, xlabel, ylabel, title) | |
373 |
|
373 | |||
374 | for i in range(len(ax.lines)): |
|
374 | for i in range(len(ax.lines)): | |
375 | line = ax.lines[i] |
|
375 | line = ax.lines[i] | |
376 | line.set_data(x,y[i,:]) |
|
376 | line.set_data(x,y[i,:]) | |
377 |
|
377 | |||
378 | def draw(fig): |
|
378 | def draw(fig): | |
379 |
|
379 | |||
380 | if type(fig) == 'int': |
|
380 | if type(fig) == 'int': | |
381 | raise ValueError, "This parameter should be of tpye matplotlib figure" |
|
381 | raise ValueError, "This parameter should be of tpye matplotlib figure" | |
382 |
|
382 | |||
383 | fig.canvas.draw() |
|
383 | fig.canvas.draw() |
General Comments 0
You need to be logged in to leave comments.
Login now