ChipOutput.py 3.55 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"]
21

Fang Yuedong's avatar
Fang Yuedong committed
22
23
        self.chipLabel = focal_plane.getChipLabel(chip.chipID)
        self.cat_name = "MSC_1%s_chip_%s_filt_%s"%(str(pointing_ID).rjust(8, '0'), self.chipLabel, filt.filter_type) + ".cat"
24

Fang Yuedong's avatar
Fang Yuedong committed
25
26
        self.subdir = subdir

27
        # Setup logger for each chip
Fang Yuedong's avatar
Fang Yuedong committed
28
        logger_filename = "MSC_1%s_chip_%s_filt_%s"%(str(pointing_ID).rjust(8, '0'), self.chipLabel, filt.filter_type) + ".log"
29
30
31
32
        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
33
        logging.getLogger('numba').setLevel(logging.WARNING)
34
        formatter = logging.Formatter('%(asctime)s - %(msecs)d - %(levelname)-8s - [%(filename)s:%(lineno)d] - %(message)s')
35
36
37
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)

38
        hdr1  = "# obj_ID ID_chip filter xImage yImage ra dec ra_orig dec_orig z mag obj_type "
Fang Yuedong's avatar
Fang Yuedong committed
39
40
41
42
43
        hdr2 = "pm_ra pm_dec RV parallax"
        fmt1  = "%20s %4d %5s %10.3f %10.3f %15.8f %15.8f %15.8f %15.8f %7.4f %8.4f %15s "
        fmt2 = "%15.8f %15.8f %15.8f %15.8f"
        self.hdr = hdr1 + hdr2
        self.fmt = fmt1 + fmt2
44

45
        self.logger.info("pointing_type = %s\n"%(pointing_type))
Fang Yuedong's avatar
Fang Yuedong committed
46
47
48
49
50
51
52
53
    
    def Log_info(self, message):
        print(message)
        self.logger.info(message)
    
    def Log_error(self, message):
        print(message)
        self.logger.error(message)
54
55
56
57
58
59
    
    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
60
            self.cat = open(os.path.join(self.subdir, self.cat_name), "w")
61
            self.logger.info("Creating catalog file %s ...\n"%(os.path.join(self.subdir, self.cat_name)))
62
63
            if not self.hdr.endswith("\n"):
                self.hdr += "\n"
Fang Yuedong's avatar
Fang Yuedong committed
64
            self.cat.write(self.hdr)
Fang Yuedong's avatar
Fang Yuedong committed
65

66
    def cat_add_obj(self, obj, pos_img, pos_shear):
xin's avatar
xin committed
67
68
        # ximg = pos_img.x - self.chip.bound.xmin + 1.0
        # yimg = pos_img.y - self.chip.bound.ymin + 1.0
Fang Yuedong's avatar
Fang Yuedong committed
69
70
        # self.logger.info('-------------debug-----------------')
        # self.logger.info('from global',ximg, yimg)
xin's avatar
xin committed
71
72
        ximg = obj.real_pos.x + 1.0
        yimg = obj.real_pos.y + 1.0
Fang Yuedong's avatar
Fang Yuedong committed
73
        # self.logger.info('from loacl',ximg, yimg)
74
75
76
77
        
        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.pmra, obj.pmdec, obj.rv, obj.parallax)
78
79
80
        line += obj.additional_output_str
        if not line.endswith("\n"):
            line += "\n"
Zhang Xin's avatar
Zhang Xin committed
81
        self.cat.write(line)