casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_imsmooth.py
Go to the documentation of this file.
00001 ########################################################################3
00002 #  task_imsmooth.py
00003 #
00004 # Copyright (C) 2008, 2009
00005 # Associated Universities, Inc. Washington DC, USA.
00006 #
00007 # This script is free software; you can redistribute it and/or modify it
00008 # under the terms of the GNU Library General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or (at your
00010 # option) any later version.
00011 #
00012 # This library is distributed in the hope that it will be useful, but WITHOUT
00013 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015 # License for more details.
00016 #
00017 # You should have received a copy of the GNU Library General Public License
00018 # along with this library; if not, write to the Free Software Foundation,
00019 # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00020 #
00021 # Correspondence concerning AIPS++ should be adressed as follows:
00022 #        Internet email: aips2-request@nrao.edu.
00023 #        Postal address: AIPS++ Project Office
00024 #                        National Radio Astronomy Observatory
00025 #                        520 Edgemont Road
00026 #                        Charlottesville, VA 22903-2475 USA
00027 #
00028 # <summary>
00029 # CASA task for smoothing an image, by doing Forier-based convolution
00030 # on a CASA image file.
00031 # </summary>
00032 #
00033 # <reviewed reviwer="" date="" tests="" demos="">
00034 # </reviewed
00035 #
00036 # <author>
00037 # Shannon Jaeger (University of Calgary)
00038 # </author>
00039 #
00040 # <etymology>
00041 # imsmooth stands for image smoothing
00042 # </etymology>
00043 #
00044 # <synopsis>
00045 # task_imsmooth.py is a Python script providing an easy to use task
00046 # for smoothing an image. 
00047 #
00048 # The convolv2d function of the image tool is used to do the work,
00049 # a description of this function can be found at
00050 # http://casa.nrao.edu/docs/casaref/image.convolve2d.html#x27-270001.1.1
00051 # 
00052 # </synopsis> 
00053 #
00054 # <example>
00055 # <srcblock>
00056 # # The following code snippet find the 1-moments, intensity-weighted
00057 # # coordinate, often used for finding velocity fields.
00058 # imsmooth( imagename='myimage', kernel='gaussian', outfile='myimage.smooth' )
00059 #
00060 #
00061 # </example>
00062 #
00063 # <motivation>
00064 # To provide a user-friendly method to smooth images.
00065 # </motivation>
00066 #
00067 # <todo>
00068 # </todo>
00069 
00070 import os
00071 import numpy
00072 from taskinit import *
00073 
00074 def imsmooth(
00075     imagename, kernel, major, minor, pa, targetres, region,
00076     box, chans, stokes, mask, outfile, stretch, overwrite, beam
00077 ):
00078     casalog.origin( 'imsmooth' )
00079     if (type(beam) == str):
00080         if len(beam) != 0:
00081             err = "beam cannot be a non-empty string"
00082             casalog.post(err, "SEVERE")
00083             raise Exception(err)
00084         beam = {}
00085     retValue = False
00086     # boxcar, tophat and user-defined kernel's are not supported
00087     # yet.
00088 
00089     if ( not ( kernel.startswith( 'gaus' ) or  kernel.startswith( 'box' ) ) ):
00090         casalog.post( 'Our deepest apologies gaussian kernels is the only'
00091                       +' type supported at this time.', 'SEVERE' )
00092         return retValue
00093     
00094 
00095     # First check to see if the output file exists.  If it
00096     # does then we abort.  CASA doesn't allow files to be
00097     # over-written, just a policy.
00098     if ( len( outfile ) < 1 ):
00099         outfile = 'imsmooth_results.im'
00100         casalog.post( "The outfile paramter is empty, consequently the" \
00101                       +" smoothed image will be\nsaved on disk in file, " \
00102                       + outfile, 'WARN')
00103     _myia = iatool()
00104     _myia.open(imagename)
00105     mycsys = _myia.coordsys()
00106     reg = rg.frombcs(
00107         mycsys.torecord(), _myia.shape(), box, chans,
00108         stokes, "a", region
00109     )
00110     _myia.done()
00111     # If the values given are integers we assume they are given in
00112     # arcsecs and alter appropriately
00113     if type( major ) == int:
00114         major=str(major)+'arcsec'
00115     if type( minor ) == int:
00116         minor=str(minor)+'arcsec'                
00117     try:       
00118         if ( kernel.startswith( "gaus" ) ):
00119             # GAUSSIAN KERNEL
00120             casalog.post( "Calling convolve2d with Gaussian kernel", 'NORMAL3' )
00121             _myia.open( imagename )
00122             retia = _myia.convolve2d(
00123                 axes=[0,1], region=reg, major=major,
00124                 minor=minor, pa=pa, outfile=outfile,
00125                 mask=mask, stretch=stretch, targetres=targetres,
00126                 overwrite=overwrite, beam=beam
00127             )
00128             _myia.done()
00129             retia.done()
00130             retValue = True
00131 
00132         elif (kernel.startswith( "box" ) ):
00133             # BOXCAR KERNEL
00134             #
00135             # Until convolve2d supports boxcar we will need to
00136             # use sepconvolve to do this.
00137             #
00138             # BIG NOTE!!!!!
00139             # According to Gaussian2D documentation the default position
00140             # angle aligns the major axis along the y-axis, which typically
00141             # be lat.  So this means that we need to use the major quantity
00142             # on the y axis (or 1) for sepconvolve.
00143 
00144             _myia.open( imagename )
00145             casalog.post( "ia.sepconvolve( axes=[0,1],"+\
00146                           "types=['boxcar','boxcar' ],"+\
00147                           "widths=[ "+str(minor)+", "+str(major)+" ],"+ \
00148                           "region="+str(reg)+",outfile="+outfile+" )",\
00149                           'DEBUG2' )
00150             #retValue = ia.sepconvolve( axes=[0,1], types=['box','box' ],\
00151             #                           widths=[ minor, major ], \
00152             #                           region=reg,outfile=outfile )
00153             retia = _myia.sepconvolve(
00154                 axes=[0,1], types=['box','box' ],
00155                 widths=[ minor, major ],
00156                 region=reg,outfile=outfile,
00157                 mask=mask, stretch=stretch,
00158                 overwrite=overwrite
00159             )
00160             _myia.done()
00161             retia.done()
00162             retValue = True
00163         else:
00164             casalog.post( 'Unrecognized kernel type: ' + kernel, 'SEVERE' )
00165             retValue = False
00166         
00167     except Exception, instance:
00168         _myia.done()
00169         retia.done()
00170         casalog.post("Exception: " + str(instance), 'SEVERE')
00171         raise instance
00172     return retValue