a
    ߙfb\v                     @   s   d dl Z d dlmZ d dlZd dlZd dlmZmZ d dl	m
Z ddlmZmZmZmZ ddlmZ ddlmZmZmZ e d	Ze d
e jZdd ZdZdd Zdd Zdd Zdd Z dd Z!dd Z"dS )    N)Sequence)UnitIrreducibleUnit)units   )BaseCoordinateFrameframe_transform_graph_get_repr_cls_get_diff_cls)ICRS)BaseRepresentationSphericalRepresentationUnitSphericalRepresentationz(\+|\-)zJ                              # J prefix
    ([0-9]{6,7}\.?[0-9]{0,2})          # RA as HHMMSS.ss or DDDMMSS.ss, optional decimal digits
    ([\+\-][0-9]{6}\.?[0-9]{0,2})\s*$  # Dec as DDMMSS.ss, optional decimal digits
    c                 C   sj   t | tr:t }| |vr.td| t|t| }n,t	| rTt
| trT| }ntd| jj|S )zk
    Get a frame class from the input `frame`, which could be a frame name
    string, or frame class.
    z?Coordinate frame name "{}" is not a known coordinate frame ({})z@Coordinate frame must be a frame name or frame class, not a '{}')
isinstancestrr   Z	get_names
ValueErrorformatsortedZlookup_nameinspectZisclass
issubclassr   	__class____name__)frameframe_names	frame_cls r   Ilib/python3.9/site-packages/astropy/coordinates/sky_coordinate_parsers.py_get_frame_class    s    
r   zCoordinate attribute '{0}'={1!r} conflicts with keyword argument '{0}'={2!r}. This usually means an attribute was set on one of the input objects and also in the keyword arguments to {3}c              	   C   s   ddl m} d}i }|dd}|durt||r|jD ]R}||v r~tt|||| kr~tt	
|t|||| dq:t||||< q:|j}t|tr| D ]4}||v rtd
|q||st||||< q|j}|d|j |d|j |du rt|}| D ]R}t|ttjfrPt| dkrPt|d	krP|d	 }d }}	t|trj|}nt||r||j}|dur.|j}	|d
}
|
dur|d|
 | D ]x}||v r||st|| t||krtd
|t|||| n$||vr||st||||< q|	dur|du rH|	}n||	urtd
|	j|jq|du rxt}t|tstd|j |jD ]}||v r||||< qd|v rt|d|d< |dd}|durt||d< ||fS )an  
    Determines the coordinate frame from input SkyCoord args and kwargs.

    This function extracts (removes) all frame attributes from the kwargs and
    determines the frame class either using the kwargs, or using the first
    element in the args (if a single frame object is passed in, for example).
    This function allows a frame to be specified as a string like 'icrs' or a
    frame class like ICRS, or an instance ICRS(), as long as the instance frame
    attributes don't conflict with kwargs passed in (which could require a
    three-way merge with the coordinate data possibly specified via the args).
    r   SkyCoordNr   r   zCannot specify frame attribute '{}' directly as an argument to SkyCoord because a frame instance was passed in. Either pass a frame class, or modify the frame attributes of the input frame instance.representation_typedifferential_typer   szFrame attribute '{}' has conflicting values between the input coordinate data and either keyword arguments or the frame specification (frame=...): {} =/= {}zfCannot override frame='{}' of input coordinate with new frame='{}'. Instead, transform the coordinate.z!Frame class has unexpected type: )sky_coordinater   popr   _extra_frameattr_namesnpanygetattrr   _conflict_err_msgr   r   r   get_frame_attr_namesZis_frame_attr_defaultr   
setdefaultr    r!   r   r   ndarraylenget_representation_clsr   r   r   frame_attributesr	   r
   )argskwargsr   r   Zframe_cls_kwargsr   attrargZcoord_frame_objZcoord_frame_clsZ
frame_diffr!   r   r   r   _get_frame_without_data>   s    













	









r4   c                 C   s(  i }i }d}t | }|D ]}|tjv r||||< qt||}|t| || |rd}	| jt	krt | 
  }
|
d }|
d }d| t | v rd||}	tddd	d
 |D |	|rt|dkrt|d | ||\}}dt|d ddv r|d j}nxt|dkr|i }i }| j }| j }t||||D ]*\}}}}| jj| }|||d||< qNntdt|| D ]4\}}||v rtt|||| d|||< q| D ]H\}}||v rt|| |krtt|||| d|||< q|||fS )aS  
    Extract coordinate data from the args and kwargs passed to SkyCoord.

    By this point, we assume that all of the frame attributes have been
    extracted from kwargs (see _get_frame_without_data()), so all that are left
    are (1) extra SkyCoord attributes, and (2) the coordinate data, specified in
    any of the valid ways.
    N r   r   Zpm_z

 By default, most frame classes expect the longitudinal proper motion to include the cos(latitude) term, named `pm_{}_cos{}`. Did you mean to pass in this component?z%Unrecognized keyword argument(s) {}{}z, c                 s   s   | ]}d | d V  qdS )'Nr   ).0keyr   r   r   	<genexpr>  s   z)_parse_coordinate_data.<locals>.<genexpr>info__dict__r      unitz;Must supply no more than three positional arguments, got {}r   )listkeysr   r/   r$   #_get_representation_component_unitsupdate_get_representation_attrsr    r   "get_representation_component_namesr   r   joinr-   _parse_coordinate_argr(   r:   representation_component_namesvalueszipattr_classesitemsr)   r&   r'   )r   r0   r1   Zvalid_skycoord_kwargsZvalid_componentsr:   Z
