U
    f»{j†B  ã                   @   s–  d dl mZ d dlmZ d dlmZ d dlZd dlZd dl	Z	d dl
ZddlmZ ddlmZmZ ddlmZ d	Zd
d„ Zdd„ ZdZdQdd„ZdZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Z d#d$„ Z!d%d&„ Z"d'd(„ Z#d)d*„ Z$d+d,„ Z%d-d.„ Z&d/d0„ Z'd1d2„ Z(dRd3d4„Z)d5d6„ Z*d7d8„ Z+dSd9d:„Z,edTd;d<„ƒZ-dUd>d?„Z.d@dA„ Z/dBdC„ Z0dDdE„ Z1dVdFdG„Z2dHdI„ Z3dJdK„ Z4dWdMdN„Z5dOdP„ Z6dS )Xé    )Úcontextmanager)ÚGzipFileNé   )ÚFIFFé   )ÚloggerÚ
_file_like)Ú_cal_to_julian)r   éÿÿÿc                 C   s”   t |tjƒr||j9 }|  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjtjdd� ¡ ¡ |  tj||d� ¡ ¡ dS )zWrite data.ú>i4©ÚdtypeN)	Ú
isinstanceÚnpZndarrayÚsizeÚwriteÚarrayÚtobytesr   ÚFIFFV_NEXT_SEQ)ÚfidÚdataÚkindÚ	data_sizeZ
FIFFT_TYPEr   © r   úK/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/io/write.pyÚ_write   s    
r   c                 C   sf   t | tƒrRtddd� | dd… d¡}|dkr6tdƒ‚tt| dd… ƒd|  ƒ} | dkrbtd	ƒ‚| S )
z7Convert human-readable bytes to machine-readable bytes.é   é   )ÚMBÚGBéþÿÿÿNz-split_size has to end with either"MB" or "GB"r   ì        z$split_size cannot be larger than 2GB)r   ÚstrÚdictÚgetÚ
ValueErrorÚintÚfloat)Z
split_sizeÚexpr   r   r   Ú_get_split_size)   s    
r)   i   Fc                 C   sx   |   tjtjdd� ¡ ¡ |   tjtjdd� ¡ ¡ |   tjddd� ¡ ¡ |rVtjntj}|   tj|dd� ¡ ¡ dS )zWrite a FIFF_NOP.r   r   r   N)	r   r   r   r   ZFIFF_NOPr   Z
FIFFT_VOIDZFIFFV_NEXT_NONEr   )r   ÚlastZnext_r   r   r   Ú	write_nop:   s
    r+   r
   c                 C   s’   d}t  |¡}|jjdkr6|jdkr6td|j› d�ƒ‚|jdkrH| ¡ nd}|tkrntd|› dt› d|› �ƒ‚| d	¡j	}t
| |||tjd	ƒ d
S )z)Write a 32-bit integer tag to a fif file.é   Zuibr   z$Cannot safely write data with dtype z as intzValue z exceeds maximum allowed (z
) for tag r   N)r   Zasarrayr   r   r   Ú	TypeErrorÚmaxÚ	INT32_MAXZastypeÚTr   r   Ú	FIFFT_INT)r   r   r   r   Zmax_valr   r   r   Ú	write_intF   s    
ÿÿr2   c                 C   s,   d}t j|dd�j}t| |||tjdƒ dS )z:Write a double-precision floating point tag to a fif file.é   ú>f8r   N)r   r   r0   r   r   ÚFIFFT_DOUBLE©r   r   r   r   r   r   r   Úwrite_doubleV   s    r7   c                 C   s,   d}t j|dd�j}t| |||tjdƒ dS )z:Write a single-precision floating point tag to a fif file.r,   ú>f4r   N)r   r   r0   r   r   ÚFIFFT_FLOATr6   r   r   r   Úwrite_float]   s    r:   c                 C   s,   d}t j|dd�j}t| |||tjdƒ dS )z%Write a dau_pack16 tag to a fif file.r   z>i2r   N)r   r   r0   r   r   ZFIFFT_DAU_PACK16r6   r   r   r   Úwrite_dau_pack16d   s    r;   c                 C   s,   d}t j|dd�j}t| |||tjdƒ dS )z8Write a 64 bit complex floating point tag to a fif file.r3   ú>c8r   N©r   r   r0   r   r   ÚFIFFT_COMPLEX_FLOATr6   r   r   r   Úwrite_complex64k   s    r?   c                 C   s,   d}t j|dd�j}t| |||tjdƒ dS )z9Write a 128 bit complex floating point tag to a fif file.é   ú>c16r   Nr=   r6   r   r   r   Úwrite_complex128r   s    rB   c                 C   sH   t |ƒdkst‚d}t t|Ž ¡}tj|dd�}t| |||tjdƒ dS )z,Write a Julian-formatted date to a FIF file.é   r,   r   r   N)	ÚlenÚAssertionErrorr   Úsumr	   r   r   r   ZFIFFT_JULIAN)r   r   r   r   Zjdr   r   r   Úwrite_juliany   s
    rG   c                 C   s6   |  d¡}t|ƒ}d}|dkr2t| |||tj|ƒ dS )zWrite a string tag.Úlatin1z>ar   N)ÚencoderD   r   r   ZFIFFT_STRING)r   r   r   Zstr_datar   Zmy_dtyper   r   r   Úwrite_string‚   s
    
