Coverage for lst_auto_rta/High_Level_analysis.py: 0%

103 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-22 14:47 +0000

1#!/usr/bin/env python 

2 

3import datetime 

4import logging 

5import os 

6import time 

7 

8import pymongo 

9 

10 

11def now(): 

12 """ 

13 Returns the current timestamp in seconds, relative to the Unix epoch. 

14 

15 :return: float 

16 """ 

17 return datetime.datetime.timestamp(datetime.datetime.now()) 

18 

19 

20def get_current_run(): 

21 """ 

22 Attempts to retrieve the current run number from the lst1_obs_summary database. 

23 

24 :return: int 

25 """ 

26 try: 

27 client = pymongo.MongoClient("lst101") 

28 database = client["lst1_obs_summary"] 

29 camera_collection = database["camera"] 

30 summaries = camera_collection.find({}) 

31 run_table = [] 

32 summaries = camera_collection.find({}) 

33 for summary in summaries: 

34 if ( 

35 summary["tstart"] 

36 > datetime.datetime.timestamp(datetime.datetime.now()) - 3600 * 24 * 100 

37 ): 

38 run_table.append(summary["run_number"]) 

39 if run_table[-2] != run_table[-1]: 

40 return run_table[-1] 

41 else: 

42 return run_table[-1] + 1 

43 except Exception as e: 

44 logging.error("Error DB !!!!!") 

45 return -1 

46 

47 

48def get_night_timestamp(today): 

49 """ 

50 Get current timestamp (YYYYMMDD) with respect to the night. 

51 It will consider a same night everything run between 08h00 until 07h59 of the next day. 

52 

53 Ex: An observation launched at 04h00 (local time) will refer to the previous day. 

54 

55 :param today: datetime object 

56 datetime object with current local time 

57 

58 :return: 

59 timestamp: str 

60 """ 

61 # TODO check that today is a datetime object ? 

62 if today.hour < 8: # script launched between 

63 yesterday = today - datetime.timedelta(days=1) 

64 timestamp = f"{yesterday.year:04d}{yesterday.month:02d}{yesterday.day:02d}" 

65 else: 

66 timestamp = f"{today.year:04d}{today.month:02d}{today.day:02d}" 

67 

68 return timestamp 

69 

70 

71def Obtain_RA_DEC(runid): 

72 try: 

73 client = pymongo.MongoClient("lst101") 

74 database = client["bridgesmonitoring"] 

75 print(client) 

76 

77 client = pymongo.MongoClient("lst101") 

78 database = client["lst1_obs_summary"] 

79 camera_collection = database["telescope"] 

80 summaries = camera_collection.find({"data.camera.run_number": runid}) 

81 

82 for summary in summaries: 

83 camera = summary["data"]["camera"] 

84 count = 0 

85 structure = summary["data"]["structure"] 

86 for c in camera: 

87 if c["run_number"] == runid: 

88 run_count = count 

89 count = count + 1 

90 run_parameters = structure[run_count] 

91 return ( 

92 run_parameters["target"]["ra"], 

93 run_parameters["target"]["dec"], 

94 run_parameters["target"]["source_ra"], 

95 run_parameters["target"]["source_dec"], 

96 ) 

97 except Exception as e: 

98 # client = pymongo.MongoClient('lst101') 

99 # database = client['lst1_obs_summary'] 

100 # camera_collection = database['camera'] 

101 # summaries = camera_collection.find({}) 

102 # for summary in summaries: 

103 # if (summary['tstart'] > datetime.datetime.timestamp(datetime.datetime.now())-3600*24*100): 

104 # if (summary['run_number']==runid and summary["kind"] != "data_taking"): 

105 # return [0,0,0,0] 

106 logging.error("Error DB !!!!!") 

107 return [0, 0, 0, 0] 

108 

109def today_to_directory(today): 

110 return today[0:4]+"/"+today[4:6]+"/"+today[6:8] 

111 

112def main(): 

113 """ 

114 Initialize current_run_is and quit variables and get the current timestamp with Start_time = now() 

115 Initialize the logging system by setting the log file name and logging level 

116 Change the current working directory to /fefs/onsite/pipeline/rta/data 

117 Find the current reservations for nodes using the scontrol command and parse the output to find the individual node names 

118 Modify a configuration file with sed commands, replacing placeholders with the node names found in step 4 

119 Check if the ib0 network interface is in connected mode for each of the nodes and log the results 

120 """ 

121 current_run_is = -1 

122 quit = False 

123 

124 Start_time = now() 

125 

126 # today = str(datetime.datetime.now().year) + str(datetime.datetime.now().month) + str(datetime.datetime.now().day) 

127 today = get_night_timestamp(datetime.datetime.now()) 

128 logging.basicConfig(filename="log_High_Level_" + today + ".txt", level=logging.INFO) 

129 logging.info("Start RTA High Level for the day " + today) 

130 

131 RTA_ready = True 

132 

133 loop_id = 0 

134 ra_dec = [0, 0, 0, 0] 

135 data_directory = "/fefs/onsite/pipeline/rta/data/" 

136 while quit == False and RTA_ready: 

137 time.sleep(10) 

138 # print(current_run_is+1) 

139 if current_run_is == -1: 

140 current_run_is = get_current_run() 

141 if current_run_is == -1: 

142 logging.info("None") 

143 current_run_is = -1 

144 continue 

145 if current_run_is != get_current_run(): 

146 time.sleep(1) 

147 current_run_is = get_current_run() 

148 if current_run_is != -1: 

149 logging.info("Start RTA check for run " + str(current_run_is)) 

