Coverage for lst_auto_rta/High_Level_analysis.py: 0%

109 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-26 14:48 +0000

1#!/usr/bin/env python 

2 

3import datetime 

4import logging 

5import os 

6import time 

7 

8import pymongo 

9 

10def wait_for_directory(directory, interval=5): 

11 """ 

12 Wait for the directory to exist 

13 

14 :param directory: directory path 

15 :param interval: Time interval between verification 

16 """ 

17 while not os.path.exists(directory): 

18 print(f"{directory} does not exist yet. let's wait {interval} seconds...") 

19 time.sleep(interval) 

20 print(f"The directory {directory} finally exist !!!") 

21 

22 

23def now(): 

24 """ 

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

26 

27 :return: float 

28 """ 

29 return datetime.datetime.timestamp(datetime.datetime.now()) 

30 

31 

32def get_current_run(): 

33 """ 

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

35 

36 :return: int 

37 """ 

38 try: 

39 client = pymongo.MongoClient("lst101") 

40 database = client["lst1_obs_summary"] 

41 camera_collection = database["camera"] 

42 summaries = camera_collection.find({}) 

43 run_table = [] 

44 summaries = camera_collection.find({}) 

45 for summary in summaries: 

46 if ( 

47 summary["tstart"] 

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

49 ): 

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

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

52 return run_table[-1] 

53 else: 

54 return run_table[-1] + 1 

55 except Exception as e: 

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

57 return -1 

58 

59 

60def get_night_timestamp(today): 

61 """ 

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

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

64 

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

66 

67 :param today: datetime object 

68 datetime object with current local time 

69 

70 :return: 

71 timestamp: str 

72 """ 

73 # TODO check that today is a datetime object ? 

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

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

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

77 else: 

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

79 

80 return timestamp 

81 

82 

83def Obtain_RA_DEC(runid): 

84 try: 

85 client = pymongo.MongoClient("lst101") 

86 database = client["bridgesmonitoring"] 

87 print(client) 

88 

89 client = pymongo.MongoClient("lst101") 

90 database = client["lst1_obs_summary"] 

91 camera_collection = database["telescope"] 

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

93 

94 for summary in summaries: 

95 camera = summary["data"]["camera"] 

96 count = 0 

97 structure = summary["data"]["structure"] 

98 for c in camera: 

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

100 run_count = count 

101 count = count + 1 

102 run_parameters = structure[run_count] 

103 return ( 

104 run_parameters["target"]["ra"], 

105 run_parameters["target"]["dec"], 

106 run_parameters["target"]["source_ra"], 

107 run_parameters["target"]["source_dec"], 

108 ) 

109 except Exception as e: 

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

111 # database = client['lst1_obs_summary'] 

112 # camera_collection = database['camera'] 

113 # summaries = camera_collection.find({}) 

114 # for summary in summaries: 

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

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

117 # return [0,0,0,0] 

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

119 return [0, 0, 0, 0] 

120 

121def today_to_directory(today): 

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

123 

124def main(): 

125 """ 

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

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

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

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

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

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

132 """ 

133 current_run_is = -1 

134 quit = False 

135 

136 Start_time = now() 

137 

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

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

140 

141 wait_for_directory("/fefs/onsite/pipeline/rta/data/"+today_to_directory(today)) 

142 logging.basicConfig(filename="/fefs/onsite/pipeline/rta/data/"+today_to_directory(today)+"/log_High_Level_" + today + ".txt", level=logging.INFO) 

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

144 

145 RTA_ready = True 

146 

147 loop_id = 0 

148 ra_dec = [0, 0, 0, 0] 

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

150 while quit == False and RTA_ready: 

151 time.sleep(10) 

152 # print(current_run_is+1) 

153 if current_run_is == -1: 

154 current_run_is = get_current_run() 

155 if current_run_is == -1: 

156 logging.info("None") 

157 current_run_is = -1 

158 continue 

159 if current_run_is != get_current_run(): 

160 time.sleep(1) 

161 current_run_is = get_current_run() 

162 if current_run_is != -1: 

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

164 time.sleep(2) 

165 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

166 logging.info( 

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

168 + today_to_directory(today) 

169 + " -r " 

170 + str(current_run_is + 1) 

171 + " -RA " 

172 + str(ra_dec[2]) 

173 + " -DEC " 

174 + str(ra_dec[3]) 

175 ) 

