Calculating the day length

556 views
Skip to first unread message

ramw...@uk2.net

unread,
Oct 31, 2014, 10:28:55 AM10/31/14
to weewx...@googlegroups.com
This is probably another question because I know nothing (almost) about Python.

I have sunrise & sunset, how can I calculate the day length from them?  Obviously daylength = sunset - sunrise, but the syntax?  This is what I have tried and it doesn't work!

#set $daylength=$almanac.sun.set - $almanac.sun.rise
               
<tr>
                 
<td class="label">Day Length:</td>
                 
<td class="data">$daylength</td>
                </
tr>




Dave Webb KB1PVH

unread,
Oct 31, 2014, 10:33:01 AM10/31/14
to weewx...@googlegroups.com

I was just thinking about this too. Not that I'm a fan of keeping track of losing daylight this time of year, but always good in the spring. I'll be very interested in this.

Dave-KB1PVH

Sent from my Samsung S4

Andrew Milner

unread,
Oct 31, 2014, 11:28:13 AM10/31/14
to weewx...@googlegroups.com
try $almanac.sun.set.raw - $almanac.sun.rise.raw

Dave Webb KB1PVH

unread,
Oct 31, 2014, 11:37:06 AM10/31/14
to weewx...@googlegroups.com

I just tried that and it displays

Day Length: 37424.7437022

Andrew Milner

unread,
Oct 31, 2014, 11:43:40 AM10/31/14
to weewx...@googlegroups.com
well format the daylength
$daylength.format("%H:%M") or formatted however you want it to be formatted.  Remember times are in seconds!  so your 37424 is about 10.?? hours

Dave Webb KB1PVH

unread,
Oct 31, 2014, 12:13:17 PM10/31/14
to weewx...@googlegroups.com

Andrew,

I'm not quite sure where to put that day length format line. I tried a couple different things and it tossed errors and another try made the format line show up as typed instead of giving a value. I know the best answer is probably learn this stuff for myself, and at some point in the near future I would like to.

Andrew Milner

unread,
Oct 31, 2014, 12:29:12 PM10/31/14
to weewx...@googlegroups.com
#set $daylength=$almanac.sun.set.raw - $almanac.sun.rise.raw

                
<tr>
                  
<td class="label">Day Length:</td>
                                <td class="data">$daylength.format("%H:%M")</td>
                              </tr>

Dave Webb KB1PVH

unread,
Oct 31, 2014, 12:31:09 PM10/31/14
to weewx...@googlegroups.com

I'll give it a shot shortly.

Thank you.

Dave Webb KB1PVH

unread,
Oct 31, 2014, 12:50:22 PM10/31/14
to weewx...@googlegroups.com

Still displays wrong. I'll have to take some time and read more.

20141031_124344.jpg

Thomas Keffer

unread,
Oct 31, 2014, 1:05:57 PM10/31/14
to weewx-user
Won't work because $daylength is just a simple floating point value. Try (NOT TESTED):

#set $seconds = $almanac.sun.set.raw -  $almanac.sun.rise.raw
#set $hours = $seconds // 3600
#set $seconds %= 3600
#set $minutes = $seconds //60
#set $seconds %= 60
$("The length of daylight is %d hours, %d minutes, and %.1f seconds" % ($hours, $minutes, $seconds))

-tk



--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Milner

unread,
Oct 31, 2014, 1:56:40 PM10/31/14
to weewx...@googlegroups.com
What would we do without you??!!

tx - it worked

Dave Webb KB1PVH

unread,
Oct 31, 2014, 1:58:52 PM10/31/14
to weewx...@googlegroups.com

Haha, I was just going to report that it worked.

That Tom guy is not bad with this stuff, LOL.

Dave Webb KB1PVH

unread,
Oct 31, 2014, 2:11:46 PM10/31/14
to weewx...@googlegroups.com

Do I dare ask how we (you) could make it say something like

Tomorrow will be x:xx shorter (or longer, depending on time of year)

ramw...@uk2.net

unread,
Oct 31, 2014, 2:18:33 PM10/31/14
to weewx...@googlegroups.com
You can get today's daylight length in seconds, you can get tomorrows, subtract one from the other and apply the same hours minutes seconds thing to that result.

Dave Webb KB1PVH

unread,
Oct 31, 2014, 3:02:55 PM10/31/14
to weewx...@googlegroups.com

