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

46 statements  

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

1#!/usr/bin/env python 

2# S. Caroff 

3 

4""" 

5ONLINE analysis: 

6Produce online alt az from RA DEC of the pointing position 

7 

8Usage: 

9$> online_alt_az_dl1_maker 

10 --input-file "./dl1_5202_0_10000evt.h5" 

11 --RA 83.254 

12 --DEC 102.35 

13""" 

14 

15import argparse 

16import time 

17 

18import astropy.units as u 

19import numpy as np 

20import tables 

21from astropy.coordinates import AltAz, EarthLocation, SkyCoord 

22from astropy.time import Time 

23from astropy.utils import iers 

24 

25iers.conf.auto_download = False 

26 

27 

28def main(): 

29 parser = argparse.ArgumentParser(description="Offline AltAz DL1 maker") 

30 

31 # Required arguments 

32 parser.add_argument( 

33 "--input-file", 

34 "-f", 

35 type=str, 

36 dest="input_file", 

37 help='DL1 file, example : "dl1_5202_0_10000evt.h5"', 

38 default=None, 

39 required=True, 

40 ) 

41 

42 parser.add_argument( 

43 "--RA", type=float, dest="RA", help="Right Ascension of the telescope", default=None, required=True 

44 ) 

45 

46 parser.add_argument( 

47 "--DEC", type=float, dest="DEC", help="Declination of the telescope", default=None, required=True 

48 ) 

49 

50 parser.add_argument( 

51 "--nbins", 

52 type=int, 

53 dest="nbins", 

54 help="number of bins for the interpolation of alt az", 

55 default=10, 

56 required=False, 

57 ) 

58 parser.add_argument( 

59 "--nb_tries", 

60 type=int, 

61 dest="nb_tries", 

62 help="Number of times the software will attempt to open the input file", 

63 default=20, 

64 required=False, 

65 ) 

66 parser.add_argument( 

67 "--timeout", 

68 type=float, 

69 dest="open_timeout", 

70 help="Time to wait between input file opening attempts", 

71 default=0.5, 

72 required=False, 

73 ) 

74 

75 args = parser.parse_args() 

76 

77 input_file = args.input_file # "dl3_LST-1.Run04190.*.fits" 

78 obs_location = EarthLocation.from_geodetic(-17.891485 * u.deg, 28.761518 * u.deg, 2187 * u.m) 

79 

80 # Failing to open a file on fefs doesn't necessarily mean we won't succeed next time ! 

81 for _ in range(args.nb_tries - 1): 

82 try: 

83 hfile = tables.open_file(input_file, mode="r+") 

84 break 

85 except Exception: 

86 print("Failed to open input file {}, re-trying in {}".format(args.input_file, args.open_timeout)) 

87 time.sleep(args.open_timeout) 

88 else: 

89 # try 1 last time, let tables error raise if failing again 

90 hfile = tables.open_file(input_file, mode="r+") 

91 

92 Trigger_time = hfile.root.dl1.event.telescope.parameters.tel_001.col("trigger_time") 

93 is_good_event_table = hfile.root.dl1.event.telescope.parameters.tel_001.col("is_good_event") 

94 selection_mask = np.logical_and(Trigger_time < 4000000000.0, is_good_event_table > 0.5) 

95 is_good_event_table = np.multiply(selection_mask, 1) 

96 

97 time_table = np.linspace(Trigger_time[selection_mask][0], Trigger_time[selection_mask][-1], args.nbins) 

98 

99 pointing_coord = SkyCoord(ra=args.RA, dec=args.DEC, unit=u.deg) 

100 event_altaz = pointing_coord.transform_to( 

101 AltAz( 

102 obstime=Time(time_table, format="unix"), 

103 location=obs_location, 

104 obswl=0.35 * u.micron, 

105 relative_humidity=0.5, 

106 temperature=10 * u.deg_C, 

107 pressure=790 * u.hPa, 

108 ) 

109 ) 

110 

111 alt_table = np.interp(Trigger_time, time_table, event_altaz.alt.rad) 

112 az_table = np.interp(Trigger_time, time_table, event_altaz.az.rad) 

113 

114 table = hfile.root.dl1.event.telescope.parameters.tel_001 

115 table.modify_column(start=0, stop=len(alt_table), step=1, column=alt_table, colname="alt_tel") 

116 table.modify_column(start=0, stop=len(az_table), step=1, column=az_table, colname="az_tel") 

117 table.modify_column( 

118 start=0, stop=len(is_good_event_table), step=1, column=is_good_event_table, colname="is_good_event" 

119 ) 

120 

121 table.flush() 

122 

123 hfile.close() 

124 

125 

126if __name__ == "__main__": 

127 main()