使用PyEphem计算阴影长度 [英] Compute shadow length using PyEphem
问题描述
我正在使用PyEphem,并想计算阴影的长度(假定地面上植有一根长度为单位的木棍).长度将由cot(phi)给出,其中phi是太阳仰角(如果我错了,请纠正我).我不确定在太阳上使用哪个字段?在下面的示例中,我使用的是角度alt:
I am using PyEphem and want to calculate the length of a shadow (assume a stick of unit length is planted in the ground). The length will be given by cot(phi), where phi is the solar elevation angle (please correct me if I'm wrong). I'm not sure what field to use on the Sun? In the example below, I'm using the angle alt:
import ephem, math
o = ephem.Observer()
o.lat, o.long = '37.0625', '-95.677068'
sun = ephem.Sun()
sunrise = o.previous_rising(sun, start=ephem.now())
noon = o.next_transit(sun, start=sunrise)
shadow = 1 / math.tan(sun.alt)
请在下面检查我的解释:
Please check my interpretation below:
- 如果切线是无限的,则表示太阳正好在头顶上方,没有阴影.
- 如果切线为零,则表示太阳在地平线上,阴影无限长.
- 我不知道如何解释cot(phi)的负面结果.有人可以帮我吗?
最后,我对如何使用PyEphem从阴影长度向后工作感到困惑,直到给定一个ephem.Observer()时,太阳将再次投射该长度的阴影.
Finally, I'm confused about how to use PyEphem to work backwards from a shadow length to the next time when the sun will cast a shadow of that length, given an ephem.Observer().
在此我将不胜感激.
推荐答案
在太阳上使用哪个字段?
what field to use on the Sun?
sun.alt
是正确的. alt
是地平线以上的高度;连同北部以东的方位角,它们定义了相对于地平线的表观位置.
The sun.alt
is correct. alt
is an altitude above horizon; together with an azimuth east of north they define an apparent position relative to horizon.
您的计算结果几乎是正确的.您忘记提供观察者了:sun = ephem.Sun(o)
.
Your calculations are almost correct. You've forgot to supply an observer: sun = ephem.Sun(o)
.
- 我不知道如何解释cot(phi)的负面结果.能 有人帮我吗?
- I don't know how to interpret negative results from cot(phi). Can someone help me?
在这种情况下,太阳在地平线以下.
The Sun is below horizon in this case.
最后,我对如何使用感到困惑 PyEphem从一个倒退的工作 到下一次的阴影长度 太阳会给它蒙上阴影 长度,给定一个ephem.Observer().
Finally, I'm confused about how to use PyEphem to work backwards from a shadow length to the next time when the sun will cast a shadow of that length, given an ephem.Observer().
这是一个提供功能的脚本:g(date) -> altitude
计算下一次太阳将投射与当前长度相同的阴影的时间(方位角-不考虑阴影的方向):
Here's a script that given a function: g(date) -> altitude
computes the next time when the sun will cast a shadow with the same length as right now (an azimuth -- direction of the shadow is not considered):
#!/usr/bin/env python
import math
import ephem
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as opt
def main():
# find a shadow length for a unit-length stick
o = ephem.Observer()
o.lat, o.long = '37.0625', '-95.677068'
now = o.date
sun = ephem.Sun(o) #NOTE: use observer; it provides coordinates and time
A = sun.alt
shadow_len = 1 / math.tan(A)
# find the next time when the sun will cast a shadow of the same length
t = ephem.Date(find_next_time(shadow_len, o, sun))
print "current time:", now, "next time:", t # UTC time
####print ephem.localtime(t) # print "next time" in a local timezone
def update(time, sun, observer):
"""Update Sun and observer using given `time`."""
observer.date = time
sun.compute(observer) # computes `sun.alt` implicitly.
# return nothing to remember that it modifies objects inplace
def find_next_time(shadow_len, observer, sun, dt=1e-3):
"""Solve `sun_altitude(time) = known_altitude` equation w.r.t. time."""
def f(t):
"""Convert the equation to `f(t) = 0` form for the Brent's method.
where f(t) = sun_altitude(t) - known_altitude
"""
A = math.atan(1./shadow_len) # len -> altitude
update(t, sun, observer)
return sun.alt - A
# find a, b such as f(a), f(b) have opposite signs
now = observer.date # time in days
x = np.arange(now, now + 1, dt) # consider 1 day
plt.plot(x, map(f, x))
plt.grid(True)
####plt.show()
# use a, b from the plot (uncomment previous line to see it)
a, b = now+0.2, now+0.8
return opt.brentq(f, a, b) # solve f(t) = 0 equation using Brent's method
if __name__=="__main__":
main()
输出
current time: 2011/4/19 23:22:52 next time: 2011/4/20 13:20:01
这篇关于使用PyEphem计算阴影长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!