ObservationSim.py 25 KB
Newer Older
1
import os
Fang Yuedong's avatar
Fang Yuedong committed
2
3
4
5
6
import numpy as np
import mpi4py.MPI as MPI
import galsim
import logging
import psutil
Fang Yuedong's avatar
Fang Yuedong committed
7
import gc
8
9
10
from astropy.io import fits
from datetime import datetime

Fang Yuedong's avatar
Fang Yuedong committed
11
12
import traceback

13
14
15
from ObservationSim.Config import config_dir, ChipOutput
from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader
from ObservationSim.Instrument import Telescope, Filter, FilterParam, FocalPlane, Chip
16
from ObservationSim.Instrument.Chip import Effects
17
18
19
from ObservationSim.MockObject import calculateSkyMap_split_g
from ObservationSim.PSF import PSFGauss, FieldDistortion, PSFInterp
from ObservationSim._util import get_shear_field, makeSubDir_PointingList
20
from ObservationSim.Astrometry.Astrometry_util import on_orbit_obs_position
Fang Yuedong's avatar
Fang Yuedong committed
21

22
23
# import tracemalloc

Fang Yuedong's avatar
Fang Yuedong committed
24
class Observation(object):
25
    def __init__(self, config, Catalog, work_dir=None, data_dir=None):
26
        self.path_dict = config_dir(config=config, work_dir=work_dir, data_dir=data_dir)
Fang Yuedong's avatar
Fang Yuedong committed
27
        self.config = config
Fang Yuedong's avatar
Fang Yuedong committed
28
        self.tel = Telescope()
29
        self.focal_plane = FocalPlane(survey_type=self.config["obs_setting"]["survey_type"]) 
Fang Yuedong's avatar
Fang Yuedong committed
30
        self.filter_param = FilterParam() 
Fang Yuedong's avatar
Fang Yuedong committed
31
32
        self.chip_list = []
        self.filter_list = []
33
        self.all_filter = []
34
        self.Catalog = Catalog
Fang Yuedong's avatar
Fang Yuedong committed
35
36

        # if we want to apply field distortion?
