source: src/mlx/util.py@ 326:cee8a7512e56

Last change on this file since 326:cee8a7512e56 was 298:24c67ec5cdca, checked in by István Váradi <ivaradi@…>, 12 years ago

Documented the non-GUI modules

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