|
| 1 | +""" |
| 2 | +This example script shows how to set up the utility rate module using a rate downloaded from the Utility Rate |
| 3 | +
|
| 4 | +This example requires an NREL developer key, which can be obtained from https://developer.nrel.gov/signup/ |
| 5 | +
|
| 6 | +Additional financial models, inputs, and outputs can be found at: |
| 7 | +* PV: https://nrel-pysam.readthedocs.io/en/master/modules/Utilityrate5.html |
| 8 | +
|
| 9 | +Most recently tested against PySAM 3.0.1 |
| 10 | +Requires: nrel-pysam, requests |
| 11 | +
|
| 12 | +@author: brtietz |
| 13 | +""" |
| 14 | +import json |
| 15 | +import os |
| 16 | +import requests |
| 17 | + |
| 18 | +import PySAM.Utilityrate5 as utility_rate |
| 19 | +import PySAM.ResourceTools |
| 20 | + |
| 21 | +# Get a key from https://developer.nrel.gov/signup/ |
| 22 | +key = "<YOUR_API_KEY>" |
| 23 | + |
| 24 | +# Download rate from URDB and save as file. If rate has already been downloaded, use file |
| 25 | +def get_urdb_rate_data(page, key): |
| 26 | + |
| 27 | + # Full API can be viewed at: https://openei.org/services/doc/rest/util_rates/?version=8 |
| 28 | + urdb_url = 'https://api.openei.org/utility_rates?format=json&detail=full&version=8' |
| 29 | + get_url = urdb_url + '&api_key={api_key}&getpage={page_id}'.format(api_key=key, page_id=page) |
| 30 | + |
| 31 | + filename = "urdb_rate_{}.json".format(page) |
| 32 | + print(filename) |
| 33 | + |
| 34 | + if not os.path.isfile(filename): |
| 35 | + print(get_url) |
| 36 | + resp = requests.get(get_url, verify=False) |
| 37 | + data = resp.text |
| 38 | + # Cache rate as file |
| 39 | + with open(filename, 'w') as f: |
| 40 | + f.write(json.dumps(data, sort_keys=True, indent=2, separators=(',', ': '))) |
| 41 | + else: |
| 42 | + with open(filename, 'r') as f: |
| 43 | + data = json.load(f) |
| 44 | + |
| 45 | + return data |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + path = os.getcwd() + os.path.sep |
| 49 | + page = "618940545457a35a1c4097ec" # https://apps.openei.org/USURDB/rate/view/618940545457a35a1c4097ec (DG-R Primary (Above 500kW)) |
| 50 | + urdb_response = get_urdb_rate_data(page, key) |
| 51 | + urdb_response_json = json.loads(urdb_response)["items"][0] |
| 52 | + rates = PySAM.ResourceTools.URDBv7_to_ElectricityRates(urdb_response_json) # To see status of version discrepancy, see https://github.com/NREL/pysam/issues/116. There's no difference between V7 and V8 for 99.9% of rates |
| 53 | + |
| 54 | + ur = utility_rate.new() |
| 55 | + for k, v in rates.items(): |
| 56 | + ur.value(k, v) |
| 57 | + |
| 58 | + # Set up other defaults |
| 59 | + analysis_period = 1 # Number of years to run the simulation |
| 60 | + ur.value("analysis_period", analysis_period) |
| 61 | + ur.value("system_use_lifetime_output", 0) # Set to 1 if load and gen have length 8760 * analysis_period |
| 62 | + ur.value("inflation_rate", 2.5) # Units of % |
| 63 | + ur.value("degradation", [0] * analysis_period) # AC energy loss per year due to degradation during analysis period (%) |
| 64 | + |
| 65 | + gen = [0] * 8760 # No renewable generation, run a technology compute module such as PVWatts8 to get this |
| 66 | + load = [100] * 8760 # Constant 100 kW load - this could also be an array imported from CSV |
| 67 | + |
| 68 | + ur.value("gen", gen) # Hourly kW |
| 69 | + ur.value("load", load) # Hourly kW |
| 70 | + |
| 71 | + ur.execute() # Run the utility rate module |
| 72 | + |
| 73 | + print("Total utility bill without system ($): ", ur.Outputs.elec_cost_without_system_year1) |
| 74 | + print("Average $/kW", ur.Outputs.elec_cost_without_system_year1 / sum(load)) |
| 75 | + |
| 76 | + |
0 commit comments