
    IR-em.                     v    d dl Z d dlm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	 Zd
 Z G d de
          ZdS )    N)defaultdict
isiterable)lazyproperty   )BaseWCSWrappersanitize_slicesSlicedLowLevelWCSc                    t          | t          t          f          s| f} t          |           |k    rt	          d|  d| d          t          d | D                       rt          d          t          |           } t          | v r|                     t                    dk    rt          d          | 	                    t                    }| 
                    t                     |t          |           z
  }t          |          D ]*}||z   }|                     |t          d                     +t          |          D ]}|t          |           k     ri| |         }t          |t                    r"|j        r|j        dk    rt          d	          Tt          |t          j                  st          d
          ~|                     t          d                     | S )z
    Given a slice as input sanitise it to an easier to parse format.format.

    This function returns a list ``ndim`` long containing slice objects (or ints).
    z*The dimensionality of the specified slice z- can not be greater than the dimensionality (z) of the wcs.c              3   4   K   | ]}t          |          V  d S Nr   ).0ss     Flib/python3.11/site-packages/astropy/wcs/wcsapi/wrappers/sliced_wcs.py	<genexpr>z"sanitize_slices.<locals>.<genexpr>   s(      
)
)Q:a==
)
)
)
)
)
)    zBThis slice is invalid, only integer or range slices are supported.r   z0an index can only have a single ellipsis ('...')Nz)Slicing WCS with a step is not supported.z*Only integer or range slices are accepted.)
isinstancetuplelistlen
ValueErrorany
IndexErrorEllipsiscountindexremoverangeinsertslicestepnumbersIntegralappend)slicesndime_indn_eiindslcs          r   r	   r	      s    fudm,, 
6{{T< < <(,< < <
 
 	

 
)
)&
)
)
))) 
P
 
 	
 &\\F6<<!!A%%OPPP X&&hS[[ s 	, 	,A!)CMM#uT{{++++4[[ 	' 	's6{{??)C#u%% O8 RA$%PQQQW%566 O !MNNNO MM%++&&&&Mr   c                    t          | t                    r| j        t          d          t          |t                    r|j        t          d          t          |t          j                  r| j        |S || j        z   S | j        W| j        |S |j        t          |j        | j                  S t          |j        t          | j        |j                            S |j        | j        }n| j        |j        z   }|j        | j        }n:| j        |j        }n|j        | j        z   }| j        t          | j        |          }t          ||          S )z
    Given two slices that can be applied to a 1-d array, find the resulting
    slice that corresponds to the combination of both slices. We assume that
    slice2 can be an integer, but slice1 cannot.
    Nz)Only slices with steps of 1 are supported)	r   r    r!   r   r"   r#   startstopmin)slice1slice2r-   r.   s       r   combine_slicesr2   >   sN    &%   FV[%<DEEE&%   FV[%<DEEE&'*++ )<MFL((|;M{"V\6;777V\3v{FK+H+HIII<LEEL6</E;;DD|#{{V\1{&6;--r   c                   H   e Zd ZdZd Zed             Zed             Zed             Z	ed             Z
ed             Zed             Zed	             Zd
 Zd Zd Zed             Zed             Zed             Zed             Zed             Zed             ZdS )r
   a&  
    A Low Level WCS wrapper which applies an array slice to a WCS.

    This class does not modify the underlying WCS object and can therefore drop
    coupled dimensions as it stores which pixel and world dimensions have been
    sliced out (or modified) in the underlying WCS and returns the modified
    results on all the Low Level WCS methods.

    Parameters
    ----------
    wcs : `~astropy.wcs.wcsapi.BaseLowLevelWCS`
        The WCS to slice.
    slices : `slice` or `tuple` or `int`
        A valid array slice to apply to the WCS.

    c                 V    t          ||j                  }t          |t                    r|j         _        |j                                        }t          |j                  D ]K}|j        j        dz
  |j        |         z
  }|j        dz
  |z
  }t          ||         ||                   ||<   L| _        n| _        | _         j        d d d          _
        t          j         fdt           j        j                  D                       d          _        t          j         j        j        d d  j        f                             d                    d          _        t!           j                  dk    st!           j                  dk    rt#          d          d S )Nr   c                 \    g | ](}t          j        |         t          j                   )S  )r   _slices_pixelr"   r#   )r   ipselfs     r   
<listcomp>z.SlicedLowLevelWCS.__init__.<locals>.<listcomp>   sA        t1"5w7GHHH  r   r   )axisz[Cannot slice WCS: the resulting WCS should have at least one pixel and one world dimension.)r	   pixel_n_dimr   r
   _wcs_slices_arraycopyr   _pixel_keepr2   r8   npnonzeroaxis_correlation_matrixr   _world_keepr   r   )r:   wcsr%   slices_originalipixelipixel_orig
