@@ -298,6 +298,92 @@ def _parse_pvgis_hourly_csv(src, map_variables=True):
298
298
return data , inputs , meta
299
299
300
300
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
+
301
387
def get_pvgis_tmy (lat , lon , outputformat = 'json' , usehorizon = True ,
302
388
userhorizon = None , startyear = None , endyear = None , url = URL ,
303
389
timeout = 30 ):
0 commit comments