Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'''
Created on 05/11/2015
@author: MMPE
'''
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
import unittest
import os
import struct
import numpy as np
import scipy.io as sio
from wetb.prepost.windIO import Turbulence, LoadResults
# path for test data files
fpath = os.path.join(os.path.dirname(__file__), 'data/')
class TestsLoadResults(unittest.TestCase):
def setUp(self):
pass
def test_load(self):
respath = '../../hawc2/tests/test_files/hawc2io/'
resfile = 'Hawc2ascii'
res = LoadResults(respath, resfile)
class TestsTurbulence(unittest.TestCase):
def setUp(self):
pass
def print_test_info(self):
pass
def test_reshaped(self):
"""
Make sure we correctly reshape the array instead of the manual
index reassignments
"""
fpath = 'data/turb_s100_3.00w.bin'
fid = open(fpath, 'rb')
turb = np.fromfile(fid, 'float32', 32*32*8192)
turb.shape
fid.close()
u = np.zeros((8192,32,32))
for i in range(8192):
for j in range(32):
for k in range(32):
u[i,j,k] = turb[ i*1024 + j*32 + k]
u2 = np.reshape(turb, (8192, 32, 32))
self.assertTrue(np.alltrue(np.equal(u, u2)))
def test_headers(self):
fpath = 'data/'
basename = 'turb_s100_3.00_refoctave_header'
fid = open(fpath + basename + '.wnd', 'rb')
R1 = struct.unpack("h",fid.read(2))[0]
R2 = struct.unpack("h",fid.read(2))[0]
turb = struct.unpack("i",fid.read(4))[0]
lat = struct.unpack("f",fid.read(4))[0]
# last line
fid.seek(100)
LongVertComp = struct.unpack("f",fid.read(4))[0]
fid.close()
basename = 'turb_s100_3.00_python_header'
fid = open(fpath + basename + '.wnd', 'rb')
R1_p = struct.unpack("h",fid.read(2))[0]
R2_p = struct.unpack("h",fid.read(2))[0]
turb_p = struct.unpack("i",fid.read(4))[0]
lat_p = struct.unpack("f",fid.read(4))[0]
# last line
fid.seek(100)
LongVertComp_p = struct.unpack("f",fid.read(4))[0]
fid.close()
self.assertEqual(R1, R1_p)
self.assertEqual(R2, R2_p)
self.assertEqual(turb, turb_p)
self.assertEqual(lat, lat_p)
self.assertEqual(LongVertComp, LongVertComp_p)
def test_write_bladed(self):
fpath = 'data/'
turb = Turbulence()
# write with Python
basename = 'turb_s100_3.00'
turb.write_bladed(fpath, basename, shape=(8192,32,32))
python = turb.read_bladed(fpath, basename)
# load octave
basename = 'turb_s100_3.00_refoctave'
octave = turb.read_bladed(fpath, basename)
# float versions of octave
basename = 'turb_s100_3.00_refoctave_float'
fid = open(fpath + basename + '.wnd', 'rb')
octave32 = np.fromfile(fid, 'float32', 8192*32*32*3)
# find the differences
nr_diff = (python-octave).__ne__(0).sum()
print(nr_diff)
print(nr_diff/len(python))
self.assertTrue(np.alltrue(python == octave))
def test_turbdata(self):
shape = (8192,32,32)
fpath = 'data/'
basename = 'turb_s100_3.00_refoctave'
fid = open(fpath + basename + '.wnd', 'rb')
# check the last element of the header
fid.seek(100)
print(struct.unpack("f",fid.read(4))[0])
# save in a list using struct
items = (os.path.getsize(fpath + basename + '.wnd')-104)/2
data_list = [struct.unpack("h",fid.read(2))[0] for k in range(items)]
fid.seek(104)
data_16 = np.fromfile(fid, 'int16', shape[0]*shape[1]*shape[2]*3)
fid.seek(104)
data_8 = np.fromfile(fid, 'int8', shape[0]*shape[1]*shape[2]*3)
self.assertTrue(np.alltrue( data_16 == data_list ))
self.assertFalse(np.alltrue( data_8 == data_list ))
def test_compare_octave(self):
"""
Compare the results from the original script run via octave
"""
turb = Turbulence()
iu, iv, iw = turb.convert2bladed('data/', 'turb_s100_3.00',
shape=(8192,32,32))
res = sio.loadmat('data/workspace.mat')
# increase tolerances, values have a range up to 5000-10000
# and these values will be written to an int16 format for BLADED!
self.assertTrue(np.allclose(res['iu'], iu, rtol=1e-03, atol=1e-2))
self.assertTrue(np.allclose(res['iv'], iv, rtol=1e-03, atol=1e-2))
self.assertTrue(np.allclose(res['iw'], iw, rtol=1e-03, atol=1e-2))
def test_allindices(self):
"""
Verify that all indices are called
"""
fpath = 'data/turb_s100_3.00w.bin'
fid = open(fpath, 'rb')
turb = np.fromfile(fid, 'float32', 32*32*8192)
turb.shape
fid.close()
check = []
for i in range(8192):
for j in range(32):
for k in range(32):
check.append(i*1024 + j*32 + k)
qq = np.array(check)
qdiff = np.diff(qq)
self.assertTrue(np.alltrue(np.equal(qdiff, np.ones(qdiff.shape))))
if __name__ == "__main__":
unittest.main()