ipixel_news   `      r   __init__zSlicedLowLevelWCS.__init__{   s    99c,-- 	( DI!/4466O00  !h2Q69PP _q069
/=#K0&2D0 0,, "1DDI!'D!/"5 :   	 566  
 

  :I-aaa1A.ABFFAFNN
 

 t  A%%T-=)>)>!)C)C>   *D)Cr   c           
      h    | j         dgt          | j                  z   }t          t                    }t          | j        j                  D ]S| j        v rd|vrt                      |d<   | j        j
        }| j        j        |d                             |                    |d                             | j        j                            |d                             | j        j                            |d                             | j        j                            |d                                                 |d                             t          t#          fd|                                                               | j        |d	<   Ut          |          S )
zF
        Information describing the dropped world dimensions.
        r   world_axis_object_classesvalueworld_axis_namesworld_axis_physical_typesworld_axis_unitsworld_axis_object_componentsc                 4    | d                  d         k    S Nr   r7   )xr)   wao_componentss    r   <lambda>z<SlicedLowLevelWCS.dropped_world_dimensions.<locals>.<lambda>   s    QqT^A->q-A%A r   serialized_classes)_pixel_to_world_values_allr   rA   r   r   r   r>   world_n_dimrE   dictrM   rR   r$   rO   rP   rQ   updatefilteritemsrX   )r:   world_coordsdropped_infowao_classesr)   rV   s       @@r   dropped_world_dimensionsz*SlicedLowLevelWCS.dropped_world_dimensions   s   
 7t6c$BR>S>S8ST"4((ty,-- 	I 	IAD$$$*,>><@FF89)=K!YCN!((a999+,33DI4Nq4QRRR45<<	3A6   +,33DI4Nq4QRRR78??q@QRRR45<<AAAAA;CTCTCVCVWW   
 261HL-..L!!!r   c                 *    t          | j                  S r   )r   rA   r:   s    r   r=   zSlicedLowLevelWCS.pixel_n_dim       4#$$$r   c                 *    t          | j                  S r   )r   rE   rd   s    r   rZ   zSlicedLowLevelWCS.world_n_dim   re   r   c                 *      fd j         D             S )Nc                 4    g | ]}j         j        |         S r7   )r>   rP   r   r)   r:   s     r   r;   z?SlicedLowLevelWCS.world_axis_physical_types.<locals>.<listcomp>   s#    QQQ1	3A6QQQr   rE   rd   s   `r   rP   z+SlicedLowLevelWCS.world_axis_physical_types   s     QQQQ@PQQQQr   c                 *      fd j         D             S )Nc                 4    g | ]}j         j        |         S r7   )r>   rQ   ri   s     r   r;   z6SlicedLowLevelWCS.world_axis_units.<locals>.<listcomp>   #    HHH!	*1-HHHr   rj   rd   s   `r   rQ   z"SlicedLowLevelWCS.world_axis_units        HHHHt7GHHHHr   c                 *      fd j         D             S )Nc                 4    g | ]}j         j        |         S r7   )r>   pixel_axis_namesri   s     r   r;   z6SlicedLowLevelWCS.pixel_axis_names.<locals>.<listcomp>   rm   r   )rA   rd   s   `r   rq   z"SlicedLowLevelWCS.pixel_axis_names   rn   r   c                 *      fd j         D             S )Nc                 4    g | ]}j         j        |         S r7   )r>   rO   ri   s     r   r;   z6SlicedLowLevelWCS.world_axis_names.<locals>.<listcomp>   rm   r   rj   rd   s   `r   rO   z"SlicedLowLevelWCS.world_axis_names   rn   r   c                    t          t          t          j        |                    }g }d}t	          | j        j                  D ]}t          | j        |         t          j
                  r!|                    | j        |                    H|dz  }| j        |         j        /|                    ||         | j        |         j        z              |                    ||                    t          j        | } | j        j        | S )Nr5   r   )r   maprB   
