Galaxy.py 18.9 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
2
3
4
import numpy as np
import galsim
from astropy.table import Table

5
from ObservationSim.MockObject._util import eObs, integrate_sed_bandpass, getNormFactorForSpecWithABMAG
6
7
8
from ObservationSim.MockObject.SpecDisperser import SpecDisperser
from ObservationSim.MockObject.MockObject import MockObject

Fang Yuedong's avatar
Fang Yuedong committed
9
10
# import tracemalloc

Fang Yuedong's avatar
Fang Yuedong committed
11
class Galaxy(MockObject):
12
    def __init__(self, param, logger=None):
13
        super().__init__(param, logger=logger)
Fang Yuedong's avatar
Fang Yuedong committed
14

Fang Yuedong's avatar
Fang Yuedong committed
15
16
17
18
        if not hasattr(self, "disk_sersic_idx"):
            self.disk_sersic_idx = 1.
        if not hasattr(self, "bulge_sersic_idx"):
            self.bulge_sersic_idx = 4.
Fang Yuedong's avatar
Fang Yuedong committed
19
20
21
22
23
24

    def unload_SED(self):
        """(Test) free up SED memory
        """
        del self.sed

Fang Yuedong's avatar
Fang Yuedong committed
25
    def getGSObj_multiband(self, tel, psf_list, bandpass_list, filt, nphotons_tot=None, g1=0, g2=0, exptime=150., fd_shear=None):
Fang Yuedong's avatar
Fang Yuedong committed
26
27
28
29
30
31
32
33
34
35
36
        if len(psf_list) != len(bandpass_list):
            raise ValueError("!!!The number of PSF profiles and the number of bandpasses must be equal.")
        objs = []
        if nphotons_tot == None:
            nphotons_tot = self.getElectronFluxFilt(filt, tel, exptime)
        # print("nphotons_tot = ", nphotons_tot)

        try:
            full = integrate_sed_bandpass(sed=self.sed, bandpass=filt.bandpass_full)
        except Exception as e:
            print(e)
37
38
            if self.logger:
                self.logger.error(e)
Fang Yuedong's avatar
Fang Yuedong committed
39
40
41
42
43
44
45
            return -1
        for i in range(len(bandpass_list)):
            bandpass = bandpass_list[i]
            try:
                sub = integrate_sed_bandpass(sed=self.sed, bandpass=bandpass)
            except Exception as e:
                print(e)
46
47
                if self.logger:
                    self.logger.error(e)
Fang Yuedong's avatar
Fang Yuedong committed
48
49
50
51
52
53
54
55
56
                return -1
            
            ratio = sub/full
            if not (ratio == -1 or (ratio != ratio)):
                nphotons = ratio * nphotons_tot
            else:
                return -1

            psf = psf_list[i]
Fang Yuedong's avatar
Fang Yuedong committed
57
            disk = galsim.Sersic(n=self.disk_sersic_idx, half_light_radius=self.hlr_disk, flux=1.0)
Fang Yuedong's avatar
Fang Yuedong committed
58
59
            disk_shape = galsim.Shear(g1=self.e1_disk, g2=self.e2_disk)
            disk = disk.shear(disk_shape)
Fang Yuedong's avatar
Fang Yuedong committed
60
            bulge = galsim.Sersic(n=self.bulge_sersic_idx, half_light_radius=self.hlr_bulge, flux=1.0)
Fang Yuedong's avatar
Fang Yuedong committed
61
62
63
            bulge_shape = galsim.Shear(g1=self.e1_bulge, g2=self.e2_bulge)
            bulge = bulge.shear(bulge_shape)

64
65
66
67
68
69
            if self.bfrac == 0:
                gal = disk
            elif self.bfrac == 1:
                gal = bulge
            else:
                gal = self.bfrac * bulge + (1.0 - self.bfrac) * disk
Fang Yuedong's avatar
Fang Yuedong committed
70
            gal = gal.withFlux(nphotons)
71
72
73
            if fd_shear is not None:
                g1 += fd_shear.g1
                g2 += fd_shear.g2
