Skip to content

Commit 64bc450

Browse files
committed
Add read_pvgis_hourly
1 parent ee9c42f commit 64bc450

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

pvlib/iotools/pvgis.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,92 @@ def _parse_pvgis_hourly_csv(src, map_variables=True):
298298
return data, inputs, meta
299299

300300

301+
def read_pvgis_hourly(filename, map_variables=True, pvgis_format=None):
302+
"""
303+
Read a file downloaded from PVGIS.
304+
305+
Parameters
306+
----------
307+
filename : str, pathlib.Path, or file-like buffer
308+
Name, path, or buffer of file downloaded from PVGIS.
309+
pvgis_format : str, default None
310+
Format of PVGIS file or buffer. Equivalent to the ``outputformat``
311+
parameter in the PVGIS TMY API. If `filename` is a file and
312+
`pvgis_format` is ``None`` then the file extension will be used to
313+
determine the PVGIS format to parse. For PVGIS files from the API with
314+
``outputformat='basic'``, please set `pvgis_format` to ``'basic'``. If
315+
`filename` is a buffer, then `pvgis_format` is required and must be in
316+
``['csv', 'json', 'basic']``.
317+
318+
Returns
319+
-------
320+
data : pandas.DataFrame
321+
the weather data
322+
inputs : dict
323+
the inputs, ``None`` for basic
324+
meta : list or dict
325+
meta data, ``None`` for basic
326+
327+
Raises
328+
------
329+
ValueError
330+
if `pvgis_format` is ``None`` and the file extension is neither
331+
``.csv`` nor ``.json`` or if `pvgis_format` is provided as
332+
input but isn't in ``['csv', 'json', 'basic']``
333+
TypeError
334+
if `pvgis_format` is ``None`` and `filename` is a buffer
335+
336+
See also
337+
--------
338+
get_pvgis_hourly
339+
"""
340+
# get the PVGIS outputformat
341+
if pvgis_format is None:
342+
# get the file extension from suffix, but remove the dot and make sure
343+
# it's lower case to compare with csv, or json
344+
# NOTE: raises TypeError if filename is a buffer
345+
outputformat = Path(filename).suffix[1:].lower()
346+
else:
347+
outputformat = pvgis_format
348+
349+
# parse the pvgis file based on the output format, either 'json', 'csv',
350+
# or 'basic'
351+
352+
# NOTE: json, csv, and basic output formats have parsers defined as private
353+
# functions in this module
354+
355+
# JSON: use Python built-in json module to convert file contents to a
356+
# Python dictionary, and pass the dictionary to the
357+
# _parse_pvgis_hourly_json() function from this module
358+
if outputformat == 'json':
359+
try:
360+
src = json.load(filename)
361+
except AttributeError: # str/path has no .read() attribute
362+
with open(str(filename), 'r') as fbuf:
363+
src = json.load(fbuf)
364+
return _parse_pvgis_hourly_json(src)
365+
366+
# CSV or basic: use the correct parser from this module
367+
# eg: _parse_pvgis_tmy_csv() or _parse_pvgist_tmy_basic()
368+
if outputformat in ['csv', 'basic']:
369+
# get the correct parser function for this output format from globals()
370+
pvgis_parser = globals()['_parse_pvgis_hourly_{:s}'.format(outputformat)] # noqa
371+
# NOTE: pvgis_parse() is a pvgis parser function from this module,
372+
# either _parse_pvgis_hourly_csv() or _parse_pvgist_hourly_basic()
373+
try:
374+
pvgis_data = pvgis_parser(filename)
375+
except AttributeError: # str/path has no .read() attribute
376+
with open(str(filename), 'r') as fbuf:
377+
pvgis_data = pvgis_parser(fbuf)
378+
return pvgis_data
379+
380+
# raise exception if pvgis format isn't in ['csv', 'basic', 'json']
381+
err_msg = (
382+
"pvgis format '{:s}' was unknown, must be either 'json', 'csv', or"
383+
"'basic'").format(outputformat)
384+
raise ValueError(err_msg)
385+
386+
301387
def get_pvgis_tmy(lat, lon, outputformat='json', usehorizon=True,
302388
userhorizon=None, startyear=None, endyear=None, url=URL,
303389
timeout=30):

0 commit comments

Comments
 (0)