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_importasdm import importasdm 00018 class importasdm_cli_: 00019 __name__ = "importasdm" 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__ = (importasdm_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'asdm':None, 'vis':None, 'singledish':None, 'antenna':None, 'corr_mode':None, 'srt':None, 'time_sampling':None, 'ocorr_mode':None, 'compression':None, 'asis':None, 'wvr_corrected_data':None, 'scans':None, 'ignore_time':None, 'process_syspower':None, 'process_caldevice':None, 'process_pointing':None, 'process_flags':None, 'tbuff':None, 'applyflags':None, 'savecmds':None, 'outfile':None, 'verbose':None, 'overwrite':None, 'showversion':None, 'useversion':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, asdm=None, vis=None, singledish=None, antenna=None, corr_mode=None, srt=None, time_sampling=None, ocorr_mode=None, compression=None, asis=None, wvr_corrected_data=None, scans=None, ignore_time=None, process_syspower=None, process_caldevice=None, process_pointing=None, process_flags=None, tbuff=None, applyflags=None, savecmds=None, outfile=None, verbose=None, overwrite=None, showversion=None, useversion=None, async=None): 00047 00048 """Convert an ALMA Science Data Model observation into a CASA visibility file (MS) or single-dish data format (Scantable) 00049 Keyword arguments: 00050 asdm -- Name of input ASDM file (directory) 00051 default: none; example: asdm='ExecBlock3' 00052 00053 vis -- Root ms or scantable name, note a prefix (.ms or .asap) is NOT appended to this name 00054 default: none 00055 00056 singledish -- Set True to write data as single-dish format (Scantable) 00057 default: False 00058 00059 >>> singledish expandable parameter 00060 antenna -- antenna name or id. 00061 00062 corr_mode -- correlation mode to be considered on input. Could 00063 be one or more of the following, ao, co, ac, or all 00064 default: 'all' 00065 00066 srt -- spectral resolution type. Could be one or more of 00067 the following, fr, ca, bw, or all 00068 default: 'all' 00069 00070 time_sampling -- specifies the time sampling, INTEGRATION and/or 00071 SUBINTEGRAION. could be one or more of the following 00072 i, si, or all. 00073 default: 'all' 00074 00075 ocorr_mode -- output data for correlation mode AUTO_ONLY 00076 (ao) or CROSS_ONLY (co) or CROSS_AND_AUTO (ca) 00077 default: 'ca' 00078 00079 00080 compression -- produces comrpressed columns in the resulting measurement set. 00081 default: False 00082 00083 00084 asis -- creates verbatim copies of the ASDM tables in 00085 the output measurement set. The value given to 00086 this option must be a list of table names separated 00087 by space characters; the wildcard character '*' is 00088 allowed in table names. 00089 default: none 00090 00091 wvr_corrected_data -- specifies wich values are considered in the 00092 ASDM binary data to fill the DATA column in 00093 the MAIN table of the MS. Expected values for 00094 this option are 'no' for the uncorrected data 00095 (this is the default), 'yes' for the corrected 00096 data and 'both' for corrected and uncorrected 00097 data. In the latter case, two measurement sets 00098 are created, one containing the uncorrected 00099 data and the other one, whose name is suffixed 00100 by '-wvr-corrected', containing the corrected 00101 data. 00102 default: 'no' 00103 00104 scans -- processes only the scans specified in the option's value. This value is a semicolon 00105 separated list of scan specifications. A scan specification consists in an exec bock index 00106 followed by the character ':' followed by a comma separated list of scan indexes or scan 00107 index ranges. A scan index is relative to the exec block it belongs to. Scan indexes are 00108 1-based while exec blocks's are 0-based. '0:1' or '2:2~6' or '0:1,1:2~6,8;2:,3:24~30' '1,2' 00109 are valid values for the option. '3:' alone will be interpreted as 'all the scans of the 00110 exec block#3'. An scan index or a scan index range not preceded by an exec block index will 00111 be interpreted as 'all the scans with such indexes in all the exec blocks'. By default 00112 all the scans are considered. 00113 default: none (all scans) 00114 00115 ignore_time -- All the rows of the tables Feed, History, Pointing, Source, SysCal, CalDevice, SysPower, 00116 and Weather are processed independently of the time range of the selected exec block / scan. 00117 default: False 00118 00119 process_syspower -- The SysPower table is processed if and only if this parameter is set to True. 00120 default: True 00121 00122 process_caldevice -- The CalDevice table is processed if and only if this parameter is set to True. 00123 default: True 00124 00125 process_pointing -- The Pointing table is processed if and only if this parameter is set to True. 00126 default: True 00127 00128 process_flags -- Create online flags based on the Flag.xml, Antenna.xml and SpectralWindow.xml files 00129 and copy them to the FLAG_CMD sub-table of the MS. The flags will NOT be applied unless 00130 the parameter applyflags is set to True. Optionally, the flags can also be saved to 00131 an external ASCII file if savecmds is set to True. 00132 default: True 00133 00134 >>> process_flags expandable parameter 00135 tbuff -- Time padding buffer (in seconds) 00136 default: 0.0 00137 00138 NOTE: this time is in seconds. You should currently 00139 set the value of tbuff to be 1.5x the correlator 00140 integration time if greater than 1 second. For 00141 example, if the SDM has integrations of 3 seconds, 00142 set tbuff=4.5. Likewise, set tbuff=15.0 for 10-sec 00143 integrations. 00144 00145 applyflags -- Apply the online flags to the MS. 00146 default: False 00147 00148 00149 savecmds -- Save the flag commands to an ASCII file given by the parameter outfile. 00150 default: False 00151 00152 outfile -- Filename where to save the flag commands. 00153 default: ' ' --> by default it will save on a filename composed from the MS name. 00154 Example: vis='uid_A02.ms', the outfile will be 'uid_A02_cmd.txt'. 00155 00156 NOTE: The file is open to save in append mode. 00157 00158 00159 verbose -- produce log output as asdm2MS is being run 00160 default: False 00161 00162 showversion -- report the version of the asdm2MS being used. 00163 default: False 00164 00165 useversion -- Selects the version of asdm2MS to be used (\'v3\' (default, should work for all data)) 00166 default: v3 00167 00168 00169 """ 00170 if not hasattr(self, "__globals__") or self.__globals__ == None : 00171 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00172 #casac = self.__globals__['casac'] 00173 casalog = self.__globals__['casalog'] 00174 #casalog = casac.casac.logsink() 00175 self.__globals__['__last_task'] = 'importasdm' 00176 self.__globals__['taskname'] = 'importasdm' 00177 ### 00178 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00179 ### 00180 ### 00181 #Handle globals or user over-ride of arguments 00182 # 00183 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00184 useLocalDefaults = False 00185 00186 for item in function_signature_defaults.iteritems(): 00187 key,val = item 00188 keyVal = eval(key) 00189 if (keyVal == None): 00190 #user hasn't set it - use global/default 00191 pass 00192 else: 00193 #user has set it - use over-ride 00194 if (key != 'self') : 00195 useLocalDefaults = True 00196 00197 myparams = {} 00198 if useLocalDefaults : 00199 for item in function_signature_defaults.iteritems(): 00200 key,val = item 00201 keyVal = eval(key) 00202 exec('myparams[key] = keyVal') 00203 self.parameters[key] = keyVal 00204 if (keyVal == None): 00205 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00206 keyVal = eval(key) 00207 if(type(keyVal) == dict) : 00208 if len(keyVal) > 0 : 00209 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00210 else : 00211 exec('myparams[key] = ' + key + ' = {}') 00212 00213 else : 00214 async = self.parameters['async'] 00215 myparams['asdm'] = asdm = self.parameters['asdm'] 00216 myparams['vis'] = vis = self.parameters['vis'] 00217 myparams['singledish'] = singledish = self.parameters['singledish'] 00218 myparams['antenna'] = antenna = self.parameters['antenna'] 00219 myparams['corr_mode'] = corr_mode = self.parameters['corr_mode'] 00220 myparams['srt'] = srt = self.parameters['srt'] 00221 myparams['time_sampling'] = time_sampling = self.parameters['time_sampling'] 00222 myparams['ocorr_mode'] = ocorr_mode = self.parameters['ocorr_mode'] 00223 myparams['compression'] = compression = self.parameters['compression'] 00224 myparams['asis'] = asis = self.parameters['asis'] 00225 myparams['wvr_corrected_data'] = wvr_corrected_data = self.parameters['wvr_corrected_data'] 00226 myparams['scans'] = scans = self.parameters['scans'] 00227 myparams['ignore_time'] = ignore_time = self.parameters['ignore_time'] 00228 myparams['process_syspower'] = process_syspower = self.parameters['process_syspower'] 00229 myparams['process_caldevice'] = process_caldevice = self.parameters['process_caldevice'] 00230 myparams['process_pointing'] = process_pointing = self.parameters['process_pointing'] 00231 myparams['process_flags'] = process_flags = self.parameters['process_flags'] 00232 myparams['tbuff'] = tbuff = self.parameters['tbuff'] 00233 myparams['applyflags'] = applyflags = self.parameters['applyflags'] 00234 myparams['savecmds'] = savecmds = self.parameters['savecmds'] 00235 myparams['outfile'] = outfile = self.parameters['outfile'] 00236 myparams['verbose'] = verbose = self.parameters['verbose'] 00237 myparams['overwrite'] = overwrite = self.parameters['overwrite'] 00238 myparams['showversion'] = showversion = self.parameters['showversion'] 00239 myparams['useversion'] = useversion = self.parameters['useversion'] 00240 00241 00242 result = None 00243 00244 # 00245 # The following is work around to avoid a bug with current python translation 00246 # 00247 mytmp = {} 00248 00249 mytmp['asdm'] = asdm 00250 mytmp['vis'] = vis 00251 mytmp['singledish'] = singledish 00252 mytmp['antenna'] = antenna 00253 mytmp['corr_mode'] = corr_mode 00254 mytmp['srt'] = srt 00255 mytmp['time_sampling'] = time_sampling 00256 mytmp['ocorr_mode'] = ocorr_mode 00257 mytmp['compression'] = compression 00258 mytmp['asis'] = asis 00259 mytmp['wvr_corrected_data'] = wvr_corrected_data 00260 mytmp['scans'] = scans 00261 mytmp['ignore_time'] = ignore_time 00262 mytmp['process_syspower'] = process_syspower 00263 mytmp['process_caldevice'] = process_caldevice 00264 mytmp['process_pointing'] = process_pointing 00265 mytmp['process_flags'] = process_flags 00266 mytmp['tbuff'] = tbuff 00267 mytmp['applyflags'] = applyflags 00268 mytmp['savecmds'] = savecmds 00269 mytmp['outfile'] = outfile 00270 mytmp['verbose'] = verbose 00271 mytmp['overwrite'] = overwrite 00272 mytmp['showversion'] = showversion 00273 mytmp['useversion'] = useversion 00274 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00275 trec = casac.casac.utils().torecord(pathname+'importasdm.xml') 00276 00277 casalog.origin('importasdm') 00278 try : 00279 #if not trec.has_key('importasdm') or not casac.casac.utils().verify(mytmp, trec['importasdm']) : 00280 #return False 00281 00282 casac.casac.utils().verify(mytmp, trec['importasdm'], True) 00283 scriptstr=[''] 00284 saveinputs = self.__globals__['saveinputs'] 00285 saveinputs('importasdm', 'importasdm.last', myparams, self.__globals__,scriptstr=scriptstr) 00286 if async : 00287 count = 0 00288 keybase = time.strftime("%y%m%d.%H%M%S") 00289 key = keybase + "_" + str(count) 00290 while self.__async__.has_key(key) : 00291 count += 1 00292 key = keybase + "_" + str(count) 00293 result = tm.execute('importasdm', asdm, vis, singledish, antenna, corr_mode, srt, time_sampling, ocorr_mode, compression, asis, wvr_corrected_data, scans, ignore_time, process_syspower, process_caldevice, process_pointing, process_flags, tbuff, applyflags, savecmds, outfile, verbose, overwrite, showversion, useversion) 00294 print "Use: " 00295 print " tm.retrieve(return_value) # to retrieve the status" 00296 print 00297 self.rkey = key 00298 self.__async__[key] = result 00299 else : 00300 tname = 'importasdm' 00301 spaces = ' '*(18-len(tname)) 00302 casalog.post('\n##########################################'+ 00303 '\n##### Begin Task: ' + tname + spaces + ' #####') 00304 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00305 result = importasdm(asdm, vis, singledish, antenna, corr_mode, srt, time_sampling, ocorr_mode, compression, asis, wvr_corrected_data, scans, ignore_time, process_syspower, process_caldevice, process_pointing, process_flags, tbuff, applyflags, savecmds, outfile, verbose, overwrite, showversion, useversion) 00306 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00307 '\n##########################################') 00308 00309 except Exception, instance: 00310 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00311 raise 00312 else : 00313 #print '**** Error **** ',instance 00314 tname = 'importasdm' 00315 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00316 pass 00317 00318 gc.collect() 00319 return result 00320 # 00321 # 00322 # 00323 def paramgui(self, useGlobals=True, ipython_globals=None): 00324 """ 00325 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00326 """ 00327 import paramgui 00328 if not hasattr(self, "__globals__") or self.__globals__ == None : 00329 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00330 00331 if useGlobals: 00332 if ipython_globals == None: 00333 myf=self.__globals__ 00334 else: 00335 myf=ipython_globals 00336 00337 paramgui.setGlobals(myf) 00338 else: 00339 paramgui.setGlobals({}) 00340 00341 paramgui.runTask('importasdm', myf['_ip']) 00342 paramgui.setGlobals({}) 00343 00344 # 00345 # 00346 # 00347 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00348 if not hasattr(self, "__globals__") or self.__globals__ == None : 00349 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00350 if ipython_globals == None: 00351 myf=self.__globals__ 00352 else: 00353 myf=ipython_globals 00354 00355 a = odict() 00356 a['asdm'] = '' 00357 a['vis'] = '' 00358 a['singledish'] = False 00359 a['corr_mode'] = 'all' 00360 a['srt'] = 'all' 00361 a['time_sampling'] = 'all' 00362 a['ocorr_mode'] = 'ca' 00363 a['compression'] = False 00364 a['asis'] = '' 00365 a['wvr_corrected_data'] = 'no' 00366 a['scans'] = '' 00367 a['ignore_time'] = False 00368 a['process_syspower'] = True 00369 a['process_caldevice'] = True 00370 a['process_pointing'] = True 00371 a['process_flags'] = True 00372 a['verbose'] = False 00373 a['overwrite'] = False 00374 a['showversion'] = False 00375 a['useversion'] = 'v3' 00376 00377 a['async']=False 00378 a['singledish'] = { 00379 0:{'value':False}, 00380 1:odict([{'value':True}, {'antenna':0}])} 00381 a['process_flags'] = { 00382 0:odict([{'value':True}, {'tbuff':0.0}, {'applyflags':False}, {'savecmds':False}, {'outfile':''}]), 00383 1:{'value':False}} 00384 00385 ### This function sets the default values but also will return the list of 00386 ### parameters or the default value of a given parameter 00387 if(param == None): 00388 myf['__set_default_parameters'](a) 00389 elif(param == 'paramkeys'): 00390 return a.keys() 00391 else: 00392 if(paramvalue==None and subparam==None): 00393 if(a.has_key(param)): 00394 return a[param] 00395 else: 00396 return self.itsdefault(param) 00397 else: 00398 retval=a[param] 00399 if(type(a[param])==dict): 00400 for k in range(len(a[param])): 00401 valornotval='value' 00402 if(a[param][k].has_key('notvalue')): 00403 valornotval='notvalue' 00404 if((a[param][k][valornotval])==paramvalue): 00405 retval=a[param][k].copy() 00406 retval.pop(valornotval) 00407 if(subparam != None): 00408 if(retval.has_key(subparam)): 00409 retval=retval[subparam] 00410 else: 00411 retval=self.itsdefault(subparam) 00412 else: 00413 retval=self.itsdefault(subparam) 00414 return retval 00415 00416 00417 # 00418 # 00419 def check_params(self, param=None, value=None, ipython_globals=None): 00420 if ipython_globals == None: 00421 myf=self.__globals__ 00422 else: 00423 myf=ipython_globals 00424 # print 'param:', param, 'value:', value 00425 try : 00426 if str(type(value)) != "<type 'instance'>" : 00427 value0 = value 00428 value = myf['cu'].expandparam(param, value) 00429 matchtype = False 00430 if(type(value) == numpy.ndarray): 00431 if(type(value) == type(value0)): 00432 myf[param] = value.tolist() 00433 else: 00434 #print 'value:', value, 'value0:', value0 00435 #print 'type(value):', type(value), 'type(value0):', type(value0) 00436 myf[param] = value0 00437 if type(value0) != list : 00438 matchtype = True 00439 else : 00440 myf[param] = value 00441 value = myf['cu'].verifyparam({param:value}) 00442 if matchtype: 00443 value = False 00444 except Exception, instance: 00445 #ignore the exception and just return it unchecked 00446 myf[param] = value 00447 return value 00448 # 00449 # 00450 def description(self, key='importasdm', subkey=None): 00451 desc={'importasdm': 'Convert an ALMA Science Data Model observation into a CASA visibility file (MS) or single-dish data format (Scantable)', 00452 'asdm': 'Name of input asdm directory (on disk)', 00453 'vis': 'Root name of the ms to be created. Note the .ms is NOT added ', 00454 'singledish': 'Set true to output single-dish data format ', 00455 'antenna': 'antenna name or id', 00456 'corr_mode': 'specifies the correlation mode to be considered on input. A quoted string containing a sequence of ao, co, ac,or all separated by whitespaces is expected', 00457 'srt': 'specifies the spectral resolution type to be considered on input. A quoted string containing a sequence of fr, ca, bw, or all separated by whitespaces is expected', 00458 'time_sampling': 'specifies the time sampling (INTEGRATION and/or SUBINTEGRATION) to be considered on input. A quoted string containing a sequence of i, si, or all separated by whitespaces is expected', 00459 'ocorr_mode': 'output data for correlation mode AUTO_ONLY (ao) or CROSS_ONLY (co) or CROSS_AND_AUTO (ca)', 00460 'compression': 'Flag for turning on data compression', 00461 'asis': 'Creates verbatim copies of the ASDMtables in the ouput measurement set. Value given must be a string of table names separated by spaces; A * wildcard is allowed.', 00462 'wvr_corrected_data': ' Specifies which values are considerd in the SDM binary data to fill the DATA column in the MAIN table of the MS. Expected values for this option are: no, for uncorrected data (default), yes, for the corrected data, and both, for for corrected and uncorrected data. Note if both is selected two measurement sets are created, one with uncorrected data and the other with corrected data. ', 00463 'scans': 'processes only the specified scans. This value is a semicolon separated list of scan specifications. A scan specification consists in an exec bock index followed by the : character; followed by a comma separated list of scan indexes or scan index ranges. A scan index is relative to the exec block it belongs to. Scan indexes are 1-based while exec blocks are 0-based. "0:1" or "2:2~6" or "0:1,1:2~6,8;2:,3:24~30" "1,2" are valid values for the option. "3:" alone will be interpreted as, all the scans of the exec block#3. An scan index or a scan index range not preceded by an exec block index will be interpreted as, all the scans with such indexes in all the exec blocks. By default all the scans are considered. ', 00464 'ignore_time': 'All the rows of the tables Feed, History, Pointing, Source, SysCal, CalDevice, SysPower, and Weather are processed independently of the time range of the selected exec block / scan.', 00465 'process_syspower': ' The SysPower table is processed if and only if this parameter is set to true.', 00466 'process_caldevice': ' The CalDevice table is processed if and only if this parameter is set to true.', 00467 'process_pointing': ' The Pointing table is processed if and only if this parameter is set to true.', 00468 'process_flags': ' Create online flags in the FLAG_CMD sub-table.', 00469 'tbuff': ' Time padding buffer (seconds)', 00470 'applyflags': ' Apply the flags to the MS.', 00471 'savecmds': 'Save flag commands to an ASCII file', 00472 'outfile': 'Name of ASCII file to save flag commands', 00473 'verbose': 'Output lots of information while the filler is working', 00474 'overwrite': 'Over write an existing MS', 00475 'showversion': 'Report the version of asdm2MS being used', 00476 'useversion': 'Version of asdm2MS to be used (\'v3\' (default, should work for all data))', 00477 00478 'async': 'If true the taskname must be started using importasdm(...)' 00479 } 00480 00481 # 00482 # Set subfields defaults if needed 00483 # 00484 00485 if(desc.has_key(key)) : 00486 return desc[key] 00487 00488 def itsdefault(self, paramname) : 00489 a = {} 00490 a['asdm'] = '' 00491 a['vis'] = '' 00492 a['singledish'] = False 00493 a['antenna'] = 0 00494 a['corr_mode'] = 'all' 00495 a['srt'] = 'all' 00496 a['time_sampling'] = 'all' 00497 a['ocorr_mode'] = 'ca' 00498 a['compression'] = False 00499 a['asis'] = '' 00500 a['wvr_corrected_data'] = 'no' 00501 a['scans'] = '' 00502 a['ignore_time'] = False 00503 a['process_syspower'] = True 00504 a['process_caldevice'] = True 00505 a['process_pointing'] = True 00506 a['process_flags'] = True 00507 a['tbuff'] = 0.0 00508 a['applyflags'] = False 00509 a['savecmds'] = True 00510 a['outfile'] = '' 00511 a['verbose'] = False 00512 a['overwrite'] = False 00513 a['showversion'] = False 00514 a['useversion'] = 'v3' 00515 00516 #a = sys._getframe(len(inspect.stack())-1).f_globals 00517 00518 if self.parameters['singledish'] == True: 00519 a['antenna'] = 0 00520 00521 if self.parameters['process_flags'] == True: 00522 a['tbuff'] = 0.0 00523 a['applyflags'] = False 00524 a['savecmds'] = False 00525 a['outfile'] = '' 00526 00527 if a.has_key(paramname) : 00528 return a[paramname] 00529 importasdm_cli = importasdm_cli_()