Module ObitTask
[hide private]
[frames] | no frames]

Source Code for Module ObitTask

  1  # Copyright (C) 2005 Joint Institute for VLBI in Europe 
  2  # Copyright (C) 2005 Associated Universities, Inc. Washington DC, USA. 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13   
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 17   
 18  """ 
 19   
 20  This module provides the ObitTask class.  It adapts the Task class from 
 21  the Task module to be able to run Obit tasks: 
 22   
 23  >>> imean = ObitTask('Template') 
 24   
 25  The resulting class instance has all associated adverbs as attributes: 
 26   
 27  >>> print imean.ind 
 28  0.0 
 29  >>> imean.ind = 1 
 30  >>> print imean.indisk 
 31  1.0 
 32  >>> imean.indi = 2.0 
 33  >>> print imean.ind 
 34  2.0 
 35   
 36  It also knows the range for these attributes: 
 37   
 38  >>> imean.ind = -1 
 39  Traceback (most recent call last): 
 40    ... 
 41  ValueError: value '-1.0' is out of range for attribute 'indisk' 
 42  >>> imean.ind = 10.0 
 43  Traceback (most recent call last): 
 44    ... 
 45  ValueError: value '10.0' is out of range for attribute 'indisk' 
 46   
 47  >>> imean.inc = 'UVDATA' 
 48   
 49  >>> print imean.inclass 
 50  UVDATA 
 51   
 52  """ 
 53   
 54  # Global AIPS defaults. 
 55  import AIPS 
 56   
 57  # AIPSTask implementation. 
 58  from AIPSTask import AIPSTask 
 59  from FITS import FITS 
 60   
 61  # Generic Task implementation. 
 62  from Task import Task, List 
 63   
 64  # Generic Python stuff. 
 65  import glob, os, pickle, sys 
 66   
67 -class ObitTask(AIPSTask):
68 69 """This class implements running Obit tasks.""" 70 71 # Package. 72 _package = 'Obit' 73 74 # List of adverbs referring to disks. 75 _disk_adverbs = ['inDisk', 'in2Disk', 'outDisk', 'out2Disk'] 76 77 # Default version. 78 version = 'OBIT' 79 80 # Default user number. 81 userno = -1 82 83 # Debugging? 84 debug = False 85
86 - def __init__(self, name):
87 AIPSTask.__init__(self, name) 88 return
89
90 - def spawn(self):
91 """Spawn the task.""" 92 93 if self.userno == 0: 94 raise RuntimeError, "AIPS user number is not set" 95 96 input_dict = {} 97 for adverb in self._input_list: 98 input_dict[adverb] = self._retype(self.__dict__[adverb]) 99 100 # Debugging? 101 input_dict["DEBUG"] = self.debug 102 103 # Figure out what proxy to use for running the task, and 104 # translate the related disk numbers. 105 url = None 106 proxy = None 107 for adverb in self._disk_adverbs: 108 if adverb in input_dict: 109 disk = int(input_dict[adverb]) 110 if disk == 0: 111 continue 112 if not url and not proxy: 113 if self.__dict__['DataType'] == 'AIPS': 114 url = AIPS.disks[disk].url 115 proxy = AIPS.disks[disk].proxy() 116 if AIPS.disks[disk].url != url: 117 raise RuntimeError, \ 118 "AIPS disks are not on the same machine" 119 input_dict[adverb] = int(AIPS.disks[disk].disk) 120 elif self.__dict__['DataType'] == 'FITS': 121 url = FITS.disks[disk].url 122 proxy = FITS.disks[disk].proxy() 123 if FITS.disks[disk].url != url: 124 raise RuntimeError, \ 125 "FITS disks are not on the same machine" 126 input_dict[adverb] = int(FITS.disks[disk].disk) 127 if not proxy: 128 raise RuntimeError, \ 129 "Unable to determine where to execute task" 130 131 inst = getattr(proxy, self.__class__.__name__) 132 tid = inst.spawn(self._name, self.version, self.userno, 133 self.msgkill, self.isbatch, input_dict) 134 135 self._message_list = [] 136 return (proxy, tid)
137
138 - def messages(self, proxy=None, tid=None):
139 """Return messages for the task specified by PROXY and TID.""" 140 141 if not proxy and not tid: 142 return self._message_list 143 144 inst = getattr(proxy, self.__class__.__name__) 145 messages = inst.messages(tid) 146 if not messages: 147 return None 148 self._message_list.extend(messages) 149 return messages
150
151 - def go(self):
152 """Run the task.""" 153 154 (proxy, tid) = self.spawn() 155 log = [] 156 count = 0 157 rotator = ['|\b', '/\b', '-\b', '\\\b'] 158 while not self.finished(proxy, tid): 159 messages = self.messages(proxy, tid) 160 if messages: 161 log.extend(messages) 162 elif sys.stdout.isatty(): 163 sys.stdout.write(rotator[count % 4]) 164 sys.stdout.flush() 165 pass 166 count += 1 167 continue 168 self.wait(proxy, tid) 169 return
170 171 172 # Tests. 173 if __name__ == '__main__': 174 import doctest, sys 175 doctest.testmod(sys.modules[__name__]) 176