The text file airports.txt contains
information on all airports of the US. The information of each airport is
on a separate line and consists of the following five fields of
information that are separated by a tab:
code: unique
identification code for the airport
latitude: latitude on
which the airport is located (expressed in decimal degrees)
longitude: longitude on
which the airport is located (expressed in decimal degrees)
city: name of the city in
which the airport is located
state: (abbreviated) name
of the state in which the airport is located
The first line of the file is a header that contains the names of the
different columns.
Assignment
Write a function
that takes the location of the text file airports.txt (or a file
with a similar layout) as an argument. This function must return a
dictionary, where information about the airports can be retrieved
quickly. The unique identifier of the airports is used as a key in this
dictionary, and the associated value is a tuple consisting of the four
information fields (latitude,
longitude, city, state). Here, the latitude and longitude are
to be included as real numbers in the tuple, and the city and country in
which the airport is situated as strings.
Write a function
distance(code1, code2, airports)
that returns the distance between any two airports (expressed in
kilometres). The unique identifiers of two airports should be passed as
an argument to this function along with a dictionary that contains the
information about the airports (in the format as it is returned by the readairports function). To
calculate the distance between two airports, you use the following
formula \[ R\cdot \arctan\left(\frac{\sqrt{(\cos b_2\sin (l_1-l_2))^2 +
(\cos b_1\sin b_2-\sin b_1\cos b_2 \cos(l_1-l_2))^2}}{\sin b_1\sin
b_2+\cos b_1\cos b_2\cos(l_1-l_2)}\right) \] This is actually a formula
that calculates distance between two points on a sphere with a radius
$$R$$. We hereby represent the Earth as a sphere with radius $$R =
6372{,}795$$ km. Furthermore, $$b_1$$ en $$b_2$$ (resp. $$l_1$$ and $$l_2$$)
represent the latitude (resp. longitude) of the two points on the
sphere, expressed in radians. For the conversion of degrees to radians
you should use the value pi
from the math module of
the Python Standard Library.
Write a function
stopover(code1, code2, airports[, scope=4000])
which can be used to determine the optimal flight schedule with a single
stopover (for refueling), in case one wants to fly between two points of
which the distance is greater than the maximum range of the aircraft.
The optimal flight schedule to fly from point $$A$$ to point $$B$$ has a
stopover in an airport at point $$C$$ and where the distance from $$A$$ to
$$C$$ and from $$C$$ to $$B$$ always lies within the range of the aircraft,
and for which the total flight distance is as small as possible. This
function must carry the same three arguments as are passed to the distance function. An optional
fourth argument can also be passed, which indicates the maximum range of
the aircraft (in kilometres). By default, 4000 km is taken as maximum
range. The function should return the unique identifier of the airport
that ensures an optimal flight schedule as a result. The function should
return a value of None, if
the two specified airports are within the range of the aircraft (if it
can be flown directly, in other words), or if a flight schedule with a
single stopover is not possible.
Example
In the following interactive Python session
we assume that the file airports.txt
is located in the current directory.
>>> airports = readairports('airports.txt')
>>> airports
{'AGN': (57.83, 134.97, 'Angoon', 'AK'), ...}
>>> airports['ADK']
(51.88, 176.65, 'Adak', 'AK')
>>> airports['DCA']
(38.85, 77.04, 'Washington/Natl', 'DC')
>>> airports['4OM']
(48.42, 119.53, 'Omak', 'WA')
>>> distance('P60', 'MSN', airports)
1694.54554995
>>> distance('ADK', 'DCA', airports)
7295.50355678
>>> stopover('ADK', 'DCA', airports, 4000)
'4OM'