casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
immath.py
Go to the documentation of this file.
00001 #
00002 # This file was generated using xslt from its XML file
00003 #
00004 # Copyright 2009, Associated Universities Inc., Washington DC
00005 #
00006 import sys
00007 import os
00008 from  casac import *
00009 import string
00010 from taskinit import casalog
00011 #from taskmanager import tm
00012 import task_immath
00013 def immath(imagename='', mode='evalexpr', outfile='immath_results.im', expr='IM0', varnames='', sigma='0.0mJy/beam', polithresh='', mask='', region='', box='', chans='', stokes='', stretch=False):
00014 
00015         """Perform math operations on images
00016 
00017     This task evaluates mathematical expressions involving existing
00018     image files. The results of the calculations are stored in the 
00019     designated output file.  Options are available to specify mathematical 
00020     expression directly or pre-defined expression for calculation of 
00021     spectral index image, and polarization intensity and position angle 
00022     images are available. The image file names imbedded in the expression or
00023     specified in the imagename parameter for the pre-defined calculations may
00024     be CASA images or FITS images.
00025 
00026 
00027     NOTE: Index values start at 0 Use the imhead task to see the range of
00028           index values for each axes.
00029     
00030 
00031     Keyword arguments:
00032     imagename  input image name(s)
00033                Default: none;
00034                Examples: mode='evalexpr'; imagename=['image1.im', 'image2.im' ]
00035                The text 'IM0' is replaced by 'image1.im' in the
00036                expression and 'IM1' is repalced with 'image2.im'
00037                mode='spix'; imagename=['image1.im','image2.im'] will calculate 
00038                an image of log(S1/S2)/log(f1/f2), where S1 and S2 are fluxes and 
00039                f1 and f2 are frequencies
00040                mode='pola'; imagename='multistokes.im' (where that image contains both Q and U
00041                stokes planes) or imagename=['imageQ.im','imageU.im'] will calculate 
00042                an image of polarization angle distribution, where imageQ.im and 
00043                imageU.im are Stokes Q and U images, respectively. Calculate 0.5*arctan(U/Q).
00044                mode='poli'; imagename=['imageQ.im','imageU.im','imageV.im'] will calculate
00045                total polarization intensity image, where imageQ.im, imageU.im, imageV.im
00046                are Stokes Q, U, and V images, respectively. Alternatively, with
00047                imagename = ['imageQ.im','imageU.im'] the linear polarization intensity
00048                image will be calculated. In the case where imagename is a single multi-stokes
00049                image, the total polarization image will be calculated if all of the Q, U, and
00050                V stokes planes are present, and the linear polarization intensity image will
00051                be calculated if the Q and U (but not V) planes are present. 
00052 
00053     mode       mode for mathematical operation
00054                Default: evalexpr
00055                Options: 'evalexpr' : evalulate a mathematical expression defined in 'expr' 
00056                         'spix' : spectalindex image 
00057                         'pola' : polarization position angle image 
00058                         'poli' : polarization intesity image 
00059               >>> mode expandable parameters
00060               sigma       (for mode='poli') standard deviation of noise of Stokes images with unit such as
00061                           Jy/beam to correct for bias 
00062                           Default: '0.0Jy/beam' (= no debiasing)
00063               polithresh  (for mode='pola') Quantity (eg '30uJy/beam') describing the linear (not total;
00064                           the stokes V contribution is not included) polarization threshold. A mask ('mask0')
00065                           is written to the output image and is False for all corresponding linear polarization
00066                           values below this threshold. This parameter overrides the mask input parameter
00067                           (below). Default ('') means use the value given in mask, or no masking if that
00068                           value is empty as well.
00069               expr        (for mode='evalexpr') A LEL expression with images.
00070                               Image file names are specified in the imagenames paramter, and
00071                           the variables IM0, IM1, ... (or optionally via the varnames parameter, see below)
00072                           are used to represent these files
00073                           in the expression. Explicit notations of file names in the 
00074                           expression are also supported, in which cases the file names must
00075                           be enclosed in double quotes (") and imagename is ignored.
00076                               Examples:
00077                           Make an image that is image1.im - image2.im
00078                           expr=' (IM0 - IM1 )'
00079                           or with an explicit notation, 
00080                           expr='("image1.im" - "image2.im")'
00081                           Clip an image below a value (0.5 in this case)
00082                           expr = ' iif( IM0 >=0.5, IM0, 0.0) '
00083                           Note: iif (a, b, c)   a is the boolean expression
00084                                                 b is the value if true
00085                                                 c is the value if false
00086                           Take the rms value of two images
00087                           expr = ' sqrt(IM0 * IM0 + IM1 * IM1) '
00088                           Note: No exponentiaion available?
00089                           Build an image pixel by pixel from the minimum of (image2.im, 2*image1.im)
00090                           expr='min(IM1,2*max(IM0))'
00091                varnames   For mode="evalexpr". Instead of the default variable names IM0, IM1, ..., use
00092                           the names in this array to represent the input images.
00093     outfile    The output image. Overwriting an existing outfile is not permitted.
00094                    Default: immath_results.im;  Example: outfile='results.im'
00095     mask       Mask to use. See help par.mask. Default is none. Also see polithresh.
00096     stretch    Stretch the input mask if necessary and possible. See below.
00097     region     Name of region file, region text description, or region dictionary.
00098     box        A rectangular region on the directional plane expressed in pixels.
00099                Example: box='10,10,50,50'
00100     chans      Channel ranges to use, expressed in pixels. Example: chans='3~20'   
00101     stokes     Stokes parameters. Example: stokes='IQUV';
00102                Options: 'I','Q','U','V','RR','RL','LR','LL','XX','YX','XY','YY', ... 
00103                Not used in for cases of mode='poli' or mode='pola'
00104 
00105     Available functions in the expr and mask paramters:
00106     pi(), e(), sin(), sinh(), asinh(), cos(), cosh(), tan(), tanh(),
00107     atan(), exp(), log(), log10(), pow(), sqrt(), complex(), conj()
00108     real(), imag(), abs(), arg(), phase(), aplitude(), min(), max()
00109     round(), isgn(), floor(), ceil(), rebin(), spectralindex(), pa(), 
00110     iif(), indexin(), replace(), ...
00111 
00112     If the mask has fewer dimensions than the image and if the shape
00113     of the dimensions the mask and image have in common are the same,
00114     the mask will automatically have the missing dimensions added so
00115     it conforms to the image.
00116 
00117     For a full description of the allowed syntax see the 
00118     Lattice Expression Language (LEL) documentation on the at:
00119     http://aips2.nrao.edu/docs/notes/223/223.html
00120 
00121     NOTE: where indexing and axis numbering are used in the above
00122     functions they are 1-based, ie. numbering starts at 1.
00123 
00124     If stretch is true and if the number of mask dimensions is less than
00125     or equal to the number of image dimensions and some axes in the
00126     mask are degenerate while the corresponding axes in the image are not,
00127     the mask will be stetched in the degenerate axis dimensions. For example,
00128     if the input image has shape [100, 200, 10] and the input
00129     mask has shape [100, 200, 1] and stretch is true, the mask will be
00130     stretched along the third dimension to shape [100, 200, 10]. However if
00131     the mask is shape [100, 200, 2], stretching is not possible and an
00132     error will result.
00133 
00134     CAUTION: Note that when multiple image are used in the expression, there is
00135     no garauntee about which of those images will be used to create the header
00136     of the output image. Therefore, one may have to modify the output header as
00137     needed if the input headers differ.
00138 
00139     Examples:
00140     # Double all values in an image.
00141     immath( imagesname='myimage.im', expr='IM0*2', outfile='double.im' )
00142     # or with an explicit notation, 
00143     immath( expr='"myimage.im"*2', outfile='double.im' )
00144 
00145     # Taking the sin of an image and adding it to another
00146     # Note that the images need to be the same size
00147     immath(images=['image1.im', 'image2.im'], expr='sin(IM1)+IM0;',outfile='newImage.im')
00148 
00149     # Adding only the plane associated with the 'V' stokes value and
00150     # the 1st channel together in two images
00151     immath(imagename=[image1', 'image2'], expr='IM0+IM1',chans='1',stokes='V')
00152 
00153 
00154     # Selecting a single plane (5th channel), of the 3-D cube and  
00155     # adding it to the original image.  In this example the 2-D plane
00156     # gets expanded out and the values are applied to each plane in the 
00157     # 3-D cube. 
00158     default('immath')
00159     imagename='ngc7538.image'
00160     outfile='chanFive.im'
00161     expr='IM0'
00162     chans='5'
00163     go
00164     default('immath')
00165     imagename=['ngc7538.image', chanFive.im']
00166     outfile='ngc7538_chanFive.im'
00167     expr='IM0+IM1'
00168     go
00169 
00170     # Selecting and saving the inner 3/4 of an image for channels 40,42,44
00171     # as well as channels less than 10
00172     default('immath')
00173     imagename='my_image.im'
00174     expr='IM0'
00175     box='25,25,123,123'
00176     chans='<10;40,42,44'
00177     outfile='my_image_inner.im' )
00178     go
00179 
00180     # Dividing an image by another, making sure we aren't dividing by zero
00181     default('immath')
00182     imagename=['orion.image', 'my.image']
00183     expr='IM0/iif(IM1==0,1.0,IM1)'
00184     outfile='my_orion.image'
00185     go
00186 
00187     # Applying a mask to all of the images in the expression
00188     default('immath')
00189     imagename=['ngc7538.image','ngc7538_clean.image']
00190     expr='(IM0*10)+IM1'
00191     mask='"ngc7538.mask"'
00192     outfile='really_noisy_ngc7538.image'
00193     go
00194 
00195 
00196     # Applying a pixel mask contained in the image information
00197     default('immath')
00198     imagename='ngc5921.image'
00199     expr='IM0*10'
00200     mask='mask("ngc5921.mask")'
00201     outfile='ngc5921.masked.image'
00202     go
00203 
00204     # Creating a total polarization intensity image from an multi-stokes image
00205     # containing IQUV.
00206     default('immath')
00207     outfile='pol_intensity'
00208     stokes=''
00209     # in imagename, you can also specify a list containing single stokes images
00210     # of Q and U (for linear polarization intensity) and V (for total
00211     # polarization intensity)
00212     imagename='3C138_pcal'
00213     mode='poli'
00214     go
00215 
00216     # Creating a polarization position angle image 
00217     default('immath')
00218     outfile='pol_angle.im'
00219     mode='pola'
00220     # you can also do imagename=['Q.im','U.im'] for single stokes images, order of
00221     # the two Stokes images does not matter
00222     imagename='3C138_pcal' # multi-stokes image containing at least Q and U stokes 
00223     go 
00224 
00225     # same as before but write a mask with values of False for pixels for which the
00226     # corresponding linear polarization ( sqrt(Q*Q+U*U)) is less than 30 microJy/beam
00227     polithresh='30uJy/beam'
00228     go
00229 
00230     # Creating a spectral index image from the images at two different observing frequencies
00231     default('immath')
00232     outfile='mySource_sp.im'
00233     mode='spix'
00234     imagename=['mySource_5GHz.im','mySource_8GHz.im']
00235     go
00236     
00237     TEMPORARY IMAGES
00238 
00239     At this time, it is usually necessary for this task to create intermediate, temporary disk images.
00240     The names of these images start with '_immath' and are created in the directory in which the task
00241     is run. The task makes reasonable attempts to remove these images before it exits, but there are
00242     conceivably instances where the temporary images may not be automatically deleted. It is generally
00243     safe to delete them by hand, assuming no immath instance is currently in progress.
00244 
00245     The hope and plan is that the necessity of these images will decrease in the future (ie the computations
00246     will require only RAM and not temporary persistent storage of intermediate results).
00247 
00248  
00249 
00250         """
00251 
00252 #
00253 #    The following is work around to avoid a bug with current python translation
00254 #
00255         mytmp = {}
00256 
00257         mytmp['imagename'] = imagename
00258         mytmp['mode'] = mode
00259         mytmp['outfile'] = outfile
00260         mytmp['expr'] = expr
00261         mytmp['varnames'] = varnames
00262         mytmp['sigma'] = sigma
00263         mytmp['polithresh'] = polithresh
00264         mytmp['mask'] = mask
00265         mytmp['region'] = region
00266         mytmp['box'] = box
00267         mytmp['chans'] = chans
00268         mytmp['stokes'] = stokes
00269         mytmp['stretch'] = stretch
00270         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00271         trec = casac.utils().torecord(pathname+'immath.xml')
00272 
00273         casalog.origin('immath')
00274         if trec.has_key('immath') and casac.utils().verify(mytmp, trec['immath']) :
00275             result = task_immath.immath(imagename, mode, outfile, expr, varnames, sigma, polithresh, mask, region, box, chans, stokes, stretch)
00276 
00277         else :
00278           result = False
00279         return result