Fang Yuedong's avatar
Fang Yuedong committed
74
75
76
            gal_shear = galsim.Shear(g1=g1, g2=g2)
            gal = gal.shear(gal_shear)
            gal = galsim.Convolve(psf, gal)
Fang Yuedong's avatar
Fang Yuedong committed
77
            
Fang Yuedong's avatar
Fang Yuedong committed
78
79
80
81
            objs.append(gal)
        final = galsim.Sum(objs)
        return final

Fang Yuedong's avatar
Fang Yuedong committed
82
    def drawObj_multiband(self, tel, pos_img, psf_model, bandpass_list, filt, chip, nphotons_tot=None, g1=0, g2=0, exptime=150., fd_shear=None):
Fang Yuedong's avatar
Fang Yuedong committed
83
84
85
86
87
88
89
90
        if nphotons_tot == None:
            nphotons_tot = self.getElectronFluxFilt(filt, tel, exptime)
        # print("nphotons_tot = ", nphotons_tot)

        try:
            full = integrate_sed_bandpass(sed=self.sed, bandpass=filt.bandpass_full)
        except Exception as e:
            print(e)
91
92
            if self.logger:
                self.logger.error(e)
Fang Yuedong's avatar
Fang Yuedong committed
93
94
95
            return 2, None
        
        # # [C6 TEST]
Fang Yuedong's avatar
Fang Yuedong committed
96
        # print('hlr_disk = %.4f, hlr_bulge = %.4f'%(self.hlr_disk, self.hlr_bulge))
Fang Yuedong's avatar
Fang Yuedong committed
97
98
        # tracemalloc.start()

Fang Yuedong's avatar
Fang Yuedong committed
99
        big_galaxy = False
Fang Yuedong's avatar
Fang Yuedong committed
100
        if self.hlr_disk > 3.0 or self.hlr_bulge > 3.0: # Very big galaxy
Fang Yuedong's avatar
Fang Yuedong committed
101
102
            big_galaxy = True

103
        # Set Galsim Parameters
Fang Yuedong's avatar
Fang Yuedong committed
104
105
106
107
108
109
        if self.getMagFilter(filt) <= 15 and (not big_galaxy):
            folding_threshold = 5.e-4
        else:
            folding_threshold = 5.e-3
        gsp = galsim.GSParams(folding_threshold=folding_threshold)

xin's avatar
xin committed
110
        self.real_pos = self.getRealPos(chip.img, global_x=self.posImg.x, global_y=self.posImg.y,
111
                                        img_real_wcs=self.chip_wcs)
xin's avatar
xin committed
112
113
114
115
116
117
118

        x, y = self.real_pos.x + 0.5, self.real_pos.y + 0.5
        x_nominal = int(np.floor(x + 0.5))
        y_nominal = int(np.floor(y + 0.5))
        dx = x - x_nominal
        dy = y - y_nominal
        offset = galsim.PositionD(dx, dy)
119
120
121
        # Get real local wcs of object (deal with chip rotation w.r.t its center)
        chip_wcs_local = self.chip_wcs.local(self.real_pos)
        is_updated = 0
xin's avatar
xin committed
122

123
        # Model the galaxy as disk + bulge
124
125
126
127
128
129
        disk = galsim.Sersic(n=self.disk_sersic_idx, half_light_radius=self.hlr_disk, flux=1.0, gsparams=gsp)
        disk_shape = galsim.Shear(g1=self.e1_disk, g2=self.e2_disk)
        disk = disk.shear(disk_shape)
        bulge = galsim.Sersic(n=self.bulge_sersic_idx, half_light_radius=self.hlr_bulge, flux=1.0, gsparams=gsp)
        bulge_shape = galsim.Shear(g1=self.e1_bulge, g2=self.e2_bulge)
        bulge = bulge.shear(bulge_shape)
130
        
131
        # Get shear and deal with shear induced by field distortion
132
133
134
135
        if fd_shear:
            g1 += fd_shear.g1
            g2 += fd_shear.g2
        gal_shear = galsim.Shear(g1=g1, g2=g2)
