Debian Codegraph @

pysynphot/tables.py

raw symbols SCIP
1 """This module handles graph and component tables.
2 They are discussed in detail in :ref:`pysynphot-appendixc`.
3
4 """
5 from __future__ import division, print_function
6
7 import numpy as N
8 from astropy.io import fits as pyfits
9
10 #Flag to control verbosity
11 DEBUG = False
12
13
14 class CompTable(object):
15 """Class to handle a :ref:`component table <pysynphot-master-comp>`.
16
17 Parameters
18 ----------
19 CFile : str
20 Component table filename.
21
22 Attributes
23 ----------
24 name
25 Same as input ``CFile``.
26
27 compnames, filenames : array_like
28 Values from ``COMPNAME`` and ``FILENAME`` columns in EXT 1.
29
30 Raises
31 ------
32 TypeError
33 No filename given.
34
35 """
36 def __init__(self, CFile=None):
37 # None is common for various errors.
38 # the default value of None is not useful; pyfits.open(None) does not work.
39 if CFile is None :
40 raise TypeError('initializing CompTable with CFile=None; possible bad/missing PYSYN_CDBS')
41
42 cp = pyfits.open(CFile)
43
44 self.compnames = cp[1].data.field('compname')
45 self.filenames = cp[1].data.field('filename')
46
47 # Is this necessary?
48 compdict = {}
49 for i in range(len(self.compnames)):
50 compdict[self.compnames[i]] = self.filenames[i]
51
52 cp.close()
53 self.name=CFile
54
55
56 class GraphTable(object):
57 """Class to handle a :ref:`graph table <pysynphot-graph>`.
58
59 Parameters
60 ----------
61 GFile : str
62 Graph table filename.
63
64 Attributes
65 ----------
66 keywords : array_like
67 Values from ``KEYWORD`` column in EXT 1, converted to lowercase.
68
69 innodes, outnodes, compnames, thcompnames : array_like
70 Values from ``INNODE``, ``OUTNODE``, ``COMPNAME``, and ``THCOMPNAME`` columns in EXT 1.
71
72 primary_area : number
73 Value from ``PRIMAREA`` in EXT 0 header, if exists.
74
75 Raises
76 ------
77 TypeError
78 No filename given.
79
80 """
81 def __init__(self, GFile=None):
82 # None is common for various errors.
83 # the default value of None is not useful; pyfits.open(None) does not work.
84 if GFile is None :
85 raise TypeError('initializing GraphTable with GFile=None; possible bad/missing PYSYN_CDBS')
86
87 gp = pyfits.open(GFile)
88
89 if 'PRIMAREA' in gp[0].header:
90 self.primary_area = gp[0].header['PRIMAREA']
91
92 self.keywords = gp[1].data.field('keyword')
93 self.innodes = gp[1].data.field('innode')
94 self.outnodes = gp[1].data.field('outnode')
95 self.compnames = gp[1].data.field('compname')
96 self.thcompnames = gp[1].data.field('thcompname')
97
98 # keywords must be forced to lower case (STIS keywords are
99 # mixed mode %^&^(*^*^%%%@#$!!!)
100 for i in range(len(self.keywords)):
101 self.keywords[i] = self.keywords[i].lower()
102
103
104 ## for comp in self.compnames:
105 ## try:
106 ## if comp.index('nic') == 0:
107 ## print comp
108 ## except:
109 ## pass
110
111 # prints components associated with a given keyword
112 ## i = -1
113 ## for keyword in self.keywords:
114 ## i = i + 1
115 ## if keyword == 'acs':
116 ## print self.compnames[i]
117
118 gp.close()
119
120 def GetNextNode(self, modes, innode):
121 """GetNextNode returns the outnode that matches an element from
122 the modes list, starting at the given innode.
123 This method isnt actually used, its just a helper method for
124 debugging purposes.
125
126 """
127 nodes = N.where(self.innodes == innode)
128
129 ## If there's no entry for the given innode, return -1
130 if nodes[0].size == 0:
131 return -1
132
133 ## If we don't match anything in the modes list, we find the
134 ## outnode corresponding the the string 'default'
135 defaultindex = N.where(self.keywords[nodes] == 'default')
136
137 if len(defaultindex[0]) != 0:
138 outnode = self.outnodes[nodes[0][defaultindex[0]]]
139
140 ## Now try and match one of the strings in the modes list with
141 ## the keywords corresponding to the list of entries with the given
142 ## innode
143 for mode in modes:
144 result = self.keywords[nodes].count(mode)
145 if result != 0:
146 index = N.where(self.keywords[nodes]==mode)
147 outnode = self.outnodes[nodes[0][index[0]]]
148
149
150 ## Return the outnode corresponding either to the matched mode,
151 ## or to 'default'
152 return outnode
153
154 def GetComponentsFromGT(self, modes, innode):
155 """Obtain components from graph table for the given
156 observation mode keywords and starting node.
157
158 .. note::
159
160 This prints extra information to screen if
161 ``pysynphot.tables.DEBUG`` is set to `True`.
162
163 Parameters
164 ----------
165 modes : list of str
166 List of individual keywords within the observation mode.
167
168 innode : int
169 Starting node, usually 1.
170
171 Returns
172 -------
173 components, thcomponents : list of str
174 List of optical and thermal component names.
175
176 Raises
177 ------
178 KeyError
179 No matches found for one of the keywords.
180
181 ValueError
182 Incomplete observation mode or unused keyword(s) detected.
183
184 """
185 components = []
186 thcomponents = []
187 outnode = 0
188 inmodes=set(modes)
189 used_modes=set()
190 count = 0
191 while outnode >= 0:
192 if (DEBUG and (outnode < 0)):
193 print("outnode == %d: stop condition"%outnode)
194
195 previous_outnode = outnode
196
197 nodes = N.where(self.innodes == innode)
198
199 # If there are no entries with this innode, we're done
200 if nodes[0].size == 0:
201 if DEBUG:
202 print("no such innode %d: stop condition"%innode)
203 #return (components,thcomponents)
204 break
205
206 # Find the entry corresponding to the component named
207 # 'default', bacause thats the one we'll use if we don't
208 # match anything in the modes list
209 defaultindex = N.where(self.keywords[nodes] =='default')
210
211 if 'default' in self.keywords[nodes]:
212 dfi=N.where(self.keywords[nodes] == 'default')[0][0]
213 outnode = self.outnodes[nodes[0][dfi]]
214 component = self.compnames[nodes[0][dfi]]
215 thcomponent = self.thcompnames[nodes[0][dfi]]
216 used_default=True
217 else:
218 #There's no default, so fail if you don't match anything
219 # in the keyword matching step.
220 outnode = -2
221 component = thcomponent = None
222
223 # Now try and match something from the modes list
224 for mode in modes:
225
226 if mode in self.keywords[nodes]:
227 used_modes.add(mode)
228 index = N.where(self.keywords[nodes]==mode)
229 if len(index[0])>1:
230 raise KeyError('%d matches found for %s'%(len(index[0]),mode))
231 idx=index[0][0]
232 component = self.compnames[nodes[0][idx]]
233 thcomponent = self.thcompnames[nodes[0][idx]]
234 outnode = self.outnodes[nodes[0][idx]]
235 used_default=False
236
237 if DEBUG:
238 print("Innode %d Outnode %d Compname %s"%(innode, outnode, component))
239 components.append(component)
240 thcomponents.append(thcomponent)
241
242
243 innode = outnode
244
245 if outnode == previous_outnode:
246 if DEBUG:
247 print("Innode: %d Outnode:%d Used default: %s"%(innode, outnode,used_default))
248 count += 1
249 if count > 3:
250 if DEBUG:
251 print("same outnode %d > 3 times: stop condition"%outnode)
252 break
253
254 if (outnode < 0):
255 if DEBUG:
256 print("outnode == %d: stop condition"%outnode)
257 raise ValueError("Incomplete obsmode %s"%str(modes))
258
259
260 #Check for unused modes
261 if inmodes != used_modes:
262 unused=str(inmodes.difference(used_modes))
263 raise ValueError("Warning: unused keywords %s"%unused)
264
265 return (components,thcomponents)