Skip to content

Commit a65ad83

Browse files
Birgit SchachlerBirgit Schachler
authored andcommitted
Adapt get_weather_data in example to new multiindex dataframe
1 parent b05ef0f commit a65ad83

File tree

1 file changed

+25
-77
lines changed

1 file changed

+25
-77
lines changed

example/basic_example.py

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@
2424
logging.getLogger().setLevel(logging.DEBUG)
2525

2626

27-
def get_weather_data(filename, datetime_column='time_index', **kwargs):
27+
def get_weather_data(filename='weather.csv', **kwargs):
2828
r"""
29-
Imports weather data from a file and specifies height of weather data.
29+
Imports weather data from a file.
3030
3131
The data include wind speed at two different heights in m/s, air
3232
temperature in two different heights in K, surface roughness length in m
33-
and air pressure in Pa.
33+
and air pressure in Pa. The file is located in the example folder of the
34+
windpowerlib. The height in m for which the data applies is specified in
35+
the second row.
3436
3537
Parameters
3638
----------
3739
filename : string
38-
Filename of the weather data file.
39-
datetime_column : string
40-
Name of the datetime column of the weather DataFrame.
41-
Default: 'time_index'.
40+
Filename of the weather data file. Default: 'weather.csv'.
4241
4342
Other Parameters
4443
----------------
@@ -48,78 +47,27 @@ def get_weather_data(filename, datetime_column='time_index', **kwargs):
4847
4948
Returns
5049
-------
51-
Tuple (pd.DataFrame, dictionary)
52-
`weather` DataFrame contains weather data time series.
53-
`data_height` dictionary contains height for which corresponding
54-
weather data applies.
50+
weather_df : pandas.DataFrame
51+
DataFrame with time series for wind speed `wind_speed` in m/s,
52+
temperature `temperature` in K, roughness length `roughness_length`
53+
in m, and pressure `pressure` in Pa.
54+
The columns of the DataFrame are a MultiIndex where the first level
55+
contains the variable name (e.g. wind_speed) and the second level
56+
contains the height at which it applies (e.g. 10, if it was
57+
measured at a height of 10 m).
5558
5659
"""
57-
def read_weather_data(filename, datetime_column='time_index',
58-
**kwargs):
59-
r"""
60-
Fetches weather data from a file.
61-
62-
The file is located in the example folder of the windpowerlib.
63-
64-
Parameters
65-
----------
66-
filename : string
67-
Filename of the weather data file.
68-
datetime_column : string
69-
Name of the datetime column of the weather DataFrame.
70-
71-
Other Parameters
72-
----------------
73-
datapath : string, optional
74-
Path where the weather data file is stored.
75-
Default: 'windpowerlib/example'.
76-
77-
Returns
78-
-------
79-
pandas.DataFrame
80-
Contains weather data time series.
81-
82-
"""
83-
if 'datapath' not in kwargs:
84-
kwargs['datapath'] = os.path.join(os.path.split(
85-
os.path.dirname(__file__))[0], 'example')
86-
87-
file = os.path.join(kwargs['datapath'], filename)
88-
df = pd.read_csv(file)
89-
return df.set_index(pd.to_datetime(df[datetime_column])).tz_localize(
90-
'UTC').tz_convert('Europe/Berlin').drop(datetime_column, 1)
91-
92-
# read weather data from csv
93-
weather_tmp = read_weather_data(filename=filename,
94-
datetime_column=datetime_column, **kwargs)
95-
96-
# dictionary specifying the height for which the weather data applies
97-
# data in m
98-
data_height = {
99-
'pressure': 0,
100-
'temp_air': 2,
101-
'v_wind': 10,
102-
'temp_air_2': 10,
103-
'v_wind_2': 80}
104-
105-
# restructure weather DataFrame
106-
107-
data = np.transpose(np.asarray([weather_tmp['v_wind'],
108-
weather_tmp['v_wind_2'],
109-
weather_tmp['temp_air'],
110-
weather_tmp['temp_air_2'],
111-
weather_tmp['pressure'],
112-
weather_tmp['z0']]))
113-
weather = pd.DataFrame(
114-
data,
115-
index=weather_tmp.index,
116-
columns=[np.array(['v_wind', 'v_wind', 'temp_air', 'temp_air',
117-
'pressure', 'z0']),
118-
np.array([data_height['v_wind'], data_height['v_wind_2'],
119-
data_height['temp_air'], data_height['temp_air_2'],
120-
data_height['pressure'], 0])])
121-
122-
return weather
60+
61+
if 'datapath' not in kwargs:
62+
kwargs['datapath'] = os.path.join(os.path.split(
63+
os.path.dirname(__file__))[0], 'example')
64+
file = os.path.join(kwargs['datapath'], filename)
65+
# read csv file
66+
weather_df = pd.read_csv(file, index_col=0, header=[0, 1])
67+
# change type of index to datetime and set time zone
68+
weather_df.index = pd.to_datetime(weather_df.index).tz_localize(
69+
'UTC').tz_convert('Europe/Berlin')
70+
return weather_df
12371

12472

12573
def initialise_wind_turbines():

0 commit comments

Comments
 (0)