It is currently April 19th, 2024, 7:01 pm

Sunrise-Moonset

Weather skins
User avatar
SilverAzide
Rainmeter Sage
Posts: 2604
Joined: March 23rd, 2015, 5:26 pm

Re: Sunrise-Moonset

Post by SilverAzide »

hardwired wrote: August 16th, 2022, 6:18 pm Never been this late to a party, but looking for ephemeris and astronomy Rainmeter skins using lua scripts instead of webparsers I found this and the planets lua from the Astronomy skin. The sun and moon lua's kindly posted (thank you) in this thread are almost perfect for my needs, except for the leading zero's in the hours. I've been trying to use a substitute to no avail, (but I'm by no means a Rainmeter scripting expert) and I looked around the lua file although I don't know anything about scripting and programming in general. There's this 'ToTime' function with a %02d in there, probably for the leading zeros for the minutes. I tried messing around, of course without luck. How do I change the lua to add zero's to the hours as well, for both the latest Moon and Sun lua's posted here?
For this skin, open the SunMoonTimes.lua file. Look for the function TimeString. Change it to this:

Code: Select all

function  TimeString( Ftime )  -- put string in Hrs : mins format - 24h version
		local hours = math.floor(Ftime)
		local minutes = math.floor( (Ftime- hours)* 60)
		--if hours>11 and hours<24 then AmPm=' pm' else AmPm=' am' end
		--if hours>0 then	hours=((hours+ 12 -1)% 12+ 1) end	
		--return hours..":"..twoDigitsFormat(minutes)..AmPm
		
		return twoDigitsFormat(hours)..":"..twoDigitsFormat(minutes)
