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

class ChipOutput(object):
7
8
    def __init__(self, config, chip, filt, pointing):
        self.config = config
Fang Yuedong's avatar
Fang Yuedong committed
9
10
        self.chip = chip
        self.filt = filt
11
12
        self.pointing_type = pointing.pointing_type
        self.chip_label = str(chip.chipID).rjust(2, '0')
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        # Get primary header based on chip and pointing
        self.h_prim = generatePrimaryHeader(
            xlen=chip.npix_x, 
            ylen=chip.npix_y, 
            pointing_id = pointing.obs_id,
            pointing_type_code = pointing.pointing_type_code, 
            ra=pointing.ra, 
            dec=pointing.dec, 
            pixel_scale=chip.pix_scale,
            time_pt = pointing.timestamp,
            exptime=pointing.exp_time,
            im_type=pointing.pointing_type,
            sat_pos=[pointing.sat_x, pointing.sat_y, pointing.sat_z],
            sat_vel=[pointing.sat_vx, pointing.sat_vy, pointing.sat_vz],
            project_cycle=self.config["project_cycle"],
            run_counter=self.config["run_counter"],
            chip_name=self.chip_label)
31

32
33
34
35
36
        obs_id = _util.get_obs_id(img_type=self.pointing_type, project_cycle=config["project_cycle"], run_counter=config["run_counter"], pointing_id=pointing.obs_id, pointing_type_code = pointing.pointing_type_code)
        
        self.subdir = pointing.output_dir
        self.cat_name = self.h_prim['FILENAME'] + '.cat'
        logger_filename = self.h_prim['FILENAME'] + '.log'
Fang Yuedong's avatar
Fang Yuedong committed
37

38
39
40
41
        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
42
        logging.getLogger('numba').setLevel(logging.WARNING)
43
        formatter = logging.Formatter('%(asctime)s - %(msecs)d - %(levelname)-8s - [%(filename)s:%(lineno)d] - %(message)s')
44
45
46
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)

47
        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
48
49
50
51
52
        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
53

54
        self.logger.info("pointing_type = %s\n"%(self.pointing_type))
Fang Yuedong's avatar
Fang Yuedong committed
55
56
57
58
59
60
61
62
    
    def Log_info(self, message):
        print(message)
        self.logger.info(message)
    
    def Log_error(self, message):
        print(message)
        self.logger.error(message)
63
    
64
    def update_output_header(self, additional_column_names=""):
65
66
67
        self.hdr += additional_column_names
    
    def create_output_file(self):
68
        if self.pointing_type == 'SCI':
Fang Yuedong's avatar
Fang Yuedong committed
69
            self.cat = open(os.path.join(self.subdir, self.cat_name), "w")
70
            self.logger.info("Creating catalog file %s ...\n"%(os.path.join(self.subdir, self.cat_name)))
71
72
            if not self.hdr.endswith("\n"):
                self.hdr += "\n"
Fang Yuedong's avatar
Fang Yuedong committed
73
            self.cat.write(self.hdr)
Fang Yuedong's avatar
Fang Yuedong committed
74

75
    def cat_add_obj(self, obj, pos_img, pos_shear):
xin's avatar
xin committed
76
77
        ximg = obj.real_pos.x + 1.0
        yimg = obj.real_pos.y + 1.0
78
79
        
        line = self.fmt%(
80
            obj.id, int(self.chip_label), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.ra_orig, obj.dec_orig, obj.z, obj.getMagFilter(self.filt), obj.type, 
81
            obj.pmra, obj.pmdec, obj.rv, obj.parallax)
82
83
84
        line += obj.additional_output_str
        if not line.endswith("\n"):
            line += "\n"
Zhang Xin's avatar
Zhang Xin committed
85
        self.cat.write(line)