Add gti union
@@ -135,3 +135,40 @@ class GTI:
)
return self.__class__(gti_within)
+
+ def union(self, gti):
+ """Performs union of two GTIs tables.
+
+ Parameters
+ ----------
+ gti : `~gammapy.data.GTI`
+ Copy of the GTI table with selection applied.
+
+ Returns
+ -------
+ union : `~gammapy.data.GTI`
+ The merged GTI table
+ """
+ for time_interval in zip(gti.time_start, gti.time_stop):
+ # Find GTIs that overlap with time interval
+ mask = self.time_start < time_interval[1]
+ mask &= self.time_stop > time_interval[0]
+
+ # If there is an overlap determine the new interval, otherwise add a new one
+ if not np.all(mask == False):
+ new_min = np.minimum(np.min(self.time_start[mask]), time_interval[0])
+ new_max = np.maximum(np.max(self.time_stop[mask]), time_interval[1])
+ else:
+ new_min = time_interval[0]
+ new_max = time_interval[1]
+
+ start_met = time_relative_to_ref(new_min, self.table.meta)
+ stop_met = time_relative_to_ref(new_max, self.table.meta)
+
+ new_tab = self.table[~mask]
+ new_tab.add_row([start_met.value, stop_met.value])
+
+ new_tab.sort('START')
+ return self.__class__(new_tab)
+
+
