##// END OF EJS Templates
Bug fixed in findDataFiles
Miguel Valdez -
r601:af37133b1bca
parent child
Show More
@@ -1,1451 +1,1451
1 1 '''
2 2 Created on Jul 2, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import sys
8 8 import glob
9 9 import time
10 10 import numpy
11 11 import fnmatch
12 12 import time, datetime
13 13 #import h5py
14 14 import traceback
15 15
16 16 try:
17 17 from gevent import sleep
18 18 except:
19 19 from time import sleep
20 20
21 21 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
22 22
23 23 LOCALTIME = True
24 24
25 25 def isNumber(cad):
26 26 """
27 27 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
28 28
29 29 Excepciones:
30 30 Si un determinado string no puede ser convertido a numero
31 31 Input:
32 32 str, string al cual se le analiza para determinar si convertible a un numero o no
33 33
34 34 Return:
35 35 True : si el string es uno numerico
36 36 False : no es un string numerico
37 37 """
38 38 try:
39 39 float( cad )
40 40 return True
41 41 except:
42 42 return False
43 43
44 44 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
45 45 """
46 46 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
47 47
48 48 Inputs:
49 49 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
50 50
51 51 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
52 52 segundos contados desde 01/01/1970.
53 53 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
54 54 segundos contados desde 01/01/1970.
55 55
56 56 Return:
57 57 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
58 58 fecha especificado, de lo contrario retorna False.
59 59
60 60 Excepciones:
61 61 Si el archivo no existe o no puede ser abierto
62 62 Si la cabecera no puede ser leida.
63 63
64 64 """
65 65 basicHeaderObj = BasicHeader(LOCALTIME)
66 66
67 67 try:
68 68 fp = open(filename,'rb')
69 69 except IOError:
70 70 traceback.print_exc()
71 71 raise IOError, "The file %s can't be opened" %(filename)
72 72
73 73 sts = basicHeaderObj.read(fp)
74 74 fp.close()
75 75
76 76 if not(sts):
77 77 print "Skipping the file %s because it has not a valid header" %(filename)
78 78 return 0
79 79
80 80 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
81 81 return 0
82 82
83 83 return 1
84 84
85 85 def isFileinThisTime(filename, startTime, endTime):
86 86 """
87 87 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
88 88
89 89 Inputs:
90 90 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
91 91
92 92 startTime : tiempo inicial del rango seleccionado en formato datetime.time
93 93
94 94 endTime : tiempo final del rango seleccionado en formato datetime.time
95 95
96 96 Return:
97 97 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
98 98 fecha especificado, de lo contrario retorna False.
99 99
100 100 Excepciones:
101 101 Si el archivo no existe o no puede ser abierto
102 102 Si la cabecera no puede ser leida.
103 103
104 104 """
105 105
106 106
107 107 try:
108 108 fp = open(filename,'rb')
109 109 except IOError:
110 110 traceback.print_exc()
111 111 raise IOError, "The file %s can't be opened" %(filename)
112 112
113 113 basicHeaderObj = BasicHeader(LOCALTIME)
114 114 sts = basicHeaderObj.read(fp)
115 115 fp.close()
116 116
117 117 thisDatetime = basicHeaderObj.datatime
118 118 thisTime = thisDatetime.time()
119 119
120 120 if not(sts):
121 121 print "Skipping the file %s because it has not a valid header" %(filename)
122 122 return None
123 123
124 124 if not ((startTime <= thisTime) and (endTime > thisTime)):
125 125 return None
126 126
127 127 return thisDatetime
128 128
129 129 def getFileFromSet(path, ext, set):
130 130 validFilelist = []
131 131 fileList = os.listdir(path)
132 132
133 133 # 0 1234 567 89A BCDE
134 134 # H YYYY DDD SSS .ext
135 135
136 136 for thisFile in fileList:
137 137 try:
138 138 year = int(thisFile[1:5])
139 139 doy = int(thisFile[5:8])
140 140 except:
141 141 continue
142 142
143 143 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
144 144 continue
145 145
146 146 validFilelist.append(thisFile)
147 147
148 148 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
149 149
150 150 if len(myfile)!= 0:
151 151 return myfile[0]
152 152 else:
153 153 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
154 154 print 'the filename %s does not exist'%filename
155 155 print '...going to the last file: '
156 156
157 157 if validFilelist:
158 158 validFilelist = sorted( validFilelist, key=str.lower )
159 159 return validFilelist[-1]
160 160
161 161 return None
162 162
163 163 def getlastFileFromPath(path, ext):
164 164 """
165 165 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
166 166 al final de la depuracion devuelve el ultimo file de la lista que quedo.
167 167
168 168 Input:
169 169 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
170 170 ext : extension de los files contenidos en una carpeta
171 171
172 172 Return:
173 173 El ultimo file de una determinada carpeta, no se considera el path.
174 174 """
175 175 validFilelist = []
176 176 fileList = os.listdir(path)
177 177
178 178 # 0 1234 567 89A BCDE
179 179 # H YYYY DDD SSS .ext
180 180
181 181 for thisFile in fileList:
182 182
183 183 year = thisFile[1:5]
184 184 if not isNumber(year):
185 185 continue
186 186
187 187 doy = thisFile[5:8]
188 188 if not isNumber(doy):
189 189 continue
190 190
191 191 year = int(year)
192 192 doy = int(doy)
193 193
194 194 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
195 195 continue
196 196
197 197 validFilelist.append(thisFile)
198 198
199 199 if validFilelist:
200 200 validFilelist = sorted( validFilelist, key=str.lower )
201 201 return validFilelist[-1]
202 202
203 203 return None
204 204
205 205 def checkForRealPath(path, foldercounter, year, doy, set, ext):
206 206 """
207 207 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
208 208 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
209 209 el path exacto de un determinado file.
210 210
211 211 Example :
212 212 nombre correcto del file es .../.../D2009307/P2009307367.ext
213 213
214 214 Entonces la funcion prueba con las siguientes combinaciones
215 215 .../.../y2009307367.ext
216 216 .../.../Y2009307367.ext
217 217 .../.../x2009307/y2009307367.ext
218 218 .../.../x2009307/Y2009307367.ext
219 219 .../.../X2009307/y2009307367.ext
220 220 .../.../X2009307/Y2009307367.ext
221 221 siendo para este caso, la ultima combinacion de letras, identica al file buscado
222 222
223 223 Return:
224 224 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
225 225 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
226 226 para el filename
227 227 """
228 228 fullfilename = None
229 229 find_flag = False
230 230 filename = None
231 231
232 232 prefixDirList = [None,'d','D']
233 233 if ext.lower() == ".r": #voltage
234 234 prefixFileList = ['d','D']
235 235 elif ext.lower() == ".pdata": #spectra
236 236 prefixFileList = ['p','P']
237 237 else:
238 238 return None, filename
239 239
240 240 #barrido por las combinaciones posibles
241 241 for prefixDir in prefixDirList:
242 242 thispath = path
243 243 if prefixDir != None:
244 244 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
245 245 if foldercounter == 0:
246 246 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
247 247 else:
248 248 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
249 249 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
250 250 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
251 251 fullfilename = os.path.join( thispath, filename ) #formo el path completo
252 252
253 253 if os.path.exists( fullfilename ): #verifico que exista
254 254 find_flag = True
255 255 break
256 256 if find_flag:
257 257 break
258 258
259 259 if not(find_flag):
260 260 return None, filename
261 261
262 262 return fullfilename, filename
263 263
264 264 def isRadarFolder(folder):
265 265 try:
266 266 year = int(folder[1:5])
267 267 doy = int(folder[5:8])
268 268 except:
269 269 return 0
270 270
271 271 return 1
272 272
273 273 def isRadarFile(file):
274 274 try:
275 275 year = int(file[1:5])
276 276 doy = int(file[5:8])
277 277 set = int(file[8:11])
278 278 except:
279 279 return 0
280 280
281 281 return 1
282 282
283 283 def getDateFromRadarFile(file):
284 284 try:
285 285 year = int(file[1:5])
286 286 doy = int(file[5:8])
287 287 set = int(file[8:11])
288 288 except:
289 289 return None
290 290
291 291 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
292 292 return thisDate
293 293
294 294 class JRODataIO:
295 295
296 296 c = 3E8
297 297
298 298 isConfig = False
299 299
300 300 basicHeaderObj = None
301 301
302 302 systemHeaderObj = None
303 303
304 304 radarControllerHeaderObj = None
305 305
306 306 processingHeaderObj = None
307 307
308 308 online = 0
309 309
310 310 dtype = None
311 311
312 312 pathList = []
313 313
314 314 filenameList = []
315 315
316 316 filename = None
317 317
318 318 ext = None
319 319
320 320 flagIsNewFile = 1
321 321
322 322 flagDiscontinuousBlock = 0
323 323
324 324 flagIsNewBlock = 0
325 325
326 326 fp = None
327 327
328 328 firstHeaderSize = 0
329 329
330 330 basicHeaderSize = 24
331 331
332 332 versionFile = 1103
333 333
334 334 fileSize = None
335 335
336 336 # ippSeconds = None
337 337
338 338 fileSizeByHeader = None
339 339
340 340 fileIndex = None
341 341
342 342 profileIndex = None
343 343
344 344 blockIndex = None
345 345
346 346 nTotalBlocks = None
347 347
348 348 maxTimeStep = 30
349 349
350 350 lastUTTime = None
351 351
352 352 datablock = None
353 353
354 354 dataOut = None
355 355
356 356 blocksize = None
357 357
358 358 getByBlock = False
359 359
360 360 def __init__(self):
361 361
362 362 raise ValueError, "Not implemented"
363 363
364 364 def run(self):
365 365
366 366 raise ValueError, "Not implemented"
367 367
368 368 class JRODataReader(JRODataIO):
369 369
370 370 nReadBlocks = 0
371 371
372 372 delay = 10 #number of seconds waiting a new file
373 373
374 374 nTries = 3 #quantity tries
375 375
376 376 nFiles = 3 #number of files for searching
377 377
378 378 path = None
379 379
380 380 foldercounter = 0
381 381
382 382 flagNoMoreFiles = 0
383 383
384 384 datetimeList = []
385 385
386 386 __isFirstTimeOnline = 1
387 387
388 388 __printInfo = True
389 389
390 390 profileIndex = None
391 391
392 392 nTxs = 1
393 393
394 394 txIndex = None
395 395
396 396 def __init__(self):
397 397
398 398 """
399 399
400 400 """
401 401
402 402 # raise NotImplementedError, "This method has not been implemented"
403 403
404 404
405 405 def createObjByDefault(self):
406 406 """
407 407
408 408 """
409 409 raise NotImplementedError, "This method has not been implemented"
410 410
411 411 def getBlockDimension(self):
412 412
413 413 raise NotImplementedError, "No implemented"
414 414
415 415 def __searchFilesOffLine(self,
416 416 path,
417 417 startDate=None,
418 418 endDate=None,
419 419 startTime=datetime.time(0,0,0),
420 420 endTime=datetime.time(23,59,59),
421 421 set=None,
422 422 expLabel='',
423 423 ext='.r',
424 424 walk=True):
425 425
426 426 self.filenameList = []
427 427 self.datetimeList = []
428 428
429 429 pathList = []
430 430
431 431 if not walk:
432 432 #pathList.append(path)
433 433 multi_path = path.split(',')
434 434 for single_path in multi_path:
435 435 pathList.append(single_path)
436 436
437 437 else:
438 438 #dirList = []
439 439 multi_path = path.split(',')
440 440 for single_path in multi_path:
441 441 dirList = []
442 442 for thisPath in os.listdir(single_path):
443 443 if not os.path.isdir(os.path.join(single_path,thisPath)):
444 444 continue
445 445 if not isRadarFolder(thisPath):
446 446 continue
447 447
448 448 dirList.append(thisPath)
449 449
450 450 if not(dirList):
451 451 return None, None
452 452
453 453 if startDate and endDate:
454 454 thisDate = startDate
455 455
456 456 while(thisDate <= endDate):
457 457 year = thisDate.timetuple().tm_year
458 458 doy = thisDate.timetuple().tm_yday
459 459
460 460 matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*')
461 461 if len(matchlist) == 0:
462 462 thisDate += datetime.timedelta(1)
463 463 continue
464 464 for match in matchlist:
465 465 pathList.append(os.path.join(single_path,match,expLabel))
466 466
467 467 thisDate += datetime.timedelta(1)
468 468 else:
469 469 for thiDir in dirList:
470 470 pathList.append(os.path.join(single_path,thiDir,expLabel))
471 471
472 472 if pathList == []:
473 473 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
474 474 return None, None
475 475
476 476 print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate)
477 477
478 478 filenameList = []
479 479 datetimeList = []
480 480 pathDict = {}
481 481 filenameList_to_sort = []
482 482
483 483 for i in range(len(pathList)):
484 484
485 485 thisPath = pathList[i]
486 486
487 487 fileList = glob.glob1(thisPath, "*%s" %ext)
488 488 if len(fileList) < 1:
489 489 continue
490 490 fileList.sort()
491 491 pathDict.setdefault(fileList[0])
492 492 pathDict[fileList[0]] = i
493 493 filenameList_to_sort.append(fileList[0])
494 494
495 495 filenameList_to_sort.sort()
496 496
497 497 for file in filenameList_to_sort:
498 498 thisPath = pathList[pathDict[file]]
499 499
500 500 fileList = glob.glob1(thisPath, "*%s" %ext)
501 501 fileList.sort()
502 502
503 503 for file in fileList:
504 504
505 505 filename = os.path.join(thisPath,file)
506 506 thisDatetime = isFileinThisTime(filename, startTime, endTime)
507 507
508 508 if not(thisDatetime):
509 509 continue
510 510
511 511 filenameList.append(filename)
512 512 datetimeList.append(thisDatetime)
513 513
514 514 if not(filenameList):
515 515 print "Any file was found for the time range %s - %s" %(startTime, endTime)
516 516 return None, None
517 517
518 518 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
519 519 print
520 520
521 521 for i in range(len(filenameList)):
522 522 print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
523 523
524 524 self.filenameList = filenameList
525 525 self.datetimeList = datetimeList
526 526
527 527 return pathList, filenameList
528 528
529 529 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
530 530
531 531 """
532 532 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
533 533 devuelve el archivo encontrado ademas de otros datos.
534 534
535 535 Input:
536 536 path : carpeta donde estan contenidos los files que contiene data
537 537
538 538 expLabel : Nombre del subexperimento (subfolder)
539 539
540 540 ext : extension de los files
541 541
542 542 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
543 543
544 544 Return:
545 545 directory : eL directorio donde esta el file encontrado
546 546 filename : el ultimo file de una determinada carpeta
547 547 year : el anho
548 548 doy : el numero de dia del anho
549 549 set : el set del archivo
550 550
551 551
552 552 """
553 553 dirList = []
554 554
555 555 if not walk:
556 556 fullpath = path
557 557 foldercounter = 0
558 558 else:
559 559 #Filtra solo los directorios
560 560 for thisPath in os.listdir(path):
561 561 if not os.path.isdir(os.path.join(path,thisPath)):
562 562 continue
563 563 if not isRadarFolder(thisPath):
564 564 continue
565 565
566 566 dirList.append(thisPath)
567 567
568 568 if not(dirList):
569 569 return None, None, None, None, None, None
570 570
571 571 dirList = sorted( dirList, key=str.lower )
572 572
573 573 doypath = dirList[-1]
574 574 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
575 575 fullpath = os.path.join(path, doypath, expLabel)
576 576
577 577
578 578 print "%s folder was found: " %(fullpath )
579 579
580 580 if set == None:
581 581 filename = getlastFileFromPath(fullpath, ext)
582 582 else:
583 583 filename = getFileFromSet(fullpath, ext, set)
584 584
585 585 if not(filename):
586 586 return None, None, None, None, None, None
587 587
588 588 print "%s file was found" %(filename)
589 589
590 590 if not(self.__verifyFile(os.path.join(fullpath, filename))):
591 591 return None, None, None, None, None, None
592 592
593 593 year = int( filename[1:5] )
594 594 doy = int( filename[5:8] )
595 595 set = int( filename[8:11] )
596 596
597 597 return fullpath, foldercounter, filename, year, doy, set
598 598
599 599 def __setNextFileOffline(self):
600 600
601 601 idFile = self.fileIndex
602 602
603 603 while (True):
604 604 idFile += 1
605 605 if not(idFile < len(self.filenameList)):
606 606 self.flagNoMoreFiles = 1
607 607 # print "[Reading] No more Files"
608 608 return 0
609 609
610 610 filename = self.filenameList[idFile]
611 611
612 612 if not(self.__verifyFile(filename)):
613 613 continue
614 614
615 615 fileSize = os.path.getsize(filename)
616 616 fp = open(filename,'rb')
617 617 break
618 618
619 619 self.flagIsNewFile = 1
620 620 self.fileIndex = idFile
621 621 self.filename = filename
622 622 self.fileSize = fileSize
623 623 self.fp = fp
624 624
625 625 # print "[Reading] Setting the file: %s"%self.filename
626 626
627 627 return 1
628 628
629 629 def __setNextFileOnline(self):
630 630 """
631 631 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
632 632 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
633 633 siguientes.
634 634
635 635 Affected:
636 636 self.flagIsNewFile
637 637 self.filename
638 638 self.fileSize
639 639 self.fp
640 640 self.set
641 641 self.flagNoMoreFiles
642 642
643 643 Return:
644 644 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
645 645 1 : si el file fue abierto con exito y esta listo a ser leido
646 646
647 647 Excepciones:
648 648 Si un determinado file no puede ser abierto
649 649 """
650 650 nFiles = 0
651 651 fileOk_flag = False
652 652 firstTime_flag = True
653 653
654 654 self.set += 1
655 655
656 656 if self.set > 999:
657 657 self.set = 0
658 658 self.foldercounter += 1
659 659
660 660 #busca el 1er file disponible
661 661 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
662 662 if fullfilename:
663 663 if self.__verifyFile(fullfilename, False):
664 664 fileOk_flag = True
665 665
666 666 #si no encuentra un file entonces espera y vuelve a buscar
667 667 if not(fileOk_flag):
668 668 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
669 669
670 670 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
671 671 tries = self.nTries
672 672 else:
673 673 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
674 674
675 675 for nTries in range( tries ):
676 676 if firstTime_flag:
677 677 print "\t[Reading] Waiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
678 678 sleep( self.delay )
679 679 else:
680 680 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
681 681
682 682 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
683 683 if fullfilename:
684 684 if self.__verifyFile(fullfilename):
685 685 fileOk_flag = True
686 686 break
687 687
688 688 if fileOk_flag:
689 689 break
690 690
691 691 firstTime_flag = False
692 692
693 693 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
694 694 self.set += 1
695 695
696 696 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
697 697 self.set = 0
698 698 self.doy += 1
699 699 self.foldercounter = 0
700 700
701 701 if fileOk_flag:
702 702 self.fileSize = os.path.getsize( fullfilename )
703 703 self.filename = fullfilename
704 704 self.flagIsNewFile = 1
705 705 if self.fp != None: self.fp.close()
706 706 self.fp = open(fullfilename, 'rb')
707 707 self.flagNoMoreFiles = 0
708 708 # print '[Reading] Setting the file: %s' % fullfilename
709 709 else:
710 710 self.fileSize = 0
711 711 self.filename = None
712 712 self.flagIsNewFile = 0
713 713 self.fp = None
714 714 self.flagNoMoreFiles = 1
715 715 # print '[Reading] No more files to read'
716 716
717 717 return fileOk_flag
718 718
719 719 def setNextFile(self):
720 720 if self.fp != None:
721 721 self.fp.close()
722 722
723 723 if self.online:
724 724 newFile = self.__setNextFileOnline()
725 725 else:
726 726 newFile = self.__setNextFileOffline()
727 727
728 728 if not(newFile):
729 729 print '[Reading] No more files to read'
730 730 return 0
731 731
732 732 print '[Reading] Setting the file: %s' % self.filename
733 733
734 734 self.__readFirstHeader()
735 735 self.nReadBlocks = 0
736 736 return 1
737 737
738 738 def __waitNewBlock(self):
739 739 """
740 740 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
741 741
742 742 Si el modo de lectura es OffLine siempre retorn 0
743 743 """
744 744 if not self.online:
745 745 return 0
746 746
747 747 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
748 748 return 0
749 749
750 750 currentPointer = self.fp.tell()
751 751
752 752 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
753 753
754 754 for nTries in range( self.nTries ):
755 755
756 756 self.fp.close()
757 757 self.fp = open( self.filename, 'rb' )
758 758 self.fp.seek( currentPointer )
759 759
760 760 self.fileSize = os.path.getsize( self.filename )
761 761 currentSize = self.fileSize - currentPointer
762 762
763 763 if ( currentSize >= neededSize ):
764 764 self.basicHeaderObj.read(self.fp)
765 765 return 1
766 766
767 767 if self.fileSize == self.fileSizeByHeader:
768 768 # self.flagEoF = True
769 769 return 0
770 770
771 771 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
772 772 sleep( self.delay )
773 773
774 774
775 775 return 0
776 776
777 777 def waitDataBlock(self,pointer_location):
778 778
779 779 currentPointer = pointer_location
780 780
781 781 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
782 782
783 783 for nTries in range( self.nTries ):
784 784 self.fp.close()
785 785 self.fp = open( self.filename, 'rb' )
786 786 self.fp.seek( currentPointer )
787 787
788 788 self.fileSize = os.path.getsize( self.filename )
789 789 currentSize = self.fileSize - currentPointer
790 790
791 791 if ( currentSize >= neededSize ):
792 792 return 1
793 793
794 794 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
795 795 sleep( self.delay )
796 796
797 797 return 0
798 798
799 799 def __jumpToLastBlock(self):
800 800
801 801 if not(self.__isFirstTimeOnline):
802 802 return
803 803
804 804 csize = self.fileSize - self.fp.tell()
805 805 blocksize = self.processingHeaderObj.blockSize
806 806
807 807 #salta el primer bloque de datos
808 808 if csize > self.processingHeaderObj.blockSize:
809 809 self.fp.seek(self.fp.tell() + blocksize)
810 810 else:
811 811 return
812 812
813 813 csize = self.fileSize - self.fp.tell()
814 814 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
815 815 while True:
816 816
817 817 if self.fp.tell()<self.fileSize:
818 818 self.fp.seek(self.fp.tell() + neededsize)
819 819 else:
820 820 self.fp.seek(self.fp.tell() - neededsize)
821 821 break
822 822
823 823 # csize = self.fileSize - self.fp.tell()
824 824 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
825 825 # factor = int(csize/neededsize)
826 826 # if factor > 0:
827 827 # self.fp.seek(self.fp.tell() + factor*neededsize)
828 828
829 829 self.flagIsNewFile = 0
830 830 self.__isFirstTimeOnline = 0
831 831
832 832 def __setNewBlock(self):
833 833
834 834 if self.fp == None:
835 835 return 0
836 836
837 837 if self.online:
838 838 self.__jumpToLastBlock()
839 839
840 840 if self.flagIsNewFile:
841 841 return 1
842 842
843 843 self.lastUTTime = self.basicHeaderObj.utc
844 844 currentSize = self.fileSize - self.fp.tell()
845 845 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
846 846
847 847 if (currentSize >= neededSize):
848 848 self.basicHeaderObj.read(self.fp)
849 849 return 1
850 850
851 851 if self.__waitNewBlock():
852 852 return 1
853 853
854 854 if not(self.setNextFile()):
855 855 return 0
856 856
857 857 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
858 858
859 859 self.flagDiscontinuousBlock = 0
860 860
861 861 if deltaTime > self.maxTimeStep:
862 862 self.flagDiscontinuousBlock = 1
863 863
864 864 return 1
865 865
866 866 def readNextBlock(self):
867 867 if not(self.__setNewBlock()):
868 868 return 0
869 869
870 870 if not(self.readBlock()):
871 871 return 0
872 872
873 873 return 1
874 874
875 875 def __readFirstHeader(self):
876 876
877 877 self.basicHeaderObj.read(self.fp)
878 878 self.systemHeaderObj.read(self.fp)
879 879 self.radarControllerHeaderObj.read(self.fp)
880 880 self.processingHeaderObj.read(self.fp)
881 881
882 882 self.firstHeaderSize = self.basicHeaderObj.size
883 883
884 884 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
885 885 if datatype == 0:
886 886 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
887 887 elif datatype == 1:
888 888 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
889 889 elif datatype == 2:
890 890 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
891 891 elif datatype == 3:
892 892 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
893 893 elif datatype == 4:
894 894 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
895 895 elif datatype == 5:
896 896 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
897 897 else:
898 898 raise ValueError, 'Data type was not defined'
899 899
900 900 self.dtype = datatype_str
901 901 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
902 902 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
903 903 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
904 904 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
905 905 self.getBlockDimension()
906 906
907 907 def __verifyFile(self, filename, msgFlag=True):
908 908 msg = None
909 909 try:
910 910 fp = open(filename, 'rb')
911 911 currentPosition = fp.tell()
912 912 except IOError:
913 913 traceback.print_exc()
914 914 if msgFlag:
915 915 print "[Reading] The file %s can't be opened" % (filename)
916 916 return False
917 917
918 918 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
919 919
920 920 if neededSize == 0:
921 921 basicHeaderObj = BasicHeader(LOCALTIME)
922 922 systemHeaderObj = SystemHeader()
923 923 radarControllerHeaderObj = RadarControllerHeader()
924 924 processingHeaderObj = ProcessingHeader()
925 925
926 926 try:
927 927 if not( basicHeaderObj.read(fp) ): raise IOError
928 928 if not( systemHeaderObj.read(fp) ): raise IOError
929 929 if not( radarControllerHeaderObj.read(fp) ): raise IOError
930 930 if not( processingHeaderObj.read(fp) ): raise IOError
931 931 # data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
932 932
933 933 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
934 934
935 935 except IOError:
936 936 traceback.print_exc()
937 937 if msgFlag:
938 938 print "[Reading] The file %s is empty or it hasn't enough data" % filename
939 939
940 940 fp.close()
941 941 return False
942 942 else:
943 943 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
944 944
945 945 fp.close()
946 946 fileSize = os.path.getsize(filename)
947 947 currentSize = fileSize - currentPosition
948 948 if currentSize < neededSize:
949 949 if msgFlag and (msg != None):
950 950 print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename
951 951 return False
952 952
953 953 return True
954 954
955 955 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True):
956 956
957 957 dateList = []
958 958 pathList = []
959 959
960 960 if not walk:
961 961 #pathList.append(path)
962 962 multi_path = path.split(',')
963 963 for single_path in multi_path:
964 964
965 965 ok = False
966 966 fileList = glob.glob1(single_path, "*"+ext)
967 967
968 968 for thisFile in fileList:
969 969
970 970 if not os.path.isfile(os.path.join(single_path, thisFile)):
971 971 continue
972 972
973 973 if not isRadarFile(thisFile):
974 974 continue
975 975
976 976 ok = True
977 977 thisDate = getDateFromRadarFile(thisFile)
978 978
979 979 if thisDate not in dateList:
980 980 dateList.append(thisDate)
981 981
982 982 if ok:
983 983 pathList.append(single_path)
984 984
985 985 return dateList
986 986
987 987 multi_path = path.split(',')
988 988 for single_path in multi_path:
989 989
990 990 dirList = []
991 991
992 992 for thisPath in os.listdir(single_path):
993 993
994 994 if not os.path.isdir(os.path.join(single_path,thisPath)):
995 995 continue
996 996
997 997 if not isRadarFolder(thisPath):
998 998 continue
999 999
1000 1000 dirList.append(thisPath)
1001 1001
1002 1002 if not dirList:
1003 1003 return dateList
1004 1004
1005 1005 if startDate and endDate:
1006 1006 thisDate = startDate
1007 1007
1008 1008 while(thisDate <= endDate):
1009 1009 year = thisDate.timetuple().tm_year
1010 1010 doy = thisDate.timetuple().tm_yday
1011 1011
1012 1012 matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*')
1013 1013 if len(matchlist) == 0:
1014 1014 thisDate += datetime.timedelta(1)
1015 1015 continue
1016 1016
1017 1017 for match in matchlist:
1018 1018 pathList.append(os.path.join(single_path,match,expLabel))
1019 1019 dateList.append(thisDate)
1020 1020
1021 1021 thisDate += datetime.timedelta(1)
1022 1022 else:
1023 for thiDir in dirList:
1024 year = int(folder[1:5])
1025 doy = int(folder[5:8])
1023 for thisDir in dirList:
1024 year = int(thisDir[1:5])
1025 doy = int(thisDir[5:8])
1026 1026 thisDate = datetime.date(year,1,1) + datetime.timedelta(doy-1)
1027 1027
1028 pathList.append(os.path.join(single_path,thiDir,expLabel))
1028 pathList.append(os.path.join(single_path,thisDir,expLabel))
1029 1029 dateList.append(thisDate)
1030 1030
1031 1031 return dateList
1032 1032
1033 1033
1034 1034 def setup(self,
1035 1035 path=None,
1036 1036 startDate=None,
1037 1037 endDate=None,
1038 1038 startTime=datetime.time(0,0,0),
1039 1039 endTime=datetime.time(23,59,59),
1040 1040 set=None,
1041 1041 expLabel = "",
1042 1042 ext = None,
1043 1043 online = False,
1044 1044 delay = 60,
1045 1045 walk = True,
1046 1046 getblock = False,
1047 1047 nTxs = 1):
1048 1048
1049 1049 if path == None:
1050 1050 raise ValueError, "[Reading] The path is not valid"
1051 1051
1052 1052 if ext == None:
1053 1053 ext = self.ext
1054 1054
1055 1055 if online:
1056 1056 print "[Reading] Searching files in online mode..."
1057 1057
1058 1058 for nTries in range( self.nTries ):
1059 1059 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1060 1060
1061 1061 if fullpath:
1062 1062 break
1063 1063
1064 1064 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1065 1065 sleep( self.delay )
1066 1066
1067 1067 if not(fullpath):
1068 1068 print "[Reading] There 'isn't any valid file in %s" % path
1069 1069 return None
1070 1070
1071 1071 self.year = year
1072 1072 self.doy = doy
1073 1073 self.set = set - 1
1074 1074 self.path = path
1075 1075 self.foldercounter = foldercounter
1076 1076 last_set = None
1077 1077
1078 1078 else:
1079 1079 print "[Reading] Searching files in offline mode ..."
1080 1080 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1081 1081 startTime=startTime, endTime=endTime,
1082 1082 set=set, expLabel=expLabel, ext=ext,
1083 1083 walk=walk)
1084 1084
1085 1085 if not(pathList):
1086 1086 print "[Reading] No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
1087 1087 datetime.datetime.combine(startDate,startTime).ctime(),
1088 1088 datetime.datetime.combine(endDate,endTime).ctime())
1089 1089
1090 1090 sys.exit(-1)
1091 1091
1092 1092
1093 1093 self.fileIndex = -1
1094 1094 self.pathList = pathList
1095 1095 self.filenameList = filenameList
1096 1096 file_name = os.path.basename(filenameList[-1])
1097 1097 basename, ext = os.path.splitext(file_name)
1098 1098 last_set = int(basename[-3:])
1099 1099
1100 1100 self.online = online
1101 1101 self.delay = delay
1102 1102 ext = ext.lower()
1103 1103 self.ext = ext
1104 1104 self.getByBlock = getblock
1105 1105 self.nTxs = int(nTxs)
1106 1106
1107 1107 if not(self.setNextFile()):
1108 1108 if (startDate!=None) and (endDate!=None):
1109 1109 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1110 1110 elif startDate != None:
1111 1111 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1112 1112 else:
1113 1113 print "[Reading] No files"
1114 1114
1115 1115 sys.exit(-1)
1116 1116
1117 1117 # self.updateDataHeader()
1118 1118 if last_set != None:
1119 1119 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1120 1120 return
1121 1121
1122 1122 def getBasicHeader(self):
1123 1123
1124 1124 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1125 1125
1126 1126 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1127 1127
1128 1128 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1129 1129
1130 1130 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1131 1131
1132 1132 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1133 1133
1134 1134 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1135 1135
1136 1136 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1137 1137
1138 1138 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1139 1139
1140 1140
1141 1141 def getFirstHeader(self):
1142 1142
1143 1143 raise ValueError, "This method has not been implemented"
1144 1144
1145 1145 def getData(self):
1146 1146
1147 1147 raise ValueError, "This method has not been implemented"
1148 1148
1149 1149 def hasNotDataInBuffer(self):
1150 1150
1151 1151 raise ValueError, "This method has not been implemented"
1152 1152
1153 1153 def readBlock(self):
1154 1154
1155 1155 raise ValueError, "This method has not been implemented"
1156 1156
1157 1157 def isEndProcess(self):
1158 1158
1159 1159 return self.flagNoMoreFiles
1160 1160
1161 1161 def printReadBlocks(self):
1162 1162
1163 1163 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1164 1164
1165 1165 def printTotalBlocks(self):
1166 1166
1167 1167 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1168 1168
1169 1169 def printNumberOfBlock(self):
1170 1170
1171 1171 if self.flagIsNewBlock:
1172 1172 print "[Reading] Block No. %04d, Total blocks %04d -> %s" %(self.basicHeaderObj.dataBlock, self.nTotalBlocks, self.dataOut.datatime.ctime())
1173 1173 self.dataOut.blocknow = self.basicHeaderObj.dataBlock
1174 1174
1175 1175 def printInfo(self):
1176 1176
1177 1177 if self.__printInfo == False:
1178 1178 return
1179 1179
1180 1180 self.basicHeaderObj.printInfo()
1181 1181 self.systemHeaderObj.printInfo()
1182 1182 self.radarControllerHeaderObj.printInfo()
1183 1183 self.processingHeaderObj.printInfo()
1184 1184
1185 1185 self.__printInfo = False
1186 1186
1187 1187
1188 1188 def run(self, **kwargs):
1189 1189
1190 1190 if not(self.isConfig):
1191 1191
1192 1192 # self.dataOut = dataOut
1193 1193 self.setup(**kwargs)
1194 1194 self.isConfig = True
1195 1195
1196 1196 self.getData()
1197 1197
1198 1198 class JRODataWriter(JRODataIO):
1199 1199
1200 1200 """
1201 1201 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1202 1202 de los datos siempre se realiza por bloques.
1203 1203 """
1204 1204
1205 1205 blockIndex = 0
1206 1206
1207 1207 path = None
1208 1208
1209 1209 setFile = None
1210 1210
1211 1211 profilesPerBlock = None
1212 1212
1213 1213 blocksPerFile = None
1214 1214
1215 1215 nWriteBlocks = 0
1216 1216
1217 1217 def __init__(self, dataOut=None):
1218 1218 raise ValueError, "Not implemented"
1219 1219
1220 1220
1221 1221 def hasAllDataInBuffer(self):
1222 1222 raise ValueError, "Not implemented"
1223 1223
1224 1224
1225 1225 def setBlockDimension(self):
1226 1226 raise ValueError, "Not implemented"
1227 1227
1228 1228
1229 1229 def writeBlock(self):
1230 1230 raise ValueError, "No implemented"
1231 1231
1232 1232
1233 1233 def putData(self):
1234 1234 raise ValueError, "No implemented"
1235 1235
1236 1236
1237 1237 def setBasicHeader(self):
1238 1238
1239 1239 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1240 1240 self.basicHeaderObj.version = self.versionFile
1241 1241 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1242 1242
1243 1243 utc = numpy.floor(self.dataOut.utctime)
1244 1244 milisecond = (self.dataOut.utctime - utc)* 1000.0
1245 1245
1246 1246 self.basicHeaderObj.utc = utc
1247 1247 self.basicHeaderObj.miliSecond = milisecond
1248 1248 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1249 1249 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1250 1250 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1251 1251
1252 1252 def setFirstHeader(self):
1253 1253 """
1254 1254 Obtiene una copia del First Header
1255 1255
1256 1256 Affected:
1257 1257
1258 1258 self.basicHeaderObj
1259 1259 self.systemHeaderObj
1260 1260 self.radarControllerHeaderObj
1261 1261 self.processingHeaderObj self.
1262 1262
1263 1263 Return:
1264 1264 None
1265 1265 """
1266 1266
1267 1267 raise ValueError, "No implemented"
1268 1268
1269 1269 def __writeFirstHeader(self):
1270 1270 """
1271 1271 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1272 1272
1273 1273 Affected:
1274 1274 __dataType
1275 1275
1276 1276 Return:
1277 1277 None
1278 1278 """
1279 1279
1280 1280 # CALCULAR PARAMETROS
1281 1281
1282 1282 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1283 1283 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1284 1284
1285 1285 self.basicHeaderObj.write(self.fp)
1286 1286 self.systemHeaderObj.write(self.fp)
1287 1287 self.radarControllerHeaderObj.write(self.fp)
1288 1288 self.processingHeaderObj.write(self.fp)
1289 1289
1290 1290 self.dtype = self.dataOut.dtype
1291 1291
1292 1292 def __setNewBlock(self):
1293 1293 """
1294 1294 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1295 1295
1296 1296 Return:
1297 1297 0 : si no pudo escribir nada
1298 1298 1 : Si escribio el Basic el First Header
1299 1299 """
1300 1300 if self.fp == None:
1301 1301 self.setNextFile()
1302 1302
1303 1303 if self.flagIsNewFile:
1304 1304 return 1
1305 1305
1306 1306 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1307 1307 self.basicHeaderObj.write(self.fp)
1308 1308 return 1
1309 1309
1310 1310 if not( self.setNextFile() ):
1311 1311 return 0
1312 1312
1313 1313 return 1
1314 1314
1315 1315
1316 1316 def writeNextBlock(self):
1317 1317 """
1318 1318 Selecciona el bloque siguiente de datos y los escribe en un file
1319 1319
1320 1320 Return:
1321 1321 0 : Si no hizo pudo escribir el bloque de datos
1322 1322 1 : Si no pudo escribir el bloque de datos
1323 1323 """
1324 1324 if not( self.__setNewBlock() ):
1325 1325 return 0
1326 1326
1327 1327 self.writeBlock()
1328 1328
1329 1329 return 1
1330 1330
1331 1331 def setNextFile(self):
1332 1332 """
1333 1333 Determina el siguiente file que sera escrito
1334 1334
1335 1335 Affected:
1336 1336 self.filename
1337 1337 self.subfolder
1338 1338 self.fp
1339 1339 self.setFile
1340 1340 self.flagIsNewFile
1341 1341
1342 1342 Return:
1343 1343 0 : Si el archivo no puede ser escrito
1344 1344 1 : Si el archivo esta listo para ser escrito
1345 1345 """
1346 1346 ext = self.ext
1347 1347 path = self.path
1348 1348
1349 1349 if self.fp != None:
1350 1350 self.fp.close()
1351 1351
1352 1352 timeTuple = time.localtime( self.dataOut.utctime)
1353 1353 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1354 1354
1355 1355 fullpath = os.path.join( path, subfolder )
1356 1356 if not( os.path.exists(fullpath) ):
1357 1357 os.mkdir(fullpath)
1358 1358 self.setFile = -1 #inicializo mi contador de seteo
1359 1359 else:
1360 1360 filesList = os.listdir( fullpath )
1361 1361 if len( filesList ) > 0:
1362 1362 filesList = sorted( filesList, key=str.lower )
1363 1363 filen = filesList[-1]
1364 1364 # el filename debera tener el siguiente formato
1365 1365 # 0 1234 567 89A BCDE (hex)
1366 1366 # x YYYY DDD SSS .ext
1367 1367 if isNumber( filen[8:11] ):
1368 1368 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1369 1369 else:
1370 1370 self.setFile = -1
1371 1371 else:
1372 1372 self.setFile = -1 #inicializo mi contador de seteo
1373 1373
1374 1374 setFile = self.setFile
1375 1375 setFile += 1
1376 1376
1377 1377 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
1378 1378 timeTuple.tm_year,
1379 1379 timeTuple.tm_yday,
1380 1380 setFile,
1381 1381 ext )
1382 1382
1383 1383 filename = os.path.join( path, subfolder, filen )
1384 1384
1385 1385 fp = open( filename,'wb' )
1386 1386
1387 1387 self.blockIndex = 0
1388 1388
1389 1389 #guardando atributos
1390 1390 self.filename = filename
1391 1391 self.subfolder = subfolder
1392 1392 self.fp = fp
1393 1393 self.setFile = setFile
1394 1394 self.flagIsNewFile = 1
1395 1395
1396 1396 self.setFirstHeader()
1397 1397
1398 1398 print '[Writing] file: %s'%self.filename
1399 1399
1400 1400 self.__writeFirstHeader()
1401 1401
1402 1402 return 1
1403 1403
1404 1404 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=0, ext=None):
1405 1405 """
1406 1406 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1407 1407
1408 1408 Inputs:
1409 1409 path : el path destino en el cual se escribiran los files a crear
1410 1410 format : formato en el cual sera salvado un file
1411 1411 set : el setebo del file
1412 1412
1413 1413 Return:
1414 1414 0 : Si no realizo un buen seteo
1415 1415 1 : Si realizo un buen seteo
1416 1416 """
1417 1417
1418 1418 if ext == None:
1419 1419 ext = self.ext
1420 1420
1421 1421 ext = ext.lower()
1422 1422
1423 1423 self.ext = ext
1424 1424
1425 1425 self.path = path
1426 1426
1427 1427 self.setFile = set - 1
1428 1428
1429 1429 self.blocksPerFile = blocksPerFile
1430 1430
1431 1431 self.profilesPerBlock = profilesPerBlock
1432 1432
1433 1433 self.dataOut = dataOut
1434 1434
1435 1435 if not(self.setNextFile()):
1436 1436 print "[Writing] There isn't a next file"
1437 1437 return 0
1438 1438
1439 1439 self.setBlockDimension()
1440 1440
1441 1441 return 1
1442 1442
1443 1443 def run(self, dataOut, **kwargs):
1444 1444
1445 1445 if not(self.isConfig):
1446 1446
1447 1447 self.setup(dataOut, **kwargs)
1448 1448 self.isConfig = True
1449 1449
1450 1450 self.putData()
1451 1451
General Comments 0
You need to be logged in to leave comments. Login now