136

137
        # Loop over all sub-bandpasses
Fang Yuedong's avatar
Fang Yuedong committed
138
139
140
141
142
143
        for i in range(len(bandpass_list)):
            bandpass = bandpass_list[i]
            try:
                sub = integrate_sed_bandpass(sed=self.sed, bandpass=bandpass)
            except Exception as e:
                print(e)
144
145
                if self.logger:
                    self.logger.error(e)
Fang Yuedong's avatar
Fang Yuedong committed
146
147
148
149
150
151
                continue
            ratio = sub/full
            if not (ratio == -1 or (ratio != ratio)):
                nphotons = ratio * nphotons_tot
            else:
                continue
152
153
            
            # nphotons_sum += nphotons
Fang Yuedong's avatar
Fang Yuedong committed
154
            # # [C6 TEST]
Fang Yuedong's avatar
Fang Yuedong committed
155
156
            # print("nphotons_sub-band_%d = %.2f"%(i, nphotons))

157
            # Get PSF model
Fang Yuedong's avatar
Fang Yuedong committed
158
            psf, pos_shear = psf_model.get_PSF(chip=chip, pos_img=pos_img, bandpass=bandpass, folding_threshold=folding_threshold)
159
            
160
161
162
163
164
165
            if self.bfrac == 0:
                gal_temp = disk
            elif self.bfrac == 1:
                gal_temp = bulge
            else:
                gal_temp = self.bfrac * bulge + (1.0 - self.bfrac) * disk
166
            gal_temp = gal_temp.shear(gal_shear)
167
168
169
170
171
172
173
174
            gal_temp = gal_temp.withFlux(nphotons)
            if not big_galaxy: # Not apply PSF for very big galaxy
                gal_temp = galsim.Convolve(psf, gal_temp)
            
            if i == 0:
                gal = gal_temp
            else:
                gal = gal + gal_temp
Fang Yuedong's avatar
Fang Yuedong committed
175

Fang Yuedong's avatar
Fang Yuedong committed
176
            # (TEST) Random knots
177
178
179
            # knots = galsim.RandomKnots(npoints=100, profile=disk)
            # kfrac = np.random.random()*(1.0 - self.bfrac)
            # gal = self.bfrac * bulge + (1.0 - self.bfrac - kfrac) * disk + kfrac * knots
Fang Yuedong's avatar
Fang Yuedong committed
180

181
182
183
184
185
        # stamp = gal.drawImage(wcs=chip_wcs_local, method='phot', offset=offset, save_photons=True)
        stamp = gal.drawImage(wcs=chip_wcs_local, offset=offset)
        if np.sum(np.isnan(stamp.array)) > 0:
            # ERROR happens
            return 2, pos_shear
xin's avatar
xin committed
186
187
        stamp.setCenter(x_nominal, y_nominal)
        bounds = stamp.bounds & galsim.BoundsI(0, chip.npix_x - 1, 0, chip.npix_y - 1)
Fang Yuedong's avatar
Fang Yuedong committed
188
189
        if bounds.area() > 0:
            chip.img.setOrigin(0, 0)
190
191
            chip.img[bounds] += stamp[bounds]
            is_updated = 1
Fang Yuedong's avatar
Fang Yuedong committed
192
            chip.img.setOrigin(chip.bound.xmin, chip.bound.ymin)
193
194
195
            del stamp
        
        if is_updated == 0:
Fang Yuedong's avatar
Fang Yuedong committed
196
197
            # Return code 0: object photons missed this detector
            print("obj %s missed"%(self.id))
198
199
            if self.logger:
                self.logger.info("obj %s missed"%(self.id))
Fang Yuedong's avatar
Fang Yuedong committed
200
201
202
203
204
            return 0, pos_shear
        
        # # [C6 TEST]
        # print("nphotons_sum = ", nphotons_sum)
        return 1, pos_shear
