SpecDisperser.py 25.4 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from astropy.table import Table

import astropy.constants as cons

import collections
from collections import OrderedDict
from scipy import interpolate

from astropy import units as u

# from numpy import *

import numpy as np

from pylab import *

import galsim

import sys
import os
21
import math
Fang Yuedong's avatar
Fang Yuedong committed
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


def rotate90(array_orig=None, xc=0, yc=0, isClockwise=0):
    if array_orig is None:
        return

    l1 = array_orig.shape[0]
    l2 = array_orig.shape[1]

    if xc < 0 or xc > l2 - 1 or yc < 0 or yc > l1 - 1:
        return

    n_xc = xc
    n_yc = yc

    array_final = np.zeros_like(array_orig.T)

    if isClockwise == 1:
        for i in np.arange(l2):
            array_final[i] = array_orig[:, l2 - i - 1]
        n_xc = yc
        n_yc = l2 - 1 - xc
    else:
        for i in np.arange(l2):
            array_final[i] = array_orig[::-1, i]

        n_xc = l1 - 1 - yc
        n_yc = xc
    return array_final, n_xc, n_yc


class SpecDisperser(object):
Zhang Xin's avatar
Zhang Xin committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    def __init__(
        self,
        orig_img=None,
        xcenter=0,
        ycenter=0,
        origin=[100, 100],
        tar_spec=None,
        band_start=6200,
        band_end=10000,
        isAlongY=0,
        conf="../param/CONF/csst.conf",
        gid=0,
        flat_cube=None,
        ignoreBeam=[],
    ):
Fang Yuedong's avatar
Fang Yuedong committed
69
70
71
72
73
74
75
76
77
78
79
80
81
        """
        orig_img: normal image,galsim image
        xcenter, ycenter: the center of galaxy in orig_img
        origin : [int, int]
            `origin` defines the lower left pixel index (y,x) of the `direct`
            cutout from a larger detector-frame image
        tar_spec: galsim.SED

        """

        # self.img_x = orig_img.shape[1]
        # self.img_y = orig_img.shape[0]

82
        # 5 orders, A, B ,
Zhang Xin's avatar
Zhang Xin committed
83
        orderName = ["A", "B", "C", "D", "E"]
84
85
86
87
88
        self.orig_img_orders = OrderedDict()
        if isinstance(orig_img, list):
            orig_img_list = orig_img
            list_len = len(orig_img_list)
            if list_len < 5:
Zhang Xin's avatar
Zhang Xin committed
89
90
                for i in np.arange(5 - list_len):
                    orig_img_list.append(orig_img_list[list_len - 1])
91
92
93
94
95
96
97
98
99
100
101
102
103
            for i, k in enumerate(orig_img_list):
                self.orig_img_orders[orderName[i]] = k

        if isinstance(orig_img, galsim.Image):
            for i in np.arange(5):
                self.orig_img_orders[orderName[i]] = orig_img

        orig_img_one = self.orig_img_orders["A"]

        self.thumb_img = np.abs(orig_img_one.array)
        self.thumb_x = orig_img_one.center.x
        self.thumb_y = orig_img_one.center.y
        self.img_sh = orig_img_one.array.shape
Fang Yuedong's avatar
Fang Yuedong committed
104
105
106
107
108
109
110
111
112

        self.id = gid

        self.xcenter = xcenter
        self.ycenter = ycenter

        self.isAlongY = isAlongY
        self.flat_cube = flat_cube
        if self.isAlongY == 1:
113
            for order in orderName:
Zhang Xin's avatar
Zhang Xin committed
114
115
116
117
118
119
                self.orig_img_orders[order], self.thumb_x, self.thumb_y = rotate90(
                    array_orig=self.orig_img_orders[order],
                    xc=orig_img_one.center.x,
                    yc=orig_img_one.center.y,
                    isClockwise=1,
                )
120
121
            # self.thumb_img, self.thumb_x, self.thumb_y = rotate90(array_orig=self.thumb_img, xc=orig_img_one.center.x,
            #                                                       yc=orig_img_one.center.y, isClockwise=1)
Fang Yuedong's avatar
Fang Yuedong committed
122

123
            self.img_sh = self.orig_img_orders[order].array.T.shape
Fang Yuedong's avatar
Fang Yuedong committed
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
            self.xcenter = ycenter
            self.ycenter = xcenter

        self.origin = origin
        self.band_start = band_start
        self.band_end = band_end
        self.spec = tar_spec

        self.beam_flux = OrderedDict()

        self.grating_conf = aXeConf(conf)
        self.grating_conf.get_beams()
        self.grating_conf_file = conf
        self.ignoreBeam = ignoreBeam

    def compute_spec_orders(self):

        all_orders = OrderedDict()
        beam_names = self.grating_conf.beams

        for beam in beam_names:
            if beam in self.ignoreBeam:
                continue
            all_orders[beam] = self.compute_spec(beam)

        return all_orders

    def compute_spec(self, beam):

