casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_exportasdm.py
Go to the documentation of this file.
00001 import os
00002 from taskinit import *
00003 
00004 def exportasdm(vis=None, asdm=None, datacolumn=None, archiveid=None, rangeid=None,
00005                subscanduration=None, sbduration=None, apcorrected=None,
00006                verbose=None, showversion=None, useversion=None):
00007         """ Convert a CASA visibility file (MS) into an ALMA Science Data Model.
00008                                           
00009         Keyword arguments:
00010         vis       -- MS name,
00011              default: none
00012 
00013         asdm -- Name of output ASDM file (directory),
00014              default: none; example: asdm='ExecBlock3'
00015 
00016         datacolumn -- specifies which of the MS data columns (DATA,
00017                   CORRECTED_DATA, or MODEL_DATA) should be used as the
00018                   visibilities in the ASDM, default: DATA
00019 
00020         archiveid -- the X0 in uid://X0/X1/X<running>
00021                   default: "S0"
00022 
00023         rangeid -- the X1 in uid://X0/X1/X<running>
00024                   default: "X1"
00025 
00026         subscanduration -- maximum duration of a subscan in the output ASDM
00027                   default: "24h"
00028 
00029         sbduration -- maximum duration of a scheduling block in the output ASDM
00030                   default: "2700s"
00031 
00032         apcorrected -- If true, the data in column datacolumn should be regarded
00033                   as having atmospheric phase correction, default: True
00034 
00035         verbose     -- produce log output, default: True
00036 
00037         showversion -- report the version of the ASDM class set, 
00038                  default: True
00039 
00040         useversion -- Selects the version of MS2asdm to be used (presently only \'v3\' is available)
00041               default: v3
00042 
00043         """
00044         #Python script
00045 
00046         # make ms and tb tool local 
00047         ms = casac.ms()
00048         tb = casac.table()
00049 
00050         try:
00051                 casalog.origin('exportasdm')
00052                 parsummary = 'vis=\"'+str(vis)+'\", asdm=\"'+str(asdm)+'\", datacolumn=\"'+str(datacolumn)+'\",'
00053                 casalog.post(parsummary)
00054                 parsummary = 'archiveid=\"'+str(archiveid)+'\", rangeid=\"'+str(rangeid)+'\", subscanduration=\"'+str(subscanduration)+'\",'
00055                 casalog.post(parsummary)
00056                 parsummary = 'sbduration=\"'+str(sbduration)+'\", apcorrected='+str(apcorrected)+', verbose='+str(verbose)+','
00057                 casalog.post(parsummary)
00058                 parsummary = 'showversion='+str(showversion)+', useversion=\"'+str(useversion)+'\"'
00059                 casalog.post(parsummary)
00060 
00061                 if not (type(vis)==str) or not (os.path.exists(vis)):
00062                         raise Exception, 'Visibility data set not found - please verify the name'
00063                 
00064                 if (asdm == ""):
00065                         raise Exception, "Must provide output data set name in parameter asdm."            
00066                 
00067                 if os.path.exists(asdm):
00068                         raise Exception, "Output ASDM %s already exists - will not overwrite." % asdm
00069 
00070                 # determine sb and subscan duration
00071                 ssdur_secs = 24.*3600 # default is one day, i.e. there will be only one subscan per scan
00072                 if not(subscanduration==""):
00073                         if (qa.canonical(subscanduration)['unit'].find('s') < 0):
00074                                 raise TypeError, "subscanduration is not a valid time quantity: %s" % subscanduration
00075                         else:
00076                                 ssdur_secs = qa.canonical(subscanduration)['value']
00077 
00078                 sbdur_secs = 2700. # default is 45 minutes
00079                 if not(sbduration==""):
00080                         if (qa.canonical(sbduration)['unit'].find('s') < 0):
00081                                 raise TypeError, "sbduration is not a valid time quantity: %s" % sbduration
00082                         else:
00083                                 sbdur_secs = qa.canonical(sbduration)['value']
00084 
00085                 # create timesorted copy of the input ms
00086                 os.system('rm -rf '+vis+'-tsorted')
00087                 ms.open(vis)
00088                 ms.timesort(vis+'-tsorted')
00089                 ms.close()
00090 
00091                 tsortvis = vis+'-tsorted'
00092 
00093                 # Prepare for actual exportasdm
00094                 casalog.post("Checking timesorted MS for potential problems ... ")
00095                 tb.open(tsortvis)
00096                 a = tb.getcol('PROCESSOR_ID')
00097                 a0 = a[0]
00098                 candoit = True
00099                 for i in xrange(0,len(a)-1):
00100                         if(a[i]!=a[i+1]):
00101                                 candoit = False
00102                                 break
00103                 tb.close()
00104 
00105                 if candoit:
00106                         casalog.post("   Checking if PROCESSOR and MAIN need modifications ...")
00107                         tb.open(tsortvis+'/PROCESSOR')
00108                         nprocrows = tb.nrows()
00109                         tb.close()
00110                         if ((nprocrows>0) and (a0>-1)):
00111                                 tb.open(tsortvis+'/PROCESSOR')
00112                                 therow = tb.nrows()-1 
00113                                 mode0 = tb.getcell('MODE_ID',a0)
00114                                 tb.close()
00115                                 offset = 1
00116                                 if nprocrows>1:
00117                                         casalog.post("   Modifying PROCESSOR subtable ...")
00118                                         while (nprocrows>1 and therow>0):
00119                                                 tb.open(tsortvis+'/PROCESSOR', nomodify=False)
00120                                                 therow = tb.nrows()-offset
00121                                                 if(tb.getcell('MODE_ID',therow)!=mode0):
00122                                                         tb.removerows([therow])
00123                                                 else:
00124                                                         offset += 1
00125                                                 nprocrows = tb.nrows()
00126                                         casalog.post("... done.")
00127 
00128                                         casalog.post("   Modifying processor ids in main table ...")
00129                                         a = a - a # set all precessor ids to zero
00130                                         tb.open(tsortvis, nomodify=False)
00131                                         tb.putcol('PROCESSOR_ID', a)
00132                                         tb.close()
00133                                         casalog.post(" ... done.")
00134                                 else:
00135                                         casalog.post("   No modifications to proc id in PROCESSOR and MAIN necessary.")
00136                         casalog.post("   Checking if SYSCAL needs modifications ...")
00137                         if(os.path.exists(tsortvis+'/SYSCAL')):
00138                                 for cname in ['TANT_SPECTRUM',
00139                                               'TSYS_SPECTRUM',
00140                                               'TANT_TSYS_SPECTRUM',
00141                                               'TCAL_SPECTRUM',
00142                                               'TRX_SPECTRUM',
00143                                               'TSKY_SPECTRUM',
00144                                               'PHASE_DIFF_SPECTRUM']:
00145                                         tb.open(tsortvis+'/SYSCAL', nomodify=False)
00146                                         if(cname in tb.colnames()):
00147                                                 cdesc = tb.getcoldesc(cname)
00148                                                 if cdesc.has_key('ndim') and (cdesc['ndim']==-1):
00149                                                         tb.removecols([cname])
00150                                                         casalog.post('   Removed empty array column '+cname+' from table SYSCAL.')
00151                                         tb.close()
00152 
00153                         casalog.post("   Checking if OBSERVATION needs modifications ...")
00154                         tb.open(tsortvis+'/OBSERVATION')
00155                         nobsrows = tb.nrows()
00156                         tb.close()
00157                         if(nobsrows>0):
00158                                 tb.open(tsortvis+'/OBSERVATION', nomodify=False)
00159                                 cdesc = tb.getcoldesc('LOG')
00160                                 if cdesc.has_key('ndim') and (cdesc['ndim']>0):
00161                                         b = tb.getvarcol('LOG')
00162                                         if not (type(b['r1'])==bool):
00163                                                 kys = b.keys()
00164                                                 modified = False
00165                                                 b2 = []
00166                                                 for i in xrange(0,len(kys)):
00167                                                         k = 'r'+str(i+1)
00168                                                         if (b[k][0] == [''])[0]:
00169                                                                 b[k][0] = ["-"]
00170                                                                 modified = True
00171                                                         b2.append([b[k][0][0]])
00172                                                 if modified:
00173                                                         tb.putcol('LOG',b2)
00174                                                         casalog.post("   Modified log column in OBSERVATION table.")
00175                                 tb.close()
00176                         casalog.post("Done.")
00177                 else:
00178                         raise Exception, "More than one processor id in use in the main table. Cannot proceed."             
00179                 
00180                 # generate call to ms2ASDM executable
00181 
00182                 execute_string=  '--datacolumn \"' + datacolumn 
00183                 execute_string+= '\" --archiveid \"' + archiveid + '\" --rangeid \"' + rangeid
00184                 execute_string+= '\" --subscanduration \"' + str(ssdur_secs)
00185                 execute_string+= '\" --schedblockduration \"' + str(sbdur_secs) 
00186                 execute_string+= '\" --logfile \"' + casalog.logfile() +'\"'
00187                 
00188                 if(not apcorrected):
00189                         execute_string+= ' --apuncorrected'
00190                 if(verbose):
00191                         execute_string+= ' --verbose'
00192                 if(showversion):
00193                         execute_string+= ' --revision'
00194 
00195                 theexecutable = 'MS2asdm'
00196                 if (useversion == 'v3'):
00197                         theexecutable = 'MS2asdm'
00198 
00199                 execute_string += ' ' + vis + '-tsorted ' + asdm
00200 
00201                 execute_string = theexecutable+' '+execute_string
00202 
00203                 if(verbose):
00204                         casalog.post('Running '+theexecutable+' standalone invoked as:')
00205                         casalog.post(execute_string)
00206                 else:
00207                         print execute_string
00208 
00209                 rval = os.system(execute_string)
00210 
00211                 os.system('rm -rf '+vis+'-tsorted')
00212 
00213                 if(rval == 0):
00214                         return True
00215                 else:
00216                         casalog.post(theexecutable+' terminated with exit code '+str(rval),'WARN')
00217                         return False
00218         
00219         except Exception, instance:
00220                 casalog.post("Error ...", 'SEVERE')
00221                 raise Exception, instance