o
    g'                     @   s   d dl Zd dlmZmZ d dlmZmZ ddl	m
Z
 G dd dZG dd	 d	ZG d
d dejZG dd dZG dd dZG dd dZdS )    N)_apiticker)Bbox	Transform   )clip_line_to_rectc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ExtremeFinderSimplezU
    A helper class to figure out the range of grid lines that need to be drawn.
    c                 C   s   || _ || _dS )zy
        Parameters
        ----------
        nx, ny : int
            The number of samples in each direction.
        Nnxny)selfr
   r    r   a/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/mpl_toolkits/axisartist/grid_finder.py__init__   s   
zExtremeFinderSimple.__init__c           
      C   sb   t t ||| jt ||| j\}}|t |t |\}}	| | | |	 |	 S )ai  
        Compute an approximation of the bounding box obtained by applying
        *transform_xy* to the box delimited by ``(x1, y1, x2, y2)``.

        The intended use is to have ``(x1, y1, x2, y2)`` in axes coordinates,
        and have *transform_xy* be the transform from axes coordinates to data
        coordinates; this method then returns the range of data coordinates
        that span the actual axes.

        The computation is done by sampling ``nx * ny`` equispaced points in
        the ``(x1, y1, x2, y2)`` box and finding the resulting points with
        extremal coordinates; then adding some padding to take into account the
        finite sampling.

        As each sampling step covers a relative range of *1/nx* or *1/ny*,
        the padding is computed by expanding the span covered by the extremal
        coordinates by these fractions.
        )	npmeshgridlinspacer
   r   ravel_add_padminmax)
r   transform_xyx1y1x2y2xyxtytr   r   r   __call__   s
    zExtremeFinderSimple.__call__c                 C   s8   || | j  }|| | j }|| || || || fS )z,Perform the padding mentioned in `__call__`.r	   )r   x_minx_maxy_miny_maxdxdyr   r   r   r   /   s   zExtremeFinderSimple._add_padN)__name__
__module____qualname____doc__r   r    r   r   r   r   r   r      s
    
r   c                   @   sX   e Zd Z					d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 )
GridFinderNc                 C   sv   |du r	t dd}|du rt }|du rt }|du rt }|du r%t }|| _|| _|| _|| _|| _| | dS )a  
        transform : transform from the image coordinate (which will be
        the transData of the axes to the world coordinate.

        or transform = (transform_xy, inv_transform_xy)

        locator1, locator2 : grid locator for 1st and 2nd axis.
        N   )	r   MaxNLocatorFormatterPrettyPrintextreme_findergrid_locator1grid_locator2tick_formatter1tick_formatter2update_transform)r   	transformr/   r0   r1   r2   r3   r   r   r   r   7   s    
zGridFinder.__init__c              
   C   sX  |  | j||||}|\}}}}	| ||\}
}}| ||	\}}}|
d| | }|d| | }| ||||||	\}}|| d }|| d }t|| || || || }|||| |||
|| ||||d}i  }|d d< dD ]}|d d | }| |||||< qxi  }|d d< dD ]}|d d | }| 	|||||< q|S )	z
        lon_values, lat_values : list of grid values. if integer is given,
                           rough number of grids in each direction.
        Ng|=)extremes	lon_lines	lat_lineslonlatr9   tick_labelsleftbottomrighttoptick_levelsr:   )
r/   inv_transform_xyr0   r1   _get_raw_grid_linesr   from_extents_clip_grid_lines_and_find_ticksr2   r3   )r   r   r   r   r   r6   lon_minlon_maxlat_minlat_maxlon_levslon_n
lon_factorlat_levslat_n
lat_factor
lon_values
lat_valuesr7   r8   ddxddybb	grid_info
tck_labels	directionlevsr   r   r   get_grid_infoW   sJ    


