Coverage for soxspipe/cl_utils.py : 13%

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
6Usage:
7 soxspipe init
8 soxspipe mbias <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>]
9 soxspipe mdark <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>]
10 soxspipe disp_sol <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>]
11 soxspipe order_centres <inputFrames> [-o <outputDirectory> -s <pathToSettingsFile>]
13Options:
14 init setup the soxspipe settings file for the first time
15 mbias the master bias recipe
16 mdark the master dark recipe
17 disp_sol the disp solution recipe
18 order_centres the order centres recipe
20 inputFrames path to a directory of frames or a set-of-files file
22 -h, --help show this help message
23 -v, --version show version
24 -s, --settings <pathToSettingsFile> the settings file
25"""
26################# GLOBAL IMPORTS ####################
27import sys
28import os
29os.environ['TERM'] = 'vt100'
30import readline
31import glob
32import pickle
33from docopt import docopt
34from fundamentals import tools, times
35from subprocess import Popen, PIPE, STDOUT
38def tab_complete(text, state):
39 return (glob.glob(text + '*') + [None])[state]
42def main(arguments=None):
43 """
44 *The main function used when `cl_utils.py` is run as a single script from the cl, or when installed as a cl command*
45 """
46 # setup the command-line util settings
47 su = tools(
48 arguments=arguments,
49 docString=__doc__,
50 logLevel="ERROR",
51 options_first=False,
52 projectName="soxspipe",
53 defaultSettingsFile=True
54 )
55 arguments, settings, log, dbConn = su.setup()
57 # ALIGN ASTROPY LOGGING LEVEL WITH SOXSPIPES
58 try:
59 from astropy import log as astrolog
60 astrolog.setLevel(settings["logging settings"]["root"]["level"])
61 except:
62 pass
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)
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,))
83 ## START LOGGING ##
84 startTime = times.get_now_sql_datetime()
85 log.info(
86 '--- STARTING TO RUN THE cl_utils.py AT %s' %
87 (startTime,))
89 # set options interactively if user requests
90 if "interactiveFlag" in a and a["interactiveFlag"]:
92 # load previous settings
93 moduleDirectory = os.path.dirname(__file__) + "/resources"
94 pathToPickleFile = "%(moduleDirectory)s/previousSettings.p" % locals()
95 try:
96 with open(pathToPickleFile):
97 pass
98 previousSettingsExist = True
99 except:
100 previousSettingsExist = False
101 previousSettings = {}
102 if previousSettingsExist:
103 previousSettings = pickle.load(open(pathToPickleFile, "rb"))
105 # x-raw-input
106 # x-boolean-raw-input
107 # x-raw-input-with-default-value-from-previous-settings
109 # save the most recently used requests
110 pickleMeObjects = []
111 pickleMe = {}
112 theseLocals = locals()
113 for k in pickleMeObjects:
114 pickleMe[k] = theseLocals[k]
115 pickle.dump(pickleMe, open(pathToPickleFile, "wb"))
117 # PACK UP SOME OF THE CL SWITCHES INTO SETTINGS DICTIONARY
118 if a['outputDirectory']:
119 settings["intermediate-data-root"] = a['outputDirectory']
121 if a["init"]:
122 from os.path import expanduser
123 home = expanduser("~")
124 filepath = home + "/.config/soxspipe/soxspipe.yaml"
125 try:
126 cmd = """open %(filepath)s""" % locals()
127 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
128 except:
129 pass
130 try:
131 cmd = """start %(filepath)s""" % locals()
132 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
133 except:
134 pass
135 return
137 if a["mbias"]:
138 from soxspipe.recipes import soxs_mbias
139 recipe = soxs_mbias(
140 log=log,
141 settings=settings,
142 inputFrames=a["inputFrames"]
143 )
144 mbiasFrame = recipe.produce_product()
145 print("You can find the master bias frame at `%(mbiasFrame)s`" % locals())
147 if a["mdark"]:
148 from soxspipe.recipes import soxs_mdark
149 recipe = soxs_mdark(
150 log=log,
151 settings=settings,
152 inputFrames=a["inputFrames"]
153 )
154 mdarkFrame = recipe.produce_product()
155 print("You can find the master bias frame at `%(mdarkFrame)s`" % locals())
157 if a["disp_sol"]:
158 from soxspipe.recipes import soxs_disp_solution
159 disp_map = soxs_disp_solution(
160 log=log,
161 settings=settings,
162 inputFrames=a["inputFrames"]
163 ).produce_product()
164 print(f"\nSingle pinhole first guess dispersion map saved to: {disp_map}")
166 if a["order_centres"]:
167 from soxspipe.recipes import soxs_order_centres
168 order_table = soxs_order_centres(
169 log=log,
170 settings=settings,
171 inputFrames=a["inputFrames"]
172 ).produce_product()
173 print(f"\nThe order centre locations have been saved to an order table: {order_table}")
175 # CALL FUNCTIONS/OBJECTS
177 if "dbConn" in locals() and dbConn:
178 dbConn.commit()
179 dbConn.close()
180 ## FINISH LOGGING ##
181 endTime = times.get_now_sql_datetime()
182 runningTime = times.calculate_time_difference(startTime, endTime)
183 log.info('-- FINISHED ATTEMPT TO RUN THE cl_utils.py AT %s (RUNTIME: %s) --' %
184 (endTime, runningTime, ))
186 return
189if __name__ == '__main__':
190 main()