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 HMpTy can be found here: http://HMpTy.readthedocs.org 

5 

6Usage: 

7 hmpty htmid <level> <ra> <dec> 

8 hmpty [-f] index <tableName> <primaryIdCol> <raCol> <decCol> (-s <pathToSettingsFile>|--host <host> --user <user> --passwd <passwd> --dbName <dbName>) 

9 hmpty search <tableName> <raCol> <decCol> <ra> <dec> <radius> (-s <pathToSettingsFile>|--host <host> --user <user> --passwd <passwd> --dbName <dbName>) [(-r <format>|-r mysql <resultsTable>)] 

10 

11Options: 

12 index add HTMids to database table 

13 search perform a conesearch on a database table 

14 htmid generate the htmID at the given coordinates for the give HTM level 

15 

16 tableName name of the table to add the HTMids to 

17 primaryIdCol the name of the unique primary ID column of the database table 

18 raCol name of the table column containing the right ascension 

19 decCol name of the table column containing the declination 

20 ra the right ascension of the centre of the conesearch circle or coordinate set 

21 dec the declination of the centre of the conesearch circle or coordinate set 

22 radius the radius of the conesearch circle (arcsec) 

23 level the HTM level required 

24 

25 -f, --force force a regeneration of all HTMIDs 

26 -h, --help show this help message 

27 -v, --version show version 

28 -s <pathToSettingsFile>, --settings <pathToSettingsFile> path to a settings file containing the database credentials 

29 --host <host> database host address 

30 --user <user> database username 

31 --passwd <passwd> database password 

32 --dbName <dbName> database name 

33 -r <format>, --render <format> select a format to render your results in 

34""" 

35from __future__ import print_function 

36import sys 

37import os 

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

39import readline 

40import glob 

41import pickle 

42from docopt import docopt 

43from fundamentals import tools, times 

44from subprocess import Popen, PIPE, STDOUT 

45 

46def tab_complete(text, state): 

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

48 

49def main(arguments=None): 

50 """ 

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

52 """ 

53 # setup the command-line util settings 

54 su = tools( 

55 arguments=arguments, 

56 docString=__doc__, 

57 logLevel="WARNING", 

58 options_first=False, 

59 projectName="HMpTy", 

60 defaultSettingsFile=True 

61 ) 

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

63 

64 # tab completion for raw_input 

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

66 readline.parse_and_bind("tab: complete") 

67 readline.set_completer(tab_complete) 

68 

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

70 # AUTOMATICALLY 

71 a = {} 

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

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

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

75 else: 

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

77 a[varname] = val 

78 if arg == "--dbConn": 

79 dbConn = val 

80 a["dbConn"] = val 

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

82 

83 hostFlag = a["hostFlag"] 

84 userFlag = a["userFlag"] 

85 passwdFlag = a["passwdFlag"] 

86 dbNameFlag = a["dbNameFlag"] 

87 tableName = a["tableName"] 

88 index = a["index"] 

89 htmid = a["htmid"] 

90 primaryIdCol = a["primaryIdCol"] 

91 raCol = a["raCol"] 

92 decCol = a["decCol"] 

93 ra = a["ra"] 

94 radius = a["radius"] 

95 level = a["level"] 

96 forceFlag = a["forceFlag"] 

97 renderFlag = a["renderFlag"] 

98 search = a["search"] 

99 

100 ## START LOGGING ## 

101 startTime = times.get_now_sql_datetime() 

102 log.info( 

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

104 (startTime,)) 

105 

106 # set options interactively if user requests 

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

108 

109 # load previous settings 

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

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

112 try: 

113 with open(pathToPickleFile): 

114 pass 

115 previousSettingsExist = True 

116 except: 

117 previousSettingsExist = False 

118 previousSettings = {} 

119 if previousSettingsExist: 

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

121 

122 # x-raw-input 

123 # x-boolean-raw-input 

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

125 

126 # save the most recently used requests 

127 pickleMeObjects = [] 

128 pickleMe = {} 

129 theseLocals = locals() 

130 for k in pickleMeObjects: 

131 pickleMe[k] = theseLocals[k] 

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

133 

134 if a["init"]: 

135 from os.path import expanduser 

136 home = expanduser("~") 

137 filepath = home + "/.config/HMpTy/HMpTy.yaml" 

138 try: 

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

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

141 except: 

142 pass 

143 try: 

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

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

146 except: 

147 pass 

148 return 

149 

150 # CALL FUNCTIONS/OBJECTS 

151 if index: 

152 add_htm_ids_to_mysql_database_table( 

153 raColName=raCol, 

154 declColName=decCol, 

155 tableName=tableName, 

156 dbConn=dbConn, 

157 log=log, 

158 primaryIdColumnName=primaryIdCol, 

159 reindex=forceFlag, 

160 dbSettings=dbSettings 

161 ) 

162 

163 if search: 

164 cs = conesearch( 

165 log=log, 

166 dbConn=dbConn, 

167 tableName=tableName, 

168 columns=False, 

169 ra=ra, 

170 dec=dec, 

171 radiusArcsec=float(radius), 

172 separations=True, 

173 distinct=False, 

174 sqlWhere=False 

175 ) 

176 matchIndies, matches = cs.search() 

177 if not renderFlag: 

178 print(matches.table()) 

179 elif renderFlag == "json": 

180 print(matches.json()) 

181 elif renderFlag == "csv": 

182 print(matches.csv()) 

183 elif renderFlag == "yaml": 

184 print(matches.yaml()) 

185 elif renderFlag == "md": 

186 print(matches.markdown()) 

187 elif renderFlag == "table": 

188 print(matches.markdown()) 

189 elif renderFlag == "mysql": 

190 print(matches.mysql(tableName=resultsTable)) 

191 

192 if level: 

193 from HMpTy import HTM 

194 mesh = HTM( 

195 depth=int(level), 

196 log=log 

197 ) 

198 

199 htmids = mesh.lookup_id(ra, dec) 

200 print(htmids[0]) 

201 

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

203 dbConn.commit() 

204 dbConn.close() 

205 ## FINISH LOGGING ## 

206 endTime = times.get_now_sql_datetime() 

207 runningTime = times.calculate_time_difference(startTime, endTime) 

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

209 (endTime, runningTime, )) 

210 

211 return 

212 

213if __name__ == '__main__': 

214 main()