source: src/mlx/util.py@ 388:61c304ff3d63

Last change on this file since 388:61c304ff3d63 was 373:cf2b6b8a3973, checked in by István Váradi <ivaradi@…>, 12 years ago

Added support for a secondary instance (#157)

File size: 5.1 KB
Line 
1
2import math
3import time
4import sys
5
6#------------------------------------------------------------------------------
7
8## @package mlx.util
9#
10# Utilities.
11
12#------------------------------------------------------------------------------
13
14## The average of the radius at the poles and a the equator, in metres
15#EARTH_RADIUS=6367467.4
16EARTH_RADIUS=6371000
17#EARTH_RADIUS=6378137
18
19#------------------------------------------------------------------------------
20
21def getDegMinSec(degrees):
22 """Break up the given floating point degrees value into a tuple.
23
24 The tuple contains 4 items:
25 - the degrees as an integer
26 - the minutes as an integer
27 - the seconds as an integer
28 - 1.0 if the value was non-negative, -1.0 if it was negative."""
29
30 if degrees<0:
31 degrees = -degrees
32 mul = -1.0
33 else:
34 mul = 1.0
35
36 deg = int(degrees)
37 min = int((degrees*60.0)%60.0)
38 sec = int((degrees*3600.0)%60.0)
39
40 return (deg, min, sec, mul)
41
42#------------------------------------------------------------------------------
43
44def getCoordinateString((latitude, longitude)):
45 """Get the string representation of the given coordinate pair."""
46
47 latitude_str = getLatitudeString(latitude)
48 longitude_str = getLongitudeString(longitude)
49
50 return latitude_str + " " + longitude_str
51
52#------------------------------------------------------------------------------
53
54def getLatitudeString(latitude):
55 """Get a string representation of the given latitude."""
56 return getDegreeString(latitude, ["N", "S"])
57
58#------------------------------------------------------------------------------
59
60def getLongitudeString(longitude):
61 """Get a string representation of the given longitude."""
62
63 return getDegreeString(longitude, ["E", "W"])
64
65#------------------------------------------------------------------------------
66
67def getDegreeString(degree, prefixes):
68 """Get a string representation of the given degree.
69
70 If the sign is positive, prefixes[0], otherwise prefixes[1] will be
71 prepended to the string."""
72
73 if degree<0:
74 prefix = prefixes[1]
75 else:
76 prefix = prefixes[0]
77
78 (deg, min, sec, _sign) = getDegMinSec(degree)
79
80 return u"%s%d\u00b0%02d\u2032%02d\u2033" % (prefix, deg, min, sec)
81
82#------------------------------------------------------------------------------
83
84def getTimestampString(timestamp):
85 """Get the string representation of the given timestamp."""
86 return time.strftime("%H:%M:%S", time.gmtime(timestamp))
87
88#------------------------------------------------------------------------------
89
90def getTimeIntervalString(seconds):
91 """Get a more human-friendly representation of the given time interval
92 expressed in seconds."""
93 hours = int(seconds / 3600)
94 minutes = int((seconds / 60) % 60)
95 seconds = int(seconds % 60)
96 return "%02d:%02d:%02d" % (hours, minutes, seconds)
97
98#------------------------------------------------------------------------------
99
100def km2nm(km):
101 """Convert the given kilometres into nautical miles."""
102 return km/1.852
103
104#------------------------------------------------------------------------------
105
106def nm2km(nm):
107 """Convert the given nautical miles into kilometres."""
108 return nm*1.852
109
110#------------------------------------------------------------------------------
111
112def radians2km(radians):
113 """Convert the given radians into kilometres"""
114 return radians * EARTH_RADIUS / 1000.0
115
116#------------------------------------------------------------------------------
117
118def radians2nm(radians):
119 """Convert the given radians into nautical miles."""
120 return km2nm(radians2km(radians))
121
122#------------------------------------------------------------------------------
123
124def getDistCourse(latitude1, longitude1, latitude2, longitude2):
125 """Get the distance and course between the two geographical coordinates.
126
127 This function calculates the rhumb distance."""
128
129 latitude1 = math.radians(latitude1)
130 longitude1 = math.radians(longitude1)
131
132 latitude2 = math.radians(latitude2)
133 longitude2 = math.radians(longitude2)
134
135 dlon_W = (longitude1 - longitude2) % (math.pi*2)
136 dlon_E = (longitude2 - longitude1) % (math.pi*2)
137
138 dphi = math.log(math.tan(latitude2/2 + math.pi/4)/
139 math.tan(latitude1/2 + math.pi/4))
140
141 if abs(latitude1-latitude2) < math.sqrt(1e-15):
142 q = math.cos(latitude1)
143 else:
144 q = (latitude1-latitude2)/dphi
145
146 if dlon_W < dlon_E:
147 tc = math.atan2(-dlon_W, dphi) % (math.pi*2)
148 d = math.sqrt(math.pow(q*dlon_W, 2) +
149 math.pow(latitude1-latitude2, 2))
150 else:
151 tc = math.atan2(dlon_E, dphi) % (math.pi*2)
152 d = math.sqrt(math.pow(q*dlon_E, 2) +
153 math.pow(latitude1-latitude2, 2))
154
155 return (radians2nm(d), math.degrees(tc))
156
157#------------------------------------------------------------------------------
158
159def visibility2String(visibility):
160 """Convert the given visibility expressed in metres into a string."""
161 return "%.0f metres" % (visibility,) if visibility<10000 \
162 else "%.1f kilometres" % (visibility/1000.0,)
163
164#------------------------------------------------------------------------------
165
166secondaryInstallation="secondary" in sys.argv
Note: See TracBrowser for help on using the repository browser.