31 Jan. 2013

Calculating darktime with Python

Cherenkov telescopes like H.E.S.S. can only operate during darktime, that means when sun and moon are far enough below the horizon.
To calculate the darktime for a night in Python, the fantastic ephem package can be used. "Far enough below the horizon" is achieved by setting the horizon to a negative value for the sun, e.g. -12 degrees.
#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-

import sys
import ephem

# create the observer
hess = ephem.Observer()
hess.lat = '-23.271333'
hess.long = '16.5'
hess.elevation = 1800
hess.date = ephem.Date(sys.argv[1] + " 12:00")

# calculate rising and setting times
darktime_start = []
darktime_end = []

# moon below horizon
moon = ephem.Moon()
moon.compute(hess)
if moon.alt < hess.horizon:
    darktime_start.append(hess.date)
    darktime_end.append(hess.next_rising(moon))
else:
    darktime_start.append(hess.next_setting(moon))
    darktime_end.append(hess.next_rising(moon))

# sun below -12 degrees
hess.horizon = "-12"
sun = ephem.Sun()
sun.compute(hess)
if sun.alt < hess.horizon:
    darktime_start.append(hess.date)
    darktime_end.append(hess.next_rising(sun))
else:
    darktime_start.append(hess.next_setting(sun))
    darktime_end.append(hess.next_rising(sun))

print "Darktime is from {0!s} UTC to {1!s} UTC".format(
    max(darktime_start), min(darktime_end))


With this code in a file "darktime.py", simply run:
> darktime.py 2013-01-31
Darktime is from 2013/1/31 18:35:36 UTC to 2013/1/31 19:29:22 UTC

comments powered by Disqus