
    ^j4                         d 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
mZ ddlmZmZmZ ddlmZ ddlmZ d	 Z G d
 de      Z G d de      Z G d de      Zy)z/
An experimental support for curvilinear grid.
    N)Path)Affine2DBboxIdentityTransform   )_FixedAxisArtistHelperBase_FloatingAxisArtistHelperBaseGridHelperBase)
AxisArtist)
GridFinderc                 t    t        j                  t        j                        t        j                                      }t        j                  t              j
                  dz  fd} ||      \  }} ||      \  }	}
fd} | fd|||	      } | fd|	|
|      }|||fS )a  
    Parameters
    ----------
    func : callable
        A function that transforms the coordinates of a point (x, y) to a new coordinate
        system (u, v), and which can also take x and y as arrays of shape *shape* and
        returns (u, v) as a ``(2, shape)`` array.
    xs, ys : array-likes
        Points where *func* and its derivatives will be evaluated.
    xlim, ylim : pairs of floats
        (min, max) beyond which *func* should not be evaluated.

    Returns
    -------
    val
        Value of *func* at each point of ``(xs, ys)``.
    thetas_dx
        Angles (in radians) defined by the (u, v) components of the numerically
        differentiated df/dx vector, at each point of ``(xs, ys)``.  If needed, the
        differentiation step size is increased until at least one component of df/dx
        is nonzero, under the constraint of not going out of the *xlims*, *ylims*
        bounds.  If the gridline at a point is actually null (and the angle is thus not
        well defined), the derivatives are evaluated after taking a small step along y;
        this ensures e.g. that the tick at r=0 on a radial axis of a polar plot is
        parallel with the ticks at r!=0.
    thetas_dy
        Like *thetas_dx*, but for df/dy.
    g      ?c                     t        |      \  }}| |z
  }|| z
  }t        j                  ||      }t        j                  ||k\  dd      t        j                  |      z  }||fS )Nr   )sortednpmaximumwhereminimum)	valslimlohidlodhieps_maxepseps0s	           z/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/mpl_toolkits/axisartist/grid_helper_curvelinear.pycalc_epsz&_value_and_jac_angle.<locals>.calc_eps7   sb    BRi4i**S#&hhscz1b)BJJtW,EEG|    c                 \   t        j                  t         j                        }t        j                  d      }|}t        d|g      D ]  \  }}|j	                         st        |      |k  j	                         s4|dk(  r|dkD  j	                         rMt        j                  ||      } | ||       | d|      z
  |z  \  }	}
||	dk7  |
dk7  z  z  }t        j                  |
|	      |   ||<   || z  }|dz  }|j	                         st        |      |k  j	                         r |S )NTr   r      )r   fullnan	enumerateanyabsr   arctan2)dfuncpseps_p0r   eps_q	thetas_dpmissingeps_pitdf_xdf_ygoodshapes               r   calc_thetasz)_value_and_jac_angle.<locals>.calc_thetasB   s   GGE266*	''%&"Au:. 		IB++-SZ'%9$>$>$@7	0

5'2#E51E!UOCuL
d419";<"$**T4"8">	$D5 
 ++-SZ'%9$>$>$@		 r    c                 "     | z   |z         S N r/   r,   funcxsyss     r   <lambda>z&_value_and_jac_angle.<locals>.<lambda>R       b5j"u*1M r    c                 "     |z   | z         S r7   r8   r9   s     r   r=   z&_value_and_jac_angle.<locals>.<lambda>T   r>   r    )r   broadcast_shapesr4   finfofloatr   )r:   r;   r<   xlimylimvalr   xepsxeps_maxyepsyeps_maxr5   	thetas_dx	thetas_dyr   r4   s   ```           @@r   _value_and_jac_anglerL      s    < bhhrl;E
