a
    ߙfbE                     @   sd  d dl Z d dlmZ d dlZd dlZd dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dl m!Z!m"Z" d dl#m$Z$m%Z%m&Z& ej'dd Z(eddZ)ddge)j*_+ddge)j*_,ddge)j*_-ddge)j*_.ddge)j*_/eddZ0g de0j*_+g de0j*_,g de0j*_-g de0j*_.g de0j*_/ej'd d! Z1ej'd"d# Z2d$d% Z3d&d' Z4d(d) Z5d*d+ Z6d,d- Z7d.d/ Z8d0d1 Z9d2d3 Z:d4d5 Z;ej'd6d7 Z<ej=>d8ej?d9 g d:d;dfej?d9 g d<d=dfej?d9 g d>d?dfej?d9 g d@dAdfej?ddddd ddf g dBdCdfej?ddddd ddf g dDdEdfej?ddddd ddf g dFdEdfej?ddd ddddf g dFdAdfgdGdH Z@ej=>dIej?ddddd ddf gdJdK ZAG dLdM dMeZBdNdO ZCdPdQ ZDej=jEdRdSej=jFed i dTdUdV ZGdS )W    N)dedent)Affine2DIdentityTransform)fits)units)BaseLowLevelWCSSlicedLowLevelWCS)SkyCoord)Time)Quantity)IMAGE_REFERENCE_DIR)get_pkg_data_filename)WCS)RectangularFrameRectangularFrame1D)WCSWorld2PixelTransformtransform_coord_meta_from_wcsapply_slicesc                   c   s   d V  t d d S )Nall)pltclose r   r   Nlib/python3.9/site-packages/astropy/visualization/wcsaxes/tests/test_wcsapi.py	plt_close   s    r      Znaxisxykm     4@     Ċ@      @           )r   r   z)r   r   r   )r   r    iM  )r!   r!      )r"   r"         ?c                  C   s   t d} ttjj| dddS )Na      WCSAXES =                    4 / Number of coordinate axes
    CRPIX1  =                  0.0 / Pixel coordinate of reference point
    CRPIX2  =                  0.0 / Pixel coordinate of reference point
    CRPIX3  =                  0.0 / Pixel coordinate of reference point
    CRPIX4  =                  5.0 / Pixel coordinate of reference point
    CDELT1  =                  0.4 / [min] Coordinate increment at reference point
    CDELT2  =                2E-11 / [m] Coordinate increment at reference point
    CDELT3  =   0.0027777777777778 / [deg] Coordinate increment at reference point
    CDELT4  =   0.0013888888888889 / [deg] Coordinate increment at reference point
    CUNIT1  = 'min'                / Units of coordinate increment and value
    CUNIT2  = 'm'                  / Units of coordinate increment and value
    CUNIT3  = 'deg'                / Units of coordinate increment and value
    CUNIT4  = 'deg'                / Units of coordinate increment and value
    CTYPE1  = 'TIME'               / Coordinate type code
    CTYPE2  = 'WAVE'               / Vacuum wavelength (linear)
    CTYPE3  = 'HPLT-TAN'           / Coordinate type codegnomonic projection
    CTYPE4  = 'HPLN-TAN'           / Coordinate type codegnomonic projection
    CRVAL1  =                  0.0 / [min] Coordinate value at reference point
    CRVAL2  =                  0.0 / [m] Coordinate value at reference point
    CRVAL3  =                  0.0 / [deg] Coordinate value at reference point
    CRVAL4  =                  0.0 / [deg] Coordinate value at reference point
    LONPOLE =                180.0 / [deg] Native longitude of celestial pole
    LATPOLE =                  0.0 / [deg] Native latitude of celestial pole
    
)sepheader)r   r   r   HeaderZ
fromstringr)   r   r   r   wcs_4d0   s    r,   c                  C   s   t d} tj| }t|dS )Nzdata/cube_headerr)   )r   r   r+   Zfromtextfiler   )Zcube_headerr*   r   r   r   cube_wcsN   s    r-   c                  C   s   t dd} ddg| j_ddg| j_ddg| j_dd	g| j_t dd}d
dg|j_ddg|j_ddg|j_ddg|j_t| }t|}|| ||  ksJ || | | ksJ || t ksJ dS )zo
    Test that the Matplotlib subtraction shorthand for composing and inverting
    transformations works.
    r   r   RA---TANDEC--TAN      p@皙g?g      ^@g      3zRA---SINzDEC--SINg     `m@g333337@N)	r   wcsctypecrpixcdeltcrvalr   invertedr   )w1Zw2t1t2r   r   r   test_shorthand_inversionU   s    

r;   c                  C   sB   t d} ttt  }|| }| |}t j| | d S )N
   r   )	nponesr   WCS2Dr   	transformr7   testingassert_allcloseworldr8   ZpixelZworld_2r   r   r   test_2dr   s
    

rF   c                  C   s   t d} ttd d dd d f t  }|| }| |}t j| d d df |d d df  t j| d d df |d d df  d S )Nr<   r      )	r>   r?   r   WCS3Dr   rA   r7   rB   rC   rD   r   r   r   test_3d}   s    
 
&rI   c                 C   s  t | tdd\}}|d }|d }|d }|g dks:J |g dksJJ |g dksZJ tdd}d	d
g|j_dgd |j_dgd |j_dgd |j_ddg|j_|j	  t |t\}}|d ddgksJ |d t
jt
jgksJ |d d d gksJ |d ddgksJ |d ddgks*J tdd}ddg|j_dgd |j_dgd |j_dgd |j_|j	  t |t\}}|d ddgksJ |d t
jt
jgksJ |d dd gksJ t |tdd\}}|d }|d }|d }|ddgksJ |ddgksJ |d d gks(J tdd}d!d"g|j_dgd |j_dgd |j_dgd |j_|j	  t |t\}}|d ddgksJ |d t
jt
jgksJ |d dd gksJ tdd}d#d$g|j_dgd |j_dgd |j_dgd |j_|j	  t |t\}}|d ddgks4J |d t
jt
jgksNJ |d d%d gksdJ tdd}d&d'g|j_dgd |j_dgd |j_dgd |j_|j	  t |t\}}|d ddgksJ |d t
jt
jgksJ |d d d gksJ tdd}d(d(g|j_dgd |j_dgd |j_dgd |j_|j	  t |t\}}|d d)d)gkspJ |d t
jt
jgksJ |d d d gksJ d S )*N)2   r   r   slicesdefault_axislabel_positiondefault_ticklabel_positiondefault_ticks_position)lrbr   r   GLON-TANGLAT-TANr0   r1         I@	Longitude type	longitudelatitudeformat_unitwrapdefault_axis_labelpos.galactic.latname)zpos.galactic.lonzglon-tanZglonrV   )r^   zglat-tanZglatzHPLN-TANzHPLT-TAN     f@)r   r   rP   rR   bltrzHGLN-TANzHGLT-TANzCRLN-TANzCRLT-TANg     v@r.   r/   Zspamscalar)r   r   r   r2   r3   r4   r5   r6   Zcnamesetudegarcsec	hourangleone)r-   _
coord_metaaxislabel_positionticklabel_positionticks_positionr2   r   r   r   test_coord_type_from_ctype   s    













rn   c                  C   s   t dd} dg| j_dg| j_dg| j_dg| j_| j  t| t\}}|d dgks\J |d	 t	j
gkspJ |d
 d gksJ d S )NrG   r   WAVEr0   r1   rU   rX   rb   r[   r\   )r   r2   r3   r4   r5   r6   rc   r   r   rd   mr2   ri   rj   r   r   r   test_coord_type_1d_1d_wcs   s    





rr   c                  C   s   t dd} ddg| j_dgd | j_dgd | j_dgd | j_| j  t| tdd	\}}|d
 ddgkspJ |d t	j
t	j
gksJ |d d d gksJ |d ddgksJ d S )Nr   r   rS   rT   r0   r1   rU   r   r   rK   rX   rY   rZ   r[   r\   visibleT)r   r2   r3   r4   r5   r6   rc   r   r   rd   re   rq   r   r   r   $test_coord_type_1d_2d_wcs_correlated  s    

ru   c                  C   s   t dd} ddg| j_dgd | j_dgd | j_dgd | j_dd	g| j_| j  t| t	d
d\}}|d ddgks|J |d t
jt
jgksJ |d d d gksJ |d ddgksJ d S )Nr   r   ro   ZUTCr0   r1   rU   Znmsrs   rK   rX   rb   r[   r\   rt   TF)r   r2   r3   r4   r5   r6   cunitrc   r   r   rd   rp   rv   rq   r   r   r   &test_coord_type_1d_2d_wcs_uncorrelated  s    

rx   c                 C   s^   t | tdd\}}|d }|d }|d }|g dks:J |g dksJJ |g dksZJ d S )Nr   r   r   r   rK   rM   rN   rO   )rW   rW   rR   rP   )rW   rW   ra   ra   )r   r   r,   ri   rj   rk   rl   rm   r   r   r   test_coord_meta_4d&  s    r{   c                 C   s^   t | tdd\}}|d }|d }|d }|g dks:J |g dksJJ |g dksZJ d S )N)r   r   r   r   rK   rM   rN   rO   )rW   rW   trR   )r   r   rz   r   r   r   test_coord_meta_4d_line_plot3  s    r}   c                 C   s
   t | |S N)r   )r,   	wcs_slicer   r   r   sub_wcsA  s    r   )r   wcsaxes_slices	world_mapndim.ry   )r   r#   )r   r   r   r   )rG   r   r#   )r   r   r   r   )r   r   r#   )r   r   r   r   )r   rG   r   r   r   )rG   r   )r   r   r   )r   rG   r   )r   r   r   c                 C   s.   t | |\}}}|j|ksJ ||ks*J d S r~   )r   world_n_dim)r   r   r   r   r   Ztransform_wcsri   Zout_world_mapr   r   r   test_apply_slicesF  s    r   r   c              	   C   s~  g d}|t | |fD ]`}t 0 tjdtd t|t|d\}}W d    n1 sZ0    Y  tdd | D s~J |d g dksJ |d	 g d
