ChipOutput.py 4.23 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
        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
14
        self.pointing_type = pointing_type
Fang Yuedong's avatar
Fang Yuedong committed
15
16
17
18
        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
19
20
            self.ra_cen = config["obs_setting"]["ra_center"]
            self.dec_cen = config["obs_setting"]["dec_center"]
Fang Yuedong's avatar
Fang Yuedong committed
21
22
23
        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
24
        
25
26
        # 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"

27
        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"
28

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

31
        # Setup logger for each chip
32
        logger_filename = "MSC_1%s_chip_%s_filt_%s"%(str(pointing_ID).rjust(8, '0'), focal_plane.getChipLabel(chip.chipID), filt.filter_type) + ".log"
33
34
35
36
        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
37
        logging.getLogger('numba').setLevel(logging.WARNING)
38
39
        # 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')
40
41
42
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)

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

        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 "
51
        fmt4 = "%15.8f %15.8f %15.8f %15.8f"
52
53
54
55

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

56
        self.logger.info("pointing_type = %s\n"%(pointing_type))
57
58
59
60
61
62
63

    
    def update_ouptut_header(self, additional_column_names=""):
        self.hdr += additional_column_names
    
    def create_output_file(self):
        if self.pointing_type == 'MS':
Fang Yuedong's avatar
Fang Yuedong committed
64
            self.cat = open(os.path.join(self.subdir, self.cat_name), "w")
65
            self.logger.info("Creating catalog file %s ...\n"%(os.path.join(self.subdir, self.cat_name)))
66
67
            if not self.hdr.endswith("\n"):
                self.hdr += "\n"
Fang Yuedong's avatar
Fang Yuedong committed
68
            self.cat.write(self.hdr)
Fang Yuedong's avatar
Fang Yuedong committed
69

70
    def cat_add_obj(self, obj, pos_img, pos_shear):
xin's avatar
xin committed
71
72
73
74
        # ximg = pos_img.x - self.chip.bound.xmin + 1.0
        # yimg = pos_img.y - self.chip.bound.ymin + 1.0
        # print('-------------debug-----------------')
        # print('from global',ximg, yimg)
xin's avatar
xin committed
75
76
        ximg = obj.real_pos.x + 1.0
        yimg = obj.real_pos.y + 1.0
xin's avatar
xin committed
77
        # print('from loacl',ximg, yimg)
78
79
80
81
82
83
        
        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)
84
85
86
        line += obj.additional_output_str
        if not line.endswith("\n"):
            line += "\n"
Zhang Xin's avatar
Zhang Xin committed
87
        self.cat.write(line)