
    IR-ex                        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 g dZej        j        ZddddddZ e
d ej        z  d ej        z  d ej        z  d ej        z  ej        z  dej        z  ej        z  d ej        z  ej        z  dd          ed<    e
d ej        z  d ej        z  d ej        z  d ej        z  ej        z  dej        z  ej        z  d ej        z  ej        z  dd          ed<    e
dej        z  dej        z  d ej        z  dez                      ej        ej        z                       ed<   i dddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>i d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdHdPdQdRdSdTdUdVdWdXdWdYdWdZdWd[dWdWdWdWdWdWdWdWdWdWd\d]d^Z g Z! G d_ d`          Z" G da dbee          Z# G dc ddee          Z$dS )e    N)units)c)ICRSGalacticSpectralCoord)attach_zero_velocitiesupdate_differentials_to_match)AstropyUserWarning   )HighLevelWCSMixin)BaseLowLevelWCS)SlicedLowLevelWCS)custom_ctype_to_ucd_mappingSlicedFITSWCSFITSWCSAPIMixingcrsicrshcrslsrklsrd)GEOCENTBARYCENT	HELIOCENTLSRKLSRDi$	cartesian)uvwUVWrepresentation_typedifferential_typeGALACTOCiLOCALGRPg}p@g      H@gv$T?)lbdistanceradial_velocityCMBDIPOLRAz	pos.eq.raDECz
pos.eq.decGLONzpos.galactic.lonGLATzpos.galactic.latELONzpos.ecliptic.lonELATzpos.ecliptic.latTLONzpos.bodyrc.lonTLATzpos.bodyrc.latHPLTzcustom:pos.helioprojective.latHPLNzcustom:pos.helioprojective.lonHPRZzcustom:pos.helioprojective.zHGLNz&custom:pos.heliographic.stonyhurst.lonHGLTz&custom:pos.heliographic.stonyhurst.latCRLNz&custom:pos.heliographic.carrington.lonCRLTz&custom:pos.heliographic.carrington.latSOLXzcustom:pos.heliocentric.xSOLYzcustom:pos.heliocentric.ySOLZzcustom:pos.heliocentric.zFREQzem.freqENERz	em.energyWAVNzem.wavenumberWAVEzem.wlVRADzspect.dopplerVeloc.radioVOPTzspect.dopplerVeloc.optZOPTzsrc.redshiftAWAVVELOzspect.dopplerVelocBETAzcustom:spect.doplerVeloc.betaSTOKESphys.polarization.stokesTIMEtimeTAITTTDTETzpos.distancez!custom:pos.distance.sunToObserver)IATUT1UTCGMTGPSTCGTCBTDBLOCALDISTDSUNc                   $    e Zd ZdZd Zd Z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                 J    t                               d|           || _        d S Nr   )CTYPE_TO_UCD1_CUSTOMinsertmapping)selfr`   s     :lib/python3.11/site-packages/astropy/wcs/wcsapi/fitswcs.py__init__z$custom_ctype_to_ucd_mapping.__init__   s#    ##Aw///    c                     d S N ra   s    rb   	__enter__z%custom_ctype_to_ucd_mapping.__enter__   s    rd   c                 D    t                               | j                   d S rf   )r^   remover`   )ra   typevaluetbs       rb   __exit__z$custom_ctype_to_ucd_mapping.__exit__   s    ##DL11111rd   N)__name__
__module____qualname____doc__rc   ri   ro   rg   rd   rb   r   r      sK         6    2 2 2 2 2rd   r   c                       e Zd ZdS )r   N)rp   rq   rr   rg   rd   rb   r   r      s        Drd   r   c                      e Zd ZdZed             Zed             Zed             Zej        d             Zed             Z	e	j        d             Z	ed             Z
e
j        d	             Z
ed
             Zed             Zed             Zed             Zd Zd Zed             Zed             Zed             Z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                     | j         S rf   )naxisrh   s    rb   pixel_n_dimzFITSWCSAPIMixin.pixel_n_dim   s
    zrd   c                 4    t          | j        j                  S rf   )lenwcsctyperh   s    rb   world_n_dimzFITSWCSAPIMixin.world_n_dim   s    48>"""rd   c                 4    | j         d S | j         d d d         S Npixel_shaperh   s    rb   array_shapezFITSWCSAPIMixin.array_shape   s$    #4#DDbD))rd   c                 <    |	d | _         d S |d d d         | _         d S r   r   ra   rm   s     rb   r   zFITSWCSAPIMixin.array_shape   s-    =#D$TTrT{Drd   c                 H    | j         ddgk    rd S t          | j                   S r]   )_naxistuplerh   s    rb   r   zFITSWCSAPIMixin.pixel_shape   s(    ;1a&  4%%%rd   c                     |ddg| _         d S t          |          | j        k    r(t          d| j         dt          |           d          t	          |          | _         d S )Nr   The number of data axes, z, does not equal the shape .)r   rz   rw   
ValueErrorlistr   s     rb   r   zFITSWCSAPIMixin.pixel_shape   sy    =a&DKKK5zzTZ'' &
 & &E

& & &   u++DKKKrd   c                     | j         S rf   )_pixel_boundsrh   s    rb   pixel_boundszFITSWCSAPIMixin.pixel_bounds   s    !!rd   c                     |	|| _         d S t          |          | j        k    r(t          d| j         dt          |           d          t	          |          | _         d S )Nr   z,, does not equal the number of pixel bounds r   )r   rz   rw   r   r   r   s     rb   r   zFITSWCSAPIMixin.pixel_bounds  s|    =!&D5zzTZ'' 2z2 2$'JJ2 2 2  
 "&eDrd   c                    g }| j         j        D ]}|                                                    d          r|                    d           ?|                    d          d         }t          D ]#}||v r|                    ||                     nA$|                    t                              |                                d                      |S )N)zUT(zTT(rK   -r   )	r{   r|   upper
startswithappendsplitr^   CTYPE_TO_UCD1get)ra   typesr|   
ctype_namecustom_mappings        rb   world_axis_physical_typesz)FITSWCSAPIMixin.world_axis_physical_types  s    X^ 
	N 
	NE{{}}''77 	NV$$$$"[[--a0
&: N NN!^33^J%?@@@ 4 LL!2!2:3C3C3E3Et!L!LMMMrd   c                 @   g }| j         j        D ]}|d}nrt          |t          j                  r|                    d          }nA	 t          j        |                              d          }n# t          j        $ r d}Y nw xY w|                    |           |S )N vounitformat)r{   cunit
isinstancer   Unit	to_string
UnitsErrorr   )ra   r   units      rb   world_axis_unitsz FITSWCSAPIMixin.world_axis_units!  s    HN 
	 
	D|D!&)) ~~X~666$<<111BBDD|   DDDLLs   (A11BBc                 4    t          | j        j                  S rf   )r   r{   cnamerh   s    rb   world_axis_namesz FITSWCSAPIMixin.world_axis_names1  s    DHN###rd   c                 t   | j         r't          j        | j        | j        ft
                    S | j                                        dk    }| j        j        dz  dz  dk    }t          j	        |          d         }|D ]9}|D ]4}||k    r,||xx         ||         z  cc<   ||xx         ||         z  cc<   5:|S )N)dtyper     
      )
has_distortionnponesr}   rx   boolr{   get_pc
axis_typesnonzero)ra   matrix	celestialcelestial_indicesworld1world2s         rb   axis_correlation_matrixz'FITSWCSAPIMixin.axis_correlation_matrix5  s    
  	M7D,d.>?tLLLL ""a' X(D0B6!;	Jy11!4' 	5 	5F+ 5 5V##6NNNfVn4NNN6NNNfVn4NNN5
 rd   c                 d     | j         g |dR  }| j        dk    r|d         nt          |          S )Nr   r   )all_pix2worldr}   r   )ra   pixel_arraysworlds      rb   pixel_to_world_valuesz%FITSWCSAPIMixin.pixel_to_world_valuesO  s@    ""4L4!444+q00uQxxeEllBrd   c                    ddl m} 	  | j        g |dR  }nG# |$ r?t          j        t                                | j        fddg|dR  }Y d nd ww xY w| j        dk    r|d         nt          |          S )Nr   )NoConvergencec                      j         S rf   )best_solution)argses    rb   <lambda>z7FITSWCSAPIMixin.world_to_pixel_values.<locals>.<lambda>^  s	    ao rd   inputr   )	astropy.wcs.wcsr   all_world2pixwarningswarnstr_array_converterrx   r   )ra   world_arraysr   pixelr   s       @rb   world_to_pixel_valuesz%FITSWCSAPIMixin.world_to_pixel_valuesS  s    111111	&D&88a888EE 	 	 	M#a&&!!! *D)----w9EGH  EEEEEE		  +q00uQxxeEllBs    A5AAc                 6    |                                  d         S r]   _get_components_and_classesrh   s    rb   world_axis_object_componentsz,FITSWCSAPIMixin.world_axis_object_componentsc      //11!44rd   c                 6    |                                  d         S )Nr   r   rh   s    rb   world_axis_object_classesz)FITSWCSAPIMixin.world_axis_object_classesg  r   rd   c                     dS )NFrg   rh   s    rb   serialized_classesz"FITSWCSAPIMixin.serialized_classesk  s    urd   c           	      *   #$%&'(  j         t           j        j                  t           j        j                   j        j         j        j         j        j         j        j         j        j	         j        j
        f	}t           dd           " j        }|d         |k    r|d         S d  _        ddlm}m}m} ddlm#m$ ddlm} ddlm} d g j         z  }i }	 j        r	  |           }
i %|
%d<   t3          j         j        j         j        j	                           t3          j         j        j         j        j
                           f%d	<   |d
%f|	d<   d| j        j	        <   d| j        j
        <   n# t6          $ r d }
Y nw xY w j        ri j        j        } j        j        |         d d         }|                                }i %t?          j          j        j!        d                   rd &n} | j        j!        d d         d	t2          j"        i} j        j#        $                                pd}t?          j          j        j%                  r # j        j&        d||          }n # j        j%        d||          } ||'                    |                    } j        j        tP          v rytP           j        j                 }|)                    |          &tU          |tV                    rtY          &          &net[          |tP           j        j                 d          &n= j        j        dk    rtY          |          &nt]          d j        j         d           j        r|
 | j        j/         j        j	                  j        j         j        j	                 z   j        j/         j        j
                  j        j         j        j
                 z  |
dt2          j0        z            (tY          (          (nd (&O	 &)                    tc                                 n,# td          $ r tg          j4        dtj                     d &Y nw xY w(O	 ()                    tc                                 n,# td          $ r tg          j4        dtj                     d (Y nw xY w|dk    r4& (fd}& fd}t2          j6        d
i |f|	d <   d d|f| j        j        <   n|d!k    r3& (fd"}& fd#}t2          j6        d
i |f|	d <   d d|f| j        j        <   nΉ j        j        |         %d	<    j        j7        dk    rw|d$k    r"d%%d&<    j        j7        t2          j8        z  %d'<   nO|d(k    r"d)%d&<    j        j7        t2          j8        z  %d'<   n'|d*k    r!d+%d&<    j        j9        t2          j"        z  %d'<   %&(fd,}%&fd-}t2          j6        d
i |f|	d <   d d|f| j        j        <   d. j:        v r j:        ;                    d.          dk    }ty           j                   D ]} j:        |         d.k    r|rd/| }nd.}d } j        j        |         =                    d0          d         $                                }|d.k    r- j        j#        r j        j#        $                                }nd}d1|v rp|>                    d1          }|d |         ||dz   d2         }}tg          j4        d3|                                 d4|                                 t~                     |d5k    r $d6d78          }d9}n\|                                |v r||                                         }n+|#j@        vr"t7          d: j        j        |                     j        jA        $                                }|B                    d;          rzt?          jC        t?          j          j        j!        d d                             rtg          j4        d<t~                     d } nn | j        j!        d d         d	t2          j"        i} nI|d=k    r |dddt2          j"        >          } n)|d?k    rd } n tg          j4        d@| dAt~                     d }  #t?          jD         j        jE        d                   t?          jD         j        jE        d                   d||           '|'|z   '#$'fdB}!'fdC}"#d
i |!f|	|<   |d|"f||<   dD j:        v r:ty           j                   D ]%} j:        |         dDk    rdE}|d
i f|	|<   |ddFf||<   &ty           j                   D ]}||         ~ j        j        |         =                    d0          d         $                                }|d?k    rdG}||	v r	|dHz  }||	v 	t2          j6        d
d	 j        j        |         if|	|<   |ddFf||<   |||	ff _        ||	fS )IN_components_and_classes_cacher   r   )EarthLocationSkyCoordStokesCoord)Time	TimeDelta)FITS_DEPRECATED_SCALES)wcs_to_celestial_frameframer   rg   r   )r   r   zspherical.lon.degree)r   r   zspherical.lat.degree      utcmjd)r   scalelocation)obstimeT)preserve_observer_frame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 SpectralCoordrD   c                     t          | t                    r| S t          | dz   j        j        z  t          j                  S )Nr   )r   observertarget)r   r   r{   restwavr   m)redshiftr   ra   r   s    rb   spectralcoord_from_redshiftzPFITSWCSAPIMixin._get_components_and_classes.<locals>.spectralcoord_from_redshift"  sM    !(M:: ('(!A)99S!)%	   rd   c                 b   | j         | j        ]d}n| j         d}nd}t          j        | dt                     |                     t          j                  j        j	        z  dz
  S | 
                                                  t          j                  j        j	        z  dz
  S )NNo observer defined on WCS$No observer defined on SpectralCoord"No target defined on SpectralCoordC, SpectralCoord will be converted without any velocity frame changeg      ?)r   r   r   r   r
   to_valuer   r   r{   r   $with_observer_stationary_relative_to)spectralcoordmsgr   ra   s     rb   redshift_from_spectralcoordzPFITSWCSAPIMixin._get_components_and_classes.<locals>.redshift_from_spectralcoord,  s     !((19(/7#+">CC*3;"HCC"FC " + + + /	    -55ac::TX=MMPSSS *NN ( &hqsmm"h./ "	"rd   spectralrG   c                     t          | t                    r| S t          | t          z  t          j        t          j        z  dj        j        t          j        z            S )Nrelativistic)r   doppler_conventiondoppler_restr   r   )r   r   C_SIr   r   sr{   r   )betar   ra   r   s    rb   spectralcoord_from_betazLFITSWCSAPIMixin._get_components_and_classes.<locals>.spectralcoord_from_betaO  s[    !$66 $#(tS13Y+9%)X%5%;!)%   rd   c                    t          j        j        j        t           j        z            }| j        | j        cd}n| j        d}nd}t          j        | dt                     | 
                    t           j        t           j        z  |          t          z  S |                               
                    t           j        t           j        z  |          t          z  S )Nr   r   r   r   )r   doppler_relativisticr{   r   r   r   r   r   r   r
   r   r  r  r   )r   doppler_equivr   r   ra   s      rb   beta_from_spectralcoordzLFITSWCSAPIMixin._get_components_and_classes.<locals>.beta_from_spectralcoord[  s     %&$:48;Kac;Q$R$RM ((19(/7#+">CC*3;"HCC"FC " + + + /	    -55acACiOORVVV *NN ( &hqsQSy-@@"#rd   rF   r  r  r  rB   radiorC   opticalc                 R    t          | t                    r| S t          | fdS )N)r   r   )r   r   )rm   kwargsr   r   s    rb   spectralcoord_from_valuezMFITSWCSAPIMixin._get_components_and_classes.<locals>.spectralcoord_from_value  sF    !%77 %$((0 CI  rd   c                     | j         | j        ;d}n| j         d}nd}t          j        | dt                      | j        di S  |                               j        di S )Nr   r   r   r   rg   )r   r   r   r   r
   r   r   )r   r   r  r   s     rb   value_from_spectralcoordzMFITSWCSAPIMixin._get_components_and_classes.<locals>.value_from_spectralcoord  s     !((19(/7#+">CC*3;"HCC"FC " + + + /	    6}5????? #}QQ$   " -  -%+ -  - -rd   rK   ztime.r   (r   zDropping unsupported sub-scale z from scale gps   secr   taizUnrecognized time CTYPE=topocentzUMissing or incomplete observer location information, setting location in Time to None	geocenter)r   r   zObservation location 'z4' is not supported, setting location in Time to Nonec                 H    t          |           r| S  | d          z   S )Nr  r   )r   )offsetr   r   reference_times    rb   time_from_reference_and_offsetzSFITSWCSAPIMixin._get_components_and_classes.<locals>.time_from_reference_and_offset  s4    %fd33 *#)M-		&0O0O0OOOrd   c                     | z
  j         S rf   )r  )rK   r  s    rb   offset_from_time_and_referencezSFITSWCSAPIMixin._get_components_and_classes.<locals>.offset_from_time_and_reference  s     $~ 5::rd   rI   stokesrm   r   _)Frw   r   r{   r|   r   radesysspecsysequinoxdateobslnglatgetattrr   astropy.coordinatesr   r   r   astropy.timer   r   astropy.time.formatsr   astropy.wcs.utilsr   has_celestialr   r   r   has_spectralspecr   r   isnanobsgeor   timesyslowermjdavgmjdobsget_itrsVELOCITY_FRAMEStransform_tor   r   r   r	   NotImplementedErrorcrvalkpcr   	Exceptionr   r   r
   QuantityrestfrqHzr   r   countranger   indexUserWarningSCALEStrefposr   any
nan_to_nummjdref))ra   wcs_hashcacher   r   r   r   r   
componentsclassescelestial_frameispecr|   earth_locationtscaler   observer_locationr   r   r  r
  r  r  r  multiple_timeinamereference_time_deltar   possubscalerH  r   r   r"  r   r   r  r   r  r   s)   `                                  @@@@@@rb   r   z+FITSWCSAPIMixin._get_components_and_classeso  s4    J    HHHHHLHL

 48$??K6EQx8##Qx592 	MLLLLLLLLL00000000??????<<<<<<Vdj(

  	TT"8"8">"> "1w F48>$(,788F48>$(,788"v
 )1"f'=$+S
48<(+S
48<((!  ' ' ' #''(  |	VHMEHN5)"1"-EKKMMEF x*++ (!.0C!N!#!N!N )//11:U8DHO,, "d$$!/	  GG #d$$!/	  G %-H^-D-DW-D-U-U$V$V!8#66+DH,<=E0==eDDH!%-- #9(#C#C#@-+DH,<=48$ $ $
 X%3356GHHHH-G48#3GGG   ! o&A
 "HN48<048>$(,3OOHN48<048>$(,3OO)!AE\	   077  #$))$&&1111  $ $ $M6*  
  $HHH$ !"''////  " " "M6*  
 "FFF"            < ()z2r;V&W
#-7<W,X
48=))&
 
 
 
 
 
 
     < ()z2r;R&S
#-7<S,T
48=)) "&!6v8#a''7E34151AAD1H~..&7>34151AAD1H~..&7@34151AAC1G~.      - - - - - -4 ()z2r;S&T
#-7<T,U
48=)
 T333 :@@HH1LM4:&& XN XN1!4>>$ &*q{{% ,0( !HN1-33C88;AACCE8+ *$(H$4$:$:$<$<EE$)E e||#kk#..*/+uS1Wr\7Jx M'~~//M M=B[[]]M M'   ~~/8yE/J/J/J, %*@@@ 6u{{}} Edk11()WDHNSTDU)W)WXXX #h.4466G))*55 (6"(48?2A2+>"?"?@@ U$M!P +  
 (,HH'4}dhobqb6I'TPQPS'T'THH K//#0=Aqqs#C#C#C B#' !JW J J J'  
 $(%)Tdhoa&899dhoa&899$#!)& & &N ,7)7:N)NP P P P P P P
; ; ; ; ; &*2r3Q$RGDM%)1.L$MJqM%)GGG4:&& 7 71!48RRR#D%0"b$9GDM%)1g$6JqM
 tz"" 	3 	3A!}$x~a(..s33A6<<>>2::"DgooCKD goo!"R&$(.:K1L M!%q' 2
1 .6
G7L-L*7""s6   (F   FF#!Q &Q.-Q.4!R &R?>R?N)rp   rq   rr   rs   propertyrx   r}   r   setterr   r   r   r   r   r   r   r   r   r   r   r   rg   rd   rb   r   r      s        
   X # # X# * * X* + + + & & X& 	& 	& 	& " " X" 
- 
- 
-   X    X $ $ X$   X2C C CC C C  5 5 X5 5 5 X5   Xv# v# v# v# v#rd   r   )%r   numpyr   astropyr   r   astropy.constantsr   r,  r   r   r   'astropy.coordinates.spectral_coordinater   r	   astropy.utils.exceptionsr
   high_level_apir   low_level_apir   wrappersr   __all__sirm   r  r:  kmr  degtor   r^   r   r   r   rg   rd   rb   <module>rj     s                    = = = = = = = = = =        8 7 7 7 7 7 - - - - - - * * * * * * ' ' ' ' ' '
M
M
Mtz  4 'h!$h!$h!$h!$hn
QTkAC!$hn#!	 	 	
 . 'h!$h!$h!$h!$hn
QTkAC!$hn#!	 	 	
 * 'hqunaemX&*..qtacz:::	  
 3+3 
<3 	3
 3 3 3 3 3 ,3 ,3 *3 43 43 43  4!3" '#3$ '%3 3& ''3* I+3, K-3. O/30 G132 &334 $536 N738 G93:  ;3< +=3> (?3B FC3D 
6E3F 	&G3H 
6I3J 	&K3 3L /c3 3 3n  $2 $2 $2 $2 $2 $2 $2 $2N	 	 	 	 	%'8 	 	 	U	# U	# U	# U	# U	#o'8 U	# U	# U	# U	# U	#rd   