<reponame>tnking97/nirspec_pipe_testing_tool<filename>nirspec_pipe_testing_tool/utils/read_hdr.py
#! /usr/bin/env python

from astropy.io import fits
import argparse
import sys

"""
This script reads the header of a fits file and gives info of its extensions.

Example usage:
    The code works from the terminal.
    To simply see the header on-screen type:
        $ nptt_read_hdr blah.fits
    To see and save the header into a text file, add the flag -s to the end of the previous command.
    
    To see and save the header of a different extension from the main, use the flag -e=extension_number:
        $ nptt_read_hdr blah.fits -s -e=1

"""


# HEADER
__author__ = "<NAME>"
__version__ = "1.0"

# HISTORY
# Nov 2017 - Version 1.0: initial version completed


def read_hdr(fits_file_name, save_txt=False, ext_number=None):
    if ext_number is None:
        extension_number = 0
    else:
        extension_number = int(ext_number)

    #  Read the fits file
    hdulist = fits.open(fits_file_name)

    # print on screen what extensions are in the file
    print('\n FILE INFORMATION: \n')
    hdulist.info()
    print()

    # get and print header
    print('\n FILE HEADER FOR EXTENSION: ', extension_number, '\n')
    hdr = hdulist[extension_number].header
    print(repr(hdr))

    # close the fits file
    hdulist.close()

    # save the info into a text file
    if save_txt:
        # set the name of the text file
        text_file_name = fits_file_name.replace('.fits', '_hdr_ext'+repr(extension_number)+'.txt')
        tf = open(text_file_name, 'w')
        tf.write(repr(hdr))
        tf.close()


def main():
    # Get arguments to run script
    parser = argparse.ArgumentParser(description='')
    parser.add_argument("fits_file_name",
                        action='store',
                        default=None,
                        help='Name of fits file, i.e. blah.fits')
    parser.add_argument("-s",
                        dest="save_txt",
                        action='store_true',
                        default=False,
                        help='Save a text file with the header information.')
    parser.add_argument("-e",
                        dest="ext_number",
                        action='store',
                        default=None,
                        help='Save a text file with the header information of the given extension.')
    args = parser.parse_args()

    # Set the variables
    fits_file_name = args.fits_file_name
    save_txt = args.save_txt
    ext_number = args.ext_number

    read_hdr(fits_file_name, save_txt, ext_number)


if __name__ == '__main__':
    sys.exit(main())

#! /usr/bin/env python

from astropy.io import fits
import argparse
import sys

"""
This script reads the header of a fits file and gives info of its extensions.

Example usage:
    The code works from the terminal.
    To simply see the header on-screen type:
        $ nptt_read_hdr blah.fits
    To see and save the header into a text file, add the flag -s to the end of the previous command.
    
    To see and save the header of a different extension from the main, use the flag -e=extension_number:
        $ nptt_read_hdr blah.fits -s -e=1

"""


# HEADER
__author__ = "<NAME>"
__version__ = "1.0"

# HISTORY
# Nov 2017 - Version 1.0: initial version completed


def read_hdr(fits_file_name, save_txt=False, ext_number=None):
    if ext_number is None:
        extension_number = 0
    else:
        extension_number = int(ext_number)

    #  Read the fits file
    hdulist = fits.open(fits_file_name)

    # print on screen what extensions are in the file
    print('\n FILE INFORMATION: \n')
    hdulist.info()
    print()

    # get and print header
    print('\n FILE HEADER FOR EXTENSION: ', extension_number, '\n')
    hdr = hdulist[extension_number].header
    print(repr(hdr))

    # close the fits file
    hdulist.close()

    # save the info into a text file
    if save_txt:
        # set the name of the text file
        text_file_name = fits_file_name.replace('.fits', '_hdr_ext'+repr(extension_number)+'.txt')
        tf = open(text_file_name, 'w')
        tf.write(repr(hdr))
        tf.close()


def main():
    # Get arguments to run script
    parser = argparse.ArgumentParser(description='')
    parser.add_argument("fits_file_name",
                        action='store',
                        default=None,
                        help='Name of fits file, i.e. blah.fits')
    parser.add_argument("-s",
                        dest="save_txt",
                        action='store_true',
                        default=False,
                        help='Save a text file with the header information.')
    parser.add_argument("-e",
                        dest="ext_number",
                        action='store',
                        default=None,
                        help='Save a text file with the header information of the given extension.')
    args = parser.parse_args()

    # Set the variables
    fits_file_name = args.fits_file_name
    save_txt = args.save_txt
    ext_number = args.ext_number

    read_hdr(fits_file_name, save_txt, ext_number)


if __name__ == '__main__':
    sys.exit(main())
#! /usr/bin/env python This script reads the header of a fits file and gives info of its extensions.  Example usage:     The code works from the terminal.     To simply see the header on-screen type:         $ nptt_read_hdr blah.fits     To see and save the header into a text file, add the flag -s to the end of the previous command.          To see and save the header of a different extension from the main, use the flag -e=extension_number:         $ nptt_read_hdr blah.fits -s -e=1 # HEADER # HISTORY # Nov 2017 - Version 1.0: initial version completed #  Read the fits file # print on screen what extensions are in the file # get and print header # close the fits file # save the info into a text file # set the name of the text file # Get arguments to run script # Set the variables