
    HR-e)9                         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	 Zd
 Zd ZddZd Z	 	 	 	 	 ddZ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

__table_column_meta__read_table_hdf5write_table_hdf5c                     | dz   t           z   S )N.)META_KEYpaths    4lib/python3.11/site-packages/astropy/io/misc/hdf5.py	meta_pathr      s    #:      c                 L    ddl g fd}|                     |           S )z5
    Find all structured arrays in an HDF5 file.
    r   Nc                     t          |j                  r'|j        j        dk    r                    |            d S d S d S )NV)
isinstanceDatasetdtypekindappend)nameobjh5pystructured_arrayss     r   append_structured_arraysz=_find_all_structured_arrays.<locals>.append_structured_arrays$   sO    c4<(( 	+SY^s-B-B$$T*****	+ 	+-B-Br   )r   
visititems)handler   r   r   s     @@r   _find_all_structured_arraysr      sP     KKK+ + + + + + .///r   c                 z   |e|                                 }	 |                    d          }|                    |           n# |                    |           w xY w|t          k    S ||                    d          S 	 dd l}t          |d         |j        |j        |j	        f          S # t          $ r Y dS w xY w)N   )z.hdf5z.h5r   F)tellreadseekHDF5_SIGNATUREendswithr   r   FileGroupr   ImportError)originfilepathfileobjargskwargsloc	signaturer   s           r   is_hdf5r1   ,   s    llnn	QILLGLLN**		  !1222J $q'DItz4<#HIII    uus   A A B, ,
B:9B:Tc                    	 ddl }n# t          $ r t          d          w xY w| }t          | |j        |j        f          r|2	 | |         } n(# t          t          f$ r t          d| d          w xY wt          | |j                  rt          |           }t          |          dk    rt          d|           t          |          dk    rZ||d         n|dz   |d         z   }t          |          dk    rt          j        d	| d
t                     t          | |          S nt          | |j                  st!          | d          r&	 | j        } n# t$          $ r t'          d          w xY w|                    | d          }	 t          |||          |                                 S # |                                 w xY wddlm}m}m}	  |t3          j        |                     }
t6          | j        v }|duot;          |          |v }|s|r|r3|                    d |t;          |                   D                       }n/|                    d | j        t6                   D                       }dt?          |                                           v r|d         |
_        d |d         D             }|
j!        "                                D ]8}dD ]3}|||j                 v r"tG          ||||j                 |                    49|	$                    |
          }
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              3   @   K   | ]}|                     d           V  dS utf-8Ndecode.0hs     r   	<genexpr>z"read_table_hdf5.<locals>.<genexpr>   ?       / /&'!!/ / / / / /r   c              3   @   K   | ]}|                     d           V  dS r=   r?   rA   s     r   rD   z"read_table_hdf5.<locals>.<genexpr>   rE   r   r:   c                      i | ]}|d          |S )r    )rB   xs     r   
<dictcomp>z#read_table_hdf5.<locals>.<dictcomp>   s    @@@qy!@@@r   datatype)descriptionformatunitr:   )'r   r)   	Exceptionr   r'   r(   KeyError
ValueErrorOSErrorr   lenwarningswarnr   r   r   hasattrr   AttributeError	TypeErrorcloseastropy.tabler9   r:   r;   nparrayr
   attrsr   get_header_from_yamllistkeyscolumnsvaluessetattr_construct_mixins_from_columnsupdateconvert_bytestring_to_unicode)inputr   r8   r   
