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_sdflag import sdflag 00018 class sdflag_cli_: 00019 __name__ = "sdflag" 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__ = (sdflag_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'infile':None, 'antenna':None, 'specunit':None, 'restfreq':None, 'frame':None, 'doppler':None, 'scanlist':None, 'field':None, 'iflist':None, 'pollist':None, 'maskflag':None, 'flagrow':None, 'clip':None, 'clipminmax':None, 'clipoutside':None, 'flagmode':None, 'interactive':None, 'showflagged':None, 'outfile':None, 'outform':None, 'overwrite':None, 'plotlevel':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, infile=None, antenna=None, specunit=None, restfreq=None, frame=None, doppler=None, scanlist=None, field=None, iflist=None, pollist=None, maskflag=None, flagrow=None, clip=None, clipminmax=None, clipoutside=None, flagmode=None, interactive=None, showflagged=None, outfile=None, outform=None, overwrite=None, plotlevel=None, async=None): 00047 00048 """ASAP SD spectral flagging task 00049 Keyword arguments: 00050 infile -- name of input SD dataset 00051 antenna -- antenna name or id (only effective for MS input). 00052 specunit -- units for spectral axis 00053 options: (str) 'channel','km/s','GHz','MHz','kHz','Hz','' 00054 default: '' (=current) 00055 example: this will be the units for maskflag 00056 >>> specunit expandable parameters 00057 restfreq -- rest frequency 00058 available type includes float, int, string, list of float, 00059 list of int, list of string, and list of dictionary. the 00060 default unit of restfreq in case of float, int, or string 00061 without unit is Hz. string input can be a value only 00062 (treated as Hz) or a value followed by unit for which 'GHz', 00063 'MHz','kHz',and 'Hz' are available. 00064 a list can be used to set different rest frequencies for 00065 each IF. the length of list input must be nIF. dictionary 00066 input should be a pair of molecule name and frequency with 00067 keys of 'name' and 'value', respectively. values in the 00068 dictionary input follows the same manner as for single 00069 float or string input. 00070 example: 345.796 00071 '1420MHz' 00072 [345.8, 347.0, 356.7] 00073 ['345.8MHz', '347.0MHz', '356.7MHz'] 00074 [{'name':'CO','value':345}] 00075 frame -- frequency frame for spectral axis 00076 options: (str) 'LSRK','REST','TOPO','LSRD','BARY', 00077 'GEO','GALACTO','LGROUP','CMB' 00078 default: currently set frame in scantable 00079 WARNING: frame='REST' not yet implemented 00080 doppler -- doppler mode 00081 options: (str) 'RADIO','OPTICAL','Z','BETA','GAMMA' 00082 default: currently set doppler in scantable 00083 scanlist -- list of scan numbers to process 00084 default: [] (use all scans) 00085 example: [21,22,23,24] 00086 this selection is in addition to field and iflist 00087 field -- selection string for selecting scans by name 00088 default: '' (no name selection) 00089 example: 'FLS3a*' 00090 this selection is in addition to scanlist, iflist, and pollist 00091 iflist -- list of IF id numbers to select 00092 default: [] (use all IFs) 00093 example: [15] 00094 this selection is in addition to scanlist, field, and pollist 00095 pollist -- list of polarization id numbers to select 00096 default: [] (use all polarizations) 00097 example: [1] 00098 this selection is in addition to scanlist, field, and iflist 00099 maskflag -- list of mask regions to apply flag/unflag 00100 Note, this parameter is ignored if one or more rows are 00101 given in flagrow, or clip=True. 00102 default: [] (entire spectrum) 00103 example: [[1000,3000],[5000,7000]] 00104 flagrow -- list of row numbers to apply flag/unflag 00105 Note, this parameter is effective only when one or more row 00106 numbers are given explicitly and also clip=False 00107 default: [] (no row selection) 00108 example: [0, 2, 3] 00109 clip -- flag data that are outside a specified range 00110 options: (bool)True,False 00111 default: False 00112 >>> clip expandable parameters 00113 clipminmax -- range of data that will NOT be flagged 00114 default: [] means do not use clip option 00115 example: [0.0,1.5] 00116 clipoutside -- clip OUTSIDE the range ? 00117 options: (bool)True,False 00118 default: True 00119 example: clipoutside=False means flag data WITHIN the range. 00120 flagmode -- flag mode 00121 default: 'flag' 00122 options: 'flag','unflag' 00123 interactive -- determines interactive flagging 00124 options: (bool) True,False 00125 default: False 00126 >>> interactive expandable parameters 00127 showflagged -- show flagged data on plots 00128 default: False 00129 outfile -- Name of output file 00130 default: '' 00131 Note: by default (outfile=''), actual output file name is set as follows: 00132 (1) if overwrite=True (default), infile (input) will be overwritten. 00133 WARNING: If the formats of input and ouput files are different, 00134 this causes complete loss of input file. 00135 (2) if overwrite=False, outfile will be <infile>_f. 00136 outform -- format of output file 00137 options: 'ASCII','SDFITS','MS','ASAP' 00138 default: 'ASAP' 00139 example: the ASAP format is easiest for further sd 00140 processing; use MS for CASA imaging. 00141 WARNING: Be sure outform is same as the input file format when you 00142 overwrite the input file by overwrite=True and outfile='' (default). 00143 overwrite -- overwrite the output file if already exists 00144 options: (bool) True,False 00145 default: True 00146 WARNING: input file is overwritten if overwrite=True and outfile='' (default). 00147 This causes the complete loss of input file if the formats of 00148 input and ouput files are different. 00149 plotlevel -- control for plotting of results 00150 options: (int) 0=none, 1=some, 2=more, <0=hardcopy 00151 default: 0 (no plotting) 00152 example: plotlevel<0 as abs(plotlevel), e.g. 00153 -1 => hardcopy of final plot (will be named 00154 <outfile>_flag.eps) 00155 WARNING: be careful plotting in fsotf mode! 00156 00157 ------------------------------------------------------------------- 00158 00159 DESCRIPTION: 00160 00161 Task sdflag performs both interactive and non-interactive channel/row 00162 based flagging on spectra. 00163 Currently, the available ways of non-interactive flagging include: 00164 (1) channel based flagging by specifying a range of spectral values 00165 with clip=True, (2) row based flagging by specifying a list of row 00166 numbers to the flagrow parameter, and (3) channel based flagging by 00167 specifying regions in channel to the maskflag parameter. 00168 These three ways of flagging can not be executed simultaneously. 00169 If more than one parameter above are specified, the task looks for 00170 them in the above order and operates the first specified way of 00171 flagging operation. 00172 00173 Interactive flagging is available when interactive=True. 00174 The available ways of interactive flagging include: 00175 (1) row based flagging by selecting 'panel' and (2) channel 00176 based flagging by selecting 'region's of channels on Flag plotter. 00177 Note that the Flag plotter is loaded after carrying out the 00178 non-interactive flag operation if any specified. See the cookbook for 00179 details of how to select channel regions and spectra on the plotter. 00180 00181 If plotlevel>=1, the task asks you if you really apply the 00182 flags before it is actually written to the data with a plot 00183 indicating flagged regions. 00184 Please note that this task is still experimental. 00185 00186 WARNING for overwrite option: 00187 Be sure 'outform' is the same as data format of input file when you 00188 overwrite it. Since CASA 3.1, the default value of the option 'overwrite' 00189 has been changed to True, thereby the current dataset (infile) is 00190 overwritten unless a different file name is set to outfile. 00191 There is a known issue in overwriting infile. If 'outform' differs to the 00192 data format of infile, the data is overwritten with the new data format 00193 (specified by 'outform') and the data in the original format will be lost. 00194 00195 WARNING for the GBT raw SDFITS format data as input: 00196 SDtasks are able to handle GBT raw SDFITS format data since the 00197 data filler is available. However, the functionality is not well 00198 tested yet, so that there may be unknown bugs. 00199 00200 00201 """ 00202 if not hasattr(self, "__globals__") or self.__globals__ == None : 00203 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00204 #casac = self.__globals__['casac'] 00205 casalog = self.__globals__['casalog'] 00206 #casalog = casac.casac.logsink() 00207 self.__globals__['__last_task'] = 'sdflag' 00208 self.__globals__['taskname'] = 'sdflag' 00209 ### 00210 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00211 ### 00212 ### 00213 #Handle globals or user over-ride of arguments 00214 # 00215 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00216 useLocalDefaults = False 00217 00218 for item in function_signature_defaults.iteritems(): 00219 key,val = item 00220 keyVal = eval(key) 00221 if (keyVal == None): 00222 #user hasn't set it - use global/default 00223 pass 00224 else: 00225 #user has set it - use over-ride 00226 if (key != 'self') : 00227 useLocalDefaults = True 00228 00229 myparams = {} 00230 if useLocalDefaults : 00231 for item in function_signature_defaults.iteritems(): 00232 key,val = item 00233 keyVal = eval(key) 00234 exec('myparams[key] = keyVal') 00235 self.parameters[key] = keyVal 00236 if (keyVal == None): 00237 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00238 keyVal = eval(key) 00239 if(type(keyVal) == dict) : 00240 if len(keyVal) > 0 : 00241 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00242 else : 00243 exec('myparams[key] = ' + key + ' = {}') 00244 00245 else : 00246 async = self.parameters['async'] 00247 myparams['infile'] = infile = self.parameters['infile'] 00248 myparams['antenna'] = antenna = self.parameters['antenna'] 00249 myparams['specunit'] = specunit = self.parameters['specunit'] 00250 myparams['restfreq'] = restfreq = self.parameters['restfreq'] 00251 myparams['frame'] = frame = self.parameters['frame'] 00252 myparams['doppler'] = doppler = self.parameters['doppler'] 00253 myparams['scanlist'] = scanlist = self.parameters['scanlist'] 00254 myparams['field'] = field = self.parameters['field'] 00255 myparams['iflist'] = iflist = self.parameters['iflist'] 00256 myparams['pollist'] = pollist = self.parameters['pollist'] 00257 myparams['maskflag'] = maskflag = self.parameters['maskflag'] 00258 myparams['flagrow'] = flagrow = self.parameters['flagrow'] 00259 myparams['clip'] = clip = self.parameters['clip'] 00260 myparams['clipminmax'] = clipminmax = self.parameters['clipminmax'] 00261 myparams['clipoutside'] = clipoutside = self.parameters['clipoutside'] 00262 myparams['flagmode'] = flagmode = self.parameters['flagmode'] 00263 myparams['interactive'] = interactive = self.parameters['interactive'] 00264 myparams['showflagged'] = showflagged = self.parameters['showflagged'] 00265 myparams['outfile'] = outfile = self.parameters['outfile'] 00266 myparams['outform'] = outform = self.parameters['outform'] 00267 myparams['overwrite'] = overwrite = self.parameters['overwrite'] 00268 myparams['plotlevel'] = plotlevel = self.parameters['plotlevel'] 00269 00270 if type(scanlist)==int: scanlist=[scanlist] 00271 if type(iflist)==int: iflist=[iflist] 00272 if type(maskflag)==int: maskflag=[maskflag] 00273 if type(flagrow)==int: flagrow=[flagrow] 00274 00275 result = None 00276 00277 # 00278 # The following is work around to avoid a bug with current python translation 00279 # 00280 mytmp = {} 00281 00282 mytmp['infile'] = infile 00283 mytmp['antenna'] = antenna 00284 mytmp['specunit'] = specunit 00285 mytmp['restfreq'] = restfreq 00286 mytmp['frame'] = frame 00287 mytmp['doppler'] = doppler 00288 mytmp['scanlist'] = scanlist 00289 mytmp['field'] = field 00290 mytmp['iflist'] = iflist 00291 mytmp['pollist'] = pollist 00292 mytmp['maskflag'] = maskflag 00293 mytmp['flagrow'] = flagrow 00294 mytmp['clip'] = clip 00295 mytmp['clipminmax'] = clipminmax 00296 mytmp['clipoutside'] = clipoutside 00297 mytmp['flagmode'] = flagmode 00298 mytmp['interactive'] = interactive 00299 mytmp['showflagged'] = showflagged 00300 mytmp['outfile'] = outfile 00301 mytmp['outform'] = outform 00302 mytmp['overwrite'] = overwrite 00303 mytmp['plotlevel'] = plotlevel 00304 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00305 trec = casac.casac.utils().torecord(pathname+'sdflag.xml') 00306 00307 casalog.origin('sdflag') 00308 try : 00309 #if not trec.has_key('sdflag') or not casac.casac.utils().verify(mytmp, trec['sdflag']) : 00310 #return False 00311 00312 casac.casac.utils().verify(mytmp, trec['sdflag'], True) 00313 scriptstr=[''] 00314 saveinputs = self.__globals__['saveinputs'] 00315 saveinputs('sdflag', 'sdflag.last', myparams, self.__globals__,scriptstr=scriptstr) 00316 if async : 00317 count = 0 00318 keybase = time.strftime("%y%m%d.%H%M%S") 00319 key = keybase + "_" + str(count) 00320 while self.__async__.has_key(key) : 00321 count += 1 00322 key = keybase + "_" + str(count) 00323 result = tm.execute('sdflag', infile, antenna, specunit, restfreq, frame, doppler, scanlist, field, iflist, pollist, maskflag, flagrow, clip, clipminmax, clipoutside, flagmode, interactive, showflagged, outfile, outform, overwrite, plotlevel) 00324 print "Use: " 00325 print " tm.retrieve(return_value) # to retrieve the status" 00326 print 00327 self.rkey = key 00328 self.__async__[key] = result 00329 else : 00330 tname = 'sdflag' 00331 spaces = ' '*(18-len(tname)) 00332 casalog.post('\n##########################################'+ 00333 '\n##### Begin Task: ' + tname + spaces + ' #####') 00334 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00335 result = sdflag(infile, antenna, specunit, restfreq, frame, doppler, scanlist, field, iflist, pollist, maskflag, flagrow, clip, clipminmax, clipoutside, flagmode, interactive, showflagged, outfile, outform, overwrite, plotlevel) 00336 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00337 '\n##########################################') 00338 00339 except Exception, instance: 00340 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00341 raise 00342 else : 00343 #print '**** Error **** ',instance 00344 tname = 'sdflag' 00345 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00346 pass 00347 00348 gc.collect() 00349 return result 00350 # 00351 # 00352 # 00353 def paramgui(self, useGlobals=True, ipython_globals=None): 00354 """ 00355 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00356 """ 00357 import paramgui 00358 if not hasattr(self, "__globals__") or self.__globals__ == None : 00359 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00360 00361 if useGlobals: 00362 if ipython_globals == None: 00363 myf=self.__globals__ 00364 else: 00365 myf=ipython_globals 00366 00367 paramgui.setGlobals(myf) 00368 else: 00369 paramgui.setGlobals({}) 00370 00371 paramgui.runTask('sdflag', myf['_ip']) 00372 paramgui.setGlobals({}) 00373 00374 # 00375 # 00376 # 00377 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00378 if not hasattr(self, "__globals__") or self.__globals__ == None : 00379 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00380 if ipython_globals == None: 00381 myf=self.__globals__ 00382 else: 00383 myf=ipython_globals 00384 00385 a = odict() 00386 a['infile'] = '' 00387 a['antenna'] = 0 00388 a['specunit'] = '' 00389 a['frame'] = '' 00390 a['doppler'] = '' 00391 a['scanlist'] = [] 00392 a['field'] = '' 00393 a['iflist'] = [] 00394 a['pollist'] = [] 00395 a['maskflag'] = [] 00396 a['flagrow'] = [] 00397 a['clip'] = False 00398 a['flagmode'] = 'flag' 00399 a['interactive'] = False 00400 a['outfile'] = '' 00401 a['outform'] = 'ASAP' 00402 a['overwrite'] = True 00403 a['plotlevel'] = 0 00404 00405 a['async']=False 00406 a['specunit'] = { 00407 0:{'value':''}, 00408 1:{'value':'channel'}, 00409 2:{'value':'GHz'}, 00410 3:{'value':'MHz'}, 00411 4:{'value':'kHz'}, 00412 5:{'value':'Hz'}, 00413 6:odict([{'value':'km/s'}, {'restfreq':''}])} 00414 a['clip'] = { 00415 0:{'value':False}, 00416 1:odict([{'value':True}, {'clipminmax':[]}, {'clipoutside':True}])} 00417 a['interactive'] = { 00418 0:{'value':False}, 00419 1:odict([{'value':True}, {'showflagged':False}])} 00420 00421 ### This function sets the default values but also will return the list of 00422 ### parameters or the default value of a given parameter 00423 if(param == None): 00424 myf['__set_default_parameters'](a) 00425 elif(param == 'paramkeys'): 00426 return a.keys() 00427 else: 00428 if(paramvalue==None and subparam==None): 00429 if(a.has_key(param)): 00430 return a[param] 00431 else: 00432 return self.itsdefault(param) 00433 else: 00434 retval=a[param] 00435 if(type(a[param])==dict): 00436 for k in range(len(a[param])): 00437 valornotval='value' 00438 if(a[param][k].has_key('notvalue')): 00439 valornotval='notvalue' 00440 if((a[param][k][valornotval])==paramvalue): 00441 retval=a[param][k].copy() 00442 retval.pop(valornotval) 00443 if(subparam != None): 00444 if(retval.has_key(subparam)): 00445 retval=retval[subparam] 00446 else: 00447 retval=self.itsdefault(subparam) 00448 else: 00449 retval=self.itsdefault(subparam) 00450 return retval 00451 00452 00453 # 00454 # 00455 def check_params(self, param=None, value=None, ipython_globals=None): 00456 if ipython_globals == None: 00457 myf=self.__globals__ 00458 else: 00459 myf=ipython_globals 00460 # print 'param:', param, 'value:', value 00461 try : 00462 if str(type(value)) != "<type 'instance'>" : 00463 value0 = value 00464 value = myf['cu'].expandparam(param, value) 00465 matchtype = False 00466 if(type(value) == numpy.ndarray): 00467 if(type(value) == type(value0)): 00468 myf[param] = value.tolist() 00469 else: 00470 #print 'value:', value, 'value0:', value0 00471 #print 'type(value):', type(value), 'type(value0):', type(value0) 00472 myf[param] = value0 00473 if type(value0) != list : 00474 matchtype = True 00475 else : 00476 myf[param] = value 00477 value = myf['cu'].verifyparam({param:value}) 00478 if matchtype: 00479 value = False 00480 except Exception, instance: 00481 #ignore the exception and just return it unchecked 00482 myf[param] = value 00483 return value 00484 # 00485 # 00486 def description(self, key='sdflag', subkey=None): 00487 desc={'sdflag': 'ASAP SD spectral flagging task ', 00488 'infile': 'name of input SD dataset', 00489 'antenna': 'antenna name or id (only effective for MS input)', 00490 'specunit': 'units for spectral axis (channel,km/s,GHz)', 00491 'restfreq': 'rest frequency (default unit: Hz)', 00492 'frame': 'frequency reference frame, e.g. LSRK (''=current)', 00493 'doppler': 'doppler convention, e.g. RADIO (''=current)', 00494 'scanlist': 'list of scans to use (e.g. [1,2,3,4])', 00495 'field': 'string for selection by source name', 00496 'iflist': 'list of IF ids to select (e.g. [0,1])', 00497 'pollist': 'list of polarization by ids or names to select (e.g. [0,1])', 00498 'maskflag': 'list of mask regions to flag/unflag', 00499 'flagrow': 'list of row numbers to apply row-based flagging/unflagging', 00500 'clip': 'flag data outside a specified range', 00501 'clipminmax': 'range of data that will NOT be flagged', 00502 'clipoutside': 'clip outside the range, or within it', 00503 'flagmode': 'flag mode (flag,unflag)', 00504 'interactive': 'determines interactive flagging', 00505 'showflagged': 'show flagged data on plots', 00506 'outfile': 'name of output file (See a WARNING in help document)', 00507 'outform': 'output file format (ASCII,MS,SDFITS,ASAP) (See a WARNING in help document)', 00508 'overwrite': 'overwrite the output file if already exists (See a WARNING in help document)', 00509 'plotlevel': 'control for plotting of results', 00510 00511 'async': 'If true the taskname must be started using sdflag(...)' 00512 } 00513 00514 # 00515 # Set subfields defaults if needed 00516 # 00517 00518 if(desc.has_key(key)) : 00519 return desc[key] 00520 00521 def itsdefault(self, paramname) : 00522 a = {} 00523 a['infile'] = '' 00524 a['antenna'] = 0 00525 a['specunit'] = '' 00526 a['restfreq'] = '' 00527 a['frame'] = '' 00528 a['doppler'] = '' 00529 a['scanlist'] = [] 00530 a['field'] = '' 00531 a['iflist'] = [] 00532 a['pollist'] = [] 00533 a['maskflag'] = [] 00534 a['flagrow'] = [] 00535 a['clip'] = False 00536 a['clipminmax'] = [] 00537 a['clipoutside'] = True 00538 a['flagmode'] = 'flag' 00539 a['interactive'] = False 00540 a['showflagged'] = False 00541 a['outfile'] = '' 00542 a['outform'] = 'ASAP' 00543 a['overwrite'] = True 00544 a['plotlevel'] = 0 00545 00546 #a = sys._getframe(len(inspect.stack())-1).f_globals 00547 00548 if self.parameters['specunit'] == 'km/s': 00549 a['restfreq'] = '' 00550 00551 if self.parameters['clip'] == True: 00552 a['clipminmax'] = [] 00553 a['clipoutside'] = True 00554 00555 if self.parameters['interactive'] == True: 00556 a['showflagged'] = False 00557 00558 if a.has_key(paramname) : 00559 return a[paramname] 00560 sdflag_cli = sdflag_cli_()