casa
$Rev:20696$
|
00001 # 00002 # This file was generated using xslt from its XML file 00003 # 00004 # Copyright 2008, Associated Universities Inc., Washington DC 00005 # 00006 import sys 00007 import os 00008 #from casac import * 00009 import casac 00010 import string 00011 import time 00012 import inspect 00013 import gc 00014 import numpy 00015 from odict import odict 00016 from taskmanager import tm 00017 from task_plotweather import plotweather 00018 class plotweather_cli_: 00019 __name__ = "plotweather" 00020 __async__ = {} 00021 rkey = None 00022 i_am_a_casapy_task = None 00023 # The existence of the i_am_a_casapy_task attribute allows help() 00024 # (and other) to treat casapy tasks as a special case. 00025 00026 def __init__(self) : 00027 self.__bases__ = (plotweather_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'vis':None, 'seasonal_weight':None, 'doPlot':None, 'plotName':None, 'async':None} 00031 00032 00033 def result(self, key=None): 00034 #### here we will scan the task-ids in __async__ 00035 #### and add any that have completed... 00036 if key is not None and self.__async__.has_key(key) and self.__async__[key] is not None: 00037 ret = tm.retrieve(self.__async__[key]) 00038 if ret['state'] == "done" : 00039 self.__async__[key] = None 00040 elif ret['state'] == 'crashed' : 00041 self.__async__[key] = None 00042 return ret 00043 return None 00044 00045 00046 def __call__(self, vis=None, seasonal_weight=None, doPlot=None, plotName=None, async=None): 00047 00048 """Plot elements of the weather table; estimate opacity. 00049 Generates opacity estimates from both the weather data and a seasonal model; intended for VLA use only. 00050 By default the returned opacity is the mean of these predictions, but this can be adjusted with seasonal_weight. 00051 00052 These methods and models are described in detail in EVLA Memo 143, VLA Test Memo 232, VLA Scientific Memo 176, and references therein. 00053 00054 Saves the plot to the following default file: MS name + .plotweather.png 00055 Custom plot filenames must end in one of: .png, .pdf, .ps, .eps or .svg 00056 00057 If run as a function, will return the mean zenith opacity per spectral window 00058 00059 Written by Josh Marvil, revised 02/06/12 00060 Please direct questions/comments/bug reports to: jmarvil@aoc.nrao.edu 00061 00062 00063 example: 00064 myTau = plotweather(vis='myMS.ms',seasonal_weight=0.5, doPlot=True) 00065 00066 """ 00067 if not hasattr(self, "__globals__") or self.__globals__ == None : 00068 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00069 #casac = self.__globals__['casac'] 00070 casalog = self.__globals__['casalog'] 00071 #casalog = casac.casac.logsink() 00072 self.__globals__['__last_task'] = 'plotweather' 00073 self.__globals__['taskname'] = 'plotweather' 00074 ### 00075 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00076 ### 00077 ### 00078 #Handle globals or user over-ride of arguments 00079 # 00080 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00081 useLocalDefaults = False 00082 00083 for item in function_signature_defaults.iteritems(): 00084 key,val = item 00085 keyVal = eval(key) 00086 if (keyVal == None): 00087 #user hasn't set it - use global/default 00088 pass 00089 else: 00090 #user has set it - use over-ride 00091 if (key != 'self') : 00092 useLocalDefaults = True 00093 00094 myparams = {} 00095 if useLocalDefaults : 00096 for item in function_signature_defaults.iteritems(): 00097 key,val = item 00098 keyVal = eval(key) 00099 exec('myparams[key] = keyVal') 00100 self.parameters[key] = keyVal 00101 if (keyVal == None): 00102 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00103 keyVal = eval(key) 00104 if(type(keyVal) == dict) : 00105 if len(keyVal) > 0 : 00106 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00107 else : 00108 exec('myparams[key] = ' + key + ' = {}') 00109 00110 else : 00111 async = self.parameters['async'] 00112 myparams['vis'] = vis = self.parameters['vis'] 00113 myparams['seasonal_weight'] = seasonal_weight = self.parameters['seasonal_weight'] 00114 myparams['doPlot'] = doPlot = self.parameters['doPlot'] 00115 myparams['plotName'] = plotName = self.parameters['plotName'] 00116 00117 00118 result = None 00119 00120 # 00121 # The following is work around to avoid a bug with current python translation 00122 # 00123 mytmp = {} 00124 00125 mytmp['vis'] = vis 00126 mytmp['seasonal_weight'] = seasonal_weight 00127 mytmp['doPlot'] = doPlot 00128 mytmp['plotName'] = plotName 00129 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00130 trec = casac.casac.utils().torecord(pathname+'plotweather.xml') 00131 00132 casalog.origin('plotweather') 00133 try : 00134 #if not trec.has_key('plotweather') or not casac.casac.utils().verify(mytmp, trec['plotweather']) : 00135 #return False 00136 00137 casac.casac.utils().verify(mytmp, trec['plotweather'], True) 00138 scriptstr=[''] 00139 saveinputs = self.__globals__['saveinputs'] 00140 saveinputs('plotweather', 'plotweather.last', myparams, self.__globals__,scriptstr=scriptstr) 00141 if async : 00142 count = 0 00143 keybase = time.strftime("%y%m%d.%H%M%S") 00144 key = keybase + "_" + str(count) 00145 while self.__async__.has_key(key) : 00146 count += 1 00147 key = keybase + "_" + str(count) 00148 result = tm.execute('plotweather', vis, seasonal_weight, doPlot, plotName) 00149 print "Use: " 00150 print " tm.retrieve(return_value) # to retrieve the status" 00151 print 00152 self.rkey = key 00153 self.__async__[key] = result 00154 else : 00155 tname = 'plotweather' 00156 spaces = ' '*(18-len(tname)) 00157 casalog.post('\n##########################################'+ 00158 '\n##### Begin Task: ' + tname + spaces + ' #####') 00159 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00160 result = plotweather(vis, seasonal_weight, doPlot, plotName) 00161 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00162 '\n##########################################') 00163 00164 except Exception, instance: 00165 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00166 raise 00167 else : 00168 #print '**** Error **** ',instance 00169 tname = 'plotweather' 00170 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00171 pass 00172 00173 gc.collect() 00174 return result 00175 # 00176 # 00177 # 00178 def paramgui(self, useGlobals=True, ipython_globals=None): 00179 """ 00180 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00181 """ 00182 import paramgui 00183 if not hasattr(self, "__globals__") or self.__globals__ == None : 00184 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00185 00186 if useGlobals: 00187 if ipython_globals == None: 00188 myf=self.__globals__ 00189 else: 00190 myf=ipython_globals 00191 00192 paramgui.setGlobals(myf) 00193 else: 00194 paramgui.setGlobals({}) 00195 00196 paramgui.runTask('plotweather', myf['_ip']) 00197 paramgui.setGlobals({}) 00198 00199 # 00200 # 00201 # 00202 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00203 if not hasattr(self, "__globals__") or self.__globals__ == None : 00204 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00205 if ipython_globals == None: 00206 myf=self.__globals__ 00207 else: 00208 myf=ipython_globals 00209 00210 a = odict() 00211 a['vis'] = '' 00212 a['seasonal_weight'] = 0.5 00213 a['doPlot'] = True 00214 a['plotName'] = '' 00215 00216 a['async']=False 00217 00218 ### This function sets the default values but also will return the list of 00219 ### parameters or the default value of a given parameter 00220 if(param == None): 00221 myf['__set_default_parameters'](a) 00222 elif(param == 'paramkeys'): 00223 return a.keys() 00224 else: 00225 if(paramvalue==None and subparam==None): 00226 if(a.has_key(param)): 00227 return a[param] 00228 else: 00229 return self.itsdefault(param) 00230 else: 00231 retval=a[param] 00232 if(type(a[param])==dict): 00233 for k in range(len(a[param])): 00234 valornotval='value' 00235 if(a[param][k].has_key('notvalue')): 00236 valornotval='notvalue' 00237 if((a[param][k][valornotval])==paramvalue): 00238 retval=a[param][k].copy() 00239 retval.pop(valornotval) 00240 if(subparam != None): 00241 if(retval.has_key(subparam)): 00242 retval=retval[subparam] 00243 else: 00244 retval=self.itsdefault(subparam) 00245 else: 00246 retval=self.itsdefault(subparam) 00247 return retval 00248 00249 00250 # 00251 # 00252 def check_params(self, param=None, value=None, ipython_globals=None): 00253 if ipython_globals == None: 00254 myf=self.__globals__ 00255 else: 00256 myf=ipython_globals 00257 # print 'param:', param, 'value:', value 00258 try : 00259 if str(type(value)) != "<type 'instance'>" : 00260 value0 = value 00261 value = myf['cu'].expandparam(param, value) 00262 matchtype = False 00263 if(type(value) == numpy.ndarray): 00264 if(type(value) == type(value0)): 00265 myf[param] = value.tolist() 00266 else: 00267 #print 'value:', value, 'value0:', value0 00268 #print 'type(value):', type(value), 'type(value0):', type(value0) 00269 myf[param] = value0 00270 if type(value0) != list : 00271 matchtype = True 00272 else : 00273 myf[param] = value 00274 value = myf['cu'].verifyparam({param:value}) 00275 if matchtype: 00276 value = False 00277 except Exception, instance: 00278 #ignore the exception and just return it unchecked 00279 myf[param] = value 00280 return value 00281 # 00282 # 00283 def description(self, key='plotweather', subkey=None): 00284 desc={'plotweather': 'Plot elements of the weather table; estimate opacity.', 00285 'vis': 'MS name', 00286 'seasonal_weight': 'weight of the seasonal model', 00287 'doPlot': 'set this to True to create a plot', 00288 'plotName': '(Optional) the name of the plot file', 00289 00290 'async': 'If true the taskname must be started using plotweather(...)' 00291 } 00292 00293 if(desc.has_key(key)) : 00294 return desc[key] 00295 00296 def itsdefault(self, paramname) : 00297 a = {} 00298 a['vis'] = '' 00299 a['seasonal_weight'] = 0.5 00300 a['doPlot'] = True 00301 a['plotName'] = '' 00302 00303 #a = sys._getframe(len(inspect.stack())-1).f_globals 00304 00305 if a.has_key(paramname) : 00306 return a[paramname] 00307 plotweather_cli = plotweather_cli_()