ChipOutput.py 8.01 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
import os
2
import logging
Fang Yuedong's avatar
Fang Yuedong committed
3
4

class ChipOutput(object):
Fang Yuedong's avatar
Fang Yuedong committed
5
    def __init__(self, config, focal_plane, chip, filt, imgKey0="", imgKey1="", imgKey2="", exptime=150., mjdTime="", ra_cen=None, dec_cen=None, pointing_type='MS', pointing_ID='0', subdir="./", prefix=""):
Fang Yuedong's avatar
Fang Yuedong committed
6
7
8
9
10
11
12
13
14
15
16
17
        self.focal_plane = focal_plane
        self.chip = chip
        self.filt = filt
        self.imgKey0 = imgKey0
        self.imgKey1 = imgKey1
        self.imgKey2 = imgKey2
        self.exptime = exptime
        self.mjdTime = mjdTime
        if (ra_cen is not None) and (dec_cen is not None):
            self.ra_cen = ra_cen
            self.dec_cen = dec_cen
        else:
Fang Yuedong's avatar
Fang Yuedong committed
18
19
            self.ra_cen = config["obs_setting"]["ra_center"]
            self.dec_cen = config["obs_setting"]["dec_center"]
Fang Yuedong's avatar
Fang Yuedong committed
20
21
22
        exp_name = imgKey0 + "_%s_%s.fits"
        self.chipLabel = focal_plane.getChipLabel(chip.chipID)
        self.img_name =  prefix + exp_name%(self.chipLabel, filt.filter_type)
Fang Yuedong's avatar
Fang Yuedong committed
23
        
24
25
        # self.cat_name = 'MSC_' +  config["obs_setting"]["date_obs"] + config["obs_setting"]["time_obs"] + "_" + str(pointing_ID).rjust(7, '0') + "_" + self.chipLabel.rjust(2,'0') + ".cat"

26
        self.cat_name = "MSC_1%s_chip_%s_filt_%s"%(str(pointing_ID).rjust(8, '0'), focal_plane.getChipLabel(chip.chipID), filt.filter_type) + ".cat"
27

Fang Yuedong's avatar
Fang Yuedong committed
28
29
        self.subdir = subdir

30
        # Setup logger for each chip
31
        logger_filename = "MSC_1%s_chip_%s_filt_%s"%(str(pointing_ID).rjust(8, '0'), focal_plane.getChipLabel(chip.chipID), filt.filter_type) + ".log"
32
33
34
35
        self.logger = logging.getLogger()
        fh = logging.FileHandler(os.path.join(self.subdir, logger_filename), mode='w+', encoding='utf-8')
        fh.setLevel(logging.DEBUG)
        self.logger.setLevel(logging.DEBUG)
Fang Yuedong's avatar
Fang Yuedong committed
36
        logging.getLogger('numba').setLevel(logging.WARNING)
37
38
        # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        formatter = logging.Formatter('%(asctime)s - %(msecs)d - %(levelname)-8s - [%(filename)s:%(lineno)d] - %(message)s')
39
40
41
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)

42
        hdr1  = "# obj_ID ID_chip filter xImage yImage ra dec ra_orig dec_orig z mag obj_type "
43
44
45
46
47
48
49
50
51
52
53
54
        hdr2  = "thetaR bfrac hlr_disk hlr_bulge e1_disk e2_disk e1_bulge e2_bulge g1 g2 "
        hdr3  = "sed_type av redden "
        hdr4 = "pm_ra pm_dec RV parallax\n"

        fmt1  = "%10d %4d %5s %10.3f %10.3f %15.8f %15.8f %15.8f %15.8f %7.4f %8.4f %15s "
        fmt2  = "%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f "
        fmt3 = "%2d %8.4f %8.4f "
        fmt4 = "%15.8f %15.8f %15.8f %15.8f\n"

        self.hdr = hdr1 + hdr2 + hdr3 + hdr4
        self.fmt = fmt1 + fmt2 + fmt3 + fmt4

55
        self.logger.info("pointing_type = %s\n"%(pointing_type))
Fang Yuedong's avatar
Fang Yuedong committed
56
57
        if pointing_type == 'MS':
            self.cat = open(os.path.join(self.subdir, self.cat_name), "w")
58
            self.logger.info("Creating catalog file %s ...\n"%(os.path.join(self.subdir, self.cat_name)))
Fang Yuedong's avatar
Fang Yuedong committed
59
            self.cat.write(self.hdr)
