##// END OF EJS Templates
merge con online_data_hour
José Chávez -
r1039:a86c6846b78a
parent child
Show More
@@ -1,1866 +1,1853
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 firstTime = True
545 firstTime = True
546 online = 0
546 online = 0
547
547
548 realtime = 0
548 realtime = 0
549
549
550 nReadBlocks = 0
550 nReadBlocks = 0
551
551
552 delay = 10 #number of seconds waiting a new file
552 delay = 10 #number of seconds waiting a new file
553
553
554 nTries = 3 #quantity tries
554 nTries = 3 #quantity tries
555
555
556 nFiles = 3 #number of files for searching
556 nFiles = 3 #number of files for searching
557
557
558 path = None
558 path = None
559
559
560 foldercounter = 0
560 foldercounter = 0
561
561
562 flagNoMoreFiles = 0
562 flagNoMoreFiles = 0
563
563
564 datetimeList = []
564 datetimeList = []
565
565
566 __isFirstTimeOnline = 1
566 __isFirstTimeOnline = 1
567
567
568 __printInfo = True
568 __printInfo = True
569
569
570 profileIndex = None
570 profileIndex = None
571
571
572 nTxs = 1
572 nTxs = 1
573
573
574 txIndex = None
574 txIndex = None
575
575
576 #Added--------------------
576 #Added--------------------
577
577
578 selBlocksize = None
578 selBlocksize = None
579
579
580 selBlocktime = None
580 selBlocktime = None
581
581
582 onlineWithDate = False
582 onlineWithDate = False
583 def __init__(self):
583 def __init__(self):
584
584
585 """
585 """
586 This class is used to find data files
586 This class is used to find data files
587
587
588 Example:
588 Example:
589 reader = JRODataReader()
589 reader = JRODataReader()
590 fileList = reader.findDataFiles()
590 fileList = reader.findDataFiles()
591
591
592 """
592 """
593 pass
593 pass
594
594
595
595
596 def createObjByDefault(self):
596 def createObjByDefault(self):
597 """
597 """
598
598
599 """
599 """
600 raise NotImplementedError
600 raise NotImplementedError
601
601
602 def getBlockDimension(self):
602 def getBlockDimension(self):
603
603
604 raise NotImplementedError
604 raise NotImplementedError
605
605
606 def __searchFilesOffLine(self,
606 def __searchFilesOffLine(self,
607 path,
607 path,
608 startDate=None,
608 startDate=None,
609 endDate=None,
609 endDate=None,
610 startTime=datetime.time(0,0,0),
610 startTime=datetime.time(0,0,0),
611 endTime=datetime.time(23,59,59),
611 endTime=datetime.time(23,59,59),
612 set=None,
612 set=None,
613 expLabel='',
613 expLabel='',
614 ext='.r',
614 ext='.r',
615 queue=None,
615 queue=None,
616 cursor=None,
616 cursor=None,
617 skip=None,
617 skip=None,
618 walk=True):
618 walk=True):
619 self.filenameList = []
619 self.filenameList = []
620 self.datetimeList = []
620 self.datetimeList = []
621
621
622 pathList = []
622 pathList = []
623
623
624 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
624 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
625
625
626 if dateList == []:
626 if dateList == []:
627 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
627 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
628 return None, None
628 return None, None
629
629
630 if len(dateList) > 1:
630 if len(dateList) > 1:
631 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
631 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
632 else:
632 else:
633 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
633 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
634
634
635 filenameList = []
635 filenameList = []
636 datetimeList = []
636 datetimeList = []
637
637
638 for thisPath in pathList:
638 for thisPath in pathList:
639 # thisPath = pathList[pathDict[file]]
639 # thisPath = pathList[pathDict[file]]
640
640
641 fileList = glob.glob1(thisPath, "*%s" %ext)
641 fileList = glob.glob1(thisPath, "*%s" %ext)
642 fileList.sort()
642 fileList.sort()
643
643
644 skippedFileList = []
644 skippedFileList = []
645
645
646 if cursor is not None and skip is not None:
646 if cursor is not None and skip is not None:
647 # if cursor*skip > len(fileList):
647 # if cursor*skip > len(fileList):
648 if skip == 0:
648 if skip == 0:
649 if queue is not None:
649 if queue is not None:
650 queue.put(len(fileList))
650 queue.put(len(fileList))
651 skippedFileList = []
651 skippedFileList = []
652 else:
652 else:
653 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
653 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
654
654
655 else:
655 else:
656 skippedFileList = fileList
656 skippedFileList = fileList
657
657
658 for file in skippedFileList:
658 for file in skippedFileList:
659
659
660 filename = os.path.join(thisPath,file)
660 filename = os.path.join(thisPath,file)
661
661
662 if not isFileInDateRange(filename, startDate, endDate):
662 if not isFileInDateRange(filename, startDate, endDate):
663 continue
663 continue
664
664
665 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
665 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
666
666
667 if not(thisDatetime):
667 if not(thisDatetime):
668 continue
668 continue
669
669
670 filenameList.append(filename)
670 filenameList.append(filename)
671 datetimeList.append(thisDatetime)
671 datetimeList.append(thisDatetime)
672
672
673 if not(filenameList):
673 if not(filenameList):
674 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
674 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
675 return None, None
675 return None, None
676
676
677 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
677 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
678 print
678 print
679
679
680 for i in range(len(filenameList)):
680 for i in range(len(filenameList)):
681 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
681 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
682
682
683 self.filenameList = filenameList
683 self.filenameList = filenameList
684 self.datetimeList = datetimeList
684 self.datetimeList = datetimeList
685 return pathList, filenameList
685 return pathList, filenameList
686
686
687 def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None, startDate=None, startTime=None):
687 def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None, startDate=None, startTime=None):
688
688
689 """
689 """
690 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
690 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
691 devuelve el archivo encontrado ademas de otros datos.
691 devuelve el archivo encontrado ademas de otros datos.
692
692
693 Input:
693 Input:
694 path : carpeta donde estan contenidos los files que contiene data
694 path : carpeta donde estan contenidos los files que contiene data
695
695
696 expLabel : Nombre del subexperimento (subfolder)
696 expLabel : Nombre del subexperimento (subfolder)
697
697
698 ext : extension de los files
698 ext : extension de los files
699
699
700 walk : Si es habilitado no realiza busquedas dentro de los subdirectorios (doypath)
700 walk : Si es habilitado no realiza busquedas dentro de los subdirectorios (doypath)
701
701
702 Return:
702 Return:
703 directory : eL directorio donde esta el file encontrado
703 directory : eL directorio donde esta el file encontrado
704 filename : el ultimo file de una determinada carpeta
704 filename : el ultimo file de una determinada carpeta
705 year : el anho
705 year : el anho
706 doy : el numero de dia del anho
706 doy : el numero de dia del anho
707 set : el set del archivo
707 set : el set del archivo
708
708
709
709
710 """
710 """
711 pathList = None
711 pathList = None
712 filenameList = None
712 filenameList = None
713 if not os.path.isdir(path):
713 if not os.path.isdir(path):
714 return None, None, None, None, None, None
714 return None, None, None, None, None, None
715
715
716 dirList = []
716 dirList = []
717
717
718 if not walk:
718 if not walk:
719 fullpath = path
719 fullpath = path
720 foldercounter = 0
720 foldercounter = 0
721 else:
721 else:
722 # Filtra solo los directorios
722 # Filtra solo los directorios
723 for thisPath in os.listdir(path):
723 for thisPath in os.listdir(path):
724 if not os.path.isdir(os.path.join(path,thisPath)):
724 if not os.path.isdir(os.path.join(path,thisPath)):
725 continue
725 continue
726 if not isRadarFolder(thisPath):
726 if not isRadarFolder(thisPath):
727 continue
727 continue
728
728
729 dirList.append(thisPath)
729 dirList.append(thisPath)
730
730
731 if not(dirList):
731 if not(dirList):
732 return None, None, None, None, None, None
732 return None, None, None, None, None, None
733
733
734 dirList = sorted( dirList, key=str.lower )
734 dirList = sorted( dirList, key=str.lower )
735
735
736 doypath = dirList[-1]
736 doypath = dirList[-1]
737 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
737 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
738 fullpath = os.path.join(path, doypath, expLabel)
738 fullpath = os.path.join(path, doypath, expLabel)
739
739
740
740
741 print "[Reading] %s folder was found: " %(fullpath )
741 print "[Reading] %s folder was found: " %(fullpath )
742
742
743 if set == None:
743 if set == None:
744 filename = getlastFileFromPath(fullpath, ext)
744 filename = getlastFileFromPath(fullpath, ext)
745 else:
745 else:
746 filename = getFileFromSet(fullpath, ext, set)
746 filename = getFileFromSet(fullpath, ext, set)
747
747
748 if not(filename):
748 if not(filename):
749 return None, None, None, None, None, None
749 return None, None, None, None, None, None
750
750
751 print "[Reading] %s file was found" %(filename)
751 print "[Reading] %s file was found" %(filename)
752
752
753 if not(self.__verifyFile(os.path.join(fullpath, filename))):
753 if not(self.__verifyFile(os.path.join(fullpath, filename))):
754 return None, None, None, None, None, None
754 return None, None, None, None, None, None
755
755
756 year = int( filename[1:5] )
756 year = int( filename[1:5] )
757 doy = int( filename[5:8] )
757 doy = int( filename[5:8] )
758 set = int( filename[8:11] )
758 set = int( filename[8:11] )
759
759
760 return fullpath, foldercounter, filename, year, doy, set
760 return fullpath, foldercounter, filename, year, doy, set
761
761
762 def __setNextFileOffline(self):
762 def __setNextFileOffline(self):
763
763
764 idFile = self.fileIndex
764 idFile = self.fileIndex
765
765
766 while (True):
766 while (True):
767 idFile += 1
767 idFile += 1
768 if not(idFile < len(self.filenameList)):
768 if not(idFile < len(self.filenameList)):
769 self.flagNoMoreFiles = 1
769 self.flagNoMoreFiles = 1
770 # print "[Reading] No more Files"
770 # print "[Reading] No more Files"
771 return 0
771 return 0
772
772
773 filename = self.filenameList[idFile]
773 filename = self.filenameList[idFile]
774
774
775 if not(self.__verifyFile(filename)):
775 if not(self.__verifyFile(filename)):
776 continue
776 continue
777
777
778 fileSize = os.path.getsize(filename)
778 fileSize = os.path.getsize(filename)
779 fp = open(filename,'rb')
779 fp = open(filename,'rb')
780 break
780 break
781
781
782 self.flagIsNewFile = 1
782 self.flagIsNewFile = 1
783 self.fileIndex = idFile
783 self.fileIndex = idFile
784 self.filename = filename
784 self.filename = filename
785 self.fileSize = fileSize
785 self.fileSize = fileSize
786 self.fp = fp
786 self.fp = fp
787
787
788 #print "[Reading] Setting the file: %s"%self.filename
788 #print "[Reading] Setting the file: %s"%self.filename
789
789
790 return 1
790 return 1
791
791
792 def __setNextFileOnline(self):
792 def __setNextFileOnline(self):
793 """
793 """
794 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
794 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
795 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
795 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
796 siguientes.
796 siguientes.
797
797
798 Affected:
798 Affected:
799 self.flagIsNewFile
799 self.flagIsNewFile
800 self.filename
800 self.filename
801 self.fileSize
801 self.fileSize
802 self.fp
802 self.fp
803 self.set
803 self.set
804 self.flagNoMoreFiles
804 self.flagNoMoreFiles
805
805
806 Return:
806 Return:
807 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
807 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
808 1 : si el file fue abierto con exito y esta listo a ser leido
808 1 : si el file fue abierto con exito y esta listo a ser leido
809
809
810 Excepciones:
810 Excepciones:
811 Si un determinado file no puede ser abierto
811 Si un determinado file no puede ser abierto
812 """
812 """
813
813
814 nFiles = 0
814 nFiles = 0
815 fileOk_flag = False
815 fileOk_flag = False
816 firstTime_flag = True
816 firstTime_flag = True
817
817
818 self.set += 1
818 self.set += 1
819
819
820 if self.set > 999:
820 if self.set > 999:
821 self.set = 0
821 self.set = 0
822 self.foldercounter += 1
822 self.foldercounter += 1
823
823
824 #busca el 1er file disponible
824 #busca el 1er file disponible
825 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
825 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
826 if fullfilename:
826 if fullfilename:
827 if self.__verifyFile(fullfilename, False):
827 if self.__verifyFile(fullfilename, False):
828 fileOk_flag = True
828 fileOk_flag = True
829
829
830 #si no encuentra un file entonces espera y vuelve a buscar
830 #si no encuentra un file entonces espera y vuelve a buscar
831 if not(fileOk_flag):
831 if not(fileOk_flag):
832 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
832 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
833
833
834 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
834 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
835 tries = self.nTries
835 tries = self.nTries
836 else:
836 else:
837 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
837 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
838
838
839 for nTries in range( tries ):
839 for nTries in range( tries ):
840 if firstTime_flag:
840 if firstTime_flag:
841 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
841 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
842 sleep( self.delay )
842 sleep( self.delay )
843 else:
843 else:
844 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
844 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
845
845
846 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
846 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
847 if fullfilename:
847 if fullfilename:
848 if self.__verifyFile(fullfilename):
848 if self.__verifyFile(fullfilename):
849 fileOk_flag = True
849 fileOk_flag = True
850 break
850 break
851
851
852 if fileOk_flag:
852 if fileOk_flag:
853 break
853 break
854
854
855 firstTime_flag = False
855 firstTime_flag = False
856
856
857 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
857 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
858 self.set += 1
858 self.set += 1
859
859
860 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
860 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
861 self.set = 0
861 self.set = 0
862 self.doy += 1
862 self.doy += 1
863 self.foldercounter = 0
863 self.foldercounter = 0
864
864
865 if fileOk_flag:
865 if fileOk_flag:
866 self.fileSize = os.path.getsize( fullfilename )
866 self.fileSize = os.path.getsize( fullfilename )
867 self.filename = fullfilename
867 self.filename = fullfilename
868 self.flagIsNewFile = 1
868 self.flagIsNewFile = 1
869 if self.fp != None: self.fp.close()
869 if self.fp != None: self.fp.close()
870 self.fp = open(fullfilename, 'rb')
870 self.fp = open(fullfilename, 'rb')
871 self.flagNoMoreFiles = 0
871 self.flagNoMoreFiles = 0
872 # print '[Reading] Setting the file: %s' % fullfilename
872 # print '[Reading] Setting the file: %s' % fullfilename
873 else:
873 else:
874 self.fileSize = 0
874 self.fileSize = 0
875 self.filename = None
875 self.filename = None
876 self.flagIsNewFile = 0
876 self.flagIsNewFile = 0
877 self.fp = None
877 self.fp = None
878 self.flagNoMoreFiles = 1
878 self.flagNoMoreFiles = 1
879 # print '[Reading] No more files to read'
879 # print '[Reading] No more files to read'
880
880
881 return fileOk_flag
881 return fileOk_flag
882
882
883 def setNextFile(self):
883 def setNextFile(self):
884 if self.fp != None:
884 if self.fp != None:
885 self.fp.close()
885 self.fp.close()
886 if self.online:
886 if self.online:
887 newFile = self.__setNextFileOnline()
887 newFile = self.__setNextFileOnline()
888 else:
888 else:
889 newFile = self.__setNextFileOffline()
889 newFile = self.__setNextFileOffline()
890 if not(newFile):
890 if not(newFile):
891 if self.onlineWithDate is True:
891 if self.onlineWithDate is True:
892 self.onlineWithDate=False
892 self.onlineWithDate=False
893 self.online = True
893 self.online = True
894 self.firstTime = False
894 self.firstTime = False
895 self.setup(
895 self.setup(
896 path=self.path,
896 path=self.path,
897 startDate=self.startDate,
897 startDate=self.startDate,
898 endDate=self.endDate,
898 endDate=self.endDate,
899 startTime=self.startTime ,
899 startTime=self.startTime ,
900 endTime=self.endTime,
900 endTime=self.endTime,
901 set=self.set,
901 set=self.set,
902 expLabel=self.expLabel,
902 expLabel=self.expLabel,
903 ext=self.ext,
903 ext=self.ext,
904 online=self.online,
904 online=self.online,
905 delay=self.delay,
905 delay=self.delay,
906 walk=self.walk,
906 walk=self.walk,
907 getblock=self.getblock,
907 getblock=self.getblock,
908 nTxs=self.nTxs,
908 nTxs=self.nTxs,
909 realtime=self.realtime,
909 realtime=self.realtime,
910 blocksize=self.blocksize,
910 blocksize=self.blocksize,
911 blocktime=self.blocktime
911 blocktime=self.blocktime
912 )
912 )
913 return 1
913 return 1
914 print '[Reading] No more files to read'
914 print '[Reading] No more files to read'
915 return 0
915 return 0
916
916
917 if self.verbose:
917 if self.verbose:
918 print '[Reading] Setting the file: %s' % self.filename
918 print '[Reading] Setting the file: %s' % self.filename
919
919
920 self.__readFirstHeader()
920 self.__readFirstHeader()
921 self.nReadBlocks = 0
921 self.nReadBlocks = 0
922 return 1
922 return 1
923
923
924 def __waitNewBlock(self):
924 def __waitNewBlock(self):
925 """
925 """
926 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
926 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
927
927
928 Si el modo de lectura es OffLine siempre retorn 0
928 Si el modo de lectura es OffLine siempre retorn 0
929 """
929 """
930 if not self.online:
930 if not self.online:
931 return 0
931 return 0
932
932
933 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
933 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
934 return 0
934 return 0
935
935
936 currentPointer = self.fp.tell()
936 currentPointer = self.fp.tell()
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
941
942 self.fp.close()
942 self.fp.close()
943 self.fp = open( self.filename, 'rb' )
943 self.fp = open( self.filename, 'rb' )
944 self.fp.seek( currentPointer )
944 self.fp.seek( currentPointer )
945
945
946 self.fileSize = os.path.getsize( self.filename )
946 self.fileSize = os.path.getsize( self.filename )
947 currentSize = self.fileSize - currentPointer
947 currentSize = self.fileSize - currentPointer
948
948
949 if ( currentSize >= neededSize ):
949 if ( currentSize >= neededSize ):
950 self.basicHeaderObj.read(self.fp)
950 self.basicHeaderObj.read(self.fp)
951 return 1
951 return 1
952
952
953 if self.fileSize == self.fileSizeByHeader:
953 if self.fileSize == self.fileSizeByHeader:
954 # self.flagEoF = True
954 # self.flagEoF = True
955 return 0
955 return 0
956
956
957 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
957 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
958 sleep( self.delay )
958 sleep( self.delay )
959
959
960
960
961 return 0
961 return 0
962
962
963 def waitDataBlock(self,pointer_location):
963 def waitDataBlock(self,pointer_location):
964
964
965 currentPointer = pointer_location
965 currentPointer = pointer_location
966
966
967 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
967 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
968
968
969 for nTries in range( self.nTries ):
969 for nTries in range( self.nTries ):
970 self.fp.close()
970 self.fp.close()
971 self.fp = open( self.filename, 'rb' )
971 self.fp = open( self.filename, 'rb' )
972 self.fp.seek( currentPointer )
972 self.fp.seek( currentPointer )
973
973
974 self.fileSize = os.path.getsize( self.filename )
974 self.fileSize = os.path.getsize( self.filename )
975 currentSize = self.fileSize - currentPointer
975 currentSize = self.fileSize - currentPointer
976
976
977 if ( currentSize >= neededSize ):
977 if ( currentSize >= neededSize ):
978 return 1
978 return 1
979
979
980 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
980 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
981 sleep( self.delay )
981 sleep( self.delay )
982
982
983 return 0
983 return 0
984
984
985 def __jumpToLastBlock(self):
985 def __jumpToLastBlock(self):
986
986
987 if not(self.__isFirstTimeOnline):
987 if not(self.__isFirstTimeOnline):
988 return
988 return
989
989
990 csize = self.fileSize - self.fp.tell()
990 csize = self.fileSize - self.fp.tell()
991 blocksize = self.processingHeaderObj.blockSize
991 blocksize = self.processingHeaderObj.blockSize
992
992
993 #salta el primer bloque de datos
993 #salta el primer bloque de datos
994 if csize > self.processingHeaderObj.blockSize:
994 if csize > self.processingHeaderObj.blockSize:
995 self.fp.seek(self.fp.tell() + blocksize)
995 self.fp.seek(self.fp.tell() + blocksize)
996 else:
996 else:
997 return
997 return
998
998
999 csize = self.fileSize - self.fp.tell()
999 csize = self.fileSize - self.fp.tell()
1000 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1000 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1001 while True:
1001 while True:
1002
1002
1003 if self.fp.tell()<self.fileSize:
1003 if self.fp.tell()<self.fileSize:
1004 self.fp.seek(self.fp.tell() + neededsize)
1004 self.fp.seek(self.fp.tell() + neededsize)
1005 else:
1005 else:
1006 self.fp.seek(self.fp.tell() - neededsize)
1006 self.fp.seek(self.fp.tell() - neededsize)
1007 break
1007 break
1008
1008
1009 # csize = self.fileSize - self.fp.tell()
1009 # csize = self.fileSize - self.fp.tell()
1010 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1010 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1011 # factor = int(csize/neededsize)
1011 # factor = int(csize/neededsize)
1012 # if factor > 0:
1012 # if factor > 0:
1013 # self.fp.seek(self.fp.tell() + factor*neededsize)
1013 # self.fp.seek(self.fp.tell() + factor*neededsize)
1014
1014
1015 self.flagIsNewFile = 0
1015 self.flagIsNewFile = 0
1016 self.__isFirstTimeOnline = 0
1016 self.__isFirstTimeOnline = 0
1017
1017
1018 def __setNewBlock(self):
1018 def __setNewBlock(self):
1019 #if self.server is None:
1019 #if self.server is None:
1020 if self.fp == None:
1020 if self.fp == None:
1021 return 0
1021 return 0
1022
1022
1023 # if self.online:
1023 # if self.online:
1024 # self.__jumpToLastBlock()
1024 # self.__jumpToLastBlock()
1025
1025
1026 if self.flagIsNewFile:
1026 if self.flagIsNewFile:
1027 self.lastUTTime = self.basicHeaderObj.utc
1027 self.lastUTTime = self.basicHeaderObj.utc
1028 return 1
1028 return 1
1029
1029
1030 if self.realtime:
1030 if self.realtime:
1031 self.flagDiscontinuousBlock = 1
1031 self.flagDiscontinuousBlock = 1
1032 if not(self.setNextFile()):
1032 if not(self.setNextFile()):
1033 return 0
1033 return 0
1034 else:
1034 else:
1035 return 1
1035 return 1
1036 #if self.server is None:
1036 #if self.server is None:
1037 currentSize = self.fileSize - self.fp.tell()
1037 currentSize = self.fileSize - self.fp.tell()
1038 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1038 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1039 if (currentSize >= neededSize):
1039 if (currentSize >= neededSize):
1040 self.basicHeaderObj.read(self.fp)
1040 self.basicHeaderObj.read(self.fp)
1041 self.lastUTTime = self.basicHeaderObj.utc
1041 self.lastUTTime = self.basicHeaderObj.utc
1042 return 1
1042 return 1
1043 # else:
1043 # else:
1044 # self.basicHeaderObj.read(self.zHeader)
1044 # self.basicHeaderObj.read(self.zHeader)
1045 # self.lastUTTime = self.basicHeaderObj.utc
1045 # self.lastUTTime = self.basicHeaderObj.utc
1046 # return 1
1046 # return 1
1047 if self.__waitNewBlock():
1047 if self.__waitNewBlock():
1048 self.lastUTTime = self.basicHeaderObj.utc
1048 self.lastUTTime = self.basicHeaderObj.utc
1049 return 1
1049 return 1
1050 #if self.server is None:
1050 #if self.server is None:
1051 if not(self.setNextFile()):
1051 if not(self.setNextFile()):
1052 return 0
1052 return 0
1053
1053
1054 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1054 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1055 self.lastUTTime = self.basicHeaderObj.utc
1055 self.lastUTTime = self.basicHeaderObj.utc
1056
1056
1057 self.flagDiscontinuousBlock = 0
1057 self.flagDiscontinuousBlock = 0
1058
1058
1059 if deltaTime > self.maxTimeStep:
1059 if deltaTime > self.maxTimeStep:
1060 self.flagDiscontinuousBlock = 1
1060 self.flagDiscontinuousBlock = 1
1061
1061
1062 return 1
1062 return 1
1063
1063
1064 def readNextBlock(self):
1064 def readNextBlock(self):
1065
1065
1066 #Skip block out of startTime and endTime
1066 #Skip block out of startTime and endTime
1067 while True:
1067 while True:
1068 if not(self.__setNewBlock()):
1068 if not(self.__setNewBlock()):
1069 print 'returning'
1069 print 'returning'
1070 return 0
1070 return 0
1071 if not(self.readBlock()):
1071 if not(self.readBlock()):
1072 return 0
1072 return 0
1073 self.getBasicHeader()
1073 self.getBasicHeader()
1074 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1074 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1075
1075
1076 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1076 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1077 self.processingHeaderObj.dataBlocksPerFile,
1077 self.processingHeaderObj.dataBlocksPerFile,
1078 self.dataOut.datatime.ctime())
1078 self.dataOut.datatime.ctime())
1079 continue
1079 continue
1080
1080
1081 break
1081 break
1082
1082
1083 if self.verbose:
1083 if self.verbose:
1084 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1084 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1085 self.processingHeaderObj.dataBlocksPerFile,
1085 self.processingHeaderObj.dataBlocksPerFile,
1086 self.dataOut.datatime.ctime())
1086 self.dataOut.datatime.ctime())
1087 return 1
1087 return 1
1088
1088
1089 def __readFirstHeader(self):
1089 def __readFirstHeader(self):
1090
1090
1091 self.basicHeaderObj.read(self.fp)
1091 self.basicHeaderObj.read(self.fp)
1092 self.systemHeaderObj.read(self.fp)
1092 self.systemHeaderObj.read(self.fp)
1093 self.radarControllerHeaderObj.read(self.fp)
1093 self.radarControllerHeaderObj.read(self.fp)
1094 self.processingHeaderObj.read(self.fp)
1094 self.processingHeaderObj.read(self.fp)
1095
1095
1096 self.firstHeaderSize = self.basicHeaderObj.size
1096 self.firstHeaderSize = self.basicHeaderObj.size
1097
1097
1098 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1098 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1099 if datatype == 0:
1099 if datatype == 0:
1100 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1100 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1101 elif datatype == 1:
1101 elif datatype == 1:
1102 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1102 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1103 elif datatype == 2:
1103 elif datatype == 2:
1104 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1104 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1105 elif datatype == 3:
1105 elif datatype == 3:
1106 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1106 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1107 elif datatype == 4:
1107 elif datatype == 4:
1108 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1108 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1109 elif datatype == 5:
1109 elif datatype == 5:
1110 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1110 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1111 else:
1111 else:
1112 raise ValueError, 'Data type was not defined'
1112 raise ValueError, 'Data type was not defined'
1113
1113
1114 self.dtype = datatype_str
1114 self.dtype = datatype_str
1115 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1115 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1116 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1116 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1117 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1117 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1118 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1118 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1119 self.getBlockDimension()
1119 self.getBlockDimension()
1120
1120
1121 def __verifyFile(self, filename, msgFlag=True):
1121 def __verifyFile(self, filename, msgFlag=True):
1122
1122
1123 msg = None
1123 msg = None
1124
1124
1125 try:
1125 try:
1126 fp = open(filename, 'rb')
1126 fp = open(filename, 'rb')
1127 except IOError:
1127 except IOError:
1128
1128
1129 if msgFlag:
1129 if msgFlag:
1130 print "[Reading] File %s can't be opened" % (filename)
1130 print "[Reading] File %s can't be opened" % (filename)
1131
1131
1132 return False
1132 return False
1133
1133
1134 currentPosition = fp.tell()
1134 currentPosition = fp.tell()
1135 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1135 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1136
1136
1137 if neededSize == 0:
1137 if neededSize == 0:
1138 basicHeaderObj = BasicHeader(LOCALTIME)
1138 basicHeaderObj = BasicHeader(LOCALTIME)
1139 systemHeaderObj = SystemHeader()
1139 systemHeaderObj = SystemHeader()
1140 radarControllerHeaderObj = RadarControllerHeader()
1140 radarControllerHeaderObj = RadarControllerHeader()
1141 processingHeaderObj = ProcessingHeader()
1141 processingHeaderObj = ProcessingHeader()
1142
1142
1143 if not( basicHeaderObj.read(fp) ):
1143 if not( basicHeaderObj.read(fp) ):
1144 fp.close()
1144 fp.close()
1145 return False
1145 return False
1146
1146
1147 if not( systemHeaderObj.read(fp) ):
1147 if not( systemHeaderObj.read(fp) ):
1148 fp.close()
1148 fp.close()
1149 return False
1149 return False
1150
1150
1151 if not( radarControllerHeaderObj.read(fp) ):
1151 if not( radarControllerHeaderObj.read(fp) ):
1152 fp.close()
1152 fp.close()
1153 return False
1153 return False
1154
1154
1155 if not( processingHeaderObj.read(fp) ):
1155 if not( processingHeaderObj.read(fp) ):
1156 fp.close()
1156 fp.close()
1157 return False
1157 return False
1158
1158
1159 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1159 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1160 else:
1160 else:
1161 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1161 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1162
1162
1163 fp.close()
1163 fp.close()
1164
1164
1165 fileSize = os.path.getsize(filename)
1165 fileSize = os.path.getsize(filename)
1166 currentSize = fileSize - currentPosition
1166 currentSize = fileSize - currentPosition
1167
1167
1168 if currentSize < neededSize:
1168 if currentSize < neededSize:
1169 if msgFlag and (msg != None):
1169 if msgFlag and (msg != None):
1170 print msg
1170 print msg
1171 return False
1171 return False
1172
1172
1173 return True
1173 return True
1174
1174
1175 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1175 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1176
1176
1177 path_empty = True
1177 path_empty = True
1178
1178
1179 dateList = []
1179 dateList = []
1180 pathList = []
1180 pathList = []
1181
1181
1182 multi_path = path.split(',')
1182 multi_path = path.split(',')
1183
1183
1184 if not walk:
1184 if not walk:
1185
1185
1186 for single_path in multi_path:
1186 for single_path in multi_path:
1187
1187
1188 if not os.path.isdir(single_path):
1188 if not os.path.isdir(single_path):
1189 continue
1189 continue
1190
1190
1191 fileList = glob.glob1(single_path, "*"+ext)
1191 fileList = glob.glob1(single_path, "*"+ext)
1192
1192
1193 if not fileList:
1193 if not fileList:
1194 continue
1194 continue
1195
1195
1196 path_empty = False
1196 path_empty = False
1197
1197
1198 fileList.sort()
1198 fileList.sort()
1199
1199
1200 for thisFile in fileList:
1200 for thisFile in fileList:
1201
1201
1202 if not os.path.isfile(os.path.join(single_path, thisFile)):
1202 if not os.path.isfile(os.path.join(single_path, thisFile)):
1203 continue
1203 continue
1204
1204
1205 if not isRadarFile(thisFile):
1205 if not isRadarFile(thisFile):
1206 continue
1206 continue
1207
1207
1208 if not isFileInDateRange(thisFile, startDate, endDate):
1208 if not isFileInDateRange(thisFile, startDate, endDate):
1209 continue
1209 continue
1210
1210
1211 thisDate = getDateFromRadarFile(thisFile)
1211 thisDate = getDateFromRadarFile(thisFile)
1212
1212
1213 if thisDate in dateList:
1213 if thisDate in dateList:
1214 continue
1214 continue
1215
1215
1216 dateList.append(thisDate)
1216 dateList.append(thisDate)
1217 pathList.append(single_path)
1217 pathList.append(single_path)
1218
1218
1219 else:
1219 else:
1220 for single_path in multi_path:
1220 for single_path in multi_path:
1221
1221
1222 if not os.path.isdir(single_path):
1222 if not os.path.isdir(single_path):
1223 continue
1223 continue
1224
1224
1225 dirList = []
1225 dirList = []
1226
1226
1227 for thisPath in os.listdir(single_path):
1227 for thisPath in os.listdir(single_path):
1228
1228
1229 if not os.path.isdir(os.path.join(single_path,thisPath)):
1229 if not os.path.isdir(os.path.join(single_path,thisPath)):
1230 continue
1230 continue
1231
1231
1232 if not isRadarFolder(thisPath):
1232 if not isRadarFolder(thisPath):
1233 continue
1233 continue
1234
1234
1235 if not isFolderInDateRange(thisPath, startDate, endDate):
1235 if not isFolderInDateRange(thisPath, startDate, endDate):
1236 continue
1236 continue
1237
1237
1238 dirList.append(thisPath)
1238 dirList.append(thisPath)
1239
1239
1240 if not dirList:
1240 if not dirList:
1241 continue
1241 continue
1242
1242
1243 dirList.sort()
1243 dirList.sort()
1244
1244
1245 for thisDir in dirList:
1245 for thisDir in dirList:
1246
1246
1247 datapath = os.path.join(single_path, thisDir, expLabel)
1247 datapath = os.path.join(single_path, thisDir, expLabel)
1248 fileList = glob.glob1(datapath, "*"+ext)
1248 fileList = glob.glob1(datapath, "*"+ext)
1249
1249
1250 if not fileList:
1250 if not fileList:
1251 continue
1251 continue
1252
1252
1253 path_empty = False
1253 path_empty = False
1254
1254
1255 thisDate = getDateFromRadarFolder(thisDir)
1255 thisDate = getDateFromRadarFolder(thisDir)
1256
1256
1257 pathList.append(datapath)
1257 pathList.append(datapath)
1258 dateList.append(thisDate)
1258 dateList.append(thisDate)
1259
1259
1260 dateList.sort()
1260 dateList.sort()
1261
1261
1262 if walk:
1262 if walk:
1263 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1263 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1264 else:
1264 else:
1265 pattern_path = multi_path[0]
1265 pattern_path = multi_path[0]
1266
1266
1267 if path_empty:
1267 if path_empty:
1268 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1268 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1269 else:
1269 else:
1270 if not dateList:
1270 if not dateList:
1271 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1271 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1272
1272
1273 if include_path:
1273 if include_path:
1274 return dateList, pathList
1274 return dateList, pathList
1275
1275
1276 return dateList
1276 return dateList
1277
1277
1278 def setup(self,
1278 def setup(self,
1279 path=None,
1279 path=None,
1280 startDate=None,
1280 startDate=None,
1281 endDate=None,
1281 endDate=None,
1282 startTime=datetime.time(0,0,0),
1282 startTime=datetime.time(0,0,0),
1283 endTime=datetime.time(23,59,59),
1283 endTime=datetime.time(23,59,59),
1284 set=None,
1284 set=None,
1285 expLabel = "",
1285 expLabel = "",
1286 ext = None,
1286 ext = None,
1287 online = False,
1287 online = False,
1288 delay = 60,
1288 delay = 60,
1289 walk = True,
1289 walk = True,
1290 getblock = False,
1290 getblock = False,
1291 nTxs = 1,
1291 nTxs = 1,
1292 realtime=False,
1292 realtime=False,
1293 blocksize=None,
1293 blocksize=None,
1294 blocktime=None):
1294 blocktime=None):
1295
1295
1296 if path == None:
1296 if path == None:
1297 raise ValueError, "[Reading] The path is not valid"
1297 raise ValueError, "[Reading] The path is not valid"
1298
1298
1299
1299
1300 if ext == None:
1300 if ext == None:
1301 ext = self.ext
1301 ext = self.ext
1302
1302
1303 self.path = path
1303 self.path = path
1304 self.startDate = startDate
1304 self.startDate = startDate
1305 self.endDate = endDate
1305 self.endDate = endDate
1306 self.startTime = startTime
1306 self.startTime = startTime
1307 self.endTime = endTime
1307 self.endTime = endTime
1308 self.set = set
1308 self.set = set
1309 self.expLabel = expLabel
1309 self.expLabel = expLabel
1310 self.ext = ext
1310 self.ext = ext
1311 self.online = online
1311 self.online = online
1312 self.delay = delay
1312 self.delay = delay
1313 self.walk = walk
1313 self.walk = walk
1314 self.getblock = getblock
1314 self.getblock = getblock
1315 self.nTxs = nTxs
1315 self.nTxs = nTxs
1316 self.realtime = realtime
1316 self.realtime = realtime
1317 self.blocksize = blocksize
1317 self.blocksize = blocksize
1318 self.blocktime = blocktime
1318 self.blocktime = blocktime
1319
1319
1320
1320
1321 if self.firstTime is True:
1321 if self.firstTime is True:
1322 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1322 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1323 startTime=startTime, endTime=endTime,
1323 startTime=startTime, endTime=endTime,
1324 set=set, expLabel=expLabel, ext=ext,
1324 set=set, expLabel=expLabel, ext=ext,
1325 walk=walk)
1325 walk=walk)
1326 filenameList = filenameList[:-1]
1326 filenameList = filenameList[:-1]
1327
1327
1328 if pathList is not None and filenameList is not None and online:
1328 if pathList is not None and filenameList is not None and online:
1329 self.onlineWithDate = True
1329 self.onlineWithDate = True
1330 online = False
1330 online = False
1331 self.fileIndex = -1
1331 self.fileIndex = -1
1332 self.pathList = pathList
1332 self.pathList = pathList
1333 self.filenameList = filenameList
1333 self.filenameList = filenameList
1334 file_name = os.path.basename(filenameList[-1])
1334 file_name = os.path.basename(filenameList[-1])
1335 basename, ext = os.path.splitext(file_name)
1335 basename, ext = os.path.splitext(file_name)
1336 last_set = int(basename[-3:])
1336 last_set = int(basename[-3:])
1337
1337
1338 if online:
1338 if online:
1339 print "[Reading] Searching files in online mode..."
1339 print "[Reading] Searching files in online mode..."
1340
1340
1341 for nTries in range(self.nTries):
1341 for nTries in range(self.nTries):
1342 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path,
1342 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path,
1343 expLabel=expLabel,
1343 expLabel=expLabel,
1344 ext=ext,
1344 ext=ext,
1345 walk=walk,
1345 walk=walk,
1346 startDate=startDate,
1346 startDate=startDate,
1347 startTime=startTime,
1347 startTime=startTime,
1348 set=set)
1348 set=set)
1349
1349
1350 if fullpath:
1350 if fullpath:
1351 break
1351 break
1352 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1352 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1353 sleep( self.delay )
1353 sleep( self.delay )
1354
1354
1355 if not(fullpath):
1355 if not(fullpath):
1356 print "[Reading] There 'isn't any valid file in %s" % path
1356 print "[Reading] There 'isn't any valid file in %s" % path
1357 return
1357 return
1358
1358
1359 self.year = year
1359 self.year = year
1360 self.doy = doy
1360 self.doy = doy
1361 self.set = set - 1
1361 self.set = set - 1
1362 self.path = path
1362 self.path = path
1363 self.foldercounter = foldercounter
1363 self.foldercounter = foldercounter
1364 last_set = None
1364 last_set = None
1365
1365
1366 else:
1366 else:
1367 print "[Reading] Searching files in offline mode ..."
1367 print "[Reading] Searching files in offline mode ..."
1368 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1368 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1369 startTime=startTime, endTime=endTime,
1369 startTime=startTime, endTime=endTime,
1370 set=set, expLabel=expLabel, ext=ext,
1370 set=set, expLabel=expLabel, ext=ext,
1371 walk=walk)
1371 walk=walk)
1372
1372
1373 if not(pathList):
1373 if not(pathList):
1374 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1374 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1375 # datetime.datetime.combine(startDate,startTime).ctime(),
1375 # datetime.datetime.combine(startDate,startTime).ctime(),
1376 # datetime.datetime.combine(endDate,endTime).ctime())
1376 # datetime.datetime.combine(endDate,endTime).ctime())
1377
1377
1378 # sys.exit(-1)
1378 # sys.exit(-1)
1379
1379
1380 self.fileIndex = -1
1380 self.fileIndex = -1
1381 self.pathList = []
1381 self.pathList = []
1382 self.filenameList = []
1382 self.filenameList = []
1383 return
1383 return
1384
1384
1385 self.fileIndex = -1
1385 self.fileIndex = -1
1386 self.pathList = pathList
1386 self.pathList = pathList
1387 self.filenameList = filenameList
1387 self.filenameList = filenameList
1388 file_name = os.path.basename(filenameList[-1])
1388 file_name = os.path.basename(filenameList[-1])
1389 basename, ext = os.path.splitext(file_name)
1389 basename, ext = os.path.splitext(file_name)
1390 last_set = int(basename[-3:])
1390 last_set = int(basename[-3:])
1391
1391
1392
1392
1393 self.online = online
1393 self.online = online
1394 self.realtime = realtime
1394 self.realtime = realtime
1395 self.delay = delay
1395 self.delay = delay
1396 ext = ext.lower()
1396 ext = ext.lower()
1397 self.ext = ext
1397 self.ext = ext
1398 self.getByBlock = getblock
1398 self.getByBlock = getblock
1399 self.nTxs = nTxs
1399 self.nTxs = nTxs
1400 self.startTime = startTime
1400 self.startTime = startTime
1401 self.endTime = endTime
1401 self.endTime = endTime
1402
1402
1403
1403
1404 #Added-----------------
1404 #Added-----------------
1405 self.selBlocksize = blocksize
1405 self.selBlocksize = blocksize
1406 self.selBlocktime = blocktime
1406 self.selBlocktime = blocktime
1407
1407
1408
1408
1409 if not(self.setNextFile()):
1409 if not(self.setNextFile()):
1410 if (startDate!=None) and (endDate!=None):
1410 if (startDate!=None) and (endDate!=None):
1411 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1411 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1412 elif startDate != None:
1412 elif startDate != None:
1413 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1413 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1414 else:
1414 else:
1415 print "[Reading] No files"
1415 print "[Reading] No files"
1416
1416
1417 self.fileIndex = -1
1417 self.fileIndex = -1
1418 self.pathList = []
1418 self.pathList = []
1419 self.filenameList = []
1419 self.filenameList = []
1420 return
1420 return
1421
1421
1422 # self.getBasicHeader()
1422 # self.getBasicHeader()
1423
1423
1424 if last_set != None:
1424 if last_set != None:
1425 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1425 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1426 return
1426 return
1427
1427
1428 def getBasicHeader(self):
1428 def getBasicHeader(self):
1429
1429
1430 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1430 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1431
1431
1432 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1432 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1433
1433
1434 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1434 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1435
1435
1436 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1436 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1437
1437
1438 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1438 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1439
1439
1440 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1440 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1441
1441
1442 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1442 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1443
1443
1444 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1444 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1445
1445
1446
1446
1447 def getFirstHeader(self):
1447 def getFirstHeader(self):
1448
1448
1449 raise NotImplementedError
1449 raise NotImplementedError
1450
1450
1451 def getData(self):
1451 def getData(self):
1452
1452
1453 raise NotImplementedError
1453 raise NotImplementedError
1454
1454
1455 def hasNotDataInBuffer(self):
1455 def hasNotDataInBuffer(self):
1456
1456
1457 raise NotImplementedError
1457 raise NotImplementedError
1458
1458
1459 def readBlock(self):
1459 def readBlock(self):
1460
1460
1461 raise NotImplementedError
1461 raise NotImplementedError
1462
1462
1463 def isEndProcess(self):
1463 def isEndProcess(self):
1464
1464
1465 return self.flagNoMoreFiles
1465 return self.flagNoMoreFiles
1466
1466
1467 def printReadBlocks(self):
1467 def printReadBlocks(self):
1468
1468
1469 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1469 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1470
1470
1471 def printTotalBlocks(self):
1471 def printTotalBlocks(self):
1472
1472
1473 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1473 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1474
1474
1475 def printNumberOfBlock(self):
1475 def printNumberOfBlock(self):
1476
1476
1477 if self.flagIsNewBlock:
1477 if self.flagIsNewBlock:
1478 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1478 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1479 self.processingHeaderObj.dataBlocksPerFile,
1479 self.processingHeaderObj.dataBlocksPerFile,
1480 self.dataOut.datatime.ctime())
1480 self.dataOut.datatime.ctime())
1481
1481
1482 def printInfo(self):
1482 def printInfo(self):
1483
1483
1484 if self.__printInfo == False:
1484 if self.__printInfo == False:
1485 return
1485 return
1486
1486
1487 self.basicHeaderObj.printInfo()
1487 self.basicHeaderObj.printInfo()
1488 self.systemHeaderObj.printInfo()
1488 self.systemHeaderObj.printInfo()
1489 self.radarControllerHeaderObj.printInfo()
1489 self.radarControllerHeaderObj.printInfo()
1490 self.processingHeaderObj.printInfo()
1490 self.processingHeaderObj.printInfo()
1491
1491
1492 self.__printInfo = False
1492 self.__printInfo = False
1493
1493
1494
1495 def run(self,
1494 def run(self,
1496 path=None,
1495 path=None,
1497 startDate=None,
1496 startDate=None,
1498 endDate=None,
1497 endDate=None,
1499 startTime=datetime.time(0,0,0),
1498 startTime=datetime.time(0,0,0),
1500 endTime=datetime.time(23,59,59),
1499 endTime=datetime.time(23,59,59),
1501 set=None,
1500 set=None,
1502 expLabel = "",
1501 expLabel = "",
1503 ext = None,
1502 ext = None,
1504 online = False,
1503 online = False,
1505 delay = 60,
1504 delay = 60,
1506 walk = True,
1505 walk = True,
1507 getblock = False,
1506 getblock = False,
1508 nTxs = 1,
1507 nTxs = 1,
1509 realtime=False,
1508 realtime=False,
1510 blocksize=None,
1509 blocksize=None,
1511 blocktime=None,
1510 blocktime=None,
1512 queue=None,
1511 queue=None,
1513 skip=None,
1512 skip=None,
1514 cursor=None,
1513 cursor=None,
1515 warnings=True,
1514 warnings=True,
1516 server=None,
1515 server=None,
1517 verbose=True, **kwargs):
1516 verbose=True, **kwargs):
1518
1517
1519 if not(self.isConfig):
1518 if not(self.isConfig):
1520 <<<<<<< HEAD
1521 # self.dataOut = dataOut
1519 # self.dataOut = dataOut
1522 self.setup( path=path,
1520 self.setup( path=path,
1523 startDate=startDate,
1521 startDate=startDate,
1524 endDate=endDate,
1522 endDate=endDate,
1525 startTime=startTime,
1523 startTime=startTime,
1526 endTime=endTime,
1524 endTime=endTime,
1527 set=set,
1525 set=set,
1528 expLabel=expLabel,
1526 expLabel=expLabel,
1529 ext=ext,
1527 ext=ext,
1530 online=online,
1528 online=online,
1531 delay=delay,
1529 delay=delay,
1532 walk=walk,
1530 walk=walk,
1533 getblock=getblock,
1531 getblock=getblock,
1534 nTxs=nTxs,
1532 nTxs=nTxs,
1535 realtime=realtime,
1533 realtime=realtime,
1536 blocksize=blocksize,
1534 blocksize=blocksize,
1537 blocktime=blocktime,
1535 blocktime=blocktime,
1538 queue=queue,
1536 queue=queue,
1539 skip=skip,
1537 skip=skip,
1540 cursor=cursor,
1538 cursor=cursor,
1541 warnings=warnings,
1539 warnings=warnings,
1542 server=server,
1540 server=server,
1543 verbose=verbose)
1541 verbose=verbose, **kwargs)
1544 =======
1545
1546 # self.dataOut = dataOut
1547 self.setup(**kwargs)
1548 >>>>>>> online_data_hour
1549 self.isConfig = True
1542 self.isConfig = True
1550 if server is None:
1543 if server is None:
1551 self.getData()
1544 self.getData()
1552 else:
1545 else:
1553 self.getFromServer()
1546 self.getFromServer()
1554
1547
1555 class JRODataWriter(JRODataIO):
1548 class JRODataWriter(JRODataIO):
1556
1549
1557 """
1550 """
1558 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1551 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1559 de los datos siempre se realiza por bloques.
1552 de los datos siempre se realiza por bloques.
1560 """
1553 """
1561
1554
1562 blockIndex = 0
1555 blockIndex = 0
1563
1556
1564 path = None
1557 path = None
1565
1558
1566 setFile = None
1559 setFile = None
1567
1560
1568 profilesPerBlock = None
1561 profilesPerBlock = None
1569
1562
1570 blocksPerFile = None
1563 blocksPerFile = None
1571
1564
1572 nWriteBlocks = 0
1565 nWriteBlocks = 0
1573
1566
1574 fileDate = None
1567 fileDate = None
1575
1568
1576 def __init__(self, dataOut=None):
1569 def __init__(self, dataOut=None):
1577 raise NotImplementedError
1570 raise NotImplementedError
1578
1571
1579
1572
1580 def hasAllDataInBuffer(self):
1573 def hasAllDataInBuffer(self):
1581 raise NotImplementedError
1574 raise NotImplementedError
1582
1575
1583
1576
1584 def setBlockDimension(self):
1577 def setBlockDimension(self):
1585 raise NotImplementedError
1578 raise NotImplementedError
1586
1579
1587
1580
1588 def writeBlock(self):
1581 def writeBlock(self):
1589 raise NotImplementedError
1582 raise NotImplementedError
1590
1583
1591
1584
1592 def putData(self):
1585 def putData(self):
1593 raise NotImplementedError
1586 raise NotImplementedError
1594
1587
1595
1588
1596 def getProcessFlags(self):
1589 def getProcessFlags(self):
1597
1590
1598 processFlags = 0
1591 processFlags = 0
1599
1592
1600 dtype_index = get_dtype_index(self.dtype)
1593 dtype_index = get_dtype_index(self.dtype)
1601 procflag_dtype = get_procflag_dtype(dtype_index)
1594 procflag_dtype = get_procflag_dtype(dtype_index)
1602
1595
1603 processFlags += procflag_dtype
1596 processFlags += procflag_dtype
1604
1597
1605 if self.dataOut.flagDecodeData:
1598 if self.dataOut.flagDecodeData:
1606 processFlags += PROCFLAG.DECODE_DATA
1599 processFlags += PROCFLAG.DECODE_DATA
1607
1600
1608 if self.dataOut.flagDeflipData:
1601 if self.dataOut.flagDeflipData:
1609 processFlags += PROCFLAG.DEFLIP_DATA
1602 processFlags += PROCFLAG.DEFLIP_DATA
1610
1603
1611 if self.dataOut.code is not None:
1604 if self.dataOut.code is not None:
1612 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1605 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1613
1606
1614 if self.dataOut.nCohInt > 1:
1607 if self.dataOut.nCohInt > 1:
1615 processFlags += PROCFLAG.COHERENT_INTEGRATION
1608 processFlags += PROCFLAG.COHERENT_INTEGRATION
1616
1609
1617 if self.dataOut.type == "Spectra":
1610 if self.dataOut.type == "Spectra":
1618 if self.dataOut.nIncohInt > 1:
1611 if self.dataOut.nIncohInt > 1:
1619 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1612 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1620
1613
1621 if self.dataOut.data_dc is not None:
1614 if self.dataOut.data_dc is not None:
1622 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1615 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1623
1616
1624 if self.dataOut.flagShiftFFT:
1617 if self.dataOut.flagShiftFFT:
1625 processFlags += PROCFLAG.SHIFT_FFT_DATA
1618 processFlags += PROCFLAG.SHIFT_FFT_DATA
1626
1619
1627 return processFlags
1620 return processFlags
1628
1621
1629 def setBasicHeader(self):
1622 def setBasicHeader(self):
1630
1623
1631 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1624 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1632 self.basicHeaderObj.version = self.versionFile
1625 self.basicHeaderObj.version = self.versionFile
1633 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1626 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1634
1627
1635 utc = numpy.floor(self.dataOut.utctime)
1628 utc = numpy.floor(self.dataOut.utctime)
1636 milisecond = (self.dataOut.utctime - utc)* 1000.0
1629 milisecond = (self.dataOut.utctime - utc)* 1000.0
1637
1630
1638 self.basicHeaderObj.utc = utc
1631 self.basicHeaderObj.utc = utc
1639 self.basicHeaderObj.miliSecond = milisecond
1632 self.basicHeaderObj.miliSecond = milisecond
1640 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1633 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1641 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1634 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1642 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1635 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1643
1636
1644 def setFirstHeader(self):
1637 def setFirstHeader(self):
1645 """
1638 """
1646 Obtiene una copia del First Header
1639 Obtiene una copia del First Header
1647
1640
1648 Affected:
1641 Affected:
1649
1642
1650 self.basicHeaderObj
1643 self.basicHeaderObj
1651 self.systemHeaderObj
1644 self.systemHeaderObj
1652 self.radarControllerHeaderObj
1645 self.radarControllerHeaderObj
1653 self.processingHeaderObj self.
1646 self.processingHeaderObj self.
1654
1647
1655 Return:
1648 Return:
1656 None
1649 None
1657 """
1650 """
1658
1651
1659 raise NotImplementedError
1652 raise NotImplementedError
1660
1653
1661 def __writeFirstHeader(self):
1654 def __writeFirstHeader(self):
1662 """
1655 """
1663 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1656 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1664
1657
1665 Affected:
1658 Affected:
1666 __dataType
1659 __dataType
1667
1660
1668 Return:
1661 Return:
1669 None
1662 None
1670 """
1663 """
1671 <<<<<<< HEAD
1672
1673 # CALCULAR PARAMETROS
1674
1675 =======
1676
1664
1677 # CALCULAR PARAMETROS
1665 # CALCULAR PARAMETROS
1678
1666
1679 >>>>>>> online_data_hour
1680 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1667 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1681 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1668 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1682
1669
1683 self.basicHeaderObj.write(self.fp)
1670 self.basicHeaderObj.write(self.fp)
1684 self.systemHeaderObj.write(self.fp)
1671 self.systemHeaderObj.write(self.fp)
1685 self.radarControllerHeaderObj.write(self.fp)
1672 self.radarControllerHeaderObj.write(self.fp)
1686 self.processingHeaderObj.write(self.fp)
1673 self.processingHeaderObj.write(self.fp)
1687
1674
1688 def __setNewBlock(self):
1675 def __setNewBlock(self):
1689 """
1676 """
1690 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1677 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1691
1678
1692 Return:
1679 Return:
1693 0 : si no pudo escribir nada
1680 0 : si no pudo escribir nada
1694 1 : Si escribio el Basic el First Header
1681 1 : Si escribio el Basic el First Header
1695 """
1682 """
1696 if self.fp == None:
1683 if self.fp == None:
1697 self.setNextFile()
1684 self.setNextFile()
1698
1685
1699 if self.flagIsNewFile:
1686 if self.flagIsNewFile:
1700 return 1
1687 return 1
1701
1688
1702 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1689 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1703 self.basicHeaderObj.write(self.fp)
1690 self.basicHeaderObj.write(self.fp)
1704 return 1
1691 return 1
1705
1692
1706 if not( self.setNextFile() ):
1693 if not( self.setNextFile() ):
1707 return 0
1694 return 0
1708
1695
1709 return 1
1696 return 1
1710
1697
1711
1698
1712 def writeNextBlock(self):
1699 def writeNextBlock(self):
1713 """
1700 """
1714 Selecciona el bloque siguiente de datos y los escribe en un file
1701 Selecciona el bloque siguiente de datos y los escribe en un file
1715
1702
1716 Return:
1703 Return:
1717 0 : Si no hizo pudo escribir el bloque de datos
1704 0 : Si no hizo pudo escribir el bloque de datos
1718 1 : Si no pudo escribir el bloque de datos
1705 1 : Si no pudo escribir el bloque de datos
1719 """
1706 """
1720 if not( self.__setNewBlock() ):
1707 if not( self.__setNewBlock() ):
1721 return 0
1708 return 0
1722
1709
1723 self.writeBlock()
1710 self.writeBlock()
1724
1711
1725 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1712 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1726 self.processingHeaderObj.dataBlocksPerFile)
1713 self.processingHeaderObj.dataBlocksPerFile)
1727
1714
1728 return 1
1715 return 1
1729
1716
1730 def setNextFile(self):
1717 def setNextFile(self):
1731 """
1718 """
1732 Determina el siguiente file que sera escrito
1719 Determina el siguiente file que sera escrito
1733
1720
1734 Affected:
1721 Affected:
1735 self.filename
1722 self.filename
1736 self.subfolder
1723 self.subfolder
1737 self.fp
1724 self.fp
1738 self.setFile
1725 self.setFile
1739 self.flagIsNewFile
1726 self.flagIsNewFile
1740
1727
1741 Return:
1728 Return:
1742 0 : Si el archivo no puede ser escrito
1729 0 : Si el archivo no puede ser escrito
1743 1 : Si el archivo esta listo para ser escrito
1730 1 : Si el archivo esta listo para ser escrito
1744 """
1731 """
1745 ext = self.ext
1732 ext = self.ext
1746 path = self.path
1733 path = self.path
1747
1734
1748 if self.fp != None:
1735 if self.fp != None:
1749 self.fp.close()
1736 self.fp.close()
1750
1737
1751 timeTuple = time.localtime( self.dataOut.utctime)
1738 timeTuple = time.localtime( self.dataOut.utctime)
1752 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1739 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1753
1740
1754 fullpath = os.path.join( path, subfolder )
1741 fullpath = os.path.join( path, subfolder )
1755 setFile = self.setFile
1742 setFile = self.setFile
1756
1743
1757 if not( os.path.exists(fullpath) ):
1744 if not( os.path.exists(fullpath) ):
1758 os.mkdir(fullpath)
1745 os.mkdir(fullpath)
1759 setFile = -1 #inicializo mi contador de seteo
1746 setFile = -1 #inicializo mi contador de seteo
1760 else:
1747 else:
1761 filesList = os.listdir( fullpath )
1748 filesList = os.listdir( fullpath )
1762 if len( filesList ) > 0:
1749 if len( filesList ) > 0:
1763 filesList = sorted( filesList, key=str.lower )
1750 filesList = sorted( filesList, key=str.lower )
1764 filen = filesList[-1]
1751 filen = filesList[-1]
1765 # el filename debera tener el siguiente formato
1752 # el filename debera tener el siguiente formato
1766 # 0 1234 567 89A BCDE (hex)
1753 # 0 1234 567 89A BCDE (hex)
1767 # x YYYY DDD SSS .ext
1754 # x YYYY DDD SSS .ext
1768 if isNumber( filen[8:11] ):
1755 if isNumber( filen[8:11] ):
1769 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1756 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1770 else:
1757 else:
1771 setFile = -1
1758 setFile = -1
1772 else:
1759 else:
1773 setFile = -1 #inicializo mi contador de seteo
1760 setFile = -1 #inicializo mi contador de seteo
1774
1761
1775 setFile += 1
1762 setFile += 1
1776
1763
1777 #If this is a new day it resets some values
1764 #If this is a new day it resets some values
1778 if self.dataOut.datatime.date() > self.fileDate:
1765 if self.dataOut.datatime.date() > self.fileDate:
1779 setFile = 0
1766 setFile = 0
1780 self.nTotalBlocks = 0
1767 self.nTotalBlocks = 0
1781
1768
1782 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1769 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1783
1770
1784 filename = os.path.join( path, subfolder, filen )
1771 filename = os.path.join( path, subfolder, filen )
1785
1772
1786 fp = open( filename,'wb' )
1773 fp = open( filename,'wb' )
1787
1774
1788 self.blockIndex = 0
1775 self.blockIndex = 0
1789
1776
1790 #guardando atributos
1777 #guardando atributos
1791 self.filename = filename
1778 self.filename = filename
1792 self.subfolder = subfolder
1779 self.subfolder = subfolder
1793 self.fp = fp
1780 self.fp = fp
1794 self.setFile = setFile
1781 self.setFile = setFile
1795 self.flagIsNewFile = 1
1782 self.flagIsNewFile = 1
1796 self.fileDate = self.dataOut.datatime.date()
1783 self.fileDate = self.dataOut.datatime.date()
1797
1784
1798 self.setFirstHeader()
1785 self.setFirstHeader()
1799
1786
1800 print '[Writing] Opening file: %s'%self.filename
1787 print '[Writing] Opening file: %s'%self.filename
1801
1788
1802 self.__writeFirstHeader()
1789 self.__writeFirstHeader()
1803
1790
1804 return 1
1791 return 1
1805
1792
1806 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1793 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1807 """
1794 """
1808 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1795 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1809
1796
1810 Inputs:
1797 Inputs:
1811 path : directory where data will be saved
1798 path : directory where data will be saved
1812 profilesPerBlock : number of profiles per block
1799 profilesPerBlock : number of profiles per block
1813 set : initial file set
1800 set : initial file set
1814 datatype : An integer number that defines data type:
1801 datatype : An integer number that defines data type:
1815 0 : int8 (1 byte)
1802 0 : int8 (1 byte)
1816 1 : int16 (2 bytes)
1803 1 : int16 (2 bytes)
1817 2 : int32 (4 bytes)
1804 2 : int32 (4 bytes)
1818 3 : int64 (8 bytes)
1805 3 : int64 (8 bytes)
1819 4 : float32 (4 bytes)
1806 4 : float32 (4 bytes)
1820 5 : double64 (8 bytes)
1807 5 : double64 (8 bytes)
1821
1808
1822 Return:
1809 Return:
1823 0 : Si no realizo un buen seteo
1810 0 : Si no realizo un buen seteo
1824 1 : Si realizo un buen seteo
1811 1 : Si realizo un buen seteo
1825 """
1812 """
1826
1813
1827 if ext == None:
1814 if ext == None:
1828 ext = self.ext
1815 ext = self.ext
1829
1816
1830 self.ext = ext.lower()
1817 self.ext = ext.lower()
1831
1818
1832 self.path = path
1819 self.path = path
1833
1820
1834 if set is None:
1821 if set is None:
1835 self.setFile = -1
1822 self.setFile = -1
1836 else:
1823 else:
1837 self.setFile = set - 1
1824 self.setFile = set - 1
1838
1825
1839 self.blocksPerFile = blocksPerFile
1826 self.blocksPerFile = blocksPerFile
1840
1827
1841 self.profilesPerBlock = profilesPerBlock
1828 self.profilesPerBlock = profilesPerBlock
1842
1829
1843 self.dataOut = dataOut
1830 self.dataOut = dataOut
1844 self.fileDate = self.dataOut.datatime.date()
1831 self.fileDate = self.dataOut.datatime.date()
1845 #By default
1832 #By default
1846 self.dtype = self.dataOut.dtype
1833 self.dtype = self.dataOut.dtype
1847
1834
1848 if datatype is not None:
1835 if datatype is not None:
1849 self.dtype = get_numpy_dtype(datatype)
1836 self.dtype = get_numpy_dtype(datatype)
1850
1837
1851 if not(self.setNextFile()):
1838 if not(self.setNextFile()):
1852 print "[Writing] There isn't a next file"
1839 print "[Writing] There isn't a next file"
1853 return 0
1840 return 0
1854
1841
1855 self.setBlockDimension()
1842 self.setBlockDimension()
1856
1843
1857 return 1
1844 return 1
1858
1845
1859 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1846 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1860
1847
1861 if not(self.isConfig):
1848 if not(self.isConfig):
1862
1849
1863 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1850 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1864 self.isConfig = True
1851 self.isConfig = True
1865
1852
1866 self.putData()
1853 self.putData()
General Comments 0
You need to be logged in to leave comments. Login now