U
    e»{jLv  ã                .   @   s  d dl Z d dlZd dlmZ d dlmZ d dlm	Z	m
Z
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dgZejjZddddddœZe
d ej d ej d ej d ej ej dej ej d ej ej ddd�ed< e
d ej d ej d ej d ej ej dej ej d ej ej ddd�ed< e
dej dej d ej de  ejej ¡ d�ed< dd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d4d8d9d:d;d;d;d;d;d;d;d;d;d;d;d;d;d;d<d=d>œ-Z g Z!G d?d„ dƒZ"G d@d„ deeƒZ#G dAd„ deeƒZ$dS )Bé    N)Úunits)Úc)ÚICRSÚGalacticÚSpectralCoord)Úattach_zero_velocitiesÚupdate_differentials_to_match)ÚAstropyUserWarningé   )ÚHighLevelWCSMixin)ÚBaseLowLevelWCS)ÚSlicedLowLevelWCSÚcustom_ctype_to_ucd_mappingÚSlicedFITSWCSÚFITSWCSAPIMixinZgcrsZicrsZhcrsZlsrkZlsrd)ZGEOCENTZBARYCENTZ	HELIOCENTZLSRKZLSRDi$ÿÿÿZ	cartesian)ÚuÚvÚwÚUÚVÚWZrepresentation_typeZdifferential_typeZGALACTOCiÔþÿÿZLOCALGRPgš™™™™}p@g      H@g¹vð¿$T?)ÚlÚbÚdistanceZradial_velocityZCMBDIPOLz	pos.eq.raz
pos.eq.deczpos.galactic.lonzpos.galactic.latzpos.ecliptic.lonzpos.ecliptic.latzpos.bodyrc.lonzpos.bodyrc.latzcustom:pos.helioprojective.latzcustom:pos.helioprojective.lonzcustom:pos.helioprojective.zz&custom:pos.heliographic.stonyhurst.lonz&custom:pos.heliographic.stonyhurst.latz&custom:pos.heliographic.carrington.lonz&custom:pos.heliographic.carrington.latzcustom:pos.heliocentric.xzcustom:pos.heliocentric.yzcustom:pos.heliocentric.zzem.freqz	em.energyzem.wavenumberzem.wlzspect.dopplerVeloc.radiozspect.dopplerVeloc.optzsrc.redshiftzspect.dopplerVeloczcustom:spect.doplerVeloc.betazphys.polarization.stokesÚtimezpos.distancez!custom:pos.distance.sunToObserver)-ZRAZDECZGLONZGLATZELONZELATZTLONZTLATZHPLTZHPLNZHPRZZHGLNZHGLTZCRLNZCRLTZSOLXZSOLYZSOLZZFREQZENERZWAVNZWAVEÚVRADÚVOPTÚZOPTZAWAVÚVELOÚBETAZSTOKESZTIMEZTAIZTTZTDTÚETZIATZUT1ÚUTCÚGMTZGPSZTCGZTCBZTDBZLOCALÚDISTZDSUNc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r   aû  
    A context manager that makes it possible to temporarily add new CTYPE to
    UCD1+ mapping used by :attr:`FITSWCSAPIMixin.world_axis_physical_types`.

    Parameters
    ----------
    mapping : dict
        A dictionary mapping a CTYPE value to a UCD1+ value

    Examples
    --------

    Consider a WCS with the following CTYPE::

        >>> from astropy.wcs import WCS
        >>> wcs = WCS(naxis=1)
        >>> wcs.wcs.ctype = ['SPAM']

    By default, :attr:`FITSWCSAPIMixin.world_axis_physical_types` returns `None`,
    but this can be overridden::

        >>> wcs.world_axis_physical_types
        [None]
        >>> with custom_ctype_to_ucd_mapping({'SPAM': 'food.spam'}):
        ...     wcs.world_axis_physical_types
        ['food.spam']
    c                 C   s   t  d|¡ || _d S ©Nr   )ÚCTYPE_TO_UCD1_CUSTOMÚinsertÚmapping)Úselfr'   © r)   úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/wcs/wcsapi/fitswcs.pyÚ__init__Â   s    z$custom_ctype_to_ucd_mapping.__init__c                 C   s   d S ©Nr)   ©r(   r)   r)   r*   Ú	__enter__Æ   s    z%custom_ctype_to_ucd_mapping.__enter__c                 C   s   t  | j¡ d S r,   )r%   Úremover'   )r(   ÚtypeÚvalueÚtbr)   r)   r*   Ú__exit__É   s    z$custom_ctype_to_ucd_mapping.__exit__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r.   r3   r)   r)   r)   r*   r   ¥   s   c                   @   s   e Zd ZdS )r   N)r4   r5   r6   r)   r)   r)   r*   r   Í   s   c                   @   sâ   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZejdd„ ƒZed	d
„ ƒZ	e	jdd
„ ƒZ	edd„ ƒZ
e
j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edd „ ƒZd!d"„ Zd#S )$r   z‘
    A mix-in class that is intended to be inherited by the
    :class:`~astropy.wcs.WCS` class and provides the low- and high-level WCS API
    c                 C   s   | j S r,   )Únaxisr-   r)   r)   r*   Úpixel_n_dim×   s    zFITSWCSAPIMixin.pixel_n_dimc                 C   s   t | jjƒS r,   )ÚlenÚwcsÚctyper-   r)   r)   r*   Úworld_n_dimÛ   s    zFITSWCSAPIMixin.world_n_dimc                 C   s"   | j d krd S | j d d d… S d S ©Néÿÿÿÿ©Úpixel_shaper-   r)   r)   r*   Úarray_shapeß   s    
zFITSWCSAPIMixin.array_shapec                 C   s$   |d krd | _ n|d d d… | _ d S r>   r@   ©r(   r1   r)   r)   r*   rB   æ   s    c                 C   s    | j ddgkrd S t| j ƒS d S r$   )Ú_naxisÚtupler-   r)   r)   r*   rA   í   s    zFITSWCSAPIMixin.pixel_shapec                 C   sL   |d krddg| _ n4t|ƒ| jkr>td| j› dt|ƒ› d�ƒ‚t|ƒ| _ d S )Nr   úThe number of data axes, z, does not equal the shape Ú.)rD   r:   r8   Ú
ValueErrorÚlistrC   r)   r)   r*   rA   ô   s    ÿc                 C   s   | j S r,   )Ú_pixel_boundsr-   r)   r)   r*   Úpixel_bounds   s    zFITSWCSAPIMixin.pixel_boundsc                 C   sH   |d kr|| _ n4t|ƒ| jkr:td| j› dt|ƒ› d�ƒ‚t|ƒ| _ d S )NrF   z,, does not equal the number of pixel bounds rG   )rJ   r:   r8   rH   rI   rC   r)   r)   r*   rK     s    ÿc                 C   sx   g }| j jD ]f}| ¡  d¡r*| d¡ q| d¡d }tD ]}||kr<| || ¡  qq<| t | ¡ d ¡¡ q|S )N)zUT(zTT(r   ú-r   )	r;   r<   ÚupperÚ
startswithÚappendÚsplitr%   ÚCTYPE_TO_UCD1Úget)r(   Útypesr<   Z
ctype_nameZcustom_mappingr)   r)   r*   Úworld_axis_physical_types  s    z)FITSWCSAPIMixin.world_axis_physical_typesc              	   C   sz   g }| j jD ]h}|d krd}nLt|tjƒr8|jdd�}n2zt |¡jdd�}W n tjk
rh   d}Y nX | |¡ q|S )NÚ Zvounit©Úformat)r;   ÚcunitÚ
isinstancer   ZUnitZ	to_stringZ
UnitsErrorrO   )r(   r   Úunitr)   r)   r*   Úworld_axis_units"  s    
z FITSWCSAPIMixin.world_axis_unitsc                 C   s   t | jjƒS r,   )rI   r;   Úcnamer-   r)   r)   r*   Úworld_axis_names2  s    z FITSWCSAPIMixin.world_axis_namesc                 C   s”   | j rtj| j| jftd�S | j ¡ dk}| jjd d dk}t 	|¡d }|D ]>}|D ]4}||krX||  || O  < ||  || O  < qXqP|S )N)Zdtyper   éè  é
   é   )