zGridFinder.get_grid_infoc           	         sL   t ||dt ||d  fdd|D }fdd|D }||fS )Nd   c                    s    g | ]} t | qS r   r   r   	full_like).0r9   )lats_ir   r   r   
<listcomp>       z2GridFinder._get_raw_grid_lines.<locals>.<listcomp>c              	      s    g | ]}  t |qS r   r[   )r]   r:   )lons_ir   r   r   r_      r`   )r   r   )	r   rP   rQ   rF   rG   rH   rI   r7   r8   r   )r^   ra   r   r   rC      s   zGridFinder._get_raw_grid_linesc              	   C   s   g g t g g g g dt g g g g dg d}|d }|d }t|||D ]?\\}}	}
}t||	|\}}|s4q"|d |
 |d | t|g dD ]\}}|D ]}|| | || | qOqIq"|S )Nr<   )valueslevelsrA   	tick_locslinesrA   rd   rc   re   )dictzipr   append)r   re   rb   rX   rT   gi
tck_levelstck_locslxlyvlevxytckstckrW   tr   r   r   rE      s.   z*GridFinder._clip_grid_lines_and_find_ticksc                 C   s(   t |tst|dkrtd|| _d S )N   zF'aux_trans' must be either a Transform instance or a pair of callables)
isinstancer   len	TypeError_aux_transform)r   	aux_transr   r   r   r4      s   
zGridFinder.update_transformc                 C   s8   | j }t|tr|t||gjS |\}}|||S N)rx   ru   r   r5   r   column_stackTr   r   r   aux_trfr   rB   r   r   r   r      s
   

zGridFinder.transform_xyc                 C   s<   | j }t|tr| t||gjS |\}}|||S rz   )rx   ru   r   invertedr5   r   r{   r|   r}   r   r   r   rB      s
   

zGridFinder.inv_transform_xyc                 K   s2   |D ]}|dv rt | |||  qtd| d S )N)r/   r0   r1   r2   r3   zUnknown update property '%s')setattr
ValueError)r   kwkr   r   r   update   s
   zGridFinder.update)NNNNN)r'   r(   r)   r   rY   rC   rE   r4   r   rB   r   r   r   r   r   r+   6   s    
 3r+   c                       sF   e Zd Z					d fdd	Z fddZed	d
d Z  ZS )r-   
   NTFc                    s(   t  j|||||d |   d| _d S )N)stepsinteger	symmetricpruner   )superr   create_dummy_axis_factor)r   nbinsr   trimr   r   r   	__class__r   r   r      s
   
zMaxNLocator.__init__c                    s8   |  || j || j  t  }t|t|| jfS rz   )
set_boundsr   r   r    r   arrayrv   r   v1v2locsr   r   r   r       s   
zMaxNLocator.__call__3.3c                 C   
   || _ d S rz   r   r   fr   r   r   
set_factor      
zMaxNLocator.set_factor)r   NTFFN)	r'   r(   r)   r   r    r   
deprecatedr   __classcell__r   r   r   r   r-      s    r-   c                   @   s.   e Zd Zdd Zdd Zeddd ZdS )	FixedLocatorc                 C   s   || _ d| _d S )Nr   )_locsr   )r   r   r   r   r   r      s   
zFixedLocator.__init__c                    sH   t  | j | j g\ t fdd| jD }|t|| jfS )Nc                    s(   g | ]} |  krkrn n|qS r   r   )r]   lr   r   r   r   r_      s   ( z)FixedLocator.__call__.<locals>.<listcomp>)sortedr   r   r   r   rv   r   r   r   r   r       s   zFixedLocator.__call__r   c                 C   r   rz   r   r   r   r   r   r      r   zFixedLocator.set_factorN)r'   r(   r)   r   r    r   r   r   r   r   r   r   r      s
    r   c                   @   s   e Zd ZdddZdd ZdS )r.   Tc                 C   s   t j|dd| _| j  d S )NF)useMathText	useOffset)mtickerScalarFormatter_fmtr   )r   r   r   r   r   r      s   zFormatterPrettyPrint.__init__c                 C   s   | j |S rz   )r   format_ticks)r   rW   factorrb   r   r   r   r      s   zFormatterPrettyPrint.__call__N)T)r'   r(   r)   r   r    r   r   r   r   r.      s    
r.   c                       s&   e Zd Zd fdd	Zdd Z  ZS )DictFormatterNc                    s   t    || _|| _dS )zq
        format_dict : dictionary for format strings to be used.
        formatter : fall-back formatter
        N)r   r   _format_dict_fallback_formatter)r   format_dict	formatterr   r   r   r     s   

zDictFormatter.__init__c                    s<    j r  |||}ndgt| } fddt||D S )zG
        factor is ignored if value is found in the dictionary
         c                    s   g | ]\}} j ||qS r   )r   get)r]   r   rn   r   r   r   r_     s    z*DictFormatter.__call__.<locals>.<listcomp>)r   rv   rg   )r   rW   r   rb   fallback_stringsr   r   r   r      s   
zDictFormatter.__call__rz   )r'   r(   r)   r   r    r   r   r   r   r   r     s    	r   )numpyr   
matplotlibr   r   r   matplotlib.transformsr   r   	clip_pathr   r   r+   r-   r   r.   r   r   r   r   r   <module>   s    . 
