Coverage for neddy/cl_utils.py: 88%

83 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-09-20 10:57 +0000

1#!/usr/bin/env python 

2# encoding: utf-8 

3""" 

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

5 

6Usage: 

7 neddy init 

8 neddy [-nuvr] cone <ra> <dec> <radiusArcsec> [--o <outputFile>] 

9 neddy [-nuvr] cones <pathToCoordinateList> <radiusArcsec> [--o <outputFile>] 

10 neddy [-v] name <objectName> [<objectName>...] [--o <outputFile>] 

11 

12Commands:  

13 init initialise neddy for the first time 

14 cone perform a single conesearch on NED and return any matches 

15 cones perform bulk conesearches on NED and return any matches 

16 name perform name search(es) on NED and return any matches 

17 

18Arguments: 

19 ra ra (decimal degrees or sexegesimal) 

20 dec dec (decimal degrees or sexegesimal) 

21 radiusArcsec radiusArcsec (conesearch radius) 

22 objectName objectName (the name of the object) 

23 pathToCoordinateList path to list of space separated ra & dec (one coordinate set per line, decimal degrees or sexegesimal) 

24 --o outputFile path to outputFile  

25  

26Options: 

27 -h, --help show this help message 

28 -n, --nearest return the nearest object only 

29 -u, --unclassified include unclassified extra-galactic objects 

30 -v, --verbose return more metadata for matches 

31 -r, --redshift redshift must be available 

32""" 

33from subprocess import Popen, PIPE, STDOUT 

34from fundamentals import tools, times 

35from docopt import docopt 

36import pickle 

37import glob 

38import readline 

39from builtins import str 

40import sys 

41import os 

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

43 

44 

45def tab_complete(text, state): 

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

47 

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="neddy", 

60 defaultSettingsFile=True 

61 ) 

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

63 

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

65 # AUTOMATICALLY 

66 a = {} 

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

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

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

70 else: 

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

72 a[varname] = val 

73 if arg == "--dbConn": 

74 dbConn = val 

75 a["dbConn"] = val 

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

77 

78 ## START LOGGING ## 

79 startTime = times.get_now_sql_datetime() 

80 log.info( 

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

82 (startTime,)) 

83 

84 if a["init"]: 

85 from os.path import expanduser 

86 home = expanduser("~") 

87 filepath = home + "/.config/neddy/neddy.yaml" 

88 try: 

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

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

91 except: 

92 pass 

93 try: 

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

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

96 except: 

97 pass 

98 return 

99 

100 # UNPACK USEAGE COMMANDS & ARGUMENTS 

101 ra = a["ra"] 

102 dec = a["dec"] 

103 radiusArcsec = a["radiusArcsec"] 

104 objectName = a["objectName"] 

105 name = a["name"] 

106 pathToCoordinateList = a["pathToCoordinateList"] 

107 outputFile = a["oFlag"] 

108 nearestFlag = a["nearestFlag"] 

109 unclassifiedFlag = a["unclassifiedFlag"] 

110 redshiftFlag = a["redshiftFlag"] 

111 verboseFlag = a["verboseFlag"] 

112 cone = a["cone"] 

113 cones = a["cones"] 

114 

115 # CALL FUNCTIONS/OBJECTS 

116 if cones: 

117 import codecs 

118 pathToReadFile = pathToCoordinateList 

119 readFile = codecs.open(pathToReadFile, encoding='utf-8', mode='r') 

120 

121 listOfCoordinates = [] 

122 for line in readFile.readlines(): 

123 line = line.strip() 

124 [ra, dec] = line.split() 

125 listOfCoordinates.append(str(ra) + " " + str(dec)) 

126 from .conesearch import conesearch 

127 search = conesearch( 

128 log=log, 

129 radiusArcsec=radiusArcsec, 

130 nearestOnly=nearestFlag, 

131 unclassified=unclassifiedFlag, 

132 listOfCoordinates=listOfCoordinates, 

133 outputFilePath=outputFile, 

134 verbose=verboseFlag, 

135 redshift=redshiftFlag) 

136 elif cone: 

137 from .conesearch import conesearch 

138 search = conesearch( 

139 log=log, 

140 ra=ra, 

141 dec=dec, 

142 radiusArcsec=radiusArcsec, 

143 nearestOnly=nearestFlag, 

144 unclassified=unclassifiedFlag, 

145 outputFilePath=outputFile, 

146 verbose=verboseFlag, 

147 redshift=redshiftFlag 

148 ) 

149 elif name: 

150 from .namesearch import namesearch 

151 search = namesearch( 

152 log=log, 

153 names=objectName, 

154 verbose=verboseFlag, 

155 outputFilePath=outputFile 

156 ) 

157 results = search.get() 

158 

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

160 dbConn.commit() 

161 dbConn.close() 

162 ## FINISH LOGGING ## 

163 endTime = times.get_now_sql_datetime() 

164 runningTime = times.calculate_time_difference(startTime, endTime) 

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

166 (endTime, runningTime, )) 

167 

168 return 

169 

170 

171if __name__ == '__main__': 

172 main()