调整时区以计算 solarPosition
Adjusting time zone to compute solarPosition
我正在获取一个包含 UTC 数据和坐标经纬度的数据集
我想为这个日期集的每一行计算日光位置,但我在操作时区时遇到了问题。
到目前为止,
我已经设法通过以下方式制作 UTC 数据和时区:
# library for timezone computations
from timezonefinder import TimezoneFinder
from pytz import timezone
import pytz
# scientific python add-ons
import numpy as np
import pandas as pd
tf = TimezoneFinder()
litteralTimeZone = tf.timezone_at(lng=longitude, lat=latitude)
print(litteralTimeZone)
tz = pytz.timezone(litteralTimeZone)
# Adjust date Time, currently in CSV like: 20070101:0000
Data['time(LOC)'] = pd.DatetimeIndex(
pd.to_datetime(Data['time(UTC)'], format='%Y%m%d:%H%M')
).tz_localize(tz, ambiguous=True, nonexistent='shift_forward')
Data = Data.set_index('time(LOC)')
现在,当我使用
将数据传递给获取太阳位置函数时
pvlib.solarposition.get_solarposition(
data.index, metadata['latitude'],metadata['longitude'])
get_solarposition
是根据数据的 UTC 部分计算的,忽略了它的本地化部分。
有什么想法吗?
感谢使用pvlib!
我认为您的问题是您有 UTC 时间戳,但您将它们与当地时区混合在一起。 UTC 是一个时区。 因此,您应该首先将原始时间戳本地化为 'UTC'
。
# make time-zone aware timestamps from string format in UTC
>>> Data['time(TZ-UTC)'] = pd.DatetimeIndex(
... pd.to_datetime(Data['time(UTC)'], format='%Y%m%d:%H%M')).tz_localize('UTC')
然后就可以直接在pvlib.solarposition.get_solarposition
中使用这些了。
# mimic OP data
>>> Data = pd.DataFrame(
... {'time(UTC)': ['20200420:2030', '20200420:2130', '20200420:2230']})
>>> Data
# time(UTC)
# 0 20200420:2030
# 1 20200420:2130
# 2 20200420:2230
# apply the UTC timezone to the naive timestamps after parsing the string format
>>> Data['time(TZ-UTC)'] = pd.DatetimeIndex(
... pd.to_datetime(Data['time(UTC)'], format='%Y%m%d:%H%M')).tz_localize('UTC')
>>> Data
# time(UTC) time(TZ-UTC)
# 0 20200420:2030 2020-04-20 20:30:00+00:00
# 1 20200420:2130 2020-04-20 21:30:00+00:00
# 2 20200420:2230 2020-04-20 22:30:00+00:00
# now call pvlib.solarposition.get_solarposition with the TZ-aware timestamps
>>> lat, lon = 39.74,-105.24
>>> solarposition.get_solarposition(Data['time(TZ-UTC)'], latitude=lat, longitude=lon)
# apparent_zenith zenith apparent_elevation elevation azimuth equation_of_time
# time(TZ-UTC)
# 2020-04-20 20:30:00+00:00 34.242212 34.253671 55.757788 55.746329 221.860950 1.249402
# 2020-04-20 21:30:00+00:00 43.246151 43.261978 46.753849 46.738022 240.532481 1.257766
# 2020-04-20 22:30:00+00:00 53.872320 53.895328 36.127680 36.104672 254.103959 1.266117
您不需要将它们转换为当地时区。如果需要,使用 pd.DatetimeIndex.tz_convert
将它们从 UTC 转换为当地(例如: Golden, CO)时区。注意:使用像 Etc/GMT+7
这样的固定偏移量可能更方便,因为夏令时可能会导致 Pandas 引发不明确的时间错误。
>>> Data['time(LOC)'] = pd.DatetimeIndex(Data['time(TZ-UTC)']).tz_convert('Etc/GMT+7')
>>> Data = Data.set_index('time(LOC)')
>>> Data
# time(UTC) time(TZ-UTC)
# time(LOC)
# 2020-04-20 13:30:00-07:00 20200420:2030 2020-04-20 20:30:00+00:00
# 2020-04-20 14:30:00-07:00 20200420:2130 2020-04-20 21:30:00+00:00
# 2020-04-20 15:30:00-07:00 20200420:2230 2020-04-20 22:30:00+00:00
太阳位置结果应与本地(例如: Golden, CO)时间或 UTC 时间完全相同:
>>> solarposition.get_solarposition(Data.index, latitude=lat, longitude=lon)
# apparent_zenith zenith apparent_elevation elevation azimuth equation_of_time
# time(LOC)
# 2020-04-20 13:30:00-07:00 34.242212 34.253671 55.757788 55.746329 221.860950 1.249402
# 2020-04-20 14:30:00-07:00 43.246151 43.261978 46.753849 46.738022 240.532481 1.257766
# 2020-04-20 15:30:00-07:00 53.872320 53.895328 36.127680 36.104672 254.103959 1.266117
这有帮助吗?很高兴回答更多问题!干杯!
我正在获取一个包含 UTC 数据和坐标经纬度的数据集 我想为这个日期集的每一行计算日光位置,但我在操作时区时遇到了问题。
到目前为止, 我已经设法通过以下方式制作 UTC 数据和时区:
# library for timezone computations
from timezonefinder import TimezoneFinder
from pytz import timezone
import pytz
# scientific python add-ons
import numpy as np
import pandas as pd
tf = TimezoneFinder()
litteralTimeZone = tf.timezone_at(lng=longitude, lat=latitude)
print(litteralTimeZone)
tz = pytz.timezone(litteralTimeZone)
# Adjust date Time, currently in CSV like: 20070101:0000
Data['time(LOC)'] = pd.DatetimeIndex(
pd.to_datetime(Data['time(UTC)'], format='%Y%m%d:%H%M')
).tz_localize(tz, ambiguous=True, nonexistent='shift_forward')
Data = Data.set_index('time(LOC)')
现在,当我使用
将数据传递给获取太阳位置函数时pvlib.solarposition.get_solarposition(
data.index, metadata['latitude'],metadata['longitude'])
get_solarposition
是根据数据的 UTC 部分计算的,忽略了它的本地化部分。
有什么想法吗?
感谢使用pvlib!
我认为您的问题是您有 UTC 时间戳,但您将它们与当地时区混合在一起。 UTC 是一个时区。 因此,您应该首先将原始时间戳本地化为 'UTC'
。
# make time-zone aware timestamps from string format in UTC
>>> Data['time(TZ-UTC)'] = pd.DatetimeIndex(
... pd.to_datetime(Data['time(UTC)'], format='%Y%m%d:%H%M')).tz_localize('UTC')
然后就可以直接在pvlib.solarposition.get_solarposition
中使用这些了。
# mimic OP data
>>> Data = pd.DataFrame(
... {'time(UTC)': ['20200420:2030', '20200420:2130', '20200420:2230']})
>>> Data
# time(UTC)
# 0 20200420:2030
# 1 20200420:2130
# 2 20200420:2230
# apply the UTC timezone to the naive timestamps after parsing the string format
>>> Data['time(TZ-UTC)'] = pd.DatetimeIndex(
... pd.to_datetime(Data['time(UTC)'], format='%Y%m%d:%H%M')).tz_localize('UTC')
>>> Data
# time(UTC) time(TZ-UTC)
# 0 20200420:2030 2020-04-20 20:30:00+00:00
# 1 20200420:2130 2020-04-20 21:30:00+00:00
# 2 20200420:2230 2020-04-20 22:30:00+00:00
# now call pvlib.solarposition.get_solarposition with the TZ-aware timestamps
>>> lat, lon = 39.74,-105.24
>>> solarposition.get_solarposition(Data['time(TZ-UTC)'], latitude=lat, longitude=lon)
# apparent_zenith zenith apparent_elevation elevation azimuth equation_of_time
# time(TZ-UTC)
# 2020-04-20 20:30:00+00:00 34.242212 34.253671 55.757788 55.746329 221.860950 1.249402
# 2020-04-20 21:30:00+00:00 43.246151 43.261978 46.753849 46.738022 240.532481 1.257766
# 2020-04-20 22:30:00+00:00 53.872320 53.895328 36.127680 36.104672 254.103959 1.266117
您不需要将它们转换为当地时区。如果需要,使用 pd.DatetimeIndex.tz_convert
将它们从 UTC 转换为当地(例如: Golden, CO)时区。注意:使用像 Etc/GMT+7
这样的固定偏移量可能更方便,因为夏令时可能会导致 Pandas 引发不明确的时间错误。
>>> Data['time(LOC)'] = pd.DatetimeIndex(Data['time(TZ-UTC)']).tz_convert('Etc/GMT+7')
>>> Data = Data.set_index('time(LOC)')
>>> Data
# time(UTC) time(TZ-UTC)
# time(LOC)
# 2020-04-20 13:30:00-07:00 20200420:2030 2020-04-20 20:30:00+00:00
# 2020-04-20 14:30:00-07:00 20200420:2130 2020-04-20 21:30:00+00:00
# 2020-04-20 15:30:00-07:00 20200420:2230 2020-04-20 22:30:00+00:00
太阳位置结果应与本地(例如: Golden, CO)时间或 UTC 时间完全相同:
>>> solarposition.get_solarposition(Data.index, latitude=lat, longitude=lon)
# apparent_zenith zenith apparent_elevation elevation azimuth equation_of_time
# time(LOC)
# 2020-04-20 13:30:00-07:00 34.242212 34.253671 55.757788 55.746329 221.860950 1.249402
# 2020-04-20 14:30:00-07:00 43.246151 43.261978 46.753849 46.738022 240.532481 1.257766
# 2020-04-20 15:30:00-07:00 53.872320 53.895328 36.127680 36.104672 254.103959 1.266117
这有帮助吗?很高兴回答更多问题!干杯!