Commits

Takeshi Nakazato authored aab313a10aa Merge
Merge branch 'master' into CAS-14072
No tags

casatasks/src/private/flaghelper.py

Modified
386 386
387 387 # Apply time buffer to list
388 388 if doPadding:
389 389 # mytbuff = dtbuff.popleft()
390 390 applyTimeBufferList(parsedlist, tbuff)
391 391
392 392 listofdict = listofdict + parsedlist
393 393
394 394 return listofdict
395 395
396 -# Use readAndParse to apply tbuff, which is slightly faster. For
397 -# small files, the difference is not noticeable.
398 -def readParseTbuff(inputlist, tbuff=None):
399 - '''Read in a list of flag commands and parse them into a dictionary.
400 - The flag commands can be from a list of files or from a list of flag commands.
401 - If tbuff is given, it will be applied to the timerange parameters.
402 -
403 - Note: when tbuff is None, it is best to use the individual functions
404 - readFile(s) and parseDictionary to avoid parsing the file twice!
405 -
406 - inputlist --> list of files in disk containing the flag commands or
407 - a list of Python strings with flag commands.
408 - tbuff --> list of time buffers to apply to each timerange. If tbuff
409 - is a list, it should be the same size of the list of inputfiles.
410 - If it is a Double value, it will be applied only to the first
411 - input file.
412 -
413 - Returns a list of dictionaries. Blank lines are skipped. Boolean values will
414 - be capitalized to avoid errors in the parser.
415 -
416 - Each parameter=value will become a key=value in the dictionary. Example:
417 - inputlist = ["mode='manual' spw='0' autocorr=true",
418 - "mode='shadow'"]
419 -
420 - Returns:
421 - [{'mode':'manual','spw':'0','autocorr':True},
422 - {'mode':'shadow'}]
423 -
424 - '''
425 - if not isinstance(inputlist, list):
426 - casalog.post('Error opening list of flag commands ' + inputlist,'ERROR')
427 - raise
428 -
429 - # List of files
430 - isFile = bool(os.path.isfile(inputlist[0]))
431 -
432 - if tbuff is None:
433 - doPadding = False
434 - else:
435 - doPadding = True
436 -
437 - # Make the list of tbuff a deque
438 - dtbuff = deque()
439 -
440 - if isinstance(tbuff, float):
441 - dtbuff.append(tbuff)
442 - elif isinstance(tbuff, list):
443 - dtbuff = deque(i for i in tbuff)
444 -
445 - # List of dictionaries to return
446 - listofdict = []
447 -
448 - # Initialize the parser
449 - myParser = Parser(' ', '=')
450 -
451 - # Read files
452 - if isFile:
453 - for flagfile in inputlist:
454 - cmdlist = readFile(flagfile)
455 - nlines = len(cmdlist)
456 - casalog.post('Read %s command(s) from file: %s'%(nlines, flagfile))
457 -
458 - if doPadding:
459 - mytbuff = dtbuff.popleft()
460 -
461 - parsedlist = []
462 - for cmd in cmdlist:
463 - #Get a dictionary without type evaluation
464 - preparsing = myParser.parseNoEval(cmd)
465 -
466 - # Evaluate the types
467 - parsed = evaluateParameters(preparsing)
468 -
469 - # Apply time buffer to single command
470 - if doPadding:
471 - applyTimeBuffer(parsed, mytbuff)
472 -
473 - parsedlist.append(parsed)
474 -
475 - if dtbuff.__len__() == 0:
476 - doPadding = False
477 -
478 - # Append dictionary to list
479 - listofdict += parsedlist
480 -
481 - # It is a list of strings
482 - else:
483 -
484 - cmdlist = inputlist
485 - nlines = len(cmdlist)
486 - casalog.post('Read %s command(s) from a Python list of strings'%nlines)
487 -
488 - if doPadding:
489 - mytbuff = dtbuff.popleft()
490 -
491 - parsedlist = []
492 - for cmd in cmdlist:
493 - #Get a dictionary without type evaluation
494 - preparsing = myParser.parseNoEval(cmd)
495 -
496 - # Evaluate the types
497 - parsed = evaluateParameters(preparsing)
498 -
499 - # Apply time buffer to single command
500 - if doPadding:
501 - applyTimeBuffer(parsed, mytbuff)
502 -
503 - parsedlist.append(parsed)
504 -
505 - listofdict += parsedlist
506 -
507 - return listofdict
508 -
509 -def applyTimeBuffer(cmddict, tbuff):
510 - ''' Apply in-place a time buffer to timerange from a dictionary with one flag command
511 -
512 - cmddict --> dictionary with a single flag command:
513 - Ex: {'antenna':'DV01', 'timerange':'2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'}
514 - tbuff --> float value of time buffer to apply to all timerange parameters
515 -
516 - * it assumes that timerange has syntax t0~t1
517 - * split timerange in '~' to get t0 and t1
518 - * convert value to time in days using qalocal.totime
519 - * convert days to seconds
520 - * subtract tbuff from t0 and add tbuff to t1
521 - * convert back to time string with the form 'ymd' using qalocal.time
522 - * write new values back to input dictionary
523 -
524 - '''
525 -# if not isinstance(tbuff, float):
526 -# casalog.post('Time buffer (tbuff) is not of type float', 'WARN')
527 -# return
528 -
529 - if 'timerange' in cmddict:
530 - timerange = cmddict['timerange']
531 - if timerange.find('~') != -1:
532 - t0,t1 = timerange.split('~',1)
533 - # start time
534 - startTime = qalocal.totime(t0)['value']
535 - startTimeSec = (startTime * 24 * 3600) - tbuff
536 - startTimeSec = qalocal.quantity(startTimeSec, 's')
537 - paddedT0 = qalocal.time(startTimeSec,form='ymd',prec=9)[0]
538 - # end time
539 - endTime = qalocal.totime(t1)['value']
540 - endTimeSec = (endTime * 24 * 3600) + tbuff
541 - endTimeSec = qalocal.quantity(endTimeSec, 's')
542 - paddedT1 = qalocal.time(endTimeSec,form='ymd',prec=9)[0]
543 -
544 - # update the original dictionary
545 - cmddict['timerange'] = paddedT0+'~'+paddedT1
546 -
547 - return
548 -
549 396 def applyTimeBufferList(alist, tbuff=None):
550 397 ''' Apply in-place a time buffer to ALL timerange parameters of a
551 398 list of dictionaries with several flag commands. It will do the following:
552 399
553 400 alist --> list of dictionaries with flag commands.
554 401 Ex: [{'antenna':'DV01', 'timerange':'2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'},
555 402 {'antenna':'DV02', 'timerange':'2013/10/15/10:25:30.110~2013/10/15/10:25:32.230'},
556 403 ...]
557 404 tbuff --> float value or list of 2 values of time buffer to apply to all timerange parameters.
558 405 When tbuff is a list of 2 values, the first value is applied to the lower time,
577 424 if isinstance(tbuff, list) and len(tbuff) == 2:
578 425 tbuff0 = tbuff[0]
579 426 tbuff1 = tbuff[1]
580 427 elif isinstance(tbuff, list) and len(tbuff) == 1:
581 428 tbuff0 = tbuff1 = tbuff[0]
582 429 elif isinstance(tbuff, float):
583 430 tbuff0 = tbuff1 = tbuff
584 431 else:
585 432 casalog.post('Time buffer (tbuff) is not of type float or list', 'WARN')
586 433 return
587 -
434 +
435 + def get_date_format(timestamp):
436 + '''Guess date-time string format to use in quanta tool time conversion,
437 + depending on whether the date seems to be present (as in
438 + 2013/11/15/10:35:05)
439 + '''
440 + if timestamp.count('/') == 3:
441 + return 'ymd'
442 + else:
443 + return ''
444 +
588 445 for cmddict in alist:
589 446 if 'timerange' in cmddict:
590 447 timerange = cmddict['timerange']
591 448 if timerange.find('~') != -1:
592 - t0,t1 = timerange.split('~',1)
449 + t0, t1 = timerange.split('~',1)
450 + date_time_format_t0 = get_date_format(t0)
451 + date_time_format_t1 = get_date_format(t1)
452 +
593 453 # start time
594 454 startTime = qalocal.totime(t0)['value']
595 455 startTimeSec = (startTime * 24 * 3600) - tbuff0
596 456 startTimeSec = qalocal.quantity(startTimeSec, 's')
597 - paddedT0 = qalocal.time(startTimeSec,form='ymd',prec=9)[0]
457 + paddedT0 = qalocal.time(startTimeSec, form=date_time_format_t0, prec=9)[0]
598 458 # end time
599 459 endTime = qalocal.totime(t1)['value']
600 460 endTimeSec = (endTime * 24 * 3600) + tbuff1
601 461 endTimeSec = qalocal.quantity(endTimeSec, 's')
602 - paddedT1 = qalocal.time(endTimeSec,form='ymd',prec=9)[0]
462 + paddedT1 = qalocal.time(endTimeSec, form=date_time_format_t1, prec=9)[0]
603 463
604 464 # update the original dictionary
605 465 cmddict['timerange'] = paddedT0+'~'+paddedT1
606 466
607 467 return
608 468
609 469 def parseDictionary(cmdlist, reason='any', shadow=True):
610 470 '''Create a dictionary after parsing a list of flag commands.
611 471 If reason is different than 'any', only the selected
612 472 rows will be parsed to the final dictionary.

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut