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 string 00010 import time 00011 import inspect 00012 import gc 00013 import numpy 00014 from odict import odict 00015 from task_statwt import statwt 00016 from task_statwt import casalog 00017 00018 class statwt_pg_: 00019 __name__ = "statwt" 00020 00021 def __init__(self) : 00022 self.__bases__ = (statwt_pg_,) 00023 self.__doc__ = self.__call__.__doc__ 00024 00025 00026 def __call__(self, vis=None, dorms=None, byantenna=None, sepacs=None, fitspw=None, fitcorr=None, combine=None, timebin=None, minsamp=None, field=None, spw=None, antenna=None, timerange=None, scan=None, intent=None, array=None, correlation=None, observation=None, datacolumn=None, async=None): 00027 00028 """ Reweight visibilities according to their scatter (Experimental) 00029 00030 The WEIGHT and SIGMA columns of measurement sets are often set to arbitrary 00031 values (e.g. 1), or theoretically estimated from poorly known antenna and 00032 receiver properties. Many tasks (e.g. clean) are insensitive to an overall 00033 scale error in WEIGHT, but are affected by errors in the relative weights 00034 between visibilities. Other tasks, such as uvmodelfit, or anything which 00035 depends on theoretical estimates of the noise, require (reasonably) correct 00036 weights and sigmas. statwt empirically measures the visibility scatter 00037 (typically as a function of time, antenna, and/or baseline) and uses that 00038 to set WEIGHT and SIGMA. It is important that all necessary calibrations 00039 are applied to the data prior to running this task for correct determination of 00040 weights and sigmas. 00041 00042 Note: Some of the parameters (byantenna, sepacs, fitcorr, and timebin) 00043 are not fully implemeted for CASA 3.4. 00044 00045 00046 Keyword arguments: 00047 vis -- Name of the measurement set. 00048 default: none; example: vis='ngc5921.ms' 00049 00050 dorms -- Estimate the scatter using rms instead of the standard 00051 deviation? 00052 00053 Ideally the visibilities used to estimate the scatter, as 00054 selected by fitspw and fitcorr, should be pure noise. If you 00055 know for certain that they are, then setting dorms to True 00056 will give the best result. Otherwise, use False (standard 00057 sample standard deviation). More robust scatter estimates 00058 like the interquartile range or median absolute deviation from 00059 the median are not offered because they require sorting by 00060 value, which is not possible for complex numbers. 00061 default: False 00062 00063 byantenna -- Assume that the noise is factorable by antenna (feed). 00064 If false, treat it seperately for each baseline 00065 (recommended if there is strong signal). 00066 default: False (*** byantenna=True is not yet implemented) 00067 00068 sepacs -- If solving by antenna, treat autocorrelations separately. 00069 (Acknowledge that what autocorrelations "see" is very 00070 different from what crosscorrelations see.) 00071 default: True (*** not yet implemented) 00072 00073 00074 --- Data Selection (see help par.selectdata for more detailed 00075 information) 00076 00077 fitspw -- The (ideally) signal-free spectral window:channels to 00078 estimate the scatter from. 00079 default: '' (All) 00080 00081 fitcorr -- The (ideally) signal-free correlations to 00082 estimate the scatter from. 00083 default: '' (All) 00084 *** not yet implemented 00085 00086 combine -- Let samples span multiple spws, corrs, scans, and/or states. 00087 combine = 'spw': Recommended when a line spans an entire spw 00088 - set fitspw to the neighboring spws and 00089 apply their weight to the line spw(s). 00090 However, the effect of the line signal per 00091 visibility may be relatively harmless 00092 compared to the noise difference between 00093 spws. 00094 combine = 'scan': Can be useful when the scan number 00095 goes up with each integration, 00096 as in many WSRT MSes. 00097 combine = ['scan', 'spw']: disregard scan and spw 00098 numbers when gathering samples. 00099 combine = 'spw,scan': Same as above. 00100 default: '' (None) 00101 00102 timebin -- Sample interval. 00103 default: '0s' or '-1s' (1 integration at a time) 00104 example: timebin='30s' 00105 '10' means '10s' 00106 *** not yet implemented 00107 00108 minsamp -- Minimum number of unflagged visibilities for estimating the 00109 scatter. Selected visibilities for which the weight cannot 00110 be estimated will be flagged. Note that minsamp is 00111 effectively at least 2 if dorms is False, and 1 if it is 00112 True. 00113 00114 field -- Select field using field id(s) or field name(s). 00115 [run listobs to obtain the list id's or names] 00116 default: ''=all fields If field string is a non-negative 00117 integer, it is assumed to be a field index 00118 otherwise, it is assumed to be a field name 00119 field='0~2'; field ids 0,1,2 00120 field='0,4,5~7'; field ids 0,4,5,6,7 00121 field='3C286,3C295'; fields named 3C286 and 3C295 00122 field = '3,4C*'; field id 3, all names starting with 4C 00123 00124 spw -- Select spectral window/channels for changing WEIGHT and SIGMA. 00125 default: ''=all spectral windows and channels 00126 spw='0~2,4'; spectral windows 0,1,2,4 (all channels) 00127 spw='<2'; spectral windows less than 2 (i.e. 0,1) 00128 spw='0:5~61'; spw 0, channels 5 to 61 00129 spw='0,10,3:3~45'; spw 0,10 all channels, spw 3 - chans 3 to 45. 00130 spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each. 00131 spw = '*:3~64' channels 3 through 64 for all sp id's 00132 spw = ' :3~64' will NOT work. 00133 statwt does not support multiple channel ranges per spectral 00134 window (';') because it is not clear whether to keep the ranges 00135 in the original spectral window or make a new spectral window 00136 for each additional range. 00137 00138 antenna -- Select antennas/baselines for changing WEIGHT and SIGMA. 00139 default: '' (all) 00140 Non-negative integers are assumed to be antenna indices, and 00141 anything else is taken as an antenna name. 00142 00143 Examples: 00144 antenna='5&6': baseline between antenna index 5 and index 6. 00145 antenna='VA05&VA06': baseline between VLA antenna 5 and 6. 00146 antenna='5&6;7&8': baselines 5-6 and 7-8 00147 antenna='5': all baselines with antenna 5 00148 antenna='5,6,10': all baselines including antennas 5, 6, or 10 00149 antenna='5,6,10&': all baselines with *only* antennas 5, 6, or 00150 10. (cross-correlations only. Use && 00151 to include autocorrelations, and &&& 00152 to get only autocorrelations.) 00153 antenna='!ea03,ea12,ea17': all baselines except those that 00154 include EVLA antennas ea03, ea12, or 00155 ea17. 00156 timerange -- Select data based on time range: 00157 default = '' (all); examples, 00158 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss' 00159 Note: if YYYY/MM/DD is missing date, timerange defaults to the 00160 first day in the dataset 00161 timerange='09:14:0~09:54:0' picks 40 min on first day 00162 timerange='25:00:00~27:30:00' picks 1 hr to 3 hr 30min 00163 on next day 00164 timerange='09:44:00' data within one integration of time 00165 timerange='>10:24:00' data after this time 00166 scan -- Scan number range 00167 default: ''=all 00168 intent -- Select by scan intent (state). Case sensitive. 00169 default: '' = all 00170 Examples: 00171 intent = 'CALIBRATE_ATMOSPHERE_REFERENCE' 00172 intent = 'calibrate_atmosphere_reference'.upper() # same as above 00173 # Select states that include one or both of CALIBRATE_WVR.REFERENCE 00174 # or OBSERVE_TARGET_ON_SOURCE. 00175 intent = 'CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE' 00176 array -- (Sub)array number range 00177 default: ''=all 00178 correlation -- Select correlations, e.g. 'rr, ll' or ['XY', 'YX']. 00179 default '' (all). 00180 observation -- Select by observation ID(s). 00181 default: '' = all 00182 datacolumn -- Which data column to calculate the scatter from 00183 default='corrected'; example: datacolumn='data' 00184 Options: 'data', 'corrected', 'model', 'float_data' 00185 note: 'corrected' will fall back to DATA if CORRECTED_DATA 00186 is absent. 00187 00188 00189 """ 00190 a=inspect.stack() 00191 stacklevel=0 00192 for k in range(len(a)): 00193 if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00194 stacklevel=k 00195 break 00196 myf=sys._getframe(stacklevel).f_globals 00197 myf['__last_task'] = 'statwt' 00198 myf['taskname'] = 'statwt' 00199 ### 00200 myf['update_params'](func=myf['taskname'],printtext=False) 00201 ### 00202 ### 00203 #Handle globals or user over-ride of arguments 00204 # 00205 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00206 useLocalDefaults = False 00207 00208 for item in function_signature_defaults.iteritems(): 00209 key,val = item 00210 keyVal = eval(key) 00211 if (keyVal == None): 00212 #user hasn't set it - use global/default 00213 pass 00214 else: 00215 #user has set it - use over-ride 00216 if (key != 'self') : 00217 useLocalDefaults = True 00218 #myf[key]=keyVal 00219 00220 myparams = {} 00221 if useLocalDefaults : 00222 for item in function_signature_defaults.iteritems(): 00223 key,val = item 00224 keyVal = eval(key) 00225 exec('myparams[key] = keyVal') 00226 if (keyVal == None): 00227 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00228 keyVal = eval(key) 00229 if(type(keyVal) == dict) : 00230 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00231 00232 else : 00233 uselessvariable = None 00234 myparams['vis'] = vis = myf['vis'] 00235 myparams['dorms'] = dorms = myf['dorms'] 00236 myparams['byantenna'] = byantenna = myf['byantenna'] 00237 myparams['sepacs'] = sepacs = myf['sepacs'] 00238 myparams['fitspw'] = fitspw = myf['fitspw'] 00239 myparams['fitcorr'] = fitcorr = myf['fitcorr'] 00240 myparams['combine'] = combine = myf['combine'] 00241 myparams['timebin'] = timebin = myf['timebin'] 00242 myparams['minsamp'] = minsamp = myf['minsamp'] 00243 myparams['field'] = field = myf['field'] 00244 myparams['spw'] = spw = myf['spw'] 00245 myparams['antenna'] = antenna = myf['antenna'] 00246 myparams['timerange'] = timerange = myf['timerange'] 00247 myparams['scan'] = scan = myf['scan'] 00248 myparams['intent'] = intent = myf['intent'] 00249 myparams['array'] = array = myf['array'] 00250 myparams['correlation'] = correlation = myf['correlation'] 00251 myparams['observation'] = observation = myf['observation'] 00252 myparams['datacolumn'] = datacolumn = myf['datacolumn'] 00253 00254 00255 result = None 00256 00257 # 00258 # The following is work around to avoid a bug with current python translation 00259 # 00260 mytmp = {} 00261 00262 mytmp['vis'] = vis 00263 mytmp['dorms'] = dorms 00264 mytmp['byantenna'] = byantenna 00265 mytmp['sepacs'] = sepacs 00266 mytmp['fitspw'] = fitspw 00267 mytmp['fitcorr'] = fitcorr 00268 mytmp['combine'] = combine 00269 mytmp['timebin'] = timebin 00270 mytmp['minsamp'] = minsamp 00271 mytmp['field'] = field 00272 mytmp['spw'] = spw 00273 mytmp['antenna'] = antenna 00274 mytmp['timerange'] = timerange 00275 mytmp['scan'] = scan 00276 mytmp['intent'] = intent 00277 mytmp['array'] = array 00278 mytmp['correlation'] = correlation 00279 mytmp['observation'] = observation 00280 mytmp['datacolumn'] = datacolumn 00281 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00282 trec = casac.utils().torecord(pathname+'statwt.xml') 00283 00284 casalog.origin('statwt') 00285 if not trec.has_key('statwt') or not casac.utils().verify(mytmp, trec['statwt']) : 00286 return False 00287 00288 00289 try : 00290 casalog.post('') 00291 casalog.post('##########################################') 00292 casalog.post('##### Begin Task: statwt #####') 00293 casalog.post('') 00294 result = statwt(vis, dorms, byantenna, sepacs, fitspw, fitcorr, combine, timebin, minsamp, field, spw, antenna, timerange, scan, intent, array, correlation, observation, datacolumn) 00295 casalog.post('') 00296 casalog.post('##### End Task: statwt #####') 00297 casalog.post('##########################################') 00298 00299 00300 # saveinputs for individule engine has no use 00301 # saveinputs should alos be removed from casa_in_py.py 00302 # 00303 # 00304 # saveinputs = myf['saveinputs'] 00305 # saveinputs('statwt', 'statwt.last', myparams) 00306 # 00307 # 00308 except Exception, instance: 00309 #print '**** Error **** ',instance 00310 pass 00311 00312 gc.collect() 00313 return result 00314 # 00315 # 00316 ## 00317 # def paramgui(self, useGlobals=True): 00318 # """ 00319 # Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00320 # """ 00321 # import paramgui 00322 # 00323 # a=inspect.stack() 00324 # stacklevel=0 00325 # for k in range(len(a)): 00326 # if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00327 # stacklevel=k 00328 # break 00329 # myf = sys._getframe(stacklevel).f_globals 00330 # 00331 # if useGlobals: 00332 # paramgui.setGlobals(myf) 00333 # else: 00334 # paramgui.setGlobals({}) 00335 # 00336 # paramgui.runTask('statwt', myf['_ip']) 00337 # paramgui.setGlobals({}) 00338 # 00339 # 00340 # 00341 # 00342 def defaults(self, param=None): 00343 a=inspect.stack() 00344 stacklevel=0 00345 for k in range(len(a)): 00346 if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00347 stacklevel=k 00348 break 00349 myf=sys._getframe(stacklevel).f_globals 00350 a = odict() 00351 a['vis'] = '' 00352 a['dorms'] = False 00353 a['byantenna'] = False 00354 a['fitspw'] = '' 00355 a['fitcorr'] = '' 00356 a['combine'] = '' 00357 a['timebin'] = '0s' 00358 a['minsamp'] = 2 00359 a['field'] = '' 00360 a['spw'] = '' 00361 a['antenna'] = '' 00362 a['timerange'] = '' 00363 a['scan'] = '' 00364 a['intent'] = '' 00365 a['array'] = '' 00366 a['correlation'] = '' 00367 a['observation'] = '' 00368 a['datacolumn'] = 'corrected' 00369 00370 a['async']=False 00371 a['byantenna'] = { 00372 0:{'value':False}, 00373 1:odict([{'value':True}, {'sepacs':True}])} 00374 00375 ### This function sets the default values but also will return the list of 00376 ### parameters or the default value of a given parameter 00377 if(param == None): 00378 myf['__set_default_parameters'](a) 00379 elif(param == 'paramkeys'): 00380 return a.keys() 00381 else: 00382 if(a.has_key(param)): 00383 #if(type(a[param]) == dict) : 00384 # return a[param][len(a[param])-1]['value'] 00385 #else : 00386 return a[param] 00387 00388 00389 # 00390 # 00391 def check_params(self, param=None, value=None): 00392 a=inspect.stack() 00393 stacklevel=0 00394 for k in range(len(a)): 00395 if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00396 stacklevel=k 00397 break 00398 myf=sys._getframe(stacklevel).f_globals 00399 00400 # print 'param:', param, 'value:', value 00401 try : 00402 if str(type(value)) != "<type 'instance'>" : 00403 value0 = value 00404 value = myf['cu'].expandparam(param, value) 00405 matchtype = False 00406 if(type(value) == numpy.ndarray): 00407 if(type(value) == type(value0)): 00408 myf[param] = value.tolist() 00409 else: 00410 #print 'value:', value, 'value0:', value0 00411 #print 'type(value):', type(value), 'type(value0):', type(value0) 00412 myf[param] = value0 00413 if type(value0) != list : 00414 matchtype = True 00415 else : 00416 myf[param] = value 00417 value = myf['cu'].verifyparam({param:value}) 00418 if matchtype: 00419 value = False 00420 except Exception, instance: 00421 #ignore the exception and just return it unchecked 00422 myf[param] = value 00423 return value 00424 00425 # 00426 # 00427 def description(self, key='statwt', subkey=None): 00428 desc={'statwt': ' Reweight visibilities according to their scatter (Experimental)', 00429 'vis': 'Name of measurement set', 00430 'dorms': 'Use rms instead of stddev?', 00431 'byantenna': 'Estimate the noise per antenna -not implemented (vs. per baseline)', 00432 'sepacs': 'If solving by antenna, treat autocorrs separately (not implemented)', 00433 'fitspw': 'The signal-free spectral window:channels to estimate the scatter from', 00434 'fitcorr': 'The signal-free correlation(s) to estimate the scatter from (not implemented)', 00435 'combine': 'Let estimates span changes in spw, corr, scan and/or state', 00436 'timebin': 'Bin length for estimates (not implemented)', 00437 'minsamp': 'Minimum number of unflagged visibilities for estimating the scatter', 00438 'field': 'Select field using ID(s) or name(s)', 00439 'spw': 'Select spectral window/channels', 00440 'antenna': 'Select data based on antenna/baseline', 00441 'timerange': 'Select data by time range', 00442 'scan': 'Select data by scan numbers', 00443 'intent': 'Select data by scan intents', 00444 'array': 'Select (sub)array(s) by array ID number', 00445 'correlation': 'Select correlations to reweight', 00446 'observation': 'Select by observation ID(s)', 00447 'datacolumn': 'Which data column to calculate the scatter from', 00448 00449 'async': 'If true the taskname must be started using statwt(...)' 00450 } 00451 00452 # 00453 # Set subfields defaults if needed 00454 # 00455 00456 if(desc.has_key(key)) : 00457 return desc[key] 00458 00459 def itsdefault(self, paramname) : 00460 a = {} 00461 a['vis'] = '' 00462 a['dorms'] = False 00463 a['byantenna'] = False 00464 a['sepacs'] = True 00465 a['fitspw'] = '' 00466 a['fitcorr'] = '' 00467 a['combine'] = '' 00468 a['timebin'] = '0s' 00469 a['minsamp'] = 2 00470 a['field'] = '' 00471 a['spw'] = '' 00472 a['antenna'] = '' 00473 a['timerange'] = '' 00474 a['scan'] = '' 00475 a['intent'] = '' 00476 a['array'] = '' 00477 a['correlation'] = '' 00478 a['observation'] = '' 00479 a['datacolumn'] = 'corrected' 00480 00481 if a.has_key(paramname) : 00482 return a[paramname] 00483 statwt_pg = statwt_pg_()