|
18 | 18 | import json
|
19 | 19 | from pathlib import Path
|
20 | 20 | import requests
|
| 21 | +import numpy as np |
21 | 22 | import pandas as pd
|
| 23 | +import pytz |
22 | 24 | from pvlib.iotools import read_epw, parse_epw
|
23 |
| -import warnings |
24 |
| -from pvlib._deprecation import pvlibDeprecationWarning |
25 | 25 |
|
26 | 26 | URL = 'https://re.jrc.ec.europa.eu/api/'
|
27 | 27 |
|
@@ -390,9 +390,33 @@ def read_pvgis_hourly(filename, pvgis_format=None, map_variables=True):
|
390 | 390 | raise ValueError(err_msg)
|
391 | 391 |
|
392 | 392 |
|
| 393 | +def _coerce_and_roll_tmy(tmy_data, tz, year): |
| 394 | + """ |
| 395 | + Assumes ``tmy_data`` input is UTC, converts from UTC to ``tz``, rolls |
| 396 | + dataframe so timeseries starts at midnight, and forces all indices to |
| 397 | + ``year``. Only works for integer ``tz``, but ``None`` and ``False`` are |
| 398 | + re-interpreted as zero / UTC. |
| 399 | + """ |
| 400 | + if tz: |
| 401 | + tzname = pytz.timezone(f'Etc/GMT{-tz:+d}') |
| 402 | + else: |
| 403 | + tz = 0 |
| 404 | + tzname = pytz.timezone('UTC') |
| 405 | + new_index = pd.DatetimeIndex([ |
| 406 | + timestamp.replace(year=year, tzinfo=tzname) |
| 407 | + for timestamp in tmy_data.index], |
| 408 | + name=f'time({tzname})') |
| 409 | + new_tmy_data = pd.DataFrame( |
| 410 | + np.roll(tmy_data, tz, axis=0), |
| 411 | + columns=tmy_data.columns, |
| 412 | + index=new_index) |
| 413 | + return new_tmy_data |
| 414 | + |
| 415 | + |
393 | 416 | def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
|
394 | 417 | userhorizon=None, startyear=None, endyear=None,
|
395 |
| - map_variables=True, url=URL, timeout=30): |
| 418 | + map_variables=True, url=URL, timeout=30, |
| 419 | + roll_utc_offset=None, coerce_year=None): |
396 | 420 | """
|
397 | 421 | Get TMY data from PVGIS.
|
398 | 422 |
|
@@ -424,6 +448,13 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
|
424 | 448 | base url of PVGIS API, append ``tmy`` to get TMY endpoint
|
425 | 449 | timeout : int, default 30
|
426 | 450 | time in seconds to wait for server response before timeout
|
| 451 | + roll_utc_offset: int, optional |
| 452 | + Use to specify a time zone other than the default UTC zero and roll |
| 453 | + dataframe by ``roll_utc_offset`` so it starts at midnight on January |
| 454 | + 1st. Ignored if ``None``, otherwise will force year to ``coerce_year``. |
| 455 | + coerce_year: int, optional |
| 456 | + Use to force indices to desired year. Will default to 1990 if |
| 457 | + ``coerce_year`` is not specified, but ``roll_utc_offset`` is specified. |
427 | 458 |
|
428 | 459 | Returns
|
429 | 460 | -------
|
@@ -510,6 +541,11 @@ def get_pvgis_tmy(latitude, longitude, outputformat='json', usehorizon=True,
|
510 | 541 | if map_variables:
|
511 | 542 | data = data.rename(columns=VARIABLE_MAP)
|
512 | 543 |
|
| 544 | + if not (roll_utc_offset is None and coerce_year is None): |
| 545 | + # roll_utc_offset is specified, but coerce_year isn't |
| 546 | + coerce_year = coerce_year or 1990 |
| 547 | + data = _coerce_and_roll_tmy(data, roll_utc_offset, coerce_year) |
| 548 | + |
513 | 549 | return data, months_selected, inputs, meta
|
514 | 550 |
|
515 | 551 |
|
|
0 commit comments