Zhas_distortionÚnpZonesr=   r9   Úboolr;   Zget_pcZ
axis_typesZnonzero)r(   ÚmatrixÚ	celestialZcelestial_indicesZworld1Zworld2r)   r)   r*   Úaxis_correlation_matrix6  s    z'FITSWCSAPIMixin.axis_correlation_matrixc                 G   s(   | j |džŽ }| jdkr |d S t|ƒS )Nr   r
   )r   )Zall_pix2worldr=   rE   )r(   Zpixel_arraysÚworldr)   r)   r*   Úpixel_to_world_valuesP  s    z%FITSWCSAPIMixin.pixel_to_world_valuesc              
      s†   ddl m} z| j|džŽ }W nL |k
rj ‰  z.t tˆ ƒ¡ | j‡ fdd„df|džŽ }W 5 d ‰ Š X Y nX | jdkr~|d S t|ƒS )	Nr   )ÚNoConvergencec                     s   ˆ j S r,   )Zbest_solution)Úargs©Úer)   r*   Ú<lambda>_  ó    z7FITSWCSAPIMixin.world_to_pixel_values.<locals>.<lambda>Úinputr
   )r   )r   )	Zastropy.wcs.wcsrh   Zall_world2pixÚwarningsÚwarnÚstrZ_array_converterr9   rE   )r(   Zworld_arraysrh   Zpixelr)   rj   r*   Úworld_to_pixel_valuesT  s    
 ÿ  ÿz%FITSWCSAPIMixin.world_to_pixel_valuesc                 C   s   |   ¡ d S r$   ©Ú_get_components_and_classesr-   r)   r)   r*   Úworld_axis_object_componentsd  s    z,FITSWCSAPIMixin.world_axis_object_componentsc                 C   s   |   ¡ d S )Nr
   rs   r-   r)   r)   r*   Úworld_axis_object_classesh  s    z)FITSWCSAPIMixin.world_axis_object_classesc                 C   s   dS )NFr)   r-   r)   r)   r*   Úserialized_classesl  s    z"FITSWCSAPIMixin.serialized_classesc           "   	      s¾  ˆj tˆjjƒtˆjjƒˆjjˆjjˆjjˆjjˆjj	ˆjj
f	}tˆdd ƒd k	rpˆj}|d |krj|d S d ˆ_ddlm}m} ddlm‰ m‰ ddlm} ddlm} d gˆj  }i }ˆj�r"z|ˆƒ}	W n tk
rä   d }	Y n>X i ‰|	ˆd< tjˆd	< |d
ˆf|d< d|ˆjj	< d|ˆjj
< ˆj�r¾ˆjj}
ˆjj|
 d d… }| ¡ }i ‰t ˆjj d ¡�rld ‰nò|ˆjj d d… d	tj!iŽ}ˆjj"�p”d}t ˆjj#¡�r¼ˆ ˆjj$d||d�}nˆ ˆjj#d||d�}||j%|d�ƒ}ˆjjt&k�r2t&ˆjj }| '|¡‰t(|t)ƒ�rt*ˆƒ‰nt+|t&ˆjj dd�‰n,ˆjjdk�rJt*|ƒ‰nt,dˆjj› d�ƒ‚ˆj�rÈ|	d k	�rÈ|ˆjj-ˆjj	 ˆjjˆjj	  ˆjj-ˆjj
 ˆjjˆjj
  |	dtj. d�‰t*ˆƒ‰nd ‰ˆd k	�rzˆ 't/ƒ ¡ W n& t0k
�r   t1 2dt3¡ d ‰Y nX ˆd k	�rPzˆ 't/ƒ ¡ W n& t0k
�rN   t1 2dt3¡ d ‰Y nX |dk�r ‡‡‡fdd„}‡‡fd d!„}tj4d
i |f|d"< d"d|f|ˆjj< �n|d#k�rî‡‡‡fd$d%„}‡‡fd&d'„}tj4d
i |f|d"< d"d|f|ˆjj< nÐˆjj|
 ˆd	< ˆjj5dk�r||d(k�r2d)ˆd*< ˆjj5tj6 ˆd+< nJ|d,k�rXd-ˆd*< ˆjj5tj6 ˆd+< n$|d.k�r|d/ˆd*< ˆjj7tj! ˆd+< ‡‡‡fd0d1„}‡‡fd2d3„}tj4d
