a
    ߙfb%                     @   sb   d dl Zd dlmZ d dlmZ d dlmZ dgZddgiZ	dZ
dZed	 Zddd
dddZdS )    N)datetime)Timeunitstime_supportZ
matplotlib)r   Z
datetime64)fitsisoisotyday)	byear_str	jyear_strTscaleformatsimplifyc                    sz   ddl m ddlm}m} ddlmm G fddd|G dd d| G  fd	d
d
j}|| ||dS )a  
    Enable support for plotting `astropy.time.Time` instances in
    matplotlib.

    May be (optionally) used with a ``with`` statement.

      >>> import matplotlib.pyplot as plt
      >>> from astropy import units as u
      >>> from astropy import visualization
      >>> with visualization.time_support():  # doctest: +IGNORE_OUTPUT
      ...     plt.figure()
      ...     plt.plot(Time(['2016-03-22T12:30:31', '2016-03-22T12:30:38', '2016-03-22T12:34:40']))
      ...     plt.draw()

    Parameters
    ----------
    scale : str, optional
        The time scale to use for the times on the axis. If not specified,
        the scale of the first Time object passed to Matplotlib is used.
    format : str, optional
        The time format to use for the times on the axis. If not specified,
        the format of the first Time object passed to Matplotlib is used.
    simplify : bool, optional
        If possible, simplify labels, e.g. by removing 00:00:00.000 times from
        ISO strings if all labels fall on that time.
    r   N)MaxNLocatorScalarFormatterselect_step_hourselect_step_scalarc                       s4   e Zd Z fddZ fddZdd Z  ZS )z(time_support.<locals>.AstropyTimeLocatorc                    s$   d|d< t  j|i | || _d S )N   Znbins)super__init__
_converterselfZ	converterargskwargs	__class__ 9lib/python3.9/site-packages/astropy/visualization/time.pyr   9   s    z1time_support.<locals>.AstropyTimeLocator.__init__c                    s  | j jtv r|| }| j jdkr*|dks4|dkrjt|| j jddj}t|| j jddj}|j}|j}||d krttd|| d }|||  }g }	t	||d |D ]}
|	
t|
ddd qn|j}|jd	||   }ttd|| d }|td||  }g }	t	||d |D ]2}|	
t||d d	  |d d	 d dd q"t|	| j jd
j}n|dkr| jg dd t ||}n`|| d d tj> }|tjd }t|| }t|| }tj||d tjd| }nt ||}|||k||k@  }|S )Nr
      in  mjd)r   r         )yearmonthZday   )r   )r$         
   )Zsteps   )Zdtype)r   r   YMDHMS_FORMATSr   r   r   r&   intmaxrangeappendr'   r#   Z
set_paramsr   tick_valuesuZ	hourangleZto_valuenpZceilZfloorZarangeZint64)r   vminvmaxZvrangeZtminZtmaxZyminZymaxZysteptimesr&   ZmminZmmaxZmstepr'   valuesZdvZiminZimax)r   r   r   r    r!   r2   >   sF    	
z4time_support.<locals>.AstropyTimeLocator.tick_valuesc                 S   s   | j  \}}| ||S N)axisZget_view_intervalr2   )r   r5   r6   r    r    r!   __call__   s    z1time_support.<locals>.AstropyTimeLocator.__call__)__name__
__module____qualname__r   r2   r;   __classcell__r    r   r   r!   AstropyTimeLocator4   s   Tr@   c                       s6   e Zd Z fddZd fdd	Z fddZ  ZS )	z*time_support.<locals>.AstropyTimeFormatterc                    s0   t  j|i | || _| d | d d S )NF)r   r   r   Zset_useOffsetZset_scientificr   r   r    r!   r      s    
z3time_support.<locals>.AstropyTimeFormatter.__init__Nc                    s0   | j jtv r| |gd S t j||dS d S )Nr   )pos)r   r   STR_FORMATSformat_ticksr   r;   )r   valuerA   r   r    r!   r;      s    z3time_support.<locals>.AstropyTimeFormatter.__call__c                    s  t |dkrg S | jjtv rt|d| jjd}t|| jj}| jjr| jjdv rtdd |D r| jjdkrrdnd	  fd
d|D }n,| jjdkrtdd |D rdd |D }|S | jjdkrt|d| jjdj	S | jjdk rt|d| jjdj
S t |S d S )Nr   r#   )r   r   )r   r   r	   c                 S   s   g | ]}| d qS )z00:00:00.000endswith.0xr    r    r!   
<listcomp>       zKtime_support.<locals>.AstropyTimeFormatter.format_ticks.<locals>.<listcomp>r    Tc                    s   g | ]}|  d  qS )r   splitrG   rN   r    r!   rJ      rK   r
   c                 S   s   g | ]}| d qS )z:001:00:00:00.000rE   rG   r    r    r!   rJ      rK   c                 S   s   g | ]}| d dd qS ):r$   r   rN   rG   r    r    r!   rJ      rK   r   byearr   jyear)lenr   r   r-   r   r   getattrr   allr   r   r   rC   )r   r8   r7   Z	formattedr   rN   r!   rC      s&    z7time_support.<locals>.AstropyTimeFormatter.format_ticks)N)r<   r=   r>   r   r;   rC   r?   r    r    r   r!   AstropyTimeFormatter   s   rV   c                       sn   e Zd Zd fdd	Zedd Zejdd Zdd Zfd	d
Zdd Z	dd Z
fddZ  ZS )z&time_support.<locals>.MplTimeConverterNc                    s8   t    || _|| _|| _jt| _| jt< d S r9   )	r   r   r   r   r   registrygetr   _original_converter)r   r   r   r   )r   r   r    r!   r      s    
z/time_support.<locals>.MplTimeConverter.__init__c                 S   s   | j S r9   )_formatr   r    r    r!   r      s    z-time_support.<locals>.MplTimeConverter.formatc                 S   s    |t v rtd| || _d S )Nz%time_support does not support format=)UNSUPPORTED_FORMATS
ValueErrorrZ   )r   rD   r    r    r!   r      s    c                 S   s   | S r9   r    r[   r    r    r!   	__enter__   s    z0time_support.<locals>.MplTimeConverter.__enter__c                    s$   | j d u r jt= n| j  jt< d S r9   )rY   rW   r   )r   typerD   tbr   r    r!   __exit__   s    

z/time_support.<locals>.MplTimeConverter.__exit__c                 S   s:   t |tr|d }| jd u r$|j| _| jd u r6|j| _dS )Nr   Zastropy_time)
isinstancetupler   r   )r   rI   r:   r    r    r!   default_units   s    


z4time_support.<locals>.MplTimeConverter.default_unitsc                 S   sL   t || j}| jtv r|jS | jdkr,|jS | jdkr<|jS t || jS dS )zD
            Convert a Time value to a scalar or array.
            r   r   N)rT   r   r   r-   r#   rQ   rR   )r   rD   unitr:   Zscaledr    r    r!   convert   s    


z.time_support.<locals>.MplTimeConverter.convertc                    s*   | } | }j ||d| j ddS )zN
            Return major and minor tick locators and formatters.
            zTime ())majfmtmajlocZlabel)ZAxisInfor   )r   re   r:   ri   rh   rV   r@   r   r    r!   axisinfo   s    z/time_support.<locals>.MplTimeConverter.axisinfo)NNN)r<   r=   r>   r   propertyr   setterr^   ra   rd   rf   rk   r?   r    rj   r   r!   MplTimeConverter   s   

	rn   r   )	Zmatplotlib.unitsr   Zmatplotlib.tickerr   r   Z#astropy.visualization.wcsaxes.utilsr   r   ZConversionInterface)r   r   r   r   r   rn   r    )rV   r@   r   r   r   r!   r      s    b%D)Znumpyr4   r   Zastropy.timer   Zastropyr   r3   __all__Z__doctest_requires__r\   r-   rB   r   r    r    r    r!   <module>   s   
