casa
$Rev:20696$
|
00001 import os 00002 from taskinit import * 00003 00004 def deconvolve(imagename,model,psf,alg,niter,gain,threshold,mask,scales,sigma,targetflux,prior): 00005 """ deconvolve: Image based deconvolver. The psf provided is 00006 deconvolved out of the image provided. 00007 00008 Keyword arguments: 00009 imagename -- Name of input image to be deconvoled 00010 model -- Name of output to store found model, a.k.a clean components 00011 psf -- Name of psf image to use e.g psf='mypsf.image' . 00012 But if the psf has 3 parameter, then 00013 a gaussian psf is assumed with the values representing 00014 the major , minor and position angle values 00015 e.g psf=['3arcsec', '2.5arcsec', '10deg'] 00016 alg -- algorithm to use clark, hogbom or multiscale or mem. if multiscale 00017 the parameter scale is used to define the number of scales. 00018 niter -- Number of iteration 00019 gain -- CLEAN gain parameter; fraction to remove from peak (< 1.0) 00020 threshold -- deconvolution stopping threshold: if no peak above 00021 found above this level 00022 mask -- mask image (same shape as image and psf) to limit region 00023 where deconvoltion is to occur 00024 ------parameters useful for multiscale only 00025 scales -- parameter needed for multiscale clean. default value [0,3,10] 00026 ------parameters useful for mem only 00027 sigma -- Estimated noise for image 00028 targetflux -- Target total flux in image 00029 prior -- Prior image to guide mem 00030 00031 """ 00032 00033 try: 00034 casalog.origin('deconvolve') 00035 00036 tmppsf='' 00037 tmpImagename='' 00038 if(len(psf)==0): 00039 raise Exception, "****give some psf please****" 00040 if(len(psf)==1): 00041 if (type(psf[0])==str and os.path.exists(psf[0])): 00042 psf=psf[0] 00043 ia.open(psf) 00044 psfaxis=len(ia.shape()) 00045 ia.close() 00046 if(psfaxis<4): 00047 modPsf=_add_axes(psf) 00048 if modPsf is False: 00049 raise Exception, "****problem with input psf image****" 00050 else: 00051 tmppsf='__decon_tmp_psf' 00052 ia.fromimage(tmppsf,modPsf) 00053 ia.close() 00054 psf=tmppsf 00055 ia.open(imagename) 00056 imnaxis=len(ia.shape()) 00057 ia.close() 00058 if(imnaxis<4): 00059 tmpImagename=_add_axes(imagename) 00060 if tmpImagename is False: 00061 raise Exception, "****problem with input diry image****" 00062 else: 00063 imagename=tmpImagename 00064 00065 else: 00066 raise Exception, "****psf file, %s does not exist****" % psf 00067 00068 if(len(psf)==3): 00069 #We've got bmaj bmin bpa 00070 # add axes if the input dirty image does not have four axes 00071 ia.open(imagename) 00072 imnaxis=len(ia.shape()) 00073 #csys=ia.coordsys() 00074 ia.close() 00075 if(imnaxis<4): 00076 tmpImagename=_add_axes(imagename) 00077 if tmpImagename is False: 00078 raise Exception, "****problem with input diry image****" 00079 else: 00080 imagename=tmpImagename 00081 tmppsf=model+'.psf' 00082 dc.open(imagename,psf='', warn=False) 00083 print 00084 dc.makegaussian(tmppsf,bmaj=psf[0],bmin=psf[1], 00085 bpa=psf[2], normalize=False) 00086 dc.close() 00087 psf=tmppsf 00088 dc.open(imagename,psf=psf) 00089 if(alg=='multiscale'): 00090 dc.setscales(scalemethod='uservector', uservector=scales) 00091 if((alg=='hogbom') or (alg=='multiscale')): 00092 dc.clean(algorithm=alg, model=model, niter=niter, gain=gain, 00093 mask=mask, threshold=qa.quantity(threshold, 'mJy')) 00094 elif(alg=='clark'): 00095 dc.clarkclean(niter=niter, model=model, mask=mask, 00096 gain=gain, threshold=qa.quantity(threshold, 'mJy')) 00097 elif(alg=='mem'): 00098 dc.mem(niter=niter, sigma=sigma, targetflux=targetflux, 00099 model=model, prior=prior) 00100 else: 00101 raise Exception, '****algorithm %s is not known****'%alg 00102 dc.restore(model=model, image=model+'.restored') 00103 dc.residual(model=model, image=model+'.residual') 00104 dc.close() 00105 #if(len(tmppsf) != 0): 00106 # os.system('rm -rf '+tmppsf) 00107 if(len(tmpImagename) != 0): 00108 #os.system('rm -rf '+tmpImagename) 00109 ia.removefile(tmpImagename) 00110 tb.clearlocks() 00111 except Exception, instance: 00112 print '*** Error ***',instance 00113 ###lets try to close if we can 00114 try: 00115 tb.clearlocks() 00116 dc.close() 00117 except: pass 00118 00119 00120 # helper function to add degenerate axes 00121 def _add_axes(inImage): 00122 tmpim='' 00123 tmpim2='' 00124 outImage='' 00125 ok=ia.open(inImage) 00126 if ok: 00127 tmpim='__decon_tmp_im' 00128 csys=ia.coordsys() 00129 isStokes=csys.findcoordinate('stokes')[0] 00130 isSpectral=csys.findcoordinate('spectral')[0] 00131 if not isStokes: 00132 ia.open(inImage) 00133 ia.adddegaxes(tmpim, stokes="I", overwrite=True) 00134 ia.close() 00135 if not isSpectral: 00136 tmpim2='__decon_tmp_im2' 00137 ia.open(tmpim) 00138 ia.adddegaxes(tmpim2, spectral=True, overwrite=True) 00139 ia.remove() 00140 outImage=tmpim2 00141 00142 else: 00143 outImage=tmpim 00144 elif not isSpectral: 00145 ia.open(inImage) 00146 ia.adddegaxes(tmpim, spectral=True, overwrite=True) 00147 ia.close() 00148 outImage=tmpim 00149 else: 00150 outImage=inImage 00151 return outImage 00152 else: 00153 return False