1 from __future__ import division
2 from spark import GenericScanner, GenericParser, GenericASTTraversal
3 from spark import GenericASTBuilder, GenericASTMatcher
4 import spectrum
5 import reddening
6 import observationmode
7 import locations
8 import catalog
9 import os
10 from obsbandpass import ObsBandpass
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 syfunctions = [
30 'spec',
31 'unit',
32 'box',
33 'bb',
34 'pl',
35 'em',
36 'icat',
37 'rn',
38 'z',
39 'ebmvx',
40 'band'
41 ]
42
43 synforms = [
44 'fnu',
45 'flam',
46 'photnu',
47 'photlam',
48 'counts',
49 'abmag',
50 'stmag',
51 'obmag',
52 'vegamag',
53 'jy',
54 'mjy'
55 ]
56
57 syredlaws = [
58 'gal1',
59 'gal2',
60 'gal3',
61 'smc',
62 'lmc',
63 'xgal'
64 ]
65
67 - def __init__(self, type=None, attr=None):
68 self.type = type
69 self.attr = attr
71 return cmp(self.type, o)
73 if self.attr is not None:
74 return str(self.attr)
75 else:
76 return self.type
77
80 self.type = type
81 self._kids = []
85 return len(self._kids)
87 self._kids[low:high] = seq
89 return cmp(self.type, o)
90
101 r' \+ | \* | - '
102 self.rv.append(Token(type=s))
104 r' \( '
105 self.rv.append(Token(type='LPAREN'))
107 r' \) '
108 self.rv.append(Token(type='RPAREN'))
110 r' , '
111 self.rv.append(Token(type=s))
113 r' \d+ '
114 self.rv.append(Token(type='INTEGER', attr=s))
116 r' [$a-z_A-Z/\//][\w/\.\$:]*'
117 self.rv.append(Token(type='IDENTIFIER', attr=s))
119 r' @\w+'
120 self.rv.append(Token(type='FILELIST', attr=s[1:]))
121
126 r' ((\d*\.\d+)|(\d+\.d*)|(\d+)) ([eE][-+]?\d+)?'
127 self.rv.append(Token(type='FLOAT', attr=s))
129 r' \s/\s '
130 self.rv.append(Token(type='/'))
131
133 - def __init__(self, ASTclass, start='top'):
136 '''
137 top ::= expr
138 top ::= FILELIST
139 expr ::= expr + term
140 expr ::= expr - term
141 expr ::= term
142 term ::= term * factor
143 term ::= term / factor
144 value ::= LPAREN expr RPAREN
145 term ::= factor
146 factor ::= unaryop value
147 factor ::= value
148 unaryop ::= +
149 unaryop ::= -
150 value ::= INTEGER
151 value ::= FLOAT
152 value ::= IDENTIFIER
153 value ::= function_call
154 function_call ::= IDENTIFIER LPAREN arglist RPAREN
155 arglist ::= arglist , expr
156 arglist ::= expr
157 '''
159 rv = AST(token.type)
160 rv.attr = token.attr
161 return rv
166
171 raise ValueError("problems in interpreting AST")
173 ''' V ::= INTEGER '''
174 tree.value = int(tree.attr)
175 tree.svalue = tree.attr
177 ''' V ::= FLOAT '''
178 tree.value = float(tree.attr)
179 tree.svalue = tree.attr
181 ''' V ::= IDENTIFIER '''
182 tree.value = tree.attr
183 tree.svalue = tree.attr
185 ''' V ::= factor ( + V ) '''
186 tree.value = convertstr(tree[1].value)
188 ''' V ::= factor ( - V ) '''
189 tree.value = - convertstr(tree[1].value)
191 ''' V ::= expr ( V + V )'''
192 tree.value = convertstr(tree[0].value) + convertstr(tree[2].value)
194 ''' V ::= expr ( V - V )'''
195 tree.value = convertstr(tree[0].value) - convertstr(tree[2].value)
197 ''' V ::= term ( V * V )'''
198 tree.value = convertstr(tree[0].value) * convertstr(tree[2].value)
200 ''' V ::= term ( V / V )'''
201 tree.value = convertstr(tree[0].value) / tree[2].value
203 ''' V ::= value ( LPAREN V RPAREN )'''
204 tree.value = convertstr(tree[1].value)
205 tree.svalue = "(%s)"%str(tree[1].value)
207 ''' V ::= arglist ( V , V )'''
208 if type(tree[0].value) == type([]):
209 tree.value = tree[0].value + [tree[2].value]
210 else:
211 tree.value = [tree[0].value, tree[2].value]
212 try:
213 tree.svalue = "%s,%s"%(tree[0].svalue,tree[2].svalue)
214 except AttributeError:
215 pass
216
218
219
220
221 ''' V ::= function_call ( V LPAREN V RPAREN )'''
222 if type(tree[2].value) != type([]):
223 args = [tree[2].value]
224 else:
225 args = tree[2].value
226 fname = tree[0].value
227 if fname not in syfunctions:
228 print "Error: unknown function:", fname
229 self.error(fname)
230 else:
231 if fname == 'unit':
232
233 tree.value = spectrum.FlatSpectrum(args[0],fluxunits=args[1])
234 elif fname == 'bb':
235
236 tree.value = spectrum.BlackBody(args[0])
237 elif fname == 'pl':
238
239 if args[2] not in synforms:
240 print "Error: unrecognized units:", args[2]
241
242 tree.value = spectrum.Powerlaw(args[0],args[1],fluxunits=args[2])
243 elif fname == 'box':
244
245 tree.value = spectrum.Box(args[0],args[1])
246 elif fname == 'spec':
247
248 name = args[0]
249 tree.value = spectrum.TabularSourceSpectrum(_handleIRAFName(name))
250 elif fname == 'band':
251
252 args=tree[2].svalue
253 tree.value = ObsBandpass(args)
254 elif fname == 'em':
255
256 tree.value = spectrum.GaussianSource(args[2],args[0],args[1],fluxunits=args[3])
257 elif fname == 'icat':
258
259 tree.value = catalog.Icat(*args)
260 elif fname == 'rn':
261
262 sp = args[0]
263 if not isinstance(sp,spectrum.SourceSpectrum):
264 name=_handleIRAFName(args[0])
265 sp = spectrum.TabularSourceSpectrum(name)
266
267
268
269 try:
270 tree.value = sp.renorm(args[2],args[3],args[1])
271 except ValueError:
272 tree.value = sp.renorm(args[2],args[3],args[1],force=True)
273 tree.value.warnings['force_renorm'] = 'Warning: Renormalization of the spectrum, to the specified value, in the specified units, exceeds the limit of the specified passband.'
274
275 elif fname == 'z':
276
277 if args[0] != 'null':
278 try:
279 tree.value = args[0].redshift(args[1])
280 except AttributeError:
281 try:
282
283 sp = spectrum.TabularSourceSpectrum( \
284 _handleIRAFName(args[0]))
285 tree.value = sp.redshift(args[1])
286 except AttributeError:
287 tree.value = spectrum.FlatSpectrum(1.0)
288 else:
289 tree.value = spectrum.FlatSpectrum(1.0)
290 elif fname == 'ebmvx':
291
292 tree.value = reddening.Extinction(args[0],args[1])
293
294 else:
295 tree.value = "would call %s with the following args: %s" % (fname, repr(args))
296
297
298
299 zzz = '''
300
301 top ::= FILELIST
302
303 '''
304
314
316 scanner = Scanner()
317 input = input.replace('%2b','+')
318 return scanner.tokenize(input)
319
323
329
331 for token in tlist:
332 print token.type, token.attr
333
334
339
346
347
349 """Parse the synphot-classic command and return the resulting spectrum"""
350 sp = interpret(parse(scan(syncommand)))
351 return sp
352