Coverage for hiperta_stream/scripts/create_all_stream_config_files.py: 0%

53 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-07-16 10:16 +0000

1#!/usr/bin/env python 

2 

3import argparse 

4import datetime 

5from pathlib import Path 

6import subprocess as sp 

7import hiperta_stream.rta_tree_structure.rta_tree_path_structure as rta_path 

8from hiperta_stream.plots import create_r0_calib_plots 

9from hiperta_stream.config import create_drs4, create_calibration, find_camera_pix_order, create_stream_base_structure 

10 

11 

12def run_load_astropy(): 

13 """ 

14 Runs `` entry point - that is needed once a day to avoid astropy downloading data every time dl2_to_dl3 

15 stage is run. 

16 """ 

17 cmd = sp.run(['rta_load_astropy']) 

18 if cmd.returncode == 0: 

19 print(f'\n====== rta_load_astropy entry point successfully run.') 

20 else: 

21 print(' ** !! rta_load_astropy entry point was not run successfully. Please try manually.\n') 

22 

23 

24def check_files(filename, prefix=''): 

25 """ 

26 Check if filename is_file() 

27 

28 :param filename: Path object 

29 Path to filename to be checked 

30 :param prefix: str 

31 Prefix to indicate the kind of file passed. 

32 """ 

33 if not filename.is_file(): 

34 raise FileNotFoundError(f'File {filename.absolute().as_posix()} does not exist. Exiting') 

35 else: 

36 if prefix == 'lstchain_config': 

37 print(f' * {prefix} file: {filename.absolute().as_posix()}') 

38 else: 

39 print(f' * {prefix} run file:\t{filename.absolute().as_posix()}') 

40 

41 

42def check_timestamp_is_correct(timestamp): 

43 """ 

44 Checks that the provided timestamp is in the correct format YYYYYMMDD, 

45 needed for the creation of the rta path tree and all the rest of sub directories. 

46 

47 :param timestamp: str 

48 String with night timestamp 

49 """ 

50 try: 

51 datetime.datetime.strptime(timestamp, '%Y%m%d') 

52 except ValueError: 

53 raise ValueError("Please provide a valid timestamp following the YYYYMMDD format. (Year Month Day)") 

54 

55 

56def main(): 

57 """ 

58 Creates all the needed (config) files to run the RTA. 

59 

60 It performs the following stages; 

61 1. Create the LST calibration file from the drs4 and the pedestal calibration runs. 

62 - lstchain_data_create_drs4_pedestal_file 

63 - lstchain_create_calibration_file 

64 2. Retrieves the pixel order of the camera 

65 - rta_get_zfits_pix_order 

66 3. Creates the hdf5 base structure file 

67 - rta_create_base_config 

68 4. Creates R0 check calibration plots 

69 - rta_plot_r0_calib 

70 """ 

71 parser = argparse.ArgumentParser(description="Creates all the configuration files needed to run the RTA within " 

72 "the /fefs/onsite/pipeline/rta/$YYYYMMDD/calibration directory") 

73 

74 parser.add_argument('--timestamp_night', '-t', type=str, 

75 dest='night_timestamp', 

76 help='Night timestamp. Follows the format at LP cluster (YYYYMMDD) , e.g., `20210310`.', 

77 default=None, required=True) 

78 

79 parser.add_argument('--run_drs4', '--rd', action='store', type=int, 

80 dest='run_drs4', 

81 help='LST drs4 run number', 

82 default=None, required=True) 

83 

84 parser.add_argument('--run_calibration', '--rc', action='store', type=int, 

85 dest='run_calibration', 

86 help='LST pedestal calibration run number', 

87 default=None, required=True) 

88 

89 parser.add_argument('--config_lstchain', '-c', action='store', type=str, 

90 dest='config_lstchain', 

91 help='Path to a lstchain configuration file.', 

92 default=None 

93 ) 

94 

95 args = parser.parse_args() 

96 

97 # 1 Create RTA path tree 

98 timestamp_night_dir = args.night_timestamp 

99 check_timestamp_is_correct(timestamp_night_dir) 

100 night_dir = rta_path.create_observation_night_path_structure(rta_path.rta_common_path, 

101 timestamp_night_dir) 

102 

103 # Output directory 

104 output_dir = Path(night_dir, 'calibration') 

105 output_dir.mkdir(exist_ok=True) 

106 

107 real_data_dir = Path(f'/fefs/onsite/data/{args.night_timestamp}') 

108 drs4_run = Path(real_data_dir, f'LST-1.1.Run{args.run_drs4:05d}.0000.fits.fz') 

109 calibration_run = Path(real_data_dir, f'LST-1.1.Run{args.run_calibration:05d}.0000.fits.fz') 

110 if args.config_lstchain is None: 

111 lstchain_config = Path(Path(__file__).parents[1], 'config', 'lstchain_standard_config_v063-RTA.json') 

112 else: 

113 lstchain_config = Path(args.config_lstchain).absolute() 

114 

115 # Check that all files exist 

116 print(f'\n====== Start creation of RTA stream configuration files') 

117 check_files(drs4_run, prefix='drs4') 

118 check_files(calibration_run, prefix='ped-cal') 

119 check_files(lstchain_config, prefix='lstchain_config') 

120 

121 # Create drs4 file 

122 drs4_file = create_drs4(drs4_run, output_dir) 

123 

124 # Create calibration file 

125 calib_file = create_calibration(drs4_file, calibration_run, lstchain_config, output_dir) 

126 

127 # Create binary file containing the pixel order of the camera 

128 pix_order_bin = find_camera_pix_order(drs4_run, output_dir) 

129 

130 # Finally create the base structure hdf5 configuration file 

131 base_hdf5_file = create_stream_base_structure(pix_order_bin, calib_file, output_dir) 

132 

133 # Create r0 calib plots from base structure 

134 create_r0_calib_plots(base_hdf5_file, output_dir) 

135 

136 # Run load astropy cache script 

137 run_load_astropy() 

138 

139 

140if __name__ == '__main__': 

141 main()