首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PVLIB:用PVLIB计算小时角的几个问题

PVLIB:用PVLIB计算小时角的几个问题
EN

Stack Overflow用户
提问于 2019-07-11 07:33:22
回答 2查看 442关注 0票数 1

我试图计算使用PVLIB函数"pvl.solarposition.hour_angle()".的小时角

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

  • 首先,我正在转换在'%Y-%m-%d %H:%M:%S'中形成的UTC时间(一周中的秒)。这里得到的结果是在UTC时间,因为GPS时间是参考UTC时间。
  • 在第二部分,我尝试用PVLIB计算小时角,在这部分我遇到了问题。

当我运行小时角时,python将返回一个错误消息:

"naive_times =times.tz_localize(无)#朴素但仍本地化的AttributeError:'str‘对象没有属性’tz_localize‘。

我知道这个错误是关于实现的代码中变量"final_time“的类。根据PVLIB文档,这个变量是需要停留在类pandas.DatetimeIndex (angle.html)上的,我不知道将GPS时间正确地转换到这个类,然后在PVLIB上使用这个结果来计算小时角。

下面是我在测试中使用的部分算法:

代码语言:javascript
复制
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时间(一周中的秒)转换为pandas.DatetimeIndex类的%Y-%m-%d %H:%M:%S'格式
代码语言:javascript
复制
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?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-11 08:48:46

在我的方法中,我使用失稳库将gps时间转换为日期时间格式。接下来,这次与熊猫一起转换为时差指数

=^..^=

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
[-356.58415383 -356.58415383]
票数 1
EN

Stack Overflow用户

发布于 2019-07-11 09:43:08

不要使用datetime或字符串来填充DatetimeIndex,而是创建pandas.Timestamp,这是datetime.datetimepandas版本。

首先构建一个Posix时间戳,也就是距时代仅几秒钟的时间:

代码语言:javascript
复制
week_number = 2024
t_gps = 138088.886582
leapsecond = 37
posix_ts = week_number * 7 * 24 * 60 * 60 + t_gps + leapsecond

然后构建一个Timestamp

代码语言:javascript
复制
pandas_ts = Timestamp.utcfromtimestamp(posix_ts)
>>> Timestamp('2008-10-17 14:22:05.886582')

此时,Timestamp是“朴素的”:虽然构建为UTC,但它不包含时区信息,因此:

代码语言:javascript
复制
pandas_ts = pandas_ts.tz_localize("UTC")
>>> Timestamp('2008-10-17 14:22:05.886582+0000', tz='UTC')

最后,您可以转换为本地时区:

代码语言:javascript
复制
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

代码语言:javascript
复制
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_localizetz_convert混为一谈,并记住始终设置时区。通过这种方式控制DatetimeIndex的创建,使用依赖于pandas自动解析的字符串,时区存在问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56983859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档