Fang Yuedong's avatar
Fang Yuedong committed
205
206

    def drawObj_slitless(self, tel, pos_img, psf_model, bandpass_list, filt, chip, nphotons_tot=None, g1=0, g2=0,
Fang Yuedong's avatar
Fang Yuedong committed
207
208
209
210
211
212
213
214
215
216
217
                         exptime=150., normFilter=None, grating_split_pos=3685, fd_shear=None):
        if normFilter is not None:
            norm_thr_rang_ids = normFilter['SENSITIVITY'] > 0.001
            sedNormFactor = getNormFactorForSpecWithABMAG(ABMag=self.param['mag_use_normal'], spectrum=self.sed,
                                                        norm_thr=normFilter,
                                                        sWave=np.floor(normFilter[norm_thr_rang_ids][0][0]),
                                                        eWave=np.ceil(normFilter[norm_thr_rang_ids][-1][0]))
            if sedNormFactor == 0:
                return 2, None
        else:
            sedNormFactor = 1.
Fang Yuedong's avatar
Fang Yuedong committed
218
219
220
        normalSED = Table(np.array([self.sed['WAVELENGTH'], self.sed['FLUX'] * sedNormFactor]).T,
                          names=('WAVELENGTH', 'FLUX'))

xin's avatar
xin committed
221
        self.real_pos = self.getRealPos(chip.img, global_x=self.posImg.x, global_y=self.posImg.y,
222
                                        img_real_wcs=self.chip_wcs)
xin's avatar
xin committed
223
224
225
226
227
228
229
230

        x, y = self.real_pos.x + 0.5, self.real_pos.y + 0.5
        x_nominal = int(np.floor(x + 0.5))
        y_nominal = int(np.floor(y + 0.5))
        dx = x - x_nominal
        dy = y - y_nominal
        offset = galsim.PositionD(dx, dy)

231
        chip_wcs_local = self.chip_wcs.local(self.real_pos)
xin's avatar
xin committed
232
233


Fang Yuedong's avatar
Fang Yuedong committed
234
        big_galaxy = False
Fang Yuedong's avatar
Fang Yuedong committed
235
        if self.hlr_disk > 3.0 or self.hlr_bulge > 3.0: # Very big galaxy
Fang Yuedong's avatar
Fang Yuedong committed
236
237
238
239
240
241
242
243
            big_galaxy = True

        if self.getMagFilter(filt) <= 15 and (not big_galaxy):
            folding_threshold = 5.e-4
        else:
            folding_threshold = 5.e-3
        gsp = galsim.GSParams(folding_threshold=folding_threshold)
        # nphotons_sum = 0
244
245
246

        flat_cube = chip.flat_cube

Zhang Xin's avatar
Zhang Xin committed
247
        xOrderSigPlus = {'A':1.3909419820029296,'B':1.4760376591236062,'C':4.035447379743442,'D':5.5684364343742825,'E':16.260021029735388}
xin's avatar
xin committed
248
        grating_split_pos_chip = 0 + grating_split_pos
Zhang Xin's avatar
Zhang Xin committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

        branges = np.zeros([len(bandpass_list), 2])

        # print(hasattr(psf_model, 'bandranges'))

        if hasattr(psf_model, 'bandranges'):
            if psf_model.bandranges is None:
                return 2, None
            if len(psf_model.bandranges) != len(bandpass_list):
                return 2, None
            branges = psf_model.bandranges
        else:
            for i in range(len(bandpass_list)):
                branges[i, 0] = bandpass_list[i].blue_limit * 10
                branges[i, 1] = bandpass_list[i].red_limit * 10

Fang Yuedong's avatar
Fang Yuedong committed
265
        for i in range(len(bandpass_list)):
Zhang Xin's avatar
Zhang Xin committed
266
267
            # bandpass = bandpass_list[i]
            brange = branges[i]
Fang Yuedong's avatar
Fang Yuedong committed
268

Zhang Xin's avatar
Zhang Xin committed
269
            # psf, pos_shear = psf_model.get_PSF(chip=chip, pos_img=pos_img, bandpass=bandpass, folding_threshold=folding_threshold)
Fang Yuedong's avatar
Fang Yuedong committed
270
            disk = galsim.Sersic(n=self.disk_sersic_idx, half_light_radius=self.hlr_disk, flux=1.0, gsparams=gsp)
