Skip to content

Commit 53b90ed

Browse files
committed
convert input times to UTC if not localized
1 parent 5d9735c commit 53b90ed

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

pvlib/iotools/era5.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ def get_era5(latitude, longitude, start, end, variables, api_key,
7373
longitude: float
7474
In decimal degrees, east is positive (ISO 19115).
7575
start : datetime like or str
76-
First day of the requested period.
76+
First day of the requested period. Assumed to be UTC if not localized.
7777
end : datetime like or str
78-
Last day of the requested period.
78+
Last day of the requested period. Assumed to be UTC if not localized.
7979
variables : list of str
8080
List of variable names to retrieve, for example ``['ghi',
8181
'temp_air']``. See [1]_ for additional options.
@@ -109,8 +109,15 @@ def get_era5(latitude, longitude, start, end, variables, api_key,
109109
.. [2] https://cds.climate.copernicus.eu/datasets/reanalysis-era5-single-levels-timeseries?tab=overview
110110
.. [3] https://confluence.ecmwf.int/pages/viewpage.action?pageId=505390919
111111
""" # noqa: E501
112-
start = pd.to_datetime(start).strftime("%Y-%m-%d")
113-
end = pd.to_datetime(end).strftime("%Y-%m-%d")
112+
113+
def _to_utc_dt_notz(dt):
114+
dt = pd.to_datetime(dt)
115+
if dt.tzinfo is not None:
116+
dt = dt.tz_convert("UTC")
117+
return dt
118+
119+
start = _to_utc_dt_notz(start).strftime("%Y-%m-%d")
120+
end = _to_utc_dt_notz(end).strftime("%Y-%m-%d")
114121

115122
headers = {'PRIVATE-TOKEN': api_key}
116123

tests/iotools/test_era5.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ def test_get_era5(params, expected):
4848
assert isinstance(meta['jobID'], str)
4949

5050

51+
@requires_ecmwf_credentials
52+
@pytest.mark.remote_data
53+
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
54+
def test_get_era5_timezone(params, expected):
55+
params['start'] = pd.to_datetime(params['start']).tz_convert('Etc/GMT+8')
56+
params['end'] = pd.to_datetime(params['end']).tz_convert('Etc/GMT+8')
57+
df, meta = pvlib.iotools.get_era5(**params)
58+
pd.testing.assert_frame_equal(df, expected, check_freq=False, atol=0.1)
59+
assert meta['longitude'] == -80.0
60+
assert meta['latitude'] == 40.0
61+
assert isinstance(meta['jobID'], str)
62+
63+
5164
@requires_ecmwf_credentials
5265
@pytest.mark.remote_data
5366
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)

0 commit comments

Comments
 (0)