176 os.system( 

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

178 + today_to_directory(today) 

179 + " -r " 

180 + str(current_run_is + 1) 

181 + " -RA " 

182 + str(ra_dec[2]) 

183 + " -DEC " 

184 + str(ra_dec[3]) 

185 ) 

186 logging.info( 

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

188 + today_to_directory(today) 

189 + " -r " 

190 + str(current_run_is + 1) 

191 + " -RA " 

192 + str(ra_dec[2]) 

193 + " -DEC " 

194 + str(ra_dec[3]) 

195 ) 

196 os.system( 

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

198 + today_to_directory(today) 

199 + " -r " 

200 + str(current_run_is + 1) 

201 + " -RA " 

202 + str(ra_dec[2]) 

203 + " -DEC " 

204 + str(ra_dec[3]) 

205 ) 

206 logging.info( 

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

208 + today_to_directory(today) 

209 + "/" 

210 + str(current_run_is + 1) 

211 + "/DL3 -ra " 

212 + str(ra_dec[2]) 

213 + " -dec " 

214 + str(ra_dec[3]) 

215 + " --output_dir "+ data_directory 

216 + today_to_directory(today) 

217 + "/" 

218 + str(current_run_is + 1) 

219 + "/plots --date " 

220 + today 

221 ) 

222 os.system( 

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

224 + today_to_directory(today) 

225 + "/" 

226 + str(current_run_is + 1) 

227 + "/DL3 -ra " 

228 + str(ra_dec[2]) 

229 + " -dec " 

230 + str(ra_dec[3]) 

231 + " --output_dir " + data_directory 

232 + today_to_directory(today) 

233 + "/" 

234 + str(current_run_is + 1) 

235 + "/plots --date " 

236 + today 

237 ) 

238 

239 if loop_id % 1 == 0: 

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

241 time.sleep(2) 

242 logging.info(datetime.datetime.now()) 

243 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

244 logging.info( 

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

246 + today_to_directory(today) 

247 + " -r " 

248 + str(current_run_is + 1) 

249 + " -RA " 

250 + str(ra_dec[2]) 

251 + " -DEC " 

252 + str(ra_dec[3]) 

253 ) 

254 os.system( 

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

256 + today_to_directory(today) 

257 + " -r " 

258 + str(current_run_is + 1) 

259 + " -RA " 

260 + str(ra_dec[2]) 

261 + " -DEC " 

262 + str(ra_dec[3]) 

263 ) 

264 logging.info( 

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

266 + today_to_directory(today) 

267 + " -r " 

268 + str(current_run_is + 1) 

269 + " -RA " 

270 + str(ra_dec[2]) 

271 + " -DEC " 

272 + str(ra_dec[3]) 

273 ) 

274 os.system( 

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

276 + today_to_directory(today) 

277 + " -r " 

278 + str(current_run_is + 1) 

279 + " -RA " 

280 + str(ra_dec[2]) 

281 + " -DEC " 

282 + str(ra_dec[3]) 

283 ) 

284 logging.info( 

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

286 + today_to_directory(today) 

287 + "/" 

288 + str(current_run_is + 1) 

289 + "/DL3 -ra " 

290 + str(ra_dec[2]) 

291 + " -dec " 

292 + str(ra_dec[3]) 

293 + " --output_dir "+ data_directory 

294 + today_to_directory(today) 

295 + "/" 

296 + str(current_run_is + 1) 

297 + "/plots --date " 

298 + today 

299 ) 

300 os.system( 

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

302 + today_to_directory(today) 

303 + "/" 

304 + str(current_run_is + 1) 

305 + "/DL3 -ra " 

306 + str(ra_dec[2]) 

307 + " -dec " 

308 + str(ra_dec[3]) 

309 + " --output_dir "+ data_directory 

310 + today_to_directory(today) 

311 + "/" 

312 + str(current_run_is + 1) 

313 + "/plots --date " 

314 + today 

315 ) 

316 

317 loop_id = loop_id + 1 

318 if ( 

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

320 ) > 14 * 3600: 

321 quit = True 

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

323 

324 

325if __name__ == "__main__": 

326 main()