我正在尝试使用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)
对于这个问题,我的问题是:
如何将GPS时间(一周的秒数)转换为格式“ %Y-%m-%d%H:%M:%S” ,并在班级< strong> pandas.DatetimeIndex ?
答案 0 :(得分:0)
在我的方法中,我使用astropy库将gps时间转换为日期时间格式。接下来用熊猫将这次转换为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]
答案 1 :(得分:0)
创建datetime
版本DatetimeIndex
,而不是使用pandas.Timestamp
或字符串来填充pandas
。
首先建立一个Posix时间戳,距纪元又是几秒钟:
datetime.datetime
然后构建一个week_number = 2024
t_gps = 138088.886582
leapsecond = 37
posix_ts = week_number * 7 * 24 * 60 * 60 + t_gps + leapsecond
:
Timestamp
此时,pandas_ts = Timestamp.utcfromtimestamp(posix_ts)
>>> Timestamp('2008-10-17 14:22:05.886582')
是“天真”的:作为UTC构建时,它不包含时区信息,因此:
Timestamp
最后,您可以转换为本地时区:
pandas_ts = pandas_ts.tz_localize("UTC")
>>> Timestamp('2008-10-17 14:22:05.886582+0000', tz='UTC')
并构建所需的pandas_ts = pandas_ts.tz_convert("my_time_zone") # replace by correct tz
>>> Timestamp('2008-10-17 NN:22:05.886582+XXXX', tz='my_time_zone')
:
DatetimeIndex
请勿混用di = DatetimeIndex([pandas_ts])
print(di) # shows the time zone in the type (dtype='datetime64[ns, my_time_zone]')
long = -46.305043
eq_time = pvl.solarposition.equation_of_time_pvcdrom(295)
hour_angle = pvl.solarposition.hour_angle(di, long, eq_time)
print(hour_angle)
和tz_localize
,切记始终设置时区。通过这种方式,您可以使用tz_convert
依赖于字符串的自动解析来控制DatetimeIndex
的创建,这对于时区是有问题的。