end -- function TimeString
This will change the output to 24H format (HH:MM), if that is what you are asking for (you weren't completely clear). If you are simply wanting zero padded hours in 12H format, then just change the "return" line, like this:

Code: Select all

function  TimeString( Ftime )  -- put string in Hrs : mins format - 12h version
		local hours = math.floor(Ftime)
		local minutes = math.floor( (Ftime- hours)* 60)
		if hours>11 and hours<24 then AmPm=' pm' else AmPm=' am' end
		if hours>0 then	hours=((hours+ 12 -1)% 12+ 1) end	
		return twoDigitsFormat(hours)..":"..twoDigitsFormat(minutes)..AmPm
end -- function TimeString
Gadgets Wiki GitHub More Gadgets...
hardwired
Posts: 12
Joined: August 16th, 2022, 5:59 pm

Re: Sunrise-Moonset

Post by hardwired »

Thanks a lot SilverAzide, I'm looking for leading zeros in 24 hour format.
User avatar
balala
Rainmeter Sage
Posts: 16145
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Sunrise-Moonset

Post by balala »

hardwired wrote: August 18th, 2022, 6:28 pm Thanks a lot SilverAzide, I'm looking for leading zeros in 24 hour format.
This time I am the one being a little bit late, but have been in a short vacation, so even thought I saw your posts, couldn't reply to them properly.
First question is which skin are you exactly using, because there have been uploaded more of them over time.
If you're using my skin, posted here, you have more possibilities. For instance to get the Sunrise and Sunset with leading zeros, you have to modify the TimeSunrise and TimeSunset functions of the Sun.lua script file this way:

Code: Select all

-- compute the times of Sunrise
function TimeSunrise(Longitude, Latitude, MTSR, DS, ET)
	local TSR
	TSR = MTSR-(180/math.pi)*math.asin(math.tan(DS*math.pi/180)*math.tan(Latitude*math.pi/180))

	TSR = 24*TSR/360+TimezoneOffset
	local SunRiseHour = math.floor(TSR)
	local SunRiseMinutes = math.floor(60*(TSR-math.floor(TSR)))
	if (SunRiseHour <= 9) then
		SKIN:Bang('!SetVariable', 'TimeSR', '0'..SunRiseHour..':'..SunRiseMinutes)
	else
		SKIN:Bang('!SetVariable', 'TimeSR', SunRiseHour..':'..SunRiseMinutes)
	end
end

-- compute the times of Sunset
function TimeSunset(Longitude, Latitude, MTSS, DS, ET)
	local TSS
	TSS = MTSS+(180/math.pi)*math.asin(math.tan(DS*math.pi/180)*math.tan(Latitude*math.pi/180))

	TSS = 24*TSS/360+TimezoneOffset
	local SunSetHour = math.floor(TSS)
	local SunSetMinutes = math.floor(60*(TSS-math.floor(TSS)))
	if (SunSetHour <= 9) then
		SKIN:Bang('!SetVariable', 'TimeSS', '0'..SunSetHour..':'..SunSetMinutes)
	else
		SKIN:Bang('!SetVariable', 'TimeSS', SunSetHour..':'..SunSetMinutes)
	end
end
To do the same for the Moonrise and Moonset, you have to alter the test_moon function of the Moon.lua script this way:

Code: Select all

function test_moon(k, t0, lat, plx)

    ha = {0,0,0}
    local a, b, c, d, e, s, z
    local hr, _min, _time
    local az, hz, nz, dz

    if (RAn[3] < RAn[1]) then
		RAn[3] = RAn[3] + 2 * M_PI
	end

    ha[1] = t0 - RAn[1] + (k * K1)
    ha[3] = t0 - RAn[3] + (k * K1) + K1

    ha[2] = (ha[3] + ha[1]) / 2	-- hour angle at half hour
    Dec[2] = (Dec[3] + Dec[1]) / 2	-- declination at half hour

    s = math.sin(DR * lat)
    c = math.cos(DR * lat)

    -- refraction + sun semidiameter at horizon + parallax correction
    z = math.cos(DR * (90.567 - 41.685 / plx))

    if (k <= 0) then			-- first call of function
		VHz[1] = s * math.sin(Dec[1]) + c * math.cos(Dec[1]) * math.cos(ha[1]) - z
	end
	
    VHz[3] = s * math.sin(Dec[3]) + c * math.cos(Dec[3]) * math.cos(ha[3]) - z

    if (sgn(VHz[1]) == sgn(VHz[3])) then
		return VHz[3]		-- no event this hour
	end
	
    VHz[2] = s * math.sin(Dec[2]) + c * math.cos(Dec[2]) * math.cos(ha[2]) - z

    a = 2 * VHz[3] - 4 * VHz[2] + 2 * VHz[1]
    b = 4 * VHz[2] - 3 * VHz[1] - VHz[3]
    d = b * b - 4 * a * VHz[1]

    if (d < 0) then
		return VHz[3]		-- no event this hour
	end
    
	d = math.sqrt(d)
    e = (-b + d) / (2 * a)

    if ((e > 1) or (e < 0)) then
		e = (-b - d) / (2 * a)
	end

    _time = k + e + 1 / 120	-- time of an event + round up
    hr = math.floor(_time)
    _min = math.floor((_time - hr) * 60)

    hz = ha[1] + e * (ha[3] - ha[1])	-- azimuth of the moon at the event
    nz = -math.cos(Dec[2]) * math.sin(hz)
    dz = c * math.sin(Dec[2]) - s * math.cos(Dec[2]) * math.cos(hz)
    az = math.atan2(nz, dz) / DR
    if (az < 0) then
		az = az + 360
	end

    if ((VHz[1] < 0) and (VHz[3] > 0)) then
		if (hr <= 9) then
			Rise_time[1] =  '0'..hr
		else
			Rise_time[1] =  hr
		end
		Rise_time[2] =  _min
		Rise_az = az
		Moonrise = 1
    end

    if ((VHz[1] > 0) and (VHz[3] < 0)) then
		if (hr <= 9) then
			Set_time[1] =  '0'..hr
		else
			Set_time[1] =  hr
		end
		Set_time[2] =  _min
		Set_az = az
		Moonset = 1
    end

    return VHz[3]

end
Hope this helps. But if you're using another skin, please let me know which one.
hardwired
Posts: 12
Joined: August 16th, 2022, 5:59 pm

Re: Sunrise-Moonset

Post by hardwired »

Thank you for your reply balala.

I'm using the 'Moon II DST' lua from here:
https://forum.rainmeter.net/viewtopic.php?t=15071#p85135
And Sun II lua from this post:
https://forum.rainmeter.net/viewtopic.php?t=15071#p85161

Both (probably, I guess) need this section edited, I can't seem to find any match to what you posted above.

Code: Select all

function ToTime(HH,MM)

	if tonumber(SKIN:GetVariable('12H'))>0 then --12H
		if MM>=60 then
			return AMPMFormat((HH+1),string.format('%02d','0'))
		else
			return AMPMFormat((HH),string.format('%02d',MM))
		end
	else -- 24H
		if MM>=60 then
			return tostring((HH+1)..':00')
		else
			return tostring(HH..':'..string.format('%02d',MM))
		end
	end

end
User avatar
balala
Rainmeter Sage
Posts: 16145
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Sunrise-Moonset

Post by balala »

hardwired wrote: September 5th, 2022, 5:00 pm Both (probably, I guess) need this section edited, I can't seem to find any match to what you posted above.
Into the Sun.lua script file of the Sun II package replace the following commands:

Code: Select all

	SKIN:Bang('!SetVariable _Rise "'..ToTime(Hours(riset),Minutes(riset))..'"')
	SKIN:Bang('!SetVariable _Set "'..ToTime(Hours(settm),Minutes(settm))..'"')
	SKIN:Bang('!SetVariable _Noon "'..ToTime(Hours(noont),Minutes(noont))..'"')
of the rscalc function as it follows:

Code: Select all

	if (riset < 12) then
		SKIN:Bang('!SetVariable', '_Rise', '0'..ToTime(Hours(riset),Minutes(riset)))
	else
		SKIN:Bang('!SetVariable', '_Rise', ToTime(Hours(riset),Minutes(riset)))
	end
	if (settm < 12) then
		SKIN:Bang('!SetVariable', '_Set', '0'..ToTime(Hours(settm),Minutes(settm)))
	else
		SKIN:Bang('!SetVariable', '_Set', ToTime(Hours(settm),Minutes(settm)))
	end
	if (noont < 12) then
		SKIN:Bang('!SetVariable', '_Noon', '0'..ToTime(Hours(noont),Minutes(noont)))
	else
		SKIN:Bang('!SetVariable', '_Noon', ToTime(Hours(noont),Minutes(noont)))
	end
For the moon, replace the test_moon function this way:

Code: Select all

function test_moon(k, t0, lat, plx)

    ha = {0,0,0}
    local a, b, c, d, e, s, z
    local hr, _min, _time
    local az, hz, nz, dz

    if (RAn[3] < RAn[1]) then
		RAn[3] = RAn[3] + 2 * M_PI
	end

    ha[1] = t0 - RAn[1] + (k * K1)
    ha[3] = t0 - RAn[3] + (k * K1) + K1

    ha[2] = (ha[3] + ha[1]) / 2	-- hour angle at half hour
    Dec[2] = (Dec[3] + Dec[1]) / 2	-- declination at half hour

    s = math.sin(DR * lat)
    c = math.cos(DR * lat)

    -- refraction + sun semidiameter at horizon + parallax correction
    z = math.cos(DR * (90.567 - 41.685 / plx))

    if (k <= 0) then			-- first call of function
		VHz[1] = s * math.sin(Dec[1]) + c * math.cos(Dec[1]) * math.cos(ha[1]) - z
	end
	
    VHz[3] = s * math.sin(Dec[3]) + c * math.cos(Dec[3]) * math.cos(ha[3]) - z

    if (sgn(VHz[1]) == sgn(VHz[3])) then
		return VHz[3]		-- no event this hour
	end
	
    VHz[2] = s * math.sin(Dec[2]) + c * math.cos(Dec[2]) * math.cos(ha[2]) - z

    a = 2 * VHz[3] - 4 * VHz[2] + 2 * VHz[1]
    b = 4 * VHz[2] - 3 * VHz[1] - VHz[3]
    d = b * b - 4 * a * VHz[1]

    if (d < 0) then
		return VHz[3]		-- no event this hour
	end
    
	d = math.sqrt(d)
    e = (-b + d) / (2 * a)

    if ((e > 1) or (e < 0)) then
		e = (-b - d) / (2 * a)
	end

    _time = k + e + 1 / 120	-- time of an event + round up
    hr = math.floor(_time)
    _min = math.floor((_time - hr) * 60)

    hz = ha[1] + e * (ha[3] - ha[1])	-- azimuth of the moon at the event
    nz = -math.cos(Dec[2]) * math.sin(hz)
    dz = c * math.sin(Dec[2]) - s * math.cos(Dec[2]) * math.cos(hz)
    az = math.atan2(nz, dz) / DR
    if (az < 0) then
		az = az + 360
	end

    if ((VHz[1] < 0) and (VHz[3] > 0)) then
		if (hr < 12) then
			Rise_time[1] =  '0'..hr
		else
			Rise_time[1] =  hr
		end
		Rise_time[2] =  _min
		Rise_az = az
		Moonrise = 1
    end

    if ((VHz[1] > 0) and (VHz[3] < 0)) then
		if (hr < 12) then
			Set_time[1] =  '0'..hr
		else
			Set_time[1] =  hr
		end
		Set_time[2] =  _min
		Set_az = az
		Moonset = 1
    end

    return VHz[3]

end
If you're interested, the difference is quite to the end, the Rise_time[1] = hr and Set_time[1] = hr commands being replaced by two if functions.
Please test this and let me know if you got the leading zeros.
hardwired
Posts: 12
Joined: August 16th, 2022, 5:59 pm

Re: Sunrise-Moonset

Post by hardwired »

Thank you so much for this balala!

I'm going to give it a try asap, hopefully this weekend. I will let you know how it worked out :thumbup:
User avatar
balala
Rainmeter Sage
Posts: 16145
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Sunrise-Moonset

Post by balala »

hardwired wrote: September 14th, 2022, 6:10 pm Thank you so much for this balala!

I'm going to give it a try asap, hopefully this weekend. I will let you know how it worked out :thumbup:
Alright, I'm curious if it works as expected.
hardwired
Posts: 12
Joined: August 16th, 2022, 5:59 pm

Re: Sunrise-Moonset

Post by hardwired »

It works perfectly!

How beautiful, ephemeris with leading zeros and 100% online parsing free. Thank you balala, thank you original lua script writers, this skin will remain on my desktop till the day I stop using windows (and then I might try to port it to conky) haha :great:
User avatar
balala
Rainmeter Sage
Posts: 16145
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Sunrise-Moonset

Post by balala »

hardwired wrote: September 20th, 2022, 5:45 pm How beautiful, ephemeris with leading zeros and 100% online parsing free. Thank you balala, thank you original lua script writers, this skin will remain on my desktop till the day I stop using windows (and then I might try to port it to conky) haha :great:
You're welcome. I'm glad if you (or anyone) find useful my skins / codes. However note that the script files are not mine, any of the two. I also got them, but to be honest I don1t exactly know where have I downloaded them from. There are some information in both .lua script files, however as said, am not sure where have I downloaded them from. Anyway, I remember that when I found out there the skin(s) using the original form of these files (I altered them over time, however the basic didn't change), I were fascinated from the first moment not to parse online the information, but to compute them locally.
hardwired
Posts: 12
Joined: August 16th, 2022, 5:59 pm

Re: Sunrise-Moonset

Post by hardwired »

balala wrote: September 20th, 2022, 7:43 pmI were fascinated from the first moment not to parse online the information, but to compute them locally.
Exactly that.
So... are you into astronomy? Because I also use the planets.lua from this skin:

https://www.deviantart.com/xenium-art/art/Astronomy-UPDATED-25-JAN-2021-781114590

...and it also doesn't support leading zero's, although it can be fixed by using a dirty substitute in the calculation. However there's another quirk (or even more I have to discover), there's this nasty little 1/60 chance it will calculate a false time, e.g. "XX:60". For instance, yesterday Jupiter rose at 19:60 at my location, a few weeks ago Mars rose at 23:60. I "solved" this by adding another sub, "60":"59", but I feel it's dirty and inaccurate.

You in for another challenge? :-)