Fang Yuedong's avatar
Fang Yuedong committed
271
272
            disk_shape = galsim.Shear(g1=self.e1_disk, g2=self.e2_disk)
            disk = disk.shear(disk_shape)
Fang Yuedong's avatar
Fang Yuedong committed
273
            bulge = galsim.Sersic(n=self.bulge_sersic_idx, half_light_radius=self.hlr_bulge, flux=1.0, gsparams=gsp)
Fang Yuedong's avatar
Fang Yuedong committed
274
275
            bulge_shape = galsim.Shear(g1=self.e1_bulge, g2=self.e2_bulge)
            bulge = bulge.shear(bulge_shape)
276
277
278
279
280
281
282
            
            if self.bfrac == 0:
                gal = disk
            elif self.bfrac == 1:
                gal = bulge
            else:
                gal = self.bfrac * bulge + (1.0 - self.bfrac) * disk
Fang Yuedong's avatar
Fang Yuedong committed
283
284

            # (TEST) Random knots
285
286
287
            # knots = galsim.RandomKnots(npoints=100, profile=disk)
            # kfrac = np.random.random()*(1.0 - self.bfrac)
            # gal = self.bfrac * bulge + (1.0 - self.bfrac - kfrac) * disk + kfrac * knots
Fang Yuedong's avatar
Fang Yuedong committed
288

Fang Yuedong's avatar
Fang Yuedong committed
289
            gal = gal.withFlux(tel.pupil_area * exptime)
290
291
292
            if fd_shear:
                g1 += fd_shear.g1
                g2 += fd_shear.g2
Fang Yuedong's avatar
Fang Yuedong committed
293
294
            gal_shear = galsim.Shear(g1=g1, g2=g2)
            gal = gal.shear(gal_shear)
Zhang Xin's avatar
Zhang Xin committed
295
            # gal = galsim.Convolve(psf, gal)
Fang Yuedong's avatar
Fang Yuedong committed
296

Zhang Xin's avatar
Zhang Xin committed
297
298
299
300
            # if not big_galaxy: # Not apply PSF for very big galaxy
            #     gal = galsim.Convolve(psf, gal)
            #     # if fd_shear is not None:
            #     #     gal = gal.shear(fd_shear)
Fang Yuedong's avatar
Fang Yuedong committed
301

Zhang Xin's avatar
Zhang Xin committed
302
            starImg = gal.drawImage(wcs=chip_wcs_local, offset=offset,method = 'real_space')
Fang Yuedong's avatar
Fang Yuedong committed
303

xin's avatar
xin committed
304
305
            origin_star = [y_nominal - (starImg.center.y - starImg.ymin),
                           x_nominal - (starImg.center.x - starImg.xmin)]
Fang Yuedong's avatar
Fang Yuedong committed
306
            starImg.setOrigin(0, 0)
Xin Zhang's avatar
Xin Zhang committed
307
308
309
310
311
312
313
314
315
            gal_origin = [origin_star[0], origin_star[1]]
            gal_end = [origin_star[0] + starImg.array.shape[0] - 1, origin_star[1] + starImg.array.shape[1] - 1]

            if gal_origin[1] < grating_split_pos_chip < gal_end[1]:
                subSlitPos = int(grating_split_pos_chip - gal_origin[1] + 1)
                ## part img disperse

                subImg_p1 = starImg.array[:, 0:subSlitPos]
                star_p1 = galsim.Image(subImg_p1)
Fang Yuedong's avatar
Fang Yuedong committed
316
                star_p1.setOrigin(0, 0)
Xin Zhang's avatar
Xin Zhang committed
317
                origin_p1 = origin_star
xin's avatar
xin committed
318
319
                xcenter_p1 = min(x_nominal,grating_split_pos_chip-1) - 0
                ycenter_p1 = y_nominal-0
