From 246ac8b7fc807d37653cd77de7497b7899206096 Mon Sep 17 00:00:00 2001
From: dave <dave@dtu.dk>
Date: Fri, 19 Aug 2016 11:50:14 +0200
Subject: [PATCH] prepost.windIO: add tests and corresponding files for user
 shear

---
 .../data/a_phi_0.00000_shear_-0.20_wdir00.txt | 30 ++++++++
 .../data/a_phi_0.00000_shear_0.00_wdir-10.txt | 30 ++++++++
 .../data/a_phi_0.50000_shear_0.20_wdir10.txt  | 30 ++++++++
 .../data/a_phi_1.00000_shear_0.00_wdir00.txt  | 30 ++++++++
 .../data/a_phi_None_shear_None_wdirNone.txt   | 30 ++++++++
 wetb/prepost/tests/test_windIO.py             | 68 +++++++++++++++++--
 6 files changed, 213 insertions(+), 5 deletions(-)
 create mode 100644 wetb/prepost/tests/data/a_phi_0.00000_shear_-0.20_wdir00.txt
 create mode 100644 wetb/prepost/tests/data/a_phi_0.00000_shear_0.00_wdir-10.txt
 create mode 100644 wetb/prepost/tests/data/a_phi_0.50000_shear_0.20_wdir10.txt
 create mode 100644 wetb/prepost/tests/data/a_phi_1.00000_shear_0.00_wdir00.txt
 create mode 100644 wetb/prepost/tests/data/a_phi_None_shear_None_wdirNone.txt

