@@ -1,1360 +1,1192 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on 23/01/2012 |
|
3 | 3 | |
|
4 | 4 | @author $Author$ |
|
5 | 5 | @version $Id$ |
|
6 | @version $Id$ | |
|
6 | 7 | ''' |
|
8 | ||
|
7 | 9 | import os, sys |
|
8 | 10 | import glob |
|
9 | 11 | import time |
|
10 | 12 | import numpy |
|
11 | 13 | import fnmatch |
|
12 | 14 | import time, datetime |
|
13 | 15 | |
|
14 | 16 | path = os.path.split(os.getcwd())[0] |
|
15 | 17 | sys.path.append(path) |
|
16 | 18 | |
|
17 | 19 | from Model.JROHeader import * |
|
18 | 20 | from Model.JROData import JROData |
|
19 | 21 | |
|
20 | 22 | def checkForRealPath(path, year, doy, set, ext): |
|
21 | 23 | """ |
|
22 | 24 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, |
|
23 | 25 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar |
|
24 | 26 | el path exacto de un determinado file. |
|
25 | 27 | |
|
26 | 28 | Example : |
|
27 | 29 | nombre correcto del file es .../.../D2009307/P2009307367.ext |
|
28 | 30 | |
|
29 | 31 | Entonces la funcion prueba con las siguientes combinaciones |
|
30 | 32 | .../.../x2009307/y2009307367.ext |
|
31 | 33 | .../.../x2009307/Y2009307367.ext |
|
32 | 34 | .../.../X2009307/y2009307367.ext |
|
33 | 35 | .../.../X2009307/Y2009307367.ext |
|
34 | 36 | siendo para este caso, la ultima combinacion de letras, identica al file buscado |
|
35 | 37 | |
|
36 | 38 | Return: |
|
37 | 39 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file |
|
38 | 40 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas |
|
39 | 41 | para el filename |
|
40 | 42 | """ |
|
41 | 43 | filepath = None |
|
42 | 44 | find_flag = False |
|
43 | 45 | filename = None |
|
44 | 46 | |
|
45 | 47 | if ext.lower() == ".r": #voltage |
|
46 |
|
|
|
47 |
|
|
|
48 | header1 = "dD" | |
|
49 | header2 = "dD" | |
|
48 | 50 | elif ext.lower() == ".pdata": #spectra |
|
49 |
|
|
|
50 |
|
|
|
51 | header1 = "dD" | |
|
52 | header2 = "pP" | |
|
51 | 53 | else: |
|
52 | 54 | return None, filename |
|
53 | 55 | |
|
54 |
for dir in |
|
|
55 |
for fil in |
|
|
56 | for dir in header1: #barrido por las dos combinaciones posibles de "D" | |
|
57 | for fil in header2: #barrido por las dos combinaciones posibles de "D" | |
|
56 | 58 | doypath = "%s%04d%03d" % ( dir, year, doy ) #formo el nombre del directorio xYYYYDDD (x=d o x=D) |
|
57 | 59 | filename = "%s%04d%03d%03d%s" % ( fil, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext |
|
58 | 60 | filepath = os.path.join( path, doypath, filename ) #formo el path completo |
|
59 | 61 | if os.path.exists( filepath ): #verifico que exista |
|
60 | 62 | find_flag = True |
|
61 | 63 | break |
|
62 | 64 | if find_flag: |
|
63 | 65 | break |
|
64 | 66 | |
|
65 | 67 | if not(find_flag): |
|
66 | 68 | return None, filename |
|
67 | 69 | |
|
68 | 70 | return filepath, filename |
|
69 | 71 | |
|
70 | 72 | |
|
71 | 73 | def isNumber(str): |
|
72 | 74 | """ |
|
73 | 75 | Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero. |
|
74 | 76 | |
|
75 | 77 | Excepciones: |
|
76 | 78 | Si un determinado string no puede ser convertido a numero |
|
77 | 79 | Input: |
|
78 | 80 | str, string al cual se le analiza para determinar si convertible a un numero o no |
|
79 | 81 | |
|
80 | 82 | Return: |
|
81 | 83 | True : si el string es uno numerico |
|
82 | 84 | False : no es un string numerico |
|
83 | 85 | """ |
|
84 | 86 | try: |
|
85 | 87 | float( str ) |
|
86 | 88 | return True |
|
87 | 89 | except: |
|
88 | 90 | return False |
|
89 | 91 | |
|
90 | 92 | |
|
91 | 93 | def isThisFileinRange(filename, startUTSeconds, endUTSeconds): |
|
92 | 94 | """ |
|
93 | 95 | Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado. |
|
94 | 96 | |
|
95 | 97 | Inputs: |
|
96 | 98 | filename : nombre completo del archivo de datos en formato Jicamarca (.r) |
|
97 | 99 | |
|
98 | 100 | startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en |
|
99 | 101 | segundos contados desde 01/01/1970. |
|
100 | 102 | endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en |
|
101 | 103 | segundos contados desde 01/01/1970. |
|
102 | 104 | |
|
103 | 105 | Return: |
|
104 | 106 | Boolean : Retorna True si el archivo de datos contiene datos en el rango de |
|
105 | 107 | fecha especificado, de lo contrario retorna False. |
|
106 | 108 | |
|
107 | 109 | Excepciones: |
|
108 | 110 | Si el archivo no existe o no puede ser abierto |
|
109 | 111 | Si la cabecera no puede ser leida. |
|
110 | 112 | |
|
111 | 113 | """ |
|
112 | 114 | m_BasicHeader = BasicHeader() |
|
113 | 115 | |
|
114 | 116 | try: |
|
115 | 117 | fp = open(filename,'rb') |
|
116 | 118 | except: |
|
117 | 119 | raise IOError, "The file %s can't be opened" %(filename) |
|
118 | 120 | |
|
119 | 121 | sts = m_BasicHeader.read(fp) |
|
120 | 122 | fp.close() |
|
121 | 123 | |
|
122 | 124 | if not(sts): |
|
123 | 125 | print "Skipping the file %s because it has not a valid header" %(filename) |
|
124 | 126 | return 0 |
|
125 | 127 | |
|
126 |
if not ((startUTSeconds <= m_BasicHeader.utc) and (endUTSeconds > |
|
|
128 | if not ((startUTSeconds <= m_BasicHeader.utc) and (endUTSeconds > m_BasicHeader.utc)): | |
|
127 | 129 | return 0 |
|
128 | 130 | |
|
129 | 131 | return 1 |
|
130 | 132 | |
|
131 | 133 | |
|
132 | 134 | def getlastFileFromPath(path, ext): |
|
133 | 135 | """ |
|
134 | 136 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" |
|
135 | 137 | al final de la depuracion devuelve el ultimo file de la lista que quedo. |
|
136 | 138 | |
|
137 | 139 | Input: |
|
138 | 140 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta |
|
139 | 141 | ext : extension de los files contenidos en una carpeta |
|
140 | 142 | |
|
141 | 143 | Return: |
|
142 | 144 | El ultimo file de una determinada carpeta, no se considera el path. |
|
143 | 145 | """ |
|
144 | 146 | |
|
145 | 147 | validFilelist = [] |
|
146 | 148 | fileList = os.listdir(path) |
|
147 | 149 | |
|
148 | 150 | # 0 1234 567 89A BCDE |
|
149 |
# |
|
|
151 | # H YYYY DDD SSS .ext | |
|
150 | 152 | |
|
151 | 153 | for file in fileList: |
|
152 | 154 | try: |
|
153 | 155 | year = int(file[1:5]) |
|
154 | 156 | doy = int(file[5:8]) |
|
157 | ||
|
158 | if (os.path.splitext(file)[-1].upper() != ext.upper()) : continue | |
|
155 | 159 | except: |
|
156 | 160 | continue |
|
157 | ||
|
158 | if (os.path.splitext(file)[-1].upper() != ext.upper()) : continue | |
|
159 | 161 | |
|
160 | 162 | validFilelist.append(file) |
|
161 | 163 | |
|
162 |
if |
|
|
164 | if validFilelist: | |
|
163 | 165 | validFilelist = sorted( validFilelist, key=str.lower ) |
|
164 | 166 | return validFilelist[-1] |
|
165 | 167 | |
|
166 | 168 | return None |
|
167 | 169 | |
|
168 | 170 | |
|
169 | 171 | class DataReader(): |
|
170 | 172 | |
|
171 | 173 | def __init__(self): |
|
172 | 174 | pass |
|
173 | 175 | |
|
174 | 176 | |
|
175 | 177 | class DataWriter(): |
|
176 | 178 | |
|
177 | 179 | def __init__(self): |
|
178 | 180 | pass |
|
179 | 181 | |
|
180 | 182 | |
|
181 | 183 | class JRODataReader(DataReader): |
|
182 | 184 | |
|
183 | 185 | """ |
|
184 | 186 | Esta clase es usada como la clase padre de las clases DataReader, |
|
185 | 187 | contiene todos lo metodos necesarios para leer datos desde archivos en formato |
|
186 | 188 | jicamarca o pdata (.r o .pdata). La lectura de los datos siempre se realiza por bloques. Los datos |
|
187 | 189 | leidos son array de 3 dimensiones: |
|
188 | perfiles*alturas*canales | |
|
190 | ||
|
191 | Voltajes - perfiles * alturas * canales | |
|
192 | ||
|
193 | Spectra - pares * alturas * perfiles (Self Spectra) | |
|
194 | canales * alturas * perfiles (Cross Spectra) | |
|
195 | canales * alturas (DC Channels) | |
|
189 | 196 | |
|
190 |
y son almacenados en |
|
|
197 | y son almacenados en su buffer respectivo. | |
|
191 | 198 | |
|
192 | 199 | Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader, |
|
193 | 200 | RadarControllerHeader y DataObj. Los tres primeros se usan para almacenar informacion de la |
|
194 | 201 | cabecera de datos (metadata), y el cuarto (DataObj) para obtener y almacenar los datos desde |
|
195 |
el |
|
|
202 | el buffer cada vez que se ejecute el metodo "getData". | |
|
196 | 203 | """ |
|
197 | 204 | |
|
198 | 205 | m_BasicHeader = BasicHeader() |
|
199 | 206 | |
|
200 | 207 | m_SystemHeader = SystemHeader() |
|
201 | 208 | |
|
202 | 209 | m_RadarControllerHeader = RadarControllerHeader() |
|
203 | 210 | |
|
204 | 211 | m_ProcessingHeader = ProcessingHeader() |
|
205 | 212 | |
|
206 | m_DataObj = None | |
|
207 | ||
|
208 | 213 | online = 0 |
|
209 | 214 | |
|
210 | startDateTime = None | |
|
211 | ||
|
212 | endDateTime = None | |
|
213 | ||
|
214 | 215 | fp = None |
|
215 | 216 | |
|
216 | 217 | fileSizeByHeader = None |
|
217 | 218 | |
|
218 | pathList = [] | |
|
219 | ||
|
220 | 219 | filenameList = [] |
|
221 | 220 | |
|
222 | fileIndex = None | |
|
223 | ||
|
224 | 221 | filename = None |
|
225 | 222 | |
|
226 | 223 | fileSize = None |
|
227 | 224 | |
|
228 | 225 | firstHeaderSize = 0 |
|
229 | 226 | |
|
230 | 227 | basicHeaderSize = 24 |
|
231 | 228 | |
|
232 | 229 | dataType = None |
|
233 | 230 | |
|
234 | blocksize = 0 | |
|
231 | maxTimeStep = 30 | |
|
235 | 232 | |
|
236 | datablock = None | |
|
237 | ||
|
238 | datablockIndex = None | |
|
239 | ||
|
240 | pts2read = 0 | |
|
241 | ||
|
242 | #Parametros para el procesamiento en linea | |
|
243 | year = 0 | |
|
244 | ||
|
245 | doy = 0 | |
|
233 | flagNoMoreFiles = 0 | |
|
246 | 234 | |
|
247 | 235 | set = 0 |
|
248 | 236 | |
|
249 | 237 | ext = None |
|
250 | 238 | |
|
251 | 239 | path = None |
|
252 | 240 | |
|
253 | optchar = None | |
|
254 | ||
|
255 | 241 | delay = 7 #seconds |
|
256 | 242 | |
|
257 | 243 | nTries = 3 #quantity tries |
|
258 | 244 | |
|
259 | 245 | nFiles = 3 #number of files for searching |
|
260 | 246 | |
|
261 | pts2read = 0 | |
|
262 | ||
|
263 | blocksize = 0 | |
|
264 | ||
|
265 | utc = 0 | |
|
266 | ||
|
267 | 247 | nBlocks = 0 |
|
268 | 248 | |
|
249 | flagIsNewFile = 1 | |
|
250 | ||
|
251 | ippSeconds = 0 | |
|
252 | ||
|
253 | flagResetProcessing = 0 | |
|
254 | ||
|
255 | flagIsNewBlock = 0 | |
|
256 | ||
|
257 | nReadBlocks = 0 | |
|
258 | ||
|
259 | blocksize = 0 | |
|
260 | ||
|
261 | datablockIndex = 9999 | |
|
262 | ||
|
269 | 263 | #speed of light |
|
270 | 264 | c = 3E8 |
|
271 | 265 | |
|
272 | def __init__(self, m_DataObj=None): | |
|
273 | """ | |
|
274 | Inicializador de la clase JRODataReader para la lectura de datos. | |
|
275 | ||
|
276 | Input: | |
|
277 | m_DataObj : Objeto de la clase JROData. Este objeto sera utilizado para | |
|
278 | almacenar un perfil de datos cada vez que se haga un requerimiento | |
|
279 | (getData). El perfil sera obtenido a partir del buffer de datos, | |
|
280 | si el buffer esta vacio se hara un nuevo proceso de lectura de un | |
|
281 | bloque de datos. | |
|
282 | Si este parametro no es pasado se creara uno internamente. | |
|
283 | ||
|
284 | Variables afectadas: | |
|
285 | self.m_DataObj | |
|
286 | self.m_BasicHeader | |
|
287 | self.m_SystemHeader | |
|
288 | self.m_RadarControllerHeader | |
|
289 | self.m_ProcessingHeader | |
|
290 | ||
|
291 | Return: | |
|
292 | None | |
|
293 | """ | |
|
294 | ||
|
295 | raise ValueError, "This class can't be instanced" | |
|
296 | ||
|
297 | 266 | |
|
298 | 267 | def __rdSystemHeader(self, fp=None): |
|
299 | 268 | |
|
300 | 269 | if fp == None: |
|
301 | 270 | fp = self.fp |
|
302 | 271 | |
|
303 | 272 | self.m_SystemHeader.read(fp) |
|
304 | 273 | |
|
305 | 274 | |
|
306 | 275 | def __rdRadarControllerHeader(self, fp=None): |
|
307 | 276 | if fp == None: |
|
308 | 277 | fp = self.fp |
|
309 | 278 | |
|
310 | 279 | self.m_RadarControllerHeader.read(fp) |
|
311 | 280 | |
|
312 | 281 | |
|
313 | 282 | def __rdProcessingHeader(self, fp=None): |
|
314 | 283 | if fp == None: |
|
315 | 284 | fp = self.fp |
|
316 | 285 | |
|
317 | 286 | self.m_ProcessingHeader.read(fp) |
|
318 | 287 | |
|
319 | 288 | |
|
320 | 289 | def __rdBasicHeader(self, fp=None): |
|
321 | 290 | |
|
322 | 291 | if fp == None: |
|
323 | 292 | fp = self.fp |
|
324 | 293 | |
|
325 | 294 | self.m_BasicHeader.read(fp) |
|
326 | 295 | |
|
296 | ||
|
327 | 297 | def __readFirstHeader(self): |
|
328 | 298 | """ |
|
329 | 299 | Lectura del First Header, es decir el Basic Header y el Long Header |
|
330 | 300 | |
|
331 | 301 | Affected: |
|
332 | 302 | self.m_BasicHeader |
|
333 | 303 | self.m_SystemHeader |
|
334 | 304 | self.m_RadarControllerHeader |
|
335 | 305 | self.m_ProcessingHeader |
|
336 | 306 | self.firstHeaderSize |
|
337 | 307 | self.heights |
|
338 | 308 | self.dataType |
|
339 | 309 | self.fileSizeByHeader |
|
340 | 310 | self.ippSeconds |
|
341 | self.nChannels | |
|
342 | self.nPairs | |
|
343 | self.pts2read_SelfSpectra | |
|
344 | self.pts2read_CrossSpectra | |
|
345 | 311 | |
|
346 | 312 | Return: |
|
347 | 313 | None |
|
348 | 314 | """ |
|
349 | 315 | self.__rdBasicHeader() |
|
350 | 316 | self.__rdSystemHeader() |
|
351 | 317 | self.__rdRadarControllerHeader() |
|
352 | 318 | self.__rdProcessingHeader() |
|
353 | 319 | self.firstHeaderSize = self.m_BasicHeader.size |
|
354 | 320 | |
|
355 | 321 | data_type=int(numpy.log2((self.m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) |
|
356 | 322 | if data_type == 0: |
|
357 | 323 | tmp = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
358 | 324 | |
|
359 | 325 | elif data_type == 1: |
|
360 | 326 | tmp = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
361 | 327 | |
|
362 | 328 | elif data_type == 2: |
|
363 | 329 | tmp = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
364 | 330 | |
|
365 | 331 | elif data_type == 3: |
|
366 | 332 | tmp = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
367 | 333 | |
|
368 | 334 | elif data_type == 4: |
|
369 | 335 | tmp = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
370 | 336 | |
|
371 | 337 | elif data_type == 5: |
|
372 | 338 | tmp = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
373 | 339 | |
|
374 | 340 | else: |
|
375 | 341 | raise ValueError, 'Data type was not defined' |
|
376 | 342 | |
|
377 | 343 | xi = self.m_ProcessingHeader.firstHeight |
|
378 | 344 | step = self.m_ProcessingHeader.deltaHeight |
|
379 | 345 | xf = xi + self.m_ProcessingHeader.numHeights*step |
|
380 | 346 | |
|
381 | 347 | self.heights = numpy.arange(xi, xf, step) |
|
382 | 348 | self.dataType = tmp |
|
383 | 349 | self.fileSizeByHeader = self.m_ProcessingHeader.dataBlocksPerFile * self.m_ProcessingHeader.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.m_ProcessingHeader.dataBlocksPerFile - 1) |
|
384 | 350 | self.ippSeconds = 2 * 1000 * self.m_RadarControllerHeader.ipp / self.c |
|
385 | 351 | |
|
386 | 352 | self.getBlockDimension() |
|
387 | 353 | |
|
354 | ||
|
388 | 355 | def __setNextFileOnline(self): |
|
389 | 356 | """ |
|
390 | 357 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si |
|
391 | 358 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files |
|
392 | 359 | siguientes. |
|
393 | 360 | |
|
394 | 361 | Affected: |
|
395 | 362 | self.flagIsNewFile |
|
396 | 363 | self.filename |
|
397 | 364 | self.fileSize |
|
398 | 365 | self.fp |
|
399 | 366 | self.set |
|
400 | 367 | self.flagNoMoreFiles |
|
401 | 368 | |
|
402 | 369 | Return: |
|
403 | 370 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado |
|
404 | 371 | 1 : si el file fue abierto con exito y esta listo a ser leido |
|
405 | 372 | |
|
406 | 373 | Excepciones: |
|
407 | 374 | Si un determinado file no puede ser abierto |
|
408 | 375 | """ |
|
409 | 376 | countFiles = 0 |
|
410 | 377 | countTries = 0 |
|
411 | 378 | |
|
412 | 379 | notFirstTime_flag = False |
|
413 | 380 | fileOk_flag = False |
|
414 | changeDir_flag = False | |
|
415 | 381 | self.flagIsNewFile = 0 |
|
416 | 382 | |
|
417 | 383 | while( True ): #este loop permite llevar la cuenta de intentos, de files y carpetas, |
|
418 | 384 | #si no encuentra alguno sale del bucle |
|
419 | 385 | countFiles += 1 |
|
420 | 386 | |
|
421 | 387 | if countFiles > (self.nFiles + 1): |
|
422 | 388 | break |
|
423 | 389 | |
|
424 | 390 | self.set += 1 |
|
425 | 391 | |
|
426 | 392 | if countFiles > self.nFiles: #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta |
|
427 | 393 | self.set = 0 |
|
428 | 394 | self.doy += 1 |
|
429 | changeDir_flag = True | |
|
430 | ||
|
395 | ||
|
431 | 396 | file = None |
|
432 | 397 | filename = None |
|
433 | 398 | fileOk_flag = False |
|
434 | 399 | |
|
435 | 400 | #busca el 1er file disponible |
|
436 | 401 | file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) |
|
402 | if file: | |
|
403 | if self.__verifyFile(file, False): | |
|
404 | fileOk_flag = True | |
|
437 | 405 | |
|
438 |
if |
|
|
406 | if not(fileOk_flag): | |
|
439 | 407 | if notFirstTime_flag: #si no es la primera vez que busca el file entonces no espera y busca for el siguiente file |
|
440 |
print "\t |
|
|
408 | print "\tSearching next \"%s\" file ..." % ( filename ) | |
|
441 | 409 | continue |
|
442 | 410 | else: #si es la primera vez que busca el file entonces espera self.nTries veces hasta encontrarlo o no |
|
443 | 411 | for nTries in range( self.nTries ): |
|
444 |
print "\t |
|
|
412 | print "\tWaiting %0.2f sec for new \"%s\" file, try %03d ..." % ( self.delay, filename, nTries+1 ) | |
|
445 | 413 | time.sleep( self.delay ) |
|
446 | 414 | |
|
447 | 415 | file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) |
|
448 |
if file |
|
|
449 |
|
|
|
450 |
|
|
|
416 | if file: | |
|
417 | if self.__verifyFile(file): | |
|
418 | fileOk_flag = True | |
|
419 | break | |
|
451 | 420 | |
|
452 | 421 | if not( fileOk_flag ): #no encontro ningun file valido a leer despues de haber esperado alguno |
|
453 | 422 | notFirstTime_flag = True |
|
454 | 423 | continue |
|
455 | 424 | |
|
456 | #una vez que se obtuvo el 1er file valido se procede a checkear si si tamanho es suficiente para empezar a leerlo | |
|
457 | currentSize = os.path.getsize( file ) | |
|
458 | neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize | |
|
459 | ||
|
460 | #si el tamanho es suficiente entonces deja de buscar | |
|
461 | if currentSize > neededSize: | |
|
462 | fileOk_flag = True | |
|
425 | if fileOk_flag: | |
|
463 | 426 | break |
|
464 | ||
|
465 | fileOk_flag = False | |
|
466 | #si el file no tiene el tamanho necesario se espera una cierta cantidad de tiempo | |
|
467 | #por una cierta cantidad de veces hasta que el contenido del file sea valido | |
|
468 | if changeDir_flag: #si al buscar un file cambie de directorio ya no espero y sigo con el siguiente file | |
|
469 | print "\tsearching next \"%s\" file ..." % filename | |
|
470 | changeDir_flag = False | |
|
471 | continue | |
|
472 | ||
|
473 | for nTries in range( self.nTries ): | |
|
474 | print "\twaiting for the First Header block of \"%s\" file, try %03d ..." % ( filename, nTries+1 ) | |
|
475 | time.sleep( self.delay ) | |
|
476 | 427 | |
|
477 | currentSize = os.path.getsize( file ) | |
|
478 | neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize | |
|
479 | ||
|
480 | if currentSize > neededSize: | |
|
481 | fileOk_flag = True | |
|
482 | break | |
|
483 | ||
|
484 | if fileOk_flag: #si encontro un file valido sale del bucle y deja de buscar | |
|
485 | break | |
|
486 | ||
|
487 | 428 | print "Skipping the file \"%s\" due to this files is empty" % filename |
|
488 | 429 | countFiles = 0 |
|
489 | 430 | |
|
490 | 431 | if fileOk_flag: |
|
491 | 432 | self.fileSize = os.path.getsize( file ) |
|
492 | 433 | self.filename = file |
|
493 | 434 | self.flagIsNewFile = 1 |
|
494 | 435 | if self.fp != None: self.fp.close() |
|
495 | 436 | self.fp = open(file) |
|
496 |
self. |
|
|
437 | self.flagNoMoreFiles = 0 | |
|
497 | 438 | print 'Setting the file: %s' % file |
|
498 | 439 | else: |
|
499 | 440 | self.fileSize = 0 |
|
500 | 441 | self.filename = None |
|
501 | 442 | self.fp = None |
|
502 |
self. |
|
|
443 | self.flagNoMoreFiles = 1 | |
|
503 | 444 | print 'No more Files' |
|
504 | 445 | |
|
505 | 446 | return fileOk_flag |
|
506 | 447 | |
|
507 | 448 | |
|
508 | 449 | def __setNextFileOffline(self): |
|
509 | 450 | """ |
|
510 | 451 | Busca el siguiente file dentro de un folder que tenga suficiente data para ser leida |
|
511 | 452 | |
|
512 | 453 | Affected: |
|
513 | 454 | self.flagIsNewFile |
|
514 | 455 | self.fileIndex |
|
515 | 456 | self.filename |
|
516 | 457 | self.fileSize |
|
517 | 458 | self.fp |
|
518 | 459 | |
|
519 | 460 | Return: |
|
520 | 461 | 0 : si un determinado file no puede ser abierto |
|
521 | 462 | 1 : si el file fue abierto con exito |
|
522 | 463 | |
|
523 | 464 | Excepciones: |
|
524 | 465 | Si un determinado file no puede ser abierto |
|
525 | 466 | """ |
|
526 | 467 | idFile = self.fileIndex |
|
527 | 468 | while(True): |
|
528 | 469 | |
|
529 | 470 | idFile += 1 |
|
530 | 471 | |
|
531 | 472 | if not(idFile < len(self.filenameList)): |
|
532 | 473 | self.flagNoMoreFiles = 1 |
|
533 | 474 | print 'No more Files' |
|
534 | 475 | return 0 |
|
535 | 476 | |
|
536 | 477 | filename = self.filenameList[idFile] |
|
537 | 478 | fileSize = os.path.getsize(filename) |
|
538 | 479 | |
|
539 | 480 | try: |
|
540 | 481 | fp = open(filename,'rb') |
|
541 | 482 | except: |
|
542 | 483 | raise IOError, "The file %s can't be opened" %filename |
|
543 | 484 | |
|
544 | 485 | currentSize = fileSize - fp.tell() |
|
545 | 486 | neededSize = self.m_ProcessingHeader.blockSize + self.firstHeaderSize |
|
546 | 487 | |
|
547 | 488 | if (currentSize < neededSize): |
|
548 | 489 | print "Skipping the file %s due to it hasn't enough data" %filename |
|
549 | 490 | continue |
|
550 | 491 | |
|
551 | 492 | break |
|
552 | 493 | |
|
553 | 494 | self.flagIsNewFile = 1 |
|
554 | 495 | self.fileIndex = idFile |
|
555 | 496 | self.filename = filename |
|
556 | 497 | self.fileSize = fileSize |
|
557 | 498 | self.fp = fp |
|
558 | 499 | |
|
559 | 500 | print 'Setting the file: %s'%self.filename |
|
560 | 501 | |
|
561 | 502 | return 1 |
|
562 | 503 | |
|
563 | 504 | |
|
564 |
def |
|
|
505 | def setNextFile(self): | |
|
565 | 506 | """ |
|
566 | 507 | Determina el siguiente file a leer y si hay uno disponible lee el First Header |
|
567 | 508 | |
|
568 | 509 | Affected: |
|
569 | 510 | self.m_BasicHeader |
|
570 | 511 | self.m_SystemHeader |
|
571 | 512 | self.m_RadarControllerHeader |
|
572 | 513 | self.m_ProcessingHeader |
|
573 | 514 | self.firstHeaderSize |
|
574 | 515 | |
|
575 | 516 | Return: |
|
576 | 517 | 0 : Si no hay files disponibles |
|
577 | 518 | 1 : Si hay mas files disponibles |
|
578 | 519 | """ |
|
579 | 520 | if self.fp != None: |
|
580 | 521 | self.fp.close() |
|
581 | 522 | |
|
582 | 523 | if self.online: |
|
583 | 524 | newFile = self.__setNextFileOnline() |
|
584 | 525 | else: |
|
585 | 526 | newFile = self.__setNextFileOffline() |
|
586 | 527 | |
|
587 |
if self. |
|
|
528 | if self.flagNoMoreFiles: | |
|
588 | 529 | sys.exit(0) |
|
589 | 530 | |
|
590 | 531 | if not(newFile): |
|
591 | 532 | return 0 |
|
592 | 533 | |
|
593 | 534 | self.__readFirstHeader() |
|
594 | 535 | self.nBlocks = 0 |
|
595 | 536 | return 1 |
|
596 | 537 | |
|
597 | 538 | |
|
598 | 539 | def __setNewBlock(self): |
|
599 | 540 | """ |
|
600 | 541 | Lee el Basic Header y posiciona le file pointer en la posicion inicial del bloque a leer |
|
601 | 542 | |
|
602 | 543 | Affected: |
|
603 | 544 | self.m_BasicHeader |
|
604 | 545 | self.flagNoContinuousBlock |
|
605 | 546 | self.ns |
|
606 | 547 | |
|
607 | 548 | Return: |
|
608 | 549 | 0 : Si el file no tiene un Basic Header que pueda ser leido |
|
609 | 550 | 1 : Si se pudo leer el Basic Header |
|
610 | 551 | """ |
|
611 | 552 | if self.fp == None: |
|
612 | 553 | return 0 |
|
613 | 554 | |
|
614 | 555 | if self.flagIsNewFile: |
|
615 | 556 | return 1 |
|
616 | 557 | |
|
617 | 558 | currentSize = self.fileSize - self.fp.tell() |
|
618 | 559 | neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize |
|
619 | 560 | |
|
620 | 561 | #If there is enough data setting new data block |
|
621 | 562 | if ( currentSize >= neededSize ): |
|
622 | 563 | self.__rdBasicHeader() |
|
623 | 564 | return 1 |
|
624 | 565 | |
|
625 | 566 | #si es OnLine y ademas aun no se han leido un bloque completo entonces se espera por uno valido |
|
626 | 567 | elif (self.nBlocks != self.m_ProcessingHeader.dataBlocksPerFile) and self.online: |
|
627 | 568 | for nTries in range( self.nTries ): |
|
628 | 569 | |
|
629 | 570 | fpointer = self.fp.tell() |
|
630 | 571 | self.fp.close() |
|
631 | 572 | |
|
632 | print "\tWaiting for the next block, try %03d ..." % (nTries+1) | |
|
573 | print "\tWaiting %0.2f sec for the next block, try %03d ..." % (self.delay, nTries+1) | |
|
633 | 574 | time.sleep( self.delay ) |
|
634 | 575 | |
|
635 | 576 | self.fp = open( self.filename, 'rb' ) |
|
636 | 577 | self.fp.seek( fpointer ) |
|
637 | 578 | |
|
638 | 579 | self.fileSize = os.path.getsize( self.filename ) |
|
639 | 580 | currentSize = self.fileSize - self.fp.tell() |
|
640 | 581 | neededSize = self.m_ProcessingHeader.blockSize + self.basicHeaderSize |
|
641 | 582 | |
|
642 | 583 | if ( currentSize >= neededSize ): |
|
643 | self.rdBasicHeader() | |
|
584 | self.__rdBasicHeader() | |
|
644 | 585 | return 1 |
|
645 | 586 | |
|
646 | 587 | #Setting new file |
|
647 |
if not( self. |
|
|
588 | if not( self.setNextFile() ): | |
|
648 | 589 | return 0 |
|
649 | 590 | |
|
650 | 591 | deltaTime = self.m_BasicHeader.utc - self.lastUTTime # check this |
|
651 | 592 | |
|
652 | 593 | self.flagResetProcessing = 0 |
|
653 | 594 | |
|
654 | 595 | if deltaTime > self.maxTimeStep: |
|
655 | 596 | self.flagResetProcessing = 1 |
|
656 | 597 | #self.nReadBlocks = 0 |
|
657 | 598 | |
|
658 | 599 | return 1 |
|
659 | ||
|
660 | def getBlockDimension(self): | |
|
661 | ||
|
662 | raise ValueError, "No implemented" | |
|
663 | ||
|
664 | def hasNotDataInBuffer(self): | |
|
665 | ||
|
666 | raise ValueError, "Not implemented" | |
|
667 | ||
|
668 | 600 | |
|
669 | def __searchFilesOnLine( self, path, startDateTime=None, endDateTime=None, expLabel = "", ext = ".pdata" ): | |
|
601 | ||
|
602 | def __searchFilesOnLine(self, path, startDateTime=None, endDateTime=None, expLabel = "", ext = None): | |
|
670 | 603 | """ |
|
671 | 604 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y |
|
672 | 605 | devuelve el archivo encontrado ademas de otros datos. |
|
673 | 606 | |
|
674 | 607 | Input: |
|
675 | 608 | path : carpeta donde estan contenidos los files que contiene data |
|
676 | 609 | startDateTime : punto especifico en el tiempo del cual se requiere la data |
|
677 | 610 | ext : extension de los files |
|
678 | 611 | |
|
679 | 612 | Return: |
|
680 | 613 | year : el anho |
|
681 | 614 | doy : el numero de dia del anho |
|
682 | 615 | set : el set del archivo |
|
683 | 616 | filename : el ultimo file de una determinada carpeta |
|
684 | 617 | directory : eL directorio donde esta el file encontrado |
|
685 | 618 | """ |
|
686 | 619 | |
|
687 | print "Searching files ..." | |
|
688 | ||
|
620 | pathList = os.listdir(path) | |
|
621 | ||
|
622 | if not(pathList): | |
|
623 | return None, None, None, None, None | |
|
624 | ||
|
689 | 625 | dirList = [] |
|
690 | 626 | for thisPath in os.listdir(path): |
|
691 | 627 | if os.path.isdir(os.path.join(path,thisPath)): |
|
692 | 628 | dirList.append(thisPath) |
|
693 | 629 | |
|
630 | if not(dirList): | |
|
631 | return None, None, None, None, None | |
|
632 | ||
|
694 | 633 | pathList = dirList |
|
695 | 634 | |
|
696 | 635 | if startDateTime != None: |
|
697 | 636 | pathList = [] |
|
698 | 637 | thisDateTime = startDateTime |
|
699 | 638 | if endDateTime == None: endDateTime = startDateTime |
|
700 | 639 | |
|
701 | 640 | while(thisDateTime <= endDateTime): |
|
702 | 641 | year = thisDateTime.timetuple().tm_year |
|
703 | 642 | doy = thisDateTime.timetuple().tm_yday |
|
704 | 643 | |
|
705 | 644 | match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy)) |
|
706 | 645 | if len(match) == 0: |
|
707 | 646 | thisDateTime += datetime.timedelta(1) |
|
708 | 647 | continue |
|
709 | 648 | |
|
710 | 649 | pathList.append(os.path.join(path,match[0], expLabel)) |
|
711 | 650 | thisDateTime += datetime.timedelta(1) |
|
712 | ||
|
713 | 651 | |
|
652 | if not(pathList): | |
|
653 | return None, None, None, None, None | |
|
654 | ||
|
714 | 655 | directory = pathList[-1] |
|
715 | 656 | |
|
716 | if directory == None: | |
|
717 | return 0, 0, 0, None, None | |
|
718 | ||
|
719 | ||
|
720 | nTries = 0 | |
|
721 | while True: | |
|
722 | if nTries >= self.nTries: | |
|
723 | break | |
|
724 | filename = getlastFileFromPath(directory, ext ) | |
|
725 | ||
|
726 | if filename != None: | |
|
727 | break | |
|
728 | ||
|
729 | print "Waiting %d seconds for the first file with extension (%s) on %s..." %(self.delay, ext, directory) | |
|
730 | time.sleep(self.delay) | |
|
731 | nTries += 1 | |
|
732 | ||
|
733 | if filename == None: | |
|
657 | filename = getlastFileFromPath(directory, ext) | |
|
658 | ||
|
659 | if not(filename): | |
|
734 | 660 | return None, None, None, None, None |
|
735 | 661 | |
|
736 | 662 | if not(self.__verifyFile(os.path.join(directory, filename))): |
|
737 | print "The file %s hasn't enough data" % filename | |
|
738 | 663 | return None, None, None, None, None |
|
739 | ||
|
664 | ||
|
740 | 665 | year = int( filename[1:5] ) |
|
741 | 666 | doy = int( filename[5:8] ) |
|
742 | 667 | set = int( filename[8:11] ) |
|
743 | 668 | |
|
744 | 669 | return directory, filename, year, doy, set |
|
745 | 670 | |
|
746 | 671 | |
|
747 | 672 | def __searchFilesOffLine(self, path, startDateTime, endDateTime, set=None, expLabel = "", ext = ".r"): |
|
748 | 673 | """ |
|
749 | 674 | Realiza una busqueda de los archivos que coincidan con los parametros |
|
750 | 675 | especificados y se encuentren ubicados en el path indicado. Para realizar una busqueda |
|
751 | 676 | correcta la estructura de directorios debe ser la siguiente: |
|
752 | 677 | |
|
753 | 678 | ...path/D[yyyy][ddd]/expLabel/D[yyyy][ddd][sss].ext |
|
754 | 679 | |
|
755 | 680 | [yyyy]: anio |
|
756 | 681 | [ddd] : dia del anio |
|
757 | 682 | [sss] : set del archivo |
|
758 | 683 | |
|
759 | 684 | Inputs: |
|
760 | 685 | path : Directorio de datos donde se realizara la busqueda. Todos los |
|
761 | 686 | ficheros que concidan con el criterio de busqueda seran |
|
762 | 687 | almacenados en una lista y luego retornados. |
|
763 | 688 | startDateTime : Fecha inicial. Rechaza todos los archivos donde |
|
764 | 689 | file end time < startDateTime (obejto datetime.datetime) |
|
765 | 690 | |
|
766 | 691 | endDateTime : Fecha final. Rechaza todos los archivos donde |
|
767 | 692 | file start time > endDateTime (obejto datetime.datetime) |
|
768 | 693 | |
|
769 | 694 | set : Set del primer archivo a leer. Por defecto None |
|
770 | 695 | |
|
771 | 696 | expLabel : Nombre del subdirectorio de datos. Por defecto "" |
|
772 | 697 | |
|
773 | 698 | ext : Extension de los archivos a leer. Por defecto .r |
|
774 | 699 | |
|
775 | 700 | Return: |
|
776 | 701 | |
|
777 | 702 | (pathList, filenameList) |
|
778 | 703 | |
|
779 | 704 | pathList : Lista de directorios donde se encontraron archivos dentro |
|
780 | 705 | de los parametros especificados |
|
781 | 706 | filenameList : Lista de archivos (ruta completa) que coincidieron con los |
|
782 | 707 | parametros especificados. |
|
783 | 708 | |
|
784 | 709 | Variables afectadas: |
|
785 | 710 | |
|
786 | 711 | self.filenameList: Lista de archivos (ruta completa) que la clase utiliza |
|
787 | 712 | como fuente para leer los bloque de datos, si se termina |
|
788 | 713 | de leer todos los bloques de datos de un determinado |
|
789 | 714 | archivo se pasa al siguiente archivo de la lista. |
|
790 | 715 | |
|
791 | 716 | Excepciones: |
|
792 | 717 | |
|
793 | 718 | """ |
|
794 | 719 | |
|
795 | 720 | print "Searching files ..." |
|
796 | 721 | |
|
797 | 722 | dirList = [] |
|
798 | 723 | for thisPath in os.listdir(path): |
|
799 | 724 | if os.path.isdir(os.path.join(path,thisPath)): |
|
800 | 725 | dirList.append(thisPath) |
|
801 | 726 | |
|
727 | if not(dirList): | |
|
728 | return None, None | |
|
729 | ||
|
802 | 730 | pathList = [] |
|
803 | 731 | |
|
804 | 732 | thisDateTime = startDateTime |
|
805 | 733 | |
|
806 | 734 | while(thisDateTime <= endDateTime): |
|
807 | 735 | year = thisDateTime.timetuple().tm_year |
|
808 | 736 | doy = thisDateTime.timetuple().tm_yday |
|
809 | 737 | |
|
810 | 738 | match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy)) |
|
811 | 739 | if len(match) == 0: |
|
812 | 740 | thisDateTime += datetime.timedelta(1) |
|
813 | 741 | continue |
|
814 | 742 | |
|
815 | 743 | pathList.append(os.path.join(path,match[0],expLabel)) |
|
816 | 744 | thisDateTime += datetime.timedelta(1) |
|
817 | 745 | |
|
818 | 746 | startUtSeconds = time.mktime(startDateTime.timetuple()) |
|
819 | 747 | endUtSeconds = time.mktime(endDateTime.timetuple()) |
|
820 | 748 | |
|
821 | 749 | filenameList = [] |
|
822 | 750 | for thisPath in pathList: |
|
823 | 751 | fileList = glob.glob1(thisPath, "*%s" %ext) |
|
824 | 752 | fileList.sort() |
|
825 | 753 | for file in fileList: |
|
826 | 754 | filename = os.path.join(thisPath,file) |
|
827 | 755 | if isThisFileinRange(filename, startUtSeconds, endUtSeconds): |
|
828 | 756 | filenameList.append(filename) |
|
829 | 757 | |
|
758 | if not(filenameList): | |
|
759 | return None, None | |
|
760 | ||
|
830 | 761 | self.filenameList = filenameList |
|
831 | 762 | |
|
832 | 763 | return pathList, filenameList |
|
833 | 764 | |
|
834 | 765 | |
|
835 |
def __verifyFile( |
|
|
766 | def __verifyFile(self, filename, msgFlag=True): | |
|
836 | 767 | """ |
|
837 |
Verifica que el filename tenga data valida, para ello leo el |
|
|
838 | del file, si no es un file valido espera una cierta cantidad de tiempo a que | |
|
839 | lo sea, si transcurrido el tiempo no logra validar el file entonces el metodo | |
|
840 | devuelve 0 caso contrario devuelve 1 | |
|
768 | Verifica que el filename tenga data valida, para ello leo el FirstHeader del file | |
|
841 | 769 | |
|
842 | Affected: | |
|
843 | m_BasicHeader | |
|
844 | ||
|
845 | 770 | Return: |
|
846 | 771 | 0 : file no valido para ser leido |
|
847 | 772 | 1 : file valido para ser leido |
|
848 | 773 | """ |
|
849 | 774 | m_BasicHeader = BasicHeader() |
|
775 | m_SystemHeader = SystemHeader() | |
|
776 | m_RadarControllerHeader = RadarControllerHeader() | |
|
777 | m_ProcessingHeader = ProcessingHeader() | |
|
778 | flagFileOK = False | |
|
850 | 779 | |
|
851 | for nTries in range( self.nTries+1 ): | |
|
852 | try: | |
|
853 | fp = open( filename,'rb' ) #lectura binaria | |
|
854 |
|
|
|
855 |
|
|
|
856 |
|
|
|
857 |
|
|
|
858 |
|
|
|
859 | except: | |
|
860 | print "The file %s is empty" % filename | |
|
861 | ||
|
862 | fp.close() | |
|
863 | ||
|
864 | if m_BasicHeader.size > 24: | |
|
865 | break | |
|
866 | ||
|
867 | if nTries >= self.nTries: #si ya espero la cantidad de veces necesarias entonces ya no vuelve a esperar | |
|
868 | break | |
|
869 | ||
|
870 | print '\twaiting for new block of file %s: try %02d' % ( filename, nTries ) | |
|
871 | time.sleep( self.delay ) | |
|
872 | ||
|
873 | if m_BasicHeader.size <= 24: | |
|
780 | try: | |
|
781 | fp = open( filename,'rb' ) #lectura binaria | |
|
782 | except: | |
|
783 | if msgFlag: | |
|
784 | print "The file %s can't be opened" % (filename) | |
|
785 | ||
|
786 | try: | |
|
787 | m_BasicHeader.read(fp) | |
|
788 | m_SystemHeader.read(fp) | |
|
789 | m_RadarControllerHeader.read(fp) | |
|
790 | m_ProcessingHeader.read(fp) | |
|
791 | data_type = int(numpy.log2((m_ProcessingHeader.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR)) | |
|
792 | if m_BasicHeader.size > self.basicHeaderSize: | |
|
793 | flagFileOK = True | |
|
794 | except: | |
|
795 | if msgFlag: | |
|
796 | print "The file %s is empty or it hasn't enough data" % filename | |
|
797 | ||
|
798 | fp.close() | |
|
799 | ||
|
800 | if not(flagFileOK): | |
|
874 | 801 | return 0 |
|
875 | 802 | |
|
876 | 803 | return 1 |
|
877 | 804 | |
|
878 | 805 | |
|
879 | 806 | def setup(self, path, startDateTime, endDateTime=None, set=0, expLabel = "", ext = None, online = 0): |
|
880 | 807 | """ |
|
881 |
setup configura los parametros de lectura de la clase |
|
|
808 | setup configura los parametros de lectura de la clase DataReader. | |
|
882 | 809 | |
|
883 | 810 | Si el modo de lectura es offline, primero se realiza una busqueda de todos los archivos |
|
884 | 811 | que coincidan con los parametros especificados; esta lista de archivos son almacenados en |
|
885 | 812 | self.filenameList. |
|
886 | 813 | |
|
887 | 814 | Input: |
|
888 | 815 | path : Directorios donde se ubican los datos a leer. Dentro de este |
|
889 | 816 | directorio deberia de estar subdirectorios de la forma: |
|
890 | 817 | |
|
891 | 818 | path/D[yyyy][ddd]/expLabel/P[yyyy][ddd][sss][ext] |
|
892 | 819 | |
|
893 | 820 | startDateTime : Fecha inicial. Rechaza todos los archivos donde |
|
894 | 821 | file end time < startDatetime (obejto datetime.datetime) |
|
895 | 822 | |
|
896 | 823 | endDateTime : Fecha final. Si no es None, rechaza todos los archivos donde |
|
897 | 824 | file end time < startDatetime (obejto datetime.datetime) |
|
898 | 825 | |
|
899 | 826 | set : Set del primer archivo a leer. Por defecto None |
|
900 | 827 | |
|
901 | 828 | expLabel : Nombre del subdirectorio de datos. Por defecto "" |
|
902 | 829 | |
|
903 | 830 | ext : Extension de los archivos a leer. Por defecto .r |
|
904 | 831 | |
|
905 | 832 | online : Si es == a 0 entonces busca files que cumplan con las condiciones dadas |
|
906 | 833 | |
|
907 | 834 | Return: |
|
908 | 835 | 0 : Si no encuentra files que cumplan con las condiciones dadas |
|
909 | 836 | 1 : Si encuentra files que cumplan con las condiciones dadas |
|
910 | 837 | |
|
911 | 838 | Affected: |
|
912 | 839 | self.startUTCSeconds |
|
913 | 840 | self.endUTCSeconds |
|
914 | 841 | self.startYear |
|
915 | 842 | self.endYear |
|
916 | 843 | self.startDoy |
|
917 | 844 | self.endDoy |
|
918 | 845 | self.pathList |
|
919 | 846 | self.filenameList |
|
920 | 847 | self.online |
|
921 | 848 | """ |
|
922 | 849 | |
|
923 | 850 | if ext == None: |
|
924 | 851 | ext = self.ext |
|
925 | 852 | |
|
926 | 853 | if online: |
|
927 | ||
|
854 | print "Searching files ..." | |
|
928 | 855 | doypath, file, year, doy, set = self.__searchFilesOnLine(path, startDateTime, endDateTime, expLabel, ext) |
|
856 | ||
|
857 | if not(doypath): | |
|
858 | for nTries in range( self.nTries ): | |
|
859 | print '\twaiting %0.2f sec for valid file in %s: try %02d ...' % (self.delay, path, nTries+1) | |
|
860 | time.sleep( self.delay ) | |
|
861 | doypath, file, year, doy, set = self.__searchFilesOnLine(path, startDateTime, endDateTime, expLabel, ext) | |
|
862 | if doypath: | |
|
863 | break | |
|
929 | 864 | |
|
930 |
if doypath |
|
|
865 | if not(doypath): | |
|
866 | print "There 'isn't valied files in %s" % path | |
|
931 | 867 | return 0 |
|
932 | 868 | |
|
933 | 869 | self.year = year |
|
934 | 870 | self.doy = doy |
|
935 | 871 | self.set = set - 1 |
|
936 | 872 | self.path = path |
|
937 | 873 | |
|
938 | else: | |
|
874 | else: # offline | |
|
939 | 875 | pathList, filenameList = self.__searchFilesOffLine(path, startDateTime, endDateTime, set, expLabel, ext) |
|
876 | if not(pathList): | |
|
877 | print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime()) | |
|
878 | return 0 | |
|
879 | ||
|
940 | 880 | self.fileIndex = -1 |
|
941 | 881 | self.pathList = pathList |
|
942 | 882 | self.filenameList = filenameList |
|
943 | 883 | |
|
944 | 884 | self.online = online |
|
945 | 885 | self.ext = ext |
|
946 | 886 | |
|
947 | 887 | ext = ext.lower() |
|
948 | 888 | |
|
949 |
if not( self. |
|
|
889 | if not( self.setNextFile() ): | |
|
950 | 890 | if (startDateTime != None) and (endDateTime != None): |
|
951 | 891 | print "No files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime()) |
|
952 | 892 | elif startDateTime != None: |
|
953 | 893 | print "No files in : %s" % startDateTime.ctime() |
|
954 | 894 | else: |
|
955 | 895 | print "No files" |
|
956 | 896 | return 0 |
|
957 | 897 | |
|
958 | 898 | if startDateTime != None: |
|
959 | 899 | self.startUTCSeconds = time.mktime(startDateTime.timetuple()) |
|
960 | 900 | self.startYear = startDateTime.timetuple().tm_year |
|
961 | 901 | self.startDoy = startDateTime.timetuple().tm_yday |
|
962 | 902 | |
|
963 | 903 | if endDateTime != None: |
|
964 | 904 | self.endUTCSeconds = time.mktime(endDateTime.timetuple()) |
|
965 | 905 | self.endYear = endDateTime.timetuple().tm_year |
|
966 | 906 | self.endDoy = endDateTime.timetuple().tm_yday |
|
967 | 907 | #call fillHeaderValues() - to Data Object |
|
968 | 908 | |
|
969 | 909 | self.m_DataObj.m_BasicHeader = self.m_BasicHeader.copy() |
|
970 | 910 | self.m_DataObj.m_ProcessingHeader = self.m_ProcessingHeader.copy() |
|
971 | 911 | self.m_DataObj.m_RadarControllerHeader = self.m_RadarControllerHeader.copy() |
|
972 | 912 | self.m_DataObj.m_SystemHeader = self.m_SystemHeader.copy() |
|
973 | 913 | self.m_DataObj.dataType = self.dataType |
|
974 | 914 | |
|
975 | 915 | return 1 |
|
976 | 916 | |
|
977 | 917 | |
|
978 |
def readNextBlock( |
|
|
918 | def readNextBlock(self): | |
|
979 | 919 | """ |
|
980 | 920 | Establece un nuevo bloque de datos a leer y los lee, si es que no existiese |
|
981 | 921 | mas bloques disponibles en el archivo actual salta al siguiente. |
|
982 | 922 | |
|
983 | 923 | Affected: |
|
984 | 924 | self.lastUTTime |
|
985 | 925 | |
|
986 | 926 | Return: None |
|
987 | 927 | """ |
|
988 | 928 | if not(self.__setNewBlock()): |
|
989 | 929 | return 0 |
|
990 | 930 | |
|
991 | self.readBlock() | |
|
931 | if not(self.readBlock()): | |
|
932 | return 0 | |
|
992 | 933 | |
|
993 | 934 | self.lastUTTime = self.m_BasicHeader.utc |
|
994 | 935 | |
|
995 | 936 | return 1 |
|
996 | 937 | |
|
997 | def readBlock(self): | |
|
998 | ||
|
999 | raise ValueError, "This method has not been implemented" | |
|
1000 | ||
|
1001 | def getData( self ): | |
|
1002 | """ | |
|
1003 | getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage" | |
|
1004 | con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de | |
|
1005 | lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock" | |
|
1006 | ||
|
1007 | Ademas incrementa el contador del buffer en 1. | |
|
1008 | ||
|
1009 | Return: | |
|
1010 | data : retorna un perfil de voltages (alturas * canales) copiados desde el | |
|
1011 | buffer. Si no hay mas archivos a leer retorna None. | |
|
1012 | ||
|
1013 | Variables afectadas: | |
|
1014 | self.m_DataObj | |
|
1015 | self.datablockIndex | |
|
1016 | ||
|
1017 | Affected: | |
|
1018 | self.m_DataObj | |
|
1019 | self.datablockIndex | |
|
1020 | self.flagNoContinuousBlock | |
|
1021 | self.flagNewBlock | |
|
1022 | """ | |
|
1023 | ||
|
1024 | raise ValueError, "This method has not been implemented" | |
|
1025 | ||
|
1026 | 938 | |
|
1027 | 939 | class JRODataWriter(DataWriter): |
|
940 | ||
|
1028 | 941 | """ |
|
1029 | 942 | Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura |
|
1030 | 943 | de los datos siempre se realiza por bloques. |
|
1031 | 944 | """ |
|
1032 | ||
|
1033 | def __init__(self): | |
|
1034 | """ | |
|
1035 | Inicializador de la clase VoltageWriter para la escritura de datos de espectros. | |
|
1036 | ||
|
1037 | Affected: | |
|
1038 | self.m_DataObj | |
|
1039 | self.m_BasicHeader | |
|
1040 | self.m_SystemHeader | |
|
1041 | self.m_RadarControllerHeader | |
|
1042 | self.m_ProcessingHeader | |
|
1043 | 945 | |
|
1044 |
|
|
|
1045 | """ | |
|
1046 | if m_Voltage == None: | |
|
1047 | m_Voltage = Voltage() | |
|
1048 | ||
|
1049 | self.m_DataObj = m_Voltage | |
|
1050 | ||
|
1051 | self.path = None | |
|
1052 | ||
|
1053 | self.fp = None | |
|
1054 | ||
|
1055 | self.format = None | |
|
946 | fp = None | |
|
1056 | 947 | |
|
1057 |
|
|
|
1058 | ||
|
1059 | self.setFile = None | |
|
1060 | ||
|
1061 | self.flagIsNewFile = 1 | |
|
1062 | ||
|
1063 | self.dataType = None | |
|
1064 | ||
|
1065 | self.datablock = None | |
|
1066 | ||
|
1067 | self.datablockIndex = 0 | |
|
1068 | ||
|
1069 | self.ext = None | |
|
1070 | ||
|
1071 | self.shapeBuffer = None | |
|
1072 | ||
|
1073 | self.shape_spc_Buffer = None | |
|
1074 | ||
|
1075 | self.shape_cspc_Buffer = None | |
|
1076 | ||
|
1077 | self.shape_dc_Buffer = None | |
|
1078 | ||
|
1079 | self.nWriteBlocks = 0 | |
|
1080 | ||
|
1081 | self.flagIsNewBlock = 0 | |
|
1082 | ||
|
1083 | self.noMoreFiles = 0 | |
|
1084 | ||
|
1085 | self.filename = None | |
|
1086 | ||
|
1087 | self.m_BasicHeader= BasicHeader() | |
|
948 | blocksCounter = 0 | |
|
1088 | 949 | |
|
1089 | self.m_SystemHeader = SystemHeader() | |
|
950 | flagIsNewFile = 1 | |
|
1090 | 951 | |
|
1091 | self.m_RadarControllerHeader = RadarControllerHeader() | |
|
952 | nWriteBlocks = 0 | |
|
1092 | 953 | |
|
1093 | self.m_ProcessingHeader = ProcessingHeader() | |
|
1094 | ||
|
1095 | self.data_spc = None | |
|
1096 | ||
|
1097 | self.data_cspc = None | |
|
1098 | ||
|
1099 | self.data_dc = None | |
|
1100 | ||
|
954 | flagIsNewBlock = 0 | |
|
955 | ||
|
956 | flagNoMoreFiles = 0 | |
|
1101 | 957 | |
|
958 | ||
|
1102 | 959 | def __writeFirstHeader(self): |
|
1103 | 960 | """ |
|
1104 | 961 | Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader) |
|
1105 | 962 | |
|
1106 | 963 | Affected: |
|
1107 | 964 | __dataType |
|
1108 | 965 | |
|
1109 | 966 | Return: |
|
1110 | 967 | None |
|
1111 | 968 | """ |
|
1112 | 969 | self.__writeBasicHeader() |
|
1113 | 970 | self.__wrSystemHeader() |
|
1114 | 971 | self.__wrRadarControllerHeader() |
|
1115 | 972 | self.__wrProcessingHeader() |
|
1116 | 973 | self.dataType = self.m_DataObj.dataType |
|
1117 | 974 | |
|
1118 | 975 | |
|
1119 | 976 | def __writeBasicHeader(self, fp=None): |
|
1120 | 977 | """ |
|
1121 | 978 | Escribe solo el Basic header en el file creado |
|
1122 | 979 | |
|
1123 | 980 | Return: |
|
1124 | 981 | None |
|
1125 | 982 | """ |
|
1126 | 983 | if fp == None: |
|
1127 | 984 | fp = self.fp |
|
1128 | 985 | |
|
1129 | self.m_BasicHeader.write(fp) | |
|
986 | self.m_DataObj.m_BasicHeader.write(fp) | |
|
1130 | 987 | |
|
1131 | 988 | |
|
1132 | 989 | def __wrSystemHeader(self, fp=None): |
|
1133 | 990 | """ |
|
1134 | 991 | Escribe solo el System header en el file creado |
|
1135 | 992 | |
|
1136 | 993 | Return: |
|
1137 | 994 | None |
|
1138 | 995 | """ |
|
1139 | 996 | if fp == None: |
|
1140 | 997 | fp = self.fp |
|
1141 | 998 | |
|
1142 | self.m_SystemHeader.write(fp) | |
|
999 | self.m_DataObj.m_SystemHeader.write(fp) | |
|
1143 | 1000 | |
|
1144 | 1001 | |
|
1145 | 1002 | def __wrRadarControllerHeader(self, fp=None): |
|
1146 | 1003 | """ |
|
1147 | 1004 | Escribe solo el RadarController header en el file creado |
|
1148 | 1005 | |
|
1149 | 1006 | Return: |
|
1150 | 1007 | None |
|
1151 | 1008 | """ |
|
1152 | 1009 | if fp == None: |
|
1153 | 1010 | fp = self.fp |
|
1154 | 1011 | |
|
1155 | self.m_RadarControllerHeader.write(fp) | |
|
1012 | self.m_DataObj.m_RadarControllerHeader.write(fp) | |
|
1156 | 1013 | |
|
1157 | 1014 | |
|
1158 | 1015 | def __wrProcessingHeader(self, fp=None): |
|
1159 | 1016 | """ |
|
1160 | 1017 | Escribe solo el Processing header en el file creado |
|
1161 | 1018 | |
|
1162 | 1019 | Return: |
|
1163 | 1020 | None |
|
1164 | 1021 | """ |
|
1165 | 1022 | if fp == None: |
|
1166 | 1023 | fp = self.fp |
|
1167 | 1024 | |
|
1168 | self.m_ProcessingHeader.write(fp) | |
|
1025 | self.m_DataObj.m_ProcessingHeader.write(fp) | |
|
1169 | 1026 | |
|
1170 | 1027 | |
|
1171 |
def |
|
|
1028 | def setNextFile(self): | |
|
1172 | 1029 | """ |
|
1173 | 1030 | Determina el siguiente file que sera escrito |
|
1174 | 1031 | |
|
1175 | 1032 | Affected: |
|
1176 | 1033 | self.filename |
|
1177 | 1034 | self.subfolder |
|
1178 | 1035 | self.fp |
|
1179 | 1036 | self.setFile |
|
1180 | 1037 | self.flagIsNewFile |
|
1181 | 1038 | |
|
1182 | 1039 | Return: |
|
1183 | 1040 | 0 : Si el archivo no puede ser escrito |
|
1184 | 1041 | 1 : Si el archivo esta listo para ser escrito |
|
1185 | 1042 | """ |
|
1186 | 1043 | ext = self.ext |
|
1187 | 1044 | path = self.path |
|
1188 | 1045 | |
|
1189 | 1046 | if self.fp != None: |
|
1190 | 1047 | self.fp.close() |
|
1191 | 1048 | |
|
1192 |
timeTuple = time.localtime( self.m_DataObj.m_BasicHeader.utc ) |
|
|
1049 | timeTuple = time.localtime( self.m_DataObj.m_BasicHeader.utc ) | |
|
1193 | 1050 | subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday) |
|
1194 | 1051 | |
|
1195 | 1052 | tmp = os.path.join( path, subfolder ) |
|
1196 | 1053 | if not( os.path.exists(tmp) ): |
|
1197 | 1054 | os.mkdir(tmp) |
|
1198 | 1055 | self.setFile = -1 #inicializo mi contador de seteo |
|
1199 | 1056 | else: |
|
1200 | 1057 | filesList = os.listdir( tmp ) |
|
1201 | 1058 | if len( filesList ) > 0: |
|
1202 | 1059 | filesList = sorted( filesList, key=str.lower ) |
|
1203 | 1060 | filen = filesList[-1] |
|
1204 | 1061 | # el filename debera tener el siguiente formato |
|
1205 | 1062 | # 0 1234 567 89A BCDE (hex) |
|
1206 |
# |
|
|
1063 | # x YYYY DDD SSS .ext | |
|
1207 | 1064 | if isNumber( filen[8:11] ): |
|
1208 | 1065 | self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file |
|
1209 | 1066 | else: |
|
1210 | 1067 | self.setFile = -1 |
|
1211 | 1068 | else: |
|
1212 | 1069 | self.setFile = -1 #inicializo mi contador de seteo |
|
1213 | 1070 | |
|
1214 | 1071 | setFile = self.setFile |
|
1215 | 1072 | setFile += 1 |
|
1216 | 1073 | |
|
1217 | 1074 | file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, |
|
1218 | 1075 | timeTuple.tm_year, |
|
1219 | 1076 | timeTuple.tm_yday, |
|
1220 | 1077 | setFile, |
|
1221 | 1078 | ext ) |
|
1222 | 1079 | |
|
1223 | 1080 | filename = os.path.join( path, subfolder, file ) |
|
1224 | 1081 | |
|
1225 | 1082 | fp = open( filename,'wb' ) |
|
1226 | 1083 | |
|
1227 | 1084 | self.blocksCounter = 0 |
|
1228 | 1085 | |
|
1229 | 1086 | #guardando atributos |
|
1230 | 1087 | self.filename = filename |
|
1231 | 1088 | self.subfolder = subfolder |
|
1232 | 1089 | self.fp = fp |
|
1233 | 1090 | self.setFile = setFile |
|
1234 | 1091 | self.flagIsNewFile = 1 |
|
1235 | 1092 | |
|
1236 | 1093 | print 'Writing the file: %s'%self.filename |
|
1237 | 1094 | |
|
1238 | 1095 | self.__writeFirstHeader() |
|
1239 | 1096 | |
|
1240 | 1097 | return 1 |
|
1241 | 1098 | |
|
1242 | 1099 | |
|
1243 | 1100 | def __setNewBlock(self): |
|
1244 | 1101 | """ |
|
1245 | 1102 | Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header |
|
1246 | 1103 | |
|
1247 | 1104 | Return: |
|
1248 | 1105 | 0 : si no pudo escribir nada |
|
1249 | 1106 | 1 : Si escribio el Basic el First Header |
|
1250 | 1107 | """ |
|
1251 | 1108 | if self.fp == None: |
|
1252 |
self. |
|
|
1109 | self.setNextFile() | |
|
1253 | 1110 | |
|
1254 | 1111 | if self.flagIsNewFile: |
|
1255 | 1112 | return 1 |
|
1256 | 1113 | |
|
1257 | 1114 | if self.blocksCounter < self.m_ProcessingHeader.dataBlocksPerFile: |
|
1258 | 1115 | self.__writeBasicHeader() |
|
1259 | 1116 | return 1 |
|
1260 | 1117 | |
|
1261 |
if not( self. |
|
|
1118 | if not( self.setNextFile() ): | |
|
1262 | 1119 | return 0 |
|
1263 | 1120 | |
|
1264 | 1121 | return 1 |
|
1265 | 1122 | |
|
1266 | 1123 | |
|
1267 | 1124 | def writeNextBlock(self): |
|
1268 | 1125 | """ |
|
1269 | 1126 | Selecciona el bloque siguiente de datos y los escribe en un file |
|
1270 | 1127 | |
|
1271 | 1128 | Return: |
|
1272 | 1129 | 0 : Si no hizo pudo escribir el bloque de datos |
|
1273 | 1130 | 1 : Si no pudo escribir el bloque de datos |
|
1274 | 1131 | """ |
|
1275 | 1132 | if not( self.__setNewBlock() ): |
|
1276 | 1133 | return 0 |
|
1277 | 1134 | |
|
1278 | 1135 | self.writeBlock() |
|
1279 | 1136 | |
|
1280 | 1137 | return 1 |
|
1138 | ||
|
1281 | 1139 | |
|
1282 | 1140 | def getHeader(self): |
|
1283 | 1141 | """ |
|
1284 | 1142 | Obtiene una copia del First Header |
|
1285 | 1143 | |
|
1286 | 1144 | Affected: |
|
1287 | 1145 | self.m_BasicHeader |
|
1288 | 1146 | self.m_SystemHeader |
|
1289 | 1147 | self.m_RadarControllerHeader |
|
1290 | 1148 | self.m_ProcessingHeader |
|
1291 | 1149 | self.dataType |
|
1292 | 1150 | |
|
1293 | 1151 | Return: |
|
1294 | 1152 | None |
|
1295 | 1153 | """ |
|
1296 | 1154 | self.m_BasicHeader = self.m_DataObj.m_BasicHeader.copy() |
|
1297 | 1155 | self.m_SystemHeader = self.m_DataObj.m_SystemHeader.copy() |
|
1298 | 1156 | self.m_RadarControllerHeader = self.m_DataObj.m_RadarControllerHeader.copy() |
|
1299 | 1157 | self.m_ProcessingHeader = self.m_DataObj.m_ProcessingHeader.copy() |
|
1300 | 1158 | self.dataType = self.m_DataObj.dataType |
|
1301 | 1159 | |
|
1160 | ||
|
1302 | 1161 | def setup(self, path, set=0, ext=None): |
|
1303 | 1162 | """ |
|
1304 | 1163 | Setea el tipo de formato en la cual sera guardada la data y escribe el First Header |
|
1305 | 1164 | |
|
1306 | 1165 | Inputs: |
|
1307 | 1166 | path : el path destino en el cual se escribiran los files a crear |
|
1308 | 1167 | format : formato en el cual sera salvado un file |
|
1309 | 1168 | set : el setebo del file |
|
1310 | 1169 | |
|
1311 | 1170 | Return: |
|
1312 | 1171 | 0 : Si no realizo un buen seteo |
|
1313 | 1172 | 1 : Si realizo un buen seteo |
|
1314 | 1173 | """ |
|
1315 | 1174 | |
|
1316 | 1175 | if ext == None: |
|
1317 | 1176 | ext = self.ext |
|
1318 | 1177 | |
|
1319 | 1178 | ext = ext.lower() |
|
1320 | 1179 | |
|
1321 | 1180 | self.path = path |
|
1322 | 1181 | self.setFile = set - 1 |
|
1323 | 1182 | self.ext = ext |
|
1324 | self.format = format | |
|
1183 | #self.format = format | |
|
1325 | 1184 | self.getHeader() |
|
1326 | 1185 | |
|
1327 | 1186 | self.setBlockDimension() |
|
1328 | 1187 | |
|
1329 |
if not( self. |
|
|
1188 | if not( self.setNextFile() ): | |
|
1330 | 1189 | print "There isn't a next file" |
|
1331 | 1190 | return 0 |
|
1332 | 1191 | |
|
1333 | 1192 | return 1 |
|
1334 | ||
|
1335 | def hasAllDataInBuffer(self): | |
|
1336 | ||
|
1337 | raise ValueError, "Not implemented" | |
|
1338 | ||
|
1339 | def setBlockDimension(self): | |
|
1340 | ||
|
1341 | raise ValueError, "Not implemented" | |
|
1342 | ||
|
1343 | def writeBlock(self): | |
|
1344 | ||
|
1345 | raise ValueError, "No implemented" | |
|
1346 | ||
|
1347 | def putData(self): | |
|
1348 | """ | |
|
1349 | Setea un bloque de datos y luego los escribe en un file | |
|
1350 | ||
|
1351 | Affected: | |
|
1352 | self.flagIsNewBlock | |
|
1353 | self.datablockIndex | |
|
1354 | ||
|
1355 | Return: | |
|
1356 | 0 : Si no hay data o no hay mas files que puedan escribirse | |
|
1357 | 1 : Si se escribio la data de un bloque en un file | |
|
1358 | """ | |
|
1359 | ||
|
1360 | raise ValueError, "No implemented" No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now