From 2a84dde5e3d2cd3daa58e80482b543caab0beef5 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Sat, 21 Dec 2024 16:10:21 +0100 Subject: [PATCH 1/4] :pencil: Replaced np.sqrt with square comparison to speed up comparison --- openfast_python/openfast_io/turbsim_file.py | 34 ++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/openfast_python/openfast_io/turbsim_file.py b/openfast_python/openfast_io/turbsim_file.py index 2756fff7b..2e8acf4ad 100644 --- a/openfast_python/openfast_io/turbsim_file.py +++ b/openfast_python/openfast_io/turbsim_file.py @@ -16,13 +16,13 @@ File=dict class TurbSimFile(File): - """ + """ Read/write a TurbSim turbulence file (.bts). The object behaves as a dictionary. Main keys --------- - 'u': velocity field, shape (3 x nt x ny x nz) - - 'y', 'z', 't': space and time coordinates + - 'y', 'z', 't': space and time coordinates - 'dt', 'ID', 'info' - 'zTwr', 'uTwr': tower coordinates and field if present (3 x nt x nTwr) - 'zHub', 'uHub': height and velocity at a reference point (usually not hub) @@ -36,7 +36,7 @@ class TurbSimFile(File): ts = TurbSimFile('Turb.bts') print(ts.keys()) - print(ts['u'].shape) + print(ts['u'].shape) """ @@ -55,7 +55,7 @@ def __init__(self,filename=None, **kwargs): self.read(filename, **kwargs) def read(self, filename=None, header_only=False): - """ read BTS file, with field: + """ read BTS file, with field: u (3 x nt x ny x nz) uTwr (3 x nt x nTwr) """ @@ -69,7 +69,7 @@ def read(self, filename=None, header_only=False): raise EmptyFileError('File is empty:',self.filename) scl = np.zeros(3, np.float32); off = np.zeros(3, np.float32) - with open(self.filename, mode='rb') as f: + with open(self.filename, mode='rb') as f: # Reading header info ID, nz, ny, nTwr, nt = struct.unpack('0) @@ -95,7 +95,7 @@ def read(self, filename=None, header_only=False): self['info'] = info self['ID'] = ID self['dt'] = dt - self['y'] = np.arange(ny)*dy + self['y'] = np.arange(ny)*dy self['y'] -= np.mean(self['y']) # y always centered on 0 self['z'] = np.arange(nz)*dz +zBottom self['t'] = np.arange(nt)*dt @@ -104,7 +104,7 @@ def read(self, filename=None, header_only=False): self['uHub'] = uHub def write(self, filename=None): - """ + """ write a BTS file, using the following keys: 'u','z','y','t','uTwr' u (3 x nt x ny x nz) uTwr (3 x nt x nTwr) @@ -151,7 +151,7 @@ def write(self, filename=None): # Providing estimates of uHub and zHub even if these fields are not used zHub,uHub, bHub = self.hubValues() - with open(self.filename, mode='wb') as f: + with open(self.filename, mode='wb') as f: f.write(struct.pack(' Date: Sat, 21 Dec 2024 16:16:05 +0100 Subject: [PATCH 2/4] :wrench: Used list comprehension to speed u_rot creation --- openfast_python/openfast_io/turbsim_file.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/openfast_python/openfast_io/turbsim_file.py b/openfast_python/openfast_io/turbsim_file.py index 2e8acf4ad..26c60daea 100644 --- a/openfast_python/openfast_io/turbsim_file.py +++ b/openfast_python/openfast_io/turbsim_file.py @@ -308,9 +308,7 @@ def compute_rot_avg(self, R): yy, zz = np.meshgrid(self['y'], self['z']) rotor_ind = (yy**2 + (zz - z_hub)**2) < R**2 - u_rot = [] - for u_plane in u_: - u_rot.append(u_plane[rotor_ind].mean()) + u_rot = [u_plane[rotor_ind].mean() for u_plane in u_] self['rot_avg'][i,:] = u_rot From 1b97110a2354d935eaf53a6a5c0d9abf3aaaf4c2 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Sat, 21 Dec 2024 16:18:20 +0100 Subject: [PATCH 3/4] :wrench: Removed unused variables and wrongly declared --- openfast_python/openfast_io/turbsim_file.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/openfast_python/openfast_io/turbsim_file.py b/openfast_python/openfast_io/turbsim_file.py index 26c60daea..e852ea838 100644 --- a/openfast_python/openfast_io/turbsim_file.py +++ b/openfast_python/openfast_io/turbsim_file.py @@ -264,8 +264,6 @@ def __repr__(self): def toDataFrame(self): dfs={} - ny = len(self['y']) - nz = len(self['y']) # Index at mid box iy,iz = self._iMid() From ab386f4003c77c6c05dc7cec3336ee1803710a8c Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Sat, 21 Dec 2024 17:04:12 +0100 Subject: [PATCH 4/4] :sparkle: Added a dedicated entrypoint --- openfast_python/openfast_io/turbsim_file.py | 25 ++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/openfast_python/openfast_io/turbsim_file.py b/openfast_python/openfast_io/turbsim_file.py index e852ea838..9cd6ed1e8 100644 --- a/openfast_python/openfast_io/turbsim_file.py +++ b/openfast_python/openfast_io/turbsim_file.py @@ -5,6 +5,7 @@ """ import pandas as pd import numpy as np +import argparse import os import struct import time @@ -311,5 +312,27 @@ def compute_rot_avg(self, R): self['rot_avg'][i,:] = u_rot +def main(): + # Create argument parser + parser = argparse.ArgumentParser( + description="Read an OpenFAST .bts wind field files") + parser.add_argument( + "input_file", + type=str, + help="Path to the input .bts file." + ) + + # Parse arguments + args = parser.parse_args() + + # Process input and output files + input_file = args.input_file + # ts = TurbSimFile('../_tests/TurbSim.bts') + ts = TurbSimFile(input_file) + df = ts.toDataFrame() + print(df['VertProfile'].info()) + print(df['MidLine'].info()) + + if __name__=='__main__': - ts = TurbSimFile('../_tests/TurbSim.bts') + main()