Coverage for sloancone/bk_cl_utils.py : 0%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/local/bin/python
2# encoding: utf-8
3"""
4Documentation for sloancone can be found here: http://sloancone.readthedocs.org/en/stable
6Usage:
7 sloancone search [-n] [(-f <outputFormat>)] [(-t <galaxyType>)] <ra> <dec> <arcsecRadius>
8 sloancone covered <ra> <dec>
10 COMMANDS
11 ========
12 search do a conesearch and report the resulting matches
13 covered test whether or not a location in the sky was covered by SDSS
15 -h, --help show this help message
16 -n, --nearest show closest match only
17 -f, --format which format to output (csv, table). Default table
18 -t, --galaxyType which galaxies to search (None, all, specz or photoz). Default None, i.e. return galaxies and stars
19"""
20from __future__ import print_function
21from __future__ import absolute_import
22################# GLOBAL IMPORTS ####################
23import sys
24import os
25os.environ['TERM'] = 'vt100'
26import readline
27import glob
28import pickle
29from docopt import docopt
30from fundamentals import tools, times
31from .cone_search import cone_search
32from .check_coverage import check_coverage
33# from ..__init__ import *
36def tab_complete(text, state):
37 return (glob.glob(text + '*') + [None])[state]
40def main(arguments=None):
41 """
42 *The main function used when ``cl_utils.py`` is run as a single script from the cl, or when installed as a cl command*
43 """
44 # setup the command-line util settings
45 su = tools(
46 arguments=arguments,
47 docString=__doc__,
48 logLevel="WARNING",
49 options_first=True,
50 projectName="sloancone"
51 )
52 arguments, settings, log, dbConn = su.setup()
54 # tab completion for raw_input
55 readline.set_completer_delims(' \t\n;')
56 readline.parse_and_bind("tab: complete")
57 readline.set_completer(tab_complete)
59 # unpack remaining cl arguments using `exec` to setup the variable names
60 # automatically
61 for arg, val in list(arguments.items()):
62 if arg[0] == "-":
63 varname = arg.replace("-", "") + "Flag"
64 else:
65 varname = arg.replace("<", "").replace(">", "")
66 if isinstance(val, ("".__class__, u"".__class__)) :
67 exec(varname + " = '%s'" % (val,))
68 else:
69 exec(varname + " = %s" % (val,))
70 if arg == "--dbConn":
71 dbConn = val
72 log.debug('%s = %s' % (varname, val,))
74 ## START LOGGING ##
75 startTime = times.get_now_sql_datetime()
76 log.info(
77 '--- STARTING TO RUN THE cl_utils.py AT %s' %
78 (startTime,))
80 # set options interactively if user requests
81 if "interactiveFlag" in locals() and interactiveFlag:
83 # load previous settings
84 moduleDirectory = os.path.dirname(__file__) + "/resources"
85 pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
86 try:
87 with open(pathToPickleFile):
88 pass
89 previousSettingsExist = True
90 except:
91 previousSettingsExist = False
92 previousSettings = {}
93 if previousSettingsExist:
94 previousSettings = pickle.load(open(pathToPickleFile, "rb"))
96 # x-raw-input
97 # x-boolean-raw-input
98 # x-raw-input-with-default-value-from-previous-settings
100 # save the most recently used requests
101 pickleMeObjects = []
102 pickleMe = {}
103 theseLocals = locals()
104 for k in pickleMeObjects:
105 pickleMe[k] = theseLocals[k]
106 pickle.dump(pickleMe, open(pathToPickleFile, "wb"))
108 # CALL FUNCTIONS/OBJECTS
110 # call the worker function
111 if search:
112 cs = cone_search(
113 log=log,
114 ra=ra,
115 dec=dec,
116 searchRadius=float(arcsecRadius),
117 nearest=nearestFlag,
118 outputFormat=outputFormat,
119 galaxyType=galaxyType
120 )
121 results = cs.get()
122 print(results)
124 # covered = True | False | 999 (i.e. not sure)
125 if covered:
126 check = check_coverage(
127 log=log,
128 ra=ra,
129 dec=dec
130 ).get()
131 print(check)
133 if "dbConn" in locals() and dbConn:
134 dbConn.commit()
135 dbConn.close()
136 ## FINISH LOGGING ##
137 endTime = times.get_now_sql_datetime()
138 runningTime = times.calculate_time_difference(startTime, endTime)
139 log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' %
140 (endTime, runningTime, ))
142 return
145if __name__ == '__main__':
146 main()
149# +++++++++++++++++++ NEW CONTENT ++++++++++++++++++
152#!/usr/local/bin/python
153# encoding: utf-8
154"""
155Documentation for sloancone can be found here: http://sloancone.readthedocs.org/en/stable
157Usage:
158 sloancone [-s <pathToSettingsFile>]
160 -h, --help show this help message
161 -s, --settings the settings file
162"""
163################# GLOBAL IMPORTS ####################
164import sys
165import os
166os.environ['TERM'] = 'vt100'
167import readline
168import glob
169import pickle
170from docopt import docopt
171from fundamentals import tools, times
172# from ..__init__ import *
175def tab_complete(text, state):
176 return (glob.glob(text + '*') + [None])[state]
179def main(arguments=None):
180 """
181 *The main function used when ``cl_utils.py`` is run as a single script from the cl, or when installed as a cl command*
182 """
183 # setup the command-line util settings
184 su = tools(
185 arguments=arguments,
186 docString=__doc__,
187 logLevel="DEBUG",
188 options_first=False,
189 projectName="sloancone"
190 )
191 arguments, settings, log, dbConn = su.setup()
193 # tab completion for raw_input
194 readline.set_completer_delims(' \t\n;')
195 readline.parse_and_bind("tab: complete")
196 readline.set_completer(tab_complete)
198 # unpack remaining cl arguments using `exec` to setup the variable names
199 # automatically
200 for arg, val in list(arguments.items()):
201 if arg[0] == "-":
202 varname = arg.replace("-", "") + "Flag"
203 else:
204 varname = arg.replace("<", "").replace(">", "")
205 if isinstance(val, ("".__class__, u"".__class__)) :
206 exec(varname + " = '%s'" % (val,))
207 else:
208 exec(varname + " = %s" % (val,))
209 if arg == "--dbConn":
210 dbConn = val
211 log.debug('%s = %s' % (varname, val,))
213 ## START LOGGING ##
214 startTime = times.get_now_sql_datetime()
215 log.info(
216 '--- STARTING TO RUN THE cl_utils.py AT %s' %
217 (startTime,))
219 # set options interactively if user requests
220 if "interactiveFlag" in locals() and interactiveFlag:
222 # load previous settings
223 moduleDirectory = os.path.dirname(__file__) + "/resources"
224 pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
225 try:
226 with open(pathToPickleFile):
227 pass
228 previousSettingsExist = True
229 except:
230 previousSettingsExist = False
231 previousSettings = {}
232 if previousSettingsExist:
233 previousSettings = pickle.load(open(pathToPickleFile, "rb"))
235 # x-raw-input
236 # x-boolean-raw-input
237 # x-raw-input-with-default-value-from-previous-settings
239 # save the most recently used requests
240 pickleMeObjects = []
241 pickleMe = {}
242 theseLocals = locals()
243 for k in pickleMeObjects:
244 pickleMe[k] = theseLocals[k]
245 pickle.dump(pickleMe, open(pathToPickleFile, "wb"))
247 # CALL FUNCTIONS/OBJECTS
249 if "dbConn" in locals() and dbConn:
250 dbConn.commit()
251 dbConn.close()
252 ## FINISH LOGGING ##
253 endTime = times.get_now_sql_datetime()
254 runningTime = times.calculate_time_difference(startTime, endTime)
255 log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' %
256 (endTime, runningTime, ))
258 return
261if __name__ == '__main__':
262 main()