B
    q\*              #   @   s   d dl Z d dlZd dlmZ ddlmZ ddlm	Z	 ddgZ
dd	d
ddddddddddddddd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dS )    N)units   )BaseLowLevelWCS)HighLevelWCSMixincustom_ctype_to_ucd_mappingFITSWCSAPIMixinz	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em.freqz	em.energyzem.wavenumberzem.wlzspect.dopplerVeloc.radiozspect.dopplerVeloc.optzsrc.redshiftzspect.dopplerVeloczcustom:spect.doplerVeloc.betatime)"ZRAZDECZGLONZGLATZELONZELATZTLONZTLATZHPLTZHPLNZFREQZENERZWAVNZWAVEZVRADZVOPTZZOPTZAWAVZVELOZBETAZTIMEZTAIZTTZTDTZETZIATZUT1ZUTCZGMTZGPSZTCGZTCBZTDBZLOCALc               @   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 overriden::

        >>> 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   9lib/python3.7/site-packages/astropy/wcs/wcsapi/fitswcs.py__init__c   s    z$custom_ctype_to_ucd_mapping.__init__c             C   s   d S )Nr   )r   r   r   r   	__enter__g   s    z%custom_ctype_to_ucd_mapping.__enter__c             C   s   t | j d S )N)r	   remover   )r   typevaluetbr   r   r   __exit__j   s    z$custom_ctype_to_ucd_mapping.__exit__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   F   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dd Z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 )N)naxis)r   r   r   r   pixel_n_dimt   s    zFITSWCSAPIMixin.pixel_n_dimc             C   s   t | jjS )N)lenwcsctype)r   r   r   r   world_n_dimx   s    zFITSWCSAPIMixin.world_n_dimc             C   s*   | j ddgkrd S t| j d d d S d S )Nr   )_naxistuple)r   r   r   r   array_shape|   s    zFITSWCSAPIMixin.array_shapec             C   sP   |d krddg| _ n8t|| jkr8td| jt|t|d d d | _ d S )Nr   z9The number of data axes, {}, does not equal the shape {}.r    )r!   r   r   
ValueErrorformatlist)r   r   r   r   r   r#      s    c             C   s    | j ddgkrd S t| j S d S )Nr   )r!   r"   )r   r   r   r   pixel_shape   s    zFITSWCSAPIMixin.pixel_shapec             C   sF   |d krddg| _ n.t|| jkr8td| jt|t|| _ d S )Nr   z9The number of data axes, {}, does not equal the shape {}.)r!   r   r   r$   r%   r&   )r   r   r   r   r   r'      s    c             C   s   | j S )N)_pixel_bounds)r   r   r   r   pixel_bounds   s    zFITSWCSAPIMixin.pixel_boundsc             C   sB   |d kr|| _ n.t|| jkr4td| jt|t|| _ d S )NzJThe number of data axes, {}, does not equal the number of pixel bounds {}.)r(   r   r   r$   r%   r&   )r   r   r   r   r   r)      s    c             C   sf   g }x\| j D ]R}|dr&|d qx6tD ]}||kr,|||  P q,W |t|d  qW |S )NzUT(r   )axis_type_names
startswithappendr	   CTYPE_TO_UCD1get)r   typesZ	axis_typeZcustom_mappingr   r   r   world_axis_physical_types   s    

z)FITSWCSAPIMixin.world_axis_physical_typesc          	   C   s~   g }xt| j jD ]h}|d kr d}nLt|tjr:|jdd}n2yt|jdd}W n tjk
rj   d}Y nX || qW |S )N Zvounit)r%   )r   cunit
isinstanceuZUnitZ	to_stringZ
UnitsErrorr,   )r   r   unitr   r   r   world_axis_units   s    
z FITSWCSAPIMixin.world_axis_unitsc             C   s   | j rtj| j| jftdS | j dk}| jjd d dk}t	|d }xJ|D ]B}x<|D ]4}||kr\||  || O  < ||  || O  < q\W qRW |S )N)dtyper   i  
      )
Zhas_distortionnpZonesr   r   boolr   Zget_pcZ
axis_typesZnonzero)r   Zmatrix	celestialZcelestial_indicesZworld1Zworld2r   r   r   axis_correlation_matrix   s    

z'FITSWCSAPIMixin.axis_correlation_matrixc             G   s   | j |d S )Nr   )r   )all_pix2world)r   pixel_arraysr   r   r   pixel_to_world_values   s    z%FITSWCSAPIMixin.pixel_to_world_valuesc             G   s   | j |d d d d S )Nr    r   )r   )r>   )r   indicesr   r   r   array_index_to_world_values   s    z+FITSWCSAPIMixin.array_index_to_world_valuesc             G   s   | j |d S )Nr   )r   )all_world2pix)r   world_arraysr   r   r   world_to_pixel_values   s    z%FITSWCSAPIMixin.world_to_pixel_valuesc             G   s.   | j |d d d d }tdd |D }|S )Nr   r    c             s   s(   | ] }t jt |d  t jdV  qdS )g      ?)r7   N)r:   ZasarrayZfloorint).0Zpixelr   r   r   	<genexpr>   s    z>FITSWCSAPIMixin.world_to_array_index_values.<locals>.<genexpr>)r   )rC   r"   )r   rD   r?   Zarray_indicesr   r   r   world_to_array_index_values   s    z+FITSWCSAPIMixin.world_to_array_index_valuesc             C   s   |   d S )Nr   )_get_components_and_classes)r   r   r   r   world_axis_object_components   s    z,FITSWCSAPIMixin.world_axis_object_componentsc             C   s   |   d S )Nr   )rJ   )r   r   r   r   world_axis_object_classes   s    z)FITSWCSAPIMixin.world_axis_object_classesc             C   s   dS )NFr   )r   r   r   r   serialized_classes   s    z"FITSWCSAPIMixin.serialized_classesc             C   s  | j t| jjt| jj| jj| jj| jj| jj| jj	f}t
| dd d k	rj| j}|d |krd|d S d | _ddlm} ddlm} d g| j  }i }| jr|| }i }||d< tj|d< |d|f|d	< d
|| jj< d|| jj	< d| jkrtdt xt| j D ]t}	||	 d k r| j|	  }
|
dkr,d}
x|
|krD|
d7 }
q.W tjdd| jj|	 if||
< |
ddf||	<  qW |||ff| _||fS )N_components_and_classes_cacher   r   )wcs_to_celestial_frame)SkyCoordframer5   r   r<   )r<   r   zspherical.lon.degree)r<   r   zspherical.lat.degreer   zJIn future, times will be represented by the Time class instead of Quantityr1   Zworld_r   )r   r&   r   r   r2   ZradesysZequinoxZdateobsZlngZlatgetattrrN   Zastropy.wcs.utilsrO   Zastropy.coordinatesrP   Zhas_celestialr4   Zdegr0   warningswarnFutureWarningranger*   lowerZQuantity)r   Zwcs_hashcacherO   rP   Z
componentsclassesrQ   kwargsinamer   r   r   rJ     sN    





z+FITSWCSAPIMixin._get_components_and_classesN)r   r   r   r   propertyr   r   r#   setterr'   r)   r0   r6   r=   r@   rB   rE   rI   rK   rL   rM   rJ   r   r   r   r   r   n   s(   )rT   Znumpyr:   Zastropyr   r4   Zlow_level_apir   Zhigh_level_apir   __all__r-   r	   r   r   r   r   r   r   <module>   sT   (