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

Commit e82647a

Browse files
committed
Tools: update for pyDatView scripter
1 parent 4f616ed commit e82647a

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

pyFAST/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
class PYFASTException(Exception):
3+
pass

pyFAST/input_output/wetb/hawc2/htc_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ def isfile_case_insensitive(f):
157157
relpath = "../" * np.argmax(found)
158158
return abspath(pjoin(os.path.dirname(self.filename), relpath))
159159
else:
160-
raise ValueError(
161-
"Modelpath cannot be autodetected for '%s'.\nInput files not found near htc file" % self.filename)
160+
print("Modelpath cannot be autodetected for '%s'.\nInput files not found near htc file" % self.filename)
161+
return 'unknown'
162162

163163
def load(self):
164164
self.contents = OrderedDict()

pyFAST/postpro/postpro.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import numpy as np
55
import re
66

7+
import pyFAST.input_output as weio
8+
from pyFAST.common import PYFASTException as WELIBException
9+
710
# --- fast libraries
811
from pyFAST.input_output.fast_input_file import FASTInputFile
912
from pyFAST.input_output.fast_output_file import FASTOutputFile
1013
from pyFAST.input_output.fast_input_deck import FASTInputDeck
14+
import pyFAST.fastfarm.fastfarm as fastfarm
1115