input_savearraysfr9   r:   r;   tableold_version_metanew_version_metaheaderheader_colscolattrs                    r   r   r   ?   s   *I I I IGHHHI J%$)TZ011 1 =dj) = = =;d;;;<<<= eTZ(( 	9077F6{{a !G!G!GHHHVq$(LvayydSj6!96Lv;;??M0(,0 0 0 +	   'u48888t|,,  5&!! 	DD
! D D D BCCCD IIeS!!	"14DVWWWGGIIIIAGGIIII 5444444444E"(5//""E  5;.4'IIdOOz,I '+ ' 	.. / /+5ioo+F/ / /  FF
 .. / /+0;x+@/ / /  F T&++--((((EJ@@VJ-?@@@='')) 	D 	DCA D D;sx000C{38'<T'BCCCD 88?? 	
%+&&& .++---Ls,    !A %A3E E5F5 5Gc                     ddl m} ddlm}  |d          5  |                    |           }ddd           n# 1 swxY w Y   |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;   )serialize_context_ashdf5N)rZ   r;   astropy.utils.data_infors   represent_mixins_as_columns)tblr;   rs   
encode_tbls       r   _encode_mixinsry      s     ('''''<<<<<< 
	f	%	% @ @::3??
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ s   :>>Fc           
         ddl m} 	 ddl}	n# t          $ r t	          d          w xY w|d}n$|                    d          rt          d          d|v r|                    dd          \  }
}nd|}}
t          ||	j	        |	j
        f          rt          t          |                                                    dk    r|dk    rt          d	          |dk    rt          j        d
|            |
r7	 ||
         }n# t           t          f$ r |                    |
          }Y nw xY w|}nt          |t$                    rt&          j                            |          r<|s:|r|st'          j        |           n!t/          t1          j        |                    |		                    ||rdnd          }	 t5          | ||||||          |                                 S # |                                 w xY wt9          d          ||v r*|r|r||= |r|dz   |v r||dz   = nt/          d| d          t;          |           } t=          d |                                 D                       r*|                      d          } | !                                 |du rP|                                 D ];}dD ]6}tE          |j#        |d          di fvrt          j        dtH                     7<|r*|du rd} |j%        |f| &                                |d|}n" |j%        |fd| &                                i|}|rY|'                    |           }tQ          j)        d |D                       }|%                    tU          |          |           dS | j        D ]X}| j        |         }	 ||j+        |<   # t8          $ r1 t          j        d| dtY          |           dtH                     Y Uw xY w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   )r:   Nr3   __astropy_table__r4   z,table path should end with table name, not /r5   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              3   @   K   | ]}|j         j        j        d k    V  dS )UN)infor   r   )rB   rp   s     r   rD   z#write_table_hdf5.<locals>.<genexpr>Q  s.      
B
B#38>#%
B
B
B
B
B
Br   F)	copy_data)rN   rM   rL   r:   ztable contains column(s) with defined 'unit', 'format', 'description', or 'meta' info attributes. These will be dropped since serialize_meta=False.Tgzip)datar~   r   c                 8    g | ]}|                     d           S )r>   )encoderA   s     r   
<listcomp>z$write_table_hdf5.<locals>.<listcomp>t  s$    "J"J"J188G#4#4"J"J"Jr   )r   zAttribute `z
` of type zm cannot be written to HDF5 files - skipping. (Consider specifying serialize_meta=True to write all meta data))-rZ   r:   r   r)   rO   r&   rQ   rsplitr   r'   r(   rS   r_   r`   rT   rU   rP   create_groupstrosr   existsremoverR   r   rM   r   rY   rX   ry   anyitercolscopyconvert_unicode_to_bytestringgetattrr   r   create_datasetas_arrayget_yaml_from_tabler[   r\   r   r]   type)rk   outputr   r~   r   r   r   create_dataset_kwargsr:   r   groupr   output_grouprj   rp   rq   dsetheader_yamlheader_encodedkeyvals                        r   r   r      sa   \ #"""""I I I IGHHHI |"	s		 IGHHH
d{{kk#q))ttDt&49dj122 .UtFKKMM""##a''D4G,G,G  
 (((M-&*- -  
  	":%e}j) : : :%22599: "LL	FC	 	  U7>>&!! 	B& 	B B B	&!!!!18@@AAA IIfV4cc55	#'#-   GGIIIIAGGIIII STTT | 	:i 	:T" B$)A"A\"Q"Q (@!@A84888999 5!!E
 
B
B1A1A
B
B
BBB .

U
++++---
 >>## 	 	CA  38T400r
BBMB +	    
$ K*|*
!!#
 
 $	
 
 +|*
 
~~''
+@
 
  ..u55"J"Jk"J"J"JKK##IdOO.#IIIII : 
	 
	C*S/C"%
3   B# B Bc B B B '	    	
	 
	s8    '8D &D+*D+<G& &G<4
N??8O:9O:c                      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)r9   rt   N)

astropy.ior   rZ   r9   register_readerr   register_writerr   register_identifierr1   )io_registryr9   s     r   register_hdf5r     sw     322222######???/?@@@##FE7;;;;;r   )NT)NFFFF)__doc__r   rT   numpyr[   astropy.utils.exceptionsr   astropy.utils.miscr   r%   r
   __all__r   r   r1   r   ry   r   r   rH   r   r   <module>r      s    
			      8 7 7 7 7 7 2 2 2 2 2 2%"0
1! ! !   J J J&@ @ @ @F  $ 
u u u up	< 	< 	< 	< 	<r   