import os import logging import ObservationSim.Config._util as _util from ObservationSim.Config.Header import generatePrimaryHeader class ChipOutput(object): def __init__(self, config, chip, filt, pointing): self.config = config self.chip = chip self.filt = filt self.pointing_type = pointing.pointing_type self.chip_label = str(chip.chipID).rjust(2, '0') # 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) 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' 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"%(self.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_output_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): ximg = obj.real_pos.x + 1.0 yimg = obj.real_pos.y + 1.0 line = self.fmt%( 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, obj.pmra, obj.pmdec, obj.rv, obj.parallax) line += obj.additional_output_str if not line.endswith("\n"): line += "\n" self.cat.write(line)