/
simgeneral_c.py
281 lines (263 loc) · 10.6 KB
/
simgeneral_c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
simgeneral_c.py
Prepares data to be fed into simulation.
"""
import numpy as np
#import matplotlib.pyplot as plt
try:
import pyfits
except ImportError:
import astropy.io.fits as pyfits
import time
from physics import n_sell, energy2counts
from printer import Printer
from printtiming import print_timing
#from slitfunctions import *
from slitfunctions import slit_image, point_source
import cfunctions
import wavefuncs
@print_timing
def sim_general(arm, fiber, wavelengths=None, intensities=None,
fiber_description=None, wavemap=None):
"""
General simulation function.
Parameters
----------
arm : SpectralArm object
Container object of spectrograph and simulation parameters.
fiber : int {0,1}
Fiber to simulate. 1 = A and 2 = B.
"""
if wavelengths is None: wavelengths = arm.wavelengths
if intensities is None: intensities = arm.intensities
#if fiber_description is None: fiber_description = arm.fiber_description
if wavemap is None: wavemap = arm.wavemap
print "Fiber %s - %s" % (arm.fib_char[fiber], arm.fiber_description)
t1 = time.time() # start time
orders = arm.OSET
norders = arm.NOSET
if arm.fib_char[fiber] in ['A', 'B']: #@MZ
offset = arm.fib_OFFSET[fiber]
# output arrays
image = arm.zero_images[fiber] # initialize image CCD array
if arm.SAMPLING == "mc":
image = np.array(image, dtype=np.uint)
if arm.SAMPLING == "grid":
wavemap_arr = np.zeros(arm.CCD_DIMS, dtype=np.uint)
elif arm.SAMPLING == "mc":
wavemap_arr = np.zeros(arm.CCD_DIMS)
# m_list = np.zeros((0,0), dtype=np.uint)
# CREATE SLIT ?
if arm.SAMPLING == "grid":
ns = arm.ns # input slit samples
if arm.slit is '0':
slitx, slity = point_source(arm, offset=offset)
else:
slitx, slity = slit_image(arm, offset=offset)
nslit = slitx.size # effective slit samples
perturb = arm.slitperturb
elif arm.SAMPLING == "mc":
ns = False
slitx = False
slity = False
perturb = False
if arm.slit[0] in '0 1 2'.split():
arm.slittyp = 'uniform'
SLIT_FLAG = 0
arm.slit_locx = 0.0
arm.slit_locy = 0.0
arm.slit_scalex = 0.0
arm.slit_scaley = 0.0
elif arm.slit[0] == '3':
arm.slittyp = 'gaussian'
SLIT_FLAG = 1
if len(arm.slit) == 1:
arm.slit_locx = 0.0
arm.slit_locy = 0.0
arm.slit_scalex = 1.0
arm.slit_scaley = 1.0
elif len(arm.slit) == 2:
arm.slit_locx = 0.0
arm.slit_locy = 0.0
arm.slit_scalex = float(arm.slit[1])
arm.slit_scaley = float(arm.slit[1])
elif len(arm.slit) == 3:
arm.slit_locx = float(arm.slit[1])
arm.slit_locy = float(arm.slit[1])
arm.slit_scalex = float(arm.slit[2])
arm.slit_scaley = float(arm.slit[2])
elif len(arm.slit) == 4:
arm.slit_locx = float(arm.slit[1])
arm.slit_locy = float(arm.slit[2])
arm.slit_scalex = float(arm.slit[3])
arm.slit_scaley = float(arm.slit[3])
elif len(arm.slit) == 5:
arm.slit_locx = float(arm.slit[1])
arm.slit_locy = float(arm.slit[2])
arm.slit_scalex = float(arm.slit[3])
arm.slit_scaley = float(arm.slit[4])
# set simulation flags
if arm.blaze:
BLAZE_FLAG = 1
else:
BLAZE_FLAG = 0
LOC_FLAG = 0
# CDF interpolation flag
for word in 'flatfield line'.split():
if word in arm.fiber_description:
interp_flag = 0 # do not interpolate (for line lists)
else:
interp_flag = 3 # interpolate
if arm.interp_flag:
interp_flag = arm.interp_flag
if arm.ARM == "NIR" and arm.gap:
print "Creating NIR detector gap of %s mm at x=%s mm" % (arm.gap, arm.gapoffset)
# SOLUTION/COMPUTATION BLOCK
# =========================================================================
if arm.SAMPLING == "mc":
print "Sampling method: Monte Carlo"
print "Wavelength samping: %s" % arm.sm
print "Fiber sampling distribution: %s" % arm.slittyp
counts_tot = np.trapz(energy2counts(wavelengths, intensities), wavelengths) # total counts
wave_range = 0.0 # wavelength coverage of each spectral order
for i,m in enumerate(orders):
waves, weights = wavefuncs.feed_wavelengths(arm, m)
counts = energy2counts(waves, weights) # flux to photon counts
wave_range = np.trapz(counts, waves) / counts_tot
nwaves = waves.size
nrays = int(arm.nr[fiber] * wave_range)
output = ' * Calculating order %i (%i of %i), total %.2f%% complete.' % (m, i+1, norders, ((i+1.) * 100.0 / norders))
if arm.sm == "cdf":
nrays_org = nrays # @MZ
irays = 1e8 # @MZ
nloop = np.int((nrays_org - 1) / irays) + 1 # @MZ
for loop in xrange(nloop): # @MZ
nrays = np.int(min(irays, nrays_org-loop*irays)) # @MZ
print 'Memory block loop {}/{}, nrays {}/{}'.format(loop+1, nloop, nrays, nrays_org) # @MZ
waves_inp = np.zeros(nrays)
print " * Sampling wavelengths using Cumulative Distribution Function (CDF)..."
cfunctions.random_wave_cdf(
waves,
counts,
nwaves,
interp_flag,
waves_inp,
nrays)
print output
print " * wavelength coverage = %.2f%%" % (wave_range * 100.0)
print " * nrays = %s" % nrays
cfunctions.compute_mc_cdf(
arm.ARM_FLAG,
BLAZE_FLAG,
arm.GAP_FLAG,
SLIT_FLAG,
nrays,
m,
arm.gap,
arm.gapoffset,
arm.XD_0,
arm.YD_0,
offset,
arm.slit_locx,
arm.slit_locy,
arm.slit_scalex,
arm.slit_scaley,
np.ascontiguousarray(waves_inp, dtype=np.float64),
image,
wavemap_arr)
nrays = nrays_org
elif arm.sm == "rej":
print output
print " * wavelength coverage = %.2f%%" % (wave_range * 100.0)
print " * nrays = %s" % nrays
cfunctions.compute_mc_rejsamp(
arm.ARM_FLAG,
BLAZE_FLAG,
arm.GAP_FLAG,
SLIT_FLAG,
nwaves,
nrays,
m,
arm.gap,
arm.gapoffset,
arm.XD_0,
arm.YD_0,
weights.max(),
offset,
arm.slit_locx,
arm.slit_locy,
arm.slit_scalex,
arm.slit_scaley,
np.ascontiguousarray(waves, dtype=np.float64),
np.ascontiguousarray(weights, dtype=np.float64),
image,
wavemap_arr)
arm.fib_nrays[fiber].append(nrays)
inds = np.where(image != 0.0)
arm.fib_mean_rays_per_pixel[fiber] = np.mean(image[inds])
arm.fib_min_rays_per_pixel[fiber] = np.min(image[inds])
arm.fib_max_rays_per_pixel[fiber] = np.max(image[inds])
arm.fib_nrays_tot[fiber] = np.sum(image)
elif arm.SAMPLING == "grid":
print "Sampling method: Grid"
print "Fiber location perturbation: %s" % arm.perturb
for i,m in enumerate(orders):
output = ' * Calculating order %i (%i of %i), total %.2f%% complete.' % (m, i+1, norders, ((i+1.) * 100.0 / norders))
print output
if arm.perturb:
waves, weights = wavefuncs.feed_wavelengths(arm, m)
n_g_sell = n_sell(arm.ARM, waves)
nwaves = waves.size
cfunctions.compute_grid_perturb(
arm.ARM_FLAG,
BLAZE_FLAG,
arm.GAP_FLAG,
nwaves,
nslit,
m,
arm.gap,
arm.gapoffset,
arm.XD_0,
arm.YD_0,
arm.slitperturb,
np.ascontiguousarray(n_g_sell, dtype=np.float64),
np.ascontiguousarray(slitx, dtype=np.float64),
np.ascontiguousarray(slity, dtype=np.float64),
np.ascontiguousarray(waves, dtype=np.float64),
np.ascontiguousarray(weights, dtype=np.float64),
image,
wavemap_arr)
else:
waves, weights = wavefuncs.feed_wavelengths(arm, m)
n_g_sell = n_sell(arm.ARM, waves)
nwaves = waves.size
returnx = np.empty(nwaves*nslit)
returny = np.empty(nwaves*nslit)
cfunctions.compute_grid(
arm.ARM_FLAG,
BLAZE_FLAG,
arm.GAP_FLAG,
nwaves,
nslit,
m,
arm.gap,
arm.gapoffset,
arm.XD_0,
arm.YD_0,
np.ascontiguousarray(n_g_sell, dtype=np.float64),
np.ascontiguousarray(slitx, dtype=np.float64),
np.ascontiguousarray(slity, dtype=np.float64),
np.ascontiguousarray(waves, dtype=np.float64),
np.ascontiguousarray(weights, dtype=np.float64),
image,
wavemap_arr)
if wavemap:
inds = np.where(wavemap_arr != 0)
image[inds] *= 1.0e7 / wavemap_arr[inds] # average each pixel and convert mm to Angstrom
else:
image *= (arm.MAX_SNR**2) / image.max()
print "Normalizing image array to a max of %s SNR." % arm.MAX_SNR
arm.fib_nslit[fiber] = nslit
arm.add_image(image)
arm.fib_sim_time[fiber] = time.time() - t1
return 0