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

5 

6Usage: 

7 transientNamer [-c] cone <ra> <dec> <arcsecRadius> [<render> | mysql <tableNamePrefix>] [-o directory] [-s pathToSettingsFile] 

8 transientNamer [-c] search <name> [<render> | mysql <tableNamePrefix>] [-o directory] [-s pathToSettingsFile] 

9 transientNamer [-c] new <reportedInLastDays> [<render> | mysql <tableNamePrefix>] [-o directory] [-s pathToSettingsFile] 

10 transientNamer [-i] notes <reportedInLastDays> [-s pathToSettingsFile] 

11 

12Commands: 

13 cone perform a conesearch on the TNS 

14 search perform a name search on the TNS 

15 new list newly reported TNS objects 

16 notes download astronotes amd cache in local directory 

17  

18Arguments: 

19 ra 

20 dec 

21 arcsecRadius 

22 name the name of the object the search for (TNS or survey name) 

23 render output format for results. Options include json, csv, table, markdown, yaml 

24 tableNamePrefix the prefix for the tables to write the mysql insert statements for 

25 directory path to the directory to save the output to 

26 reportedInLastDays download and parse data reported within the last <n> days 

27 mysql generate mysql insert scripts 

28 

29Options: 

30 -h, --help show this help message 

31 -v, --version show version 

32 -s pathToSettingsFile, --settings=pathToSettingsFile the settings file 

33 -c, --withComments return TNS comments in result sets 

34 -i, --import parse and import the content of the astronotes into a MySQL database 

35 -o directory, --output=directory output to files in the directory path 

36""" 

37from __future__ import print_function 

38import sys 

39import os 

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

41import readline 

42import glob 

43import pickle 

44from docopt import docopt 

45from fundamentals import tools, times 

46from subprocess import Popen, PIPE, STDOUT 

47import transientNamer 

48from transientNamer import astronotes 

49 

50 

51def tab_complete(text, state): 

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

53 

54 

55def main(arguments=None): 

56 """ 

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

58 """ 

59 # setup the command-line util settings 

60 su = tools( 

61 arguments=arguments, 

62 docString=__doc__, 

63 logLevel="WARNING", 

64 options_first=False, 

65 projectName="transientNamer", 

66 defaultSettingsFile=True 

67 ) 

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

69 

70 # tab completion for raw_input 

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

72 readline.parse_and_bind("tab: complete") 

73 readline.set_completer(tab_complete) 

74 

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

76 # AUTOMATICALLY 

77 a = {} 

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

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

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

81 else: 

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

83 a[varname] = val 

84 if arg == "--dbConn": 

85 dbConn = val 

86 a["dbConn"] = val 

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

88 

89 ## START LOGGING ## 

90 startTime = times.get_now_sql_datetime() 

91 log.info( 

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

93 (startTime,)) 

94 

95 cone = a["cone"] 

96 search = a["search"] 

97 new = a["new"] 

98 ra = a["ra"] 

99 dec = a["dec"] 

100 arcsecRadius = a["arcsecRadius"] 

101 name = a["name"] 

102 render = a["render"] 

103 tableNamePrefix = a["tableNamePrefix"] 

104 notes = a["notes"] 

105 

106 parse = a["importFlag"] 

107 mysql = a["mysql"] 

108 reportedInLastDays = a["reportedInLastDays"] 

109 withCommentsFlag = a["withCommentsFlag"] 

110 outputFlag = a["outputFlag"] 

111 

112 # set options interactively if user requests 

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

114 

115 # load previous settings 

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

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

118 try: 

119 with open(pathToPickleFile): 

120 pass 

121 previousSettingsExist = True 

122 except: 

123 previousSettingsExist = False 

124 previousSettings = {} 

125 if previousSettingsExist: 

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

127 

128 # x-raw-input 

129 # x-boolean-raw-input 

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

131 

132 # save the most recently used requests 

133 pickleMeObjects = [] 

134 pickleMe = {} 

135 theseLocals = locals() 

136 for k in pickleMeObjects: 

137 pickleMe[k] = theseLocals[k] 

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

139 

140 if "init" in a and a["init"]: 

141 from os.path import expanduser 

142 home = expanduser("~") 

143 filepath = home + "/.config/transientNamer/transientNamer.yaml" 

144 try: 

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

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

147 except: 

148 pass 

149 try: 

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

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

152 except: 

153 pass 

154 return 

155 

156 # CALL FUNCTIONS/OBJECTS 

157 if search or new or cone: 

158 if ra: 

159 tns = transientNamer.search( 

160 log=log, 

161 ra=ra, 

162 dec=dec, 

163 radiusArcsec=arcsecRadius, 

164 comments=withCommentsFlag, 

165 settings=settings 

166 ) 

167 if name: 

168 tns = transientNamer.search( 

169 log=log, 

170 name=name, 

171 comments=withCommentsFlag, 

172 settings=settings 

173 ) 

174 if reportedInLastDays: 

175 tns = transientNamer.search( 

176 log=log, 

177 discInLastDays=reportedInLastDays, 

178 comments=withCommentsFlag, 

179 settings=settings 

180 ) 

181 

182 # Recursively create missing directories 

183 if outputFlag and not os.path.exists(outputFlag): 

184 os.makedirs(outputFlag) 

185 

186 if tableNamePrefix: 

187 sources, phot, spec, files = tns.mysql( 

188 tableNamePrefix=tableNamePrefix, dirPath=outputFlag) 

189 numSources = len(sources.split("\n")) - 1 

190 elif not render or render == "table": 

191 sources, phot, spec, files = tns.table(dirPath=outputFlag) 

192 numSources = len(sources.split("\n")) - 4 

193 elif render == "csv": 

194 sources, phot, spec, files = tns.csv(dirPath=outputFlag) 

195 numSources = len(sources.split("\n")) - 1 

196 elif render == "json": 

197 sources, phot, spec, files = tns.json(dirPath=outputFlag) 

198 numSources = len(sources.split("{")) - 1 

199 elif render == "yaml": 

200 sources, phot, spec, files = tns.yaml(dirPath=outputFlag) 

201 numSources = len(sources.split("\n-")) 

202 elif render == "markdown": 

203 sources, phot, spec, files = tns.markdown(dirPath=outputFlag) 

204 numSources = len(sources.split("\n")) - 2 

205 

206 if numSources == 1: 

207 print("%(numSources)s transient found" % locals()) 

208 elif numSources > 1: 

209 print("%(numSources)s transients found" % locals()) 

210 

211 if not outputFlag: 

212 print("\n# Matched Transients") 

213 print(sources) 

214 print("\n# Transient Photometry") 

215 print(phot) 

216 print("\n# Transient Spectra") 

217 print(spec) 

218 print("\n# Transient Supplementary Files") 

219 print(files) 

220 print("\n# Original TNS Search URL") 

221 print(tns.url) 

222 

223 if notes: 

224 an = astronotes.astronotes( 

225 log=log, 

226 dbConn=dbConn, 

227 settings=settings 

228 ) 

229 downloadCount = an.download( 

230 cache_dir=settings["astronote-cache"], inLastDays=reportedInLastDays) 

231 print(f"{downloadCount} new astronotes downloaded and cached") 

232 

233 if parse: 

234 print(f"importing notes into database tables") 

235 an.notes_to_database() 

236 

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

238 dbConn.commit() 

239 dbConn.close() 

240 ## FINISH LOGGING ## 

241 endTime = times.get_now_sql_datetime() 

242 runningTime = times.calculate_time_difference(startTime, endTime) 

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

244 (endTime, runningTime, )) 

245 

246 return 

247 

248if __name__ == '__main__': 

249 main()