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