-
Notifications
You must be signed in to change notification settings - Fork 1
/
airfoil_data_util.py
executable file
·103 lines (58 loc) · 3.24 KB
/
airfoil_data_util.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 25 11:01:19 2020
@author: pfreitas97
Airfoil_data_util: Filter
"""
import os
import shutil
# ''.join([char for char in first_line if char not in SPECIAL_CHAR]).replace(" ","_") -- removes everything but spaces
# which it replaces with _
def _get_new_filename(absPath,filename):
SPECIAL_CHAR = (",","/",".","\\","%"," ","AIRFOILS","AIRFOIL","__")
newFilename = filename.replace(".dat","").upper()
with open(absPath,"r", encoding="utf-8") as file:
try:
newFilename = file.readline().strip()
except UnicodeDecodeError:
idxDot = filename.find(".")
newFilename = filename[:idxDot].upper()
print("Warning the following airfoil file might be in a non-standard encoding: %s \n" % filename)
for char in SPECIAL_CHAR:
newFilename = newFilename.replace(char,"_")
return newFilename + ".dat"
return newFilename + ".dat"
def rename_airfoils(path,dest_dir="RENAMED_FOLDER"):
''' Utility file to change the filenames of all airfoils from the original to the first line within each file.
E.g. n0012 -> NACA_0012
Params:
airfoil_path: Path to folder where all airfoil .dat files are stored
new_directory: Option to create new folder with renamed airfoils, if it is blank function will override current names
'''
assert(os.path.exists(path))
assigned_names = []
targetPath = path
if len(dest_dir) > 0:
os.mkdir(dest_dir)
targetPath = os.path.join(os.getcwd(),dest_dir)
for r, d, f in os.walk(path):
for file in f:
if '.dat' in file:
currentAbsPath = os.path.join(r,file)
newFilename = _get_new_filename(currentAbsPath,file)
# Checking for duplicate names:
if newFilename in assigned_names:
print("Note original name was kept for: %s to prevent a file from being overwritten \n" % file)
newFilename = file
assigned_names.append(newFilename)
if len(dest_dir) > 0: # If making a new directory, and copying all files there prior to renaming
shutil.copy(currentAbsPath,targetPath)
dest_dir = os.path.join(targetPath,file)
new_Name_dest_dir = os.path.join(targetPath,newFilename)
os.rename(dest_dir,new_Name_dest_dir)
else: # if overwriting old files
new_Name_dest_dir = os.path.join(r,newFilename)
os.rename(currentAbsPath,new_Name_dest_dir)
lists = rename_airfoils(os.path.join(os.getcwd(),"Airfoil_Coordinates"))
#rename_airfoils(os.path.join(os.getcwd(),"Airfoil_Coordinates"), dest_dir="NEWFOIL")