§
    2?Äjª}  ã                   ó<  — d Z ddlmZ ddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlZddlZddlmZmZmZ ddlmZmZ  ej        e¦  «        Z G d„ d	¦  «        Z G d
„ d¦  «        Z G d„ de¦  «        Zdej        de	ee e	e!         f                  fd„Z"dS )a…  Read SPE files.

This plugin supports reading files saved in the Princeton Instruments
SPE file format.

Parameters
----------
check_filesize : bool
    The number of frames in the file is stored in the file header. However,
    this number may be wrong for certain software. If this is `True`
    (default), derive the number of frames also from the file size and
    raise a warning if the two values do not match.
char_encoding : str
    Deprecated. Exists for backwards compatibility; use ``char_encoding`` of
    ``metadata`` instead.
sdt_meta : bool
    Deprecated. Exists for backwards compatibility; use ``sdt_control`` of
    ``metadata`` instead.

Methods
-------
.. note::
    Check the respective function for a list of supported kwargs and detailed
    documentation.

.. autosummary::
    :toctree:

    SpePlugin.read
    SpePlugin.iter
    SpePlugin.properties
    SpePlugin.metadata

é    )ÚdatetimeN)
ÚAnyÚCallableÚDictÚIteratorÚListÚMappingÚOptionalÚSequenceÚTupleÚUnioné   )ÚRequestÚIOModeÚInitializationError)ÚPluginV3ÚImagePropertiesc                   ó&  — e Zd ZdZdddddddœZi d	d
“dd ej        g d¢¦  «        df“dd“dd“dd“dd“dd“dd“dd“dd“dd “d!d"“d#d$“d%d&“d'd(“d)d*“d+d,“i d-d.“d/d0“d1d2“d3d4“d5d6“d7d8“d9d:“d;d<“d=d>“d?d@“dAdB“dCdD“dEdF“dGdH“dIdJ“dKdL“dMdN“¥dOdPdQdRdSdTdUddVdWdXdYddZœ¥Zd[Z ej        ej	        ¦  «         ej        ej
        ¦  «         ej        ej        ¦  «         ej        ej        ¦  «         ej        ej        ¦  «        d\œZg d]¢Zg d^¢Zd_gZd`S )aÚSpecaW  SPE file specification data

    Tuples of (offset, datatype, count), where offset is the offset in the SPE
    file and datatype is the datatype as used in `numpy.fromfile`()

    `data_start` is the offset of actual image data.

    `dtypes` translates SPE datatypes (0...4) to numpy ones, e. g. dtypes[0]
    is dtype("<f") (which is np.float32).

    `controllers` maps the `type` metadata to a human readable name

    `readout_modes` maps the `readoutMode` metadata to something human readable
    although this may not be accurate since there is next to no documentation
    to be found.
    )él   ú<h)é*   ú<H)i�  r   )i¦  z<Q)i¦  z<i)iÈ  ú<f)ÚdatatypeÚxdimÚydimÚxml_footer_offsetÚ	NumFramesÚfile_header_verÚNumROI)iæ  r   ÚROIsiè  ))Ústartxr   )Úendxr   )Úgroupxr   )Ústartyr   )Úendyr   )Úgroupyr   é
   ÚxDimDet)é   r   ÚyDimDet)é   r   Ú	VChipXdim)é   r   Ú	VChipYdim)é   r   Úcontroller_version)r   r   Úlogic_output)r   r   Úamp_high_cap_low_noise)é   r   Úmode)é   r   Úexposure_sec)r)   r   Údate)é   z<10SÚdetector_temp)é$   r   Údetector_type)é(   r   Úst_diode)é,   r   Ú
delay_time)é.   r   Úshutter_control)é2   r   Úabsorb_live)é4   r   Úabsorb_mode)é6   r   Úcan_do_virtual_chip)é8   r   Úthreshold_min_live)é:   r   Úthreshold_min_val)é<   r   Úthreshold_max_live)é@   r   Úthreshold_max_val)éB   r   Ú
time_local)é¬   ú<7SÚtime_utc)é³   rU   Ú
adc_offset)é¼   r   Úadc_rate)é¾   r   Úadc_type)éÀ   r   Úadc_resolution)éÂ   r   Úadc_bit_adjust)éÄ   r   Úgain)éÆ   r   Úcomments)éÈ   z<80Sé   Ú	geometric)iX  r   )i°  z<16S)iæ  z<436S)éb   r   )éd   r   )éf   r   )éh   r   )i   r   )iÀ  r   )i”  r   )iÈ  r   )iÊ  r   )Ú
sw_versionÚspare_4Ú
XPrePixelsÚXPostPixelsÚ
YPrePixelsÚYPostPixelsÚreadout_timer   ÚtypeÚclockspeed_usÚreadout_modeÚwindow_sizer    i  )r   é   r   é   r7   )znew120 (Type II)zold120 (Type I)ÚST130ÚST121ÚST138zDC131 (PentaMax)zST133 (MicroMax/Roper)zST135 (GPIB)ÚVTCCDzST116 (GPIB)zOMA3 (GPIB)ÚOMA4)z
full framezframe transferÚkineticsrm   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚbasicÚnpÚdtypeÚmetadataÚ
data_startÚfloat32Úint32Úint16Úuint16Úuint32ÚdtypesÚcontrollersÚreadout_modesÚ	no_decode© ó    úDC:\Projects\Historical\venv\Lib\site-packages\imageio/plugins/spe.pyr   r   @   s&  € € € € € ðð ð$  ØØØ(Ø!Ø'ðð €EðBà�,ðBð 	ØØˆBŒHðð ð ñ	ô 	ð ð
ðBð$ 	�9ð%Bð& 	�:ð'Bð( 	�Zð)Bð* 	�Zð+Bð. 	˜ið/Bð0 	˜	ð1Bð2 	! )ð3Bð4 	�	ð5Bð6 	˜
ð7Bð8 	�ð9Bð: 	˜ð;Bð< 	˜ð=Bð> 	�Jð?Bð@ 	�jðABðF 	˜:ðGBð BðH 	�zðIBðJ 	�zðKBðL 	˜zðMBðN 	˜jðOBðP 	˜ZðQBðR 	˜jðSBðT 	˜ZðUBðV 	�lðWBðX 	�LðYBðZ 	�kð[Bð\ 	�Kð]Bð^ 	�Kð_Bð` 	˜+ðaBðb 	˜+ðcBðd 	�ðeBðf 	Ð$ðgBðh 	�[ðiBð Bðj $Ø!Ø Ø"Ø!Ø"Ø#Ø(ØØ%Ø$Ø#Ø'ðCBð Bð B€HðH €Jð ˆ2Œ8�B”JÑÔØˆ2Œ8�B”HÑÔØˆ2Œ8�B”HÑÔØˆ2Œ8�B”IÑÔØˆ2Œ8�B”IÑÔðð €Fðð ð €Kð" AÐ@Ð@€Mð �€I€I€Ir’   r   c                   ó   ‡ — e Zd ZdZi dd“dd“dd“dd“d	d“d
d“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“Zdd d!d"d#œZ G d$„ d%¦  «        Zi d& ed ed'd(¦  «        e¦  «        “d) ed ed(d*¦  «        e¦  «        “d+ ed ed,d¦  «        e	¦  «        “d- ed ed.d/¦  «        e
d0¦  «        “d1 ed edd2¦  «        e	¦  «        “d3 ed ed4d5¦  «        e¦  «        “d6 edd2d7„ ¦  «        “d8 edd9d:„ ¦  «        “d; ed ed<d9¦  «        e¦  «        “d= edd>d?„ ¦  «        “d@ ed edAd<¦  «        e
dB¦  «        “dC ed edd¦  «        ˆ fdD„¦  «        “dE ed ed5dA¦  «        e
d0¦  «        “dF ed ed,d¦  «        e¦  «        “dG ed eddH¦  «        e
dI¦  «        “dJ ed edd¦  «        e¦  «        “dK ed edHd<¦  «        e
d0¦  «        “ ed edd¦  «        e¦  «         ed ed<dL¦  «        e
d0¦  «         ed edd¦  «        e¦  «         ed ed/dM¦  «        e
d0¦  «         ed edLdN¦  «        e
d0¦  «         ed edNdO¦  «        e
d0¦  «         ed edOd.¦  «        e
d0¦  «        dPœ¥dQ ed edRdS¦  «        dT„ ¦  «        idUœZedVee	         dWeeef         fdX„¦   «         ZedVee	         dYeeef         dWee	ef         fˆ fdZ„¦   «         Zed[e	d\e	dWeed]f         fˆ fd^„¦   «         Zedcd`edae	fˆ fdb„¦   «         Zˆ xZS )dÚSDTControlSpeca&  Extract metadata written by the SDT-control software

    Some of it is encoded in the comment strings
    (see :py:meth:`parse_comments`). Also, date and time are encoded in a
    peculiar way (see :py:meth:`get_datetime`). Use :py:meth:`extract_metadata`
    to update the metadata dict.
    u   JÃ¤nrw   ÚJanÚFebr   u   MÃ¤rrx   ÚMarÚAprr5   ÚMairf   ÚMayÚJunr+   ÚJulé   ÚAugr7   ÚSepé	   ÚOktr)   ÚOctÚNové   ÚDezé   ÚDecÚstandardÚTOCCSLr~   Ú	arbitrary)ÚSEQUÚSETOÚKINEÚSEARc            
       ó¢   — e Zd ZU dZeed<   	 eed<   	 eege	f         ed<   	 e
def         ed<   	 edfdededeege	f         dee         fd„ZdS )úSDTControlSpec.CommentDescz>Describe how to extract a metadata entry from a comment stringÚnÚsliceÚcvtNÚscalec                 ó>   — || _         || _        || _        || _        d S ©N)r²   r³   r´   rµ   )Úselfr²   r³   r´   rµ   s        r“   Ú__init__z#SDTControlSpec.CommentDesc.__init__ó   s$   € ð ˆDŒFØˆDŒJØˆDŒHØˆDŒJˆJˆJr’   )r   r€   r�   r‚   ÚintÚ__annotations__r³   r   Ústrr   r   Úfloatr
   r¹   r‘   r’   r“   ÚCommentDescr±   ç   s¼   € € € € € € ØLÐLàˆˆ‰Ø7Øˆˆ‰Ø>Ø�s�e˜S�jÔ!Ð!Ð!Ñ!Ø<Ø�T˜5�[Ô!Ð!Ð!Ñ!Ø1ð ),Ø%)ð
	ð 
	àð
	ð ð
	ð ˜3˜% ˜*Ô%ð	
	ð
 ˜E”?ð
	ð 
	ð 
	ð 
	ð 
	ð 
	r’   r¾   Úsdt_major_versionrR   éD   Úsdt_minor_versionéF   Úsdt_controller_namer   Úexposure_timerP   éI   g�íµ ÷Æ°>Ú
color_coder/   Údetection_channelsé   r1   Úbackground_subtractionc                 ó   — | dk    S )NÚBr‘   ©Úxs    r“   ú<lambda>zSDTControlSpec.<lambda>  s
   € À1ÈÂ8€ r’   Ú	em_activeé    c                 ó   — | dk    S )NÚEr‘   rÌ   s    r“   rÎ   zSDTControlSpec.<lambda>  s
   € °a¸3²h€ r’   Úem_gainé   Úmodulation_activeé!   c                 ó   — | dk    S )NÚAr‘   rÌ   s    r“   rÎ   zSDTControlSpec.<lambda>
  s
   € ¸aÀ3ºh€ r’   Ú
pixel_sizeé   gš™™™™™¹?Úsequence_typec                 ó   •— ‰j         |          S r·   )Úsequence_types)rÍ   Ú	__class__s    €r“   rÎ   zSDTControlSpec.<lambda>  s   ø€ ¨9Ô+CÀAÔ+F€ r’   ÚgridÚn_macroÚdelay_macroé   gü©ñÒMbP?Ún_miniÚ
delay_minié%   éO   rB   é7   )Ún_microÚdelay_microÚ	n_subpicsÚdelay_shutterÚdelay_prebleachÚbleach_timeÚrecovery_timeÚbleach_piezo_activeé"   é#   c                 ó   — | dk    S )NÚzr‘   rÌ   s    r“   rÎ   zSDTControlSpec.<lambda>  s   € È1ÐPSÊ8€ r’   ))rf   r   )rf   rw   rd   Úreturnc                 óÆ   — | d         dd…         dk    rdS 	 t          | d         dd…         ¦  «        t          | d         dd…         ¦  «        fS # t          $ r Y dS w xY w)a-  Get the version of SDT-control metadata encoded in the comments

        Parameters
        ----------
        comments
            List of SPE file comments, typically ``metadata["comments"]``.

        Returns
        -------
        Major and minor version. ``-1, -1`` if detection failed.
        r5   rÂ   éL   ÚCOMVER)éÿÿÿÿrø   éN   éP   )rº   Ú
ValueError)rd   s    r“   Úget_comment_versionz"SDTControlSpec.get_comment_version!  s}   € ð �AŒ;�r˜"�uÔ Ò)Ð)Ø�6ð	Ý�x ”{ 2 b 5Ô)Ñ*Ô*­C°¸´¸B¸r¸EÔ0BÑ,CÔ,CÐCÐCøÝð 	ð 	ð 	Ø�6�6ð	øøøs   ˜9A Á
A ÁA Úversionc           	      ó¾  •— i }t          |d         dz   ¦  «        D ]»}	 ‰j        |d         |f         }n# t          $ r Y Œ%w xY w|                     ¦   «         D ]}\  }}	 |                     | |j                 |j                 ¦  «        }|j        �
||j        z  }|||<   ŒH# t          $ r)}t          j
        d|› d|› �¦  «         d||<   Y d}~Œvd}~ww xY wŒ¼|‰j        vrTd                     t          d„ ‰j        ¦  «        ¦  «        }	t          j
        d|d         › d	|d         d
›d|	› d�¦  «         | d         | d         z   }
|
                     ¦   «         |d<   |S )aC  Extract SDT-control metadata from comments

        Parameters
        ----------
        comments
            List of SPE file comments, typically ``metadata["comments"]``.
        version
            Major and minor version of SDT-control metadata format

        Returns
        -------
        Dict of metadata
        rw   r   Nz-Failed to decode SDT-control metadata field `z`: z, c                 ó*   — | d         › d| d         d›�S )Nr   ú.rw   Ú02r‘   rÌ   s    r“   rÎ   z/SDTControlSpec.parse_comments.<locals>.<lambda>]  s   €   1¤Ð1Ð1¨¨!¬Ð1Ð1Ð1€ r’   z)Unsupported SDT-control metadata version r   r  z. Only versions z@ are supported. Some or all SDT-control metadata may be missing.r   Úcomment)ÚrangeÚcomment_fieldsÚKeyErrorÚitemsr´   r²   r³   rµ   Ú	ExceptionÚwarningsÚwarnÚjoinÚmapÚstrip)rd   rý   Úsdt_mdÚminorÚcmtÚnameÚspecÚvÚeÚsupported_verr  rÞ   s              €r“   Úparse_commentszSDTControlSpec.parse_comments5  sß  ø€ ð" ˆÝ˜7 1œ:¨™>Ñ*Ô*ð 	(ð 	(ˆEð
ØÔ.¨w°q¬z¸5Ð/@ÔA��øÝð ð ð Ø�ðøøøà!Ÿiši™kœkð 
(ð 
(‘
��dð	(ØŸš ¨$¬&Ô!1°$´*Ô!=Ñ>Ô>�AØ”zÐ-Ø˜TœZ™˜Ø#$�F˜4‘L�LøÝ ð (ð (ð (Ý”MØTÈÐTÐTÐQRÐTÐTñô ð ð $(�F˜4‘L�L�L�L�L�Løøøøð	(øøøð
(ð ˜)Ô2Ð2Ð2Ø ŸIšIÝÐ1Ð1°9Ô3KÑLÔLñô ˆMõ ŒMðC¸GÀA¼Jð Cð CÈÐQRÌÐZð Cð CØ!.ðCð Cð Cñô ð ð
 ˜1”+ ¨¤Ñ+ˆØ#ŸMšM™OœOˆˆyÑØˆs(   Ÿ5µ
AÁAÁAB!Â!
CÂ+CÃCr9   ÚtimeNc                 ó–  •— 	 ‰j         | dd…                  }t          t          | dd…         ¦  «        |t          | dd…         ¦  «        t          |dd…         ¦  «        t          |dd…         ¦  «        t          |dd…         ¦  «        ¦  «        S # t          $ r)}t                               d|› d�¦  «         Y d	}~d	S d	}~ww xY w)
ai  Turn date and time saved by SDT-control into proper datetime object

        Parameters
        ----------
        date
            SPE file date, typically ``metadata["date"]``.
        time
            SPE file date, typically ``metadata["time_local"]``.

        Returns
        -------
        File's datetime if parsing was succsessful, else None.
        r   rf   r¡   r   r5   r+   z1Failed to decode date from SDT-control metadata: r   N)Úmonthsr   rº   r  ÚloggerÚinfo)r9   r  Úmonthr  rÞ   s       €r“   Úget_datetimezSDTControlSpec.get_datetimeh  sÝ   ø€ ð	RØÔ$ T¨!¨A¨#¤YÔ/ˆEÝÝ�D˜˜1˜”I‘”ØÝ�D˜˜1˜”I‘”Ý�D˜˜1˜”I‘”Ý�D˜˜1˜”I‘”Ý�D˜˜1˜”I‘”ñô ð øõ ð 	Rð 	Rð 	RÝ�KŠKÐPÈAÐPÐPÐPÑQÔQÐQÐQÐQÐQÐQÐQÐQøøøøð	Røøøs   ƒBB Â
CÂCÃCÚlatin1ÚmetaÚchar_encodingc                 óä  •— ‰                      | d         ¦  «        }t          d„ |D ¦   «         ¦  «        rt                               d¦  «         dS ‰                     | d         |¦  «        }|                      d¦  «         |                      |¦  «         ‰                     | d         | d         ¦  «        }|r/|| d<   |                      d¦  «         |                      d¦  «         | d         }	 |                     |¦  «        | d	<   |                      d¦  «         n$# t          $ r t          j        d
¦  «         Y nw xY w|                      d¦  «         |                      d¦  «         dS )ar  Extract SDT-control metadata from SPE metadata

        SDT-control stores some metadata in comments and other fields.
        Extract them and remove unused entries.

        Parameters
        ----------
        meta
            SPE file metadata. Modified in place.
        char_encoding
            Character encoding used to decode strings in the metadata.
        rd   c              3   ó"   K  — | ]
}|d k     V — ŒdS )r   Nr‘   ©Ú.0Úcs     r“   ú	<genexpr>z2SDTControlSpec.extract_metadata.<locals>.<genexpr>“  s&   è è € Ð%Ð%˜ˆq�1ŠuÐ%Ð%Ð%Ð%Ð%Ð%r’   zSDT-control comments not found.Nr9   rS   r   rm   Úmodulation_scriptzHFailed to decode SDT-control laser modulation script. Bad char_encoding?rV   r8   )rü   Úanyr  Údebugr  ÚpopÚupdater  ÚdecodeÚUnicodeDecodeErrorr  r	  )r  r  ÚcomverÚsdt_metaÚdtÚsp4rÞ   s         €r“   Úextract_metadatazSDTControlSpec.extract_metadata„  sŒ  ø€ ð ×.Ò.¨t°JÔ/?Ñ@Ô@ˆÝÐ%Ð%˜fÐ%Ñ%Ô%Ñ%Ô%ð 	å�LŠLÐ:Ñ;Ô;Ð;ØˆFà×+Ò+¨D°Ô,<¸fÑEÔEˆØ�Š�ÑÔÐØ�Š�HÑÔÐð ×#Ò# D¨¤L°$°|Ô2DÑEÔEˆØð 	#Ø!ˆD�ÑØ�HŠH�VÑÔÐØ�HŠH�\Ñ"Ô"Ð"à�9Œoˆð	Ø(+¯
ª
°=Ñ(AÔ(AˆDÐ$Ñ%Ø�HŠH�YÑÔÐÐøÝ!ð 	ð 	ð 	ÝŒMð8ñô ð ð ð ð	øøøð 	�Š�ÑÔÐØ�Š�Ñ Ô Ð Ð Ð s   Ã4-D" Ä"EÅE)r  )r   r€   r�   r‚   r  rÝ   r¾   r³   rº   r¼   r½   r  Ústaticmethodr   r   rü   r   r   r  r   r   r  r	   r1  Ú__classcell__©rÞ   s   @r“   r•   r•   Á   sÔ  ø€ € € € € ðð ðà�ðð 	ˆqðð 	ˆqð	ð
 	�ðð 	ˆqðð 	ˆqðð 	ˆqðð 	ˆqðð 	ˆqðð 	ˆqðð 	ˆqðð 	ˆqðð 	ˆrðð 	ˆrðð  	ˆrð!ð" 	ˆrð#ð$ 	ˆrð%€Fð. ØØØðð €Nðð ð ð ð ñ ô ð ð2
Ø  ¨Q°°°b¸"±´¸sÑ!CÔ!Cð
à  ¨Q°°°b¸"±´¸sÑ!CÔ!Cð
ð " ; ;¨q°%°%¸¸1±+´+¸sÑ#CÔ#Cð
ð ˜[˜[¨¨E¨E°"°b©M¬M¸5À&ÑIÔIð	
ð
 ˜+˜+ a¨¨¨r°2©¬¸Ñ<Ô<ð
ð ! + +¨a°°°r¸2±´ÀÑ"DÔ"Dð
ð % k k°!°RÐ9KÐ9KÑ&LÔ&Lð
ð ˜˜ Q¨Ð,>Ð,>Ñ?Ô?ð
ð �{�{ 1 e e¨B°¡m¤m°SÑ9Ô9ð
ð    ¨Q°Ð4FÐ4FÑ!GÔ!Gð
ð ˜+˜+ a¨¨¨r°2©¬¸¸sÑCÔCð
ð ˜[˜[Ø�5�5˜˜B‘<”<Ð!FÐ!FÐ!FÐ!Fñô ð
ð �K�K  5 5¨¨R¡=¤=°%¸Ñ@Ô@ð
ð  �{�{ 1 e e¨A¨q¡k¤k°3Ñ7Ô7ð!
ð" ˜;˜; q¨%¨%°°B©-¬-¸ÀÑGÔGð#
ð$ �k�k ! U U¨1¨a¡[¤[°#Ñ6Ô6ð%
ð& ˜+˜+ a¨¨¨r°2©¬¸¸vÑFÔFð'
ð( #�{ 1 e e¨A¨r¡l¤l°CÑ8Ô8Ø&˜; q¨%¨%°°B©-¬-¸ÀÑGÔGØ$˜ Q¨¨¨a°©¬°cÑ:Ô:Ø(˜[¨¨E¨E°"°b©M¬M¸5À&ÑIÔIØ*˜{¨1¨e¨e°B¸©m¬m¸UÀFÑKÔKØ&˜; q¨%¨%°°B©-¬-¸ÀÑGÔGØ(˜[¨¨E¨E°"°b©M¬M¸5À&ÑIÔIð5
ð 
ð 
ð: " ; ;¨q°%°%¸¸B±-´-ÐASÐASÑ#TÔ#Tð
ð; ð  €NðD ð h¨s¤mð ¸¸cÀ3¸h¼ð ð ð ñ „\ðð& ð0Ø˜3”-ð0Ø*/°°S°¬/ð0à	ˆc�3ˆhŒð0ð 0ð 0ð 0ð 0ñ „\ð0ðd ðR˜3ð R cð R¨e°H¸d°NÔ.Cð Rð Rð Rð Rð Rñ „\ðRð6 ð*!ð *!˜wð *!°sð *!ð *!ð *!ð *!ð *!ñ „\ð*!ð *!ð *!ð *!ð *!r’   r•   c                   óR  ‡ — e Zd Z	 	 	 ddededee         dee         ddf
ˆ fd„Zd	d
œdede	j
        fd„Zdee	j
                 fd„Z	 	 	 	 ddededededeeef         f
d„Zdededeeef         fd„Zdeeef         fd„Zddedefd„Zdeeef         dedeeef         fd„Zˆ xZS )Ú	SpePluginTNÚrequestÚcheck_filesizer  r.  rô   c           	      ó‚  •— t          ¦   «                              |¦  «         |j        j        t          j        k    rt          d¦  «        ‚|�t          j        dt          ¦  «         || _
        |�t          j        dt          ¦  «         || _        | j                             ¦   «         | _        	 |                      t           j        d¦  «        }|d         | _        t           j        |d                  | _        |d         |d	         f| _        |d
         | _        |rß|d         dk    r	|d         }n>| j                             dt0          j        ¦  «         | j                             ¦   «         }|t           j        z
  }|| j        d         | j        d         z  | j        j        z  z  }|| j        k    rGt          j        d| j        j        › d| j        › d|› d�¦  «         t=          || j        ¦  «        | _        | j                             t           j        ¦  «         dS # t>          $ r t          d¦  «        ‚w xY w)aá  Instantiate a new SPE file plugin object

        Parameters
        ----------
        request : Request
            A request object representing the resource to be operated on.
        check_filesize : bool
            If True, compute the number of frames from the filesize, compare it
            to the frame count in the file header, and raise a warning if the
            counts don't match. (Certain software may create files with
        char_encoding : str
            Deprecated. Exists for backwards compatibility; use ``char_encoding`` of
            ``metadata`` instead.
        sdt_meta : bool
            Deprecated. Exists for backwards compatibility; use ``sdt_control`` of
            ``metadata`` instead.

        zcannot write SPE filesNz{Passing `char_encoding` to the constructor is deprecated. Use `char_encoding` parameter of the `metadata()` method instead.ztPassing `sdt_meta` to the constructor is deprecated. Use `sdt_control` parameter of the `metadata()` method instead.r  r    r   r   r   r   rx   r   r   rw   zThe file header of z claims there are z  frames, but there are actually z frames.z)SPE plugin cannot read the provided file.) Úsuperr¹   r6   Úio_moder   Úwriter   r  r	  ÚDeprecationWarningÚ_char_encodingÚ	_sdt_metar7  Úget_fileÚ_fileÚ_parse_headerr   rƒ   Ú_file_header_verr�   Ú_dtypeÚ_shapeÚ_lenÚseekÚosÚSEEK_ENDÚtellr‡   ÚitemsizeÚfilenameÚminr  )	r¸   r7  r8  r  r.  r  Údata_endÚlinerÞ   s	           €r“   r¹   zSpePlugin.__init__³  sO  ø€ õ4 	‰Œ×Ò˜Ñ!Ô!Ð!ØŒ<Ô¥6¤<Ò/Ð/Ý%Ð&>Ñ?Ô?Ð?àÐ$ÝŒMðõ #ñ	ô ð ð ,ˆÔØÐÝŒMðõ #ñ	ô ð ð "ˆŒà”\×*Ò*Ñ,Ô,ˆŒ
ð	Sð ×%Ò%¥d¤j°(Ñ;Ô;ˆDØ$(Ð):Ô$;ˆDÔ!Ýœ+ d¨:Ô&6Ô7ˆDŒKØ œ<¨¨f¬Ð6ˆDŒKØ˜[Ô)ˆDŒIàð 5ð
 Ð)Ô*¨aÒ/Ð/Ø#Ð$7Ô8�H�Hà”J—O’O A¥r¤{Ñ3Ô3Ð3Ø#œzŸšÑ0Ô0�HØ¥$¤/Ñ1�Ø˜œ Qœ¨$¬+°a¬.Ñ8¸4¼;Ô;OÑOÑO�Ø˜4œ9Ò$Ð$Ý”MðU¨d¬lÔ.Cð Uð UØœ9ðUð UØFJðUð Uð Uñô ð õ !$ D¨$¬)Ñ 4Ô 4�D”IØŒJ�OŠO�DœOÑ,Ô,Ð,Ð,Ð,øÝð 	Sð 	Sð 	SÝ%Ð&QÑRÔRÐRð	Søøøs   Â1E1H$ È$H>.©ÚindexrQ  c                óf  — |t           u r@t          j        }| j        d         | j        d         z  | j        z  }| j        g| j        ¢R }n™|dk     rt          d|› d�¦  «        ‚|| j        k    rt          d|› d| j        › d�¦  «        ‚t          j        || j        d         z  | j        d         z  | j        j        z  z   }| j        d         | j        d         z  }| j        }| j         	                    |¦  «         t          j        | j        | j        |¬¦  «        }|                     |¦  «        S )a<  Read a frame or all frames from the file

        Parameters
        ----------
        index : int
            Select the index-th frame from the file. If index is `...`,
            select all frames and stack them along a new axis.

        Returns
        -------
        A Numpy array of pixel values.

        r   rw   zIndex `z` is smaller than 0.z5` exceeds the number of frames stored in this file (`z`).©r…   Úcount)ÚEllipsisr   r‡   rE  rF  Ú
