Math problem, macro to calculate hours of daylight per day

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Math problem, macro to calculate hours of daylight per day

Post by Irrlicht »

I'm trying to write a macro to calculate the hours (or better, minutes) of daylight per day at any given date of the year, at any given latitude.
Of course, I don't want it to be the most accurate daylight-calculator ever, it's for a game, and I'm alright with writing it within loose conventional values.
1) I'm not taking into account leap years, nor DST, nor wariations due to longitude or other factors.
2) I've set equinoxes and solstices at fixed dates: Spring eq = 3/21; Summer sol = 6/21; Autumn eq = 9/22; Winter sol = 12/20.
3) I've gathered approximative data about daylight times at equinoxes and solstices at latitude 0 (equator), 30, 60, and 90 (either pole, I don't differentiate between north and south, assuming it's exactly the same for both), which are the four groups of four variables being set you can see at the bottom of the macro.

This is the macro now, with no other calculation than chosen date (chosen by the user of the macro) and equinoxes/solstices dates:
Spoiler

Code: Select all

[h: abort(input(
"month|1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12|Month|LIST",
"day|1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31|Day|LIST",
"Latitude|45|Latitude (approx.)"
))]

[h: month = month + 1]
[h: day = day + 1]

[h: daysInMonth1 = 31]
[h: daysInMonth2 = 28]
[h: daysInMonth3 = 31]
[h: daysInMonth4 = 30]
[h: daysInMonth5 = 31]
[h: daysInMonth6 = 30]
[h: daysInMonth7 = 31]
[h: daysInMonth8 = 31]
[h: daysInMonth9 = 30]
[h: daysInMonth10 = 31]
[h: daysInMonth11 = 30]
[h: daysInMonth12 = 31]

[h: dayOfSpringEq = daysInMonth1 + daysInMonth2 + 21]
[h: dayOfSummerSol = daysInMonth1 + daysInMonth2 + daysInMonth3 + daysInMonth4 + daysInMonth5 + 21]
[h: dayOfAutumnEq = daysInMonth1 + daysInMonth2 + daysInMonth3 + daysInMonth4 + daysInMonth5 + daysInMonth6 + daysInMonth7 + daysInMonth8 + 22]
[h: dayOfWinterSol = daysInMonth1 + daysInMonth2 + daysInMonth3 + daysInMonth4 + daysInMonth5 + daysInMonth6 + daysInMonth7 + daysInMonth8 + daysInMonth9 + daysInMonth10 + daysInMonth11 + 20]

[h: selectedDate = day]
[h, if(month > 1): selectedDate = selectedDate + daysInMonth1]
[h, if(month > 2): selectedDate = selectedDate + daysInMonth2]
[h, if(month > 3): selectedDate = selectedDate + daysInMonth3]
[h, if(month > 4): selectedDate = selectedDate + daysInMonth4]
[h, if(month > 5): selectedDate = selectedDate + daysInMonth5]
[h, if(month > 6): selectedDate = selectedDate + daysInMonth6]
[h, if(month > 7): selectedDate = selectedDate + daysInMonth7]
[h, if(month > 8): selectedDate = selectedDate + daysInMonth8]
[h, if(month > 9): selectedDate = selectedDate + daysInMonth9]
[h, if(month > 10): selectedDate = selectedDate + daysInMonth10]
[h, if(month > 11): selectedDate = selectedDate + daysInMonth11]

[h: minutesInADay = 1440]

[h: lat0MinOfLightSpringEq = 726]
[h: lat0MinOfLightSummerSol = 727]
[h: lat0MinOfLightAutumnEq = 726]
[h: lat0MinOfLightWinterSol = 727]

[h: lat30MinOfLightSpringEq = 726]
[h: lat30MinOfLightSummerSol = 845]
[h: lat30MinOfLightAutumnEq = 727]
[h: lat30MinOfLightWinterSol = 652]

[h: lat60MinOfLightSpringEq = 730]
[h: lat60MinOfLightSummerSol = 1132]
[h: lat60MinOfLightAutumnEq = 732]
[h: lat60MinOfLightWinterSol = 612]