i |f|d"< d"d|f|ˆjj< d4ˆj8k�r ˆj8 9d4¡dk}t:ˆj ƒD �]8}ˆj8| d4k�rä|�rd5|› �}nd4}d }ˆjj|  ;¡ }|d4k�rJˆjj"�rFˆjj" ;¡ }nd}d6|k�rœ| <d6¡}|d |… ||d d7…  }}t1 2d8| ¡ › d9| ¡ › �t=¡ |d:k�r¸ˆd;d<d=�}d>}n>| ¡ |k�rÔ|| ¡  }n"|ˆ j>k�rötd?ˆjj| › �ƒ‚ˆjj? ;¡ }| @d@¡�r\t At ˆjj d d… ¡¡�r>t1 2dAt=¡ d }n|ˆjj d d… d	tj!iŽ}nF|dBk�rz|dddtj!dC�}n(|dDk�rŠd }nt1 2dE|› dF�t=¡ d }ˆ t BˆjjCd ¡t BˆjjCd ¡d||d�‰|d k	�râˆ| ‰‡ ‡‡fdGdH„} ‡fdIdJ„}!ˆ d
i | f||< |d|!f||< �qät:ˆj ƒD ]|}|| d k�r*ˆjj|  DdK¡d  ;¡ }|dDk�rddL}||k�rz|dM7 }�qdtj4d
d	ˆjj| if||< |ddNf||< �q*|||ffˆ_||fS )ONÚ_components_and_classes_cacher   r
   )ÚEarthLocationÚSkyCoord)ÚTimeÚ	TimeDelta)ÚFITS_DEPRECATED_SCALES)Úwcs_to_celestial_frameÚframerZ   r)   rd   )rd   r   zspherical.lon.degree)rd   r
   zspherical.lat.degreeé   é   ÚutcZmjd)rW   ÚscaleÚlocation)ÚobstimeT)Zpreserve_observer_frameZTOPOCENTzSPECSYS=z not yet supportedr^   )r   r   zIobserver cannot be converted to ICRS, so will not be set on SpectralCoordzGtarget cannot be converted to ICRS, so will not be set on SpectralCoordr   c                    s,   t | tƒr| S t| d ˆjj tjˆ ˆd�S )Nr
   )rZ   ÚobserverÚtarget)rY   r   r;   Úrestwavr   Úm)Zredshift©r†   r(   r‡   r)   r*   Úspectralcoord_from_redshift  s    
üzPFITSWCSAPIMixin._get_components_and_classes.<locals>.spectralcoord_from_redshiftc                    sŠ   ˆ d ks| j d ks| jd krhˆ d kr*d}n| j d kr:d}nd}t |› d�t¡ |  tj¡ˆjj	 d S |  
ˆ ¡ tj¡ˆjj	 d S d S )NúNo observer defined on WCSú$No observer defined on SpectralCoordú"No target defined on SpectralCoordúC, SpectralCoord will be converted without any velocity frame changeg      ð?)r†   r‡   ro   rp   r	   Úto_valuer   r‰   r;   rˆ   Ú$with_observer_stationary_relative_to©ÚspectralcoordÚmsg©r†   r(   r)   r*   Úredshift_from_spectralcoord)  s4    ÿþý
üÿþýüÿzPFITSWCSAPIMixin._get_components_and_classes.<locals>.redshift_from_spectralcoordZspectralr   c                    s8   t | tƒr| S t| t tjtj dˆjjtj ˆ ˆd�S )NÚrelativistic)rZ   Údoppler_conventionÚdoppler_restr†   r‡   )rY   r   ÚC_SIr   r‰   Úsr;   rˆ   )ÚbetarŠ   r)   r*   Úspectralcoord_from_betaL  s    

úzLFITSWCSAPIMixin._get_components_and_classes.<locals>.spectralcoord_from_betac                    sž   t  ˆjjt j ¡}ˆ d ks0| jd ks0| jd kr|ˆ d kr>d}n| jd krNd}nd}t |› d�t	¡ |  
t jt j |¡t S |  ˆ ¡ 
t jt j |¡t S d S ©NrŒ   r�   rŽ   r�   )r   Zdoppler_relativisticr;   rˆ   r‰   r†   r‡   ro   rp   r	   r�   r›   rš   r‘   )r“   Zdoppler_equivr”   r•   r)   r*   Úbeta_from_spectralcoordX  s4    ÿþý
üÿ
 þýÿzLFITSWCSAPIMixin._get_components_and_classes.<locals>.beta_from_spectralcoordr   r—   r˜   r™   r   Úradior   Zopticalc                    s$   t | tƒr| S t| fˆˆdœˆ —ŽS )N)r†   r‡   )rY   r   )r1   )Úkwargsr†   r‡   r)   r*   Úspectralcoord_from_value‡  s    
