It is currently April 24th, 2024, 12:23 am

Help with Lua script

Get help with creating, editing & fixing problems with skins
User avatar
LazieWouters
Posts: 61
Joined: August 29th, 2021, 11:11 am

Help with Lua script

Post by LazieWouters »

Hi,

I don't understand anything about Lua programming. The following script takes the current day and returns the image corresponding to the current moon phase.
I just need to modify the part that gets today's date to get tomorrow's date.
I don't know how complicated this is, I even researched about adding days and found code examples, but since I don't understand anything about Lua this search was useless.
Thank you for your help

Code: Select all

function Initialize()
			iCurrentStyle=1
end

function Update()     
			SKIN:Bang("!DisableMeasure mMoonPhaseLua") 
			local today = os.date('*t')
		    Gday = today.day		Gmonth = today.month	  Gyear = today.year	
			CalcHijriDate( Gday , Gmonth , Gyear)		
			SKIN:Bang('!SetOption', 'MtMoonShade', 'ImageName',  "#@#\Images\\moon\\"..Hdate..".png")                        				
	return 'Moon set for Julian Date '..jd..' : image '..Hdate..".png"
end 
-----------------------------------------------------------------------
function  julian(year, month, day)
		if (month<=2) then	year = year-1  month = month+12	end
		local A = math.floor(year/ 100)
		local B = 2- A+ math.floor(A/ 4)
		JD = math.floor(365.25* (year+ 4716))+ math.floor(30.6001* (month+ 1))+ day+ B-1524.5
		return JD
end 

function intPart(floatNum)      
		if (floatNum< -0.0000001) 
		   then 	 return math.ceil(floatNum-0.0000001)
		   else		return math.floor(floatNum+0.0000001)	
		 end	
end 

function CalcHijriDate( d , m , y )
		jd = math.ceil( julian( y, m, d ) )		
		local l=jd-1937808
		local n=intPart((l-1)/10631)
		local l=l-10631*n+354
		local j=(intPart((10985-l)/5316))*(intPart((50*l)/17719))+(intPart(l/5670))*(intPart((43*l)/15238))
		local l=l-(intPart((30-j)/15))*(intPart((17719*j)/50))-(intPart(j/16))*(intPart((15238*j)/43))+29
		Hmonth=intPart((24*l)/709)    --   month
		Hdate = l-intPart((709*Hmonth)/24)    --   date 
end
Last edited by SilverAzide on September 20th, 2021, 10:32 pm, edited 1 time in total.
Reason: Please use code tags when posting code. It's the </> button.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2604
Joined: March 23rd, 2015, 5:26 pm

Re: Help with Lua script

Post by SilverAzide »

Whew, you must enjoy pain, LOL. ;) Taking on what I assume is Mordasius' moon calc code without knowing Lua is really gonna be a challenge! (The code somehow got pretty mangled when you posted it, so you might want to repost.)

I think I can tell you what you need to get the next day, but if you change the code too much you might end up breaking the existing code to get today's moon phase. What you need to do is basically add code to the Update function, without changing anything that is already there.

So, the first thing you need is an Image meter in your skin that will show the moon phase image for tomorrow. It appears there is a meter MtMoonShade that is used for today's image, so you'll need one for tomorrow. Let's assume you name your meter MtMoonShadeTomorrow.

Once you have that, tweak the Update function (I added comments so you can see what is happening):

Code: Select all

function Update()
    -- disable the measure that calculates moon phases so that it only runs once
    SKIN:Bang("!DisableMeasure mMoonPhaseLua")

    -- get the Unix-style date for today (now) and convert it to the Hijri date
    local today = os.date('*t')
    Gday = today.day
    Gmonth = today.month
    Gyear = today.year
    CalcHijriDate( Gday , Gmonth , Gyear)

    -- set the image for the meter showing today's moon phase, defined by variable Hdate
    SKIN:Bang('!SetOption', 'MtMoonShade', 'ImageName',  '#@#\Images\\moon\\' .. Hdate .. '.png')

    --
    -- ADD THIS
    --
    -- get date for tomorrow (86400 seconds from now)
    local tomorrow = os.date('*t', os.time() + 60*60*24)

    Gday = tomorrow.day
    Gmonth = tomorrow.month
    Gyear = tomorrow.year
    CalcHijriDate( Gday , Gmonth , Gyear)

    -- set the image for the meter showing tomorrow's moon phase
    SKIN:Bang('!SetOption', 'MtMoonShadeTomorrow', 'ImageName',  '#@#\Images\\moon\\' .. Hdate .. '.png')

    return