153
154
155
        # if beam == "B":
        #     return self.thumb_img, self.origin[1], self.origin[0], None, None, None

Fang Yuedong's avatar
Fang Yuedong committed
156
157
        from .disperse_c import interp
        from .disperse_c import disperse
Zhang Xin's avatar
Zhang Xin committed
158

Fang Yuedong's avatar
Fang Yuedong committed
159
        # from MockObject.disperse_c import disperse
160
161
162
163
        self.thumb_img = np.abs(self.orig_img_orders[beam].array)
        self.thumb_x = self.orig_img_orders[beam].center.x
        self.thumb_y = self.orig_img_orders[beam].center.y
        self.img_sh = self.orig_img_orders[beam].array.shape
Fang Yuedong's avatar
Fang Yuedong committed
164
165
        dx = self.grating_conf.dxlam[beam]
        xoff = 0
Zhang Xin's avatar
Zhang Xin committed
166
167
168
        ytrace_beam, lam_beam = self.grating_conf.get_beam_trace(
            x=self.xcenter, y=self.ycenter, dx=(dx + xoff), beam=beam
        )
Fang Yuedong's avatar
Fang Yuedong committed
169
170

        # Account for pixel centering of the trace
Zhang Xin's avatar
Zhang Xin committed
171
        yfrac_beam = ytrace_beam - floor(ytrace_beam + 0.5)
Fang Yuedong's avatar
Fang Yuedong committed
172
173
174
175
176

        ysens = lam_beam * 0
        lam_index = argsort(lam_beam)
        conf_sens = self.grating_conf.sens[beam]

Zhang Xin's avatar
Zhang Xin committed
177
178
        lam_intep = np.linspace(self.band_start, self.band_end, int(
            (self.band_end - self.band_start) / 0.1))
Fang Yuedong's avatar
Fang Yuedong committed
179

Zhang Xin's avatar
Zhang Xin committed
180
181
        thri = interpolate.interp1d(
            conf_sens["WAVELENGTH"], conf_sens["SENSITIVITY"])
Zhang Xin's avatar
Zhang Xin committed
182
        spci = interpolate.interp1d(self.spec["WAVELENGTH"], self.spec["FLUX"])
Fang Yuedong's avatar
Fang Yuedong committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

        beam_thr = thri(lam_intep)
        spec_sample = spci(lam_intep)

        bean_thr_spec = beam_thr * spec_sample

        # generate sensitivity file for aXe
        # ysensitivity = lam_beam * 0
        #
        # ysensitivity[lam_index] = interp.interp_conserve_c(lam_beam[lam_index], lam_intep,
        #                                                    beam_thr * math.pi * 100 * 100 * 1e-7 / (
        #                                                                cons.h.value * cons.c.value / (
        #                                                                    lam_intep * 1e-10)), integrate=0, left=0,
        #                                                    right=0)
        #
        # self.writerSensitivityFile(conffile = self.grating_conf_file,  beam = beam, w = lam_beam[lam_index], sens = ysensitivity[lam_index])

Zhang Xin's avatar
Zhang Xin committed
200
201
202
        ysens[lam_index] = interp.interp_conserve_c(
            lam_beam[lam_index], lam_intep, bean_thr_spec, integrate=1, left=0, right=0
        )
Fang Yuedong's avatar
Fang Yuedong committed
203
204
205
206

        sensitivity_beam = ysens

        len_spec_x = len(dx)
Zhang Xin's avatar
Zhang Xin committed
207
208
        len_spec_y = int(
            abs(ceil(ytrace_beam[-1]) - floor(ytrace_beam[0])) + 1)
Fang Yuedong's avatar
Fang Yuedong committed
209
210
211
212
213
214
215
216
217

        beam_sh = (self.img_sh[0] + len_spec_y, self.img_sh[1] + len_spec_x)
        modelf = zeros(product(beam_sh), dtype=float)
        model = modelf.reshape(beam_sh)
        idx = np.arange(modelf.size, dtype=int64).reshape(beam_sh)
        x0 = array((self.thumb_y, self.thumb_x), dtype=int64)

        dxpix = dx - dx[0] + x0[1]

Zhang Xin's avatar
Zhang Xin committed
218
        dyc = cast[int](np.floor(ytrace_beam + 0.5))
Fang Yuedong's avatar
Fang Yuedong committed
219

220
221
        # dypix = cast[int](np.floor(ytrace_beam - dyc[0] + x0[0] + 0.5))
        dypix = dyc - dyc[0] + x0[0]
Fang Yuedong's avatar
Fang Yuedong committed
222
223
224
225

        frac_ids = yfrac_beam < 0

        dypix[frac_ids] = dypix[frac_ids] - 1
