Skip to content

Commit 52d2522

Browse files
committed
more comments, space between pvgis outputformats
* raise ValueError if unknown output format * test if ValueError raised for bad output format
1 parent d8ffbca commit 52d2522

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

pvlib/iotools/pvgis.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def get_pvgis_tmy(lat, lon, outputformat='json', usehorizon=True,
7373
the error message in the response will be raised as an exception,
7474
otherwise raise whatever ``HTTP/1.1`` error occurred
7575
76+
See also
77+
--------
78+
read_pvgis_tmy
79+
7680
References
7781
----------
7882
@@ -200,27 +204,56 @@ def read_pvgis_tmy(filename, outputformat='csv'):
200204
meta : list or dict
201205
meta data, ``None`` for basic
202206
207+
Raises
208+
------
209+
ValueError
210+
if `outputformat` isn't in ``['csv', 'basic', 'epw', 'json']``
211+
203212
See also
204213
--------
205214
get_pvgis_tmy
206215
"""
216+
# parse the pvgis file based on the output format, either 'epw', 'json',
217+
# 'csv', or 'basic'
218+
219+
# EPW: use the EPW parser from the pvlib.iotools epw.py module
207220
if outputformat == 'epw':
208221
try:
209222
data, meta = parse_epw(filename)
210223
except AttributeError: # str/path has no .read() attribute
211224
data, meta = read_epw(filename)
212225
return data, None, None, meta
213-
pvgis_parser = globals()['_parse_pvgis_tmy_{:s}'.format(outputformat)]
226+
227+
# NOTE: json, csv, and basic output formats have parsers defined as private
228+
# functions in this module
229+
230+
# JSON: use Python built-in json module to convert file contents to a
231+
# Python dictionary, and pass the dictionary to the _parse_pvgis_tmy_json()
232+
# function from this module
214233
if outputformat == 'json':
215234
try:
216235
src = json.load(filename)
217236
except AttributeError: # str/path has no .read() attribute
218237
with open(str(filename), 'r') as fbuf:
219238
src = json.load(fbuf)
220-
return pvgis_parser(src)
221-
try:
222-
pvgis_data = pvgis_parser(filename)
223-
except AttributeError: # str/path has no .read() attribute
224-
with open(str(filename), 'rb') as fbuf:
225-
pvgis_data = pvgis_parser(fbuf)
226-
return pvgis_data
239+
return _parse_pvgis_tmy_json(src)
240+
241+
# CSV or basic: use the correct parser from this module
242+
# eg: _parse_pvgis_tmy_csv() or _parse_pvgist_tmy_basic()
243+
if outputformat in ['csv', 'basic']:
244+
# get the correct parser function for this output format from globals()
245+
pvgis_parser = globals()['_parse_pvgis_tmy_{:s}'.format(outputformat)]
246+
# NOTE: pvgis_parse() is a pvgis parser function from this module,
247+
# either _parse_pvgis_tmy_csv() or _parse_pvgist_tmy_basic()
248+
try:
249+
pvgis_data = pvgis_parser(filename)
250+
except AttributeError: # str/path has no .read() attribute
251+
with open(str(filename), 'rb') as fbuf:
252+
pvgis_data = pvgis_parser(fbuf)
253+
return pvgis_data
254+
255+
# raise exception if output format isn't in ['csv', 'basic', 'epw', 'json']
256+
err_msg = (
257+
"output format '{:s}' was unknown, must be either 'epw', 'json', 'csv'"
258+
", or 'basic'").format(outputformat)
259+
raise ValueError(err_msg)

pvlib/tests/iotools/test_pvgis.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,10 @@ def test_read_pvgis_tmy_basic(expected, meta_expected):
224224
with fn.open('rb') as fbuf:
225225
pvgis_data = read_pvgis_tmy(fbuf, outputformat='basic')
226226
_compare_pvgis_tmy_basic(expected, meta_expected, pvgis_data)
227+
228+
229+
def test_read_pvgis_tmy_exception():
230+
bad_outputformat = 'bad'
231+
err_msg = "output format '{:s}' was unknown".format(bad_outputformat)
232+
with pytest.raises(ValueError, match=err_msg):
233+
read_pvgis_tmy('filename', outputformat=bad_outputformat)

0 commit comments

Comments
 (0)