Coverage for lst_auto_rta/High_Level_analysis_Maps.py: 0%

95 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-03 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 summary["tstart"] > datetime.datetime.timestamp(datetime.datetime.now()) - 3600 * 24 * 100: 

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

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

37 return run_table[-1] 

38 else: 

39 return run_table[-1] + 1 

40 except Exception as e: 

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

42 return -1 

43 

44 

45def get_night_timestamp(today): 

46 """ 

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

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

49 

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

51 

52 :param today: datetime object 

53 datetime object with current local time 

54 

55 :return: 

56 timestamp: str 

57 """ 

58 # TODO check that today is a datetime object ? 

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

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

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

62 else: 

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

64 

65 return timestamp 

66 

67 

68def Obtain_RA_DEC(runid): 

69 try: 

70 client = pymongo.MongoClient("lst101") 

71 database = client["bridgesmonitoring"] 

72 print(client) 

73 

74 client = pymongo.MongoClient("lst101") 

75 database = client["lst1_obs_summary"] 

76 camera_collection = database["telescope"] 

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

78 

79 for summary in summaries: 

80 camera = summary["data"]["camera"] 

81 count = 0 

82 structure = summary["data"]["structure"] 

83 for c in camera: 

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

85 run_count = count 

86 count = count + 1 

87 run_parameters = structure[run_count] 

88 return ( 

89 run_parameters["target"]["ra"], 

90 run_parameters["target"]["dec"], 

91 run_parameters["target"]["source_ra"], 

92 run_parameters["target"]["source_dec"], 

93 ) 

94 except Exception as e: 

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

96 # database = client['lst1_obs_summary'] 

97 # camera_collection = database['camera'] 

98 # summaries = camera_collection.find({}) 

99 # for summary in summaries: 

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

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

102 # return [0,0,0,0] 

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

104 return [0, 0, 0, 0] 

105 

106def today_to_directory(today): 

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

108 

109def main(): 

110 """ 

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

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

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

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

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

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

117 """ 

118 current_run_is = -1 

119 quit = False 

120 

121 Start_time = now() 

122 

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

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

125 logging.basicConfig(filename="log_High_Level_Maps_" + today + ".txt", level=logging.INFO) 

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

127 

128 RTA_ready = True 

129 

130 loop_id = 0 

131 ra_dec = [0, 0, 0, 0] 

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

133 while quit == False and RTA_ready: 

134 time.sleep(10) 

135 # print(current_run_is+1) 

136 if current_run_is == -1: 

137 current_run_is = get_current_run() 

138 if current_run_is == -1: 

139 logging.info("None") 

140 current_run_is = -1 

141 continue 

142 if current_run_is != get_current_run(): 

143 time.sleep(1) 

144 current_run_is = get_current_run() 

145 if current_run_is != -1: 

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

147 time.sleep(2) 

148 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

149 logging.info( 

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

151 + today_to_directory(today) 

152 + " -r " 

153 + str(current_run_is + 1) 

154 + " -RA " 

155 + str(ra_dec[2]) 

156 + " -DEC " 

157 + str(ra_dec[3]) 

158 ) 

159 os.system( 

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

161 + today_to_directory(today) 

162 + " -r " 

163 + str(current_run_is + 1) 

164 + " -RA " 

165 + str(ra_dec[2]) 

166 + " -DEC " 

167 + str(ra_dec[3]) 

168 ) 

169 

170 if loop_id % 1 == 0: 

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

172 time.sleep(2) 

173 logging.info(datetime.datetime.now()) 

174 ra_dec = Obtain_RA_DEC(current_run_is + 1) 

175 logging.info( 

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

177 + today_to_directory(today) 

178 + " -r " 

179 + str(current_run_is + 1) 

180 + " -RA " 

181 + str(ra_dec[2]) 

182 + " -DEC " 

183 + str(ra_dec[3]) 

184 ) 

185 os.system( 

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

187 + today_to_directory(today) 

188 + " -r " 

189 + str(current_run_is + 1) 

190 + " -RA " 

191 + str(ra_dec[2]) 

192 + " -DEC " 

193 + str(ra_dec[3]) 

194 ) 

195 

196 loop_id = loop_id + 1 

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

198 quit = True 

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

200 

201 

202if __name__ == "__main__": 

203 main()