Zhang Xin's avatar
Zhang Xin committed
226
        yfrac_beam[frac_ids] = 1 + yfrac_beam[frac_ids]
Fang Yuedong's avatar
Fang Yuedong committed
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

        flat_index = idx[dypix, dxpix]

        nonz = sensitivity_beam != 0

        origin_in = zeros_like(self.origin)
        dx0_in = dx[0]
        dy0_in = dyc[0]
        if self.isAlongY == 1:
            origin_in[0] = self.origin[0]
            origin_in[1] = self.origin[1] - len_spec_y
            dx0_in = -dyc[0]
            dy0_in = dx[0]
        else:
            origin_in[0] = self.origin[0]
            origin_in[1] = self.origin[1]
            dx0_in = dx[0]
            dy0_in = dyc[0]
            originOut_x = origin_in[1] + dx0_in
        originOut_y = origin_in[0] + dy0_in

        if self.flat_cube is None:
            beam_flat = None
        else:
            beam_flat = zeros([len(modelf), len(self.flat_cube)])

Zhang Xin's avatar
Zhang Xin committed
253
254
            sub_flat_cube = zeros(
                [len(self.flat_cube), beam_sh[0], beam_sh[1]])
Zhang Xin's avatar
Zhang Xin committed
255
            sub_flat_cube[0] = sub_flat_cube[0] + 1.0
Fang Yuedong's avatar
Fang Yuedong committed
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

            overlap_flag = 1

            sub_y_s = originOut_y
            sub_y_e = originOut_y + beam_sh[0] - 1
            sub_x_s = originOut_x
            sub_x_e = originOut_x + beam_sh[1] - 1

            beam_x_s = max(sub_x_s, 0)
            if beam_x_s > self.flat_cube[0].shape[1] - 1:
                overlap_flag = 0
            if overlap_flag == 1:
                beam_x_e = min(sub_x_e, self.flat_cube[0].shape[1] - 1)
                if beam_x_e < 0:
                    overlap_flag = 0

            if overlap_flag == 1:
                beam_y_s = max(sub_y_s, 0)
                if beam_y_s > self.flat_cube[0].shape[0] - 1:
                    overlap_flag = 0
            if overlap_flag == 1:
                beam_y_e = min(sub_y_e, self.flat_cube[0].shape[0] - 1)
                if beam_y_e < 0:
                    overlap_flag = 0

            if overlap_flag == 1:
Zhang Xin's avatar
Zhang Xin committed
282
283
                sub_flat_cube[
                    :,
Zhang Xin's avatar
Zhang Xin committed
284
285
286
                    beam_y_s - originOut_y: beam_y_e - originOut_y + 1,
                    beam_x_s - originOut_x: beam_x_e - originOut_x + 1,
                ] = self.flat_cube[:, beam_y_s: beam_y_e + 1, beam_x_s: beam_x_e + 1]
Fang Yuedong's avatar
Fang Yuedong committed
287
288
289

            for i in arange(0, len(self.flat_cube), 1):
                beam_flat[:, i] = sub_flat_cube[i].flatten()
Zhang Xin's avatar
Zhang Xin committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
        #            beam_flat = zeros([len(modelf), len(self.flat_cube)])
        #            flat_sh = self.flat_cube[0].shape
        #            for i in arange(0, beam_sh[0], 1):
        #                for j in arange(0, beam_sh[1], 1):
        #                    k = i * beam_sh[1] + j
        #                    if originOut_y + i >= flat_sh[0] or originOut_y + i < 0 or originOut_x + j>= flat_sh[1] or originOut_x+j < 0:
        #                        temp_bf = np.zeros_like(self.flat_cube[:, 0, 0])
        #                        temp_bf[0] = 1.0
        #                        beam_flat[k] = temp_bf
        #                    else:
        #                        beam_flat[k] = self.flat_cube[:, originOut_y + i, originOut_x + j]

        status = disperse.disperse_grism_object(
            self.thumb_img.astype(np.float32),
            flat_index[nonz],
            yfrac_beam[nonz],
            sensitivity_beam[nonz],
            modelf,
            x0,
            array(self.img_sh, dtype=int64),
            array(beam_sh, dtype=int64),
            beam_flat,
            lam_beam[lam_index][nonz],
        )
Fang Yuedong's avatar
Fang Yuedong committed
314
315

        model = modelf.reshape(beam_sh)
316
317
318
319
320
321
322
        # n1 = np.sum(np.isinf(model))
        # n2 = np.sum(np.isnan(model))
        # n3 = np.sum(np.isinf(modelf))
        # n4 = np.sum(np.isnan(modelf))
        # if n1>0 or n2 > 0:
        #     print("DEBUG: SpecDisperser, inf:%d, nan:%d--------%d,%d"%(n1, n2, n3, n4))
        #     print(dypix)