end
I did not try this, so I (very probably) made a mistake.

Here's a nice tutorial Working with date / time in Lua. I probably should have read it first, LOL.
Gadgets Wiki GitHub More Gadgets...
User avatar
LazieWouters
Posts: 61
Joined: August 29th, 2021, 11:11 am

Re: Help with Lua script

Post by LazieWouters »

Whew, you must enjoy pain, LOL. ;)
lol, it really hurts more than tapping your little toe on the edge of a piece of furniture :D .

Your code worked very well, here is an example of the result showing the moon on the next 29th and 30th in the southern hemisphere:
Sem título.png
The final design won't look like this, I just added one more image to see if the code works. 100% working.
I changed the date on the computer, because tomorrow is the full moon, so the image of the shadow that this code is looking for doesn't appear. And when there is an error in the code, the background image appears, which is a circle, so to be sure I thought it was better to change the date.

My intention is to display a 7-day sequence of moon phases, and now that I understand what you've done I think I can add code for the remaining days.

Thanks a lot for the help!
You do not have the required permissions to view the files attached to this post.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2604
Joined: March 23rd, 2015, 5:26 pm

Re: Help with Lua script

Post by SilverAzide »

Wow, it actually worked? Sweet!

Is tomorrow the full moon? I thought it was today actually (20 Sep 2021). At least, it is for me where I am. Here is a really nice website that will tell you exactly: https://www.mooncalc.org/. Just enter your location and click the ">|<" button to use the current time.

If you find your calcs are off, one possible reason is that a lot of these astronomical calculations need the time converted to UTC. So if you use local time, you'll be off by however many hours your time bias is. Additionally, the calcs in appear to be truncating to the current day, so any UTC time differences are tossed out (along with the current time), and thus you might end up with a rather large deviation of +/- 1 day.

Just thought I'd mention it in case you cared.
Gadgets Wiki GitHub More Gadgets...
User avatar
LazieWouters
Posts: 61
Joined: August 29th, 2021, 11:11 am

Re: Help with Lua script

Post by LazieWouters »

Is tomorrow the full moon? I thought it was today actually (20 Sep 2021).

I was wrong, the full moon started on the 20 Sep 2021 as you said. It was about 1% missing to be fully lit, but it's considered a full moon anyway. And looking at the site you indicated, positioning it exactly over my city, it also lacked a little bit to be 100% illuminated. So I think the original code is calculating correctly, but I'll follow up on the site in the next few weeks as well and see if there's any divergence.
Sem título.png
Now that I understand your code I made this small change to get the date for the next few days:

Code: Select all

local tomorrow = os.date('*t', os.time() + 60*60*24*1)
    Gday = tomorrow.day
    Gmonth = tomorrow.month
    Gyear = tomorrow.year
    CalcHijriDate( Gday , Gmonth , Gyear)
    SKIN:Bang('!SetOption', 'MtMoonShadeAfter1Day', 'ImageName',  '#@#\Images\\moon\\' .. Hdate .. '.png')

    local tomorrow2 = os.date('*t', os.time() + 60*60*24*2)
    Gday = tomorrow2.day
    Gmonth = tomorrow2.month
    Gyear = tomorrow2.year
    CalcHijriDate( Gday , Gmonth , Gyear)
    SKIN:Bang('!SetOption', 'MtMoonShadeAfter2Day', 'ImageName',  '#@#\Images\\moon\\' .. Hdate .. '.png')
Wow, it actually worked?
Yes! I'm multiplying by 2, 3 and so on. It's working.
Thank you very much.
You do not have the required permissions to view the files attached to this post.
User avatar
LazieWouters
Posts: 61
Joined: August 29th, 2021, 11:11 am

Re: Help with Lua script

Post by LazieWouters »

It's a little polluted lol but it looks like it's working.
I'm going to follow up on the website you indicated over the next few days to see if the moon phase matches what's shown there.
Sem título.png
You do not have the required permissions to view the files attached to this post.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2604
Joined: March 23rd, 2015, 5:26 pm

Re: Help with Lua script

Post by SilverAzide »

Very nice! :thumbup:
Gadgets Wiki GitHub More Gadgets...