rJ   c                 C   s   t | |d |¡ƒ dS )zeWrite a colon-separated list of names.

    Parameters
    ----------
    data : list of strings
    ú:N)rJ   Újoin)r   r   r   r   r   r   Úwrite_name_list‹   s    rM   c                 C   sþ   d}t j|B }d|j d|jd   }|  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjt jdd� ¡ ¡ |  tj|dd� ¡ ¡ tj	|jd tj
d�}|jddd… |d|j…< |j|d< |  tj|dd� ¡ ¡ t| ƒ dS )	z3Write a single-precision floating-point matrix tag.é   @r,   r   r   r   r8   Néÿÿÿÿ)r   r9   r   Úndimr   r   r   r   r   ÚemptyÚint32ÚshapeÚcheck_fiff_length)r   r   ÚmatÚFIFFT_MATRIXZFIFFT_MATRIX_FLOATr   Údimsr   r   r   Úwrite_float_matrix•   s    

rX   c                 C   sþ   d}t j|B }d|j d|jd   }|  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjt jdd� ¡ ¡ |  tj|dd� ¡ ¡ tj	|jd tj
d�}|jddd	… |d|j…< |j|d	< |  tj|dd� ¡ ¡ t| ƒ dS )
z3Write a double-precision floating-point matrix tag.rN   r3   r,   r   r   r   r4   NrO   )r   r5   r   rP   r   r   r   r   r   rQ   rR   rS   rT   )r   r   rU   rV   ZFIFFT_MATRIX_DOUBLEr   rW   r   r   r   Úwrite_double_matrix©   s    

rY   c                 C   sî   d}t j|B }d|j d }|  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjt jdd� ¡ ¡ |  tj|dd� ¡ ¡ tjdtj	d�}|j
d |d< |j
d |d< d	|d	< |  tj|dd� ¡ ¡ t| ƒ d
S )zWrite integer 32 matrix tag.rN   r,   é   r   r   rC   r   r   r   N)r   r1   r   r   r   r   r   r   rQ   rR   rS   rT   )r   r   rU   rV   ZFIFFT_MATRIX_INTr   rW   r   r   r   Úwrite_int_matrix½   s    
r[   c                 C   sþ   d}t j|B }d|j d|jd   }|  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjt jdd� ¡ ¡ |  tj|dd� ¡ ¡ tj	|jd tj
d�}|jddd	… |d|j…< |j|d	< |  tj|dd� ¡ ¡ t| ƒ dS )
zWrite complex 64 matrix tag.rN   r3   r,   r   r   r   r<   NrO   )r   r>   r   rP   r   r   r   r   r   rQ   rR   rS   rT   )r   r   rU   rV   ZFIFFT_MATRIX_COMPLEX_FLOATr   rW   r   r   r   Úwrite_complex_float_matrixÒ   s    

r\   c                 C   sþ   d}t j|B }d|j d|jd   }|  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjt jdd� ¡ ¡ |  tj|dd� ¡ ¡ tj	|jd tj
d�}|jddd	… |d|j…< |j|d	< |  tj|dd� ¡ ¡ t| ƒ dS )
zWrite complex 128 matrix tag.rN   r@   r,   r   r   r   rA   NrO   )r   ZFIFFT_COMPLEX_DOUBLEr   rP   r   r   r   r   r   rQ   rR   rS   rT   )r   r   rU   rV   ZFIFFT_MATRIX_COMPLEX_DOUBLEr   rW   r   r   r   Úwrite_complex_double_matrixæ   s    

r]   c                     sn   dt  ¡  } t d| ¡} | ddg7 } ddlm‰  d ‡ fdd„| d	d	d
… D ƒ¡} t tj	| tj
dd�¡}|S )z‹Get (mostly) unique machine ID.

    Returns
    -------
    ids : array (length 2, int32)
        The machine identifier used in MNE.
    s   %012xs   ..s   00r   ©rI   ó    c                    s   g | ]}ˆ |d ƒ‘qS )Ú	hex_codecr   )Ú.0Úhr^   r   r   Ú
