
    HR-e/                     l	   d 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 g dZd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z! G d dej"                  Z# G d dej$                  Z%e%&                    e	j'        e           e%&                    e	j(        e           e%&                    e	j)        e           e%*                    e+e%j,                   e%*                    ej-        e           e%*                    ej.        e           e%*                    ee           e%*                    ee           e%*                    ej/        e           e%*                    ee           e%*                    ej0        ej1        j2        j3                   ej4        ej5        ej6        ej7        ej8        ej9        ej:        ej;        ej<        ej=        ej>        fD ]'Z?e%*                    e?ej1        j2        j@                   (ejA        ejB        ejC        ejD        ejE        fD ]'Z?e%*                    e?ej1        j2        jF                   (ejG        eHejI        ejJ        fD ]Z?e%*                    e?e            e#K                    de!           e#K                    de#jL                   e#K                    d e#jM                   e#K                    d!e           e#K                    d"e           e#K                    d#e           e#K                    d$e           e#K                    d%e           e#K                    d&e           e#K                    d'e           e	jN        d(fe	jO        d)fe	jP        d*fe	jQ        d+fejR        d,fejS        d-fejT        d.fejU        d/ffD ]C\  ZVZWe%&                    eV eeW                     e#K                    eW eeV                     D eXejY        jZ        [                                           eXejY        j\        [                                          z   D ]ZZVeVj]        Z^e^ejY        j        v rCd0e^z   ZWe%&                    eV eeW                     e#K                    eW eeV                     [d1 Z_d2 Z`d4d3ZadS )5a  Functions for serializing astropy objects to YAML.

It provides functions `~astropy.io.misc.yaml.dump`,
`~astropy.io.misc.yaml.load`, and `~astropy.io.misc.yaml.load_all` which
call the corresponding functions in `PyYaml <https://pyyaml.org>`_ but use the
`~astropy.io.misc.yaml.AstropyDumper` and `~astropy.io.misc.yaml.AstropyLoader`
classes to define custom YAML tags for the following astropy classes:
- `astropy.units.Unit`
- `astropy.units.Quantity`
- `astropy.time.Time`
- `astropy.time.TimeDelta`
- `astropy.coordinates.SkyCoord`
- `astropy.coordinates.Angle`
- `astropy.coordinates.Latitude`
- `astropy.coordinates.Longitude`
- `astropy.coordinates.EarthLocation`
- `astropy.table.SerializedColumn`

Examples
--------
    >>> from astropy.io.misc import yaml
    >>> import astropy.units as u
    >>> from astropy.time import Time
    >>> from astropy.coordinates import EarthLocation
    >>> t = Time(2457389.0, format='mjd',
    ...          location=EarthLocation(1000, 2000, 3000, unit=u.km))
    >>> td = yaml.dump(t)
    >>> print(td)
    !astropy.time.Time
    format: mjd
    in_subfmt: '*'
    jd1: 4857390.0
    jd2: -0.5
    location: !astropy.coordinates.earth.EarthLocation
    ellipsoid: WGS84
    x: !astropy.units.Quantity
        unit: &id001 !astropy.units.Unit {unit: km}
        value: 1000.0
    y: !astropy.units.Quantity
        unit: *id001
        value: 2000.0
    z: !astropy.units.Quantity
        unit: *id001
        value: 3000.0
    out_subfmt: '*'
    precision: 3
    scale: utc
    >>> ty = yaml.load(td)
    >>> ty
    <Time object: scale='utc' format='mjd' value=2457389.0>
    >>> ty.location  # doctest: +FLOAT_CMP
    <EarthLocation (1000., 2000., 3000.) km>
    N)coordinates)units)SerializedColumn)Time	TimeDelta)AstropyLoaderAstropyDumperloadload_alldumpc                 t    dt          |                                          i}|                     d|          S )Nunit!astropy.units.Unit)str	to_stringrepresent_mappingdumperobjouts      4lib/python3.11/site-packages/astropy/io/misc/yaml.py_unit_representerr   F   s3    3s}}''
