B
    z‹dQ  ã               @   s†   d dl Z e  d¡ZddlmZ ddlmZ d dlZ	d dl
Z
ddlmZ ddlmZ e	jZed Zddd„Zdd„ ZG dd„ deƒZdS )é    NZhealpyé   )Úprojaxes)Úrotator)ÚUNSEEN)Ú	pixelfuncg     €f@Ú é   úMollweide viewFÚastroú%gc       '      C   sš  ddl }t | ¡}tj||d |j|dd}d}| ¡ }| ¡  z8| dkr^t d¡tj	 } t 
| ¡} tj||||||
d}| |¡ |r tj| ||d	d
} n|r¶tj| ||d	d
} |j| |||||	||d | ¡ d }|j t dd|jjd ¡¡}t |jj|jj|jj¡}tjdkrL|j| ¡ d |dddt ¡ dd||d
}n(|j| ¡ d dddt ¡ dd||d	}| |¡ |jdd|jj dd|j!d |j"jdd|dd|j"j!ddd | #|¡ d}d}d }tj$||||||
d} | | ¡ |r
tj| ||d	d
} n|r"tj| ||d	d
} | j| ||||	|||||d!
 |  ¡ d }|j t dd|jjd ¡¡}t |jj|jj|jj¡}tjdkr¼|j|  ¡ d | dddt ¡ d"d||d
}n(|j|  ¡ d dddt ¡ d"d||d	}|  |¡ | jd#d$d%| jj%d& | jj%d' | jj%d( f dd)| j!d*d+ | jd#d,| jj ddd*| j!d- t &| jj'd	d.| j(¡\}!}"| jdd/d0|!|"f dd| j!d1 |j"jdd|dd|j"j!ddd | )d2d$d3d2g¡}#|# *d4¡ | )d$d$d3d2g¡}$|$ *d4¡ |$j!}%|$jdd,d5|%d6d7 |$jdd8d9|%d6d7 |$jddd:|%d6d7 |$jdd;d<|%d6d7 |$jdd=d>|%d6d7 |$jddd?|%d6d7 | #| ¡ t+| |j,||||d@}&W d| -¡  |r”| .¡  X dS )Aah  Interactive mollweide plot with zoomed gnomview.

    Parameters:
    -----------
    map : float, array-like shape (Npix,)
      An array containing the map,
      supports masked maps, see the `ma` function.
      if None, use map with inf value (white map), useful for
      overplotting
    fig : a figure number.
      Default: create a new figure
    rot : scalar or sequence, optional
      Describe the rotation to apply.
      In the form (lon, lat, psi) (unit: degrees) : the point at
      longitude *lon* and latitude *lat* will be at the center. An additional rotation
      of angle *psi* around this direction is applied.
    coord : sequence of character, optional
      Either one of 'G', 'E' or 'C' to describe the coordinate
      system of the map, or a sequence of 2 of these to rotate
      the map from the first to the second coordinate system.
    unit : str, optional
      A text describing the unit of the data. Default: ''
    xsize : int, optional
      The size of the image. Default: 800
    title : str, optional
      The title of the plot. Default: 'Mollweide view'
    nest : bool, optional
      If True, ordering scheme is NESTED. Default: False (RING)
    min : float, optional
      The minimum range value
    max : float, optional
      The maximum range value
    flip : {'astro', 'geo'}, optional
      Defines the convention of projection : 'astro' (default, east towards left, west towards right)
      or 'geo' (east towards roght, west towards left)
    remove_dip : bool, optional
      If :const:`True`, remove the dipole+monopole
    remove_mono : bool, optional
      If :const:`True`, remove the monopole
    gal_cut : float, scalar, optional
      Symmetric galactic cut for the dipole/monopole fit.
      Removes points in latitude range [-gal_cut, +gal_cut]
    format : str, optional
      The format of the scale label. Default: '%g'
    r   N)Únest)g      %@gš™™™™™@)Zfigsize)g{®Gáz”?g      Ð?gìQ¸…ëá?g
×£p=
ç?é   )ÚcoordÚrotÚformatZflipconvT)Úgal_cutr   Úcopy)r   Úxsizer   ÚvminÚvmaxÚcmapÚnormr   z0.91.0Ú
horizontalg      à?é   gš™™™™™©?gš™™™™™¹?)	ÚaxÚorientationÚshrinkÚaspectÚticksÚpadÚfractionÚ
boundariesÚvalues)r   r   r   r   r   r    r!   r"   g…ëQ¸…ë?é   Úbold)ÚfontsizeÚ
fontweightÚ	transformgÍÌÌÌÌÌð?g333333Ó?ÚleftÚcenter)r%   r&   r'   ÚhaÚvaiX  g      ð?)g333333ã?g{®Gáz¤?gR¸…ëQØ?g®Gázî?)	r   r   r   r   r   ÚysizeÚresor   r   g{®Gáz´?gìQ¸…ë±¿g{®Gáz”?z%g '/pix,   %dx%d pixr-   r   r,   ÚbottoméZ   )r%   Úverticalalignmentr'   Úrotationgš™™™™™é?)r%   r&   r1   r'   )Úlonlatg¸…ëQ¸ž¿z
on (%g,%g))r0   Zhorizontalalignmentr'   g      Ð?g)\Âõ(Ì?Úoffzr/t .... zoom out/inÚbaseline)r'   r+   gÍÌÌÌÌÌä?zp/v .... print coord/valzc ...... go to centergffffffÖ?zf ...... next color scalegš™™™™™É?zk ...... save current scalezg ...... toggle graticule)Úfigr   r   r   r   )/Úpylabr   Z	get_nsideZcheck_nsideÚfigureÚisinteractiveÚioffÚnpZzerosÚinfZma_to_arrayÚPAZHpxMollweideAxesZadd_axesZremove_dipoleZremove_monopoleÚprojmapZ
get_imagesr   ZinverseZlinspacer   ÚNr   r   Ú
matplotlibÚ__version__ÚcolorbarÚBoundaryLocatorÚ	set_titleÚtextÚprojZcoordsysstrÚ	transAxesr   ZscaZHpxGnomonicAxesÚ	arrayinfoÚaroundÚ
get_centerÚ
_coordprecZaxesÚaxisÚZoomToolÚnumberÚdrawÚion)'Úmapr5   r   r   Úunitr   Útitler   ÚminÚmaxÚflipZ
remove_dipZremove_monor   r   r   r   ZholdZmarginsÚsubr6   ZnsideÚfZextentÚwasinteractiver   ÚimÚbÚvÚcbZg_xsizeZg_resoZg_axÚlonÚlatZgrat_axZhelp_axÚtZzt© r`   ú\/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/healpy/zoomtool.pyÚmollzoom"   sX   C





















rb   c             C   sH   ddl }| ¡ }t|dƒs"tdƒ‚| |j_||j_d|j_|j ¡  dS )z5Set min/max value of the gnomview part of a mollzoom.r   NÚzoomtoolz"The current figure has no zoomtoolé   )	r6   ÚgcfÚhasattrÚ	TypeErrorrc   Úsave_minÚsave_maxÚ_range_statusÚ	draw_gnom)r   r   r6   rW   r`   r`   ra   Ú
set_g_clim5  s    
rl   c               @   sn   e Zd 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d„ Zddd„Zddd„ZdS )rL   zbA class providing zoom capability to a figure containing a Mollweide
    and a Gnomonic axis.
    NFc             C   s8  ddl }ddddddd	d
dddddddg| _|| _|| _|| _|| _|| _d| _d | _| _	d| _
t|tƒr¦t|ƒdkr†d\| _| _qât| ¡ ƒt| ¡ ƒ | _| _n<||tk }|jdkrÊd\| _| _n| ¡ | ¡  | _| _~|dkrô| ¡ }	n
| |¡}	|	| _| |	_|	 ¡ dd… \| _| _| _| _|	 ¡ d | _| jj\| _ | _!| _"| jj#j$d | _%| jj#j$d | _&y| j '| jj#j(d ¡| _)W n2 t*k
rº }
 zt*d| j ƒ‚W dd}
~
X Y nX | jj+dgdgddddd\| _,| jj+dgdgddddd\| _-| jj.d d!d"| jj/d#d$d%| _0|  1dd¡ d| _2|  3¡  dS )&zxm: the map to be zoomed (already plotted in Mollweide view)
        fig: the figure to instrument (None->gcf())
        r   Ngš™™™™™©?gš™™™™™¹?gš™™™™™É?g333333Ó?g      à?g      è?g      ð?g      ø?g      @g      @g      $@g      .@g      >@g     €F@g      N@F)g      ð¿g      ð?é   r   r,   r-   zResolution not in %sÚokr   é   )ÚmewÚmsÚalphaZxré   )rq   rr   rp   gš™™™™™Ù¿gš™™™™™É¿zscale mode: locr4   r(   )r'   r+   r*   )4r6   Ú	reso_listÚ_mapÚ_nestÚ_cmapÚ_normÚ_coordrj   rh   ri   Ú_gratonÚ
isinstanceÚdictÚlenÚ_mapminÚ_mapmaxrS   r"   rT   r   Úsizere   r7   rW   rc   Zget_axesÚ_moll_axZ_moll_cb_axÚ_gnom_axÚ_gnom_cb_axÚ_grat_axZtextsÚ
_text_resoZ_text_coordÚ	_text_locrE   rG   Ú_xsizeÚ_ysizeÚindexZ
_arrayinfoÚ	_reso_idxÚ
ValueErrorZplotÚ
zoomcenterÚzoomcenter2rD   rF   Ú_text_rangerk   Ú
_connectedÚconnect_callbacks)ÚselfÚmr5   r   r   r   r   r6   ZmgoodrW   Úer`   r`   ra   Ú__init__G  sx    
 


   
zZoomTool.__init__c          
   C   s®   dd l }yh|j}| |j|j¡\}}t |¡s8t |¡r@tdƒ‚| |j|j¡}|| _	|  
||¡ |  ||¡ W n8 tk
r¨ } z|  
ddd¡ | ¡  W d d }~X Y nX d S )Nr   zinvalid positionF)r6   ZinaxesZ
get_lonlatZxdataZydatar:   Úisnanr‹   Ú	get_valueÚlastvalÚ_move_zoom_centerrk   Ú	ExceptionZdraw_if_interactive)r‘   Úevr6   r   r]   r^   ÚvalÚsr`   r`   ra   Ú_zoom_on_click—  s    zZoomTool._zoom_on_clickc             C   sd  |j dkr|  ¡  nJ|j dkr,|  ¡  n4|j dkrLt d| j| j¡ n|j dkrp|  dd¡ |  dd¡ nð|j dkrŠt d| j	¡ nÖ|j d	krº|  j
d
7  _
|  j
d;  _
|  ¡  n¦|j dkrê| jjd jj| _| jjd jj| _nv|j dkr`t| dƒr*| jdkr*| j ¡  | j ¡  d| _n.| jjdd\| _| _| j ¡ \| _| _d| _|  ¡  d S )NÚrr_   Úpzlon,lat = %.17g,%.17gÚcr   r[   zval = %.17grW   r   rs   ÚkÚgrz   TF)Úlocal)ÚkeyÚ_decrease_resoÚ_increase_resoÚlogÚinfor]   r^   r˜   rk   r—   rj   r‚   Úimagesr   r   rh   r   ri   rf   rz   Údelgraticulesr   Ú	graticuleÚ_g_dparÚ_g_dmerÚ_m_dparÚ_m_dmer)r‘   rš   r`   r`   ra   Ú_reso_on_key©  s8    









