Code:
--[[ sun and moon position chart by mrpeachy may 2011 version x3 N and S hemispheres plot correctly
call in conkyrc like so
lua_load /path to file/fiename.lua
lua_draw_hook_pre draw_fig
mrpeachy
I=5 -- interval, if changed then kill/restart, set 1, 5 or 10. shorter interval increases cpu load
LEW="-1" -- -1 for west, 1 for east
LOD="90" --longitude degrees
LOM="11" --longitude minutes
LNS="1" -- 1 for north, -1 for south
LAD="38" --latitude degrees
LAM="37" --latitude minutes
TZ="6" --timezone
GEW="-1" -- -1 for west of Greenwich, 1 for east of Greenwich
]]
require 'cairo'
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
--------------------------------------------------------------------------------
function conky_draw_fig()
if conky_window == nil then return end
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
cr = cairo_create(cs)
local updates=tonumber(conky_parse('${updates}'))
if updates>5 then
--#########################################################################################################
--#########################################################################################################
Y=tostring(os.date("%Y"))
M=tostring(os.date("%m"))
D=tostring(os.date("%d"))
--INPUT DATA
I=5 -- interval, if changed then kill/restart, set 1, 5 or 10. shorter interval increases cpu load
LEW="-1" -- -1 for west, 1 for east
LOD="58" --longitude degrees
LOM="28" --longitude minutes
LNS="-1" -- 1 for north, -1 for south
LAD="34" --latitude degrees
LAM="37" --latitude minutes
TZ="3" --timezone
GEW="-1" -- -1 for west of Greenwich, 1 for east of Greenwich
scale=1
center_y=200
left_x=0
--------------------------------------------------------------
delay=1800 -- time between info gathering, much like execi interval
--------------------------------------------------------------
timer=(updates % delay)+1
--fetch sun and moon info
if timer==1 or updates==10 or tostring(os.date("%H:%M:%S"))=="00:00:00" then
os.execute ('curl -f --data "FFX=2&obj=11&xxy=' .. Y .. '&xxm=' .. M .. '&xxd=' .. D .. '&xxi=' .. I .. '&place=%28no+name+given%29&xx0=' .. LEW .. '&xx1=' .. LOD .. '&xx2=' .. LOM .. '&yy0=' .. LNS .. '&yy1=' .. LAD .. '&yy2=' .. LAM .. '&zz1=' .. TZ .. '&zz0=' .. GEW .. '&ZZZ=END" http://aa.usno.navy.mil/cgi-bin/aa_altazw.pl > /tmp/moon.txt')
os.execute ('curl -f --data "FFX=2&obj=10&xxy=' .. Y .. '&xxm=' .. M .. '&xxd=' .. D .. '&xxi=' .. I .. '&place=%28no+name+given%29&xx0=' .. LEW .. '&xx1=' .. LOD .. '&xx2=' .. LOM .. '&yy0=' .. LNS .. '&yy1=' .. LAD .. '&yy2=' .. LAM .. '&zz1=' .. TZ .. '&zz0=' .. GEW .. '&ZZZ=END" http://aa.usno.navy.mil/cgi-bin/aa_altazw.pl > /tmp/sun.txt')
local f = io.popen("cat /tmp/moon.txt | head -20 | tail -6")
topper= f:read("*a")
f:close()
local f = io.popen("cat /tmp/moon.txt | grep ':' | awk '{print $3}'")
azimuth= f:read("*a")
f:close()
local f = io.popen("cat /tmp/moon.txt | grep ':' | awk '{print $2}'")
altitude= f:read("*a")
f:close()
local f = io.popen("cat /tmp/moon.txt | grep ':' | awk '{print $1}'")
mtime= f:read("*a")
f:close()
local f = io.popen("cat /tmp/sun.txt | grep ':' | awk '{print $3}'")
sazimuth= f:read("*a")
f:close()
local f = io.popen("cat /tmp/sun.txt | grep ':' | awk '{print $2}'")
saltitude= f:read("*a")
f:close()
local f = io.popen("cat /tmp/sun.txt | grep ':' | awk '{print $1}'")
stime= f:read("*a")
f:close()
--format moon tables
aztab=string.split(azimuth,"\n")
alttab=string.split(altitude,"\n")
timetab=string.split(mtime,"\n")
table.remove(alttab)
table.remove(alttab)
table.remove(alttab,1)
table.remove(alttab,1)
table.remove(aztab)
table.remove(aztab)
table.remove(aztab,1)
table.remove(aztab,1)
table.remove(timetab)
table.remove(timetab)
table.remove(timetab,1)
table.remove(timetab,1)
length=#aztab
--format suntables
saztab=string.split(sazimuth,"\n")
salttab=string.split(saltitude,"\n")
stimetab=string.split(stime,"\n")
table.remove(salttab)
table.remove(salttab)
table.remove(salttab,1)
table.remove(salttab,1)
table.remove(saztab)
table.remove(saztab)
table.remove(saztab,1)
table.remove(saztab,1)
table.remove(stimetab)
table.remove(stimetab)
table.remove(stimetab,1)
table.remove(stimetab,1)
slength=#saztab
moonsort={}
az2tab={}
saz2tab={}
--correct moon data for N or S
for i=1,length do
moonsort[i]=tonumber(aztab[i])
if tonumber(aztab[2])<tonumber(aztab[1]) then
direction="N"
msetn=1
if tonumber(aztab[i])<180 then
az2tab[i]=tonumber(aztab[i])+180
moonsort[i]=tonumber(aztab[i])+180
elseif tonumber(aztab[i])>180 then
az2tab[i]=tonumber(aztab[i])-180
moonsort[i]=tonumber(aztab[i])-180
else
az2tab[i]=tonumber(aztab[i])
moonsort[i]=tonumber(aztab[i])
end
else
az2tab[i]=tonumber(aztab[i])
moonsort[i]=tonumber(aztab[i])
direction="S"
msetn=length
end
end--for
--correct sun data for N or S
for i=1,slength do
if tonumber(saztab[2])<tonumber(saztab[1]) then
if tonumber(saztab[i])<180 then
saz2tab[i]=tonumber(saztab[i])+180
elseif tonumber(saztab[i])>180 then
saz2tab[i]=tonumber(saztab[i])-180
else
saz2tab[i]=tonumber(saztab[i])
end
else
saz2tab[i]=tonumber(saztab[i])
end
end--for
table.sort(moonsort)
--get sunrise and sunset
sunrise=conky_parse("${exec conkyForecast -d SR -s 1}")
sunset=stimetab[slength]
end--timed data gathering section
--remove terminal errors when script starts
if topper==nil then topper="starting up..." end
if azimuth==nil then azimuth=0 end
if altitude==nil then altitude=0 end
if mtime==nil then mtime=0 end
if sazimuth==nil then sazimuth=0 end
if saltitude==nil then saltitude=0 end
if stime==nil then stime=0 end
if sunrise==nil then sunrise=" " end
if sunset==nil then sunset=" " end
if length==nil then length=0 end
if slength==nil then slength=0 end
--top info - mostly to confirm curl function and location data
font="Mono"
fsize=12
cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, fsize)
cairo_set_source_rgba (cr,1,1,1,1)
toppertab=string.split(topper,"\n")
cairo_move_to (cr,left_x,center_y-150)
cairo_show_text (cr, toppertab[1])
cairo_stroke (cr)
cairo_move_to (cr,left_x,center_y-138)
cairo_show_text (cr, toppertab[4])
cairo_stroke (cr)
cairo_move_to (cr,left_x,center_y-126)
cairo_show_text (cr, toppertab[5])
cairo_stroke (cr)
--horizon and north/south-------------------------------------------------------------
cairo_set_line_width (cr,1)
cairo_set_source_rgba (cr,0.5,0.5,0.5,1)
cairo_move_to (cr,left_x,center_y)
cairo_rel_line_to (cr,left_x+360,0)
cairo_stroke (cr)
cairo_move_to (cr,left_x+180,center_y)
cairo_rel_line_to (cr,0,-100)
cairo_stroke (cr)
font="Mono"
fsize=12
cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, fsize)
cairo_move_to (cr,left_x+180-4,center_y-102)
cairo_show_text (cr,direction)
cairo_stroke (cr)
--get time in appropriate intervals to set current positions--------------------------
int=I*60
count=tonumber((os.date("%M")*60)+os.date("%S"))
now_timer=(count % int)+1
if now_timer==1 then
setmin=os.date("%M")
sethour=tostring(os.date("%H"))
timenow=sethour .. ":" .. setmin
end
--compensate for timenow being nil when script starts
if timenow==nil then
min=tostring(os.date("%M"))
if I==10 then
--time minutes
mins=string.gsub(min, ".", "%1|")
mins=string.split(mins, "|")
hour=tostring(os.date("%H"))
timenow=(hour .. ":" .. mins[1] .. "0")
elseif I==5 then
mins=string.gsub(min, ".", "%1|")
mins=string.split(mins, "|")
hour=tostring(os.date("%H"))
if mins[2]=="0" or mins[2]=="1" or mins[2]=="2" or mins[2]=="3" or mins[2]=="4" then
timenow=(hour .. ":" .. mins[1] .. "0")
elseif mins[2]=="5" or mins[2]=="6" or mins[2]=="7" or mins[2]=="8" or mins[2]=="9" then
timenow=(hour .. ":" .. mins[1] .. "5")
end
elseif I==1 then
hour=tostring(os.date("%H"))
timenow=hour .. ":" .. min
end
end--if time now
--timenow="22:10"
--plot moon data----------------------------------------------------------------------
cairo_set_line_width (cr,1)
cairo_set_source_rgba (cr,1,1,1,1)
for i=1,length do
--print (aztab[i])
if az2tab[i+1]==nil then az2tab[i+1]=az2tab[i] end
if alttab[i+1]==nil then alttab[i+1]=alttab[i] end
cairo_move_to (cr,left_x+tonumber(az2tab[i]),center_y-tonumber(alttab[i]))
cairo_line_to (cr,left_x+tonumber(az2tab[i+1]),center_y-tonumber(alttab[i+1]))
cairo_stroke (cr)
if tostring(timetab[i])==tostring(timenow) then
moontime=i
end
if tostring(timetab[moontime])~=tostring(timenow) then
moontime=nil
end
if tonumber(moonsort[msetn])==tonumber(az2tab[i]) then
mset=i
end
end
--moon text and moon current position
moony=center_y+20+24
font="Mono"
fsize=12
cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, fsize)
cairo_set_source_rgba (cr,1,1,1,1)
if moontime==nil then
cairo_move_to (cr,left_x,moony)
cairo_show_text (cr,"Moon : Has set.")
cairo_stroke (cr)
else
cairo_move_to (cr,left_x,moony)
cairo_show_text (cr,"Moon : Azimuth=" .. az2tab[moontime] .. ", Altitude=" .. alttab[moontime] .. ", Set=" .. timetab[mset])
cairo_stroke (cr)
cairo_arc (cr,left_x+tonumber(az2tab[moontime]),center_y-tonumber(alttab[moontime]),6,0,2*math.pi)
cairo_fill (cr)
end
--print sun data---------------------------------------------------------------------
cairo_set_line_width (cr,1)
cairo_set_source_rgba (cr,1,1,0,1)
for i=1,slength do
if saz2tab[i+1]==nil then saz2tab[i+1]=saz2tab[i]end
if salttab[i+1]==nil then salttab[i+1]=salttab[i] end
cairo_move_to (cr,left_x+tonumber(saz2tab[i]),center_y-tonumber(salttab[i]))
cairo_line_to (cr,left_x+tonumber(saz2tab[i+1]),center_y-tonumber(salttab[i+1]))
cairo_stroke (cr)
if tostring(stimetab[i])==tostring(timenow) then
suntime=i
end
if tostring(stimetab[suntime])~=tostring(timenow) then
suntime=nil
end
end --for i= slength
--print sun info
suny=center_y+20+36
font="Mono"
fsize=12
cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, fsize)
cairo_set_source_rgba (cr,1,1,0,1)
if suntime==nil then
cairo_move_to (cr,left_x,suny)
cairo_show_text (cr,"Sun : Has set. Sunrise tomorrow at " .. sunrise)
cairo_stroke (cr)
else
cairo_move_to (cr,left_x,suny)
cairo_show_text (cr,"Sun : Azimuth=" .. saz2tab[suntime] .. ", Altitude=" .. salttab[suntime] .. ", Set=" .. sunset)
cairo_stroke (cr)
cairo_arc (cr,left_x+tonumber(saz2tab[suntime]),center_y-tonumber(salttab[suntime]),6,0,2*math.pi)
cairo_fill (cr)
end
--#########################################################################################################
--#########################################################################################################
end-- if updates>5
cairo_destroy(cr)
cairo_surface_destroy(cs)
cr=nil
end-- end main function
Bookmarks