source: src/mlx/util.py@ 442:1f3a576c23ec

Last change on this file since 442:1f3a576c23ec was 401:15ad3c16ee11, checked in by István Váradi <ivaradi@…>, 12 years ago

The exception strings are converted from UTF-8 to unicode for proper logging (re #170)

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