IndexErrorrD  rK  rA  rG  r„   ÚfromfileÚreshape)r¸   rQ  Úread_offsetrT  Ú	out_shapeÚdatas         r“   ÚreadzSpePlugin.read  s9  € ð •HÐÐÝœ/ˆKØ”K ”N T¤[°¤^Ñ3°d´iÑ?ˆEØœÐ1 T¤[Ð1Ð1ˆIˆIØ�QŠYˆYÝÐB uÐBÐBÐBÑCÔCÐCØ�d”iÒÐÝØd˜%ÐdÐdÐVZÔV_ÐdÐdÐdñô ð õ
 ”Ø˜$œ+ aœ.Ñ(¨4¬;°q¬>Ñ9¸D¼KÔ<PÑPñQð ð ”K ”N T¤[°¤^Ñ3ˆEØœˆIàŒ
�Š˜Ñ$Ô$Ð$ÝŒ{˜4œ:¨T¬[ÀÐFÑFÔFˆØ�|Š|˜IÑ&Ô&Ð&r’   c                 óD   ‡ — ˆ fd„t          ‰ j        ¦  «        D ¦   «         S )zrIterate over the frames in the file

        Yields
        ------
        A Numpy array of pixel values.
        c              3   óD   •K  — | ]}‰                      |¬ ¦  «        V — ŒdS )rP  N)r\  )r#  Úir¸   s     €r“   r%  z!SpePlugin.iter.<locals>.<genexpr>0  s1   øè è € Ð=Ð= q�—	’	 �	Ñ"Ô"Ð=Ð=Ð=Ð=Ð=Ð=r’   )r  rF  )r¸   s   `r“   ÚiterzSpePlugin.iter(  s)   ø€ ð >Ð=Ð=Ð=­E°$´)Ñ,<Ô,<Ð=Ñ=Ô=Ð=r’   r  Úexclude_appliedÚsdt_controlc                 ó¤   — | j         dk     r2| j        �| j        }| j        �| j        }|                      ||¦  «        S |                      ¦   «         S )ua  SPE specific metadata.

        Parameters
        ----------
        index : int
            Ignored as SPE files only store global metadata.
        exclude_applied : bool
            Ignored. Exists for API compatibility.
        char_encoding : str
            The encoding to use when parsing strings.
        sdt_control : bool
            If `True`, decode special metadata written by the
            SDT-control software if present.

        Returns
        -------
        metadata : dict
            Key-value pairs of metadata.

        Notes
        -----
        SPE v3 stores metadata as XML, whereas SPE v2 uses a binary format.

        .. rubric:: Supported SPE v2 Metadata fields

        ROIs : list of dict
            Regions of interest used for recording images. Each dict has the
            "top_left" key containing x and y coordinates of the top left corner,
            the "bottom_right" key with x and y coordinates of the bottom right
            corner, and the "bin" key with number of binned pixels in x and y
            directions.
        comments : list of str
            The SPE format allows for 5 comment strings of 80 characters each.
        controller_version : int
            Hardware version
        logic_output : int
            Definition of output BNC
        amp_hi_cap_low_noise : int
            Amp switching mode
        mode : int
            Timing mode
        exp_sec : float
            Alternative exposure in seconds
        date : str
            Date string
        detector_temp : float
            Detector temperature
        detector_type : int
            CCD / diode array type
        st_diode : int
            Trigger diode
        delay_time : float
            Used with async mode
        shutter_control : int
            Normal, disabled open, or disabled closed
        absorb_live : bool
            on / off
        absorb_mode : int
            Reference strip or file
        can_do_virtual_chip : bool
            True or False whether chip can do virtual chip
        threshold_min_live : bool
            on / off
        threshold_min_val : float
            Threshold minimum value
        threshold_max_live : bool
            on / off
        threshold_max_val : float
            Threshold maximum value
        time_local : str
            Experiment local time
        time_utc : str
            Experiment UTC time
        adc_offset : int
            ADC offset
        adc_rate : int
            ADC rate
        adc_type : int
            ADC type
        adc_resolution : int
            ADC resolution
        adc_bit_adjust : int
            ADC bit adjust
        gain : int
            gain
        sw_version : str
            Version of software which created this file
        spare_4 : bytes
            Reserved space
        readout_time : float
            Experiment readout time
        type : str
            Controller type
        clockspeed_us : float
            Vertical clock speed in microseconds
        readout_mode : ["full frame", "frame transfer", "kinetics", ""]
            Readout mode. Empty string means that this was not set by the
            Software.
        window_size : int
            Window size for Kinetics mode
        file_header_ver : float
            File header version
        chip_size : [int, int]
            x and y dimensions of the camera chip
        virt_chip_size : [int, int]
            Virtual chip x and y dimensions
        pre_pixels : [int, int]
            Pre pixels in x and y dimensions
        post_pixels : [int, int],
            Post pixels in x and y dimensions
        geometric : list of {"rotate", "reverse", "flip"}
            Geometric operations
        sdt_major_version : int
            (only for files created by SDT-control)
            Major version of SDT-control software
        sdt_minor_version : int
            (only for files created by SDT-control)
            Minor version of SDT-control software
        sdt_controller_name : str
            (only for files created by SDT-control)
            Controller name
        exposure_time : float
            (only for files created by SDT-control)
            Exposure time in seconds
        color_code : str
            (only for files created by SDT-control)
            Color channels used
        detection_channels : int
            (only for files created by SDT-control)
            Number of channels
        background_subtraction : bool
            (only for files created by SDT-control)
            Whether background subtraction war turned on
        em_active : bool
            (only for files created by SDT-control)
            Whether EM was turned on
        em_gain : int
            (only for files created by SDT-control)
            EM gain
        modulation_active : bool
            (only for files created by SDT-control)
            Whether laser modulation (â€œattenuateâ€�) was turned on
        pixel_size : float
            (only for files created by SDT-control)
            Camera pixel size
        sequence_type : str
            (only for files created by SDT-control)
            Type of sequnce (standard, TOCCSL, arbitrary, â€¦)
        grid : float
            (only for files created by SDT-control)
            Sequence time unit (â€œgrid sizeâ€�) in seconds
        n_macro : int
            (only for files created by SDT-control)
            Number of macro loops
        delay_macro : float
            (only for files created by SDT-control)
            Time between macro loops in seconds
        n_mini : int
            (only for files created by SDT-control)
            Number of mini loops
        delay_mini : float
            (only for files created by SDT-control)
            Time between mini loops in seconds
        n_micro : int (only for files created by SDT-control)
            Number of micro loops
        delay_micro : float (only for files created by SDT-control)
            Time between micro loops in seconds
        n_subpics : int
            (only for files created by SDT-control)
            Number of sub-pictures
        delay_shutter : float
            (only for files created by SDT-control)
            Camera shutter delay in seconds
        delay_prebleach : float
            (only for files created by SDT-control)
            Pre-bleach delay in seconds
        bleach_time : float
            (only for files created by SDT-control)
            Bleaching time in seconds
        recovery_time : float
            (only for files created by SDT-control)
            Recovery time in seconds
        comment : str
            (only for files created by SDT-control)
            User-entered comment. This replaces the "comments" field.
        datetime : datetime.datetime
            (only for files created by SDT-control)
            Combines the "date" and "time_local" keys. The latter two plus
            "time_utc" are removed.
        modulation_script : str
            (only for files created by SDT-control)
            Laser modulation script. Replaces the "spare_4" key.
        bleach_piezo_active : bool
            (only for files created by SDT-control)
            Whether piezo for bleaching was enabled
        rx   )rC  r>  r?  Ú_metadata_pre_v3Ú_metadata_post_v3)r¸   rQ  ra  r  rb  s        r“   r†   zSpePlugin.metadata2  s[   € ðX Ô  1Ò$Ð$ØÔ"Ð.Ø $Ô 3�ØŒ~Ð)Ø"œn�Ø×(Ò(¨¸ÑDÔDÐDØ×%Ò%Ñ'Ô'Ð'r’   c                 óø  ‡	— |                       t          j        |¦  «        Š	‰	                     dd¦  «        }|dk     rdn|}t	          ‰	d         d|…         ¦  «        ‰	d<   ˆ	fd„dD ¦   «         ‰	d<   ˆ	fd„d	D ¦   «         ‰	d
<   ˆ	fd„dD ¦   «         ‰	d<   ˆ	fd„dD ¦   «         ‰	d<   d„ ‰	d         D ¦   «         ‰	d<   g }‰	                     dd¦  «        }|dz  r|                     d¦  «         |dz  r|                     d¦  «         |dz  r|                     d¦  «         |‰	d<   ‰	d         }d|cxk    rt          t          j        ¦  «        k    rn nt          j        |dz
           ‰	d<   nd‰	d<   ‰	d         }d|cxk    rt          t          j        ¦  «        k    rn nt          j        |dz
           ‰	d<   nd‰	d<   dD ]}t          ‰	|         ¦  «        ‰	|<   Œ|rt                               ‰	|¦  «         ‰	S )aZ  Extract metadata from SPE v2 files

        Parameters
        ----------
        char_encoding
            String character encoding
        sdt_control
            If `True`, try to decode special metadata written by the
            SDT-control software.

        Returns
        -------
        dict mapping metadata names to values.

        r!   Nrw   r"   c                 ó<   •— g | ]}‰                      |d ¦  «        ‘ŒS r·   ©r)  ©r#  ÚkÚms     €r“   ú
