Class: Pvwatts

Inherits:
Object
  • Object
show all
Defined in:
lib/pvwatts.rb

Overview

Wrapper around the www.nrel.gov/rredc/pvwatts/ web service API. Calculates the Performance of a Grid-Connected PV System. Use of the Pvwatts web service is restricted to authorized users. For information on obtaining authorization, contact [email protected]

See Also:

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Pvwatts

Create an instance of the API wrapper.

Parameters:



23
24
25
# File 'lib/pvwatts.rb', line 23

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



17
18
19
# File 'lib/pvwatts.rb', line 17

def api_key
  @api_key
end

Instance Method Details

#get_stats(opts = {}) ⇒ Hash

Get information based on passed options.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :latitude (String, Float)

    Latitude coordinate of the location.

  • :longitude (String, Float)

    Longitude coordinate of the location.

  • :dc_rating (String, Float)

    kW rating values 0.5 to 10000.0

  • :tilt (String, Float)

    PV Array Lattitude tilt value 0 - 90

  • :azimuth (String, Integer)

    azimuth value 0 - 360 (180 for Northern Hemisphere).

  • :derate (String, Float)

    overall DC to AC derate factor values 0.10 - 0.96

  • :cost (String, Float)

    electricity cost per kWh (US ¢/kWh)

Returns:

  • (Hash)

    A hash with the yearly production with a key for each month and a ‘year’ key to represent the yearly value.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pvwatts.rb', line 79

def get_stats(opts={})
  Rails.logger.debug("pvwatts get_stats called") if Object.const_defined?(:Rails)
  keys = opts.keys 
  client = Savon::Client.new("http://pvwatts.nrel.gov/PVWATTS.asmx?WSDL")
  @latitude, @longitude = [opts[:latitude], opts[:longitude]]
  @dc_rating, @tilt, @azimuth, @derate, @cost, @array_type  = opts[:dc_rating], opts[:tilt], opts[:azimuth], opts[:derate], opts[:cost], opts[:array_type]
  unless @latitude &&  @longitude &&  @dc_rating && @tilt && @azimuth &&  @derate && @cost && @array_type 
    raise ArgumentError, "passed -> latitude: #{@latitude}, longitude: #{@longitude}, dc_rating: #{@dc_rating}, tilt: #{@tilt}, azimuth: #{@azimuth}, derate: #{@derate}, cost: #{@cost}, array_type: #{@array_type}"
  end
  req = prep_request(@latitude, @longitude, @dc_rating, @tilt, @azimuth, @derate, @array_type, @cost)
  
  response = client.get_pvwatts{|soap| soap.input = "GetPVWATTS"; soap.body = req }
  rdata = response.to_hash
  if rdata[:get_pvwatts_response] && rdata[:get_pvwatts_response][:get_pvwatts_result] && rdata[:get_pvwatts_response][:get_pvwatts_result][:pvwatt_sinfo]
    @production_data = []
    @pvwatt_info = rdata[:get_pvwatts_response][:get_pvwatts_result][:pvwatt_sinfo].compact
    @production_data = @pvwatt_info
  else
    raise 'Problem with the pvwatts response'
  end
  @production_data
end

#yearly_production(opts = {}) ⇒ Hash

Calculate the estimated yearly production based on passed options.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :latitude (String, Float)

    Latitude coordinate of the location.

  • :longitude (String, Float)

    Longitude coordinate of the location.

  • :dc_rating (String, Float)

    kW rating values 0.5 to 10000.0

  • :tilt (String, Float)

    PV Array Lattitude tilt value 0 - 90

  • :azimuth (String, Integer)

    azimuth value 0 - 360 (180 for Northern Hemisphere).

  • :derate (String, Float)

    overall DC to AC derate factor values 0.10 - 0.96

  • :cost (String, Float)

    electricity cost per kWh (US ¢/kWh)

Returns:

  • (Hash)

    A hash with the yearly production with a key for each month and a ‘year’ key to represent the yearly value.



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
# File 'lib/pvwatts.rb', line 40

def yearly_production(opts={})
  Rails.logger.debug("pvwatts yearly prod called") if Object.const_defined?(:Rails)
  keys = opts.keys 
  client = Savon::Client.new("http://pvwatts.nrel.gov/PVWATTS.asmx?WSDL")
  @latitude, @longitude = [opts[:latitude], opts[:longitude]]
  @dc_rating, @tilt, @azimuth, @derate, @cost, @array_type  = opts[:dc_rating], opts[:tilt], opts[:azimuth], opts[:derate], opts[:cost], opts[:array_type]
  unless @latitude &&  @longitude &&  @dc_rating && @tilt && @azimuth &&  @derate && @cost && @array_type 
    raise ArgumentError, "passed -> latitude: #{@latitude}, longitude: #{@longitude}, dc_rating: #{@dc_rating}, tilt: #{@tilt}, azimuth: #{@azimuth}, derate: #{@derate}, cost: #{@cost}, array_type: #{@array_type}"
  end
  req = prep_request(@latitude, @longitude, @dc_rating, @tilt, @azimuth, @derate, @array_type, @cost)
  
  response = client.get_pvwatts{|soap| soap.input = "GetPVWATTS"; soap.body = req }
  rdata = response.to_hash
  if rdata[:get_pvwatts_response] && rdata[:get_pvwatts_response][:get_pvwatts_result] && rdata[:get_pvwatts_response][:get_pvwatts_result][:pvwatt_sinfo]
    @production_data = {}
    @pvwatt_info = rdata[:get_pvwatts_response][:get_pvwatts_result][:pvwatt_sinfo].compact
    @pvwatt_info.each do |el| 
      if el.respond_to?(:has_key?) && el.has_key?(:month)
        @production_data[el[:month].downcase] = el[:a_cenergy].to_i
      end
    end
  else
    raise 'Problem with the pvwatts response'
  end
  @production_data
end