Lua 中的太阳位置(方位角)

我在网上只找到了一个 LUA 中的函数,但它给出了错误的值(使用专业的在线工具进行比较)。

从日出到正午后的一段时间内,这个函数似乎运行良好,但是午后,太阳的角度回到了日出的位置。应该是106°到253°,但目前是从106°到~180°再回到106°。

我正在使用的函数:

-- 太阳高度、方位角(度)
function sunposition(latitude, longitude, time)
    time = time or os.time()
    if type(time) == 'table' then time = os.time(time) end

    local date = os.date('*t', time)
    local timezone = (os.time(date) - os.time(os.date('!*t', time))) / 3600
    if date.isdst then timezone = timezone + 1 end

    local utcdate = os.date('*t', time - timezone * 3600)
    local latrad = math.rad(latitude)
    local fd = (utcdate.hour + utcdate.min / 60 + utcdate.sec / 3600) / 24
    local g = (2 * math.pi / 365.25) * (utcdate.yday + fd)
    local d = math.rad(0.396372 - 22.91327 * math.cos(g) + 4.02543 * math.sin(g) - 0.387205 * math.cos(2 * g)
      + 0.051967 * math.sin(2 * g) - 0.154527 * math.cos(3 * g) + 0.084798 * math.sin(3 * g))
    local t = math.rad(0.004297 + 0.107029 * math.cos(g) - 1.837877 * math.sin(g)
      - 0.837378 * math.cos(2 * g) - 2.340475 * math.sin(2 * g))
    local sha = 2 * math.pi * (fd - 0.5) + t + math.rad(longitude)

    local sza = math.acos(math.sin(latrad) * math.sin(d) + math.cos(latrad) * math.cos(d) * math.cos(sha))
    local saa = math.acos((math.sin(d) - math.sin(latrad) * math.cos(sza)) / (math.cos(latrad) * math.sin(sza)))

    return 90 - math.deg(sza), math.deg(saa)
end

示例请求:

lat, long = 45.327063, 14.442176 -- 克罗地亚里耶卡
 time = {year=2016, month=2, day=17, hour=17, min=30} -- 当天结束
 altitude, azimuth = sunposition(lat, long, time)

结果是:

  • 高度为-0.1度
  • 方位角为106度。

结果应该为:

  • 高度为-0.1度
  • 方位角为253度。

我在其他编程语言中找到了多种解决方案,甚至尝试在 Lua 中重新编写,但都没有成功。这个解决方案背后有太复杂的数学。

我正在使用它为我的 Corona SDK 应用程序显示太阳相对于设备的位置。目前唯一的解决方案是通过互联网的 PHP 或 Javascript 脚本进行 API 调用,但我真的想避免这种情况。

非常感谢社区提供的任何帮助。谢谢并爱您们! :)

点赞
用户3041972
用户3041972

Arccos(反余弦函数)的角度等于 360 减去该角度的余弦值。可以简单地返回 360-angle

2016-02-19 23:26:08
用户405845
用户405845

我已经找到了一种方法/技巧来解决这个问题。

函数仍然是一样的:

-- 太阳高度角,方位角(度)
function sunposition(latitude, longitude, time)
    time = time or os.time()
    if type(time) == 'table' then time = os.time(time) end

    local date = os.date('*t', time)
    local timezone = (os.time(date) - os.time(os.date('!*t', time))) / 3600
    if date.isdst then timezone = timezone + 1 end

    local utcdate = os.date('*t', time - timezone * 3600)
    local latrad = math.rad(latitude)
    local fd = (utcdate.hour + utcdate.min / 60 + utcdate.sec / 3600) / 24
    local g = (2 * math.pi / 365.25) * (utcdate.yday + fd)
    local d = math.rad(0.396372 - 22.91327 * math.cos(g) + 4.02543 * math.sin(g) - 0.387205 * math.cos(2 * g)
      + 0.051967 * math.sin(2 * g) - 0.154527 * math.cos(3 * g) + 0.084798 * math.sin(3 * g))
    local t = math.rad(0.004297 + 0.107029 * math.cos(g) - 1.837877 * math.sin(g)
      - 0.837378 * math.cos(2 * g) - 2.340475 * math.sin(2 * g))
    local sha = 2 * math.pi * (fd - 0.5) + t + math.rad(longitude)

    local sza = math.acos(math.sin(latrad) * math.sin(d) + math.cos(latrad) * math.cos(d) * math.cos(sha))
    local saa = math.acos((math.sin(d) - math.sin(latrad) * math.cos(sza)) / (math.cos(latrad) * math.sin(sza)))

    return 90 - math.deg(sza), math.deg(saa)
end

修复问题的添加代码:

function getSunPos(lat, long, time)
    findTime = {}
    findTime.hour, findTime.min = time.hour, time.min
    fixedAzimuthLast, fixedAzimuth = 0, 0
    for i=0,23 do
        for j=0,59 do
            time.hour, time.min = i, j
            local altitude, azimuth = sunposition(lat, long, time)
            -- 修复方位角
            if fixedAzimuthLast < azimuth then
                fixedAzimuthLast = azimuth
                fixedAzimuth = fixedAzimuthLast
            else
                fixedAzimuth = fixedAzimuthLast + (180 - azimuth)
            end
            -- 在目标时间找到方位角
            if findTime.hour == i and findTime.min == j then
                -- 最终结果
                return altitude, fixedAzimuth
            end
        end
    end
end

最终获得正确结果的方法:

lat, long = 45.327063, 14.442176
altitude, azimuth = getSunPos(lat, long, os.date('*t', os.time()))

就这样。 我更喜欢完整的函数可以正确进行数学计算,但这已足够。它已在全球三个地点进行了测试。

2016-02-26 17:25:22