casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
almahelpers.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # almahelpers.py
00004 #
00005 # History:
00006 #  v1.0 (scorder, gmoellen, jkern; 2012Apr26) == initial version
00007 #
00008 # This script defines a function that takes the Caltable you wish
00009 # to apply to a MeasurementSet and generates a "apply-cal-ready"
00010 # spwmap that provides the appropriate information regarding
00011 # the transfer Tsys calibration from TDM spectral windows to
00012 # FDM spectral windows.  To import this function, type (at
00013 # the CASA prompt):
00014 #
00015 # from recipies.almahelpers import tsysspwmap
00016 #
00017 # and then execute the function:
00018 #
00019 # tsysmap=tsysspwmap(vis='my.ms',tsystable='mytsys.cal')
00020 #
00021 # tsysmap can then be supplied to the applycal spwmap function
00022 #  to ensure proper Tsys calibration application
00023 #
00024 # Type 'help tsysspwmap' for more info.
00025 #
00026 import numpy
00027 import taskinit
00028 
00029 class SpwMap:
00030     """
00031     This object is basically set up to hold the information needed 
00032     """
00033     def __init__(self,calSpwId):
00034         self.calSpwId = calSpwId
00035         self.validFreqRange = []
00036         self.mapsToSpw = []
00037         self.bbNo = None
00038         
00039 class SpwInfo:
00040     def __init__(self,msfile,spwId) :
00041         self.tb = taskinit.tbtool()
00042         self.setTableAndSpwId(msfile,spwId)
00043 
00044     def setTableAndSpwId(self,msfile,spwId) :
00045         self.setTable(msfile)
00046         self.setSpwId(spwId)
00047 
00048     def setTable(self,msfile) :
00049         self.tableName = "%s/SPECTRAL_WINDOW" % msfile
00050         self.tb.open(self.tableName)
00051         self.parameters = self.tb.colnames()
00052         self.tb.close()
00053         
00054     def setSpwId(self,spwId) :
00055         self.tb.open(self.tableName)
00056         self.values = {}
00057         for i in self.parameters :
00058             self.values[i] = self.tb.getcell(i,spwId)
00059         self.tb.close()
00060     
00061 def areIdentical(spwInfo1,spwInfo2) :
00062     if len(spwInfo1.parameters) <= len(spwInfo2.parameters) :
00063         minState = spwInfo1 ; maxState = spwInfo2
00064     else :
00065         minState = spwInfo2 ; maxState = spwInfo1
00066     valueCompare = []
00067     for i in minState.parameters :
00068         compare = (minState.values[i] == maxState.values[i])
00069         if numpy.ndarray not in [type(compare)] :
00070             compare = numpy.array(compare)
00071         if compare.all() : valueCompare.append(True)
00072         else : valueCompare.append(False)
00073     if False in valueCompare : return False
00074     else : return True
00075 
00076 def trimSpwmap(spwMap) :
00077     compare = range(len(spwMap))
00078     evenPoint = compare[-1]
00079     for i in compare :
00080         if compare[i:] == spwMap[i:] :
00081             evenPoint = i
00082             break
00083     return spwMap[:i]
00084         
00085         
00086 def tsysspwmap(vis,tsystable,trim=True,relax=False) :
00087     """
00088     Generate default spwmap for ALMA Tsys, including TDM->FDM associations
00089     Input:
00090      vis        the target MeasurementSet 
00091      tsystable  the input Tsys caltable (w/ TDM Tsys measurements)
00092      trim       if True (the default), return minimum-length spwmap;
00093                     otherwise the spwmap will be exhaustive and include
00094                     the high-numbered (and usually irrelevant) wvr
00095                     spws
00096      relax      (not yet implemented)
00097     Output:
00098      spw list to use in applycal spwmap parameter for the Tsys caltable
00099     
00100     """
00101     localTb = taskinit.tbtool()
00102     spwMaps = []
00103     # Get the spectral windows with entries in the solution table
00104     localTb.open(tsystable)
00105     measuredTsysSpw = numpy.unique(localTb.getcol("SPECTRAL_WINDOW_ID"))
00106     localTb.close()
00107     # Get the frequency ranges for the allowed 
00108     localTb.open("%s/SPECTRAL_WINDOW" % tsystable)
00109     for i in measuredTsysSpw:
00110         spwMap = SpwMap(i)
00111         chanFreqs = localTb.getcell("CHAN_FREQ",i)
00112         chanWidth = abs(chanFreqs[1]-chanFreqs[0])
00113         spwMap.validFreqRange = [chanFreqs.min()-0.5*chanWidth,\
00114                                  chanFreqs.max()+0.5*chanWidth]
00115         spwMaps.append(spwMap)
00116     localTb.close()
00117     # Now loop through the main table's spectral window table
00118     # to map the spectral windows as desired.
00119     localTb.open("%s/SPECTRAL_WINDOW" % vis)
00120     it = localTb.nrows()
00121     localTb.close()
00122     for j in spwMaps :
00123         for i in range(it) :
00124             localTb.open("%s/SPECTRAL_WINDOW" % vis)
00125             chanFreqs = localTb.getcell("CHAN_FREQ",i)
00126             if len(chanFreqs) > 1 :
00127                 chanWidth = localTb.getcell("CHAN_WIDTH",i)[0]
00128                 freqMin = chanFreqs.min()-0.5*chanWidth
00129                 freqMax = chanFreqs.max()+0.5*chanWidth
00130             else :
00131                 chanWidth = localTb.getcell("CHAN_WIDTH",i)
00132                 freqMin = chanFreqs-0.5*chanWidth
00133                 freqMax = chanFreqs+0.5*chanWidth
00134             if freqMin >= j.validFreqRange[0] and \
00135                freqMax <= j.validFreqRange[1] :
00136                   j.mapsToSpw.append(i)
00137             localTb.close()
00138             calSpw = SpwInfo(tsystable,j.calSpwId)
00139             msSpw  = SpwInfo(vis,i)
00140             if areIdentical(calSpw,msSpw) :
00141                 j.bbNo = msSpw.values['BBC_NO']
00142     applyCalSpwMap = []
00143     localTb.open("%s/SPECTRAL_WINDOW" % vis)
00144     for i in range(it) :
00145         useSpw = None
00146         for j in spwMaps :
00147             if i in j.mapsToSpw :
00148                 if useSpw is not None :
00149                     if localTb.getcell("BBC_NO") == j.bbNo :
00150                         useSpw = j.calSpwId
00151                 else :
00152                     useSpw = j.calSpwId
00153         if useSpw == None : useSpw = i
00154         applyCalSpwMap.append(int(useSpw))        
00155     if trim :
00156         return trimSpwmap(applyCalSpwMap)
00157     else :
00158         return applyCalSpwMap
00159             
00160     
00161