<listcomp>z.SpePlugin._metadata_pre_v3.<locals>.<listcomp>  s%   ø€ ÐIÐIÐI¨Q˜!Ÿ%š%  4™.œ.ÐIÐIÐIr’   )r*   r,   Ú	chip_sizec                 ó<   •— g | ]}‰                      |d ¦  «        ‘ŒS r·   rh  ri  s     €r“   rl  z.SpePlugin._metadata_pre_v3.<locals>.<listcomp>  s%   ø€ ÐRÐRÐR°!˜qŸušu Q¨™~œ~ÐRÐRÐRr’   )r.   r0   Úvirt_chip_sizec                 ó<   •— g | ]}‰                      |d ¦  «        ‘ŒS r·   rh  ri  s     €r“   rl  z.SpePlugin._metadata_pre_v3.<locals>.<listcomp>   s%   ø€ ÐPÐPÐP¨a˜1Ÿ5š5  D™>œ>ÐPÐPÐPr’   )rn   rp   Ú
pre_pixelsc                 ó<   •— g | ]}‰                      |d ¦  «        ‘ŒS r·   rh  ri  s     €r“   rl  z.SpePlugin._metadata_pre_v3.<locals>.<listcomp>!  s%   ø€ ÐSÐSÐS¨q˜AŸEšE ! T™NœNÐSÐSÐSr’   )ro   rq   Úpost_pixelsc                 ó,   — g | ]}t          |¦  «        ‘ŒS r‘   )r¼   r"  s     r“   rl  z.SpePlugin._metadata_pre_v3.<locals>.<listcomp>$  s   € Ð7Ð7Ð7 A�˜Q™œÐ7Ð7Ð7r’   rd   rg   r   Úrotater   Úreverser5   Úfliprs   ru   )rE   rI   rK   rO   )rB  r   r†   r)  Úroi_array_to_dictÚappendÚlenrŽ   r�   Úboolr•   r1  )
r¸   r  rb  ÚnrÚgÚfÚtÚrrj  rk  s
            @r“   rd  zSpePlugin._metadata_pre_v3  st  ø€ ð" ×Ò�tœ}¨mÑ<Ô<ˆà�UŠU�8˜TÑ"Ô"ˆØ�q’&�&ˆQˆQ˜bˆÝ% a¨¤i°°°¤nÑ5Ô5ˆˆ&‰	ð JÐIÐIÐIÐ2HÐIÑIÔIˆˆ+‰ØRÐRÐRÐRÐ7QÐRÑRÔRˆÐ