Zhang Xin's avatar
Zhang Xin committed
323
324
325
326
327
328
329
        # n1 = np.sum(np.isinf(self.thumb_img.astype(np.float32)))
        # n2 = np.sum(np.isnan(self.thumb_img.astype(np.float32)))
        # n3 = np.sum(np.isinf(yfrac_beam))
        # n4 = np.sum(np.isnan(yfrac_beam))
        # n5 = np.sum(np.isinf(sensitivity_beam))
        # n6 = np.sum(np.isnan(sensitivity_beam))
        # print("DEBUG: SpecDisperser, innput ---inf:%d, nan:%d, yfrac_beam:%d/%d, sensitivity_beam:%d/%d"%(n1, n2, n3, n4, n5, n6))
Fang Yuedong's avatar
Fang Yuedong committed
330
331
332
333
334
335
        self.beam_flux[beam] = sum(modelf)

        if self.isAlongY == 1:
            model, _, _ = rotate90(array_orig=model, isClockwise=0)
        return model, originOut_x, originOut_y, dxpix, dypix, lam_beam, ysens

Zhang Xin's avatar
Zhang Xin committed
336
337
    def writerSensitivityFile(self, conffile="", beam="", w=None, sens=None):
        orders = {"A": "1st", "B": "0st", "C": "2st", "D": "-1st", "E": "-2st"}
Zhang Xin's avatar
Zhang Xin committed
338
339
        sens_file_name = conffile[0:-5] + \
            "_sensitivity_" + orders[beam] + ".fits"
Wei Chengliang's avatar
Wei Chengliang committed
340
        if os.path.exists(sens_file_name) is False:
Zhang Xin's avatar
Zhang Xin committed
341
342
            senstivity_out = Table(
                array([w, sens]).T, names=("WAVELENGTH", "SENSITIVITY"))
Zhang Xin's avatar
Zhang Xin committed
343
            senstivity_out.write(sens_file_name, format="fits")
Fang Yuedong's avatar
Fang Yuedong committed
344
345
346
347
348
349
350


"""
Demonstrate aXe trace polynomials.
"""


Zhang Xin's avatar
Zhang Xin committed
351
352
class aXeConf:
    def __init__(self, conf_file="WFC3.IR.G141.V2.5.conf"):
Fang Yuedong's avatar
Fang Yuedong committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
        """Read an aXe-compatible configuration file

        Parameters
        ----------
        conf_file: str
            Filename of the configuration file to read

        """
        if conf_file is not None:
            self.conf = self.read_conf_file(conf_file)
            self.conf_file = conf_file
            self.count_beam_orders()

            # Global XOFF/YOFF offsets
Zhang Xin's avatar
Zhang Xin committed
367
368
            if "XOFF" in self.conf.keys():
                self.xoff = np.float(self.conf["XOFF"])
Fang Yuedong's avatar
Fang Yuedong committed
369
            else:
Zhang Xin's avatar
Zhang Xin committed
370
                self.xoff = 0.0
Fang Yuedong's avatar
Fang Yuedong committed
371

Zhang Xin's avatar
Zhang Xin committed
372
373
            if "YOFF" in self.conf.keys():
                self.yoff = np.float(self.conf["YOFF"])
Fang Yuedong's avatar
Fang Yuedong committed
374
            else:
Zhang Xin's avatar
Zhang Xin committed
375
                self.yoff = 0.0
Fang Yuedong's avatar
Fang Yuedong committed
376

Zhang Xin's avatar
Zhang Xin committed
377
    def read_conf_file(self, conf_file="WFC3.IR.G141.V2.5.conf"):
Fang Yuedong's avatar
Fang Yuedong committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
        """Read an aXe config file, convert floats and arrays

        Parameters
        ----------
        conf_file: str
            Filename of the configuration file to read.

        Parameters are stored in an OrderedDict in `self.conf`.
        """
        from collections import OrderedDict

        conf = OrderedDict()
        lines = open(conf_file).readlines()
        for line in lines:
            # empty / commented lines
Zhang Xin's avatar
Zhang Xin committed
393
            if (line.startswith("#")) | (line.strip() == "") | ('"' in line):
Fang Yuedong's avatar
Fang Yuedong committed
394
395
396
                continue

            # split the line, taking out ; and # comments
Zhang Xin's avatar
Zhang Xin committed
397
            spl = line.split(";")[0].split("#")[0].split()
Fang Yuedong's avatar
Fang Yuedong committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
            param = spl[0]
            if len(spl) > 2:
                value = np.cast[float](spl[1:])
            else:
                try:
                    value = float(spl[1])
                except:
                    value = spl[1]

            conf[param] = value

        return conf

    def count_beam_orders(self):
Zhang Xin's avatar
Zhang Xin committed
412
        """Get the maximum polynomial order in DYDX or DLDP for each beam"""
