Coverage for hiperta_stream/scripts/rta_dl1_to_dl2.py: 43%

35 statements  

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

1#!/usr/bin/env python 

2# E. Garcia 

3 

4import os 

5import tables 

6import argparse 

7import subprocess as sp 

8from hiperta_stream.scripts.reorganizer_dl1hiperta300_stream_to_dl1lstchain063 import \ 

9 reorganizer_dl1hiperta_stream_to_dl1lstchain063 as hiperta_lstchain_reorganzier 

10 

11 

12def apply_is_good_event(hfile): 

13 """ 

14 Remove rows from parameters and images tables that corresponds to events that haven't passed the dl1 cuts, 

15 i.e., `is_good_event == 0`. 

16 

17 :param hfile: str reorganized (lstchain v06 like hdf5 file) 

18 """ 

19 

20 hf = tables.open_file(hfile, 'a') 

21 params_table = hf.root.dl1.event.telescope.parameters.LST_LSTCam 

22 images_table = hf.root.dl1.event.telescope.image.LST_LSTCam 

23 

24 for row in params_table: 

25 if row['is_good_event'] == 0: 

26 params_table.remove_row(row.nrow) 

27 images_table.remove_row(row.nrow) 

28 

29 hf.close() 

30 

31 

32def dl1_rearranging(input_filename): 

33 """ 

34 Reorganize dl1_rta file so that is lstchain compliant (lstchain_v0.6.X to date Dec 2020) 

35 

36 :param input_filename: [str] path to dl1_rta file 

37 :return: [str] filename of reorganized dl1_rta file 

38 """ 

39 

40 # We need to reorganize (hiperta --> lstchain) the dl1 file first 

41 input_reorganizer_ctapipe08 = input_filename 

42 output_reorganizer_lstchain06 = input_filename.replace('dl1_', 'dl1_aligned_ctapipe08_') 

43 

44 hiperta_lstchain_reorganzier(input_reorganizer_ctapipe08, output_reorganizer_lstchain06) 

45 # TODO erase and rename dl1_hiperta ? 

46 os.remove(input_reorganizer_ctapipe08) 

47 os.rename(output_reorganizer_lstchain06, input_filename) 

48 

49 apply_is_good_event(input_filename) 

50 

51 return input_filename 

52 

53 

54def main(): 

55 """ 

56 rta_dl1_dl2 stage 

57 """ 

58 parser = argparse.ArgumentParser(description="hiperta_stream rta stage DL1 to DL2") 

59 

60 # Required arguments 

61 parser.add_argument('--input-file', '-f', type=str, 

62 dest='input_file', 

63 help='path to a DL1 HDF5 file', 

64 default=None, required=True) 

65 

66 parser.add_argument('--path-models', '-p', action='store', type=str, 

67 dest='path_models', 

68 help='Path where to find the trained RF', 

69 default='./trained_models') 

70 # Optional arguments 

71 parser.add_argument('--output-dir', '-o', action='store', type=str, 

72 dest='output_dir', 

73 help='Path where to store the reco dl2 events', 

74 default='./dl2_data') 

75 

76 parser.add_argument('--config', '-c', action='store', type=str, 

77 dest='config_file', 

78 help='Path to a configuration file. If none is given, a standard configuration is applied', 

79 default='../config/lstchain_standard_config_v063-RTA.json', 

80 required=False) 

81 

82 args = parser.parse_args() 

83 

84 dl1_rta_reorganized = dl1_rearranging(args.input_file) 

85 print("Reorganization done correctly !") 

86 

87 cmd = [f"lstchain_dl1_to_dl2", 

88 "-f", f"{dl1_rta_reorganized}", 

89 "-p", f"{args.path_models}", 

90 "-o", f"{args.output_dir}", 

91 "-c", f"{args.config_file}"] 

92 

93 sp.run(cmd) 

94 

95 

96if __name__ == '__main__': 

97 main()