Logic Machine Forum
rscalc error - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: rscalc error (/showthread.php?tid=3122)



rscalc error - s0161038 - 23.01.2021

Dear all,

I just reflashed my SD card after becoming read-only (LM4). After the recovery, I updated to firmware 20200720.

After restoring my backup I noticed the common rscalc function I use to check if the sun is up is not working any more on my system. 

Code:
function sun_is_up()

  -- returns number of minutes from 0:00 until sunrise and sunset
  latitude = 57
  longitude = 24.15
  log('gerund')
  sunrise, sunset = rscalc(latitude, longitude)

    date = os.date('*t')
    now = date.hour * 60 + date.min

    -- from 0:00 to sunrise
        if now < sunrise then
          alert('before sunrise')
    return false
    -- from sunset to 23:59
        elseif now > sunset then
          alert('after sunset')
    return false
        else
          alert('day')
    return true
        end
end


I always get the following error:


Common functions:7: attempt to call global 'rscalc' (a nil value)
stack traceback:


Does anybody have an idea what this is causing? 
Or does anybody know where the sourcecode of rscalc can be found to check what is going wrong?

thanks


RE: rscalc error - buuuudzik - 23.01.2021

You don't have in "Common functions" rscalc function. I've copied it from my LM (I don't know why, maybe it is not included in backup):

Code:
-- sunrise / sunset calculation
function rscalc(latitude, longitude, when)
  local pi = math.pi
  local doublepi = pi * 2
  local rads = pi / 180.0

  local TZ = function(when)
    local ts = os.time(when)
    local utcdate, localdate = os.date('!*t', ts), os.date('*t', ts)
    localdate.isdst = false

    local diff = os.time(localdate) - os.time(utcdate)
    return math.floor(diff / 60) / 60
  end

  local range = function(x)
    local a = x / doublepi
    local b = doublepi * (a - math.floor(a))
    return b < 0 and (doublepi + b) or b
  end

  when = when or os.date('*t')

  local y2k = { year = 2000, month = 1, day = 1 }
  local y2kdays = os.time(when) - os.time(y2k)
  y2kdays = math.ceil(y2kdays / 86400)

  local meanlongitude = range(280.461 * rads + 0.9856474 * rads * y2kdays)
  local meananomaly = range(357.528 * rads + 0.9856003 * rads * y2kdays)
  local lambda = range(meanlongitude + 1.915 * rads * math.sin(meananomaly) + rads / 50 * math.sin(2 * meananomaly))

  local obliq = 23.439 * rads - y2kdays * rads / 2500000

  local alpha = math.atan2(math.cos(obliq) * math.sin(lambda), math.cos(lambda))
  local declination = math.asin(math.sin(obliq) * math.sin(lambda))

  local LL = meanlongitude - alpha
  if meanlongitude < pi then
    LL = LL + doublepi
  end

  local dfo = pi / 216.45

  if latitude < 0 then
    dfo = -dfo
  end

  local fo = math.min(math.tan(declination + dfo) * math.tan(latitude * rads), 1)
  local ha = 12 * math.asin(fo) / pi + 6

  local timezone = TZ(when)
  local equation = 12 + timezone + 24 * (1 - LL / doublepi) - longitude / 15

  local sunrise, sunset = equation - ha, equation + ha

  if sunrise > 24 then
    sunrise = sunrise - 24
  end

  if sunset > 24 then
    sunset = sunset - 24
  end

  return math.floor(sunrise * 60), math.ceil(sunset * 60)
end



RE: rscalc error - s0161038 - 23.01.2021

(23.01.2021, 12:48)buuuudzik Wrote: You don't have in "Common functions" rscalc function. I've copied it from my LM (I don't know why, maybe it is not included in backup):

Code:
-- sunrise / sunset calculation
function rscalc(latitude, longitude, when)
  local pi = math.pi
  local doublepi = pi * 2
  local rads = pi / 180.0

  local TZ = function(when)
    local ts = os.time(when)
    local utcdate, localdate = os.date('!*t', ts), os.date('*t', ts)
    localdate.isdst = false

    local diff = os.time(localdate) - os.time(utcdate)
    return math.floor(diff / 60) / 60
  end

  local range = function(x)
    local a = x / doublepi
    local b = doublepi * (a - math.floor(a))
    return b < 0 and (doublepi + b) or b
  end

  when = when or os.date('*t')

  local y2k = { year = 2000, month = 1, day = 1 }
  local y2kdays = os.time(when) - os.time(y2k)
  y2kdays = math.ceil(y2kdays / 86400)

  local meanlongitude = range(280.461 * rads + 0.9856474 * rads * y2kdays)
  local meananomaly = range(357.528 * rads + 0.9856003 * rads * y2kdays)
  local lambda = range(meanlongitude + 1.915 * rads * math.sin(meananomaly) + rads / 50 * math.sin(2 * meananomaly))

  local obliq = 23.439 * rads - y2kdays * rads / 2500000

  local alpha = math.atan2(math.cos(obliq) * math.sin(lambda), math.cos(lambda))
  local declination = math.asin(math.sin(obliq) * math.sin(lambda))

  local LL = meanlongitude - alpha
  if meanlongitude < pi then
    LL = LL + doublepi
  end

  local dfo = pi / 216.45

  if latitude < 0 then
    dfo = -dfo
  end

  local fo = math.min(math.tan(declination + dfo) * math.tan(latitude * rads), 1)
  local ha = 12 * math.asin(fo) / pi + 6

  local timezone = TZ(when)
  local equation = 12 + timezone + 24 * (1 - LL / doublepi) - longitude / 15

  local sunrise, sunset = equation - ha, equation + ha

  if sunrise > 24 then
    sunrise = sunrise - 24
  end

  if sunset > 24 then
    sunset = sunset - 24
  end

  return math.floor(sunrise * 60), math.ceil(sunset * 60)
end

Thanks, just what i was missing.


RE: rscalc error - buuuudzik - 23.01.2021

PerfectWink