Xin Zhang's avatar
Xin Zhang committed
320
321
322
323

                sdp_p1 = SpecDisperser(orig_img=star_p1, xcenter=xcenter_p1,
                                    ycenter=ycenter_p1, origin=origin_p1,
                                    tar_spec=normalSED,
Zhang Xin's avatar
Zhang Xin committed
324
                                    band_start=brange[0], band_end=brange[1],
Xin Zhang's avatar
Xin Zhang committed
325
                                    conf=chip.sls_conf[0],
326
327
                                    isAlongY=0,
                                    flat_cube=flat_cube)
Xin Zhang's avatar
Xin Zhang committed
328

Zhang Xin's avatar
Zhang Xin committed
329
330
331
332
                # self.addSLStoChipImage(sdp=sdp_p1, chip=chip, xOrderSigPlus = xOrderSigPlus, local_wcs=chip_wcs_local)
                pos_shear = self.addSLStoChipImageWithPSF(sdp=sdp_p1, chip=chip, pos_img_local=[xcenter_p1, ycenter_p1],
                                                          psf_model=psf_model, bandNo=i + 1,
                                                          grating_split_pos=grating_split_pos,
Zhang Xin's avatar
Zhang Xin committed
333
                                                          local_wcs=chip_wcs_local, pos_img = pos_img)
Xin Zhang's avatar
Xin Zhang committed
334
335
336

                subImg_p2 = starImg.array[:, subSlitPos+1:starImg.array.shape[1]]
                star_p2 = galsim.Image(subImg_p2)
Fang Yuedong's avatar
Fang Yuedong committed
337
                star_p2.setOrigin(0, 0)
Xin Zhang's avatar
Xin Zhang committed
338
                origin_p2 = [origin_star[0], grating_split_pos_chip]
xin's avatar
xin committed
339
340
                xcenter_p2 = max(x_nominal, grating_split_pos_chip - 1) - 0
                ycenter_p2 = y_nominal - 0
Xin Zhang's avatar
Xin Zhang committed
341
342
343
344

                sdp_p2 = SpecDisperser(orig_img=star_p2, xcenter=xcenter_p2,
                                       ycenter=ycenter_p2, origin=origin_p2,
                                       tar_spec=normalSED,
Zhang Xin's avatar
Zhang Xin committed
345
                                       band_start=brange[0], band_end=brange[1],
Xin Zhang's avatar
Xin Zhang committed
346
                                       conf=chip.sls_conf[1],
347
348
                                       isAlongY=0,
                                       flat_cube=flat_cube)
Xin Zhang's avatar
Xin Zhang committed
349

Zhang Xin's avatar
Zhang Xin committed
350
351
352
353
                # self.addSLStoChipImage(sdp=sdp_p2, chip=chip, xOrderSigPlus = xOrderSigPlus, local_wcs=chip_wcs_local)
                pos_shear = self.addSLStoChipImageWithPSF(sdp=sdp_p2, chip=chip, pos_img_local=[xcenter_p2, ycenter_p2],
                                                          psf_model=psf_model, bandNo=i + 1,
                                                          grating_split_pos=grating_split_pos,
Zhang Xin's avatar
Zhang Xin committed
354
                                                          local_wcs=chip_wcs_local, pos_img = pos_img)
Xin Zhang's avatar
Xin Zhang committed
355
356
357
358

                del sdp_p1
                del sdp_p2
            elif grating_split_pos_chip<=gal_origin[1]:
xin's avatar
xin committed
359
360
                sdp = SpecDisperser(orig_img=starImg, xcenter=x_nominal - 0,
                                    ycenter=y_nominal - 0, origin=origin_star,
Xin Zhang's avatar
Xin Zhang committed
361
                                    tar_spec=normalSED,
Zhang Xin's avatar
Zhang Xin committed
362
                                    band_start=brange[0], band_end=brange[1],
Xin Zhang's avatar
Xin Zhang committed
363
                                    conf=chip.sls_conf[1],
364
365
                                    isAlongY=0,
                                    flat_cube=flat_cube)
Zhang Xin's avatar
Zhang Xin committed
366
367
368
369
                # self.addSLStoChipImage(sdp=sdp, chip=chip, xOrderSigPlus = xOrderSigPlus, local_wcs=chip_wcs_local)
                pos_shear = self.addSLStoChipImageWithPSF(sdp=sdp, chip=chip, pos_img_local=[x_nominal, y_nominal],
                                                          psf_model=psf_model, bandNo=i + 1,
                                                          grating_split_pos=grating_split_pos,
Zhang Xin's avatar
Zhang Xin committed
370
                                                          local_wcs=chip_wcs_local, pos_img = pos_img)
Xin Zhang's avatar
Xin Zhang committed
371
372
                del sdp
            elif grating_split_pos_chip>=gal_end[1]:
xin's avatar
xin committed
373
374
                sdp = SpecDisperser(orig_img=starImg, xcenter=x_nominal - 0,
                                    ycenter=y_nominal - 0, origin=origin_star,
Xin Zhang's avatar
Xin Zhang committed
375
                                    tar_spec=normalSED,
Zhang Xin's avatar
Zhang Xin committed
376
                                    band_start=brange[0], band_end=brange[1],
Xin Zhang's avatar
Xin Zhang committed
377
                                    conf=chip.sls_conf[0],
378
379
                                    isAlongY=0,
                                    flat_cube=flat_cube)
Zhang Xin's avatar
Zhang Xin committed
380
381
382
383
                # self.addSLStoChipImage(sdp=sdp, chip=chip, xOrderSigPlus = xOrderSigPlus, local_wcs=chip_wcs_local)
                pos_shear = self.addSLStoChipImageWithPSF(sdp=sdp, chip=chip, pos_img_local=[x_nominal, y_nominal],
                                                          psf_model=psf_model, bandNo=i + 1,
                                                          grating_split_pos=grating_split_pos,
Zhang Xin's avatar
Zhang Xin committed
384
                                                          local_wcs=chip_wcs_local, pos_img = pos_img)
Xin Zhang's avatar
Xin Zhang committed
385
                del sdp
Fang Yuedong's avatar
Fang Yuedong committed
386

Xin Zhang's avatar
Xin Zhang committed
387
            # print(self.y_nominal, starImg.center.y, starImg.ymin)
Zhang Xin's avatar
Zhang Xin committed
388
            # del psf
Fang Yuedong's avatar
Fang Yuedong committed
389
        return 1, pos_shear
Fang Yuedong's avatar
Fang Yuedong committed
390
391
392
393

    def getGSObj(self, psf, g1=0, g2=0, flux=None, filt=None, tel=None, exptime=150.):
        if flux == None:
            flux = self.getElectronFluxFilt(filt, tel, exptime)
Fang Yuedong's avatar
Fang Yuedong committed
394
        disk = galsim.Sersic(n=self.disk_sersic_idx, half_light_radius=self.hlr_disk, flux=1.0)
Fang Yuedong's avatar
Fang Yuedong committed
395
396
397
        disk_shape = galsim.Shear(g1=self.e1_disk, g2=self.e2_disk)
        disk = disk.shear(disk_shape)

Fang Yuedong's avatar
Fang Yuedong committed
398
        bulge = galsim.Sersic(n=self.bulge_sersic_idx, half_light_radius=self.hlr_bulge, flux=1.0)
Fang Yuedong's avatar
Fang Yuedong committed
399
400
401
402
403
404
405
406
407
408
409
410
        bulge_shape = galsim.Shear(g1=self.e1_bulge, g2=self.e2_bulge)
        bulge = bulge.shear(bulge_shape)

        gal = self.bfrac * bulge + (1.0 - self.bfrac) * disk
        gal = gal.withFlux(flux)
        gal_shear = galsim.Shear(g1=g1, g2=g2)
        gal = gal.shear(gal_shear)
        final = galsim.Convolve(psf, gal)
        return final

    def getObservedEll(self, g1=0, g2=0):
        e1_obs, e2_obs, e_obs, theta = eObs(self.e1_total, self.e2_total, g1, g2)
Zhang Xin's avatar
Zhang Xin committed
411
        return self.e1_total, self.e2_total, g1, g2, e1_obs, e2_obs