/
updatenightinfo.py
139 lines (106 loc) · 4.49 KB
/
updatenightinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
################################# LICENSE ##################################
# Copyright (c) 2009, South African Astronomical Observatory (SAAO) #
# All rights reserved. #
# #
############################################################################
#!/usr/bin/env python
"""
updatenightinfo will create or update the nightinfo entry with the
correct times for sunrise/set, twilight, and moon rise/set
Author Version Date Comment
-----------------------------------------------------------------------
S M Crawford (SAAO) 0.1 25 Apr 2012
"""
# Ensure python 2.5 compatibility
from __future__ import with_statement
import os, time, glob, string, datetime
import ephem
from pyraf import iraf
from iraf import pysalt
import salttime
import saltsafeio as saltio
import saltsafemysql as saltmysql
from saltsafelog import logging
from salterror import SaltError
debug=True
# -----------------------------------------------------------
# core routine
def updatenightinfo(obsdate, sdbhost='sdb.saao', sdbname='sdb', \
sdbuser='', password='', logfile='saltlog.log', verbose=True):
"""Update the nightinfo table with current information about the
night
"""
with logging(logfile,debug) as log:
#connect the database
sdb=saltmysql.connectdb(sdbhost, sdbname, sdbuser, password)
#get the nightinfo_id
try:
night_id=saltmysql.getnightinfoid(sdb, obsdate)
except SaltError:
night_id=None
#get the information for the night
time_list=get_nightdetails(obsdate)
#create the insert command
obsdate=str(obsdate)
inst_cmd="Date='%s-%s-%s'," % (obsdate[0:4], obsdate[4:6], obsdate[6:8])
inst_cmd+="SunSet='%s', SunRise='%s', MoonSet='%s', MoonRise='%s', EveningTwilightEnd='%s', MorningTwilightStart='%s'" % \
(time_list[0], time_list[1], time_list[2], time_list[3], time_list[4], time_list[5])
inst_cmd+=",MoonPhase_Percent=%i" % (round(float(time_list[6])))
if night_id:
saltmysql.update(sdb, inst_cmd, 'NightInfo', 'NightInfo_Id=%i' % night_id)
msg="Updating information for Night_Id=%i\n" % night_id
else:
saltmysql.insert(sdb, inst_cmd, 'NightInfo')
msg="Inserting information for Night_Id=%i\n" % night_id
#log the call
log.message(msg+inst_cmd, with_stdout=verbose)
def sutherland(date=None):
"""Create an ephem.Observer object with the settings for Sutherland
"""
Suth=ephem.Observer()
Suth.lat='-32.3794444'
Suth.lon='20.81069500'
Suth.elevation=1798.0
Suth.pressure=0
if date:
Suth.date=date
return Suth
def get_nightdetails(obsdate=None):
"""Get all the night details for a given date
"""
#Set the date to noon on the given day
if obsdate:
obsdate=str(obsdate)
date='%s/%s/%s 12:00:01' % (obsdate[0:4], obsdate[4:6], obsdate[6:8])
#set up the obseratory
Suth=sutherland(date)
#now get the sunrise and sunset
#Following the definition of USNO we set the value
#for the horizon at -34'
Suth.horizon='-00:34'
sunset=ephem.localtime(Suth.next_setting(ephem.Sun()))
sunrise=ephem.localtime(Suth.next_rising(ephem.Sun()))
#get the moon rise and moonset
moonset=ephem.localtime(Suth.next_setting(ephem.Moon()))
moonrise=ephem.localtime(Suth.next_rising(ephem.Moon()))
#calculate the moon phase
m=ephem.Moon()
m.compute(Suth)
#calculate the moonphase at midnight
if date:
m.compute('%s/%s/%s 23:59:01' % (obsdate[0:4], obsdate[4:6], obsdate[6:8]))
moonphase=100*m.moon_phase
print moonphase
#get the start/end of astronomical twilight
Suth.horizon='-18'
twistart=ephem.localtime(Suth.next_setting(ephem.Sun(), use_center=True))
twiend=ephem.localtime(Suth.next_rising(ephem.Sun(), use_center=True))
return [sunset, sunrise, moonset, moonrise, twistart, twiend, moonphase]
#return [str(x).replace('/','-') for x in [sunset, sunrise, moonset, moonrise, twistart, twiend, moonphase]]
if __name__=='__main__':
import sys, getpass
obsdate=sys.argv[1]
passwd=getpass.getpass()
print get_nightdetails(obsdate)
updatenightinfo(obsdate, sdbhost='sdb.salt', sdbname='sdb', \
sdbuser='pipeline', password=passwd, logfile='saltlog.log', verbose=True)