casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
tget.py
Go to the documentation of this file.
00001 import sys
00002 import os
00003 import string
00004 import inspect
00005 import pdb
00006 from odict import odict
00007 
00008 from parameter_check import *
00009 from tasksinfo import *
00010 
00011 def tget(task=None, savefile=''):
00012         """ Get last input values from file on disk for a specified task: 
00013 
00014         task -- Name of task
00015                 default: <unset>; example: task='bandpass'
00016                 <Options: type tasklist() for the complete list>
00017         savefile -- Output file for the task inputs
00018                 default: task.last then task.saved
00019                 example: savefile=task.orion
00020 
00021         """
00022         try:
00023                 a=inspect.stack()
00024                 stacklevel=0
00025                 for k in range(len(a)):
00026                         if (string.find(a[k][1], 'ipython console') > 0):
00027                                 stacklevel=k
00028                                 break
00029                 myf=sys._getframe(stacklevel).f_globals
00030                 if task==None :
00031                         if myf.has_key('task') :
00032                                 task=myf['task']
00033                         else : 
00034                                 if myf.has_key('taskname') : task=myf['taskname']
00035                 myf['taskname']=task
00036                 myf['task']=task
00037                 if type(task)!=str:
00038                         task=task.__name__
00039                         myf['task']=task
00040                         myf['taskname']=task
00041                 parameter_checktype(['task','savefile'],[task,savefile],[str,str])
00042                 parameter_checkmenu('task',task,tasksum.keys())
00043 
00044                 ###Check if task exists by checking if task_defaults is defined
00045                 if ( not myf.has_key(task) and 
00046                      str(type(myf[task])) != "<type 'instance'>" and 
00047                      not hasattr(myf[task],"defaults") ):
00048                           raise TypeError, "task %s is not defined " %task
00049 
00050                 if task==None: task=myf['task']
00051                 myf['task']=task
00052                 #f=zip(myf[taskname].func_code.co_varnames,myf[taskname].func_defaults)
00053                 #for j in range(len(f)):
00054                 #        k=f[j][0]
00055                 #       print 'k is ',k
00056                 #       stst = myf[k]
00057                 #       if ( type(stst) == str ):
00058                 #               sex = k+"='"+stst+"'"
00059                 #       else:
00060                 #               sex = k+"="+str(stst)
00061                 #       exec(sex)
00062                 if savefile=='':
00063                         savefile=task+'.last'
00064                         try:
00065                                 taskparameterfile=open(savefile,'r')
00066                         except:
00067                                 savefile=task+'.saved'
00068                                 try:
00069                                         taskparameterfile=open(savefile,'r')
00070                                 except:
00071                                         print "Sorry - no task.last or .saved"
00072                                         return
00073                 
00074                         taskparameterfile.close()
00075                 execfile(savefile)
00076                 # Put the task parameters back into the global namespace
00077                 f=zip(myf[task].__call__.func_code.co_varnames,myf[task].__call__.func_defaults)
00078                 missing_ks = []
00079                 for j in f:
00080                         k = j[0]
00081                         if k != 'self':
00082                                 try:
00083                                         myf[k] = eval(k)
00084                                 except NameError:
00085                                         missing_ks.append(k)
00086                 if missing_ks:
00087                         print "Did not find a saved value for",
00088                         if len(missing_ks) > 1:
00089                                 print ', '.join(missing_ks[:-1]),
00090                                 print 'or', missing_ks[-1]
00091                         else:
00092                                 print missing_ks[0]
00093                         print "The set of task parameters has probably changed"
00094                         print "since", savefile, "was written."
00095                 print "Restored parameters from file "+savefile
00096         except TypeError, e:
00097                 print "tget --error: ", e
00098 
00099 def tget_defaults(param=None):
00100        a=inspect.stack()
00101        stacklevel=0
00102        for k in range(len(a)):
00103          if (string.find(a[k][1], 'ipython console') > 0):
00104                stacklevel=k
00105        myf=sys._getframe(stacklevel).f_globals
00106        a=odict()
00107        a['task']=''
00108        a['savefile']=''
00109        if(param == None):
00110                myf['__set_default_parameters'](a)
00111        elif(param == 'paramkeys'):
00112                return a.keys()
00113        else:
00114                if(a.has_key(param)):
00115                        return a[param]
00116 
00117 def tget_description(key='listobs',subkey=None):
00118    desc={'listobs': 'List data set summary in the logger:\n',
00119        'task': 'Name of task whose parameters will be set',
00120        'savefile': 'Name of parameter file (e.g., task.last)',
00121        }
00122    if(desc.has_key(key)):
00123            return desc[key]
00124    return ''
00125 
00126 def tget_check_params(param=None, value=None):
00127        if(param=='task'):
00128                if (type(value)!=str):
00129                        return False
00130                else:
00131                       return True 
00132        elif(param=='savefile'):
00133               if ((type(value)==str)):
00134                       return True
00135               else:
00136                      return False
00137        else:
00138                return True
00139