From 2eb2b3547637cbcc65683537480a09fb4fbb95a4 Mon Sep 17 00:00:00 2001 From: bennahugo Date: Wed, 13 May 2020 13:41:32 +0200 Subject: [PATCH] fixes polcal 47 --- stimela/cargo/cab/casa47_polcal/src/run.py | 49 +++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/stimela/cargo/cab/casa47_polcal/src/run.py b/stimela/cargo/cab/casa47_polcal/src/run.py index 594318a7..ebe25566 100644 --- a/stimela/cargo/cab/casa47_polcal/src/run.py +++ b/stimela/cargo/cab/casa47_polcal/src/run.py @@ -2,17 +2,21 @@ import sys import logging import Crasa.Crasa as crasa - -sys.path.append("/scratch/stimela") - -utils = __import__('utils') +from casacore.tables import table +import numpy +import glob +import yaml +import shutil CONFIG = os.environ["CONFIG"] INPUT = os.environ["INPUT"] OUTPUT = os.environ["OUTPUT"] MSDIR = os.environ["MSDIR"] -cab = utils.readJson(CONFIG) +with open(CONFIG, "r") as _std: + cab = yaml.safe_load(_std) + +junk = cab["junk"] args = {} for param in cab['parameters']: @@ -25,4 +29,37 @@ args[name] = value task = crasa.CasaTask(cab["binary"], **args) -task.run() +try: + task.run() +finally: + for item in junk: + for dest in [OUTPUT, MSDIR]: # these are the only writable volumes in the container + items = glob.glob("{dest}/{item}".format(**locals())) + for f in items: + if os.path.isfile(f): + os.remove(f) + elif os.path.isdir(f): + shutil.rmtree(f) + # Leave other types + +gtab = args["caltable"] +if not os.path.exists(gtab): + raise RuntimeError("The gaintable was not created. Please refer to CASA {0:s} logfile for further details".format(cab["binary"])) + +tab = table(gtab) +field_ids = numpy.unique(tab.getcol("FIELD_ID")) +tab.close() + +tab = table(gtab+"::FIELD") +field_names = tab.getcol("NAME") +tab.close() + +field_in = args["field"].split(",") + +try: + ids = map(int, field_in) +except ValueError: + ids = map(lambda a: field_names.index(a), field_in) + +if not set(ids).intersection(field_ids): + raise RuntimeError("None of the fields has solutions after the calibration. Please refer to CASA the {} logfile for further details".format(cab["binary"]))