Fang Yuedong's avatar
Fang Yuedong committed
413
        self.orders = {}
Zhang Xin's avatar
Zhang Xin committed
414
        for beam in ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]:
Fang Yuedong's avatar
Fang Yuedong committed
415
            order = 0
Zhang Xin's avatar
Zhang Xin committed
416
            while "DYDX_{0:s}_{1:d}".format(beam, order) in self.conf.keys():
Fang Yuedong's avatar
Fang Yuedong committed
417
418
                order += 1

Zhang Xin's avatar
Zhang Xin committed
419
            while "DLDP_{0:s}_{1:d}".format(beam, order) in self.conf.keys():
Fang Yuedong's avatar
Fang Yuedong committed
420
421
422
423
424
                order += 1

            self.orders[beam] = order - 1

    def get_beams(self):
Zhang Xin's avatar
Zhang Xin committed
425
        """Get beam parameters and read sensitivity curves"""
Fang Yuedong's avatar
Fang Yuedong committed
426
427
428
429
430
431
432
433
434
435
436
437
        import os
        from collections import OrderedDict
        from astropy.table import Table, Column

        self.dxlam = OrderedDict()
        self.nx = OrderedDict()
        self.sens = OrderedDict()
        self.beams = []

        for beam in self.orders:
            if self.orders[beam] > 0:
                self.beams.append(beam)
Zhang Xin's avatar
Zhang Xin committed
438
439
440
                self.dxlam[beam] = np.arange(
                    self.conf["BEAM{0}".format(beam)].min(), self.conf["BEAM{0}".format(beam)].max(), dtype=int
                )
Zhang Xin's avatar
Zhang Xin committed
441
442
                self.nx[beam] = int(self.dxlam[beam].max() -
                                    self.dxlam[beam].min()) + 1
Fang Yuedong's avatar
Fang Yuedong committed
443
                self.sens[beam] = Table.read(
Zhang Xin's avatar
Zhang Xin committed
444
445
                    "{0}/{1}".format(os.path.dirname(self.conf_file),
                                     self.conf["SENSITIVITY_{0}".format(beam)])
Zhang Xin's avatar
Zhang Xin committed
446
                )
Fang Yuedong's avatar
Fang Yuedong committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
                # self.sens[beam].wave = np.cast[np.double](self.sens[beam]['WAVELENGTH'])
                # self.sens[beam].sens = np.cast[np.double](self.sens[beam]['SENSITIVITY'])

                # Need doubles for interpolating functions
                for col in self.sens[beam].colnames:
                    data = np.cast[np.double](self.sens[beam][col])
                    self.sens[beam].remove_column(col)
                    self.sens[beam].add_column(Column(data=data, name=col))

        self.beams.sort()

    def field_dependent(self, xi, yi, coeffs):
        """aXe field-dependent coefficients

        See the `aXe manual <http://axe.stsci.edu/axe/manual/html/node7.html#SECTION00721200000000000000>`_ for a description of how the field-dependent coefficients are specified.

        Parameters
        ----------
        xi, yi : float or array-like
            Coordinate to evaluate the field dependent coefficients, where
            `xi = x-REFX` and `yi = y-REFY`.

        coeffs : array-like
            Field-dependency coefficients

        Returns
        -------
        a : float or array-like
            Evaluated field-dependent coefficients

        """
        # number of coefficients for a given polynomial order
        # 1:1, 2:3, 3:6, 4:10, order:order*(order+1)/2
        if isinstance(coeffs, float):
            order = 1
        else:
            order = int(-1 + np.sqrt(1 + 8 * len(coeffs))) // 2

        # Build polynomial terms array
        # $a = a_0+a_1x_i+a_2y_i+a_3x_i^2+a_4x_iy_i+a_5yi^2+$ ...
        xy = []
        for p in range(order):
            for px in range(p + 1):
                # print 'x**%d y**%d' %(p-px, px)
                xy.append(xi ** (p - px) * yi ** (px))

        # Evaluate the polynomial, allowing for N-dimensional inputs
        a = np.sum((np.array(xy).T * coeffs).T, axis=0)

        return a

    def evaluate_dp(self, dx, dydx):
Zhang Xin's avatar
pep8    
Zhang Xin committed
499
        # """Evalate arc length along the trace given trace polynomial coefficients
Fang Yuedong's avatar
Fang Yuedong committed
500

Zhang Xin's avatar
pep8    
Zhang Xin committed
501
502
503
504
        # Parameters
        # ----------
        # dx : array-like
        #     x pixel to evaluate
Fang Yuedong's avatar
Fang Yuedong committed
505

Zhang Xin's avatar
pep8    
Zhang Xin committed
506
507
        # dydx : array-like
        #     Coefficients of the trace polynomial
Fang Yuedong's avatar
Fang Yuedong committed
508

