Coverage for lst_auto_rta/High_Level_analysis_Maps.py: 0%

101 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 summary["tstart"] > datetime.datetime.timestamp(datetime.datetime.now()) - 3600 * 24 * 100: 

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

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

49 return run_table[-1] 

50 else: 

51 return run_table[-1] + 1 

52 except Exception as e: 

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

54 return -1 

55 

56 

57def get_night_timestamp(today): 

58 """ 

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

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

61 

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

63 

64 :param today: datetime object 

65 datetime object with current local time 

66 

67 :return: 

68 timestamp: str 

69 """ 

70 # TODO check that today is a datetime object ? 

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

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

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

74 else: 

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

76 

77 return timestamp 

78 

79 

80def Obtain_RA_DEC(runid): 

81 try: 

82 client = pymongo.MongoClient("lst101") 

83 database = client["bridgesmonitoring"] 

84 print(client) 

85 

86 client = pymongo.MongoClient("lst101") 

87 database = client["lst1_obs_summary"] 

88 camera_collection = database["telescope"] 

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

90 

91 for summary in summaries: 

92 camera = summary["data"]["camera"] 

93 count = 0 

94 structure = summary["data"]["structure"] 

95 for c in camera: 

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

97 run_count = count 

98 count = count + 1 

99 run_parameters = structure[run_count] 

100 return ( 

101 run_parameters["target"]["ra"], 

102 run_parameters["target"]["dec"], 

103 run_parameters["target"]["source_ra"], 

104 run_parameters["target"]["source_dec"], 

105 ) 

106 except Exception as e: 

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

108 # database = client['lst1_obs_summary'] 

109 # camera_collection = database['camera'] 

110 # summaries = camera_collection.find({}) 

111 # for summary in summaries: 

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

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

114 # return [0,0,0,0] 

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

116 return [0, 0, 0, 0] 

117 

118def today_to_directory(today): 

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

120 

121def main(): 

122 """ 

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

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

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

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

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

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

129 """ 

130 current_run_is = -1 

131 quit = False 

132 

133 Start_time = now() 

134 

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

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

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

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

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

140 

141 RTA_ready = True 

142 

143 loop_id = 0 

144 ra_dec = [0, 0, 0, 0] 

145 data_directory = "'/fefs/onsite/pipeline/rta/data/" 

146 while quit == False and RTA_ready: 

147 time.sleep(10) 

148 # print(current_run_is+1) 

149 if current_run_is == -1: 

150 current_run_is = get_current_run() 

151 if current_run_is == -1: 

152 logging.info("None") 

153 current_run_is = -1 

154 continue 

155 if current_run_is != get_current_run(): 

156 time.sleep(1) 

157 current_run_is = get_current_run() 

158 if current_run_is != -1: 

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

160 time.sleep(2) 

161 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

162 logging.info( 

163 "srun --reservation=rta_one_node ./Ring_Background_Maps.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 os.system( 

173 "srun --reservation=rta_one_node ./Ring_Background_Maps.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 

183 if loop_id % 1 == 0: 

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

185 time.sleep(2) 

186 logging.info(datetime.datetime.now()) 

187 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

188 logging.info( 

189 "srun --reservation=rta_one_node ./Ring_Background_Maps.py -da " 

190 + today_to_directory(today) 

191 + " -r " 

192 + str(current_run_is + 1) 

193 + " -RA " 

194 + str(ra_dec[2]) 

195 + " -DEC " 

196 + str(ra_dec[3]) 

197 ) 

198 os.system( 

199 "srun --reservation=rta_one_node ./Ring_Background_Maps.py -da " 

200 + today_to_directory(today) 

201 + " -r " 

202 + str(current_run_is + 1) 

203 + " -RA " 

204 + str(ra_dec[2]) 

205 + " -DEC " 

206 + str(ra_dec[3]) 

207 ) 

208 

209 loop_id = loop_id + 1 

210 if (datetime.datetime.timestamp(datetime.datetime.now()) - Start_time) > 14 * 3600: 

211 quit = True 

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

213 

214 

215if __name__ == "__main__": 

216 main()