Finishing the Airfoil Plotter
Accounting for the last corner case.
We'll cover the following...
What if you ask it to plot an airfoil that is not in the database?
Press + to interact
import requestsimport matplotlib.pyplot as pltdef plot_airfoil(x_coordinates: list, y_coordinates: list, plot_title: str) -> None:"""Plot the airfoil coordinates given the list of coordinates:param x_coordinates: list of airfoil's x coordinates:param y_coordinates: list of airfoil's y coordinates:param plot_title: str to title as the plot's title:return: None"""plt.plot(x_coordinates, y_coordinates, 'k-')plt.title('{} airfoil'.format(plot_title))plt.style.use('default')plt.xlim(-0.50, 1.25)plt.ylim(-1, 1)#plt.show()plt.savefig("output/chap4_6.png")def get_airfoil_coords(airfoil: str) -> tuple:"""Get airfoil coords from UIUC websitehttps://m-selig.ae.illinois.edu/ads/coord/__.dat:param airfoil: str of airfoil types:return: tuple of ([x coords], [y coords], plot_title)"""url = 'https://m-selig.ae.illinois.edu/ads/coord/{}.dat'.format(airfoil.lower())response_text = requests.get(url).textall_text = response_text.split('\n')x_coordinates, y_coordinates = [], []plot_title = ''for index, line in enumerate(all_text):if index == 0:plot_title = line.strip()else:try:line = line.strip()x, y = line.split(' ' * line.count(' ')) # line changedx = float(x.strip())y = float(y.strip())if x <= 1.0 and y <= 1.0: # line changedx_coordinates.append(x)y_coordinates.append(y)except ValueError:continuereturn x_coordinates, y_coordinates, plot_titleair_foil = 'not_an_airfoil_name'x_values, y_values, title = get_airfoil_coords(air_foil)plot_airfoil(x_values, y_values, title)
Oops, that is a little embarrassing. If you request a URL that does not exist, the website returns a webpage with a big, bold Not Found at the top. You can screen for this “not found” piece ...