# Sunrise / set calculations

Filed under:
|
|
##### astronomy

I'm trying to calculate the sunset / rise times using python based on the link provided below.

My results done through excel and python do not match the real values. Any ideas on what I could be doing wrong?

My Excel sheet can be found under .. http://transpotools.com/sun_time.xls

``````# Created on 2010-03-28

# @author: dassouki
# @source: [http://williams.best.vwh.net/sunrise_sunset_algorithm.htm]
# @summary: this is based on the Nautical Almanac Office, United States Naval
# Observatory.

import math, sys

class TimeOfDay(object):

def calculate_time(self, in_day, in_month, in_year,
lat, long, is_rise, utc_time_zone):

# is_rise is a bool when it's true it indicates rise,
# and if it's false it indicates setting time

#set Zenith
zenith = 96
# offical      = 90 degrees 50'
# civil        = 96 degrees
# nautical     = 102 degrees
# astronomical = 108 degrees

#1- calculate the day of year
n1 = math.floor( 275 * in_month / 9 )
n2 = math.floor( ( in_month + 9 ) / 12 )
n3 = ( 1 + math.floor( in_year - 4 * math.floor( in_year / 4 ) + 2 ) / 3 )

new_day = n1 - ( n2 * n3 ) + in_day - 30

print "new_day ", new_day

#2- calculate rising / setting time
if is_rise:
rise_or_set_time = new_day + ( ( 6 - ( long / 15 ) ) / 24 )
else:
rise_or_set_time = new_day + ( ( 18 - ( long/ 15 ) ) / 24 )

print "rise / set", rise_or_set_time

#3- calculate sun mean anamoly
sun_mean_anomaly = ( 0.9856 * rise_or_set_time ) - 3.289
print "sun mean anomaly", sun_mean_anomaly

#4 calculate true longitude
true_long = ( sun_mean_anomaly +
( 1.916 * math.sin( math.radians( sun_mean_anomaly ) ) ) +
( 0.020 * math.sin(  2 * math.radians( sun_mean_anomaly ) ) ) +
282.634 )
print "true long ", true_long

# make sure true_long is within 0, 360
if true_long < 0:
true_long = true_long + 360
elif true_long > 360:
true_long = true_long - 360
else:
true_long

print "true long (360 if) ", true_long

#5 calculate s_r_a (sun_right_ascenstion)
s_r_a = math.degrees( math.atan( 0.91764 * math.tan( math.radians( true_long ) ) ) )
print "s_r_a is ", s_r_a

#make sure it's between 0 and 360
if s_r_a < 0:
s_r_a = s_r_a + 360
elif true_long > 360:
s_r_a = s_r_a - 360
else:
s_r_a

print "s_r_a (modified) is ", s_r_a

# s_r_a has to be in the same Quadrant as true_long
true_long_quad = ( math.floor( true_long / 90 ) ) * 90
s_r_a_quad = ( math.floor( s_r_a / 90 ) ) * 90

print "s_r_a (quadrant) is ", s_r_a

# convert s_r_a to hours
s_r_a = s_r_a / 15

print "s_r_a (to hours) is ", s_r_a

#6- calculate sun diclanation in terms of cos and sin
sin_declanation = 0.39782 * math.sin( math.radians ( true_long ) )
cos_declanation = math.cos( math.asin( sin_declanation ) )

print " sin/cos declanations ", sin_declanation, ", ", cos_declanation

# sun local hour
cos_hour = ( math.cos( math.radians( zenith ) ) -
( sin_declanation * math.sin( math.radians ( lat ) ) ) /
( cos_declanation * math.cos( math.radians ( lat ) ) ) )

print "cos_hour ", cos_hour

# extreme north / south
if cos_hour > 1:
print "Sun Never Rises at this location on this date, exiting"
# sys.exit()
elif cos_hour < -1:
print "Sun Never Sets at this location on this date, exiting"
# sys.exit()

print "cos_hour (2)", cos_hour

#7- sun/set local time calculations
if is_rise:
sun_local_hour =  ( 360 - math.degrees(math.acos( cos_hour ) ) ) / 15
else:
sun_local_hour = math.degrees( math.acos( cos_hour ) ) / 15

