Source code for pipeline.hifv.cli.gotasks.hifv_selfcal

##################### generated by xml-casa (v2) from hifv_selfcal.xml ##############
##################### 8962126e17075b0055ad65043b3ac09c ##############################
from __future__ import absolute_import
from casashell.private.stack_manip import find_local as __sf__
from casashell.private.stack_manip import find_frame as _find_frame
from casatools.typecheck import validator as _pc
from casatools.coercetype import coerce as _coerce
from pipeline.hifv.cli import hifv_selfcal as _hifv_selfcal_t
from collections import OrderedDict
import numpy
import sys
import os

import shutil

[docs]def static_var(varname, value): def decorate(func): setattr(func, varname, value) return func return decorate
class _hifv_selfcal: """ hifv_selfcal ---- Selfcal task executing gaincal and applycal Output: results -- If pipeline mode is 'getinputs' then None is returned. Otherwise the results object for the pipeline task is returned. --------- parameter descriptions --------------------------------------------- vis List of visibility data files. These may be ASDMs, tar files of ASDMs, MSes, or tar files of MSes, If ASDM files are specified, they will be converted to MS format. example: vis=['X227.ms', 'asdms.tar.gz'] pipelinemode The pipeline operating mode. In 'automatic' mode the pipeline determines the values of all context defined pipeline inputs automatically. In 'interactive' mode the user can set the pipeline context defined parameters manually. In 'getinputs' mode the user can check the settings of all pipeline parameters without running the task. dryrun Run the commands (True) or generate the commands to be run but do not execute (False). This is a pipeline task execution mode. acceptresults Add the results of the task to the pipeline context (True) or reject them (False). This is a pipeline task execution mode. refantignore String list of antennas to ignore combine Data axes which to combine for solve Options: '','obs','scan','spw',field', or any comma-separated combination in a single string Example: combine='scan,spw' - Extend solutions over scan boundaries (up to the solint), and combine spws for solving selfcalmode Heuristics mode selection refantmode Reference antenna mode overwrite_modelcol Always write the model column, even if it already exists --------- examples ----------------------------------------------------------- Examples 1. Basic selfcal task hifv_selfcal() """ _info_group_ = """pipeline""" _info_desc_ = """Selfcal task executing gaincal and applycal""" __schema = {'vis': {'type': 'cStrVec', 'coerce': [_coerce.to_list,_coerce.to_strvec]}, 'pipelinemode': {'type': 'cStr', 'coerce': _coerce.to_str, 'allowed': [ 'automatic', 'interactive', 'getinputs' ]}, 'dryrun': {'type': 'cBool'}, 'acceptresults': {'type': 'cBool'}, 'refantignore': {'type': 'cStr', 'coerce': _coerce.to_str}, 'combine': {'type': 'cStr', 'coerce': _coerce.to_str}, 'selfcalmode': {'type': 'cStr', 'coerce': _coerce.to_str}, 'refantmode': {'type': 'cStr', 'coerce': _coerce.to_str}, 'overwrite_modelcol': {'type': 'cBool'}} def __init__(self): self.__stdout = None self.__stderr = None self.__root_frame_ = None def __globals_(self): if self.__root_frame_ is None: self.__root_frame_ = _find_frame( ) assert self.__root_frame_ is not None, "could not find CASAshell global frame" return self.__root_frame_ def __to_string_(self,value): if type(value) is str: return "'%s'" % value else: return str(value) def __validate_(self,doc,schema): return _pc.validate(doc,schema) def __do_inp_output(self,param_prefix,description_str,formatting_chars): out = self.__stdout or sys.stdout description = description_str.split( ) prefix_width = 23 + 18 + 4 output = [ ] addon = '' first_addon = True while len(description) > 0: ## starting a new line..................................................................... if len(output) == 0: ## for first line add parameter information............................................ if len(param_prefix)-formatting_chars > prefix_width - 1: output.append(param_prefix) continue addon = param_prefix + ' #' first_addon = True addon_formatting = formatting_chars else: ## for subsequent lines space over prefix width........................................ addon = (' ' * prefix_width) + '#' first_addon = False addon_formatting = 0 ## if first word of description puts us over the screen width, bail........................ if len(addon + description[0]) - addon_formatting + 1 > self.term_width: ## if we're doing the first line make sure it's output................................. if first_addon: output.append(addon) break while len(description) > 0: ## if the next description word puts us over break for the next line................... if len(addon + description[0]) - addon_formatting + 1 > self.term_width: break addon = addon + ' ' + description[0] description.pop(0) output.append(addon) out.write('\n'.join(output) + '\n') #--------- return nonsubparam values ---------------------------------------------- def __combine_dflt( self, glb ): return 'spw,field' def __combine( self, glb ): if 'combine' in glb: return glb['combine'] return 'spw,field' def __refantignore_dflt( self, glb ): return '' def __refantignore( self, glb ): if 'refantignore' in glb: return glb['refantignore'] return '' def __selfcalmode_dflt( self, glb ): return 'VLASS' def __selfcalmode( self, glb ): if 'selfcalmode' in glb: return glb['selfcalmode'] return 'VLASS' def __pipelinemode_dflt( self, glb ): return 'automatic' def __pipelinemode( self, glb ): if 'pipelinemode' in glb: return glb['pipelinemode'] return 'automatic' def __refantmode_dflt( self, glb ): return 'strict' def __refantmode( self, glb ): if 'refantmode' in glb: return glb['refantmode'] return 'strict' def __overwrite_modelcol_dflt( self, glb ): return False def __overwrite_modelcol( self, glb ): if 'overwrite_modelcol' in glb: return glb['overwrite_modelcol'] return False #--------- return non subparam/when values --------------------------------------------- def __vis( self, glb ): if 'vis' in glb: return glb['vis'] return [ ] #--------- return inp/go default -------------------------------------------------- def __acceptresults_dflt( self, glb ): if self.__pipelinemode( glb ) == "interactive": return bool(True) return None def __dryrun_dflt( self, glb ): if self.__pipelinemode( glb ) == "interactive": return bool(False) return None def __vis_dflt( self, glb ): if self.__pipelinemode( glb ) == "interactive": return [] if self.__pipelinemode( glb ) == "getinputs": return [] return None #--------- return subparam values ------------------------------------------------- def __dryrun( self, glb ): if 'dryrun' in glb: return glb['dryrun'] dflt = self.__dryrun_dflt( glb ) if dflt is not None: return dflt return False def __acceptresults( self, glb ): if 'acceptresults' in glb: return glb['acceptresults'] dflt = self.__acceptresults_dflt( glb ) if dflt is not None: return dflt return True #--------- subparam inp output ---------------------------------------------------- def __vis_inp(self): description = 'List of input visibility data' value = self.__vis( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'vis': value},{'vis': self.__schema['vis']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output('%-18.18s = %s%-23s%s' % ('vis',pre,self.__to_string_(value),post),description,0+len(pre)+len(post)) def __pipelinemode_inp(self): description = 'The pipeline operating mode' value = self.__pipelinemode( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'pipelinemode': value},{'pipelinemode': self.__schema['pipelinemode']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output('\x1B[1m\x1B[47m%-18.18s =\x1B[0m %s%-23s%s' % ('pipelinemode',pre,self.__to_string_(value),post),description,13+len(pre)+len(post)) def __dryrun_inp(self): if self.__dryrun_dflt( self.__globals_( ) ) is not None: description = 'Run the task (False) or display task command (True)' value = self.__dryrun( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'dryrun': value},{'dryrun': self.__schema['dryrun']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output(' \x1B[92m%-15.15s =\x1B[0m %s%-23s%s' % ('dryrun',pre,self.__to_string_(value),post),description,9+len(pre)+len(post)) def __acceptresults_inp(self): if self.__acceptresults_dflt( self.__globals_( ) ) is not None: description = 'Add the results into the pipeline context' value = self.__acceptresults( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'acceptresults': value},{'acceptresults': self.__schema['acceptresults']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output(' \x1B[92m%-15.15s =\x1B[0m %s%-23s%s' % ('acceptresults',pre,self.__to_string_(value),post),description,9+len(pre)+len(post)) def __refantignore_inp(self): description = 'String list of antennas to ignore' value = self.__refantignore( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'refantignore': value},{'refantignore': self.__schema['refantignore']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output('%-18.18s = %s%-23s%s' % ('refantignore',pre,self.__to_string_(value),post),description,0+len(pre)+len(post)) def __combine_inp(self): description = 'Data axes which to combine for solve (obs, scan, spw, and/or field)' value = self.__combine( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'combine': value},{'combine': self.__schema['combine']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output('%-18.18s = %s%-23s%s' % ('combine',pre,self.__to_string_(value),post),description,0+len(pre)+len(post)) def __selfcalmode_inp(self): description = 'Heuristics mode selection' value = self.__selfcalmode( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'selfcalmode': value},{'selfcalmode': self.__schema['selfcalmode']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output('%-18.18s = %s%-23s%s' % ('selfcalmode',pre,self.__to_string_(value),post),description,0+len(pre)+len(post)) def __refantmode_inp(self): description = 'Reference antenna mode' value = self.__refantmode( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'refantmode': value},{'refantmode': self.__schema['refantmode']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output('%-18.18s = %s%-23s%s' % ('refantmode',pre,self.__to_string_(value),post),description,0+len(pre)+len(post)) def __overwrite_modelcol_inp(self): description = 'Always write the model column, even if it already exists' value = self.__overwrite_modelcol( self.__globals_( ) ) (pre,post) = ('','') if self.__validate_({'overwrite_modelcol': value},{'overwrite_modelcol': self.__schema['overwrite_modelcol']}) else ('\x1B[91m','\x1B[0m') self.__do_inp_output('%-18.18s = %s%-23s%s' % ('overwrite_modelcol',pre,self.__to_string_(value),post),description,0+len(pre)+len(post)) #--------- global default implementation------------------------------------------- @static_var('state', __sf__('casa_inp_go_state')) def set_global_defaults(self): self.set_global_defaults.state['last'] = self glb = self.__globals_( ) if 'dryrun' in glb: del glb['dryrun'] if 'refantignore' in glb: del glb['refantignore'] if 'pipelinemode' in glb: del glb['pipelinemode'] if 'selfcalmode' in glb: del glb['selfcalmode'] if 'vis' in glb: del glb['vis'] if 'acceptresults' in glb: del glb['acceptresults'] if 'overwrite_modelcol' in glb: del glb['overwrite_modelcol'] if 'combine' in glb: del glb['combine'] if 'refantmode' in glb: del glb['refantmode'] #--------- inp function ----------------------------------------------------------- def inp(self): print("# hifv_selfcal -- %s" % self._info_desc_) self.term_width, self.term_height = shutil.get_terminal_size(fallback=(80, 24)) self.__vis_inp( ) self.__pipelinemode_inp( ) self.__dryrun_inp( ) self.__acceptresults_inp( ) self.__refantignore_inp( ) self.__combine_inp( ) self.__selfcalmode_inp( ) self.__refantmode_inp( ) self.__overwrite_modelcol_inp( ) #--------- tget function ---------------------------------------------------------- @static_var('state', __sf__('casa_inp_go_state')) def tget(self,file=None): from casashell.private.stack_manip import find_frame from runpy import run_path filename = None if file is None: if os.path.isfile("hifv_selfcal.last"): filename = "hifv_selfcal.last" elif isinstance(file, str): if os.path.isfile(file): filename = file if filename is not None: glob = find_frame( ) newglob = run_path( filename, init_globals={ } ) for i in newglob: glob[i] = newglob[i] self.tget.state['last'] = self else: print("could not find last file, setting defaults instead...") self.set_global_defaults( ) def __call__( self, vis=None, pipelinemode=None, dryrun=None, acceptresults=None, refantignore=None, combine=None, selfcalmode=None, refantmode=None, overwrite_modelcol=None ): def noobj(s): if s.startswith('<') and s.endswith('>'): return "None" else: return s _prefile = os.path.realpath('hifv_selfcal.pre') _postfile = os.path.realpath('hifv_selfcal.last') _return_result_ = None _arguments = [vis,pipelinemode,dryrun,acceptresults,refantignore,combine,selfcalmode,refantmode,overwrite_modelcol] _invocation_parameters = OrderedDict( ) if any(map(lambda x: x is not None,_arguments)): # invoke python style # set the non sub-parameters that are not None local_global = { } if vis is not None: local_global['vis'] = vis if pipelinemode is not None: local_global['pipelinemode'] = pipelinemode if refantignore is not None: local_global['refantignore'] = refantignore if combine is not None: local_global['combine'] = combine if selfcalmode is not None: local_global['selfcalmode'] = selfcalmode if refantmode is not None: local_global['refantmode'] = refantmode if overwrite_modelcol is not None: local_global['overwrite_modelcol'] = overwrite_modelcol # the invocation parameters for the non-subparameters can now be set - this picks up those defaults _invocation_parameters['vis'] = self.__vis( local_global ) _invocation_parameters['pipelinemode'] = self.__pipelinemode( local_global ) _invocation_parameters['refantignore'] = self.__refantignore( local_global ) _invocation_parameters['combine'] = self.__combine( local_global ) _invocation_parameters['selfcalmode'] = self.__selfcalmode( local_global ) _invocation_parameters['refantmode'] = self.__refantmode( local_global ) _invocation_parameters['overwrite_modelcol'] = self.__overwrite_modelcol( local_global ) # the sub-parameters can then be set. Use the supplied value if not None, else the function, which gets the appropriate default _invocation_parameters['dryrun'] = self.__dryrun( _invocation_parameters ) if dryrun is None else dryrun _invocation_parameters['acceptresults'] = self.__acceptresults( _invocation_parameters ) if acceptresults is None else acceptresults else: # invoke with inp/go semantics _invocation_parameters['vis'] = self.__vis( self.__globals_( ) ) _invocation_parameters['pipelinemode'] = self.__pipelinemode( self.__globals_( ) ) _invocation_parameters['dryrun'] = self.__dryrun( self.__globals_( ) ) _invocation_parameters['acceptresults'] = self.__acceptresults( self.__globals_( ) ) _invocation_parameters['refantignore'] = self.__refantignore( self.__globals_( ) ) _invocation_parameters['combine'] = self.__combine( self.__globals_( ) ) _invocation_parameters['selfcalmode'] = self.__selfcalmode( self.__globals_( ) ) _invocation_parameters['refantmode'] = self.__refantmode( self.__globals_( ) ) _invocation_parameters['overwrite_modelcol'] = self.__overwrite_modelcol( self.__globals_( ) ) try: with open(_prefile,'w') as _f: for _i in _invocation_parameters: _f.write("%-18s = %s\n" % (_i,noobj(repr(_invocation_parameters[_i])))) _f.write("#hifv_selfcal( ") count = 0 for _i in _invocation_parameters: _f.write("%s=%s" % (_i,noobj(repr(_invocation_parameters[_i])))) count += 1 if count < len(_invocation_parameters): _f.write(",") _f.write(" )\n") except: pass try: _return_result_ = _hifv_selfcal_t( _invocation_parameters['vis'],_invocation_parameters['pipelinemode'],_invocation_parameters['dryrun'],_invocation_parameters['acceptresults'],_invocation_parameters['refantignore'],_invocation_parameters['combine'],_invocation_parameters['selfcalmode'],_invocation_parameters['refantmode'],_invocation_parameters['overwrite_modelcol'] ) except Exception as e: from traceback import format_exc from casatasks import casalog casalog.origin('hifv_selfcal') casalog.post("Exception Reported: Error in hifv_selfcal: %s" % str(e),'SEVERE') casalog.post(format_exc( )) _return_result_ = False try: os.rename(_prefile,_postfile) except: pass return _return_result_ hifv_selfcal = _hifv_selfcal( )