File: types.py

package info (click to toggle)
gr-air-modes 0.0.2.c29eb60-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,348 kB
  • ctags: 2,759
  • sloc: python: 12,444; cpp: 457; xml: 263; makefile: 46; ansic: 35; sh: 1
file content (107 lines) | stat: -rw-r--r-- 4,006 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
# Copyright 2013 Nick Foster
# 
# This file is part of gr-air-modes
# 
# gr-air-modes is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# gr-air-modes is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with gr-air-modes; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

from collections import namedtuple

#this is a timestamp that preserves precision when used with UTC timestamps.
#ordinary double-precision timestamps lose significant fractional precision
#when the exponent is as large as necessary for UTC.
class stamp:
    def __init__(self, secs, frac_secs):
        self.secs = secs
        self.frac_secs = frac_secs
        self.secs += int(self.frac_secs)
        self.frac_secs -= int(self.frac_secs)
    def __lt__(self, other):
        if isinstance(other, self.__class__):
            if self.secs == other.secs:
                return self.frac_secs < other.frac_secs
            else:
                return self.secs < other.secs
        elif isinstance(other, float):
            return float(self) > other
        else:
            raise TypeError
    def __gt__(self, other):
        if type(other) is type(self):
            if self.secs == other.secs:
                return self.frac_secs > other.frac_secs
            else:
                return self.secs > other.secs
        elif type(other) is type(float):
            return float(self) > other
        else:
            raise TypeError
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.secs == other.secs and self.frac_secs == other.frac_secs
        elif isinstance(other, float):
            return float(self) == other
        else:
            raise TypeError
    def __ne__(self, other):
        return not (self == other)
    def __le__(self, other):
        return (self == other) or (self < other)
    def __ge__(self, other):
        return (self == other) or (self > other)

    def __add__(self, other):
        if isinstance(other, self.__class__):
            ipart = self.secs + other.secs
            fpart = self.frac_secs + other.frac_secs
            return stamp(ipart, fpart)
        elif isinstance(other, float):
            return self + stamp(0, other)
        elif isinstance(other, int):
            return self + stamp(other, 0)            
        else:
            raise TypeError
            
    def __sub__(self, other):
        if isinstance(other, self.__class__):
            ipart = self.secs - other.secs
            fpart = self.frac_secs - other.frac_secs
            return stamp(ipart, fpart)
        elif isinstance(other, float):
            return self - stamp(0, other)
        elif isinstance(other, int):
            return self - stamp(other, 0)
        else:
            raise TypeError

    #to ensure we don't hash by stamp
    #TODO fixme with a reasonable hash in case you feel like you'd hash by stamp
    __hash__ = None
    
    #good to within ms for comparison
    def __float__(self):
        return self.secs + self.frac_secs

    def __str__(self):
        return "%f" % float(self)

#a Mode S report including the modes_reply data object
modes_report = namedtuple('modes_report', ['data', 'ecc', 'rssi', 'timestamp'])
#lat, lon, alt
#TODO: a position class internally represented as ECEF XYZ which can easily be used for multilateration and distance calculation
llh = namedtuple('llh', ['lat', 'lon', 'alt'])
mlat_report = namedtuple('mlat_report', ['data', 'nreps', 'timestamp', 'llh', 'hdop', 'vdop'])