##// END OF EJS Templates
En esta version se tiene:...
Daniel Valdez -
r439:da26a7ca9b43
parent child
Show More
@@ -1,626 +1,721
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: JROData.py 173 2012-11-20 15:06:21Z murco $
4 $Id: JROData.py 173 2012-11-20 15:06:21Z murco $
5 '''
5 '''
6
6
7 import os, sys
7 import os, sys
8 import copy
8 import copy
9 import numpy
9 import numpy
10 import datetime
10 import datetime
11
11
12 from jroheaderIO import SystemHeader, RadarControllerHeader
12 from jroheaderIO import SystemHeader, RadarControllerHeader
13
13
14 def hildebrand_sekhon(data, navg):
14 def hildebrand_sekhon(data, navg):
15 """
15 """
16 This method is for the objective determination of de noise level in Doppler spectra. This
16 This method is for the objective determination of de noise level in Doppler spectra. This
17 implementation technique is based on the fact that the standard deviation of the spectral
17 implementation technique is based on the fact that the standard deviation of the spectral
18 densities is equal to the mean spectral density for white Gaussian noise
18 densities is equal to the mean spectral density for white Gaussian noise
19
19
20 Inputs:
20 Inputs:
21 Data : heights
21 Data : heights
22 navg : numbers of averages
22 navg : numbers of averages
23
23
24 Return:
24 Return:
25 -1 : any error
25 -1 : any error
26 anoise : noise's level
26 anoise : noise's level
27 """
27 """
28
28
29 dataflat = data.copy().reshape(-1)
29 dataflat = data.copy().reshape(-1)
30 dataflat.sort()
30 dataflat.sort()
31 npts = dataflat.size #numbers of points of the data
31 npts = dataflat.size #numbers of points of the data
32 npts_noise = 0.2*npts
32 npts_noise = 0.2*npts
33
33
34 if npts < 32:
34 if npts < 32:
35 print "error in noise - requires at least 32 points"
35 print "error in noise - requires at least 32 points"
36 return -1.0
36 return -1.0
37
37
38 dataflat2 = numpy.power(dataflat,2)
38 dataflat2 = numpy.power(dataflat,2)
39
39
40 cs = numpy.cumsum(dataflat)
40 cs = numpy.cumsum(dataflat)
41 cs2 = numpy.cumsum(dataflat2)
41 cs2 = numpy.cumsum(dataflat2)
42
42
43 # data sorted in ascending order
43 # data sorted in ascending order
44 nmin = int((npts + 7.)/8)
44 nmin = int((npts + 7.)/8)
45
45
46 for i in range(nmin, npts):
46 for i in range(nmin, npts):
47 s = cs[i]
47 s = cs[i]
48 s2 = cs2[i]
48 s2 = cs2[i]
49 p = s / float(i);
49 p = s / float(i);
50 p2 = p**2;
50 p2 = p**2;
51 q = s2 / float(i) - p2;
51 q = s2 / float(i) - p2;
52 leftc = p2;
52 leftc = p2;
53 rightc = q * float(navg);
53 rightc = q * float(navg);
54 R2 = leftc/rightc
54 R2 = leftc/rightc
55
55
56 # Signal detect: R2 < 1 (R2 = leftc/rightc)
56 # Signal detect: R2 < 1 (R2 = leftc/rightc)
57 if R2 < 1:
57 if R2 < 1:
58 npts_noise = i
58 npts_noise = i
59 break
59 break
60
60
61
61
62 anoise = numpy.average(dataflat[0:npts_noise])
62 anoise = numpy.average(dataflat[0:npts_noise])
63
63
64 return anoise;
64 return anoise;
65
65
66 def sorting_bruce(data, navg):
66 def sorting_bruce(data, navg):
67
67
68 data = data.copy()
68 data = data.copy()
69
69
70 sortdata = numpy.sort(data)
70 sortdata = numpy.sort(data)
71 lenOfData = len(data)
71 lenOfData = len(data)
72 nums_min = lenOfData/10
72 nums_min = lenOfData/10
73
73
74 if (lenOfData/10) > 0:
74 if (lenOfData/10) > 0:
75 nums_min = lenOfData/10
75 nums_min = lenOfData/10
76 else:
76 else:
77 nums_min = 0
77 nums_min = 0
78
78
79 rtest = 1.0 + 1.0/navg
79 rtest = 1.0 + 1.0/navg
80
80
81 sum = 0.
81 sum = 0.
82
82
83 sumq = 0.
83 sumq = 0.
84
84
85 j = 0
85 j = 0
86
86
87 cont = 1
87 cont = 1
88
88
89 while((cont==1)and(j<lenOfData)):
89 while((cont==1)and(j<lenOfData)):
90
90
91 sum += sortdata[j]
91 sum += sortdata[j]
92
92
93 sumq += sortdata[j]**2
93 sumq += sortdata[j]**2
94
94
95 j += 1
95 j += 1
96
96
97 if j > nums_min:
97 if j > nums_min:
98 if ((sumq*j) <= (rtest*sum**2)):
98 if ((sumq*j) <= (rtest*sum**2)):
99 lnoise = sum / j
99 lnoise = sum / j
100 else:
100 else:
101 j = j - 1
101 j = j - 1
102 sum = sum - sordata[j]
102 sum = sum - sordata[j]
103 sumq = sumq - sordata[j]**2
103 sumq = sumq - sordata[j]**2
104 cont = 0
104 cont = 0
105
105
106 if j == nums_min:
106 if j == nums_min:
107 lnoise = sum /j
107 lnoise = sum /j
108
108
109 return lnoise
109 return lnoise
110
110
111 class JROData:
111 class JROData:
112
112
113 # m_BasicHeader = BasicHeader()
113 # m_BasicHeader = BasicHeader()
114 # m_ProcessingHeader = ProcessingHeader()
114 # m_ProcessingHeader = ProcessingHeader()
115
115
116 systemHeaderObj = SystemHeader()
116 systemHeaderObj = SystemHeader()
117
117
118 radarControllerHeaderObj = RadarControllerHeader()
118 radarControllerHeaderObj = RadarControllerHeader()
119
119
120 # data = None
120 # data = None
121
121
122 type = None
122 type = None
123
123
124 dtype = None
124 dtype = None
125
125
126 # nChannels = None
126 # nChannels = None
127
127
128 # nHeights = None
128 # nHeights = None
129
129
130 nProfiles = None
130 nProfiles = None
131
131
132 heightList = None
132 heightList = None
133
133
134 channelList = None
134 channelList = None
135
135
136 flagNoData = True
136 flagNoData = True
137
137
138 flagTimeBlock = False
138 flagTimeBlock = False
139
139
140 useLocalTime = False
140 useLocalTime = False
141
141
142 utctime = None
142 utctime = None
143
143
144 timeZone = None
144 timeZone = None
145
145
146 dstFlag = None
146 dstFlag = None
147
147
148 errorCount = None
148 errorCount = None
149
149
150 blocksize = None
150 blocksize = None
151
151
152 nCode = None
152 nCode = None
153
153
154 nBaud = None
154 nBaud = None
155
155
156 code = None
156 code = None
157
157
158 flagDecodeData = False #asumo q la data no esta decodificada
158 flagDecodeData = False #asumo q la data no esta decodificada
159
159
160 flagDeflipData = False #asumo q la data no esta sin flip
160 flagDeflipData = False #asumo q la data no esta sin flip
161
161
162 flagShiftFFT = False
162 flagShiftFFT = False
163
163
164 ippSeconds = None
164 ippSeconds = None
165
165
166 timeInterval = None
166 timeInterval = None
167
167
168 nCohInt = None
168 nCohInt = None
169
169
170 noise = None
170 noise = None
171
171
172 windowOfFilter = 1
172 windowOfFilter = 1
173
173
174 #Speed of ligth
174 #Speed of ligth
175 C = 3e8
175 C = 3e8
176
176
177 frequency = 49.92e6
177 frequency = 49.92e6
178
178
179 realtime = False
179 realtime = False
180
180
181 def __init__(self):
181 def __init__(self):
182
182
183 raise ValueError, "This class has not been implemented"
183 raise ValueError, "This class has not been implemented"
184
184
185 def copy(self, inputObj=None):
185 def copy(self, inputObj=None):
186
186
187 if inputObj == None:
187 if inputObj == None:
188 return copy.deepcopy(self)
188 return copy.deepcopy(self)
189
189
190 for key in inputObj.__dict__.keys():
190 for key in inputObj.__dict__.keys():
191 self.__dict__[key] = inputObj.__dict__[key]
191 self.__dict__[key] = inputObj.__dict__[key]
192
192
193 def deepcopy(self):
193 def deepcopy(self):
194
194
195 return copy.deepcopy(self)
195 return copy.deepcopy(self)
196
196
197 def isEmpty(self):
197 def isEmpty(self):
198
198
199 return self.flagNoData
199 return self.flagNoData
200
200
201 def getNoise(self):
201 def getNoise(self):
202
202
203 raise ValueError, "Not implemented"
203 raise ValueError, "Not implemented"
204
204
205 def getNChannels(self):
205 def getNChannels(self):
206
206
207 return len(self.channelList)
207 return len(self.channelList)
208
208
209 def getChannelIndexList(self):
209 def getChannelIndexList(self):
210
210
211 return range(self.nChannels)
211 return range(self.nChannels)
212
212
213 def getNHeights(self):
213 def getNHeights(self):
214
214
215 return len(self.heightList)
215 return len(self.heightList)
216
216
217 def getHeiRange(self, extrapoints=0):
217 def getHeiRange(self, extrapoints=0):
218
218
219 heis = self.heightList
219 heis = self.heightList
220 # deltah = self.heightList[1] - self.heightList[0]
220 # deltah = self.heightList[1] - self.heightList[0]
221 #
221 #
222 # heis.append(self.heightList[-1])
222 # heis.append(self.heightList[-1])
223
223
224 return heis
224 return heis
225
225
226 def getltctime(self):
226 def getltctime(self):
227
227
228 if self.useLocalTime:
228 if self.useLocalTime:
229 return self.utctime - self.timeZone*60
229 return self.utctime - self.timeZone*60
230
230
231 return self.utctime
231 return self.utctime
232
232
233 def getDatatime(self):
233 def getDatatime(self):
234
234
235 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
235 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
236 return datatime
236 return datatime
237
237
238 def getTimeRange(self):
238 def getTimeRange(self):
239
239
240 datatime = []
240 datatime = []
241
241
242 datatime.append(self.ltctime)
242 datatime.append(self.ltctime)
243 datatime.append(self.ltctime + self.timeInterval)
243 datatime.append(self.ltctime + self.timeInterval)
244
244
245 datatime = numpy.array(datatime)
245 datatime = numpy.array(datatime)
246
246
247 return datatime
247 return datatime
248
248
249 def getFmax(self):
249 def getFmax(self):
250
250
251 PRF = 1./(self.ippSeconds * self.nCohInt)
251 PRF = 1./(self.ippSeconds * self.nCohInt)
252
252
253 fmax = PRF/2.
253 fmax = PRF/2.
254
254
255 return fmax
255 return fmax
256
256
257 def getVmax(self):
257 def getVmax(self):
258
258
259 _lambda = self.C/self.frequency
259 _lambda = self.C/self.frequency
260
260
261 vmax = self.getFmax() * _lambda
261 vmax = self.getFmax() * _lambda
262
262
263 return vmax
263 return vmax
264
264
265 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
265 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
266 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
266 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
267 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
267 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
268 noise = property(getNoise, "I'm the 'nHeights' property.")
268 noise = property(getNoise, "I'm the 'nHeights' property.")
269 datatime = property(getDatatime, "I'm the 'datatime' property")
269 datatime = property(getDatatime, "I'm the 'datatime' property")
270 ltctime = property(getltctime, "I'm the 'ltctime' property")
270 ltctime = property(getltctime, "I'm the 'ltctime' property")
271
271
272 class Voltage(JROData):
272 class Voltage(JROData):
273
273
274 #data es un numpy array de 2 dmensiones (canales, alturas)
274 #data es un numpy array de 2 dmensiones (canales, alturas)
275 data = None
275 data = None
276
276
277 def __init__(self):
277 def __init__(self):
278 '''
278 '''
279 Constructor
279 Constructor
280 '''
280 '''
281
281
282 self.radarControllerHeaderObj = RadarControllerHeader()
282 self.radarControllerHeaderObj = RadarControllerHeader()
283
283
284 self.systemHeaderObj = SystemHeader()
284 self.systemHeaderObj = SystemHeader()
285
285
286 self.type = "Voltage"
286 self.type = "Voltage"
287
287
288 self.data = None
288 self.data = None
289
289
290 self.dtype = None
290 self.dtype = None
291
291
292 # self.nChannels = 0
292 # self.nChannels = 0
293
293
294 # self.nHeights = 0
294 # self.nHeights = 0
295
295
296 self.nProfiles = None
296 self.nProfiles = None
297
297
298 self.heightList = None
298 self.heightList = None
299
299
300 self.channelList = None
300 self.channelList = None
301
301
302 # self.channelIndexList = None
302 # self.channelIndexList = None
303
303
304 self.flagNoData = True
304 self.flagNoData = True
305
305
306 self.flagTimeBlock = False
306 self.flagTimeBlock = False
307
307
308 self.utctime = None
308 self.utctime = None
309
309
310 self.timeZone = None
310 self.timeZone = None
311
311
312 self.dstFlag = None
312 self.dstFlag = None
313
313
314 self.errorCount = None
314 self.errorCount = None
315
315
316 self.nCohInt = None
316 self.nCohInt = None
317
317
318 self.blocksize = None
318 self.blocksize = None
319
319
320 self.flagDecodeData = False #asumo q la data no esta decodificada
320 self.flagDecodeData = False #asumo q la data no esta decodificada
321
321
322 self.flagDeflipData = False #asumo q la data no esta sin flip
322 self.flagDeflipData = False #asumo q la data no esta sin flip
323
323
324 self.flagShiftFFT = False
324 self.flagShiftFFT = False
325
325
326
326
327 def getNoisebyHildebrand(self):
327 def getNoisebyHildebrand(self):
328 """
328 """
329 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
329 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
330
330
331 Return:
331 Return:
332 noiselevel
332 noiselevel
333 """
333 """
334
334
335 for channel in range(self.nChannels):
335 for channel in range(self.nChannels):
336 daux = self.data_spc[channel,:,:]
336 daux = self.data_spc[channel,:,:]
337 self.noise[channel] = hildebrand_sekhon(daux, self.nCohInt)
337 self.noise[channel] = hildebrand_sekhon(daux, self.nCohInt)
338
338
339 return self.noise
339 return self.noise
340
340
341 def getNoise(self, type = 1):
341 def getNoise(self, type = 1):
342
342
343 self.noise = numpy.zeros(self.nChannels)
343 self.noise = numpy.zeros(self.nChannels)
344
344
345 if type == 1:
345 if type == 1:
346 noise = self.getNoisebyHildebrand()
346 noise = self.getNoisebyHildebrand()
347
347
348 return 10*numpy.log10(noise)
348 return 10*numpy.log10(noise)
349
349
350 class Spectra(JROData):
350 class Spectra(JROData):
351
351
352 #data es un numpy array de 2 dmensiones (canales, perfiles, alturas)
352 #data es un numpy array de 2 dmensiones (canales, perfiles, alturas)
353 data_spc = None
353 data_spc = None
354
354
355 #data es un numpy array de 2 dmensiones (canales, pares, alturas)
355 #data es un numpy array de 2 dmensiones (canales, pares, alturas)
356 data_cspc = None
356 data_cspc = None
357
357
358 #data es un numpy array de 2 dmensiones (canales, alturas)
358 #data es un numpy array de 2 dmensiones (canales, alturas)
359 data_dc = None
359 data_dc = None
360
360
361 nFFTPoints = None
361 nFFTPoints = None
362
362
363 nPairs = None
363 nPairs = None
364
364
365 pairsList = None
365 pairsList = None
366
366
367 nIncohInt = None
367 nIncohInt = None
368
368
369 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
369 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
370
370
371 nCohInt = None #se requiere para determinar el valor de timeInterval
371 nCohInt = None #se requiere para determinar el valor de timeInterval
372
372
373 def __init__(self):
373 def __init__(self):
374 '''
374 '''
375 Constructor
375 Constructor
376 '''
376 '''
377
377
378 self.radarControllerHeaderObj = RadarControllerHeader()
378 self.radarControllerHeaderObj = RadarControllerHeader()
379
379
380 self.systemHeaderObj = SystemHeader()
380 self.systemHeaderObj = SystemHeader()
381
381
382 self.type = "Spectra"
382 self.type = "Spectra"
383
383
384 # self.data = None
384 # self.data = None
385
385
386 self.dtype = None
386 self.dtype = None
387
387
388 # self.nChannels = 0
388 # self.nChannels = 0
389
389
390 # self.nHeights = 0
390 # self.nHeights = 0
391
391
392 self.nProfiles = None
392 self.nProfiles = None
393
393
394 self.heightList = None
394 self.heightList = None
395
395
396 self.channelList = None
396 self.channelList = None
397
397
398 # self.channelIndexList = None
398 # self.channelIndexList = None
399
399
400 self.flagNoData = True
400 self.flagNoData = True
401
401
402 self.flagTimeBlock = False
402 self.flagTimeBlock = False
403
403
404 self.utctime = None
404 self.utctime = None
405
405
406 self.nCohInt = None
406 self.nCohInt = None
407
407
408 self.nIncohInt = None
408 self.nIncohInt = None
409
409
410 self.blocksize = None
410 self.blocksize = None
411
411
412 self.nFFTPoints = None
412 self.nFFTPoints = None
413
413
414 self.wavelength = None
414 self.wavelength = None
415
415
416 self.flagDecodeData = False #asumo q la data no esta decodificada
416 self.flagDecodeData = False #asumo q la data no esta decodificada
417
417
418 self.flagDeflipData = False #asumo q la data no esta sin flip
418 self.flagDeflipData = False #asumo q la data no esta sin flip
419
419
420 self.flagShiftFFT = False
420 self.flagShiftFFT = False
421
421
422 def getNoisebyHildebrand(self):
422 def getNoisebyHildebrand(self):
423 """
423 """
424 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
424 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
425
425
426 Return:
426 Return:
427 noiselevel
427 noiselevel
428 """
428 """
429
429
430 for channel in range(self.nChannels):
430 for channel in range(self.nChannels):
431 daux = self.data_spc[channel,:,:]
431 daux = self.data_spc[channel,:,:]
432 self.noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
432 self.noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
433
433
434 return self.noise
434 return self.noise
435
435
436 def getNoisebyWindow(self, heiIndexMin=0, heiIndexMax=-1, freqIndexMin=0, freqIndexMax=-1):
436 def getNoisebyWindow(self, heiIndexMin=0, heiIndexMax=-1, freqIndexMin=0, freqIndexMax=-1):
437 """
437 """
438 Determina el ruido del canal utilizando la ventana indicada con las coordenadas:
438 Determina el ruido del canal utilizando la ventana indicada con las coordenadas:
439 (heiIndexMIn, freqIndexMin) hasta (heiIndexMax, freqIndexMAx)
439 (heiIndexMIn, freqIndexMin) hasta (heiIndexMax, freqIndexMAx)
440
440
441 Inputs:
441 Inputs:
442 heiIndexMin: Limite inferior del eje de alturas
442 heiIndexMin: Limite inferior del eje de alturas
443 heiIndexMax: Limite superior del eje de alturas
443 heiIndexMax: Limite superior del eje de alturas
444 freqIndexMin: Limite inferior del eje de frecuencia
444 freqIndexMin: Limite inferior del eje de frecuencia
445 freqIndexMax: Limite supoerior del eje de frecuencia
445 freqIndexMax: Limite supoerior del eje de frecuencia
446 """
446 """
447
447
448 data = self.data_spc[:, heiIndexMin:heiIndexMax, freqIndexMin:freqIndexMax]
448 data = self.data_spc[:, heiIndexMin:heiIndexMax, freqIndexMin:freqIndexMax]
449
449
450 for channel in range(self.nChannels):
450 for channel in range(self.nChannels):
451 daux = data[channel,:,:]
451 daux = data[channel,:,:]
452 self.noise[channel] = numpy.average(daux)
452 self.noise[channel] = numpy.average(daux)
453
453
454 return self.noise
454 return self.noise
455
455
456 def getNoisebySort(self):
456 def getNoisebySort(self):
457
457
458 for channel in range(self.nChannels):
458 for channel in range(self.nChannels):
459 daux = self.data_spc[channel,:,:]
459 daux = self.data_spc[channel,:,:]
460 self.noise[channel] = sorting_bruce(daux, self.nIncohInt)
460 self.noise[channel] = sorting_bruce(daux, self.nIncohInt)
461
461
462 return self.noise
462 return self.noise
463
463
464 def getNoise(self, type = 1):
464 def getNoise(self, type = 1):
465
465
466 self.noise = numpy.zeros(self.nChannels)
466 self.noise = numpy.zeros(self.nChannels)
467
467
468 if type == 1:
468 if type == 1:
469 noise = self.getNoisebyHildebrand()
469 noise = self.getNoisebyHildebrand()
470
470
471 if type == 2:
471 if type == 2:
472 noise = self.getNoisebySort()
472 noise = self.getNoisebySort()
473
473
474 if type == 3:
474 if type == 3:
475 noise = self.getNoisebyWindow()
475 noise = self.getNoisebyWindow()
476
476
477 return noise
477 return noise
478
478
479
479
480 def getFreqRange(self, extrapoints=0):
480 def getFreqRange(self, extrapoints=0):
481
481
482 deltafreq = self.getFmax() / self.nFFTPoints
482 deltafreq = self.getFmax() / self.nFFTPoints
483 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
483 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
484
484
485 return freqrange
485 return freqrange
486
486
487 def getVelRange(self, extrapoints=0):
487 def getVelRange(self, extrapoints=0):
488
488
489 deltav = self.getVmax() / self.nFFTPoints
489 deltav = self.getVmax() / self.nFFTPoints
490 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltav/2
490 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltav/2
491
491
492 return velrange
492 return velrange
493
493
494 def getNPairs(self):
494 def getNPairs(self):
495
495
496 return len(self.pairsList)
496 return len(self.pairsList)
497
497
498 def getPairsIndexList(self):
498 def getPairsIndexList(self):
499
499
500 return range(self.nPairs)
500 return range(self.nPairs)
501
501
502 def getNormFactor(self):
502 def getNormFactor(self):
503 pwcode = 1
503 pwcode = 1
504 if self.flagDecodeData:
504 if self.flagDecodeData:
505 pwcode = numpy.sum(self.code[0]**2)
505 pwcode = numpy.sum(self.code[0]**2)
506 normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode
506 normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode
507
507
508 return normFactor
508 return normFactor
509
509
510 def getFlagCspc(self):
510 def getFlagCspc(self):
511
511
512 if self.data_cspc == None:
512 if self.data_cspc == None:
513 return True
513 return True
514
514
515 return False
515 return False
516
516
517 def getFlagDc(self):
517 def getFlagDc(self):
518
518
519 if self.data_dc == None:
519 if self.data_dc == None:
520 return True
520 return True
521
521
522 return False
522 return False
523
523
524 nPairs = property(getNPairs, "I'm the 'nPairs' property.")
524 nPairs = property(getNPairs, "I'm the 'nPairs' property.")
525 pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.")
525 pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.")
526 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
526 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
527 flag_cspc = property(getFlagCspc)
527 flag_cspc = property(getFlagCspc)
528 flag_dc = property(getFlagDc)
528 flag_dc = property(getFlagDc)
529
529
530 class SpectraHeis(JROData):
530 class SpectraHeis(JROData):
531
531
532 data_spc = None
532 data_spc = None
533
533
534 data_cspc = None
534 data_cspc = None
535
535
536 data_dc = None
536 data_dc = None
537
537
538 nFFTPoints = None
538 nFFTPoints = None
539
539
540 nPairs = None
540 nPairs = None
541
541
542 pairsList = None
542 pairsList = None
543
543
544 nIncohInt = None
544 nIncohInt = None
545
545
546 def __init__(self):
546 def __init__(self):
547
547
548 self.radarControllerHeaderObj = RadarControllerHeader()
548 self.radarControllerHeaderObj = RadarControllerHeader()
549
549
550 self.systemHeaderObj = SystemHeader()
550 self.systemHeaderObj = SystemHeader()
551
551
552 self.type = "SpectraHeis"
552 self.type = "SpectraHeis"
553
553
554 self.dtype = None
554 self.dtype = None
555
555
556 # self.nChannels = 0
556 # self.nChannels = 0
557
557
558 # self.nHeights = 0
558 # self.nHeights = 0
559
559
560 self.nProfiles = None
560 self.nProfiles = None
561
561
562 self.heightList = None
562 self.heightList = None
563
563
564 self.channelList = None
564 self.channelList = None
565
565
566 # self.channelIndexList = None
566 # self.channelIndexList = None
567
567
568 self.flagNoData = True
568 self.flagNoData = True
569
569
570 self.flagTimeBlock = False
570 self.flagTimeBlock = False
571
571
572 self.nPairs = 0
572 self.nPairs = 0
573
573
574 self.utctime = None
574 self.utctime = None
575
575
576 self.blocksize = None
576 self.blocksize = None
577
577
578 class Fits:
578 class Fits:
579
579
580 heightList = None
581
582 channelList = None
583
584 flagNoData = True
585
586 flagTimeBlock = False
587
588 useLocalTime = False
589
590 utctime = None
591
592 timeZone = None
593
594 ippSeconds = None
595
596 timeInterval = None
597
598 nCohInt = None
599
600 nIncohInt = None
601
602 noise = None
603
604 windowOfFilter = 1
605
606 #Speed of ligth
607 C = 3e8
608
609 frequency = 49.92e6
610
611 realtime = False
612
613
580 def __init__(self):
614 def __init__(self):
581 self.useLocalTime = False
615
616 self.type = "Fits"
617
618 self.nProfiles = None
619
620 self.heightList = None
621
622 self.channelList = None
623
624 # self.channelIndexList = None
625
626 self.flagNoData = True
627
582 self.utctime = None
628 self.utctime = None
583 self.timeZone = None
629
584 self.ltctime = None
630 self.nCohInt = None
585 self.timeInterval = None
631
586 self.header = None
632 self.nIncohInt = None
587 self.data_header = None
633
588 self.data = None
634 self.useLocalTime = True
589 self.datatime = None
635
590 self.flagNoData = False
636 # self.utctime = None
591 self.expName = ''
637 # self.timeZone = None
592 self.nChannels = None
638 # self.ltctime = None
593 self.nSamples = None
639 # self.timeInterval = None
594 self.dataBlocksPerFile = None
640 # self.header = None
595 self.comments = ''
641 # self.data_header = None
642 # self.data = None
643 # self.datatime = None
644 # self.flagNoData = False
645 # self.expName = ''
646 # self.nChannels = None
647 # self.nSamples = None
648 # self.dataBlocksPerFile = None
649 # self.comments = ''
650 #
596
651
597
652
598 def getltctime(self):
653 def getltctime(self):
599
654
600 if self.useLocalTime:
655 if self.useLocalTime:
601 return self.utctime - self.timeZone*60
656 return self.utctime - self.timeZone*60
602
657
603 return self.utctime
658 return self.utctime
604
659
605 def getDatatime(self):
660 def getDatatime(self):
606
661
607 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
662 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
608 return datatime
663 return datatime
609
664
610 def getTimeRange(self):
665 def getTimeRange(self):
611
666
612 datatime = []
667 datatime = []
613
668
614 datatime.append(self.ltctime)
669 datatime.append(self.ltctime)
615 datatime.append(self.ltctime + self.timeInterval)
670 datatime.append(self.ltctime + self.timeInterval)
616
671
617 datatime = numpy.array(datatime)
672 datatime = numpy.array(datatime)
618
673
619 return datatime
674 return datatime
620
675
676 def getHeiRange(self):
677
678 heis = self.heightList
679
680 return heis
681
621 def isEmpty(self):
682 def isEmpty(self):
622
683
623 return self.flagNoData
684 return self.flagNoData
624
685
686 def getNHeights(self):
687
688 return len(self.heightList)
689
690 def getNChannels(self):
691
692 return len(self.channelList)
693
694 def getChannelIndexList(self):
695
696 return range(self.nChannels)
697
698 def getNoise(self, type = 1):
699
700 self.noise = numpy.zeros(self.nChannels)
701
702 if type == 1:
703 noise = self.getNoisebyHildebrand()
704
705 if type == 2:
706 noise = self.getNoisebySort()
707
708 if type == 3:
709 noise = self.getNoisebyWindow()
710
711 return noise
712
625 datatime = property(getDatatime, "I'm the 'datatime' property")
713 datatime = property(getDatatime, "I'm the 'datatime' property")
714 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
715 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
716 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
717 noise = property(getNoise, "I'm the 'nHeights' property.")
718 datatime = property(getDatatime, "I'm the 'datatime' property")
719 ltctime = property(getltctime, "I'm the 'ltctime' property")
720
626 ltctime = property(getltctime, "I'm the 'ltctime' property") No newline at end of file
721 ltctime = property(getltctime, "I'm the 'ltctime' property")
@@ -1,3405 +1,3452
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
5 '''
5 '''
6
6
7 import os, sys
7 import os, sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import time, datetime
12 import time, datetime
13 from xml.etree.ElementTree import Element, SubElement, ElementTree
13 from xml.etree.ElementTree import Element, SubElement, ElementTree
14 try:
14 try:
15 import pyfits
15 import pyfits
16 except:
16 except:
17 print "pyfits module has not been imported, it should be installed to save files in fits format"
17 print "pyfits module has not been imported, it should be installed to save files in fits format"
18
18
19 from jrodata import *
19 from jrodata import *
20 from jroheaderIO import *
20 from jroheaderIO import *
21 from jroprocessing import *
21 from jroprocessing import *
22
22
23 LOCALTIME = True #-18000
23 LOCALTIME = True #-18000
24
24
25 def isNumber(str):
25 def isNumber(str):
26 """
26 """
27 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
27 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
28
28
29 Excepciones:
29 Excepciones:
30 Si un determinado string no puede ser convertido a numero
30 Si un determinado string no puede ser convertido a numero
31 Input:
31 Input:
32 str, string al cual se le analiza para determinar si convertible a un numero o no
32 str, string al cual se le analiza para determinar si convertible a un numero o no
33
33
34 Return:
34 Return:
35 True : si el string es uno numerico
35 True : si el string es uno numerico
36 False : no es un string numerico
36 False : no es un string numerico
37 """
37 """
38 try:
38 try:
39 float( str )
39 float( str )
40 return True
40 return True
41 except:
41 except:
42 return False
42 return False
43
43
44 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
44 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
45 """
45 """
46 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
46 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
47
47
48 Inputs:
48 Inputs:
49 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
49 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
50
50
51 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
51 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
52 segundos contados desde 01/01/1970.
52 segundos contados desde 01/01/1970.
53 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
53 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
54 segundos contados desde 01/01/1970.
54 segundos contados desde 01/01/1970.
55
55
56 Return:
56 Return:
57 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
57 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
58 fecha especificado, de lo contrario retorna False.
58 fecha especificado, de lo contrario retorna False.
59
59
60 Excepciones:
60 Excepciones:
61 Si el archivo no existe o no puede ser abierto
61 Si el archivo no existe o no puede ser abierto
62 Si la cabecera no puede ser leida.
62 Si la cabecera no puede ser leida.
63
63
64 """
64 """
65 basicHeaderObj = BasicHeader(LOCALTIME)
65 basicHeaderObj = BasicHeader(LOCALTIME)
66
66
67 try:
67 try:
68 fp = open(filename,'rb')
68 fp = open(filename,'rb')
69 except:
69 except:
70 raise IOError, "The file %s can't be opened" %(filename)
70 raise IOError, "The file %s can't be opened" %(filename)
71
71
72 sts = basicHeaderObj.read(fp)
72 sts = basicHeaderObj.read(fp)
73 fp.close()
73 fp.close()
74
74
75 if not(sts):
75 if not(sts):
76 print "Skipping the file %s because it has not a valid header" %(filename)
76 print "Skipping the file %s because it has not a valid header" %(filename)
77 return 0
77 return 0
78
78
79 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
79 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
80 return 0
80 return 0
81
81
82 return 1
82 return 1
83
83
84 def isFileinThisTime(filename, startTime, endTime):
84 def isFileinThisTime(filename, startTime, endTime):
85 """
85 """
86 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
86 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
87
87
88 Inputs:
88 Inputs:
89 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
89 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
90
90
91 startTime : tiempo inicial del rango seleccionado en formato datetime.time
91 startTime : tiempo inicial del rango seleccionado en formato datetime.time
92
92
93 endTime : tiempo final del rango seleccionado en formato datetime.time
93 endTime : tiempo final del rango seleccionado en formato datetime.time
94
94
95 Return:
95 Return:
96 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
96 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
97 fecha especificado, de lo contrario retorna False.
97 fecha especificado, de lo contrario retorna False.
98
98
99 Excepciones:
99 Excepciones:
100 Si el archivo no existe o no puede ser abierto
100 Si el archivo no existe o no puede ser abierto
101 Si la cabecera no puede ser leida.
101 Si la cabecera no puede ser leida.
102
102
103 """
103 """
104
104
105
105
106 try:
106 try:
107 fp = open(filename,'rb')
107 fp = open(filename,'rb')
108 except:
108 except:
109 raise IOError, "The file %s can't be opened" %(filename)
109 raise IOError, "The file %s can't be opened" %(filename)
110
110
111 basicHeaderObj = BasicHeader(LOCALTIME)
111 basicHeaderObj = BasicHeader(LOCALTIME)
112 sts = basicHeaderObj.read(fp)
112 sts = basicHeaderObj.read(fp)
113 fp.close()
113 fp.close()
114
114
115 thisDatetime = basicHeaderObj.datatime
115 thisDatetime = basicHeaderObj.datatime
116 thisTime = basicHeaderObj.datatime.time()
116 thisTime = basicHeaderObj.datatime.time()
117
117
118 if not(sts):
118 if not(sts):
119 print "Skipping the file %s because it has not a valid header" %(filename)
119 print "Skipping the file %s because it has not a valid header" %(filename)
120 return None
120 return None
121
121
122 if not ((startTime <= thisTime) and (endTime > thisTime)):
122 if not ((startTime <= thisTime) and (endTime > thisTime)):
123 return None
123 return None
124
124
125 return thisDatetime
125 return thisDatetime
126
126
127 def getFileFromSet(path,ext,set):
127 def getFileFromSet(path,ext,set):
128 validFilelist = []
128 validFilelist = []
129 fileList = os.listdir(path)
129 fileList = os.listdir(path)
130
130
131 # 0 1234 567 89A BCDE
131 # 0 1234 567 89A BCDE
132 # H YYYY DDD SSS .ext
132 # H YYYY DDD SSS .ext
133
133
134 for file in fileList:
134 for file in fileList:
135 try:
135 try:
136 year = int(file[1:5])
136 year = int(file[1:5])
137 doy = int(file[5:8])
137 doy = int(file[5:8])
138
138
139
139
140 except:
140 except:
141 continue
141 continue
142
142
143 if (os.path.splitext(file)[-1].lower() != ext.lower()):
143 if (os.path.splitext(file)[-1].lower() != ext.lower()):
144 continue
144 continue
145
145
146 validFilelist.append(file)
146 validFilelist.append(file)
147
147
148 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
148 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
149
149
150 if len(myfile)!= 0:
150 if len(myfile)!= 0:
151 return myfile[0]
151 return myfile[0]
152 else:
152 else:
153 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
153 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
154 print 'the filename %s does not exist'%filename
154 print 'the filename %s does not exist'%filename
155 print '...going to the last file: '
155 print '...going to the last file: '
156
156
157 if validFilelist:
157 if validFilelist:
158 validFilelist = sorted( validFilelist, key=str.lower )
158 validFilelist = sorted( validFilelist, key=str.lower )
159 return validFilelist[-1]
159 return validFilelist[-1]
160
160
161 return None
161 return None
162
162
163
163
164 def getlastFileFromPath(path, ext):
164 def getlastFileFromPath(path, ext):
165 """
165 """
166 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
166 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
167 al final de la depuracion devuelve el ultimo file de la lista que quedo.
167 al final de la depuracion devuelve el ultimo file de la lista que quedo.
168
168
169 Input:
169 Input:
170 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
170 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
171 ext : extension de los files contenidos en una carpeta
171 ext : extension de los files contenidos en una carpeta
172
172
173 Return:
173 Return:
174 El ultimo file de una determinada carpeta, no se considera el path.
174 El ultimo file de una determinada carpeta, no se considera el path.
175 """
175 """
176 validFilelist = []
176 validFilelist = []
177 fileList = os.listdir(path)
177 fileList = os.listdir(path)
178
178
179 # 0 1234 567 89A BCDE
179 # 0 1234 567 89A BCDE
180 # H YYYY DDD SSS .ext
180 # H YYYY DDD SSS .ext
181
181
182 for file in fileList:
182 for file in fileList:
183 try:
183 try:
184 year = int(file[1:5])
184 year = int(file[1:5])
185 doy = int(file[5:8])
185 doy = int(file[5:8])
186
186
187
187
188 except:
188 except:
189 continue
189 continue
190
190
191 if (os.path.splitext(file)[-1].lower() != ext.lower()):
191 if (os.path.splitext(file)[-1].lower() != ext.lower()):
192 continue
192 continue
193
193
194 validFilelist.append(file)
194 validFilelist.append(file)
195
195
196 if validFilelist:
196 if validFilelist:
197 validFilelist = sorted( validFilelist, key=str.lower )
197 validFilelist = sorted( validFilelist, key=str.lower )
198 return validFilelist[-1]
198 return validFilelist[-1]
199
199
200 return None
200 return None
201
201
202 def checkForRealPath(path, foldercounter, year, doy, set, ext):
202 def checkForRealPath(path, foldercounter, year, doy, set, ext):
203 """
203 """
204 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
204 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
205 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
205 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
206 el path exacto de un determinado file.
206 el path exacto de un determinado file.
207
207
208 Example :
208 Example :
209 nombre correcto del file es .../.../D2009307/P2009307367.ext
209 nombre correcto del file es .../.../D2009307/P2009307367.ext
210
210
211 Entonces la funcion prueba con las siguientes combinaciones
211 Entonces la funcion prueba con las siguientes combinaciones
212 .../.../y2009307367.ext
212 .../.../y2009307367.ext
213 .../.../Y2009307367.ext
213 .../.../Y2009307367.ext
214 .../.../x2009307/y2009307367.ext
214 .../.../x2009307/y2009307367.ext
215 .../.../x2009307/Y2009307367.ext
215 .../.../x2009307/Y2009307367.ext
216 .../.../X2009307/y2009307367.ext
216 .../.../X2009307/y2009307367.ext
217 .../.../X2009307/Y2009307367.ext
217 .../.../X2009307/Y2009307367.ext
218 siendo para este caso, la ultima combinacion de letras, identica al file buscado
218 siendo para este caso, la ultima combinacion de letras, identica al file buscado
219
219
220 Return:
220 Return:
221 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
221 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
222 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
222 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
223 para el filename
223 para el filename
224 """
224 """
225 fullfilename = None
225 fullfilename = None
226 find_flag = False
226 find_flag = False
227 filename = None
227 filename = None
228
228
229 prefixDirList = [None,'d','D']
229 prefixDirList = [None,'d','D']
230 if ext.lower() == ".r": #voltage
230 if ext.lower() == ".r": #voltage
231 prefixFileList = ['d','D']
231 prefixFileList = ['d','D']
232 elif ext.lower() == ".pdata": #spectra
232 elif ext.lower() == ".pdata": #spectra
233 prefixFileList = ['p','P']
233 prefixFileList = ['p','P']
234 else:
234 else:
235 return None, filename
235 return None, filename
236
236
237 #barrido por las combinaciones posibles
237 #barrido por las combinaciones posibles
238 for prefixDir in prefixDirList:
238 for prefixDir in prefixDirList:
239 thispath = path
239 thispath = path
240 if prefixDir != None:
240 if prefixDir != None:
241 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
241 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
242 if foldercounter == 0:
242 if foldercounter == 0:
243 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
243 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
244 else:
244 else:
245 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
245 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
246 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
246 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
247 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
247 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
248 fullfilename = os.path.join( thispath, filename ) #formo el path completo
248 fullfilename = os.path.join( thispath, filename ) #formo el path completo
249
249
250 if os.path.exists( fullfilename ): #verifico que exista
250 if os.path.exists( fullfilename ): #verifico que exista
251 find_flag = True
251 find_flag = True
252 break
252 break
253 if find_flag:
253 if find_flag:
254 break
254 break
255
255
256 if not(find_flag):
256 if not(find_flag):
257 return None, filename
257 return None, filename
258
258
259 return fullfilename, filename
259 return fullfilename, filename
260
260
261 def isDoyFolder(folder):
261 def isDoyFolder(folder):
262 try:
262 try:
263 year = int(folder[1:5])
263 year = int(folder[1:5])
264 except:
264 except:
265 return 0
265 return 0
266
266
267 try:
267 try:
268 doy = int(folder[5:8])
268 doy = int(folder[5:8])
269 except:
269 except:
270 return 0
270 return 0
271
271
272 return 1
272 return 1
273
273
274 class JRODataIO:
274 class JRODataIO:
275
275
276 c = 3E8
276 c = 3E8
277
277
278 isConfig = False
278 isConfig = False
279
279
280 basicHeaderObj = BasicHeader(LOCALTIME)
280 basicHeaderObj = BasicHeader(LOCALTIME)
281
281
282 systemHeaderObj = SystemHeader()
282 systemHeaderObj = SystemHeader()
283
283
284 radarControllerHeaderObj = RadarControllerHeader()
284 radarControllerHeaderObj = RadarControllerHeader()
285
285
286 processingHeaderObj = ProcessingHeader()
286 processingHeaderObj = ProcessingHeader()
287
287
288 online = 0
288 online = 0
289
289
290 dtype = None
290 dtype = None
291
291
292 pathList = []
292 pathList = []
293
293
294 filenameList = []
294 filenameList = []
295
295
296 filename = None
296 filename = None
297
297
298 ext = None
298 ext = None
299
299
300 flagIsNewFile = 1
300 flagIsNewFile = 1
301
301
302 flagTimeBlock = 0
302 flagTimeBlock = 0
303
303
304 flagIsNewBlock = 0
304 flagIsNewBlock = 0
305
305
306 fp = None
306 fp = None
307
307
308 firstHeaderSize = 0
308 firstHeaderSize = 0
309
309
310 basicHeaderSize = 24
310 basicHeaderSize = 24
311
311
312 versionFile = 1103
312 versionFile = 1103
313
313
314 fileSize = None
314 fileSize = None
315
315
316 ippSeconds = None
316 ippSeconds = None
317
317
318 fileSizeByHeader = None
318 fileSizeByHeader = None
319
319
320 fileIndex = None
320 fileIndex = None
321
321
322 profileIndex = None
322 profileIndex = None
323
323
324 blockIndex = None
324 blockIndex = None
325
325
326 nTotalBlocks = None
326 nTotalBlocks = None
327
327
328 maxTimeStep = 30
328 maxTimeStep = 30
329
329
330 lastUTTime = None
330 lastUTTime = None
331
331
332 datablock = None
332 datablock = None
333
333
334 dataOut = None
334 dataOut = None
335
335
336 blocksize = None
336 blocksize = None
337
337
338 def __init__(self):
338 def __init__(self):
339
339
340 raise ValueError, "Not implemented"
340 raise ValueError, "Not implemented"
341
341
342 def run(self):
342 def run(self):
343
343
344 raise ValueError, "Not implemented"
344 raise ValueError, "Not implemented"
345
345
346 def getOutput(self):
346 def getOutput(self):
347
347
348 return self.dataOut
348 return self.dataOut
349
349
350 class JRODataReader(JRODataIO, ProcessingUnit):
350 class JRODataReader(JRODataIO, ProcessingUnit):
351
351
352 nReadBlocks = 0
352 nReadBlocks = 0
353
353
354 delay = 10 #number of seconds waiting a new file
354 delay = 10 #number of seconds waiting a new file
355
355
356 nTries = 3 #quantity tries
356 nTries = 3 #quantity tries
357
357
358 nFiles = 3 #number of files for searching
358 nFiles = 3 #number of files for searching
359
359
360 path = None
360 path = None
361
361
362 foldercounter = 0
362 foldercounter = 0
363
363
364 flagNoMoreFiles = 0
364 flagNoMoreFiles = 0
365
365
366 datetimeList = []
366 datetimeList = []
367
367
368 __isFirstTimeOnline = 1
368 __isFirstTimeOnline = 1
369
369
370 __printInfo = True
370 __printInfo = True
371
371
372 profileIndex = None
372 profileIndex = None
373
373
374 def __init__(self):
374 def __init__(self):
375
375
376 """
376 """
377
377
378 """
378 """
379
379
380 raise ValueError, "This method has not been implemented"
380 raise ValueError, "This method has not been implemented"
381
381
382
382
383 def createObjByDefault(self):
383 def createObjByDefault(self):
384 """
384 """
385
385
386 """
386 """
387 raise ValueError, "This method has not been implemented"
387 raise ValueError, "This method has not been implemented"
388
388
389 def getBlockDimension(self):
389 def getBlockDimension(self):
390
390
391 raise ValueError, "No implemented"
391 raise ValueError, "No implemented"
392
392
393 def __searchFilesOffLine(self,
393 def __searchFilesOffLine(self,
394 path,
394 path,
395 startDate,
395 startDate,
396 endDate,
396 endDate,
397 startTime=datetime.time(0,0,0),
397 startTime=datetime.time(0,0,0),
398 endTime=datetime.time(23,59,59),
398 endTime=datetime.time(23,59,59),
399 set=None,
399 set=None,
400 expLabel='',
400 expLabel='',
401 ext='.r',
401 ext='.r',
402 walk=True):
402 walk=True):
403
403
404 pathList = []
404 pathList = []
405
405
406 if not walk:
406 if not walk:
407 pathList.append(path)
407 pathList.append(path)
408
408
409 else:
409 else:
410 dirList = []
410 dirList = []
411 for thisPath in os.listdir(path):
411 for thisPath in os.listdir(path):
412 if not os.path.isdir(os.path.join(path,thisPath)):
412 if not os.path.isdir(os.path.join(path,thisPath)):
413 continue
413 continue
414 if not isDoyFolder(thisPath):
414 if not isDoyFolder(thisPath):
415 continue
415 continue
416
416
417 dirList.append(thisPath)
417 dirList.append(thisPath)
418
418
419 if not(dirList):
419 if not(dirList):
420 return None, None
420 return None, None
421
421
422 thisDate = startDate
422 thisDate = startDate
423
423
424 while(thisDate <= endDate):
424 while(thisDate <= endDate):
425 year = thisDate.timetuple().tm_year
425 year = thisDate.timetuple().tm_year
426 doy = thisDate.timetuple().tm_yday
426 doy = thisDate.timetuple().tm_yday
427
427
428 matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*')
428 matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*')
429 if len(matchlist) == 0:
429 if len(matchlist) == 0:
430 thisDate += datetime.timedelta(1)
430 thisDate += datetime.timedelta(1)
431 continue
431 continue
432 for match in matchlist:
432 for match in matchlist:
433 pathList.append(os.path.join(path,match,expLabel))
433 pathList.append(os.path.join(path,match,expLabel))
434
434
435 thisDate += datetime.timedelta(1)
435 thisDate += datetime.timedelta(1)
436
436
437 if pathList == []:
437 if pathList == []:
438 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
438 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
439 return None, None
439 return None, None
440
440
441 print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate)
441 print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate)
442
442
443 filenameList = []
443 filenameList = []
444 datetimeList = []
444 datetimeList = []
445
445
446 for i in range(len(pathList)):
446 for i in range(len(pathList)):
447
447
448 thisPath = pathList[i]
448 thisPath = pathList[i]
449
449
450 fileList = glob.glob1(thisPath, "*%s" %ext)
450 fileList = glob.glob1(thisPath, "*%s" %ext)
451 fileList.sort()
451 fileList.sort()
452
452
453 for file in fileList:
453 for file in fileList:
454
454
455 filename = os.path.join(thisPath,file)
455 filename = os.path.join(thisPath,file)
456 thisDatetime = isFileinThisTime(filename, startTime, endTime)
456 thisDatetime = isFileinThisTime(filename, startTime, endTime)
457
457
458 if not(thisDatetime):
458 if not(thisDatetime):
459 continue
459 continue
460
460
461 filenameList.append(filename)
461 filenameList.append(filename)
462 datetimeList.append(thisDatetime)
462 datetimeList.append(thisDatetime)
463
463
464 if not(filenameList):
464 if not(filenameList):
465 print "Any file was found for the time range %s - %s" %(startTime, endTime)
465 print "Any file was found for the time range %s - %s" %(startTime, endTime)
466 return None, None
466 return None, None
467
467
468 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
468 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
469 print
469 print
470
470
471 for i in range(len(filenameList)):
471 for i in range(len(filenameList)):
472 print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
472 print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
473
473
474 self.filenameList = filenameList
474 self.filenameList = filenameList
475 self.datetimeList = datetimeList
475 self.datetimeList = datetimeList
476
476
477 return pathList, filenameList
477 return pathList, filenameList
478
478
479 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
479 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
480
480
481 """
481 """
482 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
482 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
483 devuelve el archivo encontrado ademas de otros datos.
483 devuelve el archivo encontrado ademas de otros datos.
484
484
485 Input:
485 Input:
486 path : carpeta donde estan contenidos los files que contiene data
486 path : carpeta donde estan contenidos los files que contiene data
487
487
488 expLabel : Nombre del subexperimento (subfolder)
488 expLabel : Nombre del subexperimento (subfolder)
489
489
490 ext : extension de los files
490 ext : extension de los files
491
491
492 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
492 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
493
493
494 Return:
494 Return:
495 directory : eL directorio donde esta el file encontrado
495 directory : eL directorio donde esta el file encontrado
496 filename : el ultimo file de una determinada carpeta
496 filename : el ultimo file de una determinada carpeta
497 year : el anho
497 year : el anho
498 doy : el numero de dia del anho
498 doy : el numero de dia del anho
499 set : el set del archivo
499 set : el set del archivo
500
500
501
501
502 """
502 """
503 dirList = []
503 dirList = []
504
504
505 if not walk:
505 if not walk:
506 fullpath = path
506 fullpath = path
507 foldercounter = 0
507 foldercounter = 0
508 else:
508 else:
509 #Filtra solo los directorios
509 #Filtra solo los directorios
510 for thisPath in os.listdir(path):
510 for thisPath in os.listdir(path):
511 if not os.path.isdir(os.path.join(path,thisPath)):
511 if not os.path.isdir(os.path.join(path,thisPath)):
512 continue
512 continue
513 if not isDoyFolder(thisPath):
513 if not isDoyFolder(thisPath):
514 continue
514 continue
515
515
516 dirList.append(thisPath)
516 dirList.append(thisPath)
517
517
518 if not(dirList):
518 if not(dirList):
519 return None, None, None, None, None, None
519 return None, None, None, None, None, None
520
520
521 dirList = sorted( dirList, key=str.lower )
521 dirList = sorted( dirList, key=str.lower )
522
522
523 doypath = dirList[-1]
523 doypath = dirList[-1]
524 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
524 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
525 fullpath = os.path.join(path, doypath, expLabel)
525 fullpath = os.path.join(path, doypath, expLabel)
526
526
527
527
528 print "%s folder was found: " %(fullpath )
528 print "%s folder was found: " %(fullpath )
529
529
530 if set == None:
530 if set == None:
531 filename = getlastFileFromPath(fullpath, ext)
531 filename = getlastFileFromPath(fullpath, ext)
532 else:
532 else:
533 filename = getFileFromSet(fullpath, ext, set)
533 filename = getFileFromSet(fullpath, ext, set)
534
534
535 if not(filename):
535 if not(filename):
536 return None, None, None, None, None, None
536 return None, None, None, None, None, None
537
537
538 print "%s file was found" %(filename)
538 print "%s file was found" %(filename)
539
539
540 if not(self.__verifyFile(os.path.join(fullpath, filename))):
540 if not(self.__verifyFile(os.path.join(fullpath, filename))):
541 return None, None, None, None, None, None
541 return None, None, None, None, None, None
542
542
543 year = int( filename[1:5] )
543 year = int( filename[1:5] )
544 doy = int( filename[5:8] )
544 doy = int( filename[5:8] )
545 set = int( filename[8:11] )
545 set = int( filename[8:11] )
546
546
547 return fullpath, foldercounter, filename, year, doy, set
547 return fullpath, foldercounter, filename, year, doy, set
548
548
549 def __setNextFileOffline(self):
549 def __setNextFileOffline(self):
550
550
551 idFile = self.fileIndex
551 idFile = self.fileIndex
552
552
553 while (True):
553 while (True):
554 idFile += 1
554 idFile += 1
555 if not(idFile < len(self.filenameList)):
555 if not(idFile < len(self.filenameList)):
556 self.flagNoMoreFiles = 1
556 self.flagNoMoreFiles = 1
557 print "No more Files"
557 print "No more Files"
558 return 0
558 return 0
559
559
560 filename = self.filenameList[idFile]
560 filename = self.filenameList[idFile]
561
561
562 if not(self.__verifyFile(filename)):
562 if not(self.__verifyFile(filename)):
563 continue
563 continue
564
564
565 fileSize = os.path.getsize(filename)
565 fileSize = os.path.getsize(filename)
566 fp = open(filename,'rb')
566 fp = open(filename,'rb')
567 break
567 break
568
568
569 self.flagIsNewFile = 1
569 self.flagIsNewFile = 1
570 self.fileIndex = idFile
570 self.fileIndex = idFile
571 self.filename = filename
571 self.filename = filename
572 self.fileSize = fileSize
572 self.fileSize = fileSize
573 self.fp = fp
573 self.fp = fp
574
574
575 print "Setting the file: %s"%self.filename
575 print "Setting the file: %s"%self.filename
576
576
577 return 1
577 return 1
578
578
579 def __setNextFileOnline(self):
579 def __setNextFileOnline(self):
580 """
580 """
581 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
581 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
582 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
582 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
583 siguientes.
583 siguientes.
584
584
585 Affected:
585 Affected:
586 self.flagIsNewFile
586 self.flagIsNewFile
587 self.filename
587 self.filename
588 self.fileSize
588 self.fileSize
589 self.fp
589 self.fp
590 self.set
590 self.set
591 self.flagNoMoreFiles
591 self.flagNoMoreFiles
592
592
593 Return:
593 Return:
594 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
594 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
595 1 : si el file fue abierto con exito y esta listo a ser leido
595 1 : si el file fue abierto con exito y esta listo a ser leido
596
596
597 Excepciones:
597 Excepciones:
598 Si un determinado file no puede ser abierto
598 Si un determinado file no puede ser abierto
599 """
599 """
600 nFiles = 0
600 nFiles = 0
601 fileOk_flag = False
601 fileOk_flag = False
602 firstTime_flag = True
602 firstTime_flag = True
603
603
604 self.set += 1
604 self.set += 1
605
605
606 if self.set > 999:
606 if self.set > 999:
607 self.set = 0
607 self.set = 0
608 self.foldercounter += 1
608 self.foldercounter += 1
609
609
610 #busca el 1er file disponible
610 #busca el 1er file disponible
611 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
611 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
612 if fullfilename:
612 if fullfilename:
613 if self.__verifyFile(fullfilename, False):
613 if self.__verifyFile(fullfilename, False):
614 fileOk_flag = True
614 fileOk_flag = True
615
615
616 #si no encuentra un file entonces espera y vuelve a buscar
616 #si no encuentra un file entonces espera y vuelve a buscar
617 if not(fileOk_flag):
617 if not(fileOk_flag):
618 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
618 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
619
619
620 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
620 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
621 tries = self.nTries
621 tries = self.nTries
622 else:
622 else:
623 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
623 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
624
624
625 for nTries in range( tries ):
625 for nTries in range( tries ):
626 if firstTime_flag:
626 if firstTime_flag:
627 print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
627 print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
628 time.sleep( self.delay )
628 time.sleep( self.delay )
629 else:
629 else:
630 print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
630 print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
631
631
632 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
632 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
633 if fullfilename:
633 if fullfilename:
634 if self.__verifyFile(fullfilename):
634 if self.__verifyFile(fullfilename):
635 fileOk_flag = True
635 fileOk_flag = True
636 break
636 break
637
637
638 if fileOk_flag:
638 if fileOk_flag:
639 break
639 break
640
640
641 firstTime_flag = False
641 firstTime_flag = False
642
642
643 print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename
643 print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename
644 self.set += 1
644 self.set += 1
645
645
646 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
646 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
647 self.set = 0
647 self.set = 0
648 self.doy += 1
648 self.doy += 1
649 self.foldercounter = 0
649 self.foldercounter = 0
650
650
651 if fileOk_flag:
651 if fileOk_flag:
652 self.fileSize = os.path.getsize( fullfilename )
652 self.fileSize = os.path.getsize( fullfilename )
653 self.filename = fullfilename
653 self.filename = fullfilename
654 self.flagIsNewFile = 1
654 self.flagIsNewFile = 1
655 if self.fp != None: self.fp.close()
655 if self.fp != None: self.fp.close()
656 self.fp = open(fullfilename, 'rb')
656 self.fp = open(fullfilename, 'rb')
657 self.flagNoMoreFiles = 0
657 self.flagNoMoreFiles = 0
658 print 'Setting the file: %s' % fullfilename
658 print 'Setting the file: %s' % fullfilename
659 else:
659 else:
660 self.fileSize = 0
660 self.fileSize = 0
661 self.filename = None
661 self.filename = None
662 self.flagIsNewFile = 0
662 self.flagIsNewFile = 0
663 self.fp = None
663 self.fp = None
664 self.flagNoMoreFiles = 1
664 self.flagNoMoreFiles = 1
665 print 'No more Files'
665 print 'No more Files'
666
666
667 return fileOk_flag
667 return fileOk_flag
668
668
669
669
670 def setNextFile(self):
670 def setNextFile(self):
671 if self.fp != None:
671 if self.fp != None:
672 self.fp.close()
672 self.fp.close()
673
673
674 if self.online:
674 if self.online:
675 newFile = self.__setNextFileOnline()
675 newFile = self.__setNextFileOnline()
676 else:
676 else:
677 newFile = self.__setNextFileOffline()
677 newFile = self.__setNextFileOffline()
678
678
679 if not(newFile):
679 if not(newFile):
680 return 0
680 return 0
681
681
682 self.__readFirstHeader()
682 self.__readFirstHeader()
683 self.nReadBlocks = 0
683 self.nReadBlocks = 0
684 return 1
684 return 1
685
685
686 def __waitNewBlock(self):
686 def __waitNewBlock(self):
687 """
687 """
688 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
688 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
689
689
690 Si el modo de lectura es OffLine siempre retorn 0
690 Si el modo de lectura es OffLine siempre retorn 0
691 """
691 """
692 if not self.online:
692 if not self.online:
693 return 0
693 return 0
694
694
695 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
695 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
696 return 0
696 return 0
697
697
698 currentPointer = self.fp.tell()
698 currentPointer = self.fp.tell()
699
699
700 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
700 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
701
701
702 for nTries in range( self.nTries ):
702 for nTries in range( self.nTries ):
703
703
704 self.fp.close()
704 self.fp.close()
705 self.fp = open( self.filename, 'rb' )
705 self.fp = open( self.filename, 'rb' )
706 self.fp.seek( currentPointer )
706 self.fp.seek( currentPointer )
707
707
708 self.fileSize = os.path.getsize( self.filename )
708 self.fileSize = os.path.getsize( self.filename )
709 currentSize = self.fileSize - currentPointer
709 currentSize = self.fileSize - currentPointer
710
710
711 if ( currentSize >= neededSize ):
711 if ( currentSize >= neededSize ):
712 self.__rdBasicHeader()
712 self.__rdBasicHeader()
713 return 1
713 return 1
714
714
715 if self.fileSize == self.fileSizeByHeader:
715 if self.fileSize == self.fileSizeByHeader:
716 # self.flagEoF = True
716 # self.flagEoF = True
717 return 0
717 return 0
718
718
719 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
719 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
720 time.sleep( self.delay )
720 time.sleep( self.delay )
721
721
722
722
723 return 0
723 return 0
724
724
725 def waitDataBlock(self,pointer_location):
725 def waitDataBlock(self,pointer_location):
726
726
727 currentPointer = pointer_location
727 currentPointer = pointer_location
728
728
729 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
729 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
730
730
731 for nTries in range( self.nTries ):
731 for nTries in range( self.nTries ):
732 self.fp.close()
732 self.fp.close()
733 self.fp = open( self.filename, 'rb' )
733 self.fp = open( self.filename, 'rb' )
734 self.fp.seek( currentPointer )
734 self.fp.seek( currentPointer )
735
735
736 self.fileSize = os.path.getsize( self.filename )
736 self.fileSize = os.path.getsize( self.filename )
737 currentSize = self.fileSize - currentPointer
737 currentSize = self.fileSize - currentPointer
738
738
739 if ( currentSize >= neededSize ):
739 if ( currentSize >= neededSize ):
740 return 1
740 return 1
741
741
742 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
742 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
743 time.sleep( self.delay )
743 time.sleep( self.delay )
744
744
745 return 0
745 return 0
746
746
747
747
748 def __jumpToLastBlock(self):
748 def __jumpToLastBlock(self):
749
749
750 if not(self.__isFirstTimeOnline):
750 if not(self.__isFirstTimeOnline):
751 return
751 return
752
752
753 csize = self.fileSize - self.fp.tell()
753 csize = self.fileSize - self.fp.tell()
754 blocksize = self.processingHeaderObj.blockSize
754 blocksize = self.processingHeaderObj.blockSize
755
755
756 #salta el primer bloque de datos
756 #salta el primer bloque de datos
757 if csize > self.processingHeaderObj.blockSize:
757 if csize > self.processingHeaderObj.blockSize:
758 self.fp.seek(self.fp.tell() + blocksize)
758 self.fp.seek(self.fp.tell() + blocksize)
759 else:
759 else:
760 return
760 return
761
761
762 csize = self.fileSize - self.fp.tell()
762 csize = self.fileSize - self.fp.tell()
763 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
763 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
764 while True:
764 while True:
765
765
766 if self.fp.tell()<self.fileSize:
766 if self.fp.tell()<self.fileSize:
767 self.fp.seek(self.fp.tell() + neededsize)
767 self.fp.seek(self.fp.tell() + neededsize)
768 else:
768 else:
769 self.fp.seek(self.fp.tell() - neededsize)
769 self.fp.seek(self.fp.tell() - neededsize)
770 break
770 break
771
771
772 # csize = self.fileSize - self.fp.tell()
772 # csize = self.fileSize - self.fp.tell()
773 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
773 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
774 # factor = int(csize/neededsize)
774 # factor = int(csize/neededsize)
775 # if factor > 0:
775 # if factor > 0:
776 # self.fp.seek(self.fp.tell() + factor*neededsize)
776 # self.fp.seek(self.fp.tell() + factor*neededsize)
777
777
778 self.flagIsNewFile = 0
778 self.flagIsNewFile = 0
779 self.__isFirstTimeOnline = 0
779 self.__isFirstTimeOnline = 0
780
780
781
781
782 def __setNewBlock(self):
782 def __setNewBlock(self):
783
783
784 if self.fp == None:
784 if self.fp == None:
785 return 0
785 return 0
786
786
787 if self.online:
787 if self.online:
788 self.__jumpToLastBlock()
788 self.__jumpToLastBlock()
789
789
790 if self.flagIsNewFile:
790 if self.flagIsNewFile:
791 return 1
791 return 1
792
792
793 self.lastUTTime = self.basicHeaderObj.utc
793 self.lastUTTime = self.basicHeaderObj.utc
794 currentSize = self.fileSize - self.fp.tell()
794 currentSize = self.fileSize - self.fp.tell()
795 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
795 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
796
796
797 if (currentSize >= neededSize):
797 if (currentSize >= neededSize):
798 self.__rdBasicHeader()
798 self.__rdBasicHeader()
799 return 1
799 return 1
800
800
801 if self.__waitNewBlock():
801 if self.__waitNewBlock():
802 return 1
802 return 1
803
803
804 if not(self.setNextFile()):
804 if not(self.setNextFile()):
805 return 0
805 return 0
806
806
807 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
807 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
808
808
809 self.flagTimeBlock = 0
809 self.flagTimeBlock = 0
810
810
811 if deltaTime > self.maxTimeStep:
811 if deltaTime > self.maxTimeStep:
812 self.flagTimeBlock = 1
812 self.flagTimeBlock = 1
813
813
814 return 1
814 return 1
815
815
816
816
817 def readNextBlock(self):
817 def readNextBlock(self):
818 if not(self.__setNewBlock()):
818 if not(self.__setNewBlock()):
819 return 0
819 return 0
820
820
821 if not(self.readBlock()):
821 if not(self.readBlock()):
822 return 0
822 return 0
823
823
824 return 1
824 return 1
825
825
826 def __rdProcessingHeader(self, fp=None):
826 def __rdProcessingHeader(self, fp=None):
827 if fp == None:
827 if fp == None:
828 fp = self.fp
828 fp = self.fp
829
829
830 self.processingHeaderObj.read(fp)
830 self.processingHeaderObj.read(fp)
831
831
832 def __rdRadarControllerHeader(self, fp=None):
832 def __rdRadarControllerHeader(self, fp=None):
833 if fp == None:
833 if fp == None:
834 fp = self.fp
834 fp = self.fp
835
835
836 self.radarControllerHeaderObj.read(fp)
836 self.radarControllerHeaderObj.read(fp)
837
837
838 def __rdSystemHeader(self, fp=None):
838 def __rdSystemHeader(self, fp=None):
839 if fp == None:
839 if fp == None:
840 fp = self.fp
840 fp = self.fp
841
841
842 self.systemHeaderObj.read(fp)
842 self.systemHeaderObj.read(fp)
843
843
844 def __rdBasicHeader(self, fp=None):
844 def __rdBasicHeader(self, fp=None):
845 if fp == None:
845 if fp == None:
846 fp = self.fp
846 fp = self.fp
847
847
848 self.basicHeaderObj.read(fp)
848 self.basicHeaderObj.read(fp)
849
849
850
850
851 def __readFirstHeader(self):
851 def __readFirstHeader(self):
852 self.__rdBasicHeader()
852 self.__rdBasicHeader()
853 self.__rdSystemHeader()
853 self.__rdSystemHeader()
854 self.__rdRadarControllerHeader()
854 self.__rdRadarControllerHeader()
855 self.__rdProcessingHeader()
855 self.__rdProcessingHeader()
856
856
857 self.firstHeaderSize = self.basicHeaderObj.size
857 self.firstHeaderSize = self.basicHeaderObj.size
858
858
859 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
859 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
860 if datatype == 0:
860 if datatype == 0:
861 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
861 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
862 elif datatype == 1:
862 elif datatype == 1:
863 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
863 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
864 elif datatype == 2:
864 elif datatype == 2:
865 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
865 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
866 elif datatype == 3:
866 elif datatype == 3:
867 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
867 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
868 elif datatype == 4:
868 elif datatype == 4:
869 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
869 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
870 elif datatype == 5:
870 elif datatype == 5:
871 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
871 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
872 else:
872 else:
873 raise ValueError, 'Data type was not defined'
873 raise ValueError, 'Data type was not defined'
874
874
875 self.dtype = datatype_str
875 self.dtype = datatype_str
876 self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
876 self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
877 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
877 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
878 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
878 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
879 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
879 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
880 self.getBlockDimension()
880 self.getBlockDimension()
881
881
882
882
883 def __verifyFile(self, filename, msgFlag=True):
883 def __verifyFile(self, filename, msgFlag=True):
884 msg = None
884 msg = None
885 try:
885 try:
886 fp = open(filename, 'rb')
886 fp = open(filename, 'rb')
887 currentPosition = fp.tell()
887 currentPosition = fp.tell()
888 except:
888 except:
889 if msgFlag:
889 if msgFlag:
890 print "The file %s can't be opened" % (filename)
890 print "The file %s can't be opened" % (filename)
891 return False
891 return False
892
892
893 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
893 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
894
894
895 if neededSize == 0:
895 if neededSize == 0:
896 basicHeaderObj = BasicHeader(LOCALTIME)
896 basicHeaderObj = BasicHeader(LOCALTIME)
897 systemHeaderObj = SystemHeader()
897 systemHeaderObj = SystemHeader()
898 radarControllerHeaderObj = RadarControllerHeader()
898 radarControllerHeaderObj = RadarControllerHeader()
899 processingHeaderObj = ProcessingHeader()
899 processingHeaderObj = ProcessingHeader()
900
900
901 try:
901 try:
902 if not( basicHeaderObj.read(fp) ): raise IOError
902 if not( basicHeaderObj.read(fp) ): raise IOError
903 if not( systemHeaderObj.read(fp) ): raise IOError
903 if not( systemHeaderObj.read(fp) ): raise IOError
904 if not( radarControllerHeaderObj.read(fp) ): raise IOError
904 if not( radarControllerHeaderObj.read(fp) ): raise IOError
905 if not( processingHeaderObj.read(fp) ): raise IOError
905 if not( processingHeaderObj.read(fp) ): raise IOError
906 data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
906 data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
907
907
908 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
908 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
909
909
910 except:
910 except:
911 if msgFlag:
911 if msgFlag:
912 print "\tThe file %s is empty or it hasn't enough data" % filename
912 print "\tThe file %s is empty or it hasn't enough data" % filename
913
913
914 fp.close()
914 fp.close()
915 return False
915 return False
916 else:
916 else:
917 msg = "\tSkipping the file %s due to it hasn't enough data" %filename
917 msg = "\tSkipping the file %s due to it hasn't enough data" %filename
918
918
919 fp.close()
919 fp.close()
920 fileSize = os.path.getsize(filename)
920 fileSize = os.path.getsize(filename)
921 currentSize = fileSize - currentPosition
921 currentSize = fileSize - currentPosition
922 if currentSize < neededSize:
922 if currentSize < neededSize:
923 if msgFlag and (msg != None):
923 if msgFlag and (msg != None):
924 print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename
924 print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename
925 return False
925 return False
926
926
927 return True
927 return True
928
928
929 def setup(self,
929 def setup(self,
930 path=None,
930 path=None,
931 startDate=None,
931 startDate=None,
932 endDate=None,
932 endDate=None,
933 startTime=datetime.time(0,0,0),
933 startTime=datetime.time(0,0,0),
934 endTime=datetime.time(23,59,59),
934 endTime=datetime.time(23,59,59),
935 set=None,
935 set=None,
936 expLabel = "",
936 expLabel = "",
937 ext = None,
937 ext = None,
938 online = False,
938 online = False,
939 delay = 60,
939 delay = 60,
940 walk = True):
940 walk = True):
941
941
942 if path == None:
942 if path == None:
943 raise ValueError, "The path is not valid"
943 raise ValueError, "The path is not valid"
944
944
945 if ext == None:
945 if ext == None:
946 ext = self.ext
946 ext = self.ext
947
947
948 if online:
948 if online:
949 print "Searching files in online mode..."
949 print "Searching files in online mode..."
950
950
951 for nTries in range( self.nTries ):
951 for nTries in range( self.nTries ):
952 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
952 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
953
953
954 if fullpath:
954 if fullpath:
955 break
955 break
956
956
957 print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
957 print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
958 time.sleep( self.delay )
958 time.sleep( self.delay )
959
959
960 if not(fullpath):
960 if not(fullpath):
961 print "There 'isn't valied files in %s" % path
961 print "There 'isn't valied files in %s" % path
962 return None
962 return None
963
963
964 self.year = year
964 self.year = year
965 self.doy = doy
965 self.doy = doy
966 self.set = set - 1
966 self.set = set - 1
967 self.path = path
967 self.path = path
968 self.foldercounter = foldercounter
968 self.foldercounter = foldercounter
969
969
970 else:
970 else:
971 print "Searching files in offline mode ..."
971 print "Searching files in offline mode ..."
972 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
972 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
973 startTime=startTime, endTime=endTime,
973 startTime=startTime, endTime=endTime,
974 set=set, expLabel=expLabel, ext=ext,
974 set=set, expLabel=expLabel, ext=ext,
975 walk=walk)
975 walk=walk)
976
976
977 if not(pathList):
977 if not(pathList):
978 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
978 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
979 datetime.datetime.combine(startDate,startTime).ctime(),
979 datetime.datetime.combine(startDate,startTime).ctime(),
980 datetime.datetime.combine(endDate,endTime).ctime())
980 datetime.datetime.combine(endDate,endTime).ctime())
981
981
982 sys.exit(-1)
982 sys.exit(-1)
983
983
984
984
985 self.fileIndex = -1
985 self.fileIndex = -1
986 self.pathList = pathList
986 self.pathList = pathList
987 self.filenameList = filenameList
987 self.filenameList = filenameList
988
988
989 self.online = online
989 self.online = online
990 self.delay = delay
990 self.delay = delay
991 ext = ext.lower()
991 ext = ext.lower()
992 self.ext = ext
992 self.ext = ext
993
993
994 if not(self.setNextFile()):
994 if not(self.setNextFile()):
995 if (startDate!=None) and (endDate!=None):
995 if (startDate!=None) and (endDate!=None):
996 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
996 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
997 elif startDate != None:
997 elif startDate != None:
998 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
998 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
999 else:
999 else:
1000 print "No files"
1000 print "No files"
1001
1001
1002 sys.exit(-1)
1002 sys.exit(-1)
1003
1003
1004 # self.updateDataHeader()
1004 # self.updateDataHeader()
1005
1005
1006 return self.dataOut
1006 return self.dataOut
1007
1007
1008 def getBasicHeader(self):
1008 def getBasicHeader(self):
1009
1009
1010 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.ippSeconds
1010 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.ippSeconds
1011
1011
1012 self.dataOut.flagTimeBlock = self.flagTimeBlock
1012 self.dataOut.flagTimeBlock = self.flagTimeBlock
1013
1013
1014 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1014 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1015
1015
1016 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1016 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1017
1017
1018 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1018 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1019
1019
1020 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1020 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1021
1021
1022 def getFirstHeader(self):
1022 def getFirstHeader(self):
1023
1023
1024 raise ValueError, "This method has not been implemented"
1024 raise ValueError, "This method has not been implemented"
1025
1025
1026 def getData():
1026 def getData():
1027
1027
1028 raise ValueError, "This method has not been implemented"
1028 raise ValueError, "This method has not been implemented"
1029
1029
1030 def hasNotDataInBuffer():
1030 def hasNotDataInBuffer():
1031
1031
1032 raise ValueError, "This method has not been implemented"
1032 raise ValueError, "This method has not been implemented"
1033
1033
1034 def readBlock():
1034 def readBlock():
1035
1035
1036 raise ValueError, "This method has not been implemented"
1036 raise ValueError, "This method has not been implemented"
1037
1037
1038 def isEndProcess(self):
1038 def isEndProcess(self):
1039
1039
1040 return self.flagNoMoreFiles
1040 return self.flagNoMoreFiles
1041
1041
1042 def printReadBlocks(self):
1042 def printReadBlocks(self):
1043
1043
1044 print "Number of read blocks per file %04d" %self.nReadBlocks
1044 print "Number of read blocks per file %04d" %self.nReadBlocks
1045
1045
1046 def printTotalBlocks(self):
1046 def printTotalBlocks(self):
1047
1047
1048 print "Number of read blocks %04d" %self.nTotalBlocks
1048 print "Number of read blocks %04d" %self.nTotalBlocks
1049
1049
1050 def printNumberOfBlock(self):
1050 def printNumberOfBlock(self):
1051
1051
1052 if self.flagIsNewBlock:
1052 if self.flagIsNewBlock:
1053 print "Block No. %04d, Total blocks %04d -> %s" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks, self.dataOut.datatime.ctime())
1053 print "Block No. %04d, Total blocks %04d -> %s" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks, self.dataOut.datatime.ctime())
1054
1054
1055 def printInfo(self):
1055 def printInfo(self):
1056
1056
1057 if self.__printInfo == False:
1057 if self.__printInfo == False:
1058 return
1058 return
1059
1059
1060 self.basicHeaderObj.printInfo()
1060 self.basicHeaderObj.printInfo()
1061 self.systemHeaderObj.printInfo()
1061 self.systemHeaderObj.printInfo()
1062 self.radarControllerHeaderObj.printInfo()
1062 self.radarControllerHeaderObj.printInfo()
1063 self.processingHeaderObj.printInfo()
1063 self.processingHeaderObj.printInfo()
1064
1064
1065 self.__printInfo = False
1065 self.__printInfo = False
1066
1066
1067
1067
1068 def run(self, **kwargs):
1068 def run(self, **kwargs):
1069
1069
1070 if not(self.isConfig):
1070 if not(self.isConfig):
1071
1071
1072 # self.dataOut = dataOut
1072 # self.dataOut = dataOut
1073 self.setup(**kwargs)
1073 self.setup(**kwargs)
1074 self.isConfig = True
1074 self.isConfig = True
1075
1075
1076 self.getData()
1076 self.getData()
1077
1077
1078 class JRODataWriter(JRODataIO, Operation):
1078 class JRODataWriter(JRODataIO, Operation):
1079
1079
1080 """
1080 """
1081 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1081 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1082 de los datos siempre se realiza por bloques.
1082 de los datos siempre se realiza por bloques.
1083 """
1083 """
1084
1084
1085 blockIndex = 0
1085 blockIndex = 0
1086
1086
1087 path = None
1087 path = None
1088
1088
1089 setFile = None
1089 setFile = None
1090
1090
1091 profilesPerBlock = None
1091 profilesPerBlock = None
1092
1092
1093 blocksPerFile = None
1093 blocksPerFile = None
1094
1094
1095 nWriteBlocks = 0
1095 nWriteBlocks = 0
1096
1096
1097 def __init__(self, dataOut=None):
1097 def __init__(self, dataOut=None):
1098 raise ValueError, "Not implemented"
1098 raise ValueError, "Not implemented"
1099
1099
1100
1100
1101 def hasAllDataInBuffer(self):
1101 def hasAllDataInBuffer(self):
1102 raise ValueError, "Not implemented"
1102 raise ValueError, "Not implemented"
1103
1103
1104
1104
1105 def setBlockDimension(self):
1105 def setBlockDimension(self):
1106 raise ValueError, "Not implemented"
1106 raise ValueError, "Not implemented"
1107
1107
1108
1108
1109 def writeBlock(self):
1109 def writeBlock(self):
1110 raise ValueError, "No implemented"
1110 raise ValueError, "No implemented"
1111
1111
1112
1112
1113 def putData(self):
1113 def putData(self):
1114 raise ValueError, "No implemented"
1114 raise ValueError, "No implemented"
1115
1115
1116
1116
1117 def setBasicHeader(self):
1117 def setBasicHeader(self):
1118
1118
1119 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1119 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1120 self.basicHeaderObj.version = self.versionFile
1120 self.basicHeaderObj.version = self.versionFile
1121 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1121 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1122
1122
1123 utc = numpy.floor(self.dataOut.utctime)
1123 utc = numpy.floor(self.dataOut.utctime)
1124 milisecond = (self.dataOut.utctime - utc)* 1000.0
1124 milisecond = (self.dataOut.utctime - utc)* 1000.0
1125
1125
1126 self.basicHeaderObj.utc = utc
1126 self.basicHeaderObj.utc = utc
1127 self.basicHeaderObj.miliSecond = milisecond
1127 self.basicHeaderObj.miliSecond = milisecond
1128 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1128 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1129 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1129 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1130 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1130 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1131
1131
1132 def setFirstHeader(self):
1132 def setFirstHeader(self):
1133 """
1133 """
1134 Obtiene una copia del First Header
1134 Obtiene una copia del First Header
1135
1135
1136 Affected:
1136 Affected:
1137
1137
1138 self.basicHeaderObj
1138 self.basicHeaderObj
1139 self.systemHeaderObj
1139 self.systemHeaderObj
1140 self.radarControllerHeaderObj
1140 self.radarControllerHeaderObj
1141 self.processingHeaderObj self.
1141 self.processingHeaderObj self.
1142
1142
1143 Return:
1143 Return:
1144 None
1144 None
1145 """
1145 """
1146
1146
1147 raise ValueError, "No implemented"
1147 raise ValueError, "No implemented"
1148
1148
1149 def __writeFirstHeader(self):
1149 def __writeFirstHeader(self):
1150 """
1150 """
1151 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1151 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1152
1152
1153 Affected:
1153 Affected:
1154 __dataType
1154 __dataType
1155
1155
1156 Return:
1156 Return:
1157 None
1157 None
1158 """
1158 """
1159
1159
1160 # CALCULAR PARAMETROS
1160 # CALCULAR PARAMETROS
1161
1161
1162 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1162 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1163 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1163 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1164
1164
1165 self.basicHeaderObj.write(self.fp)
1165 self.basicHeaderObj.write(self.fp)
1166 self.systemHeaderObj.write(self.fp)
1166 self.systemHeaderObj.write(self.fp)
1167 self.radarControllerHeaderObj.write(self.fp)
1167 self.radarControllerHeaderObj.write(self.fp)
1168 self.processingHeaderObj.write(self.fp)
1168 self.processingHeaderObj.write(self.fp)
1169
1169
1170 self.dtype = self.dataOut.dtype
1170 self.dtype = self.dataOut.dtype
1171
1171
1172 def __setNewBlock(self):
1172 def __setNewBlock(self):
1173 """
1173 """
1174 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1174 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1175
1175
1176 Return:
1176 Return:
1177 0 : si no pudo escribir nada
1177 0 : si no pudo escribir nada
1178 1 : Si escribio el Basic el First Header
1178 1 : Si escribio el Basic el First Header
1179 """
1179 """
1180 if self.fp == None:
1180 if self.fp == None:
1181 self.setNextFile()
1181 self.setNextFile()
1182
1182
1183 if self.flagIsNewFile:
1183 if self.flagIsNewFile:
1184 return 1
1184 return 1
1185
1185
1186 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1186 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1187 self.basicHeaderObj.write(self.fp)
1187 self.basicHeaderObj.write(self.fp)
1188 return 1
1188 return 1
1189
1189
1190 if not( self.setNextFile() ):
1190 if not( self.setNextFile() ):
1191 return 0
1191 return 0
1192
1192
1193 return 1
1193 return 1
1194
1194
1195
1195
1196 def writeNextBlock(self):
1196 def writeNextBlock(self):
1197 """
1197 """
1198 Selecciona el bloque siguiente de datos y los escribe en un file
1198 Selecciona el bloque siguiente de datos y los escribe en un file
1199
1199
1200 Return:
1200 Return:
1201 0 : Si no hizo pudo escribir el bloque de datos
1201 0 : Si no hizo pudo escribir el bloque de datos
1202 1 : Si no pudo escribir el bloque de datos
1202 1 : Si no pudo escribir el bloque de datos
1203 """
1203 """
1204 if not( self.__setNewBlock() ):
1204 if not( self.__setNewBlock() ):
1205 return 0
1205 return 0
1206
1206
1207 self.writeBlock()
1207 self.writeBlock()
1208
1208
1209 return 1
1209 return 1
1210
1210
1211 def setNextFile(self):
1211 def setNextFile(self):
1212 """
1212 """
1213 Determina el siguiente file que sera escrito
1213 Determina el siguiente file que sera escrito
1214
1214
1215 Affected:
1215 Affected:
1216 self.filename
1216 self.filename
1217 self.subfolder
1217 self.subfolder
1218 self.fp
1218 self.fp
1219 self.setFile
1219 self.setFile
1220 self.flagIsNewFile
1220 self.flagIsNewFile
1221
1221
1222 Return:
1222 Return:
1223 0 : Si el archivo no puede ser escrito
1223 0 : Si el archivo no puede ser escrito
1224 1 : Si el archivo esta listo para ser escrito
1224 1 : Si el archivo esta listo para ser escrito
1225 """
1225 """
1226 ext = self.ext
1226 ext = self.ext
1227 path = self.path
1227 path = self.path
1228
1228
1229 if self.fp != None:
1229 if self.fp != None:
1230 self.fp.close()
1230 self.fp.close()
1231
1231
1232 timeTuple = time.localtime( self.dataOut.utctime)
1232 timeTuple = time.localtime( self.dataOut.utctime)
1233 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1233 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1234
1234
1235 fullpath = os.path.join( path, subfolder )
1235 fullpath = os.path.join( path, subfolder )
1236 if not( os.path.exists(fullpath) ):
1236 if not( os.path.exists(fullpath) ):
1237 os.mkdir(fullpath)
1237 os.mkdir(fullpath)
1238 self.setFile = -1 #inicializo mi contador de seteo
1238 self.setFile = -1 #inicializo mi contador de seteo
1239 else:
1239 else:
1240 filesList = os.listdir( fullpath )
1240 filesList = os.listdir( fullpath )
1241 if len( filesList ) > 0:
1241 if len( filesList ) > 0:
1242 filesList = sorted( filesList, key=str.lower )
1242 filesList = sorted( filesList, key=str.lower )
1243 filen = filesList[-1]
1243 filen = filesList[-1]
1244 # el filename debera tener el siguiente formato
1244 # el filename debera tener el siguiente formato
1245 # 0 1234 567 89A BCDE (hex)
1245 # 0 1234 567 89A BCDE (hex)
1246 # x YYYY DDD SSS .ext
1246 # x YYYY DDD SSS .ext
1247 if isNumber( filen[8:11] ):
1247 if isNumber( filen[8:11] ):
1248 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1248 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1249 else:
1249 else:
1250 self.setFile = -1
1250 self.setFile = -1
1251 else:
1251 else:
1252 self.setFile = -1 #inicializo mi contador de seteo
1252 self.setFile = -1 #inicializo mi contador de seteo
1253
1253
1254 setFile = self.setFile
1254 setFile = self.setFile
1255 setFile += 1
1255 setFile += 1
1256
1256
1257 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
1257 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
1258 timeTuple.tm_year,
1258 timeTuple.tm_year,
1259 timeTuple.tm_yday,
1259 timeTuple.tm_yday,
1260 setFile,
1260 setFile,
1261 ext )
1261 ext )
1262
1262
1263 filename = os.path.join( path, subfolder, file )
1263 filename = os.path.join( path, subfolder, file )
1264
1264
1265 fp = open( filename,'wb' )
1265 fp = open( filename,'wb' )
1266
1266
1267 self.blockIndex = 0
1267 self.blockIndex = 0
1268
1268
1269 #guardando atributos
1269 #guardando atributos
1270 self.filename = filename
1270 self.filename = filename
1271 self.subfolder = subfolder
1271 self.subfolder = subfolder
1272 self.fp = fp
1272 self.fp = fp
1273 self.setFile = setFile
1273 self.setFile = setFile
1274 self.flagIsNewFile = 1
1274 self.flagIsNewFile = 1
1275
1275
1276 self.setFirstHeader()
1276 self.setFirstHeader()
1277
1277
1278 print 'Writing the file: %s'%self.filename
1278 print 'Writing the file: %s'%self.filename
1279
1279
1280 self.__writeFirstHeader()
1280 self.__writeFirstHeader()
1281
1281
1282 return 1
1282 return 1
1283
1283
1284 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=0, ext=None):
1284 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=0, ext=None):
1285 """
1285 """
1286 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1286 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1287
1287
1288 Inputs:
1288 Inputs:
1289 path : el path destino en el cual se escribiran los files a crear
1289 path : el path destino en el cual se escribiran los files a crear
1290 format : formato en el cual sera salvado un file
1290 format : formato en el cual sera salvado un file
1291 set : el setebo del file
1291 set : el setebo del file
1292
1292
1293 Return:
1293 Return:
1294 0 : Si no realizo un buen seteo
1294 0 : Si no realizo un buen seteo
1295 1 : Si realizo un buen seteo
1295 1 : Si realizo un buen seteo
1296 """
1296 """
1297
1297
1298 if ext == None:
1298 if ext == None:
1299 ext = self.ext
1299 ext = self.ext
1300
1300
1301 ext = ext.lower()
1301 ext = ext.lower()
1302
1302
1303 self.ext = ext
1303 self.ext = ext
1304
1304
1305 self.path = path
1305 self.path = path
1306
1306
1307 self.setFile = set - 1
1307 self.setFile = set - 1
1308
1308
1309 self.blocksPerFile = blocksPerFile
1309 self.blocksPerFile = blocksPerFile
1310
1310
1311 self.profilesPerBlock = profilesPerBlock
1311 self.profilesPerBlock = profilesPerBlock
1312
1312
1313 self.dataOut = dataOut
1313 self.dataOut = dataOut
1314
1314
1315 if not(self.setNextFile()):
1315 if not(self.setNextFile()):
1316 print "There isn't a next file"
1316 print "There isn't a next file"
1317 return 0
1317 return 0
1318
1318
1319 self.setBlockDimension()
1319 self.setBlockDimension()
1320
1320
1321 return 1
1321 return 1
1322
1322
1323 def run(self, dataOut, **kwargs):
1323 def run(self, dataOut, **kwargs):
1324
1324
1325 if not(self.isConfig):
1325 if not(self.isConfig):
1326
1326
1327 self.setup(dataOut, **kwargs)
1327 self.setup(dataOut, **kwargs)
1328 self.isConfig = True
1328 self.isConfig = True
1329
1329
1330 self.putData()
1330 self.putData()
1331
1331
1332 class VoltageReader(JRODataReader):
1332 class VoltageReader(JRODataReader):
1333 """
1333 """
1334 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
1334 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
1335 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
1335 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
1336 perfiles*alturas*canales) son almacenados en la variable "buffer".
1336 perfiles*alturas*canales) son almacenados en la variable "buffer".
1337
1337
1338 perfiles * alturas * canales
1338 perfiles * alturas * canales
1339
1339
1340 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1340 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1341 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
1341 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
1342 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
1342 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
1343 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1343 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1344
1344
1345 Example:
1345 Example:
1346
1346
1347 dpath = "/home/myuser/data"
1347 dpath = "/home/myuser/data"
1348
1348
1349 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1349 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1350
1350
1351 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1351 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1352
1352
1353 readerObj = VoltageReader()
1353 readerObj = VoltageReader()
1354
1354
1355 readerObj.setup(dpath, startTime, endTime)
1355 readerObj.setup(dpath, startTime, endTime)
1356
1356
1357 while(True):
1357 while(True):
1358
1358
1359 #to get one profile
1359 #to get one profile
1360 profile = readerObj.getData()
1360 profile = readerObj.getData()
1361
1361
1362 #print the profile
1362 #print the profile
1363 print profile
1363 print profile
1364
1364
1365 #If you want to see all datablock
1365 #If you want to see all datablock
1366 print readerObj.datablock
1366 print readerObj.datablock
1367
1367
1368 if readerObj.flagNoMoreFiles:
1368 if readerObj.flagNoMoreFiles:
1369 break
1369 break
1370
1370
1371 """
1371 """
1372
1372
1373 ext = ".r"
1373 ext = ".r"
1374
1374
1375 optchar = "D"
1375 optchar = "D"
1376 dataOut = None
1376 dataOut = None
1377
1377
1378
1378
1379 def __init__(self):
1379 def __init__(self):
1380 """
1380 """
1381 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
1381 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
1382
1382
1383 Input:
1383 Input:
1384 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
1384 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
1385 almacenar un perfil de datos cada vez que se haga un requerimiento
1385 almacenar un perfil de datos cada vez que se haga un requerimiento
1386 (getData). El perfil sera obtenido a partir del buffer de datos,
1386 (getData). El perfil sera obtenido a partir del buffer de datos,
1387 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1387 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1388 bloque de datos.
1388 bloque de datos.
1389 Si este parametro no es pasado se creara uno internamente.
1389 Si este parametro no es pasado se creara uno internamente.
1390
1390
1391 Variables afectadas:
1391 Variables afectadas:
1392 self.dataOut
1392 self.dataOut
1393
1393
1394 Return:
1394 Return:
1395 None
1395 None
1396 """
1396 """
1397
1397
1398 self.isConfig = False
1398 self.isConfig = False
1399
1399
1400 self.datablock = None
1400 self.datablock = None
1401
1401
1402 self.utc = 0
1402 self.utc = 0
1403
1403
1404 self.ext = ".r"
1404 self.ext = ".r"
1405
1405
1406 self.optchar = "D"
1406 self.optchar = "D"
1407
1407
1408 self.basicHeaderObj = BasicHeader(LOCALTIME)
1408 self.basicHeaderObj = BasicHeader(LOCALTIME)
1409
1409
1410 self.systemHeaderObj = SystemHeader()
1410 self.systemHeaderObj = SystemHeader()
1411
1411
1412 self.radarControllerHeaderObj = RadarControllerHeader()
1412 self.radarControllerHeaderObj = RadarControllerHeader()
1413
1413
1414 self.processingHeaderObj = ProcessingHeader()
1414 self.processingHeaderObj = ProcessingHeader()
1415
1415
1416 self.online = 0
1416 self.online = 0
1417
1417
1418 self.fp = None
1418 self.fp = None
1419
1419
1420 self.idFile = None
1420 self.idFile = None
1421
1421
1422 self.dtype = None
1422 self.dtype = None
1423
1423
1424 self.fileSizeByHeader = None
1424 self.fileSizeByHeader = None
1425
1425
1426 self.filenameList = []
1426 self.filenameList = []
1427
1427
1428 self.filename = None
1428 self.filename = None
1429
1429
1430 self.fileSize = None
1430 self.fileSize = None
1431
1431
1432 self.firstHeaderSize = 0
1432 self.firstHeaderSize = 0
1433
1433
1434 self.basicHeaderSize = 24
1434 self.basicHeaderSize = 24
1435
1435
1436 self.pathList = []
1436 self.pathList = []
1437
1437
1438 self.filenameList = []
1438 self.filenameList = []
1439
1439
1440 self.lastUTTime = 0
1440 self.lastUTTime = 0
1441
1441
1442 self.maxTimeStep = 30
1442 self.maxTimeStep = 30
1443
1443
1444 self.flagNoMoreFiles = 0
1444 self.flagNoMoreFiles = 0
1445
1445
1446 self.set = 0
1446 self.set = 0
1447
1447
1448 self.path = None
1448 self.path = None
1449
1449
1450 self.profileIndex = 2**32-1
1450 self.profileIndex = 2**32-1
1451
1451
1452 self.delay = 3 #seconds
1452 self.delay = 3 #seconds
1453
1453
1454 self.nTries = 3 #quantity tries
1454 self.nTries = 3 #quantity tries
1455
1455
1456 self.nFiles = 3 #number of files for searching
1456 self.nFiles = 3 #number of files for searching
1457
1457
1458 self.nReadBlocks = 0
1458 self.nReadBlocks = 0
1459
1459
1460 self.flagIsNewFile = 1
1460 self.flagIsNewFile = 1
1461
1461
1462 self.__isFirstTimeOnline = 1
1462 self.__isFirstTimeOnline = 1
1463
1463
1464 self.ippSeconds = 0
1464 self.ippSeconds = 0
1465
1465
1466 self.flagTimeBlock = 0
1466 self.flagTimeBlock = 0
1467
1467
1468 self.flagIsNewBlock = 0
1468 self.flagIsNewBlock = 0
1469
1469
1470 self.nTotalBlocks = 0
1470 self.nTotalBlocks = 0
1471
1471
1472 self.blocksize = 0
1472 self.blocksize = 0
1473
1473
1474 self.dataOut = self.createObjByDefault()
1474 self.dataOut = self.createObjByDefault()
1475
1475
1476 def createObjByDefault(self):
1476 def createObjByDefault(self):
1477
1477
1478 dataObj = Voltage()
1478 dataObj = Voltage()
1479
1479
1480 return dataObj
1480 return dataObj
1481
1481
1482 def __hasNotDataInBuffer(self):
1482 def __hasNotDataInBuffer(self):
1483 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1483 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1484 return 1
1484 return 1
1485 return 0
1485 return 0
1486
1486
1487
1487
1488 def getBlockDimension(self):
1488 def getBlockDimension(self):
1489 """
1489 """
1490 Obtiene la cantidad de puntos a leer por cada bloque de datos
1490 Obtiene la cantidad de puntos a leer por cada bloque de datos
1491
1491
1492 Affected:
1492 Affected:
1493 self.blocksize
1493 self.blocksize
1494
1494
1495 Return:
1495 Return:
1496 None
1496 None
1497 """
1497 """
1498 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
1498 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
1499 self.blocksize = pts2read
1499 self.blocksize = pts2read
1500
1500
1501
1501
1502 def readBlock(self):
1502 def readBlock(self):
1503 """
1503 """
1504 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
1504 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
1505 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1505 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1506 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1506 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1507 es seteado a 0
1507 es seteado a 0
1508
1508
1509 Inputs:
1509 Inputs:
1510 None
1510 None
1511
1511
1512 Return:
1512 Return:
1513 None
1513 None
1514
1514
1515 Affected:
1515 Affected:
1516 self.profileIndex
1516 self.profileIndex
1517 self.datablock
1517 self.datablock
1518 self.flagIsNewFile
1518 self.flagIsNewFile
1519 self.flagIsNewBlock
1519 self.flagIsNewBlock
1520 self.nTotalBlocks
1520 self.nTotalBlocks
1521
1521
1522 Exceptions:
1522 Exceptions:
1523 Si un bloque leido no es un bloque valido
1523 Si un bloque leido no es un bloque valido
1524 """
1524 """
1525 current_pointer_location = self.fp.tell()
1525 current_pointer_location = self.fp.tell()
1526 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
1526 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
1527
1527
1528 try:
1528 try:
1529 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
1529 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
1530 except:
1530 except:
1531 #print "The read block (%3d) has not enough data" %self.nReadBlocks
1531 #print "The read block (%3d) has not enough data" %self.nReadBlocks
1532
1532
1533 if self.waitDataBlock(pointer_location=current_pointer_location):
1533 if self.waitDataBlock(pointer_location=current_pointer_location):
1534 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
1534 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
1535 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
1535 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
1536 # return 0
1536 # return 0
1537
1537
1538 junk = numpy.transpose(junk, (2,0,1))
1538 junk = numpy.transpose(junk, (2,0,1))
1539 self.datablock = junk['real'] + junk['imag']*1j
1539 self.datablock = junk['real'] + junk['imag']*1j
1540
1540
1541 self.profileIndex = 0
1541 self.profileIndex = 0
1542
1542
1543 self.flagIsNewFile = 0
1543 self.flagIsNewFile = 0
1544 self.flagIsNewBlock = 1
1544 self.flagIsNewBlock = 1
1545
1545
1546 self.nTotalBlocks += 1
1546 self.nTotalBlocks += 1
1547 self.nReadBlocks += 1
1547 self.nReadBlocks += 1
1548
1548
1549 return 1
1549 return 1
1550
1550
1551 def getFirstHeader(self):
1551 def getFirstHeader(self):
1552
1552
1553 self.dataOut.dtype = self.dtype
1553 self.dataOut.dtype = self.dtype
1554
1554
1555 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
1555 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
1556
1556
1557 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
1557 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
1558
1558
1559 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
1559 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
1560
1560
1561 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
1561 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
1562
1562
1563 self.dataOut.ippSeconds = self.ippSeconds
1563 self.dataOut.ippSeconds = self.ippSeconds
1564
1564
1565 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt
1565 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt
1566
1566
1567 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
1567 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
1568
1568
1569 self.dataOut.flagShiftFFT = False
1569 self.dataOut.flagShiftFFT = False
1570
1570
1571 if self.radarControllerHeaderObj.code != None:
1571 if self.radarControllerHeaderObj.code != None:
1572
1572
1573 self.dataOut.nCode = self.radarControllerHeaderObj.nCode
1573 self.dataOut.nCode = self.radarControllerHeaderObj.nCode
1574
1574
1575 self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
1575 self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
1576
1576
1577 self.dataOut.code = self.radarControllerHeaderObj.code
1577 self.dataOut.code = self.radarControllerHeaderObj.code
1578
1578
1579 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
1579 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
1580
1580
1581 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
1581 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
1582
1582
1583 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
1583 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
1584
1584
1585 self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip
1585 self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip
1586
1586
1587 self.dataOut.flagShiftFFT = False
1587 self.dataOut.flagShiftFFT = False
1588
1588
1589 def getData(self):
1589 def getData(self):
1590 """
1590 """
1591 getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage"
1591 getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage"
1592 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
1592 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
1593 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
1593 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
1594
1594
1595 Ademas incrementa el contador del buffer en 1.
1595 Ademas incrementa el contador del buffer en 1.
1596
1596
1597 Return:
1597 Return:
1598 data : retorna un perfil de voltages (alturas * canales) copiados desde el
1598 data : retorna un perfil de voltages (alturas * canales) copiados desde el
1599 buffer. Si no hay mas archivos a leer retorna None.
1599 buffer. Si no hay mas archivos a leer retorna None.
1600
1600
1601 Variables afectadas:
1601 Variables afectadas:
1602 self.dataOut
1602 self.dataOut
1603 self.profileIndex
1603 self.profileIndex
1604
1604
1605 Affected:
1605 Affected:
1606 self.dataOut
1606 self.dataOut
1607 self.profileIndex
1607 self.profileIndex
1608 self.flagTimeBlock
1608 self.flagTimeBlock
1609 self.flagIsNewBlock
1609 self.flagIsNewBlock
1610 """
1610 """
1611
1611
1612 if self.flagNoMoreFiles:
1612 if self.flagNoMoreFiles:
1613 self.dataOut.flagNoData = True
1613 self.dataOut.flagNoData = True
1614 print 'Process finished'
1614 print 'Process finished'
1615 return 0
1615 return 0
1616
1616
1617 self.flagTimeBlock = 0
1617 self.flagTimeBlock = 0
1618 self.flagIsNewBlock = 0
1618 self.flagIsNewBlock = 0
1619
1619
1620 if self.__hasNotDataInBuffer():
1620 if self.__hasNotDataInBuffer():
1621
1621
1622 if not( self.readNextBlock() ):
1622 if not( self.readNextBlock() ):
1623 return 0
1623 return 0
1624
1624
1625 self.getFirstHeader()
1625 self.getFirstHeader()
1626
1626
1627 if self.datablock == None:
1627 if self.datablock == None:
1628 self.dataOut.flagNoData = True
1628 self.dataOut.flagNoData = True
1629 return 0
1629 return 0
1630
1630
1631 self.dataOut.data = self.datablock[:,self.profileIndex,:]
1631 self.dataOut.data = self.datablock[:,self.profileIndex,:]
1632
1632
1633 self.dataOut.flagNoData = False
1633 self.dataOut.flagNoData = False
1634
1634
1635 self.getBasicHeader()
1635 self.getBasicHeader()
1636
1636
1637 self.profileIndex += 1
1637 self.profileIndex += 1
1638
1638
1639 self.dataOut.realtime = self.online
1639 self.dataOut.realtime = self.online
1640
1640
1641 return self.dataOut.data
1641 return self.dataOut.data
1642
1642
1643
1643
1644 class VoltageWriter(JRODataWriter):
1644 class VoltageWriter(JRODataWriter):
1645 """
1645 """
1646 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
1646 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
1647 de los datos siempre se realiza por bloques.
1647 de los datos siempre se realiza por bloques.
1648 """
1648 """
1649
1649
1650 ext = ".r"
1650 ext = ".r"
1651
1651
1652 optchar = "D"
1652 optchar = "D"
1653
1653
1654 shapeBuffer = None
1654 shapeBuffer = None
1655
1655
1656
1656
1657 def __init__(self):
1657 def __init__(self):
1658 """
1658 """
1659 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
1659 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
1660
1660
1661 Affected:
1661 Affected:
1662 self.dataOut
1662 self.dataOut
1663
1663
1664 Return: None
1664 Return: None
1665 """
1665 """
1666
1666
1667 self.nTotalBlocks = 0
1667 self.nTotalBlocks = 0
1668
1668
1669 self.profileIndex = 0
1669 self.profileIndex = 0
1670
1670
1671 self.isConfig = False
1671 self.isConfig = False
1672
1672
1673 self.fp = None
1673 self.fp = None
1674
1674
1675 self.flagIsNewFile = 1
1675 self.flagIsNewFile = 1
1676
1676
1677 self.nTotalBlocks = 0
1677 self.nTotalBlocks = 0
1678
1678
1679 self.flagIsNewBlock = 0
1679 self.flagIsNewBlock = 0
1680
1680
1681 self.setFile = None
1681 self.setFile = None
1682
1682
1683 self.dtype = None
1683 self.dtype = None
1684
1684
1685 self.path = None
1685 self.path = None
1686
1686
1687 self.filename = None
1687 self.filename = None
1688
1688
1689 self.basicHeaderObj = BasicHeader(LOCALTIME)
1689 self.basicHeaderObj = BasicHeader(LOCALTIME)
1690
1690
1691 self.systemHeaderObj = SystemHeader()
1691 self.systemHeaderObj = SystemHeader()
1692
1692
1693 self.radarControllerHeaderObj = RadarControllerHeader()
1693 self.radarControllerHeaderObj = RadarControllerHeader()
1694
1694
1695 self.processingHeaderObj = ProcessingHeader()
1695 self.processingHeaderObj = ProcessingHeader()
1696
1696
1697 def hasAllDataInBuffer(self):
1697 def hasAllDataInBuffer(self):
1698 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1698 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1699 return 1
1699 return 1
1700 return 0
1700 return 0
1701
1701
1702
1702
1703 def setBlockDimension(self):
1703 def setBlockDimension(self):
1704 """
1704 """
1705 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
1705 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
1706
1706
1707 Affected:
1707 Affected:
1708 self.shape_spc_Buffer
1708 self.shape_spc_Buffer
1709 self.shape_cspc_Buffer
1709 self.shape_cspc_Buffer
1710 self.shape_dc_Buffer
1710 self.shape_dc_Buffer
1711
1711
1712 Return: None
1712 Return: None
1713 """
1713 """
1714 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
1714 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
1715 self.processingHeaderObj.nHeights,
1715 self.processingHeaderObj.nHeights,
1716 self.systemHeaderObj.nChannels)
1716 self.systemHeaderObj.nChannels)
1717
1717
1718 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
1718 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
1719 self.processingHeaderObj.profilesPerBlock,
1719 self.processingHeaderObj.profilesPerBlock,
1720 self.processingHeaderObj.nHeights),
1720 self.processingHeaderObj.nHeights),
1721 dtype=numpy.dtype('complex64'))
1721 dtype=numpy.dtype('complex64'))
1722
1722
1723
1723
1724 def writeBlock(self):
1724 def writeBlock(self):
1725 """
1725 """
1726 Escribe el buffer en el file designado
1726 Escribe el buffer en el file designado
1727
1727
1728 Affected:
1728 Affected:
1729 self.profileIndex
1729 self.profileIndex
1730 self.flagIsNewFile
1730 self.flagIsNewFile
1731 self.flagIsNewBlock
1731 self.flagIsNewBlock
1732 self.nTotalBlocks
1732 self.nTotalBlocks
1733 self.blockIndex
1733 self.blockIndex
1734
1734
1735 Return: None
1735 Return: None
1736 """
1736 """
1737 data = numpy.zeros( self.shapeBuffer, self.dtype )
1737 data = numpy.zeros( self.shapeBuffer, self.dtype )
1738
1738
1739 junk = numpy.transpose(self.datablock, (1,2,0))
1739 junk = numpy.transpose(self.datablock, (1,2,0))
1740
1740
1741 data['real'] = junk.real
1741 data['real'] = junk.real
1742 data['imag'] = junk.imag
1742 data['imag'] = junk.imag
1743
1743
1744 data = data.reshape( (-1) )
1744 data = data.reshape( (-1) )
1745
1745
1746 data.tofile( self.fp )
1746 data.tofile( self.fp )
1747
1747
1748 self.datablock.fill(0)
1748 self.datablock.fill(0)
1749
1749
1750 self.profileIndex = 0
1750 self.profileIndex = 0
1751 self.flagIsNewFile = 0
1751 self.flagIsNewFile = 0
1752 self.flagIsNewBlock = 1
1752 self.flagIsNewBlock = 1
1753
1753
1754 self.blockIndex += 1
1754 self.blockIndex += 1
1755 self.nTotalBlocks += 1
1755 self.nTotalBlocks += 1
1756
1756
1757 def putData(self):
1757 def putData(self):
1758 """
1758 """
1759 Setea un bloque de datos y luego los escribe en un file
1759 Setea un bloque de datos y luego los escribe en un file
1760
1760
1761 Affected:
1761 Affected:
1762 self.flagIsNewBlock
1762 self.flagIsNewBlock
1763 self.profileIndex
1763 self.profileIndex
1764
1764
1765 Return:
1765 Return:
1766 0 : Si no hay data o no hay mas files que puedan escribirse
1766 0 : Si no hay data o no hay mas files que puedan escribirse
1767 1 : Si se escribio la data de un bloque en un file
1767 1 : Si se escribio la data de un bloque en un file
1768 """
1768 """
1769 if self.dataOut.flagNoData:
1769 if self.dataOut.flagNoData:
1770 return 0
1770 return 0
1771
1771
1772 self.flagIsNewBlock = 0
1772 self.flagIsNewBlock = 0
1773
1773
1774 if self.dataOut.flagTimeBlock:
1774 if self.dataOut.flagTimeBlock:
1775
1775
1776 self.datablock.fill(0)
1776 self.datablock.fill(0)
1777 self.profileIndex = 0
1777 self.profileIndex = 0
1778 self.setNextFile()
1778 self.setNextFile()
1779
1779
1780 if self.profileIndex == 0:
1780 if self.profileIndex == 0:
1781 self.setBasicHeader()
1781 self.setBasicHeader()
1782
1782
1783 self.datablock[:,self.profileIndex,:] = self.dataOut.data
1783 self.datablock[:,self.profileIndex,:] = self.dataOut.data
1784
1784
1785 self.profileIndex += 1
1785 self.profileIndex += 1
1786
1786
1787 if self.hasAllDataInBuffer():
1787 if self.hasAllDataInBuffer():
1788 #if self.flagIsNewFile:
1788 #if self.flagIsNewFile:
1789 self.writeNextBlock()
1789 self.writeNextBlock()
1790 # self.setFirstHeader()
1790 # self.setFirstHeader()
1791
1791
1792 return 1
1792 return 1
1793
1793
1794 def __getProcessFlags(self):
1794 def __getProcessFlags(self):
1795
1795
1796 processFlags = 0
1796 processFlags = 0
1797
1797
1798 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1798 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1799 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1799 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1800 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1800 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1801 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1801 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1802 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1802 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1803 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1803 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1804
1804
1805 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1805 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1806
1806
1807
1807
1808
1808
1809 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
1809 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
1810 PROCFLAG.DATATYPE_SHORT,
1810 PROCFLAG.DATATYPE_SHORT,
1811 PROCFLAG.DATATYPE_LONG,
1811 PROCFLAG.DATATYPE_LONG,
1812 PROCFLAG.DATATYPE_INT64,
1812 PROCFLAG.DATATYPE_INT64,
1813 PROCFLAG.DATATYPE_FLOAT,
1813 PROCFLAG.DATATYPE_FLOAT,
1814 PROCFLAG.DATATYPE_DOUBLE]
1814 PROCFLAG.DATATYPE_DOUBLE]
1815
1815
1816
1816
1817 for index in range(len(dtypeList)):
1817 for index in range(len(dtypeList)):
1818 if self.dataOut.dtype == dtypeList[index]:
1818 if self.dataOut.dtype == dtypeList[index]:
1819 dtypeValue = datatypeValueList[index]
1819 dtypeValue = datatypeValueList[index]
1820 break
1820 break
1821
1821
1822 processFlags += dtypeValue
1822 processFlags += dtypeValue
1823
1823
1824 if self.dataOut.flagDecodeData:
1824 if self.dataOut.flagDecodeData:
1825 processFlags += PROCFLAG.DECODE_DATA
1825 processFlags += PROCFLAG.DECODE_DATA
1826
1826
1827 if self.dataOut.flagDeflipData:
1827 if self.dataOut.flagDeflipData:
1828 processFlags += PROCFLAG.DEFLIP_DATA
1828 processFlags += PROCFLAG.DEFLIP_DATA
1829
1829
1830 if self.dataOut.code != None:
1830 if self.dataOut.code != None:
1831 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1831 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1832
1832
1833 if self.dataOut.nCohInt > 1:
1833 if self.dataOut.nCohInt > 1:
1834 processFlags += PROCFLAG.COHERENT_INTEGRATION
1834 processFlags += PROCFLAG.COHERENT_INTEGRATION
1835
1835
1836 return processFlags
1836 return processFlags
1837
1837
1838
1838
1839 def __getBlockSize(self):
1839 def __getBlockSize(self):
1840 '''
1840 '''
1841 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
1841 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
1842 '''
1842 '''
1843
1843
1844 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1844 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1845 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1845 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1846 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1846 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1847 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1847 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1848 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1848 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1849 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1849 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1850
1850
1851 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1851 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1852 datatypeValueList = [1,2,4,8,4,8]
1852 datatypeValueList = [1,2,4,8,4,8]
1853 for index in range(len(dtypeList)):
1853 for index in range(len(dtypeList)):
1854 if self.dataOut.dtype == dtypeList[index]:
1854 if self.dataOut.dtype == dtypeList[index]:
1855 datatypeValue = datatypeValueList[index]
1855 datatypeValue = datatypeValueList[index]
1856 break
1856 break
1857
1857
1858 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * datatypeValue * 2)
1858 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * datatypeValue * 2)
1859
1859
1860 return blocksize
1860 return blocksize
1861
1861
1862 def setFirstHeader(self):
1862 def setFirstHeader(self):
1863
1863
1864 """
1864 """
1865 Obtiene una copia del First Header
1865 Obtiene una copia del First Header
1866
1866
1867 Affected:
1867 Affected:
1868 self.systemHeaderObj
1868 self.systemHeaderObj
1869 self.radarControllerHeaderObj
1869 self.radarControllerHeaderObj
1870 self.dtype
1870 self.dtype
1871
1871
1872 Return:
1872 Return:
1873 None
1873 None
1874 """
1874 """
1875
1875
1876 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
1876 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
1877 self.systemHeaderObj.nChannels = self.dataOut.nChannels
1877 self.systemHeaderObj.nChannels = self.dataOut.nChannels
1878 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
1878 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
1879
1879
1880 self.setBasicHeader()
1880 self.setBasicHeader()
1881
1881
1882 processingHeaderSize = 40 # bytes
1882 processingHeaderSize = 40 # bytes
1883 self.processingHeaderObj.dtype = 0 # Voltage
1883 self.processingHeaderObj.dtype = 0 # Voltage
1884 self.processingHeaderObj.blockSize = self.__getBlockSize()
1884 self.processingHeaderObj.blockSize = self.__getBlockSize()
1885 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
1885 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
1886 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
1886 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
1887 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
1887 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
1888 self.processingHeaderObj.processFlags = self.__getProcessFlags()
1888 self.processingHeaderObj.processFlags = self.__getProcessFlags()
1889 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
1889 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
1890 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
1890 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
1891 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
1891 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
1892
1892
1893 # if self.dataOut.code != None:
1893 # if self.dataOut.code != None:
1894 # self.processingHeaderObj.code = self.dataOut.code
1894 # self.processingHeaderObj.code = self.dataOut.code
1895 # self.processingHeaderObj.nCode = self.dataOut.nCode
1895 # self.processingHeaderObj.nCode = self.dataOut.nCode
1896 # self.processingHeaderObj.nBaud = self.dataOut.nBaud
1896 # self.processingHeaderObj.nBaud = self.dataOut.nBaud
1897 # codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud)
1897 # codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud)
1898 # processingHeaderSize += codesize
1898 # processingHeaderSize += codesize
1899
1899
1900 if self.processingHeaderObj.nWindows != 0:
1900 if self.processingHeaderObj.nWindows != 0:
1901 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
1901 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
1902 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
1902 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
1903 self.processingHeaderObj.nHeights = self.dataOut.nHeights
1903 self.processingHeaderObj.nHeights = self.dataOut.nHeights
1904 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
1904 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
1905 processingHeaderSize += 12
1905 processingHeaderSize += 12
1906
1906
1907 self.processingHeaderObj.size = processingHeaderSize
1907 self.processingHeaderObj.size = processingHeaderSize
1908
1908
1909 class SpectraReader(JRODataReader):
1909 class SpectraReader(JRODataReader):
1910 """
1910 """
1911 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
1911 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
1912 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
1912 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
1913 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
1913 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
1914
1914
1915 paresCanalesIguales * alturas * perfiles (Self Spectra)
1915 paresCanalesIguales * alturas * perfiles (Self Spectra)
1916 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
1916 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
1917 canales * alturas (DC Channels)
1917 canales * alturas (DC Channels)
1918
1918
1919 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1919 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1920 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
1920 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
1921 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
1921 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
1922 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1922 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1923
1923
1924 Example:
1924 Example:
1925 dpath = "/home/myuser/data"
1925 dpath = "/home/myuser/data"
1926
1926
1927 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1927 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1928
1928
1929 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1929 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1930
1930
1931 readerObj = SpectraReader()
1931 readerObj = SpectraReader()
1932
1932
1933 readerObj.setup(dpath, startTime, endTime)
1933 readerObj.setup(dpath, startTime, endTime)
1934
1934
1935 while(True):
1935 while(True):
1936
1936
1937 readerObj.getData()
1937 readerObj.getData()
1938
1938
1939 print readerObj.data_spc
1939 print readerObj.data_spc
1940
1940
1941 print readerObj.data_cspc
1941 print readerObj.data_cspc
1942
1942
1943 print readerObj.data_dc
1943 print readerObj.data_dc
1944
1944
1945 if readerObj.flagNoMoreFiles:
1945 if readerObj.flagNoMoreFiles:
1946 break
1946 break
1947
1947
1948 """
1948 """
1949
1949
1950 pts2read_SelfSpectra = 0
1950 pts2read_SelfSpectra = 0
1951
1951
1952 pts2read_CrossSpectra = 0
1952 pts2read_CrossSpectra = 0
1953
1953
1954 pts2read_DCchannels = 0
1954 pts2read_DCchannels = 0
1955
1955
1956 ext = ".pdata"
1956 ext = ".pdata"
1957
1957
1958 optchar = "P"
1958 optchar = "P"
1959
1959
1960 dataOut = None
1960 dataOut = None
1961
1961
1962 nRdChannels = None
1962 nRdChannels = None
1963
1963
1964 nRdPairs = None
1964 nRdPairs = None
1965
1965
1966 rdPairList = []
1966 rdPairList = []
1967
1967
1968 def __init__(self):
1968 def __init__(self):
1969 """
1969 """
1970 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
1970 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
1971
1971
1972 Inputs:
1972 Inputs:
1973 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
1973 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
1974 almacenar un perfil de datos cada vez que se haga un requerimiento
1974 almacenar un perfil de datos cada vez que se haga un requerimiento
1975 (getData). El perfil sera obtenido a partir del buffer de datos,
1975 (getData). El perfil sera obtenido a partir del buffer de datos,
1976 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1976 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1977 bloque de datos.
1977 bloque de datos.
1978 Si este parametro no es pasado se creara uno internamente.
1978 Si este parametro no es pasado se creara uno internamente.
1979
1979
1980 Affected:
1980 Affected:
1981 self.dataOut
1981 self.dataOut
1982
1982
1983 Return : None
1983 Return : None
1984 """
1984 """
1985
1985
1986 self.isConfig = False
1986 self.isConfig = False
1987
1987
1988 self.pts2read_SelfSpectra = 0
1988 self.pts2read_SelfSpectra = 0
1989
1989
1990 self.pts2read_CrossSpectra = 0
1990 self.pts2read_CrossSpectra = 0
1991
1991
1992 self.pts2read_DCchannels = 0
1992 self.pts2read_DCchannels = 0
1993
1993
1994 self.datablock = None
1994 self.datablock = None
1995
1995
1996 self.utc = None
1996 self.utc = None
1997
1997
1998 self.ext = ".pdata"
1998 self.ext = ".pdata"
1999
1999
2000 self.optchar = "P"
2000 self.optchar = "P"
2001
2001
2002 self.basicHeaderObj = BasicHeader(LOCALTIME)
2002 self.basicHeaderObj = BasicHeader(LOCALTIME)
2003
2003
2004 self.systemHeaderObj = SystemHeader()
2004 self.systemHeaderObj = SystemHeader()
2005
2005
2006 self.radarControllerHeaderObj = RadarControllerHeader()
2006 self.radarControllerHeaderObj = RadarControllerHeader()
2007
2007
2008 self.processingHeaderObj = ProcessingHeader()
2008 self.processingHeaderObj = ProcessingHeader()
2009
2009
2010 self.online = 0
2010 self.online = 0
2011
2011
2012 self.fp = None
2012 self.fp = None
2013
2013
2014 self.idFile = None
2014 self.idFile = None
2015
2015
2016 self.dtype = None
2016 self.dtype = None
2017
2017
2018 self.fileSizeByHeader = None
2018 self.fileSizeByHeader = None
2019
2019
2020 self.filenameList = []
2020 self.filenameList = []
2021
2021
2022 self.filename = None
2022 self.filename = None
2023
2023
2024 self.fileSize = None
2024 self.fileSize = None
2025
2025
2026 self.firstHeaderSize = 0
2026 self.firstHeaderSize = 0
2027
2027
2028 self.basicHeaderSize = 24
2028 self.basicHeaderSize = 24
2029
2029
2030 self.pathList = []
2030 self.pathList = []
2031
2031
2032 self.lastUTTime = 0
2032 self.lastUTTime = 0
2033
2033
2034 self.maxTimeStep = 30
2034 self.maxTimeStep = 30
2035
2035
2036 self.flagNoMoreFiles = 0
2036 self.flagNoMoreFiles = 0
2037
2037
2038 self.set = 0
2038 self.set = 0
2039
2039
2040 self.path = None
2040 self.path = None
2041
2041
2042 self.delay = 60 #seconds
2042 self.delay = 60 #seconds
2043
2043
2044 self.nTries = 3 #quantity tries
2044 self.nTries = 3 #quantity tries
2045
2045
2046 self.nFiles = 3 #number of files for searching
2046 self.nFiles = 3 #number of files for searching
2047
2047
2048 self.nReadBlocks = 0
2048 self.nReadBlocks = 0
2049
2049
2050 self.flagIsNewFile = 1
2050 self.flagIsNewFile = 1
2051
2051
2052 self.__isFirstTimeOnline = 1
2052 self.__isFirstTimeOnline = 1
2053
2053
2054 self.ippSeconds = 0
2054 self.ippSeconds = 0
2055
2055
2056 self.flagTimeBlock = 0
2056 self.flagTimeBlock = 0
2057
2057
2058 self.flagIsNewBlock = 0
2058 self.flagIsNewBlock = 0
2059
2059
2060 self.nTotalBlocks = 0
2060 self.nTotalBlocks = 0
2061
2061
2062 self.blocksize = 0
2062 self.blocksize = 0
2063
2063
2064 self.dataOut = self.createObjByDefault()
2064 self.dataOut = self.createObjByDefault()
2065
2065
2066 self.profileIndex = 1 #Always
2066 self.profileIndex = 1 #Always
2067
2067
2068
2068
2069 def createObjByDefault(self):
2069 def createObjByDefault(self):
2070
2070
2071 dataObj = Spectra()
2071 dataObj = Spectra()
2072
2072
2073 return dataObj
2073 return dataObj
2074
2074
2075 def __hasNotDataInBuffer(self):
2075 def __hasNotDataInBuffer(self):
2076 return 1
2076 return 1
2077
2077
2078
2078
2079 def getBlockDimension(self):
2079 def getBlockDimension(self):
2080 """
2080 """
2081 Obtiene la cantidad de puntos a leer por cada bloque de datos
2081 Obtiene la cantidad de puntos a leer por cada bloque de datos
2082
2082
2083 Affected:
2083 Affected:
2084 self.nRdChannels
2084 self.nRdChannels
2085 self.nRdPairs
2085 self.nRdPairs
2086 self.pts2read_SelfSpectra
2086 self.pts2read_SelfSpectra
2087 self.pts2read_CrossSpectra
2087 self.pts2read_CrossSpectra
2088 self.pts2read_DCchannels
2088 self.pts2read_DCchannels
2089 self.blocksize
2089 self.blocksize
2090 self.dataOut.nChannels
2090 self.dataOut.nChannels
2091 self.dataOut.nPairs
2091 self.dataOut.nPairs
2092
2092
2093 Return:
2093 Return:
2094 None
2094 None
2095 """
2095 """
2096 self.nRdChannels = 0
2096 self.nRdChannels = 0
2097 self.nRdPairs = 0
2097 self.nRdPairs = 0
2098 self.rdPairList = []
2098 self.rdPairList = []
2099
2099
2100 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
2100 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
2101 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
2101 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
2102 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
2102 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
2103 else:
2103 else:
2104 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
2104 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
2105 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
2105 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
2106
2106
2107 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
2107 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
2108
2108
2109 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
2109 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
2110 self.blocksize = self.pts2read_SelfSpectra
2110 self.blocksize = self.pts2read_SelfSpectra
2111
2111
2112 if self.processingHeaderObj.flag_cspc:
2112 if self.processingHeaderObj.flag_cspc:
2113 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
2113 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
2114 self.blocksize += self.pts2read_CrossSpectra
2114 self.blocksize += self.pts2read_CrossSpectra
2115
2115
2116 if self.processingHeaderObj.flag_dc:
2116 if self.processingHeaderObj.flag_dc:
2117 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
2117 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
2118 self.blocksize += self.pts2read_DCchannels
2118 self.blocksize += self.pts2read_DCchannels
2119
2119
2120 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
2120 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
2121
2121
2122
2122
2123 def readBlock(self):
2123 def readBlock(self):
2124 """
2124 """
2125 Lee el bloque de datos desde la posicion actual del puntero del archivo
2125 Lee el bloque de datos desde la posicion actual del puntero del archivo
2126 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
2126 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
2127 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
2127 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
2128 es seteado a 0
2128 es seteado a 0
2129
2129
2130 Return: None
2130 Return: None
2131
2131
2132 Variables afectadas:
2132 Variables afectadas:
2133
2133
2134 self.flagIsNewFile
2134 self.flagIsNewFile
2135 self.flagIsNewBlock
2135 self.flagIsNewBlock
2136 self.nTotalBlocks
2136 self.nTotalBlocks
2137 self.data_spc
2137 self.data_spc
2138 self.data_cspc
2138 self.data_cspc
2139 self.data_dc
2139 self.data_dc
2140
2140
2141 Exceptions:
2141 Exceptions:
2142 Si un bloque leido no es un bloque valido
2142 Si un bloque leido no es un bloque valido
2143 """
2143 """
2144 blockOk_flag = False
2144 blockOk_flag = False
2145 fpointer = self.fp.tell()
2145 fpointer = self.fp.tell()
2146
2146
2147 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
2147 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
2148 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
2148 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
2149
2149
2150 if self.processingHeaderObj.flag_cspc:
2150 if self.processingHeaderObj.flag_cspc:
2151 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
2151 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
2152 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
2152 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
2153
2153
2154 if self.processingHeaderObj.flag_dc:
2154 if self.processingHeaderObj.flag_dc:
2155 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
2155 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
2156 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
2156 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
2157
2157
2158
2158
2159 if not(self.processingHeaderObj.shif_fft):
2159 if not(self.processingHeaderObj.shif_fft):
2160 #desplaza a la derecha en el eje 2 determinadas posiciones
2160 #desplaza a la derecha en el eje 2 determinadas posiciones
2161 shift = int(self.processingHeaderObj.profilesPerBlock/2)
2161 shift = int(self.processingHeaderObj.profilesPerBlock/2)
2162 spc = numpy.roll( spc, shift , axis=2 )
2162 spc = numpy.roll( spc, shift , axis=2 )
2163
2163
2164 if self.processingHeaderObj.flag_cspc:
2164 if self.processingHeaderObj.flag_cspc:
2165 #desplaza a la derecha en el eje 2 determinadas posiciones
2165 #desplaza a la derecha en el eje 2 determinadas posiciones
2166 cspc = numpy.roll( cspc, shift, axis=2 )
2166 cspc = numpy.roll( cspc, shift, axis=2 )
2167
2167
2168 # self.processingHeaderObj.shif_fft = True
2168 # self.processingHeaderObj.shif_fft = True
2169
2169
2170 spc = numpy.transpose( spc, (0,2,1) )
2170 spc = numpy.transpose( spc, (0,2,1) )
2171 self.data_spc = spc
2171 self.data_spc = spc
2172
2172
2173 if self.processingHeaderObj.flag_cspc:
2173 if self.processingHeaderObj.flag_cspc:
2174 cspc = numpy.transpose( cspc, (0,2,1) )
2174 cspc = numpy.transpose( cspc, (0,2,1) )
2175 self.data_cspc = cspc['real'] + cspc['imag']*1j
2175 self.data_cspc = cspc['real'] + cspc['imag']*1j
2176 else:
2176 else:
2177 self.data_cspc = None
2177 self.data_cspc = None
2178
2178
2179 if self.processingHeaderObj.flag_dc:
2179 if self.processingHeaderObj.flag_dc:
2180 self.data_dc = dc['real'] + dc['imag']*1j
2180 self.data_dc = dc['real'] + dc['imag']*1j
2181 else:
2181 else:
2182 self.data_dc = None
2182 self.data_dc = None
2183
2183
2184 self.flagIsNewFile = 0
2184 self.flagIsNewFile = 0
2185 self.flagIsNewBlock = 1
2185 self.flagIsNewBlock = 1
2186
2186
2187 self.nTotalBlocks += 1
2187 self.nTotalBlocks += 1
2188 self.nReadBlocks += 1
2188 self.nReadBlocks += 1
2189
2189
2190 return 1
2190 return 1
2191
2191
2192 def getFirstHeader(self):
2192 def getFirstHeader(self):
2193
2193
2194 self.dataOut.dtype = self.dtype
2194 self.dataOut.dtype = self.dtype
2195
2195
2196 self.dataOut.nPairs = self.nRdPairs
2196 self.dataOut.nPairs = self.nRdPairs
2197
2197
2198 self.dataOut.pairsList = self.rdPairList
2198 self.dataOut.pairsList = self.rdPairList
2199
2199
2200 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
2200 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
2201
2201
2202 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
2202 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
2203
2203
2204 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
2204 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
2205
2205
2206 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
2206 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
2207
2207
2208 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
2208 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
2209
2209
2210 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
2210 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
2211
2211
2212 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
2212 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
2213
2213
2214 self.dataOut.ippSeconds = self.ippSeconds
2214 self.dataOut.ippSeconds = self.ippSeconds
2215
2215
2216 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.dataOut.nFFTPoints
2216 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.dataOut.nFFTPoints
2217
2217
2218 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
2218 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
2219
2219
2220 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
2220 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
2221
2221
2222 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
2222 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
2223
2223
2224 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
2224 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
2225
2225
2226 self.dataOut.flagDeflipData = True #asumo q la data no esta sin flip
2226 self.dataOut.flagDeflipData = True #asumo q la data no esta sin flip
2227
2227
2228 if self.processingHeaderObj.code != None:
2228 if self.processingHeaderObj.code != None:
2229
2229
2230 self.dataOut.nCode = self.processingHeaderObj.nCode
2230 self.dataOut.nCode = self.processingHeaderObj.nCode
2231
2231
2232 self.dataOut.nBaud = self.processingHeaderObj.nBaud
2232 self.dataOut.nBaud = self.processingHeaderObj.nBaud
2233
2233
2234 self.dataOut.code = self.processingHeaderObj.code
2234 self.dataOut.code = self.processingHeaderObj.code
2235
2235
2236 self.dataOut.flagDecodeData = True
2236 self.dataOut.flagDecodeData = True
2237
2237
2238 def getData(self):
2238 def getData(self):
2239 """
2239 """
2240 Copia el buffer de lectura a la clase "Spectra",
2240 Copia el buffer de lectura a la clase "Spectra",
2241 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
2241 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
2242 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
2242 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
2243
2243
2244 Return:
2244 Return:
2245 0 : Si no hay mas archivos disponibles
2245 0 : Si no hay mas archivos disponibles
2246 1 : Si hizo una buena copia del buffer
2246 1 : Si hizo una buena copia del buffer
2247
2247
2248 Affected:
2248 Affected:
2249 self.dataOut
2249 self.dataOut
2250
2250
2251 self.flagTimeBlock
2251 self.flagTimeBlock
2252 self.flagIsNewBlock
2252 self.flagIsNewBlock
2253 """
2253 """
2254
2254
2255 if self.flagNoMoreFiles:
2255 if self.flagNoMoreFiles:
2256 self.dataOut.flagNoData = True
2256 self.dataOut.flagNoData = True
2257 print 'Process finished'
2257 print 'Process finished'
2258 return 0
2258 return 0
2259
2259
2260 self.flagTimeBlock = 0
2260 self.flagTimeBlock = 0
2261 self.flagIsNewBlock = 0
2261 self.flagIsNewBlock = 0
2262
2262
2263 if self.__hasNotDataInBuffer():
2263 if self.__hasNotDataInBuffer():
2264
2264
2265 if not( self.readNextBlock() ):
2265 if not( self.readNextBlock() ):
2266 self.dataOut.flagNoData = True
2266 self.dataOut.flagNoData = True
2267 return 0
2267 return 0
2268
2268
2269 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
2269 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
2270
2270
2271 if self.data_dc == None:
2271 if self.data_dc == None:
2272 self.dataOut.flagNoData = True
2272 self.dataOut.flagNoData = True
2273 return 0
2273 return 0
2274
2274
2275 self.getBasicHeader()
2275 self.getBasicHeader()
2276
2276
2277 self.getFirstHeader()
2277 self.getFirstHeader()
2278
2278
2279 self.dataOut.data_spc = self.data_spc
2279 self.dataOut.data_spc = self.data_spc
2280
2280
2281 self.dataOut.data_cspc = self.data_cspc
2281 self.dataOut.data_cspc = self.data_cspc
2282
2282
2283 self.dataOut.data_dc = self.data_dc
2283 self.dataOut.data_dc = self.data_dc
2284
2284
2285 self.dataOut.flagNoData = False
2285 self.dataOut.flagNoData = False
2286
2286
2287 self.dataOut.realtime = self.online
2287 self.dataOut.realtime = self.online
2288
2288
2289 return self.dataOut.data_spc
2289 return self.dataOut.data_spc
2290
2290
2291
2291
2292 class SpectraWriter(JRODataWriter):
2292 class SpectraWriter(JRODataWriter):
2293
2293
2294 """
2294 """
2295 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
2295 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
2296 de los datos siempre se realiza por bloques.
2296 de los datos siempre se realiza por bloques.
2297 """
2297 """
2298
2298
2299 ext = ".pdata"
2299 ext = ".pdata"
2300
2300
2301 optchar = "P"
2301 optchar = "P"
2302
2302
2303 shape_spc_Buffer = None
2303 shape_spc_Buffer = None
2304
2304
2305 shape_cspc_Buffer = None
2305 shape_cspc_Buffer = None
2306
2306
2307 shape_dc_Buffer = None
2307 shape_dc_Buffer = None
2308
2308
2309 data_spc = None
2309 data_spc = None
2310
2310
2311 data_cspc = None
2311 data_cspc = None
2312
2312
2313 data_dc = None
2313 data_dc = None
2314
2314
2315 # dataOut = None
2315 # dataOut = None
2316
2316
2317 def __init__(self):
2317 def __init__(self):
2318 """
2318 """
2319 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
2319 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
2320
2320
2321 Affected:
2321 Affected:
2322 self.dataOut
2322 self.dataOut
2323 self.basicHeaderObj
2323 self.basicHeaderObj
2324 self.systemHeaderObj
2324 self.systemHeaderObj
2325 self.radarControllerHeaderObj
2325 self.radarControllerHeaderObj
2326 self.processingHeaderObj
2326 self.processingHeaderObj
2327
2327
2328 Return: None
2328 Return: None
2329 """
2329 """
2330
2330
2331 self.isConfig = False
2331 self.isConfig = False
2332
2332
2333 self.nTotalBlocks = 0
2333 self.nTotalBlocks = 0
2334
2334
2335 self.data_spc = None
2335 self.data_spc = None
2336
2336
2337 self.data_cspc = None
2337 self.data_cspc = None
2338
2338
2339 self.data_dc = None
2339 self.data_dc = None
2340
2340
2341 self.fp = None
2341 self.fp = None
2342
2342
2343 self.flagIsNewFile = 1
2343 self.flagIsNewFile = 1
2344
2344
2345 self.nTotalBlocks = 0
2345 self.nTotalBlocks = 0
2346
2346
2347 self.flagIsNewBlock = 0
2347 self.flagIsNewBlock = 0
2348
2348
2349 self.setFile = None
2349 self.setFile = None
2350
2350
2351 self.dtype = None
2351 self.dtype = None
2352
2352
2353 self.path = None
2353 self.path = None
2354
2354
2355 self.noMoreFiles = 0
2355 self.noMoreFiles = 0
2356
2356
2357 self.filename = None
2357 self.filename = None
2358
2358
2359 self.basicHeaderObj = BasicHeader(LOCALTIME)
2359 self.basicHeaderObj = BasicHeader(LOCALTIME)
2360
2360
2361 self.systemHeaderObj = SystemHeader()
2361 self.systemHeaderObj = SystemHeader()
2362
2362
2363 self.radarControllerHeaderObj = RadarControllerHeader()
2363 self.radarControllerHeaderObj = RadarControllerHeader()
2364
2364
2365 self.processingHeaderObj = ProcessingHeader()
2365 self.processingHeaderObj = ProcessingHeader()
2366
2366
2367
2367
2368 def hasAllDataInBuffer(self):
2368 def hasAllDataInBuffer(self):
2369 return 1
2369 return 1
2370
2370
2371
2371
2372 def setBlockDimension(self):
2372 def setBlockDimension(self):
2373 """
2373 """
2374 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
2374 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
2375
2375
2376 Affected:
2376 Affected:
2377 self.shape_spc_Buffer
2377 self.shape_spc_Buffer
2378 self.shape_cspc_Buffer
2378 self.shape_cspc_Buffer
2379 self.shape_dc_Buffer
2379 self.shape_dc_Buffer
2380
2380
2381 Return: None
2381 Return: None
2382 """
2382 """
2383 self.shape_spc_Buffer = (self.dataOut.nChannels,
2383 self.shape_spc_Buffer = (self.dataOut.nChannels,
2384 self.processingHeaderObj.nHeights,
2384 self.processingHeaderObj.nHeights,
2385 self.processingHeaderObj.profilesPerBlock)
2385 self.processingHeaderObj.profilesPerBlock)
2386
2386
2387 self.shape_cspc_Buffer = (self.dataOut.nPairs,
2387 self.shape_cspc_Buffer = (self.dataOut.nPairs,
2388 self.processingHeaderObj.nHeights,
2388 self.processingHeaderObj.nHeights,
2389 self.processingHeaderObj.profilesPerBlock)
2389 self.processingHeaderObj.profilesPerBlock)
2390
2390
2391 self.shape_dc_Buffer = (self.dataOut.nChannels,
2391 self.shape_dc_Buffer = (self.dataOut.nChannels,
2392 self.processingHeaderObj.nHeights)
2392 self.processingHeaderObj.nHeights)
2393
2393
2394
2394
2395 def writeBlock(self):
2395 def writeBlock(self):
2396 """
2396 """
2397 Escribe el buffer en el file designado
2397 Escribe el buffer en el file designado
2398
2398
2399 Affected:
2399 Affected:
2400 self.data_spc
2400 self.data_spc
2401 self.data_cspc
2401 self.data_cspc
2402 self.data_dc
2402 self.data_dc
2403 self.flagIsNewFile
2403 self.flagIsNewFile
2404 self.flagIsNewBlock
2404 self.flagIsNewBlock
2405 self.nTotalBlocks
2405 self.nTotalBlocks
2406 self.nWriteBlocks
2406 self.nWriteBlocks
2407
2407
2408 Return: None
2408 Return: None
2409 """
2409 """
2410
2410
2411 spc = numpy.transpose( self.data_spc, (0,2,1) )
2411 spc = numpy.transpose( self.data_spc, (0,2,1) )
2412 if not( self.processingHeaderObj.shif_fft ):
2412 if not( self.processingHeaderObj.shif_fft ):
2413 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2413 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2414 data = spc.reshape((-1))
2414 data = spc.reshape((-1))
2415 data = data.astype(self.dtype[0])
2415 data = data.astype(self.dtype[0])
2416 data.tofile(self.fp)
2416 data.tofile(self.fp)
2417
2417
2418 if self.data_cspc != None:
2418 if self.data_cspc != None:
2419 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
2419 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
2420 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
2420 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
2421 if not( self.processingHeaderObj.shif_fft ):
2421 if not( self.processingHeaderObj.shif_fft ):
2422 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2422 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2423 data['real'] = cspc.real
2423 data['real'] = cspc.real
2424 data['imag'] = cspc.imag
2424 data['imag'] = cspc.imag
2425 data = data.reshape((-1))
2425 data = data.reshape((-1))
2426 data.tofile(self.fp)
2426 data.tofile(self.fp)
2427
2427
2428 if self.data_dc != None:
2428 if self.data_dc != None:
2429 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
2429 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
2430 dc = self.data_dc
2430 dc = self.data_dc
2431 data['real'] = dc.real
2431 data['real'] = dc.real
2432 data['imag'] = dc.imag
2432 data['imag'] = dc.imag
2433 data = data.reshape((-1))
2433 data = data.reshape((-1))
2434 data.tofile(self.fp)
2434 data.tofile(self.fp)
2435
2435
2436 self.data_spc.fill(0)
2436 self.data_spc.fill(0)
2437
2437
2438 if self.data_dc != None:
2438 if self.data_dc != None:
2439 self.data_dc.fill(0)
2439 self.data_dc.fill(0)
2440
2440
2441 if self.data_cspc != None:
2441 if self.data_cspc != None:
2442 self.data_cspc.fill(0)
2442 self.data_cspc.fill(0)
2443
2443
2444 self.flagIsNewFile = 0
2444 self.flagIsNewFile = 0
2445 self.flagIsNewBlock = 1
2445 self.flagIsNewBlock = 1
2446 self.nTotalBlocks += 1
2446 self.nTotalBlocks += 1
2447 self.nWriteBlocks += 1
2447 self.nWriteBlocks += 1
2448 self.blockIndex += 1
2448 self.blockIndex += 1
2449
2449
2450
2450
2451 def putData(self):
2451 def putData(self):
2452 """
2452 """
2453 Setea un bloque de datos y luego los escribe en un file
2453 Setea un bloque de datos y luego los escribe en un file
2454
2454
2455 Affected:
2455 Affected:
2456 self.data_spc
2456 self.data_spc
2457 self.data_cspc
2457 self.data_cspc
2458 self.data_dc
2458 self.data_dc
2459
2459
2460 Return:
2460 Return:
2461 0 : Si no hay data o no hay mas files que puedan escribirse
2461 0 : Si no hay data o no hay mas files que puedan escribirse
2462 1 : Si se escribio la data de un bloque en un file
2462 1 : Si se escribio la data de un bloque en un file
2463 """
2463 """
2464
2464
2465 if self.dataOut.flagNoData:
2465 if self.dataOut.flagNoData:
2466 return 0
2466 return 0
2467
2467
2468 self.flagIsNewBlock = 0
2468 self.flagIsNewBlock = 0
2469
2469
2470 if self.dataOut.flagTimeBlock:
2470 if self.dataOut.flagTimeBlock:
2471 self.data_spc.fill(0)
2471 self.data_spc.fill(0)
2472 self.data_cspc.fill(0)
2472 self.data_cspc.fill(0)
2473 self.data_dc.fill(0)
2473 self.data_dc.fill(0)
2474 self.setNextFile()
2474 self.setNextFile()
2475
2475
2476 if self.flagIsNewFile == 0:
2476 if self.flagIsNewFile == 0:
2477 self.setBasicHeader()
2477 self.setBasicHeader()
2478
2478
2479 self.data_spc = self.dataOut.data_spc.copy()
2479 self.data_spc = self.dataOut.data_spc.copy()
2480 if self.dataOut.data_cspc != None:
2480 if self.dataOut.data_cspc != None:
2481 self.data_cspc = self.dataOut.data_cspc.copy()
2481 self.data_cspc = self.dataOut.data_cspc.copy()
2482 self.data_dc = self.dataOut.data_dc.copy()
2482 self.data_dc = self.dataOut.data_dc.copy()
2483
2483
2484 # #self.processingHeaderObj.dataBlocksPerFile)
2484 # #self.processingHeaderObj.dataBlocksPerFile)
2485 if self.hasAllDataInBuffer():
2485 if self.hasAllDataInBuffer():
2486 # self.setFirstHeader()
2486 # self.setFirstHeader()
2487 self.writeNextBlock()
2487 self.writeNextBlock()
2488
2488
2489 return 1
2489 return 1
2490
2490
2491
2491
2492 def __getProcessFlags(self):
2492 def __getProcessFlags(self):
2493
2493
2494 processFlags = 0
2494 processFlags = 0
2495
2495
2496 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2496 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2497 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2497 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2498 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2498 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2499 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2499 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2500 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2500 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2501 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2501 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2502
2502
2503 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2503 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2504
2504
2505
2505
2506
2506
2507 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
2507 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
2508 PROCFLAG.DATATYPE_SHORT,
2508 PROCFLAG.DATATYPE_SHORT,
2509 PROCFLAG.DATATYPE_LONG,
2509 PROCFLAG.DATATYPE_LONG,
2510 PROCFLAG.DATATYPE_INT64,
2510 PROCFLAG.DATATYPE_INT64,
2511 PROCFLAG.DATATYPE_FLOAT,
2511 PROCFLAG.DATATYPE_FLOAT,
2512 PROCFLAG.DATATYPE_DOUBLE]
2512 PROCFLAG.DATATYPE_DOUBLE]
2513
2513
2514
2514
2515 for index in range(len(dtypeList)):
2515 for index in range(len(dtypeList)):
2516 if self.dataOut.dtype == dtypeList[index]:
2516 if self.dataOut.dtype == dtypeList[index]:
2517 dtypeValue = datatypeValueList[index]
2517 dtypeValue = datatypeValueList[index]
2518 break
2518 break
2519
2519
2520 processFlags += dtypeValue
2520 processFlags += dtypeValue
2521
2521
2522 if self.dataOut.flagDecodeData:
2522 if self.dataOut.flagDecodeData:
2523 processFlags += PROCFLAG.DECODE_DATA
2523 processFlags += PROCFLAG.DECODE_DATA
2524
2524
2525 if self.dataOut.flagDeflipData:
2525 if self.dataOut.flagDeflipData:
2526 processFlags += PROCFLAG.DEFLIP_DATA
2526 processFlags += PROCFLAG.DEFLIP_DATA
2527
2527
2528 if self.dataOut.code != None:
2528 if self.dataOut.code != None:
2529 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
2529 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
2530
2530
2531 if self.dataOut.nIncohInt > 1:
2531 if self.dataOut.nIncohInt > 1:
2532 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
2532 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
2533
2533
2534 if self.dataOut.data_dc != None:
2534 if self.dataOut.data_dc != None:
2535 processFlags += PROCFLAG.SAVE_CHANNELS_DC
2535 processFlags += PROCFLAG.SAVE_CHANNELS_DC
2536
2536
2537 return processFlags
2537 return processFlags
2538
2538
2539
2539
2540 def __getBlockSize(self):
2540 def __getBlockSize(self):
2541 '''
2541 '''
2542 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
2542 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
2543 '''
2543 '''
2544
2544
2545 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2545 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2546 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2546 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2547 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2547 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2548 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2548 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2549 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2549 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2550 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2550 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2551
2551
2552 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2552 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2553 datatypeValueList = [1,2,4,8,4,8]
2553 datatypeValueList = [1,2,4,8,4,8]
2554 for index in range(len(dtypeList)):
2554 for index in range(len(dtypeList)):
2555 if self.dataOut.dtype == dtypeList[index]:
2555 if self.dataOut.dtype == dtypeList[index]:
2556 datatypeValue = datatypeValueList[index]
2556 datatypeValue = datatypeValueList[index]
2557 break
2557 break
2558
2558
2559
2559
2560 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
2560 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
2561
2561
2562 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
2562 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
2563 blocksize = (pts2write_SelfSpectra*datatypeValue)
2563 blocksize = (pts2write_SelfSpectra*datatypeValue)
2564
2564
2565 if self.dataOut.data_cspc != None:
2565 if self.dataOut.data_cspc != None:
2566 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
2566 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
2567 blocksize += (pts2write_CrossSpectra*datatypeValue*2)
2567 blocksize += (pts2write_CrossSpectra*datatypeValue*2)
2568
2568
2569 if self.dataOut.data_dc != None:
2569 if self.dataOut.data_dc != None:
2570 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
2570 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
2571 blocksize += (pts2write_DCchannels*datatypeValue*2)
2571 blocksize += (pts2write_DCchannels*datatypeValue*2)
2572
2572
2573 blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
2573 blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
2574
2574
2575 return blocksize
2575 return blocksize
2576
2576
2577 def setFirstHeader(self):
2577 def setFirstHeader(self):
2578
2578
2579 """
2579 """
2580 Obtiene una copia del First Header
2580 Obtiene una copia del First Header
2581
2581
2582 Affected:
2582 Affected:
2583 self.systemHeaderObj
2583 self.systemHeaderObj
2584 self.radarControllerHeaderObj
2584 self.radarControllerHeaderObj
2585 self.dtype
2585 self.dtype
2586
2586
2587 Return:
2587 Return:
2588 None
2588 None
2589 """
2589 """
2590
2590
2591 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
2591 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
2592 self.systemHeaderObj.nChannels = self.dataOut.nChannels
2592 self.systemHeaderObj.nChannels = self.dataOut.nChannels
2593 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
2593 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
2594
2594
2595 self.setBasicHeader()
2595 self.setBasicHeader()
2596
2596
2597 processingHeaderSize = 40 # bytes
2597 processingHeaderSize = 40 # bytes
2598 self.processingHeaderObj.dtype = 1 # Spectra
2598 self.processingHeaderObj.dtype = 1 # Spectra
2599 self.processingHeaderObj.blockSize = self.__getBlockSize()
2599 self.processingHeaderObj.blockSize = self.__getBlockSize()
2600 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
2600 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
2601 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
2601 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
2602 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
2602 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
2603 self.processingHeaderObj.processFlags = self.__getProcessFlags()
2603 self.processingHeaderObj.processFlags = self.__getProcessFlags()
2604 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
2604 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
2605 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
2605 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
2606 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
2606 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
2607 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
2607 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
2608
2608
2609 if self.processingHeaderObj.totalSpectra > 0:
2609 if self.processingHeaderObj.totalSpectra > 0:
2610 channelList = []
2610 channelList = []
2611 for channel in range(self.dataOut.nChannels):
2611 for channel in range(self.dataOut.nChannels):
2612 channelList.append(channel)
2612 channelList.append(channel)
2613 channelList.append(channel)
2613 channelList.append(channel)
2614
2614
2615 pairsList = []
2615 pairsList = []
2616 if self.dataOut.nPairs > 0:
2616 if self.dataOut.nPairs > 0:
2617 for pair in self.dataOut.pairsList:
2617 for pair in self.dataOut.pairsList:
2618 pairsList.append(pair[0])
2618 pairsList.append(pair[0])
2619 pairsList.append(pair[1])
2619 pairsList.append(pair[1])
2620
2620
2621 spectraComb = channelList + pairsList
2621 spectraComb = channelList + pairsList
2622 spectraComb = numpy.array(spectraComb,dtype="u1")
2622 spectraComb = numpy.array(spectraComb,dtype="u1")
2623 self.processingHeaderObj.spectraComb = spectraComb
2623 self.processingHeaderObj.spectraComb = spectraComb
2624 sizeOfSpcComb = len(spectraComb)
2624 sizeOfSpcComb = len(spectraComb)
2625 processingHeaderSize += sizeOfSpcComb
2625 processingHeaderSize += sizeOfSpcComb
2626
2626
2627 # The processing header should not have information about code
2627 # The processing header should not have information about code
2628 # if self.dataOut.code != None:
2628 # if self.dataOut.code != None:
2629 # self.processingHeaderObj.code = self.dataOut.code
2629 # self.processingHeaderObj.code = self.dataOut.code
2630 # self.processingHeaderObj.nCode = self.dataOut.nCode
2630 # self.processingHeaderObj.nCode = self.dataOut.nCode
2631 # self.processingHeaderObj.nBaud = self.dataOut.nBaud
2631 # self.processingHeaderObj.nBaud = self.dataOut.nBaud
2632 # nCodeSize = 4 # bytes
2632 # nCodeSize = 4 # bytes
2633 # nBaudSize = 4 # bytes
2633 # nBaudSize = 4 # bytes
2634 # codeSize = 4 # bytes
2634 # codeSize = 4 # bytes
2635 # sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud)
2635 # sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud)
2636 # processingHeaderSize += sizeOfCode
2636 # processingHeaderSize += sizeOfCode
2637
2637
2638 if self.processingHeaderObj.nWindows != 0:
2638 if self.processingHeaderObj.nWindows != 0:
2639 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
2639 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
2640 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
2640 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
2641 self.processingHeaderObj.nHeights = self.dataOut.nHeights
2641 self.processingHeaderObj.nHeights = self.dataOut.nHeights
2642 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
2642 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
2643 sizeOfFirstHeight = 4
2643 sizeOfFirstHeight = 4
2644 sizeOfdeltaHeight = 4
2644 sizeOfdeltaHeight = 4
2645 sizeOfnHeights = 4
2645 sizeOfnHeights = 4
2646 sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows
2646 sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows
2647 processingHeaderSize += sizeOfWindows
2647 processingHeaderSize += sizeOfWindows
2648
2648
2649 self.processingHeaderObj.size = processingHeaderSize
2649 self.processingHeaderObj.size = processingHeaderSize
2650
2650
2651 class SpectraHeisWriter(Operation):
2651 class SpectraHeisWriter(Operation):
2652 # set = None
2652 # set = None
2653 setFile = None
2653 setFile = None
2654 idblock = None
2654 idblock = None
2655 doypath = None
2655 doypath = None
2656 subfolder = None
2656 subfolder = None
2657
2657
2658 def __init__(self):
2658 def __init__(self):
2659 self.wrObj = FITS()
2659 self.wrObj = FITS()
2660 # self.dataOut = dataOut
2660 # self.dataOut = dataOut
2661 self.nTotalBlocks=0
2661 self.nTotalBlocks=0
2662 # self.set = None
2662 # self.set = None
2663 self.setFile = None
2663 self.setFile = None
2664 self.idblock = 0
2664 self.idblock = 0
2665 self.wrpath = None
2665 self.wrpath = None
2666 self.doypath = None
2666 self.doypath = None
2667 self.subfolder = None
2667 self.subfolder = None
2668 self.isConfig = False
2668 self.isConfig = False
2669
2669
2670 def isNumber(str):
2670 def isNumber(str):
2671 """
2671 """
2672 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
2672 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
2673
2673
2674 Excepciones:
2674 Excepciones:
2675 Si un determinado string no puede ser convertido a numero
2675 Si un determinado string no puede ser convertido a numero
2676 Input:
2676 Input:
2677 str, string al cual se le analiza para determinar si convertible a un numero o no
2677 str, string al cual se le analiza para determinar si convertible a un numero o no
2678
2678
2679 Return:
2679 Return:
2680 True : si el string es uno numerico
2680 True : si el string es uno numerico
2681 False : no es un string numerico
2681 False : no es un string numerico
2682 """
2682 """
2683 try:
2683 try:
2684 float( str )
2684 float( str )
2685 return True
2685 return True
2686 except:
2686 except:
2687 return False
2687 return False
2688
2688
2689 def setup(self, dataOut, wrpath):
2689 def setup(self, dataOut, wrpath):
2690
2690
2691 if not(os.path.exists(wrpath)):
2691 if not(os.path.exists(wrpath)):
2692 os.mkdir(wrpath)
2692 os.mkdir(wrpath)
2693
2693
2694 self.wrpath = wrpath
2694 self.wrpath = wrpath
2695 # self.setFile = 0
2695 # self.setFile = 0
2696 self.dataOut = dataOut
2696 self.dataOut = dataOut
2697
2697
2698 def putData(self):
2698 def putData(self):
2699 name= time.localtime( self.dataOut.utctime)
2699 name= time.localtime( self.dataOut.utctime)
2700 ext=".fits"
2700 ext=".fits"
2701
2701
2702 if self.doypath == None:
2702 if self.doypath == None:
2703 self.subfolder = 'F%4.4d%3.3d_%d' % (name.tm_year,name.tm_yday,time.mktime(datetime.datetime.now().timetuple()))
2703 self.subfolder = 'F%4.4d%3.3d_%d' % (name.tm_year,name.tm_yday,time.mktime(datetime.datetime.now().timetuple()))
2704 self.doypath = os.path.join( self.wrpath, self.subfolder )
2704 self.doypath = os.path.join( self.wrpath, self.subfolder )
2705 os.mkdir(self.doypath)
2705 os.mkdir(self.doypath)
2706
2706
2707 if self.setFile == None:
2707 if self.setFile == None:
2708 # self.set = self.dataOut.set
2708 # self.set = self.dataOut.set
2709 self.setFile = 0
2709 self.setFile = 0
2710 # if self.set != self.dataOut.set:
2710 # if self.set != self.dataOut.set:
2711 ## self.set = self.dataOut.set
2711 ## self.set = self.dataOut.set
2712 # self.setFile = 0
2712 # self.setFile = 0
2713
2713
2714 #make the filename
2714 #make the filename
2715 file = 'D%4.4d%3.3d_%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext)
2715 file = 'D%4.4d%3.3d_%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext)
2716
2716
2717 filename = os.path.join(self.wrpath,self.subfolder, file)
2717 filename = os.path.join(self.wrpath,self.subfolder, file)
2718
2718
2719 idblock = numpy.array([self.idblock],dtype="int64")
2719 idblock = numpy.array([self.idblock],dtype="int64")
2720 header=self.wrObj.cFImage(idblock=idblock,
2720 header=self.wrObj.cFImage(idblock=idblock,
2721 year=time.gmtime(self.dataOut.utctime).tm_year,
2721 year=time.gmtime(self.dataOut.utctime).tm_year,
2722 month=time.gmtime(self.dataOut.utctime).tm_mon,
2722 month=time.gmtime(self.dataOut.utctime).tm_mon,
2723 day=time.gmtime(self.dataOut.utctime).tm_mday,
2723 day=time.gmtime(self.dataOut.utctime).tm_mday,
2724 hour=time.gmtime(self.dataOut.utctime).tm_hour,
2724 hour=time.gmtime(self.dataOut.utctime).tm_hour,
2725 minute=time.gmtime(self.dataOut.utctime).tm_min,
2725 minute=time.gmtime(self.dataOut.utctime).tm_min,
2726 second=time.gmtime(self.dataOut.utctime).tm_sec)
2726 second=time.gmtime(self.dataOut.utctime).tm_sec)
2727
2727
2728 c=3E8
2728 c=3E8
2729 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
2729 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
2730 freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)*(c/(2*deltaHeight*1000))
2730 freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)*(c/(2*deltaHeight*1000))
2731
2731
2732 colList = []
2732 colList = []
2733
2733
2734 colFreq=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq)
2734 colFreq=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq)
2735
2735
2736 colList.append(colFreq)
2736 colList.append(colFreq)
2737
2737
2738 nchannel=self.dataOut.nChannels
2738 nchannel=self.dataOut.nChannels
2739
2739
2740 for i in range(nchannel):
2740 for i in range(nchannel):
2741 col = self.wrObj.writeData(name="PCh"+str(i+1),
2741 col = self.wrObj.writeData(name="PCh"+str(i+1),
2742 format=str(self.dataOut.nFFTPoints)+'E',
2742 format=str(self.dataOut.nFFTPoints)+'E',
2743 data=10*numpy.log10(self.dataOut.data_spc[i,:]))
2743 data=10*numpy.log10(self.dataOut.data_spc[i,:]))
2744
2744
2745 colList.append(col)
2745 colList.append(col)
2746
2746
2747 data=self.wrObj.Ctable(colList=colList)
2747 data=self.wrObj.Ctable(colList=colList)
2748
2748
2749 self.wrObj.CFile(header,data)
2749 self.wrObj.CFile(header,data)
2750
2750
2751 self.wrObj.wFile(filename)
2751 self.wrObj.wFile(filename)
2752
2752
2753 #update the setFile
2753 #update the setFile
2754 self.setFile += 1
2754 self.setFile += 1
2755 self.idblock += 1
2755 self.idblock += 1
2756
2756
2757 return 1
2757 return 1
2758
2758
2759 def run(self, dataOut, **kwargs):
2759 def run(self, dataOut, **kwargs):
2760
2760
2761 if not(self.isConfig):
2761 if not(self.isConfig):
2762
2762
2763 self.setup(dataOut, **kwargs)
2763 self.setup(dataOut, **kwargs)
2764 self.isConfig = True
2764 self.isConfig = True
2765
2765
2766 self.putData()
2766 self.putData()
2767
2767
2768
2768
2769 class FITS:
2769 class FITS:
2770 name=None
2770 name=None
2771 format=None
2771 format=None
2772 array =None
2772 array =None
2773 data =None
2773 data =None
2774 thdulist=None
2774 thdulist=None
2775 prihdr=None
2775 prihdr=None
2776 hdu=None
2776 hdu=None
2777
2777
2778 def __init__(self):
2778 def __init__(self):
2779
2779
2780 pass
2780 pass
2781
2781
2782 def setColF(self,name,format,array):
2782 def setColF(self,name,format,array):
2783 self.name=name
2783 self.name=name
2784 self.format=format
2784 self.format=format
2785 self.array=array
2785 self.array=array
2786 a1=numpy.array([self.array],dtype=numpy.float32)
2786 a1=numpy.array([self.array],dtype=numpy.float32)
2787 self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1)
2787 self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1)
2788 return self.col1
2788 return self.col1
2789
2789
2790 # def setColP(self,name,format,data):
2790 # def setColP(self,name,format,data):
2791 # self.name=name
2791 # self.name=name
2792 # self.format=format
2792 # self.format=format
2793 # self.data=data
2793 # self.data=data
2794 # a2=numpy.array([self.data],dtype=numpy.float32)
2794 # a2=numpy.array([self.data],dtype=numpy.float32)
2795 # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2795 # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2796 # return self.col2
2796 # return self.col2
2797
2797
2798
2798
2799 def writeData(self,name,format,data):
2799 def writeData(self,name,format,data):
2800 self.name=name
2800 self.name=name
2801 self.format=format
2801 self.format=format
2802 self.data=data
2802 self.data=data
2803 a2=numpy.array([self.data],dtype=numpy.float32)
2803 a2=numpy.array([self.data],dtype=numpy.float32)
2804 self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2804 self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2805 return self.col2
2805 return self.col2
2806
2806
2807 def cFImage(self,idblock,year,month,day,hour,minute,second):
2807 def cFImage(self,idblock,year,month,day,hour,minute,second):
2808 self.hdu= pyfits.PrimaryHDU(idblock)
2808 self.hdu= pyfits.PrimaryHDU(idblock)
2809 self.hdu.header.set("Year",year)
2809 self.hdu.header.set("Year",year)
2810 self.hdu.header.set("Month",month)
2810 self.hdu.header.set("Month",month)
2811 self.hdu.header.set("Day",day)
2811 self.hdu.header.set("Day",day)
2812 self.hdu.header.set("Hour",hour)
2812 self.hdu.header.set("Hour",hour)
2813 self.hdu.header.set("Minute",minute)
2813 self.hdu.header.set("Minute",minute)
2814 self.hdu.header.set("Second",second)
2814 self.hdu.header.set("Second",second)
2815 return self.hdu
2815 return self.hdu
2816
2816
2817
2817
2818 def Ctable(self,colList):
2818 def Ctable(self,colList):
2819 self.cols=pyfits.ColDefs(colList)
2819 self.cols=pyfits.ColDefs(colList)
2820 self.tbhdu = pyfits.new_table(self.cols)
2820 self.tbhdu = pyfits.new_table(self.cols)
2821 return self.tbhdu
2821 return self.tbhdu
2822
2822
2823
2823
2824 def CFile(self,hdu,tbhdu):
2824 def CFile(self,hdu,tbhdu):
2825 self.thdulist=pyfits.HDUList([hdu,tbhdu])
2825 self.thdulist=pyfits.HDUList([hdu,tbhdu])
2826
2826
2827 def wFile(self,filename):
2827 def wFile(self,filename):
2828 if os.path.isfile(filename):
2828 if os.path.isfile(filename):
2829 os.remove(filename)
2829 os.remove(filename)
2830 self.thdulist.writeto(filename)
2830 self.thdulist.writeto(filename)
2831
2831
2832
2832
2833 class ParameterConf:
2833 class ParameterConf:
2834 ELEMENTNAME = 'Parameter'
2834 ELEMENTNAME = 'Parameter'
2835 def __init__(self):
2835 def __init__(self):
2836 self.name = ''
2836 self.name = ''
2837 self.value = ''
2837 self.value = ''
2838
2838
2839 def readXml(self, parmElement):
2839 def readXml(self, parmElement):
2840 self.name = parmElement.get('name')
2840 self.name = parmElement.get('name')
2841 self.value = parmElement.get('value')
2841 self.value = parmElement.get('value')
2842
2842
2843 def getElementName(self):
2843 def getElementName(self):
2844 return self.ELEMENTNAME
2844 return self.ELEMENTNAME
2845
2845
2846 class Metadata:
2846 class Metadata:
2847
2847
2848 def __init__(self, filename):
2848 def __init__(self, filename):
2849 self.parmConfObjList = []
2849 self.parmConfObjList = []
2850 self.readXml(filename)
2850 self.readXml(filename)
2851
2851
2852 def readXml(self, filename):
2852 def readXml(self, filename):
2853 self.projectElement = None
2853 self.projectElement = None
2854 self.procUnitConfObjDict = {}
2854 self.procUnitConfObjDict = {}
2855 self.projectElement = ElementTree().parse(filename)
2855 self.projectElement = ElementTree().parse(filename)
2856 self.project = self.projectElement.tag
2856 self.project = self.projectElement.tag
2857
2857
2858 parmElementList = self.projectElement.getiterator(ParameterConf().getElementName())
2858 parmElementList = self.projectElement.getiterator(ParameterConf().getElementName())
2859
2859
2860 for parmElement in parmElementList:
2860 for parmElement in parmElementList:
2861 parmConfObj = ParameterConf()
2861 parmConfObj = ParameterConf()
2862 parmConfObj.readXml(parmElement)
2862 parmConfObj.readXml(parmElement)
2863 self.parmConfObjList.append(parmConfObj)
2863 self.parmConfObjList.append(parmConfObj)
2864
2864
2865 class FitsWriter(Operation):
2865 class FitsWriter(Operation):
2866
2866
2867 def __init__(self):
2867 def __init__(self):
2868 self.isConfig = False
2868 self.isConfig = False
2869 self.dataBlocksPerFile = None
2869 self.dataBlocksPerFile = None
2870 self.blockIndex = 0
2870 self.blockIndex = 0
2871 self.flagIsNewFile = 1
2871 self.flagIsNewFile = 1
2872 self.fitsObj = None
2872 self.fitsObj = None
2873 self.optchar = 'P'
2873 self.optchar = 'P'
2874 self.ext = '.fits'
2874 self.ext = '.fits'
2875 self.setFile = 0
2875 self.setFile = 0
2876
2876
2877 def setFitsHeader(self, dataOut, metadatafile):
2877 def setFitsHeader(self, dataOut, metadatafile):
2878
2878
2879 header_data = pyfits.PrimaryHDU()
2879 header_data = pyfits.PrimaryHDU()
2880
2880
2881 metadata4fits = Metadata(metadatafile)
2881 metadata4fits = Metadata(metadatafile)
2882 for parameter in metadata4fits.parmConfObjList:
2882 for parameter in metadata4fits.parmConfObjList:
2883 parm_name = parameter.name
2883 parm_name = parameter.name
2884 parm_value = parameter.value
2884 parm_value = parameter.value
2885
2885
2886 if parm_value == 'fromdatadatetime':
2886 # if parm_value == 'fromdatadatetime':
2887 value = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple())
2887 # value = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple())
2888 elif parm_value == 'fromdataheights':
2888 # elif parm_value == 'fromdataheights':
2889 value = dataOut.nHeights
2889 # value = dataOut.nHeights
2890 elif parm_value == 'fromdatachannel':
2890 # elif parm_value == 'fromdatachannel':
2891 value = dataOut.nChannels
2891 # value = dataOut.nChannels
2892 elif parm_value == 'fromdatasamples':
2892 # elif parm_value == 'fromdatasamples':
2893 value = dataOut.nFFTPoints
2893 # value = dataOut.nFFTPoints
2894 else:
2894 # else:
2895 value = parm_value
2895 # value = parm_value
2896
2896
2897 header_data.header[parm_name] = value
2897 header_data.header[parm_name] = parm_value
2898
2898
2899
2900 header_data.header['DATETIME'] = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple())
2901 header_data.header['CHANNELLIST'] = str(dataOut.channelList)
2902 header_data.header['NCHANNELS'] = dataOut.nChannels
2903 #header_data.header['HEIGHTS'] = dataOut.heightList
2904 header_data.header['NHEIGHTS'] = dataOut.nHeights
2905
2906 header_data.header['IPPSECONDS'] = dataOut.ippSeconds
2907 header_data.header['NCOHINT'] = dataOut.nCohInt
2908 header_data.header['NINCOHINT'] = dataOut.nIncohInt
2909 header_data.header['TIMEZONE'] = dataOut.timeZone
2899 header_data.header['NBLOCK'] = self.blockIndex
2910 header_data.header['NBLOCK'] = self.blockIndex
2900
2911
2901 header_data.writeto(self.filename)
2912 header_data.writeto(self.filename)
2902
2913
2914 self.addExtension(dataOut.heightList,'HEIGHTLIST')
2915
2903
2916
2904 def setup(self, dataOut, path, dataBlocksPerFile, metadatafile):
2917 def setup(self, dataOut, path, dataBlocksPerFile, metadatafile):
2905
2918
2906 self.path = path
2919 self.path = path
2907 self.dataOut = dataOut
2920 self.dataOut = dataOut
2908 self.metadatafile = metadatafile
2921 self.metadatafile = metadatafile
2909 self.dataBlocksPerFile = dataBlocksPerFile
2922 self.dataBlocksPerFile = dataBlocksPerFile
2910
2923
2911 def open(self):
2924 def open(self):
2912 self.fitsObj = pyfits.open(self.filename, mode='update')
2925 self.fitsObj = pyfits.open(self.filename, mode='update')
2913
2926
2914
2927
2928 def addExtension(self, data, tagname):
2929 self.open()
2930 extension = pyfits.ImageHDU(data=data, name=tagname)
2931 #extension.header['TAG'] = tagname
2932 self.fitsObj.append(extension)
2933 self.write()
2934
2915 def addData(self, data):
2935 def addData(self, data):
2916 self.open()
2936 self.open()
2917 extension = pyfits.ImageHDU(data=data, name=self.fitsObj[0].header['DATA'])
2937 extension = pyfits.ImageHDU(data=data, name=self.fitsObj[0].header['DATATYPE'])
2918 extension.header['UTCTIME'] = self.dataOut.utctime
2938 extension.header['UTCTIME'] = self.dataOut.utctime
2919 self.fitsObj.append(extension)
2939 self.fitsObj.append(extension)
2920 self.blockIndex += 1
2940 self.blockIndex += 1
2921 self.fitsObj[0].header['NBLOCK'] = self.blockIndex
2941 self.fitsObj[0].header['NBLOCK'] = self.blockIndex
2922
2942
2923 self.write()
2943 self.write()
2924
2944
2925 def write(self):
2945 def write(self):
2926
2946
2927 self.fitsObj.flush(verbose=True)
2947 self.fitsObj.flush(verbose=True)
2928 self.fitsObj.close()
2948 self.fitsObj.close()
2929
2949
2930
2950
2931 def setNextFile(self):
2951 def setNextFile(self):
2932
2952
2933 ext = self.ext
2953 ext = self.ext
2934 path = self.path
2954 path = self.path
2935
2955
2936 timeTuple = time.localtime( self.dataOut.utctime)
2956 timeTuple = time.localtime( self.dataOut.utctime)
2937 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
2957 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
2938
2958
2939 fullpath = os.path.join( path, subfolder )
2959 fullpath = os.path.join( path, subfolder )
2940 if not( os.path.exists(fullpath) ):
2960 if not( os.path.exists(fullpath) ):
2941 os.mkdir(fullpath)
2961 os.mkdir(fullpath)
2942 self.setFile = -1 #inicializo mi contador de seteo
2962 self.setFile = -1 #inicializo mi contador de seteo
2943 else:
2963 else:
2944 filesList = os.listdir( fullpath )
2964 filesList = os.listdir( fullpath )
2945 if len( filesList ) > 0:
2965 if len( filesList ) > 0:
2946 filesList = sorted( filesList, key=str.lower )
2966 filesList = sorted( filesList, key=str.lower )
2947 filen = filesList[-1]
2967 filen = filesList[-1]
2948
2968
2949 if isNumber( filen[8:11] ):
2969 if isNumber( filen[8:11] ):
2950 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
2970 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
2951 else:
2971 else:
2952 self.setFile = -1
2972 self.setFile = -1
2953 else:
2973 else:
2954 self.setFile = -1 #inicializo mi contador de seteo
2974 self.setFile = -1 #inicializo mi contador de seteo
2955
2975
2956 setFile = self.setFile
2976 setFile = self.setFile
2957 setFile += 1
2977 setFile += 1
2958
2978
2959 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
2979 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
2960 timeTuple.tm_year,
2980 timeTuple.tm_year,
2961 timeTuple.tm_yday,
2981 timeTuple.tm_yday,
2962 setFile,
2982 setFile,
2963 ext )
2983 ext )
2964
2984
2965 filename = os.path.join( path, subfolder, file )
2985 filename = os.path.join( path, subfolder, file )
2966
2986
2967 self.blockIndex = 0
2987 self.blockIndex = 0
2968 self.filename = filename
2988 self.filename = filename
2969 self.setFile = setFile
2989 self.setFile = setFile
2970 self.flagIsNewFile = 1
2990 self.flagIsNewFile = 1
2971
2991
2972 print 'Writing the file: %s'%self.filename
2992 print 'Writing the file: %s'%self.filename
2973
2993
2974 self.setFitsHeader(self.dataOut, self.metadatafile)
2994 self.setFitsHeader(self.dataOut, self.metadatafile)
2975
2995
2976 return 1
2996 return 1
2977
2997
2978 def writeBlock(self):
2998 def writeBlock(self):
2979 self.addData(self.dataOut.data_spc)
2999 self.addData(self.dataOut.data_spc)
2980 self.flagIsNewFile = 0
3000 self.flagIsNewFile = 0
2981
3001
2982
3002
2983 def __setNewBlock(self):
3003 def __setNewBlock(self):
2984
3004
2985 if self.flagIsNewFile:
3005 if self.flagIsNewFile:
2986 return 1
3006 return 1
2987
3007
2988 if self.blockIndex < self.dataBlocksPerFile:
3008 if self.blockIndex < self.dataBlocksPerFile:
2989 return 1
3009 return 1
2990
3010
2991 if not( self.setNextFile() ):
3011 if not( self.setNextFile() ):
2992 return 0
3012 return 0
2993
3013
2994 return 1
3014 return 1
2995
3015
2996 def writeNextBlock(self):
3016 def writeNextBlock(self):
2997 if not( self.__setNewBlock() ):
3017 if not( self.__setNewBlock() ):
2998 return 0
3018 return 0
2999 self.writeBlock()
3019 self.writeBlock()
3000 return 1
3020 return 1
3001
3021
3002 def putData(self):
3022 def putData(self):
3003 if self.flagIsNewFile:
3023 if self.flagIsNewFile:
3004 self.setNextFile()
3024 self.setNextFile()
3005 self.writeNextBlock()
3025 self.writeNextBlock()
3006
3026
3007 def run(self, dataOut, **kwargs):
3027 def run(self, dataOut, **kwargs):
3008 if not(self.isConfig):
3028 if not(self.isConfig):
3009 self.setup(dataOut, **kwargs)
3029 self.setup(dataOut, **kwargs)
3010 self.isConfig = True
3030 self.isConfig = True
3011 self.putData()
3031 self.putData()
3012
3032
3013
3033
3014 class FitsReader(ProcessingUnit):
3034 class FitsReader(ProcessingUnit):
3015
3035
3016 __TIMEZONE = time.timezone
3036 # __TIMEZONE = time.timezone
3017
3037
3018 expName = None
3038 expName = None
3019 datetimestr = None
3039 datetimestr = None
3020 utc = None
3040 utc = None
3021 nChannels = None
3041 nChannels = None
3022 nSamples = None
3042 nSamples = None
3023 dataBlocksPerFile = None
3043 dataBlocksPerFile = None
3024 comments = None
3044 comments = None
3025 lastUTTime = None
3045 lastUTTime = None
3026 header_dict = None
3046 header_dict = None
3027 data = None
3047 data = None
3028 data_header_dict = None
3048 data_header_dict = None
3029
3049
3030 def __init__(self):
3050 def __init__(self):
3031 self.isConfig = False
3051 self.isConfig = False
3032 self.ext = '.fits'
3052 self.ext = '.fits'
3033 self.setFile = 0
3053 self.setFile = 0
3034 self.flagNoMoreFiles = 0
3054 self.flagNoMoreFiles = 0
3035 self.flagIsNewFile = 1
3055 self.flagIsNewFile = 1
3036 self.flagTimeBlock = None
3056 self.flagTimeBlock = None
3037 self.fileIndex = None
3057 self.fileIndex = None
3038 self.filename = None
3058 self.filename = None
3039 self.fileSize = None
3059 self.fileSize = None
3040 self.fitsObj = None
3060 self.fitsObj = None
3061 self.timeZone = None
3041 self.nReadBlocks = 0
3062 self.nReadBlocks = 0
3042 self.nTotalBlocks = 0
3063 self.nTotalBlocks = 0
3043 self.dataOut = self.createObjByDefault()
3064 self.dataOut = self.createObjByDefault()
3044 self.maxTimeStep = 10# deberia ser definido por el usuario usando el metodo setup()
3065 self.maxTimeStep = 10# deberia ser definido por el usuario usando el metodo setup()
3045 self.blockIndex = 1
3066 self.blockIndex = 1
3046
3067
3047 def createObjByDefault(self):
3068 def createObjByDefault(self):
3048
3069
3049 dataObj = Fits()
3070 dataObj = Fits()
3050
3071
3051 return dataObj
3072 return dataObj
3052
3073
3053 def isFileinThisTime(self, filename, startTime, endTime, useLocalTime=False):
3074 def isFileinThisTime(self, filename, startTime, endTime, useLocalTime=False):
3054 try:
3075 try:
3055 fitsObj = pyfits.open(filename,'readonly')
3076 fitsObj = pyfits.open(filename,'readonly')
3056 except:
3077 except:
3057 raise IOError, "The file %s can't be opened" %(filename)
3078 raise IOError, "The file %s can't be opened" %(filename)
3058
3079
3059 header = fitsObj[0].header
3080 header = fitsObj[0].header
3060 struct_time = time.strptime(header['DATETIME'], "%b %d %Y %H:%M:%S")
3081 struct_time = time.strptime(header['DATETIME'], "%b %d %Y %H:%M:%S")
3061 utc = time.mktime(struct_time) - time.timezone #TIMEZONE debe ser un parametro del header FITS
3082 utc = time.mktime(struct_time) - time.timezone #TIMEZONE debe ser un parametro del header FITS
3062
3083
3063 ltc = utc
3084 ltc = utc
3064 if useLocalTime:
3085 if useLocalTime:
3065 ltc -= time.timezone
3086 ltc -= time.timezone
3066 thisDatetime = datetime.datetime.utcfromtimestamp(ltc)
3087 thisDatetime = datetime.datetime.utcfromtimestamp(ltc)
3067 thisTime = thisDatetime.time()
3088 thisTime = thisDatetime.time()
3068
3089
3069 if not ((startTime <= thisTime) and (endTime > thisTime)):
3090 if not ((startTime <= thisTime) and (endTime > thisTime)):
3070 return None
3091 return None
3071
3092
3072 return thisDatetime
3093 return thisDatetime
3073
3094
3074 def __setNextFileOnline(self):
3095 def __setNextFileOnline(self):
3075 raise ValueError, "No implemented"
3096 raise ValueError, "No implemented"
3076
3097
3077 def __setNextFileOffline(self):
3098 def __setNextFileOffline(self):
3078 idFile = self.fileIndex
3099 idFile = self.fileIndex
3079
3100
3080 while (True):
3101 while (True):
3081 idFile += 1
3102 idFile += 1
3082 if not(idFile < len(self.filenameList)):
3103 if not(idFile < len(self.filenameList)):
3083 self.flagNoMoreFiles = 1
3104 self.flagNoMoreFiles = 1
3084 print "No more Files"
3105 print "No more Files"
3085 return 0
3106 return 0
3086
3107
3087 filename = self.filenameList[idFile]
3108 filename = self.filenameList[idFile]
3088
3109
3089 # if not(self.__verifyFile(filename)):
3110 # if not(self.__verifyFile(filename)):
3090 # continue
3111 # continue
3091
3112
3092 fileSize = os.path.getsize(filename)
3113 fileSize = os.path.getsize(filename)
3093 fitsObj = pyfits.open(filename,'readonly')
3114 fitsObj = pyfits.open(filename,'readonly')
3094 break
3115 break
3095
3116
3096 self.flagIsNewFile = 1
3117 self.flagIsNewFile = 1
3097 self.fileIndex = idFile
3118 self.fileIndex = idFile
3098 self.filename = filename
3119 self.filename = filename
3099 self.fileSize = fileSize
3120 self.fileSize = fileSize
3100 self.fitsObj = fitsObj
3121 self.fitsObj = fitsObj
3101
3122 self.blockIndex = 0
3102 print "Setting the file: %s"%self.filename
3123 print "Setting the file: %s"%self.filename
3103
3124
3104 return 1
3125 return 1
3105
3126
3106 def readHeader(self):
3127 def readHeader(self):
3107 headerObj = self.fitsObj[0]
3128 headerObj = self.fitsObj[0]
3108
3129
3109 self.header_dict = headerObj.header
3130 self.header_dict = headerObj.header
3110 self.expName = headerObj.header['EXPNAME']
3131 if 'EXPNAME' in headerObj.header.keys():
3132 self.expName = headerObj.header['EXPNAME']
3133
3134 if 'DATATYPE' in headerObj.header.keys():
3135 self.dataType = headerObj.header['DATATYPE']
3136
3111 self.datetimestr = headerObj.header['DATETIME']
3137 self.datetimestr = headerObj.header['DATETIME']
3112 struct_time = time.strptime(headerObj.header['DATETIME'], "%b %d %Y %H:%M:%S")
3138 self.channelList = headerObj.header['CHANNELLIST']
3113 # self.utc = time.mktime(struct_time) - self.__TIMEZONE
3139 self.nChannels = headerObj.header['NCHANNELS']
3114 self.nChannels = headerObj.header['NCHANNEL']
3140 self.nHeights = headerObj.header['NHEIGHTS']
3115 self.nSamples = headerObj.header['NSAMPLE']
3141 self.ippSeconds = headerObj.header['IPPSECONDS']
3142 self.nCohInt = headerObj.header['NCOHINT']
3143 self.nIncohInt = headerObj.header['NINCOHINT']
3116 self.dataBlocksPerFile = headerObj.header['NBLOCK']
3144 self.dataBlocksPerFile = headerObj.header['NBLOCK']
3117 self.comments = headerObj.header['COMMENT']
3145 self.timeZone = headerObj.header['TIMEZONE']
3146
3147 self.timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
3148
3149 if 'COMMENT' in headerObj.header.keys():
3150 self.comments = headerObj.header['COMMENT']
3118
3151
3152 self.readHeightList()
3153
3154 def readHeightList(self):
3155 self.blockIndex = self.blockIndex + 1
3156 obj = self.fitsObj[self.blockIndex]
3157 self.heightList = obj.data
3158 self.blockIndex = self.blockIndex + 1
3159
3160 def readExtension(self):
3161 obj = self.fitsObj[self.blockIndex]
3162 self.heightList = obj.data
3163 self.blockIndex = self.blockIndex + 1
3119
3164
3120 def setNextFile(self):
3165 def setNextFile(self):
3121
3166
3122 if self.online:
3167 if self.online:
3123 newFile = self.__setNextFileOnline()
3168 newFile = self.__setNextFileOnline()
3124 else:
3169 else:
3125 newFile = self.__setNextFileOffline()
3170 newFile = self.__setNextFileOffline()
3126
3171
3127 if not(newFile):
3172 if not(newFile):
3128 return 0
3173 return 0
3129
3174
3130 self.readHeader()
3175 self.readHeader()
3131
3176
3132 self.nReadBlocks = 0
3177 self.nReadBlocks = 0
3133 self.blockIndex = 1
3178 # self.blockIndex = 1
3134 return 1
3179 return 1
3135
3180
3136 def __searchFilesOffLine(self,
3181 def __searchFilesOffLine(self,
3137 path,
3182 path,
3138 startDate,
3183 startDate,
3139 endDate,
3184 endDate,
3140 startTime=datetime.time(0,0,0),
3185 startTime=datetime.time(0,0,0),
3141 endTime=datetime.time(23,59,59),
3186 endTime=datetime.time(23,59,59),
3142 set=None,
3187 set=None,
3143 expLabel='',
3188 expLabel='',
3144 ext='.fits',
3189 ext='.fits',
3145 walk=True):
3190 walk=True):
3146
3191
3147 pathList = []
3192 pathList = []
3148
3193
3149 if not walk:
3194 if not walk:
3150 pathList.append(path)
3195 pathList.append(path)
3151
3196
3152 else:
3197 else:
3153 dirList = []
3198 dirList = []
3154 for thisPath in os.listdir(path):
3199 for thisPath in os.listdir(path):
3155 if not os.path.isdir(os.path.join(path,thisPath)):
3200 if not os.path.isdir(os.path.join(path,thisPath)):
3156 continue
3201 continue
3157 if not isDoyFolder(thisPath):
3202 if not isDoyFolder(thisPath):
3158 continue
3203 continue
3159
3204
3160 dirList.append(thisPath)
3205 dirList.append(thisPath)
3161
3206
3162 if not(dirList):
3207 if not(dirList):
3163 return None, None
3208 return None, None
3164
3209
3165 thisDate = startDate
3210 thisDate = startDate
3166
3211
3167 while(thisDate <= endDate):
3212 while(thisDate <= endDate):
3168 year = thisDate.timetuple().tm_year
3213 year = thisDate.timetuple().tm_year
3169 doy = thisDate.timetuple().tm_yday
3214 doy = thisDate.timetuple().tm_yday
3170
3215
3171 matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*')
3216 matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*')
3172 if len(matchlist) == 0:
3217 if len(matchlist) == 0:
3173 thisDate += datetime.timedelta(1)
3218 thisDate += datetime.timedelta(1)
3174 continue
3219 continue
3175 for match in matchlist:
3220 for match in matchlist:
3176 pathList.append(os.path.join(path,match,expLabel))
3221 pathList.append(os.path.join(path,match,expLabel))
3177
3222
3178 thisDate += datetime.timedelta(1)
3223 thisDate += datetime.timedelta(1)
3179
3224
3180 if pathList == []:
3225 if pathList == []:
3181 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
3226 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
3182 return None, None
3227 return None, None
3183
3228
3184 print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate)
3229 print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate)
3185
3230
3186 filenameList = []
3231 filenameList = []
3187 datetimeList = []
3232 datetimeList = []
3188
3233
3189 for i in range(len(pathList)):
3234 for i in range(len(pathList)):
3190
3235
3191 thisPath = pathList[i]
3236 thisPath = pathList[i]
3192
3237
3193 fileList = glob.glob1(thisPath, "*%s" %ext)
3238 fileList = glob.glob1(thisPath, "*%s" %ext)
3194 fileList.sort()
3239 fileList.sort()
3195
3240
3196 for file in fileList:
3241 for file in fileList:
3197
3242
3198 filename = os.path.join(thisPath,file)
3243 filename = os.path.join(thisPath,file)
3199 thisDatetime = self.isFileinThisTime(filename, startTime, endTime, useLocalTime=True)
3244 thisDatetime = self.isFileinThisTime(filename, startTime, endTime, useLocalTime=True)
3200
3245
3201 if not(thisDatetime):
3246 if not(thisDatetime):
3202 continue
3247 continue
3203
3248
3204 filenameList.append(filename)
3249 filenameList.append(filename)
3205 datetimeList.append(thisDatetime)
3250 datetimeList.append(thisDatetime)
3206
3251
3207 if not(filenameList):
3252 if not(filenameList):
3208 print "Any file was found for the time range %s - %s" %(startTime, endTime)
3253 print "Any file was found for the time range %s - %s" %(startTime, endTime)
3209 return None, None
3254 return None, None
3210
3255
3211 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
3256 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
3212 print
3257 print
3213
3258
3214 for i in range(len(filenameList)):
3259 for i in range(len(filenameList)):
3215 print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
3260 print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
3216
3261
3217 self.filenameList = filenameList
3262 self.filenameList = filenameList
3218 self.datetimeList = datetimeList
3263 self.datetimeList = datetimeList
3219
3264
3220 return pathList, filenameList
3265 return pathList, filenameList
3221
3266
3222 def setup(self, path=None,
3267 def setup(self, path=None,
3223 startDate=None,
3268 startDate=None,
3224 endDate=None,
3269 endDate=None,
3225 startTime=datetime.time(0,0,0),
3270 startTime=datetime.time(0,0,0),
3226 endTime=datetime.time(23,59,59),
3271 endTime=datetime.time(23,59,59),
3227 set=0,
3272 set=0,
3228 expLabel = "",
3273 expLabel = "",
3229 ext = None,
3274 ext = None,
3230 online = False,
3275 online = False,
3231 delay = 60,
3276 delay = 60,
3232 walk = True):
3277 walk = True):
3233
3278
3234 if path == None:
3279 if path == None:
3235 raise ValueError, "The path is not valid"
3280 raise ValueError, "The path is not valid"
3236
3281
3237 if ext == None:
3282 if ext == None:
3238 ext = self.ext
3283 ext = self.ext
3239
3284
3240 if not(online):
3285 if not(online):
3241 print "Searching files in offline mode ..."
3286 print "Searching files in offline mode ..."
3242 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
3287 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
3243 startTime=startTime, endTime=endTime,
3288 startTime=startTime, endTime=endTime,
3244 set=set, expLabel=expLabel, ext=ext,
3289 set=set, expLabel=expLabel, ext=ext,
3245 walk=walk)
3290 walk=walk)
3246
3291
3247 if not(pathList):
3292 if not(pathList):
3248 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
3293 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
3249 datetime.datetime.combine(startDate,startTime).ctime(),
3294 datetime.datetime.combine(startDate,startTime).ctime(),
3250 datetime.datetime.combine(endDate,endTime).ctime())
3295 datetime.datetime.combine(endDate,endTime).ctime())
3251
3296
3252 sys.exit(-1)
3297 sys.exit(-1)
3253
3298
3254 self.fileIndex = -1
3299 self.fileIndex = -1
3255 self.pathList = pathList
3300 self.pathList = pathList
3256 self.filenameList = filenameList
3301 self.filenameList = filenameList
3257
3302
3258 self.online = online
3303 self.online = online
3259 self.delay = delay
3304 self.delay = delay
3260 ext = ext.lower()
3305 ext = ext.lower()
3261 self.ext = ext
3306 self.ext = ext
3262
3307
3263 if not(self.setNextFile()):
3308 if not(self.setNextFile()):
3264 if (startDate!=None) and (endDate!=None):
3309 if (startDate!=None) and (endDate!=None):
3265 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
3310 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
3266 elif startDate != None:
3311 elif startDate != None:
3267 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
3312 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
3268 else:
3313 else:
3269 print "No files"
3314 print "No files"
3270
3315
3271 sys.exit(-1)
3316 sys.exit(-1)
3272
3317
3273
3318
3274
3319
3275 def readBlock(self):
3320 def readBlock(self):
3276 dataObj = self.fitsObj[self.blockIndex]
3321 dataObj = self.fitsObj[self.blockIndex]
3277
3322
3278 self.data = dataObj.data
3323 self.data = dataObj.data
3279 self.data_header_dict = dataObj.header
3324 self.data_header_dict = dataObj.header
3280 self.utc = self.data_header_dict['UTCTIME']
3325 self.utc = self.data_header_dict['UTCTIME']
3281
3326
3282 self.flagIsNewFile = 0
3327 self.flagIsNewFile = 0
3283 self.blockIndex += 1
3328 self.blockIndex += 1
3284 self.nTotalBlocks += 1
3329 self.nTotalBlocks += 1
3285 self.nReadBlocks += 1
3330 self.nReadBlocks += 1
3286
3331
3287 return 1
3332 return 1
3288
3333
3289 def __jumpToLastBlock(self):
3334 def __jumpToLastBlock(self):
3290 raise ValueError, "No implemented"
3335 raise ValueError, "No implemented"
3291
3336
3292 def __waitNewBlock(self):
3337 def __waitNewBlock(self):
3293 """
3338 """
3294 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
3339 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
3295
3340
3296 Si el modo de lectura es OffLine siempre retorn 0
3341 Si el modo de lectura es OffLine siempre retorn 0
3297 """
3342 """
3298 if not self.online:
3343 if not self.online:
3299 return 0
3344 return 0
3300
3345
3301 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
3346 if (self.nReadBlocks >= self.dataBlocksPerFile):
3302 return 0
3347 return 0
3303
3348
3304 currentPointer = self.fp.tell()
3349 currentPointer = self.fp.tell()
3305
3350
3306 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
3351 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
3307
3352
3308 for nTries in range( self.nTries ):
3353 for nTries in range( self.nTries ):
3309
3354
3310 self.fp.close()
3355 self.fp.close()
3311 self.fp = open( self.filename, 'rb' )
3356 self.fp = open( self.filename, 'rb' )
3312 self.fp.seek( currentPointer )
3357 self.fp.seek( currentPointer )
3313
3358
3314 self.fileSize = os.path.getsize( self.filename )
3359 self.fileSize = os.path.getsize( self.filename )
3315 currentSize = self.fileSize - currentPointer
3360 currentSize = self.fileSize - currentPointer
3316
3361
3317 if ( currentSize >= neededSize ):
3362 if ( currentSize >= neededSize ):
3318 self.__rdBasicHeader()
3363 self.__rdBasicHeader()
3319 return 1
3364 return 1
3320
3365
3321 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
3366 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
3322 time.sleep( self.delay )
3367 time.sleep( self.delay )
3323
3368
3324
3369
3325 return 0
3370 return 0
3326
3371
3327 def __setNewBlock(self):
3372 def __setNewBlock(self):
3328
3373
3329 if self.online:
3374 if self.online:
3330 self.__jumpToLastBlock()
3375 self.__jumpToLastBlock()
3331
3376
3332 if self.flagIsNewFile:
3377 if self.flagIsNewFile:
3333 return 1
3378 return 1
3334
3379
3335 self.lastUTTime = self.utc
3380 self.lastUTTime = self.utc
3336
3381
3337 if self.online:
3382 if self.online:
3338 if self.__waitNewBlock():
3383 if self.__waitNewBlock():
3339 return 1
3384 return 1
3340
3385
3341 if self.nReadBlocks < self.dataBlocksPerFile:
3386 if self.nReadBlocks < self.dataBlocksPerFile:
3342 return 1
3387 return 1
3343
3388
3344 if not(self.setNextFile()):
3389 if not(self.setNextFile()):
3345 return 0
3390 return 0
3346
3391
3347 deltaTime = self.utc - self.lastUTTime
3392 deltaTime = self.utc - self.lastUTTime
3348
3393
3349 self.flagTimeBlock = 0
3394 self.flagTimeBlock = 0
3350
3395
3351 if deltaTime > self.maxTimeStep:
3396 if deltaTime > self.maxTimeStep:
3352 self.flagTimeBlock = 1
3397 self.flagTimeBlock = 1
3353
3398
3354 return 1
3399 return 1
3355
3400
3356
3401
3357 def readNextBlock(self):
3402 def readNextBlock(self):
3358 if not(self.__setNewBlock()):
3403 if not(self.__setNewBlock()):
3359 return 0
3404 return 0
3360
3405
3361 if not(self.readBlock()):
3406 if not(self.readBlock()):
3362 return 0
3407 return 0
3363
3408
3364 return 1
3409 return 1
3365
3410
3366
3411
3367 def getData(self):
3412 def getData(self):
3368
3413
3369 if self.flagNoMoreFiles:
3414 if self.flagNoMoreFiles:
3370 self.dataOut.flagNoData = True
3415 self.dataOut.flagNoData = True
3371 print 'Process finished'
3416 print 'Process finished'
3372 return 0
3417 return 0
3373
3418
3374 self.flagTimeBlock = 0
3419 self.flagTimeBlock = 0
3375 self.flagIsNewBlock = 0
3420 self.flagIsNewBlock = 0
3376
3421
3377 if not(self.readNextBlock()):
3422 if not(self.readNextBlock()):
3378 return 0
3423 return 0
3379
3424
3380 if self.data == None:
3425 if self.data == None:
3381 self.dataOut.flagNoData = True
3426 self.dataOut.flagNoData = True
3382 return 0
3427 return 0
3383
3428
3384 self.dataOut.data = self.data
3429 self.dataOut.data = self.data
3385 self.dataOut.data_header = self.data_header_dict
3430 self.dataOut.data_header = self.data_header_dict
3386 self.dataOut.utctime = self.utc
3431 self.dataOut.utctime = self.utc
3387
3432
3388 self.dataOut.header = self.header_dict
3433 self.dataOut.header = self.header_dict
3389 self.dataOut.expName = self.expName
3434 self.dataOut.expName = self.expName
3390 self.dataOut.nChannels = self.nChannels
3435 self.dataOut.nChannels = self.nChannels
3391 self.dataOut.nSamples = self.nSamples
3436 self.dataOut.timeZone = self.timeZone
3392 self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile
3437 self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile
3393 self.dataOut.comments = self.comments
3438 self.dataOut.comments = self.comments
3394
3439 self.dataOut.timeInterval = self.timeInterval
3440 self.dataOut.channelList = self.channelList
3441 self.dataOut.heightList = self.heightList
3395 self.dataOut.flagNoData = False
3442 self.dataOut.flagNoData = False
3396
3443
3397 return self.dataOut.data
3444 return self.dataOut.data
3398
3445
3399 def run(self, **kwargs):
3446 def run(self, **kwargs):
3400
3447
3401 if not(self.isConfig):
3448 if not(self.isConfig):
3402 self.setup(**kwargs)
3449 self.setup(**kwargs)
3403 self.isConfig = True
3450 self.isConfig = True
3404
3451
3405 self.getData() No newline at end of file
3452 self.getData()
@@ -1,1530 +1,1530
1 import numpy
1 import numpy
2 import time, datetime, os
2 import time, datetime, os
3 from graphics.figure import *
3 from graphics.figure import *
4 def isRealtime(utcdatatime):
4 def isRealtime(utcdatatime):
5 utcnow = time.mktime(time.localtime())
5 utcnow = time.mktime(time.localtime())
6 delta = abs(utcnow - utcdatatime) # abs
6 delta = abs(utcnow - utcdatatime) # abs
7 if delta >= 30.:
7 if delta >= 30.:
8 return False
8 return False
9 return True
9 return True
10
10
11 class CrossSpectraPlot(Figure):
11 class CrossSpectraPlot(Figure):
12
12
13 __isConfig = None
13 __isConfig = None
14 __nsubplots = None
14 __nsubplots = None
15
15
16 WIDTH = None
16 WIDTH = None
17 HEIGHT = None
17 HEIGHT = None
18 WIDTHPROF = None
18 WIDTHPROF = None
19 HEIGHTPROF = None
19 HEIGHTPROF = None
20 PREFIX = 'cspc'
20 PREFIX = 'cspc'
21
21
22 def __init__(self):
22 def __init__(self):
23
23
24 self.__isConfig = False
24 self.__isConfig = False
25 self.__nsubplots = 4
25 self.__nsubplots = 4
26 self.counter_imagwr = 0
26 self.counter_imagwr = 0
27 self.WIDTH = 250
27 self.WIDTH = 250
28 self.HEIGHT = 250
28 self.HEIGHT = 250
29 self.WIDTHPROF = 0
29 self.WIDTHPROF = 0
30 self.HEIGHTPROF = 0
30 self.HEIGHTPROF = 0
31
31
32 self.PLOT_CODE = 1
32 self.PLOT_CODE = 1
33 self.FTP_WEI = None
33 self.FTP_WEI = None
34 self.EXP_CODE = None
34 self.EXP_CODE = None
35 self.SUB_EXP_CODE = None
35 self.SUB_EXP_CODE = None
36 self.PLOT_POS = None
36 self.PLOT_POS = None
37
37
38 def getSubplots(self):
38 def getSubplots(self):
39
39
40 ncol = 4
40 ncol = 4
41 nrow = self.nplots
41 nrow = self.nplots
42
42
43 return nrow, ncol
43 return nrow, ncol
44
44
45 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
45 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
46
46
47 self.__showprofile = showprofile
47 self.__showprofile = showprofile
48 self.nplots = nplots
48 self.nplots = nplots
49
49
50 ncolspan = 1
50 ncolspan = 1
51 colspan = 1
51 colspan = 1
52
52
53 self.createFigure(id = id,
53 self.createFigure(id = id,
54 wintitle = wintitle,
54 wintitle = wintitle,
55 widthplot = self.WIDTH + self.WIDTHPROF,
55 widthplot = self.WIDTH + self.WIDTHPROF,
56 heightplot = self.HEIGHT + self.HEIGHTPROF,
56 heightplot = self.HEIGHT + self.HEIGHTPROF,
57 show=True)
57 show=True)
58
58
59 nrow, ncol = self.getSubplots()
59 nrow, ncol = self.getSubplots()
60
60
61 counter = 0
61 counter = 0
62 for y in range(nrow):
62 for y in range(nrow):
63 for x in range(ncol):
63 for x in range(ncol):
64 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
64 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
65
65
66 counter += 1
66 counter += 1
67
67
68 def run(self, dataOut, id, wintitle="", pairsList=None,
68 def run(self, dataOut, id, wintitle="", pairsList=None,
69 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
69 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
70 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
70 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
71 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
71 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
72 server=None, folder=None, username=None, password=None,
72 server=None, folder=None, username=None, password=None,
73 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
73 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
74
74
75 """
75 """
76
76
77 Input:
77 Input:
78 dataOut :
78 dataOut :
79 id :
79 id :
80 wintitle :
80 wintitle :
81 channelList :
81 channelList :
82 showProfile :
82 showProfile :
83 xmin : None,
83 xmin : None,
84 xmax : None,
84 xmax : None,
85 ymin : None,
85 ymin : None,
86 ymax : None,
86 ymax : None,
87 zmin : None,
87 zmin : None,
88 zmax : None
88 zmax : None
89 """
89 """
90
90
91 if pairsList == None:
91 if pairsList == None:
92 pairsIndexList = dataOut.pairsIndexList
92 pairsIndexList = dataOut.pairsIndexList
93 else:
93 else:
94 pairsIndexList = []
94 pairsIndexList = []
95 for pair in pairsList:
95 for pair in pairsList:
96 if pair not in dataOut.pairsList:
96 if pair not in dataOut.pairsList:
97 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
97 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
98 pairsIndexList.append(dataOut.pairsList.index(pair))
98 pairsIndexList.append(dataOut.pairsList.index(pair))
99
99
100 if pairsIndexList == []:
100 if pairsIndexList == []:
101 return
101 return
102
102
103 if len(pairsIndexList) > 4:
103 if len(pairsIndexList) > 4:
104 pairsIndexList = pairsIndexList[0:4]
104 pairsIndexList = pairsIndexList[0:4]
105 factor = dataOut.normFactor
105 factor = dataOut.normFactor
106 x = dataOut.getVelRange(1)
106 x = dataOut.getVelRange(1)
107 y = dataOut.getHeiRange()
107 y = dataOut.getHeiRange()
108 z = dataOut.data_spc[:,:,:]/factor
108 z = dataOut.data_spc[:,:,:]/factor
109 # z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
109 # z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
110 avg = numpy.abs(numpy.average(z, axis=1))
110 avg = numpy.abs(numpy.average(z, axis=1))
111 noise = dataOut.getNoise()/factor
111 noise = dataOut.getNoise()/factor
112
112
113 zdB = 10*numpy.log10(z)
113 zdB = 10*numpy.log10(z)
114 avgdB = 10*numpy.log10(avg)
114 avgdB = 10*numpy.log10(avg)
115 noisedB = 10*numpy.log10(noise)
115 noisedB = 10*numpy.log10(noise)
116
116
117
117
118 #thisDatetime = dataOut.datatime
118 #thisDatetime = dataOut.datatime
119 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
119 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
120 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
120 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
121 xlabel = "Velocity (m/s)"
121 xlabel = "Velocity (m/s)"
122 ylabel = "Range (Km)"
122 ylabel = "Range (Km)"
123
123
124 if not self.__isConfig:
124 if not self.__isConfig:
125
125
126 nplots = len(pairsIndexList)
126 nplots = len(pairsIndexList)
127
127
128 self.setup(id=id,
128 self.setup(id=id,
129 nplots=nplots,
129 nplots=nplots,
130 wintitle=wintitle,
130 wintitle=wintitle,
131 showprofile=False,
131 showprofile=False,
132 show=show)
132 show=show)
133
133
134 if xmin == None: xmin = numpy.nanmin(x)
134 if xmin == None: xmin = numpy.nanmin(x)
135 if xmax == None: xmax = numpy.nanmax(x)
135 if xmax == None: xmax = numpy.nanmax(x)
136 if ymin == None: ymin = numpy.nanmin(y)
136 if ymin == None: ymin = numpy.nanmin(y)
137 if ymax == None: ymax = numpy.nanmax(y)
137 if ymax == None: ymax = numpy.nanmax(y)
138 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
138 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
139 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
139 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
140
140
141 self.FTP_WEI = ftp_wei
141 self.FTP_WEI = ftp_wei
142 self.EXP_CODE = exp_code
142 self.EXP_CODE = exp_code
143 self.SUB_EXP_CODE = sub_exp_code
143 self.SUB_EXP_CODE = sub_exp_code
144 self.PLOT_POS = plot_pos
144 self.PLOT_POS = plot_pos
145
145
146 self.__isConfig = True
146 self.__isConfig = True
147
147
148 self.setWinTitle(title)
148 self.setWinTitle(title)
149
149
150 for i in range(self.nplots):
150 for i in range(self.nplots):
151 pair = dataOut.pairsList[pairsIndexList[i]]
151 pair = dataOut.pairsList[pairsIndexList[i]]
152 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
152 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
153 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[pair[0]], str_datetime)
153 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[pair[0]], str_datetime)
154 zdB = 10.*numpy.log10(dataOut.data_spc[pair[0],:,:]/factor)
154 zdB = 10.*numpy.log10(dataOut.data_spc[pair[0],:,:]/factor)
155 axes0 = self.axesList[i*self.__nsubplots]
155 axes0 = self.axesList[i*self.__nsubplots]
156 axes0.pcolor(x, y, zdB,
156 axes0.pcolor(x, y, zdB,
157 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
157 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
158 xlabel=xlabel, ylabel=ylabel, title=title,
158 xlabel=xlabel, ylabel=ylabel, title=title,
159 ticksize=9, colormap=power_cmap, cblabel='')
159 ticksize=9, colormap=power_cmap, cblabel='')
160
160
161 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[pair[1]], str_datetime)
161 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[pair[1]], str_datetime)
162 zdB = 10.*numpy.log10(dataOut.data_spc[pair[1],:,:]/factor)
162 zdB = 10.*numpy.log10(dataOut.data_spc[pair[1],:,:]/factor)
163 axes0 = self.axesList[i*self.__nsubplots+1]
163 axes0 = self.axesList[i*self.__nsubplots+1]
164 axes0.pcolor(x, y, zdB,
164 axes0.pcolor(x, y, zdB,
165 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
165 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
166 xlabel=xlabel, ylabel=ylabel, title=title,
166 xlabel=xlabel, ylabel=ylabel, title=title,
167 ticksize=9, colormap=power_cmap, cblabel='')
167 ticksize=9, colormap=power_cmap, cblabel='')
168
168
169 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:])
169 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:])
170 coherence = numpy.abs(coherenceComplex)
170 coherence = numpy.abs(coherenceComplex)
171 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
171 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
172 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
172 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
173
173
174 title = "Coherence %d%d" %(pair[0], pair[1])
174 title = "Coherence %d%d" %(pair[0], pair[1])
175 axes0 = self.axesList[i*self.__nsubplots+2]
175 axes0 = self.axesList[i*self.__nsubplots+2]
176 axes0.pcolor(x, y, coherence,
176 axes0.pcolor(x, y, coherence,
177 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=1,
177 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=1,
178 xlabel=xlabel, ylabel=ylabel, title=title,
178 xlabel=xlabel, ylabel=ylabel, title=title,
179 ticksize=9, colormap=coherence_cmap, cblabel='')
179 ticksize=9, colormap=coherence_cmap, cblabel='')
180
180
181 title = "Phase %d%d" %(pair[0], pair[1])
181 title = "Phase %d%d" %(pair[0], pair[1])
182 axes0 = self.axesList[i*self.__nsubplots+3]
182 axes0 = self.axesList[i*self.__nsubplots+3]
183 axes0.pcolor(x, y, phase,
183 axes0.pcolor(x, y, phase,
184 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180,
184 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180,
185 xlabel=xlabel, ylabel=ylabel, title=title,
185 xlabel=xlabel, ylabel=ylabel, title=title,
186 ticksize=9, colormap=phase_cmap, cblabel='')
186 ticksize=9, colormap=phase_cmap, cblabel='')
187
187
188
188
189
189
190 self.draw()
190 self.draw()
191
191
192 if save:
192 if save:
193
193
194 self.counter_imagwr += 1
194 self.counter_imagwr += 1
195 if (self.counter_imagwr==wr_period):
195 if (self.counter_imagwr==wr_period):
196 if figfile == None:
196 if figfile == None:
197 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
197 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
198 figfile = self.getFilename(name = str_datetime)
198 figfile = self.getFilename(name = str_datetime)
199
199
200 self.saveFigure(figpath, figfile)
200 self.saveFigure(figpath, figfile)
201
201
202 if ftp:
202 if ftp:
203 #provisionalmente envia archivos en el formato de la web en tiempo real
203 #provisionalmente envia archivos en el formato de la web en tiempo real
204 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
204 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
205 path = '%s%03d' %(self.PREFIX, self.id)
205 path = '%s%03d' %(self.PREFIX, self.id)
206 ftp_file = os.path.join(path,'ftp','%s.png'%name)
206 ftp_file = os.path.join(path,'ftp','%s.png'%name)
207 self.saveFigure(figpath, ftp_file)
207 self.saveFigure(figpath, ftp_file)
208 ftp_filename = os.path.join(figpath,ftp_file)
208 ftp_filename = os.path.join(figpath,ftp_file)
209
209
210 try:
210 try:
211 self.sendByFTP(ftp_filename, server, folder, username, password)
211 self.sendByFTP(ftp_filename, server, folder, username, password)
212 except:
212 except:
213 self.counter_imagwr = 0
213 self.counter_imagwr = 0
214 print ValueError, 'Error FTP'
214 print ValueError, 'Error FTP'
215
215
216 self.counter_imagwr = 0
216 self.counter_imagwr = 0
217
217
218
218
219 class RTIPlot(Figure):
219 class RTIPlot(Figure):
220
220
221 __isConfig = None
221 __isConfig = None
222 __nsubplots = None
222 __nsubplots = None
223
223
224 WIDTHPROF = None
224 WIDTHPROF = None
225 HEIGHTPROF = None
225 HEIGHTPROF = None
226 PREFIX = 'rti'
226 PREFIX = 'rti'
227
227
228 def __init__(self):
228 def __init__(self):
229
229
230 self.timerange = 2*60*60
230 self.timerange = 2*60*60
231 self.__isConfig = False
231 self.__isConfig = False
232 self.__nsubplots = 1
232 self.__nsubplots = 1
233
233
234 self.WIDTH = 800
234 self.WIDTH = 800
235 self.HEIGHT = 150
235 self.HEIGHT = 150
236 self.WIDTHPROF = 120
236 self.WIDTHPROF = 120
237 self.HEIGHTPROF = 0
237 self.HEIGHTPROF = 0
238 self.counter_imagwr = 0
238 self.counter_imagwr = 0
239
239
240 self.PLOT_CODE = 0
240 self.PLOT_CODE = 0
241 self.FTP_WEI = None
241 self.FTP_WEI = None
242 self.EXP_CODE = None
242 self.EXP_CODE = None
243 self.SUB_EXP_CODE = None
243 self.SUB_EXP_CODE = None
244 self.PLOT_POS = None
244 self.PLOT_POS = None
245
245
246 def getSubplots(self):
246 def getSubplots(self):
247
247
248 ncol = 1
248 ncol = 1
249 nrow = self.nplots
249 nrow = self.nplots
250
250
251 return nrow, ncol
251 return nrow, ncol
252
252
253 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
253 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
254
254
255 self.__showprofile = showprofile
255 self.__showprofile = showprofile
256 self.nplots = nplots
256 self.nplots = nplots
257
257
258 ncolspan = 1
258 ncolspan = 1
259 colspan = 1
259 colspan = 1
260 if showprofile:
260 if showprofile:
261 ncolspan = 7
261 ncolspan = 7
262 colspan = 6
262 colspan = 6
263 self.__nsubplots = 2
263 self.__nsubplots = 2
264
264
265 self.createFigure(id = id,
265 self.createFigure(id = id,
266 wintitle = wintitle,
266 wintitle = wintitle,
267 widthplot = self.WIDTH + self.WIDTHPROF,
267 widthplot = self.WIDTH + self.WIDTHPROF,
268 heightplot = self.HEIGHT + self.HEIGHTPROF,
268 heightplot = self.HEIGHT + self.HEIGHTPROF,
269 show=show)
269 show=show)
270
270
271 nrow, ncol = self.getSubplots()
271 nrow, ncol = self.getSubplots()
272
272
273 counter = 0
273 counter = 0
274 for y in range(nrow):
274 for y in range(nrow):
275 for x in range(ncol):
275 for x in range(ncol):
276
276
277 if counter >= self.nplots:
277 if counter >= self.nplots:
278 break
278 break
279
279
280 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
280 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
281
281
282 if showprofile:
282 if showprofile:
283 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
283 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
284
284
285 counter += 1
285 counter += 1
286
286
287 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
287 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
288 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
288 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
289 timerange=None,
289 timerange=None,
290 save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True,
290 save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True,
291 server=None, folder=None, username=None, password=None,
291 server=None, folder=None, username=None, password=None,
292 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
292 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
293
293
294 """
294 """
295
295
296 Input:
296 Input:
297 dataOut :
297 dataOut :
298 id :
298 id :
299 wintitle :
299 wintitle :
300 channelList :
300 channelList :
301 showProfile :
301 showProfile :
302 xmin : None,
302 xmin : None,
303 xmax : None,
303 xmax : None,
304 ymin : None,
304 ymin : None,
305 ymax : None,
305 ymax : None,
306 zmin : None,
306 zmin : None,
307 zmax : None
307 zmax : None
308 """
308 """
309
309
310 if channelList == None:
310 if channelList == None:
311 channelIndexList = dataOut.channelIndexList
311 channelIndexList = dataOut.channelIndexList
312 else:
312 else:
313 channelIndexList = []
313 channelIndexList = []
314 for channel in channelList:
314 for channel in channelList:
315 if channel not in dataOut.channelList:
315 if channel not in dataOut.channelList:
316 raise ValueError, "Channel %d is not in dataOut.channelList"
316 raise ValueError, "Channel %d is not in dataOut.channelList"
317 channelIndexList.append(dataOut.channelList.index(channel))
317 channelIndexList.append(dataOut.channelList.index(channel))
318
318
319 if timerange != None:
319 if timerange != None:
320 self.timerange = timerange
320 self.timerange = timerange
321
321
322 tmin = None
322 tmin = None
323 tmax = None
323 tmax = None
324 factor = dataOut.normFactor
324 factor = dataOut.normFactor
325 x = dataOut.getTimeRange()
325 x = dataOut.getTimeRange()
326 y = dataOut.getHeiRange()
326 y = dataOut.getHeiRange()
327
327
328 z = dataOut.data_spc[channelIndexList,:,:]/factor
328 z = dataOut.data_spc[channelIndexList,:,:]/factor
329 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
329 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
330 avg = numpy.average(z, axis=1)
330 avg = numpy.average(z, axis=1)
331
331
332 avgdB = 10.*numpy.log10(avg)
332 avgdB = 10.*numpy.log10(avg)
333
333
334
334
335 # thisDatetime = dataOut.datatime
335 # thisDatetime = dataOut.datatime
336 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
336 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
337 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
337 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
338 xlabel = ""
338 xlabel = ""
339 ylabel = "Range (Km)"
339 ylabel = "Range (Km)"
340
340
341 if not self.__isConfig:
341 if not self.__isConfig:
342
342
343 nplots = len(channelIndexList)
343 nplots = len(channelIndexList)
344
344
345 self.setup(id=id,
345 self.setup(id=id,
346 nplots=nplots,
346 nplots=nplots,
347 wintitle=wintitle,
347 wintitle=wintitle,
348 showprofile=showprofile,
348 showprofile=showprofile,
349 show=show)
349 show=show)
350
350
351 tmin, tmax = self.getTimeLim(x, xmin, xmax)
351 tmin, tmax = self.getTimeLim(x, xmin, xmax)
352 if ymin == None: ymin = numpy.nanmin(y)
352 if ymin == None: ymin = numpy.nanmin(y)
353 if ymax == None: ymax = numpy.nanmax(y)
353 if ymax == None: ymax = numpy.nanmax(y)
354 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
354 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
355 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
355 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
356
356
357 self.FTP_WEI = ftp_wei
357 self.FTP_WEI = ftp_wei
358 self.EXP_CODE = exp_code
358 self.EXP_CODE = exp_code
359 self.SUB_EXP_CODE = sub_exp_code
359 self.SUB_EXP_CODE = sub_exp_code
360 self.PLOT_POS = plot_pos
360 self.PLOT_POS = plot_pos
361
361
362 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
362 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
363 self.__isConfig = True
363 self.__isConfig = True
364
364
365
365
366 self.setWinTitle(title)
366 self.setWinTitle(title)
367
367
368 for i in range(self.nplots):
368 for i in range(self.nplots):
369 title = "Channel %d: %s" %(dataOut.channelList[i]+1, thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
369 title = "Channel %d: %s" %(dataOut.channelList[i]+1, thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
370 axes = self.axesList[i*self.__nsubplots]
370 axes = self.axesList[i*self.__nsubplots]
371 zdB = avgdB[i].reshape((1,-1))
371 zdB = avgdB[i].reshape((1,-1))
372 axes.pcolorbuffer(x, y, zdB,
372 axes.pcolorbuffer(x, y, zdB,
373 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
373 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
374 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
374 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
375 ticksize=9, cblabel='', cbsize="1%")
375 ticksize=9, cblabel='', cbsize="1%")
376
376
377 if self.__showprofile:
377 if self.__showprofile:
378 axes = self.axesList[i*self.__nsubplots +1]
378 axes = self.axesList[i*self.__nsubplots +1]
379 axes.pline(avgdB[i], y,
379 axes.pline(avgdB[i], y,
380 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
380 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
381 xlabel='dB', ylabel='', title='',
381 xlabel='dB', ylabel='', title='',
382 ytick_visible=False,
382 ytick_visible=False,
383 grid='x')
383 grid='x')
384
384
385 self.draw()
385 self.draw()
386
386
387 if save:
387 if save:
388
388
389 self.counter_imagwr += 1
389 self.counter_imagwr += 1
390 if (self.counter_imagwr==wr_period):
390 if (self.counter_imagwr==wr_period):
391 if figfile == None:
391 if figfile == None:
392 figfile = self.getFilename(name = self.name)
392 figfile = self.getFilename(name = self.name)
393 self.saveFigure(figpath, figfile)
393 self.saveFigure(figpath, figfile)
394
394
395 if ftp:
395 if ftp:
396 #provisionalmente envia archivos en el formato de la web en tiempo real
396 #provisionalmente envia archivos en el formato de la web en tiempo real
397 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
397 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
398 path = '%s%03d' %(self.PREFIX, self.id)
398 path = '%s%03d' %(self.PREFIX, self.id)
399 ftp_file = os.path.join(path,'ftp','%s.png'%name)
399 ftp_file = os.path.join(path,'ftp','%s.png'%name)
400 self.saveFigure(figpath, ftp_file)
400 self.saveFigure(figpath, ftp_file)
401 ftp_filename = os.path.join(figpath,ftp_file)
401 ftp_filename = os.path.join(figpath,ftp_file)
402 try:
402 try:
403 self.sendByFTP(ftp_filename, server, folder, username, password)
403 self.sendByFTP(ftp_filename, server, folder, username, password)
404 except:
404 except:
405 self.counter_imagwr = 0
405 self.counter_imagwr = 0
406 print ValueError, 'Error FTP'
406 print ValueError, 'Error FTP'
407
407
408 self.counter_imagwr = 0
408 self.counter_imagwr = 0
409
409
410 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
410 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
411 self.__isConfig = False
411 self.__isConfig = False
412
412
413 class SpectraPlot(Figure):
413 class SpectraPlot(Figure):
414
414
415 __isConfig = None
415 __isConfig = None
416 __nsubplots = None
416 __nsubplots = None
417
417
418 WIDTHPROF = None
418 WIDTHPROF = None
419 HEIGHTPROF = None
419 HEIGHTPROF = None
420 PREFIX = 'spc'
420 PREFIX = 'spc'
421
421
422 def __init__(self):
422 def __init__(self):
423
423
424 self.__isConfig = False
424 self.__isConfig = False
425 self.__nsubplots = 1
425 self.__nsubplots = 1
426
426
427 self.WIDTH = 280
427 self.WIDTH = 280
428 self.HEIGHT = 250
428 self.HEIGHT = 250
429 self.WIDTHPROF = 120
429 self.WIDTHPROF = 120
430 self.HEIGHTPROF = 0
430 self.HEIGHTPROF = 0
431 self.counter_imagwr = 0
431 self.counter_imagwr = 0
432
432
433 self.PLOT_CODE = 1
433 self.PLOT_CODE = 1
434 self.FTP_WEI = None
434 self.FTP_WEI = None
435 self.EXP_CODE = None
435 self.EXP_CODE = None
436 self.SUB_EXP_CODE = None
436 self.SUB_EXP_CODE = None
437 self.PLOT_POS = None
437 self.PLOT_POS = None
438
438
439 def getSubplots(self):
439 def getSubplots(self):
440
440
441 ncol = int(numpy.sqrt(self.nplots)+0.9)
441 ncol = int(numpy.sqrt(self.nplots)+0.9)
442 nrow = int(self.nplots*1./ncol + 0.9)
442 nrow = int(self.nplots*1./ncol + 0.9)
443
443
444 return nrow, ncol
444 return nrow, ncol
445
445
446 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
446 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
447
447
448 self.__showprofile = showprofile
448 self.__showprofile = showprofile
449 self.nplots = nplots
449 self.nplots = nplots
450
450
451 ncolspan = 1
451 ncolspan = 1
452 colspan = 1
452 colspan = 1
453 if showprofile:
453 if showprofile:
454 ncolspan = 3
454 ncolspan = 3
455 colspan = 2
455 colspan = 2
456 self.__nsubplots = 2
456 self.__nsubplots = 2
457
457
458 self.createFigure(id = id,
458 self.createFigure(id = id,
459 wintitle = wintitle,
459 wintitle = wintitle,
460 widthplot = self.WIDTH + self.WIDTHPROF,
460 widthplot = self.WIDTH + self.WIDTHPROF,
461 heightplot = self.HEIGHT + self.HEIGHTPROF,
461 heightplot = self.HEIGHT + self.HEIGHTPROF,
462 show=show)
462 show=show)
463
463
464 nrow, ncol = self.getSubplots()
464 nrow, ncol = self.getSubplots()
465
465
466 counter = 0
466 counter = 0
467 for y in range(nrow):
467 for y in range(nrow):
468 for x in range(ncol):
468 for x in range(ncol):
469
469
470 if counter >= self.nplots:
470 if counter >= self.nplots:
471 break
471 break
472
472
473 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
473 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
474
474
475 if showprofile:
475 if showprofile:
476 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
476 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
477
477
478 counter += 1
478 counter += 1
479
479
480 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
480 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
481 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
481 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
482 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
482 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
483 server=None, folder=None, username=None, password=None,
483 server=None, folder=None, username=None, password=None,
484 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
484 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
485
485
486 """
486 """
487
487
488 Input:
488 Input:
489 dataOut :
489 dataOut :
490 id :
490 id :
491 wintitle :
491 wintitle :
492 channelList :
492 channelList :
493 showProfile :
493 showProfile :
494 xmin : None,
494 xmin : None,
495 xmax : None,
495 xmax : None,
496 ymin : None,
496 ymin : None,
497 ymax : None,
497 ymax : None,
498 zmin : None,
498 zmin : None,
499 zmax : None
499 zmax : None
500 """
500 """
501
501
502 if realtime:
502 if realtime:
503 if not(isRealtime(utcdatatime = dataOut.utctime)):
503 if not(isRealtime(utcdatatime = dataOut.utctime)):
504 print 'Skipping this plot function'
504 print 'Skipping this plot function'
505 return
505 return
506
506
507 if channelList == None:
507 if channelList == None:
508 channelIndexList = dataOut.channelIndexList
508 channelIndexList = dataOut.channelIndexList
509 else:
509 else:
510 channelIndexList = []
510 channelIndexList = []
511 for channel in channelList:
511 for channel in channelList:
512 if channel not in dataOut.channelList:
512 if channel not in dataOut.channelList:
513 raise ValueError, "Channel %d is not in dataOut.channelList"
513 raise ValueError, "Channel %d is not in dataOut.channelList"
514 channelIndexList.append(dataOut.channelList.index(channel))
514 channelIndexList.append(dataOut.channelList.index(channel))
515 factor = dataOut.normFactor
515 factor = dataOut.normFactor
516 x = dataOut.getVelRange(1)
516 x = dataOut.getVelRange(1)
517 y = dataOut.getHeiRange()
517 y = dataOut.getHeiRange()
518
518
519 z = dataOut.data_spc[channelIndexList,:,:]/factor
519 z = dataOut.data_spc[channelIndexList,:,:]/factor
520 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
520 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
521 avg = numpy.average(z, axis=1)
521 avg = numpy.average(z, axis=1)
522 noise = dataOut.getNoise()/factor
522 noise = dataOut.getNoise()/factor
523
523
524 zdB = 10*numpy.log10(z)
524 zdB = 10*numpy.log10(z)
525 avgdB = 10*numpy.log10(avg)
525 avgdB = 10*numpy.log10(avg)
526 noisedB = 10*numpy.log10(noise)
526 noisedB = 10*numpy.log10(noise)
527
527
528 #thisDatetime = dataOut.datatime
528 #thisDatetime = dataOut.datatime
529 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
529 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
530 title = wintitle + " Spectra"
530 title = wintitle + " Spectra"
531 xlabel = "Velocity (m/s)"
531 xlabel = "Velocity (m/s)"
532 ylabel = "Range (Km)"
532 ylabel = "Range (Km)"
533
533
534 if not self.__isConfig:
534 if not self.__isConfig:
535
535
536 nplots = len(channelIndexList)
536 nplots = len(channelIndexList)
537
537
538 self.setup(id=id,
538 self.setup(id=id,
539 nplots=nplots,
539 nplots=nplots,
540 wintitle=wintitle,
540 wintitle=wintitle,
541 showprofile=showprofile,
541 showprofile=showprofile,
542 show=show)
542 show=show)
543
543
544 if xmin == None: xmin = numpy.nanmin(x)
544 if xmin == None: xmin = numpy.nanmin(x)
545 if xmax == None: xmax = numpy.nanmax(x)
545 if xmax == None: xmax = numpy.nanmax(x)
546 if ymin == None: ymin = numpy.nanmin(y)
546 if ymin == None: ymin = numpy.nanmin(y)
547 if ymax == None: ymax = numpy.nanmax(y)
547 if ymax == None: ymax = numpy.nanmax(y)
548 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
548 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
549 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
549 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
550
550
551 self.FTP_WEI = ftp_wei
551 self.FTP_WEI = ftp_wei
552 self.EXP_CODE = exp_code
552 self.EXP_CODE = exp_code
553 self.SUB_EXP_CODE = sub_exp_code
553 self.SUB_EXP_CODE = sub_exp_code
554 self.PLOT_POS = plot_pos
554 self.PLOT_POS = plot_pos
555
555
556 self.__isConfig = True
556 self.__isConfig = True
557
557
558 self.setWinTitle(title)
558 self.setWinTitle(title)
559
559
560 for i in range(self.nplots):
560 for i in range(self.nplots):
561 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
561 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
562 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i]+1, noisedB[i], str_datetime)
562 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i]+1, noisedB[i], str_datetime)
563 axes = self.axesList[i*self.__nsubplots]
563 axes = self.axesList[i*self.__nsubplots]
564 axes.pcolor(x, y, zdB[i,:,:],
564 axes.pcolor(x, y, zdB[i,:,:],
565 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
565 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
566 xlabel=xlabel, ylabel=ylabel, title=title,
566 xlabel=xlabel, ylabel=ylabel, title=title,
567 ticksize=9, cblabel='')
567 ticksize=9, cblabel='')
568
568
569 if self.__showprofile:
569 if self.__showprofile:
570 axes = self.axesList[i*self.__nsubplots +1]
570 axes = self.axesList[i*self.__nsubplots +1]
571 axes.pline(avgdB[i], y,
571 axes.pline(avgdB[i], y,
572 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
572 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
573 xlabel='dB', ylabel='', title='',
573 xlabel='dB', ylabel='', title='',
574 ytick_visible=False,
574 ytick_visible=False,
575 grid='x')
575 grid='x')
576
576
577 noiseline = numpy.repeat(noisedB[i], len(y))
577 noiseline = numpy.repeat(noisedB[i], len(y))
578 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
578 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
579
579
580 self.draw()
580 self.draw()
581
581
582 if save:
582 if save:
583
583
584 self.counter_imagwr += 1
584 self.counter_imagwr += 1
585 if (self.counter_imagwr==wr_period):
585 if (self.counter_imagwr==wr_period):
586 if figfile == None:
586 if figfile == None:
587 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
587 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
588 figfile = self.getFilename(name = str_datetime)
588 figfile = self.getFilename(name = str_datetime)
589
589
590 self.saveFigure(figpath, figfile)
590 self.saveFigure(figpath, figfile)
591
591
592 if ftp:
592 if ftp:
593 #provisionalmente envia archivos en el formato de la web en tiempo real
593 #provisionalmente envia archivos en el formato de la web en tiempo real
594 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
594 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
595 path = '%s%03d' %(self.PREFIX, self.id)
595 path = '%s%03d' %(self.PREFIX, self.id)
596 ftp_file = os.path.join(path,'ftp','%s.png'%name)
596 ftp_file = os.path.join(path,'ftp','%s.png'%name)
597 self.saveFigure(figpath, ftp_file)
597 self.saveFigure(figpath, ftp_file)
598 ftp_filename = os.path.join(figpath,ftp_file)
598 ftp_filename = os.path.join(figpath,ftp_file)
599 self.sendByFTP_Thread(ftp_filename, server, folder, username, password)
599 self.sendByFTP_Thread(ftp_filename, server, folder, username, password)
600 self.counter_imagwr = 0
600 self.counter_imagwr = 0
601
601
602
602
603 self.counter_imagwr = 0
603 self.counter_imagwr = 0
604
604
605
605
606 class Scope(Figure):
606 class Scope(Figure):
607
607
608 __isConfig = None
608 __isConfig = None
609
609
610 def __init__(self):
610 def __init__(self):
611
611
612 self.__isConfig = False
612 self.__isConfig = False
613 self.WIDTH = 600
613 self.WIDTH = 600
614 self.HEIGHT = 200
614 self.HEIGHT = 200
615
615
616 def getSubplots(self):
616 def getSubplots(self):
617
617
618 nrow = self.nplots
618 nrow = self.nplots
619 ncol = 3
619 ncol = 3
620 return nrow, ncol
620 return nrow, ncol
621
621
622 def setup(self, id, nplots, wintitle, show):
622 def setup(self, id, nplots, wintitle, show):
623
623
624 self.nplots = nplots
624 self.nplots = nplots
625
625
626 self.createFigure(id=id,
626 self.createFigure(id=id,
627 wintitle=wintitle,
627 wintitle=wintitle,
628 show=show)
628 show=show)
629
629
630 nrow,ncol = self.getSubplots()
630 nrow,ncol = self.getSubplots()
631 colspan = 3
631 colspan = 3
632 rowspan = 1
632 rowspan = 1
633
633
634 for i in range(nplots):
634 for i in range(nplots):
635 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
635 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
636
636
637
637
638
638
639 def run(self, dataOut, id, wintitle="", channelList=None,
639 def run(self, dataOut, id, wintitle="", channelList=None,
640 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
640 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
641 figpath='./', figfile=None, show=True):
641 figpath='./', figfile=None, show=True):
642
642
643 """
643 """
644
644
645 Input:
645 Input:
646 dataOut :
646 dataOut :
647 id :
647 id :
648 wintitle :
648 wintitle :
649 channelList :
649 channelList :
650 xmin : None,
650 xmin : None,
651 xmax : None,
651 xmax : None,
652 ymin : None,
652 ymin : None,
653 ymax : None,
653 ymax : None,
654 """
654 """
655
655
656 if channelList == None:
656 if channelList == None:
657 channelIndexList = dataOut.channelIndexList
657 channelIndexList = dataOut.channelIndexList
658 else:
658 else:
659 channelIndexList = []
659 channelIndexList = []
660 for channel in channelList:
660 for channel in channelList:
661 if channel not in dataOut.channelList:
661 if channel not in dataOut.channelList:
662 raise ValueError, "Channel %d is not in dataOut.channelList"
662 raise ValueError, "Channel %d is not in dataOut.channelList"
663 channelIndexList.append(dataOut.channelList.index(channel))
663 channelIndexList.append(dataOut.channelList.index(channel))
664
664
665 x = dataOut.heightList
665 x = dataOut.heightList
666 y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
666 y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
667 y = y.real
667 y = y.real
668
668
669 #thisDatetime = dataOut.datatime
669 #thisDatetime = dataOut.datatime
670 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
670 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
671 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
671 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
672 xlabel = "Range (Km)"
672 xlabel = "Range (Km)"
673 ylabel = "Intensity"
673 ylabel = "Intensity"
674
674
675 if not self.__isConfig:
675 if not self.__isConfig:
676 nplots = len(channelIndexList)
676 nplots = len(channelIndexList)
677
677
678 self.setup(id=id,
678 self.setup(id=id,
679 nplots=nplots,
679 nplots=nplots,
680 wintitle=wintitle,
680 wintitle=wintitle,
681 show=show)
681 show=show)
682
682
683 if xmin == None: xmin = numpy.nanmin(x)
683 if xmin == None: xmin = numpy.nanmin(x)
684 if xmax == None: xmax = numpy.nanmax(x)
684 if xmax == None: xmax = numpy.nanmax(x)
685 if ymin == None: ymin = numpy.nanmin(y)
685 if ymin == None: ymin = numpy.nanmin(y)
686 if ymax == None: ymax = numpy.nanmax(y)
686 if ymax == None: ymax = numpy.nanmax(y)
687
687
688 self.__isConfig = True
688 self.__isConfig = True
689
689
690 self.setWinTitle(title)
690 self.setWinTitle(title)
691
691
692 for i in range(len(self.axesList)):
692 for i in range(len(self.axesList)):
693 title = "Channel %d" %(i)
693 title = "Channel %d" %(i)
694 axes = self.axesList[i]
694 axes = self.axesList[i]
695 ychannel = y[i,:]
695 ychannel = y[i,:]
696 axes.pline(x, ychannel,
696 axes.pline(x, ychannel,
697 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
697 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
698 xlabel=xlabel, ylabel=ylabel, title=title)
698 xlabel=xlabel, ylabel=ylabel, title=title)
699
699
700 self.draw()
700 self.draw()
701
701
702 if save:
702 if save:
703 date = thisDatetime.strftime("%Y%m%d_%H%M%S")
703 date = thisDatetime.strftime("%Y%m%d_%H%M%S")
704 if figfile == None:
704 if figfile == None:
705 figfile = self.getFilename(name = date)
705 figfile = self.getFilename(name = date)
706
706
707 self.saveFigure(figpath, figfile)
707 self.saveFigure(figpath, figfile)
708
708
709 class PowerProfilePlot(Figure):
709 class PowerProfilePlot(Figure):
710 __isConfig = None
710 __isConfig = None
711 __nsubplots = None
711 __nsubplots = None
712
712
713 WIDTHPROF = None
713 WIDTHPROF = None
714 HEIGHTPROF = None
714 HEIGHTPROF = None
715 PREFIX = 'spcprofile'
715 PREFIX = 'spcprofile'
716
716
717 def __init__(self):
717 def __init__(self):
718 self.__isConfig = False
718 self.__isConfig = False
719 self.__nsubplots = 1
719 self.__nsubplots = 1
720
720
721 self.WIDTH = 300
721 self.WIDTH = 300
722 self.HEIGHT = 500
722 self.HEIGHT = 500
723
723
724 def getSubplots(self):
724 def getSubplots(self):
725 ncol = 1
725 ncol = 1
726 nrow = 1
726 nrow = 1
727
727
728 return nrow, ncol
728 return nrow, ncol
729
729
730 def setup(self, id, nplots, wintitle, show):
730 def setup(self, id, nplots, wintitle, show):
731
731
732 self.nplots = nplots
732 self.nplots = nplots
733
733
734 ncolspan = 1
734 ncolspan = 1
735 colspan = 1
735 colspan = 1
736
736
737 self.createFigure(id = id,
737 self.createFigure(id = id,
738 wintitle = wintitle,
738 wintitle = wintitle,
739 widthplot = self.WIDTH,
739 widthplot = self.WIDTH,
740 heightplot = self.HEIGHT,
740 heightplot = self.HEIGHT,
741 show=show)
741 show=show)
742
742
743 nrow, ncol = self.getSubplots()
743 nrow, ncol = self.getSubplots()
744
744
745 counter = 0
745 counter = 0
746 for y in range(nrow):
746 for y in range(nrow):
747 for x in range(ncol):
747 for x in range(ncol):
748 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
748 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
749
749
750 def run(self, dataOut, id, wintitle="", channelList=None,
750 def run(self, dataOut, id, wintitle="", channelList=None,
751 xmin=None, xmax=None, ymin=None, ymax=None,
751 xmin=None, xmax=None, ymin=None, ymax=None,
752 save=False, figpath='./', figfile=None, show=True):
752 save=False, figpath='./', figfile=None, show=True):
753
753
754 if channelList == None:
754 if channelList == None:
755 channelIndexList = dataOut.channelIndexList
755 channelIndexList = dataOut.channelIndexList
756 channelList = dataOut.channelList
756 channelList = dataOut.channelList
757 else:
757 else:
758 channelIndexList = []
758 channelIndexList = []
759 for channel in channelList:
759 for channel in channelList:
760 if channel not in dataOut.channelList:
760 if channel not in dataOut.channelList:
761 raise ValueError, "Channel %d is not in dataOut.channelList"
761 raise ValueError, "Channel %d is not in dataOut.channelList"
762 channelIndexList.append(dataOut.channelList.index(channel))
762 channelIndexList.append(dataOut.channelList.index(channel))
763
763
764 factor = dataOut.normFactor
764 factor = dataOut.normFactor
765 y = dataOut.getHeiRange()
765 y = dataOut.getHeiRange()
766 x = dataOut.data_spc[channelIndexList,:,:]/factor
766 x = dataOut.data_spc[channelIndexList,:,:]/factor
767 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
767 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
768 avg = numpy.average(x, axis=1)
768 avg = numpy.average(x, axis=1)
769
769
770 avgdB = 10*numpy.log10(avg)
770 avgdB = 10*numpy.log10(avg)
771
771
772 #thisDatetime = dataOut.datatime
772 #thisDatetime = dataOut.datatime
773 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
773 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
774 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
774 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
775 xlabel = "dB"
775 xlabel = "dB"
776 ylabel = "Range (Km)"
776 ylabel = "Range (Km)"
777
777
778 if not self.__isConfig:
778 if not self.__isConfig:
779
779
780 nplots = 1
780 nplots = 1
781
781
782 self.setup(id=id,
782 self.setup(id=id,
783 nplots=nplots,
783 nplots=nplots,
784 wintitle=wintitle,
784 wintitle=wintitle,
785 show=show)
785 show=show)
786
786
787 if ymin == None: ymin = numpy.nanmin(y)
787 if ymin == None: ymin = numpy.nanmin(y)
788 if ymax == None: ymax = numpy.nanmax(y)
788 if ymax == None: ymax = numpy.nanmax(y)
789 if xmin == None: xmin = numpy.nanmin(avgdB)*0.9
789 if xmin == None: xmin = numpy.nanmin(avgdB)*0.9
790 if xmax == None: xmax = numpy.nanmax(avgdB)*0.9
790 if xmax == None: xmax = numpy.nanmax(avgdB)*0.9
791
791
792 self.__isConfig = True
792 self.__isConfig = True
793
793
794 self.setWinTitle(title)
794 self.setWinTitle(title)
795
795
796
796
797 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
797 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
798 axes = self.axesList[0]
798 axes = self.axesList[0]
799
799
800 legendlabels = ["channel %d"%x for x in channelList]
800 legendlabels = ["channel %d"%x for x in channelList]
801 axes.pmultiline(avgdB, y,
801 axes.pmultiline(avgdB, y,
802 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
802 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
803 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
803 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
804 ytick_visible=True, nxticks=5,
804 ytick_visible=True, nxticks=5,
805 grid='x')
805 grid='x')
806
806
807 self.draw()
807 self.draw()
808
808
809 if save:
809 if save:
810 date = thisDatetime.strftime("%Y%m%d")
810 date = thisDatetime.strftime("%Y%m%d")
811 if figfile == None:
811 if figfile == None:
812 figfile = self.getFilename(name = date)
812 figfile = self.getFilename(name = date)
813
813
814 self.saveFigure(figpath, figfile)
814 self.saveFigure(figpath, figfile)
815
815
816 class CoherenceMap(Figure):
816 class CoherenceMap(Figure):
817 __isConfig = None
817 __isConfig = None
818 __nsubplots = None
818 __nsubplots = None
819
819
820 WIDTHPROF = None
820 WIDTHPROF = None
821 HEIGHTPROF = None
821 HEIGHTPROF = None
822 PREFIX = 'cmap'
822 PREFIX = 'cmap'
823
823
824 def __init__(self):
824 def __init__(self):
825 self.timerange = 2*60*60
825 self.timerange = 2*60*60
826 self.__isConfig = False
826 self.__isConfig = False
827 self.__nsubplots = 1
827 self.__nsubplots = 1
828
828
829 self.WIDTH = 800
829 self.WIDTH = 800
830 self.HEIGHT = 150
830 self.HEIGHT = 150
831 self.WIDTHPROF = 120
831 self.WIDTHPROF = 120
832 self.HEIGHTPROF = 0
832 self.HEIGHTPROF = 0
833 self.counter_imagwr = 0
833 self.counter_imagwr = 0
834
834
835 self.PLOT_CODE = 3
835 self.PLOT_CODE = 3
836 self.FTP_WEI = None
836 self.FTP_WEI = None
837 self.EXP_CODE = None
837 self.EXP_CODE = None
838 self.SUB_EXP_CODE = None
838 self.SUB_EXP_CODE = None
839 self.PLOT_POS = None
839 self.PLOT_POS = None
840 self.counter_imagwr = 0
840 self.counter_imagwr = 0
841
841
842 def getSubplots(self):
842 def getSubplots(self):
843 ncol = 1
843 ncol = 1
844 nrow = self.nplots*2
844 nrow = self.nplots*2
845
845
846 return nrow, ncol
846 return nrow, ncol
847
847
848 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
848 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
849 self.__showprofile = showprofile
849 self.__showprofile = showprofile
850 self.nplots = nplots
850 self.nplots = nplots
851
851
852 ncolspan = 1
852 ncolspan = 1
853 colspan = 1
853 colspan = 1
854 if showprofile:
854 if showprofile:
855 ncolspan = 7
855 ncolspan = 7
856 colspan = 6
856 colspan = 6
857 self.__nsubplots = 2
857 self.__nsubplots = 2
858
858
859 self.createFigure(id = id,
859 self.createFigure(id = id,
860 wintitle = wintitle,
860 wintitle = wintitle,
861 widthplot = self.WIDTH + self.WIDTHPROF,
861 widthplot = self.WIDTH + self.WIDTHPROF,
862 heightplot = self.HEIGHT + self.HEIGHTPROF,
862 heightplot = self.HEIGHT + self.HEIGHTPROF,
863 show=True)
863 show=True)
864
864
865 nrow, ncol = self.getSubplots()
865 nrow, ncol = self.getSubplots()
866
866
867 for y in range(nrow):
867 for y in range(nrow):
868 for x in range(ncol):
868 for x in range(ncol):
869
869
870 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
870 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
871
871
872 if showprofile:
872 if showprofile:
873 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
873 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
874
874
875 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
875 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
876 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
876 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
877 timerange=None,
877 timerange=None,
878 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
878 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
879 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
879 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
880 server=None, folder=None, username=None, password=None,
880 server=None, folder=None, username=None, password=None,
881 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
881 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
882
882
883 if pairsList == None:
883 if pairsList == None:
884 pairsIndexList = dataOut.pairsIndexList
884 pairsIndexList = dataOut.pairsIndexList
885 else:
885 else:
886 pairsIndexList = []
886 pairsIndexList = []
887 for pair in pairsList:
887 for pair in pairsList:
888 if pair not in dataOut.pairsList:
888 if pair not in dataOut.pairsList:
889 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
889 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
890 pairsIndexList.append(dataOut.pairsList.index(pair))
890 pairsIndexList.append(dataOut.pairsList.index(pair))
891
891
892 if timerange != None:
892 if timerange != None:
893 self.timerange = timerange
893 self.timerange = timerange
894
894
895 if pairsIndexList == []:
895 if pairsIndexList == []:
896 return
896 return
897
897
898 if len(pairsIndexList) > 4:
898 if len(pairsIndexList) > 4:
899 pairsIndexList = pairsIndexList[0:4]
899 pairsIndexList = pairsIndexList[0:4]
900
900
901 tmin = None
901 tmin = None
902 tmax = None
902 tmax = None
903 x = dataOut.getTimeRange()
903 x = dataOut.getTimeRange()
904 y = dataOut.getHeiRange()
904 y = dataOut.getHeiRange()
905
905
906 #thisDatetime = dataOut.datatime
906 #thisDatetime = dataOut.datatime
907 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
907 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
908 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
908 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
909 xlabel = ""
909 xlabel = ""
910 ylabel = "Range (Km)"
910 ylabel = "Range (Km)"
911
911
912 if not self.__isConfig:
912 if not self.__isConfig:
913 nplots = len(pairsIndexList)
913 nplots = len(pairsIndexList)
914 self.setup(id=id,
914 self.setup(id=id,
915 nplots=nplots,
915 nplots=nplots,
916 wintitle=wintitle,
916 wintitle=wintitle,
917 showprofile=showprofile,
917 showprofile=showprofile,
918 show=show)
918 show=show)
919
919
920 tmin, tmax = self.getTimeLim(x, xmin, xmax)
920 tmin, tmax = self.getTimeLim(x, xmin, xmax)
921 if ymin == None: ymin = numpy.nanmin(y)
921 if ymin == None: ymin = numpy.nanmin(y)
922 if ymax == None: ymax = numpy.nanmax(y)
922 if ymax == None: ymax = numpy.nanmax(y)
923 if zmin == None: zmin = 0.
923 if zmin == None: zmin = 0.
924 if zmax == None: zmax = 1.
924 if zmax == None: zmax = 1.
925
925
926 self.FTP_WEI = ftp_wei
926 self.FTP_WEI = ftp_wei
927 self.EXP_CODE = exp_code
927 self.EXP_CODE = exp_code
928 self.SUB_EXP_CODE = sub_exp_code
928 self.SUB_EXP_CODE = sub_exp_code
929 self.PLOT_POS = plot_pos
929 self.PLOT_POS = plot_pos
930
930
931 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
931 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
932
932
933 self.__isConfig = True
933 self.__isConfig = True
934
934
935 self.setWinTitle(title)
935 self.setWinTitle(title)
936
936
937 for i in range(self.nplots):
937 for i in range(self.nplots):
938
938
939 pair = dataOut.pairsList[pairsIndexList[i]]
939 pair = dataOut.pairsList[pairsIndexList[i]]
940 # coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:])
940 # coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:])
941 # avgcoherenceComplex = numpy.average(coherenceComplex, axis=0)
941 # avgcoherenceComplex = numpy.average(coherenceComplex, axis=0)
942 # coherence = numpy.abs(avgcoherenceComplex)
942 # coherence = numpy.abs(avgcoherenceComplex)
943
943
944 ## coherence = numpy.abs(coherenceComplex)
944 ## coherence = numpy.abs(coherenceComplex)
945 ## avg = numpy.average(coherence, axis=0)
945 ## avg = numpy.average(coherence, axis=0)
946
946
947 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
947 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
948 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
948 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
949 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
949 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
950
950
951
951
952 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
952 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
953 coherence = numpy.abs(avgcoherenceComplex)
953 coherence = numpy.abs(avgcoherenceComplex)
954
954
955 z = coherence.reshape((1,-1))
955 z = coherence.reshape((1,-1))
956
956
957 counter = 0
957 counter = 0
958
958
959 title = "Coherence %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
959 title = "Coherence %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
960 axes = self.axesList[i*self.__nsubplots*2]
960 axes = self.axesList[i*self.__nsubplots*2]
961 axes.pcolorbuffer(x, y, z,
961 axes.pcolorbuffer(x, y, z,
962 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
962 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
963 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
963 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
964 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
964 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
965
965
966 if self.__showprofile:
966 if self.__showprofile:
967 counter += 1
967 counter += 1
968 axes = self.axesList[i*self.__nsubplots*2 + counter]
968 axes = self.axesList[i*self.__nsubplots*2 + counter]
969 axes.pline(coherence, y,
969 axes.pline(coherence, y,
970 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
970 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
971 xlabel='', ylabel='', title='', ticksize=7,
971 xlabel='', ylabel='', title='', ticksize=7,
972 ytick_visible=False, nxticks=5,
972 ytick_visible=False, nxticks=5,
973 grid='x')
973 grid='x')
974
974
975 counter += 1
975 counter += 1
976 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
976 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
977 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
977 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
978 # avg = numpy.average(phase, axis=0)
978 # avg = numpy.average(phase, axis=0)
979 z = phase.reshape((1,-1))
979 z = phase.reshape((1,-1))
980
980
981 title = "Phase %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
981 title = "Phase %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
982 axes = self.axesList[i*self.__nsubplots*2 + counter]
982 axes = self.axesList[i*self.__nsubplots*2 + counter]
983 axes.pcolorbuffer(x, y, z,
983 axes.pcolorbuffer(x, y, z,
984 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180,
984 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180,
985 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
985 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
986 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
986 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
987
987
988 if self.__showprofile:
988 if self.__showprofile:
989 counter += 1
989 counter += 1
990 axes = self.axesList[i*self.__nsubplots*2 + counter]
990 axes = self.axesList[i*self.__nsubplots*2 + counter]
991 axes.pline(phase, y,
991 axes.pline(phase, y,
992 xmin=-180, xmax=180, ymin=ymin, ymax=ymax,
992 xmin=-180, xmax=180, ymin=ymin, ymax=ymax,
993 xlabel='', ylabel='', title='', ticksize=7,
993 xlabel='', ylabel='', title='', ticksize=7,
994 ytick_visible=False, nxticks=4,
994 ytick_visible=False, nxticks=4,
995 grid='x')
995 grid='x')
996
996
997 self.draw()
997 self.draw()
998
998
999 if save:
999 if save:
1000
1000
1001 self.counter_imagwr += 1
1001 self.counter_imagwr += 1
1002 if (self.counter_imagwr==wr_period):
1002 if (self.counter_imagwr==wr_period):
1003 if figfile == None:
1003 if figfile == None:
1004 figfile = self.getFilename(name = self.name)
1004 figfile = self.getFilename(name = self.name)
1005 self.saveFigure(figpath, figfile)
1005 self.saveFigure(figpath, figfile)
1006
1006
1007 if ftp:
1007 if ftp:
1008 #provisionalmente envia archivos en el formato de la web en tiempo real
1008 #provisionalmente envia archivos en el formato de la web en tiempo real
1009 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
1009 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
1010 path = '%s%03d' %(self.PREFIX, self.id)
1010 path = '%s%03d' %(self.PREFIX, self.id)
1011 ftp_file = os.path.join(path,'ftp','%s.png'%name)
1011 ftp_file = os.path.join(path,'ftp','%s.png'%name)
1012 self.saveFigure(figpath, ftp_file)
1012 self.saveFigure(figpath, ftp_file)
1013 ftp_filename = os.path.join(figpath,ftp_file)
1013 ftp_filename = os.path.join(figpath,ftp_file)
1014 try:
1014 try:
1015 self.sendByFTP(ftp_filename, server, folder, username, password)
1015 self.sendByFTP(ftp_filename, server, folder, username, password)
1016 except:
1016 except:
1017 self.counter_imagwr = 0
1017 self.counter_imagwr = 0
1018 print ValueError, 'Error FTP'
1018 print ValueError, 'Error FTP'
1019
1019
1020 self.counter_imagwr = 0
1020 self.counter_imagwr = 0
1021
1021
1022
1022
1023 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
1023 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
1024 self.__isConfig = False
1024 self.__isConfig = False
1025
1025
1026 class Noise(Figure):
1026 class Noise(Figure):
1027
1027
1028 __isConfig = None
1028 __isConfig = None
1029 __nsubplots = None
1029 __nsubplots = None
1030
1030
1031 PREFIX = 'noise'
1031 PREFIX = 'noise'
1032
1032
1033 def __init__(self):
1033 def __init__(self):
1034
1034
1035 self.timerange = 24*60*60
1035 self.timerange = 24*60*60
1036 self.__isConfig = False
1036 self.__isConfig = False
1037 self.__nsubplots = 1
1037 self.__nsubplots = 1
1038 self.counter_imagwr = 0
1038 self.counter_imagwr = 0
1039 self.WIDTH = 600
1039 self.WIDTH = 600
1040 self.HEIGHT = 300
1040 self.HEIGHT = 300
1041 self.WIDTHPROF = 120
1041 self.WIDTHPROF = 120
1042 self.HEIGHTPROF = 0
1042 self.HEIGHTPROF = 0
1043 self.xdata = None
1043 self.xdata = None
1044 self.ydata = None
1044 self.ydata = None
1045
1045
1046 self.PLOT_CODE = 77
1046 self.PLOT_CODE = 77
1047 self.FTP_WEI = None
1047 self.FTP_WEI = None
1048 self.EXP_CODE = None
1048 self.EXP_CODE = None
1049 self.SUB_EXP_CODE = None
1049 self.SUB_EXP_CODE = None
1050 self.PLOT_POS = None
1050 self.PLOT_POS = None
1051
1051
1052 def getSubplots(self):
1052 def getSubplots(self):
1053
1053
1054 ncol = 1
1054 ncol = 1
1055 nrow = 1
1055 nrow = 1
1056
1056
1057 return nrow, ncol
1057 return nrow, ncol
1058
1058
1059 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1059 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1060
1060
1061 self.__showprofile = showprofile
1061 self.__showprofile = showprofile
1062 self.nplots = nplots
1062 self.nplots = nplots
1063
1063
1064 ncolspan = 7
1064 ncolspan = 7
1065 colspan = 6
1065 colspan = 6
1066 self.__nsubplots = 2
1066 self.__nsubplots = 2
1067
1067
1068 self.createFigure(id = id,
1068 self.createFigure(id = id,
1069 wintitle = wintitle,
1069 wintitle = wintitle,
1070 widthplot = self.WIDTH+self.WIDTHPROF,
1070 widthplot = self.WIDTH+self.WIDTHPROF,
1071 heightplot = self.HEIGHT+self.HEIGHTPROF,
1071 heightplot = self.HEIGHT+self.HEIGHTPROF,
1072 show=show)
1072 show=show)
1073
1073
1074 nrow, ncol = self.getSubplots()
1074 nrow, ncol = self.getSubplots()
1075
1075
1076 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1076 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1077
1077
1078
1078
1079 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1079 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1080 xmin=None, xmax=None, ymin=None, ymax=None,
1080 xmin=None, xmax=None, ymin=None, ymax=None,
1081 timerange=None,
1081 timerange=None,
1082 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1082 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1083 server=None, folder=None, username=None, password=None,
1083 server=None, folder=None, username=None, password=None,
1084 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1084 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1085
1085
1086 if channelList == None:
1086 if channelList == None:
1087 channelIndexList = dataOut.channelIndexList
1087 channelIndexList = dataOut.channelIndexList
1088 channelList = dataOut.channelList
1088 channelList = dataOut.channelList
1089 else:
1089 else:
1090 channelIndexList = []
1090 channelIndexList = []
1091 for channel in channelList:
1091 for channel in channelList:
1092 if channel not in dataOut.channelList:
1092 if channel not in dataOut.channelList:
1093 raise ValueError, "Channel %d is not in dataOut.channelList"
1093 raise ValueError, "Channel %d is not in dataOut.channelList"
1094 channelIndexList.append(dataOut.channelList.index(channel))
1094 channelIndexList.append(dataOut.channelList.index(channel))
1095
1095
1096 if timerange != None:
1096 if timerange != None:
1097 self.timerange = timerange
1097 self.timerange = timerange
1098
1098
1099 tmin = None
1099 tmin = None
1100 tmax = None
1100 tmax = None
1101 x = dataOut.getTimeRange()
1101 x = dataOut.getTimeRange()
1102 y = dataOut.getHeiRange()
1102 y = dataOut.getHeiRange()
1103 factor = dataOut.normFactor
1103 factor = dataOut.normFactor
1104 noise = dataOut.getNoise()/factor
1104 noise = dataOut.getNoise()/factor
1105 noisedB = 10*numpy.log10(noise)
1105 noisedB = 10*numpy.log10(noise)
1106
1106
1107 #thisDatetime = dataOut.datatime
1107 #thisDatetime = dataOut.datatime
1108 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
1108 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
1109 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1109 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1110 xlabel = ""
1110 xlabel = ""
1111 ylabel = "Intensity (dB)"
1111 ylabel = "Intensity (dB)"
1112
1112
1113 if not self.__isConfig:
1113 if not self.__isConfig:
1114
1114
1115 nplots = 1
1115 nplots = 1
1116
1116
1117 self.setup(id=id,
1117 self.setup(id=id,
1118 nplots=nplots,
1118 nplots=nplots,
1119 wintitle=wintitle,
1119 wintitle=wintitle,
1120 showprofile=showprofile,
1120 showprofile=showprofile,
1121 show=show)
1121 show=show)
1122
1122
1123 tmin, tmax = self.getTimeLim(x, xmin, xmax)
1123 tmin, tmax = self.getTimeLim(x, xmin, xmax)
1124 if ymin == None: ymin = numpy.nanmin(noisedB) - 10.0
1124 if ymin == None: ymin = numpy.nanmin(noisedB) - 10.0
1125 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1125 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1126
1126
1127 self.FTP_WEI = ftp_wei
1127 self.FTP_WEI = ftp_wei
1128 self.EXP_CODE = exp_code
1128 self.EXP_CODE = exp_code
1129 self.SUB_EXP_CODE = sub_exp_code
1129 self.SUB_EXP_CODE = sub_exp_code
1130 self.PLOT_POS = plot_pos
1130 self.PLOT_POS = plot_pos
1131
1131
1132 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1132 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1133
1133
1134
1134
1135 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1135 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1136 self.__isConfig = True
1136 self.__isConfig = True
1137
1137
1138 self.xdata = numpy.array([])
1138 self.xdata = numpy.array([])
1139 self.ydata = numpy.array([])
1139 self.ydata = numpy.array([])
1140
1140
1141 self.setWinTitle(title)
1141 self.setWinTitle(title)
1142
1142
1143
1143
1144 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1144 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1145
1145
1146 legendlabels = ["channel %d"%(idchannel+1) for idchannel in channelList]
1146 legendlabels = ["channel %d"%(idchannel+1) for idchannel in channelList]
1147 axes = self.axesList[0]
1147 axes = self.axesList[0]
1148
1148
1149 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1149 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1150
1150
1151 if len(self.ydata)==0:
1151 if len(self.ydata)==0:
1152 self.ydata = noisedB[channelIndexList].reshape(-1,1)
1152 self.ydata = noisedB[channelIndexList].reshape(-1,1)
1153 else:
1153 else:
1154 self.ydata = numpy.hstack((self.ydata, noisedB[channelIndexList].reshape(-1,1)))
1154 self.ydata = numpy.hstack((self.ydata, noisedB[channelIndexList].reshape(-1,1)))
1155
1155
1156
1156
1157 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1157 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1158 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax,
1158 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax,
1159 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1159 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1160 XAxisAsTime=True, grid='both'
1160 XAxisAsTime=True, grid='both'
1161 )
1161 )
1162
1162
1163 self.draw()
1163 self.draw()
1164
1164
1165 # if save:
1165 # if save:
1166 #
1166 #
1167 # if figfile == None:
1167 # if figfile == None:
1168 # figfile = self.getFilename(name = self.name)
1168 # figfile = self.getFilename(name = self.name)
1169 #
1169 #
1170 # self.saveFigure(figpath, figfile)
1170 # self.saveFigure(figpath, figfile)
1171
1171
1172 if save:
1172 if save:
1173
1173
1174 self.counter_imagwr += 1
1174 self.counter_imagwr += 1
1175 if (self.counter_imagwr==wr_period):
1175 if (self.counter_imagwr==wr_period):
1176 if figfile == None:
1176 if figfile == None:
1177 figfile = self.getFilename(name = self.name)
1177 figfile = self.getFilename(name = self.name)
1178 self.saveFigure(figpath, figfile)
1178 self.saveFigure(figpath, figfile)
1179
1179
1180 if ftp:
1180 if ftp:
1181 #provisionalmente envia archivos en el formato de la web en tiempo real
1181 #provisionalmente envia archivos en el formato de la web en tiempo real
1182 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
1182 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
1183 path = '%s%03d' %(self.PREFIX, self.id)
1183 path = '%s%03d' %(self.PREFIX, self.id)
1184 ftp_file = os.path.join(path,'ftp','%s.png'%name)
1184 ftp_file = os.path.join(path,'ftp','%s.png'%name)
1185 self.saveFigure(figpath, ftp_file)
1185 self.saveFigure(figpath, ftp_file)
1186 ftp_filename = os.path.join(figpath,ftp_file)
1186 ftp_filename = os.path.join(figpath,ftp_file)
1187 try:
1187 try:
1188 self.sendByFTP(ftp_filename, server, folder, username, password)
1188 self.sendByFTP(ftp_filename, server, folder, username, password)
1189 except:
1189 except:
1190 self.counter_imagwr = 0
1190 self.counter_imagwr = 0
1191 print ValueError, 'Error FTP'
1191 print ValueError, 'Error FTP'
1192
1192
1193 self.counter_imagwr = 0
1193 self.counter_imagwr = 0
1194
1194
1195 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
1195 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
1196 self.__isConfig = False
1196 self.__isConfig = False
1197 del self.xdata
1197 del self.xdata
1198 del self.ydata
1198 del self.ydata
1199
1199
1200
1200
1201 class SpectraHeisScope(Figure):
1201 class SpectraHeisScope(Figure):
1202
1202
1203
1203
1204 __isConfig = None
1204 __isConfig = None
1205 __nsubplots = None
1205 __nsubplots = None
1206
1206
1207 WIDTHPROF = None
1207 WIDTHPROF = None
1208 HEIGHTPROF = None
1208 HEIGHTPROF = None
1209 PREFIX = 'spc'
1209 PREFIX = 'spc'
1210
1210
1211 def __init__(self):
1211 def __init__(self):
1212
1212
1213 self.__isConfig = False
1213 self.__isConfig = False
1214 self.__nsubplots = 1
1214 self.__nsubplots = 1
1215
1215
1216 self.WIDTH = 230
1216 self.WIDTH = 230
1217 self.HEIGHT = 250
1217 self.HEIGHT = 250
1218 self.WIDTHPROF = 120
1218 self.WIDTHPROF = 120
1219 self.HEIGHTPROF = 0
1219 self.HEIGHTPROF = 0
1220 self.counter_imagwr = 0
1220 self.counter_imagwr = 0
1221
1221
1222 def getSubplots(self):
1222 def getSubplots(self):
1223
1223
1224 ncol = int(numpy.sqrt(self.nplots)+0.9)
1224 ncol = int(numpy.sqrt(self.nplots)+0.9)
1225 nrow = int(self.nplots*1./ncol + 0.9)
1225 nrow = int(self.nplots*1./ncol + 0.9)
1226
1226
1227 return nrow, ncol
1227 return nrow, ncol
1228
1228
1229 def setup(self, id, nplots, wintitle, show):
1229 def setup(self, id, nplots, wintitle, show):
1230
1230
1231 showprofile = False
1231 showprofile = False
1232 self.__showprofile = showprofile
1232 self.__showprofile = showprofile
1233 self.nplots = nplots
1233 self.nplots = nplots
1234
1234
1235 ncolspan = 1
1235 ncolspan = 1
1236 colspan = 1
1236 colspan = 1
1237 if showprofile:
1237 if showprofile:
1238 ncolspan = 3
1238 ncolspan = 3
1239 colspan = 2
1239 colspan = 2
1240 self.__nsubplots = 2
1240 self.__nsubplots = 2
1241
1241
1242 self.createFigure(id = id,
1242 self.createFigure(id = id,
1243 wintitle = wintitle,
1243 wintitle = wintitle,
1244 widthplot = self.WIDTH + self.WIDTHPROF,
1244 widthplot = self.WIDTH + self.WIDTHPROF,
1245 heightplot = self.HEIGHT + self.HEIGHTPROF,
1245 heightplot = self.HEIGHT + self.HEIGHTPROF,
1246 show = show)
1246 show = show)
1247
1247
1248 nrow, ncol = self.getSubplots()
1248 nrow, ncol = self.getSubplots()
1249
1249
1250 counter = 0
1250 counter = 0
1251 for y in range(nrow):
1251 for y in range(nrow):
1252 for x in range(ncol):
1252 for x in range(ncol):
1253
1253
1254 if counter >= self.nplots:
1254 if counter >= self.nplots:
1255 break
1255 break
1256
1256
1257 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1257 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1258
1258
1259 if showprofile:
1259 if showprofile:
1260 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1260 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1261
1261
1262 counter += 1
1262 counter += 1
1263
1263
1264 # __isConfig = None
1264 # __isConfig = None
1265 # def __init__(self):
1265 # def __init__(self):
1266 #
1266 #
1267 # self.__isConfig = False
1267 # self.__isConfig = False
1268 # self.WIDTH = 600
1268 # self.WIDTH = 600
1269 # self.HEIGHT = 200
1269 # self.HEIGHT = 200
1270 #
1270 #
1271 # def getSubplots(self):
1271 # def getSubplots(self):
1272 #
1272 #
1273 # nrow = self.nplots
1273 # nrow = self.nplots
1274 # ncol = 3
1274 # ncol = 3
1275 # return nrow, ncol
1275 # return nrow, ncol
1276 #
1276 #
1277 # def setup(self, id, nplots, wintitle):
1277 # def setup(self, id, nplots, wintitle):
1278 #
1278 #
1279 # self.nplots = nplots
1279 # self.nplots = nplots
1280 #
1280 #
1281 # self.createFigure(id, wintitle)
1281 # self.createFigure(id, wintitle)
1282 #
1282 #
1283 # nrow,ncol = self.getSubplots()
1283 # nrow,ncol = self.getSubplots()
1284 # colspan = 3
1284 # colspan = 3
1285 # rowspan = 1
1285 # rowspan = 1
1286 #
1286 #
1287 # for i in range(nplots):
1287 # for i in range(nplots):
1288 # self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
1288 # self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
1289
1289
1290 def run(self, dataOut, id, wintitle="", channelList=None,
1290 def run(self, dataOut, id, wintitle="", channelList=None,
1291 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
1291 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
1292 figpath='./', figfile=None, ftp=False, wr_period=1, show=True):
1292 figpath='./', figfile=None, ftp=False, wr_period=1, show=True):
1293
1293
1294 """
1294 """
1295
1295
1296 Input:
1296 Input:
1297 dataOut :
1297 dataOut :
1298 id :
1298 id :
1299 wintitle :
1299 wintitle :
1300 channelList :
1300 channelList :
1301 xmin : None,
1301 xmin : None,
1302 xmax : None,
1302 xmax : None,
1303 ymin : None,
1303 ymin : None,
1304 ymax : None,
1304 ymax : None,
1305 """
1305 """
1306
1306
1307 if dataOut.realtime:
1307 if dataOut.realtime:
1308 if not(isRealtime(utcdatatime = dataOut.utctime)):
1308 if not(isRealtime(utcdatatime = dataOut.utctime)):
1309 print 'Skipping this plot function'
1309 print 'Skipping this plot function'
1310 return
1310 return
1311
1311
1312 if channelList == None:
1312 if channelList == None:
1313 channelIndexList = dataOut.channelIndexList
1313 channelIndexList = dataOut.channelIndexList
1314 else:
1314 else:
1315 channelIndexList = []
1315 channelIndexList = []
1316 for channel in channelList:
1316 for channel in channelList:
1317 if channel not in dataOut.channelList:
1317 if channel not in dataOut.channelList:
1318 raise ValueError, "Channel %d is not in dataOut.channelList"
1318 raise ValueError, "Channel %d is not in dataOut.channelList"
1319 channelIndexList.append(dataOut.channelList.index(channel))
1319 channelIndexList.append(dataOut.channelList.index(channel))
1320
1320
1321 # x = dataOut.heightList
1321 # x = dataOut.heightList
1322 c = 3E8
1322 c = 3E8
1323 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1323 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1324 #deberia cambiar para el caso de 1Mhz y 100KHz
1324 #deberia cambiar para el caso de 1Mhz y 100KHz
1325 x = numpy.arange(-1*dataOut.nHeights/2.,dataOut.nHeights/2.)*(c/(2*deltaHeight*dataOut.nHeights*1000))
1325 x = numpy.arange(-1*dataOut.nHeights/2.,dataOut.nHeights/2.)*(c/(2*deltaHeight*dataOut.nHeights*1000))
1326 x= x/(10000.0)
1326 x= x/(10000.0)
1327 # y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
1327 # y = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
1328 # y = y.real
1328 # y = y.real
1329 datadB = 10.*numpy.log10(dataOut.data_spc)
1329 datadB = 10.*numpy.log10(dataOut.data_spc)
1330 y = datadB
1330 y = datadB
1331
1331
1332 #thisDatetime = dataOut.datatime
1332 #thisDatetime = dataOut.datatime
1333 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
1333 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
1334 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1334 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1335 xlabel = "Frequency x 10000"
1335 xlabel = "Frequency x 10000"
1336 ylabel = "Intensity (dB)"
1336 ylabel = "Intensity (dB)"
1337
1337
1338 if not self.__isConfig:
1338 if not self.__isConfig:
1339 nplots = len(channelIndexList)
1339 nplots = len(channelIndexList)
1340
1340
1341 self.setup(id=id,
1341 self.setup(id=id,
1342 nplots=nplots,
1342 nplots=nplots,
1343 wintitle=wintitle,
1343 wintitle=wintitle,
1344 show=show)
1344 show=show)
1345
1345
1346 if xmin == None: xmin = numpy.nanmin(x)
1346 if xmin == None: xmin = numpy.nanmin(x)
1347 if xmax == None: xmax = numpy.nanmax(x)
1347 if xmax == None: xmax = numpy.nanmax(x)
1348 if ymin == None: ymin = numpy.nanmin(y)
1348 if ymin == None: ymin = numpy.nanmin(y)
1349 if ymax == None: ymax = numpy.nanmax(y)
1349 if ymax == None: ymax = numpy.nanmax(y)
1350
1350
1351 self.__isConfig = True
1351 self.__isConfig = True
1352
1352
1353 self.setWinTitle(title)
1353 self.setWinTitle(title)
1354
1354
1355 for i in range(len(self.axesList)):
1355 for i in range(len(self.axesList)):
1356 ychannel = y[i,:]
1356 ychannel = y[i,:]
1357 title = "Channel %d - peak:%.2f" %(i,numpy.max(ychannel))
1357 title = "Channel %d - peak:%.2f" %(i,numpy.max(ychannel))
1358 axes = self.axesList[i]
1358 axes = self.axesList[i]
1359 axes.pline(x, ychannel,
1359 axes.pline(x, ychannel,
1360 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1360 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1361 xlabel=xlabel, ylabel=ylabel, title=title, grid='both')
1361 xlabel=xlabel, ylabel=ylabel, title=title, grid='both')
1362
1362
1363
1363
1364 self.draw()
1364 self.draw()
1365
1365
1366 if save:
1366 if save:
1367 date = thisDatetime.strftime("%Y%m%d_%H%M%S")
1367 date = thisDatetime.strftime("%Y%m%d_%H%M%S")
1368 if figfile == None:
1368 if figfile == None:
1369 figfile = self.getFilename(name = date)
1369 figfile = self.getFilename(name = date)
1370
1370
1371 self.saveFigure(figpath, figfile)
1371 self.saveFigure(figpath, figfile)
1372
1372
1373 self.counter_imagwr += 1
1373 self.counter_imagwr += 1
1374 if (ftp and (self.counter_imagwr==wr_period)):
1374 if (ftp and (self.counter_imagwr==wr_period)):
1375 figfilename = os.path.join(figpath,figfile)
1375 figfilename = os.path.join(figpath,figfile)
1376 self.sendByFTP(figfilename)
1376 self.sendByFTP(figfilename)
1377 self.counter_imagwr = 0
1377 self.counter_imagwr = 0
1378
1378
1379
1379
1380 class RTIfromSpectraHeis(Figure):
1380 class RTIfromSpectraHeis(Figure):
1381
1381
1382 __isConfig = None
1382 __isConfig = None
1383 __nsubplots = None
1383 __nsubplots = None
1384
1384
1385 PREFIX = 'rtinoise'
1385 PREFIX = 'rtinoise'
1386
1386
1387 def __init__(self):
1387 def __init__(self):
1388
1388
1389 self.timerange = 24*60*60
1389 self.timerange = 24*60*60
1390 self.__isConfig = False
1390 self.__isConfig = False
1391 self.__nsubplots = 1
1391 self.__nsubplots = 1
1392
1392
1393 self.WIDTH = 820
1393 self.WIDTH = 820
1394 self.HEIGHT = 200
1394 self.HEIGHT = 200
1395 self.WIDTHPROF = 120
1395 self.WIDTHPROF = 120
1396 self.HEIGHTPROF = 0
1396 self.HEIGHTPROF = 0
1397 self.counter_imagwr = 0
1397 self.counter_imagwr = 0
1398 self.xdata = None
1398 self.xdata = None
1399 self.ydata = None
1399 self.ydata = None
1400
1400
1401 def getSubplots(self):
1401 def getSubplots(self):
1402
1402
1403 ncol = 1
1403 ncol = 1
1404 nrow = 1
1404 nrow = 1
1405
1405
1406 return nrow, ncol
1406 return nrow, ncol
1407
1407
1408 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1408 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1409
1409
1410 self.__showprofile = showprofile
1410 self.__showprofile = showprofile
1411 self.nplots = nplots
1411 self.nplots = nplots
1412
1412
1413 ncolspan = 7
1413 ncolspan = 7
1414 colspan = 6
1414 colspan = 6
1415 self.__nsubplots = 2
1415 self.__nsubplots = 2
1416
1416
1417 self.createFigure(id = id,
1417 self.createFigure(id = id,
1418 wintitle = wintitle,
1418 wintitle = wintitle,
1419 widthplot = self.WIDTH+self.WIDTHPROF,
1419 widthplot = self.WIDTH+self.WIDTHPROF,
1420 heightplot = self.HEIGHT+self.HEIGHTPROF,
1420 heightplot = self.HEIGHT+self.HEIGHTPROF,
1421 show = show)
1421 show = show)
1422
1422
1423 nrow, ncol = self.getSubplots()
1423 nrow, ncol = self.getSubplots()
1424
1424
1425 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1425 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1426
1426
1427
1427
1428 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1428 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1429 xmin=None, xmax=None, ymin=None, ymax=None,
1429 xmin=None, xmax=None, ymin=None, ymax=None,
1430 timerange=None,
1430 timerange=None,
1431 save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True):
1431 save=False, figpath='./', figfile=None, ftp=False, wr_period=1, show=True):
1432
1432
1433 if channelList == None:
1433 if channelList == None:
1434 channelIndexList = dataOut.channelIndexList
1434 channelIndexList = dataOut.channelIndexList
1435 channelList = dataOut.channelList
1435 channelList = dataOut.channelList
1436 else:
1436 else:
1437 channelIndexList = []
1437 channelIndexList = []
1438 for channel in channelList:
1438 for channel in channelList:
1439 if channel not in dataOut.channelList:
1439 if channel not in dataOut.channelList:
1440 raise ValueError, "Channel %d is not in dataOut.channelList"
1440 raise ValueError, "Channel %d is not in dataOut.channelList"
1441 channelIndexList.append(dataOut.channelList.index(channel))
1441 channelIndexList.append(dataOut.channelList.index(channel))
1442
1442
1443 if timerange != None:
1443 if timerange != None:
1444 self.timerange = timerange
1444 self.timerange = timerange
1445
1445
1446 tmin = None
1446 tmin = None
1447 tmax = None
1447 tmax = None
1448 x = dataOut.getTimeRange()
1448 x = dataOut.getTimeRange()
1449 y = dataOut.getHeiRange()
1449 y = dataOut.getHeiRange()
1450
1450
1451 factor = 1
1451 factor = 1
1452 data = dataOut.data_spc/factor
1452 data = dataOut.data_spc/factor
1453 data = numpy.average(data,axis=1)
1453 data = numpy.average(data,axis=1)
1454 datadB = 10*numpy.log10(data)
1454 datadB = 10*numpy.log10(data)
1455
1455
1456 # factor = dataOut.normFactor
1456 # factor = dataOut.normFactor
1457 # noise = dataOut.getNoise()/factor
1457 # noise = dataOut.getNoise()/factor
1458 # noisedB = 10*numpy.log10(noise)
1458 # noisedB = 10*numpy.log10(noise)
1459
1459
1460 #thisDatetime = dataOut.datatime
1460 #thisDatetime = dataOut.datatime
1461 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
1461 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[1])
1462 title = wintitle + " RTI: %s" %(thisDatetime.strftime("%d-%b-%Y"))
1462 title = wintitle + " RTI: %s" %(thisDatetime.strftime("%d-%b-%Y"))
1463 xlabel = "Local Time"
1463 xlabel = "Local Time"
1464 ylabel = "Intensity (dB)"
1464 ylabel = "Intensity (dB)"
1465
1465
1466 if not self.__isConfig:
1466 if not self.__isConfig:
1467
1467
1468 nplots = 1
1468 nplots = 1
1469
1469
1470 self.setup(id=id,
1470 self.setup(id=id,
1471 nplots=nplots,
1471 nplots=nplots,
1472 wintitle=wintitle,
1472 wintitle=wintitle,
1473 showprofile=showprofile,
1473 showprofile=showprofile,
1474 show=show)
1474 show=show)
1475
1475
1476 tmin, tmax = self.getTimeLim(x, xmin, xmax)
1476 tmin, tmax = self.getTimeLim(x, xmin, xmax)
1477 if ymin == None: ymin = numpy.nanmin(datadB)
1477 if ymin == None: ymin = numpy.nanmin(datadB)
1478 if ymax == None: ymax = numpy.nanmax(datadB)
1478 if ymax == None: ymax = numpy.nanmax(datadB)
1479
1479
1480 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1480 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1481 self.__isConfig = True
1481 self.__isConfig = True
1482
1482
1483 self.xdata = numpy.array([])
1483 self.xdata = numpy.array([])
1484 self.ydata = numpy.array([])
1484 self.ydata = numpy.array([])
1485
1485
1486 self.setWinTitle(title)
1486 self.setWinTitle(title)
1487
1487
1488
1488
1489 # title = "RTI %s" %(thisDatetime.strftime("%d-%b-%Y"))
1489 # title = "RTI %s" %(thisDatetime.strftime("%d-%b-%Y"))
1490 title = "RTI-Noise - %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1490 title = "RTI - %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1491
1491
1492 legendlabels = ["channel %d"%idchannel for idchannel in channelList]
1492 legendlabels = ["channel %d"%idchannel for idchannel in channelIndexList]
1493 axes = self.axesList[0]
1493 axes = self.axesList[0]
1494
1494
1495 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1495 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1496
1496
1497 if len(self.ydata)==0:
1497 if len(self.ydata)==0:
1498 self.ydata = datadB[channelIndexList].reshape(-1,1)
1498 self.ydata = datadB[channelIndexList].reshape(-1,1)
1499 else:
1499 else:
1500 self.ydata = numpy.hstack((self.ydata, datadB[channelIndexList].reshape(-1,1)))
1500 self.ydata = numpy.hstack((self.ydata, datadB[channelIndexList].reshape(-1,1)))
1501
1501
1502
1502
1503 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1503 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1504 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax,
1504 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax,
1505 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='.', markersize=8, linestyle="solid", grid='both',
1505 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='.', markersize=8, linestyle="solid", grid='both',
1506 XAxisAsTime=True
1506 XAxisAsTime=True
1507 )
1507 )
1508
1508
1509 self.draw()
1509 self.draw()
1510
1510
1511 if save:
1511 if save:
1512
1512
1513 if figfile == None:
1513 if figfile == None:
1514 figfile = self.getFilename(name = self.name)
1514 figfile = self.getFilename(name = self.name)
1515
1515
1516 self.saveFigure(figpath, figfile)
1516 self.saveFigure(figpath, figfile)
1517
1517
1518 self.counter_imagwr += 1
1518 self.counter_imagwr += 1
1519 if (ftp and (self.counter_imagwr==wr_period)):
1519 if (ftp and (self.counter_imagwr==wr_period)):
1520 figfilename = os.path.join(figpath,figfile)
1520 figfilename = os.path.join(figpath,figfile)
1521 self.sendByFTP(figfilename)
1521 self.sendByFTP(figfilename)
1522 self.counter_imagwr = 0
1522 self.counter_imagwr = 0
1523
1523
1524 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
1524 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
1525 self.__isConfig = False
1525 self.__isConfig = False
1526 del self.xdata
1526 del self.xdata
1527 del self.ydata
1527 del self.ydata
1528
1528
1529
1529
1530 No newline at end of file
1530
@@ -1,1702 +1,1720
1 '''
1 '''
2
2
3 $Author: dsuarez $
3 $Author: dsuarez $
4 $Id: Processor.py 1 2012-11-12 18:56:07Z dsuarez $
4 $Id: Processor.py 1 2012-11-12 18:56:07Z dsuarez $
5 '''
5 '''
6 import os
6 import os
7 import numpy
7 import numpy
8 import datetime
8 import datetime
9 import time
9 import time
10
10
11 from jrodata import *
11 from jrodata import *
12 from jrodataIO import *
12 from jrodataIO import *
13 from jroplot import *
13 from jroplot import *
14
14
15 try:
15 try:
16 import cfunctions
16 import cfunctions
17 except:
17 except:
18 pass
18 pass
19
19
20 class ProcessingUnit:
20 class ProcessingUnit:
21
21
22 """
22 """
23 Esta es la clase base para el procesamiento de datos.
23 Esta es la clase base para el procesamiento de datos.
24
24
25 Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser:
25 Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser:
26 - Metodos internos (callMethod)
26 - Metodos internos (callMethod)
27 - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos
27 - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos
28 tienen que ser agreagados con el metodo "add".
28 tienen que ser agreagados con el metodo "add".
29
29
30 """
30 """
31 # objeto de datos de entrada (Voltage, Spectra o Correlation)
31 # objeto de datos de entrada (Voltage, Spectra o Correlation)
32 dataIn = None
32 dataIn = None
33
33
34 # objeto de datos de entrada (Voltage, Spectra o Correlation)
34 # objeto de datos de entrada (Voltage, Spectra o Correlation)
35 dataOut = None
35 dataOut = None
36
36
37
37
38 objectDict = None
38 objectDict = None
39
39
40 def __init__(self):
40 def __init__(self):
41
41
42 self.objectDict = {}
42 self.objectDict = {}
43
43
44 def init(self):
44 def init(self):
45
45
46 raise ValueError, "Not implemented"
46 raise ValueError, "Not implemented"
47
47
48 def addOperation(self, object, objId):
48 def addOperation(self, object, objId):
49
49
50 """
50 """
51 Agrega el objeto "object" a la lista de objetos "self.objectList" y retorna el
51 Agrega el objeto "object" a la lista de objetos "self.objectList" y retorna el
52 identificador asociado a este objeto.
52 identificador asociado a este objeto.
53
53
54 Input:
54 Input:
55
55
56 object : objeto de la clase "Operation"
56 object : objeto de la clase "Operation"
57
57
58 Return:
58 Return:
59
59
60 objId : identificador del objeto, necesario para ejecutar la operacion
60 objId : identificador del objeto, necesario para ejecutar la operacion
61 """
61 """
62
62
63 self.objectDict[objId] = object
63 self.objectDict[objId] = object
64
64
65 return objId
65 return objId
66
66
67 def operation(self, **kwargs):
67 def operation(self, **kwargs):
68
68
69 """
69 """
70 Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los
70 Operacion directa sobre la data (dataOut.data). Es necesario actualizar los valores de los
71 atributos del objeto dataOut
71 atributos del objeto dataOut
72
72
73 Input:
73 Input:
74
74
75 **kwargs : Diccionario de argumentos de la funcion a ejecutar
75 **kwargs : Diccionario de argumentos de la funcion a ejecutar
76 """
76 """
77
77
78 raise ValueError, "ImplementedError"
78 raise ValueError, "ImplementedError"
79
79
80 def callMethod(self, name, **kwargs):
80 def callMethod(self, name, **kwargs):
81
81
82 """
82 """
83 Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase.
83 Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase.
84
84
85 Input:
85 Input:
86 name : nombre del metodo a ejecutar
86 name : nombre del metodo a ejecutar
87
87
88 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
88 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
89
89
90 """
90 """
91 if name != 'run':
91 if name != 'run':
92
92
93 if name == 'init' and self.dataIn.isEmpty():
93 if name == 'init' and self.dataIn.isEmpty():
94 self.dataOut.flagNoData = True
94 self.dataOut.flagNoData = True
95 return False
95 return False
96
96
97 if name != 'init' and self.dataOut.isEmpty():
97 if name != 'init' and self.dataOut.isEmpty():
98 return False
98 return False
99
99
100 methodToCall = getattr(self, name)
100 methodToCall = getattr(self, name)
101
101
102 methodToCall(**kwargs)
102 methodToCall(**kwargs)
103
103
104 if name != 'run':
104 if name != 'run':
105 return True
105 return True
106
106
107 if self.dataOut.isEmpty():
107 if self.dataOut.isEmpty():
108 return False
108 return False
109
109
110 return True
110 return True
111
111
112 def callObject(self, objId, **kwargs):
112 def callObject(self, objId, **kwargs):
113
113
114 """
114 """
115 Ejecuta la operacion asociada al identificador del objeto "objId"
115 Ejecuta la operacion asociada al identificador del objeto "objId"
116
116
117 Input:
117 Input:
118
118
119 objId : identificador del objeto a ejecutar
119 objId : identificador del objeto a ejecutar
120
120
121 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
121 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
122
122
123 Return:
123 Return:
124
124
125 None
125 None
126 """
126 """
127
127
128 if self.dataOut.isEmpty():
128 if self.dataOut.isEmpty():
129 return False
129 return False
130
130
131 object = self.objectDict[objId]
131 object = self.objectDict[objId]
132
132
133 object.run(self.dataOut, **kwargs)
133 object.run(self.dataOut, **kwargs)
134
134
135 return True
135 return True
136
136
137 def call(self, operationConf, **kwargs):
137 def call(self, operationConf, **kwargs):
138
138
139 """
139 """
140 Return True si ejecuta la operacion "operationConf.name" con los
140 Return True si ejecuta la operacion "operationConf.name" con los
141 argumentos "**kwargs". False si la operacion no se ha ejecutado.
141 argumentos "**kwargs". False si la operacion no se ha ejecutado.
142 La operacion puede ser de dos tipos:
142 La operacion puede ser de dos tipos:
143
143
144 1. Un metodo propio de esta clase:
144 1. Un metodo propio de esta clase:
145
145
146 operation.type = "self"
146 operation.type = "self"
147
147
148 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella:
148 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella:
149 operation.type = "other".
149 operation.type = "other".
150
150
151 Este objeto de tipo Operation debe de haber sido agregado antes con el metodo:
151 Este objeto de tipo Operation debe de haber sido agregado antes con el metodo:
152 "addOperation" e identificado con el operation.id
152 "addOperation" e identificado con el operation.id
153
153
154
154
155 con el id de la operacion.
155 con el id de la operacion.
156
156
157 Input:
157 Input:
158
158
159 Operation : Objeto del tipo operacion con los atributos: name, type y id.
159 Operation : Objeto del tipo operacion con los atributos: name, type y id.
160
160
161 """
161 """
162
162
163 if operationConf.type == 'self':
163 if operationConf.type == 'self':
164 sts = self.callMethod(operationConf.name, **kwargs)
164 sts = self.callMethod(operationConf.name, **kwargs)
165
165
166 if operationConf.type == 'other':
166 if operationConf.type == 'other':
167 sts = self.callObject(operationConf.id, **kwargs)
167 sts = self.callObject(operationConf.id, **kwargs)
168
168
169 return sts
169 return sts
170
170
171 def setInput(self, dataIn):
171 def setInput(self, dataIn):
172
172
173 self.dataIn = dataIn
173 self.dataIn = dataIn
174
174
175 def getOutput(self):
175 def getOutput(self):
176
176
177 return self.dataOut
177 return self.dataOut
178
178
179 class Operation():
179 class Operation():
180
180
181 """
181 """
182 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
182 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
183 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
183 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
184 acumulacion dentro de esta clase
184 acumulacion dentro de esta clase
185
185
186 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
186 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
187
187
188 """
188 """
189
189
190 __buffer = None
190 __buffer = None
191 __isConfig = False
191 __isConfig = False
192
192
193 def __init__(self):
193 def __init__(self):
194
194
195 pass
195 pass
196
196
197 def run(self, dataIn, **kwargs):
197 def run(self, dataIn, **kwargs):
198
198
199 """
199 """
200 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los atributos del objeto dataIn.
200 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los atributos del objeto dataIn.
201
201
202 Input:
202 Input:
203
203
204 dataIn : objeto del tipo JROData
204 dataIn : objeto del tipo JROData
205
205
206 Return:
206 Return:
207
207
208 None
208 None
209
209
210 Affected:
210 Affected:
211 __buffer : buffer de recepcion de datos.
211 __buffer : buffer de recepcion de datos.
212
212
213 """
213 """
214
214
215 raise ValueError, "ImplementedError"
215 raise ValueError, "ImplementedError"
216
216
217 class VoltageProc(ProcessingUnit):
217 class VoltageProc(ProcessingUnit):
218
218
219
219
220 def __init__(self):
220 def __init__(self):
221
221
222 self.objectDict = {}
222 self.objectDict = {}
223 self.dataOut = Voltage()
223 self.dataOut = Voltage()
224 self.flip = 1
224 self.flip = 1
225
225
226 def init(self):
226 def init(self):
227
227
228 self.dataOut.copy(self.dataIn)
228 self.dataOut.copy(self.dataIn)
229 # No necesita copiar en cada init() los atributos de dataIn
229 # No necesita copiar en cada init() los atributos de dataIn
230 # la copia deberia hacerse por cada nuevo bloque de datos
230 # la copia deberia hacerse por cada nuevo bloque de datos
231
231
232 def selectChannels(self, channelList):
232 def selectChannels(self, channelList):
233
233
234 channelIndexList = []
234 channelIndexList = []
235
235
236 for channel in channelList:
236 for channel in channelList:
237 index = self.dataOut.channelList.index(channel)
237 index = self.dataOut.channelList.index(channel)
238 channelIndexList.append(index)
238 channelIndexList.append(index)
239
239
240 self.selectChannelsByIndex(channelIndexList)
240 self.selectChannelsByIndex(channelIndexList)
241
241
242 def selectChannelsByIndex(self, channelIndexList):
242 def selectChannelsByIndex(self, channelIndexList):
243 """
243 """
244 Selecciona un bloque de datos en base a canales segun el channelIndexList
244 Selecciona un bloque de datos en base a canales segun el channelIndexList
245
245
246 Input:
246 Input:
247 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
247 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
248
248
249 Affected:
249 Affected:
250 self.dataOut.data
250 self.dataOut.data
251 self.dataOut.channelIndexList
251 self.dataOut.channelIndexList
252 self.dataOut.nChannels
252 self.dataOut.nChannels
253 self.dataOut.m_ProcessingHeader.totalSpectra
253 self.dataOut.m_ProcessingHeader.totalSpectra
254 self.dataOut.systemHeaderObj.numChannels
254 self.dataOut.systemHeaderObj.numChannels
255 self.dataOut.m_ProcessingHeader.blockSize
255 self.dataOut.m_ProcessingHeader.blockSize
256
256
257 Return:
257 Return:
258 None
258 None
259 """
259 """
260
260
261 for channelIndex in channelIndexList:
261 for channelIndex in channelIndexList:
262 if channelIndex not in self.dataOut.channelIndexList:
262 if channelIndex not in self.dataOut.channelIndexList:
263 print channelIndexList
263 print channelIndexList
264 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
264 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
265
265
266 nChannels = len(channelIndexList)
266 nChannels = len(channelIndexList)
267
267
268 data = self.dataOut.data[channelIndexList,:]
268 data = self.dataOut.data[channelIndexList,:]
269
269
270 self.dataOut.data = data
270 self.dataOut.data = data
271 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
271 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
272 # self.dataOut.nChannels = nChannels
272 # self.dataOut.nChannels = nChannels
273
273
274 return 1
274 return 1
275
275
276 def selectHeights(self, minHei=None, maxHei=None):
276 def selectHeights(self, minHei=None, maxHei=None):
277 """
277 """
278 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
278 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
279 minHei <= height <= maxHei
279 minHei <= height <= maxHei
280
280
281 Input:
281 Input:
282 minHei : valor minimo de altura a considerar
282 minHei : valor minimo de altura a considerar
283 maxHei : valor maximo de altura a considerar
283 maxHei : valor maximo de altura a considerar
284
284
285 Affected:
285 Affected:
286 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
286 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
287
287
288 Return:
288 Return:
289 1 si el metodo se ejecuto con exito caso contrario devuelve 0
289 1 si el metodo se ejecuto con exito caso contrario devuelve 0
290 """
290 """
291
291
292 if minHei == None:
292 if minHei == None:
293 minHei = self.dataOut.heightList[0]
293 minHei = self.dataOut.heightList[0]
294
294
295 if maxHei == None:
295 if maxHei == None:
296 maxHei = self.dataOut.heightList[-1]
296 maxHei = self.dataOut.heightList[-1]
297
297
298 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
298 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
299 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
299 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
300
300
301
301
302 if (maxHei > self.dataOut.heightList[-1]):
302 if (maxHei > self.dataOut.heightList[-1]):
303 maxHei = self.dataOut.heightList[-1]
303 maxHei = self.dataOut.heightList[-1]
304 # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
304 # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
305
305
306 minIndex = 0
306 minIndex = 0
307 maxIndex = 0
307 maxIndex = 0
308 heights = self.dataOut.heightList
308 heights = self.dataOut.heightList
309
309
310 inda = numpy.where(heights >= minHei)
310 inda = numpy.where(heights >= minHei)
311 indb = numpy.where(heights <= maxHei)
311 indb = numpy.where(heights <= maxHei)
312
312
313 try:
313 try:
314 minIndex = inda[0][0]
314 minIndex = inda[0][0]
315 except:
315 except:
316 minIndex = 0
316 minIndex = 0
317
317
318 try:
318 try:
319 maxIndex = indb[0][-1]
319 maxIndex = indb[0][-1]
320 except:
320 except:
321 maxIndex = len(heights)
321 maxIndex = len(heights)
322
322
323 self.selectHeightsByIndex(minIndex, maxIndex)
323 self.selectHeightsByIndex(minIndex, maxIndex)
324
324
325 return 1
325 return 1
326
326
327
327
328 def selectHeightsByIndex(self, minIndex, maxIndex):
328 def selectHeightsByIndex(self, minIndex, maxIndex):
329 """
329 """
330 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
330 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
331 minIndex <= index <= maxIndex
331 minIndex <= index <= maxIndex
332
332
333 Input:
333 Input:
334 minIndex : valor de indice minimo de altura a considerar
334 minIndex : valor de indice minimo de altura a considerar
335 maxIndex : valor de indice maximo de altura a considerar
335 maxIndex : valor de indice maximo de altura a considerar
336
336
337 Affected:
337 Affected:
338 self.dataOut.data
338 self.dataOut.data
339 self.dataOut.heightList
339 self.dataOut.heightList
340
340
341 Return:
341 Return:
342 1 si el metodo se ejecuto con exito caso contrario devuelve 0
342 1 si el metodo se ejecuto con exito caso contrario devuelve 0
343 """
343 """
344
344
345 if (minIndex < 0) or (minIndex > maxIndex):
345 if (minIndex < 0) or (minIndex > maxIndex):
346 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
346 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
347
347
348 if (maxIndex >= self.dataOut.nHeights):
348 if (maxIndex >= self.dataOut.nHeights):
349 maxIndex = self.dataOut.nHeights-1
349 maxIndex = self.dataOut.nHeights-1
350 # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
350 # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
351
351
352 nHeights = maxIndex - minIndex + 1
352 nHeights = maxIndex - minIndex + 1
353
353
354 #voltage
354 #voltage
355 data = self.dataOut.data[:,minIndex:maxIndex+1]
355 data = self.dataOut.data[:,minIndex:maxIndex+1]
356
356
357 firstHeight = self.dataOut.heightList[minIndex]
357 firstHeight = self.dataOut.heightList[minIndex]
358
358
359 self.dataOut.data = data
359 self.dataOut.data = data
360 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
360 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
361
361
362 return 1
362 return 1
363
363
364
364
365 def filterByHeights(self, window):
365 def filterByHeights(self, window):
366 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
366 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
367
367
368 if window == None:
368 if window == None:
369 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
369 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
370
370
371 newdelta = deltaHeight * window
371 newdelta = deltaHeight * window
372 r = self.dataOut.data.shape[1] % window
372 r = self.dataOut.data.shape[1] % window
373 buffer = self.dataOut.data[:,0:self.dataOut.data.shape[1]-r]
373 buffer = self.dataOut.data[:,0:self.dataOut.data.shape[1]-r]
374 buffer = buffer.reshape(self.dataOut.data.shape[0],self.dataOut.data.shape[1]/window,window)
374 buffer = buffer.reshape(self.dataOut.data.shape[0],self.dataOut.data.shape[1]/window,window)
375 buffer = numpy.sum(buffer,2)
375 buffer = numpy.sum(buffer,2)
376 self.dataOut.data = buffer
376 self.dataOut.data = buffer
377 self.dataOut.heightList = numpy.arange(self.dataOut.heightList[0],newdelta*(self.dataOut.nHeights-r)/window,newdelta)
377 self.dataOut.heightList = numpy.arange(self.dataOut.heightList[0],newdelta*(self.dataOut.nHeights-r)/window,newdelta)
378 self.dataOut.windowOfFilter = window
378 self.dataOut.windowOfFilter = window
379
379
380 def deFlip(self):
380 def deFlip(self):
381 self.dataOut.data *= self.flip
381 self.dataOut.data *= self.flip
382 self.flip *= -1.
382 self.flip *= -1.
383
383
384 def setRadarFrequency(self, frequency=None):
384 def setRadarFrequency(self, frequency=None):
385 if frequency != None:
385 if frequency != None:
386 self.dataOut.frequency = frequency
386 self.dataOut.frequency = frequency
387
387
388 return 1
388 return 1
389
389
390 class CohInt(Operation):
390 class CohInt(Operation):
391
391
392 __isConfig = False
392 __isConfig = False
393
393
394 __profIndex = 0
394 __profIndex = 0
395 __withOverapping = False
395 __withOverapping = False
396
396
397 __byTime = False
397 __byTime = False
398 __initime = None
398 __initime = None
399 __lastdatatime = None
399 __lastdatatime = None
400 __integrationtime = None
400 __integrationtime = None
401
401
402 __buffer = None
402 __buffer = None
403
403
404 __dataReady = False
404 __dataReady = False
405
405
406 n = None
406 n = None
407
407
408
408
409 def __init__(self):
409 def __init__(self):
410
410
411 self.__isConfig = False
411 self.__isConfig = False
412
412
413 def setup(self, n=None, timeInterval=None, overlapping=False):
413 def setup(self, n=None, timeInterval=None, overlapping=False):
414 """
414 """
415 Set the parameters of the integration class.
415 Set the parameters of the integration class.
416
416
417 Inputs:
417 Inputs:
418
418
419 n : Number of coherent integrations
419 n : Number of coherent integrations
420 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
420 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
421 overlapping :
421 overlapping :
422
422
423 """
423 """
424
424
425 self.__initime = None
425 self.__initime = None
426 self.__lastdatatime = 0
426 self.__lastdatatime = 0
427 self.__buffer = None
427 self.__buffer = None
428 self.__dataReady = False
428 self.__dataReady = False
429
429
430
430
431 if n == None and timeInterval == None:
431 if n == None and timeInterval == None:
432 raise ValueError, "n or timeInterval should be specified ..."
432 raise ValueError, "n or timeInterval should be specified ..."
433
433
434 if n != None:
434 if n != None:
435 self.n = n
435 self.n = n
436 self.__byTime = False
436 self.__byTime = False
437 else:
437 else:
438 self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
438 self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
439 self.n = 9999
439 self.n = 9999
440 self.__byTime = True
440 self.__byTime = True
441
441
442 if overlapping:
442 if overlapping:
443 self.__withOverapping = True
443 self.__withOverapping = True
444 self.__buffer = None
444 self.__buffer = None
445 else:
445 else:
446 self.__withOverapping = False
446 self.__withOverapping = False
447 self.__buffer = 0
447 self.__buffer = 0
448
448
449 self.__profIndex = 0
449 self.__profIndex = 0
450
450
451 def putData(self, data):
451 def putData(self, data):
452
452
453 """
453 """
454 Add a profile to the __buffer and increase in one the __profileIndex
454 Add a profile to the __buffer and increase in one the __profileIndex
455
455
456 """
456 """
457
457
458 if not self.__withOverapping:
458 if not self.__withOverapping:
459 self.__buffer += data.copy()
459 self.__buffer += data.copy()
460 self.__profIndex += 1
460 self.__profIndex += 1
461 return
461 return
462
462
463 #Overlapping data
463 #Overlapping data
464 nChannels, nHeis = data.shape
464 nChannels, nHeis = data.shape
465 data = numpy.reshape(data, (1, nChannels, nHeis))
465 data = numpy.reshape(data, (1, nChannels, nHeis))
466
466
467 #If the buffer is empty then it takes the data value
467 #If the buffer is empty then it takes the data value
468 if self.__buffer == None:
468 if self.__buffer == None:
469 self.__buffer = data
469 self.__buffer = data
470 self.__profIndex += 1
470 self.__profIndex += 1
471 return
471 return
472
472
473 #If the buffer length is lower than n then stakcing the data value
473 #If the buffer length is lower than n then stakcing the data value
474 if self.__profIndex < self.n:
474 if self.__profIndex < self.n:
475 self.__buffer = numpy.vstack((self.__buffer, data))
475 self.__buffer = numpy.vstack((self.__buffer, data))
476 self.__profIndex += 1
476 self.__profIndex += 1
477 return
477 return
478
478
479 #If the buffer length is equal to n then replacing the last buffer value with the data value
479 #If the buffer length is equal to n then replacing the last buffer value with the data value
480 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
480 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
481 self.__buffer[self.n-1] = data
481 self.__buffer[self.n-1] = data
482 self.__profIndex = self.n
482 self.__profIndex = self.n
483 return
483 return
484
484
485
485
486 def pushData(self):
486 def pushData(self):
487 """
487 """
488 Return the sum of the last profiles and the profiles used in the sum.
488 Return the sum of the last profiles and the profiles used in the sum.
489
489
490 Affected:
490 Affected:
491
491
492 self.__profileIndex
492 self.__profileIndex
493
493
494 """
494 """
495
495
496 if not self.__withOverapping:
496 if not self.__withOverapping:
497 data = self.__buffer
497 data = self.__buffer
498 n = self.__profIndex
498 n = self.__profIndex
499
499
500 self.__buffer = 0
500 self.__buffer = 0
501 self.__profIndex = 0
501 self.__profIndex = 0
502
502
503 return data, n
503 return data, n
504
504
505 #Integration with Overlapping
505 #Integration with Overlapping
506 data = numpy.sum(self.__buffer, axis=0)
506 data = numpy.sum(self.__buffer, axis=0)
507 n = self.__profIndex
507 n = self.__profIndex
508
508
509 return data, n
509 return data, n
510
510
511 def byProfiles(self, data):
511 def byProfiles(self, data):
512
512
513 self.__dataReady = False
513 self.__dataReady = False
514 avgdata = None
514 avgdata = None
515 n = None
515 n = None
516
516
517 self.putData(data)
517 self.putData(data)
518
518
519 if self.__profIndex == self.n:
519 if self.__profIndex == self.n:
520
520
521 avgdata, n = self.pushData()
521 avgdata, n = self.pushData()
522 self.__dataReady = True
522 self.__dataReady = True
523
523
524 return avgdata
524 return avgdata
525
525
526 def byTime(self, data, datatime):
526 def byTime(self, data, datatime):
527
527
528 self.__dataReady = False
528 self.__dataReady = False
529 avgdata = None
529 avgdata = None
530 n = None
530 n = None
531
531
532 self.putData(data)
532 self.putData(data)
533
533
534 if (datatime - self.__initime) >= self.__integrationtime:
534 if (datatime - self.__initime) >= self.__integrationtime:
535 avgdata, n = self.pushData()
535 avgdata, n = self.pushData()
536 self.n = n
536 self.n = n
537 self.__dataReady = True
537 self.__dataReady = True
538
538
539 return avgdata
539 return avgdata
540
540
541 def integrate(self, data, datatime=None):
541 def integrate(self, data, datatime=None):
542
542
543 if self.__initime == None:
543 if self.__initime == None:
544 self.__initime = datatime
544 self.__initime = datatime
545
545
546 if self.__byTime:
546 if self.__byTime:
547 avgdata = self.byTime(data, datatime)
547 avgdata = self.byTime(data, datatime)
548 else:
548 else:
549 avgdata = self.byProfiles(data)
549 avgdata = self.byProfiles(data)
550
550
551
551
552 self.__lastdatatime = datatime
552 self.__lastdatatime = datatime
553
553
554 if avgdata == None:
554 if avgdata == None:
555 return None, None
555 return None, None
556
556
557 avgdatatime = self.__initime
557 avgdatatime = self.__initime
558
558
559 deltatime = datatime -self.__lastdatatime
559 deltatime = datatime -self.__lastdatatime
560
560
561 if not self.__withOverapping:
561 if not self.__withOverapping:
562 self.__initime = datatime
562 self.__initime = datatime
563 else:
563 else:
564 self.__initime += deltatime
564 self.__initime += deltatime
565
565
566 return avgdata, avgdatatime
566 return avgdata, avgdatatime
567
567
568 def run(self, dataOut, **kwargs):
568 def run(self, dataOut, **kwargs):
569
569
570 if not self.__isConfig:
570 if not self.__isConfig:
571 self.setup(**kwargs)
571 self.setup(**kwargs)
572 self.__isConfig = True
572 self.__isConfig = True
573
573
574 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
574 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
575
575
576 # dataOut.timeInterval *= n
576 # dataOut.timeInterval *= n
577 dataOut.flagNoData = True
577 dataOut.flagNoData = True
578
578
579 if self.__dataReady:
579 if self.__dataReady:
580 dataOut.data = avgdata
580 dataOut.data = avgdata
581 dataOut.nCohInt *= self.n
581 dataOut.nCohInt *= self.n
582 dataOut.utctime = avgdatatime
582 dataOut.utctime = avgdatatime
583 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
583 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
584 dataOut.flagNoData = False
584 dataOut.flagNoData = False
585
585
586
586
587 class Decoder(Operation):
587 class Decoder(Operation):
588
588
589 __isConfig = False
589 __isConfig = False
590 __profIndex = 0
590 __profIndex = 0
591
591
592 code = None
592 code = None
593
593
594 nCode = None
594 nCode = None
595 nBaud = None
595 nBaud = None
596
596
597 def __init__(self):
597 def __init__(self):
598
598
599 self.__isConfig = False
599 self.__isConfig = False
600
600
601 def setup(self, code, shape):
601 def setup(self, code, shape):
602
602
603 self.__profIndex = 0
603 self.__profIndex = 0
604
604
605 self.code = code
605 self.code = code
606
606
607 self.nCode = len(code)
607 self.nCode = len(code)
608 self.nBaud = len(code[0])
608 self.nBaud = len(code[0])
609
609
610 self.__nChannels, self.__nHeis = shape
610 self.__nChannels, self.__nHeis = shape
611
611
612 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
612 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
613
613
614 __codeBuffer[:,0:self.nBaud] = self.code
614 __codeBuffer[:,0:self.nBaud] = self.code
615
615
616 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
616 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
617
617
618 self.ndatadec = self.__nHeis - self.nBaud + 1
618 self.ndatadec = self.__nHeis - self.nBaud + 1
619
619
620 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
620 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
621
621
622 def convolutionInFreq(self, data):
622 def convolutionInFreq(self, data):
623
623
624 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
624 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
625
625
626 fft_data = numpy.fft.fft(data, axis=1)
626 fft_data = numpy.fft.fft(data, axis=1)
627
627
628 conv = fft_data*fft_code
628 conv = fft_data*fft_code
629
629
630 data = numpy.fft.ifft(conv,axis=1)
630 data = numpy.fft.ifft(conv,axis=1)
631
631
632 datadec = data[:,:-self.nBaud+1]
632 datadec = data[:,:-self.nBaud+1]
633
633
634 return datadec
634 return datadec
635
635
636 def convolutionInFreqOpt(self, data):
636 def convolutionInFreqOpt(self, data):
637
637
638 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
638 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
639
639
640 data = cfunctions.decoder(fft_code, data)
640 data = cfunctions.decoder(fft_code, data)
641
641
642 datadec = data[:,:-self.nBaud+1]
642 datadec = data[:,:-self.nBaud+1]
643
643
644 return datadec
644 return datadec
645
645
646 def convolutionInTime(self, data):
646 def convolutionInTime(self, data):
647
647
648 code = self.code[self.__profIndex]
648 code = self.code[self.__profIndex]
649
649
650 for i in range(self.__nChannels):
650 for i in range(self.__nChannels):
651 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='valid')
651 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='valid')
652
652
653 return self.datadecTime
653 return self.datadecTime
654
654
655 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0):
655 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0):
656
656
657 if not self.__isConfig:
657 if not self.__isConfig:
658
658
659 if code == None:
659 if code == None:
660 code = dataOut.code
660 code = dataOut.code
661 else:
661 else:
662 code = numpy.array(code).reshape(nCode,nBaud)
662 code = numpy.array(code).reshape(nCode,nBaud)
663 dataOut.code = code
663 dataOut.code = code
664 dataOut.nCode = nCode
664 dataOut.nCode = nCode
665 dataOut.nBaud = nBaud
665 dataOut.nBaud = nBaud
666
666
667 if code == None:
667 if code == None:
668 return 1
668 return 1
669
669
670 self.setup(code, dataOut.data.shape)
670 self.setup(code, dataOut.data.shape)
671 self.__isConfig = True
671 self.__isConfig = True
672
672
673 if mode == 0:
673 if mode == 0:
674 datadec = self.convolutionInTime(dataOut.data)
674 datadec = self.convolutionInTime(dataOut.data)
675
675
676 if mode == 1:
676 if mode == 1:
677 datadec = self.convolutionInFreq(dataOut.data)
677 datadec = self.convolutionInFreq(dataOut.data)
678
678
679 if mode == 2:
679 if mode == 2:
680 datadec = self.convolutionInFreqOpt(dataOut.data)
680 datadec = self.convolutionInFreqOpt(dataOut.data)
681
681
682 dataOut.data = datadec
682 dataOut.data = datadec
683
683
684 dataOut.heightList = dataOut.heightList[0:self.ndatadec]
684 dataOut.heightList = dataOut.heightList[0:self.ndatadec]
685
685
686 dataOut.flagDecodeData = True #asumo q la data no esta decodificada
686 dataOut.flagDecodeData = True #asumo q la data no esta decodificada
687
687
688 if self.__profIndex == self.nCode-1:
688 if self.__profIndex == self.nCode-1:
689 self.__profIndex = 0
689 self.__profIndex = 0
690 return 1
690 return 1
691
691
692 self.__profIndex += 1
692 self.__profIndex += 1
693
693
694 return 1
694 return 1
695 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
695 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
696
696
697
697
698
698
699 class SpectraProc(ProcessingUnit):
699 class SpectraProc(ProcessingUnit):
700
700
701 def __init__(self):
701 def __init__(self):
702
702
703 self.objectDict = {}
703 self.objectDict = {}
704 self.buffer = None
704 self.buffer = None
705 self.firstdatatime = None
705 self.firstdatatime = None
706 self.profIndex = 0
706 self.profIndex = 0
707 self.dataOut = Spectra()
707 self.dataOut = Spectra()
708
708
709 def __updateObjFromInput(self):
709 def __updateObjFromInput(self):
710
710
711 self.dataOut.timeZone = self.dataIn.timeZone
711 self.dataOut.timeZone = self.dataIn.timeZone
712 self.dataOut.dstFlag = self.dataIn.dstFlag
712 self.dataOut.dstFlag = self.dataIn.dstFlag
713 self.dataOut.errorCount = self.dataIn.errorCount
713 self.dataOut.errorCount = self.dataIn.errorCount
714 self.dataOut.useLocalTime = self.dataIn.useLocalTime
714 self.dataOut.useLocalTime = self.dataIn.useLocalTime
715
715
716 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
716 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
717 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
717 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
718 self.dataOut.channelList = self.dataIn.channelList
718 self.dataOut.channelList = self.dataIn.channelList
719 self.dataOut.heightList = self.dataIn.heightList
719 self.dataOut.heightList = self.dataIn.heightList
720 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
720 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
721 # self.dataOut.nHeights = self.dataIn.nHeights
721 # self.dataOut.nHeights = self.dataIn.nHeights
722 # self.dataOut.nChannels = self.dataIn.nChannels
722 # self.dataOut.nChannels = self.dataIn.nChannels
723 self.dataOut.nBaud = self.dataIn.nBaud
723 self.dataOut.nBaud = self.dataIn.nBaud
724 self.dataOut.nCode = self.dataIn.nCode
724 self.dataOut.nCode = self.dataIn.nCode
725 self.dataOut.code = self.dataIn.code
725 self.dataOut.code = self.dataIn.code
726 self.dataOut.nProfiles = self.dataOut.nFFTPoints
726 self.dataOut.nProfiles = self.dataOut.nFFTPoints
727 # self.dataOut.channelIndexList = self.dataIn.channelIndexList
727 # self.dataOut.channelIndexList = self.dataIn.channelIndexList
728 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
728 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
729 self.dataOut.utctime = self.firstdatatime
729 self.dataOut.utctime = self.firstdatatime
730 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
730 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
731 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
731 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
732 # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT
732 # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT
733 self.dataOut.nCohInt = self.dataIn.nCohInt
733 self.dataOut.nCohInt = self.dataIn.nCohInt
734 self.dataOut.nIncohInt = 1
734 self.dataOut.nIncohInt = 1
735 self.dataOut.ippSeconds = self.dataIn.ippSeconds
735 self.dataOut.ippSeconds = self.dataIn.ippSeconds
736 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
736 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
737
737
738 self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt
738 self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt
739 self.dataOut.frequency = self.dataIn.frequency
739 self.dataOut.frequency = self.dataIn.frequency
740 self.dataOut.realtime = self.dataIn.realtime
740 self.dataOut.realtime = self.dataIn.realtime
741
741
742 def __getFft(self):
742 def __getFft(self):
743 """
743 """
744 Convierte valores de Voltaje a Spectra
744 Convierte valores de Voltaje a Spectra
745
745
746 Affected:
746 Affected:
747 self.dataOut.data_spc
747 self.dataOut.data_spc
748 self.dataOut.data_cspc
748 self.dataOut.data_cspc
749 self.dataOut.data_dc
749 self.dataOut.data_dc
750 self.dataOut.heightList
750 self.dataOut.heightList
751 self.profIndex
751 self.profIndex
752 self.buffer
752 self.buffer
753 self.dataOut.flagNoData
753 self.dataOut.flagNoData
754 """
754 """
755 fft_volt = numpy.fft.fft(self.buffer,n=self.dataOut.nFFTPoints,axis=1)
755 fft_volt = numpy.fft.fft(self.buffer,n=self.dataOut.nFFTPoints,axis=1)
756 fft_volt = fft_volt.astype(numpy.dtype('complex'))
756 fft_volt = fft_volt.astype(numpy.dtype('complex'))
757 dc = fft_volt[:,0,:]
757 dc = fft_volt[:,0,:]
758
758
759 #calculo de self-spectra
759 #calculo de self-spectra
760 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
760 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
761 spc = fft_volt * numpy.conjugate(fft_volt)
761 spc = fft_volt * numpy.conjugate(fft_volt)
762 spc = spc.real
762 spc = spc.real
763
763
764 blocksize = 0
764 blocksize = 0
765 blocksize += dc.size
765 blocksize += dc.size
766 blocksize += spc.size
766 blocksize += spc.size
767
767
768 cspc = None
768 cspc = None
769 pairIndex = 0
769 pairIndex = 0
770 if self.dataOut.pairsList != None:
770 if self.dataOut.pairsList != None:
771 #calculo de cross-spectra
771 #calculo de cross-spectra
772 cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
772 cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
773 for pair in self.dataOut.pairsList:
773 for pair in self.dataOut.pairsList:
774 cspc[pairIndex,:,:] = fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:])
774 cspc[pairIndex,:,:] = fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:])
775 pairIndex += 1
775 pairIndex += 1
776 blocksize += cspc.size
776 blocksize += cspc.size
777
777
778 self.dataOut.data_spc = spc
778 self.dataOut.data_spc = spc
779 self.dataOut.data_cspc = cspc
779 self.dataOut.data_cspc = cspc
780 self.dataOut.data_dc = dc
780 self.dataOut.data_dc = dc
781 self.dataOut.blockSize = blocksize
781 self.dataOut.blockSize = blocksize
782 self.dataOut.flagShiftFFT = False
782 self.dataOut.flagShiftFFT = False
783
783
784 def init(self, nProfiles=None, nFFTPoints=None, pairsList=None):
784 def init(self, nProfiles=None, nFFTPoints=None, pairsList=None):
785
785
786 self.dataOut.flagNoData = True
786 self.dataOut.flagNoData = True
787
787
788 if self.dataIn.type == "Spectra":
788 if self.dataIn.type == "Spectra":
789 self.dataOut.copy(self.dataIn)
789 self.dataOut.copy(self.dataIn)
790 return
790 return
791
791
792 if self.dataIn.type == "Voltage":
792 if self.dataIn.type == "Voltage":
793
793
794 if nFFTPoints == None:
794 if nFFTPoints == None:
795 raise ValueError, "This SpectraProc.init() need nFFTPoints input variable"
795 raise ValueError, "This SpectraProc.init() need nFFTPoints input variable"
796
796
797 if pairsList == None:
797 if pairsList == None:
798 nPairs = 0
798 nPairs = 0
799 else:
799 else:
800 nPairs = len(pairsList)
800 nPairs = len(pairsList)
801
801
802 self.dataOut.nFFTPoints = nFFTPoints
802 self.dataOut.nFFTPoints = nFFTPoints
803 self.dataOut.pairsList = pairsList
803 self.dataOut.pairsList = pairsList
804 self.dataOut.nPairs = nPairs
804 self.dataOut.nPairs = nPairs
805
805
806 if self.buffer == None:
806 if self.buffer == None:
807 self.buffer = numpy.zeros((self.dataIn.nChannels,
807 self.buffer = numpy.zeros((self.dataIn.nChannels,
808 nProfiles,
808 nProfiles,
809 self.dataIn.nHeights),
809 self.dataIn.nHeights),
810 dtype='complex')
810 dtype='complex')
811
811
812
812
813 self.buffer[:,self.profIndex,:] = self.dataIn.data.copy()
813 self.buffer[:,self.profIndex,:] = self.dataIn.data.copy()
814 self.profIndex += 1
814 self.profIndex += 1
815
815
816 if self.firstdatatime == None:
816 if self.firstdatatime == None:
817 self.firstdatatime = self.dataIn.utctime
817 self.firstdatatime = self.dataIn.utctime
818
818
819 if self.profIndex == nProfiles:
819 if self.profIndex == nProfiles:
820 self.__updateObjFromInput()
820 self.__updateObjFromInput()
821 self.__getFft()
821 self.__getFft()
822
822
823 self.dataOut.flagNoData = False
823 self.dataOut.flagNoData = False
824
824
825 self.buffer = None
825 self.buffer = None
826 self.firstdatatime = None
826 self.firstdatatime = None
827 self.profIndex = 0
827 self.profIndex = 0
828
828
829 return
829 return
830
830
831 raise ValuError, "The type object %s is not valid"%(self.dataIn.type)
831 raise ValuError, "The type object %s is not valid"%(self.dataIn.type)
832
832
833 def selectChannels(self, channelList):
833 def selectChannels(self, channelList):
834
834
835 channelIndexList = []
835 channelIndexList = []
836
836
837 for channel in channelList:
837 for channel in channelList:
838 index = self.dataOut.channelList.index(channel)
838 index = self.dataOut.channelList.index(channel)
839 channelIndexList.append(index)
839 channelIndexList.append(index)
840
840
841 self.selectChannelsByIndex(channelIndexList)
841 self.selectChannelsByIndex(channelIndexList)
842
842
843 def selectChannelsByIndex(self, channelIndexList):
843 def selectChannelsByIndex(self, channelIndexList):
844 """
844 """
845 Selecciona un bloque de datos en base a canales segun el channelIndexList
845 Selecciona un bloque de datos en base a canales segun el channelIndexList
846
846
847 Input:
847 Input:
848 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
848 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
849
849
850 Affected:
850 Affected:
851 self.dataOut.data_spc
851 self.dataOut.data_spc
852 self.dataOut.channelIndexList
852 self.dataOut.channelIndexList
853 self.dataOut.nChannels
853 self.dataOut.nChannels
854
854
855 Return:
855 Return:
856 None
856 None
857 """
857 """
858
858
859 for channelIndex in channelIndexList:
859 for channelIndex in channelIndexList:
860 if channelIndex not in self.dataOut.channelIndexList:
860 if channelIndex not in self.dataOut.channelIndexList:
861 print channelIndexList
861 print channelIndexList
862 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
862 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
863
863
864 nChannels = len(channelIndexList)
864 nChannels = len(channelIndexList)
865
865
866 data_spc = self.dataOut.data_spc[channelIndexList,:]
866 data_spc = self.dataOut.data_spc[channelIndexList,:]
867
867
868 self.dataOut.data_spc = data_spc
868 self.dataOut.data_spc = data_spc
869 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
869 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
870 # self.dataOut.nChannels = nChannels
870 # self.dataOut.nChannels = nChannels
871
871
872 return 1
872 return 1
873
873
874 def selectHeights(self, minHei, maxHei):
874 def selectHeights(self, minHei, maxHei):
875 """
875 """
876 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
876 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
877 minHei <= height <= maxHei
877 minHei <= height <= maxHei
878
878
879 Input:
879 Input:
880 minHei : valor minimo de altura a considerar
880 minHei : valor minimo de altura a considerar
881 maxHei : valor maximo de altura a considerar
881 maxHei : valor maximo de altura a considerar
882
882
883 Affected:
883 Affected:
884 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
884 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
885
885
886 Return:
886 Return:
887 1 si el metodo se ejecuto con exito caso contrario devuelve 0
887 1 si el metodo se ejecuto con exito caso contrario devuelve 0
888 """
888 """
889 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
889 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
890 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
890 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
891
891
892 if (maxHei > self.dataOut.heightList[-1]):
892 if (maxHei > self.dataOut.heightList[-1]):
893 maxHei = self.dataOut.heightList[-1]
893 maxHei = self.dataOut.heightList[-1]
894 # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
894 # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
895
895
896 minIndex = 0
896 minIndex = 0
897 maxIndex = 0
897 maxIndex = 0
898 heights = self.dataOut.heightList
898 heights = self.dataOut.heightList
899
899
900 inda = numpy.where(heights >= minHei)
900 inda = numpy.where(heights >= minHei)
901 indb = numpy.where(heights <= maxHei)
901 indb = numpy.where(heights <= maxHei)
902
902
903 try:
903 try:
904 minIndex = inda[0][0]
904 minIndex = inda[0][0]
905 except:
905 except:
906 minIndex = 0
906 minIndex = 0
907
907
908 try:
908 try:
909 maxIndex = indb[0][-1]
909 maxIndex = indb[0][-1]
910 except:
910 except:
911 maxIndex = len(heights)
911 maxIndex = len(heights)
912
912
913 self.selectHeightsByIndex(minIndex, maxIndex)
913 self.selectHeightsByIndex(minIndex, maxIndex)
914
914
915 return 1
915 return 1
916
916
917
917
918 def selectHeightsByIndex(self, minIndex, maxIndex):
918 def selectHeightsByIndex(self, minIndex, maxIndex):
919 """
919 """
920 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
920 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
921 minIndex <= index <= maxIndex
921 minIndex <= index <= maxIndex
922
922
923 Input:
923 Input:
924 minIndex : valor de indice minimo de altura a considerar
924 minIndex : valor de indice minimo de altura a considerar
925 maxIndex : valor de indice maximo de altura a considerar
925 maxIndex : valor de indice maximo de altura a considerar
926
926
927 Affected:
927 Affected:
928 self.dataOut.data_spc
928 self.dataOut.data_spc
929 self.dataOut.data_cspc
929 self.dataOut.data_cspc
930 self.dataOut.data_dc
930 self.dataOut.data_dc
931 self.dataOut.heightList
931 self.dataOut.heightList
932
932
933 Return:
933 Return:
934 1 si el metodo se ejecuto con exito caso contrario devuelve 0
934 1 si el metodo se ejecuto con exito caso contrario devuelve 0
935 """
935 """
936
936
937 if (minIndex < 0) or (minIndex > maxIndex):
937 if (minIndex < 0) or (minIndex > maxIndex):
938 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
938 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
939
939
940 if (maxIndex >= self.dataOut.nHeights):
940 if (maxIndex >= self.dataOut.nHeights):
941 maxIndex = self.dataOut.nHeights-1
941 maxIndex = self.dataOut.nHeights-1
942 # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
942 # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
943
943
944 nHeights = maxIndex - minIndex + 1
944 nHeights = maxIndex - minIndex + 1
945
945
946 #Spectra
946 #Spectra
947 data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1]
947 data_spc = self.dataOut.data_spc[:,:,minIndex:maxIndex+1]
948
948
949 data_cspc = None
949 data_cspc = None
950 if self.dataOut.data_cspc != None:
950 if self.dataOut.data_cspc != None:
951 data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1]
951 data_cspc = self.dataOut.data_cspc[:,:,minIndex:maxIndex+1]
952
952
953 data_dc = None
953 data_dc = None
954 if self.dataOut.data_dc != None:
954 if self.dataOut.data_dc != None:
955 data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1]
955 data_dc = self.dataOut.data_dc[:,minIndex:maxIndex+1]
956
956
957 self.dataOut.data_spc = data_spc
957 self.dataOut.data_spc = data_spc
958 self.dataOut.data_cspc = data_cspc
958 self.dataOut.data_cspc = data_cspc
959 self.dataOut.data_dc = data_dc
959 self.dataOut.data_dc = data_dc
960
960
961 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
961 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
962
962
963 return 1
963 return 1
964
964
965 def removeDC(self, mode = 1):
965 def removeDC(self, mode = 1):
966
966
967 dc_index = 0
967 dc_index = 0
968 freq_index = numpy.array([-2,-1,1,2])
968 freq_index = numpy.array([-2,-1,1,2])
969 data_spc = self.dataOut.data_spc
969 data_spc = self.dataOut.data_spc
970 data_cspc = self.dataOut.data_cspc
970 data_cspc = self.dataOut.data_cspc
971 data_dc = self.dataOut.data_dc
971 data_dc = self.dataOut.data_dc
972
972
973 if self.dataOut.flagShiftFFT:
973 if self.dataOut.flagShiftFFT:
974 dc_index += self.dataOut.nFFTPoints/2
974 dc_index += self.dataOut.nFFTPoints/2
975 freq_index += self.dataOut.nFFTPoints/2
975 freq_index += self.dataOut.nFFTPoints/2
976
976
977 if mode == 1:
977 if mode == 1:
978 data_spc[dc_index] = (data_spc[:,freq_index[1],:] + data_spc[:,freq_index[2],:])/2
978 data_spc[dc_index] = (data_spc[:,freq_index[1],:] + data_spc[:,freq_index[2],:])/2
979 if data_cspc != None:
979 if data_cspc != None:
980 data_cspc[dc_index] = (data_cspc[:,freq_index[1],:] + data_cspc[:,freq_index[2],:])/2
980 data_cspc[dc_index] = (data_cspc[:,freq_index[1],:] + data_cspc[:,freq_index[2],:])/2
981 return 1
981 return 1
982
982
983 if mode == 2:
983 if mode == 2:
984 pass
984 pass
985
985
986 if mode == 3:
986 if mode == 3:
987 pass
987 pass
988
988
989 raise ValueError, "mode parameter has to be 1, 2 or 3"
989 raise ValueError, "mode parameter has to be 1, 2 or 3"
990
990
991 def removeInterference(self):
991 def removeInterference(self):
992
992
993 pass
993 pass
994
994
995 def setRadarFrequency(self, frequency=None):
995 def setRadarFrequency(self, frequency=None):
996 if frequency != None:
996 if frequency != None:
997 self.dataOut.frequency = frequency
997 self.dataOut.frequency = frequency
998
998
999 return 1
999 return 1
1000
1000
1001
1001
1002 class IncohInt(Operation):
1002 class IncohInt(Operation):
1003
1003
1004
1004
1005 __profIndex = 0
1005 __profIndex = 0
1006 __withOverapping = False
1006 __withOverapping = False
1007
1007
1008 __byTime = False
1008 __byTime = False
1009 __initime = None
1009 __initime = None
1010 __lastdatatime = None
1010 __lastdatatime = None
1011 __integrationtime = None
1011 __integrationtime = None
1012
1012
1013 __buffer_spc = None
1013 __buffer_spc = None
1014 __buffer_cspc = None
1014 __buffer_cspc = None
1015 __buffer_dc = None
1015 __buffer_dc = None
1016
1016
1017 __dataReady = False
1017 __dataReady = False
1018
1018
1019 __timeInterval = None
1019 __timeInterval = None
1020
1020
1021 n = None
1021 n = None
1022
1022
1023
1023
1024
1024
1025 def __init__(self):
1025 def __init__(self):
1026
1026
1027 self.__isConfig = False
1027 self.__isConfig = False
1028
1028
1029 def setup(self, n=None, timeInterval=None, overlapping=False):
1029 def setup(self, n=None, timeInterval=None, overlapping=False):
1030 """
1030 """
1031 Set the parameters of the integration class.
1031 Set the parameters of the integration class.
1032
1032
1033 Inputs:
1033 Inputs:
1034
1034
1035 n : Number of coherent integrations
1035 n : Number of coherent integrations
1036 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1036 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1037 overlapping :
1037 overlapping :
1038
1038
1039 """
1039 """
1040
1040
1041 self.__initime = None
1041 self.__initime = None
1042 self.__lastdatatime = 0
1042 self.__lastdatatime = 0
1043 self.__buffer_spc = None
1043 self.__buffer_spc = None
1044 self.__buffer_cspc = None
1044 self.__buffer_cspc = None
1045 self.__buffer_dc = None
1045 self.__buffer_dc = None
1046 self.__dataReady = False
1046 self.__dataReady = False
1047
1047
1048
1048
1049 if n == None and timeInterval == None:
1049 if n == None and timeInterval == None:
1050 raise ValueError, "n or timeInterval should be specified ..."
1050 raise ValueError, "n or timeInterval should be specified ..."
1051
1051
1052 if n != None:
1052 if n != None:
1053 self.n = n
1053 self.n = n
1054 self.__byTime = False
1054 self.__byTime = False
1055 else:
1055 else:
1056 self.__integrationtime = timeInterval #if (type(timeInterval)!=integer) -> change this line
1056 self.__integrationtime = timeInterval #if (type(timeInterval)!=integer) -> change this line
1057 self.n = 9999
1057 self.n = 9999
1058 self.__byTime = True
1058 self.__byTime = True
1059
1059
1060 if overlapping:
1060 if overlapping:
1061 self.__withOverapping = True
1061 self.__withOverapping = True
1062 else:
1062 else:
1063 self.__withOverapping = False
1063 self.__withOverapping = False
1064 self.__buffer_spc = 0
1064 self.__buffer_spc = 0
1065 self.__buffer_cspc = 0
1065 self.__buffer_cspc = 0
1066 self.__buffer_dc = 0
1066 self.__buffer_dc = 0
1067
1067
1068 self.__profIndex = 0
1068 self.__profIndex = 0
1069
1069
1070 def putData(self, data_spc, data_cspc, data_dc):
1070 def putData(self, data_spc, data_cspc, data_dc):
1071
1071
1072 """
1072 """
1073 Add a profile to the __buffer_spc and increase in one the __profileIndex
1073 Add a profile to the __buffer_spc and increase in one the __profileIndex
1074
1074
1075 """
1075 """
1076
1076
1077 if not self.__withOverapping:
1077 if not self.__withOverapping:
1078 self.__buffer_spc += data_spc
1078 self.__buffer_spc += data_spc
1079
1079
1080 if data_cspc == None:
1080 if data_cspc == None:
1081 self.__buffer_cspc = None
1081 self.__buffer_cspc = None
1082 else:
1082 else:
1083 self.__buffer_cspc += data_cspc
1083 self.__buffer_cspc += data_cspc
1084
1084
1085 if data_dc == None:
1085 if data_dc == None:
1086 self.__buffer_dc = None
1086 self.__buffer_dc = None
1087 else:
1087 else:
1088 self.__buffer_dc += data_dc
1088 self.__buffer_dc += data_dc
1089
1089
1090 self.__profIndex += 1
1090 self.__profIndex += 1
1091 return
1091 return
1092
1092
1093 #Overlapping data
1093 #Overlapping data
1094 nChannels, nFFTPoints, nHeis = data_spc.shape
1094 nChannels, nFFTPoints, nHeis = data_spc.shape
1095 data_spc = numpy.reshape(data_spc, (1, nChannels, nFFTPoints, nHeis))
1095 data_spc = numpy.reshape(data_spc, (1, nChannels, nFFTPoints, nHeis))
1096 if data_cspc != None:
1096 if data_cspc != None:
1097 data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis))
1097 data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis))
1098 if data_dc != None:
1098 if data_dc != None:
1099 data_dc = numpy.reshape(data_dc, (1, -1, nHeis))
1099 data_dc = numpy.reshape(data_dc, (1, -1, nHeis))
1100
1100
1101 #If the buffer is empty then it takes the data value
1101 #If the buffer is empty then it takes the data value
1102 if self.__buffer_spc == None:
1102 if self.__buffer_spc == None:
1103 self.__buffer_spc = data_spc
1103 self.__buffer_spc = data_spc
1104
1104
1105 if data_cspc == None:
1105 if data_cspc == None:
1106 self.__buffer_cspc = None
1106 self.__buffer_cspc = None
1107 else:
1107 else:
1108 self.__buffer_cspc += data_cspc
1108 self.__buffer_cspc += data_cspc
1109
1109
1110 if data_dc == None:
1110 if data_dc == None:
1111 self.__buffer_dc = None
1111 self.__buffer_dc = None
1112 else:
1112 else:
1113 self.__buffer_dc += data_dc
1113 self.__buffer_dc += data_dc
1114
1114
1115 self.__profIndex += 1
1115 self.__profIndex += 1
1116 return
1116 return
1117
1117
1118 #If the buffer length is lower than n then stakcing the data value
1118 #If the buffer length is lower than n then stakcing the data value
1119 if self.__profIndex < self.n:
1119 if self.__profIndex < self.n:
1120 self.__buffer_spc = numpy.vstack((self.__buffer_spc, data_spc))
1120 self.__buffer_spc = numpy.vstack((self.__buffer_spc, data_spc))
1121
1121
1122 if data_cspc != None:
1122 if data_cspc != None:
1123 self.__buffer_cspc = numpy.vstack((self.__buffer_cspc, data_cspc))
1123 self.__buffer_cspc = numpy.vstack((self.__buffer_cspc, data_cspc))
1124
1124
1125 if data_dc != None:
1125 if data_dc != None:
1126 self.__buffer_dc = numpy.vstack((self.__buffer_dc, data_dc))
1126 self.__buffer_dc = numpy.vstack((self.__buffer_dc, data_dc))
1127
1127
1128 self.__profIndex += 1
1128 self.__profIndex += 1
1129 return
1129 return
1130
1130
1131 #If the buffer length is equal to n then replacing the last buffer value with the data value
1131 #If the buffer length is equal to n then replacing the last buffer value with the data value
1132 self.__buffer_spc = numpy.roll(self.__buffer_spc, -1, axis=0)
1132 self.__buffer_spc = numpy.roll(self.__buffer_spc, -1, axis=0)
1133 self.__buffer_spc[self.n-1] = data_spc
1133 self.__buffer_spc[self.n-1] = data_spc
1134
1134
1135 if data_cspc != None:
1135 if data_cspc != None:
1136 self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0)
1136 self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0)
1137 self.__buffer_cspc[self.n-1] = data_cspc
1137 self.__buffer_cspc[self.n-1] = data_cspc
1138
1138
1139 if data_dc != None:
1139 if data_dc != None:
1140 self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0)
1140 self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0)
1141 self.__buffer_dc[self.n-1] = data_dc
1141 self.__buffer_dc[self.n-1] = data_dc
1142
1142
1143 self.__profIndex = self.n
1143 self.__profIndex = self.n
1144 return
1144 return
1145
1145
1146
1146
1147 def pushData(self):
1147 def pushData(self):
1148 """
1148 """
1149 Return the sum of the last profiles and the profiles used in the sum.
1149 Return the sum of the last profiles and the profiles used in the sum.
1150
1150
1151 Affected:
1151 Affected:
1152
1152
1153 self.__profileIndex
1153 self.__profileIndex
1154
1154
1155 """
1155 """
1156 data_spc = None
1156 data_spc = None
1157 data_cspc = None
1157 data_cspc = None
1158 data_dc = None
1158 data_dc = None
1159
1159
1160 if not self.__withOverapping:
1160 if not self.__withOverapping:
1161 data_spc = self.__buffer_spc
1161 data_spc = self.__buffer_spc
1162 data_cspc = self.__buffer_cspc
1162 data_cspc = self.__buffer_cspc
1163 data_dc = self.__buffer_dc
1163 data_dc = self.__buffer_dc
1164
1164
1165 n = self.__profIndex
1165 n = self.__profIndex
1166
1166
1167 self.__buffer_spc = 0
1167 self.__buffer_spc = 0
1168 self.__buffer_cspc = 0
1168 self.__buffer_cspc = 0
1169 self.__buffer_dc = 0
1169 self.__buffer_dc = 0
1170 self.__profIndex = 0
1170 self.__profIndex = 0
1171
1171
1172 return data_spc, data_cspc, data_dc, n
1172 return data_spc, data_cspc, data_dc, n
1173
1173
1174 #Integration with Overlapping
1174 #Integration with Overlapping
1175 data_spc = numpy.sum(self.__buffer_spc, axis=0)
1175 data_spc = numpy.sum(self.__buffer_spc, axis=0)
1176
1176
1177 if self.__buffer_cspc != None:
1177 if self.__buffer_cspc != None:
1178 data_cspc = numpy.sum(self.__buffer_cspc, axis=0)
1178 data_cspc = numpy.sum(self.__buffer_cspc, axis=0)
1179
1179
1180 if self.__buffer_dc != None:
1180 if self.__buffer_dc != None:
1181 data_dc = numpy.sum(self.__buffer_dc, axis=0)
1181 data_dc = numpy.sum(self.__buffer_dc, axis=0)
1182
1182
1183 n = self.__profIndex
1183 n = self.__profIndex
1184
1184
1185 return data_spc, data_cspc, data_dc, n
1185 return data_spc, data_cspc, data_dc, n
1186
1186
1187 def byProfiles(self, *args):
1187 def byProfiles(self, *args):
1188
1188
1189 self.__dataReady = False
1189 self.__dataReady = False
1190 avgdata_spc = None
1190 avgdata_spc = None
1191 avgdata_cspc = None
1191 avgdata_cspc = None
1192 avgdata_dc = None
1192 avgdata_dc = None
1193 n = None
1193 n = None
1194
1194
1195 self.putData(*args)
1195 self.putData(*args)
1196
1196
1197 if self.__profIndex == self.n:
1197 if self.__profIndex == self.n:
1198
1198
1199 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1199 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1200 self.__dataReady = True
1200 self.__dataReady = True
1201
1201
1202 return avgdata_spc, avgdata_cspc, avgdata_dc
1202 return avgdata_spc, avgdata_cspc, avgdata_dc
1203
1203
1204 def byTime(self, datatime, *args):
1204 def byTime(self, datatime, *args):
1205
1205
1206 self.__dataReady = False
1206 self.__dataReady = False
1207 avgdata_spc = None
1207 avgdata_spc = None
1208 avgdata_cspc = None
1208 avgdata_cspc = None
1209 avgdata_dc = None
1209 avgdata_dc = None
1210 n = None
1210 n = None
1211
1211
1212 self.putData(*args)
1212 self.putData(*args)
1213
1213
1214 if (datatime - self.__initime) >= self.__integrationtime:
1214 if (datatime - self.__initime) >= self.__integrationtime:
1215 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1215 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
1216 self.n = n
1216 self.n = n
1217 self.__dataReady = True
1217 self.__dataReady = True
1218
1218
1219 return avgdata_spc, avgdata_cspc, avgdata_dc
1219 return avgdata_spc, avgdata_cspc, avgdata_dc
1220
1220
1221 def integrate(self, datatime, *args):
1221 def integrate(self, datatime, *args):
1222
1222
1223 if self.__initime == None:
1223 if self.__initime == None:
1224 self.__initime = datatime
1224 self.__initime = datatime
1225
1225
1226 if self.__byTime:
1226 if self.__byTime:
1227 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args)
1227 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args)
1228 else:
1228 else:
1229 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1229 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
1230
1230
1231 self.__lastdatatime = datatime
1231 self.__lastdatatime = datatime
1232
1232
1233 if avgdata_spc == None:
1233 if avgdata_spc == None:
1234 return None, None, None, None
1234 return None, None, None, None
1235
1235
1236 avgdatatime = self.__initime
1236 avgdatatime = self.__initime
1237 try:
1237 try:
1238 self.__timeInterval = (self.__lastdatatime - self.__initime)/(self.n - 1)
1238 self.__timeInterval = (self.__lastdatatime - self.__initime)/(self.n - 1)
1239 except:
1239 except:
1240 self.__timeInterval = self.__lastdatatime - self.__initime
1240 self.__timeInterval = self.__lastdatatime - self.__initime
1241
1241
1242 deltatime = datatime -self.__lastdatatime
1242 deltatime = datatime -self.__lastdatatime
1243
1243
1244 if not self.__withOverapping:
1244 if not self.__withOverapping:
1245 self.__initime = datatime
1245 self.__initime = datatime
1246 else:
1246 else:
1247 self.__initime += deltatime
1247 self.__initime += deltatime
1248
1248
1249 return avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc
1249 return avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc
1250
1250
1251 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
1251 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
1252
1252
1253 if n==1:
1253 if n==1:
1254 dataOut.flagNoData = False
1254 dataOut.flagNoData = False
1255 return
1255 return
1256
1256
1257 if not self.__isConfig:
1257 if not self.__isConfig:
1258 self.setup(n, timeInterval, overlapping)
1258 self.setup(n, timeInterval, overlapping)
1259 self.__isConfig = True
1259 self.__isConfig = True
1260
1260
1261 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1261 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
1262 dataOut.data_spc,
1262 dataOut.data_spc,
1263 dataOut.data_cspc,
1263 dataOut.data_cspc,
1264 dataOut.data_dc)
1264 dataOut.data_dc)
1265
1265
1266 # dataOut.timeInterval *= n
1266 # dataOut.timeInterval *= n
1267 dataOut.flagNoData = True
1267 dataOut.flagNoData = True
1268
1268
1269 if self.__dataReady:
1269 if self.__dataReady:
1270
1270
1271 dataOut.data_spc = avgdata_spc
1271 dataOut.data_spc = avgdata_spc
1272 dataOut.data_cspc = avgdata_cspc
1272 dataOut.data_cspc = avgdata_cspc
1273 dataOut.data_dc = avgdata_dc
1273 dataOut.data_dc = avgdata_dc
1274
1274
1275 dataOut.nIncohInt *= self.n
1275 dataOut.nIncohInt *= self.n
1276 dataOut.utctime = avgdatatime
1276 dataOut.utctime = avgdatatime
1277 #dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt * dataOut.nIncohInt * dataOut.nFFTPoints
1277 #dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt * dataOut.nIncohInt * dataOut.nFFTPoints
1278 dataOut.timeInterval = self.__timeInterval*self.n
1278 dataOut.timeInterval = self.__timeInterval*self.n
1279 dataOut.flagNoData = False
1279 dataOut.flagNoData = False
1280
1280
1281 class ProfileConcat(Operation):
1281 class ProfileConcat(Operation):
1282
1282
1283 __isConfig = False
1283 __isConfig = False
1284 buffer = None
1284 buffer = None
1285
1285
1286 def __init__(self):
1286 def __init__(self):
1287
1287
1288 self.profileIndex = 0
1288 self.profileIndex = 0
1289
1289
1290 def reset(self):
1290 def reset(self):
1291 self.buffer = numpy.zeros_like(self.buffer)
1291 self.buffer = numpy.zeros_like(self.buffer)
1292 self.start_index = 0
1292 self.start_index = 0
1293 self.times = 1
1293 self.times = 1
1294
1294
1295 def setup(self, data, m, n=1):
1295 def setup(self, data, m, n=1):
1296 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
1296 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
1297 self.profiles = data.shape[1]
1297 self.profiles = data.shape[1]
1298 self.start_index = 0
1298 self.start_index = 0
1299 self.times = 1
1299 self.times = 1
1300
1300
1301 def concat(self, data):
1301 def concat(self, data):
1302
1302
1303 self.buffer[:,self.start_index:self.profiles*self.times] = data.copy()
1303 self.buffer[:,self.start_index:self.profiles*self.times] = data.copy()
1304 self.start_index = self.start_index + self.profiles
1304 self.start_index = self.start_index + self.profiles
1305
1305
1306 def run(self, dataOut, m):
1306 def run(self, dataOut, m):
1307
1307
1308 dataOut.flagNoData = True
1308 dataOut.flagNoData = True
1309
1309
1310 if not self.__isConfig:
1310 if not self.__isConfig:
1311 self.setup(dataOut.data, m, 1)
1311 self.setup(dataOut.data, m, 1)
1312 self.__isConfig = True
1312 self.__isConfig = True
1313
1313
1314 self.concat(dataOut.data)
1314 self.concat(dataOut.data)
1315 self.times += 1
1315 self.times += 1
1316 if self.times > m:
1316 if self.times > m:
1317 dataOut.data = self.buffer
1317 dataOut.data = self.buffer
1318 self.reset()
1318 self.reset()
1319 dataOut.flagNoData = False
1319 dataOut.flagNoData = False
1320 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
1320 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
1321 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1321 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1322 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * 5
1322 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * 5
1323 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
1323 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
1324
1324
1325
1325
1326
1326
1327 class ProfileSelector(Operation):
1327 class ProfileSelector(Operation):
1328
1328
1329 profileIndex = None
1329 profileIndex = None
1330 # Tamanho total de los perfiles
1330 # Tamanho total de los perfiles
1331 nProfiles = None
1331 nProfiles = None
1332
1332
1333 def __init__(self):
1333 def __init__(self):
1334
1334
1335 self.profileIndex = 0
1335 self.profileIndex = 0
1336
1336
1337 def incIndex(self):
1337 def incIndex(self):
1338 self.profileIndex += 1
1338 self.profileIndex += 1
1339
1339
1340 if self.profileIndex >= self.nProfiles:
1340 if self.profileIndex >= self.nProfiles:
1341 self.profileIndex = 0
1341 self.profileIndex = 0
1342
1342
1343 def isProfileInRange(self, minIndex, maxIndex):
1343 def isProfileInRange(self, minIndex, maxIndex):
1344
1344
1345 if self.profileIndex < minIndex:
1345 if self.profileIndex < minIndex:
1346 return False
1346 return False
1347
1347
1348 if self.profileIndex > maxIndex:
1348 if self.profileIndex > maxIndex:
1349 return False
1349 return False
1350
1350
1351 return True
1351 return True
1352
1352
1353 def isProfileInList(self, profileList):
1353 def isProfileInList(self, profileList):
1354
1354
1355 if self.profileIndex not in profileList:
1355 if self.profileIndex not in profileList:
1356 return False
1356 return False
1357
1357
1358 return True
1358 return True
1359
1359
1360 def run(self, dataOut, profileList=None, profileRangeList=None):
1360 def run(self, dataOut, profileList=None, profileRangeList=None):
1361
1361
1362 dataOut.flagNoData = True
1362 dataOut.flagNoData = True
1363 self.nProfiles = dataOut.nProfiles
1363 self.nProfiles = dataOut.nProfiles
1364
1364
1365 if profileList != None:
1365 if profileList != None:
1366 if self.isProfileInList(profileList):
1366 if self.isProfileInList(profileList):
1367 dataOut.flagNoData = False
1367 dataOut.flagNoData = False
1368
1368
1369 self.incIndex()
1369 self.incIndex()
1370 return 1
1370 return 1
1371
1371
1372
1372
1373 elif profileRangeList != None:
1373 elif profileRangeList != None:
1374 minIndex = profileRangeList[0]
1374 minIndex = profileRangeList[0]
1375 maxIndex = profileRangeList[1]
1375 maxIndex = profileRangeList[1]
1376 if self.isProfileInRange(minIndex, maxIndex):
1376 if self.isProfileInRange(minIndex, maxIndex):
1377 dataOut.flagNoData = False
1377 dataOut.flagNoData = False
1378
1378
1379 self.incIndex()
1379 self.incIndex()
1380 return 1
1380 return 1
1381
1381
1382 else:
1382 else:
1383 raise ValueError, "ProfileSelector needs profileList or profileRangeList"
1383 raise ValueError, "ProfileSelector needs profileList or profileRangeList"
1384
1384
1385 return 0
1385 return 0
1386
1386
1387 class SpectraHeisProc(ProcessingUnit):
1387 class SpectraHeisProc(ProcessingUnit):
1388 def __init__(self):
1388 def __init__(self):
1389 self.objectDict = {}
1389 self.objectDict = {}
1390 # self.buffer = None
1390 # self.buffer = None
1391 # self.firstdatatime = None
1391 # self.firstdatatime = None
1392 # self.profIndex = 0
1392 # self.profIndex = 0
1393 self.dataOut = SpectraHeis()
1393 self.dataOut = SpectraHeis()
1394
1394
1395 def __updateObjFromInput(self):
1395 def __updateObjFromInput(self):
1396 self.dataOut.timeZone = self.dataIn.timeZone
1396 self.dataOut.timeZone = self.dataIn.timeZone
1397 self.dataOut.dstFlag = self.dataIn.dstFlag
1397 self.dataOut.dstFlag = self.dataIn.dstFlag
1398 self.dataOut.errorCount = self.dataIn.errorCount
1398 self.dataOut.errorCount = self.dataIn.errorCount
1399 self.dataOut.useLocalTime = self.dataIn.useLocalTime
1399 self.dataOut.useLocalTime = self.dataIn.useLocalTime
1400
1400
1401 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()#
1401 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()#
1402 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()#
1402 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()#
1403 self.dataOut.channelList = self.dataIn.channelList
1403 self.dataOut.channelList = self.dataIn.channelList
1404 self.dataOut.heightList = self.dataIn.heightList
1404 self.dataOut.heightList = self.dataIn.heightList
1405 # self.dataOut.dtype = self.dataIn.dtype
1405 # self.dataOut.dtype = self.dataIn.dtype
1406 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
1406 self.dataOut.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
1407 # self.dataOut.nHeights = self.dataIn.nHeights
1407 # self.dataOut.nHeights = self.dataIn.nHeights
1408 # self.dataOut.nChannels = self.dataIn.nChannels
1408 # self.dataOut.nChannels = self.dataIn.nChannels
1409 self.dataOut.nBaud = self.dataIn.nBaud
1409 self.dataOut.nBaud = self.dataIn.nBaud
1410 self.dataOut.nCode = self.dataIn.nCode
1410 self.dataOut.nCode = self.dataIn.nCode
1411 self.dataOut.code = self.dataIn.code
1411 self.dataOut.code = self.dataIn.code
1412 # self.dataOut.nProfiles = 1
1412 # self.dataOut.nProfiles = 1
1413 # self.dataOut.nProfiles = self.dataOut.nFFTPoints
1413 # self.dataOut.nProfiles = self.dataOut.nFFTPoints
1414 self.dataOut.nFFTPoints = self.dataIn.nHeights
1414 self.dataOut.nFFTPoints = self.dataIn.nHeights
1415 # self.dataOut.channelIndexList = self.dataIn.channelIndexList
1415 # self.dataOut.channelIndexList = self.dataIn.channelIndexList
1416 # self.dataOut.flagNoData = self.dataIn.flagNoData
1416 # self.dataOut.flagNoData = self.dataIn.flagNoData
1417 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
1417 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
1418 self.dataOut.utctime = self.dataIn.utctime
1418 self.dataOut.utctime = self.dataIn.utctime
1419 # self.dataOut.utctime = self.firstdatatime
1419 # self.dataOut.utctime = self.firstdatatime
1420 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
1420 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
1421 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
1421 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
1422 # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT
1422 # self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT
1423 self.dataOut.nCohInt = self.dataIn.nCohInt
1423 self.dataOut.nCohInt = self.dataIn.nCohInt
1424 self.dataOut.nIncohInt = 1
1424 self.dataOut.nIncohInt = 1
1425 self.dataOut.ippSeconds= self.dataIn.ippSeconds
1425 self.dataOut.ippSeconds= self.dataIn.ippSeconds
1426 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
1426 self.dataOut.windowOfFilter = self.dataIn.windowOfFilter
1427
1427
1428 self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nIncohInt
1428 self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nIncohInt
1429 # self.dataOut.set=self.dataIn.set
1429 # self.dataOut.set=self.dataIn.set
1430 # self.dataOut.deltaHeight=self.dataIn.deltaHeight
1430 # self.dataOut.deltaHeight=self.dataIn.deltaHeight
1431
1431
1432
1432
1433 def __updateObjFromFits(self):
1434 self.dataOut.utctime = self.dataIn.utctime
1435 self.dataOut.channelIndexList = self.dataIn.channelIndexList
1436
1437 self.dataOut.channelList = self.dataIn.channelList
1438 self.dataOut.heightList = self.dataIn.heightList
1439 self.dataOut.data_spc = self.dataIn.data
1440 self.dataOut.timeInterval = self.dataIn.timeInterval
1441 self.dataOut.timeZone = self.dataIn.timeZone
1442 self.dataOut.useLocalTime = True
1443 # self.dataOut.
1444 # self.dataOut.
1445
1433 def __getFft(self):
1446 def __getFft(self):
1434
1447
1435 fft_volt = numpy.fft.fft(self.dataIn.data, axis=1)
1448 fft_volt = numpy.fft.fft(self.dataIn.data, axis=1)
1436 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
1449 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
1437 spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))/(self.dataOut.nFFTPoints)
1450 spc = numpy.abs(fft_volt * numpy.conjugate(fft_volt))/(self.dataOut.nFFTPoints)
1438 self.dataOut.data_spc = spc
1451 self.dataOut.data_spc = spc
1439
1452
1440 def init(self):
1453 def init(self):
1441
1454
1442 self.dataOut.flagNoData = True
1455 self.dataOut.flagNoData = True
1443
1456
1457 if self.dataIn.type == "Fits":
1458 self.__updateObjFromFits()
1459 self.dataOut.flagNoData = False
1460 return
1461
1444 if self.dataIn.type == "SpectraHeis":
1462 if self.dataIn.type == "SpectraHeis":
1445 self.dataOut.copy(self.dataIn)
1463 self.dataOut.copy(self.dataIn)
1446 return
1464 return
1447
1465
1448 if self.dataIn.type == "Voltage":
1466 if self.dataIn.type == "Voltage":
1449 self.__updateObjFromInput()
1467 self.__updateObjFromInput()
1450 self.__getFft()
1468 self.__getFft()
1451 self.dataOut.flagNoData = False
1469 self.dataOut.flagNoData = False
1452
1470
1453 return
1471 return
1454
1472
1455 raise ValuError, "The type object %s is not valid"%(self.dataIn.type)
1473 raise ValuError, "The type object %s is not valid"%(self.dataIn.type)
1456
1474
1457
1475
1458 def selectChannels(self, channelList):
1476 def selectChannels(self, channelList):
1459
1477
1460 channelIndexList = []
1478 channelIndexList = []
1461
1479
1462 for channel in channelList:
1480 for channel in channelList:
1463 index = self.dataOut.channelList.index(channel)
1481 index = self.dataOut.channelList.index(channel)
1464 channelIndexList.append(index)
1482 channelIndexList.append(index)
1465
1483
1466 self.selectChannelsByIndex(channelIndexList)
1484 self.selectChannelsByIndex(channelIndexList)
1467
1485
1468 def selectChannelsByIndex(self, channelIndexList):
1486 def selectChannelsByIndex(self, channelIndexList):
1469 """
1487 """
1470 Selecciona un bloque de datos en base a canales segun el channelIndexList
1488 Selecciona un bloque de datos en base a canales segun el channelIndexList
1471
1489
1472 Input:
1490 Input:
1473 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
1491 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
1474
1492
1475 Affected:
1493 Affected:
1476 self.dataOut.data
1494 self.dataOut.data
1477 self.dataOut.channelIndexList
1495 self.dataOut.channelIndexList
1478 self.dataOut.nChannels
1496 self.dataOut.nChannels
1479 self.dataOut.m_ProcessingHeader.totalSpectra
1497 self.dataOut.m_ProcessingHeader.totalSpectra
1480 self.dataOut.systemHeaderObj.numChannels
1498 self.dataOut.systemHeaderObj.numChannels
1481 self.dataOut.m_ProcessingHeader.blockSize
1499 self.dataOut.m_ProcessingHeader.blockSize
1482
1500
1483 Return:
1501 Return:
1484 None
1502 None
1485 """
1503 """
1486
1504
1487 for channelIndex in channelIndexList:
1505 for channelIndex in channelIndexList:
1488 if channelIndex not in self.dataOut.channelIndexList:
1506 if channelIndex not in self.dataOut.channelIndexList:
1489 print channelIndexList
1507 print channelIndexList
1490 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
1508 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
1491
1509
1492 nChannels = len(channelIndexList)
1510 nChannels = len(channelIndexList)
1493
1511
1494 data_spc = self.dataOut.data_spc[channelIndexList,:]
1512 data_spc = self.dataOut.data_spc[channelIndexList,:]
1495
1513
1496 self.dataOut.data_spc = data_spc
1514 self.dataOut.data_spc = data_spc
1497 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
1515 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
1498
1516
1499 return 1
1517 return 1
1500
1518
1501 class IncohInt4SpectraHeis(Operation):
1519 class IncohInt4SpectraHeis(Operation):
1502
1520
1503 __isConfig = False
1521 __isConfig = False
1504
1522
1505 __profIndex = 0
1523 __profIndex = 0
1506 __withOverapping = False
1524 __withOverapping = False
1507
1525
1508 __byTime = False
1526 __byTime = False
1509 __initime = None
1527 __initime = None
1510 __lastdatatime = None
1528 __lastdatatime = None
1511 __integrationtime = None
1529 __integrationtime = None
1512
1530
1513 __buffer = None
1531 __buffer = None
1514
1532
1515 __dataReady = False
1533 __dataReady = False
1516
1534
1517 n = None
1535 n = None
1518
1536
1519
1537
1520 def __init__(self):
1538 def __init__(self):
1521
1539
1522 self.__isConfig = False
1540 self.__isConfig = False
1523
1541
1524 def setup(self, n=None, timeInterval=None, overlapping=False):
1542 def setup(self, n=None, timeInterval=None, overlapping=False):
1525 """
1543 """
1526 Set the parameters of the integration class.
1544 Set the parameters of the integration class.
1527
1545
1528 Inputs:
1546 Inputs:
1529
1547
1530 n : Number of coherent integrations
1548 n : Number of coherent integrations
1531 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1549 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
1532 overlapping :
1550 overlapping :
1533
1551
1534 """
1552 """
1535
1553
1536 self.__initime = None
1554 self.__initime = None
1537 self.__lastdatatime = 0
1555 self.__lastdatatime = 0
1538 self.__buffer = None
1556 self.__buffer = None
1539 self.__dataReady = False
1557 self.__dataReady = False
1540
1558
1541
1559
1542 if n == None and timeInterval == None:
1560 if n == None and timeInterval == None:
1543 raise ValueError, "n or timeInterval should be specified ..."
1561 raise ValueError, "n or timeInterval should be specified ..."
1544
1562
1545 if n != None:
1563 if n != None:
1546 self.n = n
1564 self.n = n
1547 self.__byTime = False
1565 self.__byTime = False
1548 else:
1566 else:
1549 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
1567 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
1550 self.n = 9999
1568 self.n = 9999
1551 self.__byTime = True
1569 self.__byTime = True
1552
1570
1553 if overlapping:
1571 if overlapping:
1554 self.__withOverapping = True
1572 self.__withOverapping = True
1555 self.__buffer = None
1573 self.__buffer = None
1556 else:
1574 else:
1557 self.__withOverapping = False
1575 self.__withOverapping = False
1558 self.__buffer = 0
1576 self.__buffer = 0
1559
1577
1560 self.__profIndex = 0
1578 self.__profIndex = 0
1561
1579
1562 def putData(self, data):
1580 def putData(self, data):
1563
1581
1564 """
1582 """
1565 Add a profile to the __buffer and increase in one the __profileIndex
1583 Add a profile to the __buffer and increase in one the __profileIndex
1566
1584
1567 """
1585 """
1568
1586
1569 if not self.__withOverapping:
1587 if not self.__withOverapping:
1570 self.__buffer += data.copy()
1588 self.__buffer += data.copy()
1571 self.__profIndex += 1
1589 self.__profIndex += 1
1572 return
1590 return
1573
1591
1574 #Overlapping data
1592 #Overlapping data
1575 nChannels, nHeis = data.shape
1593 nChannels, nHeis = data.shape
1576 data = numpy.reshape(data, (1, nChannels, nHeis))
1594 data = numpy.reshape(data, (1, nChannels, nHeis))
1577
1595
1578 #If the buffer is empty then it takes the data value
1596 #If the buffer is empty then it takes the data value
1579 if self.__buffer == None:
1597 if self.__buffer == None:
1580 self.__buffer = data
1598 self.__buffer = data
1581 self.__profIndex += 1
1599 self.__profIndex += 1
1582 return
1600 return
1583
1601
1584 #If the buffer length is lower than n then stakcing the data value
1602 #If the buffer length is lower than n then stakcing the data value
1585 if self.__profIndex < self.n:
1603 if self.__profIndex < self.n:
1586 self.__buffer = numpy.vstack((self.__buffer, data))
1604 self.__buffer = numpy.vstack((self.__buffer, data))
1587 self.__profIndex += 1
1605 self.__profIndex += 1
1588 return
1606 return
1589
1607
1590 #If the buffer length is equal to n then replacing the last buffer value with the data value
1608 #If the buffer length is equal to n then replacing the last buffer value with the data value
1591 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
1609 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
1592 self.__buffer[self.n-1] = data
1610 self.__buffer[self.n-1] = data
1593 self.__profIndex = self.n
1611 self.__profIndex = self.n
1594 return
1612 return
1595
1613
1596
1614
1597 def pushData(self):
1615 def pushData(self):
1598 """
1616 """
1599 Return the sum of the last profiles and the profiles used in the sum.
1617 Return the sum of the last profiles and the profiles used in the sum.
1600
1618
1601 Affected:
1619 Affected:
1602
1620
1603 self.__profileIndex
1621 self.__profileIndex
1604
1622
1605 """
1623 """
1606
1624
1607 if not self.__withOverapping:
1625 if not self.__withOverapping:
1608 data = self.__buffer
1626 data = self.__buffer
1609 n = self.__profIndex
1627 n = self.__profIndex
1610
1628
1611 self.__buffer = 0
1629 self.__buffer = 0
1612 self.__profIndex = 0
1630 self.__profIndex = 0
1613
1631
1614 return data, n
1632 return data, n
1615
1633
1616 #Integration with Overlapping
1634 #Integration with Overlapping
1617 data = numpy.sum(self.__buffer, axis=0)
1635 data = numpy.sum(self.__buffer, axis=0)
1618 n = self.__profIndex
1636 n = self.__profIndex
1619
1637
1620 return data, n
1638 return data, n
1621
1639
1622 def byProfiles(self, data):
1640 def byProfiles(self, data):
1623
1641
1624 self.__dataReady = False
1642 self.__dataReady = False
1625 avgdata = None
1643 avgdata = None
1626 n = None
1644 n = None
1627
1645
1628 self.putData(data)
1646 self.putData(data)
1629
1647
1630 if self.__profIndex == self.n:
1648 if self.__profIndex == self.n:
1631
1649
1632 avgdata, n = self.pushData()
1650 avgdata, n = self.pushData()
1633 self.__dataReady = True
1651 self.__dataReady = True
1634
1652
1635 return avgdata
1653 return avgdata
1636
1654
1637 def byTime(self, data, datatime):
1655 def byTime(self, data, datatime):
1638
1656
1639 self.__dataReady = False
1657 self.__dataReady = False
1640 avgdata = None
1658 avgdata = None
1641 n = None
1659 n = None
1642
1660
1643 self.putData(data)
1661 self.putData(data)
1644
1662
1645 if (datatime - self.__initime) >= self.__integrationtime:
1663 if (datatime - self.__initime) >= self.__integrationtime:
1646 avgdata, n = self.pushData()
1664 avgdata, n = self.pushData()
1647 self.n = n
1665 self.n = n
1648 self.__dataReady = True
1666 self.__dataReady = True
1649
1667
1650 return avgdata
1668 return avgdata
1651
1669
1652 def integrate(self, data, datatime=None):
1670 def integrate(self, data, datatime=None):
1653
1671
1654 if self.__initime == None:
1672 if self.__initime == None:
1655 self.__initime = datatime
1673 self.__initime = datatime
1656
1674
1657 if self.__byTime:
1675 if self.__byTime:
1658 avgdata = self.byTime(data, datatime)
1676 avgdata = self.byTime(data, datatime)
1659 else:
1677 else:
1660 avgdata = self.byProfiles(data)
1678 avgdata = self.byProfiles(data)
1661
1679
1662
1680
1663 self.__lastdatatime = datatime
1681 self.__lastdatatime = datatime
1664
1682
1665 if avgdata == None:
1683 if avgdata == None:
1666 return None, None
1684 return None, None
1667
1685
1668 avgdatatime = self.__initime
1686 avgdatatime = self.__initime
1669
1687
1670 deltatime = datatime -self.__lastdatatime
1688 deltatime = datatime -self.__lastdatatime
1671
1689
1672 if not self.__withOverapping:
1690 if not self.__withOverapping:
1673 self.__initime = datatime
1691 self.__initime = datatime
1674 else:
1692 else:
1675 self.__initime += deltatime
1693 self.__initime += deltatime
1676
1694
1677 return avgdata, avgdatatime
1695 return avgdata, avgdatatime
1678
1696
1679 def run(self, dataOut, **kwargs):
1697 def run(self, dataOut, **kwargs):
1680
1698
1681 if not self.__isConfig:
1699 if not self.__isConfig:
1682 self.setup(**kwargs)
1700 self.setup(**kwargs)
1683 self.__isConfig = True
1701 self.__isConfig = True
1684
1702
1685 avgdata, avgdatatime = self.integrate(dataOut.data_spc, dataOut.utctime)
1703 avgdata, avgdatatime = self.integrate(dataOut.data_spc, dataOut.utctime)
1686
1704
1687 # dataOut.timeInterval *= n
1705 # dataOut.timeInterval *= n
1688 dataOut.flagNoData = True
1706 dataOut.flagNoData = True
1689
1707
1690 if self.__dataReady:
1708 if self.__dataReady:
1691 dataOut.data_spc = avgdata
1709 dataOut.data_spc = avgdata
1692 dataOut.nIncohInt *= self.n
1710 dataOut.nIncohInt *= self.n
1693 # dataOut.nCohInt *= self.n
1711 # dataOut.nCohInt *= self.n
1694 dataOut.utctime = avgdatatime
1712 dataOut.utctime = avgdatatime
1695 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nIncohInt
1713 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nIncohInt
1696 # dataOut.timeInterval = self.__timeInterval*self.n
1714 # dataOut.timeInterval = self.__timeInterval*self.n
1697 dataOut.flagNoData = False
1715 dataOut.flagNoData = False
1698
1716
1699
1717
1700
1718
1701
1719
1702 No newline at end of file
1720
General Comments 0
You need to be logged in to leave comments. Login now