From 00e6196a26de0bdbdda3a04ba3dbff78d81e2077 Mon Sep 17 00:00:00 2001
From: David Robert Verelst <dave@dtu.dk>
Date: Thu, 11 Oct 2018 13:01:42 +0200
Subject: [PATCH] prepost.hawcstab2: load_operation returns result, added test
 + example

---
 examples/read-h2-hs2.py              | 68 ++++++++++++++++++++++++++++
 wetb/prepost/hawcstab2.py            |  9 ++--
 wetb/prepost/tests/data/dtu10mw.opt  | 22 +++++++++
 wetb/prepost/tests/data/kb6.opt      | 23 ++++++++++
 wetb/prepost/tests/test_hawcstab2.py | 27 +++++++++++
 5 files changed, 144 insertions(+), 5 deletions(-)
 create mode 100644 examples/read-h2-hs2.py
 create mode 100755 wetb/prepost/tests/data/dtu10mw.opt
 create mode 100644 wetb/prepost/tests/data/kb6.opt

diff --git a/examples/read-h2-hs2.py b/examples/read-h2-hs2.py
new file mode 100644
index 0000000..fb42683
--- /dev/null
+++ b/examples/read-h2-hs2.py
@@ -0,0 +1,68 @@
+"""
+Created on Wed Oct 10 12:47:10 2018
+
+@author: dave
+"""
+
+import os
+
+from wetb.prepost import windIO
+from wetb.hawc2 import Hawc2io
+from wetb.prepost import hawcstab2
+
+# =============================================================================
+# READ HAWC2 RESULT FILE
+# =============================================================================
+
+# METHOD A
+fname = '../wetb/hawc2/tests/test_files/hawc2io/Hawc2ascii.sel'
+res = Hawc2io.ReadHawc2(fname)
+sig = res.ReadAll()
+
+# METHOD B
+path, file = os.path.dirname(fname), os.path.basename(fname)
+res = windIO.LoadResults(path, file)
+sig = res.sig
+sel = res.ch_details
+# result in dataframe with unique channel names (instead of indices)
+sig_df = res.sig2df()
+ch_df = res.ch_df
+
+# =============================================================================
+# READ HAWCStab2 files
+# =============================================================================
+
+res = hawcstab2.results()
+
+fname = '../wetb/prepost/tests/data/campbell_diagram.cmb'
+df_cmb = res.load_cmb_df(fname)
+
+fname = '../wetb/prepost/tests/data/dtu10mw_v1_defl_u10000.ind'
+df_ind = res.load_ind(fname)
+
+fname = '../wetb/prepost/tests/data/dtu10mw.opt'
+df_opt = res.load_operation(fname)
+
+fname = '../wetb/prepost/tests/data/dtu10mw_v1.pwr'
+df_pwr, units = res.load_pwr_df(fname)
+
+fname = '../wetb/prepost/tests/data/controller_input_quadratic.txt'
+tuning = hawcstab2.ReadControlTuning()
+tuning.read_parameters(fname)
+# tuning parameters are saved as attributes
+tuning.pi_gen_reg1.K
+tuning.pi_gen_reg2.I
+tuning.pi_gen_reg2.Kp
+tuning.pi_gen_reg2.Ki
+tuning.pi_gen_reg2.Kd
+tuning.pi_pitch_reg3.Kp
+tuning.pi_pitch_reg3.Ki
+tuning.pi_pitch_reg3.Kd
+tuning.pi_pitch_reg3.K1
+tuning.pi_pitch_reg3.K2
+tuning.aero_damp.Kp2
+tuning.aero_damp.Ko1
+tuning.aero_damp.Ko2
+
+# or you can get them as a dictionary
+tune_tags = tuning.parameters2tags()
diff --git a/wetb/prepost/hawcstab2.py b/wetb/prepost/hawcstab2.py
index 62f812f..c727eef 100644
--- a/wetb/prepost/hawcstab2.py
+++ b/wetb/prepost/hawcstab2.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
 """
 Created on Tue Jan 14 14:12:58 2014
 
@@ -261,19 +260,19 @@ class results(object):
         # when the array is empty, set operation to an empty DataFrame
         if len(operation) == 0:
             cols = ['windspeed', 'pitch_deg', 'rotorspeed_rpm']
-            self.operation = pd.DataFrame(columns=cols)
-            return
+            return pd.DataFrame(columns=cols)
         # when there is only one data point, the array is 1D, we allways need
         # a 2D array otherwise the columns become rows in the DataFrame
         elif len(operation.shape) == 1:
             operation = operation.reshape((1, operation.shape[0]))
         try:
             cols = ['windspeed', 'pitch_deg', 'rotorspeed_rpm']
-            self.operation = pd.DataFrame(operation, columns=cols)
+            operation = pd.DataFrame(operation, columns=cols)
         except ValueError:
             cols = ['windspeed', 'pitch_deg', 'rotorspeed_rpm', 'P_aero',
                     'T_aero']
-            self.operation = pd.DataFrame(operation, columns=cols)
+            operation = pd.DataFrame(operation, columns=cols)
+        return operation
 
     def load_matrices(self, fpath, basename, operating_point=1,
                       control_mat=False, local_wind_mat=False):
diff --git a/wetb/prepost/tests/data/dtu10mw.opt b/wetb/prepost/tests/data/dtu10mw.opt
new file mode 100755
index 0000000..7fe1743
--- /dev/null
+++ b/wetb/prepost/tests/data/dtu10mw.opt
@@ -0,0 +1,22 @@
+  21 wind speed [m/s]     pitch [deg]     rot. speed [rpm]     
+             5.0             1.52             6.00     
+             6.0             0.46             6.00     
+             7.0             0.00             6.37     
+             8.0             0.00             7.28     
+             9.0             0.00             8.19     
+            10.0             0.00             9.10     
+            11.0             0.00             9.60     
+            12.0             4.10             9.60     
+            13.0             6.69             9.60     
+            14.0             8.62             9.60     
+            15.0            10.26             9.60     
+            16.0            11.74             9.60     
+            17.0            13.10             9.60     
+            18.0            14.38             9.60     
+            19.0            15.59             9.60     
+            20.0            16.76             9.60     
+            21.0            17.88             9.60     
+            22.0            18.97             9.60     
+            23.0            20.03             9.60     
+            24.0            21.05             9.60     
+            25.0            22.05             9.60     
diff --git a/wetb/prepost/tests/data/kb6.opt b/wetb/prepost/tests/data/kb6.opt
new file mode 100644
index 0000000..3350497
--- /dev/null
+++ b/wetb/prepost/tests/data/kb6.opt
@@ -0,0 +1,23 @@
+  22    wind speed [m/s]             pitch [deg]        rot. speed [rpm]         aero power [kw]        aero thrust [kn]
+  0.4000000000000000E+01  0.5349309632994260E-03  0.3385905837982068E+02  0.7201472127920620E+01  0.3238197412297314E+01
+  0.5000000000000000E+01  0.5349309755366098E-03  0.4210546666682672E+02  0.1415607774011505E+02  0.5017229492571652E+01
+  0.6000000000000000E+01  0.5349309755382001E-03  0.5043274240183678E+02  0.2459236895692140E+02  0.7182292864353710E+01
+  0.7000000000000000E+01  0.5349309755382001E-03  0.5876454811562412E+02  0.3923045950432546E+02  0.9727470498252524E+01
+  0.8000000000000000E+01  0.5349309755382001E-03  0.6711475958726750E+02  0.5879853446478288E+02  0.1266120196263318E+02
+  0.9000000000000000E+01  0.5349309755382001E-03  0.7000000000000000E+02  0.8323079728956920E+02  0.1498841355250623E+02
+  0.1000000000000000E+02  0.1163755866416270E+01  0.7000000000000000E+02  0.1064017983536164E+03  0.1559515367779657E+02
+  0.1100000000000000E+02  0.5244219122437820E+01  0.7000000000000000E+02  0.1064000990173816E+03  0.1261946763533536E+02
+  0.1200000000000000E+02  0.7805516131255263E+01  0.7000000000000000E+02  0.1064034294717032E+03  0.1109351229548101E+02
+  0.1300000000000000E+02  0.9888742159166926E+01  0.7000000000000000E+02  0.1063986284106002E+03  0.1004076469859912E+02
+  0.1400000000000000E+02  0.1171931406170756E+02  0.7000000000000000E+02  0.1063986146999500E+03  0.9242429622326702E+01
+  0.1500000000000000E+02  0.1338395969300175E+02  0.7000000000000000E+02  0.1063937200866419E+03  0.8609049584190897E+01
+  0.1600000000000000E+02  0.1493218874586698E+02  0.7000000000000000E+02  0.1064043646466927E+03  0.8091730136938368E+01
+  0.1700000000000000E+02  0.1639469013123552E+02  0.7000000000000000E+02  0.1064013899168824E+03  0.7658983927004478E+01
+  0.1800000000000000E+02  0.1778792307025338E+02  0.7000000000000000E+02  0.1064047949236002E+03  0.7292116562690632E+01
+  0.1900000000000000E+02  0.1912315185121887E+02  0.7000000000000000E+02  0.1063985234138157E+03  0.6977317857551209E+01
+  0.2000000000000000E+02  0.2040918213796199E+02  0.7000000000000000E+02  0.1064031491365678E+03  0.6705055846501972E+01
+  0.2100000000000000E+02  0.2165209915924424E+02  0.7000000000000000E+02  0.1063998138679043E+03  0.6468359027748811E+01
+  0.2200000000000000E+02  0.2285648140629532E+02  0.7000000000000000E+02  0.1064240428325993E+03  0.6262034347042015E+01
+  0.2300000000000000E+02  0.2402752818115222E+02  0.7000000000000000E+02  0.1064005848616629E+03  0.6078053602135600E+01
+  0.2400000000000000E+02  0.2516336964663254E+02  0.7000000000000000E+02  0.1064334951382803E+03  0.5918201578325677E+01
+  0.2500000000000000E+02  0.2627214012465693E+02  0.7000000000000000E+02  0.1064694335445150E+03  0.5775852683705587E+01
diff --git a/wetb/prepost/tests/test_hawcstab2.py b/wetb/prepost/tests/test_hawcstab2.py
index 20c95d4..e8b6f1a 100644
--- a/wetb/prepost/tests/test_hawcstab2.py
+++ b/wetb/prepost/tests/test_hawcstab2.py
@@ -157,6 +157,33 @@ class Tests(unittest.TestCase):
             self.assertEqual(data.shape, df_data.shape)
             np.testing.assert_allclose(data, df_data.values)
 
+    def test_opt_file(self):
+
+        res = results()
+
+        fname = pjoin(pdirname(__file__), 'data', 'dtu10mw.opt')
+        df = res.load_operation(fname)
+        tmp = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
+                21, 22, 23, 24, 25]
+        np.testing.assert_allclose(tmp, df['windspeed'].values)
+        self.assertEqual(df.values.shape, (21, 3))
+
+        fname = pjoin(pdirname(__file__), 'data', 'kb6.opt')
+        df = res.load_operation(fname)
+        tmp = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
+                21, 22, 23, 24, 25]
+        np.testing.assert_allclose(tmp, df['windspeed'].values)
+        tmp = [7.20147212792062, 14.1560777401151, 24.5923689569214,
+               39.2304595043255, 58.7985344647829, 83.2307972895692,
+               106.401798353616, 106.400099017382, 106.403429471703,
+               106.3986284106, 106.39861469995, 106.393720086642,
+               106.404364646693, 106.401389916882, 106.4047949236,
+               106.398523413816, 106.403149136568, 106.399813867904,
+               106.424042832599, 106.400584861663, 106.43349513828,
+               106.469433544515]
+        np.testing.assert_allclose(tmp, df['P_aero'].values)
+        self.assertEqual(df.values.shape, (22, 5))
+
 
 if __name__ == "__main__":
     unittest.main()
-- 
GitLab