Skip to content

Commit

Permalink
Add command to asdftool for removing ASDF extension from FITS file
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Mar 27, 2018
1 parent a5507fb commit 7eb25a5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Add command for extracting ASDF extension from ASDF-in-FITS file and
converting it to a pure ASDF file. [#477]

- Add command for removing ASDF extension from ASDF-in-FITS file. [#480]

1.4.0 (unreleased)
------------------

Expand Down
1 change: 1 addition & 0 deletions asdf/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# Extracting ASDF-in-FITS files requires Astropy
if importlib.util.find_spec('astropy'):
from .extract import *
from .remove_hdu import *
51 changes: 51 additions & 0 deletions asdf/commands/remove_hdu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.
"""

import sys

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))

0 comments on commit 7eb25a5

Please sign in to comment.