From b9e38beea32be60fc641c44e48f0ad2d051b82a6 Mon Sep 17 00:00:00 2001
From: "Mads M. Pedersen" <mmpe@dtu.dk>
Date: Fri, 4 May 2018 12:01:24 +0200
Subject: [PATCH] lib_reader.py and fuga tests

---
 tests/test_fuga/__init__.py                   |  0
 tests/test_fuga/test_lib_reader.py            | 27 +++++++++++
 .../tests => tests/test_fuga}/test_pyfuga.py  | 16 +++----
 topfarm/cost_models/fuga/lib_reader.py        | 46 +++++++++++++++++++
 4 files changed, 79 insertions(+), 10 deletions(-)
 create mode 100644 tests/test_fuga/__init__.py
 create mode 100644 tests/test_fuga/test_lib_reader.py
 rename {topfarm/cost_models/fuga/tests => tests/test_fuga}/test_pyfuga.py (90%)
 create mode 100644 topfarm/cost_models/fuga/lib_reader.py

diff --git a/tests/test_fuga/__init__.py b/tests/test_fuga/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/test_fuga/test_lib_reader.py b/tests/test_fuga/test_lib_reader.py
new file mode 100644
index 00000000..ef84943d
--- /dev/null
+++ b/tests/test_fuga/test_lib_reader.py
@@ -0,0 +1,27 @@
+'''
+Created on 25. apr. 2018
+
+@author: mmpe
+'''
+import os
+import unittest
+from topfarm.cost_models.fuga.lib_reader import read_lib
+import numpy as np
+from topfarm.cost_models.fuga import py_fuga
+
+
+class Test(unittest.TestCase):
+
+    def test_lib_reader(self):
+        f, A, k = read_lib(os.path.dirname(py_fuga.__file__) + "/Colonel/LUT/Farms/Horns Rev 1/hornsrev2.lib")
+        np.testing.assert_array_almost_equal(f, [0.035972, 0.039487, 0.051674, 0.070002, 0.083645, 0.064349,
+                                                 0.086432, 0.117705, 0.151576, 0.147379, 0.100121, 0.05166])
+        np.testing.assert_array_almost_equal(A, [9.176929, 9.782334, 9.531809, 9.909545, 10.04269, 9.593921,
+                                                 9.584007, 10.51499, 11.39895, 11.68746, 11.63732, 10.08803])
+        np.testing.assert_array_almost_equal(k, [2.392578, 2.447266, 2.412109, 2.591797, 2.755859, 2.595703,
+                                                 2.583984, 2.548828, 2.470703, 2.607422, 2.626953, 2.326172])
+
+
+if __name__ == "__main__":
+    #import sys;sys.argv = ['', 'Test.test_lib_reader']
+    unittest.main()
diff --git a/topfarm/cost_models/fuga/tests/test_pyfuga.py b/tests/test_fuga/test_pyfuga.py
similarity index 90%
rename from topfarm/cost_models/fuga/tests/test_pyfuga.py
rename to tests/test_fuga/test_pyfuga.py
index e26750d6..aaa304cf 100644
--- a/topfarm/cost_models/fuga/tests/test_pyfuga.py
+++ b/tests/test_fuga/test_pyfuga.py
@@ -33,13 +33,9 @@ def test_parallel(id):
 
 class Test(unittest.TestCase):
 
-    @classmethod
-    def setUpClass(cls):
-        super(Test, cls).setUpClass()
-
-    @classmethod
-    def tearDownClass(cls):
-        super(Test, cls).tearDownClass()
+    def lib_missing(self):
+        lib_path = os.path.dirname(py_fuga.__file__) + "/Colonel/FugaLib/FugaLib.%s" % ('so', 'dll')[os.name == 'nt']
+        return os.path.isfile(lib_path)
 
     def get_fuga(self, tb_x=[423974, 424033], tb_y=[6151447, 6150889]):
         return PyFuga(farm_name='Horns Rev 1',
@@ -51,8 +47,8 @@ class Test(unittest.TestCase):
     def testCheckVersion(self):
         lib = PascalDLL(fuga_path + "FugaLib/FugaLib.%s" % ('so', 'dll')[os.name == 'nt'])
         self.assertRaisesRegex(Exception, "This version of FugaLib supports interface version ", lib.CheckInterfaceVersion, 1)
-        # PyFuga(fuga_path + "FugaLib/FugaLib.dll", fuga_path + "LUT/Farms/", "Horns Rev 1", fuga_path + "LUT/",
-        #                (0, 0, 70), 0.0001, 400, 0, 'Horns Rev 1\hornsrev0.lib')
+        pyFuga = self.get_fuga()  # check that current interface version match
+        pyFuga.cleanup()
 
     def testSetup(self):
         pyFuga = self.get_fuga()
@@ -67,6 +63,7 @@ class Test(unittest.TestCase):
     def testAEP_one_tb(self):
         pyFuga = self.get_fuga([0], [0])
         np.testing.assert_array_almost_equal(pyFuga.get_aep(np.array([[0], [0]]).T), [7.44121, 7.44121, 0.424962, 1.])
+        pyFuga.cleanup()
 
     def testAEP(self):
         pyFuga = self.get_fuga()
@@ -91,5 +88,4 @@ class Test(unittest.TestCase):
 
 
 if __name__ == "__main__":
-    #import sys;sys.argv = ['', 'Test.testAEP']
     unittest.main()
diff --git a/topfarm/cost_models/fuga/lib_reader.py b/topfarm/cost_models/fuga/lib_reader.py
new file mode 100644
index 00000000..89fd410b
--- /dev/null
+++ b/topfarm/cost_models/fuga/lib_reader.py
@@ -0,0 +1,46 @@
+'''
+Created on 25. apr. 2018
+
+@author: mmpe
+'''
+import os
+import numpy as np
+
+def read_lib(filename):
+    with open(filename) as fid:
+        lines = fid.readlines()
+
+    descriptor = lines[0]
+    nRoughnessClasses, nHeights, nSectorslib = map(int, lines[2].split())
+    z0ref_lst = list(map(float, lines[4].split()))
+    
+    # TODO: Implement for specified z0 and height
+
+#
+#
+#     for i:=0 to nRoughnessClasses-1 do read(fil,z0reflib[i]);
+#       readln(fil);
+#       for i:=0 to nHeights-1 do read(fil,zreflib[i]);
+#       readln(fil);
+#       for k:=0 to nRoughnessClasses-1 do
+#       begin
+#         for i:=0 to nSectorslib-1 do
+#         begin
+#           read(fil,freq[k,i]);
+#           freq[k,i]:=freq[k,i]/100;
+#         end;
+#         readln(fil);
+#         for i:=0 to nHeights-1 do
+#         begin
+#           for j:=0 to nSectorslib-1 do read(fil,WAlib[k,i,j]);
+#           readln(fil);
+#           for j:=0 to nSectorslib-1 do read(fil,Wklib[k,i,j]);
+#           readln(fil);
+#         end;
+#       end;
+    f,A,k = [np.array(lines[i].split()[:nSectorslib], dtype=np.float) for i in [8, 10, 12]]
+    return f/100,A,k
+
+
+if __name__ == '__main__':
+    print(read_lib(os.path.dirname(__file__) + "/Colonel/LUT/Farms/Horns Rev 1/hornsrev2.lib"))
-- 
GitLab