1216
# --------------------------------------------------------------------------------}
1317
# --- Tools for IO
@@ -903,6 +907,57 @@ def spanwisePostPro(FST_In=None,avgMethod='constantwindow',avgParam=5,out_ext='.
903907
# Combine all into a dictionary
904908
return out
905909

910+
def radialAvg(filename, avgMethod, avgParam, raw_name='', df=None, raiseException=True):
911+
"""
912+
Wrapper function, for instance used by pyDatView apply either:
913+
spanwisePostPro or spanwisePostProFF (FAST.Farm)
914+
"""
915+
916+
base,out_ext = os.path.splitext(filename)
917+
if df is None:
918+
df = FASTOutputFile(filename).toDataFrame()
919+
920+
# --- Detect if it's a FAST Farm file
921+
sCols = ''.join(df.columns)
922+
if sCols.find('WkDf')>1 or sCols.find('CtT')>0:
923+
# --- FAST FARM files
924+
Files=[base+ext for ext in ['.fstf','.FSTF','.Fstf','.fmas','.FMAS','.Fmas'] if os.path.exists(base+ext)]
925+
if len(Files)==0:
926+
fst_in=None
927+
#raise Exception('Error: No .fstf file found with name: '+base+'.fstf')
928+
else:
929+
fst_in=Files[0]
930+
931+
dfRad,_,dfDiam = fastfarm.spanwisePostProFF(fst_in,avgMethod=avgMethod,avgParam=avgParam,D=1,df=df)
932+
dfs_new = [dfRad,dfDiam]
933+
names_new=[raw_name+'_rad', raw_name+'_diam']
934+
else:
935+
# --- FAST files
936+
# HACK for AD file to find the right .fst file
937+
iDotAD=base.lower().find('.ad')
938+
if iDotAD>1:
939+
base=base[:iDotAD]
940+
#
941+
Files=[base+ext for ext in ['.fst','.FST','.Fst','.dvr','.Dvr','.DVR'] if os.path.exists(base+ext)]
942+
if len(Files)==0:
943+
fst_in=None
944+
#raise Exception('Error: No .fst file found with name: '+base+'.fst')
945+
else:
946+
fst_in=Files[0]
947+
948+
try:
949+
out = spanwisePostPro(fst_in, avgMethod=avgMethod, avgParam=avgParam, out_ext=out_ext, df = df)
950+
dfRadED=out['ED_bld']; dfRadAD = out['AD']; dfRadBD = out['BD']
951+
dfs_new = [dfRadAD, dfRadED, dfRadBD]
952+
names_new=[raw_name+'_AD', raw_name+'_ED', raw_name+'_BD']
953+
except:
954+
if raiseException:
955+
raise
956+
else:
957+
print('[WARN] radialAvg failed for filename {}'.format(filename))
958+
dfs_new =[None]
959+
names_new=['']
960+
return dfs_new, names_new
906961

907962
def spanwisePostProRows(df, FST_In=None):
908963
"""

pyFAST/tools/pandalib.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,82 @@ def remap_df(df, ColMap, bColKeepNewOnly=False, inPlace=False, dataDict=None, ve
128128
print('[WARN] Signals missing and omitted for ColKeep:\n '+'\n '.join(ColKeepMiss))
129129
df=df[ColKeepSafe]
130130
return df
131+
132+
def changeUnits(df, flavor='SI', inPlace=True):
133+
""" Change units of a dataframe
134+
135+
# TODO harmonize with dfToSIunits in welib.fast.tools.lin.py !
136+
"""
137+
def splitunit(s):
138+
iu=s.rfind('[')
139+
if iu>0:
140+
return s[:iu], s[iu+1:].replace(']','')
141+
else:
142+
return s, ''
143+
def change_units_to_WE(s, c):
144+
"""
145+
Change units to wind energy units
146+
s: channel name (string) containing units, typically 'speed_[rad/s]'
147+
c: channel (array)
148+
"""
149+
svar, u = splitunit(s)
150+
u=u.lower()
151+
scalings = {}
152+
# OLD = NEW
153+
scalings['rad/s'] = (30/np.pi,'rpm') # TODO decide
154+
scalings['rad' ] = (180/np.pi,'deg')
155+
scalings['n'] = (1e-3, 'kN')
156+
scalings['nm'] = (1e-3, 'kNm')
157+
scalings['n-m'] = (1e-3, 'kNm')
158+
scalings['n*m'] = (1e-3, 'kNm')
159+
scalings['w'] = (1e-3, 'kW')
160+
if u in scalings.keys():
161+
scale, new_unit = scalings[u]
162+
s = svar+'['+new_unit+']'
163+
c *= scale
164+
return s, c
165+
166+
def change_units_to_SI(s, c):
167+
"""
168+
Change units to SI units
169+
TODO, a lot more units conversion needed...will add them as we go
170+
s: channel name (string) containing units, typically 'speed_[rad/s]'
171+
c: channel (array)
172+
"""
173+
svar, u = splitunit(s)
174+
u=u.lower()
175+
scalings = {}
176+
# OLD = NEW
177+
scalings['rpm'] = (np.pi/30,'rad/s')
178+
scalings['rad' ] = (180/np.pi,'deg')
179+
scalings['deg/s' ] = (np.pi/180,'rad/s')
180+
scalings['kn'] = (1e3, 'N')
181+
scalings['knm'] = (1e3, 'Nm')
182+
scalings['kn-m'] = (1e3, 'Nm')
183+
scalings['kn*m'] = (1e3, 'Nm')
184+
scalings['kw'] = (1e3, 'W')
185+
if u in scalings.keys():
186+
scale, new_unit = scalings[u]
187+
s = svar+'['+new_unit+']'
188+
c *= scale
189+
return s, c
190+
191+
if not inPlace:
192+
raise NotImplementedError()
193+
194+
if flavor == 'WE':
195+
cols = []
196+
for i, colname in enumerate(df.columns):
197+
colname_new, df.iloc[:,i] = change_units_to_WE(colname, df.iloc[:,i])
198+
cols.append(colname_new)
199+
df.columns = cols
200+
elif flavor == 'SI':
201+
cols = []
202+
for i, colname in enumerate(df.columns):
203+
colname_new, df.iloc[:,i] = change_units_to_SI(colname, df.iloc[:,i])
204+
cols.append(colname_new)
205+
df.columns = cols
206+
else:
207+
raise NotImplementedError(flavor)
208+
return df
209+

0 commit comments

Comments
 (0)