@@ -76,8 +76,90 def isThisFileinRange(filename, startUTSeconds, endUTSeconds): | |||||
76 |
|
76 | |||
77 | return 1 |
|
77 | return 1 | |
78 |
|
78 | |||
|
79 | def getlastFileFromPath(path, ext): | |||
|
80 | """ | |||
|
81 | Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext" | |||
|
82 | al final de la depuracion devuelve el ultimo file de la lista que quedo. | |||
|
83 | ||||
|
84 | Input: | |||
|
85 | fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta | |||
|
86 | ext : extension de los files contenidos en una carpeta | |||
|
87 | ||||
|
88 | Return: | |||
|
89 | El ultimo file de una determinada carpeta, no se considera el path. | |||
|
90 | """ | |||
|
91 | validFilelist = [] | |||
|
92 | fileList = os.listdir(path) | |||
|
93 | ||||
|
94 | # 0 1234 567 89A BCDE | |||
|
95 | # H YYYY DDD SSS .ext | |||
|
96 | ||||
|
97 | for file in fileList: | |||
|
98 | try: | |||
|
99 | year = int(file[1:5]) | |||
|
100 | doy = int(file[5:8]) | |||
|
101 | ||||
|
102 | if (os.path.splitext(file)[-1].upper() != ext.upper()) : continue | |||
|
103 | except: | |||
|
104 | continue | |||
|
105 | ||||
|
106 | validFilelist.append(file) | |||
79 |
|
107 | |||
|
108 | if validFilelist: | |||
|
109 | validFilelist = sorted( validFilelist, key=str.lower ) | |||
|
110 | return validFilelist[-1] | |||
80 |
|
111 | |||
|
112 | return None | |||
|
113 | ||||
|
114 | def checkForRealPath(path, year, doy, set, ext): | |||
|
115 | """ | |||
|
116 | Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path, | |||
|
117 | Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar | |||
|
118 | el path exacto de un determinado file. | |||
|
119 | ||||
|
120 | Example : | |||
|
121 | nombre correcto del file es .../.../D2009307/P2009307367.ext | |||
|
122 | ||||
|
123 | Entonces la funcion prueba con las siguientes combinaciones | |||
|
124 | .../.../x2009307/y2009307367.ext | |||
|
125 | .../.../x2009307/Y2009307367.ext | |||
|
126 | .../.../X2009307/y2009307367.ext | |||
|
127 | .../.../X2009307/Y2009307367.ext | |||
|
128 | siendo para este caso, la ultima combinacion de letras, identica al file buscado | |||
|
129 | ||||
|
130 | Return: | |||
|
131 | Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file | |||
|
132 | caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas | |||
|
133 | para el filename | |||
|
134 | """ | |||
|
135 | filepath = None | |||
|
136 | find_flag = False | |||
|
137 | filename = None | |||
|
138 | ||||
|
139 | if ext.lower() == ".r": #voltage | |||
|
140 | header1 = "dD" | |||
|
141 | header2 = "dD" | |||
|
142 | elif ext.lower() == ".pdata": #spectra | |||
|
143 | header1 = "dD" | |||
|
144 | header2 = "pP" | |||
|
145 | else: | |||
|
146 | return None, filename | |||
|
147 | ||||
|
148 | for dir in header1: #barrido por las dos combinaciones posibles de "D" | |||
|
149 | for fil in header2: #barrido por las dos combinaciones posibles de "D" | |||
|
150 | doypath = "%s%04d%03d" % ( dir, year, doy ) #formo el nombre del directorio xYYYYDDD (x=d o x=D) | |||
|
151 | filename = "%s%04d%03d%03d%s" % ( fil, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext | |||
|
152 | filepath = os.path.join( path, doypath, filename ) #formo el path completo | |||
|
153 | if os.path.exists( filepath ): #verifico que exista | |||
|
154 | find_flag = True | |||
|
155 | break | |||
|
156 | if find_flag: | |||
|
157 | break | |||
|
158 | ||||
|
159 | if not(find_flag): | |||
|
160 | return None, filename | |||
|
161 | ||||
|
162 | return filepath, filename | |||
81 |
|
163 | |||
82 | class JRODataIO: |
|
164 | class JRODataIO: | |
83 |
|
165 | |||
@@ -150,6 +232,13 class JRODataReader(JRODataIO): | |||||
150 |
|
232 | |||
151 | nReadBlocks = 0 |
|
233 | nReadBlocks = 0 | |
152 |
|
234 | |||
|
235 | delay = 60 #number of seconds waiting a new file | |||
|
236 | ||||
|
237 | nTries = 3 #quantity tries | |||
|
238 | ||||
|
239 | nFiles = 3 #number of files for searching | |||
|
240 | ||||
|
241 | ||||
153 | def __init__(self): |
|
242 | def __init__(self): | |
154 |
|
243 | |||
155 | pass |
|
244 | pass | |
@@ -227,7 +316,97 class JRODataReader(JRODataIO): | |||||
227 | self.filenameList = filenameList |
|
316 | self.filenameList = filenameList | |
228 |
|
317 | |||
229 | return pathList, filenameList |
|
318 | return pathList, filenameList | |
|
319 | ||||
|
320 | def __searchFilesOnLine(self, path, startDate=None, endDate=None, startTime=None, endTime=None, expLabel = "", ext = None): | |||
|
321 | ||||
|
322 | """ | |||
|
323 | Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y | |||
|
324 | devuelve el archivo encontrado ademas de otros datos. | |||
|
325 | ||||
|
326 | Input: | |||
|
327 | path : carpeta donde estan contenidos los files que contiene data | |||
|
328 | ||||
|
329 | startDate : Fecha inicial. Rechaza todos los directorios donde | |||
|
330 | file end time < startDate (obejto datetime.date) | |||
|
331 | ||||
|
332 | endDate : Fecha final. Rechaza todos los directorios donde | |||
|
333 | file start time > endDate (obejto datetime.date) | |||
|
334 | ||||
|
335 | startTime : Tiempo inicial. Rechaza todos los archivos donde | |||
|
336 | file end time < startTime (obejto datetime.time) | |||
|
337 | ||||
|
338 | endTime : Tiempo final. Rechaza todos los archivos donde | |||
|
339 | file start time > endTime (obejto datetime.time) | |||
|
340 | ||||
|
341 | expLabel : Nombre del subexperimento (subfolder) | |||
|
342 | ||||
|
343 | ext : extension de los files | |||
230 |
|
|
344 | ||
|
345 | Return: | |||
|
346 | directory : eL directorio donde esta el file encontrado | |||
|
347 | filename : el ultimo file de una determinada carpeta | |||
|
348 | year : el anho | |||
|
349 | doy : el numero de dia del anho | |||
|
350 | set : el set del archivo | |||
|
351 | ||||
|
352 | ||||
|
353 | """ | |||
|
354 | dirList = [] | |||
|
355 | pathList = [] | |||
|
356 | directory = None | |||
|
357 | ||||
|
358 | #Filtra solo los directorios | |||
|
359 | for thisPath in os.listdir(path): | |||
|
360 | if os.path.isdir(os.path.join(path, thisPath)): | |||
|
361 | dirList.append(thisPath) | |||
|
362 | ||||
|
363 | if not(dirList): | |||
|
364 | return None, None, None, None, None | |||
|
365 | ||||
|
366 | dirList = sorted( dirList, key=str.lower ) | |||
|
367 | ||||
|
368 | if startDate: | |||
|
369 | startDateTime = datetime.datetime.combine(startDate, startTime) | |||
|
370 | thisDateTime = startDateTime | |||
|
371 | if endDate == None: endDateTime = startDateTime | |||
|
372 | else: endDateTime = datetime.datetime.combine(endDate, endTime) | |||
|
373 | ||||
|
374 | while(thisDateTime <= endDateTime): | |||
|
375 | year = thisDateTime.timetuple().tm_year | |||
|
376 | doy = thisDateTime.timetuple().tm_yday | |||
|
377 | ||||
|
378 | match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy)) | |||
|
379 | if len(match) == 0: | |||
|
380 | thisDateTime += datetime.timedelta(1) | |||
|
381 | continue | |||
|
382 | ||||
|
383 | pathList.append(os.path.join(path,match[0], expLabel)) | |||
|
384 | thisDateTime += datetime.timedelta(1) | |||
|
385 | ||||
|
386 | if not(pathList): | |||
|
387 | print "\tNo files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime()) | |||
|
388 | return None, None, None, None, None | |||
|
389 | ||||
|
390 | directory = pathList[0] | |||
|
391 | ||||
|
392 | else: | |||
|
393 | directory = dirList[-1] | |||
|
394 | directory = os.path.join(path,directory) | |||
|
395 | ||||
|
396 | filename = getlastFileFromPath(directory, ext) | |||
|
397 | ||||
|
398 | if not(filename): | |||
|
399 | return None, None, None, None, None | |||
|
400 | ||||
|
401 | if not(self.__verifyFile(os.path.join(directory, filename))): | |||
|
402 | return None, None, None, None, None | |||
|
403 | ||||
|
404 | year = int( filename[1:5] ) | |||
|
405 | doy = int( filename[5:8] ) | |||
|
406 | set = int( filename[8:11] ) | |||
|
407 | ||||
|
408 | return directory, filename, year, doy, set | |||
|
409 | ||||
231 | def setup(self,dataOutObj=None, |
|
410 | def setup(self,dataOutObj=None, | |
232 | path=None, |
|
411 | path=None, | |
233 | startDate=None, |
|
412 | startDate=None, | |
@@ -237,7 +416,8 class JRODataReader(JRODataIO): | |||||
237 | set=0, |
|
416 | set=0, | |
238 | expLabel = "", |
|
417 | expLabel = "", | |
239 | ext = None, |
|
418 | ext = None, | |
240 |
online = |
|
419 | online = False, | |
|
420 | delay = 60): | |||
241 |
|
421 | |||
242 | if path == None: |
|
422 | if path == None: | |
243 | raise ValueError, "The path is not valid" |
|
423 | raise ValueError, "The path is not valid" | |
@@ -251,7 +431,25 class JRODataReader(JRODataIO): | |||||
251 | self.dataOutObj = dataOutObj |
|
431 | self.dataOutObj = dataOutObj | |
252 |
|
432 | |||
253 | if online: |
|
433 | if online: | |
254 | pass |
|
434 | print "Searching files in online mode..." | |
|
435 | doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext) | |||
|
436 | ||||
|
437 | if not(doypath): | |||
|
438 | for nTries in range( self.nTries ): | |||
|
439 | print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) | |||
|
440 | time.sleep( self.delay ) | |||
|
441 | doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=exp) | |||
|
442 | if doypath: | |||
|
443 | break | |||
|
444 | ||||
|
445 | if not(doypath): | |||
|
446 | print "There 'isn't valied files in %s" % path | |||
|
447 | return None | |||
|
448 | ||||
|
449 | self.year = year | |||
|
450 | self.doy = doy | |||
|
451 | self.set = set - 1 | |||
|
452 | self.path = path | |||
255 |
|
453 | |||
256 | else: |
|
454 | else: | |
257 | print "Searching files in offline mode ..." |
|
455 | print "Searching files in offline mode ..." | |
@@ -270,6 +468,7 class JRODataReader(JRODataIO): | |||||
270 | self.filenameList = filenameList |
|
468 | self.filenameList = filenameList | |
271 |
|
469 | |||
272 | self.online = online |
|
470 | self.online = online | |
|
471 | self.delay = delay | |||
273 | ext = ext.lower() |
|
472 | ext = ext.lower() | |
274 | self.ext = ext |
|
473 | self.ext = ext | |
275 |
|
474 | |||
@@ -288,6 +487,7 class JRODataReader(JRODataIO): | |||||
288 | return self.dataOutObj |
|
487 | return self.dataOutObj | |
289 |
|
488 | |||
290 | def __setNextFileOffline(self): |
|
489 | def __setNextFileOffline(self): | |
|
490 | ||||
291 | idFile = self.fileIndex |
|
491 | idFile = self.fileIndex | |
292 |
|
492 | |||
293 | while (True): |
|
493 | while (True): | |
@@ -316,6 +516,90 class JRODataReader(JRODataIO): | |||||
316 |
|
516 | |||
317 | return 1 |
|
517 | return 1 | |
318 |
|
518 | |||
|
519 | def __setNextFileOnline(self): | |||
|
520 | """ | |||
|
521 | Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si | |||
|
522 | no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files | |||
|
523 | siguientes. | |||
|
524 | ||||
|
525 | Affected: | |||
|
526 | self.flagIsNewFile | |||
|
527 | self.filename | |||
|
528 | self.fileSize | |||
|
529 | self.fp | |||
|
530 | self.set | |||
|
531 | self.flagNoMoreFiles | |||
|
532 | ||||
|
533 | Return: | |||
|
534 | 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado | |||
|
535 | 1 : si el file fue abierto con exito y esta listo a ser leido | |||
|
536 | ||||
|
537 | Excepciones: | |||
|
538 | Si un determinado file no puede ser abierto | |||
|
539 | """ | |||
|
540 | nFiles = 0 | |||
|
541 | fileOk_flag = False | |||
|
542 | firstTime_flag = True | |||
|
543 | ||||
|
544 | self.set += 1 | |||
|
545 | ||||
|
546 | #busca el 1er file disponible | |||
|
547 | file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) | |||
|
548 | if file: | |||
|
549 | if self.__verifyFile(file, False): | |||
|
550 | fileOk_flag = True | |||
|
551 | ||||
|
552 | #si no encuentra un file entonces espera y vuelve a buscar | |||
|
553 | if not(fileOk_flag): | |||
|
554 | for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles | |||
|
555 | ||||
|
556 | if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces | |||
|
557 | tries = self.nTries | |||
|
558 | else: | |||
|
559 | tries = 1 #si no es la 1era vez entonces solo lo hace una vez | |||
|
560 | ||||
|
561 | for nTries in range( tries ): | |||
|
562 | if firstTime_flag: | |||
|
563 | print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 ) | |||
|
564 | time.sleep( self.delay ) | |||
|
565 | else: | |||
|
566 | print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext) | |||
|
567 | ||||
|
568 | file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext ) | |||
|
569 | if file: | |||
|
570 | if self.__verifyFile(file): | |||
|
571 | fileOk_flag = True | |||
|
572 | break | |||
|
573 | ||||
|
574 | if fileOk_flag: | |||
|
575 | break | |||
|
576 | ||||
|
577 | firstTime_flag = False | |||
|
578 | ||||
|
579 | print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename | |||
|
580 | self.set += 1 | |||
|
581 | ||||
|
582 | if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta | |||
|
583 | self.set = 0 | |||
|
584 | self.doy += 1 | |||
|
585 | ||||
|
586 | if fileOk_flag: | |||
|
587 | self.fileSize = os.path.getsize( file ) | |||
|
588 | self.filename = file | |||
|
589 | self.flagIsNewFile = 1 | |||
|
590 | if self.fp != None: self.fp.close() | |||
|
591 | self.fp = open(file) | |||
|
592 | self.flagNoMoreFiles = 0 | |||
|
593 | print 'Setting the file: %s' % file | |||
|
594 | else: | |||
|
595 | self.fileSize = 0 | |||
|
596 | self.filename = None | |||
|
597 | self.flagIsNewFile = 0 | |||
|
598 | self.fp = None | |||
|
599 | self.flagNoMoreFiles = 1 | |||
|
600 | print 'No more Files' | |||
|
601 | ||||
|
602 | return fileOk_flag | |||
319 |
|
603 | |||
320 |
|
604 | |||
321 | def setNextFile(self): |
|
605 | def setNextFile(self): |
General Comments 0
You need to be logged in to leave comments.
Login now