I understand how to do it in theory, I just lack the implementation skills to do it.

ramw...@uk2.net

unread,
Oct 31, 2014, 3:23:49 PM10/31/14
to weewx...@googlegroups.com
You have an example for today.

You can get tomorrow's daylength in seconds using sun.next_rising and sun.next_setting instead of sun.rise and sun.set.

Subtract the smaller value from the larger - which is which depends on if the days are getting longer or shorter.

I have a feeling that the hours difference will always be zero e.g. the seconds are less than 3600.

The minutes are the seconds divided by 60.

The seconds are the remainder which you can see being done in the example.

So, untested:
#set $today_secs= $almanac.sun.set.raw -  $almanac.sun.rise.raw
#set $hours = $today_secs// 3600
#set $today_secs %= 3600
#set $minutes = $today_secs//60
#set $today_secs %= 60
$
("The length of daylight today is %d hours, %d minutes, and %.1f seconds" % ($hours, $minutes, $today_secs))

#set $next_secs = $almanac.sun.next_setting.raw -  $almanac.sun.next_rising.raw
#set $hours = $next_secs// 3600
#set $next_secs %= 3600
#set $minutes = $next_secs//60
#set $next_secs %= 60
$
("The length of daylight tomorrow is %d hours, %d minutes, and %.1f seconds" % ($hours, $minutes, $next_secs))

#if $next_secs > $today_secs
   
#set $change=$next_secs-$today_secs
   
#set $text="Longer"
#else
   
#set $change=$today_secs - $next_secs
   
#set $text="Shorter"
#end if

#set $hours = $change// 3600
#set $change %= 3600
#set $minutes = $change//60
#set $change %= 60$("Tomorrow is %d hours, %d minutes, and %.1f seconds $text than today" % ($hours, $minutes, $next_secs))

Does that help?

ramw...@uk2.net

unread,
Oct 31, 2014, 3:24:40 PM10/31/14
to weewx...@googlegroups.com

Mine is now working (I think!) but I have found myself wondering if PyEphem offers a more elegent way of doing this?

Dave Webb KB1PVH

unread,
Oct 31, 2014, 3:29:37 PM10/31/14
to weewx...@googlegroups.com

It helps a great deal, thank you.

Hopefully soon I will be able to learn this stuff a little bit better. All my skills revolve around running into burning buildings when everyone else is running out for 23 years , this software and programming stuff is hard work.

ramw...@uk2.net

unread,
Oct 31, 2014, 3:37:08 PM10/31/14
to weewx...@googlegroups.com
Good.  While you have been running into burning buildings, I've been sitting at a PC writing code - obviously not Python, but code so I know in outline but not detail how to 'do things'.  There are lots of 'patterns' that recur in all programming languages though the syntax varies.

Glad someone was running into burning building though I've never been in one, so far, touch wood. 

gjr80

unread,
Oct 31, 2014, 4:17:01 PM10/31/14
to weewx...@googlegroups.com
Not wanting to rain on the parade but has anyone checked exactly what $almanac.sun.rise and $almanac.sun.next_rising provides? For example, at 9am I would expect you would get today's sunrise and to,or rows sunrise respectively. But at 3am would you in fact get today's sunrise and today's sunrise ie the same value? Not something I ever found in pyephem documentation and unfortunately away from my PC for a few days so can't do some simple tests to confirm one way or another.

Also, in weewx-wd we format day length as follows (sorry no acces to code tags on iPad):

#import time
#set $dayLengthSeconds = $time.gmtime(round($almanac.sun.set.raw-$almanac.sun.rise.raw,1))

and to display day length:

$time.strftime("%M:%S", $dayLengthSeconds)

Gary

Thomas Keffer

unread,
Oct 31, 2014, 4:26:37 PM10/31/14
to weewx-user
I don't think that's going to work because "next_rising" will be tomorrow, but "next_setting" will be today. During daylight hours, of course.

Fortunately, the weewx encapsulation of PyEphem is pretty flexible. The trick is to use the undocumented parameter "almanac_time":

#set $now = $current.dateTime.raw
#set $tomorrow = $now + 24*3600
#set $today_daylight = $almanac.sun.set.raw -  $almanac.sun.rise.raw
#set $tomorrow_daylight = $almanac($almanac_time=$tomorrow).sun.set.raw - $almanac($almanac_time=$tomorrow).sun.rise.raw
#set $difference = $tomorrow_daylight - $today_daylight
<p>The difference in daylight is $difference seconds.</p>

