Coverage for src / lstautorta / High_Level_analysis_Maps.py: 0%

76 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-10 11:56 +0000

1#!/usr/bin/env python 

2 

3import datetime 

4import logging 

5import os 

6import time 

7 

8from lstautorta.shared_observation import DEFAULT_SHARED_OBS_PATH, wait_for_shared_obs 

9 

10 

11def wait_for_directory(directory, interval=5): 

12 """ 

13 Wait for the directory to exist 

14 

15 :param directory: directory path 

16 :param interval: Time interval between verification 

17 """ 

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

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

20 time.sleep(interval) 

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

22 

23 

24def now(): 

25 """ 

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

27 

28 :return: float 

29 """ 

30 return datetime.datetime.timestamp(datetime.datetime.now()) 

31 

32 

33def get_current_obs_info( 

34 db_hostname: str = "lst101", 

35 obs_target_query_timeout_s: int = 10, 

36): 

37 """ 

38 Retrieve current observation info from the shared observation file. 

39 Waits until the file is available. 

40 """ 

41 logging.info("Waiting for shared observation file at %s", DEFAULT_SHARED_OBS_PATH) 

42 return wait_for_shared_obs(DEFAULT_SHARED_OBS_PATH) 

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 get_source_ra_dec(obs_info): 

69 if obs_info is None: 

70 return 0, 0 

71 if obs_info.source_RA is None or obs_info.source_DEC is None: 

72 return 0, 0 

73 return obs_info.source_RA, obs_info.source_DEC 

74 

75 

76def today_to_directory(today): 

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

78 

79 

80def main(): 

81 """ 

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

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

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

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

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

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

88 """ 

89 current_run_is = -1 

90 quit = False 

91 

92 Start_time = now() 

93 

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

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

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

97 logging.basicConfig( 

98 filename="/fefs/onsite/pipeline/rta/data/" 

99 + today_to_directory(today) 

100 + "/log_High_Level_Maps_" 

101 + today 

102 + ".txt", 

103 level=logging.INFO, 

104 ) 

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

106 

107 RTA_ready = True 

108 

109 loop_id = 0 

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

111 while quit == False and RTA_ready: 

112 time.sleep(10) 

113 obs_info = get_current_obs_info() 

114 if obs_info is None or obs_info.obs_id is None: 

115 logging.info("None") 

116 current_run_is = -1 

117 continue 

118 if current_run_is == -1: 

119 current_run_is = obs_info.obs_id 

120 if current_run_is != obs_info.obs_id: 

121 time.sleep(1) 

122 obs_info = get_current_obs_info() 

123 if obs_info is None or obs_info.obs_id is None: 

124 logging.info("None") 

125 current_run_is = -1 

126 continue 

127 current_run_is = obs_info.obs_id 

128 if current_run_is != -1: 

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

130 time.sleep(2) 

131 source_ra, source_dec = get_source_ra_dec(obs_info) 

132 logging.info( 

133 # ~ "srun --reservation=rta_one_node ./Ring_Background_Maps.py -da " 

134 "./Ring_Background_Maps.py -da " 

135 + today_to_directory(today) 

136 + " -r " 

137 + str(current_run_is) 

138 + " -RA " 

139 + str(source_ra) 

140 + " -DEC " 

141 + str(source_dec) 

142 ) 

143 os.system( 

144 # ~ "srun --reservation=rta_one_node ./Ring_Background_Maps.py -da " 

145 "./Ring_Background_Maps.py -da " 

146 + today_to_directory(today) 

147 + " -r " 

148 + str(current_run_is) 

149 + " -RA " 

150 + str(source_ra) 

151 + " -DEC " 

152 + str(source_dec) 

153 ) 

154 

155 if loop_id % 1 == 0: 

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

157 time.sleep(2) 

158 logging.info(datetime.datetime.now()) 

159 source_ra, source_dec = get_source_ra_dec(obs_info) 

160 logging.info( 

161 # ~ "srun --reservation=rta_one_node ./Ring_Background_Maps.py -da " 

162 "./Ring_Background_Maps.py -da " 

163 + today_to_directory(today) 

164 + " -r " 

165 + str(current_run_is) 

166 + " -RA " 

167 + str(source_ra) 

168 + " -DEC " 

169 + str(source_dec) 

170 ) 

171 os.system( 

172 # ~ "srun --reservation=rta_one_node ./Ring_Background_Maps.py -da " 

173 "./Ring_Background_Maps.py -da " 

174 + today_to_directory(today) 

175 + " -r " 

176 + str(current_run_is) 

177 + " -RA " 

178 + str(source_ra) 

179 + " -DEC " 

180 + str(source_dec) 

181 ) 

182 

183 loop_id = loop_id + 1 

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

185 quit = True 

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

187 

188 

189if __name__ == "__main__": 

190 main()