(C##$93???    c                 d    |                      |          }t          j        |d         d          S )Nr   warn)parse_strict)construct_mappinguUnitloadernodemaps      r   _unit_constructorr$   K   s.    

"
"4
(
(C6#f+F3333r   c                 2    |                      d|          }|S )N!astropy.table.SerializedColumn)r   r   s      r   _serialized_column_representerr'   P   s    

"
"#Dc
J
JCJr   c                 J    |                      |          }t          |          S N)r   r   r    s      r   _serialized_column_constructorr*   U   s#    

"
"4
(
(CC   r   c                 `    |j                                         }|                     d|          S )N!astropy.time.Timeinfo_represent_as_dictr   r   s      r   _time_representerr0   Z   s,    
(
%
%
'
'C##$8#>>>r   c                 n    |                      |          }t          j                            |          }|S r)   )r   r   r.   _construct_from_dictr!   r"   r#   r   s       r   _time_constructorr4   _   s/    

"
"4
(
(C
)
(
(
-
-CJr   c                 `    |j                                         }|                     d|          S )N!astropy.time.TimeDeltar-   r   s      r   _timedelta_representerr7   e   s,    
(
%
%
'
'C##$=sCCCr   c                 n    |                      |          }t          j                            |          }|S r)   )r   r   r.   r2   r3   s       r   _timedelta_constructorr9   j   s/    

"
"4
(
(C
.
-
-c
2
2CJr   c                    |j         j        rt          d|           |j        d         s!|j        d         st	          j        |          }t	          j        |          r
|j        }d}nd}t          j	        |
                                          }t          ||j         j        st          |j                   n|j         j        |j        |          }|                     d|          S )Nz%cannot serialize numpy object array: C_CONTIGUOUSF_CONTIGUOUSFC)bufferdtypeshapeorder!numpy.ndarray)r@   	hasobject	TypeErrorflagsnpascontiguousarray	isfortranTbase64	b64encodetobytesdictfieldsr   descrrA   r   )r   r   rB   data_b64r   s        r   _ndarray_representerrR   p   s    
y GEEEFFFIn% (>)B ("3''	|C e..H
$'I$4Ic#)nnn#)/i	  C ##$4c:::r   c                     |                      |d          }t          j        |d                   |d<   |d         dk    rt          d          t	          j        di |S )NTdeepr?   r@   objectz)cannot load numpy array with dtype object )r   rK   	b64decoderE   rG   ndarrayr    s      r   _ndarray_constructorrZ      sh    
 
"
"4d
"
3
3C$S]33CM
7|xCDDD:r   c                     t          j        |                                          }t          ||j        j        st          |j                  n|j        j                  }|                     d|          S )N)r?   r@   !numpy.void)	rK   rL   rM   rN   r@   rO   r   rP   r   )r   r   rQ   r   s       r   _void_representerr]      sf    ..H
$'I$4Ic#)nnn#)/  C ##M3777r   c                     |                      |d          }t          j        |d                   |d<   t          j        dddi|d         S )NTrT   r?   rA   rW   )r   rK   rX   rG   rY   r    s      r   _void_constructorr_      sR    

"
"4d
"
3
3C$S]33CM:&&B&#&&r**r   c                       fd}|S )Nc                 b    |j                                         }|                     |          S r)   r-   )r   r   r   tags      r   representerz*_quantity_representer.<locals>.representer   s,    h))++''S111r   rW   )rb   rc   s   ` r   _quantity_representerrd      $    2 2 2 2 2 r   c                       fd}|S )Nc                 b    |                      |          }j                            |          S r)   )r   r.   r2   )r!   r"   r#   clss      r   constructorz*_quantity_constructor.<locals>.constructor   s,    &&t,,x,,S111r   rW   )rh   ri   s   ` r   _quantity_constructorrj      re   r   c                 d    |j                                         }|                     d|          }|S )N,!astropy.coordinates.sky_coordinate.SkyCoordr-   )r   r   r#   r   s       r   _skycoord_representerrm      s1    
(
%
%
'
'C

"
"#QSV
W
WCJr   c                 x    |                      |          }t          j        j                            |          }|S r)   )r   coordsSkyCoordr.   r2   r3   s       r   _skycoord_constructorrq      s2    

"
"4
(
(C
/

3
3C
8
8CJr   c                     |j         dk    r	|j        }nE|j        dk    r|j         d}n/|j         dk    r|j        d|j         d}n|j        |j         d}|                     d|          S )Ng        jr   + tag:yaml.org,2002:python/complex)imagrealrepresent_scalarselfdatas     r   _complex_representerr|      s    yC)	c		)   	Q)..	...)-ty---  !CTJJJr   c                 J    |                      |          }t          |          S r)   )construct_scalarcomplexr    s      r   _complex_constructorr      s!    

!
!$
'
'C3<<r   c                       e Zd ZdZd Zd ZdS )r   a  
    Custom SafeLoader that constructs astropy core objects as well
    as Python tuple and unicode objects.

    This class is not directly instantiated by user code, but instead is
    used to maintain the available constructor functions that are
    called when parsing a YAML stream.  See the `PyYaml documentation
    <https://pyyaml.org/wiki/PyYAMLDocumentation>`_ for details of the
    class signature.
    c                 F    t          |                     |                    S r)   )tupleconstruct_sequencerz   r"   s     r   _construct_python_tuplez%AstropyLoader._construct_python_tuple   s    T,,T22333r   c                 ,    |                      |          S r)   )r~   r   s     r   _construct_python_unicodez'AstropyLoader._construct_python_unicode   s    $$T***r   N)__name__
