B
    q\q9                 @   s|   d Z ddlZddlZddlZddlmZmZ dZdZ	ddgZ
dd	 Zd
d Zdd ZdddZdd ZdddZdd ZdS )z
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.
    N)AstropyUserWarningAstropyDeprecationWarnings   HDF

Z__table_column_meta__read_table_hdf5write_table_hdf5c             C   s   | d t  S )N.)META_KEY)path r	   3lib/python3.7/site-packages/astropy/io/misc/hdf5.py	meta_path   s    r   c                s(   ddl  g  fdd}| | S )z4
    Find all structured arrays in an HDF5 file
    r   Nc                s&   t | jr"|jjdkr"|  d S )NV)
isinstanceDatasetdtypekindappend)nameobj)h5pystructured_arraysr	   r
   append_structured_arrays"   s    z=_find_all_structured_arrays.<locals>.append_structured_arrays)r   Z
visititems)Zhandler   r	   )r   r   r
   _find_all_structured_arrays   s
    
r   c          	   O   s   |d k	r4|  }z|d}W d || X |tkS |d k	rF|dS ydd l}W n tk
rf   dS X t|d |j|j	|j
fS d S )N   )z.hdf5z.h5r   F)tellreadseekHDF5_SIGNATUREendswithr   ImportErrorr   FileGroupr   )originfilepathZfileobjargskwargsZlocZ	signaturer   r	   r	   r
   is_hdf5)   s    
r%   Tc          	   C   s  yddl }W n tk
r(   tdY nX | }t| |j|jfr|dk	r|y| | } W n& ttfk
rz   td	|Y nX t| |jrt
| }t|dkrtd	|nJt|dkr|dkr|d n|d |d  }td	|t t| |dS njt| |jsbt| d	r<y
| j} W n tk
r:   td
Y nX || d}zt|||dS |  X ddlm}m}m}	 |t| }
t| jk}|dk	ot||k}|s|r~|r|dd |t| D }n |r|dd | jt D }dt |! kr|d |
_t"dd |d D }xJ|
j#$ D ]<}x4dD ],}|||j kr<t%||||j |  q<W q2W |	&|
}
n|
j'| j |s|
(  |
S )a#  
    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.
    character_as_bytes: boolean
        If `True` then Table columns are left as bytes.
        If `False` then Table columns are converted to unicode.
    r   Nz-h5py is required to read and write HDF5 fileszPath {0} does not existz no table found in HDF5 group {0}/zdpath= was not specified but multiple tables are present, reading in first available table (path={0}))r   r   z h5py can only open regular filesr)r   character_as_bytes)Tablemeta	serializec             s   s   | ]}| d V  qdS )zutf-8N)decode).0hr	   r	   r
   	<genexpr>   s    z"read_table_hdf5.<locals>.<genexpr>c             s   s   | ]}| d V  qdS )zutf-8N)r,   )r-   r.   r	   r	   r
   r/      s    r*   c             s   s   | ]}|d  |fV  qdS )r   Nr	   )r-   xr	   r	   r
   r/      s    Zdatatype)descriptionformatunitr*   ))r   r   	Exceptionr   r   r    KeyError
ValueErrorOSErrorr2   r   lenwarningswarnr   r   r   hasattrr   AttributeError	TypeErrorcloseastropy.tabler)   r*   r+   npZarrayr   attrsr   Zget_header_from_yamllistkeysdictcolumnsvaluessetattrZ_construct_mixins_from_columnsupdateZconvert_bytestring_to_unicode)inputr   r(   r   Z
input_saveZarraysfr)   r*   r+   tableZold_version_metaZnew_version_metaheaderZheader_colscolattrr	   r	   r
   r   =   sn    
 




