Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 6e215bd

Browse files
committed
IO: adding example for MannBox file
1 parent a9315c0 commit 6e215bd

File tree

5 files changed

+122
-9
lines changed

5 files changed

+122
-9
lines changed

data/example_files/MannBox_32x4x8.bin

4 KB
Binary file not shown.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
Example usage for the MannBox class.
3+
"""
4+
5+
import os
6+
import numpy as np
7+
import matplotlib.pyplot as plt
8+
from pyFAST.input_output.mannbox_file import MannBoxFile
9+
10+
11+
scriptDir = os.path.dirname(__file__)
12+
13+
# --- Inputs
14+
file_u = os.path.join(scriptDir, '../../../data/example_files/MannBox_32x4x8.bin')
15+
dy = 3 # horizontal spacing [m]
16+
dz = 10 # vertical spacing [m]
17+
18+
# --- Read Mann Box file and display main properties
19+
mb = MannBoxFile(file_u, dy=dy, dz=dz) # create a MannBoxFile object, store it in mb
20+
print(mb) # print misc info about the object
21+
print(mb['field'].shape) # print the shape of your field.
22+
nx, ny, nz = mb['field'].shape # Store the field dimension
23+
24+
25+
# --- Plot values of the field at given indices. Note this example has only 2 values in x
26+
# NOTE: can also be obtained directly using:
27+
# u = mb.valuesAt(y=5, z=50)
28+
t = mb.t(dx=1, U=10)
29+
u = mb['field'][:, 3, 5] # Value at indices iy=3, iz=5
30+
plt.figure(1)
31+
plt.plot(t, u, label='Input')
32+
plt.xlabel('time [s]')
33+
plt.ylabel('u [m/s]')
34+
plt.title('Value of field at chosen location as function of x/t')
35+
36+
37+
# --- Plot vertical profile averaged over all x and y
38+
# NOTE: can also be obtained directly using:
39+
# z, means, stds = mb.vertProfile
40+
u_mean_vertical = np.mean(np.mean(mb['field'][:,:,:], axis=0), axis=0)
41+
plt.figure(2)
42+
plt.plot(u_mean_vertical, mb.z, label='Input')
43+
plt.xlabel('u mean [m/s]')
44+
plt.ylabel('z [m]')
45+
plt.title('Average vertical profile for all x and y')
46+
47+
# --- Compute the mean over all "x" values for each points in the y-z plane
48+
UmeanYZ = np.mean(mb['field'][:,:,:],axis=0) # This has shape ny x nz
49+
50+
# --- Modify the field (some examples)
51+
mb['field'] -= UmeanYZ # remove the mean of all datapoints along x
52+
mb['field'][:,:,:] *= 2 # multiply the field by 10 everywhere
53+
mb['field'][:,:,:] += UmeanYZ # add the mean again
54+
mb['field'][:,:,:] += 0.5 # add 0.5 everywhere in the field
55+
mb['field'][:,:,0] = 0 # set the velocity to be zero for the first z index
56+
57+
58+
# --- Plot value of the field again
59+
u = mb['field'][:, 3, 5] # Value at indices iy=3, iz=5
60+
plt.figure(1)
61+
plt.plot(t, u, label='Modified')
62+
plt.xlabel('time [s]')
63+
plt.ylabel('u [m/s]')
64+
65+
# --- Plot the vertical profile again
66+
u_mean_vertical2 = np.mean(np.mean(mb['field'][:,:,:], axis=0), axis=0)
67+
plt.figure(2)
68+
plt.plot(u_mean_vertical2, mb.z, '--', label='Modified')
69+
plt.xlabel('u mean [m/s]')
70+
plt.ylabel('z [m]')
71+
72+
73+
74+
75+
# --- Write the modified field to disk with another filename
76+
outFile = file_u.replace('.bin','_modified.bin')
77+
mb.write(outFile)
78+
print('File written: ', outFile)
79+
80+
81+
82+
if __name__ == "__main__":
83+
plt.show()
84+
85+
if __name__=='__test__':
86+
try:
87+
os.remove(outFile)
88+
except:
89+
pass
90+

pyFAST/input_output/mannbox_file.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class MannBoxFile(File):
4747
print(mb['field'].shape)
4848
4949
# Use methods to extract relevant values
50-
u,v,w = my.valuesAt(y=10.5, z=90)
51-
z, means, stds = mb.vertProfile()
50+
u = mb.valuesAt(y=10.5, z=90)
51+
z, means, stds = mb.vertProfile
5252
5353
# Write to a new file
5454
mb.write('Output_1024x16x16.u')
@@ -119,6 +119,7 @@ def read(self, filename=None, N=None, dy=1, dz=1, y0=None, z0=0, zMid=None):
119119
else:
120120
raise BrokenFormatError('Reading a Mann box requires the knowledge of the dimensions. The dimensions can be inferred from the filename, for instance: `filebase_1024x32x32.u`. Try renaming your file such that the three last digits are the dimensions in x, y and z.')
121121
nx,ny,nz=N
122+
122123
def _read_buffered():
123124
data=np.zeros((nx,ny,nz),dtype=np.float32)
124125
with open(self.filename, mode='rb') as f:
@@ -143,10 +144,6 @@ def _read_nonbuffered():
143144
self['y0']=y0
144145
self['z0']=z0
145146
self['zMid']=zMid
146-
# print('1',self['field'][:,-1,0])
147-
# print('2',self['field'][0,-1::-1,0])
148-
# print('3',self['field'][0,-1,:])
149-
150147

151148
def write(self, filename=None):
152149
""" Write mann box """
@@ -169,11 +166,13 @@ def __repr__(self):
169166
s+='| min: {}, max: {}, mean: {} \n'.format(np.min(self['field']), np.max(self['field']), np.mean(self['field']))
170167
s+='| - dy, dz: {}, {}\n'.format(self['dy'], self['dz'])
171168
s+='| - y0, z0 zMid: {}, {}, {}\n'.format(self['y0'], self['z0'], self['zMid'])
172-
s+='|useful getters: y, z, _iMid, fromTurbSim\n'
173169
z=self.z
174170
y=self.y
175-
s+='| y: [{} ... {}], dy: {}, n: {} \n'.format(y[0],y[-1],self['dy'],len(y))
176-
s+='| z: [{} ... {}], dz: {}, n: {} \n'.format(z[0],z[-1],self['dz'],len(z))
171+
s+='| * y: [{} ... {}], dy: {}, n: {} \n'.format(y[0],y[-1],self['dy'],len(y))
172+
s+='| * z: [{} ... {}], dz: {}, n: {} \n'.format(z[0],z[-1],self['dz'],len(z))
173+
s+='|useful functions:\n'
174+
s+='| - t(dx, U)\n'
175+
s+='| - valuesAt(y,z), vertProfile, fromTurbSim(*), _iMid()\n'
177176
return s
178177

179178

pyFAST/input_output/tests/example_files/MannBox_2x4x8.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.�������ܐ�h܏�1�"i^���\������ֿ�MG��Q~�'�?*�@w�&@6 �>y඿��?���?-�@�ֲ@E=A��A�T�@%@#@`�:��� �d���v#�{QO@F�:@��>�=�����w��{��i���(���j��\������ h.�kP���s�?x�@-E@��|?YE���0?'6�?��@�-�@��Ak4�@�*�@��"@<�-�GG����^�-���`@�k@�/7>��
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
import os
3+
import numpy as np
4+
from pyFAST.input_output.mannbox_file import MannBoxFile
5+
from pyFAST.input_output.tests.helpers_for_test import MyDir, reading_test
6+
7+
class Test(unittest.TestCase):
8+
9+
def test_001_read_all(self, DEBUG=True):
10+
reading_test('MannBox_*.*', MannBoxFile)
11+
12+
def test_MannBox(self):
13+
# --- Test read/write
14+
F = MannBoxFile(os.path.join(MyDir,'MannBox_2x4x8.bin'))
15+
F.write( os.path.join(MyDir,'MannBox_2x4x8_TMP.bin'))
16+
F2= MannBoxFile(os.path.join(MyDir,'MannBox_2x4x8_TMP.bin'))
17+
os.remove( os.path.join(MyDir,'MannBox_2x4x8_TMP.bin'))
18+
np.testing.assert_almost_equal(F['field'].shape ,[2,4,8])
19+
np.testing.assert_almost_equal(F['field'][:,:,:],F2['field'][:,:,:],8)
20+
np.testing.assert_almost_equal(F['field'][1,3,5], -3.6654968, 6)
21+
22+
if __name__ == '__main__':
23+
unittest.main()

0 commit comments

Comments
 (0)