From 4d6480d405e75b296864fe531a0f0be020617813 Mon Sep 17 00:00:00 2001
From: Greg Ashton <gregory.ashton@ligo.org>
Date: Thu, 13 Jan 2022 14:42:36 +0000
Subject: [PATCH] Replace get event time with GWOSC functionality

---
 bilby/gw/utils.py     | 55 +++++++++++++------------------------------
 test/gw/utils_test.py | 12 ++--------
 2 files changed, 18 insertions(+), 49 deletions(-)

diff --git a/bilby/gw/utils.py b/bilby/gw/utils.py
index a52bfd663..c975f915a 100644
--- a/bilby/gw/utils.py
+++ b/bilby/gw/utils.py
@@ -387,54 +387,31 @@ def zenith_azimuth_to_ra_dec(zenith, azimuth, geocent_time, ifos):
 
 def get_event_time(event):
     """
-    Get the merger time for known GW events.
-
-    See https://www.gw-openscience.org/catalog/GWTC-1-confident/html/
-    Last update https://arxiv.org/abs/1811.12907:
-
-    - GW150914
-    - GW151012
-    - GW151226
-    - GW170104
-    - GW170608
-    - GW170729
-    - GW170809
-    - GW170814
-    - GW170817
-    - GW170818
-    - GW170823
+    Get the merger time for known GW events using the gwosc package
 
     Parameters
-    ==========
+    ----------
     event: str
-        Event descriptor, this can deal with some prefixes, e.g.,
-        '151012', 'GW151012', 'LVT151012'
+        Event name, e.g. GW150914
 
     Returns
-    =======
+    -------
     event_time: float
         Merger time
-    """
-    event_times = {'150914': 1126259462.4,
-                   '151012': 1128678900.4,
-                   '151226': 1135136350.6,
-                   '170104': 1167559936.6,
-                   '170608': 1180922494.5,
-                   '170729': 1185389807.3,
-                   '170809': 1186302519.8,
-                   '170814': 1186741861.5,
-                   '170817': 1187008882.4,
-                   '170818': 1187058327.1,
-                   '170823': 1187529256.5}
-    if 'GW' or 'LVT' in event:
-        event = event[-6:]
 
+    Raises
+    ------
+    ImportError
+        If the gwosc package is not installed
+    ValueError
+        If the event is not in the gwosc dataset
+    """
     try:
-        event_time = event_times[event[-6:]]
-        return event_time
-    except KeyError:
-        print('Unknown event {}.'.format(event))
-        return None
+        from gwosc import datasets
+    except ImportError:
+        raise ImportError("You do not have the gwosc package installed")
+
+    return datasets.event_gps(event)
 
 
 def get_open_strain_data(
diff --git a/test/gw/utils_test.py b/test/gw/utils_test.py
index a3a7582f1..d8c3286ac 100644
--- a/test/gw/utils_test.py
+++ b/test/gw/utils_test.py
@@ -103,21 +103,13 @@ class TestGWUtils(unittest.TestCase):
     def test_get_event_time(self):
         events = [
             "GW150914",
-            "GW151012",
-            "GW151226",
             "GW170104",
-            "GW170608",
-            "GW170729",
-            "GW170809",
-            "GW170814",
-            "GW170817",
-            "GW170818",
-            "GW170823",
         ]
         for event in events:
             self.assertTrue(isinstance(gwutils.get_event_time(event), float))
 
-        self.assertTrue(gwutils.get_event_time("GW010290") is None)
+        with self.assertRaises(ValueError):
+            gwutils.get_event_time("GW010290")
 
     def test_read_frame_file(self):
         start_time = 0
-- 
GitLab