Python astropy.io.fits.Header() Examples

The following are 30 code examples of astropy.io.fits.Header(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module astropy.io.fits , or try the search function .
Example #1
Source File: test_bias_maker.py    From banzai with GNU General Public License v3.0 6 votes vote down vote up
def test_header_cal_type_bias(mock_namer):
    mock_namer.return_value = lambda *x: 'foo.fits'
    nx = 100
    ny = 100

    maker = BiasMaker(FakeContext())

    image_header = Header({'DATE-OBS': '2019-12-04T14:34:00',
                           'DETSEC': '[1:100,1:100]',
                           'DATASEC': '[1:100,1:100]',
                           'OBSTYPE': 'BIAS', 'RA': 0.0, 'DEC': 0.0})

    image = maker.do_stage([FakeLCOObservationFrame(hdu_list=[FakeCCDData(data=np.zeros((ny, nx)),
                                                                           meta=image_header,
                                                                           bias_level=0.0)])
                             for x in range(6)])

    assert image.meta['OBSTYPE'].upper() == 'BIAS' 
Example #2
Source File: fits.py    From everest with MIT License 6 votes vote down vote up
def ApertureHDU(model):
    '''
    Construct the HDU containing the aperture used to de-trend.

    '''

    # Get mission cards
    cards = model._mission.HDUCards(model.meta, hdu=3)

    # Add EVEREST info
    cards.append(('COMMENT', '************************'))
    cards.append(('COMMENT', '*     EVEREST INFO     *'))
    cards.append(('COMMENT', '************************'))
    cards.append(('MISSION', model.mission, 'Mission name'))
    cards.append(('VERSION', EVEREST_MAJOR_MINOR, 'EVEREST pipeline version'))
    cards.append(('SUBVER', EVEREST_VERSION, 'EVEREST pipeline subversion'))
    cards.append(('DATE', strftime('%Y-%m-%d'),
                  'EVEREST file creation date (YYYY-MM-DD)'))

    # Create the HDU
    header = pyfits.Header(cards=cards)
    hdu = pyfits.ImageHDU(data=model.aperture,
                          header=header, name='APERTURE MASK')

    return hdu 
Example #3
Source File: lco.py    From banzai with GNU General Public License v3.0 6 votes vote down vote up
def init_master_header(old_header, images):
        header = fits.Header()
        for key in old_header.keys():
            try:
                # Dump empty header keywords and ignore old histories.
                if len(key) > 0 and key != 'HISTORY':
                    for i in range(old_header.count(key)):
                        header[key] = (old_header[(key, i)], old_header.comments[(key, i)])
            except ValueError as e:
                logger.error('Could not add keyword {key}: {error}'.format(key=key, error=e))
                continue
        header = fits_utils.sanitize_header(header)
        observation_dates = [image.dateobs for image in images]
        mean_dateobs = date_utils.mean_date(observation_dates)

        header['DATE-OBS'] = (date_utils.date_obs_to_string(mean_dateobs), '[UTC] Mean observation start time')
        header['DAY-OBS'] = (max(images, key=lambda x: datetime.datetime.strptime(x.epoch, '%Y%m%d')).epoch, '[UTC] Date at start of local observing night')
        header['ISMASTER'] = (True, 'Is this a master calibration frame')

        header.add_history("Images combined to create master calibration image:")
        for i, image in enumerate(images):
            header[f'IMCOM{i+1:03d}'] = (image.filename, 'Image combined to create master')
        return header 
Example #4
Source File: mosaic.py    From k2mosaic with MIT License 6 votes vote down vote up
def __init__(self, campaign=0, channel=1, cadenceno=1, data_store=None,
                 shape=KEPLER_CHANNEL_SHAPE, add_background=False, time=None,
                 quality=None, dateobs=None, dateend=None, mjdbeg=None, 
                 mjdend=None, template_tpf_header0=None,
                 template_tpf_header1=None):
        self.campaign = campaign
        self.channel = channel
        self.cadenceno = cadenceno
        self.data_store = data_store
        self.header = fits.Header()
        self.data = np.empty(shape, dtype=np.float32)
        self.data[:] = np.nan
        self.uncert = np.empty(shape, dtype=np.float32)
        self.uncert[:] = np.nan
        self.add_background = add_background
        self.time = time
        self.quality = quality
        self.template_tpf_header0 = template_tpf_header0
        self.template_tpf_header1 = template_tpf_header1
        self.dateobs = dateobs
        self.dateend = dateend
        self.mjdbeg = mjdbeg
        self.mjdend = mjdend 
Example #5
Source File: targetpixelfile.py    From lightkurve with MIT License 6 votes vote down vote up
def get_header(self, ext=0):
        """Returns the metadata embedded in the file.

        Target Pixel Files contain embedded metadata headers spread across three
        different FITS extensions:

        1. The "PRIMARY" extension (``ext=0``) provides a metadata header
           providing details on the target and its CCD position.
        2. The "PIXELS" extension (``ext=1``) provides details on the
           data column and their coordinate system (WCS).
        3. The "APERTURE" extension (``ext=2``) provides details on the
           aperture pixel mask and the expected coordinate system (WCS).

        Parameters
        ----------
        ext : int or str
            FITS extension name or number.

        Returns
        -------
        header : `~astropy.io.fits.header.Header`
            Header object containing metadata keywords.
        """
        return self.hdu[ext].header 
Example #6
Source File: test_bias_maker.py    From banzai with GNU General Public License v3.0 6 votes vote down vote up
def test_makes_a_sensible_master_bias(mock_namer):
    mock_namer.return_value = lambda *x: 'foo.fits'
    nimages = 20
    expected_readnoise = 15.0

    images = [FakeLCOObservationFrame(hdu_list=[FakeCCDData(data=np.random.normal(0.0, size=(99, 99), scale=expected_readnoise),
                                                            bias_level=0.0,
                                                            read_noise= expected_readnoise,
                                                            meta=Header({'DATE-OBS': '2019-12-04T14:34:00',
                                                                         'DETSEC': '[1:100,1:100]',
                                                                         'DATASEC': '[1:100,1:100]',
                                                                         'OBSTYPE': 'BIAS', 'RA': 0.0, 'DEC': 0.0}))])
              for i in range(nimages)]

    maker = BiasMaker(FakeContext())
    stacked_image = maker.do_stage(images)
    master_bias = stacked_image.data
    assert np.abs(np.mean(master_bias)) < 0.1
    actual_readnoise = np.std(master_bias)
    assert np.abs(actual_readnoise - expected_readnoise / (nimages ** 0.5)) < 0.2 
Example #7
Source File: test_bias_maker.py    From banzai with GNU General Public License v3.0 6 votes vote down vote up
def test_bias_level_is_average_of_inputs(mock_namer):
    mock_namer.return_value = lambda *x: 'foo.fits'
    nimages = 20
    bias_levels = np.arange(nimages, dtype=float)

    images = [FakeLCOObservationFrame(hdu_list=[FakeCCDData(data=np.random.normal(i, size=(99,99)),
                                                            bias_level=i, meta=Header({'DATE-OBS': '2019-12-04T14:34:00',
                                                                                       'DETSEC': '[1:100,1:100]',
                                                                                       'DATASEC': '[1:100,1:100]',
                                                                                       'OBSTYPE': 'BIAS',
                                                                                       'RA': 0.0,
                                                                                       'DEC': 0.0}))])
              for i in bias_levels]

    fake_context = FakeContext()
    maker = BiasMaker(fake_context)
    master_bias = maker.do_stage(images)

    assert master_bias.meta['BIASLVL'] == np.mean(bias_levels) 
Example #8
Source File: test_flat_maker.py    From banzai with GNU General Public License v3.0 6 votes vote down vote up
def test_makes_a_sensible_master_flat(mock_namer):
    mock_namer.return_value = lambda *x: 'foo.fits'
    nimages = 50
    flat_level = 10000.0
    nx = 100
    ny = 100
    master_flat_variation = 0.05

    image_header = Header({'DATE-OBS': '2019-12-04T14:34:00',
                           'DETSEC': '[1:100,1:100]',
                           'DATASEC': '[1:100,1:100]',
                           'FLATLVL': flat_level,
                           'OBSTYPE': 'SKYFLAT',
                           'RA': 0.0,
                           'DEC': 0.0})

    flat_pattern = np.random.normal(1.0, master_flat_variation, size=(ny, nx))
    images = [FakeLCOObservationFrame(hdu_list=[FakeCCDData(data=flat_pattern + np.random.normal(0.0, 0.02, size=(ny, nx)),
                                                            meta=image_header)])
              for _ in range(nimages)]

    maker = FlatMaker(FakeContext())
    stacked_image = maker.do_stage(images)
    np.testing.assert_allclose(stacked_image.primary_hdu.data, flat_pattern, atol=0.1, rtol=0.1) 
Example #9
Source File: fits.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def from_tree(cls, data, ctx):
        hdus = []
        first = True
        for hdu_entry in data:
            header = fits.Header([fits.Card(*x) for x in hdu_entry['header']])
            data = hdu_entry.get('data')
            if data is not None:
                try:
                    data = data.__array__()
                except ValueError:
                    data = None
            if first:
                hdu = fits.PrimaryHDU(data=data, header=header)
                first = False
            elif data.dtype.names is not None:
                hdu = fits.BinTableHDU(data=data, header=header)
            else:
                hdu = fits.ImageHDU(data=data, header=header)
            hdus.append(hdu)
        hdulist = fits.HDUList(hdus)
        return hdulist 
Example #10
Source File: test_dark_maker.py    From banzai with GNU General Public License v3.0 6 votes vote down vote up
def test_makes_a_sensible_master_dark(mock_namer):
    mock_namer.return_value = lambda *x: 'foo.fits'
    nimages = 20
    nx = 100
    ny = 100
    image_header = Header({'DATE-OBS': '2019-12-04T14:34:00',
                           'DETSEC': '[1:100,1:100]',
                           'DATASEC': '[1:100,1:100]',
                           'OBSTYPE': 'DARK'})
    images = [FakeLCOObservationFrame(hdu_list=[FakeCCDData(data=np.ones((ny, nx)) * x,
                                                            meta=image_header)])
              for x in range(nimages)]

    expected_master_dark = stats.sigma_clipped_mean(np.arange(nimages), 3.0)
    maker = DarkMaker(FakeContext())
    stacked_image = maker.do_stage(images)

    assert (stacked_image.data == expected_master_dark).all() 
Example #11
Source File: test_dark_maker.py    From banzai with GNU General Public License v3.0 6 votes vote down vote up
def test_header_cal_type_dark(mock_namer):
    mock_namer.return_value = lambda *x: 'foo.fits'
    nx = 100
    ny = 100
    maker = DarkMaker(FakeContext())

    image_header = Header({'DATE-OBS': '2019-12-04T14:34:00',
                           'DETSEC': '[1:100,1:100]',
                           'DATASEC': '[1:100,1:100]',
                           'OBSTYPE': 'DARK'})

    image = maker.do_stage([FakeLCOObservationFrame(hdu_list=[FakeCCDData(data=np.zeros((ny,nx)),
                                                                           meta=image_header)])
                             for x in range(6)])

    assert image.meta['OBSTYPE'].upper() == 'DARK' 
Example #12
Source File: test_core.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_set_card_value(self):
        """Similar to test_update_header_card(), but tests the the
        `header['FOO'] = 'bar'` method of updating card values.
        """

        header = fits.Header()
        comment = 'number of bits per data pixel'
        card = fits.Card.fromstring(f'BITPIX  = 32 / {comment}')
        header.append(card)

        header['BITPIX'] = 32

        assert 'BITPIX' in header
        assert header['BITPIX'] == 32
        assert header.cards[0].keyword == 'BITPIX'
        assert header.cards[0].value == 32
        assert header.cards[0].comment == comment 
Example #13
Source File: test_core.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_writeto_2(self):
        """
        Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/107

        Test of `writeto()` with a trivial header containing a single keyword.
        """
        filename = self.temp('array.fits')
        data = np.zeros((100, 100))
        header = fits.Header()
        header.set('CRPIX1', 1.)
        fits.writeto(filename, data, header=header,
                     overwrite=True, output_verify='silentfix')
        with fits.open(filename) as hdul:
            assert len(hdul) == 1
            assert (data == hdul[0].data).all()
            assert 'CRPIX1' in hdul[0].header
            assert hdul[0].header['CRPIX1'] == 1.0 
Example #14
Source File: test_core.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_writeto(self):
        """
        Simple test for writing a trivial header and some data to a file
        with the `writeto()` convenience function.
        """
        filename = self.temp('array.fits')
        data = np.zeros((100, 100))
        header = fits.Header()
        fits.writeto(filename, data, header=header, overwrite=True)
        with fits.open(filename) as hdul:
            assert len(hdul) == 1
            assert (data == hdul[0].data).all() 
Example #15
Source File: test_kepler.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fake_header(extver, version, timesys, telescop):
    return Header({"SIMPLE": "T",
                   "BITPIX": 8,
                   "NAXIS": 0,
                   "EXTVER": extver,
                   "VERSION": version,
                   'TIMESYS': f"{timesys}",
                   "TELESCOP": f"{telescop}"}) 
Example #16
Source File: test_fits_utils.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def test_get_configuration_mode_na():
    header = Header({'CONFMODE': 'N/A'})

    configuration_mode = fits_utils.get_configuration_mode(header)

    assert configuration_mode == 'default' 
Example #17
Source File: test_ccddata.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_header2meta():
    hdr = fits.header.Header()
    hdr['observer'] = 'Edwin Hubble'
    hdr['exptime'] = '3600'

    d1 = CCDData(np.ones((5, 5)), unit=u.electron)
    d1.header = hdr
    assert d1.meta['OBSERVER'] == 'Edwin Hubble'
    assert d1.header['OBSERVER'] == 'Edwin Hubble' 
Example #18
Source File: test_ccddata.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_metafromheader():
    hdr = fits.header.Header()
    hdr['observer'] = 'Edwin Hubble'
    hdr['exptime'] = '3600'

    d1 = CCDData(np.ones((5, 5)), meta=hdr, unit=u.electron)
    assert d1.meta['OBSERVER'] == 'Edwin Hubble'
    assert d1.header['OBSERVER'] == 'Edwin Hubble' 
Example #19
Source File: test_ccddata.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_ccddata_meta_is_not_fits_header():
    ccd_data = create_ccd_data()
    ccd_data.meta = {'OBSERVER': 'Edwin Hubble'}
    assert not isinstance(ccd_data.meta, fits.Header) 
Example #20
Source File: test_core.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_streaming_hdu_file_wrong_mode(self):
        """
        Test that streaming an HDU to a file opened in the wrong mode fails as
        expected.
        """

        with open(self.temp('new.fits'), 'wb') as f:
            header = fits.Header()
            fits.StreamingHDU(f, header) 
Example #21
Source File: test_hdulist.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_new_hdulist_extend_keyword(self):
        """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/114

        Tests that adding a PrimaryHDU to a new HDUList object updates the
        EXTEND keyword on that HDU.
        """

        h0 = fits.Header()
        hdu = fits.PrimaryHDU(header=h0)
        sci = fits.ImageHDU(data=np.array(10))
        image = fits.HDUList([hdu, sci])
        image.writeto(self.temp('temp.fits'))
        assert 'EXTEND' in hdu.header
        assert hdu.header['EXTEND'] is True 
Example #22
Source File: test_wcs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setup(self):
        fname = get_pkg_data_filename(
            'data/header_with_time.fits')
        self.header = fits.Header.fromfile(fname)
        with pytest.warns(FITSFixedWarning):
            self.w = wcs.WCS(self.header, key='A') 
Example #23
Source File: lco.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def _munge_data_cube(hdu):
        """
        Munge the old sinistro data cube data into our new format

        :param hdu: Fits.ImageHDU
        :return: List CCDData objects
        """
        # The first extension gets to be a header only object
        hdu_list = [HeaderOnly(meta=hdu.header)]

        # We need to properly set the datasec and detsec keywords in case we didn't read out the
        # middle row (the "Missing Row Problem").
        sinistro_datasecs = {'missing': ['[1:2048,1:2048]', '[1:2048,1:2048]',
                                         '[1:2048,2:2048]', '[1:2048,2:2048]'],
                             'full': ['[1:2048,1:2048]', '[1:2048,1:2048]',
                                      '[1:2048,2:2049]', '[1:2048,2:2049]']}
        sinistro_detsecs = {'missing': ['[1:2048,1:2048]', '[4096:2049,1:2048]',
                                        '[4096:2049,4096:2050]', '[1:2048,4096:2050]'],
                            'full': ['[1:2048,1:2048]', '[4096:2049,1:2048]',
                                     '[4096:2049,4096:2049]', '[1:2048,4096:2049]']}
        for i in range(hdu.data.shape[0]):
            gain = eval(hdu.header['GAIN'])[i]
            if hdu.data.shape[1] > 2048:
                mode = 'full'
            else:
                mode = 'missing'
            datasec = sinistro_datasecs[mode][i]
            detsec = sinistro_detsecs[mode][i]
            header = {'BIASSEC': ('[2055:2080,1:2048]', '[binned pixel] Overscan Region'),
                      'GAIN': (gain, hdu.header.comments['GAIN']),
                      'DATASEC': (datasec, '[binned pixel] Data section'),
                      'DETSEC': (detsec, '[unbinned pixel] Detector section'),
                      'CCDSUM': (hdu.header['CCDSUM'], hdu.header.comments['CCDSUM'])}
            hdu_list.append(CCDData(data=hdu.data[i], meta=fits.Header(header)))
        # We have to split the gain keyword for each extension
        return hdu_list 
Example #24
Source File: test_fits_utils.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def test_get_configuration_mode_central_2k():
    header = Header({'CONFMODE': 'central_2k_2x2'})

    configuration_mode = fits_utils.get_configuration_mode(header)

    assert configuration_mode == 'central_2k_2x2' 
Example #25
Source File: test_modelcube.py    From marvin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _test_init(self, model_cube, galaxy, bintype=None, template=None):
        assert model_cube.release == galaxy.release
        assert model_cube._drpver == galaxy.drpver
        assert model_cube._dapver == galaxy.dapver
        assert model_cube.bintype.name == bintype if bintype else galaxy.bintype.name
        assert model_cube.template == template if template else galaxy.template
        assert model_cube.plateifu == galaxy.plateifu
        assert model_cube.mangaid == galaxy.mangaid
        assert isinstance(model_cube.header, fits.Header)
        assert isinstance(model_cube.wcs, WCS)
        assert model_cube._wavelength is not None
        assert model_cube._redcorr is not None 
Example #26
Source File: test_fits_utils.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def test_sanitize_header():
    header = Header({'CCDSUM': '1 1',
                     'SIMPLE': 'foo',
                     'BITPIX': 8,
                     'NAXIS': 2,
                     'CHECKSUM': 'asdf'})

    sanitized_header = fits_utils.sanitize_header(header)

    assert list(sanitized_header.keys()) == ['CCDSUM'] 
Example #27
Source File: test_saturation_qc.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def test_nonzero_but_no_pixels_saturated():
    tester = SaturationTest(None)
    image = FakeLCOObservationFrame(hdu_list=[FakeCCDData(meta=Header({'SATURATE': 65535}))])
    image.primary_hdu.data += 5.0
    image = tester.do_stage(image)
    assert image is not None
    assert image.meta.get('SATFRAC') == 0.0 
Example #28
Source File: test_saturation_qc.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def test_no_pixels_saturated():
    tester = SaturationTest(None)
    image = FakeLCOObservationFrame(hdu_list=[FakeCCDData(meta=Header({'SATURATE': 65535}))])
    image = tester.do_stage(image)
    assert image is not None
    assert image.meta.get('SATFRAC') == 0.0 
Example #29
Source File: utils.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, image_multiplier=1.0, nx=101, ny=103, name='test_image', read_noise=None,
                 bias_level=None, meta=None, data=None, mask=None, uncertainty=None, **kwargs):
        self.name = name
        if meta is not None:
            self.meta = meta
        else:
            self.meta = Header()
        if bias_level is not None:
            self.meta['BIASLVL'] = bias_level
        if read_noise is not None:
            self.meta['RDNOISE'] = read_noise
        self._detector_section = Section.parse_region_keyword(self.meta.get('DETSEC'))
        self._data_section = Section.parse_region_keyword(self.meta.get('DATASEC'))

        if data is None:
            self.data = image_multiplier * np.ones((ny, nx), dtype=np.float32)
        else:
            self.data = data
        if mask is None:
            self.mask = np.zeros(self.data.shape, dtype=np.uint8)
        else:
            self.mask = mask
        if uncertainty is None:
            self.uncertainty = self.read_noise * np.ones(self.data.shape, dtype=self.data.dtype)
        else:
            self.uncertainty = uncertainty

        for keyword in kwargs:
            setattr(self, keyword, kwargs[keyword]) 
Example #30
Source File: header.py    From baseband with GNU General Public License v3.0 5 votes vote down vote up
def copy(self):
        """Create a mutable and independent copy of the header."""
        # This method exists because io.fits.Header.copy has a docstring that
        # refers to `Header` which breaks sphinx...
        return super().copy()