casa
$Rev:20696$
|
00001 import os, re 00002 import string 00003 from taskinit import casalog, mstool, qa, tbtool, write_history 00004 from update_spw import join_spws, subtract_spws 00005 00006 def uvcontsub3(vis, fitspw, combine, fitorder, field, spw, 00007 scan, intent, correlation, observation): 00008 """Extract the line(s) of an MS.""" 00009 retval = True 00010 casalog.origin('uvcontsub3') 00011 00012 myms = mstool() 00013 mytb = tbtool() 00014 # This one is redundant - it is already checked at the XML level. 00015 if not ((type(vis) == str) and os.path.isdir(vis)): 00016 casalog.post('Visibility data set not found - please verify the name', 'SEVERE') 00017 return False 00018 00019 outputvis = vis + '.contsub' 00020 if os.path.exists(outputvis): 00021 casalog.post("Output MS " + outputvis + " already exists - will not overwrite.", 'SEVERE') 00022 return False 00023 00024 if combine and combine.lower() != 'spw': 00025 casalog.post("uvcontsub3 deliberately does not support combination by", 00026 'SEVERE') 00027 casalog.post("anything except spw.", 'SEVERE') 00028 return False 00029 00030 # MSStateGram is picky ('CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE' 00031 # doesn't work, but 'CALIBRATE_WVR.REFERENCE,OBSERVE_TARGET_ON_SOURCE' 00032 # does), and I don't want to mess with bison now. A .upper() might be a 00033 # good idea too, but the MS def'n v.2 does not say whether OBS_MODE should 00034 # be case-insensitive. 00035 intent = intent.replace(', ', ',') 00036 00037 if type(spw) == list: 00038 spw = ','.join([str(s) for s in spw]) 00039 elif type(spw) == int: 00040 spw = str(spw) 00041 00042 ## if ':' in spw: 00043 ## casalog.post("uvcontsub3 does not yet support selection by channel for the output", 00044 ## 'SEVERE') 00045 ## casalog.post("Meanwhile, use split to select the desired channels", 'WARN') 00046 ## return False 00047 00048 if ';' in spw: 00049 casalog.post("uvcontsub3 does not yet support writing multiple channel groups per output spw", 00050 'SEVERE') 00051 return False 00052 00053 mytb.open(vis + '/SPECTRAL_WINDOW') 00054 allspw = '0~' + str(mytb.nrows() - 1) 00055 mytb.close() 00056 if 'spw' not in combine: 00057 spwmfitspw = subtract_spws(spw, fitspw) 00058 if spwmfitspw == 'UNKNOWN': 00059 spwmfitspw = subtract_spws(allspw, fitspw) 00060 if spwmfitspw: 00061 raise Exception, "combine must include 'spw' when the fit is being applied to spws outside fitspw." 00062 00063 if type(correlation) == list: 00064 correlation = ', '.join(correlation) 00065 correlation = correlation.upper() 00066 00067 mytb.open(vis, nomodify=True) 00068 if 'CORRECTED_DATA' in mytb.colnames(): 00069 datacolumn = 'CORRECTED_DATA' 00070 else: 00071 # DON'T remind the user that split before uvcontsub wastes time - 00072 # scratch columns will eventually go away. 00073 datacolumn = 'DATA' 00074 mytb.close() 00075 00076 myms.open(vis, nomodify=True) 00077 if not myms.contsub(outputms=outputvis, fitspw=fitspw, 00078 fitorder=fitorder, combine=combine, 00079 spw=spw, unionspw=join_spws(fitspw, spw), 00080 field=field, scan=scan, 00081 intent=intent, correlation=correlation, 00082 obs=str(observation), whichcol=datacolumn): 00083 myms.close() 00084 return False 00085 myms.close() 00086 00087 # Write history to output MS, not the input ms. 00088 try: 00089 param_names = uvcontsub3.func_code.co_varnames[:uvcontsub3.func_code.co_argcount] 00090 param_vals = [eval(p) for p in param_names] 00091 retval &= write_history(myms, outputvis, 'uvcontsub3', param_names, param_vals, 00092 casalog) 00093 except Exception, instance: 00094 casalog.post("*** Error \'%s\' updating HISTORY" % (instance), 00095 'WARN') 00096 00097 # Update FLAG_CMD if necessary. 00098 if ((spw != '') and (spw != '*')): 00099 isopen = False 00100 try: 00101 mytb = tbtool() 00102 mytb.open(outputvis + '/FLAG_CMD', nomodify=False) 00103 isopen = True 00104 #print "is open" 00105 nflgcmds = mytb.nrows() 00106 #print "nflgcmds =", nflgcmds 00107 if nflgcmds > 0: 00108 mademod = False 00109 cmds = mytb.getcol('COMMAND') 00110 for rownum in xrange(nflgcmds): 00111 # Matches a bare number or a string quoted any way. 00112 spwmatch = re.search(r'spw\s*=\s*(\S+)', cmds[rownum]) 00113 if spwmatch: 00114 sch1 = spwmatch.groups()[0] 00115 sch1 = re.sub(r"[\'\"]", '', sch1) # Dequote 00116 # Provide a default in case the uvcontsub3 selection excludes 00117 # cmds[rownum]. update_spwchan() will throw an exception 00118 # in that case. 00119 cmd = '' 00120 try: 00121 #print 'sch1 =', sch1 00122 sch2 = update_spwchan(vis, spw, sch1, truncate=True) 00123 #print 'sch2 =', sch2 00124 ##print 'spwmatch.group() =', spwmatch.group() 00125 if sch2: 00126 repl = '' 00127 if sch2 != '*': 00128 repl = "spw='" + sch2 + "'" 00129 cmd = cmds[rownum].replace(spwmatch.group(), repl) 00130 #except: # cmd[rownum] no longer applies. 00131 except Exception, e: 00132 casalog.post( 00133 "Error %s updating row %d of FLAG_CMD" % (e, 00134 rownum), 00135 'WARN') 00136 casalog.post('sch1 = ' + sch1, 'DEBUG1') 00137 casalog.post('cmd = ' + cmd, 'DEBUG1') 00138 if cmd != cmds[rownum]: 00139 mademod = True 00140 cmds[rownum] = cmd 00141 if mademod: 00142 casalog.post('Updating FLAG_CMD', 'INFO') 00143 mytb.putcol('COMMAND', cmds) 00144 00145 00146 except Exception, instance: 00147 casalog.post("*** Error \'%s\' updating FLAG_CMD" % (instance), 00148 'SEVERE') 00149 retval = False 00150 finally: 00151 if isopen: 00152 casalog.post('Closing FLAG_CMD', 'DEBUG1') 00153 mytb.close() 00154 return retval