Hide keyboard shortcuts

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/bin/env python 

2# encoding: utf-8 

3""" 

4Documentation for sloancone can be found here: http://sloancone.readthedocs.org 

5 

6Usage: 

7 sloancone search [-n] [(-f <outputFormat>)] [(-t <galaxyType>)] <ra> <dec> <arcsecRadius> 

8 sloancone covered <ra> <dec> 

9 

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 

14 

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 

21import sys 

22import os 

23os.environ['TERM'] = 'vt100' 

24import readline 

25import glob 

26import pickle 

27from docopt import docopt 

28from fundamentals import tools, times 

29from subprocess import Popen, PIPE, STDOUT 

30 

31 

32def tab_complete(text, state): 

33 return (glob.glob(text + '*') + [None])[state] 

34 

35 

36def main(arguments=None): 

37 """ 

38 *The main function used when `cl_utils.py` is run as a single script from the cl, or when installed as a cl command* 

39 """ 

40 # setup the command-line util settings 

41 su = tools( 

42 arguments=arguments, 

43 docString=__doc__, 

44 logLevel="WARNING", 

45 options_first=False, 

46 projectName="sloancone", 

47 defaultSettingsFile=True 

48 ) 

49 arguments, settings, log, dbConn = su.setup() 

50 

51 # tab completion for raw_input 

52 readline.set_completer_delims(' \t\n;') 

53 readline.parse_and_bind("tab: complete") 

54 readline.set_completer(tab_complete) 

55 

56 # UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES 

57 # AUTOMATICALLY 

58 a = {} 

59 for arg, val in list(arguments.items()): 

60 if arg[0] == "-": 

61 varname = arg.replace("-", "") + "Flag" 

62 else: 

63 varname = arg.replace("<", "").replace(">", "") 

64 a[varname] = val 

65 if arg == "--dbConn": 

66 dbConn = val 

67 a["dbConn"] = val 

68 log.debug('%s = %s' % (varname, val,)) 

69 

70 search = a["search"] 

71 covered = a["covered"] 

72 nearest = a["nearest"] 

73 fformat = a["format"] 

74 galaxyType = a["galaxyType"] 

75 

76 ## START LOGGING ## 

77 startTime = times.get_now_sql_datetime() 

78 log.info( 

79 '--- STARTING TO RUN THE cl_utils.py AT %s' % 

80 (startTime,)) 

81 

82 # set options interactively if user requests 

83 if "interactiveFlag" in a and a["interactiveFlag"]: 

84 

85 # load previous settings 

86 moduleDirectory = os.path.dirname(__file__) + "/resources" 

87 pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals() 

88 try: 

89 with open(pathToPickleFile): 

90 pass 

91 previousSettingsExist = True 

92 except: 

93 previousSettingsExist = False 

94 previousSettings = {} 

95 if previousSettingsExist: 

96 previousSettings = pickle.load(open(pathToPickleFile, "rb")) 

97 

98 # x-raw-input 

99 # x-boolean-raw-input 

100 # x-raw-input-with-default-value-from-previous-settings 

101 

102 # save the most recently used requests 

103 pickleMeObjects = [] 

104 pickleMe = {} 

105 theseLocals = locals() 

106 for k in pickleMeObjects: 

107 pickleMe[k] = theseLocals[k] 

108 pickle.dump(pickleMe, open(pathToPickleFile, "wb")) 

109 

110 if a["init"]: 

111 from os.path import expanduser 

112 home = expanduser("~") 

113 filepath = home + "/.config/sloancone/sloancone.yaml" 

114 try: 

115 cmd = """open %(filepath)s""" % locals() 

116 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) 

117 except: 

118 pass 

119 try: 

120 cmd = """start %(filepath)s""" % locals() 

121 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) 

122 except: 

123 pass 

124 return 

125 

126 # CALL FUNCTIONS/OBJECTS 

127 # CALL THE WORKER FUNCTION 

128 if search: 

129 cs = cone_search( 

130 log=log, 

131 ra=ra, 

132 dec=dec, 

133 searchRadius=float(arcsecRadius), 

134 nearest=nearestFlag, 

135 outputFormat=outputFormat, 

136 galaxyType=galaxyType 

137 ) 

138 results = cs.get() 

139 print(results) 

140 

141 # COVERED = TRUE | FALSE | 999 (I.E. NOT SURE) 

142 if covered: 

143 check = check_coverage( 

144 log=log, 

145 ra=ra, 

146 dec=dec 

147 ).get() 

148 print(check) 

149 

150 if "dbConn" in locals() and dbConn: 

151 dbConn.commit() 

152 dbConn.close() 

153 ## FINISH LOGGING ## 

154 endTime = times.get_now_sql_datetime() 

155 runningTime = times.calculate_time_difference(startTime, endTime) 

156 log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' % 

157 (endTime, runningTime, )) 

158 

159 return 

160 

161 

162if __name__ == '__main__': 

163 main()