ÿ ÿÿzMFITSWCSAPIMixin._get_components_and_classes.<locals>.spectralcoord_from_valuec                    sr   ˆd ks| j d ks| jd kr\ˆd kr*d}n| j d kr:d}nd}t |› d�t¡ | jf ˆ ŽS |  ˆ¡jf ˆ ŽS d S rž   )r†   r‡   ro   rp   r	   r�   r‘   r’   )r¡   r†   r)   r*   Úvalue_from_spectralcoordŽ  s*    ÿþý
üÿþzMFITSWCSAPIMixin._get_components_and_classes.<locals>.value_from_spectralcoordr   ztime.ú(r?   zDropping unsupported sub-scale z from scale Zgpsé   ÚsecrV   ZtaizUnrecognized time CTYPE=ZtopocentzUMissing or incomplete observer location information, setting location in Time to NoneZ	geocenter)rZ   rU   zObservation location 'z4' is not supported, setting location in Time to Nonec                    s   t | ˆ ƒr| S ˆˆ| dd� S )Nr¦   rV   )rY   )Úoffset)r{   r|   Úreference_timer)   r*   Útime_from_reference_and_offset   s    
zSFITSWCSAPIMixin._get_components_and_classes.<locals>.time_from_reference_and_offsetc                    s
   | ˆ  j S r,   )r¦   )r   )r¨   r)   r*   Úoffset_from_time_and_reference  s    zSFITSWCSAPIMixin._get_components_and_classes.<locals>.offset_from_time_and_referencerL   rf   Ú_r1   )Er8   rI   r;   r<   rX   ZradesysZspecsysZequinoxZdateobsZlngZlatÚgetattrrx   Úastropy.coordinatesry   rz   Zastropy.timer{   r|   Zastropy.time.formatsr}   Zastropy.wcs.utilsr~   Zhas_celestialrH   r   ÚdegZhas_spectralÚspecrM   ra   ÚisnanZobsgeor‰   ZtimesysZmjdavgZmjdobsZget_itrsÚVELOCITY_FRAMESZtransform_torY   rq   r   r   ÚNotImplementedErrorZcrvalZkpcr   Ú	Exceptionro   rp   r	   ZQuantityZrestfrqZHzrˆ   rT   ÚcountÚrangeÚlowerÚindexÚUserWarningZSCALESÚtrefposrN   ÚanyZ
nan_to_numZmjdrefrP   )"r(   Zwcs_hashÚcachery   rz   r}   r~   Ú
componentsÚclassesZcelestial_frameZispecr<   Zearth_locationZtscaler…   Zobserver_locationr   r‹   r–   r�   rŸ   r¢   r£   Zmultiple_timeÚiÚnameZreference_time_deltarƒ   ÚposZsubscaler¹   r„   r©   rª   r)   )r{   r|   r¡   r†   r¨   r(   r‡   r*   rt   p  s~   

÷

üü


ý
ÿü

ý

ý










ý
ý


ýû


z+FITSWCSAPIMixin._get_components_and_classesN)r4   r5   r6   r7   Úpropertyr9   r=   rB   ÚsetterrA   rK   rT   r[   r]   re   rg   rr   ru   rv   rw   rt   r)   r)   r)   r*   r   Ñ   sD   














)%ro   Únumpyra   Zastropyr   r   Zastropy.constantsr   r­   r   r   r   Z'astropy.coordinates.spectral_coordinater   r   Zastropy.utils.exceptionsr	   Zhigh_level_apir   Zlow_level_apir   Zwrappersr   Ú__all__Úsir1   rš   r±   Úkmr›   r®   ÚtorQ   r%   r   r   r   r)   r)   r)   r*   Ú<module>   sº   
ûø
ø
ü
Ï7(