0

我正在尝试计算使用 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)

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

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

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

2 回答 2

0

在我的方法中,我使用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]
于 2019-07-11T08:48:46.230 回答
0

而不是使用datetimeor 字符串来填充您的DatetimeIndex.create版本。pandas.Timestamppandasdatetime.datetime

首先构建一个 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_localizetz_convert记住要始终设置时区。通过这种方式,您可以DatetimeIndex使用依赖于自动解析的字符串来控制pandas时区的创建。

于 2019-07-11T09:43:08.793 回答