[Tutor] lists, arrays and saving data

Emile van Sebille emile at fenx.com
Sun Nov 21 19:25:33 CET 2010


On 11/21/2010 5:12 AM Chris Begert said...
> Hi Gurus
>
> I just wrote my first little python program; so yes I'm very new to all this.
>
> The goal in the end is to have a program that shows how the sun moves  from the point of view of a given location (i.e. solar paths added to some sort of stereographic diagram).
>
> My very first program calculates the height (altitude) and direction of the sun (Azimuth) at a specific location during a specific time.
>
> So here's my question (I'm sure its obvious for most of you):
>
> How do I store a years worth of data of angles in an array / list / whatever is most useful when using Python?
>
>

I would look at moving the calculation part into a function that accepts 
as input the location, date and time parameters and returns the results. 
  Then you could write a loop to build the list for the locations and 
date ranges of interest.  Your choice for data persistence would depend 
in part on how the data is to be used and how much there is.  Take a 
look at the options python comes with at 
http://docs.python.org/py3k/library/persistence.html but for small data 
sets building the data on the fly may be sufficient as creating a full 
year of data at ten minute intervals takes thee seconds on my PC.

Below is how I refactored things.

Emile


-----

import datetime, time
from math import sin
from math import cos
from math import degrees
from math import radians
from math import acos

def getLocation():
     print("Please specify your location")
     long = float(input("Longitude in degrees: "))
     lati = float(input("Latitude in degrees: "))
     return long,lati

def getAltAZ (long,lati,month,day,hour,minutes):
     time = hour + minutes/60
     Nd = datetime.datetime(2010,month,day).timetuple().tm_yday
     gdeg = (360/365.25)*(Nd + time/24)
     g = radians(gdeg)
     D = 
0.396372-22.91327*cos(g)+4.02543*sin(g)-0.387205*cos(2*g)+0.051967*sin(2*g)-0.154527*cos(3*g) 
+ 0.084798*sin(3*g)
     TC = 
0.004297+0.107029*cos(g)-1.837877*sin(g)-0.837378*cos(2*g)-2.340475*sin(2*g)
     SHA = (time-12)*15 + long + TC
     if SHA > 180: SHA = SHA - 360
     elif SHA< -180: SHA = SHA + 360
     cosSZA = 
sin(radians(lati))*sin(radians(D))+cos(radians(lati))*cos(radians(D))*cos(radians(SHA))
     SZA = degrees(acos(cosSZA))
     altitude = 90 - SZA
     cosAZ = (sin(radians(D)) - sin(radians(lati)) * cos(radians(SZA))) 
/ (cos(radians(lati))*sin(radians(SZA)))
     AZ = degrees(acos(cosAZ))
     return altitude,AZ

def prepFullYear(long,lati, interval=10):
     return [getAltAZ(long,lati,*time.localtime(t+ii)[1:5]) for ii in 
range(0,60*24*365,interval)]

if __name__ == '__main__':
     t = time.time()
     fy = prepFullYear(100,30)
     print (time.time()-t)







More information about the Tutor mailing list