import os import logging import ObservationSim.Config._util as _util class ChipOutput(object): 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=""): 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 self.pointing_type = pointing_type if (ra_cen is not None) and (dec_cen is not None): self.ra_cen = ra_cen self.dec_cen = dec_cen else: self.ra_cen = config["obs_setting"]["ra_center"] self.dec_cen = config["obs_setting"]["dec_center"] self.chipLabel = focal_plane.getChipLabel(chip.chipID) 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" self.subdir = subdir # Setup logger for each chip # 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" 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) logging.getLogger('numba').setLevel(logging.WARNING) formatter = logging.Formatter('%(asctime)s - %(msecs)d - %(levelname)-8s - [%(filename)s:%(lineno)d] - %(message)s') fh.setFormatter(formatter) self.logger.addHandler(fh) hdr1 = "# obj_ID ID_chip filter xImage yImage ra dec ra_orig dec_orig z mag obj_type " 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 self.logger.info("pointing_type = %s\n"%(pointing_type)) def Log_info(self, message): print(message) self.logger.info(message) def Log_error(self, message): print(message) self.logger.error(message) def update_ouptut_header(self, additional_column_names=""): self.hdr += additional_column_names def create_output_file(self): if self.pointing_type == 'SCI': self.cat = open(os.path.join(self.subdir, self.cat_name), "w") self.logger.info("Creating catalog file %s ...\n"%(os.path.join(self.subdir, self.cat_name))) if not self.hdr.endswith("\n"): self.hdr += "\n" self.cat.write(self.hdr) def cat_add_obj(self, obj, pos_img, pos_shear): # self.logger.info('-------------debug-----------------') # self.logger.info('from global',ximg, yimg) ximg = obj.real_pos.x + 1.0 yimg = obj.real_pos.y + 1.0 # self.logger.info('from loacl',ximg, yimg) 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) line += obj.additional_output_str if not line.endswith("\n"): line += "\n" self.cat.write(line)