B
    Sùw\Ï$  ã               @   sZ   d dl Z d dlZd dlZG dd„ de jƒZe  e¡Ze  e¡Ze  e¡Z	G dd„ dƒZ
dS )é    Nc               @   s   e Zd ZdZdS )Ú_OpaqueStructz% creates a ctypes opaque struct type N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú7C:\Users\mkemp\Documents\FDI projects\tdms\tdms_file.pyr      s   r   c               @   sd   e 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S )ÚTDMSFilec             C   s.   t jd  t j| 7  < t d¡| _d | _d S )NÚPATHznilibddc.dll)ÚosÚenvironÚpathsepÚctypesZWinDLLÚwindllÚfile_handle)ÚselfÚbin_pathr   r   r   Ú__init__   s    zTDMSFile.__init__c             C   s\   | j j}t | ¡ ¡}tƒ }||t d¡dt |¡ƒ}|dk rRtd|d|  |¡ƒ || _	dS )z$ open the file and store the handle Né   r   z&DDC error while opening the file. Codeú:)
r   ZDDC_OpenFileExr   Úc_char_pÚencodeÚDDCFileHandleÚbyrefÚprintÚ_get_error_descriptionr   )r   Ú	file_nameÚ
c_functionZ	file_pathr   Úrcr   r   r   Úopen   s    zTDMSFile.openc             C   s   | j j}|| jƒ}dS )z close the fileN)r   ZDDC_CloseFiler   )r   r   r   r   r   r   Úclose   s    zTDMSFile.closec          
   C   s†   |   ¡ \}}|d }|  |¡\}}i }xT|D ]L}|  |d¡}|  |d¡}	|  |¡\}
}}}|
||< td|d|	d|d|ƒ q,W |||fS )z& read the data and return it in a dictr   ÚnameZunit_stringzchannel:zunits:z
nb points:ztype:)Ú_read_group_handlesÚ_read_channel_handlesÚ_read_channel_propertyÚ_read_channel_datar   )r   Zn_groupsÚgroup_handlesZgroup_hÚ	nchannelsÚchannel_handlesZchannel_dataZ	channel_hr!   ÚunitsÚdataÚperiodÚnpointsÚdtyper   r   r   Úread$   s    
zTDMSFile.readc             C   sÎ   | j j}t ¡ }||t |¡ƒ}|j}| j j}t ¡ }||t |¡ƒ}|j}| j j}t 	d 
¡ ¡}t ¡ }	|||t |	¡ƒ}|	j}
| j j}t |t d¡¡}t |¡}|j tj¡}||d||ƒ}||
||fS )z6 read the data in a float64 channel into a numpy arrayZwf_incrementZfloat64r   )r   ZDDC_GetNumDataValuesr   Zc_ulonglongr   ÚvalueZDDC_GetDataTypeZc_intZDDC_GetChannelPropertyDoubler   r   Zc_doubleZDDC_GetDataValuesÚnpZzerosr-   ZascontiguousarrayZdata_asZc_void_p)r   Úchannel_handler   Z	npoints_cr   r,   Zdtype_cr-   Ú
property_cZperiod_cr+   r*   Zcontiguous_dataZdata_cr   r   r   r%   6   s&    
zTDMSFile._read_channel_datac             C   sP   | j j}t ¡ }|| jt |¡ƒ}|j}| j j}t| ƒ }|| j||ƒ}||fS )z! read the array of group handles )	r   ZDDC_GetNumChannelGroupsr   Úc_uintr   r   r/   ZDDC_GetChannelGroupsÚDDCChannelGroupHandle)r   r   Z	ngroups_cr   Zngroupsr&   r   r   r   r"   U   s    
zTDMSFile._read_group_handlesc             C   sL   | j j}t ¡ }||t |¡ƒ}|j}| j j}t| ƒ }||||ƒ}||fS )z# read the array of channel handles )r   ZDDC_GetNumChannelsr   r3   r   r/   ZDDC_GetChannelsÚDDCChannelHandle)r   Úgroup_handler   Znchannels_cr   r'   r(   r   r   r   r#   e   s    
zTDMSFile._read_channel_handlesc             C   sl   t  | ¡ ¡}| jj}t  ¡ }|| j|t  |¡ƒ}|j}| jj	}t  
|d ¡}|| j|||d ƒ}|j ¡ S )zF read a certain file-level property --- nb: the value must be a stringr   )r   r   r   r   ZDDC_GetFileStringPropertyLengthr3   r   r   r/   ZDDC_GetFilePropertyÚcreate_string_bufferÚdecode)r   Úproperty_stringr2   r   Úlength_cr   ÚlengthÚstring_cr   r   r   Ú_read_file_propertyu   s    zTDMSFile._read_file_propertyc       	      C   sl   t  | ¡ ¡}| jj}t  ¡ }|||t  |¡ƒ}|j}| jj}t  	|d ¡}| j ||||d ¡}|j 
¡ S )zG read a certain group-level property --- nb: the value must be a stringr   )r   r   r   r   Z'DDC_GetChannelGroupStringPropertyLengthr3   r   r/   ZDDC_GetChannelGroupPropertyr7   r8   )	r   r6   r9   r2   r   r:   r   r;   r<   r   r   r   Ú_read_group_property‡   s    zTDMSFile._read_group_propertyc       	      C   sh   t  | ¡ ¡}| jj}t  ¡ }|||t  |¡ƒ}|j}| jj}t  	|d ¡}|||||d ƒ}|j 
¡ S )zG read a certain group-level property --- nb: the value must be a stringr   )r   r   r   r   Z"DDC_GetChannelStringPropertyLengthr3   r   r/   ZDDC_GetChannelPropertyr7   r8   )	r   r1   r9   r2   r   r:   r   r;   r<   r   r   r   r$   ™   s    zTDMSFile._read_channel_propertyc             C   s   | j j}tj|_||ƒ ¡ S )z return the DDC error string )r   ZDDC_GetLibraryErrorDescriptionr   r   Zrestyper8   )r   Z
error_codeZcfuncr   r   r   r   «   s    zTDMSFile._get_error_descriptionN)r   r   r   r   r   r    r.   r%   r"   r#   r=   r>   r$   r   r   r   r   r   r	      s   r	   )r   r   Únumpyr0   Z	Structurer   ZPOINTERr   r4   r5   r	   r   r   r   r   Ú<module>   s   