Zhang Xin's avatar
pep8    
Zhang Xin committed
509
510
511
512
        # Returns
        # -------
        # dp : array-like
        #     Arc length along the trace at position `dx`.
Fang Yuedong's avatar
Fang Yuedong committed
513

Zhang Xin's avatar
pep8    
Zhang Xin committed
514
515
        # For `dydx` polynomial orders 0, 1 or 2, integrate analytically.
        # Higher orders must be integrated numerically.
Fang Yuedong's avatar
Fang Yuedong committed
516

Zhang Xin's avatar
pep8    
Zhang Xin committed
517
518
        # **Constant:**
        #     .. math:: dp = dx
Fang Yuedong's avatar
Fang Yuedong committed
519

Zhang Xin's avatar
pep8    
Zhang Xin committed
520
521
        # **Linear:**
        #     .. math:: dp = \sqrt{1+\mathrm{DYDX}[1]}\cdot dx
Fang Yuedong's avatar
Fang Yuedong committed
522

Zhang Xin's avatar
pep8    
Zhang Xin committed
523
524
        # **Quadratic:**
        #     .. math:: u = \mathrm{DYDX}[1] + 2\ \mathrm{DYDX}[2]\cdot dx
Fang Yuedong's avatar
Fang Yuedong committed
525

Zhang Xin's avatar
pep8    
Zhang Xin committed
526
        #     .. math:: dp = (u \sqrt{1+u^2} + \mathrm{arcsinh}\ u) / (4\cdot \mathrm{DYDX}[2])
Fang Yuedong's avatar
Fang Yuedong committed
527

Zhang Xin's avatar
pep8    
Zhang Xin committed
528
        # """
Fang Yuedong's avatar
Fang Yuedong committed
529
530
531
532
        # dp is the arc length along the trace
        # $\lambda = dldp_0 + dldp_1 dp + dldp_2 dp^2$ ...

        poly_order = len(dydx) - 1
Zhang Xin's avatar
Zhang Xin committed
533
        if poly_order == 2:
Fang Yuedong's avatar
Fang Yuedong committed
534
535
536
537
538
539
540
541
542
            if np.abs(np.unique(dydx[2])).max() == 0:
                poly_order = 1

        if poly_order == 0:  # dy=0
            dp = dx
        elif poly_order == 1:  # constant dy/dx
            dp = np.sqrt(1 + dydx[1] ** 2) * (dx)
        elif poly_order == 2:  # quadratic trace
            u0 = dydx[1] + 2 * dydx[2] * (0)
Zhang Xin's avatar
Zhang Xin committed
543
            dp0 = (u0 * np.sqrt(1 + u0**2) + np.arcsinh(u0)) / (4 * dydx[2])
Fang Yuedong's avatar
Fang Yuedong committed
544
            u = dydx[1] + 2 * dydx[2] * (dx)
Zhang Xin's avatar
Zhang Xin committed
545
            dp = (u * np.sqrt(1 + u**2) + np.arcsinh(u)) / (4 * dydx[2]) - dp0
Fang Yuedong's avatar
Fang Yuedong committed
546
547
548
549
550
551
552
553
554
555
556
        else:
            # high order shape, numerical integration along trace
            # (this can be slow)
            xmin = np.minimum((dx).min(), 0)
            xmax = np.maximum((dx).max(), 0)
            xfull = np.arange(xmin, xmax)
            dyfull = 0
            for i in range(1, poly_order):
                dyfull += i * dydx[i] * (xfull - 0.5) ** (i - 1)

            # Integrate from 0 to dx / -dx
Zhang Xin's avatar
Zhang Xin committed
557
            dpfull = xfull * 0.0
Fang Yuedong's avatar
Fang Yuedong committed
558
559
            lt0 = xfull < 0
            if lt0.sum() > 1:
Zhang Xin's avatar
Zhang Xin committed
560
561
                dpfull[lt0] = np.cumsum(
                    np.sqrt(1 + dyfull[lt0][::-1] ** 2))[::-1]
Fang Yuedong's avatar
Fang Yuedong committed
562
563
564
565
566
567
568
569
570
571
572
573
574
                dpfull[lt0] *= -1

            #
            gt0 = xfull > 0
            if gt0.sum() > 0:
                dpfull[gt0] = np.cumsum(np.sqrt(1 + dyfull[gt0] ** 2))

            dp = np.interp(dx, xfull, dpfull)
            if dp[-1] == dp[-2]:
                dp[-1] = dp[-2] + np.diff(dp)[-2]

        return dp

Zhang Xin's avatar
Zhang Xin committed
575
    def get_beam_trace(self, x=507, y=507, dx=0.0, beam="A"):
