##// END OF EJS Templates
remove prints
Juan C. Espinoza -
r995:39af0b6e404b
parent child
Show More
@@ -1,1816 +1,1813
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import sys
7 import sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import inspect
12 import inspect
13 import time, datetime
13 import time, datetime
14 import traceback
14 import traceback
15 import zmq
15 import zmq
16
16
17 try:
17 try:
18 from gevent import sleep
18 from gevent import sleep
19 except:
19 except:
20 from time import sleep
20 from time import sleep
21
21
22 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
22 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
23 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
23 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
24
24
25 LOCALTIME = True
25 LOCALTIME = True
26
26
27 def isNumber(cad):
27 def isNumber(cad):
28 """
28 """
29 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
29 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
30
30
31 Excepciones:
31 Excepciones:
32 Si un determinado string no puede ser convertido a numero
32 Si un determinado string no puede ser convertido a numero
33 Input:
33 Input:
34 str, string al cual se le analiza para determinar si convertible a un numero o no
34 str, string al cual se le analiza para determinar si convertible a un numero o no
35
35
36 Return:
36 Return:
37 True : si el string es uno numerico
37 True : si el string es uno numerico
38 False : no es un string numerico
38 False : no es un string numerico
39 """
39 """
40 try:
40 try:
41 float( cad )
41 float( cad )
42 return True
42 return True
43 except:
43 except:
44 return False
44 return False
45
45
46 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
46 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
47 """
47 """
48 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
48 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
49
49
50 Inputs:
50 Inputs:
51 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
51 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
52
52
53 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
53 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
54 segundos contados desde 01/01/1970.
54 segundos contados desde 01/01/1970.
55 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
55 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
56 segundos contados desde 01/01/1970.
56 segundos contados desde 01/01/1970.
57
57
58 Return:
58 Return:
59 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
59 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
60 fecha especificado, de lo contrario retorna False.
60 fecha especificado, de lo contrario retorna False.
61
61
62 Excepciones:
62 Excepciones:
63 Si el archivo no existe o no puede ser abierto
63 Si el archivo no existe o no puede ser abierto
64 Si la cabecera no puede ser leida.
64 Si la cabecera no puede ser leida.
65
65
66 """
66 """
67 basicHeaderObj = BasicHeader(LOCALTIME)
67 basicHeaderObj = BasicHeader(LOCALTIME)
68
68
69 try:
69 try:
70 fp = open(filename,'rb')
70 fp = open(filename,'rb')
71 except IOError:
71 except IOError:
72 print "The file %s can't be opened" %(filename)
72 print "The file %s can't be opened" %(filename)
73 return 0
73 return 0
74
74
75 sts = basicHeaderObj.read(fp)
75 sts = basicHeaderObj.read(fp)
76 fp.close()
76 fp.close()
77
77
78 if not(sts):
78 if not(sts):
79 print "Skipping the file %s because it has not a valid header" %(filename)
79 print "Skipping the file %s because it has not a valid header" %(filename)
80 return 0
80 return 0
81
81
82 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
82 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
83 return 0
83 return 0
84
84
85 return 1
85 return 1
86
86
87 def isTimeInRange(thisTime, startTime, endTime):
87 def isTimeInRange(thisTime, startTime, endTime):
88
88
89 if endTime >= startTime:
89 if endTime >= startTime:
90 if (thisTime < startTime) or (thisTime > endTime):
90 if (thisTime < startTime) or (thisTime > endTime):
91 return 0
91 return 0
92
92
93 return 1
93 return 1
94 else:
94 else:
95 if (thisTime < startTime) and (thisTime > endTime):
95 if (thisTime < startTime) and (thisTime > endTime):
96 return 0
96 return 0
97
97
98 return 1
98 return 1
99
99
100 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
100 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
101 """
101 """
102 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
102 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
103
103
104 Inputs:
104 Inputs:
105 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
105 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
106
106
107 startDate : fecha inicial del rango seleccionado en formato datetime.date
107 startDate : fecha inicial del rango seleccionado en formato datetime.date
108
108
109 endDate : fecha final del rango seleccionado en formato datetime.date
109 endDate : fecha final del rango seleccionado en formato datetime.date
110
110
111 startTime : tiempo inicial del rango seleccionado en formato datetime.time
111 startTime : tiempo inicial del rango seleccionado en formato datetime.time
112
112
113 endTime : tiempo final del rango seleccionado en formato datetime.time
113 endTime : tiempo final del rango seleccionado en formato datetime.time
114
114
115 Return:
115 Return:
116 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
116 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
117 fecha especificado, de lo contrario retorna False.
117 fecha especificado, de lo contrario retorna False.
118
118
119 Excepciones:
119 Excepciones:
120 Si el archivo no existe o no puede ser abierto
120 Si el archivo no existe o no puede ser abierto
121 Si la cabecera no puede ser leida.
121 Si la cabecera no puede ser leida.
122
122
123 """
123 """
124
124
125
125
126 try:
126 try:
127 fp = open(filename,'rb')
127 fp = open(filename,'rb')
128 except IOError:
128 except IOError:
129 print "The file %s can't be opened" %(filename)
129 print "The file %s can't be opened" %(filename)
130 return None
130 return None
131
131
132 firstBasicHeaderObj = BasicHeader(LOCALTIME)
132 firstBasicHeaderObj = BasicHeader(LOCALTIME)
133 systemHeaderObj = SystemHeader()
133 systemHeaderObj = SystemHeader()
134 radarControllerHeaderObj = RadarControllerHeader()
134 radarControllerHeaderObj = RadarControllerHeader()
135 processingHeaderObj = ProcessingHeader()
135 processingHeaderObj = ProcessingHeader()
136
136
137 lastBasicHeaderObj = BasicHeader(LOCALTIME)
137 lastBasicHeaderObj = BasicHeader(LOCALTIME)
138
138
139 sts = firstBasicHeaderObj.read(fp)
139 sts = firstBasicHeaderObj.read(fp)
140
140
141 if not(sts):
141 if not(sts):
142 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
142 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
143 return None
143 return None
144
144
145 if not systemHeaderObj.read(fp):
145 if not systemHeaderObj.read(fp):
146 return None
146 return None
147
147
148 if not radarControllerHeaderObj.read(fp):
148 if not radarControllerHeaderObj.read(fp):
149 return None
149 return None
150
150
151 if not processingHeaderObj.read(fp):
151 if not processingHeaderObj.read(fp):
152 return None
152 return None
153
153
154 filesize = os.path.getsize(filename)
154 filesize = os.path.getsize(filename)
155
155
156 offset = processingHeaderObj.blockSize + 24 #header size
156 offset = processingHeaderObj.blockSize + 24 #header size
157
157
158 if filesize <= offset:
158 if filesize <= offset:
159 print "[Reading] %s: This file has not enough data" %filename
159 print "[Reading] %s: This file has not enough data" %filename
160 return None
160 return None
161
161
162 fp.seek(-offset, 2)
162 fp.seek(-offset, 2)
163
163
164 sts = lastBasicHeaderObj.read(fp)
164 sts = lastBasicHeaderObj.read(fp)
165
165
166 fp.close()
166 fp.close()
167
167
168 thisDatetime = lastBasicHeaderObj.datatime
168 thisDatetime = lastBasicHeaderObj.datatime
169 thisTime_last_block = thisDatetime.time()
169 thisTime_last_block = thisDatetime.time()
170
170
171 thisDatetime = firstBasicHeaderObj.datatime
171 thisDatetime = firstBasicHeaderObj.datatime
172 thisDate = thisDatetime.date()
172 thisDate = thisDatetime.date()
173 thisTime_first_block = thisDatetime.time()
173 thisTime_first_block = thisDatetime.time()
174
174
175 #General case
175 #General case
176 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
176 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
177 #-----------o----------------------------o-----------
177 #-----------o----------------------------o-----------
178 # startTime endTime
178 # startTime endTime
179
179
180 if endTime >= startTime:
180 if endTime >= startTime:
181 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
181 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
182 return None
182 return None
183
183
184 return thisDatetime
184 return thisDatetime
185
185
186 #If endTime < startTime then endTime belongs to the next day
186 #If endTime < startTime then endTime belongs to the next day
187
187
188
188
189 #<<<<<<<<<<<o o>>>>>>>>>>>
189 #<<<<<<<<<<<o o>>>>>>>>>>>
190 #-----------o----------------------------o-----------
190 #-----------o----------------------------o-----------
191 # endTime startTime
191 # endTime startTime
192
192
193 if (thisDate == startDate) and (thisTime_last_block < startTime):
193 if (thisDate == startDate) and (thisTime_last_block < startTime):
194 return None
194 return None
195
195
196 if (thisDate == endDate) and (thisTime_first_block > endTime):
196 if (thisDate == endDate) and (thisTime_first_block > endTime):
197 return None
197 return None
198
198
199 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
199 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
200 return None
200 return None
201
201
202 return thisDatetime
202 return thisDatetime
203
203
204 def isFolderInDateRange(folder, startDate=None, endDate=None):
204 def isFolderInDateRange(folder, startDate=None, endDate=None):
205 """
205 """
206 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
206 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
207
207
208 Inputs:
208 Inputs:
209 folder : nombre completo del directorio.
209 folder : nombre completo del directorio.
210 Su formato deberia ser "/path_root/?YYYYDDD"
210 Su formato deberia ser "/path_root/?YYYYDDD"
211
211
212 siendo:
212 siendo:
213 YYYY : Anio (ejemplo 2015)
213 YYYY : Anio (ejemplo 2015)
214 DDD : Dia del anio (ejemplo 305)
214 DDD : Dia del anio (ejemplo 305)
215
215
216 startDate : fecha inicial del rango seleccionado en formato datetime.date
216 startDate : fecha inicial del rango seleccionado en formato datetime.date
217
217
218 endDate : fecha final del rango seleccionado en formato datetime.date
218 endDate : fecha final del rango seleccionado en formato datetime.date
219
219
220 Return:
220 Return:
221 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
221 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
222 fecha especificado, de lo contrario retorna False.
222 fecha especificado, de lo contrario retorna False.
223 Excepciones:
223 Excepciones:
224 Si el directorio no tiene el formato adecuado
224 Si el directorio no tiene el formato adecuado
225 """
225 """
226
226
227 basename = os.path.basename(folder)
227 basename = os.path.basename(folder)
228
228
229 if not isRadarFolder(basename):
229 if not isRadarFolder(basename):
230 print "The folder %s has not the rigth format" %folder
230 print "The folder %s has not the rigth format" %folder
231 return 0
231 return 0
232
232
233 if startDate and endDate:
233 if startDate and endDate:
234 thisDate = getDateFromRadarFolder(basename)
234 thisDate = getDateFromRadarFolder(basename)
235
235
236 if thisDate < startDate:
236 if thisDate < startDate:
237 return 0
237 return 0
238
238
239 if thisDate > endDate:
239 if thisDate > endDate:
240 return 0
240 return 0
241
241
242 return 1
242 return 1
243
243
244 def isFileInDateRange(filename, startDate=None, endDate=None):
244 def isFileInDateRange(filename, startDate=None, endDate=None):
245 """
245 """
246 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
246 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
247
247
248 Inputs:
248 Inputs:
249 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
249 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
250
250
251 Su formato deberia ser "?YYYYDDDsss"
251 Su formato deberia ser "?YYYYDDDsss"
252
252
253 siendo:
253 siendo:
254 YYYY : Anio (ejemplo 2015)
254 YYYY : Anio (ejemplo 2015)
255 DDD : Dia del anio (ejemplo 305)
255 DDD : Dia del anio (ejemplo 305)
256 sss : set
256 sss : set
257
257
258 startDate : fecha inicial del rango seleccionado en formato datetime.date
258 startDate : fecha inicial del rango seleccionado en formato datetime.date
259
259
260 endDate : fecha final del rango seleccionado en formato datetime.date
260 endDate : fecha final del rango seleccionado en formato datetime.date
261
261
262 Return:
262 Return:
263 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
263 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
264 fecha especificado, de lo contrario retorna False.
264 fecha especificado, de lo contrario retorna False.
265 Excepciones:
265 Excepciones:
266 Si el archivo no tiene el formato adecuado
266 Si el archivo no tiene el formato adecuado
267 """
267 """
268
268
269 basename = os.path.basename(filename)
269 basename = os.path.basename(filename)
270
270
271 if not isRadarFile(basename):
271 if not isRadarFile(basename):
272 print "The filename %s has not the rigth format" %filename
272 print "The filename %s has not the rigth format" %filename
273 return 0
273 return 0
274
274
275 if startDate and endDate:
275 if startDate and endDate:
276 thisDate = getDateFromRadarFile(basename)
276 thisDate = getDateFromRadarFile(basename)
277
277
278 if thisDate < startDate:
278 if thisDate < startDate:
279 return 0
279 return 0
280
280
281 if thisDate > endDate:
281 if thisDate > endDate:
282 return 0
282 return 0
283
283
284 return 1
284 return 1
285
285
286 def getFileFromSet(path, ext, set):
286 def getFileFromSet(path, ext, set):
287 validFilelist = []
287 validFilelist = []
288 fileList = os.listdir(path)
288 fileList = os.listdir(path)
289
289
290 # 0 1234 567 89A BCDE
290 # 0 1234 567 89A BCDE
291 # H YYYY DDD SSS .ext
291 # H YYYY DDD SSS .ext
292
292
293 for thisFile in fileList:
293 for thisFile in fileList:
294 try:
294 try:
295 year = int(thisFile[1:5])
295 year = int(thisFile[1:5])
296 doy = int(thisFile[5:8])
296 doy = int(thisFile[5:8])
297 except:
297 except:
298 continue
298 continue
299
299
300 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
300 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
301 continue
301 continue
302
302
303 validFilelist.append(thisFile)
303 validFilelist.append(thisFile)
304
304
305 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
305 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
306
306
307 if len(myfile)!= 0:
307 if len(myfile)!= 0:
308 return myfile[0]
308 return myfile[0]
309 else:
309 else:
310 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
310 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
311 print 'the filename %s does not exist'%filename
311 print 'the filename %s does not exist'%filename
312 print '...going to the last file: '
312 print '...going to the last file: '
313
313
314 if validFilelist:
314 if validFilelist:
315 validFilelist = sorted( validFilelist, key=str.lower )
315 validFilelist = sorted( validFilelist, key=str.lower )
316 return validFilelist[-1]
316 return validFilelist[-1]
317
317
318 return None
318 return None
319
319
320 def getlastFileFromPath(path, ext):
320 def getlastFileFromPath(path, ext):
321 """
321 """
322 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
322 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
323 al final de la depuracion devuelve el ultimo file de la lista que quedo.
323 al final de la depuracion devuelve el ultimo file de la lista que quedo.
324
324
325 Input:
325 Input:
326 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
326 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
327 ext : extension de los files contenidos en una carpeta
327 ext : extension de los files contenidos en una carpeta
328
328
329 Return:
329 Return:
330 El ultimo file de una determinada carpeta, no se considera el path.
330 El ultimo file de una determinada carpeta, no se considera el path.
331 """
331 """
332 validFilelist = []
332 validFilelist = []
333 fileList = os.listdir(path)
333 fileList = os.listdir(path)
334
334
335 # 0 1234 567 89A BCDE
335 # 0 1234 567 89A BCDE
336 # H YYYY DDD SSS .ext
336 # H YYYY DDD SSS .ext
337
337
338 for thisFile in fileList:
338 for thisFile in fileList:
339
339
340 year = thisFile[1:5]
340 year = thisFile[1:5]
341 if not isNumber(year):
341 if not isNumber(year):
342 continue
342 continue
343
343
344 doy = thisFile[5:8]
344 doy = thisFile[5:8]
345 if not isNumber(doy):
345 if not isNumber(doy):
346 continue
346 continue
347
347
348 year = int(year)
348 year = int(year)
349 doy = int(doy)
349 doy = int(doy)
350
350
351 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
351 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
352 continue
352 continue
353
353
354 validFilelist.append(thisFile)
354 validFilelist.append(thisFile)
355
355
356 if validFilelist:
356 if validFilelist:
357 validFilelist = sorted( validFilelist, key=str.lower )
357 validFilelist = sorted( validFilelist, key=str.lower )
358 return validFilelist[-1]
358 return validFilelist[-1]
359
359
360 return None
360 return None
361
361
362 def checkForRealPath(path, foldercounter, year, doy, set, ext):
362 def checkForRealPath(path, foldercounter, year, doy, set, ext):
363 """
363 """
364 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
364 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
365 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
365 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
366 el path exacto de un determinado file.
366 el path exacto de un determinado file.
367
367
368 Example :
368 Example :
369 nombre correcto del file es .../.../D2009307/P2009307367.ext
369 nombre correcto del file es .../.../D2009307/P2009307367.ext
370
370
371 Entonces la funcion prueba con las siguientes combinaciones
371 Entonces la funcion prueba con las siguientes combinaciones
372 .../.../y2009307367.ext
372 .../.../y2009307367.ext
373 .../.../Y2009307367.ext
373 .../.../Y2009307367.ext
374 .../.../x2009307/y2009307367.ext
374 .../.../x2009307/y2009307367.ext
375 .../.../x2009307/Y2009307367.ext
375 .../.../x2009307/Y2009307367.ext
376 .../.../X2009307/y2009307367.ext
376 .../.../X2009307/y2009307367.ext
377 .../.../X2009307/Y2009307367.ext
377 .../.../X2009307/Y2009307367.ext
378 siendo para este caso, la ultima combinacion de letras, identica al file buscado
378 siendo para este caso, la ultima combinacion de letras, identica al file buscado
379
379
380 Return:
380 Return:
381 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
381 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
382 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
382 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
383 para el filename
383 para el filename
384 """
384 """
385 fullfilename = None
385 fullfilename = None
386 find_flag = False
386 find_flag = False
387 filename = None
387 filename = None
388
388
389 prefixDirList = [None,'d','D']
389 prefixDirList = [None,'d','D']
390 if ext.lower() == ".r": #voltage
390 if ext.lower() == ".r": #voltage
391 prefixFileList = ['d','D']
391 prefixFileList = ['d','D']
392 elif ext.lower() == ".pdata": #spectra
392 elif ext.lower() == ".pdata": #spectra
393 prefixFileList = ['p','P']
393 prefixFileList = ['p','P']
394 else:
394 else:
395 return None, filename
395 return None, filename
396
396
397 #barrido por las combinaciones posibles
397 #barrido por las combinaciones posibles
398 for prefixDir in prefixDirList:
398 for prefixDir in prefixDirList:
399 thispath = path
399 thispath = path
400 if prefixDir != None:
400 if prefixDir != None:
401 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
401 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
402 if foldercounter == 0:
402 if foldercounter == 0:
403 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
403 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
404 else:
404 else:
405 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
405 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
406 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
406 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
407 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
407 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
408 fullfilename = os.path.join( thispath, filename ) #formo el path completo
408 fullfilename = os.path.join( thispath, filename ) #formo el path completo
409
409
410 if os.path.exists( fullfilename ): #verifico que exista
410 if os.path.exists( fullfilename ): #verifico que exista
411 find_flag = True
411 find_flag = True
412 break
412 break
413 if find_flag:
413 if find_flag:
414 break
414 break
415
415
416 if not(find_flag):
416 if not(find_flag):
417 return None, filename
417 return None, filename
418
418
419 return fullfilename, filename
419 return fullfilename, filename
420
420
421 def isRadarFolder(folder):
421 def isRadarFolder(folder):
422 try:
422 try:
423 year = int(folder[1:5])
423 year = int(folder[1:5])
424 doy = int(folder[5:8])
424 doy = int(folder[5:8])
425 except:
425 except:
426 return 0
426 return 0
427
427
428 return 1
428 return 1
429
429
430 def isRadarFile(file):
430 def isRadarFile(file):
431 try:
431 try:
432 year = int(file[1:5])
432 year = int(file[1:5])
433 doy = int(file[5:8])
433 doy = int(file[5:8])
434 set = int(file[8:11])
434 set = int(file[8:11])
435 except:
435 except:
436 return 0
436 return 0
437
437
438 return 1
438 return 1
439
439
440 def getDateFromRadarFile(file):
440 def getDateFromRadarFile(file):
441 try:
441 try:
442 year = int(file[1:5])
442 year = int(file[1:5])
443 doy = int(file[5:8])
443 doy = int(file[5:8])
444 set = int(file[8:11])
444 set = int(file[8:11])
445 except:
445 except:
446 return None
446 return None
447
447
448 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
448 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
449 return thisDate
449 return thisDate
450
450
451 def getDateFromRadarFolder(folder):
451 def getDateFromRadarFolder(folder):
452 try:
452 try:
453 year = int(folder[1:5])
453 year = int(folder[1:5])
454 doy = int(folder[5:8])
454 doy = int(folder[5:8])
455 except:
455 except:
456 return None
456 return None
457
457
458 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
458 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
459 return thisDate
459 return thisDate
460
460
461 class JRODataIO:
461 class JRODataIO:
462
462
463 c = 3E8
463 c = 3E8
464
464
465 isConfig = False
465 isConfig = False
466
466
467 basicHeaderObj = None
467 basicHeaderObj = None
468
468
469 systemHeaderObj = None
469 systemHeaderObj = None
470
470
471 radarControllerHeaderObj = None
471 radarControllerHeaderObj = None
472
472
473 processingHeaderObj = None
473 processingHeaderObj = None
474
474
475 dtype = None
475 dtype = None
476
476
477 pathList = []
477 pathList = []
478
478
479 filenameList = []
479 filenameList = []
480
480
481 filename = None
481 filename = None
482
482
483 ext = None
483 ext = None
484
484
485 flagIsNewFile = 1
485 flagIsNewFile = 1
486
486
487 flagDiscontinuousBlock = 0
487 flagDiscontinuousBlock = 0
488
488
489 flagIsNewBlock = 0
489 flagIsNewBlock = 0
490
490
491 fp = None
491 fp = None
492
492
493 firstHeaderSize = 0
493 firstHeaderSize = 0
494
494
495 basicHeaderSize = 24
495 basicHeaderSize = 24
496
496
497 versionFile = 1103
497 versionFile = 1103
498
498
499 fileSize = None
499 fileSize = None
500
500
501 # ippSeconds = None
501 # ippSeconds = None
502
502
503 fileSizeByHeader = None
503 fileSizeByHeader = None
504
504
505 fileIndex = None
505 fileIndex = None
506
506
507 profileIndex = None
507 profileIndex = None
508
508
509 blockIndex = None
509 blockIndex = None
510
510
511 nTotalBlocks = None
511 nTotalBlocks = None
512
512
513 maxTimeStep = 30
513 maxTimeStep = 30
514
514
515 lastUTTime = None
515 lastUTTime = None
516
516
517 datablock = None
517 datablock = None
518
518
519 dataOut = None
519 dataOut = None
520
520
521 blocksize = None
521 blocksize = None
522
522
523 getByBlock = False
523 getByBlock = False
524
524
525 def __init__(self):
525 def __init__(self):
526
526
527 raise NotImplementedError
527 raise NotImplementedError
528
528
529 def run(self):
529 def run(self):
530
530
531 raise NotImplementedError
531 raise NotImplementedError
532
532
533 def getDtypeWidth(self):
533 def getDtypeWidth(self):
534
534
535 dtype_index = get_dtype_index(self.dtype)
535 dtype_index = get_dtype_index(self.dtype)
536 dtype_width = get_dtype_width(dtype_index)
536 dtype_width = get_dtype_width(dtype_index)
537
537
538 return dtype_width
538 return dtype_width
539
539
540 def getAllowedArgs(self):
540 def getAllowedArgs(self):
541 return inspect.getargspec(self.run).args
541 return inspect.getargspec(self.run).args
542
542
543 class JRODataReader(JRODataIO):
543 class JRODataReader(JRODataIO):
544
544
545
545
546 online = 0
546 online = 0
547
547
548 realtime = 0
548 realtime = 0
549
549
550 nReadBlocks = 0
550 nReadBlocks = 0
551
551
552 delay = 10 #number of seconds waiting a new file
552 delay = 10 #number of seconds waiting a new file
553
553
554 nTries = 3 #quantity tries
554 nTries = 3 #quantity tries
555
555
556 nFiles = 3 #number of files for searching
556 nFiles = 3 #number of files for searching
557
557
558 path = None
558 path = None
559
559
560 foldercounter = 0
560 foldercounter = 0
561
561
562 flagNoMoreFiles = 0
562 flagNoMoreFiles = 0
563
563
564 datetimeList = []
564 datetimeList = []
565
565
566 __isFirstTimeOnline = 1
566 __isFirstTimeOnline = 1
567
567
568 __printInfo = True
568 __printInfo = True
569
569
570 profileIndex = None
570 profileIndex = None
571
571
572 nTxs = 1
572 nTxs = 1
573
573
574 txIndex = None
574 txIndex = None
575
575
576 #Added--------------------
576 #Added--------------------
577
577
578 selBlocksize = None
578 selBlocksize = None
579
579
580 selBlocktime = None
580 selBlocktime = None
581
581
582
582
583 def __init__(self):
583 def __init__(self):
584
584
585 """
585 """
586 This class is used to find data files
586 This class is used to find data files
587
587
588 Example:
588 Example:
589 reader = JRODataReader()
589 reader = JRODataReader()
590 fileList = reader.findDataFiles()
590 fileList = reader.findDataFiles()
591
591
592 """
592 """
593 pass
593 pass
594
594
595
595
596 def createObjByDefault(self):
596 def createObjByDefault(self):
597 """
597 """
598
598
599 """
599 """
600 raise NotImplementedError
600 raise NotImplementedError
601
601
602 def getBlockDimension(self):
602 def getBlockDimension(self):
603
603
604 raise NotImplementedError
604 raise NotImplementedError
605
605
606 def __searchFilesOffLine(self,
606 def __searchFilesOffLine(self,
607 path,
607 path,
608 startDate=None,
608 startDate=None,
609 endDate=None,
609 endDate=None,
610 startTime=datetime.time(0,0,0),
610 startTime=datetime.time(0,0,0),
611 endTime=datetime.time(23,59,59),
611 endTime=datetime.time(23,59,59),
612 set=None,
612 set=None,
613 expLabel='',
613 expLabel='',
614 ext='.r',
614 ext='.r',
615 queue=None,
615 queue=None,
616 cursor=None,
616 cursor=None,
617 skip=None,
617 skip=None,
618 walk=True):
618 walk=True):
619
619
620 self.filenameList = []
620 self.filenameList = []
621 self.datetimeList = []
621 self.datetimeList = []
622
622
623 pathList = []
623 pathList = []
624
624
625 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
625 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
626
626
627 if dateList == []:
627 if dateList == []:
628 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
628 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
629 return None, None
629 return None, None
630
630
631 if len(dateList) > 1:
631 if len(dateList) > 1:
632 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
632 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
633 else:
633 else:
634 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
634 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
635
635
636 filenameList = []
636 filenameList = []
637 datetimeList = []
637 datetimeList = []
638
638
639 for thisPath in pathList:
639 for thisPath in pathList:
640 # thisPath = pathList[pathDict[file]]
640 # thisPath = pathList[pathDict[file]]
641
641
642 fileList = glob.glob1(thisPath, "*%s" %ext)
642 fileList = glob.glob1(thisPath, "*%s" %ext)
643 fileList.sort()
643 fileList.sort()
644
644
645 skippedFileList = []
645 skippedFileList = []
646
646
647 if cursor is not None and skip is not None:
647 if cursor is not None and skip is not None:
648 # if cursor*skip > len(fileList):
648 # if cursor*skip > len(fileList):
649 if skip == 0:
649 if skip == 0:
650 if queue is not None:
650 if queue is not None:
651 queue.put(len(fileList))
651 queue.put(len(fileList))
652 skippedFileList = []
652 skippedFileList = []
653 else:
653 else:
654 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
654 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
655
655
656 else:
656 else:
657 skippedFileList = fileList
657 skippedFileList = fileList
658
658
659 for file in skippedFileList:
659 for file in skippedFileList:
660
660
661 filename = os.path.join(thisPath,file)
661 filename = os.path.join(thisPath,file)
662
662
663 if not isFileInDateRange(filename, startDate, endDate):
663 if not isFileInDateRange(filename, startDate, endDate):
664 continue
664 continue
665
665
666 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
666 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
667
667
668 if not(thisDatetime):
668 if not(thisDatetime):
669 continue
669 continue
670
670
671 filenameList.append(filename)
671 filenameList.append(filename)
672 datetimeList.append(thisDatetime)
672 datetimeList.append(thisDatetime)
673
673
674 if not(filenameList):
674 if not(filenameList):
675 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
675 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
676 return None, None
676 return None, None
677
677
678 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
678 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
679 print
679 print
680
680
681 for i in range(len(filenameList)):
681 for i in range(len(filenameList)):
682 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
682 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
683
683
684 self.filenameList = filenameList
684 self.filenameList = filenameList
685 self.datetimeList = datetimeList
685 self.datetimeList = datetimeList
686
686
687 return pathList, filenameList
687 return pathList, filenameList
688
688
689 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
689 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
690
690
691 """
691 """
692 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
692 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
693 devuelve el archivo encontrado ademas de otros datos.
693 devuelve el archivo encontrado ademas de otros datos.
694
694
695 Input:
695 Input:
696 path : carpeta donde estan contenidos los files que contiene data
696 path : carpeta donde estan contenidos los files que contiene data
697
697
698 expLabel : Nombre del subexperimento (subfolder)
698 expLabel : Nombre del subexperimento (subfolder)
699
699
700 ext : extension de los files
700 ext : extension de los files
701
701
702 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
702 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
703
703
704 Return:
704 Return:
705 directory : eL directorio donde esta el file encontrado
705 directory : eL directorio donde esta el file encontrado
706 filename : el ultimo file de una determinada carpeta
706 filename : el ultimo file de una determinada carpeta
707 year : el anho
707 year : el anho
708 doy : el numero de dia del anho
708 doy : el numero de dia del anho
709 set : el set del archivo
709 set : el set del archivo
710
710
711
711
712 """
712 """
713 if not os.path.isdir(path):
713 if not os.path.isdir(path):
714 return None, None, None, None, None, None
714 return None, None, None, None, None, None
715
715
716 dirList = []
716 dirList = []
717
717
718 if not walk:
718 if not walk:
719 fullpath = path
719 fullpath = path
720 foldercounter = 0
720 foldercounter = 0
721 else:
721 else:
722 #Filtra solo los directorios
722 #Filtra solo los directorios
723 for thisPath in os.listdir(path):
723 for thisPath in os.listdir(path):
724 if not os.path.isdir(os.path.join(path,thisPath)):
724 if not os.path.isdir(os.path.join(path,thisPath)):
725 continue
725 continue
726 if not isRadarFolder(thisPath):
726 if not isRadarFolder(thisPath):
727 continue
727 continue
728
728
729 dirList.append(thisPath)
729 dirList.append(thisPath)
730
730
731 if not(dirList):
731 if not(dirList):
732 return None, None, None, None, None, None
732 return None, None, None, None, None, None
733
733
734 dirList = sorted( dirList, key=str.lower )
734 dirList = sorted( dirList, key=str.lower )
735
735
736 doypath = dirList[-1]
736 doypath = dirList[-1]
737 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
737 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
738 fullpath = os.path.join(path, doypath, expLabel)
738 fullpath = os.path.join(path, doypath, expLabel)
739
739
740
740
741 print "[Reading] %s folder was found: " %(fullpath )
741 print "[Reading] %s folder was found: " %(fullpath )
742
742
743 if set == None:
743 if set == None:
744 filename = getlastFileFromPath(fullpath, ext)
744 filename = getlastFileFromPath(fullpath, ext)
745 else:
745 else:
746 filename = getFileFromSet(fullpath, ext, set)
746 filename = getFileFromSet(fullpath, ext, set)
747
747
748 if not(filename):
748 if not(filename):
749 return None, None, None, None, None, None
749 return None, None, None, None, None, None
750
750
751 print "[Reading] %s file was found" %(filename)
751 print "[Reading] %s file was found" %(filename)
752
752
753 if not(self.__verifyFile(os.path.join(fullpath, filename))):
753 if not(self.__verifyFile(os.path.join(fullpath, filename))):
754 return None, None, None, None, None, None
754 return None, None, None, None, None, None
755
755
756 year = int( filename[1:5] )
756 year = int( filename[1:5] )
757 doy = int( filename[5:8] )
757 doy = int( filename[5:8] )
758 set = int( filename[8:11] )
758 set = int( filename[8:11] )
759
759
760 return fullpath, foldercounter, filename, year, doy, set
760 return fullpath, foldercounter, filename, year, doy, set
761
761
762 def __setNextFileOffline(self):
762 def __setNextFileOffline(self):
763
763
764 idFile = self.fileIndex
764 idFile = self.fileIndex
765
765
766 while (True):
766 while (True):
767 idFile += 1
767 idFile += 1
768 if not(idFile < len(self.filenameList)):
768 if not(idFile < len(self.filenameList)):
769 self.flagNoMoreFiles = 1
769 self.flagNoMoreFiles = 1
770 # print "[Reading] No more Files"
770 # print "[Reading] No more Files"
771 return 0
771 return 0
772
772
773 filename = self.filenameList[idFile]
773 filename = self.filenameList[idFile]
774
774
775 if not(self.__verifyFile(filename)):
775 if not(self.__verifyFile(filename)):
776 continue
776 continue
777
777
778 fileSize = os.path.getsize(filename)
778 fileSize = os.path.getsize(filename)
779 fp = open(filename,'rb')
779 fp = open(filename,'rb')
780 break
780 break
781
781
782 self.flagIsNewFile = 1
782 self.flagIsNewFile = 1
783 self.fileIndex = idFile
783 self.fileIndex = idFile
784 self.filename = filename
784 self.filename = filename
785 self.fileSize = fileSize
785 self.fileSize = fileSize
786 self.fp = fp
786 self.fp = fp
787
787
788 # print "[Reading] Setting the file: %s"%self.filename
788 # print "[Reading] Setting the file: %s"%self.filename
789
789
790 return 1
790 return 1
791
791
792 def __setNextFileOnline(self):
792 def __setNextFileOnline(self):
793 """
793 """
794 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
794 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
795 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
795 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
796 siguientes.
796 siguientes.
797
797
798 Affected:
798 Affected:
799 self.flagIsNewFile
799 self.flagIsNewFile
800 self.filename
800 self.filename
801 self.fileSize
801 self.fileSize
802 self.fp
802 self.fp
803 self.set
803 self.set
804 self.flagNoMoreFiles
804 self.flagNoMoreFiles
805
805
806 Return:
806 Return:
807 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
807 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
808 1 : si el file fue abierto con exito y esta listo a ser leido
808 1 : si el file fue abierto con exito y esta listo a ser leido
809
809
810 Excepciones:
810 Excepciones:
811 Si un determinado file no puede ser abierto
811 Si un determinado file no puede ser abierto
812 """
812 """
813 nFiles = 0
813 nFiles = 0
814 fileOk_flag = False
814 fileOk_flag = False
815 firstTime_flag = True
815 firstTime_flag = True
816
816
817 self.set += 1
817 self.set += 1
818
818
819 if self.set > 999:
819 if self.set > 999:
820 self.set = 0
820 self.set = 0
821 self.foldercounter += 1
821 self.foldercounter += 1
822
822
823 #busca el 1er file disponible
823 #busca el 1er file disponible
824 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
824 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
825 if fullfilename:
825 if fullfilename:
826 if self.__verifyFile(fullfilename, False):
826 if self.__verifyFile(fullfilename, False):
827 fileOk_flag = True
827 fileOk_flag = True
828
828
829 #si no encuentra un file entonces espera y vuelve a buscar
829 #si no encuentra un file entonces espera y vuelve a buscar
830 if not(fileOk_flag):
830 if not(fileOk_flag):
831 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
831 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
832
832
833 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
833 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
834 tries = self.nTries
834 tries = self.nTries
835 else:
835 else:
836 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
836 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
837
837
838 for nTries in range( tries ):
838 for nTries in range( tries ):
839 if firstTime_flag:
839 if firstTime_flag:
840 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
840 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
841 sleep( self.delay )
841 sleep( self.delay )
842 else:
842 else:
843 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
843 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
844
844
845 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
845 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
846 if fullfilename:
846 if fullfilename:
847 if self.__verifyFile(fullfilename):
847 if self.__verifyFile(fullfilename):
848 fileOk_flag = True
848 fileOk_flag = True
849 break
849 break
850
850
851 if fileOk_flag:
851 if fileOk_flag:
852 break
852 break
853
853
854 firstTime_flag = False
854 firstTime_flag = False
855
855
856 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
856 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
857 self.set += 1
857 self.set += 1
858
858
859 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
859 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
860 self.set = 0
860 self.set = 0
861 self.doy += 1
861 self.doy += 1
862 self.foldercounter = 0
862 self.foldercounter = 0
863
863
864 if fileOk_flag:
864 if fileOk_flag:
865 self.fileSize = os.path.getsize( fullfilename )
865 self.fileSize = os.path.getsize( fullfilename )
866 self.filename = fullfilename
866 self.filename = fullfilename
867 self.flagIsNewFile = 1
867 self.flagIsNewFile = 1
868 if self.fp != None: self.fp.close()
868 if self.fp != None: self.fp.close()
869 self.fp = open(fullfilename, 'rb')
869 self.fp = open(fullfilename, 'rb')
870 self.flagNoMoreFiles = 0
870 self.flagNoMoreFiles = 0
871 # print '[Reading] Setting the file: %s' % fullfilename
871 # print '[Reading] Setting the file: %s' % fullfilename
872 else:
872 else:
873 self.fileSize = 0
873 self.fileSize = 0
874 self.filename = None
874 self.filename = None
875 self.flagIsNewFile = 0
875 self.flagIsNewFile = 0
876 self.fp = None
876 self.fp = None
877 self.flagNoMoreFiles = 1
877 self.flagNoMoreFiles = 1
878 # print '[Reading] No more files to read'
878 # print '[Reading] No more files to read'
879
879
880 return fileOk_flag
880 return fileOk_flag
881
881
882 def setNextFile(self):
882 def setNextFile(self):
883 if self.fp != None:
883 if self.fp != None:
884 self.fp.close()
884 self.fp.close()
885
885
886 if self.online:
886 if self.online:
887 newFile = self.__setNextFileOnline()
887 newFile = self.__setNextFileOnline()
888 else:
888 else:
889 newFile = self.__setNextFileOffline()
889 newFile = self.__setNextFileOffline()
890
890
891 if not(newFile):
891 if not(newFile):
892 print '[Reading] No more files to read'
892 print '[Reading] No more files to read'
893 return 0
893 return 0
894
894
895 if self.verbose:
895 if self.verbose:
896 print '[Reading] Setting the file: %s' % self.filename
896 print '[Reading] Setting the file: %s' % self.filename
897
897
898 self.__readFirstHeader()
898 self.__readFirstHeader()
899 self.nReadBlocks = 0
899 self.nReadBlocks = 0
900 return 1
900 return 1
901
901
902 def __waitNewBlock(self):
902 def __waitNewBlock(self):
903 """
903 """
904 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
904 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
905
905
906 Si el modo de lectura es OffLine siempre retorn 0
906 Si el modo de lectura es OffLine siempre retorn 0
907 """
907 """
908 if not self.online:
908 if not self.online:
909 return 0
909 return 0
910
910
911 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
911 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
912 return 0
912 return 0
913
913
914 currentPointer = self.fp.tell()
914 currentPointer = self.fp.tell()
915
915
916 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
916 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
917
917
918 for nTries in range( self.nTries ):
918 for nTries in range( self.nTries ):
919
919
920 self.fp.close()
920 self.fp.close()
921 self.fp = open( self.filename, 'rb' )
921 self.fp = open( self.filename, 'rb' )
922 self.fp.seek( currentPointer )
922 self.fp.seek( currentPointer )
923
923
924 self.fileSize = os.path.getsize( self.filename )
924 self.fileSize = os.path.getsize( self.filename )
925 currentSize = self.fileSize - currentPointer
925 currentSize = self.fileSize - currentPointer
926
926
927 if ( currentSize >= neededSize ):
927 if ( currentSize >= neededSize ):
928 self.basicHeaderObj.read(self.fp)
928 self.basicHeaderObj.read(self.fp)
929 return 1
929 return 1
930
930
931 if self.fileSize == self.fileSizeByHeader:
931 if self.fileSize == self.fileSizeByHeader:
932 # self.flagEoF = True
932 # self.flagEoF = True
933 return 0
933 return 0
934
934
935 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
935 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
936 sleep( self.delay )
936 sleep( self.delay )
937
937
938
938
939 return 0
939 return 0
940
940
941 def waitDataBlock(self,pointer_location):
941 def waitDataBlock(self,pointer_location):
942
942
943 currentPointer = pointer_location
943 currentPointer = pointer_location
944
944
945 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
945 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
946
946
947 for nTries in range( self.nTries ):
947 for nTries in range( self.nTries ):
948 self.fp.close()
948 self.fp.close()
949 self.fp = open( self.filename, 'rb' )
949 self.fp = open( self.filename, 'rb' )
950 self.fp.seek( currentPointer )
950 self.fp.seek( currentPointer )
951
951
952 self.fileSize = os.path.getsize( self.filename )
952 self.fileSize = os.path.getsize( self.filename )
953 currentSize = self.fileSize - currentPointer
953 currentSize = self.fileSize - currentPointer
954
954
955 if ( currentSize >= neededSize ):
955 if ( currentSize >= neededSize ):
956 return 1
956 return 1
957
957
958 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
958 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
959 sleep( self.delay )
959 sleep( self.delay )
960
960
961 return 0
961 return 0
962
962
963 def __jumpToLastBlock(self):
963 def __jumpToLastBlock(self):
964
964
965 if not(self.__isFirstTimeOnline):
965 if not(self.__isFirstTimeOnline):
966 return
966 return
967
967
968 csize = self.fileSize - self.fp.tell()
968 csize = self.fileSize - self.fp.tell()
969 blocksize = self.processingHeaderObj.blockSize
969 blocksize = self.processingHeaderObj.blockSize
970
970
971 #salta el primer bloque de datos
971 #salta el primer bloque de datos
972 if csize > self.processingHeaderObj.blockSize:
972 if csize > self.processingHeaderObj.blockSize:
973 self.fp.seek(self.fp.tell() + blocksize)
973 self.fp.seek(self.fp.tell() + blocksize)
974 else:
974 else:
975 return
975 return
976
976
977 csize = self.fileSize - self.fp.tell()
977 csize = self.fileSize - self.fp.tell()
978 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
978 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
979 while True:
979 while True:
980
980
981 if self.fp.tell()<self.fileSize:
981 if self.fp.tell()<self.fileSize:
982 self.fp.seek(self.fp.tell() + neededsize)
982 self.fp.seek(self.fp.tell() + neededsize)
983 else:
983 else:
984 self.fp.seek(self.fp.tell() - neededsize)
984 self.fp.seek(self.fp.tell() - neededsize)
985 break
985 break
986
986
987 # csize = self.fileSize - self.fp.tell()
987 # csize = self.fileSize - self.fp.tell()
988 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
988 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
989 # factor = int(csize/neededsize)
989 # factor = int(csize/neededsize)
990 # if factor > 0:
990 # if factor > 0:
991 # self.fp.seek(self.fp.tell() + factor*neededsize)
991 # self.fp.seek(self.fp.tell() + factor*neededsize)
992
992
993 self.flagIsNewFile = 0
993 self.flagIsNewFile = 0
994 self.__isFirstTimeOnline = 0
994 self.__isFirstTimeOnline = 0
995
995
996 def __setNewBlock(self):
996 def __setNewBlock(self):
997 #if self.server is None:
997 #if self.server is None:
998 if self.fp == None:
998 if self.fp == None:
999 return 0
999 return 0
1000
1000
1001 # if self.online:
1001 # if self.online:
1002 # self.__jumpToLastBlock()
1002 # self.__jumpToLastBlock()
1003 print 'xxxx'
1004
1003
1005 if self.flagIsNewFile:
1004 if self.flagIsNewFile:
1006 self.lastUTTime = self.basicHeaderObj.utc
1005 self.lastUTTime = self.basicHeaderObj.utc
1007 return 1
1006 return 1
1008
1007
1009 if self.realtime:
1008 if self.realtime:
1010 self.flagDiscontinuousBlock = 1
1009 self.flagDiscontinuousBlock = 1
1011 if not(self.setNextFile()):
1010 if not(self.setNextFile()):
1012 return 0
1011 return 0
1013 else:
1012 else:
1014 return 1
1013 return 1
1015 print 'xxxx'
1016 #if self.server is None:
1014 #if self.server is None:
1017 currentSize = self.fileSize - self.fp.tell()
1015 currentSize = self.fileSize - self.fp.tell()
1018 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1016 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1019 if (currentSize >= neededSize):
1017 if (currentSize >= neededSize):
1020 self.basicHeaderObj.read(self.fp)
1018 self.basicHeaderObj.read(self.fp)
1021 self.lastUTTime = self.basicHeaderObj.utc
1019 self.lastUTTime = self.basicHeaderObj.utc
1022 return 1
1020 return 1
1023 # else:
1021 # else:
1024 # self.basicHeaderObj.read(self.zHeader)
1022 # self.basicHeaderObj.read(self.zHeader)
1025 # self.lastUTTime = self.basicHeaderObj.utc
1023 # self.lastUTTime = self.basicHeaderObj.utc
1026 # return 1
1024 # return 1
1027 if self.__waitNewBlock():
1025 if self.__waitNewBlock():
1028 self.lastUTTime = self.basicHeaderObj.utc
1026 self.lastUTTime = self.basicHeaderObj.utc
1029 return 1
1027 return 1
1030 #if self.server is None:
1028 #if self.server is None:
1031 if not(self.setNextFile()):
1029 if not(self.setNextFile()):
1032 return 0
1030 return 0
1033
1031
1034 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1032 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1035 self.lastUTTime = self.basicHeaderObj.utc
1033 self.lastUTTime = self.basicHeaderObj.utc
1036
1034
1037 self.flagDiscontinuousBlock = 0
1035 self.flagDiscontinuousBlock = 0
1038
1036
1039 if deltaTime > self.maxTimeStep:
1037 if deltaTime > self.maxTimeStep:
1040 self.flagDiscontinuousBlock = 1
1038 self.flagDiscontinuousBlock = 1
1041
1039
1042 return 1
1040 return 1
1043
1041
1044 def readNextBlock(self):
1042 def readNextBlock(self):
1045
1043
1046 #Skip block out of startTime and endTime
1044 #Skip block out of startTime and endTime
1047 while True:
1045 while True:
1048 print 'cxxxx'
1049 if not(self.__setNewBlock()):
1046 if not(self.__setNewBlock()):
1050 print 'returning'
1047 print 'returning'
1051 return 0
1048 return 0
1052 print 'dxxx'
1049
1053 if not(self.readBlock()):
1050 if not(self.readBlock()):
1054 return 0
1051 return 0
1055
1052
1056 self.getBasicHeader()
1053 self.getBasicHeader()
1057
1054
1058 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1055 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1059
1056
1060 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1057 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1061 self.processingHeaderObj.dataBlocksPerFile,
1058 self.processingHeaderObj.dataBlocksPerFile,
1062 self.dataOut.datatime.ctime())
1059 self.dataOut.datatime.ctime())
1063 continue
1060 continue
1064
1061
1065 break
1062 break
1066
1063
1067 if self.verbose:
1064 if self.verbose:
1068 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1065 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1069 self.processingHeaderObj.dataBlocksPerFile,
1066 self.processingHeaderObj.dataBlocksPerFile,
1070 self.dataOut.datatime.ctime())
1067 self.dataOut.datatime.ctime())
1071 return 1
1068 return 1
1072
1069
1073 def __readFirstHeader(self):
1070 def __readFirstHeader(self):
1074
1071
1075 self.basicHeaderObj.read(self.fp)
1072 self.basicHeaderObj.read(self.fp)
1076 self.systemHeaderObj.read(self.fp)
1073 self.systemHeaderObj.read(self.fp)
1077 self.radarControllerHeaderObj.read(self.fp)
1074 self.radarControllerHeaderObj.read(self.fp)
1078 self.processingHeaderObj.read(self.fp)
1075 self.processingHeaderObj.read(self.fp)
1079
1076
1080 self.firstHeaderSize = self.basicHeaderObj.size
1077 self.firstHeaderSize = self.basicHeaderObj.size
1081
1078
1082 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1079 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1083 if datatype == 0:
1080 if datatype == 0:
1084 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1081 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1085 elif datatype == 1:
1082 elif datatype == 1:
1086 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1083 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1087 elif datatype == 2:
1084 elif datatype == 2:
1088 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1085 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1089 elif datatype == 3:
1086 elif datatype == 3:
1090 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1087 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1091 elif datatype == 4:
1088 elif datatype == 4:
1092 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1089 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1093 elif datatype == 5:
1090 elif datatype == 5:
1094 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1091 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1095 else:
1092 else:
1096 raise ValueError, 'Data type was not defined'
1093 raise ValueError, 'Data type was not defined'
1097
1094
1098 self.dtype = datatype_str
1095 self.dtype = datatype_str
1099 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1096 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1100 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1097 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1101 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1098 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1102 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1099 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1103 self.getBlockDimension()
1100 self.getBlockDimension()
1104
1101
1105 def __verifyFile(self, filename, msgFlag=True):
1102 def __verifyFile(self, filename, msgFlag=True):
1106
1103
1107 msg = None
1104 msg = None
1108
1105
1109 try:
1106 try:
1110 fp = open(filename, 'rb')
1107 fp = open(filename, 'rb')
1111 except IOError:
1108 except IOError:
1112
1109
1113 if msgFlag:
1110 if msgFlag:
1114 print "[Reading] File %s can't be opened" % (filename)
1111 print "[Reading] File %s can't be opened" % (filename)
1115
1112
1116 return False
1113 return False
1117
1114
1118 currentPosition = fp.tell()
1115 currentPosition = fp.tell()
1119 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1116 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1120
1117
1121 if neededSize == 0:
1118 if neededSize == 0:
1122 basicHeaderObj = BasicHeader(LOCALTIME)
1119 basicHeaderObj = BasicHeader(LOCALTIME)
1123 systemHeaderObj = SystemHeader()
1120 systemHeaderObj = SystemHeader()
1124 radarControllerHeaderObj = RadarControllerHeader()
1121 radarControllerHeaderObj = RadarControllerHeader()
1125 processingHeaderObj = ProcessingHeader()
1122 processingHeaderObj = ProcessingHeader()
1126
1123
1127 if not( basicHeaderObj.read(fp) ):
1124 if not( basicHeaderObj.read(fp) ):
1128 fp.close()
1125 fp.close()
1129 return False
1126 return False
1130
1127
1131 if not( systemHeaderObj.read(fp) ):
1128 if not( systemHeaderObj.read(fp) ):
1132 fp.close()
1129 fp.close()
1133 return False
1130 return False
1134
1131
1135 if not( radarControllerHeaderObj.read(fp) ):
1132 if not( radarControllerHeaderObj.read(fp) ):
1136 fp.close()
1133 fp.close()
1137 return False
1134 return False
1138
1135
1139 if not( processingHeaderObj.read(fp) ):
1136 if not( processingHeaderObj.read(fp) ):
1140 fp.close()
1137 fp.close()
1141 return False
1138 return False
1142
1139
1143 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1140 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1144 else:
1141 else:
1145 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1142 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1146
1143
1147 fp.close()
1144 fp.close()
1148
1145
1149 fileSize = os.path.getsize(filename)
1146 fileSize = os.path.getsize(filename)
1150 currentSize = fileSize - currentPosition
1147 currentSize = fileSize - currentPosition
1151
1148
1152 if currentSize < neededSize:
1149 if currentSize < neededSize:
1153 if msgFlag and (msg != None):
1150 if msgFlag and (msg != None):
1154 print msg
1151 print msg
1155 return False
1152 return False
1156
1153
1157 return True
1154 return True
1158
1155
1159 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1156 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1160
1157
1161 path_empty = True
1158 path_empty = True
1162
1159
1163 dateList = []
1160 dateList = []
1164 pathList = []
1161 pathList = []
1165
1162
1166 multi_path = path.split(',')
1163 multi_path = path.split(',')
1167
1164
1168 if not walk:
1165 if not walk:
1169
1166
1170 for single_path in multi_path:
1167 for single_path in multi_path:
1171
1168
1172 if not os.path.isdir(single_path):
1169 if not os.path.isdir(single_path):
1173 continue
1170 continue
1174
1171
1175 fileList = glob.glob1(single_path, "*"+ext)
1172 fileList = glob.glob1(single_path, "*"+ext)
1176
1173
1177 if not fileList:
1174 if not fileList:
1178 continue
1175 continue
1179
1176
1180 path_empty = False
1177 path_empty = False
1181
1178
1182 fileList.sort()
1179 fileList.sort()
1183
1180
1184 for thisFile in fileList:
1181 for thisFile in fileList:
1185
1182
1186 if not os.path.isfile(os.path.join(single_path, thisFile)):
1183 if not os.path.isfile(os.path.join(single_path, thisFile)):
1187 continue
1184 continue
1188
1185
1189 if not isRadarFile(thisFile):
1186 if not isRadarFile(thisFile):
1190 continue
1187 continue
1191
1188
1192 if not isFileInDateRange(thisFile, startDate, endDate):
1189 if not isFileInDateRange(thisFile, startDate, endDate):
1193 continue
1190 continue
1194
1191
1195 thisDate = getDateFromRadarFile(thisFile)
1192 thisDate = getDateFromRadarFile(thisFile)
1196
1193
1197 if thisDate in dateList:
1194 if thisDate in dateList:
1198 continue
1195 continue
1199
1196
1200 dateList.append(thisDate)
1197 dateList.append(thisDate)
1201 pathList.append(single_path)
1198 pathList.append(single_path)
1202
1199
1203 else:
1200 else:
1204 for single_path in multi_path:
1201 for single_path in multi_path:
1205
1202
1206 if not os.path.isdir(single_path):
1203 if not os.path.isdir(single_path):
1207 continue
1204 continue
1208
1205
1209 dirList = []
1206 dirList = []
1210
1207
1211 for thisPath in os.listdir(single_path):
1208 for thisPath in os.listdir(single_path):
1212
1209
1213 if not os.path.isdir(os.path.join(single_path,thisPath)):
1210 if not os.path.isdir(os.path.join(single_path,thisPath)):
1214 continue
1211 continue
1215
1212
1216 if not isRadarFolder(thisPath):
1213 if not isRadarFolder(thisPath):
1217 continue
1214 continue
1218
1215
1219 if not isFolderInDateRange(thisPath, startDate, endDate):
1216 if not isFolderInDateRange(thisPath, startDate, endDate):
1220 continue
1217 continue
1221
1218
1222 dirList.append(thisPath)
1219 dirList.append(thisPath)
1223
1220
1224 if not dirList:
1221 if not dirList:
1225 continue
1222 continue
1226
1223
1227 dirList.sort()
1224 dirList.sort()
1228
1225
1229 for thisDir in dirList:
1226 for thisDir in dirList:
1230
1227
1231 datapath = os.path.join(single_path, thisDir, expLabel)
1228 datapath = os.path.join(single_path, thisDir, expLabel)
1232 fileList = glob.glob1(datapath, "*"+ext)
1229 fileList = glob.glob1(datapath, "*"+ext)
1233
1230
1234 if not fileList:
1231 if not fileList:
1235 continue
1232 continue
1236
1233
1237 path_empty = False
1234 path_empty = False
1238
1235
1239 thisDate = getDateFromRadarFolder(thisDir)
1236 thisDate = getDateFromRadarFolder(thisDir)
1240
1237
1241 pathList.append(datapath)
1238 pathList.append(datapath)
1242 dateList.append(thisDate)
1239 dateList.append(thisDate)
1243
1240
1244 dateList.sort()
1241 dateList.sort()
1245
1242
1246 if walk:
1243 if walk:
1247 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1244 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1248 else:
1245 else:
1249 pattern_path = multi_path[0]
1246 pattern_path = multi_path[0]
1250
1247
1251 if path_empty:
1248 if path_empty:
1252 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1249 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1253 else:
1250 else:
1254 if not dateList:
1251 if not dateList:
1255 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1252 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1256
1253
1257 if include_path:
1254 if include_path:
1258 return dateList, pathList
1255 return dateList, pathList
1259
1256
1260 return dateList
1257 return dateList
1261
1258
1262 def setup(self,
1259 def setup(self,
1263 path=None,
1260 path=None,
1264 startDate=None,
1261 startDate=None,
1265 endDate=None,
1262 endDate=None,
1266 startTime=datetime.time(0,0,0),
1263 startTime=datetime.time(0,0,0),
1267 endTime=datetime.time(23,59,59),
1264 endTime=datetime.time(23,59,59),
1268 set=None,
1265 set=None,
1269 expLabel = "",
1266 expLabel = "",
1270 ext = None,
1267 ext = None,
1271 online = False,
1268 online = False,
1272 delay = 60,
1269 delay = 60,
1273 walk = True,
1270 walk = True,
1274 getblock = False,
1271 getblock = False,
1275 nTxs = 1,
1272 nTxs = 1,
1276 realtime=False,
1273 realtime=False,
1277 blocksize=None,
1274 blocksize=None,
1278 blocktime=None,
1275 blocktime=None,
1279 queue=None,
1276 queue=None,
1280 skip=None,
1277 skip=None,
1281 cursor=None,
1278 cursor=None,
1282 warnings=True,
1279 warnings=True,
1283 verbose=True,
1280 verbose=True,
1284 server=None):
1281 server=None):
1285 if server is not None:
1282 if server is not None:
1286 if 'tcp://' in server:
1283 if 'tcp://' in server:
1287 address = server
1284 address = server
1288 else:
1285 else:
1289 address = 'ipc:///tmp/%s' % server
1286 address = 'ipc:///tmp/%s' % server
1290 self.server = address
1287 self.server = address
1291 self.context = zmq.Context()
1288 self.context = zmq.Context()
1292 self.receiver = self.context.socket(zmq.PULL)
1289 self.receiver = self.context.socket(zmq.PULL)
1293 self.receiver.connect(self.server)
1290 self.receiver.connect(self.server)
1294 time.sleep(0.5)
1291 time.sleep(0.5)
1295 print '[Starting] ReceiverData from {}'.format(self.server)
1292 print '[Starting] ReceiverData from {}'.format(self.server)
1296 else:
1293 else:
1297 self.server = None
1294 self.server = None
1298 if path == None:
1295 if path == None:
1299 raise ValueError, "[Reading] The path is not valid"
1296 raise ValueError, "[Reading] The path is not valid"
1300
1297
1301 if ext == None:
1298 if ext == None:
1302 ext = self.ext
1299 ext = self.ext
1303
1300
1304 if online:
1301 if online:
1305 print "[Reading] Searching files in online mode..."
1302 print "[Reading] Searching files in online mode..."
1306
1303
1307 for nTries in range( self.nTries ):
1304 for nTries in range( self.nTries ):
1308 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1305 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1309
1306
1310 if fullpath:
1307 if fullpath:
1311 break
1308 break
1312
1309
1313 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1310 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1314 sleep( self.delay )
1311 sleep( self.delay )
1315
1312
1316 if not(fullpath):
1313 if not(fullpath):
1317 print "[Reading] There 'isn't any valid file in %s" % path
1314 print "[Reading] There 'isn't any valid file in %s" % path
1318 return
1315 return
1319
1316
1320 self.year = year
1317 self.year = year
1321 self.doy = doy
1318 self.doy = doy
1322 self.set = set - 1
1319 self.set = set - 1
1323 self.path = path
1320 self.path = path
1324 self.foldercounter = foldercounter
1321 self.foldercounter = foldercounter
1325 last_set = None
1322 last_set = None
1326 else:
1323 else:
1327 print "[Reading] Searching files in offline mode ..."
1324 print "[Reading] Searching files in offline mode ..."
1328 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1325 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1329 startTime=startTime, endTime=endTime,
1326 startTime=startTime, endTime=endTime,
1330 set=set, expLabel=expLabel, ext=ext,
1327 set=set, expLabel=expLabel, ext=ext,
1331 walk=walk, cursor=cursor,
1328 walk=walk, cursor=cursor,
1332 skip=skip, queue=queue)
1329 skip=skip, queue=queue)
1333
1330
1334 if not(pathList):
1331 if not(pathList):
1335 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1332 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1336 # datetime.datetime.combine(startDate,startTime).ctime(),
1333 # datetime.datetime.combine(startDate,startTime).ctime(),
1337 # datetime.datetime.combine(endDate,endTime).ctime())
1334 # datetime.datetime.combine(endDate,endTime).ctime())
1338
1335
1339 # sys.exit(-1)
1336 # sys.exit(-1)
1340
1337
1341 self.fileIndex = -1
1338 self.fileIndex = -1
1342 self.pathList = []
1339 self.pathList = []
1343 self.filenameList = []
1340 self.filenameList = []
1344 return
1341 return
1345
1342
1346 self.fileIndex = -1
1343 self.fileIndex = -1
1347 self.pathList = pathList
1344 self.pathList = pathList
1348 self.filenameList = filenameList
1345 self.filenameList = filenameList
1349 file_name = os.path.basename(filenameList[-1])
1346 file_name = os.path.basename(filenameList[-1])
1350 basename, ext = os.path.splitext(file_name)
1347 basename, ext = os.path.splitext(file_name)
1351 last_set = int(basename[-3:])
1348 last_set = int(basename[-3:])
1352
1349
1353 self.online = online
1350 self.online = online
1354 self.realtime = realtime
1351 self.realtime = realtime
1355 self.delay = delay
1352 self.delay = delay
1356 ext = ext.lower()
1353 ext = ext.lower()
1357 self.ext = ext
1354 self.ext = ext
1358 self.getByBlock = getblock
1355 self.getByBlock = getblock
1359 self.nTxs = nTxs
1356 self.nTxs = nTxs
1360 self.startTime = startTime
1357 self.startTime = startTime
1361 self.endTime = endTime
1358 self.endTime = endTime
1362
1359
1363 #Added-----------------
1360 #Added-----------------
1364 self.selBlocksize = blocksize
1361 self.selBlocksize = blocksize
1365 self.selBlocktime = blocktime
1362 self.selBlocktime = blocktime
1366
1363
1367 # Verbose-----------
1364 # Verbose-----------
1368 self.verbose = verbose
1365 self.verbose = verbose
1369 self.warnings = warnings
1366 self.warnings = warnings
1370
1367
1371 if not(self.setNextFile()):
1368 if not(self.setNextFile()):
1372 if (startDate!=None) and (endDate!=None):
1369 if (startDate!=None) and (endDate!=None):
1373 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1370 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1374 elif startDate != None:
1371 elif startDate != None:
1375 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1372 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1376 else:
1373 else:
1377 print "[Reading] No files"
1374 print "[Reading] No files"
1378
1375
1379 self.fileIndex = -1
1376 self.fileIndex = -1
1380 self.pathList = []
1377 self.pathList = []
1381 self.filenameList = []
1378 self.filenameList = []
1382 return
1379 return
1383
1380
1384 # self.getBasicHeader()
1381 # self.getBasicHeader()
1385
1382
1386 if last_set != None:
1383 if last_set != None:
1387 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1384 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1388 return
1385 return
1389
1386
1390 def getBasicHeader(self):
1387 def getBasicHeader(self):
1391
1388
1392 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1389 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1393
1390
1394 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1391 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1395
1392
1396 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1393 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1397
1394
1398 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1395 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1399
1396
1400 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1397 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1401
1398
1402 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1399 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1403
1400
1404 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1401 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1405
1402
1406 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1403 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1407
1404
1408
1405
1409 def getFirstHeader(self):
1406 def getFirstHeader(self):
1410
1407
1411 raise NotImplementedError
1408 raise NotImplementedError
1412
1409
1413 def getData(self):
1410 def getData(self):
1414
1411
1415 raise NotImplementedError
1412 raise NotImplementedError
1416
1413
1417 def hasNotDataInBuffer(self):
1414 def hasNotDataInBuffer(self):
1418
1415
1419 raise NotImplementedError
1416 raise NotImplementedError
1420
1417
1421 def readBlock(self):
1418 def readBlock(self):
1422
1419
1423 raise NotImplementedError
1420 raise NotImplementedError
1424
1421
1425 def isEndProcess(self):
1422 def isEndProcess(self):
1426
1423
1427 return self.flagNoMoreFiles
1424 return self.flagNoMoreFiles
1428
1425
1429 def printReadBlocks(self):
1426 def printReadBlocks(self):
1430
1427
1431 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1428 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1432
1429
1433 def printTotalBlocks(self):
1430 def printTotalBlocks(self):
1434
1431
1435 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1432 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1436
1433
1437 def printNumberOfBlock(self):
1434 def printNumberOfBlock(self):
1438
1435
1439 if self.flagIsNewBlock:
1436 if self.flagIsNewBlock:
1440 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1437 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1441 self.processingHeaderObj.dataBlocksPerFile,
1438 self.processingHeaderObj.dataBlocksPerFile,
1442 self.dataOut.datatime.ctime())
1439 self.dataOut.datatime.ctime())
1443
1440
1444 def printInfo(self):
1441 def printInfo(self):
1445
1442
1446 if self.__printInfo == False:
1443 if self.__printInfo == False:
1447 return
1444 return
1448
1445
1449 self.basicHeaderObj.printInfo()
1446 self.basicHeaderObj.printInfo()
1450 self.systemHeaderObj.printInfo()
1447 self.systemHeaderObj.printInfo()
1451 self.radarControllerHeaderObj.printInfo()
1448 self.radarControllerHeaderObj.printInfo()
1452 self.processingHeaderObj.printInfo()
1449 self.processingHeaderObj.printInfo()
1453
1450
1454 self.__printInfo = False
1451 self.__printInfo = False
1455
1452
1456
1453
1457 def run(self,
1454 def run(self,
1458 path=None,
1455 path=None,
1459 startDate=None,
1456 startDate=None,
1460 endDate=None,
1457 endDate=None,
1461 startTime=datetime.time(0,0,0),
1458 startTime=datetime.time(0,0,0),
1462 endTime=datetime.time(23,59,59),
1459 endTime=datetime.time(23,59,59),
1463 set=None,
1460 set=None,
1464 expLabel = "",
1461 expLabel = "",
1465 ext = None,
1462 ext = None,
1466 online = False,
1463 online = False,
1467 delay = 60,
1464 delay = 60,
1468 walk = True,
1465 walk = True,
1469 getblock = False,
1466 getblock = False,
1470 nTxs = 1,
1467 nTxs = 1,
1471 realtime=False,
1468 realtime=False,
1472 blocksize=None,
1469 blocksize=None,
1473 blocktime=None,
1470 blocktime=None,
1474 queue=None,
1471 queue=None,
1475 skip=None,
1472 skip=None,
1476 cursor=None,
1473 cursor=None,
1477 warnings=True,
1474 warnings=True,
1478 server=None,
1475 server=None,
1479 verbose=True, **kwargs):
1476 verbose=True, **kwargs):
1480
1477
1481 if not(self.isConfig):
1478 if not(self.isConfig):
1482 # self.dataOut = dataOut
1479 # self.dataOut = dataOut
1483 self.setup( path=path,
1480 self.setup( path=path,
1484 startDate=startDate,
1481 startDate=startDate,
1485 endDate=endDate,
1482 endDate=endDate,
1486 startTime=startTime,
1483 startTime=startTime,
1487 endTime=endTime,
1484 endTime=endTime,
1488 set=set,
1485 set=set,
1489 expLabel=expLabel,
1486 expLabel=expLabel,
1490 ext=ext,
1487 ext=ext,
1491 online=online,
1488 online=online,
1492 delay=delay,
1489 delay=delay,
1493 walk=walk,
1490 walk=walk,
1494 getblock=getblock,
1491 getblock=getblock,
1495 nTxs=nTxs,
1492 nTxs=nTxs,
1496 realtime=realtime,
1493 realtime=realtime,
1497 blocksize=blocksize,
1494 blocksize=blocksize,
1498 blocktime=blocktime,
1495 blocktime=blocktime,
1499 queue=queue,
1496 queue=queue,
1500 skip=skip,
1497 skip=skip,
1501 cursor=cursor,
1498 cursor=cursor,
1502 warnings=warnings,
1499 warnings=warnings,
1503 server=server,
1500 server=server,
1504 verbose=verbose)
1501 verbose=verbose)
1505 self.isConfig = True
1502 self.isConfig = True
1506 if server is None:
1503 if server is None:
1507 self.getData()
1504 self.getData()
1508 else:
1505 else:
1509 self.getFromServer()
1506 self.getFromServer()
1510
1507
1511 class JRODataWriter(JRODataIO):
1508 class JRODataWriter(JRODataIO):
1512
1509
1513 """
1510 """
1514 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1511 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1515 de los datos siempre se realiza por bloques.
1512 de los datos siempre se realiza por bloques.
1516 """
1513 """
1517
1514
1518 blockIndex = 0
1515 blockIndex = 0
1519
1516
1520 path = None
1517 path = None
1521
1518
1522 setFile = None
1519 setFile = None
1523
1520
1524 profilesPerBlock = None
1521 profilesPerBlock = None
1525
1522
1526 blocksPerFile = None
1523 blocksPerFile = None
1527
1524
1528 nWriteBlocks = 0
1525 nWriteBlocks = 0
1529
1526
1530 fileDate = None
1527 fileDate = None
1531
1528
1532 def __init__(self, dataOut=None):
1529 def __init__(self, dataOut=None):
1533 raise NotImplementedError
1530 raise NotImplementedError
1534
1531
1535
1532
1536 def hasAllDataInBuffer(self):
1533 def hasAllDataInBuffer(self):
1537 raise NotImplementedError
1534 raise NotImplementedError
1538
1535
1539
1536
1540 def setBlockDimension(self):
1537 def setBlockDimension(self):
1541 raise NotImplementedError
1538 raise NotImplementedError
1542
1539
1543
1540
1544 def writeBlock(self):
1541 def writeBlock(self):
1545 raise NotImplementedError
1542 raise NotImplementedError
1546
1543
1547
1544
1548 def putData(self):
1545 def putData(self):
1549 raise NotImplementedError
1546 raise NotImplementedError
1550
1547
1551
1548
1552 def getProcessFlags(self):
1549 def getProcessFlags(self):
1553
1550
1554 processFlags = 0
1551 processFlags = 0
1555
1552
1556 dtype_index = get_dtype_index(self.dtype)
1553 dtype_index = get_dtype_index(self.dtype)
1557 procflag_dtype = get_procflag_dtype(dtype_index)
1554 procflag_dtype = get_procflag_dtype(dtype_index)
1558
1555
1559 processFlags += procflag_dtype
1556 processFlags += procflag_dtype
1560
1557
1561 if self.dataOut.flagDecodeData:
1558 if self.dataOut.flagDecodeData:
1562 processFlags += PROCFLAG.DECODE_DATA
1559 processFlags += PROCFLAG.DECODE_DATA
1563
1560
1564 if self.dataOut.flagDeflipData:
1561 if self.dataOut.flagDeflipData:
1565 processFlags += PROCFLAG.DEFLIP_DATA
1562 processFlags += PROCFLAG.DEFLIP_DATA
1566
1563
1567 if self.dataOut.code is not None:
1564 if self.dataOut.code is not None:
1568 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1565 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1569
1566
1570 if self.dataOut.nCohInt > 1:
1567 if self.dataOut.nCohInt > 1:
1571 processFlags += PROCFLAG.COHERENT_INTEGRATION
1568 processFlags += PROCFLAG.COHERENT_INTEGRATION
1572
1569
1573 if self.dataOut.type == "Spectra":
1570 if self.dataOut.type == "Spectra":
1574 if self.dataOut.nIncohInt > 1:
1571 if self.dataOut.nIncohInt > 1:
1575 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1572 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1576
1573
1577 if self.dataOut.data_dc is not None:
1574 if self.dataOut.data_dc is not None:
1578 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1575 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1579
1576
1580 if self.dataOut.flagShiftFFT:
1577 if self.dataOut.flagShiftFFT:
1581 processFlags += PROCFLAG.SHIFT_FFT_DATA
1578 processFlags += PROCFLAG.SHIFT_FFT_DATA
1582
1579
1583 return processFlags
1580 return processFlags
1584
1581
1585 def setBasicHeader(self):
1582 def setBasicHeader(self):
1586
1583
1587 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1584 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1588 self.basicHeaderObj.version = self.versionFile
1585 self.basicHeaderObj.version = self.versionFile
1589 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1586 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1590
1587
1591 utc = numpy.floor(self.dataOut.utctime)
1588 utc = numpy.floor(self.dataOut.utctime)
1592 milisecond = (self.dataOut.utctime - utc)* 1000.0
1589 milisecond = (self.dataOut.utctime - utc)* 1000.0
1593
1590
1594 self.basicHeaderObj.utc = utc
1591 self.basicHeaderObj.utc = utc
1595 self.basicHeaderObj.miliSecond = milisecond
1592 self.basicHeaderObj.miliSecond = milisecond
1596 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1593 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1597 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1594 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1598 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1595 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1599
1596
1600 def setFirstHeader(self):
1597 def setFirstHeader(self):
1601 """
1598 """
1602 Obtiene una copia del First Header
1599 Obtiene una copia del First Header
1603
1600
1604 Affected:
1601 Affected:
1605
1602
1606 self.basicHeaderObj
1603 self.basicHeaderObj
1607 self.systemHeaderObj
1604 self.systemHeaderObj
1608 self.radarControllerHeaderObj
1605 self.radarControllerHeaderObj
1609 self.processingHeaderObj self.
1606 self.processingHeaderObj self.
1610
1607
1611 Return:
1608 Return:
1612 None
1609 None
1613 """
1610 """
1614
1611
1615 raise NotImplementedError
1612 raise NotImplementedError
1616
1613
1617 def __writeFirstHeader(self):
1614 def __writeFirstHeader(self):
1618 """
1615 """
1619 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1616 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1620
1617
1621 Affected:
1618 Affected:
1622 __dataType
1619 __dataType
1623
1620
1624 Return:
1621 Return:
1625 None
1622 None
1626 """
1623 """
1627
1624
1628 # CALCULAR PARAMETROS
1625 # CALCULAR PARAMETROS
1629
1626
1630 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1627 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1631 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1628 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1632
1629
1633 self.basicHeaderObj.write(self.fp)
1630 self.basicHeaderObj.write(self.fp)
1634 self.systemHeaderObj.write(self.fp)
1631 self.systemHeaderObj.write(self.fp)
1635 self.radarControllerHeaderObj.write(self.fp)
1632 self.radarControllerHeaderObj.write(self.fp)
1636 self.processingHeaderObj.write(self.fp)
1633 self.processingHeaderObj.write(self.fp)
1637
1634
1638 def __setNewBlock(self):
1635 def __setNewBlock(self):
1639 """
1636 """
1640 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1637 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1641
1638
1642 Return:
1639 Return:
1643 0 : si no pudo escribir nada
1640 0 : si no pudo escribir nada
1644 1 : Si escribio el Basic el First Header
1641 1 : Si escribio el Basic el First Header
1645 """
1642 """
1646 if self.fp == None:
1643 if self.fp == None:
1647 self.setNextFile()
1644 self.setNextFile()
1648
1645
1649 if self.flagIsNewFile:
1646 if self.flagIsNewFile:
1650 return 1
1647 return 1
1651
1648
1652 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1649 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1653 self.basicHeaderObj.write(self.fp)
1650 self.basicHeaderObj.write(self.fp)
1654 return 1
1651 return 1
1655
1652
1656 if not( self.setNextFile() ):
1653 if not( self.setNextFile() ):
1657 return 0
1654 return 0
1658
1655
1659 return 1
1656 return 1
1660
1657
1661
1658
1662 def writeNextBlock(self):
1659 def writeNextBlock(self):
1663 """
1660 """
1664 Selecciona el bloque siguiente de datos y los escribe en un file
1661 Selecciona el bloque siguiente de datos y los escribe en un file
1665
1662
1666 Return:
1663 Return:
1667 0 : Si no hizo pudo escribir el bloque de datos
1664 0 : Si no hizo pudo escribir el bloque de datos
1668 1 : Si no pudo escribir el bloque de datos
1665 1 : Si no pudo escribir el bloque de datos
1669 """
1666 """
1670 if not( self.__setNewBlock() ):
1667 if not( self.__setNewBlock() ):
1671 return 0
1668 return 0
1672
1669
1673 self.writeBlock()
1670 self.writeBlock()
1674
1671
1675 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1672 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1676 self.processingHeaderObj.dataBlocksPerFile)
1673 self.processingHeaderObj.dataBlocksPerFile)
1677
1674
1678 return 1
1675 return 1
1679
1676
1680 def setNextFile(self):
1677 def setNextFile(self):
1681 """
1678 """
1682 Determina el siguiente file que sera escrito
1679 Determina el siguiente file que sera escrito
1683
1680
1684 Affected:
1681 Affected:
1685 self.filename
1682 self.filename
1686 self.subfolder
1683 self.subfolder
1687 self.fp
1684 self.fp
1688 self.setFile
1685 self.setFile
1689 self.flagIsNewFile
1686 self.flagIsNewFile
1690
1687
1691 Return:
1688 Return:
1692 0 : Si el archivo no puede ser escrito
1689 0 : Si el archivo no puede ser escrito
1693 1 : Si el archivo esta listo para ser escrito
1690 1 : Si el archivo esta listo para ser escrito
1694 """
1691 """
1695 ext = self.ext
1692 ext = self.ext
1696 path = self.path
1693 path = self.path
1697
1694
1698 if self.fp != None:
1695 if self.fp != None:
1699 self.fp.close()
1696 self.fp.close()
1700
1697
1701 timeTuple = time.localtime( self.dataOut.utctime)
1698 timeTuple = time.localtime( self.dataOut.utctime)
1702 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1699 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1703
1700
1704 fullpath = os.path.join( path, subfolder )
1701 fullpath = os.path.join( path, subfolder )
1705 setFile = self.setFile
1702 setFile = self.setFile
1706
1703
1707 if not( os.path.exists(fullpath) ):
1704 if not( os.path.exists(fullpath) ):
1708 os.mkdir(fullpath)
1705 os.mkdir(fullpath)
1709 setFile = -1 #inicializo mi contador de seteo
1706 setFile = -1 #inicializo mi contador de seteo
1710 else:
1707 else:
1711 filesList = os.listdir( fullpath )
1708 filesList = os.listdir( fullpath )
1712 if len( filesList ) > 0:
1709 if len( filesList ) > 0:
1713 filesList = sorted( filesList, key=str.lower )
1710 filesList = sorted( filesList, key=str.lower )
1714 filen = filesList[-1]
1711 filen = filesList[-1]
1715 # el filename debera tener el siguiente formato
1712 # el filename debera tener el siguiente formato
1716 # 0 1234 567 89A BCDE (hex)
1713 # 0 1234 567 89A BCDE (hex)
1717 # x YYYY DDD SSS .ext
1714 # x YYYY DDD SSS .ext
1718 if isNumber( filen[8:11] ):
1715 if isNumber( filen[8:11] ):
1719 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1716 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1720 else:
1717 else:
1721 setFile = -1
1718 setFile = -1
1722 else:
1719 else:
1723 setFile = -1 #inicializo mi contador de seteo
1720 setFile = -1 #inicializo mi contador de seteo
1724
1721
1725 setFile += 1
1722 setFile += 1
1726
1723
1727 #If this is a new day it resets some values
1724 #If this is a new day it resets some values
1728 if self.dataOut.datatime.date() > self.fileDate:
1725 if self.dataOut.datatime.date() > self.fileDate:
1729 setFile = 0
1726 setFile = 0
1730 self.nTotalBlocks = 0
1727 self.nTotalBlocks = 0
1731
1728
1732 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1729 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1733
1730
1734 filename = os.path.join( path, subfolder, filen )
1731 filename = os.path.join( path, subfolder, filen )
1735
1732
1736 fp = open( filename,'wb' )
1733 fp = open( filename,'wb' )
1737
1734
1738 self.blockIndex = 0
1735 self.blockIndex = 0
1739
1736
1740 #guardando atributos
1737 #guardando atributos
1741 self.filename = filename
1738 self.filename = filename
1742 self.subfolder = subfolder
1739 self.subfolder = subfolder
1743 self.fp = fp
1740 self.fp = fp
1744 self.setFile = setFile
1741 self.setFile = setFile
1745 self.flagIsNewFile = 1
1742 self.flagIsNewFile = 1
1746 self.fileDate = self.dataOut.datatime.date()
1743 self.fileDate = self.dataOut.datatime.date()
1747
1744
1748 self.setFirstHeader()
1745 self.setFirstHeader()
1749
1746
1750 print '[Writing] Opening file: %s'%self.filename
1747 print '[Writing] Opening file: %s'%self.filename
1751
1748
1752 self.__writeFirstHeader()
1749 self.__writeFirstHeader()
1753
1750
1754 return 1
1751 return 1
1755
1752
1756 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1753 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1757 """
1754 """
1758 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1755 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1759
1756
1760 Inputs:
1757 Inputs:
1761 path : directory where data will be saved
1758 path : directory where data will be saved
1762 profilesPerBlock : number of profiles per block
1759 profilesPerBlock : number of profiles per block
1763 set : initial file set
1760 set : initial file set
1764 datatype : An integer number that defines data type:
1761 datatype : An integer number that defines data type:
1765 0 : int8 (1 byte)
1762 0 : int8 (1 byte)
1766 1 : int16 (2 bytes)
1763 1 : int16 (2 bytes)
1767 2 : int32 (4 bytes)
1764 2 : int32 (4 bytes)
1768 3 : int64 (8 bytes)
1765 3 : int64 (8 bytes)
1769 4 : float32 (4 bytes)
1766 4 : float32 (4 bytes)
1770 5 : double64 (8 bytes)
1767 5 : double64 (8 bytes)
1771
1768
1772 Return:
1769 Return:
1773 0 : Si no realizo un buen seteo
1770 0 : Si no realizo un buen seteo
1774 1 : Si realizo un buen seteo
1771 1 : Si realizo un buen seteo
1775 """
1772 """
1776
1773
1777 if ext == None:
1774 if ext == None:
1778 ext = self.ext
1775 ext = self.ext
1779
1776
1780 self.ext = ext.lower()
1777 self.ext = ext.lower()
1781
1778
1782 self.path = path
1779 self.path = path
1783
1780
1784 if set is None:
1781 if set is None:
1785 self.setFile = -1
1782 self.setFile = -1
1786 else:
1783 else:
1787 self.setFile = set - 1
1784 self.setFile = set - 1
1788
1785
1789 self.blocksPerFile = blocksPerFile
1786 self.blocksPerFile = blocksPerFile
1790
1787
1791 self.profilesPerBlock = profilesPerBlock
1788 self.profilesPerBlock = profilesPerBlock
1792
1789
1793 self.dataOut = dataOut
1790 self.dataOut = dataOut
1794 self.fileDate = self.dataOut.datatime.date()
1791 self.fileDate = self.dataOut.datatime.date()
1795 #By default
1792 #By default
1796 self.dtype = self.dataOut.dtype
1793 self.dtype = self.dataOut.dtype
1797
1794
1798 if datatype is not None:
1795 if datatype is not None:
1799 self.dtype = get_numpy_dtype(datatype)
1796 self.dtype = get_numpy_dtype(datatype)
1800
1797
1801 if not(self.setNextFile()):
1798 if not(self.setNextFile()):
1802 print "[Writing] There isn't a next file"
1799 print "[Writing] There isn't a next file"
1803 return 0
1800 return 0
1804
1801
1805 self.setBlockDimension()
1802 self.setBlockDimension()
1806
1803
1807 return 1
1804 return 1
1808
1805
1809 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1806 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1810
1807
1811 if not(self.isConfig):
1808 if not(self.isConfig):
1812
1809
1813 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1810 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1814 self.isConfig = True
1811 self.isConfig = True
1815
1812
1816 self.putData()
1813 self.putData()
@@ -1,739 +1,737
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6
6
7 import numpy
7 import numpy
8
8
9 from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
9 from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
11 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
11 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
12 from schainpy.model.data.jrodata import Voltage
12 from schainpy.model.data.jrodata import Voltage
13 import zmq
13 import zmq
14 import tempfile
14 import tempfile
15 from StringIO import StringIO
15 from StringIO import StringIO
16 # from _sha import blocksize
16 # from _sha import blocksize
17
17
18 class VoltageReader(JRODataReader, ProcessingUnit):
18 class VoltageReader(JRODataReader, ProcessingUnit):
19 """
19 """
20 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
20 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
21 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
21 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
22 perfiles*alturas*canales) son almacenados en la variable "buffer".
22 perfiles*alturas*canales) son almacenados en la variable "buffer".
23
23
24 perfiles * alturas * canales
24 perfiles * alturas * canales
25
25
26 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
26 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
27 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
27 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
28 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
28 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
29 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
29 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
30
30
31 Example:
31 Example:
32
32
33 dpath = "/home/myuser/data"
33 dpath = "/home/myuser/data"
34
34
35 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
35 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
36
36
37 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
37 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
38
38
39 readerObj = VoltageReader()
39 readerObj = VoltageReader()
40
40
41 readerObj.setup(dpath, startTime, endTime)
41 readerObj.setup(dpath, startTime, endTime)
42
42
43 while(True):
43 while(True):
44
44
45 #to get one profile
45 #to get one profile
46 profile = readerObj.getData()
46 profile = readerObj.getData()
47
47
48 #print the profile
48 #print the profile
49 print profile
49 print profile
50
50
51 #If you want to see all datablock
51 #If you want to see all datablock
52 print readerObj.datablock
52 print readerObj.datablock
53
53
54 if readerObj.flagNoMoreFiles:
54 if readerObj.flagNoMoreFiles:
55 break
55 break
56
56
57 """
57 """
58
58
59 ext = ".r"
59 ext = ".r"
60
60
61 optchar = "D"
61 optchar = "D"
62 dataOut = None
62 dataOut = None
63
63
64 def __init__(self, **kwargs):
64 def __init__(self, **kwargs):
65 """
65 """
66 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
66 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
67
67
68 Input:
68 Input:
69 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
69 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
70 almacenar un perfil de datos cada vez que se haga un requerimiento
70 almacenar un perfil de datos cada vez que se haga un requerimiento
71 (getData). El perfil sera obtenido a partir del buffer de datos,
71 (getData). El perfil sera obtenido a partir del buffer de datos,
72 si el buffer esta vacio se hara un nuevo proceso de lectura de un
72 si el buffer esta vacio se hara un nuevo proceso de lectura de un
73 bloque de datos.
73 bloque de datos.
74 Si este parametro no es pasado se creara uno internamente.
74 Si este parametro no es pasado se creara uno internamente.
75
75
76 Variables afectadas:
76 Variables afectadas:
77 self.dataOut
77 self.dataOut
78
78
79 Return:
79 Return:
80 None
80 None
81 """
81 """
82
82
83 ProcessingUnit.__init__(self, **kwargs)
83 ProcessingUnit.__init__(self, **kwargs)
84
84
85 self.isConfig = False
85 self.isConfig = False
86
86
87 self.datablock = None
87 self.datablock = None
88
88
89 self.utc = 0
89 self.utc = 0
90
90
91 self.ext = ".r"
91 self.ext = ".r"
92
92
93 self.optchar = "D"
93 self.optchar = "D"
94
94
95 self.basicHeaderObj = BasicHeader(LOCALTIME)
95 self.basicHeaderObj = BasicHeader(LOCALTIME)
96
96
97 self.systemHeaderObj = SystemHeader()
97 self.systemHeaderObj = SystemHeader()
98
98
99 self.radarControllerHeaderObj = RadarControllerHeader()
99 self.radarControllerHeaderObj = RadarControllerHeader()
100
100
101 self.processingHeaderObj = ProcessingHeader()
101 self.processingHeaderObj = ProcessingHeader()
102
102
103 self.online = 0
103 self.online = 0
104
104
105 self.fp = None
105 self.fp = None
106
106
107 self.idFile = None
107 self.idFile = None
108
108
109 self.dtype = None
109 self.dtype = None
110
110
111 self.fileSizeByHeader = None
111 self.fileSizeByHeader = None
112
112
113 self.filenameList = []
113 self.filenameList = []
114
114
115 self.filename = None
115 self.filename = None
116
116
117 self.fileSize = None
117 self.fileSize = None
118
118
119 self.firstHeaderSize = 0
119 self.firstHeaderSize = 0
120
120
121 self.basicHeaderSize = 24
121 self.basicHeaderSize = 24
122
122
123 self.pathList = []
123 self.pathList = []
124
124
125 self.filenameList = []
125 self.filenameList = []
126
126
127 self.lastUTTime = 0
127 self.lastUTTime = 0
128
128
129 self.maxTimeStep = 30
129 self.maxTimeStep = 30
130
130
131 self.flagNoMoreFiles = 0
131 self.flagNoMoreFiles = 0
132
132
133 self.set = 0
133 self.set = 0
134
134
135 self.path = None
135 self.path = None
136
136
137 self.profileIndex = 2**32-1
137 self.profileIndex = 2**32-1
138
138
139 self.delay = 3 #seconds
139 self.delay = 3 #seconds
140
140
141 self.nTries = 3 #quantity tries
141 self.nTries = 3 #quantity tries
142
142
143 self.nFiles = 3 #number of files for searching
143 self.nFiles = 3 #number of files for searching
144
144
145 self.nReadBlocks = 0
145 self.nReadBlocks = 0
146
146
147 self.flagIsNewFile = 1
147 self.flagIsNewFile = 1
148
148
149 self.__isFirstTimeOnline = 1
149 self.__isFirstTimeOnline = 1
150
150
151 # self.ippSeconds = 0
151 # self.ippSeconds = 0
152
152
153 self.flagDiscontinuousBlock = 0
153 self.flagDiscontinuousBlock = 0
154
154
155 self.flagIsNewBlock = 0
155 self.flagIsNewBlock = 0
156
156
157 self.nTotalBlocks = 0
157 self.nTotalBlocks = 0
158
158
159 self.blocksize = 0
159 self.blocksize = 0
160
160
161 self.dataOut = self.createObjByDefault()
161 self.dataOut = self.createObjByDefault()
162
162
163 self.nTxs = 1
163 self.nTxs = 1
164
164
165 self.txIndex = 0
165 self.txIndex = 0
166
166
167 def createObjByDefault(self):
167 def createObjByDefault(self):
168
168
169 dataObj = Voltage()
169 dataObj = Voltage()
170
170
171 return dataObj
171 return dataObj
172
172
173 def __hasNotDataInBuffer(self):
173 def __hasNotDataInBuffer(self):
174
174
175 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock*self.nTxs:
175 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock*self.nTxs:
176 return 1
176 return 1
177
177
178 return 0
178 return 0
179
179
180
180
181 def getBlockDimension(self):
181 def getBlockDimension(self):
182 """
182 """
183 Obtiene la cantidad de puntos a leer por cada bloque de datos
183 Obtiene la cantidad de puntos a leer por cada bloque de datos
184
184
185 Affected:
185 Affected:
186 self.blocksize
186 self.blocksize
187
187
188 Return:
188 Return:
189 None
189 None
190 """
190 """
191 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
191 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
192 self.blocksize = pts2read
192 self.blocksize = pts2read
193
193
194
194
195
195
196 def readBlock(self):
196 def readBlock(self):
197 """
197 """
198 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
198 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
199 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
199 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
200 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
200 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
201 es seteado a 0
201 es seteado a 0
202
202
203 Inputs:
203 Inputs:
204 None
204 None
205
205
206 Return:
206 Return:
207 None
207 None
208
208
209 Affected:
209 Affected:
210 self.profileIndex
210 self.profileIndex
211 self.datablock
211 self.datablock
212 self.flagIsNewFile
212 self.flagIsNewFile
213 self.flagIsNewBlock
213 self.flagIsNewBlock
214 self.nTotalBlocks
214 self.nTotalBlocks
215
215
216 Exceptions:
216 Exceptions:
217 Si un bloque leido no es un bloque valido
217 Si un bloque leido no es un bloque valido
218 """
218 """
219
219
220 print 'READ BLOCK'
221 # if self.server is not None:
220 # if self.server is not None:
222 # self.zBlock = self.receiver.recv()
221 # self.zBlock = self.receiver.recv()
223 # self.zHeader = self.zBlock[:24]
222 # self.zHeader = self.zBlock[:24]
224 # self.zDataBlock = self.zBlock[24:]
223 # self.zDataBlock = self.zBlock[24:]
225 # junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')]))
224 # junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')]))
226 # self.processingHeaderObj.profilesPerBlock = 240
225 # self.processingHeaderObj.profilesPerBlock = 240
227 # self.processingHeaderObj.nHeights = 248
226 # self.processingHeaderObj.nHeights = 248
228 # self.systemHeaderObj.nChannels
227 # self.systemHeaderObj.nChannels
229 # else:
228 # else:
230 current_pointer_location = self.fp.tell()
229 current_pointer_location = self.fp.tell()
231 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
230 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
232
231
233 try:
232 try:
234 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
233 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
235 print'junked'
236 except:
234 except:
237 #print "The read block (%3d) has not enough data" %self.nReadBlocks
235 #print "The read block (%3d) has not enough data" %self.nReadBlocks
238
236
239 if self.waitDataBlock(pointer_location=current_pointer_location):
237 if self.waitDataBlock(pointer_location=current_pointer_location):
240 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
238 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
241 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
239 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
242 # return 0
240 # return 0
243
241
244 #Dimensions : nChannels, nProfiles, nSamples
242 #Dimensions : nChannels, nProfiles, nSamples
245
243
246 junk = numpy.transpose(junk, (2,0,1))
244 junk = numpy.transpose(junk, (2,0,1))
247 self.datablock = junk['real'] + junk['imag']*1j
245 self.datablock = junk['real'] + junk['imag']*1j
248
246
249 self.profileIndex = 0
247 self.profileIndex = 0
250
248
251 self.flagIsNewFile = 0
249 self.flagIsNewFile = 0
252 self.flagIsNewBlock = 1
250 self.flagIsNewBlock = 1
253
251
254 self.nTotalBlocks += 1
252 self.nTotalBlocks += 1
255 self.nReadBlocks += 1
253 self.nReadBlocks += 1
256
254
257 return 1
255 return 1
258
256
259 def getFirstHeader(self):
257 def getFirstHeader(self):
260
258
261 self.getBasicHeader()
259 self.getBasicHeader()
262
260
263 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
261 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
264
262
265 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
263 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
266
264
267 if self.nTxs > 1:
265 if self.nTxs > 1:
268 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
266 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
269
267
270 #Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
268 #Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
271
269
272 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
270 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
273 #
271 #
274 # if self.radarControllerHeaderObj.code is not None:
272 # if self.radarControllerHeaderObj.code is not None:
275 #
273 #
276 # self.dataOut.nCode = self.radarControllerHeaderObj.nCode
274 # self.dataOut.nCode = self.radarControllerHeaderObj.nCode
277 #
275 #
278 # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
276 # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
279 #
277 #
280 # self.dataOut.code = self.radarControllerHeaderObj.code
278 # self.dataOut.code = self.radarControllerHeaderObj.code
281
279
282 self.dataOut.dtype = self.dtype
280 self.dataOut.dtype = self.dtype
283
281
284 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
282 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
285
283
286 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
284 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
287
285
288 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
286 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
289
287
290 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
288 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
291
289
292 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
290 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
293
291
294 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data no esta sin flip
292 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data no esta sin flip
295
293
296 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
294 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
297
295
298 def reshapeData(self):
296 def reshapeData(self):
299
297
300 if self.nTxs < 0:
298 if self.nTxs < 0:
301 return
299 return
302
300
303 if self.nTxs == 1:
301 if self.nTxs == 1:
304 return
302 return
305
303
306 if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1./self.nTxs) != 0:
304 if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1./self.nTxs) != 0:
307 raise ValueError, "1./nTxs (=%f), should be a multiple of nProfiles (=%d)" %(1./self.nTxs, self.processingHeaderObj.profilesPerBlock)
305 raise ValueError, "1./nTxs (=%f), should be a multiple of nProfiles (=%d)" %(1./self.nTxs, self.processingHeaderObj.profilesPerBlock)
308
306
309 if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
307 if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
310 raise ValueError, "nTxs (=%d), should be a multiple of nHeights (=%d)" %(self.nTxs, self.processingHeaderObj.nHeights)
308 raise ValueError, "nTxs (=%d), should be a multiple of nHeights (=%d)" %(self.nTxs, self.processingHeaderObj.nHeights)
311
309
312 self.datablock = self.datablock.reshape((self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock*self.nTxs, self.processingHeaderObj.nHeights/self.nTxs))
310 self.datablock = self.datablock.reshape((self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock*self.nTxs, self.processingHeaderObj.nHeights/self.nTxs))
313
311
314 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
312 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
315 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights/self.nTxs) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
313 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights/self.nTxs) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
316 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
314 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
317
315
318 return
316 return
319
317
320 def readFirstHeaderFromServer(self):
318 def readFirstHeaderFromServer(self):
321
319
322 self.getFirstHeader()
320 self.getFirstHeader()
323
321
324 self.firstHeaderSize = self.basicHeaderObj.size
322 self.firstHeaderSize = self.basicHeaderObj.size
325
323
326 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
324 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
327 if datatype == 0:
325 if datatype == 0:
328 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
326 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
329 elif datatype == 1:
327 elif datatype == 1:
330 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
328 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
331 elif datatype == 2:
329 elif datatype == 2:
332 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
330 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
333 elif datatype == 3:
331 elif datatype == 3:
334 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
332 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
335 elif datatype == 4:
333 elif datatype == 4:
336 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
334 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
337 elif datatype == 5:
335 elif datatype == 5:
338 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
336 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
339 else:
337 else:
340 raise ValueError, 'Data type was not defined'
338 raise ValueError, 'Data type was not defined'
341
339
342 self.dtype = datatype_str
340 self.dtype = datatype_str
343 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
341 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
344 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
342 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
345 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
343 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
346 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
344 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
347 self.getBlockDimension()
345 self.getBlockDimension()
348
346
349
347
350 def getFromServer(self):
348 def getFromServer(self):
351 self.flagDiscontinuousBlock = 0
349 self.flagDiscontinuousBlock = 0
352 self.profileIndex = 0
350 self.profileIndex = 0
353 self.flagIsNewBlock = 1
351 self.flagIsNewBlock = 1
354 self.dataOut.flagNoData = False
352 self.dataOut.flagNoData = False
355 self.nTotalBlocks += 1
353 self.nTotalBlocks += 1
356 self.nReadBlocks += 1
354 self.nReadBlocks += 1
357 self.blockPointer = 0
355 self.blockPointer = 0
358
356
359 block = self.receiver.recv()
357 block = self.receiver.recv()
360
358
361 self.basicHeaderObj.read(block[self.blockPointer:])
359 self.basicHeaderObj.read(block[self.blockPointer:])
362 self.blockPointer += self.basicHeaderObj.length
360 self.blockPointer += self.basicHeaderObj.length
363 self.systemHeaderObj.read(block[self.blockPointer:])
361 self.systemHeaderObj.read(block[self.blockPointer:])
364 self.blockPointer += self.systemHeaderObj.length
362 self.blockPointer += self.systemHeaderObj.length
365 self.radarControllerHeaderObj.read(block[self.blockPointer:])
363 self.radarControllerHeaderObj.read(block[self.blockPointer:])
366 self.blockPointer += self.radarControllerHeaderObj.length
364 self.blockPointer += self.radarControllerHeaderObj.length
367 self.processingHeaderObj.read(block[self.blockPointer:])
365 self.processingHeaderObj.read(block[self.blockPointer:])
368 self.blockPointer += self.processingHeaderObj.length
366 self.blockPointer += self.processingHeaderObj.length
369 self.readFirstHeaderFromServer()
367 self.readFirstHeaderFromServer()
370
368
371 timestamp = self.basicHeaderObj.get_datatime()
369 timestamp = self.basicHeaderObj.get_datatime()
372 print '[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp)
370 print '[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp)
373 current_pointer_location = self.blockPointer
371 current_pointer_location = self.blockPointer
374 junk = numpy.fromstring( block[self.blockPointer:], self.dtype, self.blocksize )
372 junk = numpy.fromstring( block[self.blockPointer:], self.dtype, self.blocksize )
375
373
376 try:
374 try:
377 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
375 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
378 except:
376 except:
379 #print "The read block (%3d) has not enough data" %self.nReadBlocks
377 #print "The read block (%3d) has not enough data" %self.nReadBlocks
380 if self.waitDataBlock(pointer_location=current_pointer_location):
378 if self.waitDataBlock(pointer_location=current_pointer_location):
381 junk = numpy.fromstring( block[self.blockPointer:], self.dtype, self.blocksize )
379 junk = numpy.fromstring( block[self.blockPointer:], self.dtype, self.blocksize )
382 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
380 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
383 # return 0
381 # return 0
384
382
385 #Dimensions : nChannels, nProfiles, nSamples
383 #Dimensions : nChannels, nProfiles, nSamples
386
384
387 junk = numpy.transpose(junk, (2,0,1))
385 junk = numpy.transpose(junk, (2,0,1))
388 self.datablock = junk['real'] + junk['imag'] * 1j
386 self.datablock = junk['real'] + junk['imag'] * 1j
389 self.profileIndex = 0
387 self.profileIndex = 0
390 if self.selBlocksize == None: self.selBlocksize = self.dataOut.nProfiles
388 if self.selBlocksize == None: self.selBlocksize = self.dataOut.nProfiles
391 if self.selBlocktime != None:
389 if self.selBlocktime != None:
392 if self.dataOut.nCohInt is not None:
390 if self.dataOut.nCohInt is not None:
393 nCohInt = self.dataOut.nCohInt
391 nCohInt = self.dataOut.nCohInt
394 else:
392 else:
395 nCohInt = 1
393 nCohInt = 1
396 self.selBlocksize = int(self.dataOut.nProfiles*round(self.selBlocktime/(nCohInt*self.dataOut.ippSeconds*self.dataOut.nProfiles)))
394 self.selBlocksize = int(self.dataOut.nProfiles*round(self.selBlocktime/(nCohInt*self.dataOut.ippSeconds*self.dataOut.nProfiles)))
397 self.dataOut.data = self.datablock[:,self.profileIndex:self.profileIndex+self.selBlocksize,:]
395 self.dataOut.data = self.datablock[:,self.profileIndex:self.profileIndex+self.selBlocksize,:]
398 datasize = self.dataOut.data.shape[1]
396 datasize = self.dataOut.data.shape[1]
399 if datasize < self.selBlocksize:
397 if datasize < self.selBlocksize:
400 buffer = numpy.zeros((self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype = 'complex')
398 buffer = numpy.zeros((self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype = 'complex')
401 buffer[:,:datasize,:] = self.dataOut.data
399 buffer[:,:datasize,:] = self.dataOut.data
402 self.dataOut.data = buffer
400 self.dataOut.data = buffer
403 self.profileIndex = blockIndex
401 self.profileIndex = blockIndex
404
402
405 self.dataOut.flagDataAsBlock = True
403 self.dataOut.flagDataAsBlock = True
406 self.flagIsNewBlock = 1
404 self.flagIsNewBlock = 1
407 self.dataOut.realtime = self.online
405 self.dataOut.realtime = self.online
408
406
409 return self.dataOut.data
407 return self.dataOut.data
410
408
411 def getData(self):
409 def getData(self):
412 """
410 """
413 getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut
411 getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut
414 del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
412 del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
415 en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
413 en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
416 "readNextBlock"
414 "readNextBlock"
417
415
418 Ademas incrementa el contador del buffer "self.profileIndex" en 1.
416 Ademas incrementa el contador del buffer "self.profileIndex" en 1.
419
417
420 Return:
418 Return:
421
419
422 Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
420 Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
423 es igual al total de perfiles leidos desde el archivo.
421 es igual al total de perfiles leidos desde el archivo.
424
422
425 Si self.getByBlock == False:
423 Si self.getByBlock == False:
426
424
427 self.dataOut.data = buffer[:, thisProfile, :]
425 self.dataOut.data = buffer[:, thisProfile, :]
428
426
429 shape = [nChannels, nHeis]
427 shape = [nChannels, nHeis]
430
428
431 Si self.getByBlock == True:
429 Si self.getByBlock == True:
432
430
433 self.dataOut.data = buffer[:, :, :]
431 self.dataOut.data = buffer[:, :, :]
434
432
435 shape = [nChannels, nProfiles, nHeis]
433 shape = [nChannels, nProfiles, nHeis]
436
434
437 Variables afectadas:
435 Variables afectadas:
438 self.dataOut
436 self.dataOut
439 self.profileIndex
437 self.profileIndex
440
438
441 Affected:
439 Affected:
442 self.dataOut
440 self.dataOut
443 self.profileIndex
441 self.profileIndex
444 self.flagDiscontinuousBlock
442 self.flagDiscontinuousBlock
445 self.flagIsNewBlock
443 self.flagIsNewBlock
446 """
444 """
447 if self.flagNoMoreFiles:
445 if self.flagNoMoreFiles:
448 self.dataOut.flagNoData = True
446 self.dataOut.flagNoData = True
449 print 'Process finished'
447 print 'Process finished'
450 return 0
448 return 0
451 self.flagDiscontinuousBlock = 0
449 self.flagDiscontinuousBlock = 0
452 self.flagIsNewBlock = 0
450 self.flagIsNewBlock = 0
453 if self.__hasNotDataInBuffer():
451 if self.__hasNotDataInBuffer():
454 if not( self.readNextBlock() ):
452 if not( self.readNextBlock() ):
455 return 0
453 return 0
456
454
457 self.getFirstHeader()
455 self.getFirstHeader()
458
456
459 self.reshapeData()
457 self.reshapeData()
460 if self.datablock is None:
458 if self.datablock is None:
461 self.dataOut.flagNoData = True
459 self.dataOut.flagNoData = True
462 return 0
460 return 0
463
461
464 if not self.getByBlock:
462 if not self.getByBlock:
465
463
466 """
464 """
467 Return profile by profile
465 Return profile by profile
468
466
469 If nTxs > 1 then one profile is divided by nTxs and number of total
467 If nTxs > 1 then one profile is divided by nTxs and number of total
470 blocks is increased by nTxs (nProfiles *= nTxs)
468 blocks is increased by nTxs (nProfiles *= nTxs)
471 """
469 """
472 self.dataOut.flagDataAsBlock = False
470 self.dataOut.flagDataAsBlock = False
473 self.dataOut.data = self.datablock[:,self.profileIndex,:]
471 self.dataOut.data = self.datablock[:,self.profileIndex,:]
474 self.dataOut.profileIndex = self.profileIndex
472 self.dataOut.profileIndex = self.profileIndex
475
473
476 self.profileIndex += 1
474 self.profileIndex += 1
477
475
478 # elif self.selBlocksize==None or self.selBlocksize==self.dataOut.nProfiles:
476 # elif self.selBlocksize==None or self.selBlocksize==self.dataOut.nProfiles:
479 # """
477 # """
480 # Return all block
478 # Return all block
481 # """
479 # """
482 # self.dataOut.flagDataAsBlock = True
480 # self.dataOut.flagDataAsBlock = True
483 # self.dataOut.data = self.datablock
481 # self.dataOut.data = self.datablock
484 # self.dataOut.profileIndex = self.dataOut.nProfiles - 1
482 # self.dataOut.profileIndex = self.dataOut.nProfiles - 1
485 #
483 #
486 # self.profileIndex = self.dataOut.nProfiles
484 # self.profileIndex = self.dataOut.nProfiles
487
485
488 else:
486 else:
489 """
487 """
490 Return a block
488 Return a block
491 """
489 """
492 if self.selBlocksize == None: self.selBlocksize = self.dataOut.nProfiles
490 if self.selBlocksize == None: self.selBlocksize = self.dataOut.nProfiles
493 if self.selBlocktime != None:
491 if self.selBlocktime != None:
494 if self.dataOut.nCohInt is not None:
492 if self.dataOut.nCohInt is not None:
495 nCohInt = self.dataOut.nCohInt
493 nCohInt = self.dataOut.nCohInt
496 else:
494 else:
497 nCohInt = 1
495 nCohInt = 1
498 self.selBlocksize = int(self.dataOut.nProfiles*round(self.selBlocktime/(nCohInt*self.dataOut.ippSeconds*self.dataOut.nProfiles)))
496 self.selBlocksize = int(self.dataOut.nProfiles*round(self.selBlocktime/(nCohInt*self.dataOut.ippSeconds*self.dataOut.nProfiles)))
499
497
500 self.dataOut.data = self.datablock[:,self.profileIndex:self.profileIndex+self.selBlocksize,:]
498 self.dataOut.data = self.datablock[:,self.profileIndex:self.profileIndex+self.selBlocksize,:]
501 self.profileIndex += self.selBlocksize
499 self.profileIndex += self.selBlocksize
502 datasize = self.dataOut.data.shape[1]
500 datasize = self.dataOut.data.shape[1]
503
501
504 if datasize < self.selBlocksize:
502 if datasize < self.selBlocksize:
505 buffer = numpy.zeros((self.dataOut.data.shape[0],self.selBlocksize,self.dataOut.data.shape[2]), dtype = 'complex')
503 buffer = numpy.zeros((self.dataOut.data.shape[0],self.selBlocksize,self.dataOut.data.shape[2]), dtype = 'complex')
506 buffer[:,:datasize,:] = self.dataOut.data
504 buffer[:,:datasize,:] = self.dataOut.data
507
505
508 while datasize < self.selBlocksize: #Not enough profiles to fill the block
506 while datasize < self.selBlocksize: #Not enough profiles to fill the block
509 if not( self.readNextBlock() ):
507 if not( self.readNextBlock() ):
510 return 0
508 return 0
511 self.getFirstHeader()
509 self.getFirstHeader()
512 self.reshapeData()
510 self.reshapeData()
513 if self.datablock is None:
511 if self.datablock is None:
514 self.dataOut.flagNoData = True
512 self.dataOut.flagNoData = True
515 return 0
513 return 0
516 #stack data
514 #stack data
517 blockIndex = self.selBlocksize - datasize
515 blockIndex = self.selBlocksize - datasize
518 datablock1 = self.datablock[:,:blockIndex,:]
516 datablock1 = self.datablock[:,:blockIndex,:]
519
517
520 buffer[:,datasize:datasize+datablock1.shape[1],:] = datablock1
518 buffer[:,datasize:datasize+datablock1.shape[1],:] = datablock1
521 datasize += datablock1.shape[1]
519 datasize += datablock1.shape[1]
522
520
523 self.dataOut.data = buffer
521 self.dataOut.data = buffer
524 self.profileIndex = blockIndex
522 self.profileIndex = blockIndex
525
523
526 self.dataOut.flagDataAsBlock = True
524 self.dataOut.flagDataAsBlock = True
527 self.dataOut.nProfiles = self.dataOut.data.shape[1]
525 self.dataOut.nProfiles = self.dataOut.data.shape[1]
528
526
529 self.dataOut.flagNoData = False
527 self.dataOut.flagNoData = False
530
528
531 self.getBasicHeader()
529 self.getBasicHeader()
532
530
533 self.dataOut.realtime = self.online
531 self.dataOut.realtime = self.online
534
532
535 return self.dataOut.data
533 return self.dataOut.data
536
534
537 class VoltageWriter(JRODataWriter, Operation):
535 class VoltageWriter(JRODataWriter, Operation):
538 """
536 """
539 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
537 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
540 de los datos siempre se realiza por bloques.
538 de los datos siempre se realiza por bloques.
541 """
539 """
542
540
543 ext = ".r"
541 ext = ".r"
544
542
545 optchar = "D"
543 optchar = "D"
546
544
547 shapeBuffer = None
545 shapeBuffer = None
548
546
549
547
550 def __init__(self, **kwargs):
548 def __init__(self, **kwargs):
551 """
549 """
552 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
550 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
553
551
554 Affected:
552 Affected:
555 self.dataOut
553 self.dataOut
556
554
557 Return: None
555 Return: None
558 """
556 """
559 Operation.__init__(self, **kwargs)
557 Operation.__init__(self, **kwargs)
560
558
561 self.nTotalBlocks = 0
559 self.nTotalBlocks = 0
562
560
563 self.profileIndex = 0
561 self.profileIndex = 0
564
562
565 self.isConfig = False
563 self.isConfig = False
566
564
567 self.fp = None
565 self.fp = None
568
566
569 self.flagIsNewFile = 1
567 self.flagIsNewFile = 1
570
568
571 self.blockIndex = 0
569 self.blockIndex = 0
572
570
573 self.flagIsNewBlock = 0
571 self.flagIsNewBlock = 0
574
572
575 self.setFile = None
573 self.setFile = None
576
574
577 self.dtype = None
575 self.dtype = None
578
576
579 self.path = None
577 self.path = None
580
578
581 self.filename = None
579 self.filename = None
582
580
583 self.basicHeaderObj = BasicHeader(LOCALTIME)
581 self.basicHeaderObj = BasicHeader(LOCALTIME)
584
582
585 self.systemHeaderObj = SystemHeader()
583 self.systemHeaderObj = SystemHeader()
586
584
587 self.radarControllerHeaderObj = RadarControllerHeader()
585 self.radarControllerHeaderObj = RadarControllerHeader()
588
586
589 self.processingHeaderObj = ProcessingHeader()
587 self.processingHeaderObj = ProcessingHeader()
590
588
591 def hasAllDataInBuffer(self):
589 def hasAllDataInBuffer(self):
592 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
590 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
593 return 1
591 return 1
594 return 0
592 return 0
595
593
596
594
597 def setBlockDimension(self):
595 def setBlockDimension(self):
598 """
596 """
599 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
597 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
600
598
601 Affected:
599 Affected:
602 self.shape_spc_Buffer
600 self.shape_spc_Buffer
603 self.shape_cspc_Buffer
601 self.shape_cspc_Buffer
604 self.shape_dc_Buffer
602 self.shape_dc_Buffer
605
603
606 Return: None
604 Return: None
607 """
605 """
608 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
606 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
609 self.processingHeaderObj.nHeights,
607 self.processingHeaderObj.nHeights,
610 self.systemHeaderObj.nChannels)
608 self.systemHeaderObj.nChannels)
611
609
612 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
610 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
613 self.processingHeaderObj.profilesPerBlock,
611 self.processingHeaderObj.profilesPerBlock,
614 self.processingHeaderObj.nHeights),
612 self.processingHeaderObj.nHeights),
615 dtype=numpy.dtype('complex64'))
613 dtype=numpy.dtype('complex64'))
616
614
617 def writeBlock(self):
615 def writeBlock(self):
618 """
616 """
619 Escribe el buffer en el file designado
617 Escribe el buffer en el file designado
620
618
621 Affected:
619 Affected:
622 self.profileIndex
620 self.profileIndex
623 self.flagIsNewFile
621 self.flagIsNewFile
624 self.flagIsNewBlock
622 self.flagIsNewBlock
625 self.nTotalBlocks
623 self.nTotalBlocks
626 self.blockIndex
624 self.blockIndex
627
625
628 Return: None
626 Return: None
629 """
627 """
630 data = numpy.zeros( self.shapeBuffer, self.dtype )
628 data = numpy.zeros( self.shapeBuffer, self.dtype )
631
629
632 junk = numpy.transpose(self.datablock, (1,2,0))
630 junk = numpy.transpose(self.datablock, (1,2,0))
633
631
634 data['real'] = junk.real
632 data['real'] = junk.real
635 data['imag'] = junk.imag
633 data['imag'] = junk.imag
636
634
637 data = data.reshape( (-1) )
635 data = data.reshape( (-1) )
638
636
639 data.tofile( self.fp )
637 data.tofile( self.fp )
640
638
641 self.datablock.fill(0)
639 self.datablock.fill(0)
642
640
643 self.profileIndex = 0
641 self.profileIndex = 0
644 self.flagIsNewFile = 0
642 self.flagIsNewFile = 0
645 self.flagIsNewBlock = 1
643 self.flagIsNewBlock = 1
646
644
647 self.blockIndex += 1
645 self.blockIndex += 1
648 self.nTotalBlocks += 1
646 self.nTotalBlocks += 1
649
647
650 # print "[Writing] Block = %04d" %self.blockIndex
648 # print "[Writing] Block = %04d" %self.blockIndex
651
649
652 def putData(self):
650 def putData(self):
653 """
651 """
654 Setea un bloque de datos y luego los escribe en un file
652 Setea un bloque de datos y luego los escribe en un file
655
653
656 Affected:
654 Affected:
657 self.flagIsNewBlock
655 self.flagIsNewBlock
658 self.profileIndex
656 self.profileIndex
659
657
660 Return:
658 Return:
661 0 : Si no hay data o no hay mas files que puedan escribirse
659 0 : Si no hay data o no hay mas files que puedan escribirse
662 1 : Si se escribio la data de un bloque en un file
660 1 : Si se escribio la data de un bloque en un file
663 """
661 """
664 if self.dataOut.flagNoData:
662 if self.dataOut.flagNoData:
665 return 0
663 return 0
666
664
667 self.flagIsNewBlock = 0
665 self.flagIsNewBlock = 0
668
666
669 if self.dataOut.flagDiscontinuousBlock:
667 if self.dataOut.flagDiscontinuousBlock:
670 self.datablock.fill(0)
668 self.datablock.fill(0)
671 self.profileIndex = 0
669 self.profileIndex = 0
672 self.setNextFile()
670 self.setNextFile()
673
671
674 if self.profileIndex == 0:
672 if self.profileIndex == 0:
675 self.setBasicHeader()
673 self.setBasicHeader()
676
674
677 self.datablock[:,self.profileIndex,:] = self.dataOut.data
675 self.datablock[:,self.profileIndex,:] = self.dataOut.data
678
676
679 self.profileIndex += 1
677 self.profileIndex += 1
680
678
681 if self.hasAllDataInBuffer():
679 if self.hasAllDataInBuffer():
682 #if self.flagIsNewFile:
680 #if self.flagIsNewFile:
683 self.writeNextBlock()
681 self.writeNextBlock()
684 # self.setFirstHeader()
682 # self.setFirstHeader()
685
683
686 return 1
684 return 1
687
685
688 def __getBlockSize(self):
686 def __getBlockSize(self):
689 '''
687 '''
690 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
688 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
691 '''
689 '''
692
690
693 dtype_width = self.getDtypeWidth()
691 dtype_width = self.getDtypeWidth()
694
692
695 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * dtype_width * 2)
693 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * dtype_width * 2)
696
694
697 return blocksize
695 return blocksize
698
696
699 def setFirstHeader(self):
697 def setFirstHeader(self):
700
698
701 """
699 """
702 Obtiene una copia del First Header
700 Obtiene una copia del First Header
703
701
704 Affected:
702 Affected:
705 self.systemHeaderObj
703 self.systemHeaderObj
706 self.radarControllerHeaderObj
704 self.radarControllerHeaderObj
707 self.dtype
705 self.dtype
708
706
709 Return:
707 Return:
710 None
708 None
711 """
709 """
712
710
713 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
711 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
714 self.systemHeaderObj.nChannels = self.dataOut.nChannels
712 self.systemHeaderObj.nChannels = self.dataOut.nChannels
715 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
713 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
716
714
717 self.processingHeaderObj.dtype = 0 # Voltage
715 self.processingHeaderObj.dtype = 0 # Voltage
718 self.processingHeaderObj.blockSize = self.__getBlockSize()
716 self.processingHeaderObj.blockSize = self.__getBlockSize()
719 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
717 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
720 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
718 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
721 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
719 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
722 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
720 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
723 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
721 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
724 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
722 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
725
723
726 if self.dataOut.code is not None:
724 if self.dataOut.code is not None:
727 self.processingHeaderObj.code = self.dataOut.code
725 self.processingHeaderObj.code = self.dataOut.code
728 self.processingHeaderObj.nCode = self.dataOut.nCode
726 self.processingHeaderObj.nCode = self.dataOut.nCode
729 self.processingHeaderObj.nBaud = self.dataOut.nBaud
727 self.processingHeaderObj.nBaud = self.dataOut.nBaud
730
728
731 if self.processingHeaderObj.nWindows != 0:
729 if self.processingHeaderObj.nWindows != 0:
732 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
730 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
733 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
731 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
734 self.processingHeaderObj.nHeights = self.dataOut.nHeights
732 self.processingHeaderObj.nHeights = self.dataOut.nHeights
735 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
733 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
736
734
737 self.processingHeaderObj.processFlags = self.getProcessFlags()
735 self.processingHeaderObj.processFlags = self.getProcessFlags()
738
736
739 self.setBasicHeader()
737 self.setBasicHeader()
@@ -1,1285 +1,1283
1 import sys
1 import sys
2 import numpy
2 import numpy
3 from scipy import interpolate
3 from scipy import interpolate
4
4
5 from jroproc_base import ProcessingUnit, Operation
5 from jroproc_base import ProcessingUnit, Operation
6 from schainpy.model.data.jrodata import Voltage
6 from schainpy.model.data.jrodata import Voltage
7
7
8 class VoltageProc(ProcessingUnit):
8 class VoltageProc(ProcessingUnit):
9
9
10
10
11 def __init__(self, **kwargs):
11 def __init__(self, **kwargs):
12
12
13 ProcessingUnit.__init__(self, **kwargs)
13 ProcessingUnit.__init__(self, **kwargs)
14
14
15 # self.objectDict = {}
15 # self.objectDict = {}
16 self.dataOut = Voltage()
16 self.dataOut = Voltage()
17 self.flip = 1
17 self.flip = 1
18
18
19 def run(self):
19 def run(self):
20 if self.dataIn.type == 'AMISR':
20 if self.dataIn.type == 'AMISR':
21 self.__updateObjFromAmisrInput()
21 self.__updateObjFromAmisrInput()
22
22
23 if self.dataIn.type == 'Voltage':
23 if self.dataIn.type == 'Voltage':
24 self.dataOut.copy(self.dataIn)
24 self.dataOut.copy(self.dataIn)
25
25
26 # self.dataOut.copy(self.dataIn)
26 # self.dataOut.copy(self.dataIn)
27
27
28 def __updateObjFromAmisrInput(self):
28 def __updateObjFromAmisrInput(self):
29
29
30 self.dataOut.timeZone = self.dataIn.timeZone
30 self.dataOut.timeZone = self.dataIn.timeZone
31 self.dataOut.dstFlag = self.dataIn.dstFlag
31 self.dataOut.dstFlag = self.dataIn.dstFlag
32 self.dataOut.errorCount = self.dataIn.errorCount
32 self.dataOut.errorCount = self.dataIn.errorCount
33 self.dataOut.useLocalTime = self.dataIn.useLocalTime
33 self.dataOut.useLocalTime = self.dataIn.useLocalTime
34
34
35 self.dataOut.flagNoData = self.dataIn.flagNoData
35 self.dataOut.flagNoData = self.dataIn.flagNoData
36 self.dataOut.data = self.dataIn.data
36 self.dataOut.data = self.dataIn.data
37 self.dataOut.utctime = self.dataIn.utctime
37 self.dataOut.utctime = self.dataIn.utctime
38 self.dataOut.channelList = self.dataIn.channelList
38 self.dataOut.channelList = self.dataIn.channelList
39 # self.dataOut.timeInterval = self.dataIn.timeInterval
39 # self.dataOut.timeInterval = self.dataIn.timeInterval
40 self.dataOut.heightList = self.dataIn.heightList
40 self.dataOut.heightList = self.dataIn.heightList
41 self.dataOut.nProfiles = self.dataIn.nProfiles
41 self.dataOut.nProfiles = self.dataIn.nProfiles
42
42
43 self.dataOut.nCohInt = self.dataIn.nCohInt
43 self.dataOut.nCohInt = self.dataIn.nCohInt
44 self.dataOut.ippSeconds = self.dataIn.ippSeconds
44 self.dataOut.ippSeconds = self.dataIn.ippSeconds
45 self.dataOut.frequency = self.dataIn.frequency
45 self.dataOut.frequency = self.dataIn.frequency
46
46
47 self.dataOut.azimuth = self.dataIn.azimuth
47 self.dataOut.azimuth = self.dataIn.azimuth
48 self.dataOut.zenith = self.dataIn.zenith
48 self.dataOut.zenith = self.dataIn.zenith
49
49
50 self.dataOut.beam.codeList = self.dataIn.beam.codeList
50 self.dataOut.beam.codeList = self.dataIn.beam.codeList
51 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
51 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
52 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
52 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
53 #
53 #
54 # pass#
54 # pass#
55 #
55 #
56 # def init(self):
56 # def init(self):
57 #
57 #
58 #
58 #
59 # if self.dataIn.type == 'AMISR':
59 # if self.dataIn.type == 'AMISR':
60 # self.__updateObjFromAmisrInput()
60 # self.__updateObjFromAmisrInput()
61 #
61 #
62 # if self.dataIn.type == 'Voltage':
62 # if self.dataIn.type == 'Voltage':
63 # self.dataOut.copy(self.dataIn)
63 # self.dataOut.copy(self.dataIn)
64 # # No necesita copiar en cada init() los atributos de dataIn
64 # # No necesita copiar en cada init() los atributos de dataIn
65 # # la copia deberia hacerse por cada nuevo bloque de datos
65 # # la copia deberia hacerse por cada nuevo bloque de datos
66
66
67 def selectChannels(self, channelList):
67 def selectChannels(self, channelList):
68
68
69 channelIndexList = []
69 channelIndexList = []
70
70
71 for channel in channelList:
71 for channel in channelList:
72 if channel not in self.dataOut.channelList:
72 if channel not in self.dataOut.channelList:
73 raise ValueError, "Channel %d is not in %s" %(channel, str(self.dataOut.channelList))
73 raise ValueError, "Channel %d is not in %s" %(channel, str(self.dataOut.channelList))
74
74
75 index = self.dataOut.channelList.index(channel)
75 index = self.dataOut.channelList.index(channel)
76 channelIndexList.append(index)
76 channelIndexList.append(index)
77
77
78 self.selectChannelsByIndex(channelIndexList)
78 self.selectChannelsByIndex(channelIndexList)
79
79
80 def selectChannelsByIndex(self, channelIndexList):
80 def selectChannelsByIndex(self, channelIndexList):
81 """
81 """
82 Selecciona un bloque de datos en base a canales segun el channelIndexList
82 Selecciona un bloque de datos en base a canales segun el channelIndexList
83
83
84 Input:
84 Input:
85 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
85 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
86
86
87 Affected:
87 Affected:
88 self.dataOut.data
88 self.dataOut.data
89 self.dataOut.channelIndexList
89 self.dataOut.channelIndexList
90 self.dataOut.nChannels
90 self.dataOut.nChannels
91 self.dataOut.m_ProcessingHeader.totalSpectra
91 self.dataOut.m_ProcessingHeader.totalSpectra
92 self.dataOut.systemHeaderObj.numChannels
92 self.dataOut.systemHeaderObj.numChannels
93 self.dataOut.m_ProcessingHeader.blockSize
93 self.dataOut.m_ProcessingHeader.blockSize
94
94
95 Return:
95 Return:
96 None
96 None
97 """
97 """
98
98
99 for channelIndex in channelIndexList:
99 for channelIndex in channelIndexList:
100 if channelIndex not in self.dataOut.channelIndexList:
100 if channelIndex not in self.dataOut.channelIndexList:
101 print channelIndexList
101 print channelIndexList
102 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
102 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
103
103
104 if self.dataOut.flagDataAsBlock:
104 if self.dataOut.flagDataAsBlock:
105 """
105 """
106 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
106 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
107 """
107 """
108 data = self.dataOut.data[channelIndexList,:,:]
108 data = self.dataOut.data[channelIndexList,:,:]
109 else:
109 else:
110 data = self.dataOut.data[channelIndexList,:]
110 data = self.dataOut.data[channelIndexList,:]
111
111
112 self.dataOut.data = data
112 self.dataOut.data = data
113 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
113 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
114 # self.dataOut.nChannels = nChannels
114 # self.dataOut.nChannels = nChannels
115
115
116 return 1
116 return 1
117
117
118 def selectHeights(self, minHei=None, maxHei=None):
118 def selectHeights(self, minHei=None, maxHei=None):
119 """
119 """
120 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
120 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
121 minHei <= height <= maxHei
121 minHei <= height <= maxHei
122
122
123 Input:
123 Input:
124 minHei : valor minimo de altura a considerar
124 minHei : valor minimo de altura a considerar
125 maxHei : valor maximo de altura a considerar
125 maxHei : valor maximo de altura a considerar
126
126
127 Affected:
127 Affected:
128 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
128 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
129
129
130 Return:
130 Return:
131 1 si el metodo se ejecuto con exito caso contrario devuelve 0
131 1 si el metodo se ejecuto con exito caso contrario devuelve 0
132 """
132 """
133
133
134 if minHei == None:
134 if minHei == None:
135 minHei = self.dataOut.heightList[0]
135 minHei = self.dataOut.heightList[0]
136
136
137 if maxHei == None:
137 if maxHei == None:
138 maxHei = self.dataOut.heightList[-1]
138 maxHei = self.dataOut.heightList[-1]
139
139
140 if (minHei < self.dataOut.heightList[0]):
140 if (minHei < self.dataOut.heightList[0]):
141 minHei = self.dataOut.heightList[0]
141 minHei = self.dataOut.heightList[0]
142
142
143 if (maxHei > self.dataOut.heightList[-1]):
143 if (maxHei > self.dataOut.heightList[-1]):
144 maxHei = self.dataOut.heightList[-1]
144 maxHei = self.dataOut.heightList[-1]
145
145
146 minIndex = 0
146 minIndex = 0
147 maxIndex = 0
147 maxIndex = 0
148 heights = self.dataOut.heightList
148 heights = self.dataOut.heightList
149
149
150 inda = numpy.where(heights >= minHei)
150 inda = numpy.where(heights >= minHei)
151 indb = numpy.where(heights <= maxHei)
151 indb = numpy.where(heights <= maxHei)
152
152
153 try:
153 try:
154 minIndex = inda[0][0]
154 minIndex = inda[0][0]
155 except:
155 except:
156 minIndex = 0
156 minIndex = 0
157
157
158 try:
158 try:
159 maxIndex = indb[0][-1]
159 maxIndex = indb[0][-1]
160 except:
160 except:
161 maxIndex = len(heights)
161 maxIndex = len(heights)
162
162
163 self.selectHeightsByIndex(minIndex, maxIndex)
163 self.selectHeightsByIndex(minIndex, maxIndex)
164
164
165 return 1
165 return 1
166
166
167
167
168 def selectHeightsByIndex(self, minIndex, maxIndex):
168 def selectHeightsByIndex(self, minIndex, maxIndex):
169 """
169 """
170 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
170 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
171 minIndex <= index <= maxIndex
171 minIndex <= index <= maxIndex
172
172
173 Input:
173 Input:
174 minIndex : valor de indice minimo de altura a considerar
174 minIndex : valor de indice minimo de altura a considerar
175 maxIndex : valor de indice maximo de altura a considerar
175 maxIndex : valor de indice maximo de altura a considerar
176
176
177 Affected:
177 Affected:
178 self.dataOut.data
178 self.dataOut.data
179 self.dataOut.heightList
179 self.dataOut.heightList
180
180
181 Return:
181 Return:
182 1 si el metodo se ejecuto con exito caso contrario devuelve 0
182 1 si el metodo se ejecuto con exito caso contrario devuelve 0
183 """
183 """
184
184
185 if (minIndex < 0) or (minIndex > maxIndex):
185 if (minIndex < 0) or (minIndex > maxIndex):
186 raise ValueError, "Height index range (%d,%d) is not valid" % (minIndex, maxIndex)
186 raise ValueError, "Height index range (%d,%d) is not valid" % (minIndex, maxIndex)
187
187
188 if (maxIndex >= self.dataOut.nHeights):
188 if (maxIndex >= self.dataOut.nHeights):
189 maxIndex = self.dataOut.nHeights
189 maxIndex = self.dataOut.nHeights
190
190
191 #voltage
191 #voltage
192 if self.dataOut.flagDataAsBlock:
192 if self.dataOut.flagDataAsBlock:
193 """
193 """
194 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
194 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
195 """
195 """
196 data = self.dataOut.data[:,:, minIndex:maxIndex]
196 data = self.dataOut.data[:,:, minIndex:maxIndex]
197 else:
197 else:
198 data = self.dataOut.data[:, minIndex:maxIndex]
198 data = self.dataOut.data[:, minIndex:maxIndex]
199
199
200 # firstHeight = self.dataOut.heightList[minIndex]
200 # firstHeight = self.dataOut.heightList[minIndex]
201
201
202 self.dataOut.data = data
202 self.dataOut.data = data
203 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
203 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
204
204
205 if self.dataOut.nHeights <= 1:
205 if self.dataOut.nHeights <= 1:
206 raise ValueError, "selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights)
206 raise ValueError, "selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights)
207
207
208 return 1
208 return 1
209
209
210
210
211 def filterByHeights(self, window):
211 def filterByHeights(self, window):
212
212
213 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
213 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
214
214
215 if window == None:
215 if window == None:
216 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
216 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
217
217
218 newdelta = deltaHeight * window
218 newdelta = deltaHeight * window
219 r = self.dataOut.nHeights % window
219 r = self.dataOut.nHeights % window
220 newheights = (self.dataOut.nHeights-r)/window
220 newheights = (self.dataOut.nHeights-r)/window
221
221
222 if newheights <= 1:
222 if newheights <= 1:
223 raise ValueError, "filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window)
223 raise ValueError, "filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window)
224
224
225 if self.dataOut.flagDataAsBlock:
225 if self.dataOut.flagDataAsBlock:
226 """
226 """
227 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
227 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
228 """
228 """
229 buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r]
229 buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r]
230 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window)
230 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window)
231 buffer = numpy.sum(buffer,3)
231 buffer = numpy.sum(buffer,3)
232
232
233 else:
233 else:
234 buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r]
234 buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r]
235 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window)
235 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window)
236 buffer = numpy.sum(buffer,2)
236 buffer = numpy.sum(buffer,2)
237
237
238 self.dataOut.data = buffer
238 self.dataOut.data = buffer
239 self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta
239 self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta
240 self.dataOut.windowOfFilter = window
240 self.dataOut.windowOfFilter = window
241
241
242 def setH0(self, h0, deltaHeight = None):
242 def setH0(self, h0, deltaHeight = None):
243
243
244 if not deltaHeight:
244 if not deltaHeight:
245 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
245 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
246
246
247 nHeights = self.dataOut.nHeights
247 nHeights = self.dataOut.nHeights
248
248
249 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
249 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
250
250
251 self.dataOut.heightList = newHeiRange
251 self.dataOut.heightList = newHeiRange
252
252
253 def deFlip(self, channelList = []):
253 def deFlip(self, channelList = []):
254
254
255 data = self.dataOut.data.copy()
255 data = self.dataOut.data.copy()
256
256
257 if self.dataOut.flagDataAsBlock:
257 if self.dataOut.flagDataAsBlock:
258 flip = self.flip
258 flip = self.flip
259 profileList = range(self.dataOut.nProfiles)
259 profileList = range(self.dataOut.nProfiles)
260
260
261 if not channelList:
261 if not channelList:
262 for thisProfile in profileList:
262 for thisProfile in profileList:
263 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
263 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
264 flip *= -1.0
264 flip *= -1.0
265 else:
265 else:
266 for thisChannel in channelList:
266 for thisChannel in channelList:
267 if thisChannel not in self.dataOut.channelList:
267 if thisChannel not in self.dataOut.channelList:
268 continue
268 continue
269
269
270 for thisProfile in profileList:
270 for thisProfile in profileList:
271 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
271 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
272 flip *= -1.0
272 flip *= -1.0
273
273
274 self.flip = flip
274 self.flip = flip
275
275
276 else:
276 else:
277 if not channelList:
277 if not channelList:
278 data[:,:] = data[:,:]*self.flip
278 data[:,:] = data[:,:]*self.flip
279 else:
279 else:
280 for thisChannel in channelList:
280 for thisChannel in channelList:
281 if thisChannel not in self.dataOut.channelList:
281 if thisChannel not in self.dataOut.channelList:
282 continue
282 continue
283
283
284 data[thisChannel,:] = data[thisChannel,:]*self.flip
284 data[thisChannel,:] = data[thisChannel,:]*self.flip
285
285
286 self.flip *= -1.
286 self.flip *= -1.
287
287
288 self.dataOut.data = data
288 self.dataOut.data = data
289
289
290 def setRadarFrequency(self, frequency=None):
290 def setRadarFrequency(self, frequency=None):
291
291
292 if frequency != None:
292 if frequency != None:
293 self.dataOut.frequency = frequency
293 self.dataOut.frequency = frequency
294
294
295 return 1
295 return 1
296
296
297 def interpolateHeights(self, topLim, botLim):
297 def interpolateHeights(self, topLim, botLim):
298 #69 al 72 para julia
298 #69 al 72 para julia
299 #82-84 para meteoros
299 #82-84 para meteoros
300 if len(numpy.shape(self.dataOut.data))==2:
300 if len(numpy.shape(self.dataOut.data))==2:
301 sampInterp = (self.dataOut.data[:,botLim-1] + self.dataOut.data[:,topLim+1])/2
301 sampInterp = (self.dataOut.data[:,botLim-1] + self.dataOut.data[:,topLim+1])/2
302 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
302 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
303 #self.dataOut.data[:,botLim:limSup+1] = sampInterp
303 #self.dataOut.data[:,botLim:limSup+1] = sampInterp
304 self.dataOut.data[:,botLim:topLim+1] = sampInterp
304 self.dataOut.data[:,botLim:topLim+1] = sampInterp
305 else:
305 else:
306 nHeights = self.dataOut.data.shape[2]
306 nHeights = self.dataOut.data.shape[2]
307 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
307 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
308 y = self.dataOut.data[:,:,range(botLim)+range(topLim+1,nHeights)]
308 y = self.dataOut.data[:,:,range(botLim)+range(topLim+1,nHeights)]
309 f = interpolate.interp1d(x, y, axis = 2)
309 f = interpolate.interp1d(x, y, axis = 2)
310 xnew = numpy.arange(botLim,topLim+1)
310 xnew = numpy.arange(botLim,topLim+1)
311 ynew = f(xnew)
311 ynew = f(xnew)
312
312
313 self.dataOut.data[:,:,botLim:topLim+1] = ynew
313 self.dataOut.data[:,:,botLim:topLim+1] = ynew
314
314
315 # import collections
315 # import collections
316
316
317 class CohInt(Operation):
317 class CohInt(Operation):
318
318
319 isConfig = False
319 isConfig = False
320
320
321 __profIndex = 0
321 __profIndex = 0
322 __withOverapping = False
322 __withOverapping = False
323
323
324 __byTime = False
324 __byTime = False
325 __initime = None
325 __initime = None
326 __lastdatatime = None
326 __lastdatatime = None
327 __integrationtime = None
327 __integrationtime = None
328
328
329 __buffer = None
329 __buffer = None
330
330
331 __dataReady = False
331 __dataReady = False
332
332
333 n = None
333 n = None
334
334
335
335
336 def __init__(self, **kwargs):
336 def __init__(self, **kwargs):
337
337
338 Operation.__init__(self, **kwargs)
338 Operation.__init__(self, **kwargs)
339
339
340 # self.isConfig = False
340 # self.isConfig = False
341
341
342 def setup(self, n=None, timeInterval=None, overlapping=False, byblock=False):
342 def setup(self, n=None, timeInterval=None, overlapping=False, byblock=False):
343 """
343 """
344 Set the parameters of the integration class.
344 Set the parameters of the integration class.
345
345
346 Inputs:
346 Inputs:
347
347
348 n : Number of coherent integrations
348 n : Number of coherent integrations
349 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
349 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
350 overlapping :
350 overlapping :
351
351
352 """
352 """
353
353
354 self.__initime = None
354 self.__initime = None
355 self.__lastdatatime = 0
355 self.__lastdatatime = 0
356 self.__buffer = None
356 self.__buffer = None
357 self.__dataReady = False
357 self.__dataReady = False
358 self.byblock = byblock
358 self.byblock = byblock
359
359
360 if n == None and timeInterval == None:
360 if n == None and timeInterval == None:
361 raise ValueError, "n or timeInterval should be specified ..."
361 raise ValueError, "n or timeInterval should be specified ..."
362
362
363 if n != None:
363 if n != None:
364 self.n = n
364 self.n = n
365 self.__byTime = False
365 self.__byTime = False
366 else:
366 else:
367 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
367 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
368 self.n = 9999
368 self.n = 9999
369 self.__byTime = True
369 self.__byTime = True
370
370
371 if overlapping:
371 if overlapping:
372 self.__withOverapping = True
372 self.__withOverapping = True
373 self.__buffer = None
373 self.__buffer = None
374 else:
374 else:
375 self.__withOverapping = False
375 self.__withOverapping = False
376 self.__buffer = 0
376 self.__buffer = 0
377
377
378 self.__profIndex = 0
378 self.__profIndex = 0
379
379
380 def putData(self, data):
380 def putData(self, data):
381
381
382 """
382 """
383 Add a profile to the __buffer and increase in one the __profileIndex
383 Add a profile to the __buffer and increase in one the __profileIndex
384
384
385 """
385 """
386
386
387 if not self.__withOverapping:
387 if not self.__withOverapping:
388 self.__buffer += data.copy()
388 self.__buffer += data.copy()
389 self.__profIndex += 1
389 self.__profIndex += 1
390 return
390 return
391
391
392 #Overlapping data
392 #Overlapping data
393 nChannels, nHeis = data.shape
393 nChannels, nHeis = data.shape
394 data = numpy.reshape(data, (1, nChannels, nHeis))
394 data = numpy.reshape(data, (1, nChannels, nHeis))
395
395
396 #If the buffer is empty then it takes the data value
396 #If the buffer is empty then it takes the data value
397 if self.__buffer is None:
397 if self.__buffer is None:
398 self.__buffer = data
398 self.__buffer = data
399 self.__profIndex += 1
399 self.__profIndex += 1
400 return
400 return
401
401
402 #If the buffer length is lower than n then stakcing the data value
402 #If the buffer length is lower than n then stakcing the data value
403 if self.__profIndex < self.n:
403 if self.__profIndex < self.n:
404 self.__buffer = numpy.vstack((self.__buffer, data))
404 self.__buffer = numpy.vstack((self.__buffer, data))
405 self.__profIndex += 1
405 self.__profIndex += 1
406 return
406 return
407
407
408 #If the buffer length is equal to n then replacing the last buffer value with the data value
408 #If the buffer length is equal to n then replacing the last buffer value with the data value
409 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
409 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
410 self.__buffer[self.n-1] = data
410 self.__buffer[self.n-1] = data
411 self.__profIndex = self.n
411 self.__profIndex = self.n
412 return
412 return
413
413
414
414
415 def pushData(self):
415 def pushData(self):
416 """
416 """
417 Return the sum of the last profiles and the profiles used in the sum.
417 Return the sum of the last profiles and the profiles used in the sum.
418
418
419 Affected:
419 Affected:
420
420
421 self.__profileIndex
421 self.__profileIndex
422
422
423 """
423 """
424
424
425 if not self.__withOverapping:
425 if not self.__withOverapping:
426 data = self.__buffer
426 data = self.__buffer
427 n = self.__profIndex
427 n = self.__profIndex
428
428
429 self.__buffer = 0
429 self.__buffer = 0
430 self.__profIndex = 0
430 self.__profIndex = 0
431
431
432 return data, n
432 return data, n
433
433
434 #Integration with Overlapping
434 #Integration with Overlapping
435 data = numpy.sum(self.__buffer, axis=0)
435 data = numpy.sum(self.__buffer, axis=0)
436 n = self.__profIndex
436 n = self.__profIndex
437
437
438 return data, n
438 return data, n
439
439
440 def byProfiles(self, data):
440 def byProfiles(self, data):
441
441
442 self.__dataReady = False
442 self.__dataReady = False
443 avgdata = None
443 avgdata = None
444 # n = None
444 # n = None
445
445
446 self.putData(data)
446 self.putData(data)
447
447
448 if self.__profIndex == self.n:
448 if self.__profIndex == self.n:
449
449
450 avgdata, n = self.pushData()
450 avgdata, n = self.pushData()
451 self.__dataReady = True
451 self.__dataReady = True
452
452
453 return avgdata
453 return avgdata
454
454
455 def byTime(self, data, datatime):
455 def byTime(self, data, datatime):
456
456
457 self.__dataReady = False
457 self.__dataReady = False
458 avgdata = None
458 avgdata = None
459 n = None
459 n = None
460
460
461 self.putData(data)
461 self.putData(data)
462
462
463 if (datatime - self.__initime) >= self.__integrationtime:
463 if (datatime - self.__initime) >= self.__integrationtime:
464 avgdata, n = self.pushData()
464 avgdata, n = self.pushData()
465 self.n = n
465 self.n = n
466 self.__dataReady = True
466 self.__dataReady = True
467
467
468 return avgdata
468 return avgdata
469
469
470 def integrate(self, data, datatime=None):
470 def integrate(self, data, datatime=None):
471
471
472 if self.__initime == None:
472 if self.__initime == None:
473 self.__initime = datatime
473 self.__initime = datatime
474
474
475 if self.__byTime:
475 if self.__byTime:
476 avgdata = self.byTime(data, datatime)
476 avgdata = self.byTime(data, datatime)
477 else:
477 else:
478 avgdata = self.byProfiles(data)
478 avgdata = self.byProfiles(data)
479
479
480
480
481 self.__lastdatatime = datatime
481 self.__lastdatatime = datatime
482
482
483 if avgdata is None:
483 if avgdata is None:
484 return None, None
484 return None, None
485
485
486 avgdatatime = self.__initime
486 avgdatatime = self.__initime
487
487
488 deltatime = datatime -self.__lastdatatime
488 deltatime = datatime -self.__lastdatatime
489
489
490 if not self.__withOverapping:
490 if not self.__withOverapping:
491 self.__initime = datatime
491 self.__initime = datatime
492 else:
492 else:
493 self.__initime += deltatime
493 self.__initime += deltatime
494
494
495 return avgdata, avgdatatime
495 return avgdata, avgdatatime
496
496
497 def integrateByBlock(self, dataOut):
497 def integrateByBlock(self, dataOut):
498
498
499 times = int(dataOut.data.shape[1]/self.n)
499 times = int(dataOut.data.shape[1]/self.n)
500 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
500 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
501
501
502 id_min = 0
502 id_min = 0
503 id_max = self.n
503 id_max = self.n
504
504
505 for i in range(times):
505 for i in range(times):
506 junk = dataOut.data[:,id_min:id_max,:]
506 junk = dataOut.data[:,id_min:id_max,:]
507 avgdata[:,i,:] = junk.sum(axis=1)
507 avgdata[:,i,:] = junk.sum(axis=1)
508 id_min += self.n
508 id_min += self.n
509 id_max += self.n
509 id_max += self.n
510
510
511 timeInterval = dataOut.ippSeconds*self.n
511 timeInterval = dataOut.ippSeconds*self.n
512 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
512 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
513 self.__dataReady = True
513 self.__dataReady = True
514 return avgdata, avgdatatime
514 return avgdata, avgdatatime
515
515
516
516
517 def run(self, dataOut, n=None, timeInterval=None, overlapping=False, byblock=False, **kwargs):
517 def run(self, dataOut, n=None, timeInterval=None, overlapping=False, byblock=False, **kwargs):
518 if not self.isConfig:
518 if not self.isConfig:
519 self.setup(n=n, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
519 self.setup(n=n, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
520 self.isConfig = True
520 self.isConfig = True
521
521
522 if dataOut.flagDataAsBlock:
522 if dataOut.flagDataAsBlock:
523 """
523 """
524 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
524 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
525 """
525 """
526 avgdata, avgdatatime = self.integrateByBlock(dataOut)
526 avgdata, avgdatatime = self.integrateByBlock(dataOut)
527 dataOut.nProfiles /= self.n
527 dataOut.nProfiles /= self.n
528 else:
528 else:
529 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
529 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
530
530
531 # dataOut.timeInterval *= n
531 # dataOut.timeInterval *= n
532 dataOut.flagNoData = True
532 dataOut.flagNoData = True
533
533
534 if self.__dataReady:
534 if self.__dataReady:
535 dataOut.data = avgdata
535 dataOut.data = avgdata
536 dataOut.nCohInt *= self.n
536 dataOut.nCohInt *= self.n
537 dataOut.utctime = avgdatatime
537 dataOut.utctime = avgdatatime
538 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
538 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
539 dataOut.flagNoData = False
539 dataOut.flagNoData = False
540
540
541 class Decoder(Operation):
541 class Decoder(Operation):
542
542
543 isConfig = False
543 isConfig = False
544 __profIndex = 0
544 __profIndex = 0
545
545
546 code = None
546 code = None
547
547
548 nCode = None
548 nCode = None
549 nBaud = None
549 nBaud = None
550
550
551
551
552 def __init__(self, **kwargs):
552 def __init__(self, **kwargs):
553
553
554 Operation.__init__(self, **kwargs)
554 Operation.__init__(self, **kwargs)
555
555
556 self.times = None
556 self.times = None
557 self.osamp = None
557 self.osamp = None
558 # self.__setValues = False
558 # self.__setValues = False
559 self.isConfig = False
559 self.isConfig = False
560
560
561 def setup(self, code, osamp, dataOut):
561 def setup(self, code, osamp, dataOut):
562
562
563 self.__profIndex = 0
563 self.__profIndex = 0
564
564
565 self.code = code
565 self.code = code
566
566
567 self.nCode = len(code)
567 self.nCode = len(code)
568 self.nBaud = len(code[0])
568 self.nBaud = len(code[0])
569
569
570 if (osamp != None) and (osamp >1):
570 if (osamp != None) and (osamp >1):
571 self.osamp = osamp
571 self.osamp = osamp
572 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
572 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
573 self.nBaud = self.nBaud*self.osamp
573 self.nBaud = self.nBaud*self.osamp
574
574
575 self.__nChannels = dataOut.nChannels
575 self.__nChannels = dataOut.nChannels
576 self.__nProfiles = dataOut.nProfiles
576 self.__nProfiles = dataOut.nProfiles
577 self.__nHeis = dataOut.nHeights
577 self.__nHeis = dataOut.nHeights
578
578
579 if self.__nHeis < self.nBaud:
579 if self.__nHeis < self.nBaud:
580 raise ValueError, 'Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud)
580 raise ValueError, 'Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud)
581
581
582 #Frequency
582 #Frequency
583 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
583 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
584
584
585 __codeBuffer[:,0:self.nBaud] = self.code
585 __codeBuffer[:,0:self.nBaud] = self.code
586
586
587 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
587 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
588
588
589 if dataOut.flagDataAsBlock:
589 if dataOut.flagDataAsBlock:
590
590
591 self.ndatadec = self.__nHeis #- self.nBaud + 1
591 self.ndatadec = self.__nHeis #- self.nBaud + 1
592
592
593 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
593 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
594
594
595 else:
595 else:
596
596
597 #Time
597 #Time
598 self.ndatadec = self.__nHeis #- self.nBaud + 1
598 self.ndatadec = self.__nHeis #- self.nBaud + 1
599
599
600 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
600 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
601
601
602 def __convolutionInFreq(self, data):
602 def __convolutionInFreq(self, data):
603
603
604 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
604 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
605
605
606 fft_data = numpy.fft.fft(data, axis=1)
606 fft_data = numpy.fft.fft(data, axis=1)
607
607
608 conv = fft_data*fft_code
608 conv = fft_data*fft_code
609
609
610 data = numpy.fft.ifft(conv,axis=1)
610 data = numpy.fft.ifft(conv,axis=1)
611
611
612 return data
612 return data
613
613
614 def __convolutionInFreqOpt(self, data):
614 def __convolutionInFreqOpt(self, data):
615
615
616 raise NotImplementedError
616 raise NotImplementedError
617
617
618 def __convolutionInTime(self, data):
618 def __convolutionInTime(self, data):
619
619
620 code = self.code[self.__profIndex]
620 code = self.code[self.__profIndex]
621
621
622 for i in range(self.__nChannels):
622 for i in range(self.__nChannels):
623 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
623 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
624
624
625 return self.datadecTime
625 return self.datadecTime
626
626
627 def __convolutionByBlockInTime(self, data):
627 def __convolutionByBlockInTime(self, data):
628
628
629 repetitions = self.__nProfiles / self.nCode
629 repetitions = self.__nProfiles / self.nCode
630
630
631 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
631 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
632 junk = junk.flatten()
632 junk = junk.flatten()
633 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
633 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
634
634
635 for i in range(self.__nChannels):
635 for i in range(self.__nChannels):
636 for j in range(self.__nProfiles):
636 for j in range(self.__nProfiles):
637 print self.datadecTime[i,j,:].shape
638 print numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:].shape
639 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
637 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
640
638
641 return self.datadecTime
639 return self.datadecTime
642
640
643 def __convolutionByBlockInFreq(self, data):
641 def __convolutionByBlockInFreq(self, data):
644
642
645 raise NotImplementedError, "Decoder by frequency fro Blocks not implemented"
643 raise NotImplementedError, "Decoder by frequency fro Blocks not implemented"
646
644
647
645
648 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
646 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
649
647
650 fft_data = numpy.fft.fft(data, axis=2)
648 fft_data = numpy.fft.fft(data, axis=2)
651
649
652 conv = fft_data*fft_code
650 conv = fft_data*fft_code
653
651
654 data = numpy.fft.ifft(conv,axis=2)
652 data = numpy.fft.ifft(conv,axis=2)
655
653
656 return data
654 return data
657
655
658 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
656 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
659
657
660 if dataOut.flagDecodeData:
658 if dataOut.flagDecodeData:
661 print "This data is already decoded, recoding again ..."
659 print "This data is already decoded, recoding again ..."
662
660
663 if not self.isConfig:
661 if not self.isConfig:
664
662
665 if code is None:
663 if code is None:
666 if dataOut.code is None:
664 if dataOut.code is None:
667 raise ValueError, "Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type
665 raise ValueError, "Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type
668
666
669 code = dataOut.code
667 code = dataOut.code
670 else:
668 else:
671 code = numpy.array(code).reshape(nCode,nBaud)
669 code = numpy.array(code).reshape(nCode,nBaud)
672
670
673 self.setup(code, osamp, dataOut)
671 self.setup(code, osamp, dataOut)
674
672
675 self.isConfig = True
673 self.isConfig = True
676
674
677 if mode == 3:
675 if mode == 3:
678 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
676 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
679
677
680 if times != None:
678 if times != None:
681 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
679 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
682
680
683 if self.code is None:
681 if self.code is None:
684 print "Fail decoding: Code is not defined."
682 print "Fail decoding: Code is not defined."
685 return
683 return
686
684
687 datadec = None
685 datadec = None
688 if mode == 3:
686 if mode == 3:
689 mode = 0
687 mode = 0
690
688
691 if dataOut.flagDataAsBlock:
689 if dataOut.flagDataAsBlock:
692 """
690 """
693 Decoding when data have been read as block,
691 Decoding when data have been read as block,
694 """
692 """
695
693
696 if mode == 0:
694 if mode == 0:
697 datadec = self.__convolutionByBlockInTime(dataOut.data)
695 datadec = self.__convolutionByBlockInTime(dataOut.data)
698 if mode == 1:
696 if mode == 1:
699 datadec = self.__convolutionByBlockInFreq(dataOut.data)
697 datadec = self.__convolutionByBlockInFreq(dataOut.data)
700 else:
698 else:
701 """
699 """
702 Decoding when data have been read profile by profile
700 Decoding when data have been read profile by profile
703 """
701 """
704 if mode == 0:
702 if mode == 0:
705 datadec = self.__convolutionInTime(dataOut.data)
703 datadec = self.__convolutionInTime(dataOut.data)
706
704
707 if mode == 1:
705 if mode == 1:
708 datadec = self.__convolutionInFreq(dataOut.data)
706 datadec = self.__convolutionInFreq(dataOut.data)
709
707
710 if mode == 2:
708 if mode == 2:
711 datadec = self.__convolutionInFreqOpt(dataOut.data)
709 datadec = self.__convolutionInFreqOpt(dataOut.data)
712
710
713 if datadec is None:
711 if datadec is None:
714 raise ValueError, "Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode
712 raise ValueError, "Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode
715
713
716 dataOut.code = self.code
714 dataOut.code = self.code
717 dataOut.nCode = self.nCode
715 dataOut.nCode = self.nCode
718 dataOut.nBaud = self.nBaud
716 dataOut.nBaud = self.nBaud
719
717
720 dataOut.data = datadec
718 dataOut.data = datadec
721
719
722 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
720 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
723
721
724 dataOut.flagDecodeData = True #asumo q la data esta decodificada
722 dataOut.flagDecodeData = True #asumo q la data esta decodificada
725
723
726 if self.__profIndex == self.nCode-1:
724 if self.__profIndex == self.nCode-1:
727 self.__profIndex = 0
725 self.__profIndex = 0
728 return 1
726 return 1
729
727
730 self.__profIndex += 1
728 self.__profIndex += 1
731
729
732 return 1
730 return 1
733 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
731 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
734
732
735
733
736 class ProfileConcat(Operation):
734 class ProfileConcat(Operation):
737
735
738 isConfig = False
736 isConfig = False
739 buffer = None
737 buffer = None
740
738
741 def __init__(self, **kwargs):
739 def __init__(self, **kwargs):
742
740
743 Operation.__init__(self, **kwargs)
741 Operation.__init__(self, **kwargs)
744 self.profileIndex = 0
742 self.profileIndex = 0
745
743
746 def reset(self):
744 def reset(self):
747 self.buffer = numpy.zeros_like(self.buffer)
745 self.buffer = numpy.zeros_like(self.buffer)
748 self.start_index = 0
746 self.start_index = 0
749 self.times = 1
747 self.times = 1
750
748
751 def setup(self, data, m, n=1):
749 def setup(self, data, m, n=1):
752 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
750 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
753 self.nHeights = data.shape[1]#.nHeights
751 self.nHeights = data.shape[1]#.nHeights
754 self.start_index = 0
752 self.start_index = 0
755 self.times = 1
753 self.times = 1
756
754
757 def concat(self, data):
755 def concat(self, data):
758
756
759 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
757 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
760 self.start_index = self.start_index + self.nHeights
758 self.start_index = self.start_index + self.nHeights
761
759
762 def run(self, dataOut, m):
760 def run(self, dataOut, m):
763
761
764 dataOut.flagNoData = True
762 dataOut.flagNoData = True
765
763
766 if not self.isConfig:
764 if not self.isConfig:
767 self.setup(dataOut.data, m, 1)
765 self.setup(dataOut.data, m, 1)
768 self.isConfig = True
766 self.isConfig = True
769
767
770 if dataOut.flagDataAsBlock:
768 if dataOut.flagDataAsBlock:
771 raise ValueError, "ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False"
769 raise ValueError, "ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False"
772
770
773 else:
771 else:
774 self.concat(dataOut.data)
772 self.concat(dataOut.data)
775 self.times += 1
773 self.times += 1
776 if self.times > m:
774 if self.times > m:
777 dataOut.data = self.buffer
775 dataOut.data = self.buffer
778 self.reset()
776 self.reset()
779 dataOut.flagNoData = False
777 dataOut.flagNoData = False
780 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
778 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
781 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
779 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
782 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
780 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
783 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
781 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
784 dataOut.ippSeconds *= m
782 dataOut.ippSeconds *= m
785
783
786 class ProfileSelector(Operation):
784 class ProfileSelector(Operation):
787
785
788 profileIndex = None
786 profileIndex = None
789 # Tamanho total de los perfiles
787 # Tamanho total de los perfiles
790 nProfiles = None
788 nProfiles = None
791
789
792 def __init__(self, **kwargs):
790 def __init__(self, **kwargs):
793
791
794 Operation.__init__(self, **kwargs)
792 Operation.__init__(self, **kwargs)
795 self.profileIndex = 0
793 self.profileIndex = 0
796
794
797 def incProfileIndex(self):
795 def incProfileIndex(self):
798
796
799 self.profileIndex += 1
797 self.profileIndex += 1
800
798
801 if self.profileIndex >= self.nProfiles:
799 if self.profileIndex >= self.nProfiles:
802 self.profileIndex = 0
800 self.profileIndex = 0
803
801
804 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
802 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
805
803
806 if profileIndex < minIndex:
804 if profileIndex < minIndex:
807 return False
805 return False
808
806
809 if profileIndex > maxIndex:
807 if profileIndex > maxIndex:
810 return False
808 return False
811
809
812 return True
810 return True
813
811
814 def isThisProfileInList(self, profileIndex, profileList):
812 def isThisProfileInList(self, profileIndex, profileList):
815
813
816 if profileIndex not in profileList:
814 if profileIndex not in profileList:
817 return False
815 return False
818
816
819 return True
817 return True
820
818
821 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
819 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
822
820
823 """
821 """
824 ProfileSelector:
822 ProfileSelector:
825
823
826 Inputs:
824 Inputs:
827 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
825 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
828
826
829 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
827 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
830
828
831 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
829 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
832
830
833 """
831 """
834
832
835 if rangeList is not None:
833 if rangeList is not None:
836 if type(rangeList[0]) not in (tuple, list):
834 if type(rangeList[0]) not in (tuple, list):
837 rangeList = [rangeList]
835 rangeList = [rangeList]
838
836
839 dataOut.flagNoData = True
837 dataOut.flagNoData = True
840
838
841 if dataOut.flagDataAsBlock:
839 if dataOut.flagDataAsBlock:
842 """
840 """
843 data dimension = [nChannels, nProfiles, nHeis]
841 data dimension = [nChannels, nProfiles, nHeis]
844 """
842 """
845 if profileList != None:
843 if profileList != None:
846 dataOut.data = dataOut.data[:,profileList,:]
844 dataOut.data = dataOut.data[:,profileList,:]
847
845
848 if profileRangeList != None:
846 if profileRangeList != None:
849 minIndex = profileRangeList[0]
847 minIndex = profileRangeList[0]
850 maxIndex = profileRangeList[1]
848 maxIndex = profileRangeList[1]
851 profileList = range(minIndex, maxIndex+1)
849 profileList = range(minIndex, maxIndex+1)
852
850
853 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
851 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
854
852
855 if rangeList != None:
853 if rangeList != None:
856
854
857 profileList = []
855 profileList = []
858
856
859 for thisRange in rangeList:
857 for thisRange in rangeList:
860 minIndex = thisRange[0]
858 minIndex = thisRange[0]
861 maxIndex = thisRange[1]
859 maxIndex = thisRange[1]
862
860
863 profileList.extend(range(minIndex, maxIndex+1))
861 profileList.extend(range(minIndex, maxIndex+1))
864
862
865 dataOut.data = dataOut.data[:,profileList,:]
863 dataOut.data = dataOut.data[:,profileList,:]
866
864
867 dataOut.nProfiles = len(profileList)
865 dataOut.nProfiles = len(profileList)
868 dataOut.profileIndex = dataOut.nProfiles - 1
866 dataOut.profileIndex = dataOut.nProfiles - 1
869 dataOut.flagNoData = False
867 dataOut.flagNoData = False
870
868
871 return True
869 return True
872
870
873 """
871 """
874 data dimension = [nChannels, nHeis]
872 data dimension = [nChannels, nHeis]
875 """
873 """
876
874
877 if profileList != None:
875 if profileList != None:
878
876
879 if self.isThisProfileInList(dataOut.profileIndex, profileList):
877 if self.isThisProfileInList(dataOut.profileIndex, profileList):
880
878
881 self.nProfiles = len(profileList)
879 self.nProfiles = len(profileList)
882 dataOut.nProfiles = self.nProfiles
880 dataOut.nProfiles = self.nProfiles
883 dataOut.profileIndex = self.profileIndex
881 dataOut.profileIndex = self.profileIndex
884 dataOut.flagNoData = False
882 dataOut.flagNoData = False
885
883
886 self.incProfileIndex()
884 self.incProfileIndex()
887 return True
885 return True
888
886
889 if profileRangeList != None:
887 if profileRangeList != None:
890
888
891 minIndex = profileRangeList[0]
889 minIndex = profileRangeList[0]
892 maxIndex = profileRangeList[1]
890 maxIndex = profileRangeList[1]
893
891
894 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
892 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
895
893
896 self.nProfiles = maxIndex - minIndex + 1
894 self.nProfiles = maxIndex - minIndex + 1
897 dataOut.nProfiles = self.nProfiles
895 dataOut.nProfiles = self.nProfiles
898 dataOut.profileIndex = self.profileIndex
896 dataOut.profileIndex = self.profileIndex
899 dataOut.flagNoData = False
897 dataOut.flagNoData = False
900
898
901 self.incProfileIndex()
899 self.incProfileIndex()
902 return True
900 return True
903
901
904 if rangeList != None:
902 if rangeList != None:
905
903
906 nProfiles = 0
904 nProfiles = 0
907
905
908 for thisRange in rangeList:
906 for thisRange in rangeList:
909 minIndex = thisRange[0]
907 minIndex = thisRange[0]
910 maxIndex = thisRange[1]
908 maxIndex = thisRange[1]
911
909
912 nProfiles += maxIndex - minIndex + 1
910 nProfiles += maxIndex - minIndex + 1
913
911
914 for thisRange in rangeList:
912 for thisRange in rangeList:
915
913
916 minIndex = thisRange[0]
914 minIndex = thisRange[0]
917 maxIndex = thisRange[1]
915 maxIndex = thisRange[1]
918
916
919 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
917 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
920
918
921 self.nProfiles = nProfiles
919 self.nProfiles = nProfiles
922 dataOut.nProfiles = self.nProfiles
920 dataOut.nProfiles = self.nProfiles
923 dataOut.profileIndex = self.profileIndex
921 dataOut.profileIndex = self.profileIndex
924 dataOut.flagNoData = False
922 dataOut.flagNoData = False
925
923
926 self.incProfileIndex()
924 self.incProfileIndex()
927
925
928 break
926 break
929
927
930 return True
928 return True
931
929
932
930
933 if beam != None: #beam is only for AMISR data
931 if beam != None: #beam is only for AMISR data
934 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
932 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
935 dataOut.flagNoData = False
933 dataOut.flagNoData = False
936 dataOut.profileIndex = self.profileIndex
934 dataOut.profileIndex = self.profileIndex
937
935
938 self.incProfileIndex()
936 self.incProfileIndex()
939
937
940 return True
938 return True
941
939
942 raise ValueError, "ProfileSelector needs profileList, profileRangeList or rangeList parameter"
940 raise ValueError, "ProfileSelector needs profileList, profileRangeList or rangeList parameter"
943
941
944 return False
942 return False
945
943
946 class Reshaper(Operation):
944 class Reshaper(Operation):
947
945
948 def __init__(self, **kwargs):
946 def __init__(self, **kwargs):
949
947
950 Operation.__init__(self, **kwargs)
948 Operation.__init__(self, **kwargs)
951
949
952 self.__buffer = None
950 self.__buffer = None
953 self.__nitems = 0
951 self.__nitems = 0
954
952
955 def __appendProfile(self, dataOut, nTxs):
953 def __appendProfile(self, dataOut, nTxs):
956
954
957 if self.__buffer is None:
955 if self.__buffer is None:
958 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
956 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
959 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
957 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
960
958
961 ini = dataOut.nHeights * self.__nitems
959 ini = dataOut.nHeights * self.__nitems
962 end = ini + dataOut.nHeights
960 end = ini + dataOut.nHeights
963
961
964 self.__buffer[:, ini:end] = dataOut.data
962 self.__buffer[:, ini:end] = dataOut.data
965
963
966 self.__nitems += 1
964 self.__nitems += 1
967
965
968 return int(self.__nitems*nTxs)
966 return int(self.__nitems*nTxs)
969
967
970 def __getBuffer(self):
968 def __getBuffer(self):
971
969
972 if self.__nitems == int(1./self.__nTxs):
970 if self.__nitems == int(1./self.__nTxs):
973
971
974 self.__nitems = 0
972 self.__nitems = 0
975
973
976 return self.__buffer.copy()
974 return self.__buffer.copy()
977
975
978 return None
976 return None
979
977
980 def __checkInputs(self, dataOut, shape, nTxs):
978 def __checkInputs(self, dataOut, shape, nTxs):
981
979
982 if shape is None and nTxs is None:
980 if shape is None and nTxs is None:
983 raise ValueError, "Reshaper: shape of factor should be defined"
981 raise ValueError, "Reshaper: shape of factor should be defined"
984
982
985 if nTxs:
983 if nTxs:
986 if nTxs < 0:
984 if nTxs < 0:
987 raise ValueError, "nTxs should be greater than 0"
985 raise ValueError, "nTxs should be greater than 0"
988
986
989 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
987 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
990 raise ValueError, "nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs))
988 raise ValueError, "nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs))
991
989
992 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
990 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
993
991
994 return shape, nTxs
992 return shape, nTxs
995
993
996 if len(shape) != 2 and len(shape) != 3:
994 if len(shape) != 2 and len(shape) != 3:
997 raise ValueError, "shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights)
995 raise ValueError, "shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights)
998
996
999 if len(shape) == 2:
997 if len(shape) == 2:
1000 shape_tuple = [dataOut.nChannels]
998 shape_tuple = [dataOut.nChannels]
1001 shape_tuple.extend(shape)
999 shape_tuple.extend(shape)
1002 else:
1000 else:
1003 shape_tuple = list(shape)
1001 shape_tuple = list(shape)
1004
1002
1005 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1003 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1006
1004
1007 return shape_tuple, nTxs
1005 return shape_tuple, nTxs
1008
1006
1009 def run(self, dataOut, shape=None, nTxs=None):
1007 def run(self, dataOut, shape=None, nTxs=None):
1010
1008
1011 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1009 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1012
1010
1013 dataOut.flagNoData = True
1011 dataOut.flagNoData = True
1014 profileIndex = None
1012 profileIndex = None
1015
1013
1016 if dataOut.flagDataAsBlock:
1014 if dataOut.flagDataAsBlock:
1017
1015
1018 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1016 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1019 dataOut.flagNoData = False
1017 dataOut.flagNoData = False
1020
1018
1021 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1019 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1022
1020
1023 else:
1021 else:
1024
1022
1025 if self.__nTxs < 1:
1023 if self.__nTxs < 1:
1026
1024
1027 self.__appendProfile(dataOut, self.__nTxs)
1025 self.__appendProfile(dataOut, self.__nTxs)
1028 new_data = self.__getBuffer()
1026 new_data = self.__getBuffer()
1029
1027
1030 if new_data is not None:
1028 if new_data is not None:
1031 dataOut.data = new_data
1029 dataOut.data = new_data
1032 dataOut.flagNoData = False
1030 dataOut.flagNoData = False
1033
1031
1034 profileIndex = dataOut.profileIndex*nTxs
1032 profileIndex = dataOut.profileIndex*nTxs
1035
1033
1036 else:
1034 else:
1037 raise ValueError, "nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)"
1035 raise ValueError, "nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)"
1038
1036
1039 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1037 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1040
1038
1041 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1039 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1042
1040
1043 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1041 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1044
1042
1045 dataOut.profileIndex = profileIndex
1043 dataOut.profileIndex = profileIndex
1046
1044
1047 dataOut.ippSeconds /= self.__nTxs
1045 dataOut.ippSeconds /= self.__nTxs
1048
1046
1049 class SplitProfiles(Operation):
1047 class SplitProfiles(Operation):
1050
1048
1051 def __init__(self, **kwargs):
1049 def __init__(self, **kwargs):
1052
1050
1053 Operation.__init__(self, **kwargs)
1051 Operation.__init__(self, **kwargs)
1054
1052
1055 def run(self, dataOut, n):
1053 def run(self, dataOut, n):
1056
1054
1057 dataOut.flagNoData = True
1055 dataOut.flagNoData = True
1058 profileIndex = None
1056 profileIndex = None
1059
1057
1060 if dataOut.flagDataAsBlock:
1058 if dataOut.flagDataAsBlock:
1061
1059
1062 #nchannels, nprofiles, nsamples
1060 #nchannels, nprofiles, nsamples
1063 shape = dataOut.data.shape
1061 shape = dataOut.data.shape
1064
1062
1065 if shape[2] % n != 0:
1063 if shape[2] % n != 0:
1066 raise ValueError, "Could not split the data, n=%d has to be multiple of %d" %(n, shape[2])
1064 raise ValueError, "Could not split the data, n=%d has to be multiple of %d" %(n, shape[2])
1067
1065
1068 new_shape = shape[0], shape[1]*n, shape[2]/n
1066 new_shape = shape[0], shape[1]*n, shape[2]/n
1069
1067
1070 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1068 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1071 dataOut.flagNoData = False
1069 dataOut.flagNoData = False
1072
1070
1073 profileIndex = int(dataOut.nProfiles/n) - 1
1071 profileIndex = int(dataOut.nProfiles/n) - 1
1074
1072
1075 else:
1073 else:
1076
1074
1077 raise ValueError, "Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)"
1075 raise ValueError, "Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)"
1078
1076
1079 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1077 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1080
1078
1081 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1079 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1082
1080
1083 dataOut.nProfiles = int(dataOut.nProfiles*n)
1081 dataOut.nProfiles = int(dataOut.nProfiles*n)
1084
1082
1085 dataOut.profileIndex = profileIndex
1083 dataOut.profileIndex = profileIndex
1086
1084
1087 dataOut.ippSeconds /= n
1085 dataOut.ippSeconds /= n
1088
1086
1089 class CombineProfiles(Operation):
1087 class CombineProfiles(Operation):
1090
1088
1091 def __init__(self, **kwargs):
1089 def __init__(self, **kwargs):
1092
1090
1093 Operation.__init__(self, **kwargs)
1091 Operation.__init__(self, **kwargs)
1094
1092
1095 self.__remData = None
1093 self.__remData = None
1096 self.__profileIndex = 0
1094 self.__profileIndex = 0
1097
1095
1098 def run(self, dataOut, n):
1096 def run(self, dataOut, n):
1099
1097
1100 dataOut.flagNoData = True
1098 dataOut.flagNoData = True
1101 profileIndex = None
1099 profileIndex = None
1102
1100
1103 if dataOut.flagDataAsBlock:
1101 if dataOut.flagDataAsBlock:
1104
1102
1105 #nchannels, nprofiles, nsamples
1103 #nchannels, nprofiles, nsamples
1106 shape = dataOut.data.shape
1104 shape = dataOut.data.shape
1107 new_shape = shape[0], shape[1]/n, shape[2]*n
1105 new_shape = shape[0], shape[1]/n, shape[2]*n
1108
1106
1109 if shape[1] % n != 0:
1107 if shape[1] % n != 0:
1110 raise ValueError, "Could not split the data, n=%d has to be multiple of %d" %(n, shape[1])
1108 raise ValueError, "Could not split the data, n=%d has to be multiple of %d" %(n, shape[1])
1111
1109
1112 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1110 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1113 dataOut.flagNoData = False
1111 dataOut.flagNoData = False
1114
1112
1115 profileIndex = int(dataOut.nProfiles*n) - 1
1113 profileIndex = int(dataOut.nProfiles*n) - 1
1116
1114
1117 else:
1115 else:
1118
1116
1119 #nchannels, nsamples
1117 #nchannels, nsamples
1120 if self.__remData is None:
1118 if self.__remData is None:
1121 newData = dataOut.data
1119 newData = dataOut.data
1122 else:
1120 else:
1123 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1121 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1124
1122
1125 self.__profileIndex += 1
1123 self.__profileIndex += 1
1126
1124
1127 if self.__profileIndex < n:
1125 if self.__profileIndex < n:
1128 self.__remData = newData
1126 self.__remData = newData
1129 #continue
1127 #continue
1130 return
1128 return
1131
1129
1132 self.__profileIndex = 0
1130 self.__profileIndex = 0
1133 self.__remData = None
1131 self.__remData = None
1134
1132
1135 dataOut.data = newData
1133 dataOut.data = newData
1136 dataOut.flagNoData = False
1134 dataOut.flagNoData = False
1137
1135
1138 profileIndex = dataOut.profileIndex/n
1136 profileIndex = dataOut.profileIndex/n
1139
1137
1140
1138
1141 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1139 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1142
1140
1143 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1141 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1144
1142
1145 dataOut.nProfiles = int(dataOut.nProfiles/n)
1143 dataOut.nProfiles = int(dataOut.nProfiles/n)
1146
1144
1147 dataOut.profileIndex = profileIndex
1145 dataOut.profileIndex = profileIndex
1148
1146
1149 dataOut.ippSeconds *= n
1147 dataOut.ippSeconds *= n
1150
1148
1151 # import collections
1149 # import collections
1152 # from scipy.stats import mode
1150 # from scipy.stats import mode
1153 #
1151 #
1154 # class Synchronize(Operation):
1152 # class Synchronize(Operation):
1155 #
1153 #
1156 # isConfig = False
1154 # isConfig = False
1157 # __profIndex = 0
1155 # __profIndex = 0
1158 #
1156 #
1159 # def __init__(self, **kwargs):
1157 # def __init__(self, **kwargs):
1160 #
1158 #
1161 # Operation.__init__(self, **kwargs)
1159 # Operation.__init__(self, **kwargs)
1162 # # self.isConfig = False
1160 # # self.isConfig = False
1163 # self.__powBuffer = None
1161 # self.__powBuffer = None
1164 # self.__startIndex = 0
1162 # self.__startIndex = 0
1165 # self.__pulseFound = False
1163 # self.__pulseFound = False
1166 #
1164 #
1167 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1165 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1168 #
1166 #
1169 # #Read data
1167 # #Read data
1170 #
1168 #
1171 # powerdB = dataOut.getPower(channel = channel)
1169 # powerdB = dataOut.getPower(channel = channel)
1172 # noisedB = dataOut.getNoise(channel = channel)[0]
1170 # noisedB = dataOut.getNoise(channel = channel)[0]
1173 #
1171 #
1174 # self.__powBuffer.extend(powerdB.flatten())
1172 # self.__powBuffer.extend(powerdB.flatten())
1175 #
1173 #
1176 # dataArray = numpy.array(self.__powBuffer)
1174 # dataArray = numpy.array(self.__powBuffer)
1177 #
1175 #
1178 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1176 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1179 #
1177 #
1180 # maxValue = numpy.nanmax(filteredPower)
1178 # maxValue = numpy.nanmax(filteredPower)
1181 #
1179 #
1182 # if maxValue < noisedB + 10:
1180 # if maxValue < noisedB + 10:
1183 # #No se encuentra ningun pulso de transmision
1181 # #No se encuentra ningun pulso de transmision
1184 # return None
1182 # return None
1185 #
1183 #
1186 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1184 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1187 #
1185 #
1188 # if len(maxValuesIndex) < 2:
1186 # if len(maxValuesIndex) < 2:
1189 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1187 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1190 # return None
1188 # return None
1191 #
1189 #
1192 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1190 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1193 #
1191 #
1194 # #Seleccionar solo valores con un espaciamiento de nSamples
1192 # #Seleccionar solo valores con un espaciamiento de nSamples
1195 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1193 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1196 #
1194 #
1197 # if len(pulseIndex) < 2:
1195 # if len(pulseIndex) < 2:
1198 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1196 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1199 # return None
1197 # return None
1200 #
1198 #
1201 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1199 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1202 #
1200 #
1203 # #remover senales que se distancien menos de 10 unidades o muestras
1201 # #remover senales que se distancien menos de 10 unidades o muestras
1204 # #(No deberian existir IPP menor a 10 unidades)
1202 # #(No deberian existir IPP menor a 10 unidades)
1205 #
1203 #
1206 # realIndex = numpy.where(spacing > 10 )[0]
1204 # realIndex = numpy.where(spacing > 10 )[0]
1207 #
1205 #
1208 # if len(realIndex) < 2:
1206 # if len(realIndex) < 2:
1209 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1207 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1210 # return None
1208 # return None
1211 #
1209 #
1212 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1210 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1213 # realPulseIndex = pulseIndex[realIndex]
1211 # realPulseIndex = pulseIndex[realIndex]
1214 #
1212 #
1215 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1213 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1216 #
1214 #
1217 # print "IPP = %d samples" %period
1215 # print "IPP = %d samples" %period
1218 #
1216 #
1219 # self.__newNSamples = dataOut.nHeights #int(period)
1217 # self.__newNSamples = dataOut.nHeights #int(period)
1220 # self.__startIndex = int(realPulseIndex[0])
1218 # self.__startIndex = int(realPulseIndex[0])
1221 #
1219 #
1222 # return 1
1220 # return 1
1223 #
1221 #
1224 #
1222 #
1225 # def setup(self, nSamples, nChannels, buffer_size = 4):
1223 # def setup(self, nSamples, nChannels, buffer_size = 4):
1226 #
1224 #
1227 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1225 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1228 # maxlen = buffer_size*nSamples)
1226 # maxlen = buffer_size*nSamples)
1229 #
1227 #
1230 # bufferList = []
1228 # bufferList = []
1231 #
1229 #
1232 # for i in range(nChannels):
1230 # for i in range(nChannels):
1233 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1231 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1234 # maxlen = buffer_size*nSamples)
1232 # maxlen = buffer_size*nSamples)
1235 #
1233 #
1236 # bufferList.append(bufferByChannel)
1234 # bufferList.append(bufferByChannel)
1237 #
1235 #
1238 # self.__nSamples = nSamples
1236 # self.__nSamples = nSamples
1239 # self.__nChannels = nChannels
1237 # self.__nChannels = nChannels
1240 # self.__bufferList = bufferList
1238 # self.__bufferList = bufferList
1241 #
1239 #
1242 # def run(self, dataOut, channel = 0):
1240 # def run(self, dataOut, channel = 0):
1243 #
1241 #
1244 # if not self.isConfig:
1242 # if not self.isConfig:
1245 # nSamples = dataOut.nHeights
1243 # nSamples = dataOut.nHeights
1246 # nChannels = dataOut.nChannels
1244 # nChannels = dataOut.nChannels
1247 # self.setup(nSamples, nChannels)
1245 # self.setup(nSamples, nChannels)
1248 # self.isConfig = True
1246 # self.isConfig = True
1249 #
1247 #
1250 # #Append new data to internal buffer
1248 # #Append new data to internal buffer
1251 # for thisChannel in range(self.__nChannels):
1249 # for thisChannel in range(self.__nChannels):
1252 # bufferByChannel = self.__bufferList[thisChannel]
1250 # bufferByChannel = self.__bufferList[thisChannel]
1253 # bufferByChannel.extend(dataOut.data[thisChannel])
1251 # bufferByChannel.extend(dataOut.data[thisChannel])
1254 #
1252 #
1255 # if self.__pulseFound:
1253 # if self.__pulseFound:
1256 # self.__startIndex -= self.__nSamples
1254 # self.__startIndex -= self.__nSamples
1257 #
1255 #
1258 # #Finding Tx Pulse
1256 # #Finding Tx Pulse
1259 # if not self.__pulseFound:
1257 # if not self.__pulseFound:
1260 # indexFound = self.__findTxPulse(dataOut, channel)
1258 # indexFound = self.__findTxPulse(dataOut, channel)
1261 #
1259 #
1262 # if indexFound == None:
1260 # if indexFound == None:
1263 # dataOut.flagNoData = True
1261 # dataOut.flagNoData = True
1264 # return
1262 # return
1265 #
1263 #
1266 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1264 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1267 # self.__pulseFound = True
1265 # self.__pulseFound = True
1268 # self.__startIndex = indexFound
1266 # self.__startIndex = indexFound
1269 #
1267 #
1270 # #If pulse was found ...
1268 # #If pulse was found ...
1271 # for thisChannel in range(self.__nChannels):
1269 # for thisChannel in range(self.__nChannels):
1272 # bufferByChannel = self.__bufferList[thisChannel]
1270 # bufferByChannel = self.__bufferList[thisChannel]
1273 # #print self.__startIndex
1271 # #print self.__startIndex
1274 # x = numpy.array(bufferByChannel)
1272 # x = numpy.array(bufferByChannel)
1275 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1273 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1276 #
1274 #
1277 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1275 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1278 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1276 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1279 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1277 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1280 #
1278 #
1281 # dataOut.data = self.__arrayBuffer
1279 # dataOut.data = self.__arrayBuffer
1282 #
1280 #
1283 # self.__startIndex += self.__newNSamples
1281 # self.__startIndex += self.__newNSamples
1284 #
1282 #
1285 # return
1283 # return
General Comments 0
You need to be logged in to leave comments. Login now