diff --git a/wetb/prepost/tests/data/a_phi_0.00000_shear_-0.20_wdir00.txt b/wetb/prepost/tests/data/a_phi_0.00000_shear_-0.20_wdir00.txt
new file mode 100644
index 00000000..e3b9b999
--- /dev/null
+++ b/wetb/prepost/tests/data/a_phi_0.00000_shear_-0.20_wdir00.txt
@@ -0,0 +1,30 @@
+# User defined shear file
+3 5 # nr_hor (v), nr_vert (w)
+# v component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+# u component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 1.20491   1.20491   1.20491
+ 1.04894   1.04894   1.04894
+ 0.96723   0.96723   0.96723
+ 0.91315   0.91315   0.91315
+# w component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+# v coordinates (along the horizontal, nr_hor, 0 rotor center)
+  -57.50
+    0.00
+   57.50
+# w coordinates (zero is at ground level, height, nr_hor)
+    0.00
+   39.38
+   78.75
+  118.12
+  157.50
diff --git a/wetb/prepost/tests/data/a_phi_0.00000_shear_0.00_wdir-10.txt b/wetb/prepost/tests/data/a_phi_0.00000_shear_0.00_wdir-10.txt
new file mode 100644
index 00000000..30c83777
--- /dev/null
+++ b/wetb/prepost/tests/data/a_phi_0.00000_shear_0.00_wdir-10.txt
@@ -0,0 +1,30 @@
+# User defined shear file
+3 5 # nr_hor (v), nr_vert (w)
+# v component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.17365   0.17365   0.17365
+ 0.17365   0.17365   0.17365
+ 0.17365   0.17365   0.17365
+ 0.17365   0.17365   0.17365
+ 0.17365   0.17365   0.17365
+# u component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.98481   0.98481   0.98481
+ 0.98481   0.98481   0.98481
+ 0.98481   0.98481   0.98481
+ 0.98481   0.98481   0.98481
+ 0.98481   0.98481   0.98481
+# w component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+# v coordinates (along the horizontal, nr_hor, 0 rotor center)
+  -57.50
+    0.00
+   57.50
+# w coordinates (zero is at ground level, height, nr_hor)
+    0.00
+   39.38
+   78.75
+  118.12
+  157.50
diff --git a/wetb/prepost/tests/data/a_phi_0.50000_shear_0.20_wdir10.txt b/wetb/prepost/tests/data/a_phi_0.50000_shear_0.20_wdir10.txt
new file mode 100644
index 00000000..48da580f
--- /dev/null
+++ b/wetb/prepost/tests/data/a_phi_0.50000_shear_0.20_wdir10.txt
@@ -0,0 +1,30 @@
+# User defined shear file
+3 5 # nr_hor (v), nr_vert (w)
+# v component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+-0.00000  -0.00000  -0.00000
+-0.22662  -0.22662  -0.22662
+-0.19440  -0.19440  -0.19440
+-0.15702  -0.15702  -0.15702
+-0.12881  -0.12881  -0.12881
+# u component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.79839   0.79839   0.79839
+ 0.93331   0.93331   0.93331
+ 1.02188   1.02188   1.02188
+ 1.08750   1.08750   1.08750
+# w component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+# v coordinates (along the horizontal, nr_hor, 0 rotor center)
+  -57.50
+    0.00
+   57.50
+# w coordinates (zero is at ground level, height, nr_hor)
+    0.00
+   39.38
+   78.75
+  118.12
+  157.50
diff --git a/wetb/prepost/tests/data/a_phi_1.00000_shear_0.00_wdir00.txt b/wetb/prepost/tests/data/a_phi_1.00000_shear_0.00_wdir00.txt
new file mode 100644
index 00000000..90624d10
--- /dev/null
+++ b/wetb/prepost/tests/data/a_phi_1.00000_shear_0.00_wdir00.txt
@@ -0,0 +1,30 @@
+# User defined shear file
+3 5 # nr_hor (v), nr_vert (w)
+# v component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+-0.37387  -0.37387  -0.37387
+-0.20267  -0.20267  -0.20267
+-0.06161  -0.06161  -0.06161
+ 0.04413   0.04413   0.04413
+ 0.11303   0.11303   0.11303
+# u component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.92748   0.92748   0.92748
+ 0.97925   0.97925   0.97925
+ 0.99810   0.99810   0.99810
+ 0.99903   0.99903   0.99903
+ 0.99359   0.99359   0.99359
+# w component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+# v coordinates (along the horizontal, nr_hor, 0 rotor center)
+  -57.50
+    0.00
+   57.50
+# w coordinates (zero is at ground level, height, nr_hor)
+    0.00
+   39.38
+   78.75
+  118.12
+  157.50
diff --git a/wetb/prepost/tests/data/a_phi_None_shear_None_wdirNone.txt b/wetb/prepost/tests/data/a_phi_None_shear_None_wdirNone.txt
new file mode 100644
index 00000000..536a0528
--- /dev/null
+++ b/wetb/prepost/tests/data/a_phi_None_shear_None_wdirNone.txt
@@ -0,0 +1,30 @@
+# User defined shear file
+3 5 # nr_hor (v), nr_vert (w)
+# v component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+# u component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 1.00000   1.00000   1.00000
+ 1.00000   1.00000   1.00000
+ 1.00000   1.00000   1.00000
+ 1.00000   1.00000   1.00000
+ 1.00000   1.00000   1.00000
+# w component, normalized with U_mean, nr_hor (v) rows, nr_vert (w) columns
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+ 0.00000   0.00000   0.00000
+# v coordinates (along the horizontal, nr_hor, 0 rotor center)
+  -57.50
+    0.00
+   57.50
+# w coordinates (zero is at ground level, height, nr_hor)
+    0.00
+   39.38
+   78.75
+  118.12
+  157.50
diff --git a/wetb/prepost/tests/test_windIO.py b/wetb/prepost/tests/test_windIO.py
index 45cce4c6..84c5a9d5 100644
--- a/wetb/prepost/tests/test_windIO.py
+++ b/wetb/prepost/tests/test_windIO.py
@@ -12,10 +12,11 @@ standard_library.install_aliases()
 
 import unittest
 import os
+import tempfile
 
 import numpy as np
 
-from wetb.prepost.windIO import LoadResults
+from wetb.prepost import windIO
 
 
 class TestsLoadResults(unittest.TestCase):
@@ -27,7 +28,7 @@ class TestsLoadResults(unittest.TestCase):
         self.fbin = 'Hawc2bin'
 
     def loadresfile(self, resfile):
