File: remove_hdu.py

package info (click to toggle)
python-asdf 2.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,248 kB
  • sloc: python: 13,104; makefile: 125
file content (49 lines) | stat: -rw-r--r-- 1,453 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

"""
Implementation of command for removing ASDF HDU from ASDF-in-FITS file.
"""

from astropy.io import fits

from .main import Command


__all__ = ['remove_hdu']


class FitsExtractor(Command): # pragma: no cover
    """This class is the plugin implementation for the asdftool runner."""

    @classmethod
    def setup_arguments(cls, subparsers):
        parser = subparsers.add_parser(str("remove-hdu"),
            help="Remove ASDF extension from ASDF-in-FITS file",
            description="Removes ASDF extensions from ASDF-in-FITS files.")

        parser.add_argument('infile', action='store', type=str,
            help="Name of ASDF-in-FITS file containing extension to be removed")
        parser.add_argument('outfile', action='store', type=str,
            help="Name of new FITS output file")

        parser.set_defaults(func=cls.run)

        return parser

    @classmethod
    def run(cls, args):
        return remove_hdu(args.infile, args.outfile)


def remove_hdu(input_file, output_file):
    """Function for removing ASDF HDU from ASDF-in-FITS files"""

    try:
        with fits.open(input_file) as hdulist:
            hdulist.readall()
            asdf_hdu = hdulist['ASDF']
            hdulist.remove(asdf_hdu)
            hdulist.writeto(output_file)
    except (ValueError, KeyError) as error:
        raise RuntimeError(str(error))