ÑØPÐPÐPÐPÐ3OÐPÑPÔPˆˆ,‰ØSÐSÐSÐSÐ4RÐSÑSÔSˆˆ-Ñð 8Ð7¨¨:¬Ð7Ñ7Ô7ˆˆ*‰ð ˆØ�EŠE�+˜qÑ!Ô!ˆØˆq‰5ð 	Ø�HŠH�XÑÔÐØˆq‰5ð 	 Ø�HŠH�YÑÔÐØˆq‰5ð 	Ø�HŠH�VÑÔÐØˆˆ+‰ð ˆfŒIˆØ�Ð*Ð*Ò*Ð*•S�Ô)Ñ*Ô*Ò*Ð*Ð*Ð*Ð*ÝÔ(¨¨Q©Ô/ˆAˆf‰IˆIàˆAˆf‰IØˆnÔˆØ�Ð,Ð,Ò,Ð,•S�Ô+Ñ,Ô,Ò,Ð,Ð,Ð,Ð,Ý $Ô 2°1°q±5Ô 9ˆAˆnÑÐà $ˆAˆnÑð
ð 	ð 	ˆAõ ˜˜!œ‘:”:ˆAˆa‰DˆDð ð 	>Ý×+Ò+¨A¨}Ñ=Ô=Ð=àˆr’   c                 ó¼   — |                       t          j        d¦  «        }| j                             |d         ¦  «         | j                             ¦   «         }d|iS )z‘Extract XML metadata from SPE v3 files

        Returns
        -------
        dict with key `"__xml"`, whose value is the XML metadata
        r  r   Ú__xml)rB  r   rƒ   rA  rG  r\  )r¸   r  Úxmls      r“   re  zSpePlugin._metadata_post_v3L  sP   € ð ×!Ò!¥$¤*¨hÑ7Ô7ˆØŒ