[h: lat90MinOfLightSpringEq = 742]
[h: lat90MinOfLightSummerSol = 1304]
[h: lat90MinOfLightAutumnEq = 738]
[h: lat90MinOfLightWinterSol = 225]

 
So, now I have the fixed intervals of time and latitude, knowing that at the designated points, minutes of light have to be equal to those values.
I tried to imagine a cartesian plane where X is the number of the days and Y the number of the latitude, with the fixed daylight values as points on the plane, but in the end, after much struggling, I only ended up with my head all messed up.
I'm no mathematician, and I need the help of someone who's better than me at math (and has a fresh mind about this thing).
How can I calculate the number of minutes of daylight at any given date and latitude that is not one of the fixed values?
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Math problem, macro to calculate hours of daylight per d

Post by aliasmask »

If you want to keep it simple an linear. Assume X is morning light and Y is night. When the days get longer (after winter solstice) you can do X -50s and Y +50s and do the opposite direction. You could do it as a minute difference making it about a 6 hour difference in light between solstices. So, 5am to 9pm the longest and 8am to 6pm the shortest with a 360 day year.

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Math problem, macro to calculate hours of daylight per d

Post by Irrlicht »

Mmmh, that's a bit too simple, and doesn't account for latitude.

I had thought about getting the time intervals (in days) between Winter sol & Spring eq, Spring Eq & Summer Sol, and so on, and then isolating the chosen date within the appropriate time intarval and convert it into a percentile (for example, if the date is between Spring Eq & Summer Sol, and the total interval between the two is 92 days, 92 = 100%; if the chosen date is the 46th day of that interval, 46 = 50%).
Then the same with latitude intervals, so I ended with two percentiles, one for date variation, the other for latitude variation, but I can't get them in proper relation into a formula that will result in values consistent with the values for all the solstices and equinoxes at latitude 0, 30, 60 and 90.
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Math problem, macro to calculate hours of daylight per d

Post by aliasmask »

So, you want to know how much daylight any part of the globe gets? Is this for a scifi game? Time zones weren't invented until the late 1800s. Time was usually measured in sunrise, sunset and high noon. If you want to be that detailed, then I suggest using actual math.

http://mathforum.org/library/drmath/view/56478.html

Code: Select all

D = daylength
   L = latitude
   J = day of the year

   P = asin[.39795*cos(.2163108 + 2*atan{.9671396*tan[.00860(J-186)]})]

                          _                                         _
                         / sin(0.8333*pi/180) + sin(L*pi/180)*sin(P) \
   D = 24 - (24/pi)*acos{  -----------------------------------------  }
                         \_          cos(L*pi/180)*cos(P)           _/
MT 1.4.0.3+ has the math functions.

User avatar
Irrlicht
Dragon
Posts: 426
Joined: Mon Feb 09, 2009 10:53 am

Re: Math problem, macro to calculate hours of daylight per d

Post by Irrlicht »

It's for a fantasy game, but I'm not looking for data related to time zones, only to latitude/date (which don't rely on covenience concepts, but are objective values of the daylight you have at the given day of the year at the given distance between equator and either pole).

I had seen that site, but it's overcomplicated for me. Anyway, I tried to translate that formula into MapTool, but "just translating" results in functions lacking parameters, it seems. Where can I see the sin, asin, cos, acos and tan functions? I would have sworn to have seen them before in the wiki, but now that I need to look them up, I don't see them.
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Math problem, macro to calculate hours of daylight per d

Post by aliasmask »

Yeah, I put the formula in to MapTool and it's not giving the variance I was expecting. I'll see if I can find another source. Changing the functions to use radians gives me a closer answer, but not quite there.

There's a really simple function for San Diego: H(t) = 2.4 sin (0.017t – 1.377) + 12 which actually gives me the correct answer, so I'm going to see where the two formulas diverge.

Code: Select all

[H: sdHours = 2.4 * math.sin_r(0.017 * dayOfYear - 1.377) + 12]

Post Reply

Return to “Macros”