r2,C 88E?3'D b$'ND(b$'ND(  Mh6IMh6II&&r    c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )FixedAxisArtistHelperz(
    Helper class for a fixed axis.
    c                 l    t         |   |       || _        || j                  }|| _        || _        y)}
        nth_coord = along which coordinate value varies.
         nth_coord = 0 ->  x axis, nth_coord = 1 -> y axis
        )locN)super__init__grid_helper	nth_coordnth_coord_ticksside)selfrT   rW   rV   	__class__s       r   rS   zFixedAxisArtistHelper.__init__^   s;     	T"&""nnO.	r    c                 :    | j                   j                  |       y r7   )rT   
update_limrX   axess     r   r[   z FixedAxisArtistHelper.update_limm   s    ##D)r    c                     |j                   S r7   	transDatar\   s     r   get_tick_transformz(FixedAxisArtistHelper.get_tick_transformp       ~~r    c                      j                   dk(  r|j                         n|j                         \  }}||kD  rddddd j                     n j                  t	        dddd          fd	} |       t        g       fS )
z tick_loc, tick_angle, tick_labelr   rightleftbottomtop)re   rd   rg   rf   Z   )re   rd   rf   rg   c               3      K   j                   dfdj                   z
  dffD ]E  \  } }j                  j                  ddg|       }|d      D ]  }g |d   |r|d   nd	  G y w)
NTr   FlonlatticksrQ   label )rV   rT   
_grid_info)rU   show_labelsgitickangle_tangentrX   rW   s       r   
iter_majorz<FixedAxisArtistHelper.get_tick_iterators.<locals>.iter_major~   s     ))401t7K7K3KU2S+U C&	;%%00%	1JKwK- CDCDK C C-8DMbC CCCs   A)A,)rU   get_ylimget_xlimrW   dictiter)rX   r]   v1v2rt   rs   rW   s   `    @@r   get_tick_iteratorsz(FixedAxisArtistHelper.get_tick_iteratorss   s    $(NNa$7T]]_B7#f#u66:iiAD 99D"Bqa@F	C |T"X%%r    r7   )	__name__
__module____qualname____doc__rS   r[   ra   r{   __classcell__rY   s   @r   rN   rN   Y   s    *&r    rN   c                   P     e Zd Zd
 fd	Zd Zd Zd Zd Zd Zd Z	d Z
d	 Z xZS )FloatingAxisArtistHelperc                     t         |   ||       || _        || _        t        j
                   t        j
                  f| _        d| _        y)rP   d   N)rR   rS   valuerT   r   inf	_extremes_line_num_points)rX   rT   rU   r   axis_directionrY   s        r   rS   z!FloatingAxisArtistHelper.__init__   s@    
 	E*
&&&"&& #r    c                 `    |t         j                   }|t         j                  }||f| _        y r7   )r   r   r   )rX   e1e2s      r   set_extremesz%FloatingAxisArtistHelper.set_extremes   s+    :&&B:BRr    c           
         | j                   j                  |       |j                         \  }}|j                         \  }}| j                   j                  }|j
                  j                  |j                         j                         t        j                  ||||            }|j                  \  }}	}
}| j                  \  }}| j                  dk(  rt        ||	      }	t        ||      }n'| j                  dk(  rt        ||      }t        ||
      }
|j!                  ||
      \  }}}|j#                  |	|      \  }}}| j                  dk(  r||j                         j%                  t'        j(                  t'        j*                  | j,                  | j.                        t'        j0                  |	|| j,                        g            }n| j                  dk(  r{|j                         j%                  t'        j(                  t'        j0                  ||
