@@ -73,6 +73,10 @@ def get_pvgis_tmy(lat, lon, outputformat='json', usehorizon=True,
73
73
the error message in the response will be raised as an exception,
74
74
otherwise raise whatever ``HTTP/1.1`` error occurred
75
75
76
+ See also
77
+ --------
78
+ read_pvgis_tmy
79
+
76
80
References
77
81
----------
78
82
@@ -200,27 +204,56 @@ def read_pvgis_tmy(filename, outputformat='csv'):
200
204
meta : list or dict
201
205
meta data, ``None`` for basic
202
206
207
+ Raises
208
+ ------
209
+ ValueError
210
+ if `outputformat` isn't in ``['csv', 'basic', 'epw', 'json']``
211
+
203
212
See also
204
213
--------
205
214
get_pvgis_tmy
206
215
"""
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
207
220
if outputformat == 'epw' :
208
221
try :
209
222
data , meta = parse_epw (filename )
210
223
except AttributeError : # str/path has no .read() attribute
211
224
data , meta = read_epw (filename )
212
225
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
214
233
if outputformat == 'json' :
215
234
try :
216
235
src = json .load (filename )
217
236
except AttributeError : # str/path has no .read() attribute
218
237
with open (str (filename ), 'r' ) as fbuf :
219
238
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 )
0 commit comments