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