print "sun local hour ", sun_local_hour

sun_event_time = sun_local_hour + s_r_a - ( 0.06571 *
rise_or_set_time ) - 6.622

print "sun event time ", sun_event_time

#final result
time_in_utc =  sun_event_time - ( long / 15 ) + utc_time_zone

return time_in_utc

#test through main
def main():
print "Time of day App "
# test: fredericton, NB
long = 66.6
lat = -45.9
utc_time = -4
d = 3
m = 3
y = 2010
is_rise = True

tod = TimeOfDay()
print "TOD is ", tod.calculate_time(d, m, y, lat, long, is_rise, utc_time)

if __name__ == "__main__":
main()
``````

© Stack Overflow or respective owner

• #### unmet dependencies in Ubuntu 12.04

I tried today to install a dvb-card on my Ubuntu 12.04 (Linux blauhai-linux 3.2.0-25-generic #40-Ubuntu SMP Wed May 23 20:30:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux ). The installation failed with an error. After that, i tried to install python (it was already installed but i got this error): linux:~\$… >>> More

• #### How can I get sikuli-ide to work?

I installed sikuli-ide with sudo apt-get install sikuli-ide Everything was fine until I tried to start it from the terminal. I typed sikuli-ide But the only response I got was [info] locale: en_US The application was not started, furthermore there is no desktop file and sikuli-ide does not… >>> More

• #### Getting PATH right for python after MacPorts install

as seen on Super User - Search for 'Super User'
I can't import some python libraries (PIL, psycopg2) that I just installed with MacPorts. I looked through these forums, and tried to adjust my PATH variable in \$HOME/.bash_profile in order to fix this but it did not work. I added the location of PIL and psycopg2 to PATH. I know that Terminal is… >>> More

• #### call python with system() in R to run a python script emulating the python console

as seen on Stack Overflow - Search for 'Stack Overflow'
I want to pass a chunk of Python code to Python in R with something like system('python ...'), and I'm wondering if there is an easy way to emulate the python console in this case. For example, suppose the code is "print 'hello world'", how can I get the output like this in R? >>> print… >>> More

• #### Python - Calling a non python program from python?

as seen on Stack Overflow - Search for 'Stack Overflow'
Hi, I am currently struggling to call a non python program from a python script. I have a ~1000 files that when passed through this C++ program will generate ~1000 outputs. Each output file must have a distinct name. The command I wish to run is of the form: program_name -input -output -o1 -o2… >>> More

• #### how to make a function recursive

as seen on Stack Overflow - Search for 'Stack Overflow'
i have this huge function and i am wondering how to make it recursive. i have the base case which should never come true, so it should always go to else and keep calling itself with the variable t increases. any help would be great thanks def draw(x, y, t, planets): if 'Satellites' in planets["Moon"]: … >>> More

• #### Sunrise / set calculations

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm trying to calculate the sunset / rise times using python based on the link provided below. My results done through excel and python do not match the real values. Any ideas on what I could be doing wrong? My Excel sheet can be found under .. http://transpotools.com/sun_time.xls # Created on… >>> More

• #### Android-Java: Constructing a triangle based on Coordinates on a map and your bearing

as seen on Stack Overflow - Search for 'Stack Overflow'
Hi Guys, I'm constructing a geolocation based application and I'm trying to figure out a way to make my application realise when a user is facing the direction of the given location (a particular long / lat co-ord). I've got the math figured, I just have the triangle to construct. //UPDATE So… >>> More

• #### Math.max and Math.min outputting highest and lowest values allowed

as seen on Stack Overflow - Search for 'Stack Overflow'
so I'm trying to make a program that will output the sum, average, and smallest and largest values. I have everything basically figured out except the smallest and largest values are outputting 2147483647 and -2147483647, which I believe are the absolute smallest and largest values that Java will… >>> More

• #### Is Programming == Math?

as seen on Stack Overflow - Search for 'Stack Overflow'
I've heard many times that all programming is really a subset of math. Some suggest that OO, at its roots, is mathematically based. I don't get the connection. Aside from some obvious examples: using induction to prove a recursive algorithm formal correctness proofs functional languages lambda calculus asymptotic… >>> More