| 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 |
|
| 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 |
|
| 38 |
|
| 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 |
|
| 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 |
|
| 83 |
|
| 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 |
|
| 99 |
|
| 100 |
for i in range(len(self.keywords)): |
| 101 |
self.keywords[i] = self.keywords[i].lower() |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 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 |
|
| 130 |
if nodes[0].size == 0: |
| 131 |
return -1 |
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 151 |
|
| 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 |
|
| 200 |
if nodes[0].size == 0: |
| 201 |
if DEBUG: |
| 202 |
print("no such innode %d: stop condition"%innode) |
| 203 |
|
| 204 |
break |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 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 |
|
| 219 |
|
| 220 |
outnode = -2 |
| 221 |
component = thcomponent = None |
| 222 |
|
| 223 |
|
| 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 |
|
| 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) |