casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_sdsmooth.py
Go to the documentation of this file.
00001 import os
00002 from taskinit import *
00003 
00004 import asap as sd
00005 from asap._asap import Scantable
00006 from asap import _to_list
00007 from asap.scantable import is_scantable
00008 import sdutil
00009 
00010 def sdsmooth(infile, antenna, scanaverage, scanlist, field, iflist, pollist, kernel, kwidth, chanwidth, verify, outfile, outform, overwrite, plotlevel):
00011 
00012     casalog.origin('sdsmooth')
00013     
00014     try:
00015         worker = sdsmooth_worker(**locals())
00016         worker.initialize()
00017         worker.execute()
00018         worker.finalize()
00019         
00020     except Exception, instance:
00021         sdutil.process_exception(instance)
00022         raise Exception, instance
00023 
00024 class sdsmooth_worker(sdutil.sdtask_template):
00025     def __init__(self, **kwargs):
00026         super(sdsmooth_worker,self).__init__(**kwargs)
00027         self.suffix = '_sm'
00028 
00029     def initialize_scan(self):
00030         sorg=sd.scantable(self.infile,average=False,antenna=self.antenna)
00031         if not (isinstance(sorg,Scantable)):
00032             raise Exception, 'infile=%s is not found' % self.infile
00033 
00034         # A scantable selection
00035         sel = self.get_selector()
00036         sorg.set_selection(sel)
00037 
00038         # Copy scantable when usign disk storage not to modify
00039         # the original table.
00040         if is_scantable(self.infile) and self.is_disk_storage:
00041             self.scan = sorg.copy()
00042         else:
00043             self.scan = sorg
00044         del sorg
00045 
00046     def parameter_check(self):
00047         if self.kernel=='' or self.kernel=='none':
00048             errstr = "kernel need to be specified"
00049             raise Exception, errstr
00050         elif self.kernel!='hanning' and self.kwidth<=0:
00051             errstr = "kernel should be > 0"
00052             raise Exception, errstr
00053             
00054     def execute(self):
00055         engine = sdsmooth_engine(self)
00056         engine.prologue()
00057             
00058         # set various attributes to self.scan
00059         self.set_to_scan()
00060 
00061         #Average within each scan
00062         if self.scanaverage:
00063             self.scan = sdutil.doaverage(self.scan,
00064                                          self.scanaverage,
00065                                          True,
00066                                          'tint',
00067                                          false,
00068                                          'none')
00069 
00070         # Actual implementation is defined outside the class
00071         # since those are used in task_sdreduce.
00072         engine.drive()
00073 
00074         engine.epilogue()
00075 
00076     def save(self):
00077         sdutil.save(self.scan, self.project, self.outform, self.overwrite)
00078 
00079 
00080 class sdsmooth_engine(sdutil.sdtask_engine):
00081     def __init__(self, worker):
00082         super(sdsmooth_engine,self).__init__(worker)
00083 
00084     def prologue(self):
00085         if ( abs(self.plotlevel) > 1 ):
00086             # print summary of input data
00087             casalog.post( "Initial Scantable:" )
00088             #casalog.post( s._summary() )
00089             self.worker.scan._summary()
00090 
00091         if ( abs(self.plotlevel) > 0 ):
00092             pltfile=self.project+'_rawspec.eps'
00093             sdutil.plot_scantable(self.worker.scan, pltfile, self.plotlevel, 'Raw spectra')
00094 
00095     def drive(self):
00096         scan = self.worker.scan
00097         if self.kernel == 'regrid':
00098             if not qa.isquantity(self.chanwidth):
00099                 errstr = "Invalid quantity chanwidth "+self.chanwidth
00100                 raise Exception, errstr
00101             qchw = qa.quantity(self.chanwidth)
00102             oldUnit = scan.get_unit()
00103             if qchw['unit'] in ("", "channel", "pixel"):
00104                 scan.set_unit("channel")
00105             elif qa.compare(self.chanwidth,"1Hz") or \
00106                      qa.compare(self.chanwidth,"1m/s"):
00107                 scan.set_unit(qchw['unit'])
00108             else:
00109                 errstr = "Invalid dimension of quantity chanwidth "+self.chanwidth
00110                 raise Exception, errstr
00111             casalog.post( "Regridding spectra in width "+self.chanwidth )
00112             scan.regrid_channel(width=qchw['value'],plot=self.verify,insitu=True)
00113             scan.set_unit(oldUnit)
00114         else:
00115             casalog.post( "Smoothing spectra with kernel "+self.kernel )
00116             scan.smooth(kernel=self.kernel,width=self.kwidth,plot=self.verify,insitu=True)
00117 
00118     def epilogue(self):
00119         if ( abs(self.plotlevel) > 0 ):
00120             pltfile=self.project+'_smspec.eps'
00121             sdutil.plot_scantable(self.worker.scan, pltfile, self.plotlevel, 'Smoothed spectra')
00122