-
Notifications
You must be signed in to change notification settings - Fork 0
/
saltquery.py
177 lines (137 loc) · 6.53 KB
/
saltquery.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
################################# LICENSE ##################################
# Copyright (c) 2009, South African Astronomical Observatory (SAAO) #
# All rights reserved. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions #
# are met: #
# #
# * Redistributions of source code must retain the above copyright #
# notice, this list of conditions and the following disclaimer. #
# * Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer #
# in the documentation and/or other materials provided with the #
# distribution. #
# * Neither the name of the South African Astronomical Observatory #
# (SAAO) nor the names of its contributors may be used to endorse #
# or promote products derived from this software without specific #
# prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE SAAO ''AS IS'' AND ANY EXPRESS OR #
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
# DISCLAIMED. IN NO EVENT SHALL THE SAAO BE LIABLE FOR ANY #
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL #
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS #
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, #
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN #
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
# POSSIBILITY OF SUCH DAMAGE. #
############################################################################
#!/usr/bin/env python
"""
SALTQUERY queries the SALT Science Database for FITS data in the FileData tables. It provides a pyraf interface for querying the tables.
The user inputs the selection they want to use. They can select for any of the columns in FileData or for any of the keywords in the FITS header. The list can either be provide as a comma separated list or in a file with '@' proceeding the file name.
The user than supplies the task with a bit of logic to select the data. This should be supplied as a string and following mysql syntax. The user also supplies it with start and end dates which are automatically applied to the where statement.
Author Version Date Comment
-----------------------------------------------------------------------
S M Crawford (SAAO) 0.1 5 July 2011
Updates
-----------------------------------------------------------------------
20120607- Updated to query RSS information and then SALTICAM information
"""
# Ensure python 2.5 compatibility
from __future__ import with_statement
import os, time, glob, string
from pyraf import iraf
import salttime
import saltsafeio as saltio
import saltsafemysql as saltmysql
from saltsafelog import logging
from salterror import SaltError
debug=True
# -----------------------------------------------------------
# core routine
def saltquery(selection, logic, startdate, enddate, outfile=None, sdbhost='sdb.saao', sdbname='sdb', \
sdbuser='', password='', clobber=False, logfile='saltlog.log', verbose=True):
"""Query the salt database for FITS files
"""
with logging(logfile,debug) as log:
#check the outfiles
if not saltio.checkfornone(outfile):
outfile=None
#check that the output file can be deleted
if outfile:
saltio.overwrite(outfile, clobber)
#open outfile
if outfile:
fout=saltio.openascii(outfile, 'w')
#connect to the database
sdb=saltmysql.connectdb(sdbhost,sdbname,sdbuser,password)
#Create a list of the selection and then unpack it
selection_list = saltio.argunpack('Selection', selection)
selection=','.join(selection_list)
#write out the header for the outfile
outstr='#'+' '.join(['%s' % x for x in selection_list])
if outfile:
fout.write(outstr+'\n')
else:
print outstr
#set up the table
rsstable=''' FileData
left join FitsHeaderImage using (FileData_Id)
inner join FitsHeaderRss using (FileData_Id)
'''
#set up the table
scamtable=''' FileData
left join FitsHeaderImage using (FileData_Id)
inner join FitsHeaderSalticam using (FileData_Id)
'''
#set up the logic
logic=makelogic(logic, startdate, enddate)
for tab in [rsstable, scamtable]:
msg='''
Mysql querying data is:
SELECT %s
FROM %s
WHERE %s
''' % (selection, tab, logic)
log.message(msg, with_stdout=verbose)
record=saltmysql.select(sdb, selection, tab, logic)
print record
for r in record:
outstr=' '.join(['%s' % x for x in r])
if outfile:
fout.write(outstr+'\n')
else:
print outstr
#close outfile
if outfile: fout.close()
def makelogic(logic, startdate, enddate):
#set up the logic for different dates
date_logic=''
if startdate:
d,m,y=salttime.breakdate(startdate)
y=str(y)
m=string.zfill(m, 2)
d=string.zfill(d, 2)
date_logic += "UTStart > '%s-%s-%s 12:00:01'" % (y,m,d)
if startdate and enddate:
date_logic += " and "
if enddate:
edate=salttime.getnextdate(enddate)
d,m,y=salttime.breakdate(str(edate))
y=str(y)
m=string.zfill(m, 2)
d=string.zfill(d, 2)
date_logic += "UTStart < '%s-%s-%s 11:59:59'" % (y,m,d)
if logic and date_logic:
logic = '('+logic+')' + ' and ' + date_logic
else:
logic
return logic
# -----------------------------------------------------------
# main code
parfile = iraf.osfn("pipetools$saltquery.par")
t = iraf.IrafTaskFactory(taskname="saltquery",value=parfile,function=saltquery, pkgname='pipetools')