
[c           @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d d l m Z m	 Z	 m
 Z
 d d l m Z d d l m Z m Z m Z m Z d d l m Z d d	 l m Z m Z m Z m Z d d
 l m Z m Z m Z d d l m Z d d l m  Z  d d l! m" Z" d Z# e j$ e d   Z% e& d   e d d  D  Z' d d d Z( d d g Z) d e* f d     YZ+ e j, j- e+  e j. j- e+  d e* f d     YZ/ e j0 j- e/  e j1 j- e/  d e/ f d     YZ2 d e/ f d     YZ3 d   Z4 d    Z5 d S(!   i(   t   divisionNi   (   t   Cardt   _padt   KEYWORD_LENGTH(   t   _File(   t   encode_asciit   decode_asciit   fileobj_closedt   fileobj_is_binaryi   (   t   six(   t   string_typest
   itervaluest	   iteritemst   next(   t   zipt   ranget   zip_longest(   t
   isiterable(   t   AstropyUserWarning(   t   deprecated_renamed_argumenti@  sA   (?:(?P<valid>END {77}) *)|(?P<invalid>END$|END {0,76}[^A-Z0-9_-])c         c   s   |  ] } t  |  Vq d  S(   N(   t   chr(   t   .0t   x(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pys	   <genexpr>%   s    i    i   t   ENDt    iM   t   Headers   Header.*c           B   s}  e  Z d  Z g  e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z e d    Z e d    Z e d    Z e j d    Z e d d   Z e d e e d   Z e d    Z e d    Z d e e d  Z e d d d  d e e e d   Z e e d   Z e d d d  e e d   Z d   Z  e d  Z! d   Z" d    Z# e dE d!   Z% dE d"  Z& dE dE dE dE d#  Z' d$   Z( d%   Z) d&   Z* d'   Z+ d(   Z, d)   Z- d*   Z. dE d+  Z/ d,   Z0 d-   Z1 dE e e e d.  Z2 e e e e e e e d/  Z3 d0   Z4 dE dE d1  Z5 e e d2  Z6 e e d3  Z7 e d4  Z8 dE dE d5  Z9 dE dE d6  Z: d dE dE d7  Z; d8   Z< d9   Z= d:   Z> dE dE e d;  Z? e d<  Z@ d=   ZA d>   ZB d?   ZC d@   ZD dA   ZE dB   ZF dC   ZG dE dE dD  ZH eI jJ s{e* Z, e+ Z1 e) Z( [* [+ [) n  RS(F   s  
    FITS header class.  This class exposes both a dict-like interface and a
    list-like interface to FITS headers.

    The header may be indexed by keyword and, like a dict, the associated value
    will be returned.  When the header contains cards with duplicate keywords,
    only the value of the first card with the given keyword will be returned.
    It is also possible to use a 2-tuple as the index in the form (keyword,
    n)--this returns the n-th value with that keyword, in the case where there
    are duplicate keywords.

    For example::

        >>> header['NAXIS']
        0
        >>> header[('FOO', 1)]  # Return the value of the second FOO keyword
        'foo'

    The header may also be indexed by card number::

        >>> header[0]  # Return the value of the first card in the header
        'T'

    Commentary keywords such as HISTORY and COMMENT are special cases: When
    indexing the Header object with either 'HISTORY' or 'COMMENT' a list of all
    the HISTORY/COMMENT values is returned::

        >>> header['HISTORY']
        This is the first history entry in this header.
        This is the second history entry in this header.
        ...

    See the Astropy documentation for more details on working with headers.
    c         C   s   |  j    t | t  r: | r. | j   } n  | j } n! t | t  r[ t j |  } n  x! | D] } |  j | d t	 qb Wt
 |  _ d S(   sG  
        Construct a `Header` from an iterable and/or text file.

        Parameters
        ----------
        cards : A list of `Card` objects, optional
            The cards to initialize the header with. Also allowed are other
            `Header` (or `dict`-like) objects.

            .. versionchanged:: 1.2
                Allowed ``cards`` to be a `dict`-like object.

        copy : bool, optional

            If ``True`` copies the ``cards`` if they were another `Header`
            instance.
            Default is ``False``.

            .. versionadded:: 1.3
        t   endN(   t   cleart
   isinstanceR   t   copyt   cardst   dictR	   R   t   appendt   Truet   Falset	   _modified(   t   selfR   R   t   card(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __init__P   s    
c         C   s   t  |  j  S(   N(   t   lent   _cards(   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __len__s   s    c         c   s    x |  j  D] } | j Vq
 Wd  S(   N(   R(   t   keyword(   R$   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __iter__v   s    c         C   sR   | |  j  k s | |  j k r" t Sy |  j |  Wn t t f k
 rM t SXt S(   N(   t   _keyword_indicest   _rvkc_indicesR!   t
   _cardindext   KeyErrort
   IndexErrorR"   (   R$   R*   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __contains__z   s    c         C   s"  t  | t  r< t g  |  j | D] } t j |  ^ q   S|  j |  r t g  |  j |  D] } t j |  j |  ^ q^  St  | t  r | j   t	 j
 k r | j   } t |  |  St  | t  r | d } n | } |  j |  j |  } | j d  k	 r| | j k r| j S| j S(   Ni    (   R   t   sliceR   R(   R   t   _haswildcardt   _wildcardmatchR
   t   upperR   t   _commentary_keywordst   _HeaderCommentaryCardst   tupleR.   t   field_specifiert   Nonet
   rawkeywordt   rawvaluet   value(   R$   t   keyt   ct   idxR*   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __getitem__   s     -0c         C   s  |  j  | | |   r d  St | t  r d t |  k  oE d k n sY t d   n  t |  d k r | d d  } } | d  k r d } q q t |  d k r | \ } } | d  k r d } n  | d  k r d } q q n d  } d  } t | t  r|  j | } n( t | t  r5|  j |  j |  } n  | rt| | _	 | d  k	 r\| | _
 n  | j rt |  _ qn |  j | | | f  d  S(   Ni    i   s   A Header item may be set with either a scalar value, a 1-tuple containing a scalar value, or a 2-tuple containing a scalar value and comment string.i   t    (   t
   _set_sliceR   R8   R'   t
   ValueErrorR:   t   intR(   R.   R=   t   commentR#   R!   t   _update(   R$   R>   R=   RF   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __setitem__   s:    "			c         C   s  t  | t  s |  j |  r t  | t  rr t | j t |      } | j r | j d k  r t |  } q n |  j |  } x t |  D] } |  | =q Wd  St  | t	  r/t
 j |  } |  j } | |  j k r |  j } n  | | k r	t d j |    n  x t | |  D] } |  | =qWd  S|  j |  } |  j | } | j } |  j | =t
 j |  } |  j | } | j |  | s|  j | =n  | j d  k	 r|  j | j } | j |  | s|  j | j =qn  |  j | d t t |  _ d  S(   Ni    s   Keyword '{}' not found.t	   increment(   R   R2   R3   R   t   indicesR'   t   stept   reversedR4   R
   R   t   normalize_keywordR,   R-   R/   t   formatR.   R(   R*   t   removeR9   R:   R;   t   _updateindicesR"   R!   R#   (   R$   R>   RJ   R@   R%   R*   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __delitem__   sF    		
c         C   s   |  j  d d d t d t  S(   Nt   seps   
t   endcardt   padding(   t   tostringR"   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __repr__   s    c         C   s
   |  j    S(   N(   RU   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __str__  s    c         C   s   t  |   t  |  k S(   sg   
        Two Headers are equal only if they have the exact same string
        representation.
        (   t   str(   R$   t   other(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __eq__  s    c         C   s#   |  j  d t  } | j |  | S(   Nt   strip(   R   R"   t   extend(   R$   RY   t   temp(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __add__  s    c         C   s   |  j  |  |  S(   N(   R\   (   R$   RY   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __iadd__  s    c         C   s
   t  |   S(   s   
        The underlying physical cards that make up this Header; it can be
        looked at, but it should not be modified directly.
        (   t   _CardAccessor(   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s    c         C   s
   t  |   S(   sS  
        View the comments associated with each keyword, if any.

        For example, to see the comment on the NAXIS keyword:

            >>> header.comments['NAXIS']
            number of data axes

        Comments can also be updated through this interface:

            >>> header.comments['NAXIS'] = 'Number of data axes'

        (   t   _HeaderComments(   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   comments  s    c         C   s:   t  d   |  j D  } | r/ t |  j d <n  |  j d S(   s   
        Whether or not the header has been modified; this is a property so that
        it can also check each card for modifications--cards may have been
        modified directly without the header containing it otherwise knowing.
        c         s   s   |  ] } | j  Vq d  S(   N(   R#   (   R   R?   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pys	   <genexpr>8  s    R#   (   t   anyR(   R!   t   __dict__(   R$   t   modified_cards(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR#   0  s    c         C   s   | |  j  d <d  S(   NR#   (   Rd   (   R$   t   val(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR#   @  s    RB   c   	      C   sw  g  } t  |  j t  } d } g  } x| t |  k  rD| rR | t j } n6 y | j | |  } Wn t k
 r t |  } n X| | | !} | t |  } | r | d  d k r | j |  q* n  | j t j	 d j
 |    n  | r| t k r8g  } Pq8n) | j |  d j   d k r8g  } Pn  | g } q* W| rm| j t j	 d j
 |    n  |  |  S(   s  
        Creates an HDU header from a byte string containing the entire header
        data.

        Parameters
        ----------
        data : str
           String containing the entire header.

        sep : str, optional
            The string separating cards from each other, such as a newline.  By
            default there is no card separator (as is the case in a raw FITS
            file).

        Returns
        -------
        header
            A new `Header` instance.
        i    i   t   CONTINUERB   R   (   t   sett   issubsett   VALID_HEADER_CHARSR'   R   t   lengtht   indexRD   R    t
   fromstringt   joint   END_CARDt   splitt   rstrip(	   t   clst   dataRR   R   t   require_full_cardlengthR@   t   imaget   end_idxt
   next_image(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRm   D  s:    ""c            s   t  } t   t  r- t   d    t } n  z< t    }   f d   } |  j | | | | |  d SWd | r   j   n  Xd S(   s{  
        Similar to :meth:`Header.fromstring`, but reads the header string from
        a given file-like object or filename.

        Parameters
        ----------
        fileobj : str, file-like
            A filename or an open file-like object from which a FITS header is
            to be read.  For open file handles the file pointer must be at the
            beginning of the header.

        sep : str, optional
            The string separating cards from each other, such as a newline.  By
            default there is no card separator (as is the case in a raw FITS
            file).

        endcard : bool, optional
            If True (the default) the header must end with an END card in order
            to be considered valid.  If an END card is not found an
            `IOError` is raised.

        padding : bool, optional
            If True (the default) the header will be required to be padded out
            to a multiple of 2880, the FITS header block size.  Otherwise any
            padding, or lack thereof, is ignored.

        Returns
        -------
        header
            A new `Header` instance.
        t   rc         3   s/   x( t  r*   j |   } | r& | Vq Pq Wd  S(   N(   R!   t   read(   t   nbytesRs   (   t   fileobj(    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt
   block_iter  s
    	i   N(   R"   R   R
   t   openR!   R   t   _from_blockst   close(   Rr   R{   RR   RS   RT   t
   close_filet	   is_binaryR|   (    (   R{   s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   fromfile  s    "		c         C   s  t  |  } t j t |  } | |  } y t |  }	 Wn t k
 rW t    n X| sm t |	  }	 n  g  }
 t } t } x t	 r|  j
 |	 |  \ } }	 |
 j t |	   | r Pn  y t |  }	 Wn t k
 r t	 } Pn X|	 s t	 } Pn  | s t |	  }	 q q W| r3| r3| r3t d   n  d j |
  } | r| d d k r| r| j d  d k rt j d t  t    qt j d t  | j d d  n  | rt |  | d k rt |  | t } t d	 j t |    n  | |  j | d
 | f S(   s_  
        The meat of `Header.fromfile`; in a separate method so that
        `Header.fromfile` itself is just responsible for wrapping file
        handling.  Also used by `_BaseHDU.fromstring`.

        ``block_iter`` should be a callable which, given a block size n
        (typically 2880 bytes as used by the FITS standard) returns an iterator
        of byte strings of that block size.

        ``is_binary`` specifies whether the returned blocks are bytes or text

        Returns both the entire header *string*, and the `Header` object
        returned by Header.fromstring on that string.
        s   Header missing END card.RB   is    sh   Unexpected extra padding at the end of the file.  This padding may not be preserved when saving changes.s   Header block contains null bytes instead of spaces for padding, and is not FITS-compliant. Nulls may be replaced with spaces upon writing.R   i    s'   Header size is not multiple of {0}: {1}RR   (   t   _block_sizeR   Rk   R'   R   t   StopIterationt   EOFErrorR   R"   R!   t   _find_end_cardR    R   t   IOErrorRn   R[   t   warningst   warnR   t   replacet
   BLOCK_SIZERD   RN   Rm   (   Rr   R|   R   RR   RS   RT   t   actual_block_sizet   clent   blockst   blockt   read_blockst   is_eoft	   end_foundt
   header_strt
   actual_len(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR~     s\    		c         C   s   x t  j |  D] } | j   | d k r2 q n  | j d  r | j   } | | d | | d !j   } | r t |  j d  } t j d j	 |  t
  n t j d j	 t  t
  | |  t t  | | t t  } n  t | f SWt | f S(   s   
        Utility method to search a header block for the END card and handle
        invalid END cards.

        This method can also returned a modified copy of the input header block
        in case an invalid end card needs to be sanitized.
        i    t   invalidi   t   ubs^   Unexpected bytes trailing END keyword: {0}; these bytes will be replaced with spaces on write.s   Missing padding to end of the FITS block after the END keyword; additional spaces will be appended to the file upon writing to pad out to {0} bytes.(   t   HEADER_END_REt   finditert   startt   groupRq   t   reprt   lstripR   R   RN   R   R   R   Ro   R'   R!   R"   (   Rr   R   t   card_lent   mot   offsett   trailing(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   (  s$    
c         C   s   g  } xK |  j  D]@ } t |  } x+ | rO | j | t j   | t j } q% Wq W| j |  } | r | | t d  7} n  | r | d t t |   7} n  | S(   s  
        Returns a string representation of the header.

        By default this uses no separator between cards, adds the END card, and
        pads the string with spaces to the next multiple of 2880 bytes.  That
        is, it returns the header exactly as it would appear in a FITS file.

        Parameters
        ----------
        sep : str, optional
            The character or string with which to separate cards.  By default
            there is no separator, but one could use ``'\\n'``, for example, to
            separate each card with a new line

        endcard : bool, optional
            If True (default) adds the END card to the end of the header
            string

        padding : bool, optional
            If True (default) pads the string with spaces out to the next
            multiple of 2880 characters

        Returns
        -------
        s : str
            A string representing a FITS header.
        R   R   (	   R(   RX   R    R   Rk   Rn   R   t   _pad_lengthR'   (   R$   RR   RS   RT   t   linesR%   t   s(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRU   U  s    	t   clobbert	   overwrites   2.0c   
      C   s*  t  |  } t | t  s6 t | d d d | } n  z |  j d | d | d |  } t |  } | r t |  | d k r t d j t |  | t t    n  | j	 s| j
   y | j   }	 Wn t t f k
 r d }	 n X| j | j d	   | j
   n  Wd
 | r%| j   n  Xd
 S(   s.  
        Writes the header to file or file-like object.

        By default this writes the header exactly as it would be written to a
        FITS file, with the END card included and padding to the next multiple
        of 2880 bytes.  However, aspects of this may be controlled.

        Parameters
        ----------
        fileobj : str, file, optional
            Either the pathname of a file, or an open file handle or file-like
            object

        sep : str, optional
            The character or string with which to separate cards.  By default
            there is no separator, but one could use ``'\\n'``, for example, to
            separate each card with a new line

        endcard : bool, optional
            If `True` (default) adds the END card to the end of the header
            string

        padding : bool, optional
            If `True` (default) pads the string with spaces out to the next
            multiple of 2880 characters

        overwrite : bool, optional
            If ``True``, overwrite the output file if it exists. Raises an
            ``OSError`` (``IOError`` for Python 2) if ``False`` and the
            output file exists. Default is ``False``.

            .. versionchanged:: 1.3
               ``overwrite`` replaces the deprecated ``clobber`` argument.
        t   modet   ostreamR   RR   RS   RT   i    s6   Header size ({}) is not a multiple of block size ({}).t   asciiN(   R   R   R   RU   R   R'   R   RN   R   t   simulateonlyt   flusht   tellt   AttributeErrort   writet   encodeR   (
   R$   R{   RR   RS   RT   R   R   R   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   tofile  s*    &	

c         C   s   |  j  | d d d | d t S(   s  
        Read a header from a simple text file or file-like object.

        Equivalent to::

            >>> Header.fromfile(fileobj, sep='\n', endcard=False,
            ...                 padding=False)

        See Also
        --------
        fromfile
        RR   s   
RS   RT   (   R   R"   (   Rr   R{   RS   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   fromtextfile  s    c      
   C   s)   |  j  | d d d | d t d | d S(   s  
        Write the header as text to a file or a file-like object.

        Equivalent to::

            >>> Header.tofile(fileobj, sep='\n', endcard=False,
            ...               padding=False, overwrite=overwrite)

        .. versionchanged:: 1.3
           ``overwrite`` replaces the deprecated ``clobber`` argument.

        See Also
        --------
        tofile
        RR   s   
RS   RT   R   N(   R   R"   (   R$   R{   RS   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt
   totextfile  s    c         C   s1   g  |  _  t j t  |  _ t j t  |  _ d S(   s3   
        Remove all cards from the header.
        N(   R(   t   collectionst   defaultdictt   listR,   R-   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s    	c         C   s0   t  d   |  j D  } | r, | j   n  | S(   s  
        Make a copy of the :class:`Header`.

        .. versionchanged:: 1.3
            `copy.copy` and `copy.deepcopy` on a `Header` will call this
            method.

        Parameters
        ----------
        strip : bool, optional
           If `True`, strip any headers that are specific to one of the
           standard HDU types, so that this header can be used in a different
           HDU.

        Returns
        -------
        header
            A new :class:`Header` instance.
        c         s   s   |  ] } t  j  |  Vq d  S(   N(   R   (   R   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pys	   <genexpr>  s    (   R   R(   t   _strip(   R$   R[   t   tmp(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s    c         C   s
   |  j    S(   N(   R   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __copy__  s    c         O   s
   |  j    S(   N(   R   (   R$   t   argst   kwargs(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __deepcopy__  s    c         C   sM   |    } t  | t  s$ | f } n  x" | D] } | j | f |  q+ W| S(   ss  
        Similar to :meth:`dict.fromkeys`--creates a new `Header` from an
        iterable of keywords and an optional default value.

        This method is not likely to be particularly useful for creating real
        world FITS headers, but it is useful for testing.

        Parameters
        ----------
        iterable
            Any iterable that returns strings representing FITS keywords.

        value : optional
            A default value to assign to each keyword; must be a valid type for
            FITS keywords.

        Returns
        -------
        header
            A new `Header` instance.
        (   R   R8   R    (   Rr   t   iterableR=   t   dR>   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   fromkeys  s    	c         C   s+   y |  | SWn t  t f k
 r& | SXd S(   s;  
        Similar to :meth:`dict.get`--returns the value associated with keyword
        in the header, or a default value if the keyword is not found.

        Parameters
        ----------
        key : str
            A keyword that may or may not be in the header.

        default : optional
            A default value to return if the keyword is not found in the
            header.

        Returns
        -------
        value
            The value associated with the given keyword, or the default value
            if the keyword is not in the header.
        N(   R/   R0   (   R$   R>   t   default(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   get1  s    c   	      C   s[  t  |  t k rQ t j j |  rQ | |  j k rQ t | | |  } | j } n | } | t j k r
| |  k r
| d k r |  j	 | } n  | d k r |  | } n  | | f |  | <| d k	 s | d k	 rW|  j
 |  j |  } |  j | d | d | d t qWnM | d k	 s"| d k	 rG|  j | | | f d | d | n | | f |  | <d S(   s  
        Set the value and/or comment and/or position of a specified keyword.

        If the keyword does not already exist in the header, a new keyword is
        created in the specified position, or appended to the end of the header
        if no position is specified.

        This method is similar to :meth:`Header.update` prior to Astropy v0.1.

        .. note::
            It should be noted that ``header.set(keyword, value)`` and
            ``header.set(keyword, value, comment)`` are equivalent to
            ``header[keyword] = value`` and
            ``header[keyword] = (value, comment)`` respectively.

            New keywords can also be inserted relative to existing keywords
            using, for example::

                >>> header.insert('NAXIS1', ('NAXIS', 2, 'Number of axes'))

            to insert before an existing keyword, or::

                >>> header.insert('NAXIS', ('NAXIS1', 4096), after=True)

            to insert after an existing keyword.

            The only advantage of using :meth:`Header.set` is that it
            easily replaces the old usage of :meth:`Header.update` both
            conceptually and in terms of function signature.

        Parameters
        ----------
        keyword : str
            A header keyword

        value : str, optional
            The value to set for the given keyword; if None the existing value
            is kept, but '' may be used to set a blank value

        comment : str, optional
            The comment to set for the given keyword; if None the existing
            comment is kept, but ``''`` may be used to set a blank comment

        before : str, int, optional
            Name of the keyword, or index of the `Card` before which this card
            should be located in the header.  The argument ``before`` takes
            precedence over ``after`` if both specified.

        after : str, int, optional
            Name of the keyword, or index of the `Card` after which this card
            should be located in the header.

        t   beforet   afterR   N(   R'   R   R   t   _keywd_FSC_REt   matchR,   R*   R6   R:   Rb   R(   R.   t   _relativeinsertR!   (	   R$   R*   R=   RF   R   R   t   new_cardt   new_keywordR%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRh   K  s*    =
c         C   s   t  t |    S(   s   Like :meth:`dict.items`.(   R   R   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   items  s    c         c   s)   x" |  j  D] } | j | j f Vq
 Wd S(   s   Like :meth:`dict.iteritems`.N(   R(   R*   R=   (   R$   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s    c         C   s
   |  j    S(   sz   
        Like :meth:`dict.iterkeys`--iterating directly over the `Header`
        instance has the same behavior.
        (   R+   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   iterkeys  s    c         c   s&   x t  |   D] \ } } | Vq Wd S(   s   Like :meth:`dict.itervalues`.N(   R   (   R$   t   _t   v(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s    c         C   s   g  |  D] } | ^ q S(   s   
        Return a list of keywords in the header in the order they
        appear--like :meth:`dict.keys` but ordered.
        (    (   R$   R*   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   keys  s    c         G   s   t  |  d k r0 t d j t  |     n  t  |  d k rK d } n
 | d } y |  | } Wn4 t t f k
 r t  |  d k r | d S  n X|  | =| S(   s   
        Works like :meth:`list.pop` if no arguments or an index argument are
        supplied; otherwise works like :meth:`dict.pop`.
        i   s/   Header.pop expected at most 2 arguments, got {}i    ii   (   R'   t	   TypeErrorRN   R/   R0   (   R$   R   R>   R=   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   pop  s    		
c         C   sM   y t  t |    \ } } Wn t k
 r; t d   n X|  | =| | f S(   s    Similar to :meth:`dict.popitem`.s   Header is empty(   R   R   R   R/   (   R$   t   kR   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   popitem  s    c         C   s4   y |  | SWn! t  t f k
 r/ | |  | <n X| S(   s#   Similar to :meth:`dict.setdefault`.(   R/   R0   (   R$   R>   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt
   setdefault  s
    c   	         s_  | r | d } n d }   f d   } | d k r7 nt | d  rs x t |  D] \ } } | | |  qS Wn t | d  r x | j   D] } | | | |  q Wn x t |  D] \ } } t | t  r   j |  q t | t  r,d t	 |  k  od k n r,  j t |    q t
 d j |    q W| r[  j |  n  d S(	   s  
        Update the Header with new keyword values, updating the values of
        existing keywords and appending new keywords otherwise; similar to
        `dict.update`.

        `update` accepts either a dict-like object or an iterable.  In the
        former case the keys must be header keywords and the values may be
        either scalar values or (value, comment) tuples.  In the case of an
        iterable the items must be (keyword, value) tuples or (keyword, value,
        comment) tuples.

        Arbitrary arguments are also accepted, in which case the update() is
        called again with the kwargs dict as its only argument.  That is,

        ::

            >>> header.update(NAXIS1=100, NAXIS2=100)

        is equivalent to::

            header.update({'NAXIS1': 100, 'NAXIS2': 100})

        .. warning::
            As this method works similarly to `dict.update` it is very
            different from the ``Header.update()`` method in Astropy v0.1.
            Use of the old API was
            **deprecated** for a long time and is now removed. Most uses of the
            old API can be replaced as follows:

            * Replace ::

                  header.update(keyword, value)

              with ::

                  header[keyword] = value

            * Replace ::

                  header.update(keyword, value, comment=comment)

              with ::

                  header[keyword] = (value, comment)

            * Replace ::

                  header.update(keyword, value, before=before_keyword)

              with ::

                  header.insert(before_keyword, (keyword, value))

            * Replace ::

                  header.update(keyword, value, after=after_keyword)

              with ::

                  header.insert(after_keyword, (keyword, value),
                                after=True)

            See also :meth:`Header.set` which is a new method that provides an
            interface similar to the old ``Header.update()`` and may help make
            transition a little easier.

        i    c            sz   t  | t  s! t |  |  } nH d t |  k  o> d k n rY t |  f |   } n t d |      j |  d  S(   Ni    i   s   Header update value for key %r is invalid; the value must be either a scalar, a 1-tuple containing the scalar value, or a 2-tuple containing the value and a comment string.(   R   R8   R   R'   RD   RG   (   R   R   R%   (   R$   (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   update_from_dict:  s    "
R   R   i   i   s   Header update sequence item #{} is invalid; the item must either be a 2-tuple containing a keyword and value, or a 3-tuple containing a keyword, value, and comment string.N(   R:   t   hasattrR   R   t	   enumerateR   R   RG   R8   R'   RD   RN   t   update(	   R$   R   R   RY   R   R   R   R@   R%   (    (   R$   s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s,    E1c         C   s#   g  t  |   D] \ } } | ^ q S(   s8   Returns a list of the values of all cards in the header.(   R   (   R$   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   values^  s    c         C   sC  t  | t  r t |  } n] t  | t  r< t |   } n? | d k rT t   } n' t  | t  s{ t d j |    n  | r | j r t } n  | r |  j	 j
 |  t |  j	  d } n t |  j	  d } x* | d k r |  j	 | j r | d 8} q W| rR| j t j k rRx6 | d k rN|  j	 | j t j k rN| d 8} qWn  | d 7} |  j	 j | |  |  j |  t j | j  } |  j | j
 |  | j d k	 r|  j | j j
 |  n  | s6| r| j t j k r|  j | j   n  | r6|  j   r6|  j t t |   t j  q6n  t |  _ d S(   s<  
        Appends a new keyword+value card to the end of the Header, similar
        to `list.append`.

        By default if the last cards in the Header have commentary keywords,
        this will append the new keyword before the commentary (unless the new
        keyword is also commentary).

        Also differs from `list.append` in that it can be called with no
        arguments: In this case a blank card is appended to the end of the
        Header.  In the case all the keyword arguments are ignored.

        Parameters
        ----------
        card : str, tuple
            A keyword or a (keyword, value, [comment]) tuple representing a
            single header card; the comment is optional in which case a
            2-tuple may be used

        useblanks : bool, optional
            If there are blank cards at the end of the Header, replace the
            first blank card so that the total number of cards in the Header
            does not increase.  Otherwise preserve the number of blank cards.

        bottom : bool, optional
            If True, instead of appending after the last non-commentary card,
            append after the last non-blank card.

        end : bool, optional
            If True, ignore the useblanks and bottom options, and append at the
            very end of the Header.

        sg   The value appended to a Header must be either a keyword or (keyword, value, [comment]) tuple; got: {!r}i   i    N(   R   R
   R   R8   R:   RD   RN   t   is_blankR!   R(   R    R'   R*   R6   t   insertRP   RM   R,   R9   R-   R;   t   sortt   _countblankst
   _useblanksRX   Rk   R#   (   R$   R%   t	   useblankst   bottomR   R@   R*   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR    c  sF    #	
&c	      	   C   s  t  |  }	 | r |	 j   n  t |   r> |  j d j }
 n d }
 g  } xt |	 j  D]|\ } } | j } | t j k r]| r | r | |  k r qZ q| rM| d k r| r| d k r |
 d k s | d k r |
 d k r |  d =|  j	 d |  qJ| j
 | j f |  | <qZ| |  k r=| j
 | j f |  | <qZ| j |  q| j |  qZ | si| r| |  k r| j r| j |  qZ n  xB |  | D] } | | j
 k rPqqW| j |  qZ | j |  qZ Wx- | D]% } |  j | d | d | d | qWd S(   s  
        Appends multiple keyword+value cards to the end of the header, similar
        to `list.extend`.

        Parameters
        ----------
        cards : iterable
            An iterable of (keyword, value, [comment]) tuples; see
            `Header.append`.

        strip : bool, optional
            Remove any keywords that have meaning only to specific types of
            HDUs, so that only more general keywords are added from extension
            Header or Card list (default: `True`).

        unique : bool, optional
            If `True`, ensures that no duplicate keywords are appended;
            keywords already in this header are simply discarded.  The
            exception is commentary keywords (COMMENT, HISTORY, etc.): they are
            only treated as duplicates if their values match.

        update : bool, optional
            If `True`, update the current header with the values and comments
            from duplicate keywords in the input header.  This supercedes the
            ``unique`` argument.  Commentary keywords are treated the same as
            if ``unique=True``.

        update_first : bool, optional
            If the first keyword in the header is 'SIMPLE', and the first
            keyword in the input header is 'XTENSION', the 'SIMPLE' keyword is
            replaced by the 'XTENSION' keyword.  Likewise if the first keyword
            in the header is 'XTENSION' and the first keyword in the input
            header is 'SIMPLE', the 'XTENSION' keyword is replaced by the
            'SIMPLE' keyword.  This behavior is otherwise dumb as to whether or
            not the resulting header is a valid primary or extension header.
            This is mostly provided to support backwards compatibility with the
            old ``Header.fromTxtFile`` method, and only applies if
            ``update=True``.

        useblanks, bottom, end : bool, optional
            These arguments are passed to :meth:`Header.append` while appending
            new cards to the header.
        i    t   SIMPLEt   XTENSIONR   R   R   N(   R   R   R'   R   R*   R:   R   R   R6   R   R=   RF   R    R   (   R$   R   R[   t   uniqueR   t   update_firstR   R   R   R]   t   firstt   extend_cardsR@   R%   R*   R=   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR\     sD    .		c         C   sG   t  j |  } | |  j k r6 t d j |    n  t |  j |  S(   s  
        Returns the count of the given keyword in the header, similar to
        `list.count` if the Header object is treated as a list of keywords.

        Parameters
        ----------
        keyword : str
            The keyword to count instances of in the header

        s   Keyword {!r} not found.(   R   RM   R,   R/   RN   R'   (   R$   R*   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   count!  s    c         C   s   | d k r d } n  | d k r3 t |  j  } n  | | k  rH d } n d } t j |  } xO t | | |  D]& } |  j | j j   | k rp | Sqp Wt d j	 |    d S(   s  
        Returns the index if the first instance of the given keyword in the
        header, similar to `list.index` if the Header object is treated as a
        list of keywords.

        Parameters
        ----------
        keyword : str
            The keyword to look up in the list of all keywords in the header

        start : int, optional
            The lower bound for the index

        stop : int, optional
            The upper bound for the index

        i    ii   s'   The keyword {!r} is not in the  header.N(
   R:   R'   R(   R   RM   R   R*   R5   RD   RN   (   R$   R*   R   t   stopRK   t   norm_keywordR@   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRl   6  s    			c   	      C   s;  t  | t  s! |  j |  } n | } | rX | d k rK t |  j  } qX | d 7} n  | t |  j  k r |  j | d t d St  | t  r t |  } nE t  | t	  r t |   } n' t  | t  s t
 d j |    n  |  j j | |  | j } | d k  r>| t |  j  d 7} | d k  r>d } q>n  |  j |  t j |  } |  j | j |  t |  j |  } | d k r| t j k rt j d j |  t  n  |  j | j   n  | j d k	 r|  j | j } | j |  | j   n  | r.|  j t t |   t j  n  t |  _ d S(   s  
        Inserts a new keyword+value card into the Header at a given location,
        similar to `list.insert`.

        Parameters
        ----------
        key : int, str, or tuple
            The index into the list of header keywords before which the
            new keyword should be inserted, or the name of a keyword before
            which the new keyword should be inserted.  Can also accept a
            (keyword, index) tuple for inserting around duplicate keywords.

        card : str, tuple
            A keyword or a (keyword, value, [comment]) tuple; see
            `Header.append`

        useblanks : bool, optional
            If there are blank cards at the end of the Header, replace the
            first blank card so that the total number of cards in the Header
            does not increase.  Otherwise preserve the number of blank cards.

        after : bool, optional
            If set to `True`, insert *after* the specified index or keyword,
            rather than before it.  Defaults to `False`.
        ii   R   Nsi   The value inserted into a Header must be either a keyword or (keyword, value, [comment]) tuple; got: {!r}i    sK   A {!r} keyword already exists in this header.  Inserting duplicate keyword.(   R   RE   R.   R'   R(   R    R!   R
   R   R8   RD   RN   R   R*   RP   RM   R,   R6   R   R   R   R   R9   R:   R-   R;   R   RX   Rk   R#   (	   R$   R>   R%   R   R   R@   R*   R   t   rvkc_indices(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   ]  sR    	#c         C   s   t  j |  } | |  j k rd |  |  j | d =| r x( | |  j k r] |  |  j | d =q9 Wq n | s t d j |    n  d S(   s  
        Removes the first instance of the given keyword from the header similar
        to `list.remove` if the Header object is treated as a list of keywords.

        Parameters
        ----------
        keyword : str
            The keyword of which to remove the first instance in the header.

        ignore_missing : bool, optional
            When True, ignores missing keywords.  Otherwise, if the keyword
            is not present in the header a KeyError is raised.

        remove_all : bool, optional
            When True, all instances of keyword will be removed.
            Otherwise only the first instance of the given keyword is removed.

        i    s   Keyword '{}' not found.N(   R   RM   R,   R/   RN   (   R$   R*   t   ignore_missingt
   remove_all(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRO     s    c         C   s   t  j |  } t  j |  } | d k r9 t d   n  | t  j k sW | t  j k r | t  j k or | t  j k s t d   q n+ | r | |  k r t d j |    n  |  j |  } |  j | } |  | =|  j | | | j | j	 f  d S(   s  
        Rename a card's keyword in the header.

        Parameters
        ----------
        oldkeyword : str or int
            Old keyword or card index

        newkeyword : str
            New keyword

        force : bool, optional
            When `True`, if the new keyword already exists in the header, force
            the creation of a duplicate keyword. Otherwise a
            `ValueError` is raised.
        Rg   s   Can not rename to CONTINUEs=   Regular and commentary keys can not be renamed to each other.s-   Intended keyword {} already exists in header.N(
   R   RM   RD   R6   RN   Rl   R   R   R=   RF   (   R$   t
   oldkeywordt
   newkeywordt   forceR@   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   rename_keyword  s     	c         C   s    |  j  d | d | d | d S(   s%  
        Add a ``HISTORY`` card.

        Parameters
        ----------
        value : str
            History text to be added.

        before : str or int, optional
            Same as in `Header.update`

        after : str or int, optional
            Same as in `Header.update`
        t   HISTORYR   R   N(   t   _add_commentary(   R$   R=   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   add_history  s    c         C   s    |  j  d | d | d | d S(   s  
        Add a ``COMMENT`` card.

        Parameters
        ----------
        value : str
            Text to be added.

        before : str or int, optional
            Same as in `Header.update`

        after : str or int, optional
            Same as in `Header.update`
        t   COMMENTR   R   N(   R   (   R$   R=   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   add_comment  s    c         C   s    |  j  d | d | d | d S(   s!  
        Add a blank card.

        Parameters
        ----------
        value : str, optional
            Text to be added.

        before : str or int, optional
            Same as in `Header.update`

        after : str or int, optional
            Same as in `Header.update`
        RB   R   R   N(   R   (   R$   R=   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt	   add_blank"  s    c   
      C   s  | \ } } } | j    } | j d  r7 | d } n  | t j k r | |  j k r |  j | d } |  j | } | | _ | d
 k	 r | | _ n  | j	 r}t
 |  _	 q}n | t j k rp|  j | |  } | |  j k rI|  j | d t |   d d d } | p| p| } xV t |  D]! }	 |  j | d |	 d | q!Wq}x1 | D] }	 |  j |	 d	 t
 qPWn |  j |  d
 S(   ss  
        The real update code.  If keyword already exists, its value and/or
        comment will be updated.  Otherwise a new card will be appended.

        This will not create a duplicate keyword except in the case of
        commentary cards.  The only other way to force creation of a duplicate
        is to use the insert(), append(), or extend() methods.
        s	   HIERARCH i	   i    R   i   R   iR   R   N(   R5   t
   startswithR   R6   R,   R(   R=   R:   RF   R#   R!   t   _splitcommentaryRl   R'   RL   R   R    (
   R$   R%   R*   R=   RF   R@   t   existing_cardR   t   isblankR?   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRG   4  s.    
		%"c         C   s  t  | t  r | } d } n t  | t  r | d k  rO | t |  j  7} n  | d k  sp | t |  j  k r t d   n  | St  | t  r | St  | t  r t |  d k s t  | d t  s t  | d t  r t d   n  | \ } } n t d   t	 j
 |  } |  j j | d
  } | r| rt |  t k sXd | k rpt d j |    q|  j j | d
  } n  | st d j |    n  y | | SWn/ t k
 rt d	 j t |  |    n Xd
 S(   s@   Returns an index into the ._cards list given a valid lookup key.i    s   Header index out of range.i   i   sS   Tuple indices must be 2-tuples consisting of a keyword string and an integer index.sA   Header indices must be either a string, a 2-tuple, or an integer.t   .s   Keyword {!r} not found.s+   There are only {} {!r} cards in the header.N(   R   R
   RE   R'   R(   R0   R2   R8   RD   R   RM   R,   R   R:   R   R/   RN   R-   (   R$   R>   R*   t   nRJ   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR.   _  s@    	!&		c         C   sa   | d k  r" | t  |  j  7} n  |  j | j } t j |  } |  j | j |  } | | f S(   sx  
        Given an integer index, return the (keyword, repeat) tuple that index
        refers to.  For most keywords the repeat will always be zero, but it
        may be greater than zero for keywords that are duplicated (especially
        commentary keywords).

        In a sense this is the inverse of self.index, except that it also
        supports duplicates.
        i    (   R'   R(   R*   R   RM   R,   Rl   (   R$   R@   R*   t   repeat(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   _keyword_from_index  s    c            s9    d k r |  n        f d   } | r  j | j  } |   } | t  j  k r | t  j  d k r d S  d k	 r | | d k r d Sn | d k	 r | | k r d S | =n  |   } | d t j k rt  j | d | d   }	 n	 | g }	 x |	 D] }
  j	 | |
  qWd S(   s   
        Inserts a new card before or after an existing card; used to
        implement support for the legacy before/after keyword arguments to
        Header.update().

        If replace=True, move an existing card with the same keyword.
        c             sY   t   t  o!  t  j  k s6  j   }  n  }    d  k rU |  d 7}  n  |  S(   Ni   (   R   RE   R'   R(   R.   R:   (   R@   (   R   t   insertionkeyR$   (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   get_insertion_idx  s    i   Ni    (
   R:   R.   R*   R'   R(   R   R6   RL   R   R   (   R$   R%   R   R   R   R   t   old_idxt   insertion_idxR@   R   R?   (    (   R   R   R$   s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s,    			
	#	c         C   s   | t  |  j  k r d S| r% d n d } xm |  j |  j f D]Y } xP t |  D]B } x9 t |  D]+ \ } } | | k rd | | c | 7<qd qd WqQ Wq> Wd S(   s   
        For all cards with index above idx, increment or decrement its index
        value in the keyword_indices dict.
        Ni   i(   R'   R(   R,   R-   R   R   (   R$   R@   RI   t
   index_setsRJ   t   jdxt   keyword_index(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRP     s    c         C   s@   x9 t  d t |  j   D] } |  j | j s | d Sq Wd S(   s;   Returns the number of blank cards at the end of the Header.i   i    (   R   R'   R(   R   (   R$   R@   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s    c         C   s6   x/ t  |  D]! } |  j d j r- |  d =q Pq Wd  S(   Ni(   R   R(   R   (   R$   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR     s    
c         C   s4   t  | t  o3 | j d  p3 d | k p3 d | k S(   s?   Return `True` if the input keyword contains a wildcard pattern.s   ...t   *t   ?(   R   R
   t   endswith(   R$   R*   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR3      s    c         C   s   | j  d d  j  d d  } | j  d d  d } t j | t j  } g  t |  j  D]$ \ } } | j | j  rY | ^ qY S(   s   
        Returns a list of indices of the cards matching the given wildcard
        pattern.

         * '*' matches 0 or more characters
         * '?' matches a single character
         * '...' matches 0 or more of any non-whitespace character
        R  s   .*R  R   s   ...s   \S*t   $(   R   t   ret   compilet   IR   R(   R   R*   (   R$   t   patternt
   pattern_reR@   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR4     s
    
c         C   s   t  | t  s |  j |  r t  | t  rK t | j t |     } n |  j |  } t  | t  sv t |  r t	 j
 | t |   } n  x' t | |  D] \ } } | | | <q Wt St S(   sT   
        Used to implement Header.__setitem__ and CardAccessor.__setitem__.
        (   R   R2   R3   R   RJ   R'   R4   R
   R   t	   itertoolsR   R   R!   R"   (   R$   R>   R=   t   targetRJ   R@   Rf   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRC     s    c         C   s   t  j t } t |  } t |  | k r@ t  | |  g } nP d } g  } xA | t |  k  r | j t  | | | | | !  | | 7} qO W| S(   s*  
        Given a commentary keyword and value, returns a list of the one or more
        cards needed to represent the full value.  This is primarily used to
        create the multiple commentary cards needed to represent a long value
        that won't fit into a single commentary card.
        i    (   R   Rk   R   RX   R'   R    (   R$   R*   R=   t   maxlent   valuestrR   R@   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   ,  s    !c         C   s  d |  k r |  d } n d } d |  k r8 |  d } n d } xA t  |  D]3 } y |  d t | d  =WqK t k
 r} qK XqK WxR d D]J } xA t  |  D]3 } y |  | t | d  =Wq t k
 r q Xq Wq Wx- d D]% } y |  | =Wq t k
 rq Xq Wd S(   s   
        Strip cards specific to a certain kind of header.

        Strip cards like ``SIMPLE``, ``BITPIX``, etc. so the rest of
        the header can be used to reconstruct another kind of header.
        t   NAXISi    t   TFIELDSi   t   TFORMt   TSCALt   TZEROt   TNULLt   TTYPEt   TUNITt   TDISPt   TDIMt   THEAPt   TBCOLR   R   t   BITPIXt   EXTENDt   PCOUNTt   GCOUNTt   GROUPSt   BSCALEt   BZERON(
   R  R  R  R  R  R  R  R  R  R  (   R   R   R  R  R  R  R  R  R   R!  R  (   R   RX   R/   (   R$   t   naxist   tfieldsR@   t   name(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   G  s2    
 
c         C   sH   | d k	 s | d k	 r: |  j | | f d | d | n
 | |  | <d S(   s   
        Add a commentary card.

        If ``before`` and ``after`` are `None`, add to the last occurrence
        of cards of the same name (except blank card).  If there is no
        card (or blank card), append at the end.
        R   R   N(   R:   R   (   R$   R>   R=   R   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   w  s    	
N(K   t   __name__t
   __module__t   __doc__R"   R&   R)   R+   R1   RA   RH   RQ   RV   RW   RZ   R^   R_   t   propertyR   Rb   R#   t   settert   classmethodRm   R!   R   R~   R   RU   R   R   R   R   R   R   R   R   R:   R   R   Rh   R   R   R   R   R   R   R   R   R   R   R    R\   R   Rl   R   RO   R   R   R   R   RG   R.   R   R   RP   R   R   R3   R4   RC   R   R   R   R	   t   PY2(    (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   ,   s   "#					'	:						E;b--	?				X							
		n	Z	c	'^'	+	3	;							0	R`   c           B   sV   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 RS(	   so  
    This is a generic class for wrapping a Header in such a way that you can
    use the header's slice/filtering capabilities to return a subset of cards
    and do something with them.

    This is sort of the opposite notion of the old CardList class--whereas
    Header used to use CardList to get lists of cards, this uses Header to get
    lists of cards.
    c         C   s   | |  _  d  S(   N(   t   _header(   R$   t   header(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR&     s    c         C   s   d j  d   |  j j D  S(   Ns   
c         s   s   |  ] } t  |  Vq d  S(   N(   R   (   R   R?   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pys	   <genexpr>  s    (   Rn   R,  R(   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRV     s    c         C   s   t  |  j j  S(   N(   R'   R,  R(   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR)     s    c         C   s   t  |  j j  S(   N(   t   iterR,  R(   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR+     s    c         C   sy   t  |  s t | t  rA t |   d k r: | g } qA t Sn  x1 t |  |  D] \ } } | | k rQ t SqQ Wt Sd  S(   Ni   (   R   R   R
   R'   R"   R   R!   (   R$   RY   t   at   b(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRZ     s    c         C   s   |  | k S(   N(    (   R$   RY   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   __ne__  s    c         C   sU   t  | t  s! |  j j |  r5 |  j |  j |  S|  j j |  } |  j j | S(   N(   R   R2   R,  R3   t	   __class__R.   R(   (   R$   t   itemR@   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRA     s    !c         C   s   t  | t  s! |  j j |  r t  | t  rN t | j t |      } n |  j j |  } t  | t  s| t	 |  r t
 j | t |   } n  x' t | |  D] \ } } | |  | <q Wt St S(   s   
        Helper for implementing __setitem__ on _CardAccessor subclasses; slices
        should always be handled in this same way.
        (   R   R2   R,  R3   R   RJ   R'   R4   R
   R   R  R   R   R!   R"   (   R$   R3  R=   RJ   R@   Rf   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt	   _setslice  s    !(   R%  R&  R'  R&   RV   R)   R+   RZ   R1  RA   R4  (    (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR`     s   								Ra   c           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sa  
    A class used internally by the Header class for the Header.comments
    attribute access.

    This object can be used to display all the keyword comments in the Header,
    or look up the comments on specific keywords.  It allows all the same forms
    of keyword lookup as the Header class itself, but returns comments instead
    of values.
    c         c   s#   x |  j  j D] } | j Vq Wd  S(   N(   R,  R(   RF   (   R$   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR+     s    c            sX   t    x, |  j j D] } t   t | j     q Wd j   f d   |  j j D  S(   s9   Returns a simple list of all keywords and their comments.s   
c         3   s-   |  ]# } d  j  | j | j d   Vq d S(   s   {:>{len}}  {}R'   N(   RN   R*   RF   (   R   R?   (   t   keyword_length(    s5   lib/python2.7/site-packages/astropy/io/fits/header.pys	   <genexpr>  s   (   R   R,  R(   t   maxR'   R*   Rn   (   R$   R%   (    (   R5  s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRV     s
    c         C   s2   t  t |   j |  } t | t  r+ | S| j S(   s   
        Slices and filter strings return a new _HeaderComments containing the
        returned cards.  Otherwise the comment of a single card is returned.
        (   t   superRa   RA   R   RF   (   R$   R3  (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRA     s    c         C   sR   |  j  j | | |   r d S|  j  j |  } |  j  | } | | f |  j  | <d S(   s   
        Set/update the comment on specified card or cards.

        Slice/filter updates work similarly to how Header.__setitem__ works.
        N(   R,  RC   R.   (   R$   R3  RF   R@   R=   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRH     s
    (   R%  R&  R'  R+   RV   RA   RH   (    (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRa     s
   			
	R7   c           B   sG   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z RS(   s   
    This is used to return a list-like sequence over all the values in the
    header for a given commentary keyword, such as HISTORY.
    RB   c         C   sY   t  t |   j |  | |  _ |  j j |  j  |  _ t |  j  j |  j  |  _	 d  S(   N(
   R7  R7   R&   t   _keywordR,  R   t   _countR2   RJ   t   _indices(   R$   R-  R*   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR&     s    	c         C   s   t  t |  j    S(   N(   R'   R   R:  (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR)   !  s    c         c   s3   x, t  |  j   D] } |  j |  j | f Vq Wd  S(   N(   R   R:  R,  R8  (   R$   R@   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR+   $  s    c         C   s   d j  |   S(   Ns   
(   Rn   (   R$   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRV   (  s    c         C   s   t  | t  r@ |  j |  j |  j  } | j |  j  | _ | St  | t  sj t	 d j
 |  j    n  t t |  j    | } |  j |  j | f S(   Ns   {} index must be an integer(   R   R2   R2  R,  R8  RJ   R9  R:  RE   RD   RN   R   R   (   R$   R@   R   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRA   +  s    c         C   s6   |  j  j | | |   r d S| |  j  |  j | f <d S(   s   
        Set the value of a specified commentary card or cards.

        Slice/filter updates work similarly to how Header.__setitem__ works.
        N(   R,  RC   R8  (   R$   R3  R=   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyRH   6  s    (	   R%  R&  R'  R&   R)   R+   RV   RA   RH   (    (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR7     s   				c         C   s   t  t |   t  t j d S(   sg   
    Determine the size of a FITS header block if a non-blank separator is used
    between cards.
    i   (   R   R'   R   Rk   (   RR   (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   E  s    c         C   s   t  |  t  t  S(   s?   Bytes needed to pad the input stringlen to the next FITS block.(   R   (   t	   stringlen(    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyR   N  s    (6   t
   __future__R    R   R   R  R  R   R%   R   R   R   t   fileR   t   utilR   R   R   R   t   externR	   t
   extern.sixR
   R   R   R   t   extern.six.movesR   R   R   t   utilsR   t   utils.exceptionsR   t   utils.decoratorsR   R   R  R   Rh   Rj   Ro   t   __doctest_skip__t   objectR   t   MutableSequencet   registert   MutableMappingR`   t   Mappingt   SequenceRa   R7   R   R   (    (    (    s5   lib/python2.7/site-packages/astropy/io/fits/header.pyt   <module>   sJ   ""	       lD62		