attr_namesr2   r   Z
pm_messager   Zlon_nameZlat_nameZ_skycoord_kwargsZ_componentsframe_attr_namesrepr_attr_namesr3   frame_attr_namerepr_attr_namer>   Z
attr_classZcoord_valuevaluer   r   r   _parse_coordinate_data   s~    	






	

rQ   c              
   C   s   d|vrg d}n| d}t|tr^dd |dD }t|dkrv|d |d |d g}nt|ttfrv|||g}zDdd |D }|d	d
 tdt| D  t|dkrt	 W n. t
y } zt	d|W Y d}~n
d}~0 0 |S )zc
    Get the unit from kwargs for the *representation* components (not the
    differentials).
    r>   )NNNc                 S   s   g | ]}|  qS r   )stripr7   xr   r   r   
<listcomp>T      z7_get_representation_component_units.<locals>.<listcomp>,r   r   c                 S   s   g | ]}|rt |nd qS N)r   rS   r   r   r   rU   \  rV   c                 s   s   | ]
}d V  qd S rX   r   rS   r   r   r   r9   ]  rV   z6_get_representation_component_units.<locals>.<genexpr>r<   zSUnit keyword must have one to three unit values as tuple or comma-separated string.N)r$   r   r   splitr-   r   r   extendranger   	Exception)r0   r1   r   errr   r   r   rA   H  s&    



 
rA   c           )   
      sP  ddl m  d}i }i }t|j }t|j }t|jj }	t|}
t	| t
r`d}| g} t	|  tfr| js~td| j|jg }g }|D ]4}t	| jtr|dkr|| q|t| q|D ]&}||}||= ||= ||= |	|= q| jjrd| jjv r| jjd }| j|j|djd }|d D ]p\}}|dkrxt||sxd	| v rxqH|t|| |d
 || || |	|j|  qHtjD ]B}t| |d
}t	|  p||  v}|r|d
ur|||< qnt	| tr| jrd| jv r|d}| |j|fdd|D }|d D ]T\}}|tjd | |d
 || || |	jd j|  q`n| |jfdd|D }n t	| tjr| j j!dv r| j"dkr| j#d dkr| $ }nt	| t%tjfrg }d|jv oHd|jv } ttft&fdd| D r fdd| D }|dd
 D ]*}|'|d std(||d qt	|d jt}|d j)jD ]}t|d j)|||< q|d j*D ]}t|d |||< qg }t+||D ]~\}}|r>|dkr>q"g }|D ],}t||} ||j,rj| -dn|  qFt.|}!t	|!t/j0r| j1|!_2||! q"q| D ]l}"t	|"t
r
|"3 }#t|#dkrd4|#d
d d4|#dd
 f}"n|rt5|"}"n|#}"||" qzt6t7dd |D }$W n0 t8yb }% ztd|%W Y d
}%~%n
d
}%~%0 0 t|$dkrtd(|$|$d }$|$|
krtd(|$|j |
dd t+| D }|rdd |D }ntd z4t+||	||D ] \}&}'}}(|'||(dd!||&< qW n8 t8yF }% ztd"(||&|%W Y d
}%~%n
d
}%~%0 0 ||fS )#am  
    Single unnamed arg supplied.  This must be:
    - Coordinate frame with data
    - Representation
    - SkyCoord
    - List or tuple of:
      - String which splits into two values
      - Iterable with two values
      - SkyCoord, frame, or representation objects.

    Returns a dict mapping coordinate attribute names to values (or lists of
    values)
    r   r   FTz6Cannot initialize from a frame without coordinate dataZdistancer"   Z
