2

I have downloaded an airfoil from Airfoil Tools. I want to load it into Python to plot it and make some calculations from the geometry.

Edit: example file

To read the file I've tried using this:

with open(filename) as file:
    lines = [line.rstrip() for line in file]

However I encountered many problems because the data is a string so I have to manually convert types and delete columns and such. Is there an easier way to do this? What I want is flexibility to access and manipulate the data.

Tailwhip
  • 33
  • 6
  • 1
    Your question must be self-contained ([ask]), so please [edit] it to include a sample of the file you want to parse. IME these files are usually comma (or other character)-delimited files with a few headers that might need to be ignored, and there are _lots_ of questions addressing how to read these files on Stack Overflow. – Pranav Hosangadi Nov 07 '22 at 21:42

1 Answers1

2

You should really embrace Python libraries to perform certain tasks for which code is already tested and neatly packaged into a library. For your purposes (importing data, array manipulation and operations and plotting) libraries such as Pandas, Numpy and Matplotlib are very common and powerful, there are a bunch of resources on it's capabilities. I really recommend you to check out their websites:

On Airfoil tools you can directly download a .dat file which you can import directly using pandas. Then using simple parameters declared when calling your function will correctly parse the data and return it in the appropriate data type (float instead of str).

Code Sample:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


filename = 'airfoil.dat' #enter your file path here
data = pd.read_table(filename,delim_whitespace=True,skiprows=[0],names=['x','y'],index_col=False)

#Plot
plt.plot(data.x, data.y, 'r',marker='.',markeredgecolor='black', markersize=3)
plt.plot(0.5*data.x+0.3,0.5*data.y) #Scale & translate the datapoints

#Plot settings
plt.axis('equal')
plt.xlim((-0.05, 1.05))
plt.legend(['GOE 383 AIRFOIL','SCALED AIRFOIL'])

Airfoil Plots

Michel G
  • 116
  • 6
  • 1
    That's very nice. Thank you. How can I do this multiple times for multiple files? – Tailwhip Nov 07 '22 at 21:59
  • I suppose you could call each file within a loop. You could check out this [answer](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – Michel G Nov 07 '22 at 22:04