casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
pixelmask2cleanmask.py
Go to the documentation of this file.
00001 import shutil
00002 from casa import image as ia
00003 from casa import table as tb
00004 
00005 def pixelmask2cleanmask(imagename='',maskname='mask0',maskimage='',usemasked=False):
00006     """
00007     convert pixel(T/F) mask (in a CASA image) to a mask image (1/0)
00008     used for clean
00009     imagename - input imagename that contain a mask to be used
00010     maskname - mask name in the image (default: mask0)
00011     maskimage - output mask image name
00012     usemasked - if True use masked region as a valid region
00013     """
00014     ia.open(imagename)
00015     masks=ia.maskhandler('get')
00016     ia.close()
00017 
00018     inmaskname=''
00019     if type(masks)!=list:
00020         masks=[masks]
00021     for msk in masks:
00022         if maskname == msk:
00023              inmaskname=msk
00024              break
00025     if inmaskname=='':
00026         raise Exception, "mask %s does not exist. Available masks are: %s" % (maskname,masks)
00027 
00028     tb.open(imagename+'/'+maskname)
00029     dat0=tb.getcol('PagedArray')
00030     tb.close()
00031 
00032     #os.system('cp -r %s %s' % (imagename, maskimage))
00033     shutil.copytree(imagename,maskimage)
00034     ia.open(maskimage)
00035     # to unset mask
00036     ia.maskhandler('set',[''])
00037     # make all valid
00038     if (usemasked):
00039         ia.set(1)
00040     else:
00041         ia.set(0) 
00042     ia.close()
00043     #
00044     tb.open(maskimage,nomodify=False)
00045     imd=tb.getcol('map')
00046     # maybe shape check here
00047     #by default use True part of bool mask
00048     masked=1
00049     if (usemasked): masked=0
00050     imd[dat0]=masked
00051     tb.putcol('map',imd)
00052     tb.close()
00053