
    g                    F    d dl mZ d dlmZ ddlmZ ddZddZ	 dddZdS )    )annotations)datetime   )commonmsg0strmsg1t0int | datetimet1lat_reffloatlon_refreturnNone | tuple[float, float]c                   t          j        |           }t          j        |          }t          j        |dd                   dz  }t          j        |dd                   dz  }	t          j        |dd                   dz  }
t          j        |dd                   dz  }d}d}t          j        d|z  d|
z  z
  d	z             }t	          ||dz  |z   z            }t	          ||dz  |
z   z            }|d
z
  }|d
z
  }|dk    r|n|}|dk    r|n|}t          j        |          t          j        |          k    rdS ||k    rm|}t          j        |          }t          t          j        |          dz
  d          }t          j        |	|dz
  z  ||z  z
  d	z             }d
|z  ||z  |	z   z  }nl|}t          j        |          }t          t          j        |          dz
  d          }t          j        |	|dz
  z  ||z  z
  d	z             }d
|z  ||z  |z   z  }||d
z   |dz   |dz   g}d |D             }fd|D             }t          t          d          |j	                  }||         }||fS )a  Decode surface position from a pair of even and odd position message,
    the lat/lon of receiver must be provided to yield the correct solution.

    Args:
        msg0 (string): even message (28 hexdigits)
        msg1 (string): odd message (28 hexdigits)
        t0 (int): timestamps for the even message
        t1 (int): timestamps for the odd message
        lat_ref (float): latitude of the receiver
        lon_ref (float): longitude of the receiver

    Returns:
        (float, float): (latitude, longitude) of the aircraft
    6   G      X         ?<"h?;   <         ?Z   r   N      i  c                $    g | ]}|d z   dz  d z
  S )r   h   ).0lons     K/home/andy/.local/lib/python3.11/site-packages/pyModeS/decoder/bds/bds06.py
<listcomp>z$surface_position.<locals>.<listcomp>W   s&    444S3Y##444    c                4    g | ]}t          |z
            S r!   )abs)r"   r#   r   s     r$   r%   z$surface_position.<locals>.<listcomp>Z   s%    
.
.
.#3w}
.
.
.r&      )key)
r   hex2binbin2intfloorr   cprNLmaxminrange__getitem__)r   r	   r
   r   r   r   msgbin0msgbin1cprlat_evencprlon_even
cprlat_odd
cprlon_oddair_d_lat_evenair_d_lat_oddj
lat_even_n	lat_odd_n
lat_even_s	lat_odd_slat_evenlat_oddlatnlnimr#   lonsdlsimins        `                       r$   surface_positionrI      s   . nT""GnT""G .B0069K.B0069K2//&8J2//&8JNM 	R+%Z7#=>>A ~R+)=>??Jmq2v
':;<<I bJBI %q[[zzjH"Q;;iiIG |Hg!6!666t 
Bww\(##h''!+Q//LQ/*r/ACGHHBw1r6K/0\'""g&&*A..LQ/*r/ACGHHBw1r6J./ r39cCi0D 54t444D /
.
.
.
.
.
.CuQxxS_---D
t*C8Or&   msgtuple[float, float]c                   t          j        |           dd         }t          j        |dd                   dz  }t          j        |dd                   dz  }t          |d                   }|rdnd	}t          j        d
||z  z   |z
            }|||z   z  }	t          j        |	          |z
  }
|
dk    rd|
z  }nd}t          j        d
||z  z   |z
            }|||z   z  }|	|fS )a  Decode surface position with only one message,
    knowing reference nearby location, such as previously calculated location,
    ground station, or airport location, etc. The reference position shall
    be within 45NM of the true position.

    Args:
        msg (str): even message (28 hexdigits)
        lat_ref: previous known latitude
        lon_ref: previous known longitude

    Returns:
        (float, float): (latitude, longitude) of the aircraft
        N   '   r   8      r   r   r   r   r   )r   r+   r,   intr-   r.   )rJ   r   r   mbcprlatcprlonid_latr;   rB   rD   d_lonrE   r#   s                 r$   surface_position_with_refrY   a   s    " 
		RSS	!B^Br"uI&&/F^Br"uI&&/FBrFA%GGgE 	S7U?*V344A
1v:
C	c		Q	B	AvvRS7U?*V344A
1v:
C8Or&   Fsourcebool+tuple[None | float, None | float, int, str]c                r   t          j        |           }||dk     s|dk    rt          d| z            t          j        |           dd         }t	          |d                   }|dk    r#t          j        |dd	                   d
z  dz  }nd}t          j        |dd                   dk    sdk    rd}nldk    rd}ncdk    rd}nZg d}g d}g d}	t          fdt          |          D                       }
||
dz
           ||
dz
           z
  |	|
dz
           z  z   }|r||ddddfS ||ddfS )a  Decode surface velocity from a surface position message

    Args:
        msg (str): 28 hexdigits string
        source (boolean): Include direction and vertical rate sources in return.
            Default to False.
            If set to True, the function will return six value instead of four.

    Returns:
        int, float, int, string, [string], [string]:
            - Speed (kt)
            - Angle (degree), ground track
            - Vertical rate, always 0
            - Speed type ('GS' for ground speed, 'AS' for airspeed)
            - [Optional] Direction source ('TRUE_NORTH')
            - [Optional] Vertical rate source (None)

    N      z+%s: Not a surface message, expecting 5<TC<8rM      r         r       r   |   g        g     e@)   	   ra   rO   ^   m   rd   )      ?r   re      F   d      )ri   g      ?r   r   re   r^   c              3  @   K   | ]}|d          k    |d         V  dS )r   r   Nr!   )r"   rE   movs     r$   	<genexpr>z#surface_velocity.<locals>.<genexpr>   s/      ??!AaD3JJ1JJJJ??r&   GS
TRUE_NORTH)r   typecodeRuntimeErrorr+   rR   r,   next	enumerate)rJ   rZ   tcrS   
trk_statustrkspdmov_lbkts_lbsteprV   ro   s              @r$   surface_velocityr~      s   * 
		B	zR!VVrAvvH3NOOO			RSS	!B RVJQnR2Y''#-3 .AbD
"
"C
axx399		---===777????y00?????QUmsVAE]2d1q5kAA !CD,44CD  r&   N)r   r   r	   r   r
   r   r   r   r   r   r   r   r   r   )rJ   r   r   r   r   r   r   rK   )F)rJ   r   rZ   r[   r   r\   )
__future__r   r    r   rI   rY   r~   r!   r&   r$   <module>r      s    # " " " " "            P P P Pf) ) ) )Z #5! 5! 5! 5! 5! 5! 5!r&   