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

Source Code for Module FITSData

  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 FITSImage and FITSUVData classes.  These 
 21  classes implement most of the data oriented verb-like functionality 
 22  from classic FITS. 
 23   
 24  """ 
 25   
 26  # Global FITS defaults. 
 27  from FITS import FITS 
 28   
 29  # Generic Python stuff. 
 30  import sys 
 31   
 32  # This code is way too clever.  Instead of implementing each and every 
 33  # function call provided by a proxy, class _Method implements a 
 34  # callable object that invokes a named method of a proxy, passing a 
 35  # description of the FITS data it should operate on as the first 
 36  # argument.  To make matters worse, the same implementation is used 
 37  # for class FITSImage and class FITSData and dispatching is done based 
 38  # on the name of the class.  This way adding a function to the proxy 
 39  # automatically makes it callable as a method of both FITSImage and 
 40  # FITSUVData. 
 41   
42 -def _whoami():
43 """Return the name of the function that called us.""" 44 return sys._getframe(1).f_code.co_name
45 46
47 -class _FITSDataMethod:
48 49 """This class implements dispatching function calls to a proxy.""" 50
51 - def __init__(self, inst, name):
52 self.inst = inst 53 self.name = name
54
55 - def __call__(self, *args):
56 func = self.inst._method(self.name) 57 return func(self.inst.desc, *args)
58 59
60 -class _FITSDataDesc:
61 62 """This class implements the description of FITS data that is used 63 when dispatching function calls to a proxy.""" 64
65 - def __init__(self, filename, disk):
66 self.filename = filename 67 self.disk = disk
68 69 # Provide a dictionary-like interface to deal with the 70 # idiosyncrasies of XML-RPC.
71 - def __getitem__(self, key):
72 return self.__dict__[key]
73 74
75 -class _FITSData:
76 77 """This class describes generic FITS data.""" 78
79 - def __init__(self, name, disk):
80 self.desc = _FITSDataDesc(name, FITS.disks[disk].disk) 81 self.proxy = FITS.disks[disk].proxy() 82 self.disk = disk 83 return
84 85 filename = property(lambda self: self.desc.filename, 86 doc='Filename of this data set.') 87 disk = property(lambda self: self.desc.disk, 88 doc='Disk where this data set is stored.') 89
90 - def __repr__(self):
91 repr = "%s('%s', %d)" % \ 92 (self.filename, self.disk) 93 return repr
94
95 - def __str__(self):
96 return self.__repr__()
97
98 - def __getattr__(self, filename):
99 if filename in self.desc.__dict__: 100 return self.desc.__dict__[filename] 101 return _FITSDataMethod(self, filename)
102
103 - def table(self, type, version):
104 return _FITSTable(self, type, version)
105
106 - def _method(self, name):
107 return getattr(getattr(self.proxy), filename)
108
109 - def exists(self):
110 """Check whether this image or data set exists. 111 112 Returns True if the image or data set exists, False otherwise.""" 113 return self._method(_whoami())(self.desc)
114
115 - def verify(self):
116 """Verify whether this image or data set can be accessed.""" 117 return self._method(_whoami())(self.desc)
118
119 - def header(self):
120 """Get the header for this image or data set. 121 122 Returns the header as a dictionary.""" 123 return self._method(_whoami())(self.desc)
124
125 - def tables(self):
126 """Get the list of extension tables.""" 127 return self._method(_whoami())(self.desc)
128
129 - def table_highver(self, type):
130 """Get the highest version of an extension table. 131 132 Returns the highest available version number of the extension 133 table TYPE.""" 134 return self._method(_whoami())(self.desc, type)
135
136 - def zap(self):
137 """Destroy this image or data set.""" 138 return self._method(_whoami())(self.desc)
139
140 - def header_table(self, type, version):
141 """Get the header of an extension table. 142 143 Returns the header of version VERSION of the extension table 144 TYPE.""" 145 return self._method(_whoami())(self.desc, type, version)
146
147 - def getrow_table(self, type, version, rowno):
148 """Get a row from an extension table. 149 150 Returns row ROWNO from version VERSION of extension table TYPE 151 as a dictionary.""" 152 return self._method(_whoami())(self.desc, type, version, rowno)
153
154 - def zap_table(self, type, version):
155 """Destroy an extension table. 156 157 Deletes version VERSION of the extension table TYPE. If 158 VERSION is 0, delete the highest version of table TYPE. If 159 VERSION is -1, delete all versions of table TYPE.""" 160 return self._method(_whoami())(self.desc, type, version)
161 162
163 -class FITSImage(_FITSData):
164 165 """This class describes an FITS image.""" 166 pass
167 168
169 -class FITSUVData(_FITSData):
170 171 """This class describes an FITS UV data set.""" 172 pass
173 174
175 -class _FITSTableMethod(_FITSDataMethod):
176 177 """ This class implements dispatching table oriented function 178 calls to a proxy.""" 179
180 - def __init__(self, inst, name):
181 _FITSDataMethod.__init__(self, inst, name)
182
183 - def __call__(self, *args):
184 func = self.inst.data._method(self.name + '_table') 185 return func(self.inst.data.desc, 186 self.inst.name, self.inst.version, *args)
187 188
189 -class _FITSTable:
190 191 """This class describes a generic FITS extension table.""" 192
193 - def __init__(self, data, name, version):
194 self.data = data 195 self.name = name 196 self.version = version
197
198 - def __getattr__(self, name):
199 return _FITSTableMethod(self, name)
200