<listcomp>  s     zget_machid.<locals>.<listcomp>NrO   r   )Úcount)ÚuuidÚgetnodeÚreÚfindallÚcodecsrI   rL   r   ZflipudZ
frombufferrR   )ÚmacZidsr   r^   r   Ú
get_machidú   s    "rk   c                  C   s:   t t ¡ dƒ\} }t| ƒt|d ƒ } }tƒ tj| |dœS )zCreate a new file ID tag.g      ð?g    €„.A)ÚmachidÚversionÚsecsÚusecs)ÚdivmodÚtimer&   rk   r   ÚFIFFC_VERSION)rn   ro   r   r   r   Úget_new_file_id  s     ÿrs   c                 C   sÀ   |dkrt ƒ n|}d}|  tj|dd� ¡ ¡ |  tjtjdd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjtjdd� ¡ ¡ tj|d |d d |d d |d	 |d
 gdd�}|  | ¡ ¡ dS )zWrite fiff id.Nr   r   r   rm   rl   r   r   rn   ro   )Ú_generate_meas_idr   r   r   r   r   ZFIFFT_ID_STRUCTr   )r   r   Úid_r   Zarrr   r   r   Úwrite_id  s    

 
 þþrv   c                 C   s   t | tj|ƒ dS )zWrite a FIFF_BLOCK_START tag.N)r2   r   ZFIFF_BLOCK_START©r   r   r   r   r   Ústart_block&  s    rx   c                 C   s   t | tj|ƒ dS )zWrite a FIFF_BLOCK_END tag.N)r2   r   ZFIFF_BLOCK_ENDrw   r   r   r   Ú	end_block+  s    ry   c                 C   s¤   t | ƒr*t dt| ƒ ¡ | }| d¡ nLt| ƒ} t | ¡d  ¡ dkrbt d¡ t	| ddd�}nt d	¡ t
| dƒ}t|tj|ƒ t|tjd
ƒ t|tjd
ƒ |S )aR  Open a fif file for writing and writes the compulsory header tags.

    Parameters
    ----------
    fname : string | fid
        The name of the file to open. It is recommended
        that the name ends with .fif or .fif.gz. Can also be an
        already opened file.
    id_ : dict | None
        ID to use for the FIFF_FILE_ID.
    zWriting using %s I/Or   r   z.gzzWriting using gzipÚwbr   )ÚcompresslevelzWriting using normal I/OrO   )r   r   ÚdebugÚtypeÚseekr"   ÚopÚsplitextÚlowerr   Úopenrv   r   ZFIFF_FILE_IDr2   ZFIFF_DIR_POINTERZFIFF_FREE_LIST©Úfnameru   r   r   r   r   Ú
start_file0  s    