Fang Yuedong's avatar
Fang Yuedong committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
        """Get an aXe beam trace for an input reference pixel and list of output x pixels `dx`

        Parameters
        ----------
        x, y : float or array-like
            Evaluate trace definition at detector coordinates `x` and `y`.

        dx : float or array-like
            Offset in x pixels from `(x,y)` where to compute trace offset and
            effective wavelength

        beam : str
            Beam name (i.e., spectral order) to compute.  By aXe convention,
            `beam='A'` is the first order, 'B' is the zeroth order and
            additional beams are the higher positive and negative orders.

        Returns
        -------
        dy : float or array-like
            Center of the trace in y pixels offset from `(x,y)` evaluated at
            `dx`.

        lam : float or array-like
            Effective wavelength along the trace evaluated at `dx`.

        """
        NORDER = self.orders[beam] + 1

        xi, yi = x - self.xoff, y - self.yoff
Zhang Xin's avatar
Zhang Xin committed
605
606
607
608
        xoff_beam = self.field_dependent(
            xi, yi, self.conf["XOFF_{0}".format(beam)])
        yoff_beam = self.field_dependent(
            xi, yi, self.conf["YOFF_{0}".format(beam)])
Fang Yuedong's avatar
Fang Yuedong committed
609
610
611
612
613
614

        # y offset of trace (DYDX)
        dydx = np.zeros(NORDER)  # 0 #+1.e-80
        dydx = [0] * NORDER

        for i in range(NORDER):
Zhang Xin's avatar
Zhang Xin committed
615
616
            if "DYDX_{0:s}_{1:d}".format(beam, i) in self.conf.keys():
                coeffs = self.conf["DYDX_{0:s}_{1:d}".format(beam, i)]
Fang Yuedong's avatar
Fang Yuedong committed
617
618
619
620
621
622
623
624
625
626
627
628
629
                dydx[i] = self.field_dependent(xi, yi, coeffs)

        # $dy = dydx_0+dydx_1 dx+dydx_2 dx^2+$ ...

        dy = yoff_beam
        for i in range(NORDER):
            dy += dydx[i] * (dx - xoff_beam) ** i

        # wavelength solution
        dldp = np.zeros(NORDER)
        dldp = [0] * NORDER

        for i in range(NORDER):
Zhang Xin's avatar
Zhang Xin committed
630
631
            if "DLDP_{0:s}_{1:d}".format(beam, i) in self.conf.keys():
                coeffs = self.conf["DLDP_{0:s}_{1:d}".format(beam, i)]
Fang Yuedong's avatar
Fang Yuedong committed
632
633
                dldp[i] = self.field_dependent(xi, yi, coeffs)

Zhang Xin's avatar
Zhang Xin committed
634
635
636
637
638
639
640
641
642
643
        self.eval_input = {"x": x, "y": y, "beam": beam, "dx": dx}
        self.eval_output = {
            "xi": xi,
            "yi": yi,
            "dldp": dldp,
            "dydx": dydx,
            "xoff_beam": xoff_beam,
            "yoff_beam": yoff_beam,
            "dy": dy,
        }
Fang Yuedong's avatar
Fang Yuedong committed
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680

        dp = self.evaluate_dp(dx - xoff_beam, dydx)
        # ## dp is the arc length along the trace
        # ## $\lambda = dldp_0 + dldp_1 dp + dldp_2 dp^2$ ...
        # if self.conf['DYDX_ORDER_%s' %(beam)] == 0:   ## dy=0
        #     dp = dx-xoff_beam
        # elif self.conf['DYDX_ORDER_%s' %(beam)] == 1: ## constant dy/dx
        #     dp = np.sqrt(1+dydx[1]**2)*(dx-xoff_beam)
        # elif self.conf['DYDX_ORDER_%s' %(beam)] == 2: ## quadratic trace
        #     u0 = dydx[1]+2*dydx[2]*(0)
        #     dp0 = (u0*np.sqrt(1+u0**2)+np.arcsinh(u0))/(4*dydx[2])
        #     u = dydx[1]+2*dydx[2]*(dx-xoff_beam)
        #     dp = (u*np.sqrt(1+u**2)+np.arcsinh(u))/(4*dydx[2])-dp0
        # else:
        #     ## high order shape, numerical integration along trace
        #     ## (this can be slow)
        #     xmin = np.minimum((dx-xoff_beam).min(), 0)
        #     xmax = np.maximum((dx-xoff_beam).max(), 0)
        #     xfull = np.arange(xmin, xmax)
        #     dyfull = 0
        #     for i in range(1, NORDER):
        #         dyfull += i*dydx[i]*(xfull-0.5)**(i-1)
        #
        #     ## Integrate from 0 to dx / -dx
        #     dpfull = xfull*0.
        #     lt0 = xfull <= 0
        #     if lt0.sum() > 1:
        #         dpfull[lt0] = np.cumsum(np.sqrt(1+dyfull[lt0][::-1]**2))[::-1]
        #         dpfull[lt0] *= -1
        #     #
        #     gt0 = xfull >= 0
        #     if gt0.sum() > 0:
        #         dpfull[gt0] = np.cumsum(np.sqrt(1+dyfull[gt0]**2))
        #
        #     dp = np.interp(dx-xoff_beam, xfull, dpfull)

        # Evaluate dldp
