forked from pvlib/pvlib-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeteonorm.py
More file actions
349 lines (296 loc) · 12.5 KB
/
Copy pathmeteonorm.py
File metadata and controls
349 lines (296 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Functions for retrieving data from Meteonorm."""
import pandas as pd
import requests
from urllib.parse import urljoin
URL = 'https://api.meteonorm.com/v1/'
VARIABLE_MAP = {
'global_horizontal_irradiance': 'ghi',
'diffuse_horizontal_irradiance': 'dhi',
'direct_normal_irradiance': 'dni',
'direct_horizontal_irradiance': 'bhi',
'global_clear_sky_irradiance': 'ghi_clear',
'diffuse_tilted_irradiance': 'poa_diffuse',
'direct_tilted_irradiance': 'poa_direct',
'global_tilted_irradiance': 'poa',
'temperature': 'temp_air',
'dew_point_temperature': 'temp_dew',
}
TIME_STEP_MAP = {
'1h': '1_hour',
'h': '1_hour',
'15min': '15_minutes',
'1min': '1_minute',
'min': '1_minute',
}
def get_meteonorm(latitude, longitude, start, end, api_key, endpoint,
parameters='all', *, surface_tilt=0, surface_azimuth=180,
time_step='15min', horizon='auto', interval_index=False,
map_variables=True, url=URL):
"""
Retrieve irradiance and weather data from Meteonorm.
The Meteonorm data options are described in [1]_ and the API is described
in [2]_. A detailed list of API options can be found in [3]_.
This function supports retrieval of historical and forecast data, but not
TMY.
Parameters
----------
latitude : float
In decimal degrees, north is positive (ISO 19115).
longitude: float
In decimal degrees, east is positive (ISO 19115).
start : datetime like
First timestamp of the requested period. If a timezone is not
specified, UTC is assumed. Relative datetime strings are supported.
end : datetime like
Last timestamp of the requested period. If a timezone is not
specified, UTC is assumed. Relative datetime strings are supported.
api_key : str
Meteonorm API key.
endpoint : str
API endpoint, see [3]_. Must be one of:
* ``'observation/training'`` - historical data with a 7-day delay
* ``'observation/realtime'`` - near-real time (past 7-days)
* ``'forecast/basic'`` - forecast with hourly resolution
* ``'forecast/precision'`` - forecast with 15-min resolution
parameters : list or 'all', default : 'all'
List of parameters to request or `'all'` to get all parameters.
surface_tilt : float, default : 0
Tilt angle from horizontal plane.
surface_azimuth : float, default : 180
Orientation (azimuth angle) of the (fixed) plane. Clockwise from north
(north=0, east=90, south=180, west=270).
time_step : {'1min', '15min', '1h'}, default : '15min'
Frequency of the time series. The parameter is ignored when requesting
forcasting data.
horizon : str or list, default : 'auto'
Specification of the horizon line. Can be either a 'flat', 'auto', or
a list of 360 integer horizon elevation angles.
interval_index : bool, default : False
Index is pd.DatetimeIndex when False, and pd.IntervalIndex when True.
This is an experimental feature which may be removed without warning.
map_variables : bool, default : True
When true, renames columns of the Dataframe to pvlib variable names
where applicable. See variable :const:`VARIABLE_MAP`.
url : str, optional
Base URL of the Meteonorm API. The ``endpoint`` parameter is
appended to the url. The default is
:const:`pvlib.iotools.meteonorm.URL`.
Raises
------
requests.HTTPError
Raises an error when an incorrect request is made.
Returns
-------
data : pd.DataFrame
Time series data. The index corresponds to the start (left) of the
interval unless ``interval_index`` is set to False.
meta : dict
Metadata.
Examples
--------
>>> # Retrieve historical time series data
>>> df, meta = get_meteonorm( # doctest: +SKIP
... latitude=50, longitude=10, # doctest: +SKIP
... start='2023-01-01', end='2025-01-01', # doctest: +SKIP
... api_key='redacted', # doctest: +SKIP
... endpoint='observation/training') # doctest: +SKIP
See Also
--------
pvlib.iotools.get_meteonorm_tmy
References
----------
.. [1] `Meteonorm
<https://meteonorm.com/>`_
.. [2] `Meteonorm API
<https://docs.meteonorm.com/docs/getting-started>`_
.. [3] `Meteonorm API reference
<https://docs.meteonorm.com/api>`_
"""
start = pd.Timestamp(start)
end = pd.Timestamp(end)
start = start.tz_localize('UTC') if start.tzinfo is None else start
end = end.tz_localize('UTC') if end.tzinfo is None else end
params = {
'lat': latitude,
'lon': longitude,
'start': start.strftime('%Y-%m-%dT%H:%M:%SZ'),
'end': end.strftime('%Y-%m-%dT%H:%M:%SZ'),
'parameters': parameters,
'surface_tilt': surface_tilt,
'surface_azimuth': surface_azimuth,
'horizon': horizon,
}
# Allow specifying single parameters as string
if isinstance(parameters, str):
parameter_list = \
list(VARIABLE_MAP.keys()) + list(VARIABLE_MAP.values())
if parameters in parameter_list:
parameters = [parameters]
# convert list to string with values separated by commas
if not isinstance(parameters, (str, type(None))):
# allow the use of pvlib parameter names
parameter_dict = {v: k for k, v in VARIABLE_MAP.items()}
parameters = [parameter_dict.get(p, p) for p in parameters]
params['parameters'] = ','.join(parameters)
if not isinstance(horizon, str):
params['horizon'] = ','.join(map(str, horizon))
if 'forecast' not in endpoint.lower():
params['frequency'] = TIME_STEP_MAP.get(time_step, time_step)
headers = {"Authorization": f"Bearer {api_key}"}
response = requests.get(
urljoin(url, endpoint.lstrip('/')), headers=headers, params=params)
if not response.ok:
# response.raise_for_status() does not give a useful error message
raise requests.HTTPError(response.json())
data, meta = _parse_meteonorm(response, interval_index, map_variables)
return data, meta
TMY_ENDPOINT = 'climate/tmy'
def get_meteonorm_tmy(latitude, longitude, api_key,
parameters='all', *, surface_tilt=0,
surface_azimuth=180, time_step='15min', horizon='auto',
terrain='open', albedo=0.2, turbidity='auto',
random_seed=None, clear_sky_radiation_model='esra',
data_version='latest', future_scenario=None,
future_year=None, interval_index=False,
map_variables=True, url=URL):
"""
Retrieve TMY irradiance and weather data from Meteonorm.
The Meteonorm data options are described in [1]_ and the API is described
in [2]_. A detailed list of API options can be found in [3]_.
Parameters
----------
latitude : float
In decimal degrees, north is positive (ISO 19115).
longitude : float
In decimal degrees, east is positive (ISO 19115).
api_key : str
Meteonorm API key.
parameters : list or 'all', default : 'all'
List of parameters to request or `'all'` to get all parameters.
surface_tilt : float, default : 0
Tilt angle from horizontal plane.
surface_azimuth : float, default : 180
Orientation (azimuth angle) of the (fixed) plane. Clockwise from north
(north=0, east=90, south=180, west=270).
time_step : {'1min', '1h'}, default : '1h'
Frequency of the time series.
horizon : str, optional
Specification of the horizon line. Can be either 'flat' or 'auto', or
specified as a list of 360 integer horizon elevation angles.
'auto'.
terrain : str, default : 'open'
Local terrain situation. Must be one of: ['open', 'depression',
'cold_air_lake', 'sea_lake', 'city', 'slope_south',
'slope_west_east'].
albedo : float, default : 0.2
Ground albedo. Albedo changes due to snow fall are modelled.
turbidity : list or 'auto', optional
List of 12 monthly mean atmospheric Linke turbidity values. The default
is 'auto'.
random_seed : int, optional
Random seed to be used for stochastic processes. Two identical requests
with the same random seed will yield identical results.
clear_sky_radiation_model : str, default : 'esra'
Which clearsky model to use. Must be either `'esra'` or `'solis'`.
data_version : str, default : 'latest'
Version of Meteonorm climatological data to be used.
future_scenario : str, optional
Future climate scenario.
future_year : int, optional
Central year for a 20-year reference period in the future.
interval_index : bool, default : False
Index is pd.DatetimeIndex when False, and pd.IntervalIndex when True.
This is an experimental feature which may be removed without warning.
map_variables : bool, default : True
When true, renames columns of the Dataframe to pvlib variable names
where applicable. See variable :const:`VARIABLE_MAP`.
url : str, optional.
Base URL of the Meteonorm API. `'climate/tmy'` is
appended to the URL. The default is:
:const:`pvlib.iotools.meteonorm.URL`.
Raises
------
requests.HTTPError
Raises an error when an incorrect request is made.
Returns
-------
data : pd.DataFrame
Time series data. The index corresponds to the start (left) of the
interval unless ``interval_index`` is set to False.
meta : dict
Metadata.
See Also
--------
pvlib.iotools.get_meteonorm
References
----------
.. [1] `Meteonorm
<https://meteonorm.com/>`_
.. [2] `Meteonorm API
<https://docs.meteonorm.com/docs/getting-started>`_
.. [3] `Meteonorm API reference
<https://docs.meteonorm.com/api>`_
"""
params = {
'lat': latitude,
'lon': longitude,
'surface_tilt': surface_tilt,
'surface_azimuth': surface_azimuth,
'frequency': time_step,
'parameters': parameters,
'horizon': horizon,
'situation': terrain,
'turbidity': turbidity,
'clear_sky_radiation_model': clear_sky_radiation_model,
'data_version': data_version,
'random_seed': random_seed,
'future_scenario': future_scenario,
'future_year': future_year,
}
# Allow specifying single parameters as string
if isinstance(parameters, str):
parameter_list = \
list(VARIABLE_MAP.keys()) + list(VARIABLE_MAP.values())
if parameters in parameter_list:
parameters = [parameters]
# convert list to string with values separated by commas
if not isinstance(parameters, (str, type(None))):
# allow the use of pvlib parameter names
parameter_dict = {v: k for k, v in VARIABLE_MAP.items()}
parameters = [parameter_dict.get(p, p) for p in parameters]
params['parameters'] = ','.join(parameters)
if not isinstance(horizon, str):
params['horizon'] = ','.join(map(str, horizon))
if not isinstance(turbidity, str):
params['turbidity'] = ','.join(map(str, turbidity))
params['frequency'] = TIME_STEP_MAP.get(time_step, time_step)
headers = {"Authorization": f"Bearer {api_key}"}
response = requests.get(
urljoin(url, TMY_ENDPOINT.lstrip('/')), headers=headers, params=params)
if not response.ok:
# response.raise_for_status() does not give a useful error message
raise requests.HTTPError(response.json())
data, meta = _parse_meteonorm(response, interval_index, map_variables)
return data, meta
def _parse_meteonorm(response, interval_index, map_variables):
data_json = response.json()['values']
# identify empty columns
empty_columns = [k for k, v in data_json.items() if v is None]
# remove empty columns
_ = [data_json.pop(k) for k in empty_columns]
data = pd.DataFrame(data_json)
# xxx: experimental feature - see parameter description
if interval_index:
data.index = pd.IntervalIndex.from_arrays(
left=pd.to_datetime(response.json()['start_times']),
right=pd.to_datetime(response.json()['end_times']),
closed='left',
)
else:
data.index = pd.to_datetime(response.json()['start_times'])
meta = response.json()['meta']
if map_variables:
data = data.rename(columns=VARIABLE_MAP)
meta['latitude'] = meta.pop('lat')
meta['longitude'] = meta.pop('lon')
return data, meta