�Š˜Ð0Ô1Ñ2Ô2Ð2ØŒj�oŠoÑÔˆØ˜ˆ~Ðr’   c                 ó¢   — |t           u r+t          | j        g| j        ¢R | j        | j        d¬¦  «        S t          | j        | j        d¬¦  «        S )aÞ  Standardized ndimage metadata.

        Parameters
        ----------
        index : int
            If the index is an integer, select the index-th frame and return
            its properties. If index is an Ellipsis (...), return the
            properties of all frames in the file stacked along a new batch
            dimension.

        Returns
        -------
        properties : ImageProperties
            A dataclass filled with standardized image metadata.
        T)Úshaper…   Ún_imagesÚis_batchF)r…  r…   r‡  )rU  r   rF  rE  rD  )r¸   rQ  s     r“   Ú
propertieszSpePlugin.propertiesY  s`   € ð" •HÐÐÝ"Ø”yÐ/ 4¤;Ð/Ð/Ø”kØœØð	ñ ô ð õ  T¤[¸¼ÈeÐTÑTÔTÐTr’   r  c                 óP  ‡— i }t          j        ˆfd„¦  «        }|                     ¦   «         D ]ö\  }}| j                             |d         ¦  «         t          |¦  «        dk     rdn|d         }t          j        | j        |d         |¬¦  «        }|j        j        dk    rC|t          j
        vr5	  ||¦  «        }n(# t          $ r t          j        d|› d	�¦  «         Y nw xY w	 |                     ¦   «         }n$# t          $ r t          j        |¦  «        }Y nw xY w|||<   Œ÷|S )
a®  Get information from SPE file header

        Parameters
        ----------
        spec
            Maps header entry name to its location, data type description and
            optionally number of entries. See :py:attr:`Spec.basic` and
            :py:attr:`Spec.metadata`.
        char_encoding
            String character encoding

        Returns
        -------
        Dict mapping header entry name to its value
        c                 ó.   •— |                       ‰¦  «        S r·   )r+  )rÍ   r  s    €r“   rÎ   z)SpePlugin._parse_header.<locals>.<lambda>ˆ  s   ø€ ¨¯ª°Ñ(?Ô(?€ r’   r   rx   rw   r   rS  ÚSzFailed to decode "z3" metadata string. Check `char_encoding` parameter.)r„   Ú	vectorizer  rA  rG  rz  rW  r…   Úkindr   r�   r  r  r	  Úitemrû   Úsqueeze)	r¸   r  r  Úretr+  r  ÚspÚcntr  s	     `      r“   rB  zSpePlugin._parse_headers  sa  ø€ ð& ˆå”Ð?Ð?Ð?Ð?Ñ@Ô@ˆàŸ
