casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_sdgrid.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 sdgrid(infile, antenna, scanlist, ifno, pollist, gridfunction, convsupport, truncate, gwidth, jwidth, weight, clipminmax, outfile, overwrite, npix, cell, center, plot):
00010 
00011     casalog.origin('sdgrid')
00012     
00013     try:
00014         worker = sdgrid_worker(**locals())
00015         worker.initialize()
00016         worker.execute()
00017         worker.finalize()
00018         
00019     except Exception, instance:
00020         sdutil.process_exception(instance)
00021         raise Exception, instance
00022 
00023 class sdgrid_worker(sdutil.sdtask_interface):
00024     def __init__(self, **kwargs):
00025         super(sdgrid_worker,self).__init__(**kwargs)
00026         self.suffix = '.grid'
00027         self.gridder = None
00028 
00029     def initialize(self):
00030         self.parameter_check()
00031         self.__summarize_raw_inputs()
00032         self.__compile()
00033         self.__summarize_compiled_inputs()
00034 
00035         # create gridder
00036         self.gridder = sd.asapgrid(infile=self.infile)
00037 
00038     def parameter_check(self):
00039         if self.gridfunction.upper() == 'PB':
00040             msg='Sorry. PB gridding is not implemented yet.'
00041             raise Exception, msg
00042         elif self.gridfunction.upper() == 'BOX':
00043             casalog.post('convsupport is automatically set to -1 for BOX gridding.', priority='WARN')
00044             self.convsupport = -1
00045 
00046     def execute(self):
00047         # actual gridding
00048         self.gridder.setPolList(self.pols)
00049         self.gridder.setScanList(self.scans)
00050         if self.ifno >= 0:
00051             self.gridder.setIF(self.ifno)
00052         if self.clipminmax:
00053             self.gridder.enableClip()
00054         else:
00055             self.gridder.disableClip()
00056         self.gridder.setWeight(self.weight) 
00057         self.gridder.defineImage(nx=self.nx, ny=self.ny,
00058                                  cellx=self.cellx, celly=self.celly,
00059                                  center=self.mapcenter)
00060         self.gridder.setFunc(func=self.gridfunction,
00061                              convsupport=self.convsupport,
00062                              truncate=str(self.truncate),
00063                              gwidth=str(self.gwidth),
00064                              jwidth=str(self.jwidth))
00065         self.gridder.grid()
00066 
00067     def finalize(self):
00068         # save result
00069         self.gridder.save(outfile=self.outname)
00070 
00071         # plot result if necessary
00072         if self.plot:
00073             self.gridder.plot()
00074             
00075     def __compile(self):
00076         # infile
00077         if isinstance(self.infile, str):
00078             self.infile = [self.infile]
00079 
00080         # scanlist
00081         self.scans = sdutil._to_list(self.scanlist, int)
00082 
00083         # pollist
00084         self.pols = sdutil._to_list(self.pollist, int)
00085 
00086         # outfile
00087         self.outname = sdutil.get_default_outfile_name(self.infile[0],
00088                                                        self.outfile,
00089                                                        self.suffix)
00090         sdutil.assert_outfile_canoverwrite_or_nonexistent(self.outname,
00091                                                           'ASAP',
00092                                                           self.overwrite)
00093         
00094         # nx and ny
00095         (self.nx, self.ny) = sdutil.get_nx_ny(self.npix)
00096 
00097         # cellx and celly
00098         (self.cellx, self.celly) = sdutil.get_cellx_celly(self.cell)
00099 
00100         # map center
00101         self.mapcenter = sdutil.get_map_center(self.center)
00102 
00103     def __summarize_raw_inputs(self):
00104         params = ['infile', 'antenna', 'scanlist', 'ifno',
00105                   'pollist', 'gridfunction', 'convsupport',
00106                   'truncate', 'gwidth', 'jwidth', 'weight',
00107                   'clipminmax', 'outfile', 'overwrite',
00108                   'npix', 'cell', 'center', 'plot']
00109         summary = self.__generate_summary(header='Input Parameter Summary',
00110                                           params=params)
00111         casalog.post(summary, priority='DEBUG')
00112 
00113     def __summarize_compiled_inputs(self):
00114         params = ['infile', 'ifno', 'scans', 'pols',
00115                   'gridfunction', 'convsupport',
00116                   'truncate', 'gwidth', 'jwidth', 'weight',
00117                   'clipminmax', 'outname', 'overwrite',
00118                   'nx', 'ny', 'cellx', 'celly',
00119                   'mapcenter', 'plot']
00120         summary = self.__generate_summary(header='Grid Parameter Summary',
00121                                           params=params)
00122         casalog.post(summary, priority='DEBUG')
00123         
00124     def __generate_summary(self, header, params):
00125         summary = header+':\n'
00126         for p in params:
00127             summary += '   %12s = %s\n'%(p,getattr(self,p))
00128         return summary
00129 
00130