forked from vonSchlotzkow/CFSR2power
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertWind.py
executable file
·93 lines (77 loc) · 3.08 KB
/
convertWind.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
#!/usr/bin/env python
from CFSRwrapper import *
from optparse import OptionGroup
from pylab import interp
from configobj import ConfigObj
from validate import Validator
from StringIO import StringIO
locoptions=OptionGroup(parser, "Wind conversion options", "Options related to converting ")
locoptions.add_option("--turbinecurve", dest="turbinecurve", default=None, type=str,
help="File containing the characteristic curve of a turbine")
locoptions.add_option("--verifyturbinecurve", dest="verifyturbinecurve",
action="store_true",
help="Help verifying the turbine curve given in the cfg file by plotting it.")
parser.add_option_group(locoptions)
(options, args) = parser.parse_args()
infields=['wnd10m']
#the config file spec
spec=StringIO("""
# Turbine characteristic data
name = string
# The manufacturer of this turbine
manufacturer = string
# Link to the original datasheet of the turbine
source = string
# Hub height
H = float
# Power curve, specified as lists of velocities and powers, including
# cut-in and cut-out speeds
# Power curve velocities
V = list
# Generated power from the power curves
POW = list
""")
configspec=ConfigObj(spec,
list_values=False, file_error=True, _inspec=True)
turbineconfig=ConfigObj(options.turbinecurve,
list_values=True, file_error=True,
configspec=configspec)
turbineconfig.validate(Validator())
# it would be more elegant to give the requirement for lists of floats
# in the spec, which should result in a conversion to floats at the
# same time, but well...
assert(len(turbineconfig['V'])==len(turbineconfig['POW']))
for listkey in ['V','POW']:
turbineconfig[listkey]=map(float,turbineconfig[listkey])
def WindConversion(data,c):
"""Conversion of wind speed to wind power"""
wind10m = data[0]
#Turbine data
H = c['H'] #Hub height
V = c['V'] #Power curve velocities
POW = c['POW'] #power from the power curves
#Convert wind speed to hub height H from a height 10m above the ground
#0.143 is power law index which depends on roughness of the surface and assumed to be constant for the time being.
wind = wind10m*((H/10)**0.143)
#Apply power curve
P = interp(wind,V,POW) #interpolation using power curve data
return P
if options.verifyturbinecurve:
# plot interpolated power curve
from pylab import arange,plot,title,show
v=arange(0,30,0.1)
plot(v,interp(v,turbineconfig['V'],turbineconfig['POW']),label=turbineconfig['name'])
title("%s: %s" % (turbineconfig['manufacturer'],turbineconfig['name']))
show()
exit()
it=openfields(infields,options.year,options.month,options.lowres)
outf=file(filenamefromfield("WindPower_%s" % turbineconfig['name'],options.year,options.month,options.lowres),'wb')
#example of binding additional options to the conversion function
convfunc=lambda x:WindConversion(x,turbineconfig)
if options.debug:
#convert just one timestep
i=it.next()
unpackandapply(i,convfunc,outf)
else:
#convert all
iterateandapply(it,convfunc,outf)