150 time.sleep(2) 

151 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

152 logging.info( 

153 "srun --reservation=rta_one_node ./Theta_square.py -da " 

154 + today_to_directory(today) 

155 + " -r " 

156 + str(current_run_is + 1) 

157 + " -RA " 

158 + str(ra_dec[2]) 

159 + " -DEC " 

160 + str(ra_dec[3]) 

161 ) 

162 os.system( 

163 "srun --reservation=rta_one_node ./Theta_square.py -da " 

164 + today_to_directory(today) 

165 + " -r " 

166 + str(current_run_is + 1) 

167 + " -RA " 

168 + str(ra_dec[2]) 

169 + " -DEC " 

170 + str(ra_dec[3]) 

171 ) 

172 logging.info( 

173 "srun --reservation=rta_one_node ./Spectra.py -da " 

174 + today_to_directory(today) 

175 + " -r " 

176 + str(current_run_is + 1) 

177 + " -RA " 

178 + str(ra_dec[2]) 

179 + " -DEC " 

180 + str(ra_dec[3]) 

181 ) 

182 os.system( 

183 "srun --reservation=rta_one_node ./Spectra.py -da " 

184 + today_to_directory(today) 

185 + " -r " 

186 + str(current_run_is + 1) 

187 + " -RA " 

188 + str(ra_dec[2]) 

189 + " -DEC " 

190 + str(ra_dec[3]) 

191 ) 

192 logging.info( 

193 "srun --reservation=rta_one_node ./rta_var.py --dl3_folder " + data_directory 

194 + today_to_directory(today) 

195 + "/" 

196 + str(current_run_is + 1) 

197 + "/DL3 -ra " 

198 + str(ra_dec[2]) 

199 + " -dec " 

200 + str(ra_dec[3]) 

201 + " --output_dir "+ data_directory 

202 + today_to_directory(today) 

203 + "/" 

204 + str(current_run_is + 1) 

205 + "/plots --date " 

206 + today 

207 ) 

208 os.system( 

209 "srun --reservation=rta_one_node ./rta_var.py --dl3_folder " + data_directory 

210 + today_to_directory(today) 

211 + "/" 

212 + str(current_run_is + 1) 

213 + "/DL3 -ra " 

214 + str(ra_dec[2]) 

215 + " -dec " 

216 + str(ra_dec[3]) 

217 + " --output_dir " + data_directory 

218 + today_to_directory(today) 

219 + "/" 

220 + str(current_run_is + 1) 

221 + "/plots --date " 

222 + today 

223 ) 

224 

225 if loop_id % 1 == 0: 

226 logging.info("Start RTA check for run " + str(current_run_is)) 

227 time.sleep(2) 

228 logging.info(datetime.datetime.now()) 

229 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

230 logging.info( 

231 "srun --reservation=rta_one_node ./Theta_square.py -da " 

232 + today_to_directory(today) 

233 + " -r " 

234 + str(current_run_is + 1) 

235 + " -RA " 

236 + str(ra_dec[2]) 

237 + " -DEC " 

238 + str(ra_dec[3]) 

239 ) 

240 os.system( 

241 "srun --reservation=rta_one_node ./Theta_square.py -da " 

242 + today_to_directory(today) 

243 + " -r " 

244 + str(current_run_is + 1) 

245 + " -RA " 

246 + str(ra_dec[2]) 

247 + " -DEC " 

248 + str(ra_dec[3]) 

249 ) 

250 logging.info( 

251 "srun --reservation=rta_one_node ./Spectra.py -da " 

252 + today_to_directory(today) 

253 + " -r " 

254 + str(current_run_is + 1) 

255 + " -RA " 

256 + str(ra_dec[2]) 

257 + " -DEC " 

258 + str(ra_dec[3]) 

259 ) 

260 os.system( 

261 "srun --reservation=rta_one_node ./Spectra.py -da " 

262 + today_to_directory(today) 

263 + " -r " 

264 + str(current_run_is + 1) 

265 + " -RA " 

266 + str(ra_dec[2]) 

267 + " -DEC " 

268 + str(ra_dec[3]) 

269 ) 

270 logging.info( 

271 "srun --reservation=rta_one_node ./rta_var.py --dl3_folder "+ data_directory 

272 + today_to_directory(today) 

273 + "/" 

274 + str(current_run_is + 1) 

275 + "/DL3 -ra " 

276 + str(ra_dec[2]) 

277 + " -dec " 

278 + str(ra_dec[3]) 

279 + " --output_dir "+ data_directory 

280 + today_to_directory(today) 

281 + "/" 

282 + str(current_run_is + 1) 

283 + "/plots --date " 

284 + today 

285 ) 

286 os.system( 

287 "srun --reservation=rta_one_node ./rta_var.py --dl3_folder "+ data_directory 

288 + today_to_directory(today) 

289 + "/" 

290 + str(current_run_is + 1) 

291 + "/DL3 -ra " 

292 + str(ra_dec[2]) 

293 + " -dec " 

294 + str(ra_dec[3]) 

295 + " --output_dir "+ data_directory 

296 + today_to_directory(today) 

297 + "/" 

298 + str(current_run_is + 1) 

299 + "/plots --date " 

300 + today 

301 ) 

302 

303 loop_id = loop_id + 1 

304 if ( 

305 datetime.datetime.timestamp(datetime.datetime.now()) - Start_time 

306 ) > 14 * 3600: 

307 quit = True 

308 logging.info("End of the night, Stop the RTA check") 

309 

310 

311if __name__ == "__main__": 

312 main()