Skip to content

Commit 4fd05fb

Browse files
committed
rename roll_utc_offset in get_pvgis_tmy
- refactor utc_offset everywhere including comments and docstring - add additional test to coerce year even if utc offset is zero or none - change tzname to 'UTC' (versus Etc/GMT or Etc/GMT+0) if replacing with zero utc offset
1 parent 440e780 commit 4fd05fb

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pvlib/iotools/pvgis.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ def _coerce_and_roll_pvgis_tmy(pvgis_data, tz, year):
395395
After converting TZ, roll dataframe so timeseries starts at midnight
396396
and force all indices to a common year. Only works for integer TZ.
397397
"""
398-
tzname = pytz.timezone(f'Etc/GMT{-tz:+d}')
398+
tzname = (
399+
pytz.timezone(f'Etc/GMT{-tz:+d}') if tz != 0
400+
else pytz.timezone('UTC'))
399401
new_index = [
400402
timestamp.replace(year=year, tzinfo=tzname)
401403
for timestamp in pvgis_data.index]
@@ -409,7 +411,7 @@ def _coerce_and_roll_pvgis_tmy(pvgis_data, tz, year):
409411
def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
410412
userhorizon=None, startyear=None, endyear=None,
411413
map_variables=True, url=URL, timeout=30,
412-
utc_offset=None, coerce_year=None):
414+
roll_utc_offset=None, coerce_year=None):
413415
"""
414416
Get TMY data from PVGIS.
415417
@@ -441,13 +443,13 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
441443
base url of PVGIS API, append ``tmy`` to get TMY endpoint
442444
timeout : int, default 30
443445
time in seconds to wait for server response before timeout
444-
utc_offset: int, default None
446+
roll_utc_offset: int, default None
445447
Use to specify a time zone other than the default UTC zero and roll
446-
dataframe by ``utc_offset`` so it starts at midnight on January 1st.
447-
Ignored if ``None``, otherwise will also force year to ``coerce_year``.
448+
dataframe by ``roll_utc_offset`` so it starts at midnight on January
449+
1st. Ignored if ``None``, otherwise will force year to ``coerce_year``.
448450
coerce_year: int, default None
449451
Use to force indices to desired year. Will default to 1990 if
450-
``coerce_year`` is ``None``, but ``utc_offset`` is not ``None``.
452+
``coerce_year`` is ``None``, but ``roll_utc_offset`` is not ``None``.
451453
452454
Returns
453455
-------
@@ -534,15 +536,15 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
534536
if map_variables:
535537
data = data.rename(columns=VARIABLE_MAP)
536538

537-
if utc_offset is not None or coerce_year is not None:
539+
if roll_utc_offset is not None or coerce_year is not None:
538540
# XXX: be explicit, test for identity not implicit booleaness
539-
# utc_offset None but coerce_year isn't, set year with utc zero
540-
if utc_offset is None: # XXX: None and zero are both False
541-
utc_offset = 0
541+
# roll_utc_offset None but coerce_year isn't, set year with utc zero
542+
if roll_utc_offset is None: # XXX: None and zero are both False
543+
roll_utc_offset = 0
542544
# coerce_year is None but utc_off isn't, set year to 1990
543545
if coerce_year is None: # more explicit than (coerce_year or 1990)
544546
coerce_year = 1990
545-
data = _coerce_and_roll_pvgis_tmy(data, utc_offset, coerce_year)
547+
data = _coerce_and_roll_pvgis_tmy(data, roll_utc_offset, coerce_year)
546548

547549
return data, months_selected, inputs, meta
548550

pvlib/tests/iotools/test_pvgis.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def test_get_pvgis_tmy_coerce_year():
447447
cet_tz = 1 # Turin time is CET
448448
cet_name = 'Etc/GMT-1'
449449
# check indices of rolled data after converting timezone
450-
pvgis_data, _, _, _ = get_pvgis_tmy(45, 8, utc_offset=cet_tz)
450+
pvgis_data, _, _, _ = get_pvgis_tmy(45, 8, roll_utc_offset=cet_tz)
451451
jan1_midnight = pd.Timestamp('1990-01-01 00:00:00', tz=cet_name)
452452
dec31_midnight = pd.Timestamp('1990-12-31 23:00:00', tz=cet_name)
453453
assert pvgis_data.index[0] == jan1_midnight
@@ -459,14 +459,23 @@ def test_get_pvgis_tmy_coerce_year():
459459
# repeat tests with year coerced
460460
test_yr = 2021
461461
pvgis_data, _, _, _ = get_pvgis_tmy(
462-
45, 8, utc_offset=cet_tz, coerce_year=test_yr)
462+
45, 8, roll_utc_offset=cet_tz, coerce_year=test_yr)
463463
jan1_midnight = pd.Timestamp(f'{test_yr}-01-01 00:00:00', tz=cet_name)
464464
dec31_midnight = pd.Timestamp(f'{test_yr}-12-31 23:00:00', tz=cet_name)
465465
assert pvgis_data.index[0] == jan1_midnight
466466
assert pvgis_data.index[-1] == dec31_midnight
467467
for m, test_case in enumerate(noon_test_data):
468468
expected = pvgis_data[pvgis_data.index.month == m+1].iloc[12+cet_tz]
469469
assert all(test_case == expected)
470+
# repeat tests with year coerced but utc offset none or zero
471+
pvgis_data, _, _, _ = get_pvgis_tmy(45, 8, coerce_year=test_yr)
472+
jan1_midnight = pd.Timestamp(f'{test_yr}-01-01 00:00:00', tz='UTC')
473+
dec31_midnight = pd.Timestamp(f'{test_yr}-12-31 23:00:00', tz='UTC')
474+
assert pvgis_data.index[0] == jan1_midnight
475+
assert pvgis_data.index[-1] == dec31_midnight
476+
for m, test_case in enumerate(noon_test_data):
477+
expected = pvgis_data[pvgis_data.index.month == m+1].iloc[12]
478+
assert all(test_case == expected)
470479

471480

472481
@pytest.mark.remote_data

0 commit comments

Comments
 (0)