casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
makemask.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_makemask
00013 def makemask(mode='list', inpimage='', inpmask='', output='', overwrite=False, inpfreqs=[], outfreqs=[]):
00014 
00015         """Makes and manipulates image masks
00016 
00017 Modes : 
00018 -------------
00019 
00020 copy :  Copy/merge masks and regrid if necessary to a new or existing mask
00021 expand : Expand a mask from one range of freqs to another range
00022 list : list internal masks in inpimage to the log
00023 delete : delete an internal mask from an image (if the deleted mask was a default mask,
00024          the task chooses the first one in the remaining internal mask list (as appears 
00025          in the log when do listing with mode='list') 
00026 setdefaultmask : set a specified internal mask as a defult internal mask 
00027 
00028 
00029 
00030 
00031 In all cases (for output mask is expected), if the output image has a different coordinate system from the
00032 result of input and processing, the mask will be regridded to the output
00033 coordinate system.  
00034 
00035 
00036 Parameter Descriptions and rules:
00037 ------------------------------
00038 inpimage : Name of input image to use as a reference for the output coordinates (if output does not exist). 
00039            Also used as a reference image when regions are specified in inpmask for copy mode
00040            default: none (must specify for list, copy, expand modes)
00041 
00042 Expandable parameters for mode='copy','expand','delete' and 'setdefaultmask':
00043 inpmask : Name(s) of input mask(s)
00044           default: none
00045          To specify an image (zero/non-zero) mask, just give a image name (e.g. myimage1.im)
00046          To specify an internal (T/F) mask, you must give a parent image name and the internal mask name
00047          seperated by a colon. (e.g. myimage1.im:mask0). The internal mask names can be found by running
00048          the makemask task in mode='list'. 
00049  
00050          (expand mode)
00051          'myimage:mask0' : use(true/false) internal mask 
00052          'myimage'  : use the inpimage values to make a mask (zero/non-zero).
00053                       Non-zero values are normalized to one in the process.
00054          (merge mode)
00055          Specify the image mask(s), T/F mask(s), and region(s) to be merged in a list of strings.
00056          The regions can be specified directly in the CASA region format or in the text file(s) contains
00057          the regions. 
00058 
00059          (delete and setdefaultmask mode)
00060          Specify the internal mask with the format, image:mask
00061          
00062  
00063 output : Name of output image. 
00064          default: none
00065          *The resultant mask is written as an image (zero/one) mask if the output is a plain image name
00066          *The resultant mask is written as an internal (T/F) mask if the output name is the form of 'imagename:maskname'
00067           The created mask is set as a default internal mask.
00068          *To re-grid a mask to a different coordinate system,  
00069           give an image with the target coordinate system in inpimage. Or make a copy an imge
00070           with the target coordinate system and specified the name of the copy in output. 
00071 
00072 
00073     - If output is specifield as a plain image, if it exists, it will regrid the mask to 
00074       the new coordinate system  and modify output (if overwrite=True).
00075     - If output is specifield as an image with an internal mask, if the internal mask exists,
00076       it will regrid the mask to the new coordinate system  and modify the internal mask only (if overwrite=True).
00077     - If output does not exist, it will only copy inpimage
00078     - If output == inpimage, do not regrid. Only modify in-place.
00079 
00080 overwrite : overwrite the mask specified in output? (see also the output rules above)
00081             default: False
00082 
00083 Additional expandable parameters for mode='expand':
00084   inpfreqs : input channel/frequency/velocity range
00085              Specify channels in a list of integers. for frequency/velocity, 
00086              a range is specified in a string with '~', e.g. '1.5MHz~1.6MHz'
00087              default: []  - all channels
00088   outfreqs : output channel/frequency/velocity range  
00089              Specify same way as inpfreqs
00090              default: []  - all channels
00091 
00092  
00093 Usage examples : 
00094 ---------------------------
00095 (1) (list mode):
00096      makemask(mode='list', inpimage='mymask.im')
00097      it prints out a list of the internal mask(s) exist in mymask.im to the log 
00098 
00099 (2) (copy mode):
00100      Regrid a boolean mask from one coordinate system to another and save as boolean mask 
00101      in the output image.   
00102 
00103      makemask(mode='copy', inpimage='oldmask.im', inpmask='oldmask.im:mask0', output='newmask.im:mask0')
00104   
00105 (3) (copy mode):
00106      Same as (1), but save as integer mask in the output image.
00107      
00108      makemask(mode='copy', inpimage='oldmask.im', inpmask='oldmask.im:mask0', output='newmask.im')
00109 
00110 
00111 (4) (copy mode):
00112      Convert a boolean(true/false) mask to integer(one/zero) mask in the same image
00113      
00114      makemask(mode='copy', inpimage='oldmask.im', inpmask='oldmask.im:mask0', output='', overwrite=True)
00115 
00116 
00117 (5) (copy mode):
00118      Convert an integer (one/zero) mask to boolean(true/false) mask in the same image
00119      
00120      makemask(mode='copy', inpimage='oldmask.im', inpmask='oldmask.im', output='oldmask.im:mask0')
00121 
00122 
00123 (6) (copy mode):
00124  
00125      Merge a one/zero mask and  T/F masks, using the input coordinate-sys of inpimage and
00126      saving in a new output file. Remember, if the image specified in output already exist and 
00127      has a different coordinate system from inpimage, the mask will be regridded to it.
00128      All masks to be merged are specified in a list in inpmask.
00129      The name of internal masks must be given in the format, 'parent_image_name:internal_mask_name', 
00130      as shown the example below.
00131   
00132      In the example below, image1.im (the 1/0 mask), the internal masks, mask0 from image1.im
00133      and mask1 from image2.im, and a region (on image1.im as defined in inpimage)  are combined. 
00134      The output, newmask.im is a new mask name which has not
00135      yet exist so image specified in inpimage, image1.im's coordinates are used as a target
00136      image coordinates. If image1.im and image2.im has different coordinates, image2.im:mask1 is
00137      regridded before it is combined to the other two masks.
00138 
00139      makemask(mode='copy',
00140               inpimage='image1.im', 
00141               inpmask=['image1.im', image1.im:mask0','image2.mask:mask1', 'circle[[15pix , 15pix] ,8pix ]'],
00142               output='newmask.im);
00143 
00144 (7) (expand mode):
00145      Expand a (one/zero) mask from continuum imaging to use as an input mask image for
00146      spectral line imaging. Use an existing spectral line clean image as a template by
00147      sepecified in inpimage. 
00148      The inpfreqs is left out as it uses a default (=[], means all channels).
00149 
00150      makemask(mode='expand', inpimage='spec.clean.image', inpmask='cont.clean.mask' 
00151               outfreqs=[4,5,6,7], output='spec.clean.mask')
00152 
00153 (8) (expand mode):
00154      Expand a boolean mask from one range of channels to another range 
00155      in the same image.
00156 
00157      makemask(mode='expand', inpimage='oldmask.im', inpmask='oldmask.im:mask0', inpfreqs=[5,6], outfreqs=[4,5,6,7],
00158               output='oldmask.im:mask0', overwrite=True)
00159 
00160 
00161 (9) (expand mode):
00162      Expand a boolean mask from a range of channels in the input image to another range
00163      of channels in a  different image with a different spectral-coordinate system.
00164      Save the mask as ones/zeros so that it can be used as an input mask in the clean task. 
00165 
00166      makemask(mode='expand', inpimage='bigmask.im', inpmask='smallmask.im:mask0', 
00167               inpfreqs='1.5MHz~1.6MHz', outfreqs='1.2MHz~1.8MHz', output='bigmask.im', overwrite=True)
00168 
00169 (10) (delete mode)
00170       Delete an internal mask from an image.
00171 
00172       makemask(mode='delete', inpmask='newmask.im:mask0')
00173 
00174 (11) (setdefaultmask mode)
00175       Set an internal mask as a default internal mask.
00176      
00177       makemask(mode='setdefaultmask', inpmask='newmask.im:mask1')
00178 
00179 
00180 
00181 
00182 
00183   
00184         """
00185 
00186 #
00187 #    The following is work around to avoid a bug with current python translation
00188 #
00189         mytmp = {}
00190 
00191         mytmp['mode'] = mode
00192         mytmp['inpimage'] = inpimage
00193         mytmp['inpmask'] = inpmask
00194         mytmp['output'] = output
00195         mytmp['overwrite'] = overwrite
00196         mytmp['inpfreqs'] = inpfreqs
00197         mytmp['outfreqs'] = outfreqs
00198         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00199         trec = casac.utils().torecord(pathname+'makemask.xml')
00200 
00201         casalog.origin('makemask')
00202         if trec.has_key('makemask') and casac.utils().verify(mytmp, trec['makemask']) :
00203             result = task_makemask.makemask(mode, inpimage, inpmask, output, overwrite, inpfreqs, outfreqs)
00204 
00205         else :
00206           result = False
00207         return result