首页 > 解决方案 > PVLIB:使用 PVLIB 计算小时角的问题

问题描述

我正在尝试计算使用 PVLIB 函数“ pvl.solarposition.hour_angle() ”的小时角。

我正在开发的代码分为 02 部分:

当我运行小时角时,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)

对于这个问题,我的问题是:

  1. 如何将 GPS 时间(一周中的秒数)转换为格式 ' %Y-%m-%d %H:%M:%S'pandas.DatetimeIndex类?

    1. GPS时间转换后的结果如何是UTC时间,我需要先转换为本地时间并再次转换为UTC时间,包括tz_localize?

标签: pythonpvlib

解决方案


在我的方法中,我使用astropy库将 gps 时间转换为日期时间格式。接下来用 pandas将这个时间转换为Datetimeindex ;

=^..^=

import pandas as pd
import pvlib as pvl
from astropy.time import Time

latitude = -23.048576 # degress
longitude = -46.305043 # degrees

# convert gps seconds to time format
gps_time = 138088.886582
t = Time(gps_time, format='gps')
t = Time(t, format='iso')

# create empty df
df = pd.DataFrame(columns = ['Timestamp'])

# create date time series
time_series = pd.to_datetime(str(t), infer_datetime_format=True)

# append time series to data frame
df = df.append({'Timestamp': pd.to_datetime(time_series)}, ignore_index=True)
df = df.append({'Timestamp': pd.to_datetime(time_series)}, ignore_index=True)

# create time index
time_index = pd.DatetimeIndex(df.Timestamp)

# calculate hour angle
hour_angle = pvl.solarposition.hour_angle(time_index, longitude, latitude*60)

输出:

[-356.58415383 -356.58415383]

推荐阅读