BeamHeading.03.py Python Code |
Calculating Distance and Beam Heading between two (2) Locations. |
Lester, Pennsylvania - US; Latitude: 39.86923, Longitude: -75.28974 |
Friedrichshafen, Bavaria - Deutschland; Latitude: 47.6572, Longitude: 9.4785 |
""" Python Code written: 16 April 2013. Antenna Heading - Enter Latitudes and Longitudes to determine Beam Heading. Based on March 2007 QST Article 'Calculating Antenna Headings'. Note. Latitudes and Longitudes are entered in 'Decimal' form, not 'Degree Minute Second' form. 01. Convert Latitude and Longitude from degrees to radians. lat1 = latitude1 x pi / 180, lon1 = longitude1 x pi / 180, lat1 = latitude1 x pi / 180, lon2 = longitude2 x pi / 180, 02. Distance Calculation: distance (in radians) = acos( (sin(lat1) x sin(lat2)) + (cos(lat1) x cos(lat2)) x cos(lon1 - lon2) 03. Convert Short Distance from radians to Nautical Miles, to Statue Miles, to kilometers. spnm = distance (in radians) x 10800 / pi Short Path (in nautical miles spsm = distance (in nautical miles)) * 1.15 Short Path (in statute miles) spkm = distance (in statute miles) x 1.609344 Short Path (in kilometers) 04. Calculate Long Distance from Short Distance. Earth assumed to be a perfect Sphere. lpsm = 24856.9 - (Short Distance (in statute miles)) Long Path (in statute miles) lpnm = (Short Distance (in statute miles)) / 1.15 Long Path (in nautical miles) lpkm = 40003.302874 - (Short Distance (in kilometers)) Long Path (in kilometers) 05. Test for which direction is the Short Path, or Long Path: test = sin(lon1 - lon2) 06. Calculate Bearing: if test < 0 then bearing (in radians) = acos( (sin(lat2) - (sin(lat1 x cos(distance)))) / (sin(distance) x cos(lat1)) ) bearing (in radians) = 2 x pi - acos( (sin(lat2) - (sin(lat1 x cos(distance)))) / (sin(distance) x cos(lat1)) ) 07. Convert Bearing from radians to degrees. bearing (in degrees) = bearing (in radians) x 180 / pi """ # --------------------------------------------------------------------------------------------------------------------- # # Library Imports # Allows Python 2.x to use Python 3.x 'print()' Function correctly. This Line must be before all other 'import' Entries. from __future__ import print_function # 'import' UNIX 'os' Library, and use 'os.system' to call 'clear' command. import os; os.system('clear'); # 'import' 'sys' Library, to access System level functions. import sys; # 'import' 'math' Library, to access Trigonometric functions. import math; # 'import' 're' Library, to access Regular Expression functions. import re; # --------------------------------------------------------------------------------------------------------------------- # # User Defined Functions: def DegreesToRadians(tDegrees): return ((float(tDegrees) * math.pi) / 180.0) def RadiansToDegrees(tRadians): return ((float(tRadians) * 180.0) / math.pi) def RadiansToNaticalMiles(tRadians): return ((float(tRadians) * 10800.0 ) / math.pi) # 10800 = 180 * 60 def NaticalMilesToStatueMiles(tNaticalMiles): return (float(tNaticalMiles) * 1.15) def StatuteMilesToNaticalMiles(tStatueMiles): return (float(tStatueMiles) / 1.15) def StatuteMilesToKilometers(tStatueMiles): return (float(tStatueMiles) * 1.609344) # --------------------------------------------------------------------------------------------------------------------- # # Code starts here. # Determine Version of Python: pv = sys.version_info[:][0] # 'sys.version_info[:]' returns, for example, (2, 6, 1, 'final', 0) or (3, 3, 1, 'final', 0). # Obtain Latitude and Longitude from both Locations. if (pv < 3): station = raw_input ("Your Latitude and Longitude (in Degrees): "); # Un-Comment if you want to enter Station Location. station = station.replace(",", "") # Remove any ',' if entered. contact = raw_input ("Contacts' Latitude and Longitude (in Degrees): "); contact = contact.replace(",", "") # Remove any ',' if entered. else: station = input ("Your Latitude and Longitude (in Degrees): "); # Un-Comment if you want to enter Station Location. station = station.replace(",", "") # Remove any ',' if entered. contact = input ("Contacts' Latitude and Longitude (in Degrees): "); contact = contact.replace(",", "") # Remove any ',' if entered. # Extract Latitude and Longitude from strings 'station' and 'contact'. stationList = re.sub("[(-*)][^\w]", " ", station).split() contactList = re.sub("[(-*)][^\w]", " ", contact).split() # Place 'station's Latitude and Longitude into respective Variables. stationLatitude = stationList[0] stationLongitude = stationList[1] # Place 'contact's Latitude and Longitude into respective Variables. contactLatitude = contactList[0] contactLongitude = contactList[1] # Convert Latitude and Longitude from Degrees to Radians. lat1 = DegreesToRadians(stationLatitude) lon1 = DegreesToRadians(stationLongitude) lat2 = DegreesToRadians(contactLatitude) lon2 = DegreesToRadians(contactLongitude) # Distance (in radians) between the two 2) locations. d = math.acos((math.sin(lat1)*math.sin(lat2))+(math.cos(lat1)*math.cos(lat2))*(math.cos(lon1-lon2))) spnm = RadiansToNaticalMiles(d) spsm = NaticalMilesToStatueMiles(spnm) spkm = StatuteMilesToKilometers(spsm) # Long Path Calculations. Earth assumed to be a perfect Sphere. lpsm = (24856.9 - spsm) lpnm = StatuteMilesToNaticalMiles(lpsm) lpkm = (40003.302874 - spkm) # 40003.302874 = (24856.9 * 1.609344) # Calculate the Bearing Value. b1 = math.acos((math.sin(lat2)-(math.sin(lat1)*math.cos(d)))/(math.sin(d)*math.cos(lat1))) b2 = (2*math.pi)-b1 # Determine Short Path Bearing Value. if ((math.sin(lon1-lon2)) >= 0): tmp = b1 b1 = b2 b2 = tmp # Round out the Bearing Values. b1d = math.ceil(RadiansToDegrees(b1)) b2d = math.ceil(RadiansToDegrees(b2)) # Display the calculated Values. print() print("Start (Lat., Long.): ", str(stationLatitude), ", ", str(stationLongitude)) print(" End (Lat., Long.): ", str(contactLatitude), ", ", str(contactLongitude)) print() print("Short Path: ") print(" Bearing: ", str(b1d), " degrees.") print("Distance: ", str(round(spkm,2)), " km.") print("Distance: ", str(round(spsm,2)), " Statute Miles.") print("Distance: ", str(round(spnm,2)), " Natical Miles.") print() print("Long Path: ") print(" Bearing: ", str(b2d), " degrees.") print("Distance: ", str(round(lpkm,2)), " km.") print("Distance: ", str(round(lpsm,2)), " Statute Miles.") print("Distance: ", str(round(lpnm,2)), " Natical Miles.") print() # Code ends here. # --------------------------------------------------------------------------------------------------------------------- # |
Download, and use, the actual code provided here. |
Return to Python Code |
©2008 - 2099, Alle Rechte vorbehalten, SJWL |