##// END OF EJS Templates
letras
José Chávez -
r1069:1b24a2b59c23
parent child
Show More
@@ -1,1795 +1,1795
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import sys
7 import sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import inspect
12 import inspect
13 import time, datetime
13 import time, datetime
14 import traceback
14 import traceback
15 import zmq
15 import zmq
16
16
17 try:
17 try:
18 from gevent import sleep
18 from gevent import sleep
19 except:
19 except:
20 from time import sleep
20 from time import sleep
21
21
22 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
22 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
23 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
23 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
24
24
25 LOCALTIME = True
25 LOCALTIME = True
26
26
27 def isNumber(cad):
27 def isNumber(cad):
28 """
28 """
29 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
29 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
30
30
31 Excepciones:
31 Excepciones:
32 Si un determinado string no puede ser convertido a numero
32 Si un determinado string no puede ser convertido a numero
33 Input:
33 Input:
34 str, string al cual se le analiza para determinar si convertible a un numero o no
34 str, string al cual se le analiza para determinar si convertible a un numero o no
35
35
36 Return:
36 Return:
37 True : si el string es uno numerico
37 True : si el string es uno numerico
38 False : no es un string numerico
38 False : no es un string numerico
39 """
39 """
40 try:
40 try:
41 float( cad )
41 float( cad )
42 return True
42 return True
43 except:
43 except:
44 return False
44 return False
45
45
46 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
46 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
47 """
47 """
48 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
48 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
49
49
50 Inputs:
50 Inputs:
51 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
51 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
52
52
53 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
53 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
54 segundos contados desde 01/01/1970.
54 segundos contados desde 01/01/1970.
55 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
55 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
56 segundos contados desde 01/01/1970.
56 segundos contados desde 01/01/1970.
57
57
58 Return:
58 Return:
59 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
59 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
60 fecha especificado, de lo contrario retorna False.
60 fecha especificado, de lo contrario retorna False.
61
61
62 Excepciones:
62 Excepciones:
63 Si el archivo no existe o no puede ser abierto
63 Si el archivo no existe o no puede ser abierto
64 Si la cabecera no puede ser leida.
64 Si la cabecera no puede ser leida.
65
65
66 """
66 """
67 basicHeaderObj = BasicHeader(LOCALTIME)
67 basicHeaderObj = BasicHeader(LOCALTIME)
68
68
69 try:
69 try:
70 fp = open(filename,'rb')
70 fp = open(filename,'rb')
71 except IOError:
71 except IOError:
72 print "The file %s can't be opened" %(filename)
72 print "The file %s can't be opened" %(filename)
73 return 0
73 return 0
74
74
75 sts = basicHeaderObj.read(fp)
75 sts = basicHeaderObj.read(fp)
76 fp.close()
76 fp.close()
77
77
78 if not(sts):
78 if not(sts):
79 print "Skipping the file %s because it has not a valid header" %(filename)
79 print "Skipping the file %s because it has not a valid header" %(filename)
80 return 0
80 return 0
81
81
82 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
82 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
83 return 0
83 return 0
84
84
85 return 1
85 return 1
86
86
87 def isTimeInRange(thisTime, startTime, endTime):
87 def isTimeInRange(thisTime, startTime, endTime):
88
88
89 if endTime >= startTime:
89 if endTime >= startTime:
90 if (thisTime < startTime) or (thisTime > endTime):
90 if (thisTime < startTime) or (thisTime > endTime):
91 return 0
91 return 0
92
92
93 return 1
93 return 1
94 else:
94 else:
95 if (thisTime < startTime) and (thisTime > endTime):
95 if (thisTime < startTime) and (thisTime > endTime):
96 return 0
96 return 0
97
97
98 return 1
98 return 1
99
99
100 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
100 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
101 """
101 """
102 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
102 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
103
103
104 Inputs:
104 Inputs:
105 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
105 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
106
106
107 startDate : fecha inicial del rango seleccionado en formato datetime.date
107 startDate : fecha inicial del rango seleccionado en formato datetime.date
108
108
109 endDate : fecha final del rango seleccionado en formato datetime.date
109 endDate : fecha final del rango seleccionado en formato datetime.date
110
110
111 startTime : tiempo inicial del rango seleccionado en formato datetime.time
111 startTime : tiempo inicial del rango seleccionado en formato datetime.time
112
112
113 endTime : tiempo final del rango seleccionado en formato datetime.time
113 endTime : tiempo final del rango seleccionado en formato datetime.time
114
114
115 Return:
115 Return:
116 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
116 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
117 fecha especificado, de lo contrario retorna False.
117 fecha especificado, de lo contrario retorna False.
118
118
119 Excepciones:
119 Excepciones:
120 Si el archivo no existe o no puede ser abierto
120 Si el archivo no existe o no puede ser abierto
121 Si la cabecera no puede ser leida.
121 Si la cabecera no puede ser leida.
122
122
123 """
123 """
124
124
125
125
126 try:
126 try:
127 fp = open(filename,'rb')
127 fp = open(filename,'rb')
128 except IOError:
128 except IOError:
129 print "The file %s can't be opened" %(filename)
129 print "The file %s can't be opened" %(filename)
130 return None
130 return None
131
131
132 firstBasicHeaderObj = BasicHeader(LOCALTIME)
132 firstBasicHeaderObj = BasicHeader(LOCALTIME)
133 systemHeaderObj = SystemHeader()
133 systemHeaderObj = SystemHeader()
134 radarControllerHeaderObj = RadarControllerHeader()
134 radarControllerHeaderObj = RadarControllerHeader()
135 processingHeaderObj = ProcessingHeader()
135 processingHeaderObj = ProcessingHeader()
136
136
137 lastBasicHeaderObj = BasicHeader(LOCALTIME)
137 lastBasicHeaderObj = BasicHeader(LOCALTIME)
138
138
139 sts = firstBasicHeaderObj.read(fp)
139 sts = firstBasicHeaderObj.read(fp)
140
140
141 if not(sts):
141 if not(sts):
142 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
142 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
143 return None
143 return None
144
144
145 if not systemHeaderObj.read(fp):
145 if not systemHeaderObj.read(fp):
146 return None
146 return None
147
147
148 if not radarControllerHeaderObj.read(fp):
148 if not radarControllerHeaderObj.read(fp):
149 return None
149 return None
150
150
151 if not processingHeaderObj.read(fp):
151 if not processingHeaderObj.read(fp):
152 return None
152 return None
153
153
154 filesize = os.path.getsize(filename)
154 filesize = os.path.getsize(filename)
155
155
156 offset = processingHeaderObj.blockSize + 24 #header size
156 offset = processingHeaderObj.blockSize + 24 #header size
157
157
158 if filesize <= offset:
158 if filesize <= offset:
159 print "[Reading] %s: This file has not enough data" %filename
159 print "[Reading] %s: This file has not enough data" %filename
160 return None
160 return None
161
161
162 fp.seek(-offset, 2)
162 fp.seek(-offset, 2)
163
163
164 sts = lastBasicHeaderObj.read(fp)
164 sts = lastBasicHeaderObj.read(fp)
165
165
166 fp.close()
166 fp.close()
167
167
168 thisDatetime = lastBasicHeaderObj.datatime
168 thisDatetime = lastBasicHeaderObj.datatime
169 thisTime_last_block = thisDatetime.time()
169 thisTime_last_block = thisDatetime.time()
170
170
171 thisDatetime = firstBasicHeaderObj.datatime
171 thisDatetime = firstBasicHeaderObj.datatime
172 thisDate = thisDatetime.date()
172 thisDate = thisDatetime.date()
173 thisTime_first_block = thisDatetime.time()
173 thisTime_first_block = thisDatetime.time()
174
174
175 #General case
175 #General case
176 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
176 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
177 #-----------o----------------------------o-----------
177 #-----------o----------------------------o-----------
178 # startTime endTime
178 # startTime endTime
179
179
180 if endTime >= startTime:
180 if endTime >= startTime:
181 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
181 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
182 return None
182 return None
183
183
184 return thisDatetime
184 return thisDatetime
185
185
186 #If endTime < startTime then endTime belongs to the next day
186 #If endTime < startTime then endTime belongs to the next day
187
187
188
188
189 #<<<<<<<<<<<o o>>>>>>>>>>>
189 #<<<<<<<<<<<o o>>>>>>>>>>>
190 #-----------o----------------------------o-----------
190 #-----------o----------------------------o-----------
191 # endTime startTime
191 # endTime startTime
192
192
193 if (thisDate == startDate) and (thisTime_last_block < startTime):
193 if (thisDate == startDate) and (thisTime_last_block < startTime):
194 return None
194 return None
195
195
196 if (thisDate == endDate) and (thisTime_first_block > endTime):
196 if (thisDate == endDate) and (thisTime_first_block > endTime):
197 return None
197 return None
198
198
199 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
199 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
200 return None
200 return None
201
201
202 return thisDatetime
202 return thisDatetime
203
203
204 def isFolderInDateRange(folder, startDate=None, endDate=None):
204 def isFolderInDateRange(folder, startDate=None, endDate=None):
205 """
205 """
206 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
206 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
207
207
208 Inputs:
208 Inputs:
209 folder : nombre completo del directorio.
209 folder : nombre completo del directorio.
210 Su formato deberia ser "/path_root/?YYYYDDD"
210 Su formato deberia ser "/path_root/?YYYYDDD"
211
211
212 siendo:
212 siendo:
213 YYYY : Anio (ejemplo 2015)
213 YYYY : Anio (ejemplo 2015)
214 DDD : Dia del anio (ejemplo 305)
214 DDD : Dia del anio (ejemplo 305)
215
215
216 startDate : fecha inicial del rango seleccionado en formato datetime.date
216 startDate : fecha inicial del rango seleccionado en formato datetime.date
217
217
218 endDate : fecha final del rango seleccionado en formato datetime.date
218 endDate : fecha final del rango seleccionado en formato datetime.date
219
219
220 Return:
220 Return:
221 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
221 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
222 fecha especificado, de lo contrario retorna False.
222 fecha especificado, de lo contrario retorna False.
223 Excepciones:
223 Excepciones:
224 Si el directorio no tiene el formato adecuado
224 Si el directorio no tiene el formato adecuado
225 """
225 """
226
226
227 basename = os.path.basename(folder)
227 basename = os.path.basename(folder)
228
228
229 if not isRadarFolder(basename):
229 if not isRadarFolder(basename):
230 print "The folder %s has not the rigth format" %folder
230 print "The folder %s has not the rigth format" %folder
231 return 0
231 return 0
232
232
233 if startDate and endDate:
233 if startDate and endDate:
234 thisDate = getDateFromRadarFolder(basename)
234 thisDate = getDateFromRadarFolder(basename)
235
235
236 if thisDate < startDate:
236 if thisDate < startDate:
237 return 0
237 return 0
238
238
239 if thisDate > endDate:
239 if thisDate > endDate:
240 return 0
240 return 0
241
241
242 return 1
242 return 1
243
243
244 def isFileInDateRange(filename, startDate=None, endDate=None):
244 def isFileInDateRange(filename, startDate=None, endDate=None):
245 """
245 """
246 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
246 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
247
247
248 Inputs:
248 Inputs:
249 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
249 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
250
250
251 Su formato deberia ser "?YYYYDDDsss"
251 Su formato deberia ser "?YYYYDDDsss"
252
252
253 siendo:
253 siendo:
254 YYYY : Anio (ejemplo 2015)
254 YYYY : Anio (ejemplo 2015)
255 DDD : Dia del anio (ejemplo 305)
255 DDD : Dia del anio (ejemplo 305)
256 sss : set
256 sss : set
257
257
258 startDate : fecha inicial del rango seleccionado en formato datetime.date
258 startDate : fecha inicial del rango seleccionado en formato datetime.date
259
259
260 endDate : fecha final del rango seleccionado en formato datetime.date
260 endDate : fecha final del rango seleccionado en formato datetime.date
261
261
262 Return:
262 Return:
263 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
263 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
264 fecha especificado, de lo contrario retorna False.
264 fecha especificado, de lo contrario retorna False.
265 Excepciones:
265 Excepciones:
266 Si el archivo no tiene el formato adecuado
266 Si el archivo no tiene el formato adecuado
267 """
267 """
268
268
269 basename = os.path.basename(filename)
269 basename = os.path.basename(filename)
270
270
271 if not isRadarFile(basename):
271 if not isRadarFile(basename):
272 print "The filename %s has not the rigth format" %filename
272 print "The filename %s has not the rigth format" %filename
273 return 0
273 return 0
274
274
275 if startDate and endDate:
275 if startDate and endDate:
276 thisDate = getDateFromRadarFile(basename)
276 thisDate = getDateFromRadarFile(basename)
277
277
278 if thisDate < startDate:
278 if thisDate < startDate:
279 return 0
279 return 0
280
280
281 if thisDate > endDate:
281 if thisDate > endDate:
282 return 0
282 return 0
283
283
284 return 1
284 return 1
285
285
286 def getFileFromSet(path, ext, set):
286 def getFileFromSet(path, ext, set):
287 validFilelist = []
287 validFilelist = []
288 fileList = os.listdir(path)
288 fileList = os.listdir(path)
289
289
290 # 0 1234 567 89A BCDE
290 # 0 1234 567 89A BCDE
291 # H YYYY DDD SSS .ext
291 # H YYYY DDD SSS .ext
292
292
293 for thisFile in fileList:
293 for thisFile in fileList:
294 try:
294 try:
295 year = int(thisFile[1:5])
295 year = int(thisFile[1:5])
296 doy = int(thisFile[5:8])
296 doy = int(thisFile[5:8])
297 except:
297 except:
298 continue
298 continue
299
299
300 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
300 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
301 continue
301 continue
302
302
303 validFilelist.append(thisFile)
303 validFilelist.append(thisFile)
304
304
305 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
305 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
306
306
307 if len(myfile)!= 0:
307 if len(myfile)!= 0:
308 return myfile[0]
308 return myfile[0]
309 else:
309 else:
310 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
310 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
311 print 'the filename %s does not exist'%filename
311 print 'the filename %s does not exist'%filename
312 print '...going to the last file: '
312 print '...going to the last file: '
313
313
314 if validFilelist:
314 if validFilelist:
315 validFilelist = sorted( validFilelist, key=str.lower )
315 validFilelist = sorted( validFilelist, key=str.lower )
316 return validFilelist[-1]
316 return validFilelist[-1]
317
317
318 return None
318 return None
319
319
320 def getlastFileFromPath(path, ext):
320 def getlastFileFromPath(path, ext):
321 """
321 """
322 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
322 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
323 al final de la depuracion devuelve el ultimo file de la lista que quedo.
323 al final de la depuracion devuelve el ultimo file de la lista que quedo.
324
324
325 Input:
325 Input:
326 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
326 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
327 ext : extension de los files contenidos en una carpeta
327 ext : extension de los files contenidos en una carpeta
328
328
329 Return:
329 Return:
330 El ultimo file de una determinada carpeta, no se considera el path.
330 El ultimo file de una determinada carpeta, no se considera el path.
331 """
331 """
332 validFilelist = []
332 validFilelist = []
333 fileList = os.listdir(path)
333 fileList = os.listdir(path)
334
334
335 # 0 1234 567 89A BCDE
335 # 0 1234 567 89A BCDE
336 # H YYYY DDD SSS .ext
336 # H YYYY DDD SSS .ext
337
337
338 for thisFile in fileList:
338 for thisFile in fileList:
339
339
340 year = thisFile[1:5]
340 year = thisFile[1:5]
341 if not isNumber(year):
341 if not isNumber(year):
342 continue
342 continue
343
343
344 doy = thisFile[5:8]
344 doy = thisFile[5:8]
345 if not isNumber(doy):
345 if not isNumber(doy):
346 continue
346 continue
347
347
348 year = int(year)
348 year = int(year)
349 doy = int(doy)
349 doy = int(doy)
350
350
351 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
351 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
352 continue
352 continue
353
353
354 validFilelist.append(thisFile)
354 validFilelist.append(thisFile)
355
355
356 if validFilelist:
356 if validFilelist:
357 validFilelist = sorted( validFilelist, key=str.lower )
357 validFilelist = sorted( validFilelist, key=str.lower )
358 return validFilelist[-1]
358 return validFilelist[-1]
359
359
360 return None
360 return None
361
361
362 def checkForRealPath(path, foldercounter, year, doy, set, ext):
362 def checkForRealPath(path, foldercounter, year, doy, set, ext):
363 """
363 """
364 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
364 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
365 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
365 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
366 el path exacto de un determinado file.
366 el path exacto de un determinado file.
367
367
368 Example :
368 Example :
369 nombre correcto del file es .../.../D2009307/P2009307367.ext
369 nombre correcto del file es .../.../D2009307/P2009307367.ext
370
370
371 Entonces la funcion prueba con las siguientes combinaciones
371 Entonces la funcion prueba con las siguientes combinaciones
372 .../.../y2009307367.ext
372 .../.../y2009307367.ext
373 .../.../Y2009307367.ext
373 .../.../Y2009307367.ext
374 .../.../x2009307/y2009307367.ext
374 .../.../x2009307/y2009307367.ext
375 .../.../x2009307/Y2009307367.ext
375 .../.../x2009307/Y2009307367.ext
376 .../.../X2009307/y2009307367.ext
376 .../.../X2009307/y2009307367.ext
377 .../.../X2009307/Y2009307367.ext
377 .../.../X2009307/Y2009307367.ext
378 siendo para este caso, la ultima combinacion de letras, identica al file buscado
378 siendo para este caso, la ultima combinacion de letras, identica al file buscado
379
379
380 Return:
380 Return:
381 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
381 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
382 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
382 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
383 para el filename
383 para el filename
384 """
384 """
385 fullfilename = None
385 fullfilename = None
386 find_flag = False
386 find_flag = False
387 filename = None
387 filename = None
388
388
389 prefixDirList = [None,'d','D']
389 prefixDirList = [None,'d','D']
390 if ext.lower() == ".r": #voltage
390 if ext.lower() == ".r": #voltage
391 prefixFileList = ['d','D']
391 prefixFileList = ['d','D']
392 elif ext.lower() == ".pdata": #spectra
392 elif ext.lower() == ".pdata": #spectra
393 prefixFileList = ['p','P']
393 prefixFileList = ['p','P']
394 else:
394 else:
395 return None, filename
395 return None, filename
396
396
397 #barrido por las combinaciones posibles
397 #barrido por las combinaciones posibles
398 for prefixDir in prefixDirList:
398 for prefixDir in prefixDirList:
399 thispath = path
399 thispath = path
400 if prefixDir != None:
400 if prefixDir != None:
401 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
401 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
402 if foldercounter == 0:
402 if foldercounter == 0:
403 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
403 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
404 else:
404 else:
405 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
405 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
406 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
406 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
407 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
407 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
408 fullfilename = os.path.join( thispath, filename ) #formo el path completo
408 fullfilename = os.path.join( thispath, filename ) #formo el path completo
409
409
410 if os.path.exists( fullfilename ): #verifico que exista
410 if os.path.exists( fullfilename ): #verifico que exista
411 find_flag = True
411 find_flag = True
412 break
412 break
413 if find_flag:
413 if find_flag:
414 break
414 break
415
415
416 if not(find_flag):
416 if not(find_flag):
417 return None, filename
417 return None, filename
418
418
419 return fullfilename, filename
419 return fullfilename, filename
420
420
421 def isRadarFolder(folder):
421 def isRadarFolder(folder):
422 try:
422 try:
423 year = int(folder[1:5])
423 year = int(folder[1:5])
424 doy = int(folder[5:8])
424 doy = int(folder[5:8])
425 except:
425 except:
426 return 0
426 return 0
427
427
428 return 1
428 return 1
429
429
430 def isRadarFile(file):
430 def isRadarFile(file):
431 try:
431 try:
432 year = int(file[1:5])
432 year = int(file[1:5])
433 doy = int(file[5:8])
433 doy = int(file[5:8])
434 set = int(file[8:11])
434 set = int(file[8:11])
435 except:
435 except:
436 return 0
436 return 0
437
437
438 return 1
438 return 1
439
439
440 def getDateFromRadarFile(file):
440 def getDateFromRadarFile(file):
441 try:
441 try:
442 year = int(file[1:5])
442 year = int(file[1:5])
443 doy = int(file[5:8])
443 doy = int(file[5:8])
444 set = int(file[8:11])
444 set = int(file[8:11])
445 except:
445 except:
446 return None
446 return None
447
447
448 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
448 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
449 return thisDate
449 return thisDate
450
450
451 def getDateFromRadarFolder(folder):
451 def getDateFromRadarFolder(folder):
452 try:
452 try:
453 year = int(folder[1:5])
453 year = int(folder[1:5])
454 doy = int(folder[5:8])
454 doy = int(folder[5:8])
455 except:
455 except:
456 return None
456 return None
457
457
458 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
458 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
459 return thisDate
459 return thisDate
460
460
461 class JRODataIO:
461 class JRODataIO:
462
462
463 c = 3E8
463 c = 3E8
464
464
465 isConfig = False
465 isConfig = False
466
466
467 basicHeaderObj = None
467 basicHeaderObj = None
468
468
469 systemHeaderObj = None
469 systemHeaderObj = None
470
470
471 radarControllerHeaderObj = None
471 radarControllerHeaderObj = None
472
472
473 processingHeaderObj = None
473 processingHeaderObj = None
474
474
475 dtype = None
475 dtype = None
476
476
477 pathList = []
477 pathList = []
478
478
479 filenameList = []
479 filenameList = []
480
480
481 filename = None
481 filename = None
482
482
483 ext = None
483 ext = None
484
484
485 flagIsNewFile = 1
485 flagIsNewFile = 1
486
486
487 flagDiscontinuousBlock = 0
487 flagDiscontinuousBlock = 0
488
488
489 flagIsNewBlock = 0
489 flagIsNewBlock = 0
490
490
491 fp = None
491 fp = None
492
492
493 firstHeaderSize = 0
493 firstHeaderSize = 0
494
494
495 basicHeaderSize = 24
495 basicHeaderSize = 24
496
496
497 versionFile = 1103
497 versionFile = 1103
498
498
499 fileSize = None
499 fileSize = None
500
500
501 # ippSeconds = None
501 # ippSeconds = None
502
502
503 fileSizeByHeader = None
503 fileSizeByHeader = None
504
504
505 fileIndex = None
505 fileIndex = None
506
506
507 profileIndex = None
507 profileIndex = None
508
508
509 blockIndex = None
509 blockIndex = None
510
510
511 nTotalBlocks = None
511 nTotalBlocks = None
512
512
513 maxTimeStep = 30
513 maxTimeStep = 30
514
514
515 lastUTTime = None
515 lastUTTime = None
516
516
517 datablock = None
517 datablock = None
518
518
519 dataOut = None
519 dataOut = None
520
520
521 blocksize = None
521 blocksize = None
522
522
523 getByBlock = False
523 getByBlock = False
524
524
525 def __init__(self):
525 def __init__(self):
526
526
527 raise NotImplementedError
527 raise NotImplementedError
528
528
529 def run(self):
529 def run(self):
530
530
531 raise NotImplementedError
531 raise NotImplementedError
532
532
533 def getDtypeWidth(self):
533 def getDtypeWidth(self):
534
534
535 dtype_index = get_dtype_index(self.dtype)
535 dtype_index = get_dtype_index(self.dtype)
536 dtype_width = get_dtype_width(dtype_index)
536 dtype_width = get_dtype_width(dtype_index)
537
537
538 return dtype_width
538 return dtype_width
539
539
540 def getAllowedArgs(self):
540 def getAllowedArgs(self):
541 return inspect.getargspec(self.run).args
541 return inspect.getargspec(self.run).args
542
542
543 class JRODataReader(JRODataIO):
543 class JRODataReader(JRODataIO):
544
544
545 online = 0
545 online = 0
546
546
547 realtime = 0
547 realtime = 0
548
548
549 nReadBlocks = 0
549 nReadBlocks = 0
550
550
551 delay = 10 #number of seconds waiting a new file
551 delay = 10 #number of seconds waiting a new file
552
552
553 nTries = 3 #quantity tries
553 nTries = 3 #quantity tries
554
554
555 nFiles = 3 #number of files for searching
555 nFiles = 3 #number of files for searching
556
556
557 path = None
557 path = None
558
558
559 foldercounter = 0
559 foldercounter = 0
560
560
561 flagNoMoreFiles = 0
561 flagNoMoreFiles = 0
562
562
563 datetimeList = []
563 datetimeList = []
564
564
565 __isFirstTimeOnline = 1
565 __isFirstTimeOnline = 1
566
566
567 __printInfo = True
567 __printInfo = True
568
568
569 profileIndex = None
569 profileIndex = None
570
570
571 nTxs = 1
571 nTxs = 1
572
572
573 txIndex = None
573 txIndex = None
574
574
575 #Added--------------------
575 #Added--------------------
576
576
577 selBlocksize = None
577 selBlocksize = None
578
578
579 selBlocktime = None
579 selBlocktime = None
580
580
581 def __init__(self):
581 def __init__(self):
582
582
583 """
583 """
584 This class is used to find data files
584 This class is used to find data files
585
585
586 Example:
586 Example:
587 reader = JRODataReader()
587 reader = JRODataReader()
588 fileList = reader.findDataFiles()
588 fileList = reader.findDataFiles()
589
589
590 """
590 """
591 pass
591 pass
592
592
593
593
594 def createObjByDefault(self):
594 def createObjByDefault(self):
595 """
595 """
596
596
597 """
597 """
598 raise NotImplementedError
598 raise NotImplementedError
599
599
600 def getBlockDimension(self):
600 def getBlockDimension(self):
601
601
602 raise NotImplementedError
602 raise NotImplementedError
603
603
604 def searchFilesOffLine(self,
604 def searchFilesOffLine(self,
605 path,
605 path,
606 startDate=None,
606 startDate=None,
607 endDate=None,
607 endDate=None,
608 startTime=datetime.time(0,0,0),
608 startTime=datetime.time(0,0,0),
609 endTime=datetime.time(23,59,59),
609 endTime=datetime.time(23,59,59),
610 set=None,
610 set=None,
611 expLabel='',
611 expLabel='',
612 ext='.r',
612 ext='.r',
613 cursor=None,
613 cursor=None,
614 skip=None,
614 skip=None,
615 walk=True):
615 walk=True):
616
616
617 self.filenameList = []
617 self.filenameList = []
618 self.datetimeList = []
618 self.datetimeList = []
619
619
620 pathList = []
620 pathList = []
621
621
622 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
622 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
623
623
624 if dateList == []:
624 if dateList == []:
625 return [], []
625 return [], []
626
626
627 if len(dateList) > 1:
627 if len(dateList) > 1:
628 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
628 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
629 else:
629 else:
630 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
630 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
631
631
632 filenameList = []
632 filenameList = []
633 datetimeList = []
633 datetimeList = []
634
634
635 for thisPath in pathList:
635 for thisPath in pathList:
636
636
637 fileList = glob.glob1(thisPath, "*%s" %ext)
637 fileList = glob.glob1(thisPath, "*%s" %ext)
638 fileList.sort()
638 fileList.sort()
639
639
640 skippedFileList = []
640 skippedFileList = []
641
641
642 if cursor is not None andk skip is not None:
642 if cursor is not None and skip is not None:
643
643
644 if skip == 0:
644 if skip == 0:
645 skippedFileList = []
645 skippedFileList = []
646 else:
646 else:
647 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
647 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
648
648
649 else:
649 else:
650 skippedFileList = fileList
650 skippedFileList = fileList
651
651
652 for file in skippedFileList:
652 for file in skippedFileList:
653
653
654 filename = os.path.join(thisPath,file)
654 filename = os.path.join(thisPath,file)
655
655
656 if not isFileInDateRange(filename, startDate, endDate):
656 if not isFileInDateRange(filename, startDate, endDate):
657 continue
657 continue
658
658
659 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
659 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
660
660
661 if not(thisDatetime):
661 if not(thisDatetime):
662 continue
662 continue
663
663
664 filenameList.append(filename)
664 filenameList.append(filename)
665 datetimeList.append(thisDatetime)
665 datetimeList.append(thisDatetime)
666
666
667 if not(filenameList):
667 if not(filenameList):
668 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
668 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
669 return [], []
669 return [], []
670
670
671 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
671 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
672 print
672 print
673
673
674 # for i in range(len(filenameList)):
674 # for i in range(len(filenameList)):
675 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
675 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
676
676
677 self.filenameList = filenameList
677 self.filenameList = filenameList
678 self.datetimeList = datetimeList
678 self.datetimeList = datetimeList
679
679
680 return pathList, filenameList
680 return pathList, filenameList
681
681
682 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
682 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
683
683
684 """
684 """
685 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
685 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
686 devuelve el archivo encontrado ademas de otros datos.
686 devuelve el archivo encontrado ademas de otros datos.
687
687
688 Input:
688 Input:
689 path : carpeta donde estan contenidos los files que contiene data
689 path : carpeta donde estan contenidos los files que contiene data
690
690
691 expLabel : Nombre del subexperimento (subfolder)
691 expLabel : Nombre del subexperimento (subfolder)
692
692
693 ext : extension de los files
693 ext : extension de los files
694
694
695 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
695 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
696
696
697 Return:
697 Return:
698 directory : eL directorio donde esta el file encontrado
698 directory : eL directorio donde esta el file encontrado
699 filename : el ultimo file de una determinada carpeta
699 filename : el ultimo file de una determinada carpeta
700 year : el anho
700 year : el anho
701 doy : el numero de dia del anho
701 doy : el numero de dia del anho
702 set : el set del archivo
702 set : el set del archivo
703
703
704
704
705 """
705 """
706 if not os.path.isdir(path):
706 if not os.path.isdir(path):
707 return None, None, None, None, None, None
707 return None, None, None, None, None, None
708
708
709 dirList = []
709 dirList = []
710
710
711 if not walk:
711 if not walk:
712 fullpath = path
712 fullpath = path
713 foldercounter = 0
713 foldercounter = 0
714 else:
714 else:
715 #Filtra solo los directorios
715 #Filtra solo los directorios
716 for thisPath in os.listdir(path):
716 for thisPath in os.listdir(path):
717 if not os.path.isdir(os.path.join(path,thisPath)):
717 if not os.path.isdir(os.path.join(path,thisPath)):
718 continue
718 continue
719 if not isRadarFolder(thisPath):
719 if not isRadarFolder(thisPath):
720 continue
720 continue
721
721
722 dirList.append(thisPath)
722 dirList.append(thisPath)
723
723
724 if not(dirList):
724 if not(dirList):
725 return None, None, None, None, None, None
725 return None, None, None, None, None, None
726
726
727 dirList = sorted( dirList, key=str.lower )
727 dirList = sorted( dirList, key=str.lower )
728
728
729 doypath = dirList[-1]
729 doypath = dirList[-1]
730 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
730 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
731 fullpath = os.path.join(path, doypath, expLabel)
731 fullpath = os.path.join(path, doypath, expLabel)
732
732
733
733
734 print "[Reading] %s folder was found: " %(fullpath )
734 print "[Reading] %s folder was found: " %(fullpath )
735
735
736 if set == None:
736 if set == None:
737 filename = getlastFileFromPath(fullpath, ext)
737 filename = getlastFileFromPath(fullpath, ext)
738 else:
738 else:
739 filename = getFileFromSet(fullpath, ext, set)
739 filename = getFileFromSet(fullpath, ext, set)
740
740
741 if not(filename):
741 if not(filename):
742 return None, None, None, None, None, None
742 return None, None, None, None, None, None
743
743
744 print "[Reading] %s file was found" %(filename)
744 print "[Reading] %s file was found" %(filename)
745
745
746 if not(self.__verifyFile(os.path.join(fullpath, filename))):
746 if not(self.__verifyFile(os.path.join(fullpath, filename))):
747 return None, None, None, None, None, None
747 return None, None, None, None, None, None
748
748
749 year = int( filename[1:5] )
749 year = int( filename[1:5] )
750 doy = int( filename[5:8] )
750 doy = int( filename[5:8] )
751 set = int( filename[8:11] )
751 set = int( filename[8:11] )
752
752
753 return fullpath, foldercounter, filename, year, doy, set
753 return fullpath, foldercounter, filename, year, doy, set
754
754
755 def __setNextFileOffline(self):
755 def __setNextFileOffline(self):
756
756
757 idFile = self.fileIndex
757 idFile = self.fileIndex
758
758
759 while (True):
759 while (True):
760 idFile += 1
760 idFile += 1
761 if not(idFile < len(self.filenameList)):
761 if not(idFile < len(self.filenameList)):
762 self.flagNoMoreFiles = 1
762 self.flagNoMoreFiles = 1
763 # print "[Reading] No more Files"
763 # print "[Reading] No more Files"
764 return 0
764 return 0
765
765
766 filename = self.filenameList[idFile]
766 filename = self.filenameList[idFile]
767
767
768 if not(self.__verifyFile(filename)):
768 if not(self.__verifyFile(filename)):
769 continue
769 continue
770
770
771 fileSize = os.path.getsize(filename)
771 fileSize = os.path.getsize(filename)
772 fp = open(filename,'rb')
772 fp = open(filename,'rb')
773 break
773 break
774
774
775 self.flagIsNewFile = 1
775 self.flagIsNewFile = 1
776 self.fileIndex = idFile
776 self.fileIndex = idFile
777 self.filename = filename
777 self.filename = filename
778 self.fileSize = fileSize
778 self.fileSize = fileSize
779 self.fp = fp
779 self.fp = fp
780
780
781 # print "[Reading] Setting the file: %s"%self.filename
781 # print "[Reading] Setting the file: %s"%self.filename
782
782
783 return 1
783 return 1
784
784
785 def __setNextFileOnline(self):
785 def __setNextFileOnline(self):
786 """
786 """
787 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
787 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
788 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
788 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
789 siguientes.
789 siguientes.
790
790
791 Affected:
791 Affected:
792 self.flagIsNewFile
792 self.flagIsNewFile
793 self.filename
793 self.filename
794 self.fileSize
794 self.fileSize
795 self.fp
795 self.fp
796 self.set
796 self.set
797 self.flagNoMoreFiles
797 self.flagNoMoreFiles
798
798
799 Return:
799 Return:
800 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
800 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
801 1 : si el file fue abierto con exito y esta listo a ser leido
801 1 : si el file fue abierto con exito y esta listo a ser leido
802
802
803 Excepciones:
803 Excepciones:
804 Si un determinado file no puede ser abierto
804 Si un determinado file no puede ser abierto
805 """
805 """
806 nFiles = 0
806 nFiles = 0
807 fileOk_flag = False
807 fileOk_flag = False
808 firstTime_flag = True
808 firstTime_flag = True
809
809
810 self.set += 1
810 self.set += 1
811
811
812 if self.set > 999:
812 if self.set > 999:
813 self.set = 0
813 self.set = 0
814 self.foldercounter += 1
814 self.foldercounter += 1
815
815
816 #busca el 1er file disponible
816 #busca el 1er file disponible
817 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
817 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
818 if fullfilename:
818 if fullfilename:
819 if self.__verifyFile(fullfilename, False):
819 if self.__verifyFile(fullfilename, False):
820 fileOk_flag = True
820 fileOk_flag = True
821
821
822 #si no encuentra un file entonces espera y vuelve a buscar
822 #si no encuentra un file entonces espera y vuelve a buscar
823 if not(fileOk_flag):
823 if not(fileOk_flag):
824 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
824 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
825
825
826 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
826 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
827 tries = self.nTries
827 tries = self.nTries
828 else:
828 else:
829 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
829 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
830
830
831 for nTries in range( tries ):
831 for nTries in range( tries ):
832 if firstTime_flag:
832 if firstTime_flag:
833 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
833 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
834 sleep( self.delay )
834 sleep( self.delay )
835 else:
835 else:
836 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
836 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
837
837
838 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
838 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
839 if fullfilename:
839 if fullfilename:
840 if self.__verifyFile(fullfilename):
840 if self.__verifyFile(fullfilename):
841 fileOk_flag = True
841 fileOk_flag = True
842 break
842 break
843
843
844 if fileOk_flag:
844 if fileOk_flag:
845 break
845 break
846
846
847 firstTime_flag = False
847 firstTime_flag = False
848
848
849 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
849 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
850 self.set += 1
850 self.set += 1
851
851
852 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
852 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
853 self.set = 0
853 self.set = 0
854 self.doy += 1
854 self.doy += 1
855 self.foldercounter = 0
855 self.foldercounter = 0
856
856
857 if fileOk_flag:
857 if fileOk_flag:
858 self.fileSize = os.path.getsize( fullfilename )
858 self.fileSize = os.path.getsize( fullfilename )
859 self.filename = fullfilename
859 self.filename = fullfilename
860 self.flagIsNewFile = 1
860 self.flagIsNewFile = 1
861 if self.fp != None: self.fp.close()
861 if self.fp != None: self.fp.close()
862 self.fp = open(fullfilename, 'rb')
862 self.fp = open(fullfilename, 'rb')
863 self.flagNoMoreFiles = 0
863 self.flagNoMoreFiles = 0
864 # print '[Reading] Setting the file: %s' % fullfilename
864 # print '[Reading] Setting the file: %s' % fullfilename
865 else:
865 else:
866 self.fileSize = 0
866 self.fileSize = 0
867 self.filename = None
867 self.filename = None
868 self.flagIsNewFile = 0
868 self.flagIsNewFile = 0
869 self.fp = None
869 self.fp = None
870 self.flagNoMoreFiles = 1
870 self.flagNoMoreFiles = 1
871 # print '[Reading] No more files to read'
871 # print '[Reading] No more files to read'
872
872
873 return fileOk_flag
873 return fileOk_flag
874
874
875 def setNextFile(self):
875 def setNextFile(self):
876 if self.fp != None:
876 if self.fp != None:
877 self.fp.close()
877 self.fp.close()
878
878
879 if self.online:
879 if self.online:
880 newFile = self.__setNextFileOnline()
880 newFile = self.__setNextFileOnline()
881 else:
881 else:
882 newFile = self.__setNextFileOffline()
882 newFile = self.__setNextFileOffline()
883
883
884 if not(newFile):
884 if not(newFile):
885 print '[Reading] No more files to read'
885 print '[Reading] No more files to read'
886 return 0
886 return 0
887
887
888 if self.verbose:
888 if self.verbose:
889 print '[Reading] Setting the file: %s' % self.filename
889 print '[Reading] Setting the file: %s' % self.filename
890
890
891 self.__readFirstHeader()
891 self.__readFirstHeader()
892 self.nReadBlocks = 0
892 self.nReadBlocks = 0
893 return 1
893 return 1
894
894
895 def __waitNewBlock(self):
895 def __waitNewBlock(self):
896 """
896 """
897 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
897 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
898
898
899 Si el modo de lectura es OffLine siempre retorn 0
899 Si el modo de lectura es OffLine siempre retorn 0
900 """
900 """
901 if not self.online:
901 if not self.online:
902 return 0
902 return 0
903
903
904 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
904 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
905 return 0
905 return 0
906
906
907 currentPointer = self.fp.tell()
907 currentPointer = self.fp.tell()
908
908
909 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
909 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
910
910
911 for nTries in range( self.nTries ):
911 for nTries in range( self.nTries ):
912
912
913 self.fp.close()
913 self.fp.close()
914 self.fp = open( self.filename, 'rb' )
914 self.fp = open( self.filename, 'rb' )
915 self.fp.seek( currentPointer )
915 self.fp.seek( currentPointer )
916
916
917 self.fileSize = os.path.getsize( self.filename )
917 self.fileSize = os.path.getsize( self.filename )
918 currentSize = self.fileSize - currentPointer
918 currentSize = self.fileSize - currentPointer
919
919
920 if ( currentSize >= neededSize ):
920 if ( currentSize >= neededSize ):
921 self.basicHeaderObj.read(self.fp)
921 self.basicHeaderObj.read(self.fp)
922 return 1
922 return 1
923
923
924 if self.fileSize == self.fileSizeByHeader:
924 if self.fileSize == self.fileSizeByHeader:
925 # self.flagEoF = True
925 # self.flagEoF = True
926 return 0
926 return 0
927
927
928 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
928 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
929 sleep( self.delay )
929 sleep( self.delay )
930
930
931
931
932 return 0
932 return 0
933
933
934 def waitDataBlock(self,pointer_location):
934 def waitDataBlock(self,pointer_location):
935
935
936 currentPointer = pointer_location
936 currentPointer = pointer_location
937
937
938 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
938 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
939
939
940 for nTries in range( self.nTries ):
940 for nTries in range( self.nTries ):
941 self.fp.close()
941 self.fp.close()
942 self.fp = open( self.filename, 'rb' )
942 self.fp = open( self.filename, 'rb' )
943 self.fp.seek( currentPointer )
943 self.fp.seek( currentPointer )
944
944
945 self.fileSize = os.path.getsize( self.filename )
945 self.fileSize = os.path.getsize( self.filename )
946 currentSize = self.fileSize - currentPointer
946 currentSize = self.fileSize - currentPointer
947
947
948 if ( currentSize >= neededSize ):
948 if ( currentSize >= neededSize ):
949 return 1
949 return 1
950
950
951 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
951 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
952 sleep( self.delay )
952 sleep( self.delay )
953
953
954 return 0
954 return 0
955
955
956 def __jumpToLastBlock(self):
956 def __jumpToLastBlock(self):
957
957
958 if not(self.__isFirstTimeOnline):
958 if not(self.__isFirstTimeOnline):
959 return
959 return
960
960
961 csize = self.fileSize - self.fp.tell()
961 csize = self.fileSize - self.fp.tell()
962 blocksize = self.processingHeaderObj.blockSize
962 blocksize = self.processingHeaderObj.blockSize
963
963
964 #salta el primer bloque de datos
964 #salta el primer bloque de datos
965 if csize > self.processingHeaderObj.blockSize:
965 if csize > self.processingHeaderObj.blockSize:
966 self.fp.seek(self.fp.tell() + blocksize)
966 self.fp.seek(self.fp.tell() + blocksize)
967 else:
967 else:
968 return
968 return
969
969
970 csize = self.fileSize - self.fp.tell()
970 csize = self.fileSize - self.fp.tell()
971 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
971 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
972 while True:
972 while True:
973
973
974 if self.fp.tell()<self.fileSize:
974 if self.fp.tell()<self.fileSize:
975 self.fp.seek(self.fp.tell() + neededsize)
975 self.fp.seek(self.fp.tell() + neededsize)
976 else:
976 else:
977 self.fp.seek(self.fp.tell() - neededsize)
977 self.fp.seek(self.fp.tell() - neededsize)
978 break
978 break
979
979
980 # csize = self.fileSize - self.fp.tell()
980 # csize = self.fileSize - self.fp.tell()
981 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
981 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
982 # factor = int(csize/neededsize)
982 # factor = int(csize/neededsize)
983 # if factor > 0:
983 # if factor > 0:
984 # self.fp.seek(self.fp.tell() + factor*neededsize)
984 # self.fp.seek(self.fp.tell() + factor*neededsize)
985
985
986 self.flagIsNewFile = 0
986 self.flagIsNewFile = 0
987 self.__isFirstTimeOnline = 0
987 self.__isFirstTimeOnline = 0
988
988
989 def __setNewBlock(self):
989 def __setNewBlock(self):
990 #if self.server is None:
990 #if self.server is None:
991 if self.fp == None:
991 if self.fp == None:
992 return 0
992 return 0
993
993
994 # if self.online:
994 # if self.online:
995 # self.__jumpToLastBlock()
995 # self.__jumpToLastBlock()
996
996
997 if self.flagIsNewFile:
997 if self.flagIsNewFile:
998 self.lastUTTime = self.basicHeaderObj.utc
998 self.lastUTTime = self.basicHeaderObj.utc
999 return 1
999 return 1
1000
1000
1001 if self.realtime:
1001 if self.realtime:
1002 self.flagDiscontinuousBlock = 1
1002 self.flagDiscontinuousBlock = 1
1003 if not(self.setNextFile()):
1003 if not(self.setNextFile()):
1004 return 0
1004 return 0
1005 else:
1005 else:
1006 return 1
1006 return 1
1007 #if self.server is None:
1007 #if self.server is None:
1008 currentSize = self.fileSize - self.fp.tell()
1008 currentSize = self.fileSize - self.fp.tell()
1009 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1009 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1010 if (currentSize >= neededSize):
1010 if (currentSize >= neededSize):
1011 self.basicHeaderObj.read(self.fp)
1011 self.basicHeaderObj.read(self.fp)
1012 self.lastUTTime = self.basicHeaderObj.utc
1012 self.lastUTTime = self.basicHeaderObj.utc
1013 return 1
1013 return 1
1014 # else:
1014 # else:
1015 # self.basicHeaderObj.read(self.zHeader)
1015 # self.basicHeaderObj.read(self.zHeader)
1016 # self.lastUTTime = self.basicHeaderObj.utc
1016 # self.lastUTTime = self.basicHeaderObj.utc
1017 # return 1
1017 # return 1
1018 if self.__waitNewBlock():
1018 if self.__waitNewBlock():
1019 self.lastUTTime = self.basicHeaderObj.utc
1019 self.lastUTTime = self.basicHeaderObj.utc
1020 return 1
1020 return 1
1021 #if self.server is None:
1021 #if self.server is None:
1022 if not(self.setNextFile()):
1022 if not(self.setNextFile()):
1023 return 0
1023 return 0
1024
1024
1025 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1025 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1026 self.lastUTTime = self.basicHeaderObj.utc
1026 self.lastUTTime = self.basicHeaderObj.utc
1027
1027
1028 self.flagDiscontinuousBlock = 0
1028 self.flagDiscontinuousBlock = 0
1029
1029
1030 if deltaTime > self.maxTimeStep:
1030 if deltaTime > self.maxTimeStep:
1031 self.flagDiscontinuousBlock = 1
1031 self.flagDiscontinuousBlock = 1
1032
1032
1033 return 1
1033 return 1
1034
1034
1035 def readNextBlock(self):
1035 def readNextBlock(self):
1036
1036
1037 #Skip block out of startTime and endTime
1037 #Skip block out of startTime and endTime
1038 while True:
1038 while True:
1039 if not(self.__setNewBlock()):
1039 if not(self.__setNewBlock()):
1040 return 0
1040 return 0
1041
1041
1042 if not(self.readBlock()):
1042 if not(self.readBlock()):
1043 return 0
1043 return 0
1044
1044
1045 self.getBasicHeader()
1045 self.getBasicHeader()
1046
1046
1047 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1047 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1048
1048
1049 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1049 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1050 self.processingHeaderObj.dataBlocksPerFile,
1050 self.processingHeaderObj.dataBlocksPerFile,
1051 self.dataOut.datatime.ctime())
1051 self.dataOut.datatime.ctime())
1052 continue
1052 continue
1053
1053
1054 break
1054 break
1055
1055
1056 if self.verbose:
1056 if self.verbose:
1057 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1057 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1058 self.processingHeaderObj.dataBlocksPerFile,
1058 self.processingHeaderObj.dataBlocksPerFile,
1059 self.dataOut.datatime.ctime())
1059 self.dataOut.datatime.ctime())
1060 return 1
1060 return 1
1061
1061
1062 def __readFirstHeader(self):
1062 def __readFirstHeader(self):
1063
1063
1064 self.basicHeaderObj.read(self.fp)
1064 self.basicHeaderObj.read(self.fp)
1065 self.systemHeaderObj.read(self.fp)
1065 self.systemHeaderObj.read(self.fp)
1066 self.radarControllerHeaderObj.read(self.fp)
1066 self.radarControllerHeaderObj.read(self.fp)
1067 self.processingHeaderObj.read(self.fp)
1067 self.processingHeaderObj.read(self.fp)
1068
1068
1069 self.firstHeaderSize = self.basicHeaderObj.size
1069 self.firstHeaderSize = self.basicHeaderObj.size
1070
1070
1071 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1071 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1072 if datatype == 0:
1072 if datatype == 0:
1073 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1073 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1074 elif datatype == 1:
1074 elif datatype == 1:
1075 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1075 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1076 elif datatype == 2:
1076 elif datatype == 2:
1077 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1077 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1078 elif datatype == 3:
1078 elif datatype == 3:
1079 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1079 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1080 elif datatype == 4:
1080 elif datatype == 4:
1081 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1081 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1082 elif datatype == 5:
1082 elif datatype == 5:
1083 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1083 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1084 else:
1084 else:
1085 raise ValueError, 'Data type was not defined'
1085 raise ValueError, 'Data type was not defined'
1086
1086
1087 self.dtype = datatype_str
1087 self.dtype = datatype_str
1088 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1088 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1089 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1089 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1090 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1090 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1091 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1091 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1092 self.getBlockDimension()
1092 self.getBlockDimension()
1093
1093
1094 def __verifyFile(self, filename, msgFlag=True):
1094 def __verifyFile(self, filename, msgFlag=True):
1095
1095
1096 msg = None
1096 msg = None
1097
1097
1098 try:
1098 try:
1099 fp = open(filename, 'rb')
1099 fp = open(filename, 'rb')
1100 except IOError:
1100 except IOError:
1101
1101
1102 if msgFlag:
1102 if msgFlag:
1103 print "[Reading] File %s can't be opened" % (filename)
1103 print "[Reading] File %s can't be opened" % (filename)
1104
1104
1105 return False
1105 return False
1106
1106
1107 currentPosition = fp.tell()
1107 currentPosition = fp.tell()
1108 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1108 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1109
1109
1110 if neededSize == 0:
1110 if neededSize == 0:
1111 basicHeaderObj = BasicHeader(LOCALTIME)
1111 basicHeaderObj = BasicHeader(LOCALTIME)
1112 systemHeaderObj = SystemHeader()
1112 systemHeaderObj = SystemHeader()
1113 radarControllerHeaderObj = RadarControllerHeader()
1113 radarControllerHeaderObj = RadarControllerHeader()
1114 processingHeaderObj = ProcessingHeader()
1114 processingHeaderObj = ProcessingHeader()
1115
1115
1116 if not( basicHeaderObj.read(fp) ):
1116 if not( basicHeaderObj.read(fp) ):
1117 fp.close()
1117 fp.close()
1118 return False
1118 return False
1119
1119
1120 if not( systemHeaderObj.read(fp) ):
1120 if not( systemHeaderObj.read(fp) ):
1121 fp.close()
1121 fp.close()
1122 return False
1122 return False
1123
1123
1124 if not( radarControllerHeaderObj.read(fp) ):
1124 if not( radarControllerHeaderObj.read(fp) ):
1125 fp.close()
1125 fp.close()
1126 return False
1126 return False
1127
1127
1128 if not( processingHeaderObj.read(fp) ):
1128 if not( processingHeaderObj.read(fp) ):
1129 fp.close()
1129 fp.close()
1130 return False
1130 return False
1131
1131
1132 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1132 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1133 else:
1133 else:
1134 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1134 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1135
1135
1136 fp.close()
1136 fp.close()
1137
1137
1138 fileSize = os.path.getsize(filename)
1138 fileSize = os.path.getsize(filename)
1139 currentSize = fileSize - currentPosition
1139 currentSize = fileSize - currentPosition
1140
1140
1141 if currentSize < neededSize:
1141 if currentSize < neededSize:
1142 if msgFlag and (msg != None):
1142 if msgFlag and (msg != None):
1143 print msg
1143 print msg
1144 return False
1144 return False
1145
1145
1146 return True
1146 return True
1147
1147
1148 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1148 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1149
1149
1150 path_empty = True
1150 path_empty = True
1151
1151
1152 dateList = []
1152 dateList = []
1153 pathList = []
1153 pathList = []
1154
1154
1155 multi_path = path.split(',')
1155 multi_path = path.split(',')
1156
1156
1157 if not walk:
1157 if not walk:
1158
1158
1159 for single_path in multi_path:
1159 for single_path in multi_path:
1160
1160
1161 if not os.path.isdir(single_path):
1161 if not os.path.isdir(single_path):
1162 continue
1162 continue
1163
1163
1164 fileList = glob.glob1(single_path, "*"+ext)
1164 fileList = glob.glob1(single_path, "*"+ext)
1165
1165
1166 if not fileList:
1166 if not fileList:
1167 continue
1167 continue
1168
1168
1169 path_empty = False
1169 path_empty = False
1170
1170
1171 fileList.sort()
1171 fileList.sort()
1172
1172
1173 for thisFile in fileList:
1173 for thisFile in fileList:
1174
1174
1175 if not os.path.isfile(os.path.join(single_path, thisFile)):
1175 if not os.path.isfile(os.path.join(single_path, thisFile)):
1176 continue
1176 continue
1177
1177
1178 if not isRadarFile(thisFile):
1178 if not isRadarFile(thisFile):
1179 continue
1179 continue
1180
1180
1181 if not isFileInDateRange(thisFile, startDate, endDate):
1181 if not isFileInDateRange(thisFile, startDate, endDate):
1182 continue
1182 continue
1183
1183
1184 thisDate = getDateFromRadarFile(thisFile)
1184 thisDate = getDateFromRadarFile(thisFile)
1185
1185
1186 if thisDate in dateList:
1186 if thisDate in dateList:
1187 continue
1187 continue
1188
1188
1189 dateList.append(thisDate)
1189 dateList.append(thisDate)
1190 pathList.append(single_path)
1190 pathList.append(single_path)
1191
1191
1192 else:
1192 else:
1193 for single_path in multi_path:
1193 for single_path in multi_path:
1194
1194
1195 if not os.path.isdir(single_path):
1195 if not os.path.isdir(single_path):
1196 continue
1196 continue
1197
1197
1198 dirList = []
1198 dirList = []
1199
1199
1200 for thisPath in os.listdir(single_path):
1200 for thisPath in os.listdir(single_path):
1201
1201
1202 if not os.path.isdir(os.path.join(single_path,thisPath)):
1202 if not os.path.isdir(os.path.join(single_path,thisPath)):
1203 continue
1203 continue
1204
1204
1205 if not isRadarFolder(thisPath):
1205 if not isRadarFolder(thisPath):
1206 continue
1206 continue
1207
1207
1208 if not isFolderInDateRange(thisPath, startDate, endDate):
1208 if not isFolderInDateRange(thisPath, startDate, endDate):
1209 continue
1209 continue
1210
1210
1211 dirList.append(thisPath)
1211 dirList.append(thisPath)
1212
1212
1213 if not dirList:
1213 if not dirList:
1214 continue
1214 continue
1215
1215
1216 dirList.sort()
1216 dirList.sort()
1217
1217
1218 for thisDir in dirList:
1218 for thisDir in dirList:
1219
1219
1220 datapath = os.path.join(single_path, thisDir, expLabel)
1220 datapath = os.path.join(single_path, thisDir, expLabel)
1221 fileList = glob.glob1(datapath, "*"+ext)
1221 fileList = glob.glob1(datapath, "*"+ext)
1222
1222
1223 if not fileList:
1223 if not fileList:
1224 continue
1224 continue
1225
1225
1226 path_empty = False
1226 path_empty = False
1227
1227
1228 thisDate = getDateFromRadarFolder(thisDir)
1228 thisDate = getDateFromRadarFolder(thisDir)
1229
1229
1230 pathList.append(datapath)
1230 pathList.append(datapath)
1231 dateList.append(thisDate)
1231 dateList.append(thisDate)
1232
1232
1233 dateList.sort()
1233 dateList.sort()
1234
1234
1235 if walk:
1235 if walk:
1236 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1236 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1237 else:
1237 else:
1238 pattern_path = multi_path[0]
1238 pattern_path = multi_path[0]
1239
1239
1240 if path_empty:
1240 if path_empty:
1241 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1241 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1242 else:
1242 else:
1243 if not dateList:
1243 if not dateList:
1244 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1244 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1245
1245
1246 if include_path:
1246 if include_path:
1247 return dateList, pathList
1247 return dateList, pathList
1248
1248
1249 return dateList
1249 return dateList
1250
1250
1251 def setup(self,
1251 def setup(self,
1252 path=None,
1252 path=None,
1253 startDate=None,
1253 startDate=None,
1254 endDate=None,
1254 endDate=None,
1255 startTime=datetime.time(0,0,0),
1255 startTime=datetime.time(0,0,0),
1256 endTime=datetime.time(23,59,59),
1256 endTime=datetime.time(23,59,59),
1257 set=None,
1257 set=None,
1258 expLabel = "",
1258 expLabel = "",
1259 ext = None,
1259 ext = None,
1260 online = False,
1260 online = False,
1261 delay = 60,
1261 delay = 60,
1262 walk = True,
1262 walk = True,
1263 getblock = False,
1263 getblock = False,
1264 nTxs = 1,
1264 nTxs = 1,
1265 realtime=False,
1265 realtime=False,
1266 blocksize=None,
1266 blocksize=None,
1267 blocktime=None,
1267 blocktime=None,
1268 skip=None,
1268 skip=None,
1269 cursor=None,
1269 cursor=None,
1270 warnings=True,
1270 warnings=True,
1271 verbose=True,
1271 verbose=True,
1272 server=None,
1272 server=None,
1273 **kwargs):
1273 **kwargs):
1274 if server is not None:
1274 if server is not None:
1275 if 'tcp://' in server:
1275 if 'tcp://' in server:
1276 address = server
1276 address = server
1277 else:
1277 else:
1278 address = 'ipc:///tmp/%s' % server
1278 address = 'ipc:///tmp/%s' % server
1279 self.server = address
1279 self.server = address
1280 self.context = zmq.Context()
1280 self.context = zmq.Context()
1281 self.receiver = self.context.socket(zmq.PULL)
1281 self.receiver = self.context.socket(zmq.PULL)
1282 self.receiver.connect(self.server)
1282 self.receiver.connect(self.server)
1283 time.sleep(0.5)
1283 time.sleep(0.5)
1284 print '[Starting] ReceiverData from {}'.format(self.server)
1284 print '[Starting] ReceiverData from {}'.format(self.server)
1285 else:
1285 else:
1286 self.server = None
1286 self.server = None
1287 if path == None:
1287 if path == None:
1288 raise ValueError, "[Reading] The path is not valid"
1288 raise ValueError, "[Reading] The path is not valid"
1289
1289
1290 if ext == None:
1290 if ext == None:
1291 ext = self.ext
1291 ext = self.ext
1292
1292
1293 if online:
1293 if online:
1294 print "[Reading] Searching files in online mode..."
1294 print "[Reading] Searching files in online mode..."
1295
1295
1296 for nTries in range( self.nTries ):
1296 for nTries in range( self.nTries ):
1297 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1297 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1298
1298
1299 if fullpath:
1299 if fullpath:
1300 break
1300 break
1301
1301
1302 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1302 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1303 sleep( self.delay )
1303 sleep( self.delay )
1304
1304
1305 if not(fullpath):
1305 if not(fullpath):
1306 print "[Reading] There 'isn't any valid file in %s" % path
1306 print "[Reading] There 'isn't any valid file in %s" % path
1307 return
1307 return
1308
1308
1309 self.year = year
1309 self.year = year
1310 self.doy = doy
1310 self.doy = doy
1311 self.set = set - 1
1311 self.set = set - 1
1312 self.path = path
1312 self.path = path
1313 self.foldercounter = foldercounter
1313 self.foldercounter = foldercounter
1314 last_set = None
1314 last_set = None
1315 else:
1315 else:
1316 print "[Reading] Searching files in offline mode ..."
1316 print "[Reading] Searching files in offline mode ..."
1317 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1317 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1318 startTime=startTime, endTime=endTime,
1318 startTime=startTime, endTime=endTime,
1319 set=set, expLabel=expLabel, ext=ext,
1319 set=set, expLabel=expLabel, ext=ext,
1320 walk=walk, cursor=cursor,
1320 walk=walk, cursor=cursor,
1321 skip=skip)
1321 skip=skip)
1322
1322
1323 if not(pathList):
1323 if not(pathList):
1324 self.fileIndex = -1
1324 self.fileIndex = -1
1325 self.pathList = []
1325 self.pathList = []
1326 self.filenameList = []
1326 self.filenameList = []
1327 return
1327 return
1328
1328
1329 self.fileIndex = -1
1329 self.fileIndex = -1
1330 self.pathList = pathList
1330 self.pathList = pathList
1331 self.filenameList = filenameList
1331 self.filenameList = filenameList
1332 file_name = os.path.basename(filenameList[-1])
1332 file_name = os.path.basename(filenameList[-1])
1333 basename, ext = os.path.splitext(file_name)
1333 basename, ext = os.path.splitext(file_name)
1334 last_set = int(basename[-3:])
1334 last_set = int(basename[-3:])
1335
1335
1336 self.online = online
1336 self.online = online
1337 self.realtime = realtime
1337 self.realtime = realtime
1338 self.delay = delay
1338 self.delay = delay
1339 ext = ext.lower()
1339 ext = ext.lower()
1340 self.ext = ext
1340 self.ext = ext
1341 self.getByBlock = getblock
1341 self.getByBlock = getblock
1342 self.nTxs = nTxs
1342 self.nTxs = nTxs
1343 self.startTime = startTime
1343 self.startTime = startTime
1344 self.endTime = endTime
1344 self.endTime = endTime
1345
1345
1346 #Added-----------------
1346 #Added-----------------
1347 self.selBlocksize = blocksize
1347 self.selBlocksize = blocksize
1348 self.selBlocktime = blocktime
1348 self.selBlocktime = blocktime
1349
1349
1350 # Verbose-----------
1350 # Verbose-----------
1351 self.verbose = verbose
1351 self.verbose = verbose
1352 self.warnings = warnings
1352 self.warnings = warnings
1353
1353
1354 if not(self.setNextFile()):
1354 if not(self.setNextFile()):
1355 if (startDate!=None) and (endDate!=None):
1355 if (startDate!=None) and (endDate!=None):
1356 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1356 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1357 elif startDate != None:
1357 elif startDate != None:
1358 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1358 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1359 else:
1359 else:
1360 print "[Reading] No files"
1360 print "[Reading] No files"
1361
1361
1362 self.fileIndex = -1
1362 self.fileIndex = -1
1363 self.pathList = []
1363 self.pathList = []
1364 self.filenameList = []
1364 self.filenameList = []
1365 return
1365 return
1366
1366
1367 # self.getBasicHeader()
1367 # self.getBasicHeader()
1368
1368
1369 if last_set != None:
1369 if last_set != None:
1370 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1370 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1371 return
1371 return
1372
1372
1373 def getBasicHeader(self):
1373 def getBasicHeader(self):
1374
1374
1375 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1375 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1376
1376
1377 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1377 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1378
1378
1379 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1379 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1380
1380
1381 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1381 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1382
1382
1383 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1383 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1384
1384
1385 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1385 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1386
1386
1387 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1387 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1388
1388
1389 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1389 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1390
1390
1391
1391
1392 def getFirstHeader(self):
1392 def getFirstHeader(self):
1393
1393
1394 raise NotImplementedError
1394 raise NotImplementedError
1395
1395
1396 def getData(self):
1396 def getData(self):
1397
1397
1398 raise NotImplementedError
1398 raise NotImplementedError
1399
1399
1400 def hasNotDataInBuffer(self):
1400 def hasNotDataInBuffer(self):
1401
1401
1402 raise NotImplementedError
1402 raise NotImplementedError
1403
1403
1404 def readBlock(self):
1404 def readBlock(self):
1405
1405
1406 raise NotImplementedError
1406 raise NotImplementedError
1407
1407
1408 def isEndProcess(self):
1408 def isEndProcess(self):
1409
1409
1410 return self.flagNoMoreFiles
1410 return self.flagNoMoreFiles
1411
1411
1412 def printReadBlocks(self):
1412 def printReadBlocks(self):
1413
1413
1414 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1414 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1415
1415
1416 def printTotalBlocks(self):
1416 def printTotalBlocks(self):
1417
1417
1418 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1418 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1419
1419
1420 def printNumberOfBlock(self):
1420 def printNumberOfBlock(self):
1421
1421
1422 if self.flagIsNewBlock:
1422 if self.flagIsNewBlock:
1423 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1423 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1424 self.processingHeaderObj.dataBlocksPerFile,
1424 self.processingHeaderObj.dataBlocksPerFile,
1425 self.dataOut.datatime.ctime())
1425 self.dataOut.datatime.ctime())
1426
1426
1427 def printInfo(self):
1427 def printInfo(self):
1428
1428
1429 if self.__printInfo == False:
1429 if self.__printInfo == False:
1430 return
1430 return
1431
1431
1432 self.basicHeaderObj.printInfo()
1432 self.basicHeaderObj.printInfo()
1433 self.systemHeaderObj.printInfo()
1433 self.systemHeaderObj.printInfo()
1434 self.radarControllerHeaderObj.printInfo()
1434 self.radarControllerHeaderObj.printInfo()
1435 self.processingHeaderObj.printInfo()
1435 self.processingHeaderObj.printInfo()
1436
1436
1437 self.__printInfo = False
1437 self.__printInfo = False
1438
1438
1439 def run(self,
1439 def run(self,
1440 path=None,
1440 path=None,
1441 startDate=None,
1441 startDate=None,
1442 endDate=None,
1442 endDate=None,
1443 startTime=datetime.time(0,0,0),
1443 startTime=datetime.time(0,0,0),
1444 endTime=datetime.time(23,59,59),
1444 endTime=datetime.time(23,59,59),
1445 set=None,
1445 set=None,
1446 expLabel = "",
1446 expLabel = "",
1447 ext = None,
1447 ext = None,
1448 online = False,
1448 online = False,
1449 delay = 60,
1449 delay = 60,
1450 walk = True,
1450 walk = True,
1451 getblock = False,
1451 getblock = False,
1452 nTxs = 1,
1452 nTxs = 1,
1453 realtime=False,
1453 realtime=False,
1454 blocksize=None,
1454 blocksize=None,
1455 blocktime=None,
1455 blocktime=None,
1456 queue=None,
1456 queue=None,
1457 skip=None,
1457 skip=None,
1458 cursor=None,
1458 cursor=None,
1459 warnings=True,
1459 warnings=True,
1460 server=None,
1460 server=None,
1461 verbose=True, **kwargs):
1461 verbose=True, **kwargs):
1462 if not(self.isConfig):
1462 if not(self.isConfig):
1463 self.setup(path=path,
1463 self.setup(path=path,
1464 startDate=startDate,
1464 startDate=startDate,
1465 endDate=endDate,
1465 endDate=endDate,
1466 startTime=startTime,
1466 startTime=startTime,
1467 endTime=endTime,
1467 endTime=endTime,
1468 set=set,
1468 set=set,
1469 expLabel=expLabel,
1469 expLabel=expLabel,
1470 ext=ext,
1470 ext=ext,
1471 online=online,
1471 online=online,
1472 delay=delay,
1472 delay=delay,
1473 walk=walk,
1473 walk=walk,
1474 getblock=getblock,
1474 getblock=getblock,
1475 nTxs=nTxs,
1475 nTxs=nTxs,
1476 realtime=realtime,
1476 realtime=realtime,
1477 blocksize=blocksize,
1477 blocksize=blocksize,
1478 blocktime=blocktime,
1478 blocktime=blocktime,
1479 skip=skip,
1479 skip=skip,
1480 cursor=cursor,
1480 cursor=cursor,
1481 warnings=warnings,
1481 warnings=warnings,
1482 server=server,
1482 server=server,
1483 verbose=verbose)
1483 verbose=verbose)
1484 self.isConfig = True
1484 self.isConfig = True
1485 if server is None:
1485 if server is None:
1486 self.getData()
1486 self.getData()
1487 else:
1487 else:
1488 self.getFromServer()
1488 self.getFromServer()
1489
1489
1490 class JRODataWriter(JRODataIO):
1490 class JRODataWriter(JRODataIO):
1491
1491
1492 """
1492 """
1493 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1493 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1494 de los datos siempre se realiza por bloques.
1494 de los datos siempre se realiza por bloques.
1495 """
1495 """
1496
1496
1497 blockIndex = 0
1497 blockIndex = 0
1498
1498
1499 path = None
1499 path = None
1500
1500
1501 setFile = None
1501 setFile = None
1502
1502
1503 profilesPerBlock = None
1503 profilesPerBlock = None
1504
1504
1505 blocksPerFile = None
1505 blocksPerFile = None
1506
1506
1507 nWriteBlocks = 0
1507 nWriteBlocks = 0
1508
1508
1509 fileDate = None
1509 fileDate = None
1510
1510
1511 def __init__(self, dataOut=None):
1511 def __init__(self, dataOut=None):
1512 raise NotImplementedError
1512 raise NotImplementedError
1513
1513
1514
1514
1515 def hasAllDataInBuffer(self):
1515 def hasAllDataInBuffer(self):
1516 raise NotImplementedError
1516 raise NotImplementedError
1517
1517
1518
1518
1519 def setBlockDimension(self):
1519 def setBlockDimension(self):
1520 raise NotImplementedError
1520 raise NotImplementedError
1521
1521
1522
1522
1523 def writeBlock(self):
1523 def writeBlock(self):
1524 raise NotImplementedError
1524 raise NotImplementedError
1525
1525
1526
1526
1527 def putData(self):
1527 def putData(self):
1528 raise NotImplementedError
1528 raise NotImplementedError
1529
1529
1530
1530
1531 def getProcessFlags(self):
1531 def getProcessFlags(self):
1532
1532
1533 processFlags = 0
1533 processFlags = 0
1534
1534
1535 dtype_index = get_dtype_index(self.dtype)
1535 dtype_index = get_dtype_index(self.dtype)
1536 procflag_dtype = get_procflag_dtype(dtype_index)
1536 procflag_dtype = get_procflag_dtype(dtype_index)
1537
1537
1538 processFlags += procflag_dtype
1538 processFlags += procflag_dtype
1539
1539
1540 if self.dataOut.flagDecodeData:
1540 if self.dataOut.flagDecodeData:
1541 processFlags += PROCFLAG.DECODE_DATA
1541 processFlags += PROCFLAG.DECODE_DATA
1542
1542
1543 if self.dataOut.flagDeflipData:
1543 if self.dataOut.flagDeflipData:
1544 processFlags += PROCFLAG.DEFLIP_DATA
1544 processFlags += PROCFLAG.DEFLIP_DATA
1545
1545
1546 if self.dataOut.code is not None:
1546 if self.dataOut.code is not None:
1547 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1547 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1548
1548
1549 if self.dataOut.nCohInt > 1:
1549 if self.dataOut.nCohInt > 1:
1550 processFlags += PROCFLAG.COHERENT_INTEGRATION
1550 processFlags += PROCFLAG.COHERENT_INTEGRATION
1551
1551
1552 if self.dataOut.type == "Spectra":
1552 if self.dataOut.type == "Spectra":
1553 if self.dataOut.nIncohInt > 1:
1553 if self.dataOut.nIncohInt > 1:
1554 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1554 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1555
1555
1556 if self.dataOut.data_dc is not None:
1556 if self.dataOut.data_dc is not None:
1557 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1557 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1558
1558
1559 if self.dataOut.flagShiftFFT:
1559 if self.dataOut.flagShiftFFT:
1560 processFlags += PROCFLAG.SHIFT_FFT_DATA
1560 processFlags += PROCFLAG.SHIFT_FFT_DATA
1561
1561
1562 return processFlags
1562 return processFlags
1563
1563
1564 def setBasicHeader(self):
1564 def setBasicHeader(self):
1565
1565
1566 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1566 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1567 self.basicHeaderObj.version = self.versionFile
1567 self.basicHeaderObj.version = self.versionFile
1568 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1568 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1569
1569
1570 utc = numpy.floor(self.dataOut.utctime)
1570 utc = numpy.floor(self.dataOut.utctime)
1571 milisecond = (self.dataOut.utctime - utc)* 1000.0
1571 milisecond = (self.dataOut.utctime - utc)* 1000.0
1572
1572
1573 self.basicHeaderObj.utc = utc
1573 self.basicHeaderObj.utc = utc
1574 self.basicHeaderObj.miliSecond = milisecond
1574 self.basicHeaderObj.miliSecond = milisecond
1575 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1575 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1576 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1576 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1577 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1577 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1578
1578
1579 def setFirstHeader(self):
1579 def setFirstHeader(self):
1580 """
1580 """
1581 Obtiene una copia del First Header
1581 Obtiene una copia del First Header
1582
1582
1583 Affected:
1583 Affected:
1584
1584
1585 self.basicHeaderObj
1585 self.basicHeaderObj
1586 self.systemHeaderObj
1586 self.systemHeaderObj
1587 self.radarControllerHeaderObj
1587 self.radarControllerHeaderObj
1588 self.processingHeaderObj self.
1588 self.processingHeaderObj self.
1589
1589
1590 Return:
1590 Return:
1591 None
1591 None
1592 """
1592 """
1593
1593
1594 raise NotImplementedError
1594 raise NotImplementedError
1595
1595
1596 def __writeFirstHeader(self):
1596 def __writeFirstHeader(self):
1597 """
1597 """
1598 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1598 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1599
1599
1600 Affected:
1600 Affected:
1601 __dataType
1601 __dataType
1602
1602
1603 Return:
1603 Return:
1604 None
1604 None
1605 """
1605 """
1606
1606
1607 # CALCULAR PARAMETROS
1607 # CALCULAR PARAMETROS
1608
1608
1609 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1609 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1610 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1610 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1611
1611
1612 self.basicHeaderObj.write(self.fp)
1612 self.basicHeaderObj.write(self.fp)
1613 self.systemHeaderObj.write(self.fp)
1613 self.systemHeaderObj.write(self.fp)
1614 self.radarControllerHeaderObj.write(self.fp)
1614 self.radarControllerHeaderObj.write(self.fp)
1615 self.processingHeaderObj.write(self.fp)
1615 self.processingHeaderObj.write(self.fp)
1616
1616
1617 def __setNewBlock(self):
1617 def __setNewBlock(self):
1618 """
1618 """
1619 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1619 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1620
1620
1621 Return:
1621 Return:
1622 0 : si no pudo escribir nada
1622 0 : si no pudo escribir nada
1623 1 : Si escribio el Basic el First Header
1623 1 : Si escribio el Basic el First Header
1624 """
1624 """
1625 if self.fp == None:
1625 if self.fp == None:
1626 self.setNextFile()
1626 self.setNextFile()
1627
1627
1628 if self.flagIsNewFile:
1628 if self.flagIsNewFile:
1629 return 1
1629 return 1
1630
1630
1631 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1631 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1632 self.basicHeaderObj.write(self.fp)
1632 self.basicHeaderObj.write(self.fp)
1633 return 1
1633 return 1
1634
1634
1635 if not( self.setNextFile() ):
1635 if not( self.setNextFile() ):
1636 return 0
1636 return 0
1637
1637
1638 return 1
1638 return 1
1639
1639
1640
1640
1641 def writeNextBlock(self):
1641 def writeNextBlock(self):
1642 """
1642 """
1643 Selecciona el bloque siguiente de datos y los escribe en un file
1643 Selecciona el bloque siguiente de datos y los escribe en un file
1644
1644
1645 Return:
1645 Return:
1646 0 : Si no hizo pudo escribir el bloque de datos
1646 0 : Si no hizo pudo escribir el bloque de datos
1647 1 : Si no pudo escribir el bloque de datos
1647 1 : Si no pudo escribir el bloque de datos
1648 """
1648 """
1649 if not( self.__setNewBlock() ):
1649 if not( self.__setNewBlock() ):
1650 return 0
1650 return 0
1651
1651
1652 self.writeBlock()
1652 self.writeBlock()
1653
1653
1654 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1654 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1655 self.processingHeaderObj.dataBlocksPerFile)
1655 self.processingHeaderObj.dataBlocksPerFile)
1656
1656
1657 return 1
1657 return 1
1658
1658
1659 def setNextFile(self):
1659 def setNextFile(self):
1660 """
1660 """
1661 Determina el siguiente file que sera escrito
1661 Determina el siguiente file que sera escrito
1662
1662
1663 Affected:
1663 Affected:
1664 self.filename
1664 self.filename
1665 self.subfolder
1665 self.subfolder
1666 self.fp
1666 self.fp
1667 self.setFile
1667 self.setFile
1668 self.flagIsNewFile
1668 self.flagIsNewFile
1669
1669
1670 Return:
1670 Return:
1671 0 : Si el archivo no puede ser escrito
1671 0 : Si el archivo no puede ser escrito
1672 1 : Si el archivo esta listo para ser escrito
1672 1 : Si el archivo esta listo para ser escrito
1673 """
1673 """
1674 ext = self.ext
1674 ext = self.ext
1675 path = self.path
1675 path = self.path
1676
1676
1677 if self.fp != None:
1677 if self.fp != None:
1678 self.fp.close()
1678 self.fp.close()
1679
1679
1680 timeTuple = time.localtime( self.dataOut.utctime)
1680 timeTuple = time.localtime( self.dataOut.utctime)
1681 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1681 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1682
1682
1683 fullpath = os.path.join( path, subfolder )
1683 fullpath = os.path.join( path, subfolder )
1684 setFile = self.setFile
1684 setFile = self.setFile
1685
1685
1686 if not( os.path.exists(fullpath) ):
1686 if not( os.path.exists(fullpath) ):
1687 os.mkdir(fullpath)
1687 os.mkdir(fullpath)
1688 setFile = -1 #inicializo mi contador de seteo
1688 setFile = -1 #inicializo mi contador de seteo
1689 else:
1689 else:
1690 filesList = os.listdir( fullpath )
1690 filesList = os.listdir( fullpath )
1691 if len( filesList ) > 0:
1691 if len( filesList ) > 0:
1692 filesList = sorted( filesList, key=str.lower )
1692 filesList = sorted( filesList, key=str.lower )
1693 filen = filesList[-1]
1693 filen = filesList[-1]
1694 # el filename debera tener el siguiente formato
1694 # el filename debera tener el siguiente formato
1695 # 0 1234 567 89A BCDE (hex)
1695 # 0 1234 567 89A BCDE (hex)
1696 # x YYYY DDD SSS .ext
1696 # x YYYY DDD SSS .ext
1697 if isNumber( filen[8:11] ):
1697 if isNumber( filen[8:11] ):
1698 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1698 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1699 else:
1699 else:
1700 setFile = -1
1700 setFile = -1
1701 else:
1701 else:
1702 setFile = -1 #inicializo mi contador de seteo
1702 setFile = -1 #inicializo mi contador de seteo
1703
1703
1704 setFile += 1
1704 setFile += 1
1705
1705
1706 #If this is a new day it resets some values
1706 #If this is a new day it resets some values
1707 if self.dataOut.datatime.date() > self.fileDate:
1707 if self.dataOut.datatime.date() > self.fileDate:
1708 setFile = 0
1708 setFile = 0
1709 self.nTotalBlocks = 0
1709 self.nTotalBlocks = 0
1710
1710
1711 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1711 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1712
1712
1713 filename = os.path.join( path, subfolder, filen )
1713 filename = os.path.join( path, subfolder, filen )
1714
1714
1715 fp = open( filename,'wb' )
1715 fp = open( filename,'wb' )
1716
1716
1717 self.blockIndex = 0
1717 self.blockIndex = 0
1718
1718
1719 #guardando atributos
1719 #guardando atributos
1720 self.filename = filename
1720 self.filename = filename
1721 self.subfolder = subfolder
1721 self.subfolder = subfolder
1722 self.fp = fp
1722 self.fp = fp
1723 self.setFile = setFile
1723 self.setFile = setFile
1724 self.flagIsNewFile = 1
1724 self.flagIsNewFile = 1
1725 self.fileDate = self.dataOut.datatime.date()
1725 self.fileDate = self.dataOut.datatime.date()
1726
1726
1727 self.setFirstHeader()
1727 self.setFirstHeader()
1728
1728
1729 print '[Writing] Opening file: %s'%self.filename
1729 print '[Writing] Opening file: %s'%self.filename
1730
1730
1731 self.__writeFirstHeader()
1731 self.__writeFirstHeader()
1732
1732
1733 return 1
1733 return 1
1734
1734
1735 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1735 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1736 """
1736 """
1737 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1737 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1738
1738
1739 Inputs:
1739 Inputs:
1740 path : directory where data will be saved
1740 path : directory where data will be saved
1741 profilesPerBlock : number of profiles per block
1741 profilesPerBlock : number of profiles per block
1742 set : initial file set
1742 set : initial file set
1743 datatype : An integer number that defines data type:
1743 datatype : An integer number that defines data type:
1744 0 : int8 (1 byte)
1744 0 : int8 (1 byte)
1745 1 : int16 (2 bytes)
1745 1 : int16 (2 bytes)
1746 2 : int32 (4 bytes)
1746 2 : int32 (4 bytes)
1747 3 : int64 (8 bytes)
1747 3 : int64 (8 bytes)
1748 4 : float32 (4 bytes)
1748 4 : float32 (4 bytes)
1749 5 : double64 (8 bytes)
1749 5 : double64 (8 bytes)
1750
1750
1751 Return:
1751 Return:
1752 0 : Si no realizo un buen seteo
1752 0 : Si no realizo un buen seteo
1753 1 : Si realizo un buen seteo
1753 1 : Si realizo un buen seteo
1754 """
1754 """
1755
1755
1756 if ext == None:
1756 if ext == None:
1757 ext = self.ext
1757 ext = self.ext
1758
1758
1759 self.ext = ext.lower()
1759 self.ext = ext.lower()
1760
1760
1761 self.path = path
1761 self.path = path
1762
1762
1763 if set is None:
1763 if set is None:
1764 self.setFile = -1
1764 self.setFile = -1
1765 else:
1765 else:
1766 self.setFile = set - 1
1766 self.setFile = set - 1
1767
1767
1768 self.blocksPerFile = blocksPerFile
1768 self.blocksPerFile = blocksPerFile
1769
1769
1770 self.profilesPerBlock = profilesPerBlock
1770 self.profilesPerBlock = profilesPerBlock
1771
1771
1772 self.dataOut = dataOut
1772 self.dataOut = dataOut
1773 self.fileDate = self.dataOut.datatime.date()
1773 self.fileDate = self.dataOut.datatime.date()
1774 #By default
1774 #By default
1775 self.dtype = self.dataOut.dtype
1775 self.dtype = self.dataOut.dtype
1776
1776
1777 if datatype is not None:
1777 if datatype is not None:
1778 self.dtype = get_numpy_dtype(datatype)
1778 self.dtype = get_numpy_dtype(datatype)
1779
1779
1780 if not(self.setNextFile()):
1780 if not(self.setNextFile()):
1781 print "[Writing] There isn't a next file"
1781 print "[Writing] There isn't a next file"
1782 return 0
1782 return 0
1783
1783
1784 self.setBlockDimension()
1784 self.setBlockDimension()
1785
1785
1786 return 1
1786 return 1
1787
1787
1788 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1788 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1789
1789
1790 if not(self.isConfig):
1790 if not(self.isConfig):
1791
1791
1792 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1792 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1793 self.isConfig = True
1793 self.isConfig = True
1794
1794
1795 self.putData()
1795 self.putData()
@@ -1,751 +1,751
1
1
2 '''
2 '''
3 Created on Jul 3, 2014
3 Created on Jul 3, 2014
4
4
5 @author: roj-idl71
5 @author: roj-idl71
6 '''
6 '''
7 # SUBCHANNELS EN VEZ DE CHANNELS
7 # SUBCHANNELS EN VEZ DE CHANNELS
8 # BENCHMARKS -> PROBLEMAS CON ARCHIVOS GRANDES -> INCONSTANTE EN EL TIEMPO
8 # BENCHMARKS -> PROBLEMAS CON ARCHIVOS GRANDES -> INCONSTANTE EN EL TIEMPO
9 # ACTUALIZACION DE VERSION
9 # ACTUALIZACION DE VERSION
10 # HEADERS
10 # HEADERS
11 # MODULO DE ESCRITURA
11 # MODULO DE ESCRITURA
12 # METADATA
12 # METADATA
13
13
14 import os
14 import os
15 import datetime
15 import datetime
16 import numpy
16 import numpy
17 import timeit
17 import timeit
18 from profilehooks import coverage, profile
18 from profilehooks import coverage, profile
19 from fractions import Fraction
19 from fractions import Fraction
20
20
21 try:
21 try:
22 from gevent import sleep
22 from gevent import sleep
23 except:
23 except:
24 from time import sleep
24 from time import sleep
25
25
26 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
26 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
27 from schainpy.model.data.jrodata import Voltage
27 from schainpy.model.data.jrodata import Voltage
28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
29 from time import time
29 from time import time
30
30
31 import cPickle
31 import cPickle
32 try:
32 try:
33 import digital_rf
33 import digital_rf
34 except:
34 except:
35 print 'You should install "digital_rf" module if you want to read Digital RF data'
35 print 'You should install "digital_rf" module if you want to read Digital RF data'
36
36
37 class DigitalRFReader(ProcessingUnit):
37 class DigitalRFReader(ProcessingUnit):
38 '''
38 '''
39 classdocs
39 classdocs
40 '''
40 '''
41
41
42 def __init__(self, **kwargs):
42 def __init__(self, **kwargs):
43 '''
43 '''
44 Constructor
44 Constructor
45 '''
45 '''
46
46
47 ProcessingUnit.__init__(self, **kwargs)
47 ProcessingUnit.__init__(self, **kwargs)
48
48
49 self.dataOut = Voltage()
49 self.dataOut = Voltage()
50 self.__printInfo = True
50 self.__printInfo = True
51 self.__flagDiscontinuousBlock = False
51 self.__flagDiscontinuousBlock = False
52 self.__bufferIndex = 9999999
52 self.__bufferIndex = 9999999
53 self.__ippKm = None
53 self.__ippKm = None
54 self.__codeType = 0
54 self.__codeType = 0
55 self.__nCode = None
55 self.__nCode = None
56 self.__nBaud = None
56 self.__nBaud = None
57 self.__code = None
57 self.__code = None
58 self.dtype = None
58 self.dtype = None
59
59
60 def close(self):
60 def close(self):
61 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
61 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
62 return
62 return
63
63
64 def __getCurrentSecond(self):
64 def __getCurrentSecond(self):
65
65
66 return self.__thisUnixSample/self.__sample_rate
66 return self.__thisUnixSample/self.__sample_rate
67
67
68 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
68 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
69
69
70 def __setFileHeader(self):
70 def __setFileHeader(self):
71 '''
71 '''
72 In this method will be initialized every parameter of dataOut object (header, no data)
72 In this method will be initialized every parameter of dataOut object (header, no data)
73 '''
73 '''
74 ippSeconds = 1.0*self.__nSamples/self.__sample_rate
74 ippSeconds = 1.0*self.__nSamples/self.__sample_rate
75
75
76 nProfiles = 1.0/ippSeconds # Number of profiles in one second
76 nProfiles = 1.0/ippSeconds # Number of profiles in one second
77
77
78 try:
78 try:
79 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(self.__radarControllerHeader)
79 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(self.__radarControllerHeader)
80 except:
80 except:
81 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
81 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
82 txA=0,
82 txA=0,
83 txB=0,
83 txB=0,
84 nWindows=1,
84 nWindows=1,
85 nHeights=self.__nSamples,
85 nHeights=self.__nSamples,
86 firstHeight=self.__firstHeigth,
86 firstHeight=self.__firstHeigth,
87 deltaHeight=self.__deltaHeigth,
87 deltaHeight=self.__deltaHeigth,
88 codeType=self.__codeType,
88 codeType=self.__codeType,
89 nCode=self.__nCode, nBaud=self.__nBaud,
89 nCode=self.__nCode, nBaud=self.__nBaud,
90 code = self.__code)
90 code = self.__code)
91
91
92 try:
92 try:
93 self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader)
93 self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader)
94 except:
94 except:
95 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
95 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
96 nProfiles=nProfiles,
96 nProfiles=nProfiles,
97 nChannels=len(self.__channelList),
97 nChannels=len(self.__channelList),
98 adcResolution=14)
98 adcResolution=14)
99 self.dataOut.type = "Voltage"
99 self.dataOut.type = "Voltage"
100
100
101 self.dataOut.data = None
101 self.dataOut.data = None
102
102
103 self.dataOut.dtype = self.dtype
103 self.dataOut.dtype = self.dtype
104
104
105 # self.dataOut.nChannels = 0
105 # self.dataOut.nChannels = 0
106
106
107 # self.dataOut.nHeights = 0
107 # self.dataOut.nHeights = 0
108
108
109 self.dataOut.nProfiles = nProfiles
109 self.dataOut.nProfiles = int(nProfiles)
110
110
111 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
111 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
112
112
113 self.dataOut.channelList = range(self.__num_subchannels)
113 self.dataOut.channelList = range(self.__num_subchannels)
114
114
115 self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights()
115 self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights()
116
116
117 # self.dataOut.channelIndexList = None
117 # self.dataOut.channelIndexList = None
118
118
119 self.dataOut.flagNoData = True
119 self.dataOut.flagNoData = True
120
120
121 self.dataOut.flagDataAsBlock = False
121 self.dataOut.flagDataAsBlock = False
122 # Set to TRUE if the data is discontinuous
122 # Set to TRUE if the data is discontinuous
123 self.dataOut.flagDiscontinuousBlock = False
123 self.dataOut.flagDiscontinuousBlock = False
124
124
125 self.dataOut.utctime = None
125 self.dataOut.utctime = None
126
126
127 self.dataOut.timeZone = self.__timezone/60 # timezone like jroheader, difference in minutes between UTC and localtime
127 self.dataOut.timeZone = self.__timezone/60 # timezone like jroheader, difference in minutes between UTC and localtime
128
128
129 self.dataOut.dstFlag = 0
129 self.dataOut.dstFlag = 0
130
130
131 self.dataOut.errorCount = 0
131 self.dataOut.errorCount = 0
132
132
133 try:
133 try:
134 self.dataOut.nCohInt = self.fixed_metadata_dict.get('nCohInt', 1)
134 self.dataOut.nCohInt = self.fixed_metadata_dict.get('nCohInt', 1)
135
135
136 self.dataOut.flagDecodeData = self.fixed_metadata_dict['flagDecodeData'] # asumo que la data esta decodificada
136 self.dataOut.flagDecodeData = self.fixed_metadata_dict['flagDecodeData'] # asumo que la data esta decodificada
137
137
138 self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData'] # asumo que la data esta sin flip
138 self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData'] # asumo que la data esta sin flip
139
139
140 self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT']
140 self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT']
141
141
142 self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime']
142 self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime']
143 except:
143 except:
144 pass
144 pass
145
145
146
146
147 self.dataOut.ippSeconds = ippSeconds
147 self.dataOut.ippSeconds = ippSeconds
148
148
149 # Time interval between profiles
149 # Time interval between profiles
150 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
150 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
151
151
152 self.dataOut.frequency = self.__frequency
152 self.dataOut.frequency = self.__frequency
153
153
154 self.dataOut.realtime = self.__online
154 self.dataOut.realtime = self.__online
155
155
156 def findDatafiles(self, path, startDate=None, endDate=None):
156 def findDatafiles(self, path, startDate=None, endDate=None):
157
157
158 if not os.path.isdir(path):
158 if not os.path.isdir(path):
159 return []
159 return []
160
160
161 try:
161 try:
162 digitalReadObj = digital_rf.DigitalRFReader(path, load_all_metadata=True)
162 digitalReadObj = digital_rf.DigitalRFReader(path, load_all_metadata=True)
163 except:
163 except:
164 digitalReadObj = digital_rf.DigitalRFReader(path)
164 digitalReadObj = digital_rf.DigitalRFReader(path)
165
165
166 channelNameList = digitalReadObj.get_channels()
166 channelNameList = digitalReadObj.get_channels()
167
167
168 if not channelNameList:
168 if not channelNameList:
169 return []
169 return []
170
170
171 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
171 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
172
172
173 sample_rate = metadata_dict['sample_rate'][0]
173 sample_rate = metadata_dict['sample_rate'][0]
174
174
175 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
175 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
176
176
177 try:
177 try:
178 timezone = this_metadata_file['timezone'].value
178 timezone = this_metadata_file['timezone'].value
179 except:
179 except:
180 timezone = 0
180 timezone = 0
181
181
182 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(channelNameList[0])/sample_rate - timezone
182 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(channelNameList[0])/sample_rate - timezone
183
183
184 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
184 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
185 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
185 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
186
186
187 if not startDate:
187 if not startDate:
188 startDate = startDatetime.date()
188 startDate = startDatetime.date()
189
189
190 if not endDate:
190 if not endDate:
191 endDate = endDatatime.date()
191 endDate = endDatatime.date()
192
192
193 dateList = []
193 dateList = []
194
194
195 thisDatetime = startDatetime
195 thisDatetime = startDatetime
196
196
197 while(thisDatetime<=endDatatime):
197 while(thisDatetime<=endDatatime):
198
198
199 thisDate = thisDatetime.date()
199 thisDate = thisDatetime.date()
200
200
201 if thisDate < startDate:
201 if thisDate < startDate:
202 continue
202 continue
203
203
204 if thisDate > endDate:
204 if thisDate > endDate:
205 break
205 break
206
206
207 dateList.append(thisDate)
207 dateList.append(thisDate)
208 thisDatetime += datetime.timedelta(1)
208 thisDatetime += datetime.timedelta(1)
209
209
210 return dateList
210 return dateList
211
211
212 def setup(self, path = None,
212 def setup(self, path = None,
213 startDate = None,
213 startDate = None,
214 endDate = None,
214 endDate = None,
215 startTime = datetime.time(0,0,0),
215 startTime = datetime.time(0,0,0),
216 endTime = datetime.time(23,59,59),
216 endTime = datetime.time(23,59,59),
217 channelList = None,
217 channelList = None,
218 nSamples = None,
218 nSamples = None,
219 online = False,
219 online = False,
220 delay = 60,
220 delay = 60,
221 buffer_size = 1024,
221 buffer_size = 1024,
222 ippKm=None,
222 ippKm=None,
223 **kwargs):
223 **kwargs):
224 '''
224 '''
225 In this method we should set all initial parameters.
225 In this method we should set all initial parameters.
226
226
227 Inputs:
227 Inputs:
228 path
228 path
229 startDate
229 startDate
230 endDate
230 endDate
231 startTime
231 startTime
232 endTime
232 endTime
233 set
233 set
234 expLabel
234 expLabel
235 ext
235 ext
236 online
236 online
237 delay
237 delay
238 '''
238 '''
239 self.i = 0
239 self.i = 0
240 if not os.path.isdir(path):
240 if not os.path.isdir(path):
241 raise ValueError, "[Reading] Directory %s does not exist" %path
241 raise ValueError, "[Reading] Directory %s does not exist" %path
242
242
243 try:
243 try:
244 self.digitalReadObj = digital_rf.DigitalRFReader(path, load_all_metadata=True)
244 self.digitalReadObj = digital_rf.DigitalRFReader(path, load_all_metadata=True)
245 except:
245 except:
246 self.digitalReadObj = digital_rf.DigitalRFReader(path)
246 self.digitalReadObj = digital_rf.DigitalRFReader(path)
247
247
248 channelNameList = self.digitalReadObj.get_channels()
248 channelNameList = self.digitalReadObj.get_channels()
249
249
250 if not channelNameList:
250 if not channelNameList:
251 raise ValueError, "[Reading] Directory %s does not have any files" %path
251 raise ValueError, "[Reading] Directory %s does not have any files" %path
252
252
253 if not channelList:
253 if not channelList:
254 channelList = range(len(channelNameList))
254 channelList = range(len(channelNameList))
255
255
256
256
257 ########## Reading metadata ######################
257 ########## Reading metadata ######################
258
258
259 top_properties = self.digitalReadObj.get_properties(channelNameList[channelList[0]])
259 top_properties = self.digitalReadObj.get_properties(channelNameList[channelList[0]])
260
260
261
261
262 self.__num_subchannels = top_properties['num_subchannels']
262 self.__num_subchannels = top_properties['num_subchannels']
263 self.__sample_rate = 1.0 * top_properties['sample_rate_numerator'] / top_properties['sample_rate_denominator']
263 self.__sample_rate = 1.0 * top_properties['sample_rate_numerator'] / top_properties['sample_rate_denominator']
264 # self.__samples_per_file = top_properties['samples_per_file'][0]
264 # self.__samples_per_file = top_properties['samples_per_file'][0]
265 self.__deltaHeigth = 1e6*0.15/self.__sample_rate ## why 0.15?
265 self.__deltaHeigth = 1e6*0.15/self.__sample_rate ## why 0.15?
266
266
267 this_metadata_file = self.digitalReadObj.get_digital_metadata(channelNameList[channelList[0]])
267 this_metadata_file = self.digitalReadObj.get_digital_metadata(channelNameList[channelList[0]])
268 metadata_bounds = this_metadata_file.get_bounds()
268 metadata_bounds = this_metadata_file.get_bounds()
269 self.fixed_metadata_dict = this_metadata_file.read(metadata_bounds[0])[metadata_bounds[0]] ## GET FIRST HEADER
269 self.fixed_metadata_dict = this_metadata_file.read(metadata_bounds[0])[metadata_bounds[0]] ## GET FIRST HEADER
270
270
271 try:
271 try:
272 self.__processingHeader = self.fixed_metadata_dict['processingHeader']
272 self.__processingHeader = self.fixed_metadata_dict['processingHeader']
273 self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader']
273 self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader']
274 self.__systemHeader = self.fixed_metadata_dict['systemHeader']
274 self.__systemHeader = self.fixed_metadata_dict['systemHeader']
275 self.dtype = cPickle.loads(self.fixed_metadata_dict['dtype'])
275 self.dtype = cPickle.loads(self.fixed_metadata_dict['dtype'])
276 except:
276 except:
277 pass
277 pass
278
278
279
279
280 self.__frequency = None
280 self.__frequency = None
281
281
282 self.__frequency = self.fixed_metadata_dict.get('frequency', 1)
282 self.__frequency = self.fixed_metadata_dict.get('frequency', 1)
283
283
284 self.__timezone = self.fixed_metadata_dict.get('timezone', 300)
284 self.__timezone = self.fixed_metadata_dict.get('timezone', 300)
285
285
286
286
287 try:
287 try:
288 nSamples = self.fixed_metadata_dict['nSamples']
288 nSamples = self.fixed_metadata_dict['nSamples']
289 except:
289 except:
290 nSamples = None
290 nSamples = None
291
291
292 self.__firstHeigth = 0
292 self.__firstHeigth = 0
293
293
294 try:
294 try:
295 codeType = self.__radarControllerHeader['codeType']
295 codeType = self.__radarControllerHeader['codeType']
296 except:
296 except:
297 codeType = 0
297 codeType = 0
298
298
299 nCode = 1
299 nCode = 1
300 nBaud = 1
300 nBaud = 1
301 code = numpy.ones((nCode, nBaud), dtype=numpy.int)
301 code = numpy.ones((nCode, nBaud), dtype=numpy.int)
302
302
303 try:
303 try:
304 if codeType:
304 if codeType:
305 nCode = self.__radarControllerHeader['nCode']
305 nCode = self.__radarControllerHeader['nCode']
306 nBaud = self.__radarControllerHeader['nBaud']
306 nBaud = self.__radarControllerHeader['nBaud']
307 code = self.__radarControllerHeader['code']
307 code = self.__radarControllerHeader['code']
308 except:
308 except:
309 pass
309 pass
310
310
311
311
312 if not ippKm:
312 if not ippKm:
313 try:
313 try:
314 # seconds to km
314 # seconds to km
315 ippKm = self.__radarControllerHeader['ipp']
315 ippKm = self.__radarControllerHeader['ipp']
316 except:
316 except:
317 ippKm = None
317 ippKm = None
318 ####################################################
318 ####################################################
319 self.__ippKm = ippKm
319 self.__ippKm = ippKm
320 startUTCSecond = None
320 startUTCSecond = None
321 endUTCSecond = None
321 endUTCSecond = None
322
322
323 if startDate:
323 if startDate:
324 startDatetime = datetime.datetime.combine(startDate, startTime)
324 startDatetime = datetime.datetime.combine(startDate, startTime)
325 startUTCSecond = (startDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
325 startUTCSecond = (startDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
326
326
327 if endDate:
327 if endDate:
328 endDatetime = datetime.datetime.combine(endDate, endTime)
328 endDatetime = datetime.datetime.combine(endDate, endTime)
329 endUTCSecond = (endDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
329 endUTCSecond = (endDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
330
330
331 start_index, end_index = self.digitalReadObj.get_bounds(channelNameList[channelList[0]])
331 start_index, end_index = self.digitalReadObj.get_bounds(channelNameList[channelList[0]])
332
332
333 if not startUTCSecond:
333 if not startUTCSecond:
334 startUTCSecond = start_index/self.__sample_rate
334 startUTCSecond = start_index/self.__sample_rate
335
335
336 if start_index > startUTCSecond*self.__sample_rate:
336 if start_index > startUTCSecond*self.__sample_rate:
337 startUTCSecond = start_index/self.__sample_rate
337 startUTCSecond = start_index/self.__sample_rate
338
338
339 if not endUTCSecond:
339 if not endUTCSecond:
340 endUTCSecond = end_index/self.__sample_rate
340 endUTCSecond = end_index/self.__sample_rate
341
341
342 if end_index < endUTCSecond*self.__sample_rate:
342 if end_index < endUTCSecond*self.__sample_rate:
343 endUTCSecond = end_index/self.__sample_rate
343 endUTCSecond = end_index/self.__sample_rate
344 if not nSamples:
344 if not nSamples:
345 if not ippKm:
345 if not ippKm:
346 raise ValueError, "[Reading] nSamples or ippKm should be defined"
346 raise ValueError, "[Reading] nSamples or ippKm should be defined"
347 nSamples = int(ippKm / (1e6*0.15/self.__sample_rate))
347 nSamples = int(ippKm / (1e6*0.15/self.__sample_rate))
348 channelBoundList = []
348 channelBoundList = []
349 channelNameListFiltered = []
349 channelNameListFiltered = []
350
350
351 for thisIndexChannel in channelList:
351 for thisIndexChannel in channelList:
352 thisChannelName = channelNameList[thisIndexChannel]
352 thisChannelName = channelNameList[thisIndexChannel]
353 start_index, end_index = self.digitalReadObj.get_bounds(thisChannelName)
353 start_index, end_index = self.digitalReadObj.get_bounds(thisChannelName)
354 channelBoundList.append((start_index, end_index))
354 channelBoundList.append((start_index, end_index))
355 channelNameListFiltered.append(thisChannelName)
355 channelNameListFiltered.append(thisChannelName)
356
356
357 self.profileIndex = 0
357 self.profileIndex = 0
358 self.i= 0
358 self.i= 0
359 self.__delay = delay
359 self.__delay = delay
360
360
361 self.__codeType = codeType
361 self.__codeType = codeType
362 self.__nCode = nCode
362 self.__nCode = nCode
363 self.__nBaud = nBaud
363 self.__nBaud = nBaud
364 self.__code = code
364 self.__code = code
365
365
366 self.__datapath = path
366 self.__datapath = path
367 self.__online = online
367 self.__online = online
368 self.__channelList = channelList
368 self.__channelList = channelList
369 self.__channelNameList = channelNameListFiltered
369 self.__channelNameList = channelNameListFiltered
370 self.__channelBoundList = channelBoundList
370 self.__channelBoundList = channelBoundList
371 self.__nSamples = nSamples
371 self.__nSamples = nSamples
372 self.__samples_to_read = long(nSamples) # FIJO: AHORA 40
372 self.__samples_to_read = long(nSamples) # FIJO: AHORA 40
373 self.__nChannels = len(self.__channelList)
373 self.__nChannels = len(self.__channelList)
374
374
375 self.__startUTCSecond = startUTCSecond
375 self.__startUTCSecond = startUTCSecond
376 self.__endUTCSecond = endUTCSecond
376 self.__endUTCSecond = endUTCSecond
377
377
378 self.__timeInterval = 1.0 * self.__samples_to_read/self.__sample_rate # Time interval
378 self.__timeInterval = 1.0 * self.__samples_to_read/self.__sample_rate # Time interval
379
379
380 if online:
380 if online:
381 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
381 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
382 startUTCSecond = numpy.floor(endUTCSecond)
382 startUTCSecond = numpy.floor(endUTCSecond)
383
383
384 self.__thisUnixSample = long(startUTCSecond*self.__sample_rate) - self.__samples_to_read ## por que en el otro metodo lo primero q se hace es sumar samplestoread
384 self.__thisUnixSample = long(startUTCSecond*self.__sample_rate) - self.__samples_to_read ## por que en el otro metodo lo primero q se hace es sumar samplestoread
385
385
386 self.__data_buffer = numpy.zeros((self.__num_subchannels, self.__samples_to_read), dtype = numpy.complex)
386 self.__data_buffer = numpy.zeros((self.__num_subchannels, self.__samples_to_read), dtype = numpy.complex)
387
387
388 self.__setFileHeader()
388 self.__setFileHeader()
389 self.isConfig = True
389 self.isConfig = True
390
390
391 print "[Reading] Digital RF Data was found from %s to %s " %(
391 print "[Reading] Digital RF Data was found from %s to %s " %(
392 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
392 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
393 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
393 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
394 )
394 )
395
395
396 print "[Reading] Starting process from %s to %s" %(datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
396 print "[Reading] Starting process from %s to %s" %(datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
397 datetime.datetime.utcfromtimestamp(endUTCSecond - self.__timezone)
397 datetime.datetime.utcfromtimestamp(endUTCSecond - self.__timezone)
398 )
398 )
399 self.oldAverage = None
399 self.oldAverage = None
400 self.count = 0
400 self.count = 0
401 self.executionTime = 0
401 self.executionTime = 0
402 def __reload(self):
402 def __reload(self):
403 # print
403 # print
404 # print "%s not in range [%s, %s]" %(
404 # print "%s not in range [%s, %s]" %(
405 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
405 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
406 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
406 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
407 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
407 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
408 # )
408 # )
409 print "[Reading] reloading metadata ..."
409 print "[Reading] reloading metadata ..."
410
410
411 try:
411 try:
412 self.digitalReadObj.reload(complete_update=True)
412 self.digitalReadObj.reload(complete_update=True)
413 except:
413 except:
414 self.digitalReadObj.reload()
414 self.digitalReadObj.reload()
415
415
416 start_index, end_index = self.digitalReadObj.get_bounds(self.__channelNameList[self.__channelList[0]])
416 start_index, end_index = self.digitalReadObj.get_bounds(self.__channelNameList[self.__channelList[0]])
417
417
418 if start_index > self.__startUTCSecond*self.__sample_rate:
418 if start_index > self.__startUTCSecond*self.__sample_rate:
419 self.__startUTCSecond = 1.0*start_index/self.__sample_rate
419 self.__startUTCSecond = 1.0*start_index/self.__sample_rate
420
420
421 if end_index > self.__endUTCSecond*self.__sample_rate:
421 if end_index > self.__endUTCSecond*self.__sample_rate:
422 self.__endUTCSecond = 1.0*end_index/self.__sample_rate
422 self.__endUTCSecond = 1.0*end_index/self.__sample_rate
423 print
423 print
424 print "[Reading] New timerange found [%s, %s] " %(
424 print "[Reading] New timerange found [%s, %s] " %(
425 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
425 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
426 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
426 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
427 )
427 )
428
428
429 return True
429 return True
430
430
431 return False
431 return False
432
432
433 def timeit(self, toExecute):
433 def timeit(self, toExecute):
434 t0 = time()
434 t0 = time()
435 toExecute()
435 toExecute()
436 self.executionTime = time() - t0
436 self.executionTime = time() - t0
437 if self.oldAverage is None: self.oldAverage = self.executionTime
437 if self.oldAverage is None: self.oldAverage = self.executionTime
438 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
438 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
439 self.count = self.count + 1.0
439 self.count = self.count + 1.0
440 return
440 return
441
441
442 def __readNextBlock(self, seconds=30, volt_scale = 1):
442 def __readNextBlock(self, seconds=30, volt_scale = 1):
443 '''
443 '''
444 '''
444 '''
445
445
446 # Set the next data
446 # Set the next data
447 self.__flagDiscontinuousBlock = False
447 self.__flagDiscontinuousBlock = False
448 self.__thisUnixSample += self.__samples_to_read
448 self.__thisUnixSample += self.__samples_to_read
449
449
450 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
450 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
451 print "[Reading] There are no more data into selected time-range"
451 print "[Reading] There are no more data into selected time-range"
452 if self.__online:
452 if self.__online:
453 self.__reload()
453 self.__reload()
454 else:
454 else:
455 return False
455 return False
456
456
457 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
457 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
458 return False
458 return False
459 self.__thisUnixSample -= self.__samples_to_read
459 self.__thisUnixSample -= self.__samples_to_read
460
460
461 indexChannel = 0
461 indexChannel = 0
462
462
463 dataOk = False
463 dataOk = False
464 for thisChannelName in self.__channelNameList: ##TODO VARIOS CHANNELS?
464 for thisChannelName in self.__channelNameList: ##TODO VARIOS CHANNELS?
465 for indexSubchannel in range(self.__num_subchannels):
465 for indexSubchannel in range(self.__num_subchannels):
466 try:
466 try:
467 t0 = time()
467 t0 = time()
468 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
468 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
469 self.__samples_to_read,
469 self.__samples_to_read,
470 thisChannelName, sub_channel=indexSubchannel)
470 thisChannelName, sub_channel=indexSubchannel)
471 self.executionTime = time() - t0
471 self.executionTime = time() - t0
472 if self.oldAverage is None: self.oldAverage = self.executionTime
472 if self.oldAverage is None: self.oldAverage = self.executionTime
473 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
473 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
474 self.count = self.count + 1.0
474 self.count = self.count + 1.0
475
475
476 except IOError, e:
476 except IOError, e:
477 #read next profile
477 #read next profile
478 self.__flagDiscontinuousBlock = True
478 self.__flagDiscontinuousBlock = True
479 print "[Reading] %s" %datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e
479 print "[Reading] %s" %datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e
480 break
480 break
481
481
482 if result.shape[0] != self.__samples_to_read:
482 if result.shape[0] != self.__samples_to_read:
483 self.__flagDiscontinuousBlock = True
483 self.__flagDiscontinuousBlock = True
484 print "[Reading] %s: Too few samples were found, just %d/%d samples" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
484 print "[Reading] %s: Too few samples were found, just %d/%d samples" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
485 result.shape[0],
485 result.shape[0],
486 self.__samples_to_read)
486 self.__samples_to_read)
487 break
487 break
488
488
489 self.__data_buffer[indexSubchannel,:] = result*volt_scale
489 self.__data_buffer[indexSubchannel,:] = result*volt_scale
490
490
491 indexChannel += 1
491 indexChannel += 1
492
492
493 dataOk = True
493 dataOk = True
494
494
495 self.__utctime = self.__thisUnixSample/self.__sample_rate
495 self.__utctime = self.__thisUnixSample/self.__sample_rate
496
496
497 if not dataOk:
497 if not dataOk:
498 return False
498 return False
499
499
500 print "[Reading] %s: %d samples <> %f sec" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
500 print "[Reading] %s: %d samples <> %f sec" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
501 self.__samples_to_read,
501 self.__samples_to_read,
502 self.__timeInterval)
502 self.__timeInterval)
503
503
504 self.__bufferIndex = 0
504 self.__bufferIndex = 0
505
505
506 return True
506 return True
507
507
508 def __isBufferEmpty(self):
508 def __isBufferEmpty(self):
509 return self.__bufferIndex > self.__samples_to_read - self.__nSamples #40960 - 40
509 return self.__bufferIndex > self.__samples_to_read - self.__nSamples #40960 - 40
510
510
511 def getData(self, seconds=30, nTries=5):
511 def getData(self, seconds=30, nTries=5):
512
512
513 '''
513 '''
514 This method gets the data from files and put the data into the dataOut object
514 This method gets the data from files and put the data into the dataOut object
515
515
516 In addition, increase el the buffer counter in one.
516 In addition, increase el the buffer counter in one.
517
517
518 Return:
518 Return:
519 data : retorna un perfil de voltages (alturas * canales) copiados desde el
519 data : retorna un perfil de voltages (alturas * canales) copiados desde el
520 buffer. Si no hay mas archivos a leer retorna None.
520 buffer. Si no hay mas archivos a leer retorna None.
521
521
522 Affected:
522 Affected:
523 self.dataOut
523 self.dataOut
524 self.profileIndex
524 self.profileIndex
525 self.flagDiscontinuousBlock
525 self.flagDiscontinuousBlock
526 self.flagIsNewBlock
526 self.flagIsNewBlock
527 '''
527 '''
528
528
529 err_counter = 0
529 err_counter = 0
530 self.dataOut.flagNoData = True
530 self.dataOut.flagNoData = True
531
531
532 if self.__isBufferEmpty():
532 if self.__isBufferEmpty():
533 self.__flagDiscontinuousBlock = False
533 self.__flagDiscontinuousBlock = False
534
534
535 while True:
535 while True:
536 if self.__readNextBlock():
536 if self.__readNextBlock():
537 break
537 break
538 if self.__thisUnixSample > self.__endUTCSecond*self.__sample_rate:
538 if self.__thisUnixSample > self.__endUTCSecond*self.__sample_rate:
539 return False
539 return False
540
540
541 if self.__flagDiscontinuousBlock:
541 if self.__flagDiscontinuousBlock:
542 print '[Reading] discontinuous block found ... continue with the next block'
542 print '[Reading] discontinuous block found ... continue with the next block'
543 continue
543 continue
544
544
545 if not self.__online:
545 if not self.__online:
546 return False
546 return False
547
547
548 err_counter += 1
548 err_counter += 1
549 if err_counter > nTries:
549 if err_counter > nTries:
550 return False
550 return False
551
551
552 print '[Reading] waiting %d seconds to read a new block' %seconds
552 print '[Reading] waiting %d seconds to read a new block' %seconds
553 sleep(seconds)
553 sleep(seconds)
554
554
555 self.dataOut.data = self.__data_buffer[:,self.__bufferIndex:self.__bufferIndex+self.__nSamples]
555 self.dataOut.data = self.__data_buffer[:,self.__bufferIndex:self.__bufferIndex+self.__nSamples]
556 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex)/self.__sample_rate
556 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex)/self.__sample_rate
557 self.dataOut.flagNoData = False
557 self.dataOut.flagNoData = False
558 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
558 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
559 self.dataOut.profileIndex = self.profileIndex
559 self.dataOut.profileIndex = self.profileIndex
560
560
561 self.__bufferIndex += self.__nSamples
561 self.__bufferIndex += self.__nSamples
562 self.profileIndex += 1
562 self.profileIndex += 1
563
563
564 if self.profileIndex == self.dataOut.nProfiles:
564 if self.profileIndex == self.dataOut.nProfiles:
565 self.profileIndex = 0
565 self.profileIndex = 0
566
566
567 return True
567 return True
568
568
569 def printInfo(self):
569 def printInfo(self):
570 '''
570 '''
571 '''
571 '''
572 if self.__printInfo == False:
572 if self.__printInfo == False:
573 return
573 return
574
574
575 # self.systemHeaderObj.printInfo()
575 # self.systemHeaderObj.printInfo()
576 # self.radarControllerHeaderObj.printInfo()
576 # self.radarControllerHeaderObj.printInfo()
577
577
578 self.__printInfo = False
578 self.__printInfo = False
579
579
580 def printNumberOfBlock(self):
580 def printNumberOfBlock(self):
581 '''
581 '''
582 '''
582 '''
583 return
583 return
584 # print self.profileIndex
584 # print self.profileIndex
585
585
586
586
587 def run(self, **kwargs):
587 def run(self, **kwargs):
588 '''
588 '''
589 This method will be called many times so here you should put all your code
589 This method will be called many times so here you should put all your code
590 '''
590 '''
591
591
592 if not self.isConfig:
592 if not self.isConfig:
593 self.setup(**kwargs)
593 self.setup(**kwargs)
594 #self.i = self.i+1
594 #self.i = self.i+1
595 self.getData(seconds=self.__delay)
595 self.getData(seconds=self.__delay)
596
596
597 return
597 return
598
598
599 class DigitalRFWriter(Operation):
599 class DigitalRFWriter(Operation):
600 '''
600 '''
601 classdocs
601 classdocs
602 '''
602 '''
603
603
604 def __init__(self, **kwargs):
604 def __init__(self, **kwargs):
605 '''
605 '''
606 Constructor
606 Constructor
607 '''
607 '''
608 Operation.__init__(self, **kwargs)
608 Operation.__init__(self, **kwargs)
609 self.metadata_dict = {}
609 self.metadata_dict = {}
610 self.dataOut = None
610 self.dataOut = None
611 self.dtype = None
611 self.dtype = None
612
612
613 def setHeader(self):
613 def setHeader(self):
614
614
615 self.metadata_dict['frequency'] = self.dataOut.frequency
615 self.metadata_dict['frequency'] = self.dataOut.frequency
616 self.metadata_dict['timezone'] = self.dataOut.timeZone
616 self.metadata_dict['timezone'] = self.dataOut.timeZone
617 self.metadata_dict['dtype'] = cPickle.dumps(self.dataOut.dtype)
617 self.metadata_dict['dtype'] = cPickle.dumps(self.dataOut.dtype)
618 self.metadata_dict['nProfiles'] = self.dataOut.nProfiles
618 self.metadata_dict['nProfiles'] = self.dataOut.nProfiles
619 self.metadata_dict['heightList'] = self.dataOut.heightList
619 self.metadata_dict['heightList'] = self.dataOut.heightList
620 self.metadata_dict['channelList'] = self.dataOut.channelList
620 self.metadata_dict['channelList'] = self.dataOut.channelList
621 self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData
621 self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData
622 self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData
622 self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData
623 self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT
623 self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT
624 self.metadata_dict['flagDataAsBlock'] = self.dataOut.flagDataAsBlock
624 self.metadata_dict['flagDataAsBlock'] = self.dataOut.flagDataAsBlock
625 self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime
625 self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime
626 self.metadata_dict['nCohInt'] = self.dataOut.nCohInt
626 self.metadata_dict['nCohInt'] = self.dataOut.nCohInt
627
627
628 return
628 return
629
629
630 def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'):
630 def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'):
631 '''
631 '''
632 In this method we should set all initial parameters.
632 In this method we should set all initial parameters.
633 Input:
633 Input:
634 dataOut: Input data will also be outputa data
634 dataOut: Input data will also be outputa data
635 '''
635 '''
636 self.setHeader()
636 self.setHeader()
637 self.__ippSeconds = dataOut.ippSeconds
637 self.__ippSeconds = dataOut.ippSeconds
638 self.__deltaH = dataOut.getDeltaH()
638 self.__deltaH = dataOut.getDeltaH()
639 self.__sample_rate = 1e6*0.15/self.__deltaH
639 self.__sample_rate = 1e6*0.15/self.__deltaH
640 self.__dtype = dataOut.dtype
640 self.__dtype = dataOut.dtype
641 if len(dataOut.dtype) == 2:
641 if len(dataOut.dtype) == 2:
642 self.__dtype = dataOut.dtype[0]
642 self.__dtype = dataOut.dtype[0]
643 self.__nSamples = dataOut.systemHeaderObj.nSamples
643 self.__nSamples = dataOut.systemHeaderObj.nSamples
644 self.__nProfiles = dataOut.nProfiles
644 self.__nProfiles = dataOut.nProfiles
645 self.__blocks_per_file = dataOut.processingHeaderObj.dataBlocksPerFile
645 self.__blocks_per_file = dataOut.processingHeaderObj.dataBlocksPerFile
646
646
647 self.arr_data = arr_data = numpy.ones((self.__nSamples, len(self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
647 self.arr_data = arr_data = numpy.ones((self.__nSamples, len(self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
648
648
649 file_cadence_millisecs = long(1.0 * self.__blocks_per_file * self.__nProfiles * self.__nSamples / self.__sample_rate) * 1000
649 file_cadence_millisecs = long(1.0 * self.__blocks_per_file * self.__nProfiles * self.__nSamples / self.__sample_rate) * 1000
650 sub_cadence_secs = file_cadence_millisecs / 500
650 sub_cadence_secs = file_cadence_millisecs / 500
651
651
652 sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator()
652 sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator()
653 sample_rate_numerator = long(sample_rate_fraction.numerator)
653 sample_rate_numerator = long(sample_rate_fraction.numerator)
654 sample_rate_denominator = long(sample_rate_fraction.denominator)
654 sample_rate_denominator = long(sample_rate_fraction.denominator)
655 start_global_index = dataOut.utctime * self.__sample_rate
655 start_global_index = dataOut.utctime * self.__sample_rate
656
656
657 uuid = 'prueba'
657 uuid = 'prueba'
658 compression_level = 1
658 compression_level = 1
659 checksum = False
659 checksum = False
660 is_complex = True
660 is_complex = True
661 num_subchannels = len(dataOut.channelList)
661 num_subchannels = len(dataOut.channelList)
662 is_continuous = True
662 is_continuous = True
663 marching_periods = False
663 marching_periods = False
664
664
665 self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence,
665 self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence,
666 fileCadence, start_global_index,
666 fileCadence, start_global_index,
667 sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum,
667 sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum,
668 is_complex, num_subchannels, is_continuous, marching_periods)
668 is_complex, num_subchannels, is_continuous, marching_periods)
669
669
670 metadata_dir = os.path.join(path, 'metadata')
670 metadata_dir = os.path.join(path, 'metadata')
671 os.system('mkdir %s' % (metadata_dir))
671 os.system('mkdir %s' % (metadata_dir))
672
672
673 self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, ##236, file_cadence_millisecs / 1000
673 self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, ##236, file_cadence_millisecs / 1000
674 sample_rate_numerator, sample_rate_denominator,
674 sample_rate_numerator, sample_rate_denominator,
675 metadataFile)
675 metadataFile)
676
676
677
677
678 self.isConfig = True
678 self.isConfig = True
679 self.currentSample = 0
679 self.currentSample = 0
680 self.oldAverage = 0
680 self.oldAverage = 0
681 self.count = 0
681 self.count = 0
682 return
682 return
683
683
684 def writeMetadata(self):
684 def writeMetadata(self):
685 print '[Writing] - Writing metadata'
685 print '[Writing] - Writing metadata'
686 start_idx = self.__sample_rate * self.dataOut.utctime
686 start_idx = self.__sample_rate * self.dataOut.utctime
687
687
688 self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict()
688 self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict()
689 self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict()
689 self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict()
690 self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict()
690 self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict()
691 self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict)
691 self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict)
692 return
692 return
693
693
694
694
695 def timeit(self, toExecute):
695 def timeit(self, toExecute):
696 t0 = time()
696 t0 = time()
697 toExecute()
697 toExecute()
698 self.executionTime = time() - t0
698 self.executionTime = time() - t0
699 if self.oldAverage is None: self.oldAverage = self.executionTime
699 if self.oldAverage is None: self.oldAverage = self.executionTime
700 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
700 self.oldAverage = (self.executionTime + self.count*self.oldAverage) / (self.count + 1.0)
701 self.count = self.count + 1.0
701 self.count = self.count + 1.0
702 return
702 return
703
703
704
704
705 def writeData(self):
705 def writeData(self):
706 for i in range(self.dataOut.systemHeaderObj.nSamples):
706 for i in range(self.dataOut.systemHeaderObj.nSamples):
707 for channel in self.dataOut.channelList:
707 for channel in self.dataOut.channelList:
708 self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real
708 self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real
709 self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag
709 self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag
710
710
711 def f(): return self.digitalWriteObj.rf_write(self.arr_data)
711 def f(): return self.digitalWriteObj.rf_write(self.arr_data)
712 self.timeit(f)
712 self.timeit(f)
713
713
714 return
714 return
715
715
716 def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=100, dirCadence=25, metadataCadence=1, **kwargs):
716 def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=100, dirCadence=25, metadataCadence=1, **kwargs):
717 '''
717 '''
718 This method will be called many times so here you should put all your code
718 This method will be called many times so here you should put all your code
719 Inputs:
719 Inputs:
720 dataOut: object with the data
720 dataOut: object with the data
721 '''
721 '''
722 # print dataOut.__dict__
722 # print dataOut.__dict__
723 self.dataOut = dataOut
723 self.dataOut = dataOut
724 if not self.isConfig:
724 if not self.isConfig:
725 self.setup(dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, **kwargs)
725 self.setup(dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, **kwargs)
726 self.writeMetadata()
726 self.writeMetadata()
727
727
728 self.writeData()
728 self.writeData()
729
729
730 ## self.currentSample += 1
730 ## self.currentSample += 1
731 ## if self.dataOut.flagDataAsBlock or self.currentSample == 1:
731 ## if self.dataOut.flagDataAsBlock or self.currentSample == 1:
732 ## self.writeMetadata()
732 ## self.writeMetadata()
733 ## if self.currentSample == self.__nProfiles: self.currentSample = 0
733 ## if self.currentSample == self.__nProfiles: self.currentSample = 0
734
734
735 def close(self):
735 def close(self):
736 print '[Writing] - Closing files '
736 print '[Writing] - Closing files '
737 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
737 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
738 try:
738 try:
739 self.digitalWriteObj.close()
739 self.digitalWriteObj.close()
740 except:
740 except:
741 pass
741 pass
742
742
743 # raise
743 # raise
744 if __name__ == '__main__':
744 if __name__ == '__main__':
745
745
746 readObj = DigitalRFReader()
746 readObj = DigitalRFReader()
747
747
748 while True:
748 while True:
749 readObj.run(path='/home/jchavez/jicamarca/mocked_data/')
749 readObj.run(path='/home/jchavez/jicamarca/mocked_data/')
750 # readObj.printInfo()
750 # readObj.printInfo()
751 # readObj.printNumberOfBlock()
751 # readObj.printNumberOfBlock()
General Comments 0
You need to be logged in to leave comments. Login now