Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# (c) Stefan Countryman, August 2017 

2 

3""" 

4FileHandler for a JSON file holding data about an LVAlert. Generated when an 

5LVAlert comes in. 

6""" 

7 

8from llama.files.lvc_skymap.utils import SkymapFilename 

9from llama.filehandler import EventTriggeredFileHandler, JSONFile 

10from llama.files.lvc_skymap.utils import ( 

11 skymap_filenames, 

12 available_skymaps, 

13) 

14 

15 

16@JSONFile.set_class_attributes 

17class LVAlertJSON(EventTriggeredFileHandler, JSONFile): 

18 """An LVAlert JSON object containing skymap info.""" 

19 

20 FILENAME = 'lvalert.json' 

21 

22 def _generate(self, payload): # pylint: disable=arguments-differ 

23 """Take the payload JSON from the LVAlert message and save it to 

24 file. The payload should NOT be a dictionary; it should just be 

25 the pure JSON received from the LVAlert.""" 

26 with open(self.fullpath, "w") as outfile: 

27 outfile.write(payload) 

28 

29 @property 

30 def graceid(self): 

31 """The GraceID, a unique ID used on GraceDB to register gravitational 

32 wave triggers.""" 

33 return self.read_json()['uid'] 

34 

35 @property 

36 def notice_time_str(self): 

37 """Get the time at which the LVAlert was created.""" 

38 return self.read_json()['object']['created'] 

39 

40 # @property 

41 # def gpstime(self): 

42 # """Get the GPS time of this reconstructed event.""" 

43 # return self.read_json()['object']['gpstime'] 

44 

45 # @property 

46 # def pipeline(self): 

47 # """Get the name of the pipeline that generated this event.""" 

48 # return self.read_json()['object']['pipeline'] 

49 

50 @property 

51 def far(self): 

52 """The False Alarm Rate of this trigger.""" 

53 return self.read_json()['object']['far'] 

54 

55 @property 

56 def skymap_filename(self): 

57 """Get the exact filename, including version information, for this 

58 file. If this LVAlert does not correspond to a skymap upload, try to 

59 pull down the most recently generated skymap. 

60 

61 Returns 

62 ------- 

63 filename : files.lvc_skymap.utils.SkymapFilename 

64 The full canonical filename as a ``SkymapFilename`` instance. 

65 """ 

66 # check whether this lvalert described a new skymap; if it did, use 

67 # that. 

68 if (self.alert_type == 'log' 

69 and skymap_filenames([self.base_filename])): 

70 return SkymapFilename(f'{self.base_filename},' 

71 f'{self.skymap_version}') 

72 # otherwise, try to parse the skymap filename from gracedb. pick the 

73 # most recent one. 

74 skymap = available_skymaps(self.graceid)[-1] 

75 return SkymapFilename(f'{skymap["filename"]},' 

76 f'{skymap["file_version"]}') 

77 

78 @property 

79 def alert_type(self): 

80 """Get the alert_type of this LVAlert.""" 

81 return self.read_json()["alert_type"] 

82 

83 @property 

84 def base_filename(self): 

85 """Get the unversioned filename for this file. This only works for 

86 LVAlerts corresponding to uploads. 

87 

88 Returns 

89 ------- 

90 base_filename : files.lvc_skymap.utils.SkymapFilename 

91 The skymap filename as a ``SkymapFilename`` instance. 

92 

93 Raises 

94 ------ 

95 KeyError 

96 If this LVAlert does not contain a filename. 

97 """ 

98 return self.read_json()['data']['filename'] 

99 

100 @property 

101 def skymap_version(self): 

102 """Get the version of the skymap with this particular filename. This 

103 only works for LVAlerts corresponding to uploads. 

104 

105 Returns 

106 ------- 

107 version : int 

108 File version corresponding to this upload. 

109 """ 

110 return self.read_json()['data']['file_version']