def automask(image='', maskimage='', fracofpeak=0, rmsthresh=3.0, resolution=None, twopass=False):
image : dirty image or residual to mask
maskimage: name of mask to create. If already existant has to be same shape
fracofpeak : Fraction of peak to use as masking threshold (a number between 0 and 1)
if 0 then rmsthresh or a value based on the rms of the image is used
rmsthresh : Threshold in sigma to use; default is 3 sigma
resolution: if image has no restoring beam ..this value is used for resolution of
twopass: set to True if faint fluffy stuff is wanted else false if a less agressive masking
is needed especially a multi pass masking after some cleaning
stat=iaim.statistics(mask='abs("'+image+'") > 0.0')
resol=qa.quantity(resolution, 'arcsec')
raise TypeError, 'you cannot be serious... less than 10 pixels image'
if(len(rb) ==0 and resolution==None):
raise ValueError, 'No restoring beam or resolution given'
numpix=2*(int(2.0*abs(qa.div(rb['major'], qa.convert(csys.increment('q')['quantity']['*1'],rb['major']['unit']))['value']))/2)
elif(resol['value'] != 0.0):
numpix=2*(int(2.0*abs(qa.div(resol, qa.convert(csys.increment('q')['quantity']['*1'],resol['unit']))['value']))/2)
numpix=iaim.shape()[0]/10;
if((fracofpeak >0) and (fracofpeak < 1)):
rms=stat['max'][0]*fracofpeak
rms=stat['rms'][0]*rmsthresh
ib=iaim.rebin('__rebin.image', [numpix, numpix, 1, 1], overwrite=True)
thresh=5.0*rms/np.sqrt(float(numpix))
thresh=3.0*rms/np.sqrt(float(numpix))
ic=ib.imagecalc(outfile='__thresh.image', pixels='iif(abs("__rebin.image")> '+str(thresh)+',1.0,0.0)', overwrite=True)
ib.remove(done=True, verbose=False)
ie=ic.regrid(outfile='__threshreg.image', shape=shp, csys=csys.torecord(), axes=[0,1], overwrite=True)
ic.remove(done=True, verbose=False)
convpix=str(numpix/2)+'pix' if(twopass) else str(numpix)+'pix'