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_sdstat import sdstat 00018 class sdstat_cli_: 00019 __name__ = "sdstat" 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__ = (sdstat_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'infile':None, 'antenna':None, 'fluxunit':None, 'telescopeparm':None, 'specunit':None, 'restfreq':None, 'frame':None, 'doppler':None, 'scanlist':None, 'field':None, 'iflist':None, 'pollist':None, 'masklist':None, 'invertmask':None, 'interactive':None, 'outfile':None, 'format':None, 'overwrite':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, fluxunit=None, telescopeparm=None, specunit=None, restfreq=None, frame=None, doppler=None, scanlist=None, field=None, iflist=None, pollist=None, masklist=None, invertmask=None, interactive=None, outfile=None, format=None, overwrite=None, async=None): 00047 00048 """ASAP SD task: list statistics of spectral region 00049 Keyword arguments: 00050 infile -- name of input SD dataset 00051 default: none - must input file name 00052 example: 'mysd.asap' 00053 See sdcal for allowed formats. 00054 antenna -- antenna name or id (only effective for MS input). 00055 fluxunit -- units for line flux 00056 options: (str) 'K','Jy','' 00057 default: '' (keep current fluxunit) 00058 WARNING: For GBT data, see description below. 00059 >>> fluxunit expandable parameter 00060 telescopeparm -- the telescope characteristics 00061 options: (str) name or (list) list of gain info 00062 default: '' (none set) 00063 example: if telescopeparm='', it tries to get the telescope 00064 name from the data. 00065 Full antenna parameters (diameter,ap.eff.) known 00066 to ASAP are 00067 'ATPKSMB', 'ATPKSHOH', 'ATMOPRA', 'DSS-43', 00068 'CEDUNA','HOBART'. For GBT, it fixes default fluxunit 00069 to 'K' first then convert to a new fluxunit. 00070 telescopeparm=[104.9,0.43] diameter(m), ap.eff. 00071 telescopeparm=[0.743] gain in Jy/K 00072 telescopeparm='FIX' to change default fluxunit 00073 see description below 00074 00075 specunit -- units for spectral axis 00076 options: (str) 'channel','km/s','GHz','MHz','kHz','Hz','' 00077 default: '' (=current) 00078 >>> specunit expandable parameter 00079 restfreq -- rest frequency 00080 default: '' (use current setting) 00081 example: 4.6e10 (float value in Hz), 00082 '46GHz' (string with unit), 00083 ['345.8GHz', 347.0e9, 356.7e9] (for each IF) 00084 [{'name':'CO','value':345e9}] (a value with name) 00085 frame -- frequency frame for spectral axis 00086 options: (str) 'LSRK','REST','TOPO','LSRD','BARY', 00087 'GEO','GALACTO','LGROUP','CMB' 00088 default: currently set frame in scantable 00089 WARNING: frame='REST' not yet implemented 00090 doppler -- doppler mode 00091 options: (str) 'RADIO','OPTICAL','Z','BETA','GAMMA' 00092 default: currently set doppler in scantable 00093 scanlist -- list of scan numbers to process 00094 default: [] (use all scans) 00095 example: [21,22,23,24] 00096 this selection is in addition to field, iflist, and pollist 00097 field -- selection string for selecting scans by name 00098 default: '' (no name selection) 00099 example: 'FLS3a*' 00100 this selection is in addition to scanlist, iflist, and pollist 00101 iflist -- list of IF id numbers to select 00102 default: [] (use all IFs) 00103 example: [15] 00104 this selection is in addition to field, scanlist, and pollist 00105 pollist -- list of polarization id numbers to select 00106 default: [] (use all pols) 00107 example: [1] 00108 this selection is in addition to field, scanlist, and iflist 00109 masklist -- list of mask regions to INCLUDE in stats 00110 default: [] (whole spectrum) 00111 example: [4000,4500] for one region 00112 [[1000,3000],[5000,7000]] 00113 these must be pairs of [lo,hi] boundaries 00114 invertmask -- invert mask (EXCLUDE masklist instead) 00115 options: (bool) True,False 00116 default: false 00117 interactive -- determines interactive masking 00118 options: (bool) True,False 00119 default: False 00120 example: interactive=True allows adding and deleting mask 00121 regions by drawing rectangles on the plot with mouse. 00122 Draw a rectangle with LEFT-mouse to ADD the region to 00123 the mask and with RIGHT-mouse to DELETE the region. 00124 outfile -- name of output file for line statistics 00125 default: '' (no output statistics file) 00126 example: 'stat.txt' 00127 format -- format string to print statistic values 00128 default: '3.3f' 00129 overwrite -- overwrite the statistics file if already exists 00130 options: (bool) True,False 00131 default: False 00132 00133 ------------------------------------------------------------------- 00134 Returns: a Python dictionary of line statistics 00135 keys: 'rms','stddev','max','min','max_abscissa', 00136 'min_abscissa','sum','median','mean','totint','eqw' 00137 example: xstat=sdstat(); print "rms = ",xstat['rms'] 00138 these can be used for testing in scripts or 00139 for regression 00140 00141 'max_abscissa' and 'min_abscissa' refer to the abscissa 00142 (channel/frequency/velocity) of max and min intensity. 00143 'totint' is the integrated intensity (sum*dx) 00144 where dx is the abscissa interval in 'specunit'. 00145 'eqw' is equivalent width (totint/mag) where mag 00146 is either max or min depending on which has 00147 greater magnitude. 00148 Note that 'max_abscissa', 'min_abscissa', 'totint' 00149 and 'eqw' are quantities (python dictionaries with 00150 keys, 'unit' and 'value'). 00151 00152 00153 DESCRIPTION: 00154 00155 Task sdstat computes basic statistics (rms,mean,median,sum) 00156 for single-dish spectra. It assumes that the spectra have 00157 been calibrated. Furthermore, it assumes that any 00158 time and channel averaging/smoothing has also already been done as 00159 there are no controls for these. 00160 00161 Note that multiple scans and IFs can in principle be handled, but 00162 we recommend that you use scanlist, field, iflist, and pollist to give 00163 a single selection for each run. 00164 00165 WARNING: If you do have multiple scantable rows, then the returning 00166 values will be lists. 00167 00168 ASAP recognizes the data of the "AT" telescopes, but currently 00169 does not know about the GBT or any other telescope. This task 00170 does know about GBT. Telescope name is obtained from the data. 00171 If you wish to change the fluxunit (see below), and telescopeparm='', 00172 for the AT telescopes it will use internal telescope parameters for 00173 flux conversion. For GBT, it will use an approximate aperture 00174 efficiency conversion. If you give telescopeparm a list, then 00175 if the list has a single float it is assumed to be the gain in Jy/K, 00176 if two or more elements they are assumed to be telescope diameter (m) 00177 and aperture efficiency respectively. 00178 00179 WARNING for the GBT raw SDFITS format data as input: 00180 SDtasks are able to handle GBT raw SDFITS format data since the 00181 data filler is available. However, the functionality is not well 00182 tested yet, so that there may be unknown bugs. 00183 00184 00185 """ 00186 if not hasattr(self, "__globals__") or self.__globals__ == None : 00187 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00188 #casac = self.__globals__['casac'] 00189 casalog = self.__globals__['casalog'] 00190 #casalog = casac.casac.logsink() 00191 self.__globals__['__last_task'] = 'sdstat' 00192 self.__globals__['taskname'] = 'sdstat' 00193 ### 00194 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00195 ### 00196 ### 00197 #Handle globals or user over-ride of arguments 00198 # 00199 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00200 useLocalDefaults = False 00201 00202 for item in function_signature_defaults.iteritems(): 00203 key,val = item 00204 keyVal = eval(key) 00205 if (keyVal == None): 00206 #user hasn't set it - use global/default 00207 pass 00208 else: 00209 #user has set it - use over-ride 00210 if (key != 'self') : 00211 useLocalDefaults = True 00212 00213 myparams = {} 00214 if useLocalDefaults : 00215 for item in function_signature_defaults.iteritems(): 00216 key,val = item 00217 keyVal = eval(key) 00218 exec('myparams[key] = keyVal') 00219 self.parameters[key] = keyVal 00220 if (keyVal == None): 00221 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00222 keyVal = eval(key) 00223 if(type(keyVal) == dict) : 00224 if len(keyVal) > 0 : 00225 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00226 else : 00227 exec('myparams[key] = ' + key + ' = {}') 00228 00229 else : 00230 async = self.parameters['async'] 00231 myparams['infile'] = infile = self.parameters['infile'] 00232 myparams['antenna'] = antenna = self.parameters['antenna'] 00233 myparams['fluxunit'] = fluxunit = self.parameters['fluxunit'] 00234 myparams['telescopeparm'] = telescopeparm = self.parameters['telescopeparm'] 00235 myparams['specunit'] = specunit = self.parameters['specunit'] 00236 myparams['restfreq'] = restfreq = self.parameters['restfreq'] 00237 myparams['frame'] = frame = self.parameters['frame'] 00238 myparams['doppler'] = doppler = self.parameters['doppler'] 00239 myparams['scanlist'] = scanlist = self.parameters['scanlist'] 00240 myparams['field'] = field = self.parameters['field'] 00241 myparams['iflist'] = iflist = self.parameters['iflist'] 00242 myparams['pollist'] = pollist = self.parameters['pollist'] 00243 myparams['masklist'] = masklist = self.parameters['masklist'] 00244 myparams['invertmask'] = invertmask = self.parameters['invertmask'] 00245 myparams['interactive'] = interactive = self.parameters['interactive'] 00246 myparams['outfile'] = outfile = self.parameters['outfile'] 00247 myparams['format'] = format = self.parameters['format'] 00248 myparams['overwrite'] = overwrite = self.parameters['overwrite'] 00249 00250 if type(scanlist)==int: scanlist=[scanlist] 00251 if type(iflist)==int: iflist=[iflist] 00252 if type(pollist)==int: pollist=[pollist] 00253 if type(masklist)==int: masklist=[masklist] 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['infile'] = infile 00263 mytmp['antenna'] = antenna 00264 mytmp['fluxunit'] = fluxunit 00265 mytmp['telescopeparm'] = telescopeparm 00266 mytmp['specunit'] = specunit 00267 mytmp['restfreq'] = restfreq 00268 mytmp['frame'] = frame 00269 mytmp['doppler'] = doppler 00270 mytmp['scanlist'] = scanlist 00271 mytmp['field'] = field 00272 mytmp['iflist'] = iflist 00273 mytmp['pollist'] = pollist 00274 mytmp['masklist'] = masklist 00275 mytmp['invertmask'] = invertmask 00276 mytmp['interactive'] = interactive 00277 mytmp['outfile'] = outfile 00278 mytmp['format'] = format 00279 mytmp['overwrite'] = overwrite 00280 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00281 trec = casac.casac.utils().torecord(pathname+'sdstat.xml') 00282 00283 casalog.origin('sdstat') 00284 try : 00285 #if not trec.has_key('sdstat') or not casac.casac.utils().verify(mytmp, trec['sdstat']) : 00286 #return False 00287 00288 casac.casac.utils().verify(mytmp, trec['sdstat'], True) 00289 scriptstr=[''] 00290 saveinputs = self.__globals__['saveinputs'] 00291 saveinputs('sdstat', 'sdstat.last', myparams, self.__globals__,scriptstr=scriptstr) 00292 if async : 00293 count = 0 00294 keybase = time.strftime("%y%m%d.%H%M%S") 00295 key = keybase + "_" + str(count) 00296 while self.__async__.has_key(key) : 00297 count += 1 00298 key = keybase + "_" + str(count) 00299 result = tm.execute('sdstat', infile, antenna, fluxunit, telescopeparm, specunit, restfreq, frame, doppler, scanlist, field, iflist, pollist, masklist, invertmask, interactive, outfile, format, overwrite) 00300 print "Use: " 00301 print " tm.retrieve(return_value) # to retrieve the status" 00302 print 00303 self.rkey = key 00304 self.__async__[key] = result 00305 else : 00306 tname = 'sdstat' 00307 spaces = ' '*(18-len(tname)) 00308 casalog.post('\n##########################################'+ 00309 '\n##### Begin Task: ' + tname + spaces + ' #####') 00310 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00311 result = sdstat(infile, antenna, fluxunit, telescopeparm, specunit, restfreq, frame, doppler, scanlist, field, iflist, pollist, masklist, invertmask, interactive, outfile, format, overwrite) 00312 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00313 '\n##########################################') 00314 00315 except Exception, instance: 00316 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00317 raise 00318 else : 00319 #print '**** Error **** ',instance 00320 tname = 'sdstat' 00321 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00322 pass 00323 00324 gc.collect() 00325 return result 00326 # 00327 # 00328 # 00329 def paramgui(self, useGlobals=True, ipython_globals=None): 00330 """ 00331 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00332 """ 00333 import paramgui 00334 if not hasattr(self, "__globals__") or self.__globals__ == None : 00335 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00336 00337 if useGlobals: 00338 if ipython_globals == None: 00339 myf=self.__globals__ 00340 else: 00341 myf=ipython_globals 00342 00343 paramgui.setGlobals(myf) 00344 else: 00345 paramgui.setGlobals({}) 00346 00347 paramgui.runTask('sdstat', myf['_ip']) 00348 paramgui.setGlobals({}) 00349 00350 # 00351 # 00352 # 00353 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00354 if not hasattr(self, "__globals__") or self.__globals__ == None : 00355 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00356 if ipython_globals == None: 00357 myf=self.__globals__ 00358 else: 00359 myf=ipython_globals 00360 00361 a = odict() 00362 a['infile'] = '' 00363 a['antenna'] = 0 00364 a['fluxunit'] = '' 00365 a['specunit'] = '' 00366 a['frame'] = '' 00367 a['doppler'] = '' 00368 a['scanlist'] = [] 00369 a['field'] = '' 00370 a['iflist'] = [] 00371 a['pollist'] = [] 00372 a['masklist'] = [] 00373 a['invertmask'] = False 00374 a['interactive'] = False 00375 a['outfile'] = '' 00376 a['format'] = '3.3f' 00377 a['overwrite'] = False 00378 00379 a['async']=False 00380 a['fluxunit'] = { 00381 0:{'value':''}, 00382 1:odict([{'value':'K'}, {'telescopeparm':''}]), 00383 2:odict([{'value':'k'}, {'telescopeparm':''}]), 00384 3:odict([{'value':'Jy'}, {'telescopeparm':''}]), 00385 4:odict([{'value':'jy'}, {'telescopeparm':''}])} 00386 a['specunit'] = { 00387 0:{'value':''}, 00388 1:{'value':'channel'}, 00389 2:odict([{'value':'km/s'}, {'restfreq':''}]), 00390 3:{'value':'GHz'}, 00391 4:{'value':'MHz'}, 00392 5:{'value':'kHz'}, 00393 6:{'value':'Hz'}} 00394 00395 ### This function sets the default values but also will return the list of 00396 ### parameters or the default value of a given parameter 00397 if(param == None): 00398 myf['__set_default_parameters'](a) 00399 elif(param == 'paramkeys'): 00400 return a.keys() 00401 else: 00402 if(paramvalue==None and subparam==None): 00403 if(a.has_key(param)): 00404 return a[param] 00405 else: 00406 return self.itsdefault(param) 00407 else: 00408 retval=a[param] 00409 if(type(a[param])==dict): 00410 for k in range(len(a[param])): 00411 valornotval='value' 00412 if(a[param][k].has_key('notvalue')): 00413 valornotval='notvalue' 00414 if((a[param][k][valornotval])==paramvalue): 00415 retval=a[param][k].copy() 00416 retval.pop(valornotval) 00417 if(subparam != None): 00418 if(retval.has_key(subparam)): 00419 retval=retval[subparam] 00420 else: 00421 retval=self.itsdefault(subparam) 00422 else: 00423 retval=self.itsdefault(subparam) 00424 return retval 00425 00426 00427 # 00428 # 00429 def check_params(self, param=None, value=None, ipython_globals=None): 00430 if ipython_globals == None: 00431 myf=self.__globals__ 00432 else: 00433 myf=ipython_globals 00434 # print 'param:', param, 'value:', value 00435 try : 00436 if str(type(value)) != "<type 'instance'>" : 00437 value0 = value 00438 value = myf['cu'].expandparam(param, value) 00439 matchtype = False 00440 if(type(value) == numpy.ndarray): 00441 if(type(value) == type(value0)): 00442 myf[param] = value.tolist() 00443 else: 00444 #print 'value:', value, 'value0:', value0 00445 #print 'type(value):', type(value), 'type(value0):', type(value0) 00446 myf[param] = value0 00447 if type(value0) != list : 00448 matchtype = True 00449 else : 00450 myf[param] = value 00451 value = myf['cu'].verifyparam({param:value}) 00452 if matchtype: 00453 value = False 00454 except Exception, instance: 00455 #ignore the exception and just return it unchecked 00456 myf[param] = value 00457 return value 00458 # 00459 # 00460 def description(self, key='sdstat', subkey=None): 00461 desc={'sdstat': 'ASAP SD task: list statistics of spectral region', 00462 'infile': 'name of input SD dataset', 00463 'antenna': 'antenna name or id (only effective for MS input)', 00464 'fluxunit': 'units for line flux (K,Jy) (''=current)', 00465 'telescopeparm': 'param of telescope for flux conversion', 00466 'specunit': 'units for spectral axis (channel,km/s,GHz)', 00467 'restfreq': 'rest frequency (default unit: Hz)', 00468 'frame': 'frequency reference frame, e.g. LSRK (''=current)', 00469 'doppler': 'doppler convention, e.g. RADIO (''=current)', 00470 'scanlist': 'list of scans to use (e.g. [1,2,3,4])', 00471 'field': 'string for selection by source name', 00472 'iflist': 'list of IF ids to select (e.g. [0,1])', 00473 'pollist': 'list of polarization ids to select (e.g. [0,1])', 00474 'masklist': 'list of mask regions to INCLUDE in stats', 00475 'invertmask': 'invert mask (EXCLUDE masklist instead)', 00476 'interactive': 'determines interactive masking', 00477 'outfile': 'name of output file for line statistics', 00478 'format': 'format string to print statistic values', 00479 'overwrite': 'overwrite the statistics file if already exists', 00480 00481 'async': 'If true the taskname must be started using sdstat(...)' 00482 } 00483 00484 # 00485 # Set subfields defaults if needed 00486 # 00487 00488 if(desc.has_key(key)) : 00489 return desc[key] 00490 00491 def itsdefault(self, paramname) : 00492 a = {} 00493 a['infile'] = '' 00494 a['antenna'] = 0 00495 a['fluxunit'] = '' 00496 a['telescopeparm'] = '' 00497 a['specunit'] = '' 00498 a['restfreq'] = '' 00499 a['frame'] = '' 00500 a['doppler'] = '' 00501 a['scanlist'] = [] 00502 a['field'] = '' 00503 a['iflist'] = [] 00504 a['pollist'] = [] 00505 a['masklist'] = [] 00506 a['invertmask'] = False 00507 a['interactive'] = False 00508 a['outfile'] = '' 00509 a['format'] = '3.3f' 00510 a['overwrite'] = False 00511 00512 #a = sys._getframe(len(inspect.stack())-1).f_globals 00513 00514 if self.parameters['fluxunit'] == 'K': 00515 a['telescopeparm'] = '' 00516 00517 if self.parameters['fluxunit'] == 'k': 00518 a['telescopeparm'] = '' 00519 00520 if self.parameters['fluxunit'] == 'Jy': 00521 a['telescopeparm'] = '' 00522 00523 if self.parameters['fluxunit'] == 'jy': 00524 a['telescopeparm'] = '' 00525 00526 if self.parameters['specunit'] == 'km/s': 00527 a['restfreq'] = '' 00528 00529 if a.has_key(paramname) : 00530 return a[paramname] 00531 sdstat_cli = sdstat_cli_()