d_distancer>   Nc                    s   g | ]}t  |qS r   r(   r7   rO   datar   r   rU     rV   z)_parse_coordinate_arg.<locals>.<listcomp>c                    s   g | ]}t  |qS r   r^   r_   r`   r   r   rU     rV   if   r<   ZraZdecc                 3   s   | ]}t | V  qd S rX   )r   r7   coord)coord_typesr   r   r9     rV   z(_parse_coordinate_arg.<locals>.<genexpr>c                    s   g | ]} |fi qS r   r   rd   )r   init_kwargsr   r   rU     rV   r   z5List of inputs don't have equivalent frames: {} != {}    c                 s   s   | ]}t |V  qd S rX   )r-   rS   r   r   r   r9     rV   z>One or more elements of input sequence does not have a length.zCInput coordinate values must have same number of elements, found {}zFInput coordinates have {} values but representation {} only accepts {}c                 S   s   g | ]}t |qS r   )r?   rS   r   r   r   rU   "  rV   c                 S   s   g | ]}|d  qS )r   r   rS   r   r   r   rU   %  rV   z,Cannot parse coordinates from first argument)r>   copyz6Cannot parse first argument data "{}" for attribute {})9r#   r   r?   rG   r@   rH   r    rJ   r-   r   r   r   Zhas_datar   ra   Zrepresent_asr   appendr(   indexZdifferentialsr.   rD   rK   hasattrZget_namer   r/   r*   r   r&   r,   ZdtypekindndimshapeZ	transposer   r'   Zis_equivalent_framer   r   r%   rI   ZisscalarZreshapeZconcatenateuZQuantityr>   Z_unitrY   rE   _parse_ra_decr   setr\   ))Zcoordsr   r   rg   Z	is_scalarZ
componentsZskycoord_kwargsrL   rM   repr_attr_classesZn_attr_namesrH   Zrepr_attr_name_to_droprO   Z
nametodropZnameidxZorig_velZvelZfrnameZreprnamer2   rP   Z	use_valueZdiffsZvalsZis_ra_dec_representationZscsZscZallunitsphreprZfattrnmZdata_attr_nameZ	data_valsZdata_valZconcat_valsre   coord1Zn_coordsr]   rN   repr_attr_classr>   r   )r   rf   ra   rg   r   rF   g  s"   
















 
&


rF   c                 C   s   | j  }| jj }i }t|||D ]\}}}||d}	|	dur&z||	|d||< W q& tjy }
 z2d| d|j	 d| d}t||
W Y d}
~
q&d}
~
0 0 q&| j
}|dur| d D ]2\}}|j| }||d}	|	dur||	||< q|S )a`  
    Find instances of the "representation attributes" for specifying data
    for this frame.  Pop them off of kwargs, run through the appropriate class
    constructor (to validate and apply unit), and put into the output
    valid_kwargs.  "Representation attributes" are the frame-specific aliases
    for the underlying data values in the representation, e.g. "ra" for "lon"
    for many equatorial spherical representations, or "w" for "x" in the
    cartesian representation of Galactic.

    This also gets any *differential* kwargs, because they go into the same
    frame initializer later on.
    Nr=   zUnit 'z' (z) could not be applied to 'z'. This can occur when passing units for some coordinate components when other components are specified as Quantity objects. Either pass a list of units for all components (and unit-less coordinate data), or pass Quantities for all components.r"   )rG   r@   r    rJ   rH   rI   r$   rq   ZUnitConversionErrorZphysical_typer!   rD   rK   )r   r   r1   rL   rt   Zvalid_kwargsrN   rv   r>   rP   r]   Zerror_messager!   Z
frame_nameZ	repr_nameZdiff_attr_classr   r   r   rC   7  s(    
$
rC   c                 C   s  t | tr|  }ntdt|dkrPd|dd d|dd f}nt|dkrt| }|d d|dd f}nPt|dkrt| }|r|	 }t|d d	d d
kr8|d dd  d|d dd  d|d dd  |d dd  d|d dd  d|d dd  f}np|d dd  d|d dd  d|d dd  |d dd  d|d dd  d|d dd  f}n$t| }|d d|dd f}n|}|S )aj  
    Parse RA and Dec values from a coordinate string. Currently the
    following formats are supported:

     * space separated 6-value format
     * space separated <6-value format, this requires a plus or minus sign
       separation between RA and Dec
     * sign separated format
     * JHHMMSS.ss+DDMMSS.ss format, with up to two optional decimal digits
     * JDDDMMSS.ss+DDMMSS.ss format, with up to two optional decimal digits

    Parameters
    ----------
    coord_str : str
        Coordinate string to parse.

    Returns
    -------
    coord : str or list of str
        Parsed coordinate values.
    zcoord_str must be a single strrh   ri   Nr<   rc   r   r   .         )
r   r   rY   	TypeErrorr-   rE   PLUS_MINUS_REJ_PREFIXED_RA_DEC_REmatchgroups)Z	coord_strru   re   Zmatch_jr   r   r   rr   e  s.    

(

6666
rr   )#recollections.abcr   r   Znumpyr&   Zastropy.unitsr   r   Zastropyr   rq   Z	baseframer   r   r	   r
   Zbuiltin_framesr   Zrepresentationr   r   r   compiler|   VERBOSEr}   r   r)   r4   rQ   rA   rF   rC   rr   r   r   r   r   <module>   s.   	
 u Q.