σ
ίΘ[c           @` sΗ   d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l Z	 d d l
 m Z d d l m Z d Z d Z d	 d
 g Z d   Z d   Z d d  Z d e e e e d  Z d   Z d S(   uΤ   
This package contains functions for reading and writing HDF5 tables that are
not meant to be used directly, but instead are available as readers/writers in
`astropy.table`. See :ref:`table_io` for more details.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   AstropyUserWarning(   t   sixs   HDF

u   __table_column_meta__u   read_table_hdf5u   write_table_hdf5c         ` s5   d d l    g      f d   } |  j |   S(   u4   
    Find all structured arrays in an HDF5 file
    i    Nc         ` s8   t  |   j  r4 | j j d k r4  j |   n  d  S(   Nu   V(   t
   isinstancet   Datasett   dtypet   kindt   append(   t   namet   obj(   t   h5pyt   structured_arrays(    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pyt   append_structured_arrays$   s    $(   R   t
   visititems(   t   handleR   (    (   R   R   s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pyt   _find_all_structured_arrays   s
    c         O` s«   | d  k	 rF | j   } z | j d  } Wd  | j |  X| t k S| d  k	 r_ | j d  Sy d d  l } Wn t k
 r t SXt	 | d | j
 | j | j f  Sd  S(   Ni   u   .hdf5u   .h5i    (   u   .hdf5u   .h5(   t   Nonet   tellt   readt   seekt   HDF5_SIGNATUREt   endswithR   t   ImportErrort   FalseR   t   Filet   GroupR   (   t   origint   filepatht   fileobjt   argst   kwargst   loct	   signatureR   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pyt   is_hdf5+   s    
c         C` sά  y d d l  } Wn t k
 r/ t d   n Xt |  | j | j f  rB| r y |  | }  Wq t t f k
 r t d j	 |    q Xn  t |  | j  rΚt
 |   } t |  d k rΩ t d j	 |    q?t |  d k r?| d k r| d n | d | d } t j d j	 |  t  t |  d | SqΚn t |  | j  sΚt |  d	  ry |  j }  Wqt k
 rt d
   qXn  | j |  d  } z t | d | SWd | j   Xn  d d l m } m } | t j |    } t |  j k rΕ| j d   |  j t D  } d t | j     k rL| d | _ n  t! d   | d D  }	 xo | j" j#   D]H }
 x? d D]7 } | |	 |
 j k rt$ |
 | |	 |
 j |  qqWqvWn | j j% |  j  | S(   u  
    Read a Table object from an HDF5 file

    This requires `h5py <http://www.h5py.org/>`_ to be installed. If more than one
    table is present in the HDF5 file or group, the first table is read in and
    a warning is displayed.

    Parameters
    ----------
    input : str or :class:`h5py:File` or :class:`h5py:Group` or
        :class:`h5py:Dataset` If a string, the filename to read the table from.
        If an h5py object, either the file or the group object to read the
        table from.
    path : str
        The path from which to read the table inside the HDF5 file.
        This should be relative to the input file or group.
    i    Nu-   h5py is required to read and write HDF5 filesu   Path {0} does not existu    no table found in HDF5 group {0}u   /ud   path= was not specified but multiple tables are present, reading in first available table (path={0})t   pathu   readu    h5py can only open regular filesu   ri   (   t   Tablet   metac         s` s   |  ] } | j  d   Vq d S(   u   utf-8N(   t   decode(   t   .0t   h(    (    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pys	   <genexpr>   s    u   metac         s` s   |  ] } | d  | f Vq d S(   u   nameN(    (   R)   t   x(    (    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pys	   <genexpr>   s    u   datatypeu   descriptionu   formatu   unit(   u   descriptionu   formatu   unitu   meta(&   R   R   t	   ExceptionR   R   R   t   KeyErrort
   ValueErrort   IOErrort   formatR   t   lenR   t   warningst   warnR   t   read_table_hdf5R   t   hasattrR   t   AttributeErrort	   TypeErrort   closet   tableR&   R'   t   npt   arrayt   META_KEYt   attrst   get_header_from_yamlt   listt   keyst   dictt   columnst   valuest   setattrt   update(   t   inputR%   R   t   arrayst   fR&   R'   R9   t   headert   header_colst   colt   attr(    (    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pyR4   ?   sZ    	()c         C` s§  d d l  m } y d d l } Wn t k
 r? t d   n X|  j r g  |  j j   D]$ \ }	 }
 t |
 |  j	  sY |	 ^ qY } t
 d j |    n  | d k rΆ t
 d   n | j d  rΤ t
 d	   n  d | k rϋ | j d d
  \ } }	 n d | } }	 t | | j | j f  rl| rcy | | } Wqit t
 f k
 r_| j |  } qiXq6| } nΚ t | t j  r*t j j |  rΜ| rΜ| r΄| r΄t j |  qΜt d j |    n  | j | | rαd n d  } z/ t |  | d | d | d | d | d | SWd | j   Xn t d   |	 | k rp| rX| rX| |	 =qpt d j |    n  | r―| t k rd } n  | j |	 d |  j   d | } n | j |	 d |  j   } | r;| j  |   } y- g  | D] } | j! d  ^ qι| j" t# <Wq£t k
 r7} t$ j% d j |   q£Xnh xe |  j D]Z } |  j | } y | | j" | <WqEt k
 rt$ j% d j | t& |   t'  qEXqEWd S(   uτ  
    Write a Table object to an HDF5 file

    This requires `h5py <http://www.h5py.org/>`_ to be installed.

    Parameters
    ----------
    table : `~astropy.table.Table`
        Data table that is to be written to file.
    output : str or :class:`h5py:File` or :class:`h5py:Group`
        If a string, the filename to write the table to. If an h5py object,
        either the file or the group object to write the table to.
    path : str
        The path to which to write the table inside the HDF5 file.
        This should be relative to the input file or group.
    compression : bool or str or int
        Whether to compress the table inside the HDF5 file. If set to `True`,
        ``'gzip'`` compression is used. If a string is specified, it should be
        one of ``'gzip'``, ``'szip'``, or ``'lzf'``. If an integer is
        specified (in the range 0-9), ``'gzip'`` compression is used, and the
        integer denotes the compression level.
    append : bool
        Whether to append the table to an existing HDF5 file.
    overwrite : bool
        Whether to overwrite any existing file without warning.
        If ``append=True`` and ``overwrite=True`` then only the dataset will be
        replaced; the file/group will not be overwritten.
    i   (   R'   i    Nu-   h5py is required to read and write HDF5 filesu3   cannot write table with mixin column(s) {0} to HDF5u/   table path should be set via the path= argumentu   /u,   table path should end with table name, not /i   u   File exists: {0}u   au   wR%   t   compressionR
   t	   overwritet   serialize_metau9   output should be a string or an h5py File or Group objectu   Table {0} already existsu   gzipt   datau   utf8u<   Attributes could not be written to the output HDF5 file: {0}u   Attribute `{0}` of type {1} cannot be written to HDF5 files - skipping. (Consider specifying serialize_meta=True to write all meta data)((   R9   R'   R   R   R,   t   has_mixin_columnsRB   t   itemsR   t   ColumnClassR.   R0   R   R   t   rsplitR   R   R-   t   create_groupR   t   string_typest   osR%   t   existst   removeR/   t   write_table_hdf5R8   R7   t   Truet   create_datasett   as_arrayt   get_yaml_from_tablet   encodeR=   R<   R2   R3   t   typeR   (   R9   t   outputR%   RM   R
   RN   RO   R'   R   R   RK   t   mixin_namest   groupt   output_groupRH   t   dsett   header_yamlR*   t   et   keyt   val(    (    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pyRZ   ¦   s|    			
	-c          C` s]   d d l  m }  d d l m } |  j d | t  |  j d | t  |  j d | t	  d S(   u)   
    Register HDF5 with Unified I/O.
    i   (   t   registryi   (   R&   u   hdf5N(
   t    Rj   R9   R&   t   register_readerR4   t   register_writerRZ   t   register_identifierR$   (   t   io_registryR&   (    (    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pyt   register_hdf5&  s
    (   t   __doc__t
   __future__R    R   R   R   RW   R2   t   numpyR:   t   utils.exceptionsR   t   externR   R   R<   t   __all__R   R$   R   R4   R   RZ   Rp   (    (    (    s3   lib/python2.7/site-packages/astropy/io/misc/hdf5.pyt   <module>   s   "		g