zZoomTool._reso_on_keyc             C   sf  | j  ¡  | j  d¡ | jrb| j }|j}|jddd|dd t t | j	t
 d¡¡}| j	t
 | d }|jdd	d
||f |d t t | jt
 d¡¡}| jt
 | d }|jddd||f |d |jddd|dd t t | jt
 d¡¡}| jt
 | d }|jddd
||f |d t t | jt
 d¡¡}| jt
 | d }|jddd||f |d d S )Nr3   gš™™™™™¹?gš™™™™™é?zmoll. grat.:r$   )r'   Úweighté
   g      N@gÍÌÌÌÌÌä?z   -par: %d d %.2f ')r'   g      à?z   -mer: %d d %.2f 'gffffffÖ?zgnom. grat.:gš™™™™™É?gš™™™™™©?)r„   ÚclarK   rz   rF   rD   r:   ÚfloorrH   r®   Údtorr¯   r¬   r­   )r‘   Úar_   ZvdegZvarcminr`   r`   ra   Ú_update_grat_infoÉ  s&    
zZoomTool._update_grat_infoc             C   s,   | j dkr(|  j d8  _ |  | j| j¡ d S )Nr   r   )rŠ   rk   r]   r^   )r‘   r`   r`   ra   r¦   Þ  s    
zZoomTool._increase_resoc             C   s6   | j t| jƒd k r2|  j d7  _ |  | j| j¡ d S )Nr   )rŠ   r}   rt   rk   r]   r^   )r‘   r`   r`   ra   r¥   ã  s    zZoomTool._decrease_resoc             C   s   | j | j S )N)rt   rŠ   )r‘   r`   r`   ra   Úget_resoè  s    zZoomTool.get_resoc             C   sR   | j sNg | _| jj d| j¡}| j |¡ | jj d| j¡}| j |¡ d| _ d S )NZbutton_press_eventZkey_press_eventT)r   Ú_callbacks_idrW   ÚcanvasZmpl_connectr   Úappendr°   )r‘   Úcidr`   r`   ra   r   ë  s    zZoomTool.connect_callbacksc             C   s(   | j r$x| jD ]}| jj |¡ qW d S )N)r   r¹   r7   rº   Zmpl_disconnect)r‘   r¼   r`   r`   ra   Údisconnect_callbacksô  s    zZoomTool.disconnect_callbacksTc             C   s   | j rF| jjj||dd\}}| j  |g¡ | j  |g¡ | j  |¡ | jrŒ| jjj||dd\}}| j |g¡ | j |g¡ | j |¡ d S )NT)r2   )rŒ   r   rE   Zang2xyZ	set_xdataZ	set_ydataZset_visibler   )r‘   r]   r^   ÚvisibleÚxÚyr`   r`   ra   r˜   ù  s    zZoomTool._move_zoom_centerc             C   s
  dd l }| ¡ }| ¡  zÊyž|d kr0| j}n|| _|d krF| j}n|| _| jjjj 	¡  | jjjj 
