-
Notifications
You must be signed in to change notification settings - Fork 7
/
windIO.py
executable file
·2669 lines (2236 loc) · 102 KB
/
windIO.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 3 19:53:59 2014
@author: dave
"""
__author__ = 'David Verelst'
__license__ = 'GPL'
__version__ = '0.5'
import os
import copy
import struct
import math
from time import time
import codecs
from itertools import chain
import re as re
import numpy as np
import scipy as sp
import scipy.io as sio
import scipy.integrate as integrate
import pandas as pd
# misc is part of prepost, which is available on the dtu wind gitlab server:
# https://gitlab.windenergy.dtu.dk/dave/prepost
from wetb.prepost import misc
# wind energy python toolbox, available on the dtu wind redmine server:
# http://vind-redmine.win.dtu.dk/projects/pythontoolbox/repository/show/fatigue_tools
from wetb.hawc2.Hawc2io import ReadHawc2
from wetb.fatigue_tools.fatigue import (eq_load, cycle_matrix2)
class LogFile(object):
"""Check a HAWC2 log file for errors.
"""
def __init__(self):
# the total message list log:
self.MsgListLog = []
# a smaller version, just indication if there are errors:
self.MsgListLog2 = dict()
# specify which message to look for. The number track's the order.
# this makes it easier to view afterwards in spreadsheet:
# every error will have its own column
# error messages that appear during initialisation
self.err_init = {}
self.err_init[' *** ERROR *** Error in com'] = len(self.err_init)
self.err_init[' *** ERROR *** in command '] = len(self.err_init)
# *** WARNING *** A comma "," is written within the command line
self.err_init[' *** WARNING *** A comma ",'] = len(self.err_init)
# *** ERROR *** Not correct number of parameters
self.err_init[' *** ERROR *** Not correct '] = len(self.err_init)
# *** INFO *** End of file reached
self.err_init[' *** INFO *** End of file r'] = len(self.err_init)
# *** ERROR *** No line termination in command line
self.err_init[' *** ERROR *** No line term'] = len(self.err_init)
# *** ERROR *** MATRIX IS NOT DEFINITE
self.err_init[' *** ERROR *** MATRIX IS NO'] = len(self.err_init)
# *** ERROR *** There are unused relative
self.err_init[' *** ERROR *** There are un'] = len(self.err_init)
# *** ERROR *** Error finding body based
self.err_init[' *** ERROR *** Error findin'] = len(self.err_init)
# *** ERROR *** In body actions
self.err_init[' *** ERROR *** In body acti'] = len(self.err_init)
# *** ERROR *** Command unknown and ignored
self.err_init[' *** ERROR *** Command unkn'] = len(self.err_init)
# *** ERROR *** ERROR - More bodies than elements on main_body: tower
self.err_init[' *** ERROR *** ERROR - More'] = len(self.err_init)
# *** ERROR *** The program will stop
self.err_init[' *** ERROR *** The program '] = len(self.err_init)
# *** ERROR *** Unknown begin command in topologi.
self.err_init[' *** ERROR *** Unknown begi'] = len(self.err_init)
# *** ERROR *** Not all needed topologi main body commands present
self.err_init[' *** ERROR *** Not all need'] = len(self.err_init)
# *** ERROR *** opening timoschenko data file
self.err_init[' *** ERROR *** opening tim'] = len(self.err_init)
# *** ERROR *** Error opening AE data file
self.err_init[' *** ERROR *** Error openin'] = len(self.err_init)
# *** ERROR *** Requested blade _ae set number not found in _ae file
self.err_init[' *** ERROR *** Requested bl'] = len(self.err_init)
# Error opening PC data file
self.err_init[' Error opening PC data file'] = len(self.err_init)
# *** ERROR *** error reading mann turbulence
self.err_init[' *** ERROR *** error readin'] = len(self.err_init)
# # *** INFO *** The DLL subroutine
# self.err_init[' *** INFO *** The DLL subro'] = len(self.err_init)
# ** WARNING: FROM ESYS ELASTICBAR: No keyword
self.err_init[' ** WARNING: FROM ESYS ELAS'] = len(self.err_init)
# *** ERROR *** DLL ./control/killtrans.dll could not be loaded - error!
self.err_init[' *** ERROR *** DLL'] = len(self.err_init)
# *** ERROR *** The DLL subroutine
self.err_init[' *** ERROR *** The DLL subr'] = len(self.err_init)
# *** ERROR *** Mann turbulence length scale must be larger than zero!
# *** ERROR *** Mann turbulence alpha eps value must be larger than zero!
# *** ERROR *** Mann turbulence gamma value must be larger than zero!
self.err_init[' *** ERROR *** Mann turbule'] = len(self.err_init)
# *** WARNING *** Shear center x location not in elastic center, set to zero
self.err_init[' *** WARNING *** Shear cent'] = len(self.err_init)
# Turbulence file ./xyz.bin does not exist
self.err_init[' Turbulence file '] = len(self.err_init)
self.err_init[' *** WARNING ***'] = len(self.err_init)
self.err_init[' *** ERROR ***'] = len(self.err_init)
self.err_init[' WARNING'] = len(self.err_init)
self.err_init[' ERROR'] = len(self.err_init)
# error messages that appear during simulation
self.err_sim = {}
# *** ERROR *** Wind speed requested inside
self.err_sim[' *** ERROR *** Wind speed r'] = len(self.err_sim)
# Maximum iterations exceeded at time step:
self.err_sim[' Maximum iterations exceede'] = len(self.err_sim)
# Solver seems not to converge:
self.err_sim[' Solver seems not to conver'] = len(self.err_sim)
# *** ERROR *** Out of x bounds:
self.err_sim[' *** ERROR *** Out of x bou'] = len(self.err_sim)
# *** ERROR *** Out of limits in user defined shear field - limit value used
self.err_sim[' *** ERROR *** Out of limit'] = len(self.err_sim)
# NEAR WAKE ERRORS
# ERROR in Near Wake! The radius of the tip is smaller (or equal) to
self.err_sim[' ERROR in Near Wake! The ra'] = len(self.err_sim)
# ERROR: Maximum number of near wake iterations reached
self.err_sim[' ERROR: Maximum number of n'] = len(self.err_sim)
# TODO: error message from a non existing channel output/input
# add more messages if required...
self.init_cols = len(self.err_init)
self.sim_cols = len(self.err_sim)
self.header = None
def readlog(self, fname, case=None, save_iter=False):
"""
"""
# be cautious and try a few encodings when reading the file
lines = misc.readlines_try_encodings(fname)
# keep track of the messages allready found in this file
tempLog = []
tempLog.append(fname)
exit_correct, found_error = False, False
subcols_sim = 4
subcols_init = 2
# create empty list item for the different messages and line
# number. Include one column for non identified messages
for j in range(self.init_cols):
# 2 sub-columns per message: nr, msg
for k in range(subcols_init):
tempLog.append('')
for j in range(self.sim_cols):
# 4 sub-columns per message: first, last, nr, msg
for k in range(subcols_sim):
tempLog.append('')
# and two more columns at the end for messages of unknown origin
tempLog.append('')
tempLog.append('')
# if there is a cases object, see how many time steps we expect
if case is not None:
dt = float(case['[dt_sim]'])
time_steps = int(float(case['[time_stop]']) / dt)
iterations = np.ndarray( (time_steps+1,3), dtype=np.float32 )
else:
iterations = np.ndarray( (len(lines),3), dtype=np.float32 )
dt = False
iterations[:,0:2] = np.nan
iterations[:,2] = 0
# keep track of the time_step number
time_step, init_block = 0, True
# check for messages in the current line
# for speed: delete from message watch list if message is found
for j, line in enumerate(lines):
# all id's of errors are 27 characters long
msg = line[:27]
# remove the line terminator, this seems to take 2 characters
# on PY2, but only one in PY3
line = line.replace('\n', '')
# keep track of the number of iterations
if line[:12] == ' Global time':
iterations[time_step,0] = float(line[14:40])
# for PY2, new line is 2 characters, for PY3 it is one char
iterations[time_step,1] = int(line[-6:])
# time step is the first time stamp
if not dt:
dt = float(line[15:40])
time_step += 1
# no need to look for messages if global time is mentioned
continue
elif line[:4] == ' kfw':
pass
# Global time = 17.7800000000000 Iter = 2
# kfw 0.861664060457402
# nearwake iterations 17
# computed relaxation factor 0.300000000000000
elif line[:20] == ' Starting simulation':
init_block = False
elif init_block:
# if string is shorter, we just get a shorter string.
# checking presence in dict is faster compared to checking
# the length of the string
# first, last, nr, msg
if msg in self.err_init:
# icol=0 -> fname
icol = subcols_init*self.err_init[msg] + 1
# 0: number of occurances
if tempLog[icol] == '':
tempLog[icol] = '1'
else:
tempLog[icol] = str(int(tempLog[icol]) + 1)
# 1: the error message itself
tempLog[icol+1] = line
found_error = True
# find errors that can occur during simulation
elif msg in self.err_sim:
icol = subcols_sim*self.err_sim[msg]
icol += subcols_init*self.init_cols + 1
# 1: time step of first occurance
if tempLog[icol] == '':
tempLog[icol] = '%i' % time_step
# 2: time step of last occurance
tempLog[icol+1] = '%i' % time_step
# 3: number of occurances
if tempLog[icol+2] == '':
tempLog[icol+2] = '1'
else:
tempLog[icol+2] = str(int(tempLog[icol+2]) + 1)
# 4: the error message itself
tempLog[icol+3] = line
found_error = True
iterations[time_step-1,2] = 1
# method of last resort, we have no idea what message
elif line[:10] == ' *** ERROR' or line[:10]==' ** WARNING' \
or line[:6] == ' ERROR':
icol = subcols_sim*self.sim_cols
icol += subcols_init*self.init_cols + 1
# line number of the message
tempLog[icol] = j
# and message
tempLog[icol+1] = line
found_error = True
iterations[time_step-1,2] = 1
# remove not-used rows from iterations
iterations = iterations[:time_step,:]
# simulation and simulation output time based on the tags
# FIXME: ugly, do not mix tags with what is actually happening in the
# log files!!
if case is not None:
t_stop = float(case['[time_stop]'])
duration = float(case['[duration]'])
else:
t_stop = np.nan
duration = -1
# if no time steps have passed
if iterations.shape == (0,3):
elapsed_time = -1
tempLog.append('')
# see if the last line holds the sim time
elif line[:15] == ' Elapsed time :':
exit_correct = True
elapsed_time = float(line[15:-1])
tempLog.append( elapsed_time )
# in some cases, Elapsed time is not given, and the last message
# might be: " Closing of external type2 DLL"
elif line[:20] == ' Closing of external':
exit_correct = True
elapsed_time = iterations[time_step-1,0]
tempLog.append( elapsed_time )
# FIXME: this is weird mixing of referring to t_stop from the tags
# and the actual last recorded time step
elif np.allclose(iterations[time_step-1,0], t_stop):
exit_correct = True
elapsed_time = iterations[time_step-1,0]
tempLog.append( elapsed_time )
else:
elapsed_time = -1
tempLog.append('')
if iterations.shape == (0,3):
last_time_step = 0
else:
last_time_step = iterations[time_step-1,0]
# give the last recorded time step
tempLog.append('%1.11f' % last_time_step)
# simulation_time, as taken from cases
tempLog.append('%1.01f' % t_stop)
# real_sim_time
tempLog.append('%1.04f' % (last_time_step/elapsed_time))
tempLog.append('%1.01f' % duration)
# as last element, add the total number of iterations
itertotal = np.nansum(iterations[:,1])
tempLog.append('%1.0f' % itertotal)
# the delta t used for the simulation
if dt:
tempLog.append('%1.7f' % dt)
else:
tempLog.append('nan')
# number of time steps
tempLog.append('%i' % (time_step))
# if the simulation didn't end correctly, the elapsed_time doesn't
# exist. Add the average and maximum nr of iterations per step
# or, if only the structural and eigen analysis is done, we have 0
try:
ratio = float(elapsed_time)/float(itertotal)
# FIXME: this needs to be fixed proper while testing the analysis
# of various log files and edge cases
if elapsed_time < 0:
tempLog.append('')
else:
tempLog.append('%1.6f' % ratio)
except (UnboundLocalError, ZeroDivisionError, ValueError) as e:
tempLog.append('')
# when there are no time steps (structural analysis only)
try:
tempLog.append('%1.2f' % iterations[:,1].mean())
tempLog.append('%1.2f' % iterations[:,1].max())
except ValueError:
tempLog.append('')
tempLog.append('')
# FIXME: we the sim crashes at generating the turbulence box
# there is one element too much at the end
tempLog = tempLog[:len(self._header().split(';'))]
# save the iterations in the results folder
if save_iter:
fiter = os.path.basename(fname).replace('.log', '.iter')
fmt = ['%12.06f', '%4i', '%4i']
if case is not None:
fpath = os.path.join(case['[run_dir]'], case['[iter_dir]'])
# in case it has subdirectories
for tt in [3,2,1]:
tmp = os.path.sep.join(fpath.split(os.path.sep)[:-tt])
if not os.path.exists(tmp):
os.makedirs(tmp)
if not os.path.exists(fpath):
os.makedirs(fpath)
np.savetxt(fpath + fiter, iterations, fmt=fmt)
else:
logpath = os.path.dirname(fname)
np.savetxt(os.path.join(logpath, fiter), iterations, fmt=fmt)
# append the messages found in the current file to the overview log
self.MsgListLog.append(tempLog)
self.MsgListLog2[fname] = [found_error, exit_correct]
def _msglistlog2csv(self, contents):
"""Write LogFile.MsgListLog to a csv file. Use LogFile._header to
create a header.
"""
for k in self.MsgListLog:
for n in k:
contents = contents + str(n) + ';'
# at the end of each line, new line symbol
contents = contents + '\n'
return contents
def csv2df(self, fname, header=0):
"""Read a csv log file analysis and convert to a pandas.DataFrame
"""
colnames, min_itemsize, dtypes = self.headers4df()
df = pd.read_csv(fname, header=header, names=colnames, sep=';')
for col, dtype in dtypes.items():
df[col] = df[col].astype(dtype)
# replace nan with empty for str columns
if dtype == str:
df[col] = df[col].str.replace('nan', '')
return df
def _header(self):
"""Header for log analysis csv file
"""
# write the results in a file, start with a header
contents = 'file name;' + 'nr;msg;'*(self.init_cols)
contents += 'first_tstep;last_tstep;nr;msg;'*(self.sim_cols)
contents += 'lnr;msg;'
# and add headers for elapsed time, nr of iterations, and sec/iteration
contents += 'Elapsted time;last time step;Simulation time;'
contents += 'real sim time;Sim output time;'
contents += 'total iterations;dt;nr time steps;'
contents += 'seconds/iteration;average iterations/time step;'
contents += 'maximum iterations/time step;\n'
return contents
def headers4df(self):
"""Create header and a minimum itemsize for string columns when
converting a Log check analysis to a pandas.DataFrame
Returns
-------
header : list
List of column names as generated by WindIO.LogFile._header
min_itemsize : dict
Dictionary with column names as keys, and the minimum string lenght
as values.
dtypes : dict
Dictionary with column names as keys, and data types as values
"""
chain_iter = chain.from_iterable
nr_init = len(self.err_init)
nr_sim = len(self.err_sim)
colnames = ['file_name']
colnames.extend(list(chain_iter(('nr_%i' % i, 'msg_%i' % i)
for i in range(nr_init))) )
gr = ('first_tstep_%i', 'last_step_%i', 'nr_%i', 'msg_%i')
colnames.extend(list(chain_iter( (k % i for k in gr)
for i in range(100,100+nr_sim,1))) )
colnames.extend(['nr_extra', 'msg_extra'])
colnames.extend(['elapsted_time',
'last_time_step',
'simulation_time',
'real_sim_time',
'sim_output_time',
'total_iterations',
'dt',
'nr_time_steps',
'seconds_p_iteration',
'mean_iters_p_time_step',
'max_iters_p_time_step',
'sim_id'])
dtypes = {}
# str and float datatypes for
msg_cols = ['msg_%i' % i for i in range(nr_init-1)]
msg_cols.extend(['msg_%i' % i for i in range(100,100+nr_sim,1)])
msg_cols.append('msg_extra')
dtypes.update({k:str for k in msg_cols})
# make the message/str columns long enough
min_itemsize = {'msg_%i' % i : 100 for i in range(nr_init-1)}
# column names holding the number of occurances of messages
nr_cols = ['nr_%i' % i for i in range(nr_init-1)]
nr_cols.extend(['nr_%i' % i for i in range(100,100+nr_sim,1)])
# other float values
nr_cols.extend(['elapsted_time', 'total_iterations'])
# NaN only exists in float arrays, not integers (NumPy limitation)
# so use float instead of int
dtypes.update({k:np.float64 for k in nr_cols})
return colnames, min_itemsize, dtypes
class LoadResults(ReadHawc2):
"""Read a HAWC2 result data file
Usage:
obj = LoadResults(file_path, file_name)
This class is called like a function:
HawcResultData() will read the specified file upon object initialization.
Available output:
obj.sig[timeStep,channel] : complete result file in a numpy array
obj.ch_details[channel,(0=ID; 1=units; 2=description)] : np.array
obj.error_msg: is 'none' if everything went OK, otherwise it holds the
error
The ch_dict key/values pairs are structured differently for different
type of channels. Currently supported channels are:
For forcevec, momentvec, state commands:
key:
coord-bodyname-pos-sensortype-component
global-tower-node-002-forcevec-z
local-blade1-node-005-momentvec-z
hub1-blade1-elem-011-zrel-1.00-state pos-z
value:
ch_dict[tag]['coord']
ch_dict[tag]['bodyname']
ch_dict[tag]['pos'] = pos
ch_dict[tag]['sensortype']
ch_dict[tag]['component']
ch_dict[tag]['chi']
ch_dict[tag]['sensortag']
ch_dict[tag]['units']
For the DLL's this is:
key:
DLL-dll_name-io-io_nr
DLL-yaw_control-outvec-3
DLL-yaw_control-inpvec-1
value:
ch_dict[tag]['dll_name']
ch_dict[tag]['io']
ch_dict[tag]['io_nr']
ch_dict[tag]['chi']
ch_dict[tag]['sensortag']
ch_dict[tag]['units']
For the bearings this is:
key:
bearing-bearing_name-output_type-units
bearing-shaft_nacelle-angle_speed-rpm
value:
ch_dict[tag]['bearing_name']
ch_dict[tag]['output_type']
ch_dict[tag]['chi']
ch_dict[tag]['units']
"""
# ch_df columns, these are created by LoadResults._unified_channel_names
cols = set(['bearing_name', 'sensortag', 'bodyname', 'chi', 'component',
'pos', 'coord', 'sensortype', 'radius', 'blade_nr', 'units',
'output_type', 'io_nr', 'io', 'dll', 'azimuth', 'flap_nr',
'direction', 'wake_source_nr', 'center', 's', 'srel',
'radius_actual'])
# start with reading the .sel file, containing the info regarding
# how to read the binary file and the channel information
def __init__(self, file_path, file_name, debug=False, usecols=None,
readdata=True):
self.debug = debug
# timer in debug mode
if self.debug:
start = time()
self.file_path = file_path
# remove .log, .dat, .sel extensions who might be accedental left
ext = file_name.split('.')[-1]
if ext in ['htc', 'sel', 'dat', 'log', 'hdf5']:
file_name = file_name.replace('.' + ext, '')
# FIXME: since HAWC2 will always have lower case output files, convert
# any wrongly used upper case letters to lower case here
self.file_name = file_name
FileName = os.path.join(self.file_path, self.file_name)
super(LoadResults, self).__init__(FileName, ReadOnly=readdata)
self.FileType = self.FileFormat
if self.FileType.find('HAWC2_') > -1:
self.FileType = self.FileType[6:]
if readdata:
ChVec = [] if usecols is None else usecols
self.sig = self.ReadAll(ChVec=ChVec)
# info in sel file is not available when not reading gtsdf
# so this is only skipped when readdata is false and FileType is gtsdf
if not (not readdata and (self.FileType == 'GTSDF')):
self.N = int(self.NrSc)
self.Nch = int(self.NrCh)
self.ch_details = np.ndarray(shape=(self.Nch, 3), dtype='<U150')
for ic in range(self.Nch):
self.ch_details[ic, 0] = self.ChInfo[0][ic]
self.ch_details[ic, 1] = self.ChInfo[1][ic]
self.ch_details[ic, 2] = self.ChInfo[2][ic]
self._unified_channel_names()
if self.debug:
stop = time() - start
print('time to load HAWC2 file:', stop, 's')
# TODO: THIS IS STILL A WIP
def _make_channel_names(self):
"""Give every channel a unique channel name which is (nearly) identical
to the channel names as defined in the htc output section. Instead
of spaces, use colon (;) to seperate the different commands.
THIS IS STILL A WIP
see also issue #11:
https://gitlab.windenergy.dtu.dk/toolbox/WindEnergyToolbox/issues/11
"""
index = {}
names = {'htc_name':[], 'chi':[], 'label':[], 'unit':[], 'index':[],
'name':[], 'description':[]}
constraint_fmts = {'bea1':'constraint;bearing1',
'bea2':'constraint;bearing2',
'bea3':'constraint;bearing3',
'bea4':'constraint;bearing4'}
# mbdy momentvec tower 1 1 global
force_fmts = {'F':'mbdy;forcevec;{body};{nodenr:03i};{coord};{comp}',
'M':'mbdy;momentvec;{body};{nodenr:03i};{coord};{comp}'}
state_fmt = 'mbdy;{state};{typ};{body};{elnr:03i};{zrel:01.02f};{coord}'
wind_coord_map = {'Vx':'1', 'Vy':'2', 'Vz':'3'}
wind_fmt = 'wind;{typ};{coord};{x};{y};{z};{comp}'
for ch in range(self.Nch):
name = self.ch_details[ch, 0]
name_items = misc.remove_items(name.split(' '), '')
description = self.ch_details[ch, 2]
descr_items = misc.remove_items(description.split(' '), '')
unit = self.ch_details[ch, 1]
# default names
htc_name = ' '.join(name_items+descr_items)
label = ''
coord = ''
typ = ''
elnr = ''
nodenr = ''
zrel = ''
state = ''
# CONSTRAINTS: BEARINGS
if name_items[0] in constraint_fmts:
htc_name = constraint_fmts[name_items[0]] + ';'
htc_name += (descr_items[0] + ';')
htc_name += unit
# MBDY FORCES/MOMENTS
elif name_items[0][0] in force_fmts:
comp = name_items[0]
if comp[0] == 'F':
i0 = 1
else:
i0 = 0
label = description.split('coo: ')[1].split(' ')[1]
coord = descr_items[i0+5]
body = descr_items[i0+1][5:]#.replace('Mbdy:', '')
nodenr = int(descr_items[i0+3])
htc_name = force_fmts[comp[0]].format(body=body, coord=coord,
nodenr=nodenr, comp=comp)
# STATE: POS, VEL, ACC, STATE_ROT
elif descr_items[0][:5] == 'State':
if name_items[0] == 'State':
i0 = 1
state = 'state'
else:
i0 = 0
state = 'state_rot'
typ = name_items[i0+0]
comp = name_items[i0+1]
coord = name_items[i0+3]
body = descr_items[3][5:]#.replace('Mbdy:', '')
elnr = int(descr_items[5])
zrel = float(descr_items[6][6:])#.replace('Z-rel:', ''))
if len(descr_items) > 8:
label = ' '.join(descr_items[9:])
htc_name = state_fmt.format(typ=typ, body=body, elnr=elnr,
zrel=zrel, coord=coord,
state=state)
# WINDSPEED
elif description[:9] == 'Free wind':
if descr_items[4] == 'gl.':
coord = '1' # global
else:
coord = '2' # non-rotating rotor coordinates
try:
comp = wind_coord_map[descr_items[3][:-1]]
typ = 'free_wind'
except KeyError:
comp = descr_items[3]
typ = 'free_wind_hor'
tmp = description.split('pos')[1]
x, y, z = tmp.split(',')
# z might hold a label....
z_items = z.split(' ')
if len(z_items) > 1:
label = ' '.join(z_items[1:])
z = z_items[0]
x, y, z = x.strip(), y.strip(), z.strip()
htc_name = wind_fmt.format(typ=typ, coord=coord, x=x, y=y, z=z,
comp=comp)
names['htc_name'].append(htc_name)
names['chi'].append(ch)
# this is the Channel column from the sel file, so the unique index
# which is dependent on the order of the channels
names['index'].append(ch+1)
names['unit'].append(unit)
names['name'].append(name)
names['description'].append(description)
names['label'].append(label)
names['state'].append(state)
names['type'].append(typ)
names['comp'].append(comp)
names['coord'].append(coord)
names['elnr'].append(coord)
names['nodenr'].append(coord)
names['zrel'].append(coord)
index[name] = ch
return names, index
def _unified_channel_names(self):
"""
Make certain channels independent from their index.
The unified channel dictionary ch_dict holds consequently named
channels as the key, and the all information is stored in the value
as another dictionary.
The ch_dict key/values pairs are structured differently for different
type of channels. Currently supported channels are:
For forcevec, momentvec, state commands:
node numbers start with 0 at the root
element numbers start with 1 at the root
key:
coord-bodyname-pos-sensortype-component
global-tower-node-002-forcevec-z
local-blade1-node-005-momentvec-z
hub1-blade1-elem-011-zrel-1.00-state pos-z
value:
ch_dict[tag]['coord']
ch_dict[tag]['bodyname']
ch_dict[tag]['pos']
ch_dict[tag]['sensortype']
ch_dict[tag]['component']
ch_dict[tag]['chi']
ch_dict[tag]['sensortag']
ch_dict[tag]['units']
For the DLL's this is:
key:
DLL-dll_name-io-io_nr
DLL-yaw_control-outvec-3
DLL-yaw_control-inpvec-1
value:
ch_dict[tag]['dll_name']
ch_dict[tag]['io']
ch_dict[tag]['io_nr']
ch_dict[tag]['chi']
ch_dict[tag]['sensortag']
ch_dict[tag]['units']
For the bearings this is:
key:
bearing-bearing_name-output_type-units
bearing-shaft_nacelle-angle_speed-rpm
value:
ch_dict[tag]['bearing_name']
ch_dict[tag]['output_type']
ch_dict[tag]['chi']
ch_dict[tag]['units']
For many of the aero sensors:
'Cl', 'Cd', 'Alfa', 'Vrel'
key:
sensortype-blade_nr-pos
Cl-1-0.01
value:
ch_dict[tag]['sensortype']
ch_dict[tag]['blade_nr']
ch_dict[tag]['pos']
ch_dict[tag]['chi']
ch_dict[tag]['units']
"""
# save them in a dictionary, use the new coherent naming structure
# as the key, and as value again a dict that hols all the different
# classifications: (chi, channel nr), (coord, coord), ...
self.ch_dict = dict()
# some channel ID's are unique, use them
ch_unique = set(['Omega', 'Ae rot. torque', 'Ae rot. power',
'Ae rot. thrust', 'Time', 'Azi 1'])
ch_aero = set(['Cl', 'Cd', 'Cm', 'Alfa', 'Vrel', 'Tors_e', 'Alfa',
'Lift', 'Drag'])
ch_aerogrid = set(['a_grid', 'am_grid', 'CT', 'CQ'])
# also safe as df
# cols = set(['bearing_name', 'sensortag', 'bodyname', 'chi',
# 'component', 'pos', 'coord', 'sensortype', 'radius',
# 'blade_nr', 'units', 'output_type', 'io_nr', 'io', 'dll',
# 'azimuth', 'flap_nr'])
df_dict = {col: [] for col in self.cols}
df_dict['unique_ch_name'] = []
# -----------------------------------------------------------------
# REGEXes
# -----------------------------------------------------------------
# ESYS output: ESYS line3 SENSOR 66
re_esys = re.compile(r'ESYS (\w+) SENSOR\s*(\d*)')
# FORCE fext_damp 1
re_force = re.compile(r'FORCE (\w+) \s*(\d*)')
# scan through all channels and see which can be converted
# to sensible unified name
for ch in range(self.Nch):
items_ch0 = self.ch_details[ch, 0].split()
items_ch2 = self.ch_details[ch, 2].split()
dll = False
# be carefull, identify only on the starting characters, because
# the signal tag can hold random text that in some cases might
# trigger a false positive
# -----------------------------------------------------------------
# check for all the unique channel descriptions
if self.ch_details[ch,0].strip() in ch_unique:
tag = self.ch_details[ch, 0].strip()
channelinfo = {}
channelinfo['units'] = self.ch_details[ch, 1]
channelinfo['sensortag'] = self.ch_details[ch, 2]
channelinfo['chi'] = ch
# -----------------------------------------------------------------
# or in the long description:
# 0 1 2 3 4 5 6 and up
# MomentMz Mbdy:blade nodenr: 5 coo: blade TAG TEXT
elif self.ch_details[ch, 2].startswith('MomentM'):
coord = items_ch2[5]
bodyname = items_ch2[1].replace('Mbdy:', '')
# set nodenr to sortable way, include leading zeros
# node numbers start with 0 at the root
nodenr = '%03i' % int(items_ch2[3])
# skip the attached the component
# sensortype = items[0][:-2]
# or give the sensor type the same name as in HAWC2
sensortype = 'momentvec'
component = items_ch2[0][-1:len(items_ch2[0])]
# the tag only exists if defined
if len(items_ch2) > 6:
sensortag = ' '.join(items_ch2[6:])
else:
sensortag = ''
# and tag it
pos = 'node-%s' % nodenr
tagitems = (coord, bodyname, pos, sensortype, component)
tag = '%s-%s-%s-%s-%s' % tagitems
# save all info in the dict
channelinfo = {}
channelinfo['coord'] = coord
channelinfo['bodyname'] = bodyname
channelinfo['pos'] = pos
channelinfo['sensortype'] = sensortype
channelinfo['component'] = component
channelinfo['chi'] = ch
channelinfo['sensortag'] = sensortag
channelinfo['units'] = self.ch_details[ch, 1]
# -----------------------------------------------------------------
# 0 1 2 3 4 5 6 7 and up
# Force Fx Mbdy:blade nodenr: 2 coo: blade TAG TEXT
elif self.ch_details[ch, 2].startswith('Force F'):
coord = items_ch2[6]
bodyname = items_ch2[2].replace('Mbdy:', '')
nodenr = '%03i' % int(items_ch2[4])
# skipe the attached the component
# sensortype = items[0]
# or give the sensor type the same name as in HAWC2
sensortype = 'forcevec'
component = items_ch2[1][1]
if len(items_ch2) > 7:
sensortag = ' '.join(items_ch2[7:])
else:
sensortag = ''
# and tag it
pos = 'node-%s' % nodenr
tagitems = (coord, bodyname, pos, sensortype, component)
tag = '%s-%s-%s-%s-%s' % tagitems
# save all info in the dict
channelinfo = {}
channelinfo['coord'] = coord
channelinfo['bodyname'] = bodyname
channelinfo['pos'] = pos
channelinfo['sensortype'] = sensortype
channelinfo['component'] = component
channelinfo['chi'] = ch
channelinfo['sensortag'] = sensortag
channelinfo['units'] = self.ch_details[ch, 1]
# -----------------------------------------------------------------
# 0 1 2 3 4 5 6 7 8 9 and up
# Force_intp Fz Mbdy:blade1 s= 11.87[m] s/S= 0.95 coo: local_aero center:default
# Moment_intp Mx Mbdy:blade1 s= 11.87[m] s/S= 0.95 coo: local_aero center:default
elif items_ch2[0].endswith('_intp'):
sensortype = 'forcemomentvec_interp'
coord = items_ch2[8]
bodyname = items_ch2[2].replace('Mbdy:', '')
s = items_ch2[4].replace('[m]', '')
srel = items_ch2[6]
center = items_ch2[9].split(':')[1]
component = items_ch2[1]
if len(items_ch2) > 9:
sensortag = ' '.join(items_ch2[10:])
else:
sensortag = ''
# and tag it
pos = 's-%s' % (s)
tag = f'{sensortype}-{bodyname}-{center}-{coord}-{s}-{component}'
# save all info in the dict
channelinfo = {}
channelinfo['coord'] = coord
channelinfo['bodyname'] = bodyname
channelinfo['s'] = float(s)
channelinfo['srel'] = float(srel)
channelinfo['sensortype'] = sensortype
# channelinfo['output_type'] = output_type
channelinfo['component'] = component
channelinfo['center'] = center
channelinfo['chi'] = ch
channelinfo['sensortag'] = sensortag
channelinfo['units'] = self.ch_details[ch, 1]
# -----------------------------------------------------------------
# ELEMENT STATES: pos, vel, acc, rot, ang
# 0 1 2 3 4 5 6 7 8
# State pos x Mbdy:blade E-nr: 1 Z-rel:0.00 coo: blade
# 0 1 2 3 4 5 6 7 8 9+
# State_rot proj_ang tx Mbdy:bname E-nr: 1 Z-rel:0.00 coo: cname label
# State_rot omegadot tz Mbdy:bname E-nr: 1 Z-rel:1.00 coo: cname label
elif self.ch_details[ch,2].startswith('State'):
# or self.ch_details[ch,0].startswith('euler') \
# or self.ch_details[ch,0].startswith('ax') \
# or self.ch_details[ch,0].startswith('omega') \
# or self.ch_details[ch,0].startswith('proj'):
coord = items_ch2[8]
bodyname = items_ch2[3].replace('Mbdy:', '')
# element numbers start with 1 at the root
elementnr = '%03i' % int(items_ch2[5])
zrel = '%04.2f' % float(items_ch2[6].replace('Z-rel:', ''))
# skip the attached the component
#sensortype = ''.join(items[0:2])
# or give the sensor type the same name as in HAWC2
tmp = self.ch_details[ch, 0].split(' ')
sensortype = tmp[0]
if sensortype.startswith('State'):
sensortype += ' ' + tmp[1]
component = items_ch2[2]
if len(items_ch2) > 8:
sensortag = ' '.join(items_ch2[9:])
else:
sensortag = ''
# and tag it
pos = 'elem-%s-zrel-%s' % (elementnr, zrel)
tagitems = (coord, bodyname, pos, sensortype, component)
tag = '%s-%s-%s-%s-%s' % tagitems
# save all info in the dict
channelinfo = {}
channelinfo['coord'] = coord
channelinfo['bodyname'] = bodyname
channelinfo['pos'] = pos
channelinfo['sensortype'] = sensortype
channelinfo['component'] = component
channelinfo['chi'] = ch
channelinfo['sensortag'] = sensortag
channelinfo['units'] = self.ch_details[ch, 1]
# -----------------------------------------------------------------
# statevec_new
# 0 1 2 3 4 5 6 7
# elastic Deflection blade1 Dx Mbdy:blade1 s= 0.00[m] s/S=
# 8 9 10 11
# 0.00 coo: blade1 center:c2def
# note that: 2 and 10 are the same
elif items_ch2[0] == 'elastic' or items_ch2[0] == 'absolute':
output_type = ' '.join(items_ch2[0:2])
bodyname = items_ch2[4].replace('Mbdy:', '')
s = '%06.02f' % float(items_ch2[6].replace('[m]', ''))
srel = '%04.02f' % float(items_ch2[8])
coord = items_ch2[10]
center = items_ch2[11].split(':')[1]
sensortype = 'statevec_new'