ó
ßČ[c           @` sĀ   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l m Z d d l	 m
 Z
 d	 e f d
     YZ d e f d     YZ d e f d     YZ d   Z d   Z d d  Z d S(   s  
``fitsheader`` is a command line script based on astropy.io.fits for printing
the header(s) of one or more FITS file(s) to the standard output in a human-
readable format.

Example uses of fitsheader:

1. Print the header of all the HDUs of a .fits file::

    $ fitsheader filename.fits

2. Print the header of the third and fifth HDU extension::

    $ fitsheader --extension 3 --extension 5 filename.fits

3. Print the header of a named extension, e.g. select the HDU containing
   keywords EXTNAME='SCI' and EXTVER='2'::

    $ fitsheader --extension "SCI,2" filename.fits

4. Print only specific keywords::

    $ fitsheader --keyword BITPIX --keyword NAXIS filename.fits

5. Print keywords NAXIS, NAXIS1, NAXIS2, etc using a wildcard::

    $ fitsheader --keyword NAXIS* filename.fits

6. Dump the header keywords of all the files in the current directory into a
   machine-readable csv file::

    $ fitsheader --table ascii.csv *.fits > keywords.csv

Note that compressed images (HDUs of type
:class:`~astropy.io.fits.CompImageHDU`) really have two headers: a real
BINTABLE header to describe the compressed data, and a fake IMAGE header
representing the image that was compressed. Astropy returns the latter by
default. You must supply the ``--compressed`` option if you require the real
header that describes the compression.

With Astropy installed, please run ``fitsheader --help`` to see the full usage
documentation.
i    (   t   absolute_importt   divisiont   print_functionNi   (   t   fitsi   (   t   log(   t   ranget   ExtensionNotFoundExceptionc           B` s   e  Z d  Z RS(   s@   Raised if an HDU extension requested by the user does not exist.(   t   __name__t
   __module__t   __doc__(    (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyR   6   s   t   HeaderFormatterc           B` s;   e  Z d  Z d   Z d d e d  Z d   Z d   Z RS(   sĒ  Class to format the header(s) of a FITS file for display by the
    `fitsheader` tool; essentially a wrapper around a `HDUList` object.

    Example usage:
    fmt = HeaderFormatter('/path/to/file.fits')
    print(fmt.parse(extensions=[0, 3], keywords=['NAXIS', 'BITPIX']))

    Parameters
    ----------
    filename : str
        Path to a single FITS file.

    Raises
    ------
    IOError
        If `filename` does not exist or cannot be read.
    c         C` s   | |  _  t j |  |  _ d  S(   N(   t   filenameR   t   opent   _hdulist(   t   selfR   (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyt   __init__M   s    	c   	      C` sć   | d k r$ t t |  j   } n¬ g  } x£ | D] } y | j t |   Wq1 t k
 rĖ | j d  } t |  d k r» d j | d d ! } t | d  } | j | | f  qĢ | j |  q1 Xq1 W|  j	 | | |  S(   sŃ  Returns the FITS file header(s) in a readable format.

        Parameters
        ----------
        extensions : list of int or str, optional
            Format only specific HDU(s), identified by number or name.
            The name can be composed of the "EXTNAME" or "EXTNAME,EXTVER"
            keywords.

        keywords : list of str, optional
            Keywords for which the value(s) should be returned.
            If not specified, then the entire header is returned.

        compressed : boolean, optional
            If True, shows the header describing the compression, rather than
            the header obtained after decompression. (Affects FITS files
            containing `CompImageHDU` extensions only.)

        Returns
        -------
        formatted_header : str or astropy.table.Table
            Traditional 80-char wide format in the case of `HeaderFormatter`;
            an Astropy Table object in the case of `TableHeaderFormatter`.
        t   ,i   i    i’’’’N(
   t   NoneR   t   lenR   t   appendt   intt
   ValueErrort   splitt   joint   _parse_internal(	   R   t
   extensionst   keywordst
   compressedt   hdukeyst   extt   partst   extnamet   extver(    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyt   parseQ   s    c   	      C` sæ   g  } x© t  |  D] \ } } y |  j | | |  } Wn t k
 rN q n X| d k rk | j d  n  | j d j | |  j   x$ | D] } | j d j |   q Wq Wd j |  S(   sN   The meat of the formatting; in a separate method to allow overriding.
        i    s   
s   # HDU {} in {}:
s   {}
t    (   t	   enumeratet
   _get_cardsR   R   t   formatR   R   (	   R   R   R   R   t   resultt   idxt   hdut   cardst   c(    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyR      s    c   
   	   C` s#  y- | r |  j  | j } n |  j  | j } WnE t t f k
 rt d j |  j |  } t j |  t	 |   n X| s | j
 } n g  } x | D] } yC | j
 | } t | t j j  rĻ | j |  n | j |  Wq t k
 r}	 t j d j d |  j d | d |   q Xq W| S(   sŖ  Returns a list of `astropy.io.fits.card.Card` objects.

        This function will return the desired header cards, taking into
        account the user's preference to see the compressed or uncompressed
        version.

        Parameters
        ----------
        hdukey : int or str
            Key of a single HDU in the HDUList.

        keywords : list of str, optional
            Keywords for which the cards should be returned.

        compressed : boolean, optional
            If True, shows the header describing the compression.

        Raises
        ------
        ExtensionNotFoundException
            If the hdukey does not correspond to an extension.
        s   {0}: Extension {1} not found.s2   {filename} (HDU {hdukey}): Keyword {kw} not found.R   t   hdukeyt   kw(   R   t   _headert   headert
   IndexErrort   KeyErrorR%   R   R   t   warningR   R)   t
   isinstanceR   t   cardt   CardR   t   extend(
   R   R+   R   R   R.   t   messageR)   R,   t   crdt   e(    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyR$      s0    		N(	   R   R   R	   R   R   t   FalseR!   R   R$   (    (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyR
   ;   s
   	/	t   TableHeaderFormatterc           B` s   e  Z d  Z d   Z RS(   s  Class to convert the header(s) of a FITS file into a Table object.
    The table returned by the `parse` method will contain four columns:
    filename, hdu, keyword, and value.

    Subclassed from HeaderFormatter, which contains the meat of the formatting.
    c         C` s®   g  } x~ | D]v } y\ xU |  j  | | |  D]> } | j i |  j d 6| d 6| j d 6t | j  d 6 q, WWq t k
 r q Xq W| rŖ d d l m } | j	 |  Sd S(   s6   Method called by the parse method in the parent class.R   R(   t   keywordt   valuei   (   t   tableN(   R$   R   R   R;   t   strR<   R   R"   R=   t   TableR   (   R   R   R   R   t	   tablerowsR(   R3   R=   (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyR   Ń   s    
(   R   R   R	   R   (    (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyR:   Ź   s   c         C` s„   x t  |  j  D] \ } } | d k r< |  j r< t   n  y8 t |  } t | j |  j |  j |  j  d d Wq t k
 r } t	 j
 t |   q Xq Wd S(   s¾   Prints FITS header(s) using the traditional 80-char format.

    Parameters
    ----------
    args : argparse.Namespace
        Arguments passed from the command-line as defined below.
    i    t   endR"   N(   R#   R   R   t   printR
   R!   R   R   t   IOErrorR   t   errorR>   (   t   argsR'   R   t	   formatterR8   (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyt   print_headers_traditionalä   s    
c         C` sų   g  } x~ |  j  D]s } yD t |  } | j |  j |  j |  j  } | rY | j |  n  Wq t k
 r } t j	 t
 |   q Xq Wt |  d k r t St |  d k r¼ | d } n d d l m } | j |  } | j t j d |  j d S(   s¼   Prints FITS header(s) in a machine-readable table format.

    Parameters
    ----------
    args : argparse.Namespace
        Arguments passed from the command-line as defined below.
    i    i   i   (   R=   R%   N(   R   R:   R!   R   R   R   R   RC   R   RD   R>   R   R9   R"   R=   t   vstackt   writet   syst   stdout(   RE   t   tablesR   RF   t   tblR8   t   resulting_tableR=   (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyt   print_headers_as_tableų   s$    c         C` s3  d d l  } | j d d  } | j d d d d d	 d
 d d d d | j d d d d d	 d
 d d d d | j d d d d d t d d d d | j d d d	 d d d | j d d d  d d! | j |   }  |  j d k rõ d" |  _ n  y$ |  j rt |   n
 t |   Wn t	 k
 r.} n Xd S(#   s<   This is the main function called by the `fitsheader` script.i    Nt   descriptionsß   Print the header(s) of a FITS file. Optional arguments allow the desired extension(s), keyword(s), and output format to be specified. Note that in the case of a compressed image, the decompressed header is shown by default.s   -es   --extensiont   metavart   HDUt   actionR   t   destR   t   helpsd   specify the extension by name or number; this argument can be repeated to select multiple extensionss   -ks	   --keywordt   KEYWORDR   se   specify a keyword; this argument can be repeated to select multiple keywords; also supports wildcardss   -ts   --tablet   nargst   ?t   defaultt   FORMATs   print the header(s) in machine-readable table format; the default format is "ascii.fixed_width" (can be "ascii.csv", "ascii.html", "ascii.latex", "fits", etc)s   -cs   --compressedt
   store_truesd   for compressed image data, show the true header which describes the compression rather than the dataR   t   +s2   path to one or more files; wildcards are supporteds   ascii.fixed_width(
   t   argparset   ArgumentParsert   add_argumentR9   t
   parse_argsR=   R   RO   RG   RC   (   RE   R]   t   parserR8   (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyt   main  s2    			(   R	   t
   __future__R    R   R   RJ   R"   R   R   t   extern.six.movesR   t	   ExceptionR   t   objectR
   R:   RG   RO   R   Rb   (    (    (    sA   lib/python2.7/site-packages/astropy/io/fits/scripts/fitsheader.pyt   <module>,   s   		 