def get_master_calibration_image(image, calibration_type, group_by_keywords, db_address=_DEFAULT_DB): calibration_criteria = CalibrationImage.type == calibration_type.upper() calibration_criteria &= CalibrationImage.telescope_id == image.telescope_id for criterion in group_by_keywords: if criterion == 'filter': calibration_criteria &= CalibrationImage.filter_name == getattr(image, criterion) else: calibration_criteria &= getattr(CalibrationImage, criterion) == getattr(image, criterion) # Only grab the last year. In principle we could go farther back, but this limits the number # of files we get back. And if we are using calibrations that are more than a year old # it is probably a bad idea anyway. epoch_datetime = date_utils.epoch_string_to_date(image.epoch) calibration_criteria &= CalibrationImage.dayobs < (epoch_datetime + datetime.timedelta(days=365)) calibration_criteria &= CalibrationImage.dayobs > (epoch_datetime - datetime.timedelta(days=365)) db_session = get_session(db_address=db_address) calibration_images = db_session.query(CalibrationImage).filter(calibration_criteria).all() # Find the closest date if len(calibration_images) == 0: calibration_file = None else: date_deltas = np.abs(np.array([i.dayobs - epoch_datetime for i in calibration_images])) closest_calibration_image = calibration_images[np.argmin(date_deltas)] calibration_file = os.path.join(closest_calibration_image.filepath, closest_calibration_image.filename) db_session.close() return calibration_file
def save_calibration_info(cal_type, output_file, image_config, db_address=_DEFAULT_DB): # Store the information into the calibration table # Check and see if the bias file is already in the database db_session = get_session(db_address=db_address) image_query = db_session.query(CalibrationImage) output_filename = os.path.basename(output_file) image_query = image_query.filter(CalibrationImage.filename == output_filename) image_query = image_query.all() if len(image_query) == 0: # Create a new row calibration_image = CalibrationImage() else: # Otherwise update the existing data # In principle we could just skip this, but this should be fast calibration_image = image_query[0] calibration_image.dayobs = date_utils.epoch_string_to_date(image_config.epoch) calibration_image.ccdsum = image_config.ccdsum calibration_image.filter_name = image_config.filter calibration_image.telescope_id = image_config.telescope_id calibration_image.type = cal_type.upper() calibration_image.filename = output_filename calibration_image.filepath = os.path.dirname(output_file) db_session.add(calibration_image) db_session.commit() db_session.close()
def get_calibration_filename(self, image): calibration_criteria = dbs.CalibrationImage.type == self.calibration_type.upper() calibration_criteria &= dbs.CalibrationImage.telescope_id == image.telescope_id for criterion in self.group_by_keywords: if criterion == 'filter': calibration_criteria &= dbs.CalibrationImage.filter_name == getattr(image, criterion) else: calibration_criteria &= getattr(dbs.CalibrationImage, criterion) == getattr(image, criterion) db_session = dbs.get_session(db_address=self.pipeline_context.db_address) calibration_query = db_session.query(dbs.CalibrationImage).filter(calibration_criteria) epoch_datetime = date_utils.epoch_string_to_date(image.epoch) find_closest = func.DATEDIFF(epoch_datetime, dbs.CalibrationImage.dayobs) find_closest = func.ABS(find_closest) calibration_query = calibration_query.order_by(find_closest.asc()) calibration_image = calibration_query.first() if calibration_image is None: calibration_file = None else: calibration_file = os.path.join(calibration_image.filepath, calibration_image.filename) db_session.close() return calibration_file
def save_calibration_info(cal_type, output_file, image_config, db_address=_DEFAULT_DB): # Store the information into the calibration table # Check and see if the bias file is already in the database db_session = get_session(db_address=db_address) output_filename = os.path.basename(output_file) add_or_update_record(db_session, CalibrationImage, {'filename': output_filename}, {'dayobs': date_utils.epoch_string_to_date(image_config.epoch), 'ccdsum': image_config.ccdsum, 'filter_name': image_config.filter, 'telescope_id': image_config.telescope_id, 'type': cal_type.upper(), 'filename': output_filename, 'filepath': os.path.dirname(output_file)}) db_session.commit() db_session.close()
def populate_bpm_table(directory, db_address=_DEFAULT_DB): db_session = get_session(db_address=db_address) bpm_filenames = glob(os.path.join(directory, 'bpm*.fits*')) for bpm_filename in bpm_filenames: if bpm_filename[-3:] == '.fz': extension_number = 1 else: extension_number = 0 site = fits.getval(bpm_filename, 'SITEID', extension_number).lower() instrument = fits.getval(bpm_filename, 'INSTRUME', extension_number).lower() ccdsum = fits.getval(bpm_filename, 'CCDSUM', extension_number) creation_date = date_utils.epoch_string_to_date(fits.getval(bpm_filename, 'DAY-OBS', extension_number)) telescope_id = get_telescope_id(site=site, instrument=instrument, db_address=db_address) bpm_attributes = {'telescope_id': telescope_id, 'filepath': os.path.abspath(directory), 'filename': os.path.basename(bpm_filename), 'ccdsum': ccdsum, 'creation_date': creation_date} add_or_update_record(db_session, BadPixelMask, bpm_attributes, bpm_attributes) db_session.commit() db_session.close()