| j,                        t'        j*                  | j,                  | j.                        g            }t        j                  ||	|
|      ||t'        j2                  |      f||t'        j2                  |      f|j5                  dd||      |j5                  dd||      d| _        y )Nr   r   rf   r"   )extremeslon_infolat_info
lon_labels
lat_labelsline_xy)rT   r[   rv   ru   grid_finderextreme_finder_find_transformed_bboxget_transforminvertedr   from_extentsextentsr   rU   maxmingrid_locator1grid_locator2	transformr   column_stackr#   r   r   linspaceasarray_format_ticksro   )rX   r]   x1x2y1y2r   tbboxlon_minlat_minlon_maxlat_maxe_mine_maxlon_levslon_n
lon_factorlat_levslat_n
lat_factorxyss                        r   r[   z#FloatingAxisArtistHelper.update_lim   s`   ##D)BB&&22**AA%%'002D4E4Eb"bRT4UW .3]]*'7~~u>>Q%)G%)G^^q %)G%)G&1&?&?&Q#%&1&?&?&Q#%>>Q++-77--tzz:GWd.C.CDI 9 C ^^q ++-77GWd.C.CD--tzz:I 9 C ))'7GWM!5"**Z*@A!5"**Z*@A%338Z3%338Z3	
r    c                     t               S r7   )r   r\   s     r   get_axislabel_transformz0FloatingAxisArtistHelper.get_axislabel_transform   s
    zr    c                      fd} j                   d   j                  \  }}}} j                  dk(  r j                  }||z   dz  }n# j                  dk(  r||z   dz  } j                  }t	        |||f||f      \  }	}
}j
                  j                         j                  |	      }d|d   cxk  rdk  r:n yd|d   cxk  rdk  r)n y|	t        j                  ||