"c       	   	   C   s   ddl m} ddlm} ddlm} ddlm}m} yddl	}W nT t
k
r   x>|  D ]2}|||rZ|j|jk	rZtd|jj|jjqZW Y nX |d || }W dQ R X |S )	zEncode a Table ``tbl`` that may have mixin columns to a Table with only
    astropy Columns + appropriate meta-data to allow subsequent decoding.
    r   )r+   )has_info_class)units)	MixinInfoserialize_context_asNzBcannot write type {} column '{}' to HDF5 without PyYAML installed.hdf5)r?   r+   Zastropy.table.tablerO   ZastropyrP   Zastropy.utils.data_inforQ   rR   yamlr   itercols	__class__ZQuantityr=   r2   __name__infor   Z_represent_mixins_as_columns)	Ztblr+   rO   urQ   rR   rT   rM   Z
encode_tblr	   r	   r
   _encode_mixins   s    

rZ   Fc             C   s,  ddl m} yddl}	W n tk
r4   tdY nX |dkrHtdn|drZtdd|krt|dd\}
}n
d| }
}t||	j	|	j
fr|
ry||
 }W q ttfk
r   ||
}Y qX n|}nt|trNtj|r|s|r|st| ntd	||		||r"d
nd}zt| |||||||dS |  X ntd||kr|rt|rt||= ntd|t| } tdd |  D r| jdd} |   |dkrxF|  D ]:}x2dD ]*}t|j|ddi fkrtdt  qW qW |r2|dkrd}|j!|| " |d}n|j!|| " d}|r|#| }dd |D }|rtdt$ y||j%t&< W n4 tk
r } ztd| W dd}~X Y nX n|j!t'||d nZxX| jD ]N}| j| }y||j%|< W n. tk
r    td|t(|t  Y nX qW dS )a  
    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.
    r   )r*   Nz-h5py is required to read and write HDF5 filesz/table path should be set via the path= argumentr&   z,table path should end with table name, not /   zFile exists: {0}aw)r   compressionr   	overwriteserialize_metacompatibility_modez9output should be a string or an h5py File or Group objectzTable {0} already existsc             s   s   | ]}|j jjd kV  qdS )UN)rX   r   r   )r-   rM   r	   r	   r
   r/   A  s    z#write_table_hdf5.<locals>.<genexpr>F)Z	copy_data)r3   r2   r1   r*   ztable contains column(s) with defined 'unit', 'format', 'description', or 'meta' info attributes. These will be dropped since serialize_meta=False.TZgzip)datar^   )rc   c             S   s   g | ]}| d qS )zutf-8)encode)r-   r.   r	   r	   r
   
<listcomp>]  s    z$write_table_hdf5.<locals>.<listcomp>z,compatibility mode for writing is deprecatedz<Attributes could not be written to the output HDF5 file: {0}zAttribute `{0}` of type {1} cannot be written to HDF5 files - skipping. (Consider specifying serialize_meta=True to write all meta data)))r?   r*   r   r   r4   r6   r   rsplitr   r   r    r5   Zcreate_groupstrosr   existsremover7   r2   r   r>   r=   rZ   anyrU   copyZconvert_unicode_to_bytestringgetattrrX   r9   r:   r   Zcreate_datasetZas_arrayZget_yaml_from_tabler   rA   r   r   type)rK   outputr   r^   r   r_   r`   ra   r*   r   groupr   Zoutput_grouprJ   rM   rN   ZdsetZheader_yamlZheader_encodedekeyvalr	   r	   r
   r      s     












c              C   sF   ddl m}  ddlm} | d|t | d|t | d|t	 dS )z)
    Register HDF5 with Unified I/O.
    r   )registry)r)   rS   N)
Z
astropy.iort   r?   r)   Zregister_readerr   Zregister_writerr   Zregister_identifierr%   )Zio_registryr)   r	   r	   r
   register_hdf5|  s
    ru   )NT)NFFFFF)__doc__rh   r9   Znumpyr@   Zastropy.utils.exceptionsr   r   r   r   __all__r   r   r%   r   rZ   r   ru   r	   r	   r	   r
   <module>   s$   
    
 