ChipOutput.py 3.85 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
import os
2
import logging
3
import ObservationSim.Config._util as _util
Fang Yuedong's avatar
Fang Yuedong committed
4
5

class ChipOutput(object):
6
    def __init__(self, config, focal_plane, chip, filt, imgKey0="", imgKey1="", imgKey2="", exptime=150., mjdTime="", ra_cen=None, dec_cen=None, pointing_type='SCI', pointing_ID='0', subdir="./", prefix=""):
Fang Yuedong's avatar
Fang Yuedong committed
7
8
9
10
11
12
13
14
        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
15
        self.pointing_type = pointing_type
Fang Yuedong's avatar
Fang Yuedong committed
16
17
18
19
        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
20
21
            self.ra_cen = config["obs_setting"]["ra_center"]
            self.dec_cen = config["obs_setting"]["dec_center"]
22

Fang Yuedong's avatar
Fang Yuedong committed
23
        self.chipLabel = focal_plane.getChipLabel(chip.chipID)
24
25
26
        obs_id = _util.get_obs_id(img_type=pointing_type, project_cycle=config["project_cycle"], run_counter=config["run_counter"], pointing_num=pointing_ID)
        # self.cat_name = "MSC_1%s_chip_%s_filt_%s"%(str(pointing_ID).rjust(8, '0'), self.chipLabel, filt.filter_type) + ".cat"
        self.cat_name = "MSC_%s_chip_%s_filt_%s"%(obs_id, self.chipLabel, filt.filter_type) + ".cat"
27

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

30
        # Setup logger for each chip
31
32
        # logger_filename = "MSC_1%s_chip_%s_filt_%s"%(str(pointing_ID).rjust(8, '0'), self.chipLabel, filt.filter_type) + ".log"
        logger_filename = "MSC_%s_chip_%s_filt_%s"%(obs_id, self.chipLabel, 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
        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 "
Fang Yuedong's avatar
Fang Yuedong committed
43
44
45
46
47
        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
48

49
        self.logger.info("pointing_type = %s\n"%(pointing_type))
Fang Yuedong's avatar
Fang Yuedong committed
50
51
52
53
54
55
56
57
    
    def Log_info(self, message):
        print(message)
        self.logger.info(message)
    
    def Log_error(self, message):
        print(message)
        self.logger.error(message)
58
59
60
61
62
    
    def update_ouptut_header(self, additional_column_names=""):
        self.hdr += additional_column_names
    
    def create_output_file(self):
63
        if self.pointing_type == 'SCI':
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):
Fang Yuedong's avatar
Fang Yuedong committed
71
72
        # self.logger.info('-------------debug-----------------')
        # self.logger.info('from global',ximg, yimg)
xin's avatar
xin committed
73
74
        ximg = obj.real_pos.x + 1.0
        yimg = obj.real_pos.y + 1.0
Fang Yuedong's avatar
Fang Yuedong committed
75
        # self.logger.info('from loacl',ximg, yimg)
76
77
78
79
        
        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)
80
81
82
        line += obj.additional_output_str
        if not line.endswith("\n"):
            line += "\n"
Zhang Xin's avatar
Zhang Xin committed
83
        self.cat.write(line)