U
    e»{j(9  ã                   @   s„   d 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	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:`astropy:table_io` for more details.
é    N)ÚAstropyUserWarning)ÚNOT_OVERWRITING_MSGs   ‰HDF

Z__table_column_meta__Úread_table_hdf5Úwrite_table_hdf5c                 C   s   | d t  S )NÚ.)ÚMETA_KEY©Úpath© r
   úS/var/www/html/bwave/python/venv/lib/python3.8/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©Úh5pyZstructured_arraysr
   r   Úappend_structured_arrays$   s    z=_find_all_structured_arrays.<locals>.append_structured_arrays)r   Z
visititems)Úhandler   r
   r   r   Ú_find_all_structured_arrays   s
    
r   c              	   O   sˆ   |d k	r4|  ¡ }z| d¡}W 5 | |¡ X |tkS |d k	rF| d¡S zdd l}W n tk
rh   Y dS X t|d |j|j	|j
fƒS d S )Né   )z.hdf5z.h5r   F)ÚtellÚseekÚreadÚHDF5_SIGNATUREÚendswithr   ÚImportErrorr   ÚFileÚGroupr   )ÚoriginÚfilepathÚfileobjÚargsÚkwargsÚlocÚ	signaturer   r
   r
   r   Úis_hdf5,   s    