This might not work tomorrow, because DST ends!

-tk




--

Dave Vaughan

unread,
Oct 31, 2014, 4:36:36 PM10/31/14
to weewx...@googlegroups.com
Which just goes to show that nothing is easy.
The:

#set $seconds = $almanac.sun.set.raw -  $almanac.sun.rise.raw
#set $hours = $seconds // 3600
#set $seconds %= 3600
#set $minutes = $seconds //60
#set $seconds %= 60
$("The length of daylight is %d hours, %d minutes, and %.1f seconds" % ($hours, $minutes, $seconds))

Does not work either, well it returns something,Might even be close to right, but it is different every time you run it. Just wait 5 minutes between runs and look at the seconds.

Dave Vaughan

gjr80

unread,
Oct 31, 2014, 5:02:12 PM10/31/14
to weewx...@googlegroups.com
Darn, I should have said "%H:%M:%S" instead of "%M:%S", posting more than a few words via the ipad is a pain.

Gary

Thomas Keffer

unread,
Oct 31, 2014, 6:28:49 PM10/31/14
to weewx-user
I thought this was such a cool idea, I installed it on my website. The delta has held steady at 172.8 seconds for about an hour now.

Sigh. Losing almost 3 minutes of daylight every day...

-tk

gjr80

unread,
Oct 31, 2014, 6:46:54 PM10/31/14
to weewx...@googlegroups.com
Haha Tom, thanks for sending the extra seconds down here, though some must be getting lost somewhere as we are only gaining 90 seconds a day :)

Gary

Dave Webb KB1PVH

unread,
Oct 31, 2014, 6:48:11 PM10/31/14
to weewx...@googlegroups.com

Tom,

How would we get it to give the difference in minutes and seconds instead of seconds. I don't think I need the 9 places after the decimal for accuracy.

Thomas Keffer

unread,
Oct 31, 2014, 6:50:25 PM10/31/14
to weewx-user
Do the same thing as you did for converting length of daylight

$difference_minutes = $difference// 60
$difference %= 60

Where $difference is the difference in daylight in seconds.

-tk

--

Dave Webb KB1PVH

unread,
Oct 31, 2014, 6:59:49 PM10/31/14
to weewx...@googlegroups.com
I'll give it a try later, thanks.

Dave

Dave Webb KB1PVH

unread,
Oct 31, 2014, 8:26:39 PM10/31/14
to weewx...@googlegroups.com

I'm not sure where and how that goes, so I'll just leave well enough alone for now. I think I'll wait until the kids are 18 and kick them out so I can have more than 30 seconds to myself to sit down and learn this stuff.

ramw...@uk2.net

unread,
Nov 1, 2014, 4:12:53 AM11/1/14
to weewx...@googlegroups.com
Something like that is going on:















                <tr>
                  <td class="label">Start civil twilight:</td>
                  <td class="data">$almanac(horizon=-6).sun(use_center=1).rise</td>
                  <td class="data">$almanac(horizon=-6).sun(use_center=1).next_rising</td>
                </tr>
                <tr>
                  <td class="label">Rise:</td>
                  <td class="data">$almanac.sun.rise</td>
                  <td class="data">$almanac.sun.next_rising</td>
                </tr>
                <tr>
                  <td class="label">Transit:</td>
                  <td class="data">$almanac.sun.transit</td>
                  <td></td>
                </tr>
                <tr>
                  <td class="label">Set:</td>
                  <td class="data">$almanac.sun.set</td>
                  <td class="data">$almanac.sun.next_setting</td>
                </tr>
                <tr>
                  <td class="label">End civil twilight:</td>
                  <td class="data">$almanac(horizon=-6).sun(use_center=1).set</td>
                  <td class="data">$almanac(horizon=-6).sun(use_center=1).next_setting</td>
                </tr>

Gives:

Start civil twilight:06:37:4006:41:26
Rise:07:20:0607:22:15
Transit:11:56:31
Set:16:32:0616:32:06
End civil twilight:17:14:2817:12:42

               

Surely it's possible to construct the date for tomorrow with a time of 12:00?  However here in the UK we change the clocks at 02:00 which is after sunset and before the earliest sunrise in any part of the UK so I don't think the change from GMT to BST should cause a problem - the timing of the change doesn't mean all of a sudden it's tomorrow or yesterday..

I did find another issue - sun.next_transit gave errors - but I can use the technique you've shown above to get the sun's transit for tomorrow.

gjr80

unread,
Nov 2, 2014, 1:15:32 AM11/2/14
to weewx...@googlegroups.com
Hmm, interesting. Back home today so I sat down to look at using $almanac_time in my templates to calculate the difference in length of day between yesterday and today(as opposed to day and tomorrow - same concept though). My approach had been to use a search list extension to return an almanac object call $yestAlmanac. I knocked up a quick test report that I could play with and I found I had 3 different day lengths for today (let alone bringing 'yesterday' into the mix to work out a change of day). My online system using my SLE had 1 day length, my test system using the same SLE had something about 20 seconds longer and my test system using $almanac_time was about 20 seconds longer again. Change in day varied between 1min 30s to about 2min 15s so it was a significant difference. Eventually found that my test system (which runs the simulator) had outTemp around 13C and in reality its about 30C here now and of course there is a pressure difference too. Almanac uses both temp and pressure in its setup so when I added these to $almanac eg $almanac($almanac_time=xxx, $temp=ttt, $pressure=ppp) my SLE and the $almanac_time methods gave identical results, albeit still 20 odd seconds longer day length than my live system. Thinking the higher temp and pressure could account for that I found that increasing the temp that $alamanc uses by 10 deg C resulted in about a 12 sec longer day. So I guess that explains why I am seeing different day lengths.

I can understand that changing temp and pressure affects the optical properties of the atmosphere, and hence what we see when observing the sun, moon etc and the horizon; but it is doing my head in trying to understand why if it was 30C today why is the sunrise 12 sec earlier than if it was 10C.

I can't help feeling a sense of déjà vu here with earlier conversations about how to measure/quantify/describe wind properties...

Gary

Andrew Milner

unread,
Nov 2, 2014, 1:40:18 AM11/2/14
to weewx...@googlegroups.com
...have you booked your appointment with the shrink yet??  Sounds like you'll be needing one!!  While you're sending yourself insane you could always try rainrates as well!!

gjr80

unread,
Nov 2, 2014, 7:57:46 AM11/2/14
to weewx...@googlegroups.com
It seems that if I had dug a little further I would discovered some words in the Almanac section of the Customisation Guide re USNO time that would have given me the clue. A bit more reading revealed that sunrise and sunset are generally defined as when the top edge of the sun's disc is coincident with an ideal horizon under standard (or average) refraction. The USNO takes refraction into account by using the time when the centre of the sun is 50 arcminutes below the horizon, wikipedia talks of the top edge of the sun as being 34 arcminutes below the horizon. The Customisation Guide covers this perfectly and now with pressure=0, horizon =-(34.0/60) I now get sunrise/sunset/day length/change in day that appear correct and do not change through the day. It seems I have my sanity back :)

I guess one could maybe make a case that the sunrise and sunset times displayed on the default Weewx template page should include such settings, maybe not. The difference in only a handful of seconds which is insignificant when talking hours and minutes but for change of day which is a few handfuls of seconds it is significant.

Oh, and I have a Vantage so fortunately I will not suffer a similar fate with rainrate!

Gary

vigilancewx

unread,
Nov 2, 2014, 8:21:08 AM11/2/14
to weewx...@googlegroups.com

Hi

I wonder if anyone would be kind enough to share what you actually add to the index file

Ok the first posting  did not work but I can keep up with the where and how of

 #set $daylength=$almanac.sun.set - $almanac.sun.rise


               
<tr>
                 
<td class="label">Day Length:</td>


                 
<td class="data">$daylength</td>
                </
tr>

 

 

 

Moving on to

 ​#set $seconds = $almanac.sun.set.raw -  $almanac.sun.rise.raw

#set $hours = $seconds // 3600

 

#set $seconds %= 3600

 

#set $minutes = $seconds //60

 

#set $seconds %= 60

 

$("The length of daylight is %d hours, %d minutes, and %.1f seconds" % ($hours, $minutes, $seconds))

 

I tried adding a class label and class data but it just displayed text ($hours, $minutes, $seconds))

normally I can keep up with any little mods but  lost on this one dare say its obvious if I see the mod
What txt do I need to add to the tmpl file

 

Thanks for your help



On Friday, October 31, 2014 2:28:55 PM UTC, ramw...@uk2.net wrote:
This is probably another question because I know nothing (almost) about Python.

I have sunrise & sunset, how can I calculate the day length from them?  Obviously daylength = sunset - sunrise, but the syntax?  This is what I have tried and it doesn't work!

#set $daylength=$almanac.sun.set - $almanac.sun.rise

               
<tr>
                 
<td class="label">Day Length:</td>

                 
<td class="data">$daylength</td>
                </
tr>




Thomas Keffer

unread,
Nov 2, 2014, 8:37:01 AM11/2/14
to weewx-user
The almanac extensions take great care in getting this right, and giving you options. If you want to know "how much time between sunrise and sunset at my location," then the defaults are appropriate. If you want to know the theoretical daylight if there were no atmosphere, then you can do that. If you want to match the naval observatory, that's possible too.

Personally, I'm most interested in the first.

I can see the length-of-daylight changing a lot during the day if you have big diurnal heating. Here in the maritime Pacific Northwest, we have very little heating and near-constant temperatures this time of the year, and so my length of daylight is pretty steady. 

However, the difference in daylight. day-to-day, should hardly change at all, even with big heating.

-tk


 

--

Thomas Keffer

unread,
Nov 2, 2014, 8:40:35 AM11/2/14
to weewx-user
Here's what I have in my index.html.tmpl. I've excerpted a little bit around the additions so you can orient yourself. The additions are highlighted.

               #end if
                <tr>
                  <td class="label">Phase:</td>
                  <td class="data">$almanac.moon_phase<br/>($almanac.moon_fullness% full)</td>
                </tr>
              </table>
            </div> <!-- end class "celestial_body" -->
	    #set $now = $current.dateTime.raw
	    #set $yesterday = $now - 24*3600
	    #set $today_daylight = $almanac.sun.set.raw -  $almanac.sun.rise.raw
	    #set $yesterday_daylight = $almanac($almanac_time=$yesterday).sun.set.raw - $almanac($almanac_time=$yesterday).sun.rise.raw
	    #set $difference = $today_daylight - $yesterday_daylight
	    #set $seconds = $almanac.sun.set.raw - $almanac.sun.rise.raw
	    #set $hours = $seconds //3600
	    #set $seconds %= 3600
	    #set $minutes = $seconds//60
	    #set $seconds %= 60
	    <p>$("Today has %d hours, %d minutes, and %d seconds of daylight" % ($hours, $minutes, $seconds)),<br/>
	    #if $difference > 0
	    $("%d seconds more than yesterday." % $difference)</p>
            #else
	    $("%d seconds less than yesterday." % -$difference)</p>
	    #end if
	    #else
            ## No extended almanac information available. Fall back to a simple table.
            <table>
              <tr>
                <td class="label">Sunrise:</td>
                <td class="data">$almanac.sunrise</td>
              </tr>
  


Andrew Milner

unread,
Nov 2, 2014, 8:52:06 AM11/2/14
to weewx...@googlegroups.com
#set $seconds = $almanac.sun.set.raw - $almanac.sun.rise.raw
#set $hours = $seconds // 3600
#set $seconds %= 3600
#set $minutes = $seconds // 60
#set $seconds %= 60
                  <tr>
                    <td class="label">Day length:</td>
                    <td class="data">$("%d hrs, %d mins, %.1f secs" % ($hours, $minutes, $seconds))</td>
                  </tr>


--
You received this message because you are subscribed to a topic in the Google Groups "weewx-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/weewx-user/gc1urPvD_1M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to weewx-user+...@googlegroups.com.

gjr80

unread,
Nov 2, 2014, 8:52:27 AM11/2/14
to weewx...@googlegroups.com
I agree with all you say; almanac has it exactly right and yes I am concerned with how much time between sunrise and sunset. I guess where we diverge is that the definition of sunrise and sunset used by the likes of USNO, NWS (which defers to USNO), Australian BoM (which defers to Geoscience Australia) is that sunrise and sunset occurs when the top of the sun is actually 34' below the horizon and this is worked out in pyephem with pressure=0, horizon=-(34.0/60). Logically, change in day length and sunrise/sunset should not vary with temperature, which is why I did not understand my results to start with. It was only when I discovered the definition of sunrise and sunset that my error became apparent.

I feel I have hijacked the original question far too much and I will now be quiet.

Gary
Reply all
Reply to author
Forward
0 new messages