updated example code

This commit is contained in:
dh1tw 2015-12-30 19:55:51 +01:00
parent bd22b50e33
commit dc4664591f

View file

@ -11,7 +11,7 @@ def latlong_to_locator (latitude, longitude):
"""converts WGS84 coordinates into the corresponding Maidenhead Locator """converts WGS84 coordinates into the corresponding Maidenhead Locator
Args: Args:
latitude (float): Latitude latitude (float): Latitude
longitude (float): Longitude longitude (float): Longitude
Returns: Returns:
@ -27,30 +27,31 @@ def latlong_to_locator (latitude, longitude):
>>> from pyhamtools.locator import latlong_to_locator >>> from pyhamtools.locator import latlong_to_locator
>>> latitude = 48.5208333 >>> latitude = 48.5208333
>>> longitude = 9.375 >>> longitude = 9.375
>>> latlong_to_locator(latitude, longitude)
'JN48QM' 'JN48QM'
Note: Note:
Latitude (negative = West, positive = East) Latitude (negative = West, positive = East)
Longitude (negative = South, positive = North) Longitude (negative = South, positive = North)
""" """
if longitude >= 180 or longitude <= -180: if longitude >= 180 or longitude <= -180:
raise ValueError raise ValueError
if latitude >= 90 or latitude <= -90: if latitude >= 90 or latitude <= -90:
raise ValueError raise ValueError
longitude += 180; longitude += 180;
latitude +=90; latitude +=90;
locator = chr(ord('A') + int(longitude / 20)) locator = chr(ord('A') + int(longitude / 20))
locator += chr(ord('A') + int(latitude / 10)) locator += chr(ord('A') + int(latitude / 10))
locator += chr(ord('0') + int((longitude % 20) / 2)) locator += chr(ord('0') + int((longitude % 20) / 2))
locator += chr(ord('0') + int(latitude % 10)) locator += chr(ord('0') + int(latitude % 10))
locator += chr(ord('A') + int((longitude - int(longitude / 2) * 2) / (2 / 24))) locator += chr(ord('A') + int((longitude - int(longitude / 2) * 2) / (2 / 24)))
locator += chr(ord('A') + int((latitude - int(latitude / 1) * 1 ) / (1 / 24))) locator += chr(ord('A') + int((latitude - int(latitude / 1) * 1 ) / (1 / 24)))
return locator return locator
def locator_to_latlong (locator): def locator_to_latlong (locator):
@ -65,7 +66,7 @@ def locator_to_latlong (locator):
Raises: Raises:
ValueError: When called with wrong or invalid input arg ValueError: When called with wrong or invalid input arg
TypeError: When arg is not a string TypeError: When arg is not a string
Example: Example:
The following example converts a Maidenhead locator into Latitude and Longitude The following example converts a Maidenhead locator into Latitude and Longitude
@ -73,57 +74,57 @@ def locator_to_latlong (locator):
>>> latitude, longitude = locator_to_latlong("JN48QM") >>> latitude, longitude = locator_to_latlong("JN48QM")
>>> print latitude, longitude >>> print latitude, longitude
48.5208333333 9.375 48.5208333333 9.375
Note: Note:
Latitude (negative = West, positive = East) Latitude (negative = West, positive = East)
Longitude (negative = South, positive = North) Longitude (negative = South, positive = North)
""" """
locator = locator.upper() locator = locator.upper()
if len(locator) == 5 or len(locator) < 4: if len(locator) == 5 or len(locator) < 4:
raise ValueError raise ValueError
if ord(locator[0]) > ord('R') or ord(locator[0]) < ord('A'): if ord(locator[0]) > ord('R') or ord(locator[0]) < ord('A'):
raise ValueError raise ValueError
if ord(locator[1]) > ord('R') or ord(locator[1]) < ord('A'): if ord(locator[1]) > ord('R') or ord(locator[1]) < ord('A'):
raise ValueError raise ValueError
if ord(locator[2]) > ord('9') or ord(locator[2]) < ord('0'): if ord(locator[2]) > ord('9') or ord(locator[2]) < ord('0'):
raise ValueError raise ValueError
if ord(locator[3]) > ord('9') or ord(locator[3]) < ord('0'): if ord(locator[3]) > ord('9') or ord(locator[3]) < ord('0'):
raise ValueError raise ValueError
if len(locator) == 6: if len(locator) == 6:
if ord(locator[4]) > ord('X') or ord(locator[4]) < ord('A'): if ord(locator[4]) > ord('X') or ord(locator[4]) < ord('A'):
raise ValueError raise ValueError
if ord (locator[5]) > ord('X') or ord(locator[5]) < ord('A'): if ord (locator[5]) > ord('X') or ord(locator[5]) < ord('A'):
raise ValueError raise ValueError
longitude = (ord(locator[0]) - ord('A')) * 20 - 180 longitude = (ord(locator[0]) - ord('A')) * 20 - 180
latitude = (ord(locator[1]) - ord('A')) * 10 - 90 latitude = (ord(locator[1]) - ord('A')) * 10 - 90
longitude += (ord(locator[2]) - ord('0')) * 2 longitude += (ord(locator[2]) - ord('0')) * 2
latitude += (ord(locator[3]) - ord('0')) latitude += (ord(locator[3]) - ord('0'))
if len(locator) == 6: if len(locator) == 6:
longitude += ((ord(locator[4])) - ord('A')) * (2 / 24) longitude += ((ord(locator[4])) - ord('A')) * (2 / 24)
latitude += ((ord(locator[5])) - ord('A')) * (1 / 24) latitude += ((ord(locator[5])) - ord('A')) * (1 / 24)
# move to center of subsquare # move to center of subsquare
longitude += 1 / 24 longitude += 1 / 24
latitude += 0.5 / 24 latitude += 0.5 / 24
else: else:
# move to center of square # move to center of square
longitude += 1; longitude += 1;
latitude += 0.5; latitude += 0.5;
return latitude, longitude return latitude, longitude
def calculate_distance(locator1, locator2): def calculate_distance(locator1, locator2):
"""calculates the (shortpath) distance between two Maidenhead locators """calculates the (shortpath) distance between two Maidenhead locators
@ -137,34 +138,34 @@ def calculate_distance(locator1, locator2):
Raises: Raises:
ValueError: When called with wrong or invalid input arg ValueError: When called with wrong or invalid input arg
AttributeError: When args are not a string AttributeError: When args are not a string
Example: Example:
The following calculates the distance between two Maidenhead locators in km The following calculates the distance between two Maidenhead locators in km
>>> from pyhamtools.locator import calculate_distance >>> from pyhamtools.locator import calculate_distance
>>> calculate_distance("JN48QM", "QF67bf") >>> calculate_distance("JN48QM", "QF67bf")
16466.413 16466.413
""" """
R = 6371 #earh radius R = 6371 #earh radius
lat1, long1 = locator_to_latlong(locator1) lat1, long1 = locator_to_latlong(locator1)
lat2, long2 = locator_to_latlong(locator2) lat2, long2 = locator_to_latlong(locator2)
d_lat = radians(lat2) - radians(lat1) d_lat = radians(lat2) - radians(lat1)
d_long = radians(long2) - radians(long1) d_long = radians(long2) - radians(long1)
r_lat1 = radians(lat1) r_lat1 = radians(lat1)
r_long1 = radians(long1) r_long1 = radians(long1)
r_lat2 = radians(lat2) r_lat2 = radians(lat2)
r_long2 = radians(long2) r_long2 = radians(long2)
a = sin(d_lat/2) * sin(d_lat/2) + cos(r_lat1) * cos(r_lat2) * sin(d_long/2) * sin(d_long/2) a = sin(d_lat/2) * sin(d_lat/2) + cos(r_lat1) * cos(r_lat2) * sin(d_long/2) * sin(d_long/2)
c = 2 * atan2(sqrt(a), sqrt(1-a)) c = 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c #distance in km d = R * c #distance in km
return d; return d;
def calculate_distance_longpath(locator1, locator2): def calculate_distance_longpath(locator1, locator2):
"""calculates the (longpath) distance between two Maidenhead locators """calculates the (longpath) distance between two Maidenhead locators
@ -179,22 +180,22 @@ def calculate_distance_longpath(locator1, locator2):
Raises: Raises:
ValueError: When called with wrong or invalid input arg ValueError: When called with wrong or invalid input arg
AttributeError: When args are not a string AttributeError: When args are not a string
Example: Example:
The following calculates the longpath distance between two Maidenhead locators in km The following calculates the longpath distance between two Maidenhead locators in km
>>> from pyhamtools.locator import calculate_distance_longpath >>> from pyhamtools.locator import calculate_distance_longpath
>>> calculate_distance_longpath("JN48QM", "QF67bf") >>> calculate_distance_longpath("JN48QM", "QF67bf")
23541.5867 23541.5867
""" """
c = 40008 #[km] earth circumference c = 40008 #[km] earth circumference
sp = calculate_distance(locator1, locator2) sp = calculate_distance(locator1, locator2)
return c - sp return c - sp
def calculate_heading(locator1, locator2): def calculate_heading(locator1, locator2):
"""calculates the heading from the first to the second locator """calculates the heading from the first to the second locator
@ -208,33 +209,33 @@ def calculate_heading(locator1, locator2):
Raises: Raises:
ValueError: When called with wrong or invalid input arg ValueError: When called with wrong or invalid input arg
AttributeError: When args are not a string AttributeError: When args are not a string
Example: Example:
The following calculates the heading from locator1 to locator2 The following calculates the heading from locator1 to locator2
>>> from pyhamtools.locator import calculate_heading >>> from pyhamtools.locator import calculate_heading
>>> calculate_heading("JN48QM", "QF67bf") >>> calculate_heading("JN48QM", "QF67bf")
74.3136 74.3136
""" """
lat1, long1 = locator_to_latlong(locator1) lat1, long1 = locator_to_latlong(locator1)
lat2, long2 = locator_to_latlong(locator2) lat2, long2 = locator_to_latlong(locator2)
r_lat1 = radians(lat1) r_lat1 = radians(lat1)
r_lon1 = radians(long1) r_lon1 = radians(long1)
r_lat2 = radians(lat2) r_lat2 = radians(lat2)
r_lon2 = radians(long2) r_lon2 = radians(long2)
d_lon = radians(long2 - long1) d_lon = radians(long2 - long1)
b = atan2(sin(d_lon)*cos(r_lat2),cos(r_lat1)*sin(r_lat2)-sin(r_lat1)*cos(r_lat2)*cos(d_lon)) # bearing calc b = atan2(sin(d_lon)*cos(r_lat2),cos(r_lat1)*sin(r_lat2)-sin(r_lat1)*cos(r_lat2)*cos(d_lon)) # bearing calc
bd = degrees(b) bd = degrees(b)
br,bn = divmod(bd+360,360) # the bearing remainder and final bearing br,bn = divmod(bd+360,360) # the bearing remainder and final bearing
return bn return bn
def calculate_heading_longpath(locator1, locator2): def calculate_heading_longpath(locator1, locator2):
"""calculates the heading from the first to the second locator (long path) """calculates the heading from the first to the second locator (long path)
@ -248,22 +249,22 @@ def calculate_heading_longpath(locator1, locator2):
Raises: Raises:
ValueError: When called with wrong or invalid input arg ValueError: When called with wrong or invalid input arg
AttributeError: When args are not a string AttributeError: When args are not a string
Example: Example:
The following calculates the long path heading from locator1 to locator2 The following calculates the long path heading from locator1 to locator2
>>> from pyhamtools.locator import calculate_heading_longpath >>> from pyhamtools.locator import calculate_heading_longpath
>>> calculate_heading_longpath("JN48QM", "QF67bf") >>> calculate_heading_longpath("JN48QM", "QF67bf")
254.3136 254.3136
""" """
heading = calculate_heading(locator1, locator2) heading = calculate_heading(locator1, locator2)
lp = (heading + 180)%360 lp = (heading + 180)%360
return lp return lp
def calculate_sunrise_sunset(locator, calc_date=datetime.utcnow()): def calculate_sunrise_sunset(locator, calc_date=datetime.utcnow()):
"""calculates the next sunset and sunrise for a Maidenhead locator at a give date & time """calculates the next sunset and sunrise for a Maidenhead locator at a give date & time
@ -277,7 +278,7 @@ def calculate_sunrise_sunset(locator, calc_date=datetime.utcnow()):
Raises: Raises:
ValueError: When called with wrong or invalid input arg ValueError: When called with wrong or invalid input arg
AttributeError: When args are not a string AttributeError: When args are not a string
Example: Example:
The following calculates the next sunrise & sunset for JN48QM on the 1./Jan/2014 The following calculates the next sunrise & sunset for JN48QM on the 1./Jan/2014
@ -288,20 +289,20 @@ def calculate_sunrise_sunset(locator, calc_date=datetime.utcnow()):
>>> myDate = datetime(year=2014, month=1, day=1, tzinfo=UTC) >>> myDate = datetime(year=2014, month=1, day=1, tzinfo=UTC)
>>> calculate_sunrise_sunset("JN48QM", myDate) >>> calculate_sunrise_sunset("JN48QM", myDate)
{ {
'morning_dawn': datetime.datetime(2014, 1, 1, 6, 36, 51, 710524, tzinfo=<UTC>), 'morning_dawn': datetime.datetime(2014, 1, 1, 6, 36, 51, 710524, tzinfo=<UTC>),
'sunset': datetime.datetime(2014, 1, 1, 16, 15, 23, 31016, tzinfo=<UTC>), 'sunset': datetime.datetime(2014, 1, 1, 16, 15, 23, 31016, tzinfo=<UTC>),
'evening_dawn': datetime.datetime(2014, 1, 1, 15, 38, 8, 355315, tzinfo=<UTC>), 'evening_dawn': datetime.datetime(2014, 1, 1, 15, 38, 8, 355315, tzinfo=<UTC>),
'sunrise': datetime.datetime(2014, 1, 1, 7, 14, 6, 162063, tzinfo=<UTC>) 'sunrise': datetime.datetime(2014, 1, 1, 7, 14, 6, 162063, tzinfo=<UTC>)
} }
""" """
morning_dawn = None morning_dawn = None
sunrise = None sunrise = None
evening_dawn = None evening_dawn = None
sunset = None sunset = None
latitude, longitude = locator_to_latlong(locator) latitude, longitude = locator_to_latlong(locator)
if type(calc_date) != datetime: if type(calc_date) != datetime:
raise ValueError raise ValueError
@ -311,9 +312,9 @@ def calculate_sunrise_sunset(locator, calc_date=datetime.utcnow()):
home.lat = str(latitude) home.lat = str(latitude)
home.long = str(longitude) home.long = str(longitude)
home.date = calc_date home.date = calc_date
sun.compute(home) sun.compute(home)
try: try:
nextrise = home.next_rising(sun) nextrise = home.next_rising(sun)
nextset = home.next_setting(sun) nextset = home.next_setting(sun)
@ -339,13 +340,13 @@ def calculate_sunrise_sunset(locator, calc_date=datetime.utcnow()):
sunrise = None sunrise = None
evening_dawn = None evening_dawn = None
sunset = None sunset = None
result = {} result = {}
result['morning_dawn'] = morning_dawn result['morning_dawn'] = morning_dawn
result['sunrise'] = sunrise result['sunrise'] = sunrise
result['evening_dawn'] = evening_dawn result['evening_dawn'] = evening_dawn
result['sunset'] = sunset result['sunset'] = sunset
if morning_dawn: if morning_dawn:
result['morning_dawn'] = morning_dawn.replace(tzinfo=UTC) result['morning_dawn'] = morning_dawn.replace(tzinfo=UTC)
if sunrise: if sunrise:
@ -355,4 +356,3 @@ def calculate_sunrise_sunset(locator, calc_date=datetime.utcnow()):
if sunset: if sunset:
result['sunset'] = sunset.replace(tzinfo=UTC) result['sunset'] = sunset.replace(tzinfo=UTC)
return result return result