a
    ߙfb                     @   s~   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ G d
d deZdd ZdddZdS )a  
Currently the only site accessible without internet access is the Royal
Greenwich Observatory, as an example (and for testing purposes).  In future
releases, a canonical set of sites may be bundled into astropy for when the
online registry is unavailable.

Additions or corrections to the observatory list can be submitted via Pull
Request to the [astropy-data GitHub repository](https://github.com/astropy/astropy-data),
updating the ``location.json`` file.
    N)get_close_matches)Mapping)get_pkg_data_contentsget_file_contents   )EarthLocation)UnknownSiteException)unitsc                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dd Zedd ZdS )SiteRegistrya[  
    A bare-bones registry of EarthLocation objects.

    This acts as a mapping (dict-like object) but with the important caveat that
    it's always transforms its inputs to lower-case.  So keys are always all
    lower-case, and even if you ask for something that's got mixed case, it will
    be interpreted as the all lower-case version.
    c                 C   s   i | _ g | _d S N)_lowercase_names_to_locations_namesself r   8lib/python3.9/site-packages/astropy/coordinates/sites.py__init__!   s    zSiteRegistry.__init__c                 C   sB   |  | jvr4t|| j}t|td}t|d|d| j|   S )aF  
        Returns an EarthLocation for a known site in this registry.

        Parameters
        ----------
        site_name : str
            Name of the observatory (case-insensitive).

        Returns
        -------
        site : `~astropy.coordinates.EarthLocation`
            The location of the observatory.
        )keyzthe 'names' attribute)close_names)lowerr   r   sortedlenr   )r   	site_namer   r   r   r   __getitem__'   s
    zSiteRegistry.__getitem__c                 C   s
   t | jS r   )r   r   r   r   r   r   __len__>   s    zSiteRegistry.__len__c                 C   s
   t | jS r   )iterr   r   r   r   r   __iter__A   s    zSiteRegistry.__iter__c                 C   s   |  | jv S r   )r   r   )r   r   r   r   r   __contains__D   s    zSiteRegistry.__contains__c                 C   s
   t | jS )ac  
        The names in this registry.  Note that these are *not* exactly the same
        as the keys: keys are always lower-case, while `names` is what you
        should use for the actual readable names (which may be case-sensitive)

        Returns
        -------
        site : list of str
            The names of the sites in this registry
        )r   r   r   r   r   r   namesG   s    zSiteRegistry.namesc                 C   s(   |D ]}|| j | < | j| qdS )a  
        Adds a location to the registry.

        Parameters
        ----------
        names : list of str
            All the names this site should go under
        locationobj : `~astropy.coordinates.EarthLocation`
            The actual site object
        N)r   r   r   append)r   r   Zlocationobjnamer   r   r   add_siteU   s    zSiteRegistry.add_sitec                 C   s   |  }|D ]}||   }t|dt|d |dt|d |dt|d }|d|j_|d}||j_|	|g| | q
||_
|S )	NZ	longitudeZlongitude_unitZlatitudeZlatitude_unitZ	elevationZelevation_unitr    aliases)copyr   Zfrom_geodeticpopuZUnitinfor    metar!   Z_loaded_jsondb)clsjsondbZregZsiteZ	site_infolocationr"   r   r   r   	from_jsond   s    
zSiteRegistry.from_jsonN)__name__
__module____qualname____doc__r   r   r   r   r   propertyr   r!   classmethodr+   r   r   r   r   r
      s   
r
   c                  C   s   t td} t| S )zh
    Load observatory database from data/observatories.json and parse them into
    a SiteRegistry.
    zdata/sites.json)jsonloadsr   r
   r+   )r)   r   r   r   get_builtin_sitesv   s    r4   c                 C   s6   | du rt ddd}nt| dd}t|}t|S )zW
    Load observatory database from data.astropy.org and parse into a SiteRegistry
    Nzcoordinates/sites.jsonzUTF-8)encoding)r   r   r2   r3   r
   r+   )ZjsonurlZcontentr)   r   r   r   get_downloaded_sites   s
    
r6   )N)r/   r2   Zdifflibr   collections.abcr   Zastropy.utils.datar   r   Zearthr   errorsr   Zastropyr	   r%   r
   r4   r6   r   r   r   r   <module>   s   ^	