casa
$Rev:20696$
|
00001 # -*- coding: utf-8 -*- 00002 import os 00003 from taskinit import * 00004 import flaghelper as fh 00005 from casac import casac 00006 00007 00008 def importasdm( 00009 asdm=None, 00010 vis=None, 00011 singledish=None, 00012 antenna=None, 00013 corr_mode=None, 00014 srt=None, 00015 time_sampling=None, 00016 ocorr_mode=None, 00017 compression=None, 00018 asis=None, 00019 wvr_corrected_data=None, 00020 scans=None, 00021 ignore_time=None, 00022 process_syspower=None, 00023 process_caldevice=None, 00024 process_pointing=None, 00025 process_flags=None, 00026 tbuff=None, 00027 applyflags=None, 00028 savecmds=None, 00029 outfile=None, 00030 verbose=None, 00031 overwrite=None, 00032 showversion=None, 00033 useversion=None, 00034 ): 00035 """Convert an ALMA Science Data Model observation into a CASA visibility file (MS) or single-dish data format (Scantable). 00036 The conversion of the ALMA SDM archive format into a measurement set. This version 00037 is under development and is geared to handling many spectral windows of different 00038 shapes. 00039 ................ 00040 Keyword arguments: 00041 asdm -- Name of input ASDM file (directory) 00042 default: none; example: asdm='ExecBlock3' 00043 00044 .... vis -- Root ms or scantable name, note a prefix (.ms or .asap) is NOT appended to this name 00045 default: none 00046 00047 .... singledish -- Set True to write data as single-dish format (Scantable) 00048 default: False 00049 00050 >>> singledish expandable parameter 00051 antenna -- antenna name or id. 00052 00053 .... corr_mode -- correlation mode to be considered on input. Could 00054 .... be one or more of the following, ao, co, ac, or all 00055 .... default: all 00056 00057 .... srt -- spectral resolution type. Could be one or more of 00058 .... the following, fr, ca, bw, or all 00059 .... default: all 00060 00061 .... time_sampling -- specifies the time sampling, INTEGRATION and/or 00062 SUBINTEGRATION. could be one or more of the following 00063 i, si, or all. 00064 ........ default: all 00065 00066 .... ocorr_mode -- output data for correlation mode AUTO_ONLY 00067 (ao) or CROSS_ONLY (co) or CROSS_AND_AUTO (ca) 00068 ........ default: ca 00069 ........ 00070 .... compression -- produces comrpressed columns in the resulting measurement set. 00071 default: False 00072 00073 .... asis -- creates verbatim copies of the ASDM tables in 00074 .... the output measurement set. The value given to 00075 ........ this option must be a list of table names separated 00076 ........ by space characters; the wildcard character '*' is 00077 allowed in table names. 00078 00079 .... wvr_corrected_data -- specifies wich values are considered in the 00080 ASDM binary data to fill the DATA column in 00081 the MAIN table of the MS. Expected values for 00082 this option are 'no' for the uncorrected data 00083 (this is the default), 'yes' for the corrected 00084 data and 'both' for corrected and uncorrected 00085 data. In the latter case, two measurement sets 00086 are created, one containing the uncorrected 00087 data and the other one, whose name is suffixed 00088 by '-wvr-corrected', containing the corrected 00089 data. 00090 00091 .... scans --.... processes only the scans specified in the option's value. This value is a semicolon 00092 .... separated list of scan specifications. A scan specification consists in an exec bock index 00093 followed by the character ':' followed by a comma separated list of scan indexes or scan 00094 index ranges. A scan index is relative to the exec block it belongs to. Scan indexes are 00095 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" 00096 are valid values for the option. "3:" alone will be interpreted as 'all the scans of the 00097 exec block#3'. An scan index or a scan index range not preceded by an exec block index will 00098 be interpreted as 'all the scans with such indexes in all the exec blocks'. By default 00099 all the scans are considered. 00100 00101 .... ignore_time -- All the rows of the tables Feed, History, Pointing, Source, SysCal, CalDevice, SysPower, 00102 and Weather are processed independently of the time range of the selected exec block / scan. 00103 00104 .... process_syspower -- The SysPower table is processed if and only if this parameter is set to True. 00105 .... default: True 00106 00107 .... process_caldevice -- The CalDevice table is processed if and only if this parameter is set to True. 00108 .... default: True 00109 00110 .... process_pointing -- The Pointing table is processed if and only if this parameter is set to True. 00111 .... default: True 00112 00113 .... process_flags -- Process the online flggs and save them to the FLAG_CMD sub-table. 00114 .... default: True 00115 00116 >>> process_flags expandable parameter 00117 tbuff -- Time padding buffer (in seconds). 00118 default: 0.0 00119 00120 .... applyflags -- Apply the online flags to the MS. 00121 .... default: False 00122 00123 savecmds -- Save the online flags to an ASCII file. 00124 default: False 00125 00126 outfile -- Filename to save the online flags. 00127 default: '' 00128 00129 .... verbose -- produce log output as asdm2MS is being run. 00130 00131 .... overwrite -- Over write an existing MS. 00132 00133 .... showversion -- report the version of the asdm2MS being used. 00134 00135 .... useversion -- Selects the version of asdm2MS to be used (presently only \'v3\' is available). 00136 default: v3 00137 """ 00138 00139 # Python script 00140 00141 # make agentflagger tool local 00142 aflocal = casac.agentflagger() 00143 00144 try: 00145 casalog.origin('importasdm') 00146 viso = '' 00147 visoc = '' # for the wvr corrected version, if needed 00148 # ----------------------------------------- 00149 # beginning of importasdm_sd implementation 00150 # ----------------------------------------- 00151 if singledish: 00152 theexecutable = 'asdm2ASAP' 00153 # if useversion == 'v2': 00154 # theexecutable = 'oldasdm2ASAP' 00155 if compression: 00156 casalog.post('compression=True has no effect for single-dish format.' 00157 ) 00158 cmd = 'which %s > /dev/null 2>&1' % theexecutable 00159 ret = os.system(cmd) 00160 if ret == 0: 00161 import commands 00162 casalog.post('found %s' % theexecutable) 00163 if showversion: 00164 execute_string = theexecutable + ' --help' 00165 else: 00166 execute_string = theexecutable + ' -asdm ' + asdm 00167 if len(vis) != 0: 00168 execute_string += ' -asap ' + vis.rstrip('/') 00169 execute_string += ' -antenna ' + str(antenna) \ 00170 + ' -apc ' + wvr_corrected_data \ 00171 + ' -time-sampling ' + time_sampling.lower() \ 00172 + ' -overwrite ' + str(overwrite) 00173 if corr_mode == 'all': 00174 execute_string += \ 00175 ' -corr-mode ao,ca -ocorr-mode ao' 00176 else: 00177 execute_string += ' -corr-mode ' \ 00178 + corr_mode.replace(' ', ',') \ 00179 + ' -ocorr-mode ao' 00180 if srt == 'all': 00181 execute_string += ' -srt ' + srt 00182 else: 00183 execute_string += ' -srt ' + srt.replace(' ', 00184 ',') 00185 execute_string += ' -logfile ' + casalog.logfile() 00186 casalog.post('execute_string is') 00187 casalog.post(' ' + execute_string) 00188 ret = os.system(execute_string) 00189 if ret != 0 and not showversion: 00190 casalog.post(theexecutable 00191 + ' terminated with exit code ' 00192 + str(ret), 'SEVERE') 00193 # raise Exception, "ASDM conversion error, please check if it is a valid ASDM and/or useversion='%s' is consistent with input ASDM."%(useversion) 00194 raise Exception, \ 00195 'ASDM conversion error, please check if it is a valid ASDM.' 00196 else: 00197 casalog.post('You have to build ASAP to be able to create single-dish data.' 00198 , 'SEVERE') 00199 # implementation of asis option using tb.fromASDM 00200 if asis != '': 00201 import commands 00202 asdmTables = commands.getoutput('ls %s/*.xml' 00203 % asdm).split('\n') 00204 asdmTabNames = [] 00205 for tab in asdmTables: 00206 asdmTabNames.append(tab.split('/')[-1].rstrip('.xml' 00207 )) 00208 if asis == '*': 00209 targetTables = asdmTables 00210 targetTabNames = asdmTabNames 00211 else: 00212 targetTables = [] 00213 targetTabNames = [] 00214 tmpTabNames = asis.split() 00215 for i in xrange(len(tmpTabNames)): 00216 tab = tmpTabNames[i] 00217 try: 00218 targetTables.append(asdmTables[asdmTabNames.index(tab)]) 00219 targetTabNames.append(tab) 00220 except: 00221 pass 00222 outTabNames = [] 00223 outTables = [] 00224 for tab in targetTabNames: 00225 out = 'ASDM_' + tab.upper() 00226 outTabNames.append(out) 00227 outTables.append(vis + '/' + out) 00228 #tbtool = casac.table() 00229 tb = casac.table() 00230 tb.open(vis, nomodify=False) 00231 wtb = casac.table() 00232 for i in xrange(len(outTables)): 00233 wtb.fromASDM(outTables[i], targetTables[i]) 00234 tb.putkeyword(outTabNames[i], 'Table: %s' 00235 % outTables[i]) 00236 tb.flush() 00237 tb.close() 00238 return 00239 # ----------------------------------- 00240 # end of importasdm_sd implementation 00241 # ----------------------------------- 00242 if len(vis) > 0: 00243 viso = vis 00244 tmps = vis.rstrip('.ms') 00245 if tmps == vis: 00246 visoc = vis + '-wvr-corrected' 00247 else: 00248 visoc = tmps + '-wvr-corrected.ms' 00249 if singledish: 00250 viso = vis.rstrip('/') + '.importasdm.tmp.ms' 00251 else: 00252 viso = asdm + '.ms' 00253 visoc = asdm + '-wvr-corrected.ms' 00254 vis = asdm 00255 if singledish: 00256 viso = asdm.rstrip('/') + '.importasdm.tmp.ms' 00257 vis = asdm.rstrip('/') + '.asap' 00258 00259 useversion = 'v3' 00260 theexecutable = 'asdm2MS' 00261 00262 execute_string = theexecutable + ' --icm "' + corr_mode \ 00263 + '" --isrt "' + srt + '" --its "' + time_sampling \ 00264 + '" --ocm "' + ocorr_mode + '" --wvr-corrected-data "' \ 00265 + wvr_corrected_data + '" --asis "' + asis \ 00266 + '" --logfile "' + casalog.logfile() + '"' 00267 00268 if len(scans) > 0: 00269 execute_string = execute_string + ' --scans ' + scans 00270 if ignore_time: 00271 execute_string = execute_string + ' --ignore-time' 00272 if useversion == 'v3': 00273 if not process_syspower: 00274 execute_string = execute_string + ' --no-syspower' 00275 if not process_caldevice: 00276 execute_string = execute_string + ' --no-caldevice' 00277 if not process_pointing: 00278 execute_string = execute_string + ' --no-pointing' 00279 00280 if compression: 00281 execute_string = execute_string + ' --compression' 00282 if verbose: 00283 execute_string = execute_string + ' --verbose' 00284 if not overwrite and os.path.exists(viso): 00285 raise Exception, \ 00286 'You have specified and existing ms and have indicated you do not wish to overwrite it' 00287 00288 # 00289 # If viso+".flagversions" then process differently depending on the value of overwrite.. 00290 # 00291 dotFlagversion = viso + '.flagversions' 00292 if os.path.exists(dotFlagversion): 00293 if overwrite: 00294 casalog.post("Found '" + dotFlagversion 00295 + "' . It'll be deleted before running the filler." 00296 ) 00297 os.system('rm -rf %s' % dotFlagversion) 00298 else: 00299 casalog.post("Found '%s' but can't overwrite it." 00300 % dotFlagversion) 00301 raise Exception, "Found '%s' but can't overwrite it." \ 00302 % dotFlagversion 00303 00304 execute_string = execute_string + ' ' + asdm + ' ' + viso 00305 00306 if showversion: 00307 casalog.post("You set option \'showversion\' to True. Will just output the version information and then terminate." 00308 , 'WARN') 00309 execute_string = theexecutable + ' --revision' 00310 00311 casalog.post('Running ' + theexecutable 00312 + ' standalone invoked as:') 00313 # print execute_string 00314 casalog.post(execute_string) 00315 exitcode = os.system(execute_string) 00316 if exitcode != 0: 00317 if not showversion: 00318 casalog.post(theexecutable 00319 + ' terminated with exit code ' 00320 + str(exitcode), 'SEVERE') 00321 raise Exception, \ 00322 'ASDM conversion error, please check if it is a valid ASDM and that data/alma/asdm is up-to-date' 00323 if compression: 00324 # viso = viso + '.compressed' 00325 viso = viso.rstrip('.ms') + '.compressed.ms' 00326 visoc = visoc.rstrip('.ms') + '.compressed.ms' 00327 00328 if wvr_corrected_data == 'no' or wvr_corrected_data == 'both': 00329 if os.path.exists(viso): 00330 aflocal.open(viso) 00331 aflocal.saveflagversion('Original', 00332 comment='Original flags at import into CASA', 00333 merge='save') 00334 aflocal.done() 00335 elif wvr_corrected_data == 'yes' or wvr_corrected_data \ 00336 == 'both': 00337 if os.path.exists(visoc): 00338 aflocal.open(visoc) 00339 aflocal.saveflagversion('Original', 00340 comment='Original flags at import into CASA', 00341 merge='save') 00342 aflocal.done() 00343 00344 00345 # Importasdm Flag Parsing 00346 if os.access(asdm + '/Flag.xml', os.F_OK): 00347 # Find Flag.xml 00348 casalog.post('Found Flag.xml in SDM') 00349 00350 # Find Antenna.xml 00351 if os.access(asdm + '/Antenna.xml', os.F_OK): 00352 casalog.post('Found Antenna.xml in SDM') 00353 00354 else: 00355 raise Exception, 'Failed to find Antenna.xml in SDM' 00356 00357 # Find SpectralWindow.xml 00358 if os.access(asdm + '/SpectralWindow.xml', os.F_OK): 00359 casalog.post('Found SpectralWindow.xml in SDM') 00360 00361 else: 00362 raise Exception, \ 00363 'Failed to find SpectralWindow.xml in SDM' 00364 00365 # 00366 # Parse Flag.xml into flag dictionary 00367 # 00368 if process_flags: 00369 flagcmds = fh.readXML(asdm, float(tbuff)) 00370 onlinekeys = flagcmds.keys() 00371 00372 nflags = onlinekeys.__len__() 00373 00374 # Apply flags to the MS 00375 saved_list = {} 00376 if nflags > 0: 00377 if applyflags: 00378 00379 # Open the MS and attach it to the tool 00380 aflocal.open(viso) 00381 00382 # Select the data 00383 aflocal.selectdata() 00384 00385 # Setup the agent's parameters 00386 saved_list = fh.setupAgent(aflocal, flagcmds, [], True, True) 00387 00388 # Initialize the agents 00389 aflocal.init() 00390 00391 # Run the tool 00392 aflocal.run(True, True) 00393 00394 casalog.post('Applied %s flag commands to data'%str(nflags)) 00395 00396 # Destroy the tool and de-attach the MS 00397 aflocal.done() 00398 00399 # Save to FLAG_CMD table. APPLIED is set to True. 00400 fh.writeFlagCmd(viso, flagcmds, onlinekeys, True, '', '') 00401 00402 else: 00403 casalog.post('Will not apply flags (apply_flags=False), use flagcmd to apply') 00404 00405 # Write to FLAG_CMD, APPLIED is set to False 00406 fh.writeFlagCmd(viso, flagcmds, onlinekeys, False, '', '') 00407 00408 # Save the flag cmds to an ASCII file 00409 if savecmds: 00410 # Save to standard filename 00411 if outfile == '': 00412 outfile = viso.replace('.ms','_cmd.txt') 00413 00414 fh.writeFlagCmd(viso, flagcmds, onlinekeys, False, '', outfile) 00415 casalog.post('Saved %s flag commands to %s'%(nflags,outfile)) 00416 00417 else: 00418 casalog.post('There are no flag commands to process') 00419 00420 00421 00422 else: 00423 casalog.post('There is not Flag.xml in ASDM', 'WARN') 00424 00425 00426 00427 except Exception, instance: 00428 00429 print '*** Error ***', instance 00430 00431