karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Sun up in the sky?

Mon Sep 12, 2016 7:44 am

Hello

A question for astronomy types, you know how during the year the sun moves across the sky, appearing earlier in the day and further to the east in the summer than it does during the winter. does this have a name?

What I want to do is write a python script to have the Pi take a photo with a webcam every day for a few weeks, capturing autumnal hues, with the sun in the same position in each photo, this is for a timelapse video. Taking the photo at the same time each day would have the sun moving left, or right, across the sky. So if I start on day one at 10:00, then day two might be 10:02, day three: 10:04 etc.

I've found a couple of python modules:
PyEphem: http://rhodesmill.org/pyephem/tutorial.html
PySolar: https://pysolar.readthedocs.io/en/latest/ (looks to be more to do with sun activity)
astral: https://pypi.python.org/pypi/astral/0.5

The first two have clearly been written by astronomers and use that special astronomer speak. Astral gives the sunrise, perhaps I could do a thing with that?

Thanks
Karl.

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Sun up in the sky?

Mon Sep 12, 2016 7:53 am

this may be worth a checkout
http://www.risacher.org/sunwait/
it's not python but you get the idea
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
karrika
Posts: 1124
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Sun up in the sky?

Mon Sep 12, 2016 8:21 am

The position of the sun is very regular. It moves 360 degrees around you in 24 hours. As you are only interested in pictures where the sun is close to the horizon the actual control of camera movement should be trivial as one axis of rotation is enough. At noon the sun is directly to the south if you live in the northern hemisphere.

The big question is when to press the trigger for the best photo. It may not be the exact time of sunset. Usually a few minutes before you get much nicer photos.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sun up in the sky?

Mon Sep 12, 2016 8:23 am

Are you aiming to take your photo at sunset, civil twilight (6°), naval twilight (12°) or astronomical twilight (18°)? Those times will move with the seasons because of the 23.4° inclination in the Earth's axis.

http://rhodesmill.org/pyephem/rise-set.html has lots of details and some pyephem code.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: Sun up in the sky?

Mon Sep 12, 2016 9:00 am

I don't want to photograph the sun, I want to photo the trees doing the traditional autumn thing, going brown and dropping leaves eventually making a timelapse video.

The best time at this time of year would be around 3pm, with the sun in the West, with the sun full on the trees but out of the camera.

My first post was about the sun being in the east, rising later in the day. In the west it would be setting earlier. I would like to have the sun in the same vertical position in each daily picture.

So, I give a start time: 12 September 2016 at 15:00 and the program works out the daily future times from the starting point.

Thanks
Karl.

User avatar
Burngate
Posts: 6303
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Sun up in the sky?

Mon Sep 12, 2016 9:51 am

karl101 wrote:A question for astronomy types, you know how during the year the sun moves across the sky, appearing earlier in the day and further to the east in the summer than it does during the winter. does this have a name?
Karl.
Yes - Analemma
https://en.wikipedia.org/wiki/Analemma

And as you can see it's rather complicated - a combination of the Earth's axial tilt, causing a (vertical) figure-of-eight movement, and the non-circularity of its orbit, causing a side-to-side movement.

You can account for the side-to-side part by adjusting the time of exposure, but you're stuck with the vertical bit, so shadows will grow / shrink during the year (if you're above the Arctic circle, you may have trouble for up to six months of the year!)

User avatar
karrika
Posts: 1124
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Sun up in the sky?

Mon Sep 12, 2016 9:54 am

Sounds like a very nice idea. Good luck with the math.

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: Sun up in the sky?

Mon Sep 12, 2016 11:25 am

Burngate wrote: Yes - Analemma
https://en.wikipedia.org/wiki/Analemma
Excellent, thank you. Turns out it doesn't really help with my quest, but its good to know.

Karl.

User avatar
Burngate
Posts: 6303
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Sun up in the sky?

Mon Sep 12, 2016 3:33 pm

Most things I know turn out to be useless. I like knowing them though!

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Sun up in the sky?

Mon Sep 12, 2016 8:19 pm

Altitude will get you close.
Look at the example on the pysolar page https://pysolar.readthedocs.io/en/latest/

Get the Suns altitude for your location now and compare it to the starting altitude.

example

Code: Select all

from pysolar.solar import *
import datetime
import time
months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# your location
lat = 0
lon = 0

# with-in +- this number
delta_range = .5

#start time
year = 2016
month = 9
day = 1
hour = 15
min = 0
sec = 0

# get our starting point
start_date = datetime.datetime(year, month, day, hour, min, sec)
start_altitude = get_altitude(lat, lon, start_date)


