def get_bpm(image, pipeline_context): bpm_filename = dbs.get_bpm(image.telescope_id, image.ccdsum, db_address=pipeline_context.db_address) if bpm_filename is None: bpm = None image.header['L1IDMASK'] = ('', 'Id. of mask file used') else: bpm_hdu = fits_utils.open_fits_file(bpm_filename) bpm_extensions = fits_utils.get_extensions_by_name(bpm_hdu, 'BPM') if len(bpm_extensions) > 1: extension_shape = bpm_extensions[0].data.shape bpm_shape = (len(bpm_extensions), extension_shape[0], extension_shape[1]) bpm = np.zeros(bpm_shape, dtype=np.uint8) for i, extension in enumerate(bpm_extensions): bpm[i, :, :] = extension.data[:, :] elif len(bpm_extensions) == 1: bpm = np.array(bpm_extensions[0].data, dtype=np.uint8) else: bpm = np.array(bpm_hdu[0].data, dtype=np.uint8) if not bpm_has_valid_size(bpm, image): tags = logs.image_config_to_tags(image, None) logs.add_tag(tags, 'filename', image.filename) logger.error('BPM shape mismatch', extra=tags) err_msg = 'BPM shape mismatch for {filename} ' \ '{site}/{instrument}'.format(filename=image.filename, site=image.site, instrument=image.instrument) raise ValueError(err_msg) image.header['L1IDMASK'] = (os.path.basename(bpm_filename), 'Id. of mask file used') return bpm
def test_get_sci_extensions(): hdulist = [fits.PrimaryHDU(header=fits.Header({'test': 'test'}))] input_data = [] for i in range(4): data = np.random.uniform(0, 1, size=(101, 101)).astype(dtype=np.float32) input_data.append(data) # Build the fits header manually because of a bug in the latest stable version of astropy # This should be # header = fits.Header({'EXTNAME': 'SCI', 'EXTVER': i + 1}) header = fits.Header() header['EXTNAME'] = 'SCI' header['EXTVER'] = i + 1 hdulist.append(fits.ImageHDU(data=data, header=header)) bpm_header = fits.Header() bpm_header['EXTNAME'] = 'BPM' bpm_hdu = fits.ImageHDU(data=np.zeros((101, 101), dtype=np.uint8), header=bpm_header) hdulist.append(bpm_hdu) hdulist = fits.HDUList(hdulist) sci_extensions = fits_utils.get_extensions_by_name(hdulist, 'SCI') assert len(sci_extensions) == 4 for i in range(4): np.testing.assert_allclose(sci_extensions[i].data, input_data[i], atol=1e-5)
def load_bpm(bpm_filename): bpm_hdu = fits_utils.open_fits_file(bpm_filename) bpm_extensions = fits_utils.get_extensions_by_name(bpm_hdu, 'BPM') # Filter out BPM extensions without data bpm_extensions = [ extension for extension in bpm_extensions if extension.data is not None ] if len(bpm_extensions) > 1: extension_shape = bpm_extensions[0].data.shape bpm_shape = (len(bpm_extensions), extension_shape[0], extension_shape[1]) bpm = np.zeros(bpm_shape, dtype=np.uint8) for i, extension in enumerate(bpm_extensions): bpm[i, :, :] = extension.data[:, :] elif len(bpm_extensions) == 1: bpm = np.array(bpm_extensions[0].data, dtype=np.uint8) else: bpm = np.array(bpm_hdu[0].data, dtype=np.uint8) return bpm