r…   c              	   c   s*   t | |d��}|V  t|ƒ W 5 Q R X dS )z3Start and (if successfully written) close the file.©ru   N)r…   Úend_filerƒ   r   r   r   Ústart_and_end_fileQ  s    rˆ   Tc                 C   s$   |   ¡ dkr |r|  ¡  tdƒ‚dS )z8Ensure our file hasn't grown too large to work properly.r!   zoFIFF file exceeded 2GB limit, please split file, reduce split_size (if possible), or save to a different formatN)ÚtellÚcloseÚIOError)r   rŠ   r   r   r   rT   Y  s    rT   c                 C   s    t | dd� t| ƒ |  ¡  dS )z9Write the closing tags to a fif file and closes the file.T)r*   N)r+   rT   rŠ   )r   r   r   r   r‡   c  s    r‡   c                 C   sf  d}|   tjtjdd� ¡ ¡ |   tjtjdd� ¡ ¡ |   tj|dd� ¡ ¡ |   tjtjdd� ¡ ¡ |   tj|d dd� ¡ ¡ |   tj|d dd� ¡ ¡ |d dd…dd…f }|d dd…df }|   tj|d	d� ¡ ¡ |   tj|d	d� ¡ ¡ tj 	|d ¡}|dd…dd…f }|dd…df }|   tj|d	d� ¡ ¡ |   tj|d	d� ¡ ¡ dS )
z,Write a coordinate transformation structure.éh   r   r   ÚfromÚtoÚtransNrC   r8   )
r   r   r   r   ZFIFF_COORD_TRANSr   ZFIFFT_COORD_TRANS_STRUCTr   ZlinalgÚinv)r   r�   r   ZrotÚmoveZ	trans_invr   r   r   Úwrite_coord_transj  s     r’   c                 C   s¨  d}|   tjtjdd� ¡ ¡ |   tjtjdd� ¡ ¡ |   tj|dd� ¡ ¡ |   tjtjdd� ¡ ¡ |   tj|d dd� ¡ ¡ |   tj|d dd� ¡ ¡ |   tj|d dd� ¡ ¡ |   tj|d dd� ¡ ¡ |   tj|d	 dd� ¡ ¡ |   tj|d
 dd� ¡ ¡ |   tj|d dd� ¡ ¡ |   tj|d dd� ¡ ¡ |   tj|d dd� ¡ ¡ |d dd… }|   tj|dd� ¡ ¡ |   ddt|ƒ  ¡ dS )z1Write a channel information record to a fif file.é`   r   r   ZscannoZlognor   Úranger8   ÚcalZ	coil_typeÚlocÚunitZunit_mulÚch_nameNé   z>có    r@   )	r   r   r   r   ZFIFF_CH_INFOr   ZFIFFT_CH_INFO_STRUCTr   rD   )r   Úchr   r˜   r   r   r   Úwrite_ch_info‚  s"    rœ   c                 C   s
  |dk	�rd}|rt | tjƒ |dk	r4t| tj|ƒ |D ]º}|  t tjd¡ 	¡ ¡ |  t tj
d¡ 	¡ ¡ |  tj|dd� 	¡ ¡ |  t tjd¡ 	¡ ¡ |  t |d d¡ 	¡ ¡ |  t |d d¡ 	¡ ¡ |  t |d dd… d	¡ 	¡ ¡ q8|�rt| tjƒ dS )
z5Write a set of digitizer data points into a fif file.Nr   r   r   r   ÚidentÚrrC   r8   )rx   r   ZFIFFB_ISOTRAKr2   ZFIFF_MNE_COORD_FRAMEr   r   r   ZFIFF_DIG_POINTr   ZFIFFT_DIG_POINT_STRUCTr   ry   )r   ÚdigÚblockZcoord_framer   Údr   r   r   Úwrite_dig_pointsž  s     
$r¢   c                 C   s   t | ||dd�S )z:Write a single-precision sparse compressed row matrix tag.Úcsr©Úfmt©Úwrite_float_sparse©r   r   rU   r   r   r   Úwrite_float_sparse_rcs³  s    r©   c                 C   s   t | ||dd�S )z=Write a single-precision sparse compressed column matrix tag.Úcscr¤   r¦   r¨   r   r   r   Úwrite_float_sparse_ccs¸  s    r«   Úautoc                 C   s¦  ddl m} ddlm}m} |dkr8t||jƒr4dnd}|dkrL|j}|}n
|j}|}t||ƒsxtd| 	¡ t
|ƒf ƒ‚|d	> }	tj|	B }
|j}|jd }d
| d
|  d
|d   d	 }|  tj|dd� ¡ ¡ |  tj|
dd� ¡ ¡ |  tj|dd� ¡ ¡ |  tjtjdd� ¡ ¡ |  tj|jdd� ¡ ¡ |  tj|jdd� ¡ ¡ |  tj|jdd� ¡ ¡ ||jd |jd dg}|  tj|dd� ¡ ¡ t| ƒ dS )z:Write a single-precision floating-point sparse matrix tag.r   )Úsparser   )Ú_matrix_coding_CCSÚ_matrix_coding_RCSr¬   r£   rª   zMust write %s, got %sr@   r,   r   r   r8   r   N)Zscipyr­   Útagr®   r¯   r   Z
csr_matrixZ
csc_matrixr-   Úupperr}   r   r9   ZnnzrS   r   r   r   r   r   r   ÚindicesZindptrrT   )r   r   rU   r¥   r­   r®   r¯   ZneedÚbitsrV   ZFIFFT_MATRIX_FLOAT_RCSZnnzmZnrowr   rW   r   r   r   r§   ½  s4    


 r§   c                  C   s.   t ƒ } tj| d< tƒ | d< t\| d< | d< | S )zGenerate a new meas_id dict.rm   rl   rn   ro   )r#   r   rr   rk   Ú	DATE_NONEr†   r   r   r   rt   à  s
    

rt   )F)N)N)N)T)FN)r¬   )7Ú
contextlibr   Úgzipr   Úos.pathÚpathr   rg   rq   re   Únumpyr   Ú	constantsr   Úutilsr   r   Zutils.numericsr	   r´   r   r)   Z_NEXT_FILE_BUFFERr+   r/   r2   r7   r:   r;   r?   rB   rG   rJ   rM   rX   rY   r[   r\   r]   rk   rs   rv   rx   ry   r…   rˆ   rT   r‡   r’   rœ   r¢   r©   r«   r§   rt   r   r   r   r   Ú<module>   s\   
			


!



#