__module____qualname____doc__r   r   rW   r   r   r   r      s<        	 	4 4 4+ + + + +r   r   c                       e Zd ZdZd ZdS )r	   a  
    Custom SafeDumper that represents astropy core objects as well
    as Python tuple and unicode objects.

    This class is not directly instantiated by user code, but instead is
    used to maintain the available representer functions that are
    called when generating a YAML stream from an object.  See the
    `PyYaml documentation <https://pyyaml.org/wiki/PyYAMLDocumentation>`_
    for details of the class signature.
    c                 .    |                      d|          S )Ntag:yaml.org,2002:python/tuple)represent_sequencery   s     r   _represent_tuplezAstropyDumper._represent_tuple   s    &&'GNNNr   N)r   r   r   r   r   rW   r   r   r	   r	      s2        	 	O O O O Or   r	   ru   r   z tag:yaml.org,2002:python/unicoder   rC   r\   r,   r6   rl   r&   z!astropy.units.Quantityz!astropy.units.Magnitudez!astropy.units.Dexz!astropy.units.Decibelz!astropy.coordinates.Anglez!astropy.coordinates.Latitudez!astropy.coordinates.Longitudez(!astropy.coordinates.earth.EarthLocationz!astropy.coordinates.c                 8    t          j        | t                    S )a  Parse the first YAML document in a stream using the AstropyLoader and
    produce the corresponding Python object.

    Parameters
    ----------
    stream : str or file-like
        YAML input

    Returns
    -------
    obj : object
        Object corresponding to YAML document
    Loader)yamlr
   r   streams    r   r
   r
   F  s     9VM2222r   c                 8    t          j        | t                    S )a%  Parse the all YAML documents in a stream using the AstropyLoader class and
    produce the corresponding Python object.

    Parameters
    ----------
    stream : str or file-like
        YAML input

    Returns
    -------
    obj : object
        Object corresponding to YAML document

    r   )r   r   r   r   s    r   r   r   W  s     =6666r   c                 j    t           |d<   |                    dd           t          j        | fd|i|S )a  Serialize a Python object into a YAML stream using the AstropyDumper class.
    If stream is None, return the produced string instead.

    Parameters
    ----------
    data : object
        Object to serialize to YAML
    stream : file-like, optional
        YAML output (if not supplied a string is returned)
    **kwargs
        Other keyword arguments that get passed to yaml.dump()

    Returns
    -------
    out : str or None
        If no ``stream`` is supplied then YAML output is returned as str

    Dumperdefault_flow_styleNr   )r	   
setdefaultr   r   )r{   r   kwargss      r   r   r   i  sB    & %F8
*D1119T33&3F333r   r)   )br   rK   numpyrG   r   astropyr   ro   r   r   astropy.tabler   astropy.timer   r   __all__r   r$   r'   r*   r0   r4   r7   r9   rR   rZ   r]   r_   rd   rj   rm   rq   r|   r   
SafeLoaderr   
SafeDumperr	   add_multi_representerUnitBaseFunctionUnitBaseStructuredUnitadd_representerr   r   rY   voidrp   bool_rc   SafeRepresenterrepresent_boolint_intcintpint8int16int32int64uint8uint16uint32uint64np_typerepresent_intfloat_float16float32float64
longdoublerepresent_floatcomplex_r   	complex64
complex128add_constructorr   r   Quantity	MagnitudeDexDecibelAngleLatitude	LongitudeEarthLocationrh   rb   listrepresentationREPRESENTATION_CLASSESvaluesDIFFERENTIAL_CLASSESr   namer
   r   r   rW   r   r   <module>r      s  4 4l       ) ) ) ) ) )       * * * * * * ( ( ( ( ( ( ( (
H
H
H@ @ @
4 4 4
  
! ! !
? ? ?
  D D D
  ; ; ;2
 
 
8 8 8+ + +        	K 	K 	K  
+ + + + +DO + + +&O O O O ODO O O O   # #AJ0A B B B  # #A$68I J J J  # #A$46G H H H   e]%C D D D   bj*> ? ? ?   bg'8 9 9 9   d$5 6 6 6   i)? @ @ @   fo/D E E E   .0N O O O   bh(8(H(W X X XGGGGHHHHIII  G !!!1?    	2:rz2:r}M  G!!!1A    WblBMB A AG!!'+?@@@@   @BV W W W   $m&K     &(O     35F G G G   .0D E E E   m-> ? ? ?   24E F F F   79O P P P   24I     %'E  
 Z*+[,-U !Y()\/0_5678EF	 C CHC ''-B-B3-G-GHHH!!#'<'<S'A'ABBBB4%<CCEEFF
.5577J J  G GC <Dv$,,,%,++C1F1Fs1K1KLLL%%c+@+@+E+EFFF3 3 3"7 7 7$4 4 4 4 4 4r   