tj||fdd¡ | jjj ¡  | jdkržd  }}n2| jdkr¸| j| j }}n| jdkrÐ| j| j }}| jj 	¡  | jj| j| j| j||| j| j|  ¡ | j| jd
 t| jdƒrŒdd	„ | jjD ƒ}xZ|D ]R}|\}	}
| jj  |	¡ | jj  |¡ |
\}}}}| jj!|f|žd
|i|—Ž q4W ~| j"r´| j #¡  | jj$dd\| _%| _&| j' (¡  | jjd }t)j*dkrö| j+j,|| j| j'dt- .¡ d}n| j+j,|| j'dt- .¡ d}t/ 0| jjj1dd| jj2¡\}}| j3 4d||f ¡ | jjj5d }| jjj5d }| jjj5d }| j6 4d|||f ¡ dddg| j }| j7 4d| ¡ || | _8| _9|  :¡  W n$ t;k
rà } zW d d }~X Y nX W d |r| <¡  | =¡  | >¡  X d S )Nr   T)Údegr   rd   )	r   r   r   r   r   r,   r-   r   r   Ú_scatter_datac             S   s   g | ]}|‘qS r`   r`   )Ú.0r¿   r`   r`   ra   ú
<listcomp>.  s    z&ZoomTool.draw_gnom.<locals>.<listcomp>ÚphiF)r£   z0.91.0r   )r   Úcaxr   r   )rÆ   r   r   )r2   z
on (%g,%g)r-   r   r,   z%g '/pix,   %dx%d pixÚlocrP   Zsavzscale mode: %s)?r6   r8   r9   Z_lonZ_latr‚   rE   r   Z_rotsÚpopr»   ÚRZnormalise_rotZ_update_matrixrj   r~   r   rh   ri   r©   r=   ru   rv   ry   r‡   rˆ   r¸   rw   rx   rf   rÂ   ÚcollectionsÚremoveZprojscatterrz   rª   r«   r¬   r­   rƒ   r³   r?   r@   rW   rA   r<   rB   r:   rH   rI   rJ   r†   Zset_textrG   r…   rŽ   r]   r^   r·   r™   rO   rN   Úshow)r‘   r]   r^   r6   rX   r   r   ÚlÚsdrœ   Z
input_dataÚthetarÅ   ÚargsÚkwdsrY   r\   r-   r   r,   Úmoder“   r`   r`   ra   rk     s˜    





"

zZoomTool.draw_gnom)NFNNN)T)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r”   r   r°   r·   r¦   r¥   r¸   r   r½   r˜   rk   r`   r`   r`   ra   rL   B  s   
P 	
rL   )NNNNr   r   r	   FNNr
   FFr   r   NNFNN)ÚloggingÚ	getLoggerr§   r   r   r<   r   rÉ   Únumpyr:   r?   Z_healpy_pixel_libr   r   Úpirµ   rb   rl   ÚobjectrL   r`   r`   r`   ra   Ú<module>   sB   
                   
  