š
™œð 	ð 	‰HˆD�"ØŒJ�OŠO˜B˜qœEÑ"Ô"Ð"Ý˜2‘w”w ’{�{�!�!¨¨1¬ˆCÝ”˜DœJ¨b°¬e¸3Ð?Ñ?Ô?ˆAØŒwŒ|˜sÒ"Ð" tµ4´>Ð'AÐ'AðØ˜˜q™	œ	�A�AøÝ ð ð ð Ý”MðC¨Tð Cð Cð Cñô ð ð ð ðøøøð"ð —F’F‘H”H��øÝð "ð "ð "Ý”J˜q‘M”M���ð"øøøàˆC�‰IˆIØˆ
s$   Â2B>Â>"C#Ã"C#Ã'C<Ã<DÄD)TNN).Tr  T).)r   r€   r�   r   r{  r
   r¼   r¹   rº   r„   Úndarrayr\  r   r`  r   r   r†   rd  re  r   rˆ  r	   r   rB  r3  r4  s   @r“   r6  r6  ²  s&  ø€ € € € € ð  $Ø'+Ø#'ðNSð NSàðNSð ðNSð   ”}ð	NSð
 ˜4”.ðNSð 
ðNSð NSð NSð NSð NSð NSð` $'ð #'ð #'ð #'˜Sð #'¨2¬:ð #'ð #'ð #'ð #'ðJ>�h˜rœzÔ*ð >ð >ð >ð >ð Ø $Ø%Ø ðR(ð R(àðR(ð ðR(ð ð	R(ð
 ðR(ð 
ˆc�3ˆhŒðR(ð R(ð R(ð R(ðhD¨cð DÀð DÈÈcÐSVÈhÌð Dð Dð Dð DðL 4¨¨S¨¤>ð ð ð ð ðUð U ð U¨oð Uð Uð Uð Uð4,Ø˜C ˜JÔ'ð,Ø8;ð,à	ˆc�3ˆhŒð,ð ,ð ,ð ,ð ,ð ,ð ,ð ,r’   r6  Úarô   c                 ó  — g }| g d¢         } | D ]x\  }}}}}}t          |¦  «        t          |¦  «        gt          |¦  «        t          |¦  «        gt          |¦  «        t          |¦  «        gdœ}|                     |¦  «         Œy|S )a\  Convert the `ROIs` structured arrays to :py:class:`dict`

    Parameters
    ----------
    a
        Structured array containing ROI data

    Returns
    -------
    One dict per ROI. Keys are "top_left", "bottom_right", and "bin",
    values are tuples whose first element is the x axis value and the
    second element is the y axis value.
    )r#   r&   r$   r'   r%   r(   )Útop_leftÚbottom_rightÚbin)rº   ry  )	r”  Ú	dict_listÚsxÚsyÚexÚeyÚgxÚgyÚroi_dicts	            r“   rx  rx  ¢  s�   € ð €IØ	Ð
BÐ
BÐ
BÔC€AØ"#ð #ð #ÑˆˆB��B˜˜Bå˜R™œ¥# b¡'¤'Ð*Ý  ™WœW¥c¨"¡g¤gÐ.Ý˜‘G”G�S ™WœWÐ%ð
ð 
ˆð
 	×Ò˜Ñ"Ô"Ð"Ð"ØÐr’   )#r‚   r   ÚloggingrH  Útypingr   r   r   r   r   r	   r
   r   r   r   r  Únumpyr„   Úcore.requestr   r   r   Úcore.v3_plugin_apir   r   Ú	getLoggerr   r  r   r•   r6  r“  r¼   rº   rx  r‘   r’   r“   ú<module>r§     sâ  ðð!ð !ðF Ð Ð Ð Ð Ð Ø €€€Ø 	€	€	€	ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð €€€à Ð Ð Ð à ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø :Ð :Ð :Ð :Ð :Ð :Ð :Ð :à	ˆÔ	˜8Ñ	$Ô	$€ð~ð ~ð ~ð ~ð ~ñ ~ô ~ð ~ðBn!ð n!ð n!ð n!ð n!ñ n!ô n!ð n!ðbmð mð mð mð m�ñ mô mð mð`˜œð ¨¨T°#°t¸C´y°.Ô-AÔ(Bð ð ð ð ð ð r’   