Thursday 8 January 2015

Declination angle for all 365 days

Question:To find the declination angle using python. The formula for the declination angle is given by:
δ = 23.45*sin[360*n/365]
δ(Declination angle) is the angle made by the line joining the centers of the sun and the earth with the projection of this line on equatorial plane(equator of earth). To make it simple a simple diagram can be given as follows:

One thing to be remembered about the earth is that it is not vertical when compared to sun. So there will always be an angle with the sun. If you don't understand anything just ignore the theory and generate the result using the formula. You can find more here.
Here the question is: considering n = 1 as January 1st and December 31 as n = 365 and ignoring the leap years generate the values of Declination angle using python.

Answer 

# -*- coding: utf-8 -*-
#Firstly indicated that we are going to use UTF-8 format
import math #Imported the math module
def declination_angle(n): #Defined a function for declination angle
    angle = 23.45*math.sin(360*n/365) #Defined the formula
    return angle

def month(n):#Defined a function to seperate the month on the basis of day number
    if n>=1 and n<=31:
        return "January"#31 days
    elif n>31 and n<=59:
        return "February"#28 days
    elif n>59 and n<=90:
        return "March"#31 days
    elif n>90 and n<=120:
        return "April"#30 days
    elif n>120 and n<=151:
        return "May"#31 days
    elif n>151 and n<=181:
        return "June"#30 days
    elif n>181 and n<=212:
        return "July"#31 days
    elif n>212 and n<=243:
        return "August"#31 days
    elif n>243 and n<=273:
        return "September"#30 days
    elif n>273 and n<=304:
        return "October"#31 days
    elif n>304 and n<=334:
        return "November"#30 days
    elif n>334 and n<=365:
        return "December"#31 days
    
def day(n):#Function to make the day number to month number
    #For example n = 53 will become 22(53-31)
    if n>=1 and n<=31:#Number of days before the month are indicated
        return n#0 days before January
    elif n>31 and n<=59:
        return n-31#31 days before February
    elif n>59 and n<=90:
        return n-59#59 days days before March
    elif n>90 and n<=120:
        return n-90#90 days before April
    elif n>120 and n<=151:
        return n-120#120 days before May
    elif n>151 and n<=181:
        return n-151#151 days before June
    elif n>181 and n<=212:
        return n-181#181 days before July
    elif n>212 and n<=243:
        return n-212#212 days before August
    elif n>243 and n<=273:
        return n-243#243 days before September
    elif n>273 and n<=304:
        return n-273#273 days before October
    elif n>304 and n<=334:
        return n-304#304 days before November
    elif n>334 and n<=365:
        return n-334#334 days before December

for i in range(1,366): #Looping for 365 days
    angle = declination_angle(i) #Storing the angle in a variable
    mon = month(i) #storing the month in mon variable
    print u"Declination angle(δ)", #indicated "u" before string to say that there is "δ" in string
    print "on", #comma at the end of string to make it come on same day
    print day(i),
    print "of",
    print mon,
    print "is",
    print "{}".format(angle)#Format method to indicate angle

Output:

Declination angle(δ) on 1 of January is  0.000000
Declination angle(δ) on 2 of January is 19.732495
Declination angle(δ) on 3 of January is 21.323025
...........


Full output can be seen here:Output for declination angle problem
I have written the code and commented the code so that everyone will be able to understand the program easily. Each part of the program is as folllows:


# -*- coding: utf-8 -*-
This will tell the interpreter that the code is in UTF-8 format. And if we use other characters like delta, alpha then interpreter will show the symbols and otherwise it will not.



import math #Imported the math module

The above statement will import the Math module from where we can use the sine function. We can also use the "from" method where we will not use the "math.sin" in the code. But the above way is recommended so that there will not be any confusion



def declination_angle(n): #Defined a function for declination angle
    angle = 23.45*math.sin(360*n/365) #Defined the formula
    return angle

This will define a function named declination_angle which will take "n" as argument. This function has a variable name angle which contains the formula. This function will take the value of the "n" and return the declination angle which is stored in the angle variable



def month(n):#Defined a function to seperate the month on the basis of day number
    if n>=1 and n<=31:
        return "January"#31 days
    elif n>31 and n<=59:
        return "February"#28 days
    elif n>59 and n<=90:
        return "March"#31 days
    elif n>90 and n<=120:
        return "April"#30 days
    elif n>120 and n<=151:
        return "May"#31 days
    elif n>151 and n<=181:
        return "June"#30 days
    elif n>181 and n<=212:
        return "July"#31 days
    elif n>212 and n<=243:
        return "August"#31 days
    elif n>243 and n<=273:
        return "September"#30 days
    elif n>273 and n<=304:
        return "October"#31 days
    elif n>304 and n<=334:
        return "November"#30 days
    elif n>334 and n<=365:
        return "December"#31 days

This will define a function which will take the day number in the year as argument. We have used series of if elif which will divide the day numbers and will return the month. This is useful to give an output with the month name so that the user will understand the output easily.



def day(n):#Function to make the day number to month number
    #For example n = 53 will become 22(53-31)
    if n>=1 and n<=31:#Number of days before the month are indicated
        return n#0 days before January
    elif n>31 and n<=59:
        return n-31#31 days before February
    elif n>59 and n<=90:
        return n-59#59 days days before March
    elif n>90 and n<=120:
        return n-90#90 days before April
    elif n>120 and n<=151:
        return n-120#120 days before May
    elif n>151 and n<=181:
        return n-151#151 days before June
    elif n>181 and n<=212:
        return n-181#181 days before July
    elif n>212 and n<=243:
        return n-212#212 days before August
    elif n>243 and n<=273:
        return n-243#243 days before September
    elif n>273 and n<=304:
        return n-273#273 days before October
    elif n>304 and n<=334:
        return n-304#304 days before November
    elif n>334 and n<=365:
        return n-334#334 days before December

We know that every month has days 31 or 30 or 28 and the dates of the month are represented between these numbers depending on the month. But we use an input of numbers from 1 to 365 which will represent the day number in the year instead of month. The above function "day" is uses a series of if and elif to represent the day in the month instead of the year.



for i in range(1,366): #Looping for 365 days
    angle = declination_angle(i) #Storing the angle in a variable
    mon = month(i) #storing the month in mon variable
    print u"Declination angle(δ)", #indicated "u" before string to say that there is "δ" in string
    print "on", #comma at the end of string to make it come on same day
    print day(i),
    print "of",
    print mon,
    print "is",
    print "{}".format(angle)#Format method to indicate angle

Finally we will loop to generate the result of 365 days. For this we have used for loop. This loop will give our functions(declination_angle, month) each day in the year. Here we have used a series of print statements to show our output for our users.
It should be noted that we have used "u" in front of the string, this will tell the interpreter to print the characted δ in the same way. For more information you can see here: Unicode HOWTO &mdash; Python 2.7.9 documentation

Please do contact me if you have a better solution than this. Thanks! 

No comments:

Post a Comment