Fang Yuedong's avatar
Fang Yuedong committed
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    # def updateHDR(self, hdr):
    #     hdrNew = [{"name":"RDNOISE", "value":self.chip.read_noise,        "comment":"read noise in e-/pixel"},
    #             {"name":"DARK",    "value":self.chip.dark_noise,        "comment":"Dark noise (e-/pixel/s)"},
    #             {"name":"EXPTIME", "value":self.exptime,          "comment":"exposure time in second"},
    #             {"name":"GAIN",    "value":self.chip.gain,             "comment":"CCD gain in e-/ADU"},
    #             {"name":"SATURATE","value":65535.0,          "comment":"saturation level"},
    #             {"name":"CCDCHIP",  "value":int(self.chipLabel), "comment":"chip ID in the CCD mosaic"},
    #             {"name":"FILTER",  "value":self.filt.filter_type,          "comment":"filter name"},
    #             {"name":"MJD-OBS", "value":self.mjdTime,          "comment":"Modified Julian Date (MJD) of observation"},
    #             {"name":"DATE-OBS","value":self.imgKey1,          "comment":"Date of observation"},
    #             {"name":"EQUINOX", "value":2000.0},
    #             {"name":"RADECSYS","value":"ICRS"},
    #             {"name":"RA",      "value":self.ra_cen,           "comment":"telescope pointing center"},
    #             {"name":"DEC",     "value":self.dec_cen,          "comment":"telescope pointing center"},
    #             {"name":"OBJECT",  "value":"CSS-OS"},
    #             {"name":"WCSDIM",  "value":2.0,              "comment":"WCS Dimensionality"},
    #             {"name":"EXTNAME", "value":"IM1",            "comment":"Extension name"},
    #             {"name":"BSCALE",  "value":1.0},
    #             {"name":"BZERO",   "value":0.0},
    #             {"name":"OBSID",   "value":self.imgKey0,          "comment":"Observation ID"},
    #             {"name":"CCDNAME", "value":"ccd"+self.chipLabel,"comment":"CCD name"},
    #             {"name":"RSPEED",  "value":10.0,             "comment":"Read speed"},
    #             {"name":"CHIPTEMP","value":-100.0,           "comment":"Chip temperature"},
    #             {"name":"DATASEC", "value":"1:%d,1:%d"%(self.chip.npix_x,self.chip.npix_y), "comment":"Data section"},
    #             {"name":"CCDSUM",  "value":self.chip.npix_x*self.chip.npix_y,      "comment":"CCD pixel summing"},
    #             {"name":"NSUM",    "value":self.chip.npix_x*self.chip.npix_y,      "comment":"CCD pixel summing"},
    #             {"name":"AUTHOR",  "value":"CSST-Sim Group"},
    #             {"name":"GROUP",   "value":"Weak Lensing Working Group for CSST"}]
    #     for item in hdrNew:
    #         hdr.add_record(item)
    #     return hdr
Fang Yuedong's avatar
Fang Yuedong committed
92
93

    # def cat_add_obj(self, obj, pos_img, snr, pos_shear, g1, g2):
94
    def cat_add_obj(self, obj, pos_img, pos_shear):
Fang Yuedong's avatar
Fang Yuedong committed
95
96
        ximg = pos_img.x - self.chip.bound.xmin + 1.0
        yimg = pos_img.y - self.chip.bound.ymin + 1.0
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        # if obj.type == 'galaxy':
        #     line = self.fmt%(obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.z, obj.getMagFilter(self.filt), obj.param["star"], obj.thetaR, obj.bfrac, obj.hlr_disk, obj.hlr_bulge,
        #         obj.e1_disk, obj.e2_disk, obj.e1_bulge, obj.e2_bulge,
        #         pos_shear.g1, pos_shear.g2, e1, e2, g1, g2, e1OBS, e2OBS, obj.sed_type, obj.param['av'], obj.param['redden'], 'n', 0, 0, 0)
        # elif obj.type == "quasar":
        #     line = self.fmt % (obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.z,
        #                        obj.getMagFilter(self.filt), obj.param["star"], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        #                        pos_shear.g1, pos_shear.g2, e1, e2, g1, g2, e1OBS, e2OBS, obj.sed_type, obj.param['av'], obj.param['redden'], 'n', 0.0, 0.0, 0.0)
        # else:
        #     line = self.fmt%(obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.z, obj.getMagFilter(self.filt), obj.param["star"], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
        #         pos_shear.g1, pos_shear.g2, e1, e2, g1, g2, e1OBS, e2OBS, 0, 0.0, 0.0, obj.param['model_tag'], obj.param['teff'], obj.param['logg'],obj.param['feh'])
        # print(
        #     obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.ra_orig, obj.dec_orig, obj.z, obj.getMagFilter(self.filt), obj.type, 
        #     obj.thetaR, obj.bfrac, obj.hlr_disk, obj.hlr_bulge, obj.e1_disk, obj.e2_disk, obj.e1_bulge, obj.e2_bulge, obj.g1, obj.g2,
        #     obj.sed_type, obj.av, obj.redden,
        #     obj.pmra, obj.pmdec, obj.rv, obj.parallax)
        
        line = self.fmt%(
            obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.ra_orig, obj.dec_orig, obj.z, obj.getMagFilter(self.filt), obj.type, 
            obj.thetaR, obj.bfrac, obj.hlr_disk, obj.hlr_bulge, obj.e1_disk, obj.e2_disk, obj.e1_bulge, obj.e2_bulge, obj.g1, obj.g2,
            obj.sed_type, obj.av, obj.redden,
            obj.pmra, obj.pmdec, obj.rv, obj.parallax)
Zhang Xin's avatar
Zhang Xin committed
119
        self.cat.write(line)