diff options
author | Aaron LI <aly@aaronly.me> | 2017-11-24 13:29:46 +0800 |
---|---|---|
committer | Aaron LI <aly@aaronly.me> | 2017-11-24 13:29:46 +0800 |
commit | e0606e038299a2c9262bfc45085d454c5a473079 (patch) | |
tree | c7f3b089f63e141852ac99cf569530bd23a2101d | |
parent | 13163368bfaeaa7e2087d64a5b581d314c47018d (diff) | |
download | atoolbox-e0606e038299a2c9262bfc45085d454c5a473079.tar.bz2 |
astro/wsclean.py: Capture wsclean output and save into a log file
-rwxr-xr-x | astro/oskar/wsclean.py | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/astro/oskar/wsclean.py b/astro/oskar/wsclean.py index f331e32..76e9229 100755 --- a/astro/oskar/wsclean.py +++ b/astro/oskar/wsclean.py @@ -11,26 +11,46 @@ import os +import sys import re import argparse import subprocess import time -def wsclean(args, dryrun=False): +def printlog(msg, logfile=None, **kwargs): + if logfile: + files = [sys.stdout, logfile] + else: + files = [sys.stdout] + for f in files: + print(msg, file=f, **kwargs) + + +def wsclean(args, dryrun=False, logfile=None): # NOTE: Convert all arguments to strings cmd = ["wsclean"] + [str(arg) for arg in args] - print("CMD: %s" % " ".join(cmd)) + printlog("CMD: %s" % " ".join(cmd), logfile=logfile) if dryrun: print(">>> DRY RUN MODE <<<") return t1 = time.perf_counter() - subprocess.check_call(cmd) + with subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + universal_newlines=True) as proc: + for line in proc.stdout: + printlog(line.strip(), logfile=logfile) + retcode = proc.wait() + if retcode: + raise subprocess.CalledProcessError(retcode, cmd) t2 = time.perf_counter() - print("-----------------------------------------------------------") - print("WSClean Elapsed time: %.1f [min]" % ((t2-t1)/60)) - print("-----------------------------------------------------------") + printlog("-----------------------------------------------------------", + logfile=logfile) + printlog("WSClean Elapsed time: %.1f [min]" % ((t2-t1)/60), + logfile=logfile) + printlog("-----------------------------------------------------------", + logfile=logfile) def main(): @@ -174,7 +194,13 @@ def main(): cmdargs += ["-name", nameprefix] cmdargs += args.ms - wsclean(cmdargs, dryrun=args.dryrun) + if args.dryrun: + logfile = None + else: + logfilename = nameprefix + "-wsclean.log" + logfile = open(logfilename, "w") + logfile.write(" ".join(sys.argv) + "\n") + wsclean(cmdargs, dryrun=args.dryrun, logfile=logfile) if args.dirty and not args.dryrun: # Remove the output "-image" since it is identical to "-dirty" |