From abdefd268f5bc7dd1e86b8fa47af7c03cb32012c Mon Sep 17 00:00:00 2001
From: David Robert Verelst <dave@dtu.dk>
Date: Mon, 13 Feb 2017 20:38:49 +0100
Subject: [PATCH] matrix reader for HS2, rename hs2_control_tuning to
 ReadControlTuning

---
 wetb/prepost/h2_vs_hs2.py            |  2 +-
 wetb/prepost/hawcstab2.py            | 71 +++++++++++++++++++++++++---
 wetb/prepost/tests/test_hawcstab2.py |  6 +--
 3 files changed, 68 insertions(+), 11 deletions(-)

diff --git a/wetb/prepost/h2_vs_hs2.py b/wetb/prepost/h2_vs_hs2.py
index b9d41578..cd065486 100644
--- a/wetb/prepost/h2_vs_hs2.py
+++ b/wetb/prepost/h2_vs_hs2.py
@@ -442,7 +442,7 @@ class Sims(object):
         """
         Read a HAWCStab2 controller tuning file and return as tags
         """
-        tuning = hs2.hs2_control_tuning()
+        tuning = hs2.ReadControlTuning()
         tuning.read_parameters(fpath)
 
         tune_tags = {}
diff --git a/wetb/prepost/hawcstab2.py b/wetb/prepost/hawcstab2.py
index 00f705e9..1129dc7e 100644
--- a/wetb/prepost/hawcstab2.py
+++ b/wetb/prepost/hawcstab2.py
@@ -15,9 +15,6 @@ from future import standard_library
 standard_library.install_aliases()
 from builtins import object
 
-
-
-import unittest
 import os
 import re
 
@@ -28,8 +25,8 @@ from wetb.prepost import mplutils
 
 
 class dummy(object):
-    def __init__(self):
-        pass
+    def __init__(self, name='dummy'):
+        self.__name__ = name
 
 
 def ReadFileHAWCStab2Header(fname, widths):
@@ -172,6 +169,66 @@ class results(object):
                     'T_aero']
             self.operation = pd.DataFrame(operation, columns=cols)
 
+    def load_matrices(self, fpath, basename, operating_point=1,
+                      control_mat=False, local_wind_mat=False):
+        """Load HAWCStab2 State Space system matrices
+
+        The general file name format is:
+        BASENAMETYPE_ase_ops_OPERATING_POINT_NUMBER.dat
+
+        Where TYPE can be of the following:
+            * amat, bmat, bvmat, cmat, dmat, dvmat, emat, fmat, fvmat
+
+        Additionally, when including the control matrices:
+            * BASENAMETYPE_ops_OPERATING_POINT_NUMBER.dat
+            * TYPE: acmat, bcmat, ccmat, dcmat
+
+        Or when including local wind speed
+            * BASENAMETYPE_ase_ops_OPERATING_POINT_NUMBER.dat
+            * TYPE: bvmat_loc_v, dvmat_loc_v, fvmat_loc_v
+
+
+        Parameters
+        ----------
+
+        fpath : str
+
+        basename : str
+
+        operating_point : int, default=1
+
+
+        Returns
+        -------
+
+        matrices : dict
+
+        """
+        mnames = ['amat', 'bmat', 'bvmat', 'cmat', 'dmat', 'dvmat', 'emat',
+                  'fmat', 'fvmat']
+        mnames_c = ['acmat', 'bcmat', 'ccmat', 'dcmat']
+        mnames_v = ['bvmat_loc_v', 'dvmat_loc_v', 'fvmat_loc_v']
+
+        if control_mat:
+            mnames += mnames_c
+        if local_wind_mat:
+            mnames += mnames_v
+
+        matrices = {}
+
+        ase_template = '{:s}{:s}_ase_ops_{:d}.dat'
+        ops_template = '{:s}{:s}_ops_{:d}.dat'
+
+        for mname in mnames:
+            rpl = (basename, mname, operating_point)
+            template = ase_template
+            if mname in mnames_c:
+                template = ops_template
+            fname = os.path.join(fpath, template.format(*rpl))
+            matrices[mname] = np.loadtxt(fname)
+
+        return matrices
+
     def write_ae_sections_h2(self):
         """
         Get the aerosection positions from the HS2 ind result file and
@@ -237,7 +294,7 @@ class results(object):
         print('done!')
 
 
-class hs2_control_tuning(object):
+class ReadControlTuning(object):
 
     def __init__(self):
         """
@@ -293,4 +350,4 @@ class hs2_control_tuning(object):
 
 if __name__ == '__main__':
 
-    unittest.main()
+    pass
diff --git a/wetb/prepost/tests/test_hawcstab2.py b/wetb/prepost/tests/test_hawcstab2.py
index 249df209..90d623ef 100644
--- a/wetb/prepost/tests/test_hawcstab2.py
+++ b/wetb/prepost/tests/test_hawcstab2.py
@@ -15,7 +15,7 @@ import os
 
 import numpy as np
 
-from wetb.prepost.hawcstab2 import results, hs2_control_tuning
+from wetb.prepost.hawcstab2 import results, ReadControlTuning
 
 
 class Tests(unittest.TestCase):
@@ -49,7 +49,7 @@ class Tests(unittest.TestCase):
 
     def test_linear_file(self):
 
-        hs2 = hs2_control_tuning()
+        hs2 = ReadControlTuning()
         hs2.read_parameters(self.fpath_linear)
 
         self.assertEqual(hs2.pi_gen_reg1.K, 0.108313E+07)
@@ -69,7 +69,7 @@ class Tests(unittest.TestCase):
 
     def test_quadratic_file(self):
 
-        hs2 = hs2_control_tuning()
+        hs2 = ReadControlTuning()
         hs2.read_parameters(self.fpath_quad)
 
         self.assertEqual(hs2.pi_gen_reg1.K, 0.108313E+07)
-- 
GitLab