Fang Yuedong's avatar
Fang Yuedong committed
37
        if self.config["ins_effects"]["field_dist"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
38
            self.fd_model = FieldDistortion(fdModel_path=self.path_dict["fd_path"])
Fang Yuedong's avatar
Fang Yuedong committed
39
40
41
42
43
44
45
46
47
        else:
            self.fd_model = None

        # Construct chips & filters:
        nchips = self.focal_plane.nchip_x*self.focal_plane.nchip_y
        for i in range(nchips):
            chipID = i + 1

            # Make Chip & Filter lists
48
49
            chip = Chip(
                chipID=chipID, 
Fang Yuedong's avatar
Fang Yuedong committed
50
                config=self.config)
Fang Yuedong's avatar
Fang Yuedong committed
51
            filter_id, filter_type = chip.getChipFilter()
52
53
            filt = Filter(filter_id=filter_id, 
                filter_type=filter_type, 
54
                filter_param=self.filter_param)
55
56
57
58
            if not self.focal_plane.isIgnored(chipID=chipID):
                self.chip_list.append(chip)
                self.filter_list.append(filt)
            self.all_filter.append(filt)
Fang Yuedong's avatar
Fang Yuedong committed
59
60

        # Read catalog and shear(s)
61
        self.g1_field, self.g2_field, self.nshear = get_shear_field(config=self.config)
Fang Yuedong's avatar
Fang Yuedong committed
62

63
    def run_one_chip(self, chip, filt, pointing, chip_output, wcs_fp=None, psf_model=None, shear_cat_file=None, cat_dir=None, sed_dir=None):
Fang Yuedong's avatar
Fang Yuedong committed
64

65
66
        # print(':::::::::::::::::::Current Pointing Information::::::::::::::::::')
        # print("RA: %f, DEC; %f" % (pointing.ra, pointing.dec))
Fang Yuedong's avatar
Fang Yuedong committed
67
        # print("Time: %s" % datetime.utcfromtimestamp(pointing.timestamp).isoformat())
68
69
70
71
72
73
74
75
        # print("Exposure time: %f" % pointing.exp_time)
        # print("Satellite Position (x, y, z): (%f, %f, %f)" % (pointing.sat_x, pointing.sat_y, pointing.sat_z))
        # print("Satellite Velocity (x, y, z): (%f, %f, %f)" % (pointing.sat_vx, pointing.sat_vy, pointing.sat_vz))
        # print("Position Angle: %f" % pointing.img_pa.deg)
        # print('Chip : %d' % chip.chipID)
        # print(':::::::::::::::::::::::::::END:::::::::::::::::::::::::::::::::::')
        chip_output.logger.info(':::::::::::::::::::Current Pointing Information::::::::::::::::::')
        chip_output.logger.info("RA: %f, DEC; %f" % (pointing.ra, pointing.dec))
Fang Yuedong's avatar
Fang Yuedong committed
76
        chip_output.logger.info("Time: %s" % datetime.utcfromtimestamp(pointing.timestamp).isoformat())
77
78
79
80
81
82
        chip_output.logger.info("Exposure time: %f" % pointing.exp_time)
        chip_output.logger.info("Satellite Position (x, y, z): (%f, %f, %f)" % (pointing.sat_x, pointing.sat_y, pointing.sat_z))
        chip_output.logger.info("Satellite Velocity (x, y, z): (%f, %f, %f)" % (pointing.sat_vx, pointing.sat_vy, pointing.sat_vz))
        chip_output.logger.info("Position Angle: %f" % pointing.img_pa.deg)
        chip_output.logger.info('Chip : %d' % chip.chipID)
        chip_output.logger.info(':::::::::::::::::::::::::::END:::::::::::::::::::::::::::::::::::')
Fang Yuedong's avatar
Fang Yuedong committed
83

Fang Yuedong's avatar
Fang Yuedong committed
84
        if self.config["psf_setting"]["psf_model"] == "Gauss":
Fang Yuedong's avatar
Fang Yuedong committed
85
            psf_model = PSFGauss(chip=chip, psfRa=self.config["psf_setting"]["psf_rcont"])
Fang Yuedong's avatar
Fang Yuedong committed
86
        elif self.config["psf_setting"]["psf_model"] == "Interp":
Fang Yuedong's avatar
Fang Yuedong committed
87
            psf_model = PSFInterp(chip=chip, PSF_data_file=self.path_dict["psf_dir"])
Fang Yuedong's avatar
Fang Yuedong committed
88
        else:
89
90
            # print("unrecognized PSF model type!!", flush=True)
            chip_output.logger.error("unrecognized PSF model type!!", flush=True)
Fang Yuedong's avatar
Fang Yuedong committed
91
92
93

        # Get (extra) shear fields
        if shear_cat_file is not None:
94
            self.g1_field, self.g2_field, self.nshear = get_shear_field(config=self.config, shear_cat_file=shear_cat_file)
Fang Yuedong's avatar
Fang Yuedong committed
95

96
97
        # Apply astrometric simulation for pointing
        if self.config["obs_setting"]["enable_astrometric_model"]:
Fang Yuedong's avatar
Fang Yuedong committed
98
            dt = datetime.utcfromtimestamp(pointing.timestamp)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
            date_str = dt.date().isoformat()
            time_str = dt.time().isoformat()
            ra_cen, dec_cen = on_orbit_obs_position(
                input_ra_list=[pointing.ra],
                input_dec_list=[pointing.dec],
                input_pmra_list=[0.],
                input_pmdec_list=[0.],
                input_rv_list=[0.],
                input_parallax_list=[1e-9],
                input_nstars=1,
                input_x=pointing.sat_x,
                input_y=pointing.sat_y,
                input_z=pointing.sat_z,
                input_vx=pointing.sat_vx,
                input_vy=pointing.sat_vy,
                input_vz=pointing.sat_vz,
Fang Yuedong's avatar
Fang Yuedong committed
115
                input_epoch="J2000",
116
117
118
119
120
121
122
123
                input_date_str=date_str,
                input_time_str=time_str
            )
            ra_cen, dec_cen = ra_cen[0], dec_cen[0]
        else:
            ra_cen = pointing.ra
            dec_cen = pointing.dec

Fang Yuedong's avatar
Fang Yuedong committed
124
125
        # Get WCS for the focal plane
        if wcs_fp == None:
126
            wcs_fp = self.focal_plane.getTanWCS(ra_cen, dec_cen, pointing.img_pa, chip.pix_scale)
Fang Yuedong's avatar
Fang Yuedong committed
127
128
129
130
131
132
133

        # Create chip Image
        chip.img = galsim.ImageF(chip.npix_x, chip.npix_y)
        chip.img.setOrigin(chip.bound.xmin, chip.bound.ymin)
        chip.img.wcs = wcs_fp
        if chip.survey_type == "photometric":
            sky_map = None
134
135
        # elif chip.survey_type == "spectroscopic":
        #     sky_map = calculateSkyMap_split_g(xLen=chip.npix_x, yLen=chip.npix_y, blueLimit=filt.blue_limit, redLimit=filt.red_limit, skyfn=self.path_dict["sky_file"], conf=chip.sls_conf, pixelSize=chip.pix_scale, isAlongY=0)
Fang Yuedong's avatar
Fang Yuedong committed
136
        elif chip.survey_type == "spectroscopic":
xin's avatar
xin committed
137
            # chip.loadSLSFLATCUBE(flat_fn='flat_cube.fits')
138
139
            flat_normal = np.ones_like(chip.img.array)
            if self.config["ins_effects"]["flat_fielding"] == True:
140
141
142
143
144
                # print("SLS flat preprocess,CHIP %d : Creating and applying Flat-Fielding"%chip.chipID, flush=True)
                # print(chip.img.bounds, flush=True)
                chip_output.logger.info("SLS flat preprocess,CHIP %d : Creating and applying Flat-Fielding"%chip.chipID)
                msg = str(chip.img.bounds)
                chip_output.logger.info(msg)
145
146
147
148
149
                flat_img = Effects.MakeFlatSmooth(
                    chip.img.bounds,
                    int(self.config["random_seeds"]["seed_flat"]))
                flat_normal = flat_normal * flat_img.array / np.mean(flat_img.array)
            if self.config["ins_effects"]["shutter_effect"] == True:
150
151
                # print("SLS flat preprocess,CHIP %d : Apply shutter effect"%chip.chipID, flush=True)
                chip_output.logger.info("SLS flat preprocess,CHIP %d : Apply shutter effect"%chip.chipID)
152
153
154
155
                shuttimg = Effects.ShutterEffectArr(chip.img, t_shutter=1.3, dist_bearing=735,
                                                    dt=1E-3)  # shutter effect normalized image for this chip
                flat_normal = flat_normal*shuttimg
                flat_normal = np.array(flat_normal,dtype='float32')
Fang Yuedong's avatar
Fang Yuedong committed
156
            sky_map = calculateSkyMap_split_g(
157
158
159
160
161
162
163
164
                skyMap=flat_normal,
                blueLimit=filt.blue_limit,
                redLimit=filt.red_limit,
                conf=chip.sls_conf,
                pixelSize=chip.pix_scale,
                isAlongY=0,
                flat_cube=chip.flat_cube)
            # sky_map = np.ones([9216, 9232])
165
            del flat_normal
Fang Yuedong's avatar
Fang Yuedong committed
166

167
        if pointing.pointing_type == 'MS':
Fang Yuedong's avatar
Fang Yuedong committed
168
            # Load catalogues and templates
Fang Yuedong's avatar
Fang Yuedong committed
169
            self.cat = self.Catalog(config=self.config, chip=chip, pointing=pointing, cat_dir=cat_dir, sed_dir=sed_dir, chip_output=chip_output, filt=filt)
170
            chip_output.create_output_file()
Fang Yuedong's avatar
Fang Yuedong committed
171
172
            self.nobj = len(self.cat.objs)

Fang Yuedong's avatar
Fang Yuedong committed
173
174
175
176
177
178
179
180
181
            for ifilt in range(len(self.all_filter)):
                temp_filter = self.all_filter[ifilt]
                # Update the limiting magnitude using exposure time in pointing
                temp_filter.update_limit_saturation_mags(exptime=pointing.exp_time, chip=chip)

                # Select cutting band filter for saturation/limiting magnitude
                if temp_filter.filter_type.lower() == self.config["obs_setting"]["cut_in_band"].lower():
                    cut_filter = temp_filter

Fang Yuedong's avatar
Fang Yuedong committed
182
183
184
185
186
            # Loop over objects
            missed_obj = 0
            bright_obj = 0
            dim_obj = 0
            for j in range(self.nobj):
187
188
                
                # (DEBUG)
189
                # if j >= 10:
Xin Zhang's avatar
Xin Zhang committed
190
                #     break
191
                # tracemalloc.start()
192

Fang Yuedong's avatar
Fang Yuedong committed
193
                obj = self.cat.objs[j]
Fang Yuedong's avatar
Fang Yuedong committed
194

Wei Chengliang's avatar
Wei Chengliang committed
195
196
197
198
199
200
201
202
203
                #if obj.type == 'star' and self.config["run_option"]["galaxy_only"]:
                #    continue
                #elif obj.type == 'galaxy' and self.config["run_option"]["star_only"]:
                #    continue
                #elif obj.type == 'quasar' and self.config["run_option"]["star_only"]:
                #    continue

                ###mock_stamp_START
                if obj.type == 'star' and not self.config["run_option"]["star_yes"]:
204
                    continue
Wei Chengliang's avatar
Wei Chengliang committed
205
                elif obj.type == 'galaxy' and not self.config["run_option"]["galaxy_yes"]:
206
                    continue
Wei Chengliang's avatar
Wei Chengliang committed
207
                elif obj.type == 'quasar' and not self.config["run_option"]["galaxy_yes"]:
208
                    continue
Wei Chengliang's avatar
Wei Chengliang committed
209
210
211
212
213
214
215
216
217
                elif obj.type == 'stamp' and not self.config["run_option"]["stamp_yes"]:
                    continue
                #if obj.type == 'star' or obj.type == 'galaxy':
                #    continue #for test

                #mu_temp = 1.
                #if obj.type == 'stamp':
                #    mu_temp = obj.param["mu"]
                ###mock_stamp_END
Fang Yuedong's avatar
Fang Yuedong committed
218

219
                # load and convert SED; also caculate object's magnitude in all CSST bands
220
221
222
                try:
                    sed_data = self.cat.load_sed(obj)
                    norm_filt = self.cat.load_norm_filt(obj)
223
                    obj.sed, obj.param["mag_%s"%filt.filter_type], obj.param["flux_%s"%filt.filter_type] = self.cat.convert_sed(
224
225
226
227
228
                        mag=obj.param["mag_use_normal"],
                        sed=sed_data,
                        target_filt=filt, 
                        norm_filt=norm_filt,
                    )
229
                    _, obj.param["mag_%s"%cut_filter.filter_type], obj.param["flux_%s"%cut_filter.filter_type] = self.cat.convert_sed(
Fang Yuedong's avatar
Fang Yuedong committed
230
231
232
233
234
                        mag=obj.param["mag_use_normal"],
                        sed=sed_data,
                        target_filt=cut_filter, 
                        norm_filt=norm_filt,
                    )
235
                except Exception as e:
Fang Yuedong's avatar
Fang Yuedong committed
236
237
                    # print(e)
                    traceback.print_exc()
238
                    chip_output.logger.error(e)
239
                    continue
Fang Yuedong's avatar
Fang Yuedong committed
240
241
                # print("mag_%s = %.3f"%(filt.filter_type, obj.param["mag_%s"%filt.filter_type]))
                # chip_output.logger.info("mag_%s = %.3f"%(filt.filter_type, obj.param["mag_%s"%filt.filter_type]))
Fang Yuedong's avatar
Fang Yuedong committed
242
243

                # Exclude very bright/dim objects (for now)
244
                # if filt.is_too_bright(mag=obj.getMagFilter(filt)):
245
                # if filt.is_too_bright(mag=obj.mag_use_normal):
246
247
248
                if cut_filter.is_too_bright(
                    mag=obj.param["mag_%s"%self.config["obs_setting"]["cut_in_band"].lower()],
                    margin=self.config["obs_setting"]["mag_sat_margin"]):
Fang Yuedong's avatar
Fang Yuedong committed
249
                    # print("obj too birght!!", flush=True)
Fang Yuedong's avatar
Fang Yuedong committed
250
251
252
253
254
255
256
                    # if obj.type != 'galaxy':
                    #     bright_obj += 1
                    #     obj.unload_SED()
                    #     continue
                    bright_obj += 1
                    obj.unload_SED()
                    continue
257
258
259
                if filt.is_too_dim(
                    mag=obj.getMagFilter(filt),
                    margin=self.config["obs_setting"]["mag_lim_margin"]):
260
                # if cut_filter.is_too_dim(mag=obj.param["mag_%s"%self.config["obs_setting"]["cut_in_band"].lower()]):
Fang Yuedong's avatar
Fang Yuedong committed
261
262
                    # print("obj too dim!!", flush=True)
                    dim_obj += 1
Fang Yuedong's avatar
Fang Yuedong committed
263
                    obj.unload_SED()
Fang Yuedong's avatar
Fang Yuedong committed
264
                    # print(obj.getMagFilter(filt))
Fang Yuedong's avatar
Fang Yuedong committed
265
266
                    continue

Fang Yuedong's avatar
Fang Yuedong committed
267
                if self.config["shear_setting"]["shear_type"] == "constant":
Fang Yuedong's avatar
Fang Yuedong committed
268
                    if obj.type == 'star':
269
                        obj.g1, obj.g2 = 0., 0.
Fang Yuedong's avatar
Fang Yuedong committed
270
                    else:
271
                        obj.g1, obj.g2 = self.g1_field, self.g2_field
Fang Yuedong's avatar
Fang Yuedong committed
272
273
274
                elif self.config["shear_setting"]["shear_type"] == "extra":
                    try:
                        # TODO: every object with individual shear from input catalog(s)
275
                        obj.g1, obj.g2 = self.g1_field[j], self.g2_field[j]
Fang Yuedong's avatar
Fang Yuedong committed
276
                    except:
277
278
                        # print("failed to load external shear.")
                        chip_output.logger.error("failed to load external shear.")
Fang Yuedong's avatar
Fang Yuedong committed
279
                        pass
280
281
282
                elif self.config["shear_setting"]["shear_type"] == "catalog":
                    pass
                else:
283
                    chip_output.logger.error("Unknown shear input")
284
                    raise ValueError("Unknown shear input")
Fang Yuedong's avatar
Fang Yuedong committed
285

xin's avatar
xin committed
286
                header_wcs = generateExtensionHeader(
xin's avatar
xin committed
287
288
                    xlen=chip.npix_x,
                    ylen=chip.npix_y,
xin's avatar
xin committed
289
290
                    ra=ra_cen,
                    dec=dec_cen,
xin's avatar
xin committed
291
292
293
294
295
296
297
298
299
                    pa=pointing.img_pa.deg,
                    gain=chip.gain,
                    readout=chip.read_noise,
                    dark=chip.dark_noise,
                    saturation=90000,
                    psize=chip.pix_scale,
                    row_num=chip.rowID,
                    col_num=chip.colID,
                    extName='raw')
Fang Yuedong's avatar
Fang Yuedong committed
300

xin's avatar
xin committed
301
                pos_img, offset, local_wcs, real_wcs = obj.getPosImg_Offset_WCS(img=chip.img, fdmodel=self.fd_model, chip=chip, verbose=False, img_header=header_wcs)
Fang Yuedong's avatar
Fang Yuedong committed
302
                # pos_img, offset, local_wcs, real_wcs = obj.getPosImg_Offset_WCS(img=chip.img, fdmodel=self.fd_model, chip=chip, verbose=True, img_header=header_wcs)
Fang Yuedong's avatar
Fang Yuedong committed
303
304
                if pos_img.x == -1 or pos_img.y == -1:
                    # Exclude object which is outside the chip area (after field distortion)
Fang Yuedong's avatar
Fang Yuedong committed
305
306
307
                    # print('obj_ra = ', obj.ra, 'obj_dec = ', obj.dec, 'obj_ra_orig = ', obj.ra_orig, 'obj_dec_orig = ',obj.dec_orig)
                    # print("obj missed: %s"%(obj.id))
                    chip_output.logger.error("obj missed: %s"%(obj.id))
Fang Yuedong's avatar
Fang Yuedong committed
308
309
310
                    missed_obj += 1
                    obj.unload_SED()
                    continue
Fang Yuedong's avatar
Fang Yuedong committed
311

Fang Yuedong's avatar
Fang Yuedong committed
312
313
                # Draw object & update output catalog
                try:
314
                    # chip_output.logger.info("current filter type: %s"%filt.filter_type)
Fang Yuedong's avatar
Fang Yuedong committed
315
                    if self.config["run_option"]["out_cat_only"]:
Fang Yuedong's avatar
Fang Yuedong committed
316
                        isUpdated = True
317
318
                        obj.real_pos = obj.getRealPos(chip.img, global_x=obj.posImg.x, global_y=obj.posImg.y,
                                        img_real_wcs=obj.real_wcs)
319
                        pos_shear = 0.
320
                    elif chip.survey_type == "photometric" and not self.config["run_option"]["out_cat_only"]:
Fang Yuedong's avatar
Fang Yuedong committed
321
322
323
324
325
326
327
                        isUpdated, pos_shear = obj.drawObj_multiband(
                            tel=self.tel,
                            pos_img=pos_img, 
                            psf_model=psf_model, 
                            bandpass_list=filt.bandpass_sub_list, 
                            filt=filt, 
                            chip=chip, 
328
329
                            g1=obj.g1, 
                            g2=obj.g2, 
330
331
                            exptime=pointing.exp_time
                            )
Fang Yuedong's avatar
Fang Yuedong committed
332

Fang Yuedong's avatar
Fang Yuedong committed
333
                    elif chip.survey_type == "spectroscopic" and not self.config["run_option"]["out_cat_only"]:
Fang Yuedong's avatar
Fang Yuedong committed
334
335
336
337
338
339
340
                        isUpdated, pos_shear = obj.drawObj_slitless(
                            tel=self.tel, 
                            pos_img=pos_img, 
                            psf_model=psf_model, 
                            bandpass_list=filt.bandpass_sub_list, 
                            filt=filt, 
                            chip=chip, 
341
342
                            g1=obj.g1, 
                            g2=obj.g2, 
343
                            exptime=pointing.exp_time,
344
                            normFilter=norm_filt)
Fang Yuedong's avatar
Fang Yuedong committed
345
346
                    if isUpdated:
                        # TODO: add up stats
347
348
                        # print("updating output catalog...")
                        chip_output.cat_add_obj(obj, pos_img, pos_shear)
Fang Yuedong's avatar
Fang Yuedong committed
349
350
351
352
353
                        pass
                    else:
                        # print("object omitted", flush=True)
                        continue
                except Exception as e:
Fang Yuedong's avatar
Fang Yuedong committed
354
355
                    # print(e)
                    traceback.print_exc()
356
                    chip_output.logger.error(e)
Fang Yuedong's avatar
Fang Yuedong committed
357
                    pass
Fang Yuedong's avatar
Fang Yuedong committed
358
                    
359
                # # [C6 TEST]
Fang Yuedong's avatar
Fang Yuedong committed
360
361
362
                # print("check running:1: pointing-{:} chip-{:} pid-{:} memory-{:6.2}GB".format(pointing.id, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ), flush=True)
                # print('draw object %s'%obj.id)
                # print('mag = %.3f'%obj.param['mag_use_normal'])
363
364
365
366
367
                # # Output memory usage
                # snapshot = tracemalloc.take_snapshot()
                # top_stats = snapshot.statistics('lineno')
                # for stat in top_stats[:10]:
                #     print(stat)
Fang Yuedong's avatar
Fang Yuedong committed
368

Fang Yuedong's avatar
Fang Yuedong committed
369
370
371
                # Unload SED:
                obj.unload_SED()
                del obj
Fang Yuedong's avatar
Fang Yuedong committed
372
                gc.collect()
Fang Yuedong's avatar
Fang Yuedong committed
373

Fang Yuedong's avatar
Fang Yuedong committed
374
375
            del psf_model
            del self.cat
Fang Yuedong's avatar
Fang Yuedong committed
376
            gc.collect()
Fang Yuedong's avatar
Fang Yuedong committed
377

378
379
        # print("check running:1: pointing-{:} chip-{:} pid-{:} memory-{:6.2}GB".format(pointing.id, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ), flush=True)
        chip_output.logger.info("check running:1: pointing-%d chip-%d pid-%d memory-%6.2fGB"%(pointing.id, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ))
Fang Yuedong's avatar
Fang Yuedong committed
380
381
382

        # Detector Effects
        # ===========================================================
Fang Yuedong's avatar
Fang Yuedong committed
383
        # whether to output zero, dark, flat calibration images.
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

        if not self.config["run_option"]["out_cat_only"]:
            chip.img = chip.addEffects(
                config=self.config, 
                img=chip.img, 
                chip_output=chip_output, 
                filt=filt, 
                ra_cen=pointing.ra, 
                dec_cen=pointing.dec,
                img_rot=pointing.img_pa,
                pointing_ID=pointing.id,
                timestamp_obs=pointing.timestamp,
                pointing_type=pointing.pointing_type,
                sky_map=sky_map, tel = self.tel,
                logger=chip_output.logger)
            
            if pointing.pointing_type == 'MS':
Fang Yuedong's avatar
Fang Yuedong committed
401
                datetime_obs = datetime.utcfromtimestamp(pointing.timestamp)
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
                date_obs = datetime_obs.strftime("%y%m%d")
                time_obs = datetime_obs.strftime("%H%M%S")
                h_prim = generatePrimaryHeader(
                    xlen=chip.npix_x, 
                    ylen=chip.npix_y, 
                    pointNum = str(pointing.id),
                    ra=pointing.ra, 
                    dec=pointing.dec, 
                    psize=chip.pix_scale, 
                    row_num=chip.rowID, 
                    col_num=chip.colID,
                    date=date_obs,
                    time_obs=time_obs,
                    exptime=pointing.exp_time,
                    im_type='SCI',
                    sat_pos=[pointing.sat_x, pointing.sat_y, pointing.sat_z],
                    sat_vel=[pointing.sat_vx, pointing.sat_vy, pointing.sat_vz])
                h_ext = generateExtensionHeader(
                    xlen=chip.npix_x, 
                    ylen=chip.npix_y, 
                    ra=pointing.ra, 
                    dec=pointing.dec, 
                    pa=pointing.img_pa.deg, 
                    gain=chip.gain, 
                    readout=chip.read_noise, 
                    dark=chip.dark_noise, 
                    saturation=90000, 
                    psize=chip.pix_scale, 
                    row_num=chip.rowID, 
                    col_num=chip.colID,
                    extName='raw')
                chip.img = galsim.Image(chip.img.array, dtype=np.uint16)
                hdu1 = fits.PrimaryHDU(header=h_prim)
435
                hdu1.add_checksum()
436
                hdu2 = fits.ImageHDU(chip.img.array, header=h_ext)
437
                hdu2.add_checksum()
438
439
440
441
442
443
444
445
446
                hdu1 = fits.HDUList([hdu1, hdu2])
                fname = os.path.join(chip_output.subdir, h_prim['FILENAME'] + '.fits')
                hdu1.writeto(fname, output_verify='ignore', overwrite=True)
                # print("# objects that are too bright %d out of %d"%(bright_obj, self.nobj))
                # print("# objects that are too dim %d out of %d"%(dim_obj, self.nobj))
                # print("# objects that are missed %d out of %d"%(missed_obj, self.nobj))
                chip_output.logger.info("# objects that are too bright %d out of %d"%(bright_obj, self.nobj))
                chip_output.logger.info("# objects that are too dim %d out of %d"%(dim_obj, self.nobj))
                chip_output.logger.info("# objects that are missed %d out of %d"%(missed_obj, self.nobj))
Fang Yuedong's avatar
Fang Yuedong committed
447
448
        del chip.img

449
450
        # print("check running:2: pointing-{:} chip-{:} pid-{:} memory-{:6.2}GB".format(pointing.id, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ), flush=True)
        chip_output.logger.info("check running:2: pointing-%d chip-%d pid-%d memory-%6.2fGB"%(pointing.id, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ))
Fang Yuedong's avatar
Fang Yuedong committed
451

452
    def runExposure_MPI_PointingList(self, pointing_list, shear_cat_file=None, chips=None, use_mpi=False):
Fang Yuedong's avatar
Fang Yuedong committed
453
454
455
456
        if use_mpi:
            comm = MPI.COMM_WORLD
            ind_thread = comm.Get_rank()
            num_thread = comm.Get_size()
Fang Yuedong's avatar
Fang Yuedong committed
457

Fang Yuedong's avatar
Fang Yuedong committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
        if chips is None:
            nchips_per_fp = len(self.chip_list)
            run_chips = self.chip_list
            run_filts = self.filter_list
        else:
            # Only run a particular set of chips
            run_chips = []
            run_filts = []
            nchips_per_fp = len(chips)
            for ichip in range(len(self.chip_list)):
                chip = self.chip_list[ichip]
                filt = self.filter_list[ichip]
                if chip.chipID in chips:
                    run_chips.append(chip)
                    run_filts.append(filt)
Fang Yuedong's avatar
Fang Yuedong committed
473

474
        for ipoint in range(len(pointing_list)):
Fang Yuedong's avatar
Fang Yuedong committed
475
476
            for ichip in range(nchips_per_fp):
                i = ipoint*nchips_per_fp + ichip
477
478
                pointing = pointing_list[ipoint]
                pointing_ID = pointing.id
Fang Yuedong's avatar
Fang Yuedong committed
479
480
481
                if use_mpi:
                    if i % num_thread != ind_thread:
                        continue
Fang Yuedong's avatar
Fang Yuedong committed
482
483
484
485
486

                pid = os.getpid()

                sub_img_dir, prefix = makeSubDir_PointingList(path_dict=self.path_dict, config=self.config, pointing_ID=pointing_ID)

Fang Yuedong's avatar
Fang Yuedong committed
487
488
                chip = run_chips[ichip]
                filt = run_filts[ichip]
489
                # print("running pointing#%d, chip#%d, at PID#%d..."%(pointing_ID, chip.chipID, pid), flush=True)
Fang Yuedong's avatar
Fang Yuedong committed
490
491
492
493
494
                chip_output = ChipOutput(
                    config=self.config, 
                    focal_plane=self.focal_plane, 
                    chip=chip, 
                    filt=filt,  
495
496
                    exptime=pointing.exp_time,
                    pointing_type=pointing.pointing_type,
Fang Yuedong's avatar
Fang Yuedong committed
497
498
499
                    pointing_ID=pointing_ID,  
                    subdir=sub_img_dir,
                    prefix=prefix)
500
                chip_output.logger.info("running pointing#%d, chip#%d, at PID#%d..."%(pointing_ID, chip.chipID, pid))
501
                self.run_one_chip(
Fang Yuedong's avatar
Fang Yuedong committed
502
503
504
                    chip=chip, 
                    filt=filt, 
                    chip_output=chip_output, 
505
                    pointing=pointing,
Fang Yuedong's avatar
Fang Yuedong committed
506
                    cat_dir=self.path_dict["cat_dir"])
Zhang Xin's avatar
Zhang Xin committed
507
                print("finished running chip#%d..."%(chip.chipID), flush=True)
508
509
510
                chip_output.logger.info("finished running chip#%d..."%(chip.chipID))
                for handler in chip_output.logger.handlers[:]:
                    chip_output.logger.removeHandler(handler)
Fang Yuedong's avatar
Fang Yuedong committed
511
                gc.collect()