g j                           fS y)Nc                     j                   j                  j                         j                  z   }|j	                  | |g      j
                  S r7   )rT   r   r   r`   r   Txytrfr]   rX   s      r   trf_xyz@FloatingAxisArtistHelper.get_axislabel_pos_angle.<locals>.trf_xy   s?    ""..<<>OC==!Q(***r    r   r   r"   r   )NN)
ro   r   rU   r   rL   	transAxesr   r   r   rad2deg)rX   r]   r   xminyminxmaxymaxxx0yy0xy1angle_dxangle_dyps   ``           r   get_axislabel_pos_anglez0FloatingAxisArtistHelper.get_axislabel_pos_angle   s    	+ "&!<!D!DdD$>>Q**C$;!#C^^q $;!#C**C"6CtTlT4L#:XxNN##%//4!>>   1Q4n1n  

Hh#7#GHHHr    c                     t               S r7   )r   r\   s     r   ra   z+FloatingAxisArtistHelper.get_tick_transform   s     ""r    c                 v     j                   d   \  }}}||z  } j                   d   \  }}}||z  }	 j                  \  }
} fd} j                  dk(  r_|
|k  ||k  z  }t        | j                  ||   t
        j                   t
        j                  f|
|f      \  \   j                   d   nm j                  dk(  r^|
|	k  |	|k  z  }t        ||	|    j                  t
        j                   t
        j                  f|
|f      \  \   j                   d   t              D cg c]
  \  }}|s	| c}} j                        j                  z
  t        j                  t        j                  j                  d      fd	} |       t        g       fS c c}}w )
z9tick_loc, tick_angle, tick_label, (optionally) tick_labelr   r   c                     j                   j                  j                         j                  z   }|j	                  t        j                  t        j                  | |                  j                  S r7   )	rT   r   r   r`   r   r   r   broadcast_arraysr   r   s      r   r   z;FloatingAxisArtistHelper.get_tick_iterators.<locals>.trf_xy   sQ    ""..<<>OC==1D1DQ1J!KLNNNr    r   r   r   r   )r   r   c               3      K   t        	      D ]T  \  } }}}}
j                  | |f      } |d         s( |d         s4| |ggt        j                  ||g      | V y w)Nr   r   )zipr   r   r   )r   r   normaltangentlabc2angle_normalrs   in_01labelstick_to_axesxx1yy1s         r   rt   z?FloatingAxisArtistHelper.get_tick_iterators.<locals>.iter_major  s~     3\=&IF*1fgs!++QF3A<E"Q%La&E2::vw.?#@E#EE	Fs   7A+A+$A+)ro   r   rU   rL   r   r   r   r   ra   r   	functoolspartialmpl
transforms_interval_contains_closerx   )rX   r]   r   r   r   r   r   r   r   r   e0r   r   masklmrt   r   rs   r   r   r   r   r   s   ``               @@@@@@@r   r{   z+FloatingAxisArtistHelper.get_tick_iterators   s    '+ooj&A#%#&*ooj&A#%#B	O
 >>Q#I#),D6J

CI/@2r(7L3JS#m__\2F^^q #I#),D6JD	4::/@2r(7L3JS#|__\2F #FD 171Q!7..t4t~~E!!NN33V=	F 	F |T"X%% 8s   9
F5F5c                     |j                   S r7   r_   r\   s     r   get_line_transformz+FloatingAxisArtistHelper.get_line_transform  rb   r    c                 T    | j                  |       t        | j                  d         S )Nr   )r[   r   ro   r\   s     r   get_linez!FloatingAxisArtistHelper.get_line  s"    DOOI.//r    r7   )r|   r}   r~   rS   r   r[   r   r   ra   r{   r   r   r   r   s   @r   r   r      s2    	$ )
V(#(&T0r    r   c                   Z     e Zd Z	 	 	 	 	 d	 fd	Zd
dZddddddZddZd ZddZ xZ	S )GridHelperCurveLinearNc                 Z    t         |           d| _        t        ||||||      | _        y)a  
        Parameters
        ----------
        aux_trans : `.Transform` or tuple[Callable, Callable]
            The transform from curved coordinates to rectilinear coordinate:
            either a `.Transform` instance (which provides also its inverse),
            or a pair of callables ``(trans, inv_trans)`` that define the
            transform and its inverse.  The callables should have signature::

                x_rect, y_rect = trans(x_curved, y_curved)
                x_curved, y_curved = inv_trans(x_rect, y_rect)

        extreme_finder

        grid_locator1, grid_locator2
            Grid locators for each axis.

        tick_formatter1, tick_formatter2
            Tick formatters for each axis.
        N)rR   rS   ro   r   r   )rX   	aux_transr   r   r   tick_formatter1tick_formatter2rY   s          r   rS   zGridHelperCurveLinear.__init__  s5    4 	%i&4&3&3&5&57r    c                     || j                   j                  |        | j                   j                  di | d | _        y )Nr8   )r   update_transformupdate_old_limits)rX   r   kwargss      r   update_grid_finderz(GridHelperCurveLinear.update_grid_finder8  s=     --i8)&)r    )r   offsetr]   rU   c                b    || j                   }||}t        | ||      }t        |||      }|S )N)rV   )r   )r]   rN   r   )rX   rQ   r   r   r]   rU   helperaxislines           r   new_fixed_axisz$GridHelperCurveLinear.new_fixed_axis>  s@     <99D! N&tS)LdF>J r    c                     || j                   }t        | |||      }t        ||      }|j                  j	                  d       |j                  j                  |j                   j                         |S )NT)r]   r   r   lineset_clip_onset_clip_boxbbox)rX   rU   r   r]   r   r   r   s          r   new_floating_axisz'GridHelperCurveLinear.new_floating_axisK  sd    <99D))UN4dF+!!$'""8==#5#56 r    c                 D    | j                   j                  |      | _        y r7   )r   get_grid_inforo   )rX   r  s     r   _update_gridz"GridHelperCurveLinear._update_gridW  s    **88>r    c                    g }|dv r9|j                  | j                  d   d   D cg c]  }|j                   c}       |dv r9|j                  | j                  d   d   D cg c]  }|j                   c}       |S c c}w c c}w )N)bothr   rj   lines)r  r   rk   )extendro   r   )rX   whichaxis
grid_linesgls        r   get_gridlinesz#GridHelperCurveLinear.get_gridlinesZ  s|    
= dooe.DW.MNrttNO= dooe.DW.MNrttNO ONs   A?#B)NNNNNr7   )Nrf   )majorr  )
r|   r}   r~   rS   r   r   r  r  r  r   r   s   @r   r   r     s<     $##!%!%!7F  &*$TT
?r    r   )r   r   numpyr   
matplotlibr   matplotlib.pathr   matplotlib.transformsr   r   r   	axislinesr   r	   r
   axis_artistr   r   r   rL   rN   r   r   r8   r    r   <module>r     sc         C CO O # #D'N-&6 -&`H0< H0VLN Lr    