ó
šßÈ[c           @   sF  d  d l  m Z 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 Z d  d l	 Z	 d  d l
 Z
 d  d l Z d  d l m Z d  d l Z d  d l m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z d d	 l  m! Z! m" Z" d d
 l# m$ Z$ m% Z% d d l& m' Z' i d d 6d d 6d d 6d d 6d d 6d d 6Z( i d d 6d d 6d d 6d d 6d d 6d d 6Z) i d d 6d d 6d d 6d d 6d d 6Z* e d ƒ Z+ e d ƒ Z, e d ƒ Z- y d  d l. Z. Wn e/ k
 re0 Z1 n Xe2 Z1 d e3 f d „  ƒ  YZ4 d  „  Z5 d S(!   iÿÿÿÿ(   t   divisiont   with_statementN(   t   reduce(   t   memmapi   (
   t
   isreadablet
   iswritablet   isfilet   fileobj_opent   fileobj_namet   fileobj_closedt   fileobj_modet   _array_from_filet   _array_to_filet   _write_stringi   (   t   bt   string_types(   t   download_filet   _is_url(   t   classpropertyt   deprecated_renamed_argument(   t   AstropyUserWarningt   rbt   readonlyt   copyonwrites   rb+t   updates   ab+t   appendt   wbt   ostreamt	   denywrites   wb+t   abt   cs   r+t   rs   ‹s   PKt   BZt   _Filec           B   s  e  Z d  Z e d d d ƒ d d d e e d „ ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d d	 „ Z d d
 e j d d „ Z d „  Z d „  Z d „  Z d „  Z d
 d „ Z d „  Z d d „ Z d „  Z d
 d „ Z d „  Z d „  Z d „  Z d „  Z e d e ƒ d „  ƒ Z d „  Z RS(   sM   
    Represents a FITS file on disk (or in some other file-like object).
    t   clobbert	   overwrites   2.0c         C   s¯  t  | ƒ |  _ | d  k r! t n | } | d  k r‘ d  |  _ t |  _ t |  _ | |  _ | |  _	 d  |  _
 t |  _ t |  _ t |  _ t |  _ d  St |  _ t rÁ t | t j ƒ rÁ t | ƒ } n  d  |  _ | d  k r	t | ƒ r t | ƒ } t j | ƒ } q	d } n  | t k r-t d j | ƒ ƒ ‚ n  t | t ƒ rl| d k rlt | ƒ rlt | d | ƒ|  _ n t  | ƒ |  _ t |  _ t |  _ | |  _ | |  _	 t |  _! t |  _ d  |  _
 t |  _ t |  _ t | ƒ rî|  j" | | | ƒ n8 t | t ƒ r|  j# | | | ƒ n |  j$ | | | ƒ t |  j ƒ |  _ t | t% j& ƒ rVd |  _
 n< t | t' j( ƒ rtd |  _
 n t | t) j* ƒ r’d |  _
 n  | d k s³|  j
 r¿| d k r¿t |  _ n- | d k sà|  j
 rì| d k rìt |  _ n  | d k s|  j
 st+ |  j d ƒ r d |  _, nD |  j j- ƒ  } |  j j. d d ƒ |  j j- ƒ  |  _, |  j j. | ƒ |  j	 r«t/ |  j ƒ sˆt |  _	 q«|  j r«|  j0 r«t |  _	 q«n  d  S(   NR   s   Mode '{}' not recognizedR   R   t   cachet   gzipt   zipt   bzip2R   R   R   t   seeki    i   (   R   R   (   R   R   R   (1   t   boolt   strict_memmapt   Nonet   Truet   _filet   Falset   closedt   binaryt   modeR   t   compressionR   t	   writeonlyt   simulateonlyt   close_on_errort   HAS_PATHLIBt
   isinstancet   pathlibt   Patht   strt   _mmapt   _is_random_access_file_backedR
   t
   FILE_MODESt   gett   IO_FITS_MODESt
   ValueErrort   formatR   R   R   t   nameR   t	   file_liket   _open_fileobjt   _open_filenamet   _open_filelikeR%   t   GzipFilet   zipfilet   ZipFilet   bz2t   BZ2Filet   hasattrt   sizet   tellR(   R   t   _mmap_available(   t   selft   fileobjR1   R   R#   R$   t   fmodet   pos(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt   __init__S   sŠ    																							c         C   s   d j  |  j |  j j |  j ƒ S(   Ns
   <{}.{} {}>(   RA   t
   __module__t	   __class__t   __name__R-   (   RP   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt   __repr__À   s    c         C   s   |  S(   N(    (   RP   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt	   __enter__Å   s    c         C   s   |  j  ƒ  d  S(   N(   t   close(   RP   t   typet   valuet	   traceback(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt   __exit__È   s    c         C   s   |  j  r t St |  j ƒ S(   N(   R3   R.   R   R-   (   RP   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt   readableË   s    	c         C   s]   t  |  j d ƒ s t ‚ n  y |  j j | ƒ SWn' t k
 rX |  j d k rR d S‚  n Xd  S(   Nt   readR%   t    (   RL   R-   t   EOFErrorR`   t   IOErrorR2   (   RP   RM   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR`   Ð   s    	i    c   
   	   C   s8  t  |  j d ƒ s t ‚ n  t | t j ƒ s? t j | ƒ } n  | rs | | j d k rs t d j | | ƒ ƒ ‚ n  t | t	 ƒ rŽ | f } n  | p— | s³ t
 j d t ƒ d } n  | rÓ | rÓ | | j f } n  | rL| rLt j | ƒ | j } | | k rt d j | | | ƒ ƒ ‚ qL| | k  rLt d j | | | ƒ ƒ ‚ qLn  |  j j ƒ  } zÅ |  j rÛ|  j d k r¶t |  j d t |  j d	 t j ƒ} | j |  _ d | _ ~ n  t j d
 | d	 | d | d |  j ƒ St t j | ƒ } |  j j | ƒ t |  j | | ƒ }	 | |	 _ |	 SWd |  j j | ƒ Xd S(   s*  
        Similar to file.read(), but returns the contents of the underlying
        file as a numpy array (or mmap'd array if memmap=True) rather than a
        string.

        Usually it's best not to use the `size` argument with this method, but
        it's provided for compatibility.
        R`   i    s   size {} not a multiple of {}s?   No size or shape given to readarray(); assuming a shape of (1,)i   s-   size {} is too few bytes for a {} array of {}s.   size {} is too many bytes for a {} array of {}R1   t   dtypet   shapet   offsett   bufferN(   i   (   RL   R-   Rb   R7   t   npRd   t   itemsizeR@   RA   t   intt   warningst   warnR   t   prodRN   R   R;   R+   t   Memmapt   MEMMAP_MODESR1   t   uint8t   baset   ndarrayR   t   operatort   mulR(   R   Re   (
   RP   RM   Rf   Rd   Re   t
   actualsizet   fileposR   t   countt   data(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt	   readarrayÝ   sN    
							
	c         C   s   |  j  r t St |  j ƒ S(   N(   R   R.   R   R-   (   RP   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt   writable*  s    	c         C   s)   t  |  j d ƒ r% t |  j | ƒ n  d  S(   Nt   write(   RL   R-   R   (   RP   t   string(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR{   /  s    c         C   s)   t  |  j d ƒ r% t | |  j ƒ n  d S(   s×   
        Similar to file.write(), but writes a numpy array instead of a string.

        Also like file.write(), a flush() or close() may be needed before
        the file on disk reflects the data written.
        R{   N(   RL   R-   R   (   RP   t   array(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt
   writearray3  s    c         C   s&   t  |  j d ƒ r" |  j j ƒ  n  d  S(   Nt   flush(   RL   R-   R   (   RP   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR   >  s    c         C   sÒ   t  |  j d ƒ s d  St |  j t j ƒ rr | r_ | d k rP |  j j | } q_ t d ƒ ‚ n  |  j j | ƒ n |  j j | | ƒ |  j j ƒ  } |  j	 rÎ | |  j	 k rÎ t
 j d j |  j	 | ƒ t ƒ n  d  S(   NR(   i   s   Seek from end not supporteds\   File may have been truncated: actual file length ({}) is smaller than the expected size ({})(   RL   R-   R7   R%   RG   Rf   R@   R(   RN   RM   Rk   Rl   RA   R   (   RP   Rf   t   whenceRS   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR(   B  s    c         C   s(   t  |  j d ƒ s t ‚ n  |  j j ƒ  S(   NRN   (   RL   R-   Rb   RN   (   RP   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyRN   X  s    	c         C   s)   t  |  j d ƒ r% |  j j | ƒ n  d  S(   Nt   truncate(   RL   R-   R   (   RP   RM   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR   ]  s    c         C   sK   t  |  j d ƒ r" |  j j ƒ  n  |  j ƒ  d |  _ t |  _ t |  _	 d S(   s1   
        Close the 'physical' FITS file.
        RZ   N(
   RL   R-   RZ   t   _maybe_close_mmapR+   R;   R,   R/   R.   R5   (   RP   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyRZ   a  s    
		c         C   sH   |  j  d k	 rD t j |  j  ƒ d | k rD |  j  j ƒ  d |  _  n  d S(   sú   
        When mmap is in use these objects hold a reference to the mmap of the
        file (so there is only one, shared by all HDUs that reference this
        file).

        This will close the mmap if there are no arrays referencing it.
        i   N(   R;   R+   t   syst   getrefcountRZ   (   RP   t   refcount_delta(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR‚   q  s    	c         C   sÊ   |  j  r' t | d ƒ r' | j d k sW t j j |  j ƒ rÆ t j j |  j ƒ d k rÆ | r« |  j  r… t | d ƒ r… | j d ƒ qÃ | s˜ | j	 ƒ  n  t j
 |  j ƒ qÆ t d j |  j ƒ ƒ ‚ n  d S(   sý   Overwrite an existing file if ``overwrite`` is ``True``, otherwise
        raise an IOError.  The exact behavior of this method depends on the
        _File object state and is only meant for use within the ``_open_*``
        internal methods.
        t   leni    R   s   File {!r} already exists.N(   RC   RL   R†   t   ost   patht   existsRB   t   getsizeR   RZ   t   removeRc   RA   (   RP   R#   RQ   R/   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt   _overwrite_existing  s    '0c         C   s^  t  | ƒ } t | ƒ p t | } | d k rD |  j | | | ƒ n  | s÷ | d k ro d | k pk d | k sÐ | d k r‡ | d k sÐ | d k r¸ d
 | k p´ d | k p´ d | k sÐ | d k rë | d k rë t d j | | ƒ ƒ ‚ n  | |  _ nD t | ƒ rt |  j	 t | ƒ |  _ n t
 j |  j	 t | ƒ |  _ | d k rZ|  j j d ƒ n  d S(   s9   Open a FITS file from a file object or a GzipFile object.R   R   R   R   R   t   +R   s   ab+s   rb+t   wt   aR   s   wb+s>   Mode argument '{}' does not match mode of the input file ({}).i    N(   R   R   R   (   s   ab+s   rb+(   s   rb+s   wb+(   R	   R
   R?   RŒ   R@   RA   R-   R   R   RB   R%   t   openR(   (   RP   RQ   R1   R#   R/   RR   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyRD   “  s(    %c         C   sB  t  |  _ | |  _ t | ƒ r6 t d j | ƒ ƒ ‚ n  t | t j ƒ rl |  j	 | | ƒ |  j j
 d ƒ d St |  j d ƒ s’ t |  j d ƒ r¢ d |  _ } n  | d k rÄ |  j | | t ƒ n  |  j d k rt |  j d	 ƒ rt d
 j |  j ƒ ƒ ‚ n  |  j d k r>t |  j d ƒ r>t d j |  j ƒ ƒ ‚ n  d S(   sg   Open a FITS file from a file-like object, i.e. one that has
        read and/or write methods.
        s;   Cannot read from/write to a closed file-like object ({!r}).i    NR(   RN   R   R   R   R{   sH   File-like object does not have a 'write' method, required for mode '{}'.R`   sG   File-like object does not have a 'read' method, required for mode {!r}.(   R   R   R   (   R,   RC   R-   R	   Rc   RA   R7   RH   RI   t   _open_zipfileR(   RL   R1   RŒ   R.   (   RP   RQ   R1   R#   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyRF   µ  s*    				"	c         C   sÀ  | d k r" |  j  | d t ƒ n  t j j |  j ƒ rd t |  j d ƒ  } | j d ƒ } Wd QXn t	 d ƒ } t j j
 |  j ƒ d } | d k s¤ | j t ƒ rÌ t j |  j t | ƒ |  _ d |  _ n¼ | d	 k sç | j t ƒ rý |  j |  j | ƒ n‹ | d
 k s| j t ƒ rf| d k r3t d ƒ ‚ n  | d k rEd n d } t j |  j | ƒ |  _ n" t |  j t | ƒ |  _ t |  _ t |  j t j ƒ r¬| d k r¬n |  j j d ƒ d S(   s(   Open a FITS file from a filename string.R   R   i   NRa   i   s   .gzR%   s   .zips   .bz2R   R   s:   update and append modes are not supported with bzip2 filesRŽ   R   i    (   R   R   (   RŒ   R+   R,   R‡   Rˆ   R‰   RB   R   R`   R   t   splitextt
   startswitht
   GZIP_MAGICR%   R   R?   R-   R2   t   PKZIP_MAGICR‘   t   BZIP2_MAGICRc   RJ   RK   R5   R7   R(   (   RP   t   filenameR1   R#   t   ft   magict   extt
   bzip2_mode(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyRE   Ü  s,    	!t   lazyc         C   sø   t  j ƒ  \ } } zÄ t j | d ƒ t j | ƒ y t j | d d t j ƒ} Wn9 t j k
 rŒ } t j	 d j
 t | ƒ ƒ t ƒ ~ t SXz: y | j ƒ  Wn% t j k
 rÅ t j	 d t ƒ t SXWd | j ƒ  XWd t j | ƒ t j | ƒ Xt S(   s  Tests that mmap, and specifically mmap.flush works.  This may
        be the case on some uncommon platforms (see
        https://github.com/astropy/astropy/issues/968).

        If mmap.flush is found not to work, ``self.memmap = False`` is
        set and a warning is issued.
        t    i   t   accesss4   Failed to create mmap: {}; mmap use will be disabledsY   mmap.flush is unavailable on this platform; using mmap in writeable mode will be disabledN(   t   tempfilet   mkstempR‡   R{   t   fsynct   mmapt   ACCESS_WRITEt   errorRk   Rl   RA   R:   R   R.   R   RZ   R‹   R,   (   t   clst   tmpfdt   tmpnamet   mmt   exc(    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyRO     s,    
 		c         C   sÐ   | d k r t  d ƒ ‚ n  t | t j ƒ sE t j | ƒ } t } n | } t } | j ƒ  } t | ƒ d k r~ t  d ƒ ‚ n  t j	 d d ƒ |  _
 |  j
 j | j | d ƒ ƒ | rÃ | j ƒ  n  d	 |  _ d
 S(   s£   Limited support for zipfile.ZipFile objects containing a single
        a file.  Allows reading only for now by extracting the file to a
        tempfile.
        R   R   s7   Writing to zipped fits files is not currently supportedi   s2   Zip files with multiple members are not supported.t   suffixs   .fitsi    R&   N(   R   R   (   Rc   R7   RH   RI   R,   R.   t   namelistR†   RŸ   t   NamedTemporaryFileR-   R{   R`   RZ   R2   (   RP   RQ   R1   t   zfileRZ   R«   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR‘   *  s"    	N(    RW   RU   t   __doc__R   R+   R.   R,   RT   RX   RY   R^   R_   R`   Rh   Rp   Ry   Rz   R{   R~   R   R(   RN   R   RZ   R‚   RŒ   RD   RF   RE   R   RO   R‘   (    (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR!   N   s2   k				M								"	'	)%c         C   s   t  |  ƒ p t |  t j ƒ S(   s*  Returns `True` if fileobj is a `file` or `io.FileIO` object or a
    `gzip.GzipFile` object.

    Although reading from a zip file is supported, this does not include
    support for random access, and we do not yet support reading directly
    from an already opened `zipfile.ZipFile` object.
    (   R   R7   R%   RG   (   RQ   (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyR<   H  s    	(6   t
   __future__R    R   RJ   R%   R¢   Rs   R‡   Rƒ   RŸ   Rk   RH   t	   functoolsR   t   numpyRh   R   Rn   t   utilR   R   R   R   R   R	   R
   R   R   R   t
   extern.sixR   R   t
   utils.dataR   R   t   utils.decoratorsR   R   t   utils.exceptionsR   R?   R=   Ro   R”   R•   R–   R8   t   ImportErrorR.   R6   R,   t   objectR!   R<   (    (    (    s3   lib/python2.7/site-packages/astropy/io/fits/file.pyt   <module>   sR   F


ÿ û