ksJ |d g dksJ |d t	
dt	
dt	
dgksJ |d g dksJ |d t	
dt	
dt	
dgks J |d g dks6J |d g dksLJ |d g dksbJ t  tj||d qd S )Nr   ignore)categoryrK   c                 s   s   | ]}t |d kV  qdS )r#   N)len).0r   r   r   r   	<genexpr>b      z'test_sliced_ND_input.<locals>.<genexpr>r_   )time)zcustom:pos.helioprojective.latzhplt-tanZhplt)zcustom:pos.helioprojective.lonzhpln-tanZhplnrX   )rb   rZ   rY   r\   )NNr`   unitminre   rt   )FTTr[   rf   rM   )rW   rR   rP   rN   rO   )rW   ra   ra   )
projectionrL   )r   warningscatch_warningsfilterwarningsFutureWarningr   r   r   valuesrd   Unitr   ZclfZsubplot)r,   r   r   r   Zslices_wcsaxesri   rj   r   r   r   test_sliced_ND_inputY  s"    
0(*r   c                   @   st   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	dd Z
dd Zedd Zedd ZdS )LowLevelWCS5Dr   c                 C   s   | j S r~   )	pixel_dimselfr   r   r   pixel_n_dimx  s    zLowLevelWCS5D.pixel_n_dimc                 C   s   dS )N   r   r   r   r   r   r   |  s    zLowLevelWCS5D.world_n_dimc                 C   s   g dS )N)em.freqr   	pos.eq.ra
pos.eq.decphys.polarization.stokesr   r   r   r   r   world_axis_physical_types  s    z'LowLevelWCS5D.world_axis_physical_typesc                 C   s   g dS )N)HzZdayre   re   rW   r   r   r   r   r   world_axis_units  s    zLowLevelWCS5D.world_axis_unitsc                 C   s   g dS )N)	FrequencyrW   RADECrW   r   r   r   r   r   world_axis_names  s    zLowLevelWCS5D.world_axis_namesc                 G   s,   t |d d d }dd t|g dD S )Nr#   c                 S   s   g | ]\}}t || qS r   r>   Zasarray)r   Zpixscaler   r   r   
<listcomp>  r   z7LowLevelWCS5D.pixel_to_world_values.<locals>.<listcomp>)r=   皙?g?g(\?r   )listzip)r   Zpixel_arraysr   r   r   pixel_to_world_values  s    z#LowLevelWCS5D.pixel_to_world_valuesc                 G   s$   |d d }dd t |ddgD S )Nr   c                 S   s   g | ]\}}t || qS r   r   )r   rE   r   r   r   r   r     r   z7LowLevelWCS5D.world_to_pixel_values.<locals>.<listcomp>r=   r   )r   )r   Zworld_arraysr   r   r   world_to_pixel_values  s    z#LowLevelWCS5D.world_to_pixel_valuesc                 C   s   g dS )N))freqr   value)r   r   mjd)	celestialr   zspherical.lon.degree)r   rG   zspherical.lat.degree)stokesr   r   r   r   r   r   r   world_axis_object_components  s    z*LowLevelWCS5D.world_axis_object_componentsc                 C   s6   t dddiftdddiftdddiftdddifdS )	Nr   r   re   formatr   r   rh   )r   r   r   r   )r	   r
   r   r   r   r   r   world_axis_object_classes  s
    z'LowLevelWCS5D.world_axis_object_classesN)__name__
__module____qualname__r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   u  s"   





r   c                  C   s   ddg} t jj|  }ddddddddd	d
d
d
dd}t|}t }|jg d|d}|j|dd |jd }|jd }|j	
  t j|jjd t g d t j|jjd t g d d S )N   ih  r   g     f@g     V@r&   re   zCRLN-CARzCRLT-CARr"        V@)ZwcsaxesZcrpix1Zcrpix2Zcdelt1Zcdelt2Zcunit1Zcunit2Zctype1Zctype2Zcrval1Zcrval2ZlonpoleZlatpole皙?r   皙?r   r   lower)originr   rG   rR   )r   r`   r`   g     p@r"   rP   )g     Vg      Ng      >r"   g      >@g      N@r   )r>   ZrandomZrandr   r   figureadd_axesZimshowZcoordsZcanvasZdrawrB   Zassert_equalZticksrE   Zarray)shapedatar*   r2   figaxZlonZlatr   r   r   test_edge_axes  s.    


r   c                  C   s4  t  } d| _t| tg dd\}}|d g dks6J |d g dksJJ |d g d	ks^J |d
 tdtdtdtdtjgksJ |d g dksJ |d tdtdtdtdtjgksJ |d g dksJ |d g dksJ |d g dksJ |d g dks0J d S )Nr   )r   r   r   r   r   rK   r_   ))r   r   r   )r   r   )r   r   r   rX   )rb   rb   rY   rZ   rb   r\   )NNNNNr   r   dre   rt   )TTTTTr[   rg   rM   )rR   rP   r|   rQ   rW   rN   rO   r]   )r   r   r   r   r   )r   r   r   r   rd   r   rh   rq   r   r   r   test_coord_meta_wcsapi  s    44r   astropy)source)Zbaseline_dirZ	toleranceZstylec                 C   s<   t jdd}|jg dt d}|dd |dd |S )N)   r   )Zfigsizer   r   g      g     b@)r   r   r   r   Zset_xlimZset_ylim)r   r   r   r   r   r   test_wcsapi_5d_with_names  s
    r   )Hr   textwrapr   ZpytestZnumpyr>   Zmatplotlib.pyplotZpyplotr   Zmatplotlib.transformsr   r   Z
astropy.ior   r   r   rd   Zastropy.wcs.wcsapir   r   Zastropy.coordinatesr	   Zastropy.timer
   Zastropy.unitsr   Zastropy.tests.image_testsr   Zastropy.utils.datar   Zastropy.wcsr   Z#astropy.visualization.wcsaxes.framer   r   Z$astropy.visualization.wcsaxes.wcsapir   r   r   Zfixturer   r@   r2   r3   rw   r4   r5   r6   rH   r,   r-   r;   rF   rI   rn   rr   ru   rx   r{   r}   r   ZmarkZparametrizeZs_r   r   r   r   r   Zremote_dataZmpl_image_comparer   r   r   r   r   <module>   s   




m
((((
(
/