while True:
    # The time it is now
    now_date = datetime.datetime(year, month, day, hour, min, sec)
    # now_date = datetime.datetime.now()

    # altitude of the Sun now
    now_altitude = get_altitude(lat, lon, now_date)
    
    # get the azimuth so we can know which horizon it is over
    azimuth = get_azimuth(lat, lon, now_date)
    
    delta = start_altitude - now_altitude
    
    # after noon azimuth is 0 to -90 deg
    # delta with-in -range +range
    if  azimuth >= -90 and (-delta_range <= delta <= delta_range):
        # take a photo
        # sleep for 20 hours
        # time.sleep(72000)
        
        # the reset is to simulate the different times and dates
        print (start_date, start_altitude, now_date, now_altitude, delta)
        day += 1
        hour = 0
        min = 0
    
    # the reset is to simulate the different times and dates
    min += 1
    if min == 60:
        min = 0
        hour += 1
    
    if hour == 24:
        hour = 0
        day += 1
    
    if day > months[month]:
        day = 1
        month += 1
    
    if month == 13:
        month = 1
        year += 1
Also keep in mind that the further you get into winter the lower on the horizon the Sun is. For example in my location on Sept. 1 at 15:00 the Sun was at 50.6 deg, after Oct 7 the Sun is below 50 deg all day.

And the Sun's azimuth will change every day, so it will never be the exact same position.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Sun up in the sky?

Mon Sep 12, 2016 8:45 pm

What you are looking for is the "equation of time"
wiki: https://en.wikipedia.org/wiki/Equation_of_time
Image
It is the discrepancy between a clock and a sundial.
>)))'><'(((<

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: Sun up in the sky?

Wed Sep 14, 2016 7:19 pm

Hello
some code I've written, based around that provided by tom.slick (thanks tom!), but using pyephem. Its written for python3

As in tom's code it works out the altitude at a given location and time, and matches the altitude to subsequent days to give a time when the sun is going to be in the same place. Unless anyone sees a glaring error, I'll build on this, adding in a sqlite database and a camera shutter control running of the gpio pins. Not got too long before autumn starts.

Code: Select all

#! /usr/bin/python3

import datetime
import ephem     # pip install pyephem
import pytz      # pip install pytz
import tzlocal   # pip install tzlocal

def toUTC(d):
    d = d.datetime()
    d = d.replace(tzinfo=pytz.timezone('UTC'))
    d = d.replace(microsecond=0)
    return d

def findSunTime(h, noon, sunset, startAltitude):

    tolerance = .002
    dd = toUTC(h.date)
    nH = int(noon.strftime("%H"))
    nM = int(noon.strftime("%M"))
    dd = dd.replace(hour=nH, minute=nM)  # set the start time to noon

    # loop through each minute from noon until sunset until we hit
    # a tolerance match for the altitude
    while dd < sunset:
        h.date = dd
        delta = float(startAltitude) - float(ephem.Sun(h).alt)
        if (-tolerance <= delta <= tolerance):
             break
        dd += datetime.timedelta(seconds=30)
    return h

# your location
lat = 53.617865
lon = -2.165194
ele = 127

# start time
hh = 14
mm = 30

# get our starting point
startDate = datetime.datetime.now(pytz.timezone('UTC'))
startDate = startDate.replace(hour=hh, minute=mm, second=0)
endDate = startDate + datetime.timedelta(days=10)  # number of days to generate data for 
nowDate = startDate
print("Start Date : %s" % startDate.strftime("%Y-%m-%d %H:%M:%S %Z%z"))

here = ephem.Observer()
here.date = startDate
here.lat = str(lat)
here.lon = str(lon)
here.elevation = ele

startAzimuth = ephem.Sun(here).az
startAltitude = ephem.Sun(here).alt
print ("Start Altitude/Azimuth: %s (%s) / %s (%s)" % (startAltitude, float(startAltitude), startAzimuth, float(startAzimuth)))

while nowDate < endDate:

    # get sun times for the day and convert to a dateime object with timezone info (UTC)
    sunrise = toUTC(here.previous_rising(ephem.Sun()))  # Sunrise
    noon = toUTC(here.next_transit(ephem.Sun(), start=sunrise))  # Solar noon
    sunset = toUTC(here.next_setting(ephem.Sun()))  # Sunset

    newPosition = findSunTime(here, noon, sunset, startAltitude)
    np = toUTC(newPosition.date)  # converts into dateime object with UTC timezone
    np = np.strftime("%Y-%m-%d %H:%M:%S %Z")
    az = ephem.Sun(newPosition).az

    alt = ephem.Sun(newPosition).alt
    print ("%s %s (%s) / %s (%s)" % (np, alt, float(alt), az, float(az)))

    nowDate += datetime.timedelta(days=1)
    here.date = nowDate
Thanks
Karl

Return to “Python”