Skip to content

Instantly share code, notes, and snippets.

@digitalWestie
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save digitalWestie/8865667 to your computer and use it in GitHub Desktop.

Select an option

Save digitalWestie/8865667 to your computer and use it in GitHub Desktop.
# USAGE (in ruby interpreter)
#
# require './tiree-data.rb'
# wind_speed = 5.0 #in m/s
# result = estimate(wind_speed)
#For estimating E-44 output
def estimate(windspeed)
#some rounding to make sure ranges are intact
windspeed = windspeed.round(3)
#lookup format => [windspeed (ms), output (kw), coefficient]
lookup = [[1, 0, 0.0], [2, 0, 0.0], [3, 4, 0.16], [4, 20, 0.34], [5, 50, 0.43], [6, 96, 0.48],
[7, 156, 0.49], [8, 238, 0.5], [9, 340, 0.5], [10, 466, 0.5], [11, 600, 0.48], [12, 710, 0.44],
[13, 790, 0.39], [14, 850, 0.33], [15, 880, 0.28], [16, 905, 0.24], [17, 910, 0.2], [18, 910, 0.17],
[19, 910, 0.14], [20, 910, 0.12], [21, 910, 0.11], [22, 910, 0.09], [23, 910, 0.08], [24, 910, 0.07], [25, 910, 0.06]]
#turn windspeeds into ranges for lookup
ranges = []
min = 0.0
max = 1.999
lookup.each_with_index do |row, index|
min = row[0] unless index.eql?(0)
max = row[0] + 0.999 #speed
ranges << [(min..max), row[1].to_f]
end
#find windspeed value
output = 0.0
return output if windspeed > 25.0 #cut off point
ranges.each_with_index do |power_range, index|
if power_range[0].include? windspeed
output = power_range[1] # find power from lookup
decimals = windspeed - windspeed.floor # take account of decimal places
break if decimals.eql?(0.0) or output.eql?(910) or output.eql?(0.0) #no need to fill in the blanks
next_output = ranges[index+1][1]
output = ((next_output - output) * decimals) + output
break
end
end
return output
end
#For converting wunderground json to csv
def observations
f= File.read("weather.json")
j = JSON.parse f
data = []
j["history"]["observations"].each do |obsv|
#2013-11-01 00:00:00
yr = obsv['date']['year']
mm = obsv['date']['mon']
dd = obsv['date']['mday']
hr = obsv['date']['hour']
min = obsv['date']['min']
time = "#{yr}-#{mm}-#{dd} #{hr}:#{min}:00"
windspeed = obsv["wspdm"].to_f*1000/3600
data << [time, estimate(windspeed), windspeed]
end
CSV.open("nov-weather.csv", "wb") do |csv|
csv << ["timestamp", "estimate", "windspeed"]
for row in data
csv << row
end
end
end
#For converting SCADA format
def tiree_csv(filepath)
data = CSV.read(filepath)
#EACH ROW IS A DAY
days = []
for row in data
day = []
next if row.eql? data[0] # skip heading
tp_count = 0
data[0].each_with_index do |heading, i|
if heading[0..1].eql? "TP" #add value to day if TP column
timestamp = Time.strptime row[4], "%d-%b-%y"
timestamp = timestamp + (tp_count * 30.minutes)
tp_count += 1
day << [timestamp, row[i]]
end
end
days << day
end
CSV.open("tiree-redone.csv", "wb") do |csv|
csv << ["Timestamp", "Output"]
for day in days.flatten(1)
csv << day
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment