I am a fledgling at best when it comes to programing. I am trying to acclimate myself with Python's date and time functions. Below is a program I wrote to calculate the Sun rise and set times for a given lat, long. What I am hoping to find out is if there is a way to obtain the Julian date from a given date input so I do not have to calculate the date ahead of time and input it as a parameter, and avoid having to, otherwise, add several lines just to make this happen myself? Also are there functions, as they have in excel, to convert between radians and degrees so I can avoid the lengthy lines of having to do the conversions within my code? Thanks

#parameter JD is today's current Julian Date
def SunRise(lat,lng,JD,y,m,d):
  import math
  from datetime import datetime
  import time
  from datetime import date
  now = date.today
  day = date(y,m,d)
  Day =day.strftime('%j')
  J = 2451545.0
  n = round(JD-J-.0009*(lng/360.0))
  J1 = J+.0009+(lng/360.0)+n
  J2 = J1-J
  Mi= ((357.5291+(.98560028*J2))/360)
  M = (360*(Mi%1))
  z = (360/math.pi)*.0167
  C = z*math.sin(M*math.pi/180)+0.0200*math.sin(2*M*math.pi/180)+0.0003*math.sin(3*M*math.pi/180)
  La = (M+102.9372+C+180)
  Jt = J1+(0.0053*math.sin(M*math.pi/180))-(0.0009*math.sin(2*La*math.pi/180))
  D =180/math.pi*(math.asin((math.sin(23.45*math.pi/180))*math.sin(La*math.pi/180)))
  Phi = (180/math.pi)*(math.acos((math.sin(-5.06/6.0*math.pi/180)-(math.sin(lat*math.pi/180)*math.sin(D*math.pi/180)))/(math.cos(lat*math.pi/180)*math.cos(D*math.pi/180)))) 
  Jset = J+0.0009+((Phi+lng)/360)+n+(0.0053*math.sin(M*math.pi/180)-(0.0069*math.sin(2*La*math.pi/180)))
  Jrise = Jt-(Jset-Jt)
  DdayS = Jset-(J+0.5)
  SShour = int((DdayS%1)*24)-5
  if SShour <= 0:
      SShour = SShour+24
  SSmin = int(((24*(DdayS%1)-5.0)%1)*60)
  SSsec = int(((((24*(DdayS%1)-5.0)%1)*60)%1)*60)
  DdayR = Jrise-(J+0.5)
  SRhour = int((DdayR%1)*24)-5
  SRmin = int(((24*(DdayR%1)-5.0)%1)*60)
  SRsec = int(((((24*(DdayR%1)-5.0)%1)*60)%1)*60)
  DSTstart = date(2012,03,11)
  DSTS = DSTstart.strftime('%j')
  DSTend = date(2012,11,04)
  DSTE = DSTend.strftime('%j')
  if Day > DSTS and Day< DSTE:
    SShour += 1
    SRhour += 1
  print "The Sun will rise at:",SRhour,":",SRmin,":",SRsec,"AM EST",d,m,y
  print "The Sun will set at:",SShour,":",SSmin,":",SSsec,"PM EST",d,m,y


I don't know about julian date, but google led me to this module (under GPL) which seems to implement the desired functionality by subclassing datetime. Conversion between degrees and radians can be done with math.degrees() and math.radians() in the standard library (numpy versions exist as well).

Thanks for your resonse.That looks like it will help me, unfortunately, I do not know how to install such additions. Any links on how to do that??

You can download the code by pushing the ZIP button in the project page, then uncompress the zip file. In a console (or cmd in windows), change directory to the module directory (the directory with the file setup.py), then in your console, as administrator or root, type

python setup.py install

and this should install the module in your python system. Test it, in a python console this time, with >>> import apwlib.

YOu should start using the pandas package. It has explicit support for handling timeseries data and lots of builtin methods to convert. I imagine you're reinventing wheels somewhat.

Yeah, I think there is definately a learning curve to installing packages in Python, mostly because some packages are easy to install and others are strange. What OS do you use?

One thing I do is I use the Enthought Python Distribution, which comes packaged with most of the standard scientific libraries pre-installed (numpy, scipy, biophyton, ipython etc...). I actually have a subscription, but the free version is pretty good anyway. Still, you will probably have the same issues with xephem as I doubt it is natively supported.

Did you read the install directions in the xephem package? If so, maybe email the authors for further help.

I should note that there are some standards to python packages. (http://docs.python.org/install/index.html)

In an ideal world, someone has made a binary which you can basically double click and it will install. These are .exe files on Windows, .deb files on ubuntu and I forget the extension for mac.

If those aren't available, you'd like for someone to have used distilus and made a setup.py file. The link I sent calls this the "standard way" and generally will involve you typing some variation of:

python setup.py install

Unfortunately, the xemphem package seems to be nonstandard. You're probably at the mercy of their installation instructions and maybe you can email and ask why they don't have a setup.py file.

Pandas, the libary I mentioned, for example does use distilus.