-        res = LoadResults(self.respath, resfile)
+        res = windIO.LoadResults(self.respath, resfile)
         self.assertTrue(hasattr(res, 'sig'))
         self.assertEqual(res.Freq, 40.0)
         self.assertEqual(res.N, 800)
@@ -45,15 +46,15 @@ class TestsLoadResults(unittest.TestCase):
         self.assertEqual(res.FileType, 'BINARY')
 
     def test_compare_ascii_bin(self):
-        res_ascii = LoadResults(self.respath, self.fascii)
-        res_bin = LoadResults(self.respath, self.fbin)
+        res_ascii = windIO.LoadResults(self.respath, self.fascii)
+        res_bin = windIO.LoadResults(self.respath, self.fbin)
 
         for k in range(res_ascii.sig.shape[1]):
             np.testing.assert_allclose(res_ascii.sig[:,k], res_bin.sig[:,k],
                                        rtol=1e-02, atol=0.001)
 
     def test_unified_chan_names(self):
-        res = LoadResults(self.respath, self.fascii, readdata=False)
+        res = windIO.LoadResults(self.respath, self.fascii, readdata=False)
         self.assertFalse(hasattr(res, 'sig'))
 
         np.testing.assert_array_equal(res.ch_df.index.values, np.arange(0,28))
@@ -62,5 +63,62 @@ class TestsLoadResults(unittest.TestCase):
                          'windspeed-global-Vy--2.50-1.00--52.50')
 
 
+class TestUserWind(unittest.TestCase):
+
+    def setUp(self):
+        self.path = os.path.join(os.path.dirname(__file__), 'data')
+        self.z_h = 100.0
+        self.r_blade_tip = 50.0
+        self.h_ME = 650
+        self.z = np.array([self.z_h - self.r_blade_tip,
+                           self.z_h + self.r_blade_tip])
+
+    def test_deltaphi2aphi(self):
+
+        uwind = windIO.UserWind()
+        profiles = windIO.WindProfiles
+
+        for a_phi_ref in [-1.0, 0.0, 0.5]:
+            phis = profiles.veer_ekman_mod(self.z, self.z_h, h_ME=self.h_ME,
+                                           a_phi=a_phi_ref)
+            d_phi_ref = phis[1] - phis[0]
+#            a_phi1 = uwind.deltaphi2aphi(d_phi_ref, self.z_h, self.r_blade_tip,
+#                                         h_ME=self.h_ME)
+            a_phi2 = uwind.deltaphi2aphi_opt(d_phi_ref, self.z, self.z_h,
+                                             self.r_blade_tip, self.h_ME)
+            self.assertAlmostEqual(a_phi_ref, a_phi2)
+
+    def test_usershear(self):
+
+        uwind = windIO.UserWind()
+
+        # phi, shear, wdir
+        combinations = [[1,0,0], [0,-0.2,0], [0,0,-10], [None, None, None],
+                        [0.5,0.2,10]]
+
+        for a_phi, shear, wdir in combinations:
+            rpl = (a_phi, shear, wdir)
+            try:
+                fname = 'a_phi_%1.05f_shear_%1.02f_wdir%02i.txt' % rpl
+            except:
+                fname = 'a_phi_%s_shear_%s_wdir%s.txt' % rpl
+            target = os.path.join(self.path, fname)
+
+            fid = tempfile.NamedTemporaryFile(delete=False, mode='wb')
+            target = os.path.join(self.path, fname)
+            uu, vv, ww, xx, zz = uwind(self.z_h, self.r_blade_tip, a_phi=a_phi,
+                                       nr_vert=5, nr_hor=3, h_ME=650.0,
+                                       wdir=wdir, io=fid, shear_exp=shear)
+            # FIXME: this has to be done more clean and Pythonic
+            # load again for comparison with the reference
+            uwind.fid.close()
+            with open(uwind.fid.name) as fid:
+                contents = fid.readlines()
+            os.remove(uwind.fid.name)
+            with open(target) as fid:
+                ref = fid.readlines()
+            self.assertEqual(contents, ref)
+
+
 if __name__ == "__main__":
     unittest.main()
-- 
GitLab