r*   Tc              	   C   s¨  zddl }W n tk
r(   tdƒ‚Y nX | }t| |j|jfƒ�r|dk	r€z| | } W n( ttfk
r~   td|› d�ƒ‚Y nX t| |jƒ�r~t	| ƒ}t
|ƒdkr²td|› �ƒ‚n\t
|ƒdk�r~|dkrÐ|d n|d |d  }t
|ƒdk�rt d	|› d
�t¡ t| |d�S nnt| |jƒ�s~t| dƒ�rTz
| j} W n tk
�rR   tdƒ‚Y nX | | d¡}zt|||d�W ¢S | ¡  X ddlm}m}m}	 |t | ¡ƒ}
t| jk}|dk	�o¾t|ƒ|k}|�sÌ|�rˆ|�rð| dd„ |t|ƒ D ƒ¡}n| dd„ | jt D ƒ¡}dt|  ¡ ƒk�r&|d |
_dd„ |d D ƒ}|
j! "¡ D ]8}dD ],}|||j k�rJt#||||j | ƒ �qJ�qB|	 $|
¡}
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 : bool
        If `True` then Table columns are left as bytes.
        If `False` then Table columns are converted to unicode.
    r   Nú-h5py is required to read and write HDF5 fileszPath z does not existzno table found in HDF5 group ú/é   z`path= was not specified but multiple tables are present, reading in first available table (path=ú)r   r   z h5py can only open regular filesÚr)r	   Úcharacter_as_bytes)ÚTableÚmetaÚ	serializec                 s   s   | ]}|  d ¡V  qdS ©úutf-8N©Údecode©Ú.0Úhr
   r
   r   Ú	<genexpr>¤   s    z"read_table_hdf5.<locals>.<genexpr>c                 s   s   | ]}|  d ¡V  qdS r4   r6   r8   r
   r
   r   r;   ©   s    r2   c                 S   s   i | ]}|d  |“qS )r   r
   )r9   Úxr
   r
   r   Ú
<dictcomp>¯   s      z#read_table_hdf5.<locals>.<dictcomp>Údatatype)ÚdescriptionÚformatÚunitr2   )'r   r    Ú	Exceptionr   r!   r"   ÚKeyErrorÚ
ValueErrorÚOSErrorr   ÚlenÚwarningsÚwarnr   r   r   Úhasattrr   ÚAttributeErrorÚ	TypeErrorÚcloseÚastropy.tabler1   r2   r3   ÚnpÚarrayr   Úattrsr   Zget_header_from_yamlÚlistÚkeysÚcolumnsÚvaluesÚsetattrZ_construct_mixins_from_columnsÚupdateZconvert_bytestring_to_unicode)Úinputr	   r0   r   Z
input_saveZarraysÚfr1   r2   r3   ÚtableZold_version_metaZnew_version_metaÚheaderZheader_colsÚcolÚattrr
   r
   r   r   ?   sr     
ü




ÿ

ÿ
c              	   C   s:   ddl m} ddlm} |dƒ� | | ¡}W 5 Q R X |S )z–Encode a Table ``tbl`` that may have mixin columns to a Table with only
    astropy Columns + appropriate meta-data to allow subsequent decoding.
    r   )r3   )Úserialize_context_asÚhdf5)rM   r3   Zastropy.utils.data_infor]   Zrepresent_mixins_as_columns)Ztblr3   r]   Z
encode_tblr
   r
   r   Ú_encode_mixinsÃ   s
    
r_   Fc                 K   sF  ddl m} zddl}	W n tk
r4   tdƒ‚Y nX |dkrDd}n| d¡rVtdƒ‚d|krp| dd¡\}
}n
d| }
}t||	j	|	j
fƒ�rtt| ¡ ƒƒdkr´|dkr´td	ƒ‚n|dkrÌt d
|› �¡ |
�rz||
 }W n$ ttfk
�r   | |
¡}Y nX n|}nŒt|tƒ�r�tj |¡�rR|�sR|�rD|�sDt |¡ ntt |¡ƒ‚|	 	||�rbdnd¡}zt| ||||||d�W ¢S | ¡  X ntdƒ‚||k�rä|�rÔ|�rÔ||= |�rä|d |k�rä||d = ntd|› d�ƒ‚t| ƒ} tdd„ |  ¡ D ƒƒ�r| j dd�} |  !¡  |dk�rb|  ¡ D ]6}dD ]*}t"|j#|dƒdi fk�r2t dt$¡ �q2�q*|�r”|dk�rvd}|j%|f|  &¡ |dœ|—Ž}n|j%|fd|  &¡ i|—Ž}|�ræ| '| ¡}t( )dd„ |D ƒ¡}|j%t*|ƒ|d� n\| jD ]T}| j| }z||j+|< W n4 tk
�r<   t d|› d t,|ƒ› d!�t$¡ Y nX �qì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.
        If not specified, defaults to ``__astropy_table__``.
    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.
    serialize_meta : bool
        Whether to serialize rich table meta-data when writing the HDF5 file, in
        particular such data required to write and read back mixin columns like
        ``Time``, ``SkyCoord``, or ``Quantity`` to the file.
    **create_dataset_kwargs
        Additional keyword arguments are passed to
        ``h5py.File.create_dataset()`` or ``h5py.Group.create_dataset()``.
    r   )r2   Nr+   Z__astropy_table__r,   z,table path should end with table name, not /r-   zUtable path should always be set via the path= argument when writing to existing fileszBtable path was not set via the path= argument; using default path ÚaÚw)r	   Úcompressionr   Ú	overwriteÚserialize_metaz9output should be a string or an h5py File or Group objectz.__table_column_meta__zTable z already existsc                 s   s   | ]}|j jjd kV  qdS )ÚUN)Úinfor   r   )r9   r[   r
   r
   r   r;   S  s     z#write_table_hdf5.<locals>.<genexpr>F)Z	copy_data)rA   r@   r?   r2   z“table contains column(s) with defined 'unit', 'format', 'description', or 'meta' info attributes. These will be dropped since serialize_meta=False.TÚgzip)Údatarb   rh   c                 S   s   g | ]}|  d ¡‘qS )r5   )Úencoder8   r
   r
   r   Ú
<listcomp>v  s     z$write_table_hdf5.<locals>.<listcomp>)rh   zAttribute `z
` of type zm cannot be written to HDF5 files - skipping. (Consider specifying serialize_meta=True to write all meta data))-rM   r2   r   r    rB   r   rD   Úrsplitr   r!   r"   rF   rQ   rR   rG   rH   rC   Zcreate_groupÚstrÚosr	   ÚexistsÚremoverE   r   r@   rL   r   rK   r_   ÚanyZitercolsÚcopyZconvert_unicode_to_bytestringÚgetattrrf   r   Zcreate_datasetZas_arrayZget_yaml_from_tablerN   rO   r   rP   Útype)rY   Úoutputr	   rb   r   rc   rd   Zcreate_dataset_kwargsr2   r   Úgroupr   Zoutput_grouprX   r[   r\   ZdsetZheader_yamlZheader_encodedÚ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)r1   r^   N)
Z
astropy.iorx   rM   r1   Zregister_readerr   Zregister_writerr   Zregister_identifierr*   )Zio_registryr1   r
   r
   r   Úregister_hdf5‹  s
    ry   )NT)NFFFF)Ú__doc__rm   rG   ÚnumpyrN   Zastropy.utils.exceptionsr   Zastropy.utils.miscr   r   r   Ú__all__r   r   r*   r   r_   r   ry   r
   r
   r
   r   Ú<module>   s,   
      ù
 :