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