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

5 

6Usage: 

7 soxspipe init 

8 soxspipe [-V] mbias <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>]  

9 soxspipe [-V] mdark <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>] 

10 soxspipe [-V] mflat <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>] 

11 soxspipe [-V] disp_sol <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>] 

12 soxspipe [-V] order_centres <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>] 

13 soxspipe [-V] spat_sol <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>] 

14 

15Options: 

16 init setup the soxspipe settings file for the first time 

17 mbias the master bias recipe 

18 mdark the master dark recipe 

19 mflat the master flat recipe 

20 disp_sol the disp solution recipe 

21 order_centres the order centres recipe 

22 spat_sol the spatial solution recipe 

23 

24 inputFrames path to a directory of frames or a set-of-files file 

25 

26 -h, --help show this help message 

27 -v, --version show version 

28 -s, --settings <pathToSettingsFile> the settings file 

29 -V, --verbose more verbose output 

30""" 

31################# GLOBAL IMPORTS #################### 

32import sys 

33import os 

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

35import readline 

36import glob 

37import pickle 

38from docopt import docopt 

39from fundamentals import tools, times 

40from subprocess import Popen, PIPE, STDOUT 

41 

42 

43def tab_complete(text, state): 

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

45 

46 

47def main(arguments=None): 

48 """ 

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

50 """ 

51 # setup the command-line util settings 

52 su = tools( 

53 arguments=arguments, 

54 docString=__doc__, 

55 logLevel="ERROR", 

56 options_first=False, 

57 projectName="soxspipe", 

58 defaultSettingsFile=True 

59 ) 

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

61 

62 # ALIGN ASTROPY LOGGING LEVEL WITH SOXSPIPES 

63 try: 

64 from astropy import log as astrolog 

65 astrolog.setLevel(settings["logging settings"]["root"]["level"]) 

66 except: 

67 pass 

68 

69 # tab completion for raw_input 

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

71 readline.parse_and_bind("tab: complete") 

72 readline.set_completer(tab_complete) 

73 

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

75 # AUTOMATICALLY 

76 a = {} 

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

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

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

80 else: 

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

82 a[varname] = val 

83 if arg == "--dbConn": 

84 dbConn = val 

85 a["dbConn"] = val 

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

87 

88 ## START LOGGING ## 

89 startTime = times.get_now_sql_datetime() 

90 log.info( 

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

92 (startTime,)) 

93 

94 # set options interactively if user requests 

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

96 

97 # load previous settings 

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

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

100 try: 

101 with open(pathToPickleFile): 

102 pass 

103 previousSettingsExist = True 

104 except: 

105 previousSettingsExist = False 

106 previousSettings = {} 

107 if previousSettingsExist: 

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

109 

110 # x-raw-input 

111 # x-boolean-raw-input 

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

113 

114 # save the most recently used requests 

115 pickleMeObjects = [] 

116 pickleMe = {} 

117 theseLocals = locals() 

118 for k in pickleMeObjects: 

119 pickleMe[k] = theseLocals[k] 

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

121 

122 verbose = a['verboseFlag'] 

123 

124 # PACK UP SOME OF THE CL SWITCHES INTO SETTINGS DICTIONARY 

125 if a['outputDirectory']: 

126 settings["intermediate-data-root"] = a['outputDirectory'] 

127 

128 if a["init"]: 

129 from os.path import expanduser 

130 home = expanduser("~") 

131 filepath = home + "/.config/soxspipe/soxspipe.yaml" 

132 try: 

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

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

135 except: 

136 pass 

137 try: 

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

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

140 except: 

141 pass 

142 return 

143 

144 if a["mbias"]: 

145 from soxspipe.recipes import soxs_mbias 

146 recipe = soxs_mbias( 

147 log=log, 

148 settings=settings, 

149 inputFrames=a["inputFrames"], 

150 verbose=verbose 

151 ) 

152 mbiasFrame = recipe.produce_product() 

153 print("You can find the master bias frame at `%(mbiasFrame)s`" % locals()) 

154 

155 if a["mdark"]: 

156 from soxspipe.recipes import soxs_mdark 

157 recipe = soxs_mdark( 

158 log=log, 

159 settings=settings, 

160 inputFrames=a["inputFrames"], 

161 verbose=verbose 

162 ) 

163 mdarkFrame = recipe.produce_product() 

164 print("You can find the master bias frame at `%(mdarkFrame)s`" % locals()) 

165 

166 if a["disp_sol"]: 

167 from soxspipe.recipes import soxs_disp_solution 

168 disp_map = soxs_disp_solution( 

169 log=log, 

170 settings=settings, 

171 inputFrames=a["inputFrames"], 

172 verbose=verbose 

173 ).produce_product() 

174 print(f"\nSingle pinhole first guess dispersion map saved to: {disp_map}") 

175 

176 if a["order_centres"]: 

177 from soxspipe.recipes import soxs_order_centres 

178 order_table = soxs_order_centres( 

179 log=log, 

180 settings=settings, 

181 inputFrames=a["inputFrames"], 

182 verbose=verbose 

183 ).produce_product() 

184 print(f"\nThe order centre locations have been saved to an order table: {order_table}") 

185 

186 if a["spat_sol"]: 

187 from soxspipe.recipes import soxs_spatial_solution 

188 disp_map = soxs_spatial_solution( 

189 log=log, 

190 settings=settings, 

191 inputFrames=a["inputFrames"], 

192 verbose=verbose 

193 ).produce_product() 

194 print(f"\nFull 2D dispersion map saved to: {disp_map}") 

195 

196 if a["mflat"]: 

197 from soxspipe.recipes import soxs_mflat 

198 recipe = soxs_mflat( 

199 log=log, 

200 settings=settings, 

201 inputFrames=a["inputFrames"], 

202 verbose=verbose 

203 ) 

204 mflatFrame = recipe.produce_product() 

205 

206 # CALL FUNCTIONS/OBJECTS 

207 

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

209 dbConn.commit() 

210 dbConn.close() 

211 ## FINISH LOGGING ## 

212 endTime = times.get_now_sql_datetime() 

213 runningTime = times.calculate_time_difference(startTime, endTime) 

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

215 (endTime, runningTime, )) 

216 

217 return 

218 

219 

220if __name__ == '__main__': 

221 main()