a
    ߙfb                     @   s   d Z ddlZddlZddlZddlZ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lmZmZ ddlmZ d	gZG d
d deZG dd deZG dd deZdd Zddd	ZdS )an  
This module contains convenience functions for getting a coordinate object
for a named object by querying SESAME and getting the first returned result.
Note that this is intended to be a convenience, and is very simple. If you
need precise coordinates for an object you should find the appropriate
reference for that measurement and input the coordinates manually.
    N)units   )SkyCoord)data)download_fileget_file_contents)ScienceStateget_icrs_coordinatesc                   @   s$   e Zd ZdZddgZedd ZdS )
sesame_urlz8
    The URL(s) to Sesame's web-queryable database.
    z.http://cdsweb.u-strasbg.fr/cgi-bin/nph-sesame/z1http://vizier.cfa.harvard.edu/viz-bin/nph-sesame/c                 C   s   |S )N clsvaluer   r   ?lib/python3.9/site-packages/astropy/coordinates/name_resolve.pyvalidate$   s    zsesame_url.validateN__name__
__module____qualname____doc__Z_valueclassmethodr   r   r   r   r   r
      s   r
   c                   @   s    e Zd ZdZdZedd ZdS )sesame_databasez
    This specifies the default database that SESAME will query when
    using the name resolve mechanism in the coordinates
    subpackage. Default is to search all databases, but this can be
    'all', 'simbad', 'ned', or 'vizier'.
    allc                 C   s   |dvrt d| d|S )N)r   ZsimbadZnedZvizierzUnknown database '')
ValueErrorr   r   r   r   r   3   s    zsesame_database.validateNr   r   r   r   r   r   *   s   r   c                   @   s   e Zd ZdS )NameResolveErrorN)r   r   r   r   r   r   r   r   :   s   r   c                 C   s8   t d}|| }|du r dS | \}}||fS dS )a  
    Given a string response from SESAME, parse out the coordinates by looking
    for a line starting with a J, meaning ICRS J2000 coordinates.

    Parameters
    ----------
    resp_data : str
        The string HTTP response from SESAME.

    Returns
    -------
    ra : str
        The string Right Ascension parsed from the HTTP response.
    dec : str
        The string Declination parsed from the HTTP response.
    z %J\s*([0-9\.]+)\s*([\+\-\.0-9]+)N)NN)recompilesearchgroups)	resp_dataZpattrZmatchedradecr   r   r   _parse_response>   s    

r#   Fc                 C   s  |r$ddl m} || r$|| S t }| d }g }g }t D ]R}tj	
|j}	|	|vrH||	 tj|d}
|
jtj	| |d}
||
 qHg }|D ]}ztt||dd}W  qW q tjjy } z"|| W Y d}~qW Y d}~qd}~0  tjyV } z6d	tjjd
d|_|| W Y d}~qW Y d}~qd}~0 0 qdd t||D }tdtj| t|\}}|du s|du r|dkrd|  d| }nd|  d| d| }t|t ||t!j"t!j"fdd}|S )a  
    Retrieve an ICRS object by using an online name resolving service to
    retrieve coordinates for the specified name. By default, this will
    search all available databases until a match is found. If you would like
    to specify the database, use the science state
    ``astropy.coordinates.name_resolve.sesame_database``. You can also
    specify a list of servers to use for querying Sesame using the science
    state ``astropy.coordinates.name_resolve.sesame_url``. This will try
    each one in order until a valid response is returned. By default, this
    list includes the main Sesame host and a mirror at vizier.  The
    configuration item `astropy.utils.data.Conf.remote_timeout` controls the
    number of seconds to wait for a response from the server before giving
    up.

    Parameters
    ----------
    name : str
        The name of the object to get coordinates for, e.g. ``'M42'``.
    parse : bool
        Whether to attempt extracting the coordinates from the name by
        parsing with a regex. For objects catalog names that have
        J-coordinates embedded in their names eg:
        'CRTS SSS100805 J194428-420209', this may be much faster than a
        sesame query for the same object name. The coordinates extracted
        in this way may differ from the database coordinates by a few
        deci-arcseconds, so only use this option if you do not need
        sub-arcsecond accuracy for coordinates.
    cache : bool, str, optional
        Determines whether to cache the results or not. Passed through to
        `~astropy.utils.data.download_file`, so pass "update" to update the
        cached value.

    Returns
    -------
    coord : `astropy.coordinates.ICRS` object
        The object's coordinates in the ICRS frame.

    r   )jparserr   z{db}?{name})namedbF)cacheZshow_progressNz%Request took longer than the allowed z.1fz secondsc                 S   s    g | ]\}}| d |j  qS )z: )reason).0urler   r   r   
<listcomp>   s   z(get_icrs_coordinates.<locals>.<listcomp>zWAll Sesame queries failed. Unable to retrieve coordinates. See errors per URL below: 
 Az%Unable to find coordinates for name 'z' using z' in database z using Zicrs)r!   r"   Zunitframe)# r$   r   Zto_skycoordr   getupperr
   urllibparseZurlparseZnetlocappendospathjoinformatZquoter   r   errorZURLErrorsocketZtimeoutr   ZconfZremote_timeoutr(   zipr   linesepr#   r   uZdegree)r%   r3   r'   r$   Zdatabaser&   ZurlsZdomainsr*   ZdomainZfmt_url
exceptionsr    r+   Zmessagesr!   r"   errZscr   r   r   r	   Z   sZ    *



 

$

)FF)r   r5   r   r:   Zurllib.requestr2   Zurllib.parseZurllib.errorZastropyr   r=   Zsky_coordinater   Zastropy.utilsr   Zastropy.utils.datar   r   Zastropy.utils.stater   __all__r
   r   	Exceptionr   r#   r	   r   r   r   r   <module>   s"   	