gpt4 book ai didi

Python ephem 模块无法识别看似有效的纬度字符串

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:47 26 4
gpt4 key购买 nike

我正在尝试使用以下代码计算日出和日落的方位角:

import ephem
import csv
import math

def convert_angle(angle):
fields = math.modf(angle)
deg = fields[1]
min_dec = abs(fields[0]*60)
min_fields = math.modf(min_dec)
minu = min_fields[1]
sec = min_fields[0]*60
s = '\'{:2.0f}:{:2.0f}:{:2.1f}\''.format(deg,minu,sec)
return s

sun = ephem.Sun()

with open('locations.txt', 'rb') as csvfile:
rdrreader = csv.reader(csvfile, delimiter='\t', quotechar='"')
for row in rdrreader:
print row[2],row[5],row[6]

lat = convert_angle(float(row[5]))
lon = convert_angle(float(row[6]))

print lat,lon

obs = ephem.Observer()
obs.lat = lat
obs.lon = lon
rising = obs.next_rising(sun)
print('Visual sunrise: %s' % rising)

脚本对我的格式不满意:

ipdb> c
'60:47:31.2' '-161:52:35.5'
> /Users/mmuratet/weather/solar_ephem.py(35)<module>()
34 obs = ephem.Observer()
3--> 35 obs.lat = lat
36 obs.lon = lon
ipdb> s
ValueError: 'your angle string %r does not have the format [number[:number[:number]]]'
> /Users/mmuratet/weather/solar_ephem.py(35)<module>()
34 obs = ephem.Observer()
3--> 35 obs.lat = lat
36 obs.lon = lon

但同样的命令可以在带有剪切和粘贴的 python 控制台中运行

>>> obs.lat = '60:47:31.2' 
>>> print obs
<ephem.Observer date='2015/3/3 20:00:58' epoch='2000/1/1 12:00:00' lon=-161:52:35.5 lat=60:47:31.2 elevation=0.0m horizon=0:00:00.0 temp=15.0C pressure=1010.0mBar>

我知道一定有一个简单明了的解决方案,但我没有看到。任何建议将不胜感激。

这是一些数据:

AK      Bethel  PABC    60.791987#N     161.876539#W    60.7919870      -161.876539
AK Fairbanks/Pedro Dome PAPD 65.0351238#N 147.5014222#W 65.0351238 -147.5014222

最佳答案

看起来你的 latlon 字符串里面都有单引号,一个在字符串的开头,一个在字符串的结尾:

s = '\'{:2.0f}:{:2.0f}:{:2.1f}\''.format(deg,minu,sec)

单引号不是科学家书写纬度或经度的方式的一部分,因此 PyEphem 感到困惑。尝试构建没有额外两个字符的字符串:

s = '{:2.0f}:{:2.0f}:{:2.1f}'.format(deg,minu,sec)

当它只看到数字和冒号时,PyEphem 应该很高兴!

关于Python ephem 模块无法识别看似有效的纬度字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28842052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com