Commit df30e781 authored by David Verelst's avatar David Verelst

add save method to PCFile

parent 74d51824
Pipeline #20323 passed with stages
in 2 minutes and 3 seconds
......@@ -43,9 +43,9 @@ Both Python2 and Python3 are supported.
- [Hawc2io](wetb/hawc2/Hawc2io.py): Read binary, ascii and flex result files
- [sel_file](wetb/hawc2/sel_file.py): Read/write *.sel (sensor list) files
- [htc_file](wetb/hawc2/htc_file.py): Read/write/manipulate htc files
- [ae_file](wetb/hawc2/ae_file.py): Read AE (aerodynamic blade layout) files
- [pc_file](wetb/hawc2/pc_file.py): Read PC (profile coefficient) files
- [st_file](wetb/hawc2/st_file.py): Read ST (structural properties) files
- [ae_file](wetb/hawc2/ae_file.py): Read/write/manipulate AE (aerodynamic blade layout) files
- [pc_file](wetb/hawc2/pc_file.py): Read/write/manipulate PC (profile coefficient) files
- [st_file](wetb/hawc2/st_file.py): Read/write/manipulate ST (structural properties) files
- [shear_file](wetb/hawc2/shear_file.py): Create user defined shear file
- [at_time_file](wetb/hawc2/at_time_file.py): Read at output_at_time files
- [log_file](wetb/hawc2/log_file.py): Read and interpret log files
......
......@@ -13,6 +13,7 @@ from builtins import int
from future import standard_library
standard_library.install_aliases()
import os
import numpy as np
class PCFile(object):
......@@ -28,11 +29,19 @@ class PCFile(object):
>>> pcfile.CM(21,10) # CM for thickness 21% and AOA=10deg
-0.1103
"""
def __init__(self, filename):
with open (filename) as fid:
lines = fid.readlines()
nsets = int(lines[0].split()[0])
def __init__(self, filename=None):
self.pc_sets = {}
if filename is not None:
with open (filename) as fid:
lines = fid.readlines()
self._parse_lines(lines)
self.filename = filename
self.fmt = ' 19.015e'
def _parse_lines(self, lines):
"""Read HAWC2 PC file (profile coefficient file).
"""
nsets = int(lines[0].split()[0])
lptr = 1
for nset in range(1, nsets + 1):
nprofiles = int(lines[lptr].split()[0])
......@@ -61,7 +70,7 @@ class PCFile(object):
Cx1 = np.interp(alpha, Cx1[:, 0], Cx1[:, column])
th0, th1 = thicknesses[index - 1:index + 1]
return Cx0 + (Cx1 - Cx0) * (thickness - th0) / (th1 - th0)
def _CxxxH2(self, thickness, alpha, column, pc_set_nr=1):
thicknesses, profiles = self.pc_sets[pc_set_nr]
index = np.searchsorted(thicknesses, thickness)
......@@ -69,7 +78,7 @@ class PCFile(object):
index = 1
Cx0, Cx1 = profiles[index - 1:index + 1]
Cx0 = np.interp(np.arange(360), Cx0[:,0]+180, Cx0[:,column])
Cx1 = np.interp(np.arange(360), Cx1[:,0]+180, Cx1[:,column])
#Cx0 = np.interp(alpha, Cx0[:, 0], Cx0[:, column])
......@@ -77,8 +86,6 @@ class PCFile(object):
th0, th1 = thicknesses[index - 1:index + 1]
cx = Cx0 + (Cx1 - Cx0) * (thickness - th0) / (th1 - th0)
return np.interp(alpha+180, np.arange(360), cx)
def CL(self, thickness, alpha, pc_set_nr=1):
"""Lift coefficient
......@@ -98,10 +105,9 @@ class PCFile(object):
"""
return self._Cxxx(thickness, alpha, 1, pc_set_nr)
def CL_H2(self, thickness, alpha, pc_set_nr=1):
return self._CxxxH2(thickness, alpha, 1, pc_set_nr)
def CD(self, thickness, alpha, pc_set_nr=1):
"""Drag coefficient
......@@ -123,9 +129,39 @@ class PCFile(object):
def CM(self, thickness, alpha, pc_set_nr=1):
return self._Cxxx(thickness, alpha, 3, pc_set_nr)
def __str__(self, comments=None):
"""This method will create a string that is formatted like a pc file
with the data in this class.
"""
if comments is None:
comments = {}
cols = ['Angle of Attac', 'cl', 'cd', 'cm']
linefmt = ' '.join(['{%i:%s}' % (i, self.fmt) for i in range(len(cols))])
n_sets = len(self.pc_sets)
retval = str(n_sets) + '\n'
for idx_pc, (set_tcs, set_pcs) in self.pc_sets.items():
retval += str(len(set_tcs)) + '\n'
for i, (tc, pc) in enumerate(zip(set_tcs, set_pcs)):
nr = pc.shape[0]
retval += '%i %i %1.08f\n' % (i+1, nr, tc)
for line in pc:
retval += linefmt.format(*line) + '\n'
return retval
def save(self, filename):
if not os.path.isdir(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
with open(filename, 'w') as fid:
fid.write(str(self))
self.filename = filename
if __name__ == "__main__":
pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt")
print (pcfile.CL(21,10)) # CL for thickness 21% and AOA=10deg
#1.358
print (pcfile.CD(21,10)) # CD for thickness 21% and AOA=10deg
......
......@@ -10,13 +10,14 @@ from __future__ import absolute_import
from future import standard_library
from wetb.hawc2.ae_file import AEFile
standard_library.install_aliases()
import os
import unittest
from wetb.hawc2.pc_file import PCFile
import tempfile
import numpy as np
from wetb.hawc2.pc_file import PCFile
class TestPCFile(unittest.TestCase):
......@@ -33,6 +34,22 @@ class TestPCFile(unittest.TestCase):
self.assertEqual(pc.CD(thickness, 10), 0.0255)
self.assertEqual(pc.CM(thickness, 10), -0.1103)
def test_write_PCFile(self):
"""Round trip loading and saving a pc file
"""
pc1 = PCFile(self.testfilepath + "NREL_5MW_pc.txt")
with tempfile.NamedTemporaryFile(delete=True, mode='w') as fid:
pc1.save(fid.name)
pc2 = PCFile(pc1.filename)
self.assertEqual(str(pc1), str(pc2))
tc1, pcs1 = pc1.pc_sets[1]
tc2, pcs2 = pc2.pc_sets[1]
np.testing.assert_array_almost_equal(tc1, tc2)
for pc1, pc2 in zip(pcs1, pcs2):
np.testing.assert_array_almost_equal(pc1, pc2)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment