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