##// END OF EJS Templates
Bug fixed: Seleccion de directorio al leer en linea
Miguel Valdez -
r294:1886dc09bbff
parent child
Show More
@@ -1,2584 +1,2601
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
5 '''
5 '''
6
6
7 import os, sys
7 import os, sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import time, datetime
12 import time, datetime
13
13
14 from jrodata import *
14 from jrodata import *
15 from jroheaderIO import *
15 from jroheaderIO import *
16 from jroprocessing import *
16 from jroprocessing import *
17
17
18 LOCALTIME = -18000
18 LOCALTIME = -18000
19
19
20 def isNumber(str):
20 def isNumber(str):
21 """
21 """
22 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
22 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
23
23
24 Excepciones:
24 Excepciones:
25 Si un determinado string no puede ser convertido a numero
25 Si un determinado string no puede ser convertido a numero
26 Input:
26 Input:
27 str, string al cual se le analiza para determinar si convertible a un numero o no
27 str, string al cual se le analiza para determinar si convertible a un numero o no
28
28
29 Return:
29 Return:
30 True : si el string es uno numerico
30 True : si el string es uno numerico
31 False : no es un string numerico
31 False : no es un string numerico
32 """
32 """
33 try:
33 try:
34 float( str )
34 float( str )
35 return True
35 return True
36 except:
36 except:
37 return False
37 return False
38
38
39 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
39 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
40 """
40 """
41 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
41 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
42
42
43 Inputs:
43 Inputs:
44 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
44 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
45
45
46 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
46 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
47 segundos contados desde 01/01/1970.
47 segundos contados desde 01/01/1970.
48 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
48 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
49 segundos contados desde 01/01/1970.
49 segundos contados desde 01/01/1970.
50
50
51 Return:
51 Return:
52 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
52 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
53 fecha especificado, de lo contrario retorna False.
53 fecha especificado, de lo contrario retorna False.
54
54
55 Excepciones:
55 Excepciones:
56 Si el archivo no existe o no puede ser abierto
56 Si el archivo no existe o no puede ser abierto
57 Si la cabecera no puede ser leida.
57 Si la cabecera no puede ser leida.
58
58
59 """
59 """
60 basicHeaderObj = BasicHeader(LOCALTIME)
60 basicHeaderObj = BasicHeader(LOCALTIME)
61
61
62 try:
62 try:
63 fp = open(filename,'rb')
63 fp = open(filename,'rb')
64 except:
64 except:
65 raise IOError, "The file %s can't be opened" %(filename)
65 raise IOError, "The file %s can't be opened" %(filename)
66
66
67 sts = basicHeaderObj.read(fp)
67 sts = basicHeaderObj.read(fp)
68 fp.close()
68 fp.close()
69
69
70 if not(sts):
70 if not(sts):
71 print "Skipping the file %s because it has not a valid header" %(filename)
71 print "Skipping the file %s because it has not a valid header" %(filename)
72 return 0
72 return 0
73
73
74 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
74 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
75 return 0
75 return 0
76
76
77 return 1
77 return 1
78
78
79 def isFileinThisTime(filename, startTime, endTime):
79 def isFileinThisTime(filename, startTime, endTime):
80 """
80 """
81 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
81 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
82
82
83 Inputs:
83 Inputs:
84 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
84 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
85
85
86 startTime : tiempo inicial del rango seleccionado en formato datetime.time
86 startTime : tiempo inicial del rango seleccionado en formato datetime.time
87
87
88 endTime : tiempo final del rango seleccionado en formato datetime.time
88 endTime : tiempo final del rango seleccionado en formato datetime.time
89
89
90 Return:
90 Return:
91 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
91 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
92 fecha especificado, de lo contrario retorna False.
92 fecha especificado, de lo contrario retorna False.
93
93
94 Excepciones:
94 Excepciones:
95 Si el archivo no existe o no puede ser abierto
95 Si el archivo no existe o no puede ser abierto
96 Si la cabecera no puede ser leida.
96 Si la cabecera no puede ser leida.
97
97
98 """
98 """
99
99
100
100
101 try:
101 try:
102 fp = open(filename,'rb')
102 fp = open(filename,'rb')
103 except:
103 except:
104 raise IOError, "The file %s can't be opened" %(filename)
104 raise IOError, "The file %s can't be opened" %(filename)
105
105
106 basicHeaderObj = BasicHeader(LOCALTIME)
106 basicHeaderObj = BasicHeader(LOCALTIME)
107 sts = basicHeaderObj.read(fp)
107 sts = basicHeaderObj.read(fp)
108 fp.close()
108 fp.close()
109
109
110 thisTime = basicHeaderObj.datatime.time()
110 thisTime = basicHeaderObj.datatime.time()
111
111
112 if not(sts):
112 if not(sts):
113 print "Skipping the file %s because it has not a valid header" %(filename)
113 print "Skipping the file %s because it has not a valid header" %(filename)
114 return 0
114 return 0
115
115
116 if not ((startTime <= thisTime) and (endTime > thisTime)):
116 if not ((startTime <= thisTime) and (endTime > thisTime)):
117 return 0
117 return 0
118
118
119 return 1
119 return 1
120
120
121 def getlastFileFromPath(path, ext):
121 def getlastFileFromPath(path, ext):
122 """
122 """
123 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
123 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
124 al final de la depuracion devuelve el ultimo file de la lista que quedo.
124 al final de la depuracion devuelve el ultimo file de la lista que quedo.
125
125
126 Input:
126 Input:
127 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
127 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
128 ext : extension de los files contenidos en una carpeta
128 ext : extension de los files contenidos en una carpeta
129
129
130 Return:
130 Return:
131 El ultimo file de una determinada carpeta, no se considera el path.
131 El ultimo file de una determinada carpeta, no se considera el path.
132 """
132 """
133 validFilelist = []
133 validFilelist = []
134 fileList = os.listdir(path)
134 fileList = os.listdir(path)
135
135
136 # 0 1234 567 89A BCDE
136 # 0 1234 567 89A BCDE
137 # H YYYY DDD SSS .ext
137 # H YYYY DDD SSS .ext
138
138
139 for file in fileList:
139 for file in fileList:
140 try:
140 try:
141 year = int(file[1:5])
141 year = int(file[1:5])
142 doy = int(file[5:8])
142 doy = int(file[5:8])
143
143
144
144
145 except:
145 except:
146 continue
146 continue
147
147
148 if (os.path.splitext(file)[-1].lower() != ext.lower()):
148 if (os.path.splitext(file)[-1].lower() != ext.lower()):
149 continue
149 continue
150
150
151 validFilelist.append(file)
151 validFilelist.append(file)
152
152
153 if validFilelist:
153 if validFilelist:
154 validFilelist = sorted( validFilelist, key=str.lower )
154 validFilelist = sorted( validFilelist, key=str.lower )
155 return validFilelist[-1]
155 return validFilelist[-1]
156
156
157 return None
157 return None
158
158
159 def checkForRealPath(path, year, doy, set, ext):
159 def checkForRealPath(path, year, doy, set, ext):
160 """
160 """
161 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
161 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
162 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
162 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
163 el path exacto de un determinado file.
163 el path exacto de un determinado file.
164
164
165 Example :
165 Example :
166 nombre correcto del file es .../.../D2009307/P2009307367.ext
166 nombre correcto del file es .../.../D2009307/P2009307367.ext
167
167
168 Entonces la funcion prueba con las siguientes combinaciones
168 Entonces la funcion prueba con las siguientes combinaciones
169 .../.../y2009307367.ext
169 .../.../y2009307367.ext
170 .../.../Y2009307367.ext
170 .../.../Y2009307367.ext
171 .../.../x2009307/y2009307367.ext
171 .../.../x2009307/y2009307367.ext
172 .../.../x2009307/Y2009307367.ext
172 .../.../x2009307/Y2009307367.ext
173 .../.../X2009307/y2009307367.ext
173 .../.../X2009307/y2009307367.ext
174 .../.../X2009307/Y2009307367.ext
174 .../.../X2009307/Y2009307367.ext
175 siendo para este caso, la ultima combinacion de letras, identica al file buscado
175 siendo para este caso, la ultima combinacion de letras, identica al file buscado
176
176
177 Return:
177 Return:
178 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
178 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
179 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
179 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
180 para el filename
180 para el filename
181 """
181 """
182 fullfilename = None
182 fullfilename = None
183 find_flag = False
183 find_flag = False
184 filename = None
184 filename = None
185
185
186 prefixDirList = [None,'d','D']
186 prefixDirList = [None,'d','D']
187 if ext.lower() == ".r": #voltage
187 if ext.lower() == ".r": #voltage
188 prefixFileList = ['d','D']
188 prefixFileList = ['d','D']
189 elif ext.lower() == ".pdata": #spectra
189 elif ext.lower() == ".pdata": #spectra
190 prefixFileList = ['p','P']
190 prefixFileList = ['p','P']
191 else:
191 else:
192 return None, filename
192 return None, filename
193
193
194 #barrido por las combinaciones posibles
194 #barrido por las combinaciones posibles
195 for prefixDir in prefixDirList:
195 for prefixDir in prefixDirList:
196 thispath = path
196 thispath = path
197 if prefixDir != None:
197 if prefixDir != None:
198 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
198 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
199 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
199 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
200
200
201 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
201 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
202 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
202 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
203 fullfilename = os.path.join( thispath, filename ) #formo el path completo
203 fullfilename = os.path.join( thispath, filename ) #formo el path completo
204
204
205 if os.path.exists( fullfilename ): #verifico que exista
205 if os.path.exists( fullfilename ): #verifico que exista
206 find_flag = True
206 find_flag = True
207 break
207 break
208 if find_flag:
208 if find_flag:
209 break
209 break
210
210
211 if not(find_flag):
211 if not(find_flag):
212 return None, filename
212 return None, filename
213
213
214 return fullfilename, filename
214 return fullfilename, filename
215
215
216 def isDoyFolder(folder):
217
218 try:
219 year = int(folder[1:5])
220 except:
221 return 0
222
223 try:
224 doy = int(folder[5:8])
225 except:
226 return 0
227 return 1
228
216 class JRODataIO:
229 class JRODataIO:
217
230
218 c = 3E8
231 c = 3E8
219
232
220 isConfig = False
233 isConfig = False
221
234
222 basicHeaderObj = BasicHeader(LOCALTIME)
235 basicHeaderObj = BasicHeader(LOCALTIME)
223
236
224 systemHeaderObj = SystemHeader()
237 systemHeaderObj = SystemHeader()
225
238
226 radarControllerHeaderObj = RadarControllerHeader()
239 radarControllerHeaderObj = RadarControllerHeader()
227
240
228 processingHeaderObj = ProcessingHeader()
241 processingHeaderObj = ProcessingHeader()
229
242
230 online = 0
243 online = 0
231
244
232 dtype = None
245 dtype = None
233
246
234 pathList = []
247 pathList = []
235
248
236 filenameList = []
249 filenameList = []
237
250
238 filename = None
251 filename = None
239
252
240 ext = None
253 ext = None
241
254
242 flagIsNewFile = 1
255 flagIsNewFile = 1
243
256
244 flagTimeBlock = 0
257 flagTimeBlock = 0
245
258
246 flagIsNewBlock = 0
259 flagIsNewBlock = 0
247
260
248 fp = None
261 fp = None
249
262
250 firstHeaderSize = 0
263 firstHeaderSize = 0
251
264
252 basicHeaderSize = 24
265 basicHeaderSize = 24
253
266
254 versionFile = 1103
267 versionFile = 1103
255
268
256 fileSize = None
269 fileSize = None
257
270
258 ippSeconds = None
271 ippSeconds = None
259
272
260 fileSizeByHeader = None
273 fileSizeByHeader = None
261
274
262 fileIndex = None
275 fileIndex = None
263
276
264 profileIndex = None
277 profileIndex = None
265
278
266 blockIndex = None
279 blockIndex = None
267
280
268 nTotalBlocks = None
281 nTotalBlocks = None
269
282
270 maxTimeStep = 30
283 maxTimeStep = 30
271
284
272 lastUTTime = None
285 lastUTTime = None
273
286
274 datablock = None
287 datablock = None
275
288
276 dataOut = None
289 dataOut = None
277
290
278 blocksize = None
291 blocksize = None
279
292
280 def __init__(self):
293 def __init__(self):
281
294
282 raise ValueError, "Not implemented"
295 raise ValueError, "Not implemented"
283
296
284 def run(self):
297 def run(self):
285
298
286 raise ValueError, "Not implemented"
299 raise ValueError, "Not implemented"
287
300
288 def getOutput(self):
301 def getOutput(self):
289
302
290 return self.dataOut
303 return self.dataOut
291
304
292 class JRODataReader(JRODataIO, ProcessingUnit):
305 class JRODataReader(JRODataIO, ProcessingUnit):
293
306
294 nReadBlocks = 0
307 nReadBlocks = 0
295
308
296 delay = 10 #number of seconds waiting a new file
309 delay = 10 #number of seconds waiting a new file
297
310
298 nTries = 3 #quantity tries
311 nTries = 3 #quantity tries
299
312
300 nFiles = 3 #number of files for searching
313 nFiles = 3 #number of files for searching
301
314
302 flagNoMoreFiles = 0
315 flagNoMoreFiles = 0
303
316
304 def __init__(self):
317 def __init__(self):
305
318
306 """
319 """
307
320
308 """
321 """
309
322
310 raise ValueError, "This method has not been implemented"
323 raise ValueError, "This method has not been implemented"
311
324
312
325
313 def createObjByDefault(self):
326 def createObjByDefault(self):
314 """
327 """
315
328
316 """
329 """
317 raise ValueError, "This method has not been implemented"
330 raise ValueError, "This method has not been implemented"
318
331
319 def getBlockDimension(self):
332 def getBlockDimension(self):
320
333
321 raise ValueError, "No implemented"
334 raise ValueError, "No implemented"
322
335
323 def __searchFilesOffLine(self,
336 def __searchFilesOffLine(self,
324 path,
337 path,
325 startDate,
338 startDate,
326 endDate,
339 endDate,
327 startTime=datetime.time(0,0,0),
340 startTime=datetime.time(0,0,0),
328 endTime=datetime.time(23,59,59),
341 endTime=datetime.time(23,59,59),
329 set=None,
342 set=None,
330 expLabel='',
343 expLabel='',
331 ext='.r',
344 ext='.r',
332 walk=True):
345 walk=True):
333
346
334 pathList = []
347 pathList = []
335
348
336 if not walk:
349 if not walk:
337 pathList.append(path)
350 pathList.append(path)
338
351
339 else:
352 else:
340 dirList = []
353 dirList = []
341 for thisPath in os.listdir(path):
354 for thisPath in os.listdir(path):
342 if os.path.isdir(os.path.join(path,thisPath)):
355 if not os.path.isdir(os.path.join(path,thisPath)):
343 dirList.append(thisPath)
356 continue
357 if not isDoyFolder(thisPath):
358 continue
359
360 dirList.append(thisPath)
344
361
345 if not(dirList):
362 if not(dirList):
346 return None, None
363 return None, None
347
364
348 thisDate = startDate
365 thisDate = startDate
349
366
350 while(thisDate <= endDate):
367 while(thisDate <= endDate):
351 year = thisDate.timetuple().tm_year
368 year = thisDate.timetuple().tm_year
352 doy = thisDate.timetuple().tm_yday
369 doy = thisDate.timetuple().tm_yday
353
370
354 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
371 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
355 if len(match) == 0:
372 if len(match) == 0:
356 thisDate += datetime.timedelta(1)
373 thisDate += datetime.timedelta(1)
357 continue
374 continue
358
375
359 pathList.append(os.path.join(path,match[0],expLabel))
376 pathList.append(os.path.join(path,match[0],expLabel))
360 thisDate += datetime.timedelta(1)
377 thisDate += datetime.timedelta(1)
361
378
362 if pathList == []:
379 if pathList == []:
363 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
380 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
364 return None, None
381 return None, None
365
382
366 print "%d folder(s) was(were) found for the date range: %s-%s" %(len(pathList), startDate, endDate)
383 print "%d folder(s) was(were) found for the date range: %s-%s" %(len(pathList), startDate, endDate)
367
384
368 filenameList = []
385 filenameList = []
369 for thisPath in pathList:
386 for thisPath in pathList:
370
387
371 fileList = glob.glob1(thisPath, "*%s" %ext)
388 fileList = glob.glob1(thisPath, "*%s" %ext)
372 fileList.sort()
389 fileList.sort()
373
390
374 for file in fileList:
391 for file in fileList:
375
392
376 filename = os.path.join(thisPath,file)
393 filename = os.path.join(thisPath,file)
377
394
378 if isFileinThisTime(filename, startTime, endTime):
395 if isFileinThisTime(filename, startTime, endTime):
379 filenameList.append(filename)
396 filenameList.append(filename)
380
397
381 if not(filenameList):
398 if not(filenameList):
382 print "Any file was found for the time range %s - %s" %(startTime, endTime)
399 print "Any file was found for the time range %s - %s" %(startTime, endTime)
383 return None, None
400 return None, None
384
401
385 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
402 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
386
403
387 self.filenameList = filenameList
404 self.filenameList = filenameList
388
405
389 return pathList, filenameList
406 return pathList, filenameList
390
407
391 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True):
408 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True):
392
409
393 """
410 """
394 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
411 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
395 devuelve el archivo encontrado ademas de otros datos.
412 devuelve el archivo encontrado ademas de otros datos.
396
413
397 Input:
414 Input:
398 path : carpeta donde estan contenidos los files que contiene data
415 path : carpeta donde estan contenidos los files que contiene data
399
416
400 expLabel : Nombre del subexperimento (subfolder)
417 expLabel : Nombre del subexperimento (subfolder)
401
418
402 ext : extension de los files
419 ext : extension de los files
403
420
404 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
421 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
405
422
406 Return:
423 Return:
407 directory : eL directorio donde esta el file encontrado
424 directory : eL directorio donde esta el file encontrado
408 filename : el ultimo file de una determinada carpeta
425 filename : el ultimo file de una determinada carpeta
409 year : el anho
426 year : el anho
410 doy : el numero de dia del anho
427 doy : el numero de dia del anho
411 set : el set del archivo
428 set : el set del archivo
412
429
413
430
414 """
431 """
415 dirList = []
432 dirList = []
416
433
417 if walk:
434 if walk:
418
435
419 #Filtra solo los directorios
436 #Filtra solo los directorios
420 for thisPath in os.listdir(path):
437 for thisPath in os.listdir(path):
421 if os.path.isdir(os.path.join(path, thisPath)):
438 if os.path.isdir(os.path.join(path, thisPath)):
422 dirList.append(thisPath)
439 dirList.append(thisPath)
423
440
424 if not(dirList):
441 if not(dirList):
425 return None, None, None, None, None
442 return None, None, None, None, None
426
443
427 dirList = sorted( dirList, key=str.lower )
444 dirList = sorted( dirList, key=str.lower )
428
445
429 doypath = dirList[-1]
446 doypath = dirList[-1]
430 fullpath = os.path.join(path, doypath, expLabel)
447 fullpath = os.path.join(path, doypath, expLabel)
431
448
432 else:
449 else:
433 fullpath = path
450 fullpath = path
434
451
435 print "%s folder was found: " %(fullpath )
452 print "%s folder was found: " %(fullpath )
436
453
437 filename = getlastFileFromPath(fullpath, ext)
454 filename = getlastFileFromPath(fullpath, ext)
438
455
439 if not(filename):
456 if not(filename):
440 return None, None, None, None, None
457 return None, None, None, None, None
441
458
442 print "%s file was found" %(filename)
459 print "%s file was found" %(filename)
443
460
444 if not(self.__verifyFile(os.path.join(fullpath, filename))):
461 if not(self.__verifyFile(os.path.join(fullpath, filename))):
445 return None, None, None, None, None
462 return None, None, None, None, None
446
463
447 year = int( filename[1:5] )
464 year = int( filename[1:5] )
448 doy = int( filename[5:8] )
465 doy = int( filename[5:8] )
449 set = int( filename[8:11] )
466 set = int( filename[8:11] )
450
467
451 return fullpath, filename, year, doy, set
468 return fullpath, filename, year, doy, set
452
469
453
470
454
471
455 def __setNextFileOffline(self):
472 def __setNextFileOffline(self):
456
473
457 idFile = self.fileIndex
474 idFile = self.fileIndex
458
475
459 while (True):
476 while (True):
460 idFile += 1
477 idFile += 1
461 if not(idFile < len(self.filenameList)):
478 if not(idFile < len(self.filenameList)):
462 self.flagNoMoreFiles = 1
479 self.flagNoMoreFiles = 1
463 print "No more Files"
480 print "No more Files"
464 return 0
481 return 0
465
482
466 filename = self.filenameList[idFile]
483 filename = self.filenameList[idFile]
467
484
468 if not(self.__verifyFile(filename)):
485 if not(self.__verifyFile(filename)):
469 continue
486 continue
470
487
471 fileSize = os.path.getsize(filename)
488 fileSize = os.path.getsize(filename)
472 fp = open(filename,'rb')
489 fp = open(filename,'rb')
473 break
490 break
474
491
475 self.flagIsNewFile = 1
492 self.flagIsNewFile = 1
476 self.fileIndex = idFile
493 self.fileIndex = idFile
477 self.filename = filename
494 self.filename = filename
478 self.fileSize = fileSize
495 self.fileSize = fileSize
479 self.fp = fp
496 self.fp = fp
480
497
481 print "Setting the file: %s"%self.filename
498 print "Setting the file: %s"%self.filename
482
499
483 return 1
500 return 1
484
501
485 def __setNextFileOnline(self):
502 def __setNextFileOnline(self):
486 """
503 """
487 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
504 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
488 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
505 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
489 siguientes.
506 siguientes.
490
507
491 Affected:
508 Affected:
492 self.flagIsNewFile
509 self.flagIsNewFile
493 self.filename
510 self.filename
494 self.fileSize
511 self.fileSize
495 self.fp
512 self.fp
496 self.set
513 self.set
497 self.flagNoMoreFiles
514 self.flagNoMoreFiles
498
515
499 Return:
516 Return:
500 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
517 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
501 1 : si el file fue abierto con exito y esta listo a ser leido
518 1 : si el file fue abierto con exito y esta listo a ser leido
502
519
503 Excepciones:
520 Excepciones:
504 Si un determinado file no puede ser abierto
521 Si un determinado file no puede ser abierto
505 """
522 """
506 nFiles = 0
523 nFiles = 0
507 fileOk_flag = False
524 fileOk_flag = False
508 firstTime_flag = True
525 firstTime_flag = True
509
526
510 self.set += 1
527 self.set += 1
511
528
512 #busca el 1er file disponible
529 #busca el 1er file disponible
513 fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
530 fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
514 if fullfilename:
531 if fullfilename:
515 if self.__verifyFile(fullfilename, False):
532 if self.__verifyFile(fullfilename, False):
516 fileOk_flag = True
533 fileOk_flag = True
517
534
518 #si no encuentra un file entonces espera y vuelve a buscar
535 #si no encuentra un file entonces espera y vuelve a buscar
519 if not(fileOk_flag):
536 if not(fileOk_flag):
520 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
537 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
521
538
522 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
539 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
523 tries = self.nTries
540 tries = self.nTries
524 else:
541 else:
525 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
542 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
526
543
527 for nTries in range( tries ):
544 for nTries in range( tries ):
528 if firstTime_flag:
545 if firstTime_flag:
529 print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
546 print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
530 time.sleep( self.delay )
547 time.sleep( self.delay )
531 else:
548 else:
532 print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
549 print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
533
550
534 fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
551 fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
535 if fullfilename:
552 if fullfilename:
536 if self.__verifyFile(fullfilename):
553 if self.__verifyFile(fullfilename):
537 fileOk_flag = True
554 fileOk_flag = True
538 break
555 break
539
556
540 if fileOk_flag:
557 if fileOk_flag:
541 break
558 break
542
559
543 firstTime_flag = False
560 firstTime_flag = False
544
561
545 print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename
562 print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename
546 self.set += 1
563 self.set += 1
547
564
548 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
565 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
549 self.set = 0
566 self.set = 0
550 self.doy += 1
567 self.doy += 1
551
568
552 if fileOk_flag:
569 if fileOk_flag:
553 self.fileSize = os.path.getsize( fullfilename )
570 self.fileSize = os.path.getsize( fullfilename )
554 self.filename = fullfilename
571 self.filename = fullfilename
555 self.flagIsNewFile = 1
572 self.flagIsNewFile = 1
556 if self.fp != None: self.fp.close()
573 if self.fp != None: self.fp.close()
557 self.fp = open(fullfilename, 'rb')
574 self.fp = open(fullfilename, 'rb')
558 self.flagNoMoreFiles = 0
575 self.flagNoMoreFiles = 0
559 print 'Setting the file: %s' % fullfilename
576 print 'Setting the file: %s' % fullfilename
560 else:
577 else:
561 self.fileSize = 0
578 self.fileSize = 0
562 self.filename = None
579 self.filename = None
563 self.flagIsNewFile = 0
580 self.flagIsNewFile = 0
564 self.fp = None
581 self.fp = None
565 self.flagNoMoreFiles = 1
582 self.flagNoMoreFiles = 1
566 print 'No more Files'
583 print 'No more Files'
567
584
568 return fileOk_flag
585 return fileOk_flag
569
586
570
587
571 def setNextFile(self):
588 def setNextFile(self):
572 if self.fp != None:
589 if self.fp != None:
573 self.fp.close()
590 self.fp.close()
574
591
575 if self.online:
592 if self.online:
576 newFile = self.__setNextFileOnline()
593 newFile = self.__setNextFileOnline()
577 else:
594 else:
578 newFile = self.__setNextFileOffline()
595 newFile = self.__setNextFileOffline()
579
596
580 if not(newFile):
597 if not(newFile):
581 return 0
598 return 0
582
599
583 self.__readFirstHeader()
600 self.__readFirstHeader()
584 self.nReadBlocks = 0
601 self.nReadBlocks = 0
585 return 1
602 return 1
586
603
587 def __waitNewBlock(self):
604 def __waitNewBlock(self):
588 """
605 """
589 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
606 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
590
607
591 Si el modo de lectura es OffLine siempre retorn 0
608 Si el modo de lectura es OffLine siempre retorn 0
592 """
609 """
593 if not self.online:
610 if not self.online:
594 return 0
611 return 0
595
612
596 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
613 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
597 return 0
614 return 0
598
615
599 currentPointer = self.fp.tell()
616 currentPointer = self.fp.tell()
600
617
601 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
618 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
602
619
603 for nTries in range( self.nTries ):
620 for nTries in range( self.nTries ):
604
621
605 self.fp.close()
622 self.fp.close()
606 self.fp = open( self.filename, 'rb' )
623 self.fp = open( self.filename, 'rb' )
607 self.fp.seek( currentPointer )
624 self.fp.seek( currentPointer )
608
625
609 self.fileSize = os.path.getsize( self.filename )
626 self.fileSize = os.path.getsize( self.filename )
610 currentSize = self.fileSize - currentPointer
627 currentSize = self.fileSize - currentPointer
611
628
612 if ( currentSize >= neededSize ):
629 if ( currentSize >= neededSize ):
613 self.__rdBasicHeader()
630 self.__rdBasicHeader()
614 return 1
631 return 1
615
632
616 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
633 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
617 time.sleep( self.delay )
634 time.sleep( self.delay )
618
635
619
636
620 return 0
637 return 0
621
638
622 def __setNewBlock(self):
639 def __setNewBlock(self):
623
640
624 if self.fp == None:
641 if self.fp == None:
625 return 0
642 return 0
626
643
627 if self.flagIsNewFile:
644 if self.flagIsNewFile:
628 return 1
645 return 1
629
646
630 self.lastUTTime = self.basicHeaderObj.utc
647 self.lastUTTime = self.basicHeaderObj.utc
631 currentSize = self.fileSize - self.fp.tell()
648 currentSize = self.fileSize - self.fp.tell()
632 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
649 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
633
650
634 if (currentSize >= neededSize):
651 if (currentSize >= neededSize):
635 self.__rdBasicHeader()
652 self.__rdBasicHeader()
636 return 1
653 return 1
637
654
638 if self.__waitNewBlock():
655 if self.__waitNewBlock():
639 return 1
656 return 1
640
657
641 if not(self.setNextFile()):
658 if not(self.setNextFile()):
642 return 0
659 return 0
643
660
644 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
661 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
645
662
646 self.flagTimeBlock = 0
663 self.flagTimeBlock = 0
647
664
648 if deltaTime > self.maxTimeStep:
665 if deltaTime > self.maxTimeStep:
649 self.flagTimeBlock = 1
666 self.flagTimeBlock = 1
650
667
651 return 1
668 return 1
652
669
653
670
654 def readNextBlock(self):
671 def readNextBlock(self):
655 if not(self.__setNewBlock()):
672 if not(self.__setNewBlock()):
656 return 0
673 return 0
657
674
658 if not(self.readBlock()):
675 if not(self.readBlock()):
659 return 0
676 return 0
660
677
661 return 1
678 return 1
662
679
663 def __rdProcessingHeader(self, fp=None):
680 def __rdProcessingHeader(self, fp=None):
664 if fp == None:
681 if fp == None:
665 fp = self.fp
682 fp = self.fp
666
683
667 self.processingHeaderObj.read(fp)
684 self.processingHeaderObj.read(fp)
668
685
669 def __rdRadarControllerHeader(self, fp=None):
686 def __rdRadarControllerHeader(self, fp=None):
670 if fp == None:
687 if fp == None:
671 fp = self.fp
688 fp = self.fp
672
689
673 self.radarControllerHeaderObj.read(fp)
690 self.radarControllerHeaderObj.read(fp)
674
691
675 def __rdSystemHeader(self, fp=None):
692 def __rdSystemHeader(self, fp=None):
676 if fp == None:
693 if fp == None:
677 fp = self.fp
694 fp = self.fp
678
695
679 self.systemHeaderObj.read(fp)
696 self.systemHeaderObj.read(fp)
680
697
681 def __rdBasicHeader(self, fp=None):
698 def __rdBasicHeader(self, fp=None):
682 if fp == None:
699 if fp == None:
683 fp = self.fp
700 fp = self.fp
684
701
685 self.basicHeaderObj.read(fp)
702 self.basicHeaderObj.read(fp)
686
703
687
704
688 def __readFirstHeader(self):
705 def __readFirstHeader(self):
689 self.__rdBasicHeader()
706 self.__rdBasicHeader()
690 self.__rdSystemHeader()
707 self.__rdSystemHeader()
691 self.__rdRadarControllerHeader()
708 self.__rdRadarControllerHeader()
692 self.__rdProcessingHeader()
709 self.__rdProcessingHeader()
693
710
694 self.firstHeaderSize = self.basicHeaderObj.size
711 self.firstHeaderSize = self.basicHeaderObj.size
695
712
696 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
713 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
697 if datatype == 0:
714 if datatype == 0:
698 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
715 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
699 elif datatype == 1:
716 elif datatype == 1:
700 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
717 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
701 elif datatype == 2:
718 elif datatype == 2:
702 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
719 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
703 elif datatype == 3:
720 elif datatype == 3:
704 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
721 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
705 elif datatype == 4:
722 elif datatype == 4:
706 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
723 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
707 elif datatype == 5:
724 elif datatype == 5:
708 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
725 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
709 else:
726 else:
710 raise ValueError, 'Data type was not defined'
727 raise ValueError, 'Data type was not defined'
711
728
712 self.dtype = datatype_str
729 self.dtype = datatype_str
713 self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
730 self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
714 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
731 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
715 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
732 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
716 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
733 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
717 self.getBlockDimension()
734 self.getBlockDimension()
718
735
719
736
720 def __verifyFile(self, filename, msgFlag=True):
737 def __verifyFile(self, filename, msgFlag=True):
721 msg = None
738 msg = None
722 try:
739 try:
723 fp = open(filename, 'rb')
740 fp = open(filename, 'rb')
724 currentPosition = fp.tell()
741 currentPosition = fp.tell()
725 except:
742 except:
726 if msgFlag:
743 if msgFlag:
727 print "The file %s can't be opened" % (filename)
744 print "The file %s can't be opened" % (filename)
728 return False
745 return False
729
746
730 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
747 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
731
748
732 if neededSize == 0:
749 if neededSize == 0:
733 basicHeaderObj = BasicHeader(LOCALTIME)
750 basicHeaderObj = BasicHeader(LOCALTIME)
734 systemHeaderObj = SystemHeader()
751 systemHeaderObj = SystemHeader()
735 radarControllerHeaderObj = RadarControllerHeader()
752 radarControllerHeaderObj = RadarControllerHeader()
736 processingHeaderObj = ProcessingHeader()
753 processingHeaderObj = ProcessingHeader()
737
754
738 try:
755 try:
739 if not( basicHeaderObj.read(fp) ): raise IOError
756 if not( basicHeaderObj.read(fp) ): raise IOError
740 if not( systemHeaderObj.read(fp) ): raise IOError
757 if not( systemHeaderObj.read(fp) ): raise IOError
741 if not( radarControllerHeaderObj.read(fp) ): raise IOError
758 if not( radarControllerHeaderObj.read(fp) ): raise IOError
742 if not( processingHeaderObj.read(fp) ): raise IOError
759 if not( processingHeaderObj.read(fp) ): raise IOError
743 data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
760 data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
744
761
745 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
762 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
746
763
747 except:
764 except:
748 if msgFlag:
765 if msgFlag:
749 print "\tThe file %s is empty or it hasn't enough data" % filename
766 print "\tThe file %s is empty or it hasn't enough data" % filename
750
767
751 fp.close()
768 fp.close()
752 return False
769 return False
753 else:
770 else:
754 msg = "\tSkipping the file %s due to it hasn't enough data" %filename
771 msg = "\tSkipping the file %s due to it hasn't enough data" %filename
755
772
756 fp.close()
773 fp.close()
757 fileSize = os.path.getsize(filename)
774 fileSize = os.path.getsize(filename)
758 currentSize = fileSize - currentPosition
775 currentSize = fileSize - currentPosition
759 if currentSize < neededSize:
776 if currentSize < neededSize:
760 if msgFlag and (msg != None):
777 if msgFlag and (msg != None):
761 print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename
778 print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename
762 return False
779 return False
763
780
764 return True
781 return True
765
782
766 def setup(self,
783 def setup(self,
767 path=None,
784 path=None,
768 startDate=None,
785 startDate=None,
769 endDate=None,
786 endDate=None,
770 startTime=datetime.time(0,0,0),
787 startTime=datetime.time(0,0,0),
771 endTime=datetime.time(23,59,59),
788 endTime=datetime.time(23,59,59),
772 set=0,
789 set=0,
773 expLabel = "",
790 expLabel = "",
774 ext = None,
791 ext = None,
775 online = False,
792 online = False,
776 delay = 60,
793 delay = 60,
777 walk = True):
794 walk = True):
778
795
779 if path == None:
796 if path == None:
780 raise ValueError, "The path is not valid"
797 raise ValueError, "The path is not valid"
781
798
782 if ext == None:
799 if ext == None:
783 ext = self.ext
800 ext = self.ext
784
801
785 if online:
802 if online:
786 print "Searching files in online mode..."
803 print "Searching files in online mode..."
787
804
788 for nTries in range( self.nTries ):
805 for nTries in range( self.nTries ):
789 fullpath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk)
806 fullpath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk)
790
807
791 if fullpath:
808 if fullpath:
792 break
809 break
793
810
794 print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
811 print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
795 time.sleep( self.delay )
812 time.sleep( self.delay )
796
813
797 if not(fullpath):
814 if not(fullpath):
798 print "There 'isn't valied files in %s" % path
815 print "There 'isn't valied files in %s" % path
799 return None
816 return None
800
817
801 self.year = year
818 self.year = year
802 self.doy = doy
819 self.doy = doy
803 self.set = set - 1
820 self.set = set - 1
804 self.path = path
821 self.path = path
805
822
806 else:
823 else:
807 print "Searching files in offline mode ..."
824 print "Searching files in offline mode ..."
808 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
825 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
809 startTime=startTime, endTime=endTime,
826 startTime=startTime, endTime=endTime,
810 set=set, expLabel=expLabel, ext=ext,
827 set=set, expLabel=expLabel, ext=ext,
811 walk=walk)
828 walk=walk)
812
829
813 if not(pathList):
830 if not(pathList):
814 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
831 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
815 datetime.datetime.combine(startDate,startTime).ctime(),
832 datetime.datetime.combine(startDate,startTime).ctime(),
816 datetime.datetime.combine(endDate,endTime).ctime())
833 datetime.datetime.combine(endDate,endTime).ctime())
817
834
818 sys.exit(-1)
835 sys.exit(-1)
819
836
820
837
821 self.fileIndex = -1
838 self.fileIndex = -1
822 self.pathList = pathList
839 self.pathList = pathList
823 self.filenameList = filenameList
840 self.filenameList = filenameList
824
841
825 self.online = online
842 self.online = online
826 self.delay = delay
843 self.delay = delay
827 ext = ext.lower()
844 ext = ext.lower()
828 self.ext = ext
845 self.ext = ext
829
846
830 if not(self.setNextFile()):
847 if not(self.setNextFile()):
831 if (startDate!=None) and (endDate!=None):
848 if (startDate!=None) and (endDate!=None):
832 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
849 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
833 elif startDate != None:
850 elif startDate != None:
834 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
851 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
835 else:
852 else:
836 print "No files"
853 print "No files"
837
854
838 sys.exit(-1)
855 sys.exit(-1)
839
856
840 # self.updateDataHeader()
857 # self.updateDataHeader()
841
858
842 return self.dataOut
859 return self.dataOut
843
860
844 def getData():
861 def getData():
845
862
846 raise ValueError, "This method has not been implemented"
863 raise ValueError, "This method has not been implemented"
847
864
848 def hasNotDataInBuffer():
865 def hasNotDataInBuffer():
849
866
850 raise ValueError, "This method has not been implemented"
867 raise ValueError, "This method has not been implemented"
851
868
852 def readBlock():
869 def readBlock():
853
870
854 raise ValueError, "This method has not been implemented"
871 raise ValueError, "This method has not been implemented"
855
872
856 def isEndProcess(self):
873 def isEndProcess(self):
857
874
858 return self.flagNoMoreFiles
875 return self.flagNoMoreFiles
859
876
860 def printReadBlocks(self):
877 def printReadBlocks(self):
861
878
862 print "Number of read blocks per file %04d" %self.nReadBlocks
879 print "Number of read blocks per file %04d" %self.nReadBlocks
863
880
864 def printTotalBlocks(self):
881 def printTotalBlocks(self):
865
882
866 print "Number of read blocks %04d" %self.nTotalBlocks
883 print "Number of read blocks %04d" %self.nTotalBlocks
867
884
868 def printNumberOfBlock(self):
885 def printNumberOfBlock(self):
869
886
870 if self.flagIsNewBlock:
887 if self.flagIsNewBlock:
871 print "Block No. %04d, Total blocks %04d" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks)
888 print "Block No. %04d, Total blocks %04d" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks)
872
889
873 def printInfo(self):
890 def printInfo(self):
874
891
875 print self.basicHeaderObj.printInfo()
892 print self.basicHeaderObj.printInfo()
876 print self.systemHeaderObj.printInfo()
893 print self.systemHeaderObj.printInfo()
877 print self.radarControllerHeaderObj.printInfo()
894 print self.radarControllerHeaderObj.printInfo()
878 print self.processingHeaderObj.printInfo()
895 print self.processingHeaderObj.printInfo()
879
896
880
897
881 def run(self, **kwargs):
898 def run(self, **kwargs):
882
899
883 if not(self.isConfig):
900 if not(self.isConfig):
884
901
885 # self.dataOut = dataOut
902 # self.dataOut = dataOut
886 self.setup(**kwargs)
903 self.setup(**kwargs)
887 self.isConfig = True
904 self.isConfig = True
888
905
889 self.getData()
906 self.getData()
890
907
891 class JRODataWriter(JRODataIO, Operation):
908 class JRODataWriter(JRODataIO, Operation):
892
909
893 """
910 """
894 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
911 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
895 de los datos siempre se realiza por bloques.
912 de los datos siempre se realiza por bloques.
896 """
913 """
897
914
898 blockIndex = 0
915 blockIndex = 0
899
916
900 path = None
917 path = None
901
918
902 setFile = None
919 setFile = None
903
920
904 profilesPerBlock = None
921 profilesPerBlock = None
905
922
906 blocksPerFile = None
923 blocksPerFile = None
907
924
908 nWriteBlocks = 0
925 nWriteBlocks = 0
909
926
910 def __init__(self, dataOut=None):
927 def __init__(self, dataOut=None):
911 raise ValueError, "Not implemented"
928 raise ValueError, "Not implemented"
912
929
913
930
914 def hasAllDataInBuffer(self):
931 def hasAllDataInBuffer(self):
915 raise ValueError, "Not implemented"
932 raise ValueError, "Not implemented"
916
933
917
934
918 def setBlockDimension(self):
935 def setBlockDimension(self):
919 raise ValueError, "Not implemented"
936 raise ValueError, "Not implemented"
920
937
921
938
922 def writeBlock(self):
939 def writeBlock(self):
923 raise ValueError, "No implemented"
940 raise ValueError, "No implemented"
924
941
925
942
926 def putData(self):
943 def putData(self):
927 raise ValueError, "No implemented"
944 raise ValueError, "No implemented"
928
945
929 def getDataHeader(self):
946 def getDataHeader(self):
930 """
947 """
931 Obtiene una copia del First Header
948 Obtiene una copia del First Header
932
949
933 Affected:
950 Affected:
934
951
935 self.basicHeaderObj
952 self.basicHeaderObj
936 self.systemHeaderObj
953 self.systemHeaderObj
937 self.radarControllerHeaderObj
954 self.radarControllerHeaderObj
938 self.processingHeaderObj self.
955 self.processingHeaderObj self.
939
956
940 Return:
957 Return:
941 None
958 None
942 """
959 """
943
960
944 raise ValueError, "No implemented"
961 raise ValueError, "No implemented"
945
962
946 def getBasicHeader(self):
963 def getBasicHeader(self):
947
964
948 self.basicHeaderObj.size = self.basicHeaderSize #bytes
965 self.basicHeaderObj.size = self.basicHeaderSize #bytes
949 self.basicHeaderObj.version = self.versionFile
966 self.basicHeaderObj.version = self.versionFile
950 self.basicHeaderObj.dataBlock = self.nTotalBlocks
967 self.basicHeaderObj.dataBlock = self.nTotalBlocks
951
968
952 utc = numpy.floor(self.dataOut.utctime)
969 utc = numpy.floor(self.dataOut.utctime)
953 milisecond = (self.dataOut.utctime - utc)* 1000.0
970 milisecond = (self.dataOut.utctime - utc)* 1000.0
954
971
955 self.basicHeaderObj.utc = utc
972 self.basicHeaderObj.utc = utc
956 self.basicHeaderObj.miliSecond = milisecond
973 self.basicHeaderObj.miliSecond = milisecond
957 self.basicHeaderObj.timeZone = 0
974 self.basicHeaderObj.timeZone = 0
958 self.basicHeaderObj.dstFlag = 0
975 self.basicHeaderObj.dstFlag = 0
959 self.basicHeaderObj.errorCount = 0
976 self.basicHeaderObj.errorCount = 0
960
977
961 def __writeFirstHeader(self):
978 def __writeFirstHeader(self):
962 """
979 """
963 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
980 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
964
981
965 Affected:
982 Affected:
966 __dataType
983 __dataType
967
984
968 Return:
985 Return:
969 None
986 None
970 """
987 """
971
988
972 # CALCULAR PARAMETROS
989 # CALCULAR PARAMETROS
973
990
974 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
991 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
975 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
992 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
976
993
977 self.basicHeaderObj.write(self.fp)
994 self.basicHeaderObj.write(self.fp)
978 self.systemHeaderObj.write(self.fp)
995 self.systemHeaderObj.write(self.fp)
979 self.radarControllerHeaderObj.write(self.fp)
996 self.radarControllerHeaderObj.write(self.fp)
980 self.processingHeaderObj.write(self.fp)
997 self.processingHeaderObj.write(self.fp)
981
998
982 self.dtype = self.dataOut.dtype
999 self.dtype = self.dataOut.dtype
983
1000
984 def __setNewBlock(self):
1001 def __setNewBlock(self):
985 """
1002 """
986 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1003 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
987
1004
988 Return:
1005 Return:
989 0 : si no pudo escribir nada
1006 0 : si no pudo escribir nada
990 1 : Si escribio el Basic el First Header
1007 1 : Si escribio el Basic el First Header
991 """
1008 """
992 if self.fp == None:
1009 if self.fp == None:
993 self.setNextFile()
1010 self.setNextFile()
994
1011
995 if self.flagIsNewFile:
1012 if self.flagIsNewFile:
996 return 1
1013 return 1
997
1014
998 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1015 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
999 self.basicHeaderObj.write(self.fp)
1016 self.basicHeaderObj.write(self.fp)
1000 return 1
1017 return 1
1001
1018
1002 if not( self.setNextFile() ):
1019 if not( self.setNextFile() ):
1003 return 0
1020 return 0
1004
1021
1005 return 1
1022 return 1
1006
1023
1007
1024
1008 def writeNextBlock(self):
1025 def writeNextBlock(self):
1009 """
1026 """
1010 Selecciona el bloque siguiente de datos y los escribe en un file
1027 Selecciona el bloque siguiente de datos y los escribe en un file
1011
1028
1012 Return:
1029 Return:
1013 0 : Si no hizo pudo escribir el bloque de datos
1030 0 : Si no hizo pudo escribir el bloque de datos
1014 1 : Si no pudo escribir el bloque de datos
1031 1 : Si no pudo escribir el bloque de datos
1015 """
1032 """
1016 if not( self.__setNewBlock() ):
1033 if not( self.__setNewBlock() ):
1017 return 0
1034 return 0
1018
1035
1019 self.writeBlock()
1036 self.writeBlock()
1020
1037
1021 return 1
1038 return 1
1022
1039
1023 def setNextFile(self):
1040 def setNextFile(self):
1024 """
1041 """
1025 Determina el siguiente file que sera escrito
1042 Determina el siguiente file que sera escrito
1026
1043
1027 Affected:
1044 Affected:
1028 self.filename
1045 self.filename
1029 self.subfolder
1046 self.subfolder
1030 self.fp
1047 self.fp
1031 self.setFile
1048 self.setFile
1032 self.flagIsNewFile
1049 self.flagIsNewFile
1033
1050
1034 Return:
1051 Return:
1035 0 : Si el archivo no puede ser escrito
1052 0 : Si el archivo no puede ser escrito
1036 1 : Si el archivo esta listo para ser escrito
1053 1 : Si el archivo esta listo para ser escrito
1037 """
1054 """
1038 ext = self.ext
1055 ext = self.ext
1039 path = self.path
1056 path = self.path
1040
1057
1041 if self.fp != None:
1058 if self.fp != None:
1042 self.fp.close()
1059 self.fp.close()
1043
1060
1044 timeTuple = time.localtime( self.dataOut.utctime)
1061 timeTuple = time.localtime( self.dataOut.utctime)
1045 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1062 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1046
1063
1047 fullpath = os.path.join( path, subfolder )
1064 fullpath = os.path.join( path, subfolder )
1048 if not( os.path.exists(fullpath) ):
1065 if not( os.path.exists(fullpath) ):
1049 os.mkdir(fullpath)
1066 os.mkdir(fullpath)
1050 self.setFile = -1 #inicializo mi contador de seteo
1067 self.setFile = -1 #inicializo mi contador de seteo
1051 else:
1068 else:
1052 filesList = os.listdir( fullpath )
1069 filesList = os.listdir( fullpath )
1053 if len( filesList ) > 0:
1070 if len( filesList ) > 0:
1054 filesList = sorted( filesList, key=str.lower )
1071 filesList = sorted( filesList, key=str.lower )
1055 filen = filesList[-1]
1072 filen = filesList[-1]
1056 # el filename debera tener el siguiente formato
1073 # el filename debera tener el siguiente formato
1057 # 0 1234 567 89A BCDE (hex)
1074 # 0 1234 567 89A BCDE (hex)
1058 # x YYYY DDD SSS .ext
1075 # x YYYY DDD SSS .ext
1059 if isNumber( filen[8:11] ):
1076 if isNumber( filen[8:11] ):
1060 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1077 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1061 else:
1078 else:
1062 self.setFile = -1
1079 self.setFile = -1
1063 else:
1080 else:
1064 self.setFile = -1 #inicializo mi contador de seteo
1081 self.setFile = -1 #inicializo mi contador de seteo
1065
1082
1066 setFile = self.setFile
1083 setFile = self.setFile
1067 setFile += 1
1084 setFile += 1
1068
1085
1069 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
1086 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
1070 timeTuple.tm_year,
1087 timeTuple.tm_year,
1071 timeTuple.tm_yday,
1088 timeTuple.tm_yday,
1072 setFile,
1089 setFile,
1073 ext )
1090 ext )
1074
1091
1075 filename = os.path.join( path, subfolder, file )
1092 filename = os.path.join( path, subfolder, file )
1076
1093
1077 fp = open( filename,'wb' )
1094 fp = open( filename,'wb' )
1078
1095
1079 self.blockIndex = 0
1096 self.blockIndex = 0
1080
1097
1081 #guardando atributos
1098 #guardando atributos
1082 self.filename = filename
1099 self.filename = filename
1083 self.subfolder = subfolder
1100 self.subfolder = subfolder
1084 self.fp = fp
1101 self.fp = fp
1085 self.setFile = setFile
1102 self.setFile = setFile
1086 self.flagIsNewFile = 1
1103 self.flagIsNewFile = 1
1087
1104
1088 self.getDataHeader()
1105 self.getDataHeader()
1089
1106
1090 print 'Writing the file: %s'%self.filename
1107 print 'Writing the file: %s'%self.filename
1091
1108
1092 self.__writeFirstHeader()
1109 self.__writeFirstHeader()
1093
1110
1094 return 1
1111 return 1
1095
1112
1096 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=None, set=0, ext=None):
1113 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=None, set=0, ext=None):
1097 """
1114 """
1098 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1115 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1099
1116
1100 Inputs:
1117 Inputs:
1101 path : el path destino en el cual se escribiran los files a crear
1118 path : el path destino en el cual se escribiran los files a crear
1102 format : formato en el cual sera salvado un file
1119 format : formato en el cual sera salvado un file
1103 set : el setebo del file
1120 set : el setebo del file
1104
1121
1105 Return:
1122 Return:
1106 0 : Si no realizo un buen seteo
1123 0 : Si no realizo un buen seteo
1107 1 : Si realizo un buen seteo
1124 1 : Si realizo un buen seteo
1108 """
1125 """
1109
1126
1110 if ext == None:
1127 if ext == None:
1111 ext = self.ext
1128 ext = self.ext
1112
1129
1113 ext = ext.lower()
1130 ext = ext.lower()
1114
1131
1115 self.ext = ext
1132 self.ext = ext
1116
1133
1117 self.path = path
1134 self.path = path
1118
1135
1119 self.setFile = set - 1
1136 self.setFile = set - 1
1120
1137
1121 self.blocksPerFile = blocksPerFile
1138 self.blocksPerFile = blocksPerFile
1122
1139
1123 self.profilesPerBlock = profilesPerBlock
1140 self.profilesPerBlock = profilesPerBlock
1124
1141
1125 self.dataOut = dataOut
1142 self.dataOut = dataOut
1126
1143
1127 if not(self.setNextFile()):
1144 if not(self.setNextFile()):
1128 print "There isn't a next file"
1145 print "There isn't a next file"
1129 return 0
1146 return 0
1130
1147
1131 self.setBlockDimension()
1148 self.setBlockDimension()
1132
1149
1133 return 1
1150 return 1
1134
1151
1135 def run(self, dataOut, **kwargs):
1152 def run(self, dataOut, **kwargs):
1136
1153
1137 if not(self.isConfig):
1154 if not(self.isConfig):
1138
1155
1139 self.setup(dataOut, **kwargs)
1156 self.setup(dataOut, **kwargs)
1140 self.isConfig = True
1157 self.isConfig = True
1141
1158
1142 self.putData()
1159 self.putData()
1143
1160
1144 class VoltageReader(JRODataReader):
1161 class VoltageReader(JRODataReader):
1145 """
1162 """
1146 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
1163 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
1147 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
1164 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
1148 perfiles*alturas*canales) son almacenados en la variable "buffer".
1165 perfiles*alturas*canales) son almacenados en la variable "buffer".
1149
1166
1150 perfiles * alturas * canales
1167 perfiles * alturas * canales
1151
1168
1152 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1169 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1153 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
1170 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
1154 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
1171 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
1155 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1172 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1156
1173
1157 Example:
1174 Example:
1158
1175
1159 dpath = "/home/myuser/data"
1176 dpath = "/home/myuser/data"
1160
1177
1161 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1178 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1162
1179
1163 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1180 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1164
1181
1165 readerObj = VoltageReader()
1182 readerObj = VoltageReader()
1166
1183
1167 readerObj.setup(dpath, startTime, endTime)
1184 readerObj.setup(dpath, startTime, endTime)
1168
1185
1169 while(True):
1186 while(True):
1170
1187
1171 #to get one profile
1188 #to get one profile
1172 profile = readerObj.getData()
1189 profile = readerObj.getData()
1173
1190
1174 #print the profile
1191 #print the profile
1175 print profile
1192 print profile
1176
1193
1177 #If you want to see all datablock
1194 #If you want to see all datablock
1178 print readerObj.datablock
1195 print readerObj.datablock
1179
1196
1180 if readerObj.flagNoMoreFiles:
1197 if readerObj.flagNoMoreFiles:
1181 break
1198 break
1182
1199
1183 """
1200 """
1184
1201
1185 ext = ".r"
1202 ext = ".r"
1186
1203
1187 optchar = "D"
1204 optchar = "D"
1188 dataOut = None
1205 dataOut = None
1189
1206
1190
1207
1191 def __init__(self):
1208 def __init__(self):
1192 """
1209 """
1193 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
1210 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
1194
1211
1195 Input:
1212 Input:
1196 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
1213 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
1197 almacenar un perfil de datos cada vez que se haga un requerimiento
1214 almacenar un perfil de datos cada vez que se haga un requerimiento
1198 (getData). El perfil sera obtenido a partir del buffer de datos,
1215 (getData). El perfil sera obtenido a partir del buffer de datos,
1199 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1216 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1200 bloque de datos.
1217 bloque de datos.
1201 Si este parametro no es pasado se creara uno internamente.
1218 Si este parametro no es pasado se creara uno internamente.
1202
1219
1203 Variables afectadas:
1220 Variables afectadas:
1204 self.dataOut
1221 self.dataOut
1205
1222
1206 Return:
1223 Return:
1207 None
1224 None
1208 """
1225 """
1209
1226
1210 self.isConfig = False
1227 self.isConfig = False
1211
1228
1212 self.datablock = None
1229 self.datablock = None
1213
1230
1214 self.utc = 0
1231 self.utc = 0
1215
1232
1216 self.ext = ".r"
1233 self.ext = ".r"
1217
1234
1218 self.optchar = "D"
1235 self.optchar = "D"
1219
1236
1220 self.basicHeaderObj = BasicHeader(LOCALTIME)
1237 self.basicHeaderObj = BasicHeader(LOCALTIME)
1221
1238
1222 self.systemHeaderObj = SystemHeader()
1239 self.systemHeaderObj = SystemHeader()
1223
1240
1224 self.radarControllerHeaderObj = RadarControllerHeader()
1241 self.radarControllerHeaderObj = RadarControllerHeader()
1225
1242
1226 self.processingHeaderObj = ProcessingHeader()
1243 self.processingHeaderObj = ProcessingHeader()
1227
1244
1228 self.online = 0
1245 self.online = 0
1229
1246
1230 self.fp = None
1247 self.fp = None
1231
1248
1232 self.idFile = None
1249 self.idFile = None
1233
1250
1234 self.dtype = None
1251 self.dtype = None
1235
1252
1236 self.fileSizeByHeader = None
1253 self.fileSizeByHeader = None
1237
1254
1238 self.filenameList = []
1255 self.filenameList = []
1239
1256
1240 self.filename = None
1257 self.filename = None
1241
1258
1242 self.fileSize = None
1259 self.fileSize = None
1243
1260
1244 self.firstHeaderSize = 0
1261 self.firstHeaderSize = 0
1245
1262
1246 self.basicHeaderSize = 24
1263 self.basicHeaderSize = 24
1247
1264
1248 self.pathList = []
1265 self.pathList = []
1249
1266
1250 self.filenameList = []
1267 self.filenameList = []
1251
1268
1252 self.lastUTTime = 0
1269 self.lastUTTime = 0
1253
1270
1254 self.maxTimeStep = 30
1271 self.maxTimeStep = 30
1255
1272
1256 self.flagNoMoreFiles = 0
1273 self.flagNoMoreFiles = 0
1257
1274
1258 self.set = 0
1275 self.set = 0
1259
1276
1260 self.path = None
1277 self.path = None
1261
1278
1262 self.profileIndex = 9999
1279 self.profileIndex = 9999
1263
1280
1264 self.delay = 3 #seconds
1281 self.delay = 3 #seconds
1265
1282
1266 self.nTries = 3 #quantity tries
1283 self.nTries = 3 #quantity tries
1267
1284
1268 self.nFiles = 3 #number of files for searching
1285 self.nFiles = 3 #number of files for searching
1269
1286
1270 self.nReadBlocks = 0
1287 self.nReadBlocks = 0
1271
1288
1272 self.flagIsNewFile = 1
1289 self.flagIsNewFile = 1
1273
1290
1274 self.ippSeconds = 0
1291 self.ippSeconds = 0
1275
1292
1276 self.flagTimeBlock = 0
1293 self.flagTimeBlock = 0
1277
1294
1278 self.flagIsNewBlock = 0
1295 self.flagIsNewBlock = 0
1279
1296
1280 self.nTotalBlocks = 0
1297 self.nTotalBlocks = 0
1281
1298
1282 self.blocksize = 0
1299 self.blocksize = 0
1283
1300
1284 self.dataOut = self.createObjByDefault()
1301 self.dataOut = self.createObjByDefault()
1285
1302
1286 def createObjByDefault(self):
1303 def createObjByDefault(self):
1287
1304
1288 dataObj = Voltage()
1305 dataObj = Voltage()
1289
1306
1290 return dataObj
1307 return dataObj
1291
1308
1292 def __hasNotDataInBuffer(self):
1309 def __hasNotDataInBuffer(self):
1293 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1310 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1294 return 1
1311 return 1
1295 return 0
1312 return 0
1296
1313
1297
1314
1298 def getBlockDimension(self):
1315 def getBlockDimension(self):
1299 """
1316 """
1300 Obtiene la cantidad de puntos a leer por cada bloque de datos
1317 Obtiene la cantidad de puntos a leer por cada bloque de datos
1301
1318
1302 Affected:
1319 Affected:
1303 self.blocksize
1320 self.blocksize
1304
1321
1305 Return:
1322 Return:
1306 None
1323 None
1307 """
1324 """
1308 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
1325 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
1309 self.blocksize = pts2read
1326 self.blocksize = pts2read
1310
1327
1311
1328
1312 def readBlock(self):
1329 def readBlock(self):
1313 """
1330 """
1314 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
1331 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
1315 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1332 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1316 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1333 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1317 es seteado a 0
1334 es seteado a 0
1318
1335
1319 Inputs:
1336 Inputs:
1320 None
1337 None
1321
1338
1322 Return:
1339 Return:
1323 None
1340 None
1324
1341
1325 Affected:
1342 Affected:
1326 self.profileIndex
1343 self.profileIndex
1327 self.datablock
1344 self.datablock
1328 self.flagIsNewFile
1345 self.flagIsNewFile
1329 self.flagIsNewBlock
1346 self.flagIsNewBlock
1330 self.nTotalBlocks
1347 self.nTotalBlocks
1331
1348
1332 Exceptions:
1349 Exceptions:
1333 Si un bloque leido no es un bloque valido
1350 Si un bloque leido no es un bloque valido
1334 """
1351 """
1335
1352
1336 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
1353 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
1337
1354
1338 try:
1355 try:
1339 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
1356 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
1340 except:
1357 except:
1341 print "The read block (%3d) has not enough data" %self.nReadBlocks
1358 print "The read block (%3d) has not enough data" %self.nReadBlocks
1342 return 0
1359 return 0
1343
1360
1344 junk = numpy.transpose(junk, (2,0,1))
1361 junk = numpy.transpose(junk, (2,0,1))
1345 self.datablock = junk['real'] + junk['imag']*1j
1362 self.datablock = junk['real'] + junk['imag']*1j
1346
1363
1347 self.profileIndex = 0
1364 self.profileIndex = 0
1348
1365
1349 self.flagIsNewFile = 0
1366 self.flagIsNewFile = 0
1350 self.flagIsNewBlock = 1
1367 self.flagIsNewBlock = 1
1351
1368
1352 self.nTotalBlocks += 1
1369 self.nTotalBlocks += 1
1353 self.nReadBlocks += 1
1370 self.nReadBlocks += 1
1354
1371
1355 return 1
1372 return 1
1356
1373
1357
1374
1358 def getData(self):
1375 def getData(self):
1359 """
1376 """
1360 getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage"
1377 getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage"
1361 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
1378 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
1362 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
1379 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
1363
1380
1364 Ademas incrementa el contador del buffer en 1.
1381 Ademas incrementa el contador del buffer en 1.
1365
1382
1366 Return:
1383 Return:
1367 data : retorna un perfil de voltages (alturas * canales) copiados desde el
1384 data : retorna un perfil de voltages (alturas * canales) copiados desde el
1368 buffer. Si no hay mas archivos a leer retorna None.
1385 buffer. Si no hay mas archivos a leer retorna None.
1369
1386
1370 Variables afectadas:
1387 Variables afectadas:
1371 self.dataOut
1388 self.dataOut
1372 self.profileIndex
1389 self.profileIndex
1373
1390
1374 Affected:
1391 Affected:
1375 self.dataOut
1392 self.dataOut
1376 self.profileIndex
1393 self.profileIndex
1377 self.flagTimeBlock
1394 self.flagTimeBlock
1378 self.flagIsNewBlock
1395 self.flagIsNewBlock
1379 """
1396 """
1380
1397
1381 if self.flagNoMoreFiles:
1398 if self.flagNoMoreFiles:
1382 self.dataOut.flagNoData = True
1399 self.dataOut.flagNoData = True
1383 print 'Process finished'
1400 print 'Process finished'
1384 return 0
1401 return 0
1385
1402
1386 self.flagTimeBlock = 0
1403 self.flagTimeBlock = 0
1387 self.flagIsNewBlock = 0
1404 self.flagIsNewBlock = 0
1388
1405
1389 if self.__hasNotDataInBuffer():
1406 if self.__hasNotDataInBuffer():
1390
1407
1391 if not( self.readNextBlock() ):
1408 if not( self.readNextBlock() ):
1392 return 0
1409 return 0
1393
1410
1394 self.dataOut.dtype = self.dtype
1411 self.dataOut.dtype = self.dtype
1395
1412
1396 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
1413 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
1397
1414
1398 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
1415 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
1399
1416
1400 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
1417 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
1401
1418
1402 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
1419 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
1403
1420
1404 self.dataOut.flagTimeBlock = self.flagTimeBlock
1421 self.dataOut.flagTimeBlock = self.flagTimeBlock
1405
1422
1406 self.dataOut.ippSeconds = self.ippSeconds
1423 self.dataOut.ippSeconds = self.ippSeconds
1407
1424
1408 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt
1425 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt
1409
1426
1410 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
1427 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
1411
1428
1412 self.dataOut.flagShiftFFT = False
1429 self.dataOut.flagShiftFFT = False
1413
1430
1414 if self.radarControllerHeaderObj.code != None:
1431 if self.radarControllerHeaderObj.code != None:
1415
1432
1416 self.dataOut.nCode = self.radarControllerHeaderObj.nCode
1433 self.dataOut.nCode = self.radarControllerHeaderObj.nCode
1417
1434
1418 self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
1435 self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
1419
1436
1420 self.dataOut.code = self.radarControllerHeaderObj.code
1437 self.dataOut.code = self.radarControllerHeaderObj.code
1421
1438
1422 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
1439 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
1423
1440
1424 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
1441 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
1425
1442
1426 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
1443 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
1427
1444
1428 self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip
1445 self.dataOut.flagDeflipData = False #asumo q la data no esta sin flip
1429
1446
1430 self.dataOut.flagShiftFFT = False
1447 self.dataOut.flagShiftFFT = False
1431
1448
1432
1449
1433 # self.updateDataHeader()
1450 # self.updateDataHeader()
1434
1451
1435 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
1452 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
1436
1453
1437 if self.datablock == None:
1454 if self.datablock == None:
1438 self.dataOut.flagNoData = True
1455 self.dataOut.flagNoData = True
1439 return 0
1456 return 0
1440
1457
1441 self.dataOut.data = self.datablock[:,self.profileIndex,:]
1458 self.dataOut.data = self.datablock[:,self.profileIndex,:]
1442
1459
1443 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.ippSeconds
1460 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.ippSeconds
1444
1461
1445 self.profileIndex += 1
1462 self.profileIndex += 1
1446
1463
1447 self.dataOut.flagNoData = False
1464 self.dataOut.flagNoData = False
1448
1465
1449 # print self.profileIndex, self.dataOut.utctime
1466 # print self.profileIndex, self.dataOut.utctime
1450 # if self.profileIndex == 800:
1467 # if self.profileIndex == 800:
1451 # a=1
1468 # a=1
1452
1469
1453
1470
1454 return self.dataOut.data
1471 return self.dataOut.data
1455
1472
1456
1473
1457 class VoltageWriter(JRODataWriter):
1474 class VoltageWriter(JRODataWriter):
1458 """
1475 """
1459 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
1476 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
1460 de los datos siempre se realiza por bloques.
1477 de los datos siempre se realiza por bloques.
1461 """
1478 """
1462
1479
1463 ext = ".r"
1480 ext = ".r"
1464
1481
1465 optchar = "D"
1482 optchar = "D"
1466
1483
1467 shapeBuffer = None
1484 shapeBuffer = None
1468
1485
1469
1486
1470 def __init__(self):
1487 def __init__(self):
1471 """
1488 """
1472 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
1489 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
1473
1490
1474 Affected:
1491 Affected:
1475 self.dataOut
1492 self.dataOut
1476
1493
1477 Return: None
1494 Return: None
1478 """
1495 """
1479
1496
1480 self.nTotalBlocks = 0
1497 self.nTotalBlocks = 0
1481
1498
1482 self.profileIndex = 0
1499 self.profileIndex = 0
1483
1500
1484 self.isConfig = False
1501 self.isConfig = False
1485
1502
1486 self.fp = None
1503 self.fp = None
1487
1504
1488 self.flagIsNewFile = 1
1505 self.flagIsNewFile = 1
1489
1506
1490 self.nTotalBlocks = 0
1507 self.nTotalBlocks = 0
1491
1508
1492 self.flagIsNewBlock = 0
1509 self.flagIsNewBlock = 0
1493
1510
1494 self.setFile = None
1511 self.setFile = None
1495
1512
1496 self.dtype = None
1513 self.dtype = None
1497
1514
1498 self.path = None
1515 self.path = None
1499
1516
1500 self.filename = None
1517 self.filename = None
1501
1518
1502 self.basicHeaderObj = BasicHeader(LOCALTIME)
1519 self.basicHeaderObj = BasicHeader(LOCALTIME)
1503
1520
1504 self.systemHeaderObj = SystemHeader()
1521 self.systemHeaderObj = SystemHeader()
1505
1522
1506 self.radarControllerHeaderObj = RadarControllerHeader()
1523 self.radarControllerHeaderObj = RadarControllerHeader()
1507
1524
1508 self.processingHeaderObj = ProcessingHeader()
1525 self.processingHeaderObj = ProcessingHeader()
1509
1526
1510 def hasAllDataInBuffer(self):
1527 def hasAllDataInBuffer(self):
1511 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1528 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1512 return 1
1529 return 1
1513 return 0
1530 return 0
1514
1531
1515
1532
1516 def setBlockDimension(self):
1533 def setBlockDimension(self):
1517 """
1534 """
1518 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
1535 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
1519
1536
1520 Affected:
1537 Affected:
1521 self.shape_spc_Buffer
1538 self.shape_spc_Buffer
1522 self.shape_cspc_Buffer
1539 self.shape_cspc_Buffer
1523 self.shape_dc_Buffer
1540 self.shape_dc_Buffer
1524
1541
1525 Return: None
1542 Return: None
1526 """
1543 """
1527 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
1544 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
1528 self.processingHeaderObj.nHeights,
1545 self.processingHeaderObj.nHeights,
1529 self.systemHeaderObj.nChannels)
1546 self.systemHeaderObj.nChannels)
1530
1547
1531 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
1548 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
1532 self.processingHeaderObj.profilesPerBlock,
1549 self.processingHeaderObj.profilesPerBlock,
1533 self.processingHeaderObj.nHeights),
1550 self.processingHeaderObj.nHeights),
1534 dtype=numpy.dtype('complex64'))
1551 dtype=numpy.dtype('complex64'))
1535
1552
1536
1553
1537 def writeBlock(self):
1554 def writeBlock(self):
1538 """
1555 """
1539 Escribe el buffer en el file designado
1556 Escribe el buffer en el file designado
1540
1557
1541 Affected:
1558 Affected:
1542 self.profileIndex
1559 self.profileIndex
1543 self.flagIsNewFile
1560 self.flagIsNewFile
1544 self.flagIsNewBlock
1561 self.flagIsNewBlock
1545 self.nTotalBlocks
1562 self.nTotalBlocks
1546 self.blockIndex
1563 self.blockIndex
1547
1564
1548 Return: None
1565 Return: None
1549 """
1566 """
1550 data = numpy.zeros( self.shapeBuffer, self.dtype )
1567 data = numpy.zeros( self.shapeBuffer, self.dtype )
1551
1568
1552 junk = numpy.transpose(self.datablock, (1,2,0))
1569 junk = numpy.transpose(self.datablock, (1,2,0))
1553
1570
1554 data['real'] = junk.real
1571 data['real'] = junk.real
1555 data['imag'] = junk.imag
1572 data['imag'] = junk.imag
1556
1573
1557 data = data.reshape( (-1) )
1574 data = data.reshape( (-1) )
1558
1575
1559 data.tofile( self.fp )
1576 data.tofile( self.fp )
1560
1577
1561 self.datablock.fill(0)
1578 self.datablock.fill(0)
1562
1579
1563 self.profileIndex = 0
1580 self.profileIndex = 0
1564 self.flagIsNewFile = 0
1581 self.flagIsNewFile = 0
1565 self.flagIsNewBlock = 1
1582 self.flagIsNewBlock = 1
1566
1583
1567 self.blockIndex += 1
1584 self.blockIndex += 1
1568 self.nTotalBlocks += 1
1585 self.nTotalBlocks += 1
1569
1586
1570 def putData(self):
1587 def putData(self):
1571 """
1588 """
1572 Setea un bloque de datos y luego los escribe en un file
1589 Setea un bloque de datos y luego los escribe en un file
1573
1590
1574 Affected:
1591 Affected:
1575 self.flagIsNewBlock
1592 self.flagIsNewBlock
1576 self.profileIndex
1593 self.profileIndex
1577
1594
1578 Return:
1595 Return:
1579 0 : Si no hay data o no hay mas files que puedan escribirse
1596 0 : Si no hay data o no hay mas files que puedan escribirse
1580 1 : Si se escribio la data de un bloque en un file
1597 1 : Si se escribio la data de un bloque en un file
1581 """
1598 """
1582 if self.dataOut.flagNoData:
1599 if self.dataOut.flagNoData:
1583 return 0
1600 return 0
1584
1601
1585 self.flagIsNewBlock = 0
1602 self.flagIsNewBlock = 0
1586
1603
1587 if self.dataOut.flagTimeBlock:
1604 if self.dataOut.flagTimeBlock:
1588
1605
1589 self.datablock.fill(0)
1606 self.datablock.fill(0)
1590 self.profileIndex = 0
1607 self.profileIndex = 0
1591 self.setNextFile()
1608 self.setNextFile()
1592
1609
1593 if self.profileIndex == 0:
1610 if self.profileIndex == 0:
1594 self.getBasicHeader()
1611 self.getBasicHeader()
1595
1612
1596 self.datablock[:,self.profileIndex,:] = self.dataOut.data
1613 self.datablock[:,self.profileIndex,:] = self.dataOut.data
1597
1614
1598 self.profileIndex += 1
1615 self.profileIndex += 1
1599
1616
1600 if self.hasAllDataInBuffer():
1617 if self.hasAllDataInBuffer():
1601 #if self.flagIsNewFile:
1618 #if self.flagIsNewFile:
1602 self.writeNextBlock()
1619 self.writeNextBlock()
1603 # self.getDataHeader()
1620 # self.getDataHeader()
1604
1621
1605 return 1
1622 return 1
1606
1623
1607 def __getProcessFlags(self):
1624 def __getProcessFlags(self):
1608
1625
1609 processFlags = 0
1626 processFlags = 0
1610
1627
1611 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1628 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1612 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1629 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1613 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1630 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1614 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1631 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1615 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1632 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1616 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1633 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1617
1634
1618 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1635 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1619
1636
1620
1637
1621
1638
1622 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
1639 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
1623 PROCFLAG.DATATYPE_SHORT,
1640 PROCFLAG.DATATYPE_SHORT,
1624 PROCFLAG.DATATYPE_LONG,
1641 PROCFLAG.DATATYPE_LONG,
1625 PROCFLAG.DATATYPE_INT64,
1642 PROCFLAG.DATATYPE_INT64,
1626 PROCFLAG.DATATYPE_FLOAT,
1643 PROCFLAG.DATATYPE_FLOAT,
1627 PROCFLAG.DATATYPE_DOUBLE]
1644 PROCFLAG.DATATYPE_DOUBLE]
1628
1645
1629
1646
1630 for index in range(len(dtypeList)):
1647 for index in range(len(dtypeList)):
1631 if self.dataOut.dtype == dtypeList[index]:
1648 if self.dataOut.dtype == dtypeList[index]:
1632 dtypeValue = datatypeValueList[index]
1649 dtypeValue = datatypeValueList[index]
1633 break
1650 break
1634
1651
1635 processFlags += dtypeValue
1652 processFlags += dtypeValue
1636
1653
1637 if self.dataOut.flagDecodeData:
1654 if self.dataOut.flagDecodeData:
1638 processFlags += PROCFLAG.DECODE_DATA
1655 processFlags += PROCFLAG.DECODE_DATA
1639
1656
1640 if self.dataOut.flagDeflipData:
1657 if self.dataOut.flagDeflipData:
1641 processFlags += PROCFLAG.DEFLIP_DATA
1658 processFlags += PROCFLAG.DEFLIP_DATA
1642
1659
1643 if self.dataOut.code != None:
1660 if self.dataOut.code != None:
1644 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1661 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1645
1662
1646 if self.dataOut.nCohInt > 1:
1663 if self.dataOut.nCohInt > 1:
1647 processFlags += PROCFLAG.COHERENT_INTEGRATION
1664 processFlags += PROCFLAG.COHERENT_INTEGRATION
1648
1665
1649 return processFlags
1666 return processFlags
1650
1667
1651
1668
1652 def __getBlockSize(self):
1669 def __getBlockSize(self):
1653 '''
1670 '''
1654 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
1671 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
1655 '''
1672 '''
1656
1673
1657 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1674 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1658 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1675 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1659 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1676 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1660 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1677 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1661 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1678 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1662 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1679 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1663
1680
1664 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1681 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1665 datatypeValueList = [1,2,4,8,4,8]
1682 datatypeValueList = [1,2,4,8,4,8]
1666 for index in range(len(dtypeList)):
1683 for index in range(len(dtypeList)):
1667 if self.dataOut.dtype == dtypeList[index]:
1684 if self.dataOut.dtype == dtypeList[index]:
1668 datatypeValue = datatypeValueList[index]
1685 datatypeValue = datatypeValueList[index]
1669 break
1686 break
1670
1687
1671 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.dataOut.nProfiles * datatypeValue * 2)
1688 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.dataOut.nProfiles * datatypeValue * 2)
1672
1689
1673 return blocksize
1690 return blocksize
1674
1691
1675 def getDataHeader(self):
1692 def getDataHeader(self):
1676
1693
1677 """
1694 """
1678 Obtiene una copia del First Header
1695 Obtiene una copia del First Header
1679
1696
1680 Affected:
1697 Affected:
1681 self.systemHeaderObj
1698 self.systemHeaderObj
1682 self.radarControllerHeaderObj
1699 self.radarControllerHeaderObj
1683 self.dtype
1700 self.dtype
1684
1701
1685 Return:
1702 Return:
1686 None
1703 None
1687 """
1704 """
1688
1705
1689 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
1706 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
1690 self.systemHeaderObj.nChannels = self.dataOut.nChannels
1707 self.systemHeaderObj.nChannels = self.dataOut.nChannels
1691 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
1708 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
1692
1709
1693 self.getBasicHeader()
1710 self.getBasicHeader()
1694
1711
1695 processingHeaderSize = 40 # bytes
1712 processingHeaderSize = 40 # bytes
1696 self.processingHeaderObj.dtype = 0 # Voltage
1713 self.processingHeaderObj.dtype = 0 # Voltage
1697 self.processingHeaderObj.blockSize = self.__getBlockSize()
1714 self.processingHeaderObj.blockSize = self.__getBlockSize()
1698 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
1715 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
1699 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
1716 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
1700 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
1717 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
1701 self.processingHeaderObj.processFlags = self.__getProcessFlags()
1718 self.processingHeaderObj.processFlags = self.__getProcessFlags()
1702 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
1719 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
1703 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
1720 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
1704 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
1721 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
1705
1722
1706 if self.dataOut.code != None:
1723 if self.dataOut.code != None:
1707 self.processingHeaderObj.code = self.dataOut.code
1724 self.processingHeaderObj.code = self.dataOut.code
1708 self.processingHeaderObj.nCode = self.dataOut.nCode
1725 self.processingHeaderObj.nCode = self.dataOut.nCode
1709 self.processingHeaderObj.nBaud = self.dataOut.nBaud
1726 self.processingHeaderObj.nBaud = self.dataOut.nBaud
1710 codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud)
1727 codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud)
1711 processingHeaderSize += codesize
1728 processingHeaderSize += codesize
1712
1729
1713 if self.processingHeaderObj.nWindows != 0:
1730 if self.processingHeaderObj.nWindows != 0:
1714 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
1731 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
1715 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
1732 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
1716 self.processingHeaderObj.nHeights = self.dataOut.nHeights
1733 self.processingHeaderObj.nHeights = self.dataOut.nHeights
1717 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
1734 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
1718 processingHeaderSize += 12
1735 processingHeaderSize += 12
1719
1736
1720 self.processingHeaderObj.size = processingHeaderSize
1737 self.processingHeaderObj.size = processingHeaderSize
1721
1738
1722 class SpectraReader(JRODataReader):
1739 class SpectraReader(JRODataReader):
1723 """
1740 """
1724 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
1741 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
1725 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
1742 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
1726 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
1743 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
1727
1744
1728 paresCanalesIguales * alturas * perfiles (Self Spectra)
1745 paresCanalesIguales * alturas * perfiles (Self Spectra)
1729 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
1746 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
1730 canales * alturas (DC Channels)
1747 canales * alturas (DC Channels)
1731
1748
1732 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1749 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1733 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
1750 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
1734 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
1751 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
1735 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1752 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1736
1753
1737 Example:
1754 Example:
1738 dpath = "/home/myuser/data"
1755 dpath = "/home/myuser/data"
1739
1756
1740 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1757 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1741
1758
1742 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1759 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1743
1760
1744 readerObj = SpectraReader()
1761 readerObj = SpectraReader()
1745
1762
1746 readerObj.setup(dpath, startTime, endTime)
1763 readerObj.setup(dpath, startTime, endTime)
1747
1764
1748 while(True):
1765 while(True):
1749
1766
1750 readerObj.getData()
1767 readerObj.getData()
1751
1768
1752 print readerObj.data_spc
1769 print readerObj.data_spc
1753
1770
1754 print readerObj.data_cspc
1771 print readerObj.data_cspc
1755
1772
1756 print readerObj.data_dc
1773 print readerObj.data_dc
1757
1774
1758 if readerObj.flagNoMoreFiles:
1775 if readerObj.flagNoMoreFiles:
1759 break
1776 break
1760
1777
1761 """
1778 """
1762
1779
1763 pts2read_SelfSpectra = 0
1780 pts2read_SelfSpectra = 0
1764
1781
1765 pts2read_CrossSpectra = 0
1782 pts2read_CrossSpectra = 0
1766
1783
1767 pts2read_DCchannels = 0
1784 pts2read_DCchannels = 0
1768
1785
1769 ext = ".pdata"
1786 ext = ".pdata"
1770
1787
1771 optchar = "P"
1788 optchar = "P"
1772
1789
1773 dataOut = None
1790 dataOut = None
1774
1791
1775 nRdChannels = None
1792 nRdChannels = None
1776
1793
1777 nRdPairs = None
1794 nRdPairs = None
1778
1795
1779 rdPairList = []
1796 rdPairList = []
1780
1797
1781
1798
1782 def __init__(self):
1799 def __init__(self):
1783 """
1800 """
1784 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
1801 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
1785
1802
1786 Inputs:
1803 Inputs:
1787 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
1804 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
1788 almacenar un perfil de datos cada vez que se haga un requerimiento
1805 almacenar un perfil de datos cada vez que se haga un requerimiento
1789 (getData). El perfil sera obtenido a partir del buffer de datos,
1806 (getData). El perfil sera obtenido a partir del buffer de datos,
1790 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1807 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1791 bloque de datos.
1808 bloque de datos.
1792 Si este parametro no es pasado se creara uno internamente.
1809 Si este parametro no es pasado se creara uno internamente.
1793
1810
1794 Affected:
1811 Affected:
1795 self.dataOut
1812 self.dataOut
1796
1813
1797 Return : None
1814 Return : None
1798 """
1815 """
1799
1816
1800 self.isConfig = False
1817 self.isConfig = False
1801
1818
1802 self.pts2read_SelfSpectra = 0
1819 self.pts2read_SelfSpectra = 0
1803
1820
1804 self.pts2read_CrossSpectra = 0
1821 self.pts2read_CrossSpectra = 0
1805
1822
1806 self.pts2read_DCchannels = 0
1823 self.pts2read_DCchannels = 0
1807
1824
1808 self.datablock = None
1825 self.datablock = None
1809
1826
1810 self.utc = None
1827 self.utc = None
1811
1828
1812 self.ext = ".pdata"
1829 self.ext = ".pdata"
1813
1830
1814 self.optchar = "P"
1831 self.optchar = "P"
1815
1832
1816 self.basicHeaderObj = BasicHeader(LOCALTIME)
1833 self.basicHeaderObj = BasicHeader(LOCALTIME)
1817
1834
1818 self.systemHeaderObj = SystemHeader()
1835 self.systemHeaderObj = SystemHeader()
1819
1836
1820 self.radarControllerHeaderObj = RadarControllerHeader()
1837 self.radarControllerHeaderObj = RadarControllerHeader()
1821
1838
1822 self.processingHeaderObj = ProcessingHeader()
1839 self.processingHeaderObj = ProcessingHeader()
1823
1840
1824 self.online = 0
1841 self.online = 0
1825
1842
1826 self.fp = None
1843 self.fp = None
1827
1844
1828 self.idFile = None
1845 self.idFile = None
1829
1846
1830 self.dtype = None
1847 self.dtype = None
1831
1848
1832 self.fileSizeByHeader = None
1849 self.fileSizeByHeader = None
1833
1850
1834 self.filenameList = []
1851 self.filenameList = []
1835
1852
1836 self.filename = None
1853 self.filename = None
1837
1854
1838 self.fileSize = None
1855 self.fileSize = None
1839
1856
1840 self.firstHeaderSize = 0
1857 self.firstHeaderSize = 0
1841
1858
1842 self.basicHeaderSize = 24
1859 self.basicHeaderSize = 24
1843
1860
1844 self.pathList = []
1861 self.pathList = []
1845
1862
1846 self.lastUTTime = 0
1863 self.lastUTTime = 0
1847
1864
1848 self.maxTimeStep = 30
1865 self.maxTimeStep = 30
1849
1866
1850 self.flagNoMoreFiles = 0
1867 self.flagNoMoreFiles = 0
1851
1868
1852 self.set = 0
1869 self.set = 0
1853
1870
1854 self.path = None
1871 self.path = None
1855
1872
1856 self.delay = 60 #seconds
1873 self.delay = 60 #seconds
1857
1874
1858 self.nTries = 3 #quantity tries
1875 self.nTries = 3 #quantity tries
1859
1876
1860 self.nFiles = 3 #number of files for searching
1877 self.nFiles = 3 #number of files for searching
1861
1878
1862 self.nReadBlocks = 0
1879 self.nReadBlocks = 0
1863
1880
1864 self.flagIsNewFile = 1
1881 self.flagIsNewFile = 1
1865
1882
1866 self.ippSeconds = 0
1883 self.ippSeconds = 0
1867
1884
1868 self.flagTimeBlock = 0
1885 self.flagTimeBlock = 0
1869
1886
1870 self.flagIsNewBlock = 0
1887 self.flagIsNewBlock = 0
1871
1888
1872 self.nTotalBlocks = 0
1889 self.nTotalBlocks = 0
1873
1890
1874 self.blocksize = 0
1891 self.blocksize = 0
1875
1892
1876 self.dataOut = self.createObjByDefault()
1893 self.dataOut = self.createObjByDefault()
1877
1894
1878
1895
1879 def createObjByDefault(self):
1896 def createObjByDefault(self):
1880
1897
1881 dataObj = Spectra()
1898 dataObj = Spectra()
1882
1899
1883 return dataObj
1900 return dataObj
1884
1901
1885 def __hasNotDataInBuffer(self):
1902 def __hasNotDataInBuffer(self):
1886 return 1
1903 return 1
1887
1904
1888
1905
1889 def getBlockDimension(self):
1906 def getBlockDimension(self):
1890 """
1907 """
1891 Obtiene la cantidad de puntos a leer por cada bloque de datos
1908 Obtiene la cantidad de puntos a leer por cada bloque de datos
1892
1909
1893 Affected:
1910 Affected:
1894 self.nRdChannels
1911 self.nRdChannels
1895 self.nRdPairs
1912 self.nRdPairs
1896 self.pts2read_SelfSpectra
1913 self.pts2read_SelfSpectra
1897 self.pts2read_CrossSpectra
1914 self.pts2read_CrossSpectra
1898 self.pts2read_DCchannels
1915 self.pts2read_DCchannels
1899 self.blocksize
1916 self.blocksize
1900 self.dataOut.nChannels
1917 self.dataOut.nChannels
1901 self.dataOut.nPairs
1918 self.dataOut.nPairs
1902
1919
1903 Return:
1920 Return:
1904 None
1921 None
1905 """
1922 """
1906 self.nRdChannels = 0
1923 self.nRdChannels = 0
1907 self.nRdPairs = 0
1924 self.nRdPairs = 0
1908 self.rdPairList = []
1925 self.rdPairList = []
1909
1926
1910 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
1927 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
1911 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
1928 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
1912 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
1929 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
1913 else:
1930 else:
1914 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
1931 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
1915 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
1932 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
1916
1933
1917 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
1934 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
1918
1935
1919 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
1936 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
1920 self.blocksize = self.pts2read_SelfSpectra
1937 self.blocksize = self.pts2read_SelfSpectra
1921
1938
1922 if self.processingHeaderObj.flag_cspc:
1939 if self.processingHeaderObj.flag_cspc:
1923 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
1940 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
1924 self.blocksize += self.pts2read_CrossSpectra
1941 self.blocksize += self.pts2read_CrossSpectra
1925
1942
1926 if self.processingHeaderObj.flag_dc:
1943 if self.processingHeaderObj.flag_dc:
1927 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
1944 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
1928 self.blocksize += self.pts2read_DCchannels
1945 self.blocksize += self.pts2read_DCchannels
1929
1946
1930 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
1947 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
1931
1948
1932
1949
1933 def readBlock(self):
1950 def readBlock(self):
1934 """
1951 """
1935 Lee el bloque de datos desde la posicion actual del puntero del archivo
1952 Lee el bloque de datos desde la posicion actual del puntero del archivo
1936 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1953 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1937 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1954 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1938 es seteado a 0
1955 es seteado a 0
1939
1956
1940 Return: None
1957 Return: None
1941
1958
1942 Variables afectadas:
1959 Variables afectadas:
1943
1960
1944 self.flagIsNewFile
1961 self.flagIsNewFile
1945 self.flagIsNewBlock
1962 self.flagIsNewBlock
1946 self.nTotalBlocks
1963 self.nTotalBlocks
1947 self.data_spc
1964 self.data_spc
1948 self.data_cspc
1965 self.data_cspc
1949 self.data_dc
1966 self.data_dc
1950
1967
1951 Exceptions:
1968 Exceptions:
1952 Si un bloque leido no es un bloque valido
1969 Si un bloque leido no es un bloque valido
1953 """
1970 """
1954 blockOk_flag = False
1971 blockOk_flag = False
1955 fpointer = self.fp.tell()
1972 fpointer = self.fp.tell()
1956
1973
1957 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
1974 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
1958 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
1975 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
1959
1976
1960 if self.processingHeaderObj.flag_cspc:
1977 if self.processingHeaderObj.flag_cspc:
1961 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
1978 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
1962 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
1979 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
1963
1980
1964 if self.processingHeaderObj.flag_dc:
1981 if self.processingHeaderObj.flag_dc:
1965 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
1982 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
1966 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
1983 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
1967
1984
1968
1985
1969 if not(self.processingHeaderObj.shif_fft):
1986 if not(self.processingHeaderObj.shif_fft):
1970 #desplaza a la derecha en el eje 2 determinadas posiciones
1987 #desplaza a la derecha en el eje 2 determinadas posiciones
1971 shift = int(self.processingHeaderObj.profilesPerBlock/2)
1988 shift = int(self.processingHeaderObj.profilesPerBlock/2)
1972 spc = numpy.roll( spc, shift , axis=2 )
1989 spc = numpy.roll( spc, shift , axis=2 )
1973
1990
1974 if self.processingHeaderObj.flag_cspc:
1991 if self.processingHeaderObj.flag_cspc:
1975 #desplaza a la derecha en el eje 2 determinadas posiciones
1992 #desplaza a la derecha en el eje 2 determinadas posiciones
1976 cspc = numpy.roll( cspc, shift, axis=2 )
1993 cspc = numpy.roll( cspc, shift, axis=2 )
1977
1994
1978 # self.processingHeaderObj.shif_fft = True
1995 # self.processingHeaderObj.shif_fft = True
1979
1996
1980 spc = numpy.transpose( spc, (0,2,1) )
1997 spc = numpy.transpose( spc, (0,2,1) )
1981 self.data_spc = spc
1998 self.data_spc = spc
1982
1999
1983 if self.processingHeaderObj.flag_cspc:
2000 if self.processingHeaderObj.flag_cspc:
1984 cspc = numpy.transpose( cspc, (0,2,1) )
2001 cspc = numpy.transpose( cspc, (0,2,1) )
1985 self.data_cspc = cspc['real'] + cspc['imag']*1j
2002 self.data_cspc = cspc['real'] + cspc['imag']*1j
1986 else:
2003 else:
1987 self.data_cspc = None
2004 self.data_cspc = None
1988
2005
1989 if self.processingHeaderObj.flag_dc:
2006 if self.processingHeaderObj.flag_dc:
1990 self.data_dc = dc['real'] + dc['imag']*1j
2007 self.data_dc = dc['real'] + dc['imag']*1j
1991 else:
2008 else:
1992 self.data_dc = None
2009 self.data_dc = None
1993
2010
1994 self.flagIsNewFile = 0
2011 self.flagIsNewFile = 0
1995 self.flagIsNewBlock = 1
2012 self.flagIsNewBlock = 1
1996
2013
1997 self.nTotalBlocks += 1
2014 self.nTotalBlocks += 1
1998 self.nReadBlocks += 1
2015 self.nReadBlocks += 1
1999
2016
2000 return 1
2017 return 1
2001
2018
2002
2019
2003 def getData(self):
2020 def getData(self):
2004 """
2021 """
2005 Copia el buffer de lectura a la clase "Spectra",
2022 Copia el buffer de lectura a la clase "Spectra",
2006 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
2023 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
2007 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
2024 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
2008
2025
2009 Return:
2026 Return:
2010 0 : Si no hay mas archivos disponibles
2027 0 : Si no hay mas archivos disponibles
2011 1 : Si hizo una buena copia del buffer
2028 1 : Si hizo una buena copia del buffer
2012
2029
2013 Affected:
2030 Affected:
2014 self.dataOut
2031 self.dataOut
2015
2032
2016 self.flagTimeBlock
2033 self.flagTimeBlock
2017 self.flagIsNewBlock
2034 self.flagIsNewBlock
2018 """
2035 """
2019
2036
2020 if self.flagNoMoreFiles:
2037 if self.flagNoMoreFiles:
2021 self.dataOut.flagNoData = True
2038 self.dataOut.flagNoData = True
2022 print 'Process finished'
2039 print 'Process finished'
2023 return 0
2040 return 0
2024
2041
2025 self.flagTimeBlock = 0
2042 self.flagTimeBlock = 0
2026 self.flagIsNewBlock = 0
2043 self.flagIsNewBlock = 0
2027
2044
2028 if self.__hasNotDataInBuffer():
2045 if self.__hasNotDataInBuffer():
2029
2046
2030 if not( self.readNextBlock() ):
2047 if not( self.readNextBlock() ):
2031 self.dataOut.flagNoData = True
2048 self.dataOut.flagNoData = True
2032 return 0
2049 return 0
2033
2050
2034 # self.updateDataHeader()
2051 # self.updateDataHeader()
2035
2052
2036 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
2053 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
2037
2054
2038 if self.data_dc == None:
2055 if self.data_dc == None:
2039 self.dataOut.flagNoData = True
2056 self.dataOut.flagNoData = True
2040 return 0
2057 return 0
2041
2058
2042 self.dataOut.data_spc = self.data_spc
2059 self.dataOut.data_spc = self.data_spc
2043
2060
2044 self.dataOut.data_cspc = self.data_cspc
2061 self.dataOut.data_cspc = self.data_cspc
2045
2062
2046 self.dataOut.data_dc = self.data_dc
2063 self.dataOut.data_dc = self.data_dc
2047
2064
2048 self.dataOut.flagTimeBlock = self.flagTimeBlock
2065 self.dataOut.flagTimeBlock = self.flagTimeBlock
2049
2066
2050 self.dataOut.flagNoData = False
2067 self.dataOut.flagNoData = False
2051
2068
2052 self.dataOut.dtype = self.dtype
2069 self.dataOut.dtype = self.dtype
2053
2070
2054 # self.dataOut.nChannels = self.nRdChannels
2071 # self.dataOut.nChannels = self.nRdChannels
2055
2072
2056 self.dataOut.nPairs = self.nRdPairs
2073 self.dataOut.nPairs = self.nRdPairs
2057
2074
2058 self.dataOut.pairsList = self.rdPairList
2075 self.dataOut.pairsList = self.rdPairList
2059
2076
2060 # self.dataOut.nHeights = self.processingHeaderObj.nHeights
2077 # self.dataOut.nHeights = self.processingHeaderObj.nHeights
2061
2078
2062 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
2079 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
2063
2080
2064 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
2081 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
2065
2082
2066 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
2083 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
2067
2084
2068 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
2085 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
2069
2086
2070 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
2087 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
2071
2088
2072 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
2089 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
2073
2090
2074 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
2091 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
2075
2092
2076 # self.dataOut.channelIndexList = range(self.systemHeaderObj.nChannels)
2093 # self.dataOut.channelIndexList = range(self.systemHeaderObj.nChannels)
2077
2094
2078 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000.#+ self.profileIndex * self.ippSeconds
2095 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000.#+ self.profileIndex * self.ippSeconds
2079
2096
2080 self.dataOut.ippSeconds = self.ippSeconds
2097 self.dataOut.ippSeconds = self.ippSeconds
2081
2098
2082 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.dataOut.nFFTPoints
2099 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.dataOut.nFFTPoints
2083
2100
2084 # self.profileIndex += 1
2101 # self.profileIndex += 1
2085
2102
2086 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
2103 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
2087
2104
2088 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
2105 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
2089
2106
2090 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
2107 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
2091
2108
2092 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
2109 self.dataOut.flagDecodeData = False #asumo q la data no esta decodificada
2093
2110
2094 self.dataOut.flagDeflipData = True #asumo q la data no esta sin flip
2111 self.dataOut.flagDeflipData = True #asumo q la data no esta sin flip
2095
2112
2096 if self.processingHeaderObj.code != None:
2113 if self.processingHeaderObj.code != None:
2097
2114
2098 self.dataOut.nCode = self.processingHeaderObj.nCode
2115 self.dataOut.nCode = self.processingHeaderObj.nCode
2099
2116
2100 self.dataOut.nBaud = self.processingHeaderObj.nBaud
2117 self.dataOut.nBaud = self.processingHeaderObj.nBaud
2101
2118
2102 self.dataOut.code = self.processingHeaderObj.code
2119 self.dataOut.code = self.processingHeaderObj.code
2103
2120
2104 self.dataOut.flagDecodeData = True
2121 self.dataOut.flagDecodeData = True
2105
2122
2106 return self.dataOut.data_spc
2123 return self.dataOut.data_spc
2107
2124
2108
2125
2109 class SpectraWriter(JRODataWriter):
2126 class SpectraWriter(JRODataWriter):
2110
2127
2111 """
2128 """
2112 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
2129 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
2113 de los datos siempre se realiza por bloques.
2130 de los datos siempre se realiza por bloques.
2114 """
2131 """
2115
2132
2116 ext = ".pdata"
2133 ext = ".pdata"
2117
2134
2118 optchar = "P"
2135 optchar = "P"
2119
2136
2120 shape_spc_Buffer = None
2137 shape_spc_Buffer = None
2121
2138
2122 shape_cspc_Buffer = None
2139 shape_cspc_Buffer = None
2123
2140
2124 shape_dc_Buffer = None
2141 shape_dc_Buffer = None
2125
2142
2126 data_spc = None
2143 data_spc = None
2127
2144
2128 data_cspc = None
2145 data_cspc = None
2129
2146
2130 data_dc = None
2147 data_dc = None
2131
2148
2132 # dataOut = None
2149 # dataOut = None
2133
2150
2134 def __init__(self):
2151 def __init__(self):
2135 """
2152 """
2136 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
2153 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
2137
2154
2138 Affected:
2155 Affected:
2139 self.dataOut
2156 self.dataOut
2140 self.basicHeaderObj
2157 self.basicHeaderObj
2141 self.systemHeaderObj
2158 self.systemHeaderObj
2142 self.radarControllerHeaderObj
2159 self.radarControllerHeaderObj
2143 self.processingHeaderObj
2160 self.processingHeaderObj
2144
2161
2145 Return: None
2162 Return: None
2146 """
2163 """
2147
2164
2148 self.isConfig = False
2165 self.isConfig = False
2149
2166
2150 self.nTotalBlocks = 0
2167 self.nTotalBlocks = 0
2151
2168
2152 self.data_spc = None
2169 self.data_spc = None
2153
2170
2154 self.data_cspc = None
2171 self.data_cspc = None
2155
2172
2156 self.data_dc = None
2173 self.data_dc = None
2157
2174
2158 self.fp = None
2175 self.fp = None
2159
2176
2160 self.flagIsNewFile = 1
2177 self.flagIsNewFile = 1
2161
2178
2162 self.nTotalBlocks = 0
2179 self.nTotalBlocks = 0
2163
2180
2164 self.flagIsNewBlock = 0
2181 self.flagIsNewBlock = 0
2165
2182
2166 self.setFile = None
2183 self.setFile = None
2167
2184
2168 self.dtype = None
2185 self.dtype = None
2169
2186
2170 self.path = None
2187 self.path = None
2171
2188
2172 self.noMoreFiles = 0
2189 self.noMoreFiles = 0
2173
2190
2174 self.filename = None
2191 self.filename = None
2175
2192
2176 self.basicHeaderObj = BasicHeader(LOCALTIME)
2193 self.basicHeaderObj = BasicHeader(LOCALTIME)
2177
2194
2178 self.systemHeaderObj = SystemHeader()
2195 self.systemHeaderObj = SystemHeader()
2179
2196
2180 self.radarControllerHeaderObj = RadarControllerHeader()
2197 self.radarControllerHeaderObj = RadarControllerHeader()
2181
2198
2182 self.processingHeaderObj = ProcessingHeader()
2199 self.processingHeaderObj = ProcessingHeader()
2183
2200
2184
2201
2185 def hasAllDataInBuffer(self):
2202 def hasAllDataInBuffer(self):
2186 return 1
2203 return 1
2187
2204
2188
2205
2189 def setBlockDimension(self):
2206 def setBlockDimension(self):
2190 """
2207 """
2191 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
2208 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
2192
2209
2193 Affected:
2210 Affected:
2194 self.shape_spc_Buffer
2211 self.shape_spc_Buffer
2195 self.shape_cspc_Buffer
2212 self.shape_cspc_Buffer
2196 self.shape_dc_Buffer
2213 self.shape_dc_Buffer
2197
2214
2198 Return: None
2215 Return: None
2199 """
2216 """
2200 self.shape_spc_Buffer = (self.dataOut.nChannels,
2217 self.shape_spc_Buffer = (self.dataOut.nChannels,
2201 self.processingHeaderObj.nHeights,
2218 self.processingHeaderObj.nHeights,
2202 self.processingHeaderObj.profilesPerBlock)
2219 self.processingHeaderObj.profilesPerBlock)
2203
2220
2204 self.shape_cspc_Buffer = (self.dataOut.nPairs,
2221 self.shape_cspc_Buffer = (self.dataOut.nPairs,
2205 self.processingHeaderObj.nHeights,
2222 self.processingHeaderObj.nHeights,
2206 self.processingHeaderObj.profilesPerBlock)
2223 self.processingHeaderObj.profilesPerBlock)
2207
2224
2208 self.shape_dc_Buffer = (self.dataOut.nChannels,
2225 self.shape_dc_Buffer = (self.dataOut.nChannels,
2209 self.processingHeaderObj.nHeights)
2226 self.processingHeaderObj.nHeights)
2210
2227
2211
2228
2212 def writeBlock(self):
2229 def writeBlock(self):
2213 """
2230 """
2214 Escribe el buffer en el file designado
2231 Escribe el buffer en el file designado
2215
2232
2216 Affected:
2233 Affected:
2217 self.data_spc
2234 self.data_spc
2218 self.data_cspc
2235 self.data_cspc
2219 self.data_dc
2236 self.data_dc
2220 self.flagIsNewFile
2237 self.flagIsNewFile
2221 self.flagIsNewBlock
2238 self.flagIsNewBlock
2222 self.nTotalBlocks
2239 self.nTotalBlocks
2223 self.nWriteBlocks
2240 self.nWriteBlocks
2224
2241
2225 Return: None
2242 Return: None
2226 """
2243 """
2227
2244
2228 spc = numpy.transpose( self.data_spc, (0,2,1) )
2245 spc = numpy.transpose( self.data_spc, (0,2,1) )
2229 if not( self.processingHeaderObj.shif_fft ):
2246 if not( self.processingHeaderObj.shif_fft ):
2230 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2247 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2231 data = spc.reshape((-1))
2248 data = spc.reshape((-1))
2232 data = data.astype(self.dtype[0])
2249 data = data.astype(self.dtype[0])
2233 data.tofile(self.fp)
2250 data.tofile(self.fp)
2234
2251
2235 if self.data_cspc != None:
2252 if self.data_cspc != None:
2236 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
2253 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
2237 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
2254 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
2238 if not( self.processingHeaderObj.shif_fft ):
2255 if not( self.processingHeaderObj.shif_fft ):
2239 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2256 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2240 data['real'] = cspc.real
2257 data['real'] = cspc.real
2241 data['imag'] = cspc.imag
2258 data['imag'] = cspc.imag
2242 data = data.reshape((-1))
2259 data = data.reshape((-1))
2243 data.tofile(self.fp)
2260 data.tofile(self.fp)
2244
2261
2245 if self.data_dc != None:
2262 if self.data_dc != None:
2246 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
2263 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
2247 dc = self.data_dc
2264 dc = self.data_dc
2248 data['real'] = dc.real
2265 data['real'] = dc.real
2249 data['imag'] = dc.imag
2266 data['imag'] = dc.imag
2250 data = data.reshape((-1))
2267 data = data.reshape((-1))
2251 data.tofile(self.fp)
2268 data.tofile(self.fp)
2252
2269
2253 self.data_spc.fill(0)
2270 self.data_spc.fill(0)
2254 self.data_dc.fill(0)
2271 self.data_dc.fill(0)
2255 if self.data_cspc != None:
2272 if self.data_cspc != None:
2256 self.data_cspc.fill(0)
2273 self.data_cspc.fill(0)
2257
2274
2258 self.flagIsNewFile = 0
2275 self.flagIsNewFile = 0
2259 self.flagIsNewBlock = 1
2276 self.flagIsNewBlock = 1
2260 self.nTotalBlocks += 1
2277 self.nTotalBlocks += 1
2261 self.nWriteBlocks += 1
2278 self.nWriteBlocks += 1
2262 self.blockIndex += 1
2279 self.blockIndex += 1
2263
2280
2264
2281
2265 def putData(self):
2282 def putData(self):
2266 """
2283 """
2267 Setea un bloque de datos y luego los escribe en un file
2284 Setea un bloque de datos y luego los escribe en un file
2268
2285
2269 Affected:
2286 Affected:
2270 self.data_spc
2287 self.data_spc
2271 self.data_cspc
2288 self.data_cspc
2272 self.data_dc
2289 self.data_dc
2273
2290
2274 Return:
2291 Return:
2275 0 : Si no hay data o no hay mas files que puedan escribirse
2292 0 : Si no hay data o no hay mas files que puedan escribirse
2276 1 : Si se escribio la data de un bloque en un file
2293 1 : Si se escribio la data de un bloque en un file
2277 """
2294 """
2278
2295
2279 if self.dataOut.flagNoData:
2296 if self.dataOut.flagNoData:
2280 return 0
2297 return 0
2281
2298
2282 self.flagIsNewBlock = 0
2299 self.flagIsNewBlock = 0
2283
2300
2284 if self.dataOut.flagTimeBlock:
2301 if self.dataOut.flagTimeBlock:
2285 self.data_spc.fill(0)
2302 self.data_spc.fill(0)
2286 self.data_cspc.fill(0)
2303 self.data_cspc.fill(0)
2287 self.data_dc.fill(0)
2304 self.data_dc.fill(0)
2288 self.setNextFile()
2305 self.setNextFile()
2289
2306
2290 if self.flagIsNewFile == 0:
2307 if self.flagIsNewFile == 0:
2291 self.getBasicHeader()
2308 self.getBasicHeader()
2292
2309
2293 self.data_spc = self.dataOut.data_spc.copy()
2310 self.data_spc = self.dataOut.data_spc.copy()
2294 self.data_cspc = self.dataOut.data_cspc.copy()
2311 self.data_cspc = self.dataOut.data_cspc.copy()
2295 self.data_dc = self.dataOut.data_dc.copy()
2312 self.data_dc = self.dataOut.data_dc.copy()
2296
2313
2297 # #self.processingHeaderObj.dataBlocksPerFile)
2314 # #self.processingHeaderObj.dataBlocksPerFile)
2298 if self.hasAllDataInBuffer():
2315 if self.hasAllDataInBuffer():
2299 # self.getDataHeader()
2316 # self.getDataHeader()
2300 self.writeNextBlock()
2317 self.writeNextBlock()
2301
2318
2302 return 1
2319 return 1
2303
2320
2304
2321
2305 def __getProcessFlags(self):
2322 def __getProcessFlags(self):
2306
2323
2307 processFlags = 0
2324 processFlags = 0
2308
2325
2309 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2326 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2310 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2327 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2311 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2328 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2312 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2329 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2313 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2330 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2314 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2331 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2315
2332
2316 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2333 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2317
2334
2318
2335
2319
2336
2320 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
2337 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
2321 PROCFLAG.DATATYPE_SHORT,
2338 PROCFLAG.DATATYPE_SHORT,
2322 PROCFLAG.DATATYPE_LONG,
2339 PROCFLAG.DATATYPE_LONG,
2323 PROCFLAG.DATATYPE_INT64,
2340 PROCFLAG.DATATYPE_INT64,
2324 PROCFLAG.DATATYPE_FLOAT,
2341 PROCFLAG.DATATYPE_FLOAT,
2325 PROCFLAG.DATATYPE_DOUBLE]
2342 PROCFLAG.DATATYPE_DOUBLE]
2326
2343
2327
2344
2328 for index in range(len(dtypeList)):
2345 for index in range(len(dtypeList)):
2329 if self.dataOut.dtype == dtypeList[index]:
2346 if self.dataOut.dtype == dtypeList[index]:
2330 dtypeValue = datatypeValueList[index]
2347 dtypeValue = datatypeValueList[index]
2331 break
2348 break
2332
2349
2333 processFlags += dtypeValue
2350 processFlags += dtypeValue
2334
2351
2335 if self.dataOut.flagDecodeData:
2352 if self.dataOut.flagDecodeData:
2336 processFlags += PROCFLAG.DECODE_DATA
2353 processFlags += PROCFLAG.DECODE_DATA
2337
2354
2338 if self.dataOut.flagDeflipData:
2355 if self.dataOut.flagDeflipData:
2339 processFlags += PROCFLAG.DEFLIP_DATA
2356 processFlags += PROCFLAG.DEFLIP_DATA
2340
2357
2341 if self.dataOut.code != None:
2358 if self.dataOut.code != None:
2342 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
2359 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
2343
2360
2344 if self.dataOut.nIncohInt > 1:
2361 if self.dataOut.nIncohInt > 1:
2345 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
2362 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
2346
2363
2347 if self.dataOut.data_dc != None:
2364 if self.dataOut.data_dc != None:
2348 processFlags += PROCFLAG.SAVE_CHANNELS_DC
2365 processFlags += PROCFLAG.SAVE_CHANNELS_DC
2349
2366
2350 return processFlags
2367 return processFlags
2351
2368
2352
2369
2353 def __getBlockSize(self):
2370 def __getBlockSize(self):
2354 '''
2371 '''
2355 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
2372 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
2356 '''
2373 '''
2357
2374
2358 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2375 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2359 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2376 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2360 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2377 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2361 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2378 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2362 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2379 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2363 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2380 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2364
2381
2365 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2382 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2366 datatypeValueList = [1,2,4,8,4,8]
2383 datatypeValueList = [1,2,4,8,4,8]
2367 for index in range(len(dtypeList)):
2384 for index in range(len(dtypeList)):
2368 if self.dataOut.dtype == dtypeList[index]:
2385 if self.dataOut.dtype == dtypeList[index]:
2369 datatypeValue = datatypeValueList[index]
2386 datatypeValue = datatypeValueList[index]
2370 break
2387 break
2371
2388
2372
2389
2373 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
2390 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
2374
2391
2375 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
2392 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
2376 blocksize = (pts2write_SelfSpectra*datatypeValue)
2393 blocksize = (pts2write_SelfSpectra*datatypeValue)
2377
2394
2378 if self.dataOut.data_cspc != None:
2395 if self.dataOut.data_cspc != None:
2379 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
2396 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
2380 blocksize += (pts2write_CrossSpectra*datatypeValue*2)
2397 blocksize += (pts2write_CrossSpectra*datatypeValue*2)
2381
2398
2382 if self.dataOut.data_dc != None:
2399 if self.dataOut.data_dc != None:
2383 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
2400 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
2384 blocksize += (pts2write_DCchannels*datatypeValue*2)
2401 blocksize += (pts2write_DCchannels*datatypeValue*2)
2385
2402
2386 blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
2403 blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
2387
2404
2388 return blocksize
2405 return blocksize
2389
2406
2390 def getDataHeader(self):
2407 def getDataHeader(self):
2391
2408
2392 """
2409 """
2393 Obtiene una copia del First Header
2410 Obtiene una copia del First Header
2394
2411
2395 Affected:
2412 Affected:
2396 self.systemHeaderObj
2413 self.systemHeaderObj
2397 self.radarControllerHeaderObj
2414 self.radarControllerHeaderObj
2398 self.dtype
2415 self.dtype
2399
2416
2400 Return:
2417 Return:
2401 None
2418 None
2402 """
2419 """
2403
2420
2404 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
2421 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
2405 self.systemHeaderObj.nChannels = self.dataOut.nChannels
2422 self.systemHeaderObj.nChannels = self.dataOut.nChannels
2406 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
2423 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
2407
2424
2408 self.getBasicHeader()
2425 self.getBasicHeader()
2409
2426
2410 processingHeaderSize = 40 # bytes
2427 processingHeaderSize = 40 # bytes
2411 self.processingHeaderObj.dtype = 0 # Voltage
2428 self.processingHeaderObj.dtype = 0 # Voltage
2412 self.processingHeaderObj.blockSize = self.__getBlockSize()
2429 self.processingHeaderObj.blockSize = self.__getBlockSize()
2413 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
2430 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
2414 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
2431 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
2415 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
2432 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
2416 self.processingHeaderObj.processFlags = self.__getProcessFlags()
2433 self.processingHeaderObj.processFlags = self.__getProcessFlags()
2417 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
2434 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
2418 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
2435 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
2419 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
2436 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
2420
2437
2421 if self.processingHeaderObj.totalSpectra > 0:
2438 if self.processingHeaderObj.totalSpectra > 0:
2422 channelList = []
2439 channelList = []
2423 for channel in range(self.dataOut.nChannels):
2440 for channel in range(self.dataOut.nChannels):
2424 channelList.append(channel)
2441 channelList.append(channel)
2425 channelList.append(channel)
2442 channelList.append(channel)
2426
2443
2427 pairsList = []
2444 pairsList = []
2428 for pair in self.dataOut.pairsList:
2445 for pair in self.dataOut.pairsList:
2429 pairsList.append(pair[0])
2446 pairsList.append(pair[0])
2430 pairsList.append(pair[1])
2447 pairsList.append(pair[1])
2431 spectraComb = channelList + pairsList
2448 spectraComb = channelList + pairsList
2432 spectraComb = numpy.array(spectraComb,dtype="u1")
2449 spectraComb = numpy.array(spectraComb,dtype="u1")
2433 self.processingHeaderObj.spectraComb = spectraComb
2450 self.processingHeaderObj.spectraComb = spectraComb
2434 sizeOfSpcComb = len(spectraComb)
2451 sizeOfSpcComb = len(spectraComb)
2435 processingHeaderSize += sizeOfSpcComb
2452 processingHeaderSize += sizeOfSpcComb
2436
2453
2437 if self.dataOut.code != None:
2454 if self.dataOut.code != None:
2438 self.processingHeaderObj.code = self.dataOut.code
2455 self.processingHeaderObj.code = self.dataOut.code
2439 self.processingHeaderObj.nCode = self.dataOut.nCode
2456 self.processingHeaderObj.nCode = self.dataOut.nCode
2440 self.processingHeaderObj.nBaud = self.dataOut.nBaud
2457 self.processingHeaderObj.nBaud = self.dataOut.nBaud
2441 nCodeSize = 4 # bytes
2458 nCodeSize = 4 # bytes
2442 nBaudSize = 4 # bytes
2459 nBaudSize = 4 # bytes
2443 codeSize = 4 # bytes
2460 codeSize = 4 # bytes
2444 sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud)
2461 sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud)
2445 processingHeaderSize += sizeOfCode
2462 processingHeaderSize += sizeOfCode
2446
2463
2447 if self.processingHeaderObj.nWindows != 0:
2464 if self.processingHeaderObj.nWindows != 0:
2448 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
2465 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
2449 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
2466 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
2450 self.processingHeaderObj.nHeights = self.dataOut.nHeights
2467 self.processingHeaderObj.nHeights = self.dataOut.nHeights
2451 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
2468 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
2452 sizeOfFirstHeight = 4
2469 sizeOfFirstHeight = 4
2453 sizeOfdeltaHeight = 4
2470 sizeOfdeltaHeight = 4
2454 sizeOfnHeights = 4
2471 sizeOfnHeights = 4
2455 sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows
2472 sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows
2456 processingHeaderSize += sizeOfWindows
2473 processingHeaderSize += sizeOfWindows
2457
2474
2458 self.processingHeaderObj.size = processingHeaderSize
2475 self.processingHeaderObj.size = processingHeaderSize
2459
2476
2460 class SpectraHeisWriter():
2477 class SpectraHeisWriter():
2461
2478
2462 i=0
2479 i=0
2463
2480
2464 def __init__(self, dataOut):
2481 def __init__(self, dataOut):
2465
2482
2466 self.wrObj = FITS()
2483 self.wrObj = FITS()
2467 self.dataOut = dataOut
2484 self.dataOut = dataOut
2468
2485
2469 def isNumber(str):
2486 def isNumber(str):
2470 """
2487 """
2471 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
2488 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
2472
2489
2473 Excepciones:
2490 Excepciones:
2474 Si un determinado string no puede ser convertido a numero
2491 Si un determinado string no puede ser convertido a numero
2475 Input:
2492 Input:
2476 str, string al cual se le analiza para determinar si convertible a un numero o no
2493 str, string al cual se le analiza para determinar si convertible a un numero o no
2477
2494
2478 Return:
2495 Return:
2479 True : si el string es uno numerico
2496 True : si el string es uno numerico
2480 False : no es un string numerico
2497 False : no es un string numerico
2481 """
2498 """
2482 try:
2499 try:
2483 float( str )
2500 float( str )
2484 return True
2501 return True
2485 except:
2502 except:
2486 return False
2503 return False
2487
2504
2488 def setup(self, wrpath,):
2505 def setup(self, wrpath,):
2489
2506
2490 if not(os.path.exists(wrpath)):
2507 if not(os.path.exists(wrpath)):
2491 os.mkdir(wrpath)
2508 os.mkdir(wrpath)
2492
2509
2493 self.wrpath = wrpath
2510 self.wrpath = wrpath
2494 self.setFile = 0
2511 self.setFile = 0
2495
2512
2496 def putData(self):
2513 def putData(self):
2497 # self.wrObj.writeHeader(nChannels=self.dataOut.nChannels, nFFTPoints=self.dataOut.nFFTPoints)
2514 # self.wrObj.writeHeader(nChannels=self.dataOut.nChannels, nFFTPoints=self.dataOut.nFFTPoints)
2498 #name = self.dataOut.utctime
2515 #name = self.dataOut.utctime
2499 name= time.localtime( self.dataOut.utctime)
2516 name= time.localtime( self.dataOut.utctime)
2500 ext=".fits"
2517 ext=".fits"
2501 #folder='D%4.4d%3.3d'%(name.tm_year,name.tm_yday)
2518 #folder='D%4.4d%3.3d'%(name.tm_year,name.tm_yday)
2502 subfolder = 'D%4.4d%3.3d' % (name.tm_year,name.tm_yday)
2519 subfolder = 'D%4.4d%3.3d' % (name.tm_year,name.tm_yday)
2503
2520
2504 fullpath = os.path.join( self.wrpath, subfolder )
2521 fullpath = os.path.join( self.wrpath, subfolder )
2505 if not( os.path.exists(fullpath) ):
2522 if not( os.path.exists(fullpath) ):
2506 os.mkdir(fullpath)
2523 os.mkdir(fullpath)
2507 self.setFile += 1
2524 self.setFile += 1
2508 file = 'D%4.4d%3.3d%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext)
2525 file = 'D%4.4d%3.3d%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext)
2509
2526
2510 filename = os.path.join(self.wrpath,subfolder, file)
2527 filename = os.path.join(self.wrpath,subfolder, file)
2511
2528
2512 # print self.dataOut.ippSeconds
2529 # print self.dataOut.ippSeconds
2513 freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)/(2*self.dataOut.ippSeconds)
2530 freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)/(2*self.dataOut.ippSeconds)
2514
2531
2515 col1=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq)
2532 col1=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq)
2516 col2=self.wrObj.writeData(name="P_Ch1",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[0,:]))
2533 col2=self.wrObj.writeData(name="P_Ch1",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[0,:]))
2517 col3=self.wrObj.writeData(name="P_Ch2",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[1,:]))
2534 col3=self.wrObj.writeData(name="P_Ch2",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[1,:]))
2518 col4=self.wrObj.writeData(name="P_Ch3",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[2,:]))
2535 col4=self.wrObj.writeData(name="P_Ch3",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[2,:]))
2519 col5=self.wrObj.writeData(name="P_Ch4",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[3,:]))
2536 col5=self.wrObj.writeData(name="P_Ch4",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[3,:]))
2520 col6=self.wrObj.writeData(name="P_Ch5",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[4,:]))
2537 col6=self.wrObj.writeData(name="P_Ch5",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[4,:]))
2521 col7=self.wrObj.writeData(name="P_Ch6",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[5,:]))
2538 col7=self.wrObj.writeData(name="P_Ch6",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[5,:]))
2522 col8=self.wrObj.writeData(name="P_Ch7",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[6,:]))
2539 col8=self.wrObj.writeData(name="P_Ch7",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[6,:]))
2523 col9=self.wrObj.writeData(name="P_Ch8",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[7,:]))
2540 col9=self.wrObj.writeData(name="P_Ch8",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[7,:]))
2524 #n=numpy.arange((100))
2541 #n=numpy.arange((100))
2525 n=self.dataOut.data_spc[6,:]
2542 n=self.dataOut.data_spc[6,:]
2526 a=self.wrObj.cFImage(n)
2543 a=self.wrObj.cFImage(n)
2527 b=self.wrObj.Ctable(col1,col2,col3,col4,col5,col6,col7,col8,col9)
2544 b=self.wrObj.Ctable(col1,col2,col3,col4,col5,col6,col7,col8,col9)
2528 self.wrObj.CFile(a,b)
2545 self.wrObj.CFile(a,b)
2529 self.wrObj.wFile(filename)
2546 self.wrObj.wFile(filename)
2530 return 1
2547 return 1
2531
2548
2532 class FITS:
2549 class FITS:
2533
2550
2534 name=None
2551 name=None
2535 format=None
2552 format=None
2536 array =None
2553 array =None
2537 data =None
2554 data =None
2538 thdulist=None
2555 thdulist=None
2539
2556
2540 def __init__(self):
2557 def __init__(self):
2541
2558
2542 pass
2559 pass
2543
2560
2544 def setColF(self,name,format,array):
2561 def setColF(self,name,format,array):
2545 self.name=name
2562 self.name=name
2546 self.format=format
2563 self.format=format
2547 self.array=array
2564 self.array=array
2548 a1=numpy.array([self.array],dtype=numpy.float32)
2565 a1=numpy.array([self.array],dtype=numpy.float32)
2549 self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1)
2566 self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1)
2550 return self.col1
2567 return self.col1
2551
2568
2552 # def setColP(self,name,format,data):
2569 # def setColP(self,name,format,data):
2553 # self.name=name
2570 # self.name=name
2554 # self.format=format
2571 # self.format=format
2555 # self.data=data
2572 # self.data=data
2556 # a2=numpy.array([self.data],dtype=numpy.float32)
2573 # a2=numpy.array([self.data],dtype=numpy.float32)
2557 # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2574 # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2558 # return self.col2
2575 # return self.col2
2559
2576
2560 def writeHeader(self,):
2577 def writeHeader(self,):
2561 pass
2578 pass
2562
2579
2563 def writeData(self,name,format,data):
2580 def writeData(self,name,format,data):
2564 self.name=name
2581 self.name=name
2565 self.format=format
2582 self.format=format
2566 self.data=data
2583 self.data=data
2567 a2=numpy.array([self.data],dtype=numpy.float32)
2584 a2=numpy.array([self.data],dtype=numpy.float32)
2568 self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2585 self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2569 return self.col2
2586 return self.col2
2570
2587
2571 def cFImage(self,n):
2588 def cFImage(self,n):
2572 self.hdu= pyfits.PrimaryHDU(n)
2589 self.hdu= pyfits.PrimaryHDU(n)
2573 return self.hdu
2590 return self.hdu
2574
2591
2575 def Ctable(self,col1,col2,col3,col4,col5,col6,col7,col8,col9):
2592 def Ctable(self,col1,col2,col3,col4,col5,col6,col7,col8,col9):
2576 self.cols=pyfits.ColDefs( [col1,col2,col3,col4,col5,col6,col7,col8,col9])
2593 self.cols=pyfits.ColDefs( [col1,col2,col3,col4,col5,col6,col7,col8,col9])
2577 self.tbhdu = pyfits.new_table(self.cols)
2594 self.tbhdu = pyfits.new_table(self.cols)
2578 return self.tbhdu
2595 return self.tbhdu
2579
2596
2580 def CFile(self,hdu,tbhdu):
2597 def CFile(self,hdu,tbhdu):
2581 self.thdulist=pyfits.HDUList([hdu,tbhdu])
2598 self.thdulist=pyfits.HDUList([hdu,tbhdu])
2582
2599
2583 def wFile(self,filename):
2600 def wFile(self,filename):
2584 self.thdulist.writeto(filename) No newline at end of file
2601 self.thdulist.writeto(filename)
General Comments 0
You need to be logged in to leave comments. Login now