-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.py
1359 lines (994 loc) · 56.7 KB
/
configuration.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
import xml.dom.minidom
import dateutil
import os
import pandas as pd
import numpy as np
import datetime
isoDateFormat = "%Y-%m-%dT%H:%M:00"
class XmlBase:
def readDoc(self, path):
return xml.dom.minidom.parse(path)
def getPath(self, node):
return self.getValue(node).replace("\\", os.sep).replace("/", os.sep)
def nodeValueExists(self, node, query):
if self.nodeExists(node, query):
subNode = self.getNode(node, query)
if subNode.firstChild == None:
return False
elif subNode.firstChild.data == None:
return False
else:
return True
else:
return False
def getValue(self, node):
firstChild = node.firstChild
if firstChild != None:
return node.firstChild.data
else:
return ""
def getNodeDate(self, node, query):
return dateutil.parser.parse(self.getNodeValue(node, query))
def getNodeBool(self, node, query):
return self.getNodeValue(node, query) == "1"
def getNodeInt(self, node, query):
return int(self.getNodeValue(node, query))
def getNodeFloat(self, node, query):
return float(self.getNodeValue(node, query))
def getNodeValue(self, node, query):
return self.getValue(self.getNode(node, query))
def getNodePath(self, node, query):
return self.getPath(self.getNode(node, query))
def getNode(self, node, query):
if not self.nodeExists(node, query):
raise Exception("Node not found %s" % query)
return self.getNodes(node, query)[0]
def getNodes(self, node, query):
return node.getElementsByTagNameNS("http://www.pcwg.org", query)
def nodeExists(self, node, query):
return(len(self.getNodes(node, query)) > 0)
if exists: #? unreachable code here?
return len(self.getNodeValue(node, query)) > 0
else:
return False
def nodeValueExists(self, node, query): #seems to be defined above
if self.nodeExists(node, query):
subNode = self.getNode(node, query)
return (subNode.firstChild != None)
else:
return False
def addNode(self, doc, parentNode, nodeName):
node = doc.createElement(nodeName)
parentNode.appendChild(node)
return node
def addTextNode(self, doc, parentNode, nodeName, value):
value = '' if value is None else value
node = self.addNode(doc, parentNode, nodeName)
node.appendChild(doc.createTextNode(value.strip()))
def addDateNode(self, doc, parentNode, nodeName, value):
node = self.addNode(doc, parentNode, nodeName)
textValue = value.strftime(isoDateFormat)
node.appendChild(doc.createTextNode(textValue))
def addIntNode(self, doc, parentNode, nodeName, value):
self.addTextNode(doc, parentNode, nodeName, "%d" % value)
def addBoolNode(self, doc, parentNode, nodeName, value):
if value:
self.addTextNode(doc, parentNode, nodeName, "1")
else:
self.addTextNode(doc, parentNode, nodeName, "0")
def addFloatNode(self, doc, parentNode, nodeName, value):
self.addTextNode(doc, parentNode, nodeName, "%f" % float(value))
def addFloatNode2DP(self, doc, parentNode, nodeName, value):
self.addTextNode(doc, parentNode, nodeName, "{0:.2f}".format(float(value)))
def addFloatNode3DP(self, doc, parentNode, nodeName, value):
self.addTextNode(doc, parentNode, nodeName, "{0:.3f}".format(float(value)))
def createDocument(self):
return xml.dom.minidom.Document()
def addRootNode(self, doc, nodeName, namespace, schema = ""):
root = self.addNode(doc, doc, nodeName)
root.setAttribute("xmlns", namespace)
if len(schema) > 0:
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
root.setAttribute("xsi:schemaLocation", "%s %s" % (namespace, schema))
return root
def saveDocument(self, doc, path):
file_handle = open(path,"wb")
file_handle.write(doc.toprettyxml())
file_handle.close()
def getNodeValueIfExists(self, parent, query, valueNotExist):
if self.nodeExists(parent, query):
node = self.getNode(parent, query)
if node.firstChild != None:
return self.getNodeValue(parent, query)
else:
return valueNotExist
else:
return valueNotExist
def readSimpleFilter(self,node,active=True):
column = self.getNodeValue(node, 'DataColumn')
inclusive = self.getNodeBool(node, 'Inclusive')
filterType = self.getNodeValue(node, 'FilterType')
active = active if active else self.getNodeBool(node, 'Active')
if not len(self.getNode(node, 'FilterValue').childNodes) >1:
value = self.getNodeValue(node, 'FilterValue')
if not "," in value:
value = float(value)
return Filter(active, column, filterType, inclusive, value)
else:
valueNode = self.getNode(node, 'FilterValue')
columnFactors = []
factorNodes = self.getNode(valueNode,'ColumnFactors') if self.nodeExists(valueNode,'ColumnFactors') else valueNode
for columnFactor in self.getNodes(factorNodes,'ColumnFactor'):
columnFactors.append((
self.getNodeValueIfExists(columnFactor, 'ColumnName', 'Actual Power'),
self.getNodeValueIfExists(columnFactor, 'A', 1),
self.getNodeValueIfExists(columnFactor, 'B', 0),
self.getNodeValueIfExists(columnFactor, 'C', 1)
))
return Filter(active, column, filterType, inclusive, columnFactors,derived=True)
def readToDFilter(self,active,node):
startTime = self.getNodeDate(node, 'StartTime')
endTime = self.getNodeDate(node, 'EndTime')
days = self.getNodeValueIfExists(node,"DaysOfTheWeek","1,2,3,4,5,6,7")
months = self.getNodeValueIfExists(node,"Months",[])
if months != []:
months = [int(a) for a in months.split(",")]
days = [int(a) for a in days.split(",")]
return TimeOfDayFilter(active,startTime,endTime,days,months)
def readRelationshipFilter(self, active, node):
conjunction = self.getNodeValue(node,"Conjunction")
clauses = []
for clause in self.getNodes(node,"Clause"):
clauses.append(self.readSimpleFilter(clause))
return RelationshipFilter(active, conjunction, clauses)
class RelativePath:
def __init__(self, basePath):
self.baseFolder = self.replaceFileSeparators(os.path.dirname(os.path.abspath(basePath)))
def convertToAbsolutePath(self, path):
return os.path.join(self.baseFolder, path);
def convertToRelativePath(self, path):
if len(path) <= len(self.baseFolder): return path
filePath = self.replaceFileSeparators(path)
folderLength = len(self.baseFolder)
pathLength = len(filePath)
if self.baseFolder == filePath[0:folderLength]:
return filePath[folderLength + 1: pathLength]
else:
return filePath
def replaceFileSeparators(self, filePath):
replacedFilePath = filePath.replace("\\", os.path.sep)
replacedFilePath = filePath.replace("/", os.path.sep)
return replacedFilePath
class Preferences(XmlBase):
def __init__(self):
self.path = "preferences.xml"
try:
loaded = self.loadPreferences()
except Exception as e:
print e
loaded = False
if not loaded:
self.analysisLastOpened = ""
self.workSpaceFolder = ""
def loadPreferences(self):
if os.path.isfile(self.path):
doc = self.readDoc(self.path)
root = self.getNode(doc, "Preferences")
self.analysisLastOpened = self.getNodeValueIfExists(doc, "AnalysisLastOpened", "")
self.workSpaceFolder = self.getNodeValueIfExists(doc, "WorkSpaceFolder", "")
return True
else:
return False
def save(self):
doc = self.createDocument()
root = self.addRootNode(doc, "Preferences", "http://www.pcwg.org")
self.addTextNode(doc, root, "AnalysisLastOpened", self.analysisLastOpened)
self.addTextNode(doc, root, "WorkSpaceFolder", self.workSpaceFolder)
self.saveDocument(doc, self.path)
class BenchmarkConfiguration(XmlBase):
def __init__(self, path):
self.path = path
doc = self.readDoc(path)
configurationNode = self.getNode(doc, 'Configuration')
self.name = self.getNodeValueIfExists(configurationNode, 'Name',None)
self.tolerance = self.getNodeFloat(configurationNode, 'Tolerance')
self.readBenchmarks(configurationNode)
def readBenchmarks(self, configurationNode):
benchmarksNode = self.getNode(configurationNode, 'Benchmarks')
self.benchmarks = []
for bnode in self.getNodes(benchmarksNode, 'Benchmark'):
benchmark = Benchmark()
#get the path
benchmark.analysisPath = self.getNodePath(bnode, 'AnalysisConfigPath')
#get the expected results
benchmark.expectedResults = {}
for enode in self.getNodes(self.getNode(bnode, 'ExpectedResults'), 'ExpectedResult'):
benchmark.expectedResults[self.getNodeValue(enode, 'Field')] = self.getNodeFloat(enode, 'Value')
self.benchmarks.append(benchmark)
class Benchmark:
def __init__(self):
self.analysisPath = None
self.expectedResults = None
class AnalysisConfiguration(XmlBase):
def __init__(self, path = None):
defaultPaddingMode = 'None'
if path != None:
self.isNew = False
self.path = path
doc = self.readDoc(path)
configurationNode = self.getNode(doc, 'Configuration')
self.Name = self.getNodeValueIfExists(configurationNode, 'Name',None)
self.powerCurveMinimumCount = self.getNodeInt(configurationNode, 'PowerCurveMinimumCount')
self.baseLineMode = self.getNodeValue(configurationNode, 'BaseLineMode')
self.filterMode = self.getNodeValue(configurationNode, 'FilterMode')
self.powerCurveMode = self.getNodeValue(configurationNode, 'PowerCurveMode')
self.powerCurvePaddingMode = self.getNodeValueIfExists(configurationNode, 'PowerCurvePaddingMode', defaultPaddingMode)
if self.nodeExists(configurationNode, 'PowerCurveBins'):
powerCurveBinsNode = self.getNode(configurationNode, 'PowerCurveBins')
self.powerCurveFirstBin = self.getNodeFloat(powerCurveBinsNode, 'FirstBinCentre')
self.powerCurveLastBin = self.getNodeFloat(powerCurveBinsNode, 'LastBinCentre')
self.powerCurveBinSize = self.getNodeFloat(powerCurveBinsNode, 'BinSize')
else:
self.setDefaultPowerCurveBins()
self.readDatasets(configurationNode)
self.readInnerRange(configurationNode)
self.readTurbine(configurationNode)
self.nominalWindSpeedDistribution = self.getNodeValueIfExists(configurationNode,'NominalWindSpeedDistribution',None)
self.readDensityCorrection(configurationNode)
self.readREWS(configurationNode)
self.readTurbRenorm(configurationNode)
self.readPowerDeviationMatrix(configurationNode)
else:
self.isNew = True
self.Name = ""
self.powerCurveMinimumCount = 10
self.baseLineMode = 'Hub'
self.filterMode = 'All'
self.powerCurveMode = 'Specified'
self.powerCurvePaddingMode = defaultPaddingMode
self.setDefaultPowerCurveBins()
self.setDefaultInnerRangeTurbulence()
self.setDefaultInnerRangeShear()
self.hubHeight = 80.0
self.diameter = 90.0
self.cutInWindSpeed = 3.0
self.cutOutWindSpeed = 25.0
self.ratedPower = 1000.0
self.specifiedPowerCurve = ''
self.nominalWindSpeedDistribution = None
self.rewsActive = False
self.turbRenormActive = False
self.densityCorrectionActive = False
self.specifiedPowerDeviationMatrix = ""
self.powerDeviationMatrixActive = False
def setDefaultInnerRangeTurbulence(self):
self.innerRangeLowerTurbulence = 0.08
self.innerRangeUpperTurbulence = 0.12
def setDefaultInnerRangeShear(self):
self.innerRangeLowerShear = 0.05
self.innerRangeUpperShear = 0.20
def setDefaultPowerCurveBins(self):
self.powerCurveFirstBin = 1.0
self.powerCurveLastBin = 30.0
self.powerCurveBinSize = 1.0
def save(self):
self.isNew = False
doc = self.createDocument()
root = self.addRootNode(doc, "Configuration", "http://www.pcwg.org")
self.writeSettings(doc, root)
self.saveDocument(doc, self.path)
def writeSettings(self, doc, root):
self.addIntNode(doc, root, "PowerCurveMinimumCount", self.powerCurveMinimumCount)
self.addTextNode(doc, root, "FilterMode", self.filterMode)
self.addTextNode(doc, root, "BaseLineMode", self.baseLineMode)
self.addTextNode(doc, root, "PowerCurveMode", self.powerCurveMode)
self.addTextNode(doc, root, "PowerCurvePaddingMode", self.powerCurvePaddingMode)
self.addTextNode(doc, root, "NominalWindSpeedDistribution", self.nominalWindSpeedDistribution)
powerCurveBinsNode = self.addNode(doc, root, "PowerCurveBins")
self.addFloatNode(doc, powerCurveBinsNode, "FirstBinCentre", self.powerCurveFirstBin)
self.addFloatNode(doc, powerCurveBinsNode, "LastBinCentre", self.powerCurveLastBin)
self.addFloatNode(doc, powerCurveBinsNode, "BinSize", self.powerCurveBinSize)
datasetsNode = self.addNode(doc, root, "Datasets")
for dataset in self.datasets:
self.addTextNode(doc, datasetsNode, "Dataset", dataset)
innerRangeNode = self.addNode(doc, root, "InnerRange")
self.addFloatNode(doc, innerRangeNode, "InnerRangeLowerTurbulence", self.innerRangeLowerTurbulence)
self.addFloatNode(doc, innerRangeNode, "InnerRangeUpperTurbulence", self.innerRangeUpperTurbulence)
self.addFloatNode(doc, innerRangeNode, "InnerRangeLowerShear", self.innerRangeLowerShear)
self.addFloatNode(doc, innerRangeNode, "InnerRangeUpperShear", self.innerRangeUpperShear)
turbineNode = self.addNode(doc, root, "Turbine")
self.addFloatNode(doc, turbineNode, "CutInWindSpeed", self.cutInWindSpeed)
self.addFloatNode(doc, turbineNode, "CutOutWindSpeed", self.cutOutWindSpeed)
self.addFloatNode(doc, turbineNode, "RatedPower", self.ratedPower)
self.addFloatNode(doc, turbineNode, "HubHeight", self.hubHeight)
self.addFloatNode(doc, turbineNode, "Diameter", self.diameter)
self.addTextNode(doc, turbineNode, "SpecifiedPowerCurve", self.specifiedPowerCurve)
densityCorrectionNode = self.addNode(doc, root, "DensityCorrection")
self.addBoolNode(doc, densityCorrectionNode, "Active", self.densityCorrectionActive)
turbulenceRenormNode = self.addNode(doc, root, "TurbulenceRenormalisation")
self.addBoolNode(doc, turbulenceRenormNode, "Active", self.turbRenormActive)
rewsNode = self.addNode(doc, root, "RotorEquivalentWindSpeed")
self.addBoolNode(doc, rewsNode, "Active", self.rewsActive)
powerDeviationMatrixNode = self.addNode(doc, root, "PowerDeviationMatrix")
self.addTextNode(doc, powerDeviationMatrixNode, "SpecifiedPowerDeviationMatrix", self.specifiedPowerDeviationMatrix)
self.addBoolNode(doc, powerDeviationMatrixNode, "Active", self.powerDeviationMatrixActive)
def readDatasets(self, configurationNode):
datasetsNode = self.getNode(configurationNode, 'Datasets')
self.datasets = []
for node in self.getNodes(datasetsNode, 'Dataset'):
self.datasets.append(self.getPath(node))
def readInnerRange(self, configurationNode):
innerRangeNode = self.getNode(configurationNode, 'InnerRange')
self.innerRangeLowerTurbulence = self.getNodeFloat(innerRangeNode, 'InnerRangeLowerTurbulence')
self.innerRangeUpperTurbulence = self.getNodeFloat(innerRangeNode, 'InnerRangeUpperTurbulence')
self.setDefaultInnerRangeShear()
if self.nodeExists(innerRangeNode, 'InnerRangeLowerShear'): self.innerRangeLowerShear = self.getNodeFloat(innerRangeNode, 'InnerRangeLowerShear')
if self.nodeExists(innerRangeNode, 'InnerRangeUpperShear'): self.innerRangeUpperShear = self.getNodeFloat(innerRangeNode, 'InnerRangeUpperShear')
def readTurbine(self, configurationNode):
turbineNode = self.getNode(configurationNode, 'Turbine')
self.hubHeight = self.getNodeFloat(turbineNode, 'HubHeight')
self.diameter = self.getNodeFloat(turbineNode, 'Diameter')
self.cutInWindSpeed = self.getNodeFloat(turbineNode, 'CutInWindSpeed')
self.cutOutWindSpeed = self.getNodeFloat(turbineNode, 'CutOutWindSpeed')
self.ratedPower = self.getNodeFloat(turbineNode, 'RatedPower')
self.specifiedPowerCurve = self.getNodeValueIfExists(turbineNode, 'SpecifiedPowerCurve','')
def readPowerDeviationMatrix(self, configurationNode):
if self.nodeExists(configurationNode, 'PowerDeviationMatrix'):
powerDeviationMatrixNode = self.getNode(configurationNode, 'PowerDeviationMatrix')
self.powerDeviationMatrixActive = self.getNodeBool(powerDeviationMatrixNode, 'Active')
self.specifiedPowerDeviationMatrix = self.getNodeValue(powerDeviationMatrixNode, 'SpecifiedPowerDeviationMatrix')
else:
self.powerDeviationMatrixActive = False
self.specifiedPowerDeviationMatrix = ""
def readREWS(self, configurationNode):
if self.nodeExists(configurationNode, 'RotorEquivalentWindSpeed'):
rewsNode = self.getNode(configurationNode, 'RotorEquivalentWindSpeed')
self.rewsActive = self.getNodeBool(rewsNode, 'Active')
else:
self.rewsActive = False
def readTurbRenorm(self, configurationNode):
if self.nodeExists(configurationNode, 'TurbulenceRenormalisation'):
turbulenceNode = self.getNode(configurationNode, 'TurbulenceRenormalisation')
self.turbRenormActive = self.getNodeBool(turbulenceNode, 'Active')
else:
self.turbRenormActive = False
def readDensityCorrection(self, configurationNode):
if self.nodeExists(configurationNode, 'DensityCorrection'):
densityNode = self.getNode(configurationNode, 'DensityCorrection')
self.densityCorrectionActive = self.getNodeBool(densityNode, 'Active')
else:
self.densityCorrectionActive = False
class PowerCurveConfiguration(XmlBase):
def __init__(self, path = None):
if path != None:
self.isNew = False
doc = self.readDoc(path)
self.path = path
powerCurveNode = self.getNode(doc, 'PowerCurve')
self.name = self.getNodeValue(powerCurveNode, 'Name')
self.powerCurveDensity = self.getNodeFloat(powerCurveNode, 'PowerCurveDensity')
self.powerCurveTurbulence = self.getNodeFloat(powerCurveNode, 'PowerCurveTurbulence')
powerCurveDictionary = {}
for node in self.getNodes(powerCurveNode, 'PowerCurveLevel'):
speed = self.getNodeFloat(node, 'PowerCurveLevelWindSpeed')
power = self.getNodeFloat(node, 'PowerCurveLevelPower')
powerCurveDictionary[speed] = power
self.setPowerCurve(powerCurveDictionary)
else:
self.isNew = True
self.name = ""
self.powerCurveDensity = 1.225 #0.0
self.powerCurveTurbulence = 0.0
self.setPowerCurve()
def setPowerCurve(self, powerCurveDictionary = {}):
self.powerCurveDictionary = powerCurveDictionary
speeds, powers = [], []
for speed in self.powerCurveDictionary:
speeds.append(speed)
powers.append(self.powerCurveDictionary[speed])
if len(speeds) == 0:
self.powerCurveLevels = pd.Series()
else:
self.powerCurveLevels = pd.DataFrame(powers, index = speeds, columns = ['Specified Power'])
self.powerCurveLevels['Specified Turbulence'] = self.powerCurveTurbulence
def save(self):
print"saving power curve"
doc = self.createDocument()
root = self.addRootNode(doc, "PowerCurve", "http://www.pcwg.org")
self.addTextNode(doc, root, "Name", self.name)
self.addFloatNode(doc, root, "PowerCurveDensity", self.powerCurveDensity)
self.addFloatNode(doc, root, "PowerCurveTurbulence", self.powerCurveTurbulence)
for speed in sorted(self.powerCurveDictionary):
power = self.powerCurveDictionary[speed]
levelNode = self.addNode(doc, root, "PowerCurveLevel")
self.addFloatNode(doc, levelNode, "PowerCurveLevelWindSpeed", speed)
self.addFloatNode(doc, levelNode, "PowerCurveLevelPower", power)
self.saveDocument(doc, self.path)
class TimeOfDayFilter(XmlBase):
""" Time of Day filter. everything after startTime is removed AND before endTime is removed.
"""
def __init__(self,active, startTime, endTime, daysOfTheWeek, months=[]):
self.active = active
self.startTime = startTime
self.endTime = endTime
self.daysOfTheWeek = daysOfTheWeek
self.applied = False
self.months = months
self.column = "TimeStamp"
def printSummary(self):
print str(self)
def __str__(self):
strMonths = "" if len(self.months) == 0 else "in months {0}".format(self.months)
return "TimeOfDayFilter: {st} - {end} on days:{days} {months}".format (st=self.startTime.time(),
end= self.endTime.time(),
days=",".join(str(a) for a in self.daysOfTheWeek),
months= strMonths)
class Filter(XmlBase):
def __init__(self, active, column,filterType,inclusive,value,derived=False):
self.active = active
self.derived = derived
self.column = column
self.filterType = filterType
self.inclusive = inclusive
self.value = value
self.applied = False
def printSummary(self):
print "{dev}\t{col}\t{typ}\t{incl}\t{desc}".format (dev=self.derived,
col= self.column,
typ=self.filterType,
incl=self.inclusive,
desc=self.__str__())
if not self.derived:
return str(self.value)
else:
return " * ".join(["({col}*{A} + {B})^{C}".format(col=factor[0],A=factor[1],B=factor[2],C=factor[3]) for factor in self.value])
def __str__(self):
if not self.derived:
return str(self.value)
else:
return " * ".join(["({col}*{A} + {B})^{C}".format(col=factor[0],A=factor[1],B=factor[2],C=factor[3]) for factor in self.value])
class RelationshipFilter(XmlBase):
def __str__(self):
return " - ".join([" {0} ".format(self.conjunction).join(["{0}:{1} ".format(c.filterType,c.value) for c in self.clauses])])
def printSummary(self):
print "{dev}\t{col}\t{typ}\t{incl}\t{desc}\t{conj}".format (dev="\t",
col= self.column,
typ=self.filterType,
incl=self.inclusive,
desc=self.__str__(),
conj=self.conjunction)
return self.__str__()
def __init__(self, active, conjunction, filters):
self.active = active
self.applied = False
self.conjunction = conjunction
self.clauses = filters
self.sortClauses()
def sortClauses(self):
# for excel reporting
self.column = ", ".join([", ".join(str(f.column) for f in self.clauses)])
self.filterType = ", ".join([", ".join(str(f.filterType) for f in self.clauses)])
self.inclusive = ", ".join([", ".join(str(f.inclusive) for f in self.clauses)])
self.value = ", ".join([", ".join(str(f.value) for f in self.clauses)])
class DatasetConfiguration(XmlBase):
def __init__(self, path = None):
if path != None:
self.isNew = False
self.path = path
doc = self.readDoc(path)
configurationNode = self.getNode(doc, 'Configuration')
if self.nodeExists(configurationNode, 'GeneralSettings'):
collectorNode = self.getNode(configurationNode, 'GeneralSettings')
else:
collectorNode = configurationNode
self.name = self.getNodeValue(collectorNode, 'Name')
self.startDate = self.getNodeDate(collectorNode, 'StartDate') if self.nodeValueExists(collectorNode, 'StartDate') else None
self.endDate = self.getNodeDate(collectorNode, 'EndDate') if self.nodeValueExists(collectorNode, 'EndDate') else None
self.hubWindSpeedMode = self.getNodeValue(collectorNode, 'HubWindSpeedMode')
self.calculateHubWindSpeed = self.getCalculateMode(self.hubWindSpeedMode)
self.densityMode = self.getNodeValue(collectorNode, 'DensityMode')
self.calculateDensity = self.getCalculateMode(self.densityMode)
if self.nodeExists(collectorNode, 'CalibrationMethod'):
try:
self.calibrationMethod = self.getNodeValue(collectorNode, 'CalibrationMethod')
except:
self.calibrationMethod = ""
else:
self.calibrationMethod = ""
self.turbulenceWSsource = self.getNodeValueIfExists(collectorNode, 'TurbulenceWindSpeedSource', 'Reference')
self.referenceWindDirection = self.getNodeValueIfExists(configurationNode, 'ReferenceWindDirection', None)
profileNode = self.getNode(configurationNode, 'ProfileLevels') if self.nodeExists(configurationNode, 'ProfileLevels') else configurationNode
self.readProfileLevels(profileNode)
self.readREWS(configurationNode) # duplicate?
self.readMeasurements(configurationNode)
measNode = self.getNode(configurationNode, 'Measurements')
shearNode = measNode if self.nodeExists(measNode, 'ShearMeasurements') else configurationNode
self.setUpShearMeasurements(shearNode)
if self.nodeExists(configurationNode,"Filters"):
self.filters = self.readFilters([n for n in self.getNode(configurationNode,"Filters").childNodes if not n.nodeType in (n.TEXT_NODE,n.COMMENT_NODE)])
self.hasFilters = (len(self.filters) > 0)
self.readExclusions(configurationNode)
self.readCalibration(configurationNode)
if self.nodeExists(configurationNode, 'SensitivityAnalysis'):
self.readSensitivityAnalysis(configurationNode)
else:
self.sensitivityDataColumns = []
self.invariant_rand_id = self.getNodeValueIfExists(configurationNode, 'InvariantRandomID', None)
if self.invariant_rand_id is None:
self.save()
else:
self.isNew = True
self.name = None
self.startDate = None
self.endDate = None
self.hubWindSpeedMode = 'None'
self.calculateHubWindSpeed = False
self.densityMode = 'None'
self.calculateDensity = False
self.turbulenceWSsource = 'Reference'
self.calibrationMethod = 'None'
self.rewsDefined = False
self.numberOfRotorLevels = 0
self.rotorMode = ''
self.hubMode = ''
self.inputTimeSeriesPath = ''
self.badData = -99.99
self.timeStepInSeconds = 600
self.dateFormat = '%Y-%m-%d %H:%M:%S'
self.separator = "TAB"
self.decimal = "FULL STOP"
self.headerRows = 0
self.timeStamp = ''
self.referenceWindSpeed = ''
self.referenceWindSpeedStdDev = ''
self.referenceWindDirection = None
self.referenceWindDirectionOffset = 0
self.turbineLocationWindSpeed = ''
self.turbineAvailabilityCount = ''
self.hubWindSpeed= ''
self.hubTurbulence = ''
self.temperature = ''
self.pressure = ''
self.power = ''
self.powerMin = ''
self.powerMax = ''
self.powerSD = ''
self.density = ''
self.shearMeasurements = {}
self.shearMeasurements[50.0] = ''
self.shearMeasurements[60.0] = ''
self.filters = []
self.calibrationDirections = {}
self.exclusions = []
self.calibrationStartDate = None
self.calibrationEndDate = None
self.siteCalibrationNumberOfSectors = 36
self.siteCalibrationCenterOfFirstSector = 0
self.calibrationFilters = []
self.calibrationSlopes = {}
self.calibrationOffsets = {}
self.calibrationActives = {}
self.invariant_rand_id = None
def parseDate(self, dateText):
if dateText != None and len(dateText) > 0:
try:
return datetime.datetime.strptime(dateText, isoDateFormat)
except Exception as e:
print "Cannot parse date (%s) using isoformat (%s): %s. Attemping parse with %s" % (dateText, isoDateFormat, e.message, self.dateFormat)
try:
return datetime.datetime.strptime(dateText, self.dateFormat)
except Exception as e:
raise Exception("Cannot parse date: %s (%s)" % (dateText, e.message))
else:
return None
def save(self):
self.isNew = False
doc = self.createDocument()
root = self.addRootNode(doc, "Configuration", "http://www.pcwg.org")
self.writeSettings(doc, root)
self.saveDocument(doc, self.path)
def writeSettings(self, doc, root):
if self.invariant_rand_id is not None:
self.addTextNode(doc, root, 'InvariantRandomID', self.invariant_rand_id)
else:
inv_id = str(np.random.rand())[2:8]
while len(inv_id) != 6:
inv_id = str(np.random.rand())[2:8]
self.invariant_rand_id = "D%06d" % int(inv_id)
self.addTextNode(doc, root, 'InvariantRandomID', self.invariant_rand_id)
genSettingsNode = self.addNode(doc, root, "GeneralSettings")
self.addTextNode(doc, genSettingsNode, "Name", self.name)
if self.startDate != None: self.addDateNode(doc, genSettingsNode, "StartDate", self.startDate)
if self.endDate != None: self.addDateNode(doc, genSettingsNode, "EndDate", self.endDate)
self.addTextNode(doc, genSettingsNode, "HubWindSpeedMode", self.hubWindSpeedMode)
self.addTextNode(doc, genSettingsNode, "CalibrationMethod", self.calibrationMethod)
self.addTextNode(doc, genSettingsNode, "DensityMode", self.densityMode)
if self.rewsDefined:
rewsNode = self.addNode(doc, root, "RotorEquivalentWindSpeed")
self.addIntNode(doc, rewsNode, "NumberOfRotorLevels", self.numberOfRotorLevels)
self.addTextNode(doc, rewsNode, "RotorMode", self.rotorMode)
self.addTextNode(doc, rewsNode, "HubMode", self.hubMode)
measurementsNode = self.addNode(doc, root, "Measurements")
self.addTextNode(doc, measurementsNode, "InputTimeSeriesPath", self.inputTimeSeriesPath)
try:
self.addFloatNode(doc, measurementsNode, "BadDataValue", self.badData)
except:
self.addTextNode(doc, measurementsNode, "BadDataValue", self.badData)
self.addTextNode(doc, measurementsNode, "DateFormat", self.dateFormat)
self.addTextNode(doc, measurementsNode, "Separator", self.separator)
self.addTextNode(doc, measurementsNode, "Decimal", self.decimal)
self.addIntNode(doc, measurementsNode, "HeaderRows", self.headerRows)
self.addTextNode(doc, measurementsNode, "TimeStamp", self.timeStamp)
self.addIntNode(doc, measurementsNode, "TimeStepInSeconds", self.timeStepInSeconds)
self.addTextNode(doc, measurementsNode, "ReferenceWindSpeed", self.referenceWindSpeed)
self.addTextNode(doc, measurementsNode, "ReferenceWindSpeedStdDev", self.referenceWindSpeedStdDev)
self.addTextNode(doc, measurementsNode, "ReferenceWindDirection", self.referenceWindDirection)
self.addFloatNode(doc, measurementsNode, "ReferenceWindDirectionOffset", self.referenceWindDirectionOffset)
self.addTextNode(doc, measurementsNode, "Temperature", self.temperature)
self.addTextNode(doc, measurementsNode, "Pressure", self.pressure)
self.addTextNode(doc, measurementsNode, "Density", self.density)
self.addTextNode(doc, measurementsNode, "TurbineLocationWindSpeed", self.turbineLocationWindSpeed)
if self.turbineAvailabilityCount != '':
self.addTextNode(doc, measurementsNode, "TurbineAvailabilityCount", self.turbineAvailabilityCount)
if self.power is not None:
self.addTextNode(doc, measurementsNode, "Power", self.power)
if self.powerMin is not None:
self.addTextNode(doc, measurementsNode, "PowerMin", self.powerMin)
if self.powerMax is not None:
self.addTextNode(doc, measurementsNode, "PowerMax", self.powerMax)
if self.powerSD is not None:
self.addTextNode(doc, measurementsNode, "PowerSD", self.powerSD)
self.addTextNode(doc, measurementsNode, "HubWindSpeed", self.hubWindSpeed)
self.addTextNode(doc, measurementsNode, "HubTurbulence", self.hubTurbulence)
# todo - change for ref and turbine shears.
if 'ReferenceLocation' in self.shearMeasurements.keys() and 'TurbineLocation' in self.shearMeasurements.keys():
raise NotImplementedError
else:
shearMeasurementsNode = self.addNode(doc, root, "ShearMeasurements")
for shearMeas in self.shearMeasurements.iteritems():
measNode = self.addNode(doc, shearMeasurementsNode, "ShearMeasurement")
self.addFloatNode(doc, measNode, "Height", shearMeas[0])
self.addTextNode(doc, measNode, "WindSpeed", shearMeas[1])
levelsNode = self.addNode(doc, root, "ProfileLevels")
for height in self.windSpeedLevels:
levelNode = self.addNode(doc, levelsNode, "ProfileLevel")
self.addFloatNode(doc, levelNode, "Height", height)
self.addTextNode(doc, levelNode, "ProfileWindSpeed", self.windSpeedLevels[height])
self.addTextNode(doc, levelNode, "ProfileWindDirection", self.windDirectionLevels[height])
#write clibrations
calibrationNode = self.addNode(doc, root, "Calibration")
calibrationParamsNode = self.addNode(doc, calibrationNode, "CalibrationParameters")
if self.calibrationStartDate != None: self.addDateNode(doc, calibrationParamsNode, "CalibrationStartDate", self.calibrationStartDate)
if self.calibrationEndDate != None: self.addDateNode(doc, calibrationParamsNode, "CalibrationEndDate", self.calibrationEndDate)
if self.siteCalibrationNumberOfSectors is not None:
self.addIntNode(doc, calibrationParamsNode, "NumberOfSectors", self.siteCalibrationNumberOfSectors)
self.addIntNode(doc, calibrationParamsNode, "CenterOfFirstSector", self.siteCalibrationCenterOfFirstSector)
calibrationFiltersNode = self.addNode(doc, calibrationNode, "CalibrationFilters")
for calibrationFilterItem in self.calibrationFilters:
if isinstance(calibrationFilterItem, RelationshipFilter):
self.writeRelationshipFilter(doc, calibrationFiltersNode, calibrationFilterItem, "CalibrationFilter")
else:
self.writeFilter(doc, calibrationFiltersNode, calibrationFilterItem, "CalibrationFilter")
calibrationDirectionsNode = self.addNode(doc, calibrationNode, "CalibrationDirections")
for direction in self.calibrationDirections:
calibrationDirectionNode = self.addNode(doc, calibrationDirectionsNode, "CalibrationDirection")
self.addFloatNode(doc, calibrationDirectionNode, "DirectionCentre", direction)
self.addFloatNode(doc, calibrationDirectionNode, "Slope", self.calibrationSlopes[direction])
self.addFloatNode(doc, calibrationDirectionNode, "Offset", self.calibrationOffsets[direction])
self.addBoolNode(doc, calibrationDirectionNode, "Active", self.calibrationActives[direction])
#write filters
filtersNode = self.addNode(doc, root, "Filters")
for filterItem in self.filters:
if isinstance(filterItem, RelationshipFilter):
self.writeRelationshipFilter(doc, filtersNode, filterItem, "Filter")
else:
self.writeFilter(doc, filtersNode, filterItem, "Filter")
#write exclusions
exclusionsNode = self.addNode(doc, root, "Exclusions")
for exclusion in self.exclusions:
exclusionNode = self.addNode(doc, exclusionsNode, "Exclusion")
self.addBoolNode(doc, exclusionNode, "ExclusionActive", exclusion[2])
self.addDateNode(doc, exclusionNode, "ExclusionStartDate", exclusion[0])
self.addDateNode(doc, exclusionNode, "ExclusionEndDate", exclusion[1])
def readREWS(self, configurationNode):
if self.nodeExists(configurationNode, 'RotorEquivalentWindSpeed'):
rewsNode = self.getNode(configurationNode, 'RotorEquivalentWindSpeed')
self.rewsDefined = True
self.rotorMode = self.getNodeValue(rewsNode, 'RotorMode')
self.hubMode = self.getNodeValue(rewsNode, 'HubMode')
self.numberOfRotorLevels = self.getNodeInt(rewsNode, 'NumberOfRotorLevels')
else:
self.rewsDefined = False
self.rotorMode = ""
self.hubMode = ""
self.numberOfRotorLevels = 0
def readShearMeasurements(self, node):
measurements = {}
for shearMeasureNode in self.getNodes(node,"ShearMeasurement"):
shearColName = self.getNodeValue(shearMeasureNode,"WindSpeed")
shearHeight = self.getNodeFloat(shearMeasureNode,"Height")
measurements[shearHeight] = shearColName
#backwards compatibility
if self.nodeValueExists(node, "LowerWindSpeedHeight"):
shearColName = self.getNodeValue(node,"LowerWindSpeed")
shearHeight = self.getNodeFloat(node,"LowerWindSpeedHeight")
if not shearHeight in measurements:
measurements[shearHeight] = shearColName
#backwards compatibility
if self.nodeValueExists(node, "UpperWindSpeedHeight"):
shearColName = self.getNodeValue(node,"UpperWindSpeed")
shearHeight = self.getNodeFloat(node,"UpperWindSpeedHeight")
if not shearHeight in measurements:
measurements[shearHeight] = shearColName
return measurements
def readMeasurements(self, configurationNode):
measurementsNode = self.getNode(configurationNode, 'Measurements')
self.inputTimeSeriesPath = self.getNodePath(measurementsNode, 'InputTimeSeriesPath')
self.dateFormat = self.getNodeValue(measurementsNode, 'DateFormat')
self.timeStepInSeconds = self.getNodeInt(measurementsNode, 'TimeStepInSeconds')
self.timeStamp = self.getNodeValue(measurementsNode, 'TimeStamp')
self.badData = self.getNodeValue(measurementsNode, 'BadDataValue')
self.headerRows = self.getNodeInt(measurementsNode, 'HeaderRows')
self.separator = self.getNodeValueIfExists(measurementsNode, 'Separator', 'TAB')
self.decimal = self.getNodeValueIfExists(measurementsNode, 'Decimal', 'FULL STOP')
self.turbineLocationWindSpeed = self.getNodeValueIfExists(measurementsNode, 'TurbineLocationWindSpeed', '')
self.turbineAvailabilityCount = self.getNodeValueIfExists(measurementsNode, 'TurbineAvailabilityCount', '')
self.hubWindSpeed = self.getNodeValueIfExists(measurementsNode, 'HubWindSpeed', '')
self.hubTurbulence = self.getNodeValueIfExists(measurementsNode, 'HubTurbulence', '')