casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_sdimaging.py
Go to the documentation of this file.
00001 # sd task for imaging
00002 import os
00003 import numpy
00004 import pylab as pl
00005 import asap as sd
00006 from taskinit import *
00007 import sdutil
00008 
00009 def sdimaging(infile, specunit, restfreq, scanlist, field, spw, antenna, stokes, gridfunction, convsupport, truncate, gwidth, jwidth, outfile, overwrite, imsize, cell, dochannelmap, nchan, start, step, phasecenter, ephemsrcname, pointingcolumn):
00010 
00011         casalog.origin('sdimaging')
00012         try:
00013             worker = sdimaging_worker(**locals())
00014             worker.initialize()
00015             worker.execute()
00016             worker.finalize()
00017 
00018         except Exception, instance:
00019             im.close()
00020             sdutil.process_exception(instance)
00021             raise Exception, instance
00022 
00023 class sdimaging_worker(sdutil.sdtask_template_imaging):
00024     def __init__(self, **kwargs):
00025         super(sdimaging_worker,self).__init__(**kwargs)
00026         self.imager_param = sdutil.parameter_registration(self)
00027 
00028     def __del__(self, base=sdutil.sdtask_template_imaging):
00029         super(sdimaging_worker,self).__del__()
00030 
00031     def __register(self, key, attr=None):
00032         self.imager_param.register(key,attr)
00033 
00034     def __get_param(self):
00035         return self.imager_param.get_registered()
00036 
00037     def parameter_check(self):
00038         # outfile check
00039         sdutil.assert_outfile_canoverwrite_or_nonexistent(self.outfile,
00040                                                           'im',
00041                                                           self.overwrite)
00042 
00043     def compile(self):
00044         # imaging mode
00045         spunit = self.specunit.lower()
00046         if spunit == 'channel' or len(spunit) == 0:
00047             self.mode = 'channel'
00048         elif spunit == 'km/s':
00049             self.mode = 'velocity'
00050         else:
00051             self.mode = 'frequency'
00052         if not self.dochannelmap and self.mode != 'channel':
00053             casalog.post('Setting imaging mode as \'channel\'','INFO')
00054             self.mode = 'channel'
00055         self.__register('mode')
00056 
00057         # scanlist
00058 
00059         # field
00060         self.fieldid=-1
00061         sourceid=-1
00062         self.open_table(self.field_table)
00063         field_names = self.table.getcol('NAME')
00064         source_ids = self.table.getcol('SOURCE_ID')
00065         self.close_table()
00066         if type(self.field)==str:
00067             try:
00068                 self.fieldid = field_names.tolist().index(self.field)
00069             except:
00070                 msg = 'field name '+field+' not found in FIELD table'
00071                 raise ValueError, msg
00072         else:
00073             if self.field == -1:
00074                 self.sourceid = source_ids[0]
00075             elif self.field < len(field_names):
00076                 self.fieldid = self.field
00077                 self.sourceid = source_ids[self.field]
00078             else:
00079                 msg = 'field id %s does not exist' % (self.field)
00080                 raise ValueError, msg
00081 
00082         # restfreq
00083         if self.restfreq=='' and self.source_table != '':
00084             self.open_table(self.source_table)
00085             source_ids = self.table.getcol('SOURCE_ID')
00086             for i in range(self.table.nrows()):
00087                 if self.sourceid == source_ids[i] \
00088                        and self.table.iscelldefined('REST_FREQUENCY',i):
00089                     rf = self.table.getcell('REST_FREQUENCY',i)
00090                     if len(rf) > 0:
00091                         self.restfreq=self.table.getcell('REST_FREQUENCY',i)[0]
00092                         break
00093             self.close_table()
00094             casalog.post("restfreq set to %s"%self.restfreq, "INFO")
00095         self.__register('restfreq')
00096             
00097         # 
00098         # spw
00099         self.spwid=-1
00100         self.open_table(self.spw_table)
00101         nrows=self.table.nrows()
00102         if self.spw < nrows:
00103             self.spwid=self.spw
00104         else:
00105             self.close_table()
00106             msg='spw id %s does not exist' % (self.spw)
00107             raise ValueError, msg
00108         self.allchannels=self.table.getcell('NUM_CHAN',self.spwid)
00109         self.close_table()
00110         self.__register('spw','spwid')
00111         
00112         # antenna
00113         if type(self.antenna)==int:
00114             if self.antenna >= 0:
00115                 self.antenna=str(self.antenna)+'&&&'
00116         else:
00117             if (len(self.antenna) != 0) and (self.antenna.find('&') == -1) \
00118                    and (self.antenna.find(';')==-1):
00119                 self.antenna = self.antenna + '&&&'
00120 
00121         # stokes
00122         if self.stokes == '':
00123             self.stokes = 'I'
00124         self.__register('stokes')
00125 
00126         # gridfunction
00127 
00128         # outfile
00129         if os.path.exists(self.outfile) and self.overwrite:
00130             os.system('rm -rf %s'%(self.outfile))
00131 
00132         # imsize
00133         (self.nx,self.ny) = sdutil.get_nx_ny(self.imsize)
00134         self.__register('nx')
00135         self.__register('ny')
00136 
00137         # cell
00138         (self.cellx,self.celly) = sdutil.get_cellx_celly(self.cell,
00139                                                          unit='arcmin')
00140         self.__register('cellx')
00141         self.__register('celly')
00142 
00143         # channel map
00144         if self.dochannelmap:
00145             # start
00146             if self.mode == 'velocity':
00147                 self.startval = ['LSRK', '%s%s'%(self.start,self.specunit)]
00148             elif self.mode == 'frequency':
00149                 self.startval = '%s%s'%(self.start,self.specunit)
00150             else:
00151                 self.startval = self.start
00152 
00153             # step
00154             if self.mode in ['velocity', 'frequency']:
00155                 self.stepval = '%s%s'%(self.step,self.specunit)
00156             else:
00157                 self.stepval = self.step
00158         else:
00159             self.startval = 0
00160             self.stepval = self.allchannels
00161             self.nchan = 1
00162         self.__register('start','startval')
00163         self.__register('step', 'stepval')
00164         self.__register('nchan')
00165                 
00166         # phasecenter
00167         # if empty, it should be determined here...
00168 
00169         self.__register('phasecenter')
00170         self.__register('movingsource', 'ephemsrcname')
00171 
00172     def execute(self):
00173         # imaging
00174         casalog.post("Start imaging...", "INFO")
00175         self.open_imager(self.infile)
00176         self.imager.selectvis(field=self.fieldid, spw=self.spwid, nchan=-1, start=0, step=1, baseline=self.antenna, scan=self.scanlist)
00177         #self.imager.selectvis(vis=infile, field=fieldid, spw=spwid, nchan=-1, start=0, step=1, baseline=antenna, scan=scanlist)
00178         self.imager.defineimage(**self.__get_param())
00179 ##         if self.dochannelmap:
00180 ##             self.imager.defineimage(mode=self.mode, nx=self.nx, ny=self.ny, cellx=self.cellx, celly=self.celly, nchan=self.nchan, start=self.startval, step=self.stepval, restfreq=self.restfreq, phasecenter=self.phasecenter, spw=self.spwid, stokes=self.stokes, movingsource=self.ephemsrcname)
00181 ##         else:
00182 ## ##             if self.mode != 'channel':
00183 ## ##                 casalog.post('Setting imaging mode as \'channel\'','INFO')
00184 ##             self.imager.defineimage(mode='channel', nx=self.nx, ny=self.ny, cellx=self.cellx, celly=self.celly, nchan=1, start=0, step=self.allchannels, phasecenter=self.phasecenter, spw=self.spwid, restfreq=self.restfreq, stokes=self.stokes, movingsource=self.ephemsrcname)
00185         self.imager.setoptions(ftmachine='sd', gridfunction=self.gridfunction)
00186         self.imager.setsdoptions(pointingcolumntouse=self.pointingcolumn, convsupport=self.convsupport, truncate=self.truncate, gwidth=self.gwidth, jwidth=self.jwidth)
00187         self.imager.makeimage(type='singledish', image=self.outfile)
00188         self.close_imager()
00189