casa
$Rev:20696$
|
00001 #""" 00002 #Helper functions for the vishead task that might also be useful outside it, 00003 #when working with measurement sets as tables. 00004 #""" 00005 00006 from taskinit import * 00007 import os 00008 00009 def getput_keyw(mode, vis, key, hdindex, hdvalue='', hdref=None): 00010 table = vis + '/' + key[0] 00011 00012 col = key[1] 00013 00014 tb.open(table, nomodify = (mode == 'get')) 00015 colinfo = tb.getcolkeywords(col) 00016 00017 if mode == 'get': 00018 try: 00019 i = int(hdindex) 00020 if i < 0: 00021 # allowed by python, but... 00022 raise Exception, "Illegal index " + str(i) 00023 00024 value = tb.getcell(col, i) # throws exception if index too large 00025 except (ValueError, TypeError): # This is almost certainly from 00026 if(tb.isvarcol(col)): # int('') or int(None). Default 00027 value = tb.getvarcol(col) # to returning the full column. 00028 else: 00029 value = tb.getcol(col) 00030 00031 elif mode == 'put': 00032 if(tb.isvarcol(col)): 00033 tb.close() 00034 raise Exception, "vishead does not yet read/write variably sized columns" 00035 else: 00036 #TODO: Apply colinfo and hdref. 00037 00038 i = None 00039 try: 00040 i = int(hdindex) 00041 except (ValueError, TypeError): 00042 i = None # hdindex is not convertable to an int. 00043 00044 if isinstance(i, int): 00045 # Get full column, change one element, write it back. Not 00046 # efficient but columns used by this task are short 00047 00048 c = tb.getcol(col) 00049 00050 # Must be careful here: 00051 if isinstance(c[0], basestring): 00052 # The new hdvalue may be longer than 00053 # the current string. 00054 # numpy arrays do *not* expand flexibly, 00055 # therefore convert to a python list 00056 c = list(c) 00057 # else: For numerical values, 00058 # the size of the array needs to be unchanged, 00059 # otherwise the following tb.putcol() will fail, 00060 # therefore let c still be a numpy array 00061 00062 c[i] = hdvalue 00063 00064 tb.putcol(col, c) 00065 else: 00066 tb.putcol(col, hdvalue) # hdvalue expected to be an array 00067 00068 value = None 00069 else: 00070 tb.close() 00071 raise Exception, "Assertion error" 00072 00073 #print "Will return", value 00074 00075 tb.close() 00076 return value, colinfo 00077 00078 00079 def keyword_exists(vis, key): 00080 table = vis + '/' + key[0] 00081 col = key[1] 00082 00083 if not os.path.exists(table): 00084 return False 00085 00086 try: 00087 # Throws StandardError if subtable 00088 # does not exist 00089 tb.open(table) 00090 except: 00091 return False 00092 00093 00094 return (col in tb.colnames()) 00095 00096 def dict2direction_strs(raddict, csys='J2000', units=('rad', 'rad')): 00097 """ 00098 Returns a list containing the values of raddict, sorted by the keys, and 00099 converted to directions if possible. 00100 """ 00101 retlist = [] 00102 rkeys = raddict.keys() 00103 rkeys.sort() 00104 for rk in rkeys: 00105 val = raddict[rk] 00106 if hasattr(val, 'flatten'): # So we don't have to do val[0][0][0] 00107 val = val.flatten() # and val[1][0][0] for arrays. 00108 lon = qa.formxxx(qa.toangle('%f%s' % (val[0], units[0])), format='hms') 00109 lat = qa.formxxx(qa.toangle('%f%s' % (val[1], units[1])), format='dms') 00110 retlist.append("%s %s %s" % (csys, lon, lat)) 00111 return retlist 00112 00113 def getrefunits(d, defunits=None): 00114 """ 00115 Given a dictionary d, this tries to extract a reference system and units 00116 from it. Returns some combination of ('UNKNOWN', defunits) on failure. 00117 """ 00118 rsys = 'UNKNOWN' 00119 try: 00120 if d.has_key('MEASINFO'): 00121 rsys = d['MEASINFO'].get('Ref', 'UNKNOWN') 00122 except: 00123 print "d =", d 00124 return rsys, d.get('QuantumUnits', defunits) 00125 00126 def valref2direction_strs(valreftuple): 00127 """ 00128 Splits a (values, ref_desc) pair and passes it on to dict2direction_strs(). 00129 """ 00130 coordsys, angunits = getrefunits(valreftuple[1], ('rad', 'rad')) 00131 return dict2direction_strs(valreftuple[0], csys=coordsys, units=angunits) 00132 00133 def secArray2localDate(secArray, timesys='UTC', timeunit='s'): 00134 """ 00135 Given an array containing a float assumed to be timesys timeunits, returns a 00136 string of it as a local date. 00137 """ 00138 return qa.time({'unit': timeunit, 'value': secArray[0]}, 00139 form=['ymd', 'local'])[0] 00140 00141 def valref2localDate(valreftuple): 00142 """ 00143 Splits a (values, ref_desc) pair and passes it on to secArray2localDate(). 00144 """ 00145 timeref, tunits = getrefunits(valreftuple[1], ['s']) 00146 return secArray2localDate(valreftuple[0], timesys=timeref, timeunit=tunits[0]) 00147 00148 def strip_r1(scheddict): 00149 """ 00150 Given a dictionary with an 'r1' key, remove the r1 layer. 00151 """ 00152 return scheddict.get('r1', scheddict) 00153 00154 def digest(tup): 00155 """ 00156 Given a (val, dict) tuple, returns a string with the boring stuff removed. 00157 """ 00158 t0 = tup[0] 00159 if hasattr(t0, 'shape') and len(t0.shape) < 2: 00160 t0 = list(t0.flatten()) 00161 elif hasattr(t0, 'get'): 00162 t0 = strip_r1(t0) 00163 retval = str(t0) 00164 if len(tup[1].keys()) > 0: 00165 retval += " " + str(tup[1]) 00166 return retval