asanyarrayr   r>   r=   r   r8   r"   r#   r$   r-   broadcast_arrayspixel_to_world_values)r:   pixel_arrayspixel_arrays_new	ipix_curripixs        r   rY   z,SlicedLowLevelWCS._pixel_to_world_values_all   s   S==>>	$)/00 
	E 
	ED$,T2G4DEE 	E ''(:4(@AAAAQ	%d+1=$++$Y/$2DT2J2PP    %++L,CDDDD.0@A.ty.0@AAr   c                      | j         | t          t          j                  r	j        sS | j        j        dk    r&fd| j        D             | j        dk    rd         S )Nr   c                      g | ]
}|         S r7   r7   )r   iwworld_arrayss     r   r;   z;SlicedLowLevelWCS.pixel_to_world_values.<locals>.<listcomp>   s    HHHL,HHHr   r   )rY   r   rB   ndarrayshaper>   rZ   rE   )r:   ry   r   s     @r   rx   z'SlicedLowLevelWCS.pixel_to_world_values   s    6t6E lBJ// 	 8J 	 9 1$$HHHHt7GHHHL1$$+Ar   c                 \    | j         dgt          | j                  z   }t          t	          t
          j        |                    }g }d}t          | j        j	                  D ]G}|| j
        v r!|dz  }|                    ||                    ,|                    ||                    Ht          j        | }t           | j        j        |           t          | j        j                  D ]T}t!          | j        |         t$                    r2| j        |         j         |xx         | j        |         j        z  cc<   Ut!          t
          j                  r	j        sS t          fd| j        D                       }| j        dk    r| j        j        dk    r|d         }|S )Nr   r5   r   c              3   (   K   | ]}|         V  d S r   r7   )r   r9   ry   s     r   r   z:SlicedLowLevelWCS.world_to_pixel_values.<locals>.<genexpr>  s(      BB2l2&BBBBBBr   )rY   r   rA   r   ru   rB   rv   r   r>   rZ   rE   r$   rw   r   world_to_pixel_valuesr=   r   r8   r    r-   r   r   )	r:   r   sliced_out_world_coordsworld_arrays_newiworld_curriworldrH   pixelry   s	           @r   r   z'SlicedLowLevelWCS.world_to_pixel_values   s   "A$"AS3t'(((#
 S==>>DI122 	I 	IF)))q  ''[(ABBBB ''(?(GHHHH.0@A;DI;=MNOODI122 	I 	IF4-f5u==I&v.4@V$$$(:6(B(HH$$$ lBJ// 	 8J 	 BBBB1ABBBBBq  TY%:Q%>%>!HEr   c                 *      fd j         D             S )Nc                 4    g | ]}j         j        |         S r7   )r>   rR   )r   idxr:   s     r   r;   zBSlicedLowLevelWCS.world_axis_object_components.<locals>.<listcomp>#  s#    XXX	6s;XXXr   rj   rd   s   `r   rR   z.SlicedLowLevelWCS.world_axis_object_components!  s     XXXXtGWXXXXr   c                     d | j         D             t          fd| j        j                                        D                       S )Nc                     g | ]
}|d          S r   r7   )r   items     r   r;   z?SlicedLowLevelWCS.world_axis_object_classes.<locals>.<listcomp>'  s    KKKT!WKKKr   c                 (    g | ]}|d          v |S r   r7   )r   r   	keys_keeps     r   r;   z?SlicedLowLevelWCS.world_axis_object_classes.<locals>.<listcomp>)  s2       7i'' '''r   )rR   r[   r>   rM   r^   )r:   r   s    @r   rM   z+SlicedLowLevelWCS.world_axis_object_classes%  sb    KK)JKKK	    I?EEGG  
 
 	
r   c                     | j         j        r<t          j        d| j         j                  t	          | j                           j        S d S rT   )r>   array_shaperB   broadcast_tor   r?   r   rd   s    r   r   zSlicedLowLevelWCS.array_shape0  sH    9  	?1di&;<<d())	 	r   c                 N    | j         rt          | j         d d d                   S d S )Nr5   )r   r   rd   s    r   pixel_shapezSlicedLowLevelWCS.pixel_shape7  s3     	1)$$B$/000	1 	1r   c                 L   | j         j        d S g }| j        D ]~}| j        |         j        &|                    | j         j        |                    :| j         j        |         \  }}| j        |         j        }|                    ||z
  ||z
  f           t          |          S r   )r>   pixel_boundsrA   r8   r-   r$   r   )r:   boundsr   iminimaxr-   s         r   r   zSlicedLowLevelWCS.pixel_bounds<  s    9!)F# 	< 	<C!#&,4di4S9::::!Y3C8
d*3/5te|TE\:;;;;V}}r   c                 N    | j         j        | j                 d d | j        f         S r   )r>   rD   rE   rA   rd   s    r   rD   z)SlicedLowLevelWCS.axis_correlation_matrixL  s&    y01AB111dFVCVWWr   N)__name__
__module____qualname____doc__rK   r   rb   propertyr=   rZ   rP   rQ   rq   rO   rY   rx   r   rR   rM   r   r   r   rD   r7   r   r   r
   r
   i   s        "' ' 'R " " \"@ % % X% % % X% R R XR I I XI I I XI I I XIB B B&     B Y Y XY 
 
 X
   X 1 1 X1   X X X XX X Xr   )r"   collectionsr   numpyrB   astropy.utilsr   astropy.utils.decoratorsr   baser   __all__r	   r2   r
   r7   r   r   <module>r      s     # # # # # #     $ $ $ $ $ $ 1 1 1 1 1 1            1
2- - -`( ( (VeX eX eX eX eX eX eX eX eX eXr   