我正在尝试计算使用 PVLIB 函数“ pvl.solarposition.hour_angle() ”的小时角。
我正在开发的代码分为 02 部分:
- 首先,我将 GPS 时间(星期几)转换为 ' %Y-%m-%d %H:%M:%S'格式的 UTC 时间。此处获得的结果采用 UTC 时间,因为 GPS 时间以 UTC 为单位。
- 在第二部分,我尝试使用 PVLIB 计算小时角,在这部分我遇到了问题。
当我运行小时角时,python 将返回一条错误消息:
"naive_times = times.tz_localize(None) # 天真但仍然本地化的 AttributeError: 'str' 对象没有属性 'tz_localize"。
我知道这个错误是关于实现代码上的变量“final_time”的类。根据 PVLIB 文档,此变量需要保留在pandas.DatetimeIndex类(https://pvlib-python.readthedocs.io/en/latest/generated/pvlib.solarposition.hour_angle.html)上,我不知道正确转换 GPS 时间到这个类,然后在 PVLIB 上使用这个结果来计算小时角。
下面遵循我在测试中使用的部分算法:
import datetime
import pvlib as pvl
t_gps = 138088.886582 #seconds of week
lat = -23.048576 # degress
long = -46.305043 # degrees
## Transfomring the GPS time (seconds of week) in Date Time
## The result printed here is in UTC time because the GPS time is refered in UTC Time.
datetimeformat = ('%Y-%m-%d %H:%M:%S')
leapsecond = 37
epoch = datetime.datetime.strptime ( "1980-01-06 00:00:00" , datetimeformat)
decorrido = datetime.timedelta( days = (2024 * 7 ), seconds = t_gps + leapsecond)
final_time = datetime.datetime.strftime(epoch + decorrido, datetimeformat)
print(final_time)
print(type(final_time))
## Calculating the hour angle using PVLIB
solar_declin = pvl.solarposition.declination_spencer71(295)
print(solar_declin)
eq_time = pvl.solarposition.equation_of_time_pvcdrom(295)
print(eq_time)
hour_angle = pvl.solarposition.hour_angle(final_time, long, eq_time)
print(hour_angle)
对于这个问题,我的问题是:
如何将 GPS 时间(一周中的秒数)转换为格式 ' %Y-%m-%d %H:%M:%S'和pandas.DatetimeIndex类?
- GPS时间转换后的结果如何是UTC时间,我需要先转换为本地时间并再次转换为UTC时间,包括tz_localize?