a
    İa1                     @   sV  d dl Z d dlZd dlZd dlmZ d dlZddlmZ ddl	m
Z
mZmZmZ ddlmZ ddlmZmZmZmZmZ dd	lmZmZ dd
lmZmZmZmZ ddlmZm Z m!Z!m"Z"m#Z# ddl$m%Z% zd dl&Z&dZ'W n e(y   dZ'Y n0 G dd deZ)dd Z*dd Z+e j,e!ddddZ-dd Z.G dd deZ/G dd deZ0e0ed< dS )    N)LooseVersion   )indexing)
FrozenDictis_remote_uriread_magic_number_from_file'try_read_magic_number_from_file_or_path)Variable   )BACKEND_ENTRYPOINTSBackendEntrypointWritableCFDataStore_normalize_pathfind_root_and_group)CachingFileManagerDummyFileManager)	HDF5_LOCKcombine_locksensure_lockget_write_lock)BaseNetCDF4Array_encode_nc4_variable_extract_nc4_variable_encoding_get_datatype_nc4_require_group)StoreBackendEntrypointTFc                   @   s&   e Zd Zd	ddZdd Zdd ZdS )
H5NetCDFArrayWrapperTc                 C   s   | j |}|j| j S N)	datastore_acquire	variablesZvariable_name)self
needs_lockds r$   8lib/python3.9/site-packages/xarray/backends/h5netcdf_.py	get_array+   s    zH5NetCDFArrayWrapper.get_arrayc                 C   s   t || jt jj| jS r   )r   Zexplicit_indexing_adaptershapeZIndexingSupportZOUTER_1VECTOR_getitem)r!   keyr$   r$   r%   __getitem__/   s    z H5NetCDFArrayWrapper.__getitem__c                 C   sR   t dd |D }| jj$ | jdd}|| W  d    S 1 sD0    Y  d S )Nc                 s   s&   | ]}t |tjrt|n|V  qd S r   )
isinstancenpZndarraylist).0kr$   r$   r%   	<genexpr>7       z0H5NetCDFArrayWrapper._getitem.<locals>.<genexpr>F)r"   )tupler   lockr&   )r!   r)   Zarrayr$   r$   r%   r(   4   s    
zH5NetCDFArrayWrapper._getitemN)T)__name__
__module____qualname__r&   r*   r(   r$   r$   r$   r%   r   *   s   
r   c                 C   s   t | tr| dS | S d S )Nzutf-8)r+   bytesdecode)Ztxtr$   r$   r%   maybe_decode_bytes=   s    

r9   c                 C   s4   i }| j  D ] \}}|dvr&t|}|||< q|S )N)
_FillValueZmissing_value)attrsitemsr9   )Zh5netcdf_varr;   r/   vr$   r$   r%   _read_attributesD   s    
r>   h5netcdf)Zlsd_okayZ	h5py_okayZbackendc                 C   s
   |  |S r   Zcreate_group)Zdatasetnamer$   r$   r%   _h5netcdf_create_groupU   s    rB   c                	   @   s   e Zd ZdZdZddedfddZed%d	d
Zd&ddZ	e
dd Zdd Zdd Zdd Zdd Zdd Zd'ddZdd Zdd Zd(dd Zd!d" Zd#d$ ZdS ))H5NetCDFStorez/Store for reading and writing data via h5netcdf)	autocloseformat	is_remoter3   	_filename_group_manager_modeNFc                 C   s   t |tjtjfrJ|d u r(t|\}}nt|tjur>td|}t|}|| _|| _	|| _
d | _t| jd j| _t| j| _t|| _|| _d S )Nz=must supply a h5netcdf.File if the group argument is providedr   )r+   r?   FileZGroupr   type
ValueErrorr   rI   rH   rJ   rE   r#   filenamerG   r   rF   r   r3   rD   )r!   managergroupmoder3   rD   rootr$   r$   r%   __init__g   s"    
zH5NetCDFStore.__init__rTc
                 C   s   t |trtdn,t |tjr@t|}
|
ds@t|
 d|dvrPtdd|i}|d urttj	tdkr|||d< ntd	ttj	td
krttj
jj	tdkr|	|d< |d u r|dkrt}nttt|g}ttj|||d}| |||||dS )NzGcan't open netCDF4/HDF5 as bytes try passing a path or file-like object   HDF

z- is not the signature of a valid netCDF4 file)NZNETCDF4z#invalid format for h5netcdf backendinvalid_netcdfz0.8.0
phony_dimszGh5netcdf backend keyword argument 'phony_dims' needs h5netcdf >= 0.8.0.z0.10.0z3.0.0decode_vlen_stringsrT   )rQ   kwargs)rP   rQ   r3   rD   )r+   r7   rM   ioIOBaser   
startswithr   r?   __version__coreh5pyr   r   r   r   rK   )clsrN   rQ   rE   rP   r3   rD   rV   rW   rX   magic_numberrY   rO   r$   r$   r%   open   s>    



zH5NetCDFStore.openc                 C   sD   | j |$}t|| j| jtd}W d    n1 s60    Y  |S )Nr@   )rI   Zacquire_contextr   rH   rJ   rB   )r!   r"   rR   r#   r$   r$   r%   r      s
    $zH5NetCDFStore._acquirec                 C   s   |   S r   )r   r!   r$   r$   r%   r#      s    zH5NetCDFStore.dsc           	      C   s   dd l }|j}tt|| }t|}|j|j|jd}|j	dkrVd|d< |j
|d< n|j	d urt|j	|d< |j
|d< | j|d	< |j|d
< |j|jd}|tu rt|d< n|d urn
|j|d< t||||S )Nr   )
chunksizes
fletcher32shufflegzipTzlib	complevelcompressioncompression_optssourceZoriginal_shapeZvlendtype)r_   
dimensionsr   ZLazilyIndexedArrayr   r>   chunksre   rf   rj   rk   rG   r'   Zcheck_dtypern   strr	   )	r!   rA   varr_   ro   datar;   encodingZ
vlen_dtyper$   r$   r%   open_store_variable   s.    







z!H5NetCDFStore.open_store_variablec                    s   t  fdd jj D S )Nc                 3   s"   | ]\}}|  ||fV  qd S r   )ru   r.   r/   r=   rc   r$   r%   r0      s   z.H5NetCDFStore.get_variables.<locals>.<genexpr>)r   r#   r    r<   rc   r$   rc   r%   get_variables   s    
zH5NetCDFStore.get_variablesc                 C   s   t t| jS r   )r   r>   r#   rc   r$   r$   r%   	get_attrs   s    zH5NetCDFStore.get_attrsc                 C   s   | j jS r   )r#   ro   rc   r$   r$   r%   get_dimensions   s    zH5NetCDFStore.get_dimensionsc                 C   s   ddd | j j D iS )Nunlimited_dimsc                 S   s   h | ]\}}|d u r|qS r   r$   rv   r$   r$   r%   	<setcomp>   r1   z-H5NetCDFStore.get_encoding.<locals>.<setcomp>)r#   ro   r<   rc   r$   r$   r%   get_encoding   s    zH5NetCDFStore.get_encodingc                 C   s0   |r d | j j|< | j || n|| j j|< d S r   )r#   ro   Zresize_dimension)r!   rA   lengthZis_unlimitedr$   r$   r%   set_dimension   s    zH5NetCDFStore.set_dimensionc                 C   s   || j j|< d S r   )r#   r;   )r!   r)   valuer$   r$   r%   set_attribute   s    zH5NetCDFStore.set_attributec                 C   s   t |S r   )r   )r!   variabler$   r$   r%   encode_variable   s    zH5NetCDFStore.encode_variablec                 C   s  dd l }|j }t||d}|dd }|tu rJ|d urJtd|d|tu r^|jtd}t||d}	i }
|	dd	r|r|		d
dvrt
d|	d
d |rd|	v rd|	v r|	d |	d krt
d|	dd}|dkr|	d| |	dd |	d< |jr&dD ]}||	v r|	| |
|< q|| jvrR| jj|f||j|d|
}n
| j| }| D ]\}}||j|< qdt|| }||jfS )Nr   )Zraise_on_invalid_encodingr:   zh5netcdf does not yet support setting a fill value for variable-length strings (https://github.com/shoyer/h5netcdf/issues/37). Either remove '_FillValue' from encoding on variable zH or set {'dtype': 'S1'} in encoding to use the fixed width NC_CHAR type.rm   )Zraise_on_invalidrh   Frj   )Nrg   z+'zlib' and 'compression' encodings mismatchrg   ri   rk   z5'complevel' and 'compression_opts' encodings mismatchrd   rp   )rj   rk   rf   rp   re   )rn   ro   	fillvalue)r_   r;   copyr   poprq   NotImplementedErrorZspecial_dtype_extract_h5nc_encodinggetrM   
setdefaultr'   r#   Zcreate_variableZdimsr<   r   rs   )r!   rA   r   Zcheck_encodingrz   r_   r;   rn   r   rt   rY   ri   r)   Znc4_varr/   r=   targetr$   r$   r%   prepare_variable  sd    
	


zH5NetCDFStore.prepare_variablec                 C   s   | j   d S r   )r#   syncrc   r$   r$   r%   r   L  s    zH5NetCDFStore.syncc                 K   s   | j jf i | d S r   )rI   close)r!   rY   r$   r$   r%   r   O  s    zH5NetCDFStore.close)rT   NNNFNNT)T)F)FN)r4   r5   r6   __doc__	__slots__r   rS   classmethodrb   r   propertyr#   ru   rw   rx   ry   r|   r~   r   r   r   r   r   r$   r$   r$   r%   rC   Y   s8           3

%
 
KrC   c                   @   s>   e Zd ZeZdd ZddddddddddddddddZdS )H5netcdfBackendEntrypointc                 C   sL   t |}|d ur|dS ztj|\}}W n tyB   Y dS 0 |dv S )NrU   F>   z.nc4z.cdfz.nc)r   r\   ospathsplitext	TypeError)r!   filename_or_objra   _extr$   r$   r%   guess_can_openV  s    
z(H5netcdfBackendEntrypoint.guess_can_openTN)mask_and_scaledecode_timesconcat_charactersdecode_coordsdrop_variables
use_cftimedecode_timedeltarE   rP   r3   rV   rW   rX   c             
   C   sD   t |}tj||	|
||||d}t }|j||||||||d}|S )N)rE   rP   r3   rV   rW   rX   )r   r   r   r   r   r   r   )r   rC   rb   r   open_dataset)r!   r   r   r   r   r   r   r   r   rE   rP   r3   rV   rW   rX   storeZstore_entrypointr#   r$   r$   r%   r   b  s,    

z&H5netcdfBackendEntrypoint.open_dataset)r4   r5   r6   has_h5netcdfZ	availabler   r   r$   r$   r$   r%   r   S  s    r   )1	functoolsrZ   r   Zdistutils.versionr   Znumpyr,   r^   r   Z
core.utilsr   r   r   r   Zcore.variabler	   commonr   r   r   r   r   Zfile_managerr   r   locksr   r   r   r   ZnetCDF4_r   r   r   r   r   r   r   r?   r   ModuleNotFoundErrorr   r9   r>   partialr   rB   rC   r   r$   r$   r$   r%   <module>   s8   
 {<