casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_wvrgcal.py
Go to the documentation of this file.
00001 import os
00002 import numpy
00003 from taskinit import *
00004 
00005 def wvrgcal(vis=None, caltable=None, toffset=None, segsource=None,
00006             sourceflag=None, tie=None, nsol=None, disperse=None, 
00007             wvrflag=None, statfield=None, statsource=None, smooth=None,
00008             scale=None, reversespw=None,  cont=None):
00009         """
00010         Generate a gain table based on Water Vapour Radiometer data
00011 
00012                                                                                                                                 
00013           vis -- Name of input visibility file                                                                                  
00014                       default: none; example: vis='ngc5921.ms'                                                                  
00015                                                                                                                                 
00016           caltable -- Name of output gain calibration table                                                                     
00017                       default: none; example: caltable='ngc5921.wvr'                                                            
00018                                                                                                                                 
00019           toffset -- Time offset (sec) between interferometric and WVR data                                                     
00020                      default: -1 (ALMA default)                                                                                 
00021                                                                                                                                 
00022           segsource -- Do a new coefficient calculation for each source                                                         
00023                      default: True                                                                                              
00024                                                                                                                                 
00025           tie -- Prioritise tieing the phase of these sources as well as possible                                               
00026                  (requires segsource=True)                                                                                      
00027                      default: [] example: ['3C273,NGC253', 'IC433,3C279']                                                       
00028                                                                                                                                 
00029           sourceflag -- Flag the WVR data for these source(s) as bad and do not produce corrections for it                      
00030                        (requires segsource=True)                                                                                
00031                        default: [] (none) example: ['3C273']                                                                    
00032                                                                                                                                 
00033           nsol -- Number of solutions for phase correction coefficients during this observation.                                
00034                   By default only one set of coefficients is generated for the entire observation.                              
00035                   If more sets are requested, then they will be evenly distributed in time throughout                           
00036                   the observation. Values > 1 require segsource=False.                                                          
00037                      default: 1                                                                                                 
00038                                                                                                                                 
00039           disperse -- Apply correction for dispersion                                                                           
00040                      default: False                                                                                             
00041                                                                                                                                 
00042           wvrflag -- Flag the WVR data for these antenna(s) as bad and replace its data with interpolated values                
00043                        default: [] (none) example: ['DV03','DA05','PM02']                                                       
00044                                                                                                                                 
00045           statfield -- Compute the statistics (Phase RMS, Disc) on this field only                                              
00046                        default: '' (all)                                                                                        
00047                                                                                                                                 
00048           statsource -- Compute the statistics (Phase RMS, Disc) on this source only                                            
00049                        default: '' (all)                                                                                        
00050                                                                                                                                 
00051           smooth -- Smooth WVR data by this many samples before applying the correction                                         
00052                      default: 1 (no smoothing) example: 3                                                                       
00053                                                                                                                                 
00054           scale -- Scale the entire phase correction by this factor                                                             
00055                      default: 1. (no scaling)                                                                                   
00056                                                                                                                                 
00057           reversespw -- Reverse the sign of the correction for the listed SPWs                                                  
00058                         (only neede for early ALMA data before Cycle 0)                                                         
00059                      default: '' (none), example: reversespw='0~2,4'; spectral windows 0,1,2,4                                  
00060                                                                                                                                 
00061           cont -- Estimate the continuum (e.g., due to clouds)                                                                  
00062                      default: False
00063         """
00064         #Python script
00065 
00066         # make ms tool local 
00067         mst = casac.ms()
00068 
00069         try:
00070                 casalog.origin('wvrgcal')
00071 
00072                 # compile parameter summary
00073                 
00074                 parsummary = 'vis=\"'+str(vis)+'\", caltable=\"'+str(caltable)+'\", toffset='+str(toffset)+','
00075                 casalog.post(parsummary)
00076                 parsummary = 'nsol='+str(nsol)+', segsource='+str(segsource)+', reversespw=\"'+str(reversespw)+'\",'
00077                 casalog.post(parsummary)
00078                 parsummary = 'disperse='+str(disperse)+', cont='+str(cont)+', wvrflag=\"'+str(wvrflag)+'\",'
00079                 casalog.post(parsummary)
00080                 parsummary = 'sourceflag=\"'+str(sourceflag)+'\", statfield='+str(statfield)+', statsource=\"'+str(statsource)+'\",'
00081                 casalog.post(parsummary)
00082                 parsummary = 'tie=\"'+str(tie)+'\", smooth='+str(smooth)+', scale='+str(scale)
00083                 casalog.post(parsummary)
00084 
00085                 if not (type(vis)==str) or not (os.path.exists(vis)):
00086                         raise Exception, 'Visibility data set not found - please verify the name'
00087                 
00088                 if (caltable == ""):
00089                         raise Exception, "Must provide output caltable name in parameter gaintable."            
00090                 
00091                 if os.path.exists(caltable):
00092                         raise Exception, "Output gaintable %s already exists - will not overwrite." % caltable
00093 
00094                 execute_string=  '--ms ' + vis 
00095                 execute_string+= ' --output ' + caltable + ' --toffset ' + str(toffset)
00096 
00097                 if nsol>1:
00098                         if not segsource:
00099                                 execute_string+= ' --nsol ' + str(nsol)
00100                         else:
00101                                 raise Exception, "In order to use nsol>1, segsource must be set to False." % caltable
00102                                 
00103                 if segsource:
00104                         execute_string+= ' --segsource'
00105 
00106                 if segsource and (len(sourceflag)>0):
00107                         for src in sourceflag:
00108                                 if not (type(src)==int or type(src)==str):
00109                                         raise Exception, "List elements of parameter sourceflag must be int or string."
00110                                 if (src != ''):
00111                                         execute_string += ' --sourceflag \"'+str(src)+'\"'
00112 
00113                 if segsource and (len(tie)>0):
00114                         for i in xrange(0,len(tie)):
00115                                 src = tie[i]
00116                                 if not (type(src)==str):
00117                                         raise Exception, "List elements of parameter tie must be strings."
00118                                 if (src != ''):
00119                                         execute_string += ' --tie '
00120                                         execute_string += '\"'+str(src)+'\"'
00121                                         if not (i==len(tie)-1):
00122                                                 execute_string += ' '
00123 
00124                 if (not reversespw==''):
00125                         spws = mst.msseltoindex(vis=vis,spw=reversespw)['spw']
00126                         for id in spws:
00127                                 execute_string += ' --reversespw '+str(id)
00128 
00129                 if disperse:
00130                         dispdirpath = os.getenv('WVRGCAL_DISPDIR', '')
00131                         if not os.path.exists(dispdirpath+'/libair-ddefault.csv'):
00132                                 path1 = dispdirpath
00133                                 dispdirpath = os.getenv("CASAPATH").split(' ')[0] + "/data/alma/wvrgcal"
00134                                 if not os.path.exists(dispdirpath+'/libair-ddefault.csv'):
00135                                         raise Exception, "Dispersion table libair-ddefault.csv not found in path "\
00136                                               +"given by WVRGCAL_DISPDIR nor in \""+dispdirpath+"\""
00137                                 
00138                                 os.putenv('WVRGCAL_DISPDIR', dispdirpath)
00139                                 
00140                         execute_string+= ' --disperse'
00141                         casalog.post('Using dispersion table '+dispdirpath+'/libair-ddefault.csv')
00142 
00143                 if cont:
00144                         if not segsource:
00145                                 execute_string+= ' --cont'
00146                         else:
00147                                 raise Exception, "cont and segsource are not permitted to be True at the same time."
00148 
00149                 if (len(wvrflag)>0):
00150                         for ant in wvrflag:
00151                                 if not (type(ant)==int or type(ant)==str):
00152                                         raise Exception, "List elements of parameter wvrflag must be int or string."
00153                                 if (ant != ''):
00154                                         execute_string += ' --wvrflag \"'+str(ant)+'\"'
00155 
00156                 if not (statfield==None or statfield=="") and type(statfield)==str:
00157                         execute_string += ' --statfield \"'+ statfield + '\"'
00158 
00159                 if not (statsource==None or statsource=="") and type(statsource)==str:
00160                         execute_string += ' --statsource \"'+ statsource + '\"'
00161 
00162                 if (smooth > 1):
00163                         execute_string+= ' --smooth ' + str(smooth)
00164 
00165                 if (scale != 1.):
00166                         execute_string+= ' --scale ' + str(scale)
00167                 
00168                 theexecutable = 'wvrgcal'
00169 
00170                 execute_string = theexecutable+' '+execute_string
00171 
00172                 casalog.post('Running '+theexecutable+' standalone invoked as:')
00173                 casalog.post(execute_string)
00174                 print execute_string
00175 
00176                 templogfile = 'wvrgcal_tmp_'+str(numpy.random.randint(1E6,1E8))
00177 
00178                 rval = os.system(execute_string + " > "+ templogfile)
00179 
00180                 fp = file(templogfile)
00181                 loglines = fp.readlines()
00182                 fp.close()
00183                 for ll in loglines:
00184                         casalog.post(ll.expandtabs())
00185                 os.system('rm -rf '+templogfile)
00186 
00187                 if(rval == 0):
00188                         return True
00189                 else:
00190                         if(rval < 32512):
00191                                 casalog.post(theexecutable+' terminated with exit code '+str(rval),'WARN')
00192                                 return False
00193                         else:
00194                                 raise Exception, "wvrgcal executable not available."
00195         
00196         except Exception, instance:
00197                 print '*** Error *** ',instance
00198                 raise Exception, instance