casa
$Rev:20696$
|
00001 import os 00002 import sys 00003 import re 00004 from taskinit import * 00005 00006 import asap as sd 00007 from asap import _to_list 00008 from asap.scantable import is_scantable 00009 import pylab as pl 00010 import sdutil 00011 00012 def sdmath(expr, varlist, antenna, fluxunit, telescopeparm, specunit, frame, doppler, scanlist, field, iflist, pollist, outfile, outform, overwrite): 00013 00014 casalog.origin('sdmath') 00015 00016 ### 00017 ### Now the actual task code 00018 ### 00019 try: 00020 worker = sdmath_worker(**locals()) 00021 worker.initialize() 00022 worker.execute() 00023 worker.finalize() 00024 00025 except Exception, instance: 00026 sdutil.process_exception(instance) 00027 raise Exception, instance 00028 00029 class sdmath_worker(sdutil.sdtask_template): 00030 def __init__(self, **kwargs): 00031 super(sdmath_worker,self).__init__(**kwargs) 00032 00033 def initialize(self): 00034 # completely override initialize 00035 00036 # check expr 00037 if self.expr=='': 00038 raise Exception, 'expr is undefined' 00039 00040 # check outfile 00041 sdutil.assert_outfile_canoverwrite_or_nonexistent(self.outfile, 00042 self.outform, 00043 self.overwrite) 00044 00045 # save current insitu parameter and overwrite it 00046 self.insitu_saved = sd.rcParams['insitu'] 00047 if self.is_disk_storage: 00048 sd.rcParams['insitu'] = False 00049 00050 def execute(self): 00051 # insert varlist into expr 00052 varlist = self.varlist 00053 for key in varlist.keys(): 00054 regex = re.compile( key ) 00055 if isinstance( varlist[key], str ): 00056 self.expr = regex.sub( '\"%s\"' % varlist[key], self.expr ) 00057 else: 00058 self.expr = regex.sub( "varlist['%s']" % key, self.expr ) 00059 00060 # default flux unit 00061 fluxunit_now = self.fluxunit 00062 00063 # set filename list 00064 self.__parse() 00065 00066 # selector 00067 sel = self.get_selector() 00068 00069 # actual operation 00070 scanlist = {} 00071 for i in range(len(self.filenames)): 00072 skey='s'+str(i) 00073 isfactor = None 00074 # file type check 00075 if os.path.isdir( self.filenames[i] ): 00076 isfactor = False 00077 else: 00078 f = open( self.filenames[i] ) 00079 line = f.readline().rstrip('\n') 00080 f.close() 00081 del f 00082 try: 00083 isfactor = True 00084 vtmp = float( line[0] ) 00085 del line 00086 except ValueError, e: 00087 isfactor = False 00088 del line 00089 00090 if isfactor: 00091 # variable 00092 f = open( self.filenames[i] ) 00093 lines = f.readlines() 00094 f.close() 00095 del f 00096 for lin in range( len(lines) ): 00097 lines[lin] = lines[lin].rstrip('\n') 00098 lines[lin] = lines[lin].split() 00099 for ljn in range( len(lines[lin]) ): 00100 lines[lin][ljn] = float( lines[lin][ljn] ) 00101 scanlist[skey] = lines 00102 else: 00103 # scantable 00104 thisscan=sd.scantable(self.filenames[i],average=False,antenna=self.antenna) 00105 00106 # Apply the selection 00107 thisscan.set_selection(sel) 00108 if fluxunit_now == '': 00109 fluxunit_now = thisscan.get_fluxunit() 00110 # copy scantable since convert_flux overwrites spectral data 00111 if self.is_disk_storage: 00112 casalog.post('copy data to keep original one') 00113 s = thisscan.copy() 00114 else: 00115 s = thisscan 00116 sdutil.set_fluxunit(s, self.fluxunit, self.telescopeparm, True) 00117 scanlist[skey] = s 00118 00119 #regex=re.compile('[\',\"]') 00120 regex=re.compile('[\',\"]%s[\',\"]' % self.filenames[i]) 00121 #expr=regex.sub('',expr) 00122 self.expr=regex.sub("scanlist['%s']" % skey ,self.expr) 00123 self.expr="tmpout="+self.expr 00124 exec(self.expr) 00125 00126 # set flux unit 00127 if tmpout.get_fluxunit() != fluxunit_now: 00128 tmpout.set_fluxunit(fluxunit_now) 00129 00130 self.scan = tmpout 00131 00132 def save(self): 00133 # avoid to call set_fluxunit 00134 del self.fluxunit 00135 00136 self.set_to_scan() 00137 00138 sdutil.save(self.scan, self.outfile, self.outform, self.overwrite) 00139 00140 def cleanup(self): 00141 # restore insitu parameter 00142 sd.rcParams['insitu'] = self.insitu_saved 00143 00144 def __parse(self): 00145 self.filenames=[] 00146 #p=re.compile(r'[\',\"]\w+[\',\"]') 00147 #p=re.compile(r'[\',\"]\w+[\.,\-,\w+]*[\',\"]') 00148 p=re.compile(r'(?!varlist\[)[\',\"]\w+[\.,\-,/,\w+]*[\',\"](?!\])') 00149 fnames=p.findall(self.expr) 00150 p=re.compile('[\',\"]') 00151 for fname in fnames: 00152 fnamestr=p.sub('',fname) 00153 self.filenames.append(fnamestr) 00154