From 33a173003f5d1df56b3a7afb2285185015513af1 Mon Sep 17 00:00:00 2001 From: uvchik Date: Thu, 13 Feb 2020 17:48:10 +0100 Subject: [PATCH 1/3] Download weather file if not present --- example/modelchain_example.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/example/modelchain_example.py b/example/modelchain_example.py index acf85b8e..916ce24a 100644 --- a/example/modelchain_example.py +++ b/example/modelchain_example.py @@ -15,6 +15,7 @@ """ import os import pandas as pd +import requests try: from matplotlib import pyplot as plt @@ -65,11 +66,16 @@ def get_weather_data(filename='weather.csv', **kwargs): """ if "datapath" not in kwargs: - kwargs["datapath"] = os.path.join( - os.path.split(os.path.dirname(__file__))[0], "example" - ) + kwargs["datapath"] = os.path.dirname(__file__) + file = os.path.join(kwargs["datapath"], filename) + if not os.path.isfile(file): + logging.debug("Download weather data for example.") + req = requests.get("https://osf.io/59bqn/download") + with open(file, "wb") as fout: + fout.write(req.content) + # read csv file weather_df = pd.read_csv( file, From ca2f4277e74d8be92b66fdab534db934e676003a Mon Sep 17 00:00:00 2001 From: uvchik Date: Thu, 13 Feb 2020 17:48:40 +0100 Subject: [PATCH 2/3] Add comments to make example cleaner --- example/modelchain_example.py | 69 +++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/example/modelchain_example.py b/example/modelchain_example.py index 916ce24a..7a73a222 100644 --- a/example/modelchain_example.py +++ b/example/modelchain_example.py @@ -10,26 +10,27 @@ you need to specify your wind turbine, and in the last step call the windpowerlib functions to calculate the feed-in time series. +Install the windpowerlib and optionally matplotlib to see the plots: + + pip install windpowerlib + pip install matplotlib + +Go down to the "run_example()" function to start the example. + SPDX-FileCopyrightText: 2019 oemof developer group SPDX-License-Identifier: MIT """ import os import pandas as pd import requests +import logging +from windpowerlib import ModelChain, WindTurbine try: from matplotlib import pyplot as plt except ImportError: plt = None -from windpowerlib import ModelChain -from windpowerlib import WindTurbine - -# You can use the logging package to get logging messages from the windpowerlib -# Change the logging level if you want more or less messages -import logging -logging.getLogger().setLevel(logging.DEBUG) - def get_weather_data(filename='weather.csv', **kwargs): r""" @@ -114,18 +115,18 @@ def initialize_wind_turbines(): :class:`~.wind_turbine.WindTurbine`) """ + # ************************************************************************ + # **** Data is provided in the oedb turbine library ********************** - # specification of wind turbine where data is provided in the oedb - # turbine library enercon_e126 = { "turbine_type": "E-126/4200", # turbine type as in register "hub_height": 135, # in m } - # initialize WindTurbine object e126 = WindTurbine(**enercon_e126) - # specification of own wind turbine (Note: power values and nominal power - # have to be in Watt) + # ************************************************************************ + # **** Specification of wind turbine with your own data ****************** + # **** NOTE: power values and nominal power have to be in Watt my_turbine = { "nominal_power": 3e6, # in W "hub_height": 105, # in m @@ -139,11 +140,10 @@ def initialize_wind_turbines(): } ), # in m/s } - # initialize WindTurbine object my_turbine = WindTurbine(**my_turbine) - # specification of wind turbine where power coefficient curve and nominal - # power is provided in an own csv file + # ************************************************************************ + # **** Specification of wind turbine with data in own file *************** csv_path = os.path.join(os.path.dirname(__file__), "data") dummy_turbine = { "turbine_type": "DUMMY 1", @@ -151,7 +151,6 @@ def initialize_wind_turbines(): "rotor_diameter": 70, # in m "path": csv_path, } - # initialize WindTurbine object dummy_turbine = WindTurbine(**dummy_turbine) return my_turbine, e126, dummy_turbine @@ -183,15 +182,9 @@ def calculate_power_output(weather, my_turbine, e126, dummy_turbine): """ - # power output calculation for my_turbine - # initialize ModelChain with default parameters and use run_model method - # to calculate power output - mc_my_turbine = ModelChain(my_turbine).run_model(weather) - # write power output time series to WindTurbine object - my_turbine.power_output = mc_my_turbine.power_output - - # power output calculation for e126 - # own specifications for ModelChain setup + # ************************************************************************ + # **** Data is provided in the oedb turbine library ********************** + # **** ModelChain with non-default specifications modelchain_data = { "wind_speed_model": "logarithmic", # 'logarithmic' (default), # 'hellman' or @@ -212,8 +205,16 @@ def calculate_power_output(weather, my_turbine, e126, dummy_turbine): # write power output time series to WindTurbine object e126.power_output = mc_e126.power_output - # power output calculation for example_turbine - # own specification for 'power_output_model' + # ************************************************************************ + # **** Specification of wind turbine with your own data ****************** + # **** ModelChain with default parameter + mc_my_turbine = ModelChain(my_turbine).run_model(weather) + # write power output time series to WindTurbine object + my_turbine.power_output = mc_my_turbine.power_output + + # ************************************************************************ + # **** Specification of wind turbine with data in own file *************** + # **** Using "power_coefficient_curve" as "power_output_model". mc_example_turbine = ModelChain( dummy_turbine, power_output_model="power_coefficient_curve" ).run_model(weather) @@ -284,10 +285,16 @@ def run_example(): Runs the basic example. """ + # You can use the logging package to get logging messages from the + # windpowerlib. Change the logging level if you want more or less messages: + # logging.DEBUG -> many messages + # logging.INFO -> few messages + logging.getLogger().setLevel(logging.DEBUG) + weather = get_weather_data("weather.csv") - my_turbine, e126, dummy_turbine = initialize_wind_turbines() - calculate_power_output(weather, my_turbine, e126, dummy_turbine) - plot_or_print(my_turbine, e126, dummy_turbine) + my_turbine, e126 = initialize_wind_turbines() + calculate_power_output(weather, my_turbine, e126) + plot_or_print(my_turbine, e126) if __name__ == "__main__": From 777cf9798f1eaa59d5d050c8812a70a0d8dc6c24 Mon Sep 17 00:00:00 2001 From: uvchik Date: Thu, 13 Feb 2020 20:20:00 +0100 Subject: [PATCH 3/3] Move data from data dir to parent dir --- example/modelchain_example.ipynb | 2 +- example/modelchain_example.py | 3 +-- example/{data => }/power_coefficient_curves.csv | 0 example/{data => }/power_curves.csv | 0 example/{data => }/turbine_data.csv | 0 tests/data/power_coefficient_curves.csv | 3 +++ tests/data/power_curves.csv | 3 +++ tests/data/turbine_data.csv | 5 +++++ tests/test_wind_turbine.py | 2 +- windpowerlib/wind_turbine.py | 4 ++-- 10 files changed, 16 insertions(+), 6 deletions(-) rename example/{data => }/power_coefficient_curves.csv (100%) rename example/{data => }/power_curves.csv (100%) rename example/{data => }/turbine_data.csv (100%) create mode 100755 tests/data/power_coefficient_curves.csv create mode 100755 tests/data/power_curves.csv create mode 100644 tests/data/turbine_data.csv diff --git a/example/modelchain_example.ipynb b/example/modelchain_example.ipynb index f4b1f748..1b4dfa9e 100644 --- a/example/modelchain_example.ipynb +++ b/example/modelchain_example.ipynb @@ -288,7 +288,7 @@ "source": [ "# specification of wind turbine where power coefficient curve and nominal\n", "# power is provided in an own csv file\n", - "csv_path = 'data'\n", + "csv_path = ''\n", "dummy_turbine = {\n", " 'turbine_type': 'DUMMY 1', # turbine type as in file\n", " 'hub_height': 100, # in m\n", diff --git a/example/modelchain_example.py b/example/modelchain_example.py index 7a73a222..b1d68c7b 100644 --- a/example/modelchain_example.py +++ b/example/modelchain_example.py @@ -144,12 +144,11 @@ def initialize_wind_turbines(): # ************************************************************************ # **** Specification of wind turbine with data in own file *************** - csv_path = os.path.join(os.path.dirname(__file__), "data") dummy_turbine = { "turbine_type": "DUMMY 1", "hub_height": 100, # in m "rotor_diameter": 70, # in m - "path": csv_path, + "path": os.path.dirname(__file__), } dummy_turbine = WindTurbine(**dummy_turbine) diff --git a/example/data/power_coefficient_curves.csv b/example/power_coefficient_curves.csv similarity index 100% rename from example/data/power_coefficient_curves.csv rename to example/power_coefficient_curves.csv diff --git a/example/data/power_curves.csv b/example/power_curves.csv similarity index 100% rename from example/data/power_curves.csv rename to example/power_curves.csv diff --git a/example/data/turbine_data.csv b/example/turbine_data.csv similarity index 100% rename from example/data/turbine_data.csv rename to example/turbine_data.csv diff --git a/tests/data/power_coefficient_curves.csv b/tests/data/power_coefficient_curves.csv new file mode 100755 index 00000000..a3c70cc1 --- /dev/null +++ b/tests/data/power_coefficient_curves.csv @@ -0,0 +1,3 @@ +turbine_type,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16,16.5,17,17.5,18,18.5,19,19.5,20,20.5,21,21.5,22,22.5,23,23.5,24,24.5,25,25.5,26 +DUMMY 1,0,,0,,0,,0,0,0.13,,0.38,,0.46,,0.48,,0.47,,0.44,,0.4,,0.36,,0.31,,0.26,,0.23,,0.2,,0.18,,0.14,,0.11,,0.1,,0.09,,0.07,,0.05,,0.04,,0.04,,0.03,,0 +DUMMY 2,0,,0,,0,,0.16,0.29,0.35,0.38,0.4,0.41,0.42,0.43,0.43,0.44,0.44,0.44,0.44,0.43,0.42,0.39,0.36,0.32,0.29,0.26,0.23,0.2,0.18,0.16,0.15,0.14,0.12,0.11,0.1,0.09,0.09,0.08,0.07,0.07,0.06,0.06,0.05,0.05,0.05,0.04,0.04,0.04,0.04,0.03,0.03,0,0 diff --git a/tests/data/power_curves.csv b/tests/data/power_curves.csv new file mode 100755 index 00000000..85890d50 --- /dev/null +++ b/tests/data/power_curves.csv @@ -0,0 +1,3 @@ +turbine_type,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16,16.5,17,17.5,18,18.5,19,19.5,20,20.5,21,21.5,22,22.5,23,23.5,24,24.5,25,25.5,26 +DUMMY 3,0,0,0,0,0,0,0,18000,34000,70000,10000,150000,190000,260000,330000,420000,510000,620000,740000,880000,1020000,1180000,1330000,1420000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,1500000,0, +DUMMY 4,0,,0,,0,,0,,4000,,22000,,46000,,76000,,111000,,147000,,184000,,219000,,249000,,274000,,290000,,297000,,302000,,307000,,307000,,305000,,295000,,280000,,260000,,240000,,230000,,225000,0, diff --git a/tests/data/turbine_data.csv b/tests/data/turbine_data.csv new file mode 100644 index 00000000..0e9b9f9f --- /dev/null +++ b/tests/data/turbine_data.csv @@ -0,0 +1,5 @@ +turbine_type,nominal_power +DUMMY 1,4200000 +DUMMY 2,4200000 +DUMMY 3,1500000 +DUMMY 4,225000 diff --git a/tests/test_wind_turbine.py b/tests/test_wind_turbine.py index 4140911f..fb23f084 100644 --- a/tests/test_wind_turbine.py +++ b/tests/test_wind_turbine.py @@ -22,7 +22,7 @@ class TestWindTurbine: @classmethod def setup_class(cls): """Setup default values""" - cls.source = os.path.join(os.path.dirname(__file__), "../example/data") + cls.source = os.path.join(os.path.dirname(__file__), "data") def test_warning(self, recwarn): test_turbine_data = { diff --git a/windpowerlib/wind_turbine.py b/windpowerlib/wind_turbine.py index 6ab1c52a..0b4fb2fb 100644 --- a/windpowerlib/wind_turbine.py +++ b/windpowerlib/wind_turbine.py @@ -101,7 +101,7 @@ class WindTurbine(object): >>> print(e126.nominal_power) 4200000.0 >>> # Example with own path - >>> path=os.path.join(os.path.dirname(__file__), '../example/data') + >>> path=os.path.join(os.path.dirname(__file__), '../tests/data') >>> example_turbine={ ... 'hub_height': 100, ... 'rotor_diameter': 70, @@ -378,7 +378,7 @@ def get_turbine_data_from_file(turbine_type, path): -------- >>> from windpowerlib import wind_turbine >>> import os - >>> path=os.path.join(os.path.dirname(__file__), '../example/data', + >>> path=os.path.join(os.path.dirname(__file__), '../tests/data', ... 'power_curves.csv') >>> d3=get_turbine_data_from_file('DUMMY 3', path) >>> print(d3['value'][7])