casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_pclean.py
Go to the documentation of this file.
00001 from parallel.pimager import pimager
00002 import pdb
00003 import simple_cluster
00004 import string
00005 import commands
00006 from taskinit import *
00007 import shutil
00008 import os
00009 
00010 
00011 def pclean(vis=None,
00012            imagename=None,
00013            imsize=None,
00014            cell=None,
00015            phasecenter=None,
00016            stokes=None,
00017            mask=None,
00018            field=None,
00019            spw=None,
00020            ftmachine=None,
00021            alg=None,
00022            scales=None,
00023            cyclefactor=None,
00024            majorcycles=None,
00025            niter=None,
00026            gain=None,
00027            threshold=None,
00028            weighting=None,
00029            robust=None,
00030            npixels=None,
00031            mode=None,
00032            nterms=None,
00033            start=None,
00034            nchan=None,
00035            width=None,
00036            restfreq=None,
00037            interactive=None,
00038            npercycle=None,
00039            wprojplanes=None,
00040            facets=None,          
00041            overwrite=None,
00042            uvtaper=None, 
00043            outertaper=None,
00044            timerange=None,
00045            uvrange=None, antenna=None, scan=None, observation=None, pbcor=None,
00046            minpb=None,
00047            clusterdef=None):
00048               
00049 
00050     """ Invert and deconvolve images with parallel engines
00051 
00052         Form images from visibilities. Handles continuum and spectral line 
00053         cubes using module pcont and pcube respectively.
00054     """
00055     #Python script    
00056 
00057 ####checking section
00058     if( (mode=='channel') or (mode=='frequency') or (mode=='velocity')):
00059         mode='cube'
00060     if (mode=='cube') and (nchan <2) :
00061         raise ValueError, 'Not going to handle cube with 1 channel; use continuum'
00062     if(mode=='mfs'):
00063         mode='continuum'
00064 
00065     
00066     if((type(cell)==list) and (len(cell)==1)):
00067         cell.append(cell[0])
00068     elif ((type(cell)==str) or (type(cell)==int) or (type(cell)==float)):
00069         cell=[cell, cell]
00070     elif (type(cell) != list):
00071         raise TypeError, "parameter cell %s is not understood" % str(cell)
00072     cellx=qa.quantity(cell[0], 'arcsec')
00073     celly=qa.quantity(cell[1], 'arcsec')
00074     if(cellx['unit']==''):
00075             #string with no units given
00076         cellx['unit']='arcsec'
00077     if(celly['unit']==''):
00078         #string with no units given
00079         celly['unit']='arcsec'
00080     if((type(imsize)==list) and (len(imsize)==1)):
00081         imsize.append(imsize[0])
00082     elif(type(imsize)==int):
00083         imsize=[imsize, imsize]
00084     elif(type(imsize) != list):
00085         raise TypeError, "parameter imsize %s is not understood" % str(imsize)
00086 
00087     if((uvtaper==True) and (type(outertaper) in (str, int, float, long))):
00088         outertaper=[outertaper]
00089     if((uvtaper==True) and (type(outertaper)==list) and (len(outertaper) > 0)):
00090         if(len(outertaper)==1):
00091             outertaper.append(outertaper[0])
00092         if(len(outertaper)==2):
00093             outertaper.append('0deg')
00094         if(qa.quantity(outertaper[0])['unit']==''):
00095             outertaper[0]=qa.quantity(qa.quantity(outertaper[0])['value'],'lambda')
00096         if(qa.quantity(outertaper[1])['unit']==''):
00097             outertaper[1]=qa.quantity(qa.quantity(outertaper[1])['value'],'lambda')
00098     else:
00099         uvtaper=False
00100 
00101     ###make sure units are consistent
00102     cubemode='channel'
00103     if (qa.quantity(start)['unit'].find('Hz') > 0):
00104         cubemode='frequency'
00105         if (qa.quantity(width)['unit'].find('Hz') < 0):
00106             raise TypeError, "start and width parameter should be in the same units" 
00107     if (qa.quantity(start)['unit'].find('m/s') > 0):
00108         cubemode='velocity'
00109         if (qa.quantity(width)['unit'].find('m/s') < 0):
00110             raise TypeError, "start and width parameter should be in the same units" 
00111 
00112     if(type(phasecenter)==str):
00113             ### blank means take field[0]
00114         if (phasecenter==''):
00115             fieldoo=field
00116             if(fieldoo==''):
00117                 fieldoo='0'
00118             phasecenter=int(ms.msseltoindex(vis,field=fieldoo)['field'][0])
00119         else:
00120             tmppc=phasecenter
00121             try:
00122                 if(len(ms.msseltoindex(vis, field=phasecenter)['field']) > 0):
00123                     tmppc = int(ms.msseltoindex(vis,
00124                                                 field=phasecenter)['field'][0])
00125                     ##succesful must be string like '0' or 'NGC*'
00126             except Exception, instance:
00127                     ##failed must be a string 'J2000 18h00m00 10d00m00'
00128                 tmppc = phasecenter
00129             phasecenter = tmppc           
00130     if((clusterdef != '') and os.path.exists(clusterdef)):
00131         cl=simple_cluster.simple_cluster()
00132         if(cl.get_status()==None):
00133             cl.init_cluster(clusterdef)
00134 ####checking done
00135     #I'll assume this machine is representative in memory
00136     arch=os.uname()[0].lower()
00137     totmem=8.0e9
00138     if(arch=='linux'):
00139         totmem=string.atof(commands.getoutput('cat /proc/meminfo | grep -i memtotal').split()[1])*1024.0
00140     elif(arch=='darwin'):
00141         totmem=string.atof(commands.getoutput('sysctl hw.memsize').split()[1])
00142 
00143     sc=simple_cluster.simple_cluster.getCluster()
00144     ###################
00145     #sc.stop_cluster()
00146     #sc.start_cluster()
00147     #######have to do this to get rid of a responsiveness problem on the main CLI
00148     cluster=sc._cluster
00149     #cluster=simple_cluster.simple_cluster.getCluster()._cluster
00150     #pdb.set_trace()
00151     numproc=len(cluster.get_engines())
00152     numprocperhost=len(cluster.get_engines())/len(cluster.get_nodes()) if (len(cluster.get_nodes()) >0 ) else 1
00153 
00154     pim=pimager(cluster)
00155     #pdb.set_trace()
00156 
00157     if spw=='':
00158        spw='*'
00159 
00160     if imagename=='':
00161        imagename=vis
00162 
00163     if mode=='continuum':
00164         if(nterms==1):
00165             pim.pcont(msname=vis, imagename=imagename, 
00166                       imsize=imsize, pixsize=[cellx, celly], 
00167                       phasecenter=phasecenter, 
00168                       field=field, spw=spw, 
00169                       ftmachine=ftmachine, alg=alg, 
00170                       hostnames='', numcpuperhost=-1, 
00171                       majorcycles=majorcycles, niter=niter, npercycle=npercycle, gain=gain,
00172                       threshold=threshold, weight=weighting, robust=robust, scales=scales,
00173                       wprojplanes=wprojplanes,facets=facets,  stokes=stokes,
00174                       contclean=(not overwrite), uvtaper=uvtaper, outertaper=outertaper,
00175                       timerange=timerange,
00176                       uvrange=uvrange, baselines=antenna, scan=scan, observation=scan,
00177                       visinmem=False, maskimage=mask, interactive=interactive, 
00178                       numthreads=1, pbcorr=pbcor, minpb=minpb, savemodel=True)
00179         else:
00180             if(ftmachine != 'ft'):
00181                 raise ValueError, "ftmachine %s is not supported yet with multiterm MFS" % ftmachine
00182             if(alg != 'multiscale'):
00183                 raise ValueError, "Decolvolution %s is not supported with multiterm MFS; use 'multiscale'" % alg
00184             pim.pcontmt(msname=vis, imagename=imagename, 
00185                       imsize=imsize, pixsize=[cellx, celly], 
00186                       phasecenter=phasecenter, 
00187                       field=field, spw=spw, 
00188                       ftmachine=ftmachine, alg=alg,  
00189                       majorcycles=majorcycles, niter=niter, npercycle=npercycle, gain=gain,
00190                       threshold=threshold, weight=weighting, robust=robust, scales=scales,
00191                       wprojplanes=wprojplanes,facets=facets,  stokes=stokes,
00192                       contclean=(not overwrite), uvtaper=uvtaper, outertaper=outertaper,
00193                       timerange=timerange,
00194                       uvrange=uvrange, baselines=antenna, scan=scan, observation=scan,
00195                       visinmem=False, maskimage=mask, interactive=interactive, 
00196                       numthreads=1, pbcorr=pbcor, minpb=minpb, savemodel=True, nterms=nterms)
00197     else:
00198         ##need to calculate chanchunk
00199         memperproc=totmem/float(numprocperhost)/2.0
00200         estmem=18.0*float(imsize[0]*imsize[1])*4
00201         chanchunk=int(memperproc/estmem)
00202         while((chanchunk*numproc) > nchan):
00203             chanchunk=chanchunk-1
00204         if(chanchunk <1):
00205             chanchunk=1
00206         if(interactive and (mask=='')):
00207             mask=imagename+'.mask'
00208         pim.pcube_driver(msname=vis, imagename=imagename, 
00209                   imsize=imsize, pixsize=[cellx, celly], 
00210                   phasecenter=phasecenter, 
00211                   field=field, spw=spw, 
00212                   ftmachine=ftmachine, alg=alg,
00213                   hostnames='', numcpuperhost=-1, cyclefactor=cyclefactor,
00214                   majorcycles=majorcycles, niter=niter, npercycle=npercycle, gain=gain,
00215                   threshold=threshold, weight=weighting, robust=robust, scales=scales,
00216                   mode=cubemode, 
00217                   wprojplanes=wprojplanes,facets=facets, 
00218                   start=start, nchan=nchan, step=width, restfreq=restfreq,stokes=stokes, 
00219                   imagetilevol=1000000, chanchunk=chanchunk, maskimage=mask,  
00220                   uvtaper=uvtaper, outertaper=outertaper,
00221                   timerange=timerange,
00222                   uvrange=uvrange, baselines=antenna, scan=scan, observation=scan,
00223                   contclean=(not overwrite), visinmem=False, numthreads=1, pbcorr=pbcor,
00224                   minpb=minpb, interactive=interactive, savemodel=True)
00225             
00226 #parallel_clean=pclean
00227     
00228