Zhang Xin's avatar
Zhang Xin committed
681
        lam = dp * 0.0
Fang Yuedong's avatar
Fang Yuedong committed
682
        for i in range(NORDER):
Zhang Xin's avatar
Zhang Xin committed
683
            lam += dldp[i] * dp**i
Fang Yuedong's avatar
Fang Yuedong committed
684
685
686

        return dy, lam

Zhang Xin's avatar
Zhang Xin committed
687
    def show_beams(self, beams=["E", "D", "C", "B", "A"]):
Fang Yuedong's avatar
Fang Yuedong committed
688
689
690
691
692
693
694
695
        """
        Make a demo plot of the beams of a given configuration file
        """
        import matplotlib.pyplot as plt

        x0, x1 = 507, 507
        dx = np.arange(-800, 1200)

Zhang Xin's avatar
Zhang Xin committed
696
        if "WFC3.UV" in self.conf_file:
Fang Yuedong's avatar
Fang Yuedong committed
697
698
            x0, x1 = 2073, 250
            dx = np.arange(-1200, 1200)
Zhang Xin's avatar
Zhang Xin committed
699
        if "G800L" in self.conf_file:
Fang Yuedong's avatar
Fang Yuedong committed
700
701
702
703
704
            x0, x1 = 2124, 1024
            dx = np.arange(-1200, 1200)

        s = 200  # marker size
        fig = plt.figure(figsize=[10, 3])
Zhang Xin's avatar
Zhang Xin committed
705
706
        plt.scatter(0, 0, marker="s", s=s, color="black",
                    edgecolor="0.8", label="Direct")
Fang Yuedong's avatar
Fang Yuedong committed
707
708

        for beam in beams:
Zhang Xin's avatar
Zhang Xin committed
709
            if "XOFF_{0}".format(beam) not in self.conf.keys():
Fang Yuedong's avatar
Fang Yuedong committed
710
711
                continue

Zhang Xin's avatar
Zhang Xin committed
712
713
            xoff = self.field_dependent(
                x0, x1, self.conf["XOFF_{0}".format(beam)])
Fang Yuedong's avatar
Fang Yuedong committed
714
            dy, lam = self.get_beam_trace(x0, x1, dx=dx, beam=beam)
Zhang Xin's avatar
Zhang Xin committed
715
            xlim = self.conf["BEAM{0}".format(beam)]
Fang Yuedong's avatar
Fang Yuedong committed
716
            ok = (dx >= xlim[0]) & (dx <= xlim[1])
Zhang Xin's avatar
Zhang Xin committed
717
718
719
720
721
722
            plt.scatter(dx[ok] + xoff, dy[ok], c=lam[ok] / 1.0e4,
                        marker="s", s=s, alpha=0.5, edgecolor="None")
            plt.text(np.median(dx[ok]), np.median(
                dy[ok]) + 1, beam, ha="center", va="center", fontsize=14)
            print("Beam {0}, lambda=({1:.1f} - {2:.1f})".format(beam,
                  lam[ok].min(), lam[ok].max()))
Fang Yuedong's avatar
Fang Yuedong committed
723
724

        plt.grid()
Zhang Xin's avatar
Zhang Xin committed
725
726
        plt.xlabel(r"$\Delta x$")
        plt.ylabel(r"$\Delta y$")
Fang Yuedong's avatar
Fang Yuedong committed
727
728

        cb = plt.colorbar(pad=0.01, fraction=0.05)
Zhang Xin's avatar
Zhang Xin committed
729
        cb.set_label(r"$\lambda\,(\mu\mathrm{m})$")
Fang Yuedong's avatar
Fang Yuedong committed
730
731
        plt.title(self.conf_file)
        plt.tight_layout()
Zhang Xin's avatar
Zhang Xin committed
732
        plt.savefig("{0}.pdf".format(self.conf_file))
Fang Yuedong's avatar
Fang Yuedong committed
733
734

    # def load_grism_config(conf_file):
Zhang Xin's avatar
Zhang Xin committed
735
736


Fang Yuedong's avatar
Fang Yuedong committed
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#     """Load parameters from an aXe configuration file

#     Parameters
#     ----------
#     conf_file : str
#         Filename of the configuration file

#     Returns
#     -------
#     conf : `~grizli.grismconf.aXeConf`
#         Configuration file object.  Runs `conf.get_beams()` to read the
#         sensitivity curves.
#     """
#     conf = aXeConf(conf_file)
#     conf.get_beams()
#     return conf