-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaca_aerofoil.py
55 lines (45 loc) · 1.81 KB
/
naca_aerofoil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import urllib2
import numpy as np
from bs4 import BeautifulSoup
class Naca4DigitAerofoil(object):
"""
Object to represent 4 digit NACA aerofoil generated by airfoiltools.com
"""
def __init__(self, camber, camber_x, thickness, n):
self.camber = camber
self.camber_x = camber_x
self.thickness = thickness
self.url = "http://airfoiltools.com/airfoil/naca4digit?" \
"MNaca4DigitForm%5Bcamber%5D={}" \
"&MNaca4DigitForm%5Bposition%5D={}" \
"&MNaca4DigitForm%5Bthick%5D={}" \
"&MNaca4DigitForm%5BnumPoints%5D={}" \
"&MNaca4DigitForm%5BcosSpace%5D=0&MNaca4DigitForm%5BcosSpace%5D=1" \
"&MNaca4DigitForm%5BcloseTe%5D=1&yt0=Plot".format(self.camber, self.camber_x, self.thickness, n - 1)
self.data = np.empty((n, 2))
self.page = None
def __call__(self, *args, **kwargs):
self._get_html()
self._parse_data()
def _get_html(self):
self.page = urllib2.urlopen(self.url)
def _parse_data(self):
soup = BeautifulSoup(self.page, 'html.parser')
dat = soup.find('pre')
dat = dat.text.split('\n')
dat = dat[2:-1] # Fudge to remove header and whitespace rows
for i, row in enumerate(dat):
ascii = row.encode('ascii')
values = ascii.split(' ')
x_y = [float(val) for val in values if len(val)]
self.data[i] = x_y
def write_dat(self):
"""
Function to write .dat file containing x, y coordinates.
"""
data = self.get_data()
data.savetxt('{}.dat'.format(self.get_name()))
def get_data(self):
return self.data
def get_name(self):
return 'NACA{}{}{}'.format(self.camber, self.camber_x, self.thickness)