@@ -261,7 +261,7 @@ def _parse_pvgis_hourly_json(src, map_variables):
261
261
262
262
def _parse_pvgis_hourly_csv (src , map_variables ):
263
263
# The first 4 rows are latitude, longitude, elevation, radiation database
264
- metadata = {'inputs' : {}}
264
+ metadata = {'inputs' : {}, 'descriptions' : {} }
265
265
# 'location' metadata
266
266
# 'Latitude (decimal degrees): 45.000\r\n'
267
267
metadata ['inputs' ]['latitude' ] = float (src .readline ().split (':' )[1 ])
@@ -440,6 +440,13 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
440
440
441
441
For more information see the PVGIS [1]_ TMY tool documentation [2]_.
442
442
443
+ .. versionchanged:: 0.13.0
444
+ The function now returns two items ``(data,meta)``. Previous
445
+ versions of this function returned three elements
446
+ ``(data,months_selected,inputs,meta)``. The ``inputs`` dictionary and
447
+ ``months_selected`` are now included in ``meta``, which has changed
448
+ structure to accommodate it.
449
+
443
450
Parameters
444
451
----------
445
452
latitude : float
@@ -478,10 +485,6 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
478
485
-------
479
486
data : pandas.DataFrame
480
487
the weather data
481
- months_selected : list
482
- TMY year for each month, ``None`` for EPW
483
- inputs : dict
484
- the inputs, ``None`` for EPW
485
488
metadata : list or dict
486
489
file metadata
487
490
@@ -527,17 +530,16 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
527
530
else :
528
531
raise requests .HTTPError (err_msg ['message' ])
529
532
# initialize data to None in case API fails to respond to bad outputformat
530
- data = None , None , None , None
533
+ data = None , None
531
534
if outputformat == 'json' :
532
535
src = res .json ()
533
- data , months_selected , inputs , meta = _parse_pvgis_tmy_json (src )
536
+ data , meta = _parse_pvgis_tmy_json (src )
534
537
elif outputformat == 'csv' :
535
538
with io .BytesIO (res .content ) as src :
536
- data , months_selected , inputs , meta = _parse_pvgis_tmy_csv (src )
539
+ data , meta = _parse_pvgis_tmy_csv (src )
537
540
elif outputformat == 'epw' :
538
541
with io .StringIO (res .content .decode ('utf-8' )) as src :
539
542
data , meta = read_epw (src )
540
- months_selected , inputs = None , None
541
543
elif outputformat == 'basic' :
542
544
err_msg = ("outputformat='basic' is no longer supported by pvlib, "
543
545
"please use outputformat='csv' instead." )
@@ -551,34 +553,37 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
551
553
coerce_year = coerce_year or 1990
552
554
data = _coerce_and_roll_tmy (data , roll_utc_offset , coerce_year )
553
555
554
- return data , months_selected , inputs , meta
556
+ return data , meta
555
557
556
558
557
559
def _parse_pvgis_tmy_json (src ):
558
- inputs = src ['inputs' ]
559
- meta = src ['meta' ]
560
- months_selected = src ['outputs' ]['months_selected' ]
560
+ meta = src ['meta' ].copy ()
561
+ # Override the "inputs" in metadata
562
+ meta ['inputs' ] = src ['inputs' ]
563
+ # Re-add the inputs in metadata one-layer down
564
+ meta ['inputs' ]['descriptions' ] = src ['meta' ]['inputs' ]
565
+ meta ['months_selected' ] = src ['outputs' ]['months_selected' ]
561
566
data = pd .DataFrame (src ['outputs' ]['tmy_hourly' ])
562
567
data .index = pd .to_datetime (
563
568
data ['time(UTC)' ], format = '%Y%m%d:%H%M' , utc = True )
564
569
data = data .drop ('time(UTC)' , axis = 1 )
565
- return data , months_selected , inputs , meta
570
+ return data , meta
566
571
567
572
568
573
def _parse_pvgis_tmy_csv (src ):
569
574
# the first 3 rows are latitude, longitude, elevation
570
- inputs = {}
575
+ meta = {'inputs' : {}, 'descriptions' : {} }
571
576
# 'Latitude (decimal degrees): 45.000\r\n'
572
- inputs ['latitude' ] = float (src .readline ().split (b':' )[1 ])
577
+ meta [ ' inputs' ] ['latitude' ] = float (src .readline ().split (b':' )[1 ])
573
578
# 'Longitude (decimal degrees): 8.000\r\n'
574
- inputs ['longitude' ] = float (src .readline ().split (b':' )[1 ])
579
+ meta [ ' inputs' ] ['longitude' ] = float (src .readline ().split (b':' )[1 ])
575
580
# Elevation (m): 1389.0\r\n
576
- inputs ['elevation' ] = float (src .readline ().split (b':' )[1 ])
581
+ meta [ ' inputs' ] ['elevation' ] = float (src .readline ().split (b':' )[1 ])
577
582
578
583
# TMY has an extra line here: Irradiance Time Offset (h): 0.1761\r\n
579
584
line = src .readline ()
580
585
if line .startswith (b'Irradiance Time Offset' ):
581
- inputs ['irradiance time offset' ] = float (line .split (b':' )[1 ])
586
+ meta [ ' inputs' ] ['irradiance time offset' ] = float (line .split (b':' )[1 ])
582
587
src .readline () # skip over the "month,year\r\n"
583
588
else :
584
589
# `line` is already the "month,year\r\n" line, so nothing to do
@@ -589,6 +594,7 @@ def _parse_pvgis_tmy_csv(src):
589
594
for month in range (12 ):
590
595
months_selected .append (
591
596
{'month' : month + 1 , 'year' : int (src .readline ().split (b',' )[1 ])})
597
+ meta ['months_selected' ] = months_selected
592
598
# then there's the TMY (typical meteorological year) data
593
599
# first there's a header row:
594
600
# time(UTC),T2m,RH,G(h),Gb(n),Gd(h),IR(h),WS10m,WD10m,SP
@@ -601,14 +607,24 @@ def _parse_pvgis_tmy_csv(src):
601
607
data = pd .DataFrame (data , dtype = float )
602
608
data .index = dtidx
603
609
# finally there's some meta data
604
- meta = [line .decode ('utf-8' ).strip () for line in src .readlines ()]
605
- return data , months_selected , inputs , meta
610
+ for line in src .readlines ():
611
+ if ':' in line :
612
+ meta ['descriptions' ][line .split (':' )[0 ]] = \
613
+ line .split (':' )[1 ].strip ()
614
+ return data , meta
606
615
607
616
608
617
def read_pvgis_tmy (filename , pvgis_format = None , map_variables = True ):
609
618
"""
610
619
Read a TMY file downloaded from PVGIS.
611
620
621
+ .. versionchanged:: 0.13.0
622
+ The function now returns two items ``(data,meta)``. Previous
623
+ versions of this function returned three elements
624
+ ``(data,months_selected,inputs,meta)``. The ``inputs`` dictionary and
625
+ ``months_selected`` are now included in ``meta``, which has changed
626
+ structure to accommodate it.
627
+
612
628
Parameters
613
629
----------
614
630
filename : str, pathlib.Path, or file-like buffer
@@ -629,10 +645,6 @@ def read_pvgis_tmy(filename, pvgis_format=None, map_variables=True):
629
645
-------
630
646
data : pandas.DataFrame
631
647
the weather data
632
- months_selected : list
633
- TMY year for each month, ``None`` for EPW
634
- inputs : dict
635
- the inputs, ``None`` for EPW
636
648
metadata : list or dict
637
649
file metadata
638
650
@@ -662,7 +674,6 @@ def read_pvgis_tmy(filename, pvgis_format=None, map_variables=True):
662
674
# EPW: use the EPW parser from the pvlib.iotools epw.py module
663
675
if outputformat == 'epw' :
664
676
data , meta = read_epw (filename )
665
- months_selected , inputs = None , None
666
677
667
678
# NOTE: json and csv output formats have parsers defined as private
668
679
# functions in this module
@@ -676,16 +687,14 @@ def read_pvgis_tmy(filename, pvgis_format=None, map_variables=True):
676
687
except AttributeError : # str/path has no .read() attribute
677
688
with open (str (filename ), 'r' ) as fbuf :
678
689
src = json .load (fbuf )
679
- data , months_selected , inputs , meta = _parse_pvgis_tmy_json (src )
690
+ data , meta = _parse_pvgis_tmy_json (src )
680
691
681
692
elif outputformat == 'csv' :
682
693
try :
683
- data , months_selected , inputs , meta = \
684
- _parse_pvgis_tmy_csv (filename )
694
+ data , meta = _parse_pvgis_tmy_csv (filename )
685
695
except AttributeError : # str/path has no .read() attribute
686
696
with open (str (filename ), 'rb' ) as fbuf :
687
- data , months_selected , inputs , meta = \
688
- _parse_pvgis_tmy_csv (fbuf )
697
+ data , meta = _parse_pvgis_tmy_csv (fbuf )
689
698
690
699
elif outputformat == 'basic' :
691
700
err_msg = "outputformat='basic' is no longer supported, please use " \
@@ -702,7 +711,7 @@ def read_pvgis_tmy(filename, pvgis_format=None, map_variables=True):
702
711
if map_variables :
703
712
data = data .rename (columns = VARIABLE_MAP )
704
713
705
- return data , months_selected , inputs , meta
714
+ return data , meta
706
715
707
716
708
717
def get_pvgis_horizon (latitude , longitude , url = URL , ** kwargs ):
0 commit comments