我试图计算使用PVLIB函数"pvl.solarposition.hour_angle()".的小时角
我正在开发的代码分为02部分:
当我运行小时角时,python将返回一个错误消息:
"naive_times =times.tz_localize(无)#朴素但仍本地化的AttributeError:'str‘对象没有属性’tz_localize‘。
我知道这个错误是关于实现的代码中变量"final_time“的类。根据PVLIB文档,这个变量是需要停留在类pandas.DatetimeIndex (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. How the result after the GPS time transformation is in UTC time, I need convert to the local time first and transform again for UTC time including the tz\_localize?
发布于 2019-07-11 08:48:46
在我的方法中,我使用失稳库将gps时间转换为日期时间格式。接下来,这次与熊猫一起转换为时差指数;
=^..^=
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]发布于 2019-07-11 09:43:08
不要使用datetime或字符串来填充DatetimeIndex,而是创建pandas.Timestamp,这是datetime.datetime的pandas版本。
首先构建一个Posix时间戳,也就是距时代仅几秒钟的时间:
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')此时,Timestamp是“朴素的”:虽然构建为UTC,但它不包含时区信息,因此:
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的创建,使用依赖于pandas自动解析的字符串,时区存在问题。
https://stackoverflow.com/questions/56983859
复制相似问题