Python – Sun Position Calculator

HOME Forums Tech Software Talk Python – Sun Position Calculator

This topic contains 0 replies, has 1 voice, and was last updated by Ed P 2 weeks, 4 days ago.

  • Creator
  • #31428

    Ed P

    I was being plagued by Pigeons nesting under my solar panels, so I set up a Pi 3A and a powered speaker in my loft to play loud scary pigeon sounds – predators, shotgun blasts and pigeon wing flaps. As may be appreciated I did not want anything human-audible at night so I used Audacity to phase shift the sounds and required a program that knew when it was night, twilight etc. The Python libraries contain a beautiful little script called Astral that does all the hard grunt for you. Obviously this sort of program has utility if you need to switch on IR lights for your security camera etc.

    Although this example uses Python it is quite possible to call the Astral program from C/C++:


    I stripped the code for posting so feel free to modify and use for your own purposes.


    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Copyright 2019 ed <ed@Mint-Tara>
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; either version 2 of the License, or
    # (at your option) any later version.
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # GNU General Public License for more details.
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    # MA 02110-1301, USA.

    #Just one prerequisite:-
    # pip install astral
    # Astral documentation can be found on Github
    # If running via SSH and want to fire and forget, do not forget to use nohup

    from astral import *
    import datetime
    from datetime import time
    import time as t
    from time import sleep

    import subprocess
    from subprocess import check_output
    import sys, types, os

    # Add some required defs

    def Sun_period(tdata):
    caldate = tdata.year
    thour = tdata.hour
    tminute = tdata.minute
    a = Astral()
    location = a[‘London’]
    timezone = location.timezone
    d = tdata
    l = Location()
    # add edited location data = ‘Your Town’
    l.region = ‘England’
    l.timezone = ‘Europe/London’
    l.latitude = 5*.*****
    l.longitude = *.***** #Negative if west of Greenwich

    sun = location.sun(local=True, date=d)
    dusk_d = dusk_now = sun[‘dusk’]
    sunset_d = sun[‘sunset’]
    dawn_d = sun[‘dawn’]
    sunrise_d = sun[‘sunrise’]
    noon_d = sun[‘noon’]

    moon_phase = a.moon_phase(date=d)

    # All this casting required as Python does not allow objects with/wothout time zone to be compared.
    # after this everything is a time object
    dawn_thms = time(dawn_d.hour, dawn_d.minute, dawn_d.second)
    sunrise_thms = time(sunrise_d.hour, sunrise_d.minute, sunrise_d.second)
    dusk_thms = time(dusk_d.hour, dusk_d.minute, dusk_d.second)
    sunset_thms = time(sunset_d.hour, sunset_d.minute, sunset_d.second)
    noon_thms = time(noon_d.hour, noon_d.minute, noon_d.second)
    thms = time(d.hour, d.minute, d.second)

    if (((thms >=dawn_thms) and (thms<=sunrise_thms)) or ((thms>=sunset_thms) and (thms<=dusk_thms))):
    tperiod = ‘Twilight’ # Twilight action
    if ((thms>dusk_thms) or (thms<dawn_thms)):
    tperiod = ‘Night’ # Night action
    if ((thms>sunrise_thms) and (thms<sunset_thms)):
    tperiod = ‘Daylight’ #

    return tperiod

    def main(args):
    # A trivial program loop that runs for ever and allows different actions based
    # on the position of sun
    while True:
    d =
    today =
    one_day = datetime.timedelta(days=1)
    offsethour = datetime.timedelta(hours=1)
    yesterday = today – one_day
    tomorrow = today + one_day
    hourearlier = d – offsethour
    hourlater = d + offsethour

    tperiod = Sun_period(d)
    nperiod = Sun_period(hourlater)
    pperiod = Sun_period(hourearlier)
    print (‘The Current Period is:’, tperiod)

    if ((tperiod==’Night’) and (nperiod == ‘Night’)):“mpg123 ./Sounds/NightSound.mp3”, shell=True)
    if (tperiod==’Twilight’):“mpg123 ./Sounds/TwilightSound.mp3”, shell=True)
    if (tperiod == ‘Daylight’):
    # may need to check time to sunset
    R = randint(1,8)
    S = randint(30,120)
    if R== 1:
    # could be a random action“mpg123 ./Sounds/DaySounds.mp3”, shell=True)

    if R== 2: etc

    return 0

    if __name__ == ‘__main__’:
    import sys



Spread the love

You must be logged in to reply to this topic.

Spread the love