casa
$Rev:20696$
|
00001 # 00002 # This file was generated using xslt from its XML file 00003 # 00004 # Copyright 2008, Associated Universities Inc., Washington DC 00005 # 00006 import sys 00007 import os 00008 #from casac import * 00009 import casac 00010 import string 00011 import time 00012 import inspect 00013 import gc 00014 import numpy 00015 from odict import odict 00016 from taskmanager import tm 00017 from task_flagcmd import flagcmd 00018 class flagcmd_cli_: 00019 __name__ = "flagcmd" 00020 __async__ = {} 00021 rkey = None 00022 i_am_a_casapy_task = None 00023 # The existence of the i_am_a_casapy_task attribute allows help() 00024 # (and other) to treat casapy tasks as a special case. 00025 00026 def __init__(self) : 00027 self.__bases__ = (flagcmd_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'vis':None, 'inpmode':None, 'inpfile':None, 'tablerows':None, 'reason':None, 'useapplied':None, 'tbuff':None, 'ants':None, 'action':None, 'flagbackup':None, 'clearall':None, 'rowlist':None, 'plotfile':None, 'savepars':None, 'outfile':None, 'async':None} 00031 00032 00033 def result(self, key=None): 00034 #### here we will scan the task-ids in __async__ 00035 #### and add any that have completed... 00036 if key is not None and self.__async__.has_key(key) and self.__async__[key] is not None: 00037 ret = tm.retrieve(self.__async__[key]) 00038 if ret['state'] == "done" : 00039 self.__async__[key] = None 00040 elif ret['state'] == 'crashed' : 00041 self.__async__[key] = None 00042 return ret 00043 return None 00044 00045 00046 def __call__(self, vis=None, inpmode=None, inpfile=None, tablerows=None, reason=None, useapplied=None, tbuff=None, ants=None, action=None, flagbackup=None, clearall=None, rowlist=None, plotfile=None, savepars=None, outfile=None, async=None): 00047 00048 """Flagging task based on batches of flag-commands 00049 00050 Flag commands follow the mode and parameter names from the flagdata task 00051 (also explained below). The available modes are: manual, clip, shadow, quack, 00052 elevation, tfcrop, rflag, extend and unflag. 00053 00054 The flagcmd task will flag data based on the commands input on inpmode: 00055 table = input from FLAG_CMD table in MS 00056 list = input from a file or a (Python) list of command strings 00057 xml = input from Flag.xml in the MS given by vis 00058 00059 Batch operations include : apply/unapply/list/plot/clear/extract 00060 00061 IMPORTANT: The FLAG_CMD sub-table is meant only for meta-data selections such as online flags. 00062 Using it to save other parameters (from modes such as clip, quack, shadow, etc) is 00063 possible but carries a risk that in future releases these parameters maybe renamed 00064 or changed their default values. Use it at your own risk! There will be no automatic 00065 way to rename any parameter that changes in the future. 00066 00067 NOTE on flagging calibration tables. 00068 ----------------------------------- 00069 00070 It is possible to flag cal tables using this task, although we recommend using the flagdata 00071 task for this. 00072 00073 When using this task to flag cal tables, only the apply and list actions are supported. 00074 Because cal tables do not have a FLAG_CMD sub-table, the default inpmode='table' can only 00075 be used if an MS is given in the inpfile parameter so that flags from the MS are applied to 00076 the cal table. Otherwise, the flag commands must be given using inpmode='list', either from a 00077 file or from a list of strings. See below for more information about these parameters. 00078 Data selection for calibration tables is limited to field, antenna and spw at the moment. 00079 00080 00081 Keyword arguments: 00082 00083 vis -- Name of input visibility file or calibration table. 00084 default: '' (none) 00085 example1: vis='uid___A002_X2a5c2f_X54.ms' or 00086 example2: vis='cal-X54.B1' 00087 00088 -- INPUT of flag commands -- 00089 00090 inpmode -- Input mode for flag commands. 00091 options: 'table','list','xml' 00092 default: 'table' 00093 00094 inpmode "table" -- input commands from FLAG_CMD table of MS. 00095 00096 inpfile -- path to MS containing FLAG_CMD 00097 default: '' (read from FLAG_CMD table in the MS specified via 'vis') 00098 00099 Main use is to read flags from internal FLAG_CMD, 00100 but by setting inpfile to a different MS you can 00101 use this to copy the flags from one MS to another. 00102 00103 One use case is to read the flag commands from an MS given in 00104 inpfile and apply them to a cal table given in vis. Example: 00105 00106 flagcmd(vis='cal-X54.B1', inpmode='table', inpfile='uid___A002_X2a5c2f_X54.ms', action='apply') 00107 00108 tablerows -- list of rows of the FLAG_CMD table to read 00109 default: [] (read all rows) 00110 example: [0,1,2,10] 00111 00112 NOTE: currently only takes integer lists, not 00113 parseable strings with ranges. Use the Python 00114 range function to generate ranges, e.g. 00115 tablerows = range(0,30) + range(50,55) 00116 instead of '0~29,50~54' for now. 00117 00118 reason -- select flag commands based on REASON(s) 00119 default: 'any' (all flags regardless of reason) 00120 can be a string, or list of strings 00121 example: reason='FOCUS_ERROR' 00122 reason=['FOCUS_ERROR','SUBREFLECTOR_ERROR'] 00123 00124 NOTE: what is within the string is literally 00125 mateched, e.g. reason='' matches only blank reasons, 00126 and reason = 'FOCUS_ERROR,SUBREFLECTOR_ERROR' 00127 matches this compound reason string only 00128 00129 useapplied -- select the flag commands of rows that have APPLIED=True 00130 options: True,False 00131 default: False 00132 00133 If useapplied=True it will read in both applied and 00134 unapplied flags 00135 00136 00137 inpmode "list" -- input commands from an ASCII file or via a list of strings. 00138 00139 inpfile -- name of an ASCII file or a list of strings. 00140 default: '' 00141 options: [] or '' with filename. 00142 example1: the following commands can be saved into a file and given 00143 to the task (e.g. save it to flags.txt). 00144 00145 scan='1~3' mode='manual' 00146 mode='clip' clipminmax=[0,2] correlation='ABS_XX' clipoutside=False 00147 spw='9' mode='tfcrop' correlation='ABS_YY' ntime=51.0 00148 mode='extend' extendpols=True 00149 00150 flagcmd(vis, inpmode='list',inpfile='flags.txt') 00151 00152 example2: the same commands can be written in a Python list of strings and given 00153 to the task. 00154 cmd=["scan='1~3' mode='manual'", 00155 "mode='clip' clipminmax=[0,2] correlation='ABS_XX' clipoutside=False", 00156 "spw='9' mode='tfcrop' correlation='ABS_YY' ntime=51.0", 00157 "mode='extend' extendpols=True"] 00158 00159 flagcmd(vis, inpmode='list',inpfile=cmd) 00160 00161 00162 reason -- select flag commands based on REASON(s) 00163 default: 'any' (all flags regardless of reason) 00164 can be a string, or list of strings 00165 example: reason='FOCUS_ERROR' 00166 reason=['FOCUS_ERROR','SUBREFLECTOR_ERROR'] 00167 00168 NOTE: what is within the string is literally 00169 mateched, e.g. reason='' matches only blank reasons, 00170 and reason = 'FOCUS_ERROR,SUBREFLECTOR_ERROR' 00171 matches this compound reason string only 00172 00173 00174 inpmode "xml" -- input online flags from Flag.xml file in the MS. This mode 00175 is not available for cal tables. 00176 00177 tbuff -- (float) time padding buffer (seconds, default=1.0) 00178 00179 ants -- select flags based on antenna, 00180 e.g. antenna='ea01' 00181 default: '' (all flags regardless of antenna) 00182 00183 reason -- select flag commands based on REASON(s), 00184 default: 'Any' (all flags regardless of reason) 00185 can be a string, or list of strings 00186 example: reason='FOCUS_ERROR' 00187 reason=['FOCUS_ERROR','SUBREFLECTOR_ERROR'] 00188 00189 NOTE: what is within the string is literally 00190 mateched, e.g. reason='' matches only blank reasons, 00191 and reason = 'FOCUS_ERROR,SUBREFLECTOR_ERROR' 00192 matches this compound reason string only 00193 00194 --ACTIONS-- 00195 00196 action -- operation to perform on MS and/or in flag commands from inpfile 00197 options: 'apply','clear','list','plot','unapply','extract' 00198 default: 'apply' 00199 00200 action "apply" -- apply flags to MS or cal table. 00201 00202 This operation will apply the commands chosen by inpmode. 00203 If inpmode='table' it will set the APPLIED column to True. 00204 00205 flagbackup -- Automatically backup MS/cal table FLAG column before applying. 00206 options: True,False 00207 default: True 00208 00209 00210 action "unapply" -- unapply flags in MS. (Not available for cal tables) 00211 00212 This operation will unapply the commands chosen by inpmode. 00213 If inpmode='table' it will set the APPLIED column to False. 00214 00215 IMPORTANT: In order to guarantee that only the data selected in 00216 the command is unapplied, the framework will first unapply the selected 00217 rows and then re-apply the overlapping data that got unapplied in the 00218 first pass. This is a true unapply action, but it will take longer to 00219 process because it will re-apply all the remaining commands that have 00220 APPLIED = True! 00221 00222 flagbackup -- Automatically backup MS FLAG column before unapplying? 00223 options: True,False 00224 default: True 00225 00226 00227 action "list" -- list and/or save flag commands. 00228 00229 This operation will list the commands chosen by inpmode on the screen 00230 and save them to the MS or to a file without applying. It will save the commands 00231 to outfile if the parameter savepars is set to True. If outfile is None, it 00232 will save the commands to the MS given in vis. 00233 00234 00235 action "plot" -- plot flags (ant vs. time). (Not available for cal tables) 00236 00237 This operation will plot the flags chosen by inpmode to a 00238 matplotlib gui or to a file. These will be sorted by 00239 antenna vs. time. Most useful for showing the online 00240 flags. 00241 00242 plotfile -- output plot file 00243 default: '' (plot to matplotlib window) 00244 00245 WARNING: will only reliably plot individual flags 00246 per antenna and timerange (e.g. direct from xml) 00247 00248 action "clear" -- clear flags from FLAG_CMD in MS. (Not available for cal tables) 00249 00250 This operation will delete the selected flag rows from 00251 the internal FLAG_CMD table of the MS. 00252 00253 NOTE: choosing this option will disregard anything you 00254 set in inpmode and will always work on the FLAG_CMD table 00255 in vis. 00256 00257 clearall -- really clear all flags? 00258 default: False (will not clear) 00259 00260 rowlist -- list of FLAG_CMD rows to clear 00261 default: [] (all flags in table) 00262 example: [0,1,2,10] 00263 00264 NOTE: currently only takes integer lists, not 00265 parseable strings with ranges. Use the Python 00266 range function to generate ranges, e.g. 00267 rowlist = range(0,30) + range(50,55) 00268 instead of '0~29,50~54' for now. 00269 00270 WARNING: this can be dangerous, and you must set clearall=True 00271 to use this!!! This will delete the specified rows from the 00272 internal FLAG_CMD table for vis regardless of what mode is set 00273 to (useful for when you import from xml or file), and decide to 00274 redo it). This action will NOT unapply the commands. 00275 00276 00277 action "extract" -- extract internal flag dictionary. (Not available for cal tables) 00278 00279 This option will return the internal flagging dictionary to 00280 python. 00281 00282 00283 savepars -- Save the flag commands to the FLAG_CMD table of the MS or to an output text file. 00284 default: False 00285 options: True/False 00286 00287 outfile -- Name of output file to save the flag commands. 00288 default: ' '; it will save the commands in the FLAG_CMD table of the MS. 00289 example: outfile='flags.txt' will save the parameters in a text file. 00290 00291 =========================================================================== 00292 00293 -- Internal FLAG_CMD input 'inpmode' useage -- 00294 00295 (For inpmode='table') 00296 00297 * It is a good idea to use action='list' first to see what is there 00298 before doing anything else, e.g. 00299 00300 inpmode = 'table' 00301 action = 'list' 00302 00303 * To apply the flags stored in the FLAG_CMD table in the MS, 00304 simply set inpmode='table' and action='apply', e.g. 00305 00306 inpmode = 'table' 00307 action = 'apply' 00308 useapplied = False 00309 00310 This is the default setup of flagcmd. 00311 00312 * To merely save to FLAG_CMD but not apply, then 00313 00314 inpmode = 'table' 00315 inpfile = 'other.ms' 00316 action = 'list' 00317 00318 * To save commands from a file into the MS without applying. 00319 00320 inpmode = 'list' 00321 inpfile = 'flags.txt' 00322 action = 'list' 00323 00324 If you need to select only certain rows from the FLAG_CMD table, 00325 use the tablerows parameter to control this. Currently this must 00326 be a list of individual row numbers (0-based), e.g. 00327 00328 tablerows = [0,1,2,3,10,11] 00329 00330 or 00331 00332 tablerows = range(29) 00333 00334 NOTE: the useapplied=True/False tag is important if you are 00335 going to (re)apply flags marked as APPLIED True in FLAG_CMD. 00336 It is common to have a "failed" flagging operation mark the flags 00337 as already applied and then they don't show up when you re-run 00338 (e.g. in 'list'). Set useapplied=True so that it will use these 00339 anyway. 00340 00341 * To apply the flag commands from an MS to a calibration table. 00342 00343 vis = 'mycaltable' 00344 inpmode = 'table' 00345 inpfile = 'myMS.ms' 00346 action = 'apply' 00347 00348 00349 -- Online flag input inpmode useage -- 00350 00351 (For inpmode='xml') 00352 00353 * To list the online flags stored in the Flag.xml file in the 00354 MS, simply set: 00355 00356 inpmode = 'xml' 00357 action = 'list' 00358 savepars = False 00359 00360 * It is then straightforward to save these to FLAG_CMD 00361 00362 inpmode = 'xml' 00363 action = 'list' 00364 savepars = True 00365 00366 * To directly apply the online flags stored in the Flag.xml file in the 00367 MS, set inpmode='xml' and desired buffer, e.g. 00368 00369 inpmode = 'xml' 00370 tbuff = 1.0 # pad flag times by 1 sec 00371 action = 'apply' 00372 set savepars to save or not the commands in the MS 00373 00374 * You can also specify a set of reasons (a comma separated list) 00375 for flags to apply, e.g. 00376 00377 reason = 'FOCUS_ERROR,SUBREFLECTOR_ERROR' # select these flags 00378 reason = 'ANTENNA_NOT_ON_SOURCE' 00379 00380 NOTE: The online flag time buffer tbuff is specified in 00381 seconds, but in fact should be keyed to the intrinsic online 00382 integration time. This is particularly true for EVLA data, 00383 were a tbuff value of 0.5x to 1.5x the integration time is 00384 needed (currently you should use 1.5x for data taken in 00385 early 2011 or before). 00386 00387 Because the Flag.xml is copied to the MS by importevla, 00388 you can re-apply the online flags with an increased tbuff 00389 simply by running with inpmode='xml' and optype='apply', e.g. 00390 00391 inpmode = 'xml' 00392 tbuff = 15.0 # pad flag times by 15 sec for 10sec integrations 00393 optype = 'apply' 00394 00395 if you originally used a smaller value (e.g. 1.0) by mistake 00396 or you want to try longer values. Note these will be added to 00397 the FLAG_CMD table which you would have to clean up manually 00398 if you care about this. 00399 00400 00401 -- Flag command useage -- 00402 00403 (For inpmode='list') 00404 00405 * For example, a series of commands might be: 00406 00407 antenna='ea01' timerange='00:00:00~01:00:00' 00408 antenna='ea11' timerange='00:00:00~03:00:00' spw='0~4' 00409 mode='clip' clipminmax=[0,5] correlation='ABS_ALL' 00410 mode='quack' quackmode='end' quackinterval=1.0 00411 mode='shadow' 00412 00413 Any other mode can also use selection (see the help of flagdata): 00414 00415 mode='shadow' antenna='ea01,ea02,ea03' 00416 mode='quack' quackmode='end' quackinterval=1.0 antenna='ea22' 00417 00418 These commands can be saved in an ASCII file, e.g. "myflags.txt" 00419 and input using inpmode='list', e.g. 00420 00421 flagcmd(vis='myvis.ms',inpmode='list',inpfile='myflags.txt') 00422 00423 or input from the interface 00424 00425 flagcmd(vis='myvis.ms',inpmode='list', 00426 inpfile=["mode='shadow'", 00427 "mode='clip' clipminmax=[0,5] correlation='ABS_ALL'", 00428 "mode='quack' quackmode='end' quackinterval=1.0", 00429 "antenna='ea01' timerange='00:00:00~01:00:00'", 00430 "antenna='ea11' timerange='00:00:00~03:00:00' spw='0~4'"]) 00431 00432 00433 -- ACTIONs -- 00434 00435 The action parameter controls what flagcmd will actually do with the 00436 flag commands: 00437 00438 * action = 'apply' 00439 00440 This will apply the selected commands to the data. 00441 00442 If inpmode='table' and inpfile='' then the APPLIED column in FLAG_CMD 00443 will be set to True. 00444 00445 * action = 'unapply' 00446 00447 This will effectively unapply any commands on the selected data. 00448 00449 * action = 'list' 00450 00451 List what is selected and or save in the MS or in a file. It is wise to do 00452 this first before doing any other action. It will list the output in the logger 00453 and save them to the FLAG_CMD table of the MS when savepars=True and outfile = '' or 00454 to a file if outfile is non-blank. 00455 00456 * action = 'plot' 00457 00458 Will pop up a little matplotlib GUI (if outfile='') or plot to a 00459 file. Currently only gives an antenna vs time plot, mostly useful 00460 for looking at the online flags. 00461 00462 * action = 'clear' 00463 00464 DANGER! This can be used to totally delete rows from the FLAG_CMD 00465 table. It ignores what inpmode is pointing to and always works 00466 on FLAG_CMD. Use at your own peril but sometimes you need to just 00467 blow that table away, e.g. 00468 00469 vis = msfile 00470 optype = 'clear' 00471 rowlist = [] # all rows 00472 clearall = True # disarm the safety 00473 00474 Note you have to explicitly set clearall=True to arm the deletion 00475 (a minimal precaution). 00476 00477 * action = 'extract' 00478 00479 This option will return the internal flagging dictionary to 00480 python. This will allow a power-user to manipulate these 00481 commands directly (e.g. for plotting etc.). For example, 00482 00483 myflagd = flagcmd(vis=msfile,useapplied=True,action='extract') 00484 00485 will extract all the commands (including those already applied) 00486 in the FLAG_CMD MS table. 00487 00488 NOTE: There is no extant description of the format of this 00489 dictionary, as it is an internal device used by the flagcmd 00490 task. This action is provided for the convenience of 00491 advanced users. 00492 00493 00494 ------- FLAG COMMAND SYNTAX ------- 00495 00496 The command syntax is based on the flagdata parameters. 00497 00498 Basic Syntax Rules 00499 00500 Commands are a string (which may contain internal "strings") consisting of 00501 KEY=VALUE pairs separated by whitespace (see examples below). 00502 00503 NOTE: There should be no whitespace between KEY=VALUE or within each KEY or 00504 VALUE, since the simple parser first breaks command lines on whitespace, 00505 then on "=". 00506 00507 Each key should only appear once on a given command line/string 00508 00509 There is an implicit "mode" for each command, with the default 00510 being 'manual' if not given. 00511 00512 Comment lines can start with '#' and will be ignored. 00513 00514 00515 1. Data selection parameters (used by all flagging modes) 00516 00517 timerange='' 00518 antenna='' 00519 spw='' 00520 correlation='' 00521 field='' 00522 scan='' 00523 feed='' 00524 array='' 00525 uvrange='' 00526 intent='' 00527 observation='' 00528 00529 Note: a command consisting only of selection key-value pairs is a 00530 basic "manual" operation, ie. flag the data meeting the selection. 00531 00532 00533 2. Modes specific parameters with default values (for further details and updated 00534 default values, refer to the task flagdata). 00535 00536 2.1 Mode manual. 00537 autocorr=False 00538 00539 2.2 Mode clip. 00540 datacolumn='DATA' 00541 clipminmax=[] 00542 clipoutside=True 00543 channelavg=False 00544 clipzeros=False 00545 00546 2.3 Mode shadow. 00547 tolerance=0.0 00548 addantenna='' 00549 00550 2.4 Mode quack. 00551 quackinterval=1.0 00552 quackmode='beg' 00553 quackincrement=False 00554 00555 2.5 Mode elevation. 00556 lowerlimit=0.0 00557 upperlimit=90.0 00558 00559 2.6 Mode tfcrop. 00560 ntime='scan' 00561 combinescans=False 00562 datacolumn='DATA' 00563 timecutoff=4.0 00564 freqcutoff=3.0 00565 timefit='line' 00566 freqfit='poly' 00567 maxnpieces=7 00568 flagdimension='freqtime' 00569 usewindowstats='none' 00570 halfwin=1 00571 00572 2.7 Mode extend. 00573 ntime='scan' 00574 combinescans=False 00575 extendpols=True 00576 growtime=50.0 00577 growfreq=50.0 00578 growaround=False 00579 flagneartime=False 00580 flagnearfreq=False 00581 00582 2.8 Mode rflag. 00583 ntime='scan' 00584 combinescans=False 00585 datacolumn='DATA' 00586 winsize=3 00587 timedev='' 00588 freqdev='' 00589 timedevscale=5.0 00590 freqdevscale=5.0 00591 spectralmax=1000000.0 00592 spectralmin=0.0 00593 00594 2.9 Mode unflag. 00595 00596 00597 3. Basic elaboration options for online and interface use 00598 00599 id='' # flag ID tag (not necessary) 00600 reason='' # reason string for flag 00601 flagtime='' # a timestamp for when this flag was generated (for 00602 user history use) 00603 00604 NOTE: there is no flagtime column in FLAG_CMD at 00605 this time, but we will propose to add this as an 00606 optional column 00607 00608 NOTE: These are currently ignored and not used. 00609 00610 4. Extended elaboration options for online and interface use 00611 Note: these are FLAG_CMD columns, but their use is not clear but included 00612 here for compatibility and future expansion 00613 00614 level=N # flagging "level" for flags with same reason 00615 severity=N # Severity code for the flag, on a scale of 0-10 in order 00616 of increasing severity; user specified 00617 00618 00619 00620 """ 00621 if not hasattr(self, "__globals__") or self.__globals__ == None : 00622 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00623 #casac = self.__globals__['casac'] 00624 casalog = self.__globals__['casalog'] 00625 #casalog = casac.casac.logsink() 00626 self.__globals__['__last_task'] = 'flagcmd' 00627 self.__globals__['taskname'] = 'flagcmd' 00628 ### 00629 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00630 ### 00631 ### 00632 #Handle globals or user over-ride of arguments 00633 # 00634 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00635 useLocalDefaults = False 00636 00637 for item in function_signature_defaults.iteritems(): 00638 key,val = item 00639 keyVal = eval(key) 00640 if (keyVal == None): 00641 #user hasn't set it - use global/default 00642 pass 00643 else: 00644 #user has set it - use over-ride 00645 if (key != 'self') : 00646 useLocalDefaults = True 00647 00648 myparams = {} 00649 if useLocalDefaults : 00650 for item in function_signature_defaults.iteritems(): 00651 key,val = item 00652 keyVal = eval(key) 00653 exec('myparams[key] = keyVal') 00654 self.parameters[key] = keyVal 00655 if (keyVal == None): 00656 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00657 keyVal = eval(key) 00658 if(type(keyVal) == dict) : 00659 if len(keyVal) > 0 : 00660 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00661 else : 00662 exec('myparams[key] = ' + key + ' = {}') 00663 00664 else : 00665 async = self.parameters['async'] 00666 myparams['vis'] = vis = self.parameters['vis'] 00667 myparams['inpmode'] = inpmode = self.parameters['inpmode'] 00668 myparams['inpfile'] = inpfile = self.parameters['inpfile'] 00669 myparams['tablerows'] = tablerows = self.parameters['tablerows'] 00670 myparams['reason'] = reason = self.parameters['reason'] 00671 myparams['useapplied'] = useapplied = self.parameters['useapplied'] 00672 myparams['tbuff'] = tbuff = self.parameters['tbuff'] 00673 myparams['ants'] = ants = self.parameters['ants'] 00674 myparams['action'] = action = self.parameters['action'] 00675 myparams['flagbackup'] = flagbackup = self.parameters['flagbackup'] 00676 myparams['clearall'] = clearall = self.parameters['clearall'] 00677 myparams['rowlist'] = rowlist = self.parameters['rowlist'] 00678 myparams['plotfile'] = plotfile = self.parameters['plotfile'] 00679 myparams['savepars'] = savepars = self.parameters['savepars'] 00680 myparams['outfile'] = outfile = self.parameters['outfile'] 00681 00682 if type(tablerows)==int: tablerows=[tablerows] 00683 if type(rowlist)==int: rowlist=[rowlist] 00684 00685 result = None 00686 00687 # 00688 # The following is work around to avoid a bug with current python translation 00689 # 00690 mytmp = {} 00691 00692 mytmp['vis'] = vis 00693 mytmp['inpmode'] = inpmode 00694 mytmp['inpfile'] = inpfile 00695 mytmp['tablerows'] = tablerows 00696 mytmp['reason'] = reason 00697 mytmp['useapplied'] = useapplied 00698 mytmp['tbuff'] = tbuff 00699 mytmp['ants'] = ants 00700 mytmp['action'] = action 00701 mytmp['flagbackup'] = flagbackup 00702 mytmp['clearall'] = clearall 00703 mytmp['rowlist'] = rowlist 00704 mytmp['plotfile'] = plotfile 00705 mytmp['savepars'] = savepars 00706 mytmp['outfile'] = outfile 00707 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00708 trec = casac.casac.utils().torecord(pathname+'flagcmd.xml') 00709 00710 casalog.origin('flagcmd') 00711 try : 00712 #if not trec.has_key('flagcmd') or not casac.casac.utils().verify(mytmp, trec['flagcmd']) : 00713 #return False 00714 00715 casac.casac.utils().verify(mytmp, trec['flagcmd'], True) 00716 scriptstr=[''] 00717 saveinputs = self.__globals__['saveinputs'] 00718 saveinputs('flagcmd', 'flagcmd.last', myparams, self.__globals__,scriptstr=scriptstr) 00719 if async : 00720 count = 0 00721 keybase = time.strftime("%y%m%d.%H%M%S") 00722 key = keybase + "_" + str(count) 00723 while self.__async__.has_key(key) : 00724 count += 1 00725 key = keybase + "_" + str(count) 00726 result = tm.execute('flagcmd', vis, inpmode, inpfile, tablerows, reason, useapplied, tbuff, ants, action, flagbackup, clearall, rowlist, plotfile, savepars, outfile) 00727 print "Use: " 00728 print " tm.retrieve(return_value) # to retrieve the status" 00729 print 00730 self.rkey = key 00731 self.__async__[key] = result 00732 else : 00733 tname = 'flagcmd' 00734 spaces = ' '*(18-len(tname)) 00735 casalog.post('\n##########################################'+ 00736 '\n##### Begin Task: ' + tname + spaces + ' #####') 00737 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00738 result = flagcmd(vis, inpmode, inpfile, tablerows, reason, useapplied, tbuff, ants, action, flagbackup, clearall, rowlist, plotfile, savepars, outfile) 00739 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00740 '\n##########################################') 00741 00742 except Exception, instance: 00743 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00744 raise 00745 else : 00746 #print '**** Error **** ',instance 00747 tname = 'flagcmd' 00748 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00749 pass 00750 00751 gc.collect() 00752 return result 00753 # 00754 # 00755 # 00756 def paramgui(self, useGlobals=True, ipython_globals=None): 00757 """ 00758 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00759 """ 00760 import paramgui 00761 if not hasattr(self, "__globals__") or self.__globals__ == None : 00762 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00763 00764 if useGlobals: 00765 if ipython_globals == None: 00766 myf=self.__globals__ 00767 else: 00768 myf=ipython_globals 00769 00770 paramgui.setGlobals(myf) 00771 else: 00772 paramgui.setGlobals({}) 00773 00774 paramgui.runTask('flagcmd', myf['_ip']) 00775 paramgui.setGlobals({}) 00776 00777 # 00778 # 00779 # 00780 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00781 if not hasattr(self, "__globals__") or self.__globals__ == None : 00782 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00783 if ipython_globals == None: 00784 myf=self.__globals__ 00785 else: 00786 myf=ipython_globals 00787 00788 a = odict() 00789 a['vis'] = '' 00790 a['inpmode'] = 'table' 00791 a['action'] = 'apply' 00792 a['savepars'] = False 00793 00794 a['async']=False 00795 a['inpmode'] = { 00796 0:odict([{'value':'table'}, {'inpfile':''}, {'tablerows':[]}, {'reason':'any'}, {'useapplied':False}]), 00797 1:odict([{'value':'list'}, {'inpfile':''}, {'reason':'any'}]), 00798 2:odict([{'value':'xml'}, {'tbuff':0.0}, {'ants':''}, {'reason':'any'}])} 00799 a['action'] = { 00800 0:odict([{'value':'apply'}, {'flagbackup':True}]), 00801 1:odict([{'value':'unapply'}, {'flagbackup':True}]), 00802 2:{'value':'list'}, 00803 3:odict([{'value':'plot'}, {'plotfile':''}]), 00804 4:odict([{'value':'clear'}, {'clearall':False}, {'rowlist':[]}])} 00805 a['savepars'] = { 00806 0:{'value':False}, 00807 1:odict([{'value':True}, {'outfile':''}])} 00808 00809 ### This function sets the default values but also will return the list of 00810 ### parameters or the default value of a given parameter 00811 if(param == None): 00812 myf['__set_default_parameters'](a) 00813 elif(param == 'paramkeys'): 00814 return a.keys() 00815 else: 00816 if(paramvalue==None and subparam==None): 00817 if(a.has_key(param)): 00818 return a[param] 00819 else: 00820 return self.itsdefault(param) 00821 else: 00822 retval=a[param] 00823 if(type(a[param])==dict): 00824 for k in range(len(a[param])): 00825 valornotval='value' 00826 if(a[param][k].has_key('notvalue')): 00827 valornotval='notvalue' 00828 if((a[param][k][valornotval])==paramvalue): 00829 retval=a[param][k].copy() 00830 retval.pop(valornotval) 00831 if(subparam != None): 00832 if(retval.has_key(subparam)): 00833 retval=retval[subparam] 00834 else: 00835 retval=self.itsdefault(subparam) 00836 else: 00837 retval=self.itsdefault(subparam) 00838 return retval 00839 00840 00841 # 00842 # 00843 def check_params(self, param=None, value=None, ipython_globals=None): 00844 if ipython_globals == None: 00845 myf=self.__globals__ 00846 else: 00847 myf=ipython_globals 00848 # print 'param:', param, 'value:', value 00849 try : 00850 if str(type(value)) != "<type 'instance'>" : 00851 value0 = value 00852 value = myf['cu'].expandparam(param, value) 00853 matchtype = False 00854 if(type(value) == numpy.ndarray): 00855 if(type(value) == type(value0)): 00856 myf[param] = value.tolist() 00857 else: 00858 #print 'value:', value, 'value0:', value0 00859 #print 'type(value):', type(value), 'type(value0):', type(value0) 00860 myf[param] = value0 00861 if type(value0) != list : 00862 matchtype = True 00863 else : 00864 myf[param] = value 00865 value = myf['cu'].verifyparam({param:value}) 00866 if matchtype: 00867 value = False 00868 except Exception, instance: 00869 #ignore the exception and just return it unchecked 00870 myf[param] = value 00871 return value 00872 # 00873 # 00874 def description(self, key='flagcmd', subkey=None): 00875 desc={'flagcmd': 'Flagging task based on batches of flag-commands', 00876 'vis': 'Name of MS file or calibration table to flag', 00877 'inpmode': 'Input mode for flag commands(table/list/xml)', 00878 'inpfile': 'Source of flag commands', 00879 'tablerows': 'Rows of inpfile to read', 00880 'reason': 'Select by REASON types', 00881 'useapplied': 'Select commands whose rows have APPLIED column set to True', 00882 'tbuff': 'Time buffer (sec) to pad flags', 00883 'ants': 'Allowed flag antenna names to select by', 00884 'action': 'Action to perform in MS and/or in inpfile (apply/unapply/list/plot/clear/extract)', 00885 'flagbackup': 'Automatically backup the FLAG column before execution', 00886 'clearall': 'Delete all rows from FLAG_CMD', 00887 'rowlist': 'FLAG_CMD rows to clear', 00888 'plotfile': 'Name of output file to save plot', 00889 'savepars': 'Save flag commands to the MS or to a file', 00890 'outfile': 'Name of output file to save commands', 00891 00892 'async': 'If true the taskname must be started using flagcmd(...)' 00893 } 00894 00895 # 00896 # Set subfields defaults if needed 00897 # 00898 00899 if(desc.has_key(key)) : 00900 return desc[key] 00901 00902 def itsdefault(self, paramname) : 00903 a = {} 00904 a['vis'] = '' 00905 a['inpmode'] = 'table' 00906 a['inpfile'] = '' 00907 a['tablerows'] = [] 00908 a['reason'] = 'any' 00909 a['useapplied'] = False 00910 a['tbuff'] = 0.0 00911 a['ants'] = '' 00912 a['action'] = 'apply' 00913 a['flagbackup'] = True 00914 a['clearall'] = False 00915 a['rowlist'] = [] 00916 a['plotfile'] = '' 00917 a['savepars'] = False 00918 a['outfile'] = '' 00919 00920 #a = sys._getframe(len(inspect.stack())-1).f_globals 00921 00922 if self.parameters['inpmode'] == 'table': 00923 a['inpfile'] = '' 00924 a['tablerows'] = [] 00925 a['reason'] = 'any' 00926 a['useapplied'] = False 00927 00928 if self.parameters['inpmode'] == 'list': 00929 a['inpfile'] = '' 00930 a['reason'] = 'any' 00931 00932 if self.parameters['inpmode'] == 'xml': 00933 a['tbuff'] = 0.0 00934 a['ants'] = '' 00935 a['reason'] = 'any' 00936 00937 if self.parameters['action'] == 'apply': 00938 a['flagbackup'] = True 00939 00940 if self.parameters['action'] == 'unapply': 00941 a['flagbackup'] = True 00942 00943 if self.parameters['action'] == 'plot': 00944 a['plotfile'] = '' 00945 00946 if self.parameters['action'] == 'clear': 00947 a['clearall'] = False 00948 a['rowlist'] = [] 00949 00950 if self.parameters['savepars'] == True: 00951 a['outfile'] = '' 00952 00953 if a.has_key(paramname) : 00954 return a[paramname] 00955 flagcmd_cli = flagcmd_cli_()