B
    }d                 @   sn   d Z ddlZddlmZ ddlmZ ddlZddl	m
Z
 dZdd	d
Zdd ZdddZdd ZdddZdS )z!Write GWpy objects to HDF5 files
    N)wraps)is_hdf5   )	FILE_LIKEz(Duncan Macleod <duncan.macleod@ligo.org>rc             C   s:   t | tjtjfr| S t | tr.t| j|S t| |S )z_Wrapper to open a :class:`h5py.File` from disk, gracefully
    handling a few corner cases
    )
isinstanceh5pyGroupDatasetr   Filename)filenamemode r   Y/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/gwpy/io/hdf5.py	open_hdf5#   s
    
r   c                s   t   fdd}|S )zDecorate an HDF5-reading function to open a filepath if needed

    ``func`` should be written to presume an `h5py.Group` as the first
    positional argument.
    c          	      sP   t | tjs@t | tr| j} t| d} |f||S Q R X  | f||S )Nr   )r   r   HLObjectr   r   r   )fobjargskwargsh5f)funcr   r   decorated_func4   s    
z&with_read_hdf5.<locals>.decorated_func)r   )r   r   r   )r   r   with_read_hdf5.   s    
r   c             C   sN   t | tjr| S |dkr6t| dkr6t|  d }n|dkrFtd| | S )a  Find and return the relevant dataset inside the given H5 object

    If ``path=None`` is given, and ``h5o`` contains a single dataset, that
    will be returned

    Parameters
    ----------
    h5o : `h5py.File`, `h5py.Group`
        the HDF5 object in which to search

    path : `str`, optional
        the path (relative to ``h5o``) of the desired data set

    Returns
    -------
    dset : `h5py.Dataset`
        the recovered dataset object

    Raises
    ------
    ValueError
        if ``path=None`` and the HDF5 object contains multiple datasets
    KeyError
        if ``path`` is given but is not found within the HDF5 object
    Nr   r   z?Please specify the HDF5 path via the ``path=`` keyword argument)r   r   r
   lenlistkeys
ValueError)Zh5opathr   r   r   find_datasetA   s    r   c                s   t   fdd}|S )a  Decorate an HDF5-writing function to open a filepath if needed

    ``func`` should be written to take the object to be written as the
    first argument, and then presume an `h5py.Group` as the second.

    This method uses keywords ``append`` and ``overwrite`` as follows if
    the output file already exists:

    - ``append=False, overwrite=False``: raise `~exceptions.IOError`
    - ``append=True``: open in mode ``a``
    - ``append=False, overwrite=True``: open in mode ``w``
    c          	      s   t |tjst|dd}|dd}tj|rF|sF|sFtd| t||rTdnd} | |f||S Q R X  | |f||S )NappendF	overwritezFile exists: aw)	r   r   r   getosr   existsIOErrorr   )objr   r   r   r    r!   r   )r   r   r   r   u   s    z'with_write_hdf5.<locals>.decorated_func)r   )r   r   r   )r   r   with_write_hdf5h   s    r)   Fc          
   K   sj   || kr|r| |= y| j |f|S  tk
rd } z&t|dkrR| d| df|_ W dd}~X Y nX dS )a+  Create a new dataset inside the parent HDF5 object

    Parameters
    ----------
    parent : `h5py.Group`, `h5py.File`
        the object in which to create a new dataset

    path : `str`
        the path at which to create the new dataset

    overwrite : `bool`
        if `True`, delete any existing dataset at the desired path,
        default: `False`

    **kwargs
        other arguments are passed directly to
        :meth:`h5py.Group.create_dataset`

    Returns
    -------
    dataset : `h5py.Dataset`
        the newly created dataset
    z+Unable to create link (Name already exists)z: 'z2', pass overwrite=True to ignore existing datasetsN)create_datasetRuntimeErrorstrr   )parentr   r!   r   excr   r   r   r*      s    r*   )r   )N)F)__doc__os.pathr%   	functoolsr   Zastropy.io.misc.hdf5r   Zidentify_